1284990Scy#include "config.h" 2284990Scy 3284990Scy#include "sntptest.h" 4284990Scy#include "fileHandlingTest.h" 5284990Scy#include "main.h" 6284990Scy#include "utilities.h" 7284990Scy 8284990Scy#include "unity.h" 9284990Scy 10289997Sglebius#include <math.h> 11289997Sglebius 12289997Sglebiussockaddr_u CreateSockaddr4(const char* address); 13289997Sglebiusstruct addrinfo CreateAddrinfo(sockaddr_u* sock); 14289997Sglebiusvoid InitDebugTest(const char * filename); 15289997Sglebiusvoid FinishDebugTest(const char * expected,const char * actual); 16289997Sglebiusvoid test_IPv4Address(void); 17289997Sglebiusvoid test_IPv6Address(void); 18289997Sglebiusvoid test_SetLiVnMode1(void); 19289997Sglebiusvoid test_SetLiVnMode2(void); 20289997Sglebiusvoid test_PktOutput(void); 21289997Sglebiusvoid test_LfpOutputBinaryFormat(void); 22289997Sglebiusvoid test_LfpOutputDecimalFormat(void); 23289997Sglebius 24289997Sglebius 25284990Scyconst char * Version = "stub unit test Version string"; 26284990Scy 27284990Scy 28289997Sglebiussockaddr_u 29289997SglebiusCreateSockaddr4(const char* address) { 30284990Scy sockaddr_u s; 31284990Scy s.sa4.sin_family = AF_INET; 32284990Scy s.sa4.sin_addr.s_addr = inet_addr(address); 33284990Scy SET_PORT(&s, 123); 34284990Scy 35284990Scy return s; 36284990Scy} 37284990Scy 38289997Sglebius 39289997Sglebiusstruct addrinfo 40289997SglebiusCreateAddrinfo(sockaddr_u* sock) { 41284990Scy struct addrinfo a; 42284990Scy a.ai_family = sock->sa.sa_family; 43284990Scy a.ai_addrlen = SIZEOF_SOCKADDR(a.ai_family); 44284990Scy a.ai_addr = &sock->sa; 45284990Scy return a; 46284990Scy} 47284990Scy 48284990Scy 49284990Scybool outputFileOpened; 50284990ScyFILE* outputFile; 51284990Scy 52284990Scy 53289997Sglebiusvoid 54289997SglebiusInitDebugTest(const char * filename) { 55284990Scy // Clear the contents of the current file. 56284990Scy // Open the output file 57284990Scy outputFile = fopen(filename, "w+"); 58289997Sglebius TEST_ASSERT_NOT_NULL(outputFile); 59284990Scy outputFileOpened = true; 60284990Scy} 61284990Scy 62289997Sglebius 63284990Scy// Closes outputFile, and compare contents. 64289997Sglebiusvoid 65289997SglebiusFinishDebugTest(const char * expected, 66284990Scy const char * actual) { 67284990Scy if (outputFileOpened) 68284990Scy fclose(outputFile); 69284990Scy 70284990Scy FILE * e = fopen(expected,"rb"); 71284990Scy FILE * a = fopen(actual,"rb"); 72289997Sglebius TEST_ASSERT_NOT_NULL(e); 73289997Sglebius TEST_ASSERT_NOT_NULL(a); 74284990Scy 75284990Scy CompareFileContent(e, a); 76284990Scy} 77284990Scy 78284990Scy 79284990Scy/* 80284990Scy * These tests are essentially a copy of the tests for socktoa() 81284990Scy * in libntp. If sntp switches to using that functions, these 82284990Scy * tests can be removed. 83284990Scy */ 84284990Scy 85289997Sglebiusvoid 86289997Sglebiustest_IPv4Address(void) { 87284990Scy const char* ADDR = "192.0.2.10"; 88284990Scy 89284990Scy sockaddr_u input = CreateSockaddr4(ADDR); 90284990Scy struct addrinfo inputA = CreateAddrinfo(&input); 91284990Scy 92284990Scy TEST_ASSERT_EQUAL_STRING(ADDR, ss_to_str(&input)); 93284990Scy TEST_ASSERT_EQUAL_STRING(ADDR, addrinfo_to_str(&inputA)); 94284990Scy} 95284990Scy 96289997Sglebius 97289997Sglebiusvoid 98289997Sglebiustest_IPv6Address(void) { 99293650Sglebius const struct in6_addr address = { { { 100284990Scy 0x20, 0x01, 0x0d, 0xb8, 101284990Scy 0x85, 0xa3, 0x08, 0xd3, 102284990Scy 0x13, 0x19, 0x8a, 0x2e, 103284990Scy 0x03, 0x70, 0x73, 0x34 104293650Sglebius } } }; 105284990Scy const char * expected = "2001:db8:85a3:8d3:1319:8a2e:370:7334"; 106284990Scy sockaddr_u input; 107284990Scy struct addrinfo inputA; 108284990Scy 109284990Scy memset(&input, 0, sizeof(input)); 110284990Scy input.sa6.sin6_family = AF_INET6; 111284990Scy input.sa6.sin6_addr = address; 112284990Scy TEST_ASSERT_EQUAL_STRING(expected, ss_to_str(&input)); 113284990Scy 114284990Scy inputA = CreateAddrinfo(&input); 115284990Scy TEST_ASSERT_EQUAL_STRING(expected, addrinfo_to_str(&inputA)); 116284990Scy} 117284990Scy 118289997Sglebius 119289997Sglebiusvoid 120289997Sglebiustest_SetLiVnMode1(void) { 121284990Scy struct pkt expected; 122284990Scy expected.li_vn_mode = PKT_LI_VN_MODE(LEAP_NOWARNING, 123284990Scy NTP_VERSION, 124284990Scy MODE_SERVER); 125284990Scy 126284990Scy struct pkt actual; 127284990Scy set_li_vn_mode(&actual, LEAP_NOWARNING, NTP_VERSION, 128284990Scy MODE_SERVER); 129284990Scy 130284990Scy TEST_ASSERT_EQUAL(expected.li_vn_mode, actual.li_vn_mode); 131284990Scy} 132284990Scy 133289997Sglebius 134289997Sglebiusvoid 135289997Sglebiustest_SetLiVnMode2(void) { 136284990Scy struct pkt expected; 137284990Scy expected.li_vn_mode = PKT_LI_VN_MODE(LEAP_NOTINSYNC, 138284990Scy NTP_OLDVERSION, 139284990Scy MODE_BROADCAST); 140284990Scy 141284990Scy struct pkt actual; 142284990Scy set_li_vn_mode(&actual, LEAP_NOTINSYNC, NTP_OLDVERSION, 143284990Scy MODE_BROADCAST); 144284990Scy 145284990Scy TEST_ASSERT_EQUAL(expected.li_vn_mode, actual.li_vn_mode); 146284990Scy} 147284990Scy 148284990Scy/* Debug utilities tests */ 149284990Scy 150289997Sglebiusvoid 151289997Sglebiustest_PktOutput(void) { 152289997Sglebius char * filename = "debug-output-pkt"; 153284990Scy InitDebugTest(filename); 154284990Scy 155284990Scy struct pkt testpkt; 156284990Scy memset(&testpkt, 0, sizeof(struct pkt)); 157284990Scy testpkt.li_vn_mode = PKT_LI_VN_MODE(LEAP_NOWARNING, 158284990Scy NTP_VERSION, 159284990Scy MODE_SERVER); 160284990Scy 161284990Scy l_fp test; 162284990Scy test.l_ui = 8; 163284990Scy test.l_uf = 2147483647; // Lots of ones. 164284990Scy HTONL_FP(&test, &testpkt.xmt); 165284990Scy 166284990Scy pkt_output(&testpkt, LEN_PKT_NOMAC, outputFile); 167284990Scy 168284990Scy FinishDebugTest(CreatePath("debug-input-pkt", INPUT_DIR), filename); 169284990Scy} 170284990Scy 171289997Sglebius 172289997Sglebiusvoid 173289997Sglebiustest_LfpOutputBinaryFormat(void) { 174284990Scy char * filename = "debug-output-lfp-bin";//CreatePath("debug-output-lfp-bin", OUTPUT_DIR); 175284990Scy InitDebugTest(filename); 176284990Scy 177284990Scy l_fp test; 178284990Scy test.l_ui = 63; // 00000000 00000000 00000000 00111111 179284990Scy test.l_uf = 127; // 00000000 00000000 00000000 01111111 180284990Scy 181284990Scy l_fp network; 182284990Scy HTONL_FP(&test, &network); 183284990Scy 184284990Scy l_fp_output_bin(&network, outputFile); 185284990Scy 186284990Scy FinishDebugTest(CreatePath("debug-input-lfp-bin", INPUT_DIR), filename); 187284990Scy} 188284990Scy 189289997Sglebius 190289997Sglebiusvoid 191289997Sglebiustest_LfpOutputDecimalFormat(void) { 192289997Sglebius char * filename = "debug-output-lfp-dec"; 193284990Scy InitDebugTest(filename); 194284990Scy 195284990Scy l_fp test; 196284990Scy test.l_ui = 6310; // 0x000018A6 197284990Scy test.l_uf = 308502; // 0x00004B516 198284990Scy 199284990Scy l_fp network; 200284990Scy HTONL_FP(&test, &network); 201284990Scy 202284990Scy l_fp_output_dec(&network, outputFile); 203284990Scy 204284990Scy FinishDebugTest(CreatePath("debug-input-lfp-dec", INPUT_DIR), filename); 205284990Scy} 206