1 #include "g_sockaddrtest.h"
3 class socktoaTest : public sockaddrtest {
6 TEST_F(socktoaTest, IPv4AddressWithPort) {
7 sockaddr_u input = CreateSockaddr4("192.0.2.10", 123);
9 EXPECT_STREQ("192.0.2.10", socktoa(&input));
10 EXPECT_STREQ("192.0.2.10:123", sockporttoa(&input));
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
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";
27 memset(&input, 0, sizeof(input));
28 AF(&input) = AF_INET6;
29 SET_ADDR6N(&input, address);
30 SET_PORT(&input, 123);
32 EXPECT_STREQ(expected, socktoa(&input));
33 EXPECT_STREQ(expected_port, sockporttoa(&input));
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
45 const char* expected =
46 "fe80::212:3fff:fe29:fffa%5";
47 const char* expected_port =
48 "[fe80::212:3fff:fe29:fffa%5]:123";
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;
57 EXPECT_STREQ(expected, socktoa(&input));
58 EXPECT_STREQ(expected_port, sockporttoa(&input));
60 #endif /* ISC_PLATFORM_HAVESCOPEID */
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);
66 ASSERT_TRUE(IsEqual(input1, input2));
67 EXPECT_EQ(sock_hash(&input1), sock_hash(&input2));
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);
75 ASSERT_FALSE(IsEqual(input1, input2));
76 EXPECT_NE(sock_hash(&input1), sock_hash(&input2));
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
87 sockaddr_u input1, input2;
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);
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);
99 EXPECT_EQ(sock_hash(&input1), sock_hash(&input2));