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