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