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