xref: /freebsd/contrib/ntp/sntp/tests/utilities.c (revision 3311ff84eac3b7e82f28e331df0586036c6d361c)
1276da39aSCy Schubert #include "config.h"
2276da39aSCy Schubert 
3276da39aSCy Schubert #include "sntptest.h"
4276da39aSCy Schubert #include "fileHandlingTest.h"
5276da39aSCy Schubert #include "main.h"
6276da39aSCy Schubert #include "utilities.h"
7276da39aSCy Schubert 
8276da39aSCy Schubert #include "unity.h"
9276da39aSCy Schubert 
109034852cSGleb Smirnoff #include <math.h>
119034852cSGleb Smirnoff 
129034852cSGleb Smirnoff sockaddr_u CreateSockaddr4(const char* address);
139034852cSGleb Smirnoff struct addrinfo CreateAddrinfo(sockaddr_u* sock);
149034852cSGleb Smirnoff void InitDebugTest(const char * filename);
159034852cSGleb Smirnoff void FinishDebugTest(const char * expected,const char * actual);
169034852cSGleb Smirnoff void test_IPv4Address(void);
179034852cSGleb Smirnoff void test_IPv6Address(void);
189034852cSGleb Smirnoff void test_SetLiVnMode1(void);
199034852cSGleb Smirnoff void test_SetLiVnMode2(void);
209034852cSGleb Smirnoff void test_PktOutput(void);
219034852cSGleb Smirnoff void test_LfpOutputBinaryFormat(void);
229034852cSGleb Smirnoff void test_LfpOutputDecimalFormat(void);
239034852cSGleb Smirnoff 
249034852cSGleb Smirnoff 
25276da39aSCy Schubert const char * Version = "stub unit test Version string";
26276da39aSCy Schubert 
27276da39aSCy Schubert 
289034852cSGleb Smirnoff sockaddr_u
299034852cSGleb Smirnoff CreateSockaddr4(const char* address) {
30276da39aSCy Schubert 	sockaddr_u s;
31276da39aSCy Schubert 	s.sa4.sin_family = AF_INET;
32276da39aSCy Schubert 	s.sa4.sin_addr.s_addr = inet_addr(address);
33276da39aSCy Schubert 	SET_PORT(&s, 123);
34276da39aSCy Schubert 
35276da39aSCy Schubert 	return s;
36276da39aSCy Schubert }
37276da39aSCy Schubert 
389034852cSGleb Smirnoff 
399034852cSGleb Smirnoff struct addrinfo
409034852cSGleb Smirnoff CreateAddrinfo(sockaddr_u* sock) {
41276da39aSCy Schubert 	struct addrinfo a;
42276da39aSCy Schubert 	a.ai_family = sock->sa.sa_family;
43276da39aSCy Schubert 	a.ai_addrlen = SIZEOF_SOCKADDR(a.ai_family);
44276da39aSCy Schubert 	a.ai_addr = &sock->sa;
45276da39aSCy Schubert 	return a;
46276da39aSCy Schubert }
47276da39aSCy Schubert 
48276da39aSCy Schubert 
49276da39aSCy Schubert bool outputFileOpened;
50276da39aSCy Schubert FILE* outputFile;
51276da39aSCy Schubert 
52276da39aSCy Schubert 
539034852cSGleb Smirnoff void
549034852cSGleb Smirnoff InitDebugTest(const char * filename) {
55276da39aSCy Schubert 	// Clear the contents of the current file.
56276da39aSCy Schubert 	// Open the output file
57276da39aSCy Schubert 	outputFile = fopen(filename, "w+");
589034852cSGleb Smirnoff 	TEST_ASSERT_NOT_NULL(outputFile);
59276da39aSCy Schubert 	outputFileOpened = true;
60276da39aSCy Schubert }
61276da39aSCy Schubert 
629034852cSGleb Smirnoff 
63276da39aSCy Schubert // Closes outputFile, and compare contents.
649034852cSGleb Smirnoff void
659034852cSGleb Smirnoff FinishDebugTest(const char * expected,
66276da39aSCy Schubert 		     const char * actual) {
67276da39aSCy Schubert 	if (outputFileOpened)
68276da39aSCy Schubert 		fclose(outputFile);
69276da39aSCy Schubert 
70276da39aSCy Schubert 	FILE * e = fopen(expected,"rb");
71276da39aSCy Schubert 	FILE * a = fopen(actual,"rb");
729034852cSGleb Smirnoff 	TEST_ASSERT_NOT_NULL(e);
739034852cSGleb Smirnoff 	TEST_ASSERT_NOT_NULL(a);
74276da39aSCy Schubert 
75276da39aSCy Schubert 	CompareFileContent(e, a);
76276da39aSCy Schubert }
77276da39aSCy Schubert 
78276da39aSCy Schubert 
79276da39aSCy Schubert /*
80276da39aSCy Schubert  * These tests are essentially a copy of the tests for socktoa()
81276da39aSCy Schubert  * in libntp. If sntp switches to using that functions, these
82276da39aSCy Schubert  * tests can be removed.
83276da39aSCy Schubert  */
84276da39aSCy Schubert 
859034852cSGleb Smirnoff void
869034852cSGleb Smirnoff test_IPv4Address(void) {
87276da39aSCy Schubert 	const char* ADDR = "192.0.2.10";
88276da39aSCy Schubert 
89276da39aSCy Schubert 	sockaddr_u input = CreateSockaddr4(ADDR);
90276da39aSCy Schubert 	struct addrinfo inputA = CreateAddrinfo(&input);
91276da39aSCy Schubert 
92276da39aSCy Schubert 	TEST_ASSERT_EQUAL_STRING(ADDR, ss_to_str(&input));
93276da39aSCy Schubert 	TEST_ASSERT_EQUAL_STRING(ADDR, addrinfo_to_str(&inputA));
94276da39aSCy Schubert }
95276da39aSCy Schubert 
969034852cSGleb Smirnoff 
979034852cSGleb Smirnoff void
989034852cSGleb Smirnoff test_IPv6Address(void) {
99*3311ff84SXin LI 	const struct in6_addr address = { { {
100276da39aSCy Schubert 						0x20, 0x01, 0x0d, 0xb8,
101276da39aSCy Schubert 						0x85, 0xa3, 0x08, 0xd3,
102276da39aSCy Schubert 						0x13, 0x19, 0x8a, 0x2e,
103276da39aSCy Schubert 						0x03, 0x70, 0x73, 0x34
104*3311ff84SXin LI 					} } };
105276da39aSCy Schubert 	const char * expected = "2001:db8:85a3:8d3:1319:8a2e:370:7334";
106276da39aSCy Schubert 	sockaddr_u	input;
107276da39aSCy Schubert 	struct addrinfo	inputA;
108276da39aSCy Schubert 
109276da39aSCy Schubert 	memset(&input, 0, sizeof(input));
110276da39aSCy Schubert 	input.sa6.sin6_family = AF_INET6;
111276da39aSCy Schubert 	input.sa6.sin6_addr = address;
112276da39aSCy Schubert 	TEST_ASSERT_EQUAL_STRING(expected, ss_to_str(&input));
113276da39aSCy Schubert 
114276da39aSCy Schubert 	inputA = CreateAddrinfo(&input);
115276da39aSCy Schubert 	TEST_ASSERT_EQUAL_STRING(expected, addrinfo_to_str(&inputA));
116276da39aSCy Schubert }
117276da39aSCy Schubert 
1189034852cSGleb Smirnoff 
1199034852cSGleb Smirnoff void
1209034852cSGleb Smirnoff test_SetLiVnMode1(void) {
121276da39aSCy Schubert 	struct pkt expected;
122276da39aSCy Schubert 	expected.li_vn_mode = PKT_LI_VN_MODE(LEAP_NOWARNING,
123276da39aSCy Schubert 					     NTP_VERSION,
124276da39aSCy Schubert 					     MODE_SERVER);
125276da39aSCy Schubert 
126276da39aSCy Schubert 	struct pkt actual;
127276da39aSCy Schubert 	set_li_vn_mode(&actual, LEAP_NOWARNING, NTP_VERSION,
128276da39aSCy Schubert 				   MODE_SERVER);
129276da39aSCy Schubert 
130276da39aSCy Schubert 	TEST_ASSERT_EQUAL(expected.li_vn_mode, actual.li_vn_mode);
131276da39aSCy Schubert }
132276da39aSCy Schubert 
1339034852cSGleb Smirnoff 
1349034852cSGleb Smirnoff void
1359034852cSGleb Smirnoff test_SetLiVnMode2(void) {
136276da39aSCy Schubert 	struct pkt expected;
137276da39aSCy Schubert 	expected.li_vn_mode = PKT_LI_VN_MODE(LEAP_NOTINSYNC,
138276da39aSCy Schubert 					     NTP_OLDVERSION,
139276da39aSCy Schubert 					     MODE_BROADCAST);
140276da39aSCy Schubert 
141276da39aSCy Schubert 	struct pkt actual;
142276da39aSCy Schubert 	set_li_vn_mode(&actual, LEAP_NOTINSYNC, NTP_OLDVERSION,
143276da39aSCy Schubert 				   MODE_BROADCAST);
144276da39aSCy Schubert 
145276da39aSCy Schubert 	TEST_ASSERT_EQUAL(expected.li_vn_mode, actual.li_vn_mode);
146276da39aSCy Schubert }
147276da39aSCy Schubert 
148276da39aSCy Schubert /* Debug utilities tests */
149276da39aSCy Schubert 
1509034852cSGleb Smirnoff void
1519034852cSGleb Smirnoff test_PktOutput(void) {
1529034852cSGleb Smirnoff 	char * filename = "debug-output-pkt";
153276da39aSCy Schubert 	InitDebugTest(filename);
154276da39aSCy Schubert 
155276da39aSCy Schubert 	struct pkt testpkt;
156276da39aSCy Schubert 	memset(&testpkt, 0, sizeof(struct pkt));
157276da39aSCy Schubert 	testpkt.li_vn_mode = PKT_LI_VN_MODE(LEAP_NOWARNING,
158276da39aSCy Schubert 					    NTP_VERSION,
159276da39aSCy Schubert 					    MODE_SERVER);
160276da39aSCy Schubert 
161276da39aSCy Schubert 	l_fp test;
162276da39aSCy Schubert 	test.l_ui = 8;
163276da39aSCy Schubert 	test.l_uf = 2147483647; // Lots of ones.
164276da39aSCy Schubert 	HTONL_FP(&test, &testpkt.xmt);
165276da39aSCy Schubert 
166276da39aSCy Schubert 	pkt_output(&testpkt, LEN_PKT_NOMAC, outputFile);
167276da39aSCy Schubert 
168276da39aSCy Schubert 	FinishDebugTest(CreatePath("debug-input-pkt", INPUT_DIR), filename);
169276da39aSCy Schubert }
170276da39aSCy Schubert 
1719034852cSGleb Smirnoff 
1729034852cSGleb Smirnoff void
1739034852cSGleb Smirnoff test_LfpOutputBinaryFormat(void) {
174276da39aSCy Schubert 	char * filename = "debug-output-lfp-bin";//CreatePath("debug-output-lfp-bin", OUTPUT_DIR);
175276da39aSCy Schubert 	InitDebugTest(filename);
176276da39aSCy Schubert 
177276da39aSCy Schubert 	l_fp test;
178276da39aSCy Schubert 	test.l_ui = 63;  // 00000000 00000000 00000000 00111111
179276da39aSCy Schubert 	test.l_uf = 127; // 00000000 00000000 00000000 01111111
180276da39aSCy Schubert 
181276da39aSCy Schubert 	l_fp network;
182276da39aSCy Schubert 	HTONL_FP(&test, &network);
183276da39aSCy Schubert 
184276da39aSCy Schubert 	l_fp_output_bin(&network, outputFile);
185276da39aSCy Schubert 
186276da39aSCy Schubert 	FinishDebugTest(CreatePath("debug-input-lfp-bin", INPUT_DIR), filename);
187276da39aSCy Schubert }
188276da39aSCy Schubert 
1899034852cSGleb Smirnoff 
1909034852cSGleb Smirnoff void
1919034852cSGleb Smirnoff test_LfpOutputDecimalFormat(void) {
1929034852cSGleb Smirnoff 	char * filename = "debug-output-lfp-dec";
193276da39aSCy Schubert 	InitDebugTest(filename);
194276da39aSCy Schubert 
195276da39aSCy Schubert 	l_fp test;
196276da39aSCy Schubert 	test.l_ui = 6310; // 0x000018A6
197276da39aSCy Schubert 	test.l_uf = 308502; // 0x00004B516
198276da39aSCy Schubert 
199276da39aSCy Schubert 	l_fp network;
200276da39aSCy Schubert 	HTONL_FP(&test, &network);
201276da39aSCy Schubert 
202276da39aSCy Schubert 	l_fp_output_dec(&network, outputFile);
203276da39aSCy Schubert 
204276da39aSCy Schubert 	FinishDebugTest(CreatePath("debug-input-lfp-dec", INPUT_DIR), filename);
205276da39aSCy Schubert }
206