]> CyberLeo.Net >> Repos - FreeBSD/releng/10.2.git/blob - contrib/ntp/tests/libntp/g_socktoa.cpp
Fix a regression with SA-15:24 patch that prevented NIS from
[FreeBSD/releng/10.2.git] / contrib / ntp / tests / libntp / g_socktoa.cpp
1 #include "g_sockaddrtest.h"
2
3 class socktoaTest : public sockaddrtest {
4 };
5
6 TEST_F(socktoaTest, IPv4AddressWithPort) {
7         sockaddr_u input = CreateSockaddr4("192.0.2.10", 123);
8
9         EXPECT_STREQ("192.0.2.10", socktoa(&input));
10         EXPECT_STREQ("192.0.2.10:123", sockporttoa(&input));
11 }
12
13 TEST_F(socktoaTest, IPv6AddressWithPort) {
14         const struct in6_addr address = {
15                 0x20, 0x01, 0x0d, 0xb8,
16                 0x85, 0xa3, 0x08, 0xd3, 
17                 0x13, 0x19, 0x8a, 0x2e,
18                 0x03, 0x70, 0x73, 0x34
19         };
20
21         const char* expected =
22                 "2001:db8:85a3:8d3:1319:8a2e:370:7334";
23         const char* expected_port = 
24                 "[2001:db8:85a3:8d3:1319:8a2e:370:7334]:123";
25
26         sockaddr_u input;
27         memset(&input, 0, sizeof(input));
28         AF(&input) = AF_INET6;
29         SET_ADDR6N(&input, address);
30         SET_PORT(&input, 123);
31
32         EXPECT_STREQ(expected, socktoa(&input));
33         EXPECT_STREQ(expected_port, sockporttoa(&input));
34 }
35
36 #ifdef ISC_PLATFORM_HAVESCOPEID
37 TEST_F(socktoaTest, ScopedIPv6AddressWithPort) {
38         const struct in6_addr address = {
39                 0xfe, 0x80, 0x00, 0x00,
40                 0x00, 0x00, 0x00, 0x00,
41                 0x02, 0x12, 0x3f, 0xff, 
42                 0xfe, 0x29, 0xff, 0xfa
43         };
44
45         const char* expected =
46                 "fe80::212:3fff:fe29:fffa%5";
47         const char* expected_port = 
48                 "[fe80::212:3fff:fe29:fffa%5]:123";
49
50         sockaddr_u input;
51         memset(&input, 0, sizeof(input));
52         AF(&input) = AF_INET6;
53         SET_ADDR6N(&input, address);
54         SET_PORT(&input, 123);
55         SCOPE_VAR(&input) = 5;
56
57         EXPECT_STREQ(expected, socktoa(&input));
58         EXPECT_STREQ(expected_port, sockporttoa(&input));
59 }
60 #endif  /* ISC_PLATFORM_HAVESCOPEID */
61
62 TEST_F(socktoaTest, HashEqual) {
63         sockaddr_u input1 = CreateSockaddr4("192.00.2.2", 123);
64         sockaddr_u input2 = CreateSockaddr4("192.0.2.2", 123);
65
66         ASSERT_TRUE(IsEqual(input1, input2));
67         EXPECT_EQ(sock_hash(&input1), sock_hash(&input2));
68 }
69
70 TEST_F(socktoaTest, HashNotEqual) {
71         /* These two addresses should not generate the same hash. */
72         sockaddr_u input1 = CreateSockaddr4("192.0.2.1", 123);
73         sockaddr_u input2 = CreateSockaddr4("192.0.2.2", 123);
74
75         ASSERT_FALSE(IsEqual(input1, input2));
76         EXPECT_NE(sock_hash(&input1), sock_hash(&input2));
77 }
78
79 TEST_F(socktoaTest, IgnoreIPv6Fields) {
80         const struct in6_addr address = {
81                 0x20, 0x01, 0x0d, 0xb8,
82         0x85, 0xa3, 0x08, 0xd3, 
83         0x13, 0x19, 0x8a, 0x2e,
84         0x03, 0x70, 0x73, 0x34
85         };
86
87         sockaddr_u input1, input2;
88
89         input1.sa6.sin6_family = AF_INET6;
90         input1.sa6.sin6_addr = address;
91         input1.sa6.sin6_flowinfo = 30L; // This value differs from input2.
92         SET_PORT(&input1, NTP_PORT);
93
94         input2.sa6.sin6_family = AF_INET6;
95         input2.sa6.sin6_addr = address;
96         input2.sa6.sin6_flowinfo = 10L; // This value differs from input1.
97         SET_PORT(&input2, NTP_PORT);
98
99         EXPECT_EQ(sock_hash(&input1), sock_hash(&input2));
100 }