summaryrefslogtreecommitdiff
path: root/sntp/tests/utilities.cpp
blob: 4c8c305350649570627d011a0c00f26c500e26b6 (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
#include "sntptest.h"
#include "fileHandlingTest.h"

extern "C" {
#include "main.h"
#include "utilities.h"
const char * Version = "stub unit test Version string";
};

using std::string;

class utilitiesTest : public sntptest {
protected:
	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;
	}

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

class debugUtilitiesTest : public fileHandlingTest {
protected:
	bool outputFileOpened;
	FILE* outputFile;

	debugUtilitiesTest() : outputFileOpened(false) {}

	void InitDebugTest(const string& filename) {
		// Clear the contents of the current file.
		// Open the output file
		outputFile = fopen(filename.c_str(), "w+");
		ASSERT_TRUE(outputFile != NULL);
		outputFileOpened = true;
	}

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

		ifstream e(expected.c_str());
		ifstream a(actual.c_str());
		ASSERT_TRUE(e.good());
		ASSERT_TRUE(a.good());

		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.
 */

TEST_F(utilitiesTest, IPv4Address) {
	const char* ADDR = "192.0.2.10";

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

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

TEST_F(utilitiesTest, IPv6Address) {
	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;
	addrinfo	inputA;

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

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

TEST_F(utilitiesTest, SetLiVnMode1) {
	pkt expected;
	expected.li_vn_mode = PKT_LI_VN_MODE(LEAP_NOWARNING,
					     NTP_VERSION,
					     MODE_SERVER);

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

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

TEST_F(utilitiesTest, SetLiVnMode2) {
	pkt expected;
	expected.li_vn_mode = PKT_LI_VN_MODE(LEAP_NOTINSYNC,
										 NTP_OLDVERSION,
										 MODE_BROADCAST);

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

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

/* Debug utilities tests */

TEST_F(debugUtilitiesTest, PktOutput) {
	string filename = CreatePath("debug-output-pkt", OUTPUT_DIR);
	InitDebugTest(filename);

	pkt testpkt;
	memset(&testpkt, 0, sizeof(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);
}

TEST_F(debugUtilitiesTest, LfpOutputBinaryFormat) {
	string filename = 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);
}

TEST_F(debugUtilitiesTest, LfpOutputDecimalFormat) {
	string filename = CreatePath("debug-output-lfp-dec", OUTPUT_DIR);
	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);
}