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