]> CyberLeo.Net >> Repos - FreeBSD/releng/9.3.git/blob - contrib/ntp/tests/libntp/socktoa.c
Fix BIND remote denial of service vulnerability. [SA-16:08]
[FreeBSD/releng/9.3.git] / contrib / ntp / tests / libntp / socktoa.c
1 #include "config.h"
2
3 #include "ntp_stdlib.h"
4 #include "ntp_calendar.h"
5
6 #include "unity.h"
7 #include "sockaddrtest.h"
8
9
10 void setUp(void);
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);
17
18
19 void
20 setUp(void)
21 {
22         init_lib();
23 }
24
25
26 void 
27 test_IPv4AddressWithPort(void)
28 {
29         sockaddr_u input = CreateSockaddr4("192.0.2.10", 123);
30
31         TEST_ASSERT_EQUAL_STRING("192.0.2.10", socktoa(&input));
32         TEST_ASSERT_EQUAL_STRING("192.0.2.10:123", sockporttoa(&input));
33 }
34
35
36 void 
37 test_IPv6AddressWithPort(void)
38 {
39 #ifdef ISC_PLATFORM_WANTIPV6
40
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
46         };
47
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";
52
53         sockaddr_u input;
54         memset(&input, 0, sizeof(input));
55         AF(&input) = AF_INET6;
56         SET_ADDR6N(&input, address);
57         SET_PORT(&input, 123);
58
59         TEST_ASSERT_EQUAL_STRING(expected, socktoa(&input));
60         TEST_ASSERT_EQUAL_STRING(expected_port, sockporttoa(&input));
61
62 #else
63
64         TEST_IGNORE_MESSAGE("IPV6 disabled in build, skipping.");
65
66 #endif /* ISC_PLATFORM_HAVEIPV6 */
67 }
68
69
70 void 
71 test_ScopedIPv6AddressWithPort(void)
72 {
73 #ifdef ISC_PLATFORM_HAVESCOPEID
74     
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
80         } } };
81
82         const char* expected =
83                 "fe80::212:3fff:fe29:fffa%5";
84         const char* expected_port = 
85                 "[fe80::212:3fff:fe29:fffa%5]:123";
86
87         sockaddr_u input;
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;
93
94         TEST_ASSERT_EQUAL_STRING(expected, socktoa(&input));
95         TEST_ASSERT_EQUAL_STRING(expected_port, sockporttoa(&input));
96 #else
97         
98         TEST_IGNORE_MESSAGE("Skipping because ISC_PLATFORM does not have Scope ID");
99         
100 #endif
101 }
102
103
104 void 
105 test_HashEqual(void)
106 {
107         sockaddr_u input1 = CreateSockaddr4("192.00.2.2", 123);
108         sockaddr_u input2 = CreateSockaddr4("192.0.2.2", 123);
109
110         TEST_ASSERT_TRUE(IsEqual(input1, input2));
111         TEST_ASSERT_EQUAL(sock_hash(&input1), sock_hash(&input2));
112 }
113
114
115 void 
116 test_HashNotEqual(void)
117 {
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);
121
122         TEST_ASSERT_FALSE(IsEqual(input1, input2));
123         TEST_ASSERT_FALSE(sock_hash(&input1) == sock_hash(&input2)); 
124 }
125
126
127 void 
128 test_IgnoreIPv6Fields(void)
129 {
130 #ifdef ISC_PLATFORM_WANTIPV6
131
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
137         };
138
139         sockaddr_u input1, input2;
140
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);
145
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);
150
151         TEST_ASSERT_EQUAL(sock_hash(&input1), sock_hash(&input2));
152
153 #else
154
155         TEST_IGNORE_MESSAGE("IPV6 disabled in build, skipping.");
156
157 #endif /* ISC_PLATFORM_HAVEIPV6 */
158 }