1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #include "net/base/ip_endpoint.h" 6 7 #include "base/string_number_conversions.h" 8 #include "net/base/net_util.h" 9 #include "testing/gtest/include/gtest/gtest.h" 10 #include "testing/platform_test.h" 11 #if defined(OS_WIN) 12 #include <winsock2.h> 13 #elif defined(OS_POSIX) 14 #include <netinet/in.h> 15 #endif 16 17 namespace net { 18 19 namespace { 20 21 struct TestData { 22 std::string host; 23 std::string host_normalized; 24 bool ipv6; 25 IPAddressNumber ip_address; 26 } tests[] = { 27 { "127.0.00.1", "127.0.0.1", false}, 28 { "192.168.1.1", "192.168.1.1", false }, 29 { "::1", "[::1]", true }, 30 { "2001:db8:0::42", "[2001:db8::42]", true }, 31 }; 32 int test_count = ARRAYSIZE_UNSAFE(tests); 33 34 class IPEndPointTest : public PlatformTest { 35 public: 36 virtual void SetUp() { 37 // This is where we populate the TestData. 38 for (int index = 0; index < test_count; ++index) { 39 EXPECT_TRUE(ParseIPLiteralToNumber(tests[index].host, 40 &tests[index].ip_address)); 41 } 42 } 43 }; 44 45 TEST_F(IPEndPointTest, Constructor) { 46 IPEndPoint endpoint; 47 EXPECT_EQ(0, endpoint.port()); 48 49 for (int index = 0; index < test_count; ++index) { 50 IPEndPoint endpoint(tests[index].ip_address, 80); 51 EXPECT_EQ(80, endpoint.port()); 52 EXPECT_EQ(tests[index].ip_address, endpoint.address()); 53 } 54 } 55 56 TEST_F(IPEndPointTest, Assignment) { 57 for (int index = 0; index < test_count; ++index) { 58 IPEndPoint src(tests[index].ip_address, index); 59 IPEndPoint dest = src; 60 61 EXPECT_EQ(src.port(), dest.port()); 62 EXPECT_EQ(src.address(), dest.address()); 63 } 64 } 65 66 TEST_F(IPEndPointTest, Copy) { 67 for (int index = 0; index < test_count; ++index) { 68 IPEndPoint src(tests[index].ip_address, index); 69 IPEndPoint dest(src); 70 71 EXPECT_EQ(src.port(), dest.port()); 72 EXPECT_EQ(src.address(), dest.address()); 73 } 74 } 75 76 TEST_F(IPEndPointTest, ToFromSockAddr) { 77 for (int index = 0; index < test_count; ++index) { 78 IPEndPoint ip_endpoint(tests[index].ip_address, index); 79 80 // Convert to a sockaddr. 81 struct sockaddr_storage addr; 82 size_t addr_len = sizeof(addr); 83 struct sockaddr* sockaddr = reinterpret_cast<struct sockaddr*>(&addr); 84 EXPECT_TRUE(ip_endpoint.ToSockAddr(sockaddr, &addr_len)); 85 86 // Basic verification. 87 size_t expected_size = tests[index].ipv6 ? 88 sizeof(struct sockaddr_in6) : sizeof(struct sockaddr_in); 89 EXPECT_EQ(expected_size, addr_len); 90 EXPECT_EQ(ip_endpoint.port(), GetPortFromSockaddr(sockaddr, addr_len)); 91 92 // And convert back to an IPEndPoint. 93 IPEndPoint ip_endpoint2; 94 EXPECT_TRUE(ip_endpoint2.FromSockAddr(sockaddr, addr_len)); 95 EXPECT_EQ(ip_endpoint.port(), ip_endpoint2.port()); 96 EXPECT_EQ(ip_endpoint.address(), ip_endpoint2.address()); 97 } 98 } 99 100 TEST_F(IPEndPointTest, ToSockAddrBufTooSmall) { 101 for (int index = 0; index < test_count; ++index) { 102 IPEndPoint ip_endpoint(tests[index].ip_address, index); 103 104 struct sockaddr_storage addr; 105 size_t addr_len = index; // size is too small! 106 struct sockaddr* sockaddr = reinterpret_cast<struct sockaddr*>(&addr); 107 EXPECT_FALSE(ip_endpoint.ToSockAddr(sockaddr, &addr_len)); 108 } 109 } 110 111 TEST_F(IPEndPointTest, Equality) { 112 for (int index = 0; index < test_count; ++index) { 113 IPEndPoint src(tests[index].ip_address, index); 114 IPEndPoint dest(src); 115 EXPECT_TRUE(src == dest); 116 } 117 } 118 119 TEST_F(IPEndPointTest, LessThan) { 120 // Vary by port. 121 IPEndPoint ip_endpoint1(tests[0].ip_address, 100); 122 IPEndPoint ip_endpoint2(tests[0].ip_address, 1000); 123 EXPECT_TRUE(ip_endpoint1 < ip_endpoint2); 124 EXPECT_FALSE(ip_endpoint2 < ip_endpoint1); 125 126 // IPv4 vs IPv6 127 ip_endpoint1 = IPEndPoint(tests[0].ip_address, 81); 128 ip_endpoint2 = IPEndPoint(tests[2].ip_address, 80); 129 EXPECT_TRUE(ip_endpoint1 < ip_endpoint2); 130 EXPECT_FALSE(ip_endpoint2 < ip_endpoint1); 131 132 // IPv4 vs IPv4 133 ip_endpoint1 = IPEndPoint(tests[0].ip_address, 81); 134 ip_endpoint2 = IPEndPoint(tests[1].ip_address, 80); 135 EXPECT_TRUE(ip_endpoint1 < ip_endpoint2); 136 EXPECT_FALSE(ip_endpoint2 < ip_endpoint1); 137 138 // IPv6 vs IPv6 139 ip_endpoint1 = IPEndPoint(tests[2].ip_address, 81); 140 ip_endpoint2 = IPEndPoint(tests[3].ip_address, 80); 141 EXPECT_TRUE(ip_endpoint1 < ip_endpoint2); 142 EXPECT_FALSE(ip_endpoint2 < ip_endpoint1); 143 144 // Compare equivalent endpoints. 145 ip_endpoint1 = IPEndPoint(tests[0].ip_address, 80); 146 ip_endpoint2 = IPEndPoint(tests[0].ip_address, 80); 147 EXPECT_FALSE(ip_endpoint1 < ip_endpoint2); 148 EXPECT_FALSE(ip_endpoint2 < ip_endpoint1); 149 } 150 151 TEST_F(IPEndPointTest, ToString) { 152 IPEndPoint endpoint; 153 EXPECT_EQ(0, endpoint.port()); 154 155 for (int index = 0; index < test_count; ++index) { 156 int port = 100 + index; 157 IPEndPoint endpoint(tests[index].ip_address, port); 158 const std::string result = endpoint.ToString(); 159 if (tests[index].ipv6 && result.empty()) { 160 // NetAddressToStringWithPort may fail on systems without IPv6. 161 continue; 162 } 163 EXPECT_EQ(tests[index].host_normalized + ":" + base::IntToString(port), 164 result); 165 } 166 } 167 168 } // namespace 169 170 } // namespace net 171