3 #include "ntp_stdlib.h"
4 #include "ntp_calendar.h"
7 #include "sockaddrtest.h"
11 void test_IPv4AddressWithPort(void);
12 //#ifdef ISC_PLATFORM_HAVEIPV6
13 void test_IPv6AddressWithPort(void);
14 void test_IgnoreIPv6Fields(void);
15 //#endif /* ISC_PLATFORM_HAVEIPV6 */
16 void test_ScopedIPv6AddressWithPort(void);
17 void test_HashEqual(void);
18 void test_HashNotEqual(void);
31 test_IPv4AddressWithPort(void) {
32 sockaddr_u input = CreateSockaddr4("192.0.2.10", 123);
34 TEST_ASSERT_EQUAL_STRING("192.0.2.10", socktoa(&input));
35 TEST_ASSERT_EQUAL_STRING("192.0.2.10:123", sockporttoa(&input));
40 test_IPv6AddressWithPort(void) {
42 #ifdef ISC_PLATFORM_WANTIPV6
44 const struct in6_addr address = {
45 0x20, 0x01, 0x0d, 0xb8,
46 0x85, 0xa3, 0x08, 0xd3,
47 0x13, 0x19, 0x8a, 0x2e,
48 0x03, 0x70, 0x73, 0x34
51 const char* expected =
52 "2001:db8:85a3:8d3:1319:8a2e:370:7334";
53 const char* expected_port =
54 "[2001:db8:85a3:8d3:1319:8a2e:370:7334]:123";
57 memset(&input, 0, sizeof(input));
58 AF(&input) = AF_INET6;
59 SET_ADDR6N(&input, address);
60 SET_PORT(&input, 123);
62 TEST_ASSERT_EQUAL_STRING(expected, socktoa(&input));
63 TEST_ASSERT_EQUAL_STRING(expected_port, sockporttoa(&input));
66 TEST_IGNORE_MESSAGE("IPV6 disabled in build, skipping.");
68 #endif /* ISC_PLATFORM_HAVEIPV6 */
74 test_ScopedIPv6AddressWithPort(void) {
75 #ifdef ISC_PLATFORM_HAVESCOPEID
76 const struct in6_addr address = { { {
77 0xfe, 0x80, 0x00, 0x00,
78 0x00, 0x00, 0x00, 0x00,
79 0x02, 0x12, 0x3f, 0xff,
80 0xfe, 0x29, 0xff, 0xfa
83 const char* expected =
84 "fe80::212:3fff:fe29:fffa%5";
85 const char* expected_port =
86 "[fe80::212:3fff:fe29:fffa%5]:123";
89 memset(&input, 0, sizeof(input));
90 AF(&input) = AF_INET6;
91 SET_ADDR6N(&input, address);
92 SET_PORT(&input, 123);
93 SCOPE_VAR(&input) = 5;
95 TEST_ASSERT_EQUAL_STRING(expected, socktoa(&input));
96 TEST_ASSERT_EQUAL_STRING(expected_port, sockporttoa(&input));
98 TEST_IGNORE_MESSAGE("Skipping because ISC_PLATFORM does not have Scope ID");
103 test_HashEqual(void) {
104 sockaddr_u input1 = CreateSockaddr4("192.00.2.2", 123);
105 sockaddr_u input2 = CreateSockaddr4("192.0.2.2", 123);
107 TEST_ASSERT_TRUE(IsEqual(input1, input2));
108 TEST_ASSERT_EQUAL(sock_hash(&input1), sock_hash(&input2));
112 test_HashNotEqual(void) {
113 /* These two addresses should not generate the same hash. */
114 sockaddr_u input1 = CreateSockaddr4("192.0.2.1", 123);
115 sockaddr_u input2 = CreateSockaddr4("192.0.2.2", 123);
117 TEST_ASSERT_FALSE(IsEqual(input1, input2));
118 TEST_ASSERT_FALSE(sock_hash(&input1) == sock_hash(&input2));
123 test_IgnoreIPv6Fields(void) {
125 #ifdef ISC_PLATFORM_WANTIPV6
127 const struct in6_addr address = {
128 0x20, 0x01, 0x0d, 0xb8,
129 0x85, 0xa3, 0x08, 0xd3,
130 0x13, 0x19, 0x8a, 0x2e,
131 0x03, 0x70, 0x73, 0x34
134 sockaddr_u input1, input2;
136 input1.sa6.sin6_family = AF_INET6;
137 input1.sa6.sin6_addr = address;
138 input1.sa6.sin6_flowinfo = 30L; // This value differs from input2.
139 SET_PORT(&input1, NTP_PORT);
141 input2.sa6.sin6_family = AF_INET6;
142 input2.sa6.sin6_addr = address;
143 input2.sa6.sin6_flowinfo = 10L; // This value differs from input1.
144 SET_PORT(&input2, NTP_PORT);
146 TEST_ASSERT_EQUAL(sock_hash(&input1), sock_hash(&input2));
149 TEST_IGNORE_MESSAGE("IPV6 disabled in build, skipping.");
150 #endif /* ISC_PLATFORM_HAVEIPV6 */