4 #include "fileHandlingTest.h"
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);
25 const char * Version = "stub unit test Version string";
29 CreateSockaddr4(const char* address) {
31 s.sa4.sin_family = AF_INET;
32 s.sa4.sin_addr.s_addr = inet_addr(address);
40 CreateAddrinfo(sockaddr_u* sock) {
42 a.ai_family = sock->sa.sa_family;
43 a.ai_addrlen = SIZEOF_SOCKADDR(a.ai_family);
44 a.ai_addr = &sock->sa;
49 bool outputFileOpened;
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;
63 // Closes outputFile, and compare contents.
65 FinishDebugTest(const char * expected,
66 const char * actual) {
70 FILE * e = fopen(expected,"rb");
71 FILE * a = fopen(actual,"rb");
72 TEST_ASSERT_NOT_NULL(e);
73 TEST_ASSERT_NOT_NULL(a);
75 CompareFileContent(e, a);
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.
86 test_IPv4Address(void) {
87 const char* ADDR = "192.0.2.10";
89 sockaddr_u input = CreateSockaddr4(ADDR);
90 struct addrinfo inputA = CreateAddrinfo(&input);
92 TEST_ASSERT_EQUAL_STRING(ADDR, ss_to_str(&input));
93 TEST_ASSERT_EQUAL_STRING(ADDR, addrinfo_to_str(&inputA));
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
105 const char * expected = "2001:db8:85a3:8d3:1319:8a2e:370:7334";
107 struct addrinfo inputA;
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));
114 inputA = CreateAddrinfo(&input);
115 TEST_ASSERT_EQUAL_STRING(expected, addrinfo_to_str(&inputA));
120 test_SetLiVnMode1(void) {
122 expected.li_vn_mode = PKT_LI_VN_MODE(LEAP_NOWARNING,
127 set_li_vn_mode(&actual, LEAP_NOWARNING, NTP_VERSION,
130 TEST_ASSERT_EQUAL(expected.li_vn_mode, actual.li_vn_mode);
135 test_SetLiVnMode2(void) {
137 expected.li_vn_mode = PKT_LI_VN_MODE(LEAP_NOTINSYNC,
142 set_li_vn_mode(&actual, LEAP_NOTINSYNC, NTP_OLDVERSION,
145 TEST_ASSERT_EQUAL(expected.li_vn_mode, actual.li_vn_mode);
148 /* Debug utilities tests */
151 test_PktOutput(void) {
152 char * filename = "debug-output-pkt";
153 InitDebugTest(filename);
156 memset(&testpkt, 0, sizeof(struct pkt));
157 testpkt.li_vn_mode = PKT_LI_VN_MODE(LEAP_NOWARNING,
163 test.l_uf = 2147483647; // Lots of ones.
164 HTONL_FP(&test, &testpkt.xmt);
166 pkt_output(&testpkt, LEN_PKT_NOMAC, outputFile);
168 FinishDebugTest(CreatePath("debug-input-pkt", INPUT_DIR), filename);
173 test_LfpOutputBinaryFormat(void) {
174 char * filename = "debug-output-lfp-bin";//CreatePath("debug-output-lfp-bin", OUTPUT_DIR);
175 InitDebugTest(filename);
178 test.l_ui = 63; // 00000000 00000000 00000000 00111111
179 test.l_uf = 127; // 00000000 00000000 00000000 01111111
182 HTONL_FP(&test, &network);
184 l_fp_output_bin(&network, outputFile);
186 FinishDebugTest(CreatePath("debug-input-lfp-bin", INPUT_DIR), filename);
191 test_LfpOutputDecimalFormat(void) {
192 char * filename = "debug-output-lfp-dec";
193 InitDebugTest(filename);
196 test.l_ui = 6310; // 0x000018A6
197 test.l_uf = 308502; // 0x00004B516
200 HTONL_FP(&test, &network);
202 l_fp_output_dec(&network, outputFile);
204 FinishDebugTest(CreatePath("debug-input-lfp-dec", INPUT_DIR), filename);