1/*	$NetBSD: socktoa.c,v 1.2 2020/05/25 20:47:36 christos Exp $	*/
2
3#include "config.h"
4
5#include "ntp_stdlib.h"
6#include "ntp_calendar.h"
7
8#include "unity.h"
9#include "sockaddrtest.h"
10
11
12void setUp(void);
13void test_IPv4AddressWithPort(void);
14void test_IPv6AddressWithPort(void);
15void test_IgnoreIPv6Fields(void);
16void test_ScopedIPv6AddressWithPort(void);
17void test_HashEqual(void);
18void test_HashNotEqual(void);
19
20
21void
22setUp(void)
23{
24	init_lib();
25}
26
27
28void
29test_IPv4AddressWithPort(void)
30{
31	sockaddr_u input = CreateSockaddr4("192.0.2.10", 123);
32
33	TEST_ASSERT_EQUAL_STRING("192.0.2.10", socktoa(&input));
34	TEST_ASSERT_EQUAL_STRING("192.0.2.10:123", sockporttoa(&input));
35}
36
37
38void
39test_IPv6AddressWithPort(void)
40{
41#if defined(ISC_PLATFORM_HAVEIPV6) && defined(WANT_IPV6)
42
43	const struct in6_addr address = {
44		0x20, 0x01, 0x0d, 0xb8,
45		0x85, 0xa3, 0x08, 0xd3,
46		0x13, 0x19, 0x8a, 0x2e,
47		0x03, 0x70, 0x73, 0x34
48	};
49
50	const char* expected =
51		"2001:db8:85a3:8d3:1319:8a2e:370:7334";
52	const char* expected_port =
53		"[2001:db8:85a3:8d3:1319:8a2e:370:7334]:123";
54
55	sockaddr_u input;
56	memset(&input, 0, sizeof(input));
57	AF(&input) = AF_INET6;
58	SET_ADDR6N(&input, address);
59	SET_PORT(&input, 123);
60
61	TEST_ASSERT_EQUAL_STRING(expected, socktoa(&input));
62	TEST_ASSERT_EQUAL_STRING(expected_port, sockporttoa(&input));
63
64#else
65
66	TEST_IGNORE_MESSAGE("IPV6 disabled in build");
67
68#endif
69}
70
71
72void
73test_ScopedIPv6AddressWithPort(void)
74{
75#if defined(ISC_PLATFORM_HAVESCOPEID) && defined(WANT_IPV6)
76
77	const struct in6_addr address = { { {
78		0xfe, 0x80, 0x00, 0x00,
79		0x00, 0x00, 0x00, 0x00,
80		0x02, 0x12, 0x3f, 0xff,
81		0xfe, 0x29, 0xff, 0xfa
82	} } };
83
84	const char* expected =
85		"fe80::212:3fff:fe29:fffa%5";
86	const char* expected_port =
87		"[fe80::212:3fff:fe29:fffa%5]:123";
88
89	sockaddr_u input;
90	memset(&input, 0, sizeof(input));
91	AF(&input) = AF_INET6;
92	SET_ADDR6N(&input, address);
93	SET_PORT(&input, 123);
94	SCOPE_VAR(&input) = 5;
95
96	TEST_ASSERT_EQUAL_STRING(expected, socktoa(&input));
97	TEST_ASSERT_EQUAL_STRING(expected_port, sockporttoa(&input));
98#else
99
100	TEST_IGNORE_MESSAGE("IPV6 scopes unavailable or IPV6 disabled in build");
101
102#endif
103}
104
105
106void
107test_HashEqual(void)
108{
109	sockaddr_u input1 = CreateSockaddr4("192.00.2.2", 123);
110	sockaddr_u input2 = CreateSockaddr4("192.0.2.2", 123);
111
112	TEST_ASSERT_TRUE(IsEqual(input1, input2));
113	TEST_ASSERT_EQUAL(sock_hash(&input1), sock_hash(&input2));
114}
115
116
117void
118test_HashNotEqual(void)
119{
120	/* These two addresses should not generate the same hash. */
121	sockaddr_u input1 = CreateSockaddr4("192.0.2.1", 123);
122	sockaddr_u input2 = CreateSockaddr4("192.0.2.2", 123);
123
124	TEST_ASSERT_FALSE(IsEqual(input1, input2));
125	TEST_ASSERT_FALSE(sock_hash(&input1) == sock_hash(&input2));
126}
127
128
129void
130test_IgnoreIPv6Fields(void)
131{
132#if defined(ISC_PLATFORM_HAVEIPV6) && defined(WANT_IPV6)
133
134	const struct in6_addr address = {
135		0x20, 0x01, 0x0d, 0xb8,
136		0x85, 0xa3, 0x08, 0xd3,
137		0x13, 0x19, 0x8a, 0x2e,
138		0x03, 0x70, 0x73, 0x34
139	};
140
141	sockaddr_u input1, input2;
142
143	input1.sa6.sin6_family = AF_INET6;
144	input1.sa6.sin6_addr = address;
145	input1.sa6.sin6_flowinfo = 30L; // This value differs from input2.
146	SET_PORT(&input1, NTP_PORT);
147
148	input2.sa6.sin6_family = AF_INET6;
149	input2.sa6.sin6_addr = address;
150	input2.sa6.sin6_flowinfo = 10L; // This value differs from input1.
151	SET_PORT(&input2, NTP_PORT);
152
153	TEST_ASSERT_EQUAL(sock_hash(&input1), sock_hash(&input2));
154
155#else
156
157	TEST_IGNORE_MESSAGE("IPV6 disabled in build");
158
159#endif
160}
161