1284990Scy#include "config.h"
2284990Scy#include "ntp_stdlib.h"
3289999Sglebius#include "sockaddrtest.h"
4289999Sglebius
5284990Scy#include "unity.h"
6284990Scy
7293893Sglebiusvoid setUp(void);
8289999Sglebiusextern void test_IPv4AddressOnly(void);
9289999Sglebiusextern void test_IPv4AddressWithPort(void);
10289999Sglebiusextern void test_IPv6AddressOnly(void);
11289999Sglebiusextern void test_IPv6AddressWithPort(void);
12289999Sglebiusextern void test_IllegalAddress(void);
13289999Sglebiusextern void test_IllegalCharInPort(void);
14284990Scy
15294904Sdelphij/*
16294904Sdelphij * NOTE: The IPv6 specific tests are reduced to stubs when IPv6 is
17294904Sdelphij * disabled.
18294904Sdelphij *
19294904Sdelphij * ISC_PLATFORM_HAVEIPV6 checks if system has IPV6 capabilies. WANTIPV6
20294904Sdelphij * ISC_PLATFORM_WANTIPV6 can be changed with build --disable-ipv6.
21294904Sdelphij *
22294904Sdelphij * If we want IPv6 but don't have it, the tests should fail, I think.
23294904Sdelphij */
24289999Sglebiusvoid
25293893SglebiussetUp(void)
26293893Sglebius{
27293893Sglebius	init_lib();
28293893Sglebius}
29293893Sglebius
30293893Sglebius
31293893Sglebiusvoid
32294904Sdelphijtest_IPv4AddressOnly(void)
33294904Sdelphij{
34284990Scy	const char *str = "192.0.2.1";
35284990Scy	sockaddr_u actual;
36284990Scy
37284990Scy	sockaddr_u expected;
38284990Scy	expected.sa4.sin_family = AF_INET;
39284990Scy	expected.sa4.sin_addr.s_addr = inet_addr("192.0.2.1");
40284990Scy	SET_PORT(&expected, NTP_PORT);
41284990Scy
42284990Scy	TEST_ASSERT_TRUE(decodenetnum(str, &actual));
43284990Scy	TEST_ASSERT_TRUE(IsEqual(expected, actual));
44284990Scy}
45284990Scy
46289999Sglebiusvoid
47294904Sdelphijtest_IPv4AddressWithPort(void)
48294904Sdelphij{
49284990Scy	const char *str = "192.0.2.2:2000";
50284990Scy	sockaddr_u actual;
51284990Scy
52284990Scy	sockaddr_u expected;
53284990Scy	expected.sa4.sin_family = AF_INET;
54284990Scy	expected.sa4.sin_addr.s_addr = inet_addr("192.0.2.2");
55284990Scy	SET_PORT(&expected, 2000);
56284990Scy
57284990Scy	TEST_ASSERT_TRUE(decodenetnum(str, &actual));
58284990Scy	TEST_ASSERT_TRUE(IsEqual(expected, actual));
59284990Scy}
60284990Scy
61289999Sglebius
62289999Sglebiusvoid
63294904Sdelphijtest_IPv6AddressOnly(void)
64294904Sdelphij{
65294904Sdelphij#ifdef ISC_PLATFORM_WANTIPV6
66289999Sglebius
67284990Scy	const struct in6_addr address = {
68284990Scy		0x20, 0x01, 0x0d, 0xb8,
69294904Sdelphij		0x85, 0xa3, 0x08, 0xd3,
70294904Sdelphij		0x13, 0x19, 0x8a, 0x2e,
71294904Sdelphij		0x03, 0x70, 0x73, 0x34
72284990Scy	};
73284990Scy
74284990Scy	const char *str = "2001:0db8:85a3:08d3:1319:8a2e:0370:7334";
75284990Scy	sockaddr_u actual;
76284990Scy
77284990Scy	sockaddr_u expected;
78284990Scy	expected.sa6.sin6_family = AF_INET6;
79284990Scy	expected.sa6.sin6_addr = address;
80284990Scy	SET_PORT(&expected, NTP_PORT);
81284990Scy
82284990Scy	TEST_ASSERT_TRUE(decodenetnum(str, &actual));
83284990Scy	TEST_ASSERT_TRUE(IsEqual(expected, actual));
84289999Sglebius
85289999Sglebius#else
86294904Sdelphij
87289999Sglebius	TEST_IGNORE_MESSAGE("IPV6 disabled in build, skipping.");
88294904Sdelphij
89289999Sglebius#endif /* ISC_PLATFORM_HAVEIPV6 */
90284990Scy}
91284990Scy
92289999Sglebius
93289999Sglebiusvoid
94294904Sdelphijtest_IPv6AddressWithPort(void)
95294904Sdelphij{
96289999Sglebius#ifdef ISC_PLATFORM_WANTIPV6
97289999Sglebius
98284990Scy	const struct in6_addr address = {
99284990Scy		0x20, 0x01, 0x0d, 0xb8,
100294904Sdelphij		0x85, 0xa3, 0x08, 0xd3,
101294904Sdelphij		0x13, 0x19, 0x8a, 0x2e,
102294904Sdelphij		0x03, 0x70, 0x73, 0x34
103284990Scy	};
104284990Scy
105284990Scy	const char *str = "[2001:0db8:85a3:08d3:1319:8a2e:0370:7334]:3000";
106284990Scy	sockaddr_u actual;
107284990Scy
108284990Scy	sockaddr_u expected;
109284990Scy	expected.sa6.sin6_family = AF_INET6;
110284990Scy	expected.sa6.sin6_addr = address;
111284990Scy	SET_PORT(&expected, 3000);
112284990Scy
113284990Scy	TEST_ASSERT_TRUE(decodenetnum(str, &actual));
114284990Scy	TEST_ASSERT_TRUE(IsEqual(expected, actual));
115289999Sglebius
116289999Sglebius#else
117294904Sdelphij
118289999Sglebius	TEST_IGNORE_MESSAGE("IPV6 disabled in build, skipping.");
119294904Sdelphij
120289999Sglebius#endif /* ISC_PLATFORM_HAVEIPV6 */
121284990Scy}
122284990Scy
123289999Sglebius
124289999Sglebiusvoid
125294904Sdelphijtest_IllegalAddress(void)
126294904Sdelphij{
127284990Scy	const char *str = "192.0.2.270:2000";
128284990Scy	sockaddr_u actual;
129284990Scy
130284990Scy	TEST_ASSERT_FALSE(decodenetnum(str, &actual));
131284990Scy}
132284990Scy
133294904Sdelphij
134289999Sglebiusvoid
135294904Sdelphijtest_IllegalCharInPort(void)
136294904Sdelphij{
137284990Scy	/* An illegal port does not make the decodenetnum fail, but instead
138284990Scy	 * makes it use the standard port.
139284990Scy	 */
140284990Scy	const char *str = "192.0.2.1:a700";
141284990Scy	sockaddr_u actual;
142284990Scy
143284990Scy	sockaddr_u expected;
144284990Scy	expected.sa4.sin_family = AF_INET;
145284990Scy	expected.sa4.sin_addr.s_addr = inet_addr("192.0.2.1");
146284990Scy	SET_PORT(&expected, NTP_PORT);
147284990Scy
148284990Scy	TEST_ASSERT_TRUE(decodenetnum(str, &actual));
149284990Scy	TEST_ASSERT_TRUE(IsEqual(expected, actual));
150284990Scy}
151