xref: /netbsd-src/external/bsd/ntp/dist/tests/libntp/decodenetnum.c (revision eabc0478de71e4e011a5b4e0392741e01d491794)
1 /*	$NetBSD: decodenetnum.c,v 1.4 2024/08/18 20:47:26 christos Exp $	*/
2 
3 #include "config.h"
4 #include "ntp_stdlib.h"
5 #include "sockaddrtest.h"
6 
7 #include "unity.h"
8 
9 void setUp(void);
10 extern void test_IPv4AddressOnly(void);
11 extern void test_IPv4AddressWithPort(void);
12 extern void test_IPv6AddressOnly(void);
13 extern void test_IPv6AddressWithPort(void);
14 extern void test_IPv6AddressWithScope(void);
15 extern void test_IPv6AddressWithPortAndScope(void);
16 extern void test_IllegalAddress(void);
17 extern void test_IllegalCharInPort(void);
18 extern void test_NameBufOverflow(void);
19 
20 /*
21  * NOTE: The IPv6 specific tests are reduced to stubs when IPv6 is
22  * disabled.
23  *
24  * ISC_PLATFORM_HAVEIPV6 checks if system has IPV6 capabilies. WANTIPV6
25  * ISC_PLATFORM_WANTIPV6 can be changed with build --disable-ipv6.
26  *
27  * If we want IPv6 but don't have it, the tests should fail, I think.
28  */
29 void
30 setUp(void)
31 {
32 	init_lib();
33 }
34 
35 
36 void
37 test_IPv4AddressOnly(void)
38 {
39 	const char *str = "192.0.2.1";
40 	sockaddr_u actual;
41 	sockaddr_u expected;
42 
43 	ZERO(expected);
44 	AF(&expected) = AF_INET;
45 	expected.sa4.sin_addr.s_addr = inet_addr("192.0.2.1");
46 	SET_PORT(&expected, NTP_PORT);
47 
48 	TEST_ASSERT_TRUE(decodenetnum(str, &actual));
49 	TEST_ASSERT_TRUE(IsEqual(expected, actual));
50 }
51 
52 void
53 test_IPv4AddressWithPort(void)
54 {
55 	const char *str = "192.0.2.2:2000";
56 	sockaddr_u actual;
57 	sockaddr_u expected;
58 
59 	ZERO(expected);
60 	AF(&expected) = AF_INET;
61 	expected.sa4.sin_addr.s_addr = inet_addr("192.0.2.2");
62 	SET_PORT(&expected, 2000);
63 
64 	TEST_ASSERT_TRUE(decodenetnum(str, &actual));
65 	TEST_ASSERT_TRUE(IsEqual(expected, actual));
66 }
67 
68 
69 void
70 test_IPv6AddressOnly(void)
71 {
72 #if defined(ISC_PLATFORM_HAVEIPV6) && defined(WANT_IPV6)
73 
74 	const struct in6_addr address = { { {
75 		0x20, 0x01, 0x0d, 0xb8,
76 		0x85, 0xa3, 0x08, 0xd3,
77 		0x13, 0x19, 0x8a, 0x2e,
78 		0x03, 0x70, 0x73, 0x34
79 	} } };
80 
81 	const char *str1 = "2001:db8:85a3:08d3:1319:8a2e:0370:7334";
82 	const char *str2 = "[2001:0db8:85a3:8d3:1319:8a2e:370:7334]";
83 	sockaddr_u actual;
84 	sockaddr_u expected;
85 
86 	ZERO(expected);
87 	AF(&expected) = AF_INET6;
88 	SET_ADDR6N(&expected, address);
89 	SET_PORT(&expected, NTP_PORT);
90 
91 	TEST_ASSERT_TRUE(decodenetnum(str1, &actual));
92 	TEST_ASSERT_TRUE(IsEqual(expected, actual));
93 
94 	TEST_ASSERT_TRUE(decodenetnum(str2, &actual));
95 	TEST_ASSERT_TRUE(IsEqual(expected, actual));
96 
97 #else
98 
99 	TEST_IGNORE_MESSAGE("IPV6 disabled in build");
100 
101 #endif
102 }
103 
104 
105 void
106 test_IPv6AddressWithPort(void)
107 {
108 #if defined(ISC_PLATFORM_HAVEIPV6) && defined(WANT_IPV6)
109 
110 	const struct in6_addr address = { { {
111 		0x20, 0x01, 0x0d, 0xb8,
112 		0x85, 0xa3, 0x08, 0xd3,
113 		0x13, 0x19, 0x8a, 0x2e,
114 		0x03, 0x70, 0x73, 0x34
115 	} } };
116 
117 	const char *str = "[2001:0db8:85a3:08d3:1319:8a2e:0370:7334]:3000";
118 	sockaddr_u actual;
119 	sockaddr_u expected;
120 
121 	ZERO(expected);
122 	AF(&expected) = AF_INET6;
123 	SET_ADDR6N(&expected, address);
124 	SET_PORT(&expected, 3000);
125 
126 	TEST_ASSERT_TRUE(decodenetnum(str, &actual));
127 	TEST_ASSERT_TRUE(IsEqual(expected, actual));
128 
129 #else
130 
131 	TEST_IGNORE_MESSAGE("IPV6 disabled in build");
132 
133 #endif
134 }
135 
136 void test_IPv6AddressWithScope(void)
137 {
138 #if defined(ISC_PLATFORM_HAVEIPV6) && defined(WANT_IPV6)
139 
140 	const struct in6_addr address = { { {
141 		0x20, 0x01, 0x0d, 0xb8,
142 		0x85, 0xa3, 0x08, 0xd3,
143 		0x13, 0x19, 0x8a, 0x2e,
144 		0x03, 0x70, 0x73, 0x34
145 	} } };
146 
147 	const char *str1 = "2001:db8:85a3:8d3:1319:8a2e:370:7334%42";
148 	const char *str2 = "[2001:0db8:85a3:08d3:1319:8a2e:0370:7334%42]";
149 	sockaddr_u actual;
150 	sockaddr_u expected;
151 
152 	ZERO(expected);
153 	AF(&expected) = AF_INET6;
154 	SET_ADDR6N(&expected, address);
155 	SET_SCOPE(&expected, 42);
156 	SET_PORT(&expected, NTP_PORT);
157 
158 	TEST_ASSERT_TRUE(decodenetnum(str1, &actual));
159 	TEST_ASSERT_TRUE(IsEqual(expected, actual));
160 
161 	TEST_ASSERT_TRUE(decodenetnum(str2, &actual));
162 	TEST_ASSERT_TRUE(IsEqual(expected, actual));
163 
164 #else
165 
166 	TEST_IGNORE_MESSAGE("IPV6 disabled in build");
167 
168 #endif
169 }
170 
171 void test_IPv6AddressWithPortAndScope(void)
172 {
173 #if defined(ISC_PLATFORM_HAVEIPV6) && defined(WANT_IPV6)
174 
175 	const struct in6_addr address = { { {
176 		0x20, 0x01, 0x0d, 0xb8,
177 		0x85, 0xa3, 0x08, 0xd3,
178 		0x13, 0x19, 0x8a, 0x2e,
179 		0x03, 0x70, 0x73, 0x34
180 	} } };
181 
182 	const char *str = "[2001:db8:85a3:08d3:1319:8a2e:370:7334%42]:3000";
183 	sockaddr_u actual;
184 	sockaddr_u expected;
185 
186 	ZERO(expected);
187 	AF(&expected) = AF_INET6;
188 	SET_ADDR6N(&expected, address);
189 	SET_SCOPE(&expected, 42);
190 	SET_PORT(&expected, 3000);
191 
192 	TEST_ASSERT_TRUE(decodenetnum(str, &actual));
193 	TEST_ASSERT_TRUE(IsEqual(expected, actual));
194 
195 #else
196 
197 	TEST_IGNORE_MESSAGE("IPV6 disabled in build");
198 
199 #endif
200 }
201 
202 void
203 test_IllegalAddress(void)
204 {
205 	const char *str = "192.0.2.270:2000";
206 	sockaddr_u actual;
207 
208 	TEST_ASSERT_FALSE(decodenetnum(str, &actual));
209 }
210 
211 
212 void
213 test_IllegalCharInPort(void)
214 {
215 	/* An illegal port does not make the decodenetnum fail, but instead
216 	 * makes it use the standard port.
217 	 */
218 	const char *str = "192.0.2.1:a700";
219 	sockaddr_u actual;
220 	sockaddr_u expected;
221 
222 	ZERO(expected);
223 	AF(&expected) = AF_INET;
224 	expected.sa4.sin_addr.s_addr = inet_addr("192.0.2.1");
225 	SET_PORT(&expected, NTP_PORT);
226 
227 	TEST_ASSERT_TRUE(decodenetnum(str, &actual));
228 	TEST_ASSERT_TRUE(IsEqual(expected, actual));
229 }
230 
231 void
232 test_NameBufOverflow(void)
233 {
234 	const char *str =
235 	    "loremipsumloremipsumloremipsumloremipsumloremipsum"
236 	    "loremipsumloremipsumloremipsumloremipsum";
237 	sockaddr_u actual;
238 
239 	TEST_ASSERT_FALSE(decodenetnum(str, &actual));
240 }
241