xref: /netbsd-src/external/bsd/ntp/dist/tests/libntp/socktoa.c (revision eabc0478de71e4e011a5b4e0392741e01d491794)
1 /*	$NetBSD: socktoa.c,v 1.3 2024/08/18 20:47:27 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 
12 void setUp(void);
13 void test_IPv4AddressWithPort(void);
14 void test_IPv6AddressWithPort(void);
15 void test_IgnoreIPv6Fields(void);
16 void test_ScopedIPv6AddressWithPort(void);
17 void test_HashEqual(void);
18 void test_HashNotEqual(void);
19 
20 
21 void
22 setUp(void)
23 {
24 	init_lib();
25 }
26 
27 
28 void
29 test_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 
38 void
39 test_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 	sockaddr_u input;
55 
56 	ZERO(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 
72 void
73 test_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 	sockaddr_u input;
89 
90 	ZERO(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 
106 void
107 test_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 
117 void
118 test_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 
129 void
130 test_IgnoreIPv6Fields(void)
131 {
132 #if defined(ISC_PLATFORM_HAVEIPV6) && defined(WANT_IPV6)
133 
134 	const struct in6_addr address = { { {
135 		0xfe, 0x80, 0x00, 0x00,
136 		0x00, 0x00, 0x00, 0x00,
137 		0x02, 0x12, 0x3f, 0xff,
138 		0xfe, 0x29, 0xff, 0xfa
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