3 #include "ntp_stdlib.h"
4 #include "ntp_calendar.h"
7 #include "sockaddrtest.h"
10 void test_IPv4AddressWithPort(void);
11 //#ifdef ISC_PLATFORM_HAVEIPV6
12 void test_IPv6AddressWithPort(void);
13 void test_IgnoreIPv6Fields(void);
14 //#endif /* ISC_PLATFORM_HAVEIPV6 */
15 void test_ScopedIPv6AddressWithPort(void);
16 void test_HashEqual(void);
17 void test_HashNotEqual(void);
20 test_IPv4AddressWithPort(void) {
21 sockaddr_u input = CreateSockaddr4("192.0.2.10", 123);
23 TEST_ASSERT_EQUAL_STRING("192.0.2.10", socktoa(&input));
24 TEST_ASSERT_EQUAL_STRING("192.0.2.10:123", sockporttoa(&input));
29 test_IPv6AddressWithPort(void) {
31 #ifdef ISC_PLATFORM_WANTIPV6
33 const struct in6_addr address = {
34 0x20, 0x01, 0x0d, 0xb8,
35 0x85, 0xa3, 0x08, 0xd3,
36 0x13, 0x19, 0x8a, 0x2e,
37 0x03, 0x70, 0x73, 0x34
40 const char* expected =
41 "2001:db8:85a3:8d3:1319:8a2e:370:7334";
42 const char* expected_port =
43 "[2001:db8:85a3:8d3:1319:8a2e:370:7334]:123";
46 memset(&input, 0, sizeof(input));
47 AF(&input) = AF_INET6;
48 SET_ADDR6N(&input, address);
49 SET_PORT(&input, 123);
51 TEST_ASSERT_EQUAL_STRING(expected, socktoa(&input));
52 TEST_ASSERT_EQUAL_STRING(expected_port, sockporttoa(&input));
55 TEST_IGNORE_MESSAGE("IPV6 disabled in build, skipping.");
57 #endif /* ISC_PLATFORM_HAVEIPV6 */
63 test_ScopedIPv6AddressWithPort(void) {
64 #ifdef ISC_PLATFORM_HAVESCOPEID
65 const struct in6_addr address = {
66 0xfe, 0x80, 0x00, 0x00,
67 0x00, 0x00, 0x00, 0x00,
68 0x02, 0x12, 0x3f, 0xff,
69 0xfe, 0x29, 0xff, 0xfa
72 const char* expected =
73 "fe80::212:3fff:fe29:fffa%5";
74 const char* expected_port =
75 "[fe80::212:3fff:fe29:fffa%5]:123";
78 memset(&input, 0, sizeof(input));
79 AF(&input) = AF_INET6;
80 SET_ADDR6N(&input, address);
81 SET_PORT(&input, 123);
82 SCOPE_VAR(&input) = 5;
84 TEST_ASSERT_EQUAL_STRING(expected, socktoa(&input));
85 TEST_ASSERT_EQUAL_STRING(expected_port, sockporttoa(&input));
87 TEST_IGNORE_MESSAGE("Skipping because ISC_PLATFORM does not have Scope ID");
92 test_HashEqual(void) {
93 sockaddr_u input1 = CreateSockaddr4("192.00.2.2", 123);
94 sockaddr_u input2 = CreateSockaddr4("192.0.2.2", 123);
96 TEST_ASSERT_TRUE(IsEqual(input1, input2));
97 TEST_ASSERT_EQUAL(sock_hash(&input1), sock_hash(&input2));
101 test_HashNotEqual(void) {
102 /* These two addresses should not generate the same hash. */
103 sockaddr_u input1 = CreateSockaddr4("192.0.2.1", 123);
104 sockaddr_u input2 = CreateSockaddr4("192.0.2.2", 123);
106 TEST_ASSERT_FALSE(IsEqual(input1, input2));
107 TEST_ASSERT_FALSE(sock_hash(&input1) == sock_hash(&input2));
112 test_IgnoreIPv6Fields(void) {
114 #ifdef ISC_PLATFORM_WANTIPV6
116 const struct in6_addr address = {
117 0x20, 0x01, 0x0d, 0xb8,
118 0x85, 0xa3, 0x08, 0xd3,
119 0x13, 0x19, 0x8a, 0x2e,
120 0x03, 0x70, 0x73, 0x34
123 sockaddr_u input1, input2;
125 input1.sa6.sin6_family = AF_INET6;
126 input1.sa6.sin6_addr = address;
127 input1.sa6.sin6_flowinfo = 30L; // This value differs from input2.
128 SET_PORT(&input1, NTP_PORT);
130 input2.sa6.sin6_family = AF_INET6;
131 input2.sa6.sin6_addr = address;
132 input2.sa6.sin6_flowinfo = 10L; // This value differs from input1.
133 SET_PORT(&input2, NTP_PORT);
135 TEST_ASSERT_EQUAL(sock_hash(&input1), sock_hash(&input2));
138 TEST_IGNORE_MESSAGE("IPV6 disabled in build, skipping.");
139 #endif /* ISC_PLATFORM_HAVEIPV6 */