1290001Sglebius#include "config.h"
2290001Sglebius
3290001Sglebius#include "ntp_stdlib.h"
4290001Sglebius#include "ntp_calendar.h"
5290001Sglebius
6290001Sglebius#include "unity.h"
7290001Sglebius#include "sockaddrtest.h"
8290001Sglebius
9290001Sglebius
10293896Sglebiusvoid setUp(void);
11290001Sglebiusvoid test_IPv4AddressWithPort(void);
12290001Sglebiusvoid test_IPv6AddressWithPort(void);
13290001Sglebiusvoid test_IgnoreIPv6Fields(void);
14290001Sglebiusvoid test_ScopedIPv6AddressWithPort(void);
15290001Sglebiusvoid test_HashEqual(void);
16290001Sglebiusvoid test_HashNotEqual(void);
17290001Sglebius
18293896Sglebius
19293896Sglebiusvoid
20293896SglebiussetUp(void)
21293896Sglebius{
22293896Sglebius	init_lib();
23293896Sglebius}
24293896Sglebius
25293896Sglebius
26290001Sglebiusvoid
27294905Sdelphijtest_IPv4AddressWithPort(void)
28294905Sdelphij{
29290001Sglebius	sockaddr_u input = CreateSockaddr4("192.0.2.10", 123);
30290001Sglebius
31290001Sglebius	TEST_ASSERT_EQUAL_STRING("192.0.2.10", socktoa(&input));
32290001Sglebius	TEST_ASSERT_EQUAL_STRING("192.0.2.10:123", sockporttoa(&input));
33290001Sglebius}
34290001Sglebius
35290001Sglebius
36290001Sglebiusvoid
37294905Sdelphijtest_IPv6AddressWithPort(void)
38294905Sdelphij{
39290001Sglebius#ifdef ISC_PLATFORM_WANTIPV6
40290001Sglebius
41290001Sglebius	const struct in6_addr address = {
42290001Sglebius		0x20, 0x01, 0x0d, 0xb8,
43290001Sglebius		0x85, 0xa3, 0x08, 0xd3,
44290001Sglebius		0x13, 0x19, 0x8a, 0x2e,
45290001Sglebius		0x03, 0x70, 0x73, 0x34
46290001Sglebius	};
47290001Sglebius
48290001Sglebius	const char* expected =
49290001Sglebius		"2001:db8:85a3:8d3:1319:8a2e:370:7334";
50290001Sglebius	const char* expected_port =
51290001Sglebius		"[2001:db8:85a3:8d3:1319:8a2e:370:7334]:123";
52290001Sglebius
53290001Sglebius	sockaddr_u input;
54290001Sglebius	memset(&input, 0, sizeof(input));
55290001Sglebius	AF(&input) = AF_INET6;
56290001Sglebius	SET_ADDR6N(&input, address);
57290001Sglebius	SET_PORT(&input, 123);
58290001Sglebius
59290001Sglebius	TEST_ASSERT_EQUAL_STRING(expected, socktoa(&input));
60290001Sglebius	TEST_ASSERT_EQUAL_STRING(expected_port, sockporttoa(&input));
61290001Sglebius
62290001Sglebius#else
63294905Sdelphij
64290001Sglebius	TEST_IGNORE_MESSAGE("IPV6 disabled in build, skipping.");
65290001Sglebius
66290001Sglebius#endif /* ISC_PLATFORM_HAVEIPV6 */
67290001Sglebius}
68290001Sglebius
69290001Sglebius
70290001Sglebiusvoid
71294905Sdelphijtest_ScopedIPv6AddressWithPort(void)
72294905Sdelphij{
73290001Sglebius#ifdef ISC_PLATFORM_HAVESCOPEID
74294905Sdelphij
75293896Sglebius	const struct in6_addr address = { { {
76290001Sglebius		0xfe, 0x80, 0x00, 0x00,
77290001Sglebius		0x00, 0x00, 0x00, 0x00,
78290001Sglebius		0x02, 0x12, 0x3f, 0xff,
79290001Sglebius		0xfe, 0x29, 0xff, 0xfa
80293896Sglebius	} } };
81290001Sglebius
82290001Sglebius	const char* expected =
83290001Sglebius		"fe80::212:3fff:fe29:fffa%5";
84290001Sglebius	const char* expected_port =
85290001Sglebius		"[fe80::212:3fff:fe29:fffa%5]:123";
86290001Sglebius
87290001Sglebius	sockaddr_u input;
88290001Sglebius	memset(&input, 0, sizeof(input));
89290001Sglebius	AF(&input) = AF_INET6;
90290001Sglebius	SET_ADDR6N(&input, address);
91290001Sglebius	SET_PORT(&input, 123);
92290001Sglebius	SCOPE_VAR(&input) = 5;
93290001Sglebius
94290001Sglebius	TEST_ASSERT_EQUAL_STRING(expected, socktoa(&input));
95290001Sglebius	TEST_ASSERT_EQUAL_STRING(expected_port, sockporttoa(&input));
96290001Sglebius#else
97294905Sdelphij
98290001Sglebius	TEST_IGNORE_MESSAGE("Skipping because ISC_PLATFORM does not have Scope ID");
99294905Sdelphij
100290001Sglebius#endif
101290001Sglebius}
102290001Sglebius
103294905Sdelphij
104290001Sglebiusvoid
105294905Sdelphijtest_HashEqual(void)
106294905Sdelphij{
107290001Sglebius	sockaddr_u input1 = CreateSockaddr4("192.00.2.2", 123);
108290001Sglebius	sockaddr_u input2 = CreateSockaddr4("192.0.2.2", 123);
109290001Sglebius
110290001Sglebius	TEST_ASSERT_TRUE(IsEqual(input1, input2));
111290001Sglebius	TEST_ASSERT_EQUAL(sock_hash(&input1), sock_hash(&input2));
112290001Sglebius}
113290001Sglebius
114294905Sdelphij
115290001Sglebiusvoid
116294905Sdelphijtest_HashNotEqual(void)
117294905Sdelphij{
118290001Sglebius	/* These two addresses should not generate the same hash. */
119290001Sglebius	sockaddr_u input1 = CreateSockaddr4("192.0.2.1", 123);
120290001Sglebius	sockaddr_u input2 = CreateSockaddr4("192.0.2.2", 123);
121290001Sglebius
122290001Sglebius	TEST_ASSERT_FALSE(IsEqual(input1, input2));
123290001Sglebius	TEST_ASSERT_FALSE(sock_hash(&input1) == sock_hash(&input2));
124290001Sglebius}
125290001Sglebius
126290001Sglebius
127290001Sglebiusvoid
128294905Sdelphijtest_IgnoreIPv6Fields(void)
129294905Sdelphij{
130290001Sglebius#ifdef ISC_PLATFORM_WANTIPV6
131290001Sglebius
132290001Sglebius	const struct in6_addr address = {
133290001Sglebius		0x20, 0x01, 0x0d, 0xb8,
134294905Sdelphij		0x85, 0xa3, 0x08, 0xd3,
135294905Sdelphij		0x13, 0x19, 0x8a, 0x2e,
136294905Sdelphij		0x03, 0x70, 0x73, 0x34
137290001Sglebius	};
138290001Sglebius
139290001Sglebius	sockaddr_u input1, input2;
140290001Sglebius
141290001Sglebius	input1.sa6.sin6_family = AF_INET6;
142290001Sglebius	input1.sa6.sin6_addr = address;
143290001Sglebius	input1.sa6.sin6_flowinfo = 30L; // This value differs from input2.
144290001Sglebius	SET_PORT(&input1, NTP_PORT);
145290001Sglebius
146290001Sglebius	input2.sa6.sin6_family = AF_INET6;
147290001Sglebius	input2.sa6.sin6_addr = address;
148290001Sglebius	input2.sa6.sin6_flowinfo = 10L; // This value differs from input1.
149290001Sglebius	SET_PORT(&input2, NTP_PORT);
150290001Sglebius
151290001Sglebius	TEST_ASSERT_EQUAL(sock_hash(&input1), sock_hash(&input2));
152290001Sglebius
153290001Sglebius#else
154294905Sdelphij
155290001Sglebius	TEST_IGNORE_MESSAGE("IPV6 disabled in build, skipping.");
156294905Sdelphij
157290001Sglebius#endif /* ISC_PLATFORM_HAVEIPV6 */
158290001Sglebius}
159