xref: /freebsd/contrib/ntp/sntp/tests/packetHandling.c (revision 276da39af92f48350aa01091a2b8b3e735217eea)
1*276da39aSCy Schubert #include "config.h"
2*276da39aSCy Schubert #include "ntp_debug.h"
3*276da39aSCy Schubert #include "ntp_stdlib.h"
4*276da39aSCy Schubert #include "ntp_types.h"
5*276da39aSCy Schubert 
6*276da39aSCy Schubert #include "sntptest.h"
7*276da39aSCy Schubert 
8*276da39aSCy Schubert #include "kod_management.h"
9*276da39aSCy Schubert #include "main.h"
10*276da39aSCy Schubert #include "networking.h"
11*276da39aSCy Schubert #include "ntp.h"
12*276da39aSCy Schubert 
13*276da39aSCy Schubert #include "unity.h"
14*276da39aSCy Schubert 
15*276da39aSCy Schubert 
16*276da39aSCy Schubert int counter = 0;
17*276da39aSCy Schubert 
18*276da39aSCy Schubert 
19*276da39aSCy Schubert // old code from google test framework, moved to SetUp() for unity
20*276da39aSCy Schubert void setUp(void)
21*276da39aSCy Schubert {
22*276da39aSCy Schubert 	init_lib();
23*276da39aSCy Schubert }
24*276da39aSCy Schubert 
25*276da39aSCy Schubert 
26*276da39aSCy Schubert 
27*276da39aSCy Schubert int LfpEquality(const l_fp expected, const l_fp actual) {
28*276da39aSCy Schubert 		if (L_ISEQU(&expected, &actual)) {
29*276da39aSCy Schubert 			return TRUE;
30*276da39aSCy Schubert 		} else {
31*276da39aSCy Schubert 			return FALSE;
32*276da39aSCy Schubert 		}
33*276da39aSCy Schubert }
34*276da39aSCy Schubert 
35*276da39aSCy Schubert void test_GenerateUnauthenticatedPacket(void) {
36*276da39aSCy Schubert 	struct pkt testpkt;
37*276da39aSCy Schubert 
38*276da39aSCy Schubert 	struct timeval xmt;
39*276da39aSCy Schubert 	GETTIMEOFDAY(&xmt, NULL);
40*276da39aSCy Schubert 	xmt.tv_sec += JAN_1970;
41*276da39aSCy Schubert 
42*276da39aSCy Schubert 	TEST_ASSERT_EQUAL(LEN_PKT_NOMAC,
43*276da39aSCy Schubert 			  generate_pkt(&testpkt, &xmt, 0, NULL));
44*276da39aSCy Schubert 
45*276da39aSCy Schubert 	TEST_ASSERT_EQUAL(LEAP_NOTINSYNC, PKT_LEAP(testpkt.li_vn_mode));
46*276da39aSCy Schubert 	TEST_ASSERT_EQUAL(NTP_VERSION, PKT_VERSION(testpkt.li_vn_mode));
47*276da39aSCy Schubert 	TEST_ASSERT_EQUAL(MODE_CLIENT, PKT_MODE(testpkt.li_vn_mode));
48*276da39aSCy Schubert 
49*276da39aSCy Schubert 	TEST_ASSERT_EQUAL(STRATUM_UNSPEC, PKT_TO_STRATUM(testpkt.stratum));
50*276da39aSCy Schubert 	TEST_ASSERT_EQUAL(8, testpkt.ppoll);
51*276da39aSCy Schubert 
52*276da39aSCy Schubert 	l_fp expected_xmt, actual_xmt;
53*276da39aSCy Schubert 	TVTOTS(&xmt, &expected_xmt);
54*276da39aSCy Schubert 	NTOHL_FP(&testpkt.xmt, &actual_xmt);
55*276da39aSCy Schubert 	TEST_ASSERT_TRUE(LfpEquality(expected_xmt, actual_xmt));
56*276da39aSCy Schubert }
57*276da39aSCy Schubert 
58*276da39aSCy Schubert void test_GenerateAuthenticatedPacket(void) {
59*276da39aSCy Schubert 	struct key testkey;
60*276da39aSCy Schubert 	testkey.next = NULL;
61*276da39aSCy Schubert 	testkey.key_id = 30;
62*276da39aSCy Schubert 	testkey.key_len = 9;
63*276da39aSCy Schubert 	memcpy(testkey.key_seq, "123456789", testkey.key_len);
64*276da39aSCy Schubert 	memcpy(testkey.type, "MD5", 3);
65*276da39aSCy Schubert 
66*276da39aSCy Schubert 	struct pkt testpkt;
67*276da39aSCy Schubert 
68*276da39aSCy Schubert 	struct timeval xmt;
69*276da39aSCy Schubert 	GETTIMEOFDAY(&xmt, NULL);
70*276da39aSCy Schubert 	xmt.tv_sec += JAN_1970;
71*276da39aSCy Schubert 
72*276da39aSCy Schubert 	const int EXPECTED_PKTLEN = LEN_PKT_NOMAC + MAX_MD5_LEN;
73*276da39aSCy Schubert 
74*276da39aSCy Schubert 	TEST_ASSERT_EQUAL(EXPECTED_PKTLEN,
75*276da39aSCy Schubert 			  generate_pkt(&testpkt, &xmt, testkey.key_id, &testkey));
76*276da39aSCy Schubert 
77*276da39aSCy Schubert 	TEST_ASSERT_EQUAL(LEAP_NOTINSYNC, PKT_LEAP(testpkt.li_vn_mode));
78*276da39aSCy Schubert 	TEST_ASSERT_EQUAL(NTP_VERSION, PKT_VERSION(testpkt.li_vn_mode));
79*276da39aSCy Schubert 	TEST_ASSERT_EQUAL(MODE_CLIENT, PKT_MODE(testpkt.li_vn_mode));
80*276da39aSCy Schubert 
81*276da39aSCy Schubert 	TEST_ASSERT_EQUAL(STRATUM_UNSPEC, PKT_TO_STRATUM(testpkt.stratum));
82*276da39aSCy Schubert 	TEST_ASSERT_EQUAL(8, testpkt.ppoll);
83*276da39aSCy Schubert 
84*276da39aSCy Schubert 	l_fp expected_xmt, actual_xmt;
85*276da39aSCy Schubert 	TVTOTS(&xmt, &expected_xmt);
86*276da39aSCy Schubert 	NTOHL_FP(&testpkt.xmt, &actual_xmt);
87*276da39aSCy Schubert 	TEST_ASSERT_TRUE(LfpEquality(expected_xmt, actual_xmt));
88*276da39aSCy Schubert 
89*276da39aSCy Schubert 	TEST_ASSERT_EQUAL(testkey.key_id, ntohl(testpkt.exten[0]));
90*276da39aSCy Schubert 
91*276da39aSCy Schubert 	char expected_mac[MAX_MD5_LEN];
92*276da39aSCy Schubert 	TEST_ASSERT_EQUAL(MAX_MD5_LEN - 4, // Remove the key_id, only keep the mac.
93*276da39aSCy Schubert 			  make_mac((char*)&testpkt, LEN_PKT_NOMAC, MAX_MD5_LEN, &testkey, expected_mac));
94*276da39aSCy Schubert 	TEST_ASSERT_TRUE(memcmp(expected_mac, (char*)&testpkt.exten[1], MAX_MD5_LEN -4) == 0);
95*276da39aSCy Schubert }
96*276da39aSCy Schubert 
97*276da39aSCy Schubert void test_OffsetCalculationPositiveOffset(void) {
98*276da39aSCy Schubert 	struct pkt rpkt;
99*276da39aSCy Schubert 
100*276da39aSCy Schubert 	rpkt.precision = -16; // 0,000015259
101*276da39aSCy Schubert 	rpkt.rootdelay = HTONS_FP(DTOUFP(0.125));
102*276da39aSCy Schubert 	rpkt.rootdisp = HTONS_FP(DTOUFP(0.25));
103*276da39aSCy Schubert 	// Synch Distance: (0.125+0.25)/2.0 == 0.1875
104*276da39aSCy Schubert 	l_fp reftime;
105*276da39aSCy Schubert 	get_systime(&reftime);
106*276da39aSCy Schubert 	HTONL_FP(&reftime, &rpkt.reftime);
107*276da39aSCy Schubert 
108*276da39aSCy Schubert 	l_fp tmp;
109*276da39aSCy Schubert 
110*276da39aSCy Schubert 	// T1 - Originate timestamp
111*276da39aSCy Schubert 	tmp.l_ui = 1000000000UL;
112*276da39aSCy Schubert 	tmp.l_uf = 0UL;
113*276da39aSCy Schubert 	HTONL_FP(&tmp, &rpkt.org);
114*276da39aSCy Schubert 
115*276da39aSCy Schubert 	// T2 - Receive timestamp
116*276da39aSCy Schubert 	tmp.l_ui = 1000000001UL;
117*276da39aSCy Schubert 	tmp.l_uf = 2147483648UL;
118*276da39aSCy Schubert 	HTONL_FP(&tmp, &rpkt.rec);
119*276da39aSCy Schubert 
120*276da39aSCy Schubert 	// T3 - Transmit timestamp
121*276da39aSCy Schubert 	tmp.l_ui = 1000000002UL;
122*276da39aSCy Schubert 	tmp.l_uf = 0UL;
123*276da39aSCy Schubert 	HTONL_FP(&tmp, &rpkt.xmt);
124*276da39aSCy Schubert 
125*276da39aSCy Schubert 	// T4 - Destination timestamp as standard timeval
126*276da39aSCy Schubert 	tmp.l_ui = 1000000001UL;
127*276da39aSCy Schubert 	tmp.l_uf = 0UL;
128*276da39aSCy Schubert 	struct timeval dst;
129*276da39aSCy Schubert 	TSTOTV(&tmp, &dst);
130*276da39aSCy Schubert 	dst.tv_sec -= JAN_1970;
131*276da39aSCy Schubert 
132*276da39aSCy Schubert 	double offset, precision, synch_distance;
133*276da39aSCy Schubert 	offset_calculation(&rpkt, LEN_PKT_NOMAC, &dst, &offset, &precision, &synch_distance);
134*276da39aSCy Schubert 
135*276da39aSCy Schubert 	TEST_ASSERT_EQUAL_FLOAT(1.25, offset);
136*276da39aSCy Schubert 	TEST_ASSERT_EQUAL_FLOAT(1. / ULOGTOD(16), precision);
137*276da39aSCy Schubert 	// 1.1250150000000001 ?
138*276da39aSCy Schubert 	TEST_ASSERT_EQUAL_FLOAT(1.125015, synch_distance);
139*276da39aSCy Schubert }
140*276da39aSCy Schubert 
141*276da39aSCy Schubert void test_OffsetCalculationNegativeOffset(void) {
142*276da39aSCy Schubert 	struct pkt rpkt;
143*276da39aSCy Schubert 
144*276da39aSCy Schubert 	rpkt.precision = -1;
145*276da39aSCy Schubert 	rpkt.rootdelay = HTONS_FP(DTOUFP(0.5));
146*276da39aSCy Schubert 	rpkt.rootdisp = HTONS_FP(DTOUFP(0.5));
147*276da39aSCy Schubert 	// Synch Distance is (0.5+0.5)/2.0, or 0.5
148*276da39aSCy Schubert 	l_fp reftime;
149*276da39aSCy Schubert 	get_systime(&reftime);
150*276da39aSCy Schubert 	HTONL_FP(&reftime, &rpkt.reftime);
151*276da39aSCy Schubert 
152*276da39aSCy Schubert 	l_fp tmp;
153*276da39aSCy Schubert 
154*276da39aSCy Schubert 	// T1 - Originate timestamp
155*276da39aSCy Schubert 	tmp.l_ui = 1000000001UL;
156*276da39aSCy Schubert 	tmp.l_uf = 0UL;
157*276da39aSCy Schubert 	HTONL_FP(&tmp, &rpkt.org);
158*276da39aSCy Schubert 
159*276da39aSCy Schubert 	// T2 - Receive timestamp
160*276da39aSCy Schubert 	tmp.l_ui = 1000000000UL;
161*276da39aSCy Schubert 	tmp.l_uf = 2147483648UL;
162*276da39aSCy Schubert 	HTONL_FP(&tmp, &rpkt.rec);
163*276da39aSCy Schubert 
164*276da39aSCy Schubert 	// T3 - Transmit timestamp
165*276da39aSCy Schubert 	tmp.l_ui = 1000000001UL;
166*276da39aSCy Schubert 	tmp.l_uf = 2147483648UL;
167*276da39aSCy Schubert 	HTONL_FP(&tmp, &rpkt.xmt);
168*276da39aSCy Schubert 
169*276da39aSCy Schubert 	// T4 - Destination timestamp as standard timeval
170*276da39aSCy Schubert 	tmp.l_ui = 1000000003UL;
171*276da39aSCy Schubert 	tmp.l_uf = 0UL;
172*276da39aSCy Schubert 	struct timeval dst;
173*276da39aSCy Schubert 	TSTOTV(&tmp, &dst);
174*276da39aSCy Schubert 	dst.tv_sec -= JAN_1970;
175*276da39aSCy Schubert 
176*276da39aSCy Schubert 	double offset, precision, synch_distance;
177*276da39aSCy Schubert 	offset_calculation(&rpkt, LEN_PKT_NOMAC, &dst, &offset, &precision, &synch_distance);
178*276da39aSCy Schubert 
179*276da39aSCy Schubert 	TEST_ASSERT_EQUAL_FLOAT(-1, offset);
180*276da39aSCy Schubert 	TEST_ASSERT_EQUAL_FLOAT(1. / ULOGTOD(1), precision);
181*276da39aSCy Schubert 	TEST_ASSERT_EQUAL_FLOAT(1.3333483333333334, synch_distance);
182*276da39aSCy Schubert }
183*276da39aSCy Schubert 
184*276da39aSCy Schubert void test_HandleUnusableServer(void) {
185*276da39aSCy Schubert 	struct pkt		rpkt;
186*276da39aSCy Schubert 	sockaddr_u	host;
187*276da39aSCy Schubert 	int		rpktl;
188*276da39aSCy Schubert 
189*276da39aSCy Schubert 	ZERO(rpkt);
190*276da39aSCy Schubert 	ZERO(host);
191*276da39aSCy Schubert 	rpktl = SERVER_UNUSEABLE;
192*276da39aSCy Schubert 	TEST_ASSERT_EQUAL(-1, handle_pkt(rpktl, &rpkt, &host, ""));
193*276da39aSCy Schubert }
194*276da39aSCy Schubert 
195*276da39aSCy Schubert void test_HandleUnusablePacket(void) {
196*276da39aSCy Schubert 	struct pkt		rpkt;
197*276da39aSCy Schubert 	sockaddr_u	host;
198*276da39aSCy Schubert 	int		rpktl;
199*276da39aSCy Schubert 
200*276da39aSCy Schubert 	ZERO(rpkt);
201*276da39aSCy Schubert 	ZERO(host);
202*276da39aSCy Schubert 	rpktl = PACKET_UNUSEABLE;
203*276da39aSCy Schubert 	TEST_ASSERT_EQUAL(1, handle_pkt(rpktl, &rpkt, &host, ""));
204*276da39aSCy Schubert }
205*276da39aSCy Schubert 
206*276da39aSCy Schubert void test_HandleServerAuthenticationFailure(void) {
207*276da39aSCy Schubert 	struct pkt		rpkt;
208*276da39aSCy Schubert 	sockaddr_u	host;
209*276da39aSCy Schubert 	int		rpktl;
210*276da39aSCy Schubert 
211*276da39aSCy Schubert 	ZERO(rpkt);
212*276da39aSCy Schubert 	ZERO(host);
213*276da39aSCy Schubert 	rpktl = SERVER_AUTH_FAIL;
214*276da39aSCy Schubert 	TEST_ASSERT_EQUAL(1, handle_pkt(rpktl, &rpkt, &host, ""));
215*276da39aSCy Schubert }
216*276da39aSCy Schubert 
217*276da39aSCy Schubert void test_HandleKodDemobilize(void) {
218*276da39aSCy Schubert 	const char *	HOSTNAME = "192.0.2.1";
219*276da39aSCy Schubert 	const char *	REASON = "DENY";
220*276da39aSCy Schubert 	struct pkt		rpkt;
221*276da39aSCy Schubert 	sockaddr_u	host;
222*276da39aSCy Schubert 	int		rpktl;
223*276da39aSCy Schubert 	struct kod_entry *	entry;
224*276da39aSCy Schubert 
225*276da39aSCy Schubert 	rpktl = KOD_DEMOBILIZE;
226*276da39aSCy Schubert 	ZERO(rpkt);
227*276da39aSCy Schubert 	memcpy(&rpkt.refid, REASON, 4);
228*276da39aSCy Schubert 	ZERO(host);
229*276da39aSCy Schubert 	host.sa4.sin_family = AF_INET;
230*276da39aSCy Schubert 	host.sa4.sin_addr.s_addr = inet_addr(HOSTNAME);
231*276da39aSCy Schubert 
232*276da39aSCy Schubert 	// Test that the KOD-entry is added to the database.
233*276da39aSCy Schubert 	kod_init_kod_db("/dev/null", TRUE);
234*276da39aSCy Schubert 
235*276da39aSCy Schubert 	TEST_ASSERT_EQUAL(1, handle_pkt(rpktl, &rpkt, &host, HOSTNAME));
236*276da39aSCy Schubert 
237*276da39aSCy Schubert 	TEST_ASSERT_EQUAL(1, search_entry(HOSTNAME, &entry));
238*276da39aSCy Schubert 	TEST_ASSERT_TRUE(memcmp(REASON, entry->type, 4) == 0);
239*276da39aSCy Schubert }
240*276da39aSCy Schubert 
241*276da39aSCy Schubert void test_HandleKodRate(void) {
242*276da39aSCy Schubert 	struct 	pkt		rpkt;
243*276da39aSCy Schubert 	sockaddr_u	host;
244*276da39aSCy Schubert 	int		rpktl;
245*276da39aSCy Schubert 
246*276da39aSCy Schubert 	ZERO(rpkt);
247*276da39aSCy Schubert 	ZERO(host);
248*276da39aSCy Schubert 	rpktl = KOD_RATE;
249*276da39aSCy Schubert 	TEST_ASSERT_EQUAL(1, handle_pkt(rpktl, &rpkt, &host, ""));
250*276da39aSCy Schubert }
251*276da39aSCy Schubert 
252*276da39aSCy Schubert void test_HandleCorrectPacket(void) {
253*276da39aSCy Schubert 	struct pkt		rpkt;
254*276da39aSCy Schubert 	sockaddr_u	host;
255*276da39aSCy Schubert 	int		rpktl;
256*276da39aSCy Schubert 	l_fp		now;
257*276da39aSCy Schubert 
258*276da39aSCy Schubert 	// We don't want our testing code to actually change the system clock.
259*276da39aSCy Schubert 	TEST_ASSERT_FALSE(ENABLED_OPT(STEP));
260*276da39aSCy Schubert 	TEST_ASSERT_FALSE(ENABLED_OPT(SLEW));
261*276da39aSCy Schubert 
262*276da39aSCy Schubert 	get_systime(&now);
263*276da39aSCy Schubert 	HTONL_FP(&now, &rpkt.reftime);
264*276da39aSCy Schubert 	HTONL_FP(&now, &rpkt.org);
265*276da39aSCy Schubert 	HTONL_FP(&now, &rpkt.rec);
266*276da39aSCy Schubert 	HTONL_FP(&now, &rpkt.xmt);
267*276da39aSCy Schubert 	rpktl = LEN_PKT_NOMAC;
268*276da39aSCy Schubert 	ZERO(host);
269*276da39aSCy Schubert 	AF(&host) = AF_INET;
270*276da39aSCy Schubert 
271*276da39aSCy Schubert 	TEST_ASSERT_EQUAL(0, handle_pkt(rpktl, &rpkt, &host, ""));
272*276da39aSCy Schubert }
273*276da39aSCy Schubert 
274*276da39aSCy Schubert /* packetHandling.c */
275