[email protected] | 7054e78f | 2012-05-07 21:44:56 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | 76ff86a | 2011-03-04 03:21:31 | [diff] [blame] | 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 | |
tfarina | 256bce7 | 2015-12-22 23:35:20 | [diff] [blame] | 7 | #include "build/build_config.h" |
| 8 | |
[email protected] | 76ff86a | 2011-03-04 03:21:31 | [diff] [blame] | 9 | #if defined(OS_WIN) |
| 10 | #include <winsock2.h> |
| 11 | #elif defined(OS_POSIX) |
| 12 | #include <netinet/in.h> |
| 13 | #endif |
| 14 | |
tfarina | 256bce7 | 2015-12-22 23:35:20 | [diff] [blame] | 15 | #include "base/logging.h" |
| 16 | #include "base/strings/string_number_conversions.h" |
| 17 | #include "base/sys_byteorder.h" |
tfarina | 3d87d7cd | 2016-01-13 02:26:59 | [diff] [blame] | 18 | #include "net/base/sockaddr_storage.h" |
tfarina | 256bce7 | 2015-12-22 23:35:20 | [diff] [blame] | 19 | #include "testing/gtest/include/gtest/gtest.h" |
| 20 | #include "testing/platform_test.h" |
| 21 | |
[email protected] | 76ff86a | 2011-03-04 03:21:31 | [diff] [blame] | 22 | namespace net { |
| 23 | |
| 24 | namespace { |
| 25 | |
tfarina | 256bce7 | 2015-12-22 23:35:20 | [diff] [blame] | 26 | // Retuns the port field of the |sockaddr|. |
| 27 | const uint16_t* GetPortFieldFromSockaddr(const struct sockaddr* address, |
| 28 | socklen_t address_len) { |
| 29 | if (address->sa_family == AF_INET) { |
| 30 | DCHECK_LE(sizeof(sockaddr_in), static_cast<size_t>(address_len)); |
| 31 | const struct sockaddr_in* sockaddr = |
| 32 | reinterpret_cast<const struct sockaddr_in*>(address); |
| 33 | return &sockaddr->sin_port; |
| 34 | } else if (address->sa_family == AF_INET6) { |
| 35 | DCHECK_LE(sizeof(sockaddr_in6), static_cast<size_t>(address_len)); |
| 36 | const struct sockaddr_in6* sockaddr = |
| 37 | reinterpret_cast<const struct sockaddr_in6*>(address); |
| 38 | return &sockaddr->sin6_port; |
| 39 | } else { |
| 40 | NOTREACHED(); |
| 41 | return NULL; |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | // Returns the value of port in |sockaddr| (in host byte ordering). |
| 46 | int GetPortFromSockaddr(const struct sockaddr* address, socklen_t address_len) { |
| 47 | const uint16_t* port_field = GetPortFieldFromSockaddr(address, address_len); |
| 48 | if (!port_field) |
| 49 | return -1; |
| 50 | return base::NetToHost16(*port_field); |
| 51 | } |
| 52 | |
[email protected] | 76ff86a | 2011-03-04 03:21:31 | [diff] [blame] | 53 | struct TestData { |
| 54 | std::string host; |
[email protected] | 1a2123c6 | 2011-03-12 04:42:39 | [diff] [blame] | 55 | std::string host_normalized; |
[email protected] | 76ff86a | 2011-03-04 03:21:31 | [diff] [blame] | 56 | bool ipv6; |
martijn | 98acb9b | 2016-01-27 07:14:07 | [diff] [blame] | 57 | IPAddress ip_address; |
[email protected] | 76ff86a | 2011-03-04 03:21:31 | [diff] [blame] | 58 | } tests[] = { |
[email protected] | 1a2123c6 | 2011-03-12 04:42:39 | [diff] [blame] | 59 | { "127.0.00.1", "127.0.0.1", false}, |
| 60 | { "192.168.1.1", "192.168.1.1", false }, |
[email protected] | 17e06f1 | 2011-04-05 23:10:10 | [diff] [blame] | 61 | { "::1", "[::1]", true }, |
| 62 | { "2001:db8:0::42", "[2001:db8::42]", true }, |
[email protected] | 76ff86a | 2011-03-04 03:21:31 | [diff] [blame] | 63 | }; |
[email protected] | 76ff86a | 2011-03-04 03:21:31 | [diff] [blame] | 64 | |
| 65 | class IPEndPointTest : public PlatformTest { |
| 66 | public: |
dcheng | 67be2b1f | 2014-10-27 21:47:29 | [diff] [blame] | 67 | void SetUp() override { |
[email protected] | 76ff86a | 2011-03-04 03:21:31 | [diff] [blame] | 68 | // This is where we populate the TestData. |
Ryan Sleevi | 4625214fa | 2018-05-10 16:42:45 | [diff] [blame] | 69 | for (auto& test : tests) { |
| 70 | EXPECT_TRUE(test.ip_address.AssignFromIPLiteral(test.host)); |
[email protected] | 76ff86a | 2011-03-04 03:21:31 | [diff] [blame] | 71 | } |
| 72 | } |
| 73 | }; |
| 74 | |
| 75 | TEST_F(IPEndPointTest, Constructor) { |
| 76 | IPEndPoint endpoint; |
| 77 | EXPECT_EQ(0, endpoint.port()); |
| 78 | |
Ryan Sleevi | 4625214fa | 2018-05-10 16:42:45 | [diff] [blame] | 79 | for (const auto& test : tests) { |
| 80 | IPEndPoint endpoint(test.ip_address, 80); |
[email protected] | 76ff86a | 2011-03-04 03:21:31 | [diff] [blame] | 81 | EXPECT_EQ(80, endpoint.port()); |
Ryan Sleevi | 4625214fa | 2018-05-10 16:42:45 | [diff] [blame] | 82 | EXPECT_EQ(test.ip_address, endpoint.address()); |
[email protected] | 76ff86a | 2011-03-04 03:21:31 | [diff] [blame] | 83 | } |
| 84 | } |
| 85 | |
| 86 | TEST_F(IPEndPointTest, Assignment) { |
Ryan Sleevi | 4625214fa | 2018-05-10 16:42:45 | [diff] [blame] | 87 | uint16_t port = 0; |
| 88 | for (const auto& test : tests) { |
| 89 | IPEndPoint src(test.ip_address, ++port); |
[email protected] | 76ff86a | 2011-03-04 03:21:31 | [diff] [blame] | 90 | IPEndPoint dest = src; |
| 91 | |
| 92 | EXPECT_EQ(src.port(), dest.port()); |
| 93 | EXPECT_EQ(src.address(), dest.address()); |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | TEST_F(IPEndPointTest, Copy) { |
Ryan Sleevi | 4625214fa | 2018-05-10 16:42:45 | [diff] [blame] | 98 | uint16_t port = 0; |
| 99 | for (const auto& test : tests) { |
| 100 | IPEndPoint src(test.ip_address, ++port); |
[email protected] | 76ff86a | 2011-03-04 03:21:31 | [diff] [blame] | 101 | IPEndPoint dest(src); |
| 102 | |
| 103 | EXPECT_EQ(src.port(), dest.port()); |
| 104 | EXPECT_EQ(src.address(), dest.address()); |
| 105 | } |
| 106 | } |
| 107 | |
[email protected] | 43d4a026 | 2011-03-09 19:26:04 | [diff] [blame] | 108 | TEST_F(IPEndPointTest, ToFromSockAddr) { |
Ryan Sleevi | 4625214fa | 2018-05-10 16:42:45 | [diff] [blame] | 109 | uint16_t port = 0; |
| 110 | for (const auto& test : tests) { |
| 111 | IPEndPoint ip_endpoint(test.ip_address, ++port); |
[email protected] | 76ff86a | 2011-03-04 03:21:31 | [diff] [blame] | 112 | |
| 113 | // Convert to a sockaddr. |
[email protected] | 7054e78f | 2012-05-07 21:44:56 | [diff] [blame] | 114 | SockaddrStorage storage; |
| 115 | EXPECT_TRUE(ip_endpoint.ToSockAddr(storage.addr, &storage.addr_len)); |
[email protected] | 76ff86a | 2011-03-04 03:21:31 | [diff] [blame] | 116 | |
| 117 | // Basic verification. |
Ryan Sleevi | 4625214fa | 2018-05-10 16:42:45 | [diff] [blame] | 118 | socklen_t expected_size = |
| 119 | test.ipv6 ? sizeof(struct sockaddr_in6) : sizeof(struct sockaddr_in); |
[email protected] | 7054e78f | 2012-05-07 21:44:56 | [diff] [blame] | 120 | EXPECT_EQ(expected_size, storage.addr_len); |
| 121 | EXPECT_EQ(ip_endpoint.port(), GetPortFromSockaddr(storage.addr, |
| 122 | storage.addr_len)); |
[email protected] | 76ff86a | 2011-03-04 03:21:31 | [diff] [blame] | 123 | |
| 124 | // And convert back to an IPEndPoint. |
| 125 | IPEndPoint ip_endpoint2; |
[email protected] | 7054e78f | 2012-05-07 21:44:56 | [diff] [blame] | 126 | EXPECT_TRUE(ip_endpoint2.FromSockAddr(storage.addr, storage.addr_len)); |
[email protected] | 76ff86a | 2011-03-04 03:21:31 | [diff] [blame] | 127 | EXPECT_EQ(ip_endpoint.port(), ip_endpoint2.port()); |
| 128 | EXPECT_EQ(ip_endpoint.address(), ip_endpoint2.address()); |
| 129 | } |
| 130 | } |
| 131 | |
[email protected] | 43d4a026 | 2011-03-09 19:26:04 | [diff] [blame] | 132 | TEST_F(IPEndPointTest, ToSockAddrBufTooSmall) { |
Ryan Sleevi | 4625214fa | 2018-05-10 16:42:45 | [diff] [blame] | 133 | uint16_t port = 0; |
| 134 | for (const auto& test : tests) { |
| 135 | IPEndPoint ip_endpoint(test.ip_address, port); |
[email protected] | 76ff86a | 2011-03-04 03:21:31 | [diff] [blame] | 136 | |
[email protected] | 7054e78f | 2012-05-07 21:44:56 | [diff] [blame] | 137 | SockaddrStorage storage; |
Ryan Sleevi | 4625214fa | 2018-05-10 16:42:45 | [diff] [blame] | 138 | storage.addr_len = 3; // size is too small! |
[email protected] | 7054e78f | 2012-05-07 21:44:56 | [diff] [blame] | 139 | EXPECT_FALSE(ip_endpoint.ToSockAddr(storage.addr, &storage.addr_len)); |
[email protected] | 76ff86a | 2011-03-04 03:21:31 | [diff] [blame] | 140 | } |
| 141 | } |
| 142 | |
[email protected] | 1a872df | 2011-08-20 07:38:11 | [diff] [blame] | 143 | TEST_F(IPEndPointTest, FromSockAddrBufTooSmall) { |
| 144 | struct sockaddr_in addr; |
| 145 | memset(&addr, 0, sizeof(addr)); |
| 146 | addr.sin_family = AF_INET; |
| 147 | IPEndPoint ip_endpoint; |
| 148 | struct sockaddr* sockaddr = reinterpret_cast<struct sockaddr*>(&addr); |
| 149 | EXPECT_FALSE(ip_endpoint.FromSockAddr(sockaddr, sizeof(addr) - 1)); |
| 150 | } |
| 151 | |
[email protected] | 76ff86a | 2011-03-04 03:21:31 | [diff] [blame] | 152 | TEST_F(IPEndPointTest, Equality) { |
Ryan Sleevi | 4625214fa | 2018-05-10 16:42:45 | [diff] [blame] | 153 | uint16_t port = 0; |
| 154 | for (const auto& test : tests) { |
| 155 | IPEndPoint src(test.ip_address, ++port); |
[email protected] | 76ff86a | 2011-03-04 03:21:31 | [diff] [blame] | 156 | IPEndPoint dest(src); |
| 157 | EXPECT_TRUE(src == dest); |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | TEST_F(IPEndPointTest, LessThan) { |
| 162 | // Vary by port. |
| 163 | IPEndPoint ip_endpoint1(tests[0].ip_address, 100); |
| 164 | IPEndPoint ip_endpoint2(tests[0].ip_address, 1000); |
| 165 | EXPECT_TRUE(ip_endpoint1 < ip_endpoint2); |
[email protected] | 17e06f1 | 2011-04-05 23:10:10 | [diff] [blame] | 166 | EXPECT_FALSE(ip_endpoint2 < ip_endpoint1); |
[email protected] | 76ff86a | 2011-03-04 03:21:31 | [diff] [blame] | 167 | |
| 168 | // IPv4 vs IPv6 |
[email protected] | 17e06f1 | 2011-04-05 23:10:10 | [diff] [blame] | 169 | ip_endpoint1 = IPEndPoint(tests[0].ip_address, 81); |
[email protected] | 76ff86a | 2011-03-04 03:21:31 | [diff] [blame] | 170 | ip_endpoint2 = IPEndPoint(tests[2].ip_address, 80); |
[email protected] | 17e06f1 | 2011-04-05 23:10:10 | [diff] [blame] | 171 | EXPECT_TRUE(ip_endpoint1 < ip_endpoint2); |
| 172 | EXPECT_FALSE(ip_endpoint2 < ip_endpoint1); |
[email protected] | 76ff86a | 2011-03-04 03:21:31 | [diff] [blame] | 173 | |
| 174 | // IPv4 vs IPv4 |
[email protected] | 17e06f1 | 2011-04-05 23:10:10 | [diff] [blame] | 175 | ip_endpoint1 = IPEndPoint(tests[0].ip_address, 81); |
[email protected] | 76ff86a | 2011-03-04 03:21:31 | [diff] [blame] | 176 | ip_endpoint2 = IPEndPoint(tests[1].ip_address, 80); |
| 177 | EXPECT_TRUE(ip_endpoint1 < ip_endpoint2); |
[email protected] | 17e06f1 | 2011-04-05 23:10:10 | [diff] [blame] | 178 | EXPECT_FALSE(ip_endpoint2 < ip_endpoint1); |
[email protected] | 76ff86a | 2011-03-04 03:21:31 | [diff] [blame] | 179 | |
| 180 | // IPv6 vs IPv6 |
[email protected] | 17e06f1 | 2011-04-05 23:10:10 | [diff] [blame] | 181 | ip_endpoint1 = IPEndPoint(tests[2].ip_address, 81); |
[email protected] | 76ff86a | 2011-03-04 03:21:31 | [diff] [blame] | 182 | ip_endpoint2 = IPEndPoint(tests[3].ip_address, 80); |
| 183 | EXPECT_TRUE(ip_endpoint1 < ip_endpoint2); |
[email protected] | 17e06f1 | 2011-04-05 23:10:10 | [diff] [blame] | 184 | EXPECT_FALSE(ip_endpoint2 < ip_endpoint1); |
| 185 | |
| 186 | // Compare equivalent endpoints. |
| 187 | ip_endpoint1 = IPEndPoint(tests[0].ip_address, 80); |
| 188 | ip_endpoint2 = IPEndPoint(tests[0].ip_address, 80); |
| 189 | EXPECT_FALSE(ip_endpoint1 < ip_endpoint2); |
| 190 | EXPECT_FALSE(ip_endpoint2 < ip_endpoint1); |
[email protected] | 76ff86a | 2011-03-04 03:21:31 | [diff] [blame] | 191 | } |
| 192 | |
[email protected] | 17e06f1 | 2011-04-05 23:10:10 | [diff] [blame] | 193 | TEST_F(IPEndPointTest, ToString) { |
[email protected] | 1a2123c6 | 2011-03-12 04:42:39 | [diff] [blame] | 194 | IPEndPoint endpoint; |
| 195 | EXPECT_EQ(0, endpoint.port()); |
| 196 | |
Ryan Sleevi | 4625214fa | 2018-05-10 16:42:45 | [diff] [blame] | 197 | uint16_t port = 100; |
| 198 | for (const auto& test : tests) { |
| 199 | ++port; |
| 200 | IPEndPoint endpoint(test.ip_address, port); |
[email protected] | 17e06f1 | 2011-04-05 23:10:10 | [diff] [blame] | 201 | const std::string result = endpoint.ToString(); |
Ryan Sleevi | 4625214fa | 2018-05-10 16:42:45 | [diff] [blame] | 202 | EXPECT_EQ(test.host_normalized + ":" + base::UintToString(port), result); |
[email protected] | 1a2123c6 | 2011-03-12 04:42:39 | [diff] [blame] | 203 | } |
martijn | 7246f78 | 2016-02-18 09:00:51 | [diff] [blame] | 204 | |
| 205 | // ToString() shouldn't crash on invalid addresses. |
| 206 | IPAddress invalid_address; |
| 207 | IPEndPoint invalid_endpoint(invalid_address, 8080); |
| 208 | EXPECT_EQ("", invalid_endpoint.ToString()); |
| 209 | EXPECT_EQ("", invalid_endpoint.ToStringWithoutPort()); |
[email protected] | 1a2123c6 | 2011-03-12 04:42:39 | [diff] [blame] | 210 | } |
| 211 | |
[email protected] | 76ff86a | 2011-03-04 03:21:31 | [diff] [blame] | 212 | } // namespace |
| 213 | |
| 214 | } // namespace net |