3 #include "ntp_stdlib.h"
4 #include "ntp_calendar.h"
7 #include "sockaddrtest.h"
11 void test_IPv4AddressWithPort(void);
12 void test_IPv6AddressWithPort(void);
13 void test_IgnoreIPv6Fields(void);
14 void test_ScopedIPv6AddressWithPort(void);
15 void test_HashEqual(void);
16 void test_HashNotEqual(void);
27 test_IPv4AddressWithPort(void)
29 sockaddr_u input = CreateSockaddr4("192.0.2.10", 123);
31 TEST_ASSERT_EQUAL_STRING("192.0.2.10", socktoa(&input));
32 TEST_ASSERT_EQUAL_STRING("192.0.2.10:123", sockporttoa(&input));
37 test_IPv6AddressWithPort(void)
39 #ifdef ISC_PLATFORM_WANTIPV6
41 const struct in6_addr address = {
42 0x20, 0x01, 0x0d, 0xb8,
43 0x85, 0xa3, 0x08, 0xd3,
44 0x13, 0x19, 0x8a, 0x2e,
45 0x03, 0x70, 0x73, 0x34
48 const char* expected =
49 "2001:db8:85a3:8d3:1319:8a2e:370:7334";
50 const char* expected_port =
51 "[2001:db8:85a3:8d3:1319:8a2e:370:7334]:123";
54 memset(&input, 0, sizeof(input));
55 AF(&input) = AF_INET6;
56 SET_ADDR6N(&input, address);
57 SET_PORT(&input, 123);
59 TEST_ASSERT_EQUAL_STRING(expected, socktoa(&input));
60 TEST_ASSERT_EQUAL_STRING(expected_port, sockporttoa(&input));
64 TEST_IGNORE_MESSAGE("IPV6 disabled in build, skipping.");
66 #endif /* ISC_PLATFORM_HAVEIPV6 */
71 test_ScopedIPv6AddressWithPort(void)
73 #ifdef ISC_PLATFORM_HAVESCOPEID
75 const struct in6_addr address = { { {
76 0xfe, 0x80, 0x00, 0x00,
77 0x00, 0x00, 0x00, 0x00,
78 0x02, 0x12, 0x3f, 0xff,
79 0xfe, 0x29, 0xff, 0xfa
82 const char* expected =
83 "fe80::212:3fff:fe29:fffa%5";
84 const char* expected_port =
85 "[fe80::212:3fff:fe29:fffa%5]:123";
88 memset(&input, 0, sizeof(input));
89 AF(&input) = AF_INET6;
90 SET_ADDR6N(&input, address);
91 SET_PORT(&input, 123);
92 SCOPE_VAR(&input) = 5;
94 TEST_ASSERT_EQUAL_STRING(expected, socktoa(&input));
95 TEST_ASSERT_EQUAL_STRING(expected_port, sockporttoa(&input));
98 TEST_IGNORE_MESSAGE("Skipping because ISC_PLATFORM does not have Scope ID");
107 sockaddr_u input1 = CreateSockaddr4("192.00.2.2", 123);
108 sockaddr_u input2 = CreateSockaddr4("192.0.2.2", 123);
110 TEST_ASSERT_TRUE(IsEqual(input1, input2));
111 TEST_ASSERT_EQUAL(sock_hash(&input1), sock_hash(&input2));
116 test_HashNotEqual(void)
118 /* These two addresses should not generate the same hash. */
119 sockaddr_u input1 = CreateSockaddr4("192.0.2.1", 123);
120 sockaddr_u input2 = CreateSockaddr4("192.0.2.2", 123);
122 TEST_ASSERT_FALSE(IsEqual(input1, input2));
123 TEST_ASSERT_FALSE(sock_hash(&input1) == sock_hash(&input2));
128 test_IgnoreIPv6Fields(void)
130 #ifdef ISC_PLATFORM_WANTIPV6
132 const struct in6_addr address = {
133 0x20, 0x01, 0x0d, 0xb8,
134 0x85, 0xa3, 0x08, 0xd3,
135 0x13, 0x19, 0x8a, 0x2e,
136 0x03, 0x70, 0x73, 0x34
139 sockaddr_u input1, input2;
141 input1.sa6.sin6_family = AF_INET6;
142 input1.sa6.sin6_addr = address;
143 input1.sa6.sin6_flowinfo = 30L; // This value differs from input2.
144 SET_PORT(&input1, NTP_PORT);
146 input2.sa6.sin6_family = AF_INET6;
147 input2.sa6.sin6_addr = address;
148 input2.sa6.sin6_flowinfo = 10L; // This value differs from input1.
149 SET_PORT(&input2, NTP_PORT);
151 TEST_ASSERT_EQUAL(sock_hash(&input1), sock_hash(&input2));
155 TEST_IGNORE_MESSAGE("IPV6 disabled in build, skipping.");
157 #endif /* ISC_PLATFORM_HAVEIPV6 */