xref: /netbsd-src/external/bsd/ntp/dist/tests/libntp/decodenetnum.c (revision eabc0478de71e4e011a5b4e0392741e01d491794)
1*eabc0478Schristos /*	$NetBSD: decodenetnum.c,v 1.4 2024/08/18 20:47:26 christos Exp $	*/
2067f5680Schristos 
3f17b710fSchristos #include "config.h"
4f17b710fSchristos #include "ntp_stdlib.h"
5f17b710fSchristos #include "sockaddrtest.h"
6f17b710fSchristos 
7a6f3f22fSchristos #include "unity.h"
8f17b710fSchristos 
94c290c01Schristos void setUp(void);
10a6f3f22fSchristos extern void test_IPv4AddressOnly(void);
11a6f3f22fSchristos extern void test_IPv4AddressWithPort(void);
12a6f3f22fSchristos extern void test_IPv6AddressOnly(void);
13a6f3f22fSchristos extern void test_IPv6AddressWithPort(void);
1450c1baceSchristos extern void test_IPv6AddressWithScope(void);
1550c1baceSchristos extern void test_IPv6AddressWithPortAndScope(void);
16a6f3f22fSchristos extern void test_IllegalAddress(void);
17a6f3f22fSchristos extern void test_IllegalCharInPort(void);
1850c1baceSchristos extern void test_NameBufOverflow(void);
19a6f3f22fSchristos 
20ae49d4a4Schristos /*
21ae49d4a4Schristos  * NOTE: The IPv6 specific tests are reduced to stubs when IPv6 is
22ae49d4a4Schristos  * disabled.
23ae49d4a4Schristos  *
24ae49d4a4Schristos  * ISC_PLATFORM_HAVEIPV6 checks if system has IPV6 capabilies. WANTIPV6
25ae49d4a4Schristos  * ISC_PLATFORM_WANTIPV6 can be changed with build --disable-ipv6.
26ae49d4a4Schristos  *
27ae49d4a4Schristos  * If we want IPv6 but don't have it, the tests should fail, I think.
28ae49d4a4Schristos  */
29a6f3f22fSchristos void
304c290c01Schristos setUp(void)
314c290c01Schristos {
324c290c01Schristos 	init_lib();
334c290c01Schristos }
344c290c01Schristos 
354c290c01Schristos 
364c290c01Schristos void
37ae49d4a4Schristos test_IPv4AddressOnly(void)
38ae49d4a4Schristos {
39f17b710fSchristos 	const char *str = "192.0.2.1";
40f17b710fSchristos 	sockaddr_u actual;
41f17b710fSchristos 	sockaddr_u expected;
42*eabc0478Schristos 
43*eabc0478Schristos 	ZERO(expected);
44*eabc0478Schristos 	AF(&expected) = AF_INET;
45f17b710fSchristos 	expected.sa4.sin_addr.s_addr = inet_addr("192.0.2.1");
46f17b710fSchristos 	SET_PORT(&expected, NTP_PORT);
47f17b710fSchristos 
48f17b710fSchristos 	TEST_ASSERT_TRUE(decodenetnum(str, &actual));
49f17b710fSchristos 	TEST_ASSERT_TRUE(IsEqual(expected, actual));
50f17b710fSchristos }
51f17b710fSchristos 
52a6f3f22fSchristos void
53ae49d4a4Schristos test_IPv4AddressWithPort(void)
54ae49d4a4Schristos {
55f17b710fSchristos 	const char *str = "192.0.2.2:2000";
56f17b710fSchristos 	sockaddr_u actual;
57f17b710fSchristos 	sockaddr_u expected;
58*eabc0478Schristos 
59*eabc0478Schristos 	ZERO(expected);
60*eabc0478Schristos 	AF(&expected) = AF_INET;
61f17b710fSchristos 	expected.sa4.sin_addr.s_addr = inet_addr("192.0.2.2");
62f17b710fSchristos 	SET_PORT(&expected, 2000);
63f17b710fSchristos 
64f17b710fSchristos 	TEST_ASSERT_TRUE(decodenetnum(str, &actual));
65f17b710fSchristos 	TEST_ASSERT_TRUE(IsEqual(expected, actual));
66f17b710fSchristos }
67f17b710fSchristos 
68a6f3f22fSchristos 
69a6f3f22fSchristos void
70ae49d4a4Schristos test_IPv6AddressOnly(void)
71ae49d4a4Schristos {
72067f5680Schristos #if defined(ISC_PLATFORM_HAVEIPV6) && defined(WANT_IPV6)
73ae49d4a4Schristos 
74*eabc0478Schristos 	const struct in6_addr address = { { {
75f17b710fSchristos 		0x20, 0x01, 0x0d, 0xb8,
76f17b710fSchristos 		0x85, 0xa3, 0x08, 0xd3,
77f17b710fSchristos 		0x13, 0x19, 0x8a, 0x2e,
78f17b710fSchristos 		0x03, 0x70, 0x73, 0x34
79*eabc0478Schristos 	} } };
80f17b710fSchristos 
81*eabc0478Schristos 	const char *str1 = "2001:db8:85a3:08d3:1319:8a2e:0370:7334";
82*eabc0478Schristos 	const char *str2 = "[2001:0db8:85a3:8d3:1319:8a2e:370:7334]";
83f17b710fSchristos 	sockaddr_u actual;
84f17b710fSchristos 	sockaddr_u expected;
85*eabc0478Schristos 
86*eabc0478Schristos 	ZERO(expected);
87*eabc0478Schristos 	AF(&expected) = AF_INET6;
88*eabc0478Schristos 	SET_ADDR6N(&expected, address);
89f17b710fSchristos 	SET_PORT(&expected, NTP_PORT);
90f17b710fSchristos 
9150c1baceSchristos 	TEST_ASSERT_TRUE(decodenetnum(str1, &actual));
9250c1baceSchristos 	TEST_ASSERT_TRUE(IsEqual(expected, actual));
9350c1baceSchristos 
9450c1baceSchristos 	TEST_ASSERT_TRUE(decodenetnum(str2, &actual));
95f17b710fSchristos 	TEST_ASSERT_TRUE(IsEqual(expected, actual));
96a6f3f22fSchristos 
97a6f3f22fSchristos #else
98ae49d4a4Schristos 
99067f5680Schristos 	TEST_IGNORE_MESSAGE("IPV6 disabled in build");
100ae49d4a4Schristos 
101067f5680Schristos #endif
102f17b710fSchristos }
103f17b710fSchristos 
104a6f3f22fSchristos 
105a6f3f22fSchristos void
106ae49d4a4Schristos test_IPv6AddressWithPort(void)
107ae49d4a4Schristos {
108067f5680Schristos #if defined(ISC_PLATFORM_HAVEIPV6) && defined(WANT_IPV6)
109a6f3f22fSchristos 
110*eabc0478Schristos 	const struct in6_addr address = { { {
111f17b710fSchristos 		0x20, 0x01, 0x0d, 0xb8,
112f17b710fSchristos 		0x85, 0xa3, 0x08, 0xd3,
113f17b710fSchristos 		0x13, 0x19, 0x8a, 0x2e,
114f17b710fSchristos 		0x03, 0x70, 0x73, 0x34
115*eabc0478Schristos 	} } };
116f17b710fSchristos 
117f17b710fSchristos 	const char *str = "[2001:0db8:85a3:08d3:1319:8a2e:0370:7334]:3000";
118f17b710fSchristos 	sockaddr_u actual;
119f17b710fSchristos 	sockaddr_u expected;
120*eabc0478Schristos 
121*eabc0478Schristos 	ZERO(expected);
122*eabc0478Schristos 	AF(&expected) = AF_INET6;
123*eabc0478Schristos 	SET_ADDR6N(&expected, address);
124f17b710fSchristos 	SET_PORT(&expected, 3000);
125f17b710fSchristos 
126f17b710fSchristos 	TEST_ASSERT_TRUE(decodenetnum(str, &actual));
127f17b710fSchristos 	TEST_ASSERT_TRUE(IsEqual(expected, actual));
128a6f3f22fSchristos 
129a6f3f22fSchristos #else
130ae49d4a4Schristos 
131067f5680Schristos 	TEST_IGNORE_MESSAGE("IPV6 disabled in build");
132ae49d4a4Schristos 
133067f5680Schristos #endif
134f17b710fSchristos }
135f17b710fSchristos 
13650c1baceSchristos void test_IPv6AddressWithScope(void)
13750c1baceSchristos {
13850c1baceSchristos #if defined(ISC_PLATFORM_HAVEIPV6) && defined(WANT_IPV6)
13950c1baceSchristos 
140*eabc0478Schristos 	const struct in6_addr address = { { {
14150c1baceSchristos 		0x20, 0x01, 0x0d, 0xb8,
14250c1baceSchristos 		0x85, 0xa3, 0x08, 0xd3,
14350c1baceSchristos 		0x13, 0x19, 0x8a, 0x2e,
14450c1baceSchristos 		0x03, 0x70, 0x73, 0x34
145*eabc0478Schristos 	} } };
14650c1baceSchristos 
147*eabc0478Schristos 	const char *str1 = "2001:db8:85a3:8d3:1319:8a2e:370:7334%42";
14850c1baceSchristos 	const char *str2 = "[2001:0db8:85a3:08d3:1319:8a2e:0370:7334%42]";
14950c1baceSchristos 	sockaddr_u actual;
15050c1baceSchristos 	sockaddr_u expected;
151*eabc0478Schristos 
152*eabc0478Schristos 	ZERO(expected);
153*eabc0478Schristos 	AF(&expected) = AF_INET6;
154*eabc0478Schristos 	SET_ADDR6N(&expected, address);
155*eabc0478Schristos 	SET_SCOPE(&expected, 42);
15650c1baceSchristos 	SET_PORT(&expected, NTP_PORT);
15750c1baceSchristos 
15850c1baceSchristos 	TEST_ASSERT_TRUE(decodenetnum(str1, &actual));
15950c1baceSchristos 	TEST_ASSERT_TRUE(IsEqual(expected, actual));
16050c1baceSchristos 
16150c1baceSchristos 	TEST_ASSERT_TRUE(decodenetnum(str2, &actual));
16250c1baceSchristos 	TEST_ASSERT_TRUE(IsEqual(expected, actual));
16350c1baceSchristos 
16450c1baceSchristos #else
16550c1baceSchristos 
16650c1baceSchristos 	TEST_IGNORE_MESSAGE("IPV6 disabled in build");
16750c1baceSchristos 
16850c1baceSchristos #endif
16950c1baceSchristos }
17050c1baceSchristos 
17150c1baceSchristos void test_IPv6AddressWithPortAndScope(void)
17250c1baceSchristos {
17350c1baceSchristos #if defined(ISC_PLATFORM_HAVEIPV6) && defined(WANT_IPV6)
17450c1baceSchristos 
175*eabc0478Schristos 	const struct in6_addr address = { { {
17650c1baceSchristos 		0x20, 0x01, 0x0d, 0xb8,
17750c1baceSchristos 		0x85, 0xa3, 0x08, 0xd3,
17850c1baceSchristos 		0x13, 0x19, 0x8a, 0x2e,
17950c1baceSchristos 		0x03, 0x70, 0x73, 0x34
180*eabc0478Schristos 	} } };
18150c1baceSchristos 
182*eabc0478Schristos 	const char *str = "[2001:db8:85a3:08d3:1319:8a2e:370:7334%42]:3000";
18350c1baceSchristos 	sockaddr_u actual;
18450c1baceSchristos 	sockaddr_u expected;
185*eabc0478Schristos 
186*eabc0478Schristos 	ZERO(expected);
187*eabc0478Schristos 	AF(&expected) = AF_INET6;
188*eabc0478Schristos 	SET_ADDR6N(&expected, address);
189*eabc0478Schristos 	SET_SCOPE(&expected, 42);
19050c1baceSchristos 	SET_PORT(&expected, 3000);
19150c1baceSchristos 
19250c1baceSchristos 	TEST_ASSERT_TRUE(decodenetnum(str, &actual));
19350c1baceSchristos 	TEST_ASSERT_TRUE(IsEqual(expected, actual));
19450c1baceSchristos 
19550c1baceSchristos #else
19650c1baceSchristos 
19750c1baceSchristos 	TEST_IGNORE_MESSAGE("IPV6 disabled in build");
19850c1baceSchristos 
19950c1baceSchristos #endif
20050c1baceSchristos }
201a6f3f22fSchristos 
202a6f3f22fSchristos void
203ae49d4a4Schristos test_IllegalAddress(void)
204ae49d4a4Schristos {
205f17b710fSchristos 	const char *str = "192.0.2.270:2000";
206f17b710fSchristos 	sockaddr_u actual;
207f17b710fSchristos 
208f17b710fSchristos 	TEST_ASSERT_FALSE(decodenetnum(str, &actual));
209f17b710fSchristos }
210f17b710fSchristos 
211ae49d4a4Schristos 
212a6f3f22fSchristos void
213ae49d4a4Schristos test_IllegalCharInPort(void)
214ae49d4a4Schristos {
215f17b710fSchristos 	/* An illegal port does not make the decodenetnum fail, but instead
216f17b710fSchristos 	 * makes it use the standard port.
217f17b710fSchristos 	 */
218f17b710fSchristos 	const char *str = "192.0.2.1:a700";
219f17b710fSchristos 	sockaddr_u actual;
220f17b710fSchristos 	sockaddr_u expected;
221*eabc0478Schristos 
222*eabc0478Schristos 	ZERO(expected);
223*eabc0478Schristos 	AF(&expected) = AF_INET;
224f17b710fSchristos 	expected.sa4.sin_addr.s_addr = inet_addr("192.0.2.1");
225f17b710fSchristos 	SET_PORT(&expected, NTP_PORT);
226f17b710fSchristos 
227f17b710fSchristos 	TEST_ASSERT_TRUE(decodenetnum(str, &actual));
228f17b710fSchristos 	TEST_ASSERT_TRUE(IsEqual(expected, actual));
229f17b710fSchristos }
23050c1baceSchristos 
23150c1baceSchristos void
23250c1baceSchristos test_NameBufOverflow(void)
23350c1baceSchristos {
23450c1baceSchristos 	const char *str =
23550c1baceSchristos 	    "loremipsumloremipsumloremipsumloremipsumloremipsum"
23650c1baceSchristos 	    "loremipsumloremipsumloremipsumloremipsum";
23750c1baceSchristos 	sockaddr_u actual;
238*eabc0478Schristos 
23950c1baceSchristos 	TEST_ASSERT_FALSE(decodenetnum(str, &actual));
24050c1baceSchristos }
241