2 #include "ntp_stdlib.h"
3 #include "sockaddrtest.h"
8 extern void test_IPv4AddressOnly(void);
9 extern void test_IPv4AddressWithPort(void);
10 extern void test_IPv6AddressOnly(void);
11 extern void test_IPv6AddressWithPort(void);
12 extern void test_IllegalAddress(void);
13 extern void test_IllegalCharInPort(void);
16 * NOTE: The IPv6 specific tests are reduced to stubs when IPv6 is
19 * ISC_PLATFORM_HAVEIPV6 checks if system has IPV6 capabilies. WANTIPV6
20 * ISC_PLATFORM_WANTIPV6 can be changed with build --disable-ipv6.
22 * If we want IPv6 but don't have it, the tests should fail, I think.
32 test_IPv4AddressOnly(void)
34 const char *str = "192.0.2.1";
38 expected.sa4.sin_family = AF_INET;
39 expected.sa4.sin_addr.s_addr = inet_addr("192.0.2.1");
40 SET_PORT(&expected, NTP_PORT);
42 TEST_ASSERT_TRUE(decodenetnum(str, &actual));
43 TEST_ASSERT_TRUE(IsEqual(expected, actual));
47 test_IPv4AddressWithPort(void)
49 const char *str = "192.0.2.2:2000";
53 expected.sa4.sin_family = AF_INET;
54 expected.sa4.sin_addr.s_addr = inet_addr("192.0.2.2");
55 SET_PORT(&expected, 2000);
57 TEST_ASSERT_TRUE(decodenetnum(str, &actual));
58 TEST_ASSERT_TRUE(IsEqual(expected, actual));
63 test_IPv6AddressOnly(void)
65 #ifdef ISC_PLATFORM_WANTIPV6
67 const struct in6_addr address = {
68 0x20, 0x01, 0x0d, 0xb8,
69 0x85, 0xa3, 0x08, 0xd3,
70 0x13, 0x19, 0x8a, 0x2e,
71 0x03, 0x70, 0x73, 0x34
74 const char *str = "2001:0db8:85a3:08d3:1319:8a2e:0370:7334";
78 expected.sa6.sin6_family = AF_INET6;
79 expected.sa6.sin6_addr = address;
80 SET_PORT(&expected, NTP_PORT);
82 TEST_ASSERT_TRUE(decodenetnum(str, &actual));
83 TEST_ASSERT_TRUE(IsEqual(expected, actual));
87 TEST_IGNORE_MESSAGE("IPV6 disabled in build, skipping.");
89 #endif /* ISC_PLATFORM_HAVEIPV6 */
94 test_IPv6AddressWithPort(void)
96 #ifdef ISC_PLATFORM_WANTIPV6
98 const struct in6_addr address = {
99 0x20, 0x01, 0x0d, 0xb8,
100 0x85, 0xa3, 0x08, 0xd3,
101 0x13, 0x19, 0x8a, 0x2e,
102 0x03, 0x70, 0x73, 0x34
105 const char *str = "[2001:0db8:85a3:08d3:1319:8a2e:0370:7334]:3000";
109 expected.sa6.sin6_family = AF_INET6;
110 expected.sa6.sin6_addr = address;
111 SET_PORT(&expected, 3000);
113 TEST_ASSERT_TRUE(decodenetnum(str, &actual));
114 TEST_ASSERT_TRUE(IsEqual(expected, actual));
118 TEST_IGNORE_MESSAGE("IPV6 disabled in build, skipping.");
120 #endif /* ISC_PLATFORM_HAVEIPV6 */
125 test_IllegalAddress(void)
127 const char *str = "192.0.2.270:2000";
130 TEST_ASSERT_FALSE(decodenetnum(str, &actual));
135 test_IllegalCharInPort(void)
137 /* An illegal port does not make the decodenetnum fail, but instead
138 * makes it use the standard port.
140 const char *str = "192.0.2.1:a700";
144 expected.sa4.sin_family = AF_INET;
145 expected.sa4.sin_addr.s_addr = inet_addr("192.0.2.1");
146 SET_PORT(&expected, NTP_PORT);
148 TEST_ASSERT_TRUE(decodenetnum(str, &actual));
149 TEST_ASSERT_TRUE(IsEqual(expected, actual));