blob: 545f2e6d5185dbc8539f6e34f4a795822010eafe [file] [log] [blame]
[email protected]7054e78f2012-05-07 21:44:561// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]76ff86a2011-03-04 03:21:312// 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
tfarina256bce72015-12-22 23:35:207#include "build/build_config.h"
8
[email protected]76ff86a2011-03-04 03:21:319#if defined(OS_WIN)
10#include <winsock2.h>
11#elif defined(OS_POSIX)
12#include <netinet/in.h>
13#endif
14
tfarina256bce72015-12-22 23:35:2015#include "base/logging.h"
16#include "base/strings/string_number_conversions.h"
17#include "base/sys_byteorder.h"
tfarina3d87d7cd2016-01-13 02:26:5918#include "net/base/sockaddr_storage.h"
tfarina256bce72015-12-22 23:35:2019#include "testing/gtest/include/gtest/gtest.h"
20#include "testing/platform_test.h"
21
[email protected]76ff86a2011-03-04 03:21:3122namespace net {
23
24namespace {
25
tfarina256bce72015-12-22 23:35:2026// Retuns the port field of the |sockaddr|.
27const 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).
46int 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]76ff86a2011-03-04 03:21:3153struct TestData {
54 std::string host;
[email protected]1a2123c62011-03-12 04:42:3955 std::string host_normalized;
[email protected]76ff86a2011-03-04 03:21:3156 bool ipv6;
martijn98acb9b2016-01-27 07:14:0757 IPAddress ip_address;
[email protected]76ff86a2011-03-04 03:21:3158} tests[] = {
[email protected]1a2123c62011-03-12 04:42:3959 { "127.0.00.1", "127.0.0.1", false},
60 { "192.168.1.1", "192.168.1.1", false },
[email protected]17e06f12011-04-05 23:10:1061 { "::1", "[::1]", true },
62 { "2001:db8:0::42", "[2001:db8::42]", true },
[email protected]76ff86a2011-03-04 03:21:3163};
[email protected]76ff86a2011-03-04 03:21:3164
65class IPEndPointTest : public PlatformTest {
66 public:
dcheng67be2b1f2014-10-27 21:47:2967 void SetUp() override {
[email protected]76ff86a2011-03-04 03:21:3168 // This is where we populate the TestData.
Ryan Sleevi4625214fa2018-05-10 16:42:4569 for (auto& test : tests) {
70 EXPECT_TRUE(test.ip_address.AssignFromIPLiteral(test.host));
[email protected]76ff86a2011-03-04 03:21:3171 }
72 }
73};
74
75TEST_F(IPEndPointTest, Constructor) {
76 IPEndPoint endpoint;
77 EXPECT_EQ(0, endpoint.port());
78
Ryan Sleevi4625214fa2018-05-10 16:42:4579 for (const auto& test : tests) {
80 IPEndPoint endpoint(test.ip_address, 80);
[email protected]76ff86a2011-03-04 03:21:3181 EXPECT_EQ(80, endpoint.port());
Ryan Sleevi4625214fa2018-05-10 16:42:4582 EXPECT_EQ(test.ip_address, endpoint.address());
[email protected]76ff86a2011-03-04 03:21:3183 }
84}
85
86TEST_F(IPEndPointTest, Assignment) {
Ryan Sleevi4625214fa2018-05-10 16:42:4587 uint16_t port = 0;
88 for (const auto& test : tests) {
89 IPEndPoint src(test.ip_address, ++port);
[email protected]76ff86a2011-03-04 03:21:3190 IPEndPoint dest = src;
91
92 EXPECT_EQ(src.port(), dest.port());
93 EXPECT_EQ(src.address(), dest.address());
94 }
95}
96
97TEST_F(IPEndPointTest, Copy) {
Ryan Sleevi4625214fa2018-05-10 16:42:4598 uint16_t port = 0;
99 for (const auto& test : tests) {
100 IPEndPoint src(test.ip_address, ++port);
[email protected]76ff86a2011-03-04 03:21:31101 IPEndPoint dest(src);
102
103 EXPECT_EQ(src.port(), dest.port());
104 EXPECT_EQ(src.address(), dest.address());
105 }
106}
107
[email protected]43d4a0262011-03-09 19:26:04108TEST_F(IPEndPointTest, ToFromSockAddr) {
Ryan Sleevi4625214fa2018-05-10 16:42:45109 uint16_t port = 0;
110 for (const auto& test : tests) {
111 IPEndPoint ip_endpoint(test.ip_address, ++port);
[email protected]76ff86a2011-03-04 03:21:31112
113 // Convert to a sockaddr.
[email protected]7054e78f2012-05-07 21:44:56114 SockaddrStorage storage;
115 EXPECT_TRUE(ip_endpoint.ToSockAddr(storage.addr, &storage.addr_len));
[email protected]76ff86a2011-03-04 03:21:31116
117 // Basic verification.
Ryan Sleevi4625214fa2018-05-10 16:42:45118 socklen_t expected_size =
119 test.ipv6 ? sizeof(struct sockaddr_in6) : sizeof(struct sockaddr_in);
[email protected]7054e78f2012-05-07 21:44:56120 EXPECT_EQ(expected_size, storage.addr_len);
121 EXPECT_EQ(ip_endpoint.port(), GetPortFromSockaddr(storage.addr,
122 storage.addr_len));
[email protected]76ff86a2011-03-04 03:21:31123
124 // And convert back to an IPEndPoint.
125 IPEndPoint ip_endpoint2;
[email protected]7054e78f2012-05-07 21:44:56126 EXPECT_TRUE(ip_endpoint2.FromSockAddr(storage.addr, storage.addr_len));
[email protected]76ff86a2011-03-04 03:21:31127 EXPECT_EQ(ip_endpoint.port(), ip_endpoint2.port());
128 EXPECT_EQ(ip_endpoint.address(), ip_endpoint2.address());
129 }
130}
131
[email protected]43d4a0262011-03-09 19:26:04132TEST_F(IPEndPointTest, ToSockAddrBufTooSmall) {
Ryan Sleevi4625214fa2018-05-10 16:42:45133 uint16_t port = 0;
134 for (const auto& test : tests) {
135 IPEndPoint ip_endpoint(test.ip_address, port);
[email protected]76ff86a2011-03-04 03:21:31136
[email protected]7054e78f2012-05-07 21:44:56137 SockaddrStorage storage;
Ryan Sleevi4625214fa2018-05-10 16:42:45138 storage.addr_len = 3; // size is too small!
[email protected]7054e78f2012-05-07 21:44:56139 EXPECT_FALSE(ip_endpoint.ToSockAddr(storage.addr, &storage.addr_len));
[email protected]76ff86a2011-03-04 03:21:31140 }
141}
142
[email protected]1a872df2011-08-20 07:38:11143TEST_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]76ff86a2011-03-04 03:21:31152TEST_F(IPEndPointTest, Equality) {
Ryan Sleevi4625214fa2018-05-10 16:42:45153 uint16_t port = 0;
154 for (const auto& test : tests) {
155 IPEndPoint src(test.ip_address, ++port);
[email protected]76ff86a2011-03-04 03:21:31156 IPEndPoint dest(src);
157 EXPECT_TRUE(src == dest);
158 }
159}
160
161TEST_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]17e06f12011-04-05 23:10:10166 EXPECT_FALSE(ip_endpoint2 < ip_endpoint1);
[email protected]76ff86a2011-03-04 03:21:31167
168 // IPv4 vs IPv6
[email protected]17e06f12011-04-05 23:10:10169 ip_endpoint1 = IPEndPoint(tests[0].ip_address, 81);
[email protected]76ff86a2011-03-04 03:21:31170 ip_endpoint2 = IPEndPoint(tests[2].ip_address, 80);
[email protected]17e06f12011-04-05 23:10:10171 EXPECT_TRUE(ip_endpoint1 < ip_endpoint2);
172 EXPECT_FALSE(ip_endpoint2 < ip_endpoint1);
[email protected]76ff86a2011-03-04 03:21:31173
174 // IPv4 vs IPv4
[email protected]17e06f12011-04-05 23:10:10175 ip_endpoint1 = IPEndPoint(tests[0].ip_address, 81);
[email protected]76ff86a2011-03-04 03:21:31176 ip_endpoint2 = IPEndPoint(tests[1].ip_address, 80);
177 EXPECT_TRUE(ip_endpoint1 < ip_endpoint2);
[email protected]17e06f12011-04-05 23:10:10178 EXPECT_FALSE(ip_endpoint2 < ip_endpoint1);
[email protected]76ff86a2011-03-04 03:21:31179
180 // IPv6 vs IPv6
[email protected]17e06f12011-04-05 23:10:10181 ip_endpoint1 = IPEndPoint(tests[2].ip_address, 81);
[email protected]76ff86a2011-03-04 03:21:31182 ip_endpoint2 = IPEndPoint(tests[3].ip_address, 80);
183 EXPECT_TRUE(ip_endpoint1 < ip_endpoint2);
[email protected]17e06f12011-04-05 23:10:10184 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]76ff86a2011-03-04 03:21:31191}
192
[email protected]17e06f12011-04-05 23:10:10193TEST_F(IPEndPointTest, ToString) {
[email protected]1a2123c62011-03-12 04:42:39194 IPEndPoint endpoint;
195 EXPECT_EQ(0, endpoint.port());
196
Ryan Sleevi4625214fa2018-05-10 16:42:45197 uint16_t port = 100;
198 for (const auto& test : tests) {
199 ++port;
200 IPEndPoint endpoint(test.ip_address, port);
[email protected]17e06f12011-04-05 23:10:10201 const std::string result = endpoint.ToString();
Ryan Sleevi4625214fa2018-05-10 16:42:45202 EXPECT_EQ(test.host_normalized + ":" + base::UintToString(port), result);
[email protected]1a2123c62011-03-12 04:42:39203 }
martijn7246f782016-02-18 09:00:51204
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]1a2123c62011-03-12 04:42:39210}
211
[email protected]76ff86a2011-03-04 03:21:31212} // namespace
213
214} // namespace net