xref: /NextBSD/contrib/ntp/tests/libntp/decodenetnum.c (revision ea41b2069698db5cfd8b1d3888556269b4fdc668)
1 #include "config.h"
2 #include "ntp_stdlib.h"
3 #include "sockaddrtest.h"
4 
5 #include "unity.h"
6 
7 void setUp(void);
8 extern void test_IPv4AddressOnly(void);
9 extern void test_IPv4AddressWithPort(void);
10 //#ifdef ISC_PLATFORM_HAVEIPV6
11 extern void test_IPv6AddressOnly(void);
12 extern void test_IPv6AddressWithPort(void);
13 //#endif /* ISC_PLATFORM_HAVEIPV6 */
14 extern void test_IllegalAddress(void);
15 extern void test_IllegalCharInPort(void);
16 
17 
18 void
setUp(void)19 setUp(void)
20 {
21 	init_lib();
22 
23 	return;
24 }
25 
26 
27 void
test_IPv4AddressOnly(void)28 test_IPv4AddressOnly(void) {
29 	const char *str = "192.0.2.1";
30 	sockaddr_u actual;
31 
32 	sockaddr_u expected;
33 	expected.sa4.sin_family = AF_INET;
34 	expected.sa4.sin_addr.s_addr = inet_addr("192.0.2.1");
35 	SET_PORT(&expected, NTP_PORT);
36 
37 	TEST_ASSERT_TRUE(decodenetnum(str, &actual));
38 	TEST_ASSERT_TRUE(IsEqual(expected, actual));
39 }
40 
41 void
test_IPv4AddressWithPort(void)42 test_IPv4AddressWithPort(void) {
43 	const char *str = "192.0.2.2:2000";
44 	sockaddr_u actual;
45 
46 	sockaddr_u expected;
47 	expected.sa4.sin_family = AF_INET;
48 	expected.sa4.sin_addr.s_addr = inet_addr("192.0.2.2");
49 	SET_PORT(&expected, 2000);
50 
51 	TEST_ASSERT_TRUE(decodenetnum(str, &actual));
52 	TEST_ASSERT_TRUE(IsEqual(expected, actual));
53 }
54 
55 
56 void
test_IPv6AddressOnly(void)57 test_IPv6AddressOnly(void) {
58 
59 //#ifdef ISC_PLATFORM_HAVEIPV6 //looks like HAVEIPV6 checks if system has IPV6 capabilies. WANTIPV6 can be changed with build --disable-ipv6
60 #ifdef ISC_PLATFORM_WANTIPV6
61 	const struct in6_addr address = {
62 		0x20, 0x01, 0x0d, 0xb8,
63         0x85, 0xa3, 0x08, 0xd3,
64         0x13, 0x19, 0x8a, 0x2e,
65         0x03, 0x70, 0x73, 0x34
66 	};
67 
68 	const char *str = "2001:0db8:85a3:08d3:1319:8a2e:0370:7334";
69 	sockaddr_u actual;
70 
71 	sockaddr_u expected;
72 	expected.sa6.sin6_family = AF_INET6;
73 	expected.sa6.sin6_addr = address;
74 	SET_PORT(&expected, NTP_PORT);
75 
76 	TEST_ASSERT_TRUE(decodenetnum(str, &actual));
77 	TEST_ASSERT_TRUE(IsEqual(expected, actual));
78 
79 #else
80 	TEST_IGNORE_MESSAGE("IPV6 disabled in build, skipping.");
81 #endif /* ISC_PLATFORM_HAVEIPV6 */
82 
83 
84 }
85 
86 
87 
88 void
test_IPv6AddressWithPort(void)89 test_IPv6AddressWithPort(void) {
90 
91 #ifdef ISC_PLATFORM_WANTIPV6
92 
93 	const struct in6_addr address = {
94 		0x20, 0x01, 0x0d, 0xb8,
95         0x85, 0xa3, 0x08, 0xd3,
96         0x13, 0x19, 0x8a, 0x2e,
97         0x03, 0x70, 0x73, 0x34
98 	};
99 
100 	const char *str = "[2001:0db8:85a3:08d3:1319:8a2e:0370:7334]:3000";
101 	sockaddr_u actual;
102 
103 	sockaddr_u expected;
104 	expected.sa6.sin6_family = AF_INET6;
105 	expected.sa6.sin6_addr = address;
106 	SET_PORT(&expected, 3000);
107 
108 	TEST_ASSERT_TRUE(decodenetnum(str, &actual));
109 	TEST_ASSERT_TRUE(IsEqual(expected, actual));
110 
111 #else
112 	TEST_IGNORE_MESSAGE("IPV6 disabled in build, skipping.");
113 #endif /* ISC_PLATFORM_HAVEIPV6 */
114 }
115 
116 
117 void
test_IllegalAddress(void)118 test_IllegalAddress(void) {
119 	const char *str = "192.0.2.270:2000";
120 	sockaddr_u actual;
121 
122 	TEST_ASSERT_FALSE(decodenetnum(str, &actual));
123 }
124 
125 void
test_IllegalCharInPort(void)126 test_IllegalCharInPort(void) {
127 	/* An illegal port does not make the decodenetnum fail, but instead
128 	 * makes it use the standard port.
129 	 */
130 	const char *str = "192.0.2.1:a700";
131 	sockaddr_u actual;
132 
133 	sockaddr_u expected;
134 	expected.sa4.sin_family = AF_INET;
135 	expected.sa4.sin_addr.s_addr = inet_addr("192.0.2.1");
136 	SET_PORT(&expected, NTP_PORT);
137 
138 	TEST_ASSERT_TRUE(decodenetnum(str, &actual));
139 	TEST_ASSERT_TRUE(IsEqual(expected, actual));
140 }
141