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