summaryrefslogtreecommitdiffstats
path: root/contrib/ntp/sntp/tests/utilities.c
blob: a68aaacae9aef0aaf1ff7b4fa024071c0154f809 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
#include "config.h"

#include "sntptest.h"
#include "fileHandlingTest.h"
#include "main.h"
#include "utilities.h"

#include "unity.h"

#include <math.h>

sockaddr_u CreateSockaddr4(const char* address);
struct addrinfo CreateAddrinfo(sockaddr_u* sock);
void InitDebugTest(const char * filename);
void FinishDebugTest(const char * expected,const char * actual);
void test_IPv4Address(void);
void test_IPv6Address(void);
void test_SetLiVnMode1(void);
void test_SetLiVnMode2(void);
void test_PktOutput(void);
void test_LfpOutputBinaryFormat(void);
void test_LfpOutputDecimalFormat(void);


const char * Version = "stub unit test Version string";


sockaddr_u
CreateSockaddr4(const char* address) {
	sockaddr_u s;
	s.sa4.sin_family = AF_INET;
	s.sa4.sin_addr.s_addr = inet_addr(address);
	SET_PORT(&s, 123);

	return s;
}


struct addrinfo
CreateAddrinfo(sockaddr_u* sock) {
	struct addrinfo a;
	a.ai_family = sock->sa.sa_family;
	a.ai_addrlen = SIZEOF_SOCKADDR(a.ai_family);
	a.ai_addr = &sock->sa;
	return a;
}


bool outputFileOpened;
FILE* outputFile;


void
InitDebugTest(const char * filename) {
	// Clear the contents of the current file.
	// Open the output file
	outputFile = fopen(filename, "w+");
	TEST_ASSERT_NOT_NULL(outputFile);
	outputFileOpened = true;
}


// Closes outputFile, and compare contents.
void
FinishDebugTest(const char * expected,
		     const char * actual) {
	if (outputFileOpened)
		fclose(outputFile);

	FILE * e = fopen(expected,"rb");
	FILE * a = fopen(actual,"rb");
	TEST_ASSERT_NOT_NULL(e);
	TEST_ASSERT_NOT_NULL(a);

	CompareFileContent(e, a);
}


/* 
 * These tests are essentially a copy of the tests for socktoa()
 * in libntp. If sntp switches to using that functions, these
 * tests can be removed.
 */

void
test_IPv4Address(void) {
	const char* ADDR = "192.0.2.10";

	sockaddr_u input = CreateSockaddr4(ADDR);
	struct addrinfo inputA = CreateAddrinfo(&input);

	TEST_ASSERT_EQUAL_STRING(ADDR, ss_to_str(&input));
	TEST_ASSERT_EQUAL_STRING(ADDR, addrinfo_to_str(&inputA));
}


void
test_IPv6Address(void) {
	const struct in6_addr address = { { {
						0x20, 0x01, 0x0d, 0xb8,
						0x85, 0xa3, 0x08, 0xd3, 
						0x13, 0x19, 0x8a, 0x2e,
						0x03, 0x70, 0x73, 0x34
					} } };
	const char * expected = "2001:db8:85a3:8d3:1319:8a2e:370:7334";
	sockaddr_u	input;
	struct addrinfo	inputA;

	memset(&input, 0, sizeof(input));
	input.sa6.sin6_family = AF_INET6;
	input.sa6.sin6_addr = address;
	TEST_ASSERT_EQUAL_STRING(expected, ss_to_str(&input));

	inputA = CreateAddrinfo(&input);
	TEST_ASSERT_EQUAL_STRING(expected, addrinfo_to_str(&inputA));
}


void
test_SetLiVnMode1(void) {
	struct pkt expected;
	expected.li_vn_mode = PKT_LI_VN_MODE(LEAP_NOWARNING,
					     NTP_VERSION,
					     MODE_SERVER);

	struct pkt actual;
	set_li_vn_mode(&actual, LEAP_NOWARNING, NTP_VERSION,
				   MODE_SERVER);

	TEST_ASSERT_EQUAL(expected.li_vn_mode, actual.li_vn_mode);
}


void
test_SetLiVnMode2(void) {
	struct pkt expected;
	expected.li_vn_mode = PKT_LI_VN_MODE(LEAP_NOTINSYNC,
					     NTP_OLDVERSION,
					     MODE_BROADCAST);

	struct pkt actual;
	set_li_vn_mode(&actual, LEAP_NOTINSYNC, NTP_OLDVERSION,
				   MODE_BROADCAST);

	TEST_ASSERT_EQUAL(expected.li_vn_mode, actual.li_vn_mode);
}

/* Debug utilities tests */

void
test_PktOutput(void) {
	char * filename = "debug-output-pkt";
	InitDebugTest(filename);

	struct pkt testpkt;
	memset(&testpkt, 0, sizeof(struct pkt));
	testpkt.li_vn_mode = PKT_LI_VN_MODE(LEAP_NOWARNING,
					    NTP_VERSION,
					    MODE_SERVER);

	l_fp test;
	test.l_ui = 8;
	test.l_uf = 2147483647; // Lots of ones.
	HTONL_FP(&test, &testpkt.xmt);

	pkt_output(&testpkt, LEN_PKT_NOMAC, outputFile);

	FinishDebugTest(CreatePath("debug-input-pkt", INPUT_DIR), filename);
}


void
test_LfpOutputBinaryFormat(void) {
	char * filename = "debug-output-lfp-bin";//CreatePath("debug-output-lfp-bin", OUTPUT_DIR);
	InitDebugTest(filename);

	l_fp test;
	test.l_ui = 63;  // 00000000 00000000 00000000 00111111
	test.l_uf = 127; // 00000000 00000000 00000000 01111111

	l_fp network;
	HTONL_FP(&test, &network);

	l_fp_output_bin(&network, outputFile);

	FinishDebugTest(CreatePath("debug-input-lfp-bin", INPUT_DIR), filename);
}


void
test_LfpOutputDecimalFormat(void) {
	char * filename = "debug-output-lfp-dec";
	InitDebugTest(filename);

	l_fp test;
	test.l_ui = 6310; // 0x000018A6
	test.l_uf = 308502; // 0x00004B516

	l_fp network;
	HTONL_FP(&test, &network);

	l_fp_output_dec(&network, outputFile);

	FinishDebugTest(CreatePath("debug-input-lfp-dec", INPUT_DIR), filename);
}
OpenPOWER on IntegriCloud