1 #include "config.h" 2 3 #include "sntptest.h" 4 #include "fileHandlingTest.h" 5 #include "main.h" 6 #include "utilities.h" 7 8 #include "unity.h" 9 10 #include <math.h> 11 12 sockaddr_u CreateSockaddr4(const char* address); 13 struct addrinfo CreateAddrinfo(sockaddr_u* sock); 14 void InitDebugTest(const char * filename); 15 void FinishDebugTest(const char * expected,const char * actual); 16 void test_IPv4Address(void); 17 void test_IPv6Address(void); 18 void test_SetLiVnMode1(void); 19 void test_SetLiVnMode2(void); 20 void test_PktOutput(void); 21 void test_LfpOutputBinaryFormat(void); 22 void test_LfpOutputDecimalFormat(void); 23 24 25 const char * Version = "stub unit test Version string"; 26 27 28 sockaddr_u 29 CreateSockaddr4(const char* address) { 30 sockaddr_u s; 31 s.sa4.sin_family = AF_INET; 32 s.sa4.sin_addr.s_addr = inet_addr(address); 33 SET_PORT(&s, 123); 34 35 return s; 36 } 37 38 39 struct addrinfo 40 CreateAddrinfo(sockaddr_u* sock) { 41 struct addrinfo a; 42 a.ai_family = sock->sa.sa_family; 43 a.ai_addrlen = SIZEOF_SOCKADDR(a.ai_family); 44 a.ai_addr = &sock->sa; 45 return a; 46 } 47 48 49 bool outputFileOpened; 50 FILE* outputFile; 51 52 53 void 54 InitDebugTest(const char * filename) { 55 // Clear the contents of the current file. 56 // Open the output file 57 outputFile = fopen(filename, "w+"); 58 TEST_ASSERT_NOT_NULL(outputFile); 59 outputFileOpened = true; 60 } 61 62 63 // Closes outputFile, and compare contents. 64 void 65 FinishDebugTest(const char * expected, 66 const char * actual) { 67 if (outputFileOpened) 68 fclose(outputFile); 69 70 FILE * e = fopen(expected,"rb"); 71 FILE * a = fopen(actual,"rb"); 72 TEST_ASSERT_NOT_NULL(e); 73 TEST_ASSERT_NOT_NULL(a); 74 75 CompareFileContent(e, a); 76 } 77 78 79 /* 80 * These tests are essentially a copy of the tests for socktoa() 81 * in libntp. If sntp switches to using that functions, these 82 * tests can be removed. 83 */ 84 85 void 86 test_IPv4Address(void) { 87 const char* ADDR = "192.0.2.10"; 88 89 sockaddr_u input = CreateSockaddr4(ADDR); 90 struct addrinfo inputA = CreateAddrinfo(&input); 91 92 TEST_ASSERT_EQUAL_STRING(ADDR, ss_to_str(&input)); 93 TEST_ASSERT_EQUAL_STRING(ADDR, addrinfo_to_str(&inputA)); 94 } 95 96 97 void 98 test_IPv6Address(void) { 99 const struct in6_addr address = { { { 100 0x20, 0x01, 0x0d, 0xb8, 101 0x85, 0xa3, 0x08, 0xd3, 102 0x13, 0x19, 0x8a, 0x2e, 103 0x03, 0x70, 0x73, 0x34 104 } } }; 105 const char * expected = "2001:db8:85a3:8d3:1319:8a2e:370:7334"; 106 sockaddr_u input; 107 struct addrinfo inputA; 108 109 memset(&input, 0, sizeof(input)); 110 input.sa6.sin6_family = AF_INET6; 111 input.sa6.sin6_addr = address; 112 TEST_ASSERT_EQUAL_STRING(expected, ss_to_str(&input)); 113 114 inputA = CreateAddrinfo(&input); 115 TEST_ASSERT_EQUAL_STRING(expected, addrinfo_to_str(&inputA)); 116 } 117 118 119 void 120 test_SetLiVnMode1(void) { 121 struct pkt expected; 122 expected.li_vn_mode = PKT_LI_VN_MODE(LEAP_NOWARNING, 123 NTP_VERSION, 124 MODE_SERVER); 125 126 struct pkt actual; 127 set_li_vn_mode(&actual, LEAP_NOWARNING, NTP_VERSION, 128 MODE_SERVER); 129 130 TEST_ASSERT_EQUAL(expected.li_vn_mode, actual.li_vn_mode); 131 } 132 133 134 void 135 test_SetLiVnMode2(void) { 136 struct pkt expected; 137 expected.li_vn_mode = PKT_LI_VN_MODE(LEAP_NOTINSYNC, 138 NTP_OLDVERSION, 139 MODE_BROADCAST); 140 141 struct pkt actual; 142 set_li_vn_mode(&actual, LEAP_NOTINSYNC, NTP_OLDVERSION, 143 MODE_BROADCAST); 144 145 TEST_ASSERT_EQUAL(expected.li_vn_mode, actual.li_vn_mode); 146 } 147 148 /* Debug utilities tests */ 149 150 void 151 test_PktOutput(void) { 152 char * filename = "debug-output-pkt"; 153 InitDebugTest(filename); 154 155 struct pkt testpkt; 156 memset(&testpkt, 0, sizeof(struct pkt)); 157 testpkt.li_vn_mode = PKT_LI_VN_MODE(LEAP_NOWARNING, 158 NTP_VERSION, 159 MODE_SERVER); 160 161 l_fp test; 162 test.l_ui = 8; 163 test.l_uf = 2147483647; // Lots of ones. 164 HTONL_FP(&test, &testpkt.xmt); 165 166 pkt_output(&testpkt, LEN_PKT_NOMAC, outputFile); 167 168 FinishDebugTest(CreatePath("debug-input-pkt", INPUT_DIR), filename); 169 } 170 171 172 void 173 test_LfpOutputBinaryFormat(void) { 174 char * filename = "debug-output-lfp-bin";//CreatePath("debug-output-lfp-bin", OUTPUT_DIR); 175 InitDebugTest(filename); 176 177 l_fp test; 178 test.l_ui = 63; // 00000000 00000000 00000000 00111111 179 test.l_uf = 127; // 00000000 00000000 00000000 01111111 180 181 l_fp network; 182 HTONL_FP(&test, &network); 183 184 l_fp_output_bin(&network, outputFile); 185 186 FinishDebugTest(CreatePath("debug-input-lfp-bin", INPUT_DIR), filename); 187 } 188 189 190 void 191 test_LfpOutputDecimalFormat(void) { 192 char * filename = "debug-output-lfp-dec"; 193 InitDebugTest(filename); 194 195 l_fp test; 196 test.l_ui = 6310; // 0x000018A6 197 test.l_uf = 308502; // 0x00004B516 198 199 l_fp network; 200 HTONL_FP(&test, &network); 201 202 l_fp_output_dec(&network, outputFile); 203 204 FinishDebugTest(CreatePath("debug-input-lfp-dec", INPUT_DIR), filename); 205 } 206