• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2012 The Chromium Authors
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 <string.h>
8 
9 #include <optional>
10 #include <string>
11 #include <tuple>
12 
13 #include "base/check_op.h"
14 #include "base/notreached.h"
15 #include "base/numerics/safe_conversions.h"
16 #include "base/strings/string_number_conversions.h"
17 #include "base/sys_byteorder.h"
18 #include "base/values.h"
19 #include "build/build_config.h"
20 #include "net/base/ip_address.h"
21 #include "net/base/sockaddr_storage.h"
22 #include "net/base/sys_addrinfo.h"
23 #include "testing/gmock/include/gmock/gmock.h"
24 #include "testing/gtest/include/gtest/gtest.h"
25 #include "testing/platform_test.h"
26 
27 #if BUILDFLAG(IS_WIN)
28 #include <winsock2.h>
29 #include <ws2bth.h>
30 
31 #include "base/test/gtest_util.h"   // For EXPECT_DCHECK_DEATH
32 #include "net/base/winsock_util.h"  // For kBluetoothAddressSize
33 #elif BUILDFLAG(IS_POSIX)
34 #include <netinet/in.h>
35 #endif
36 
37 using testing::Optional;
38 
39 namespace net {
40 
41 namespace {
42 
43 // Retuns the port field of the |sockaddr|.
GetPortFieldFromSockaddr(const struct sockaddr * address,socklen_t address_len)44 const uint16_t* GetPortFieldFromSockaddr(const struct sockaddr* address,
45                                          socklen_t address_len) {
46   if (address->sa_family == AF_INET) {
47     DCHECK_LE(sizeof(sockaddr_in), static_cast<size_t>(address_len));
48     const struct sockaddr_in* sockaddr =
49         reinterpret_cast<const struct sockaddr_in*>(address);
50     return &sockaddr->sin_port;
51   } else if (address->sa_family == AF_INET6) {
52     DCHECK_LE(sizeof(sockaddr_in6), static_cast<size_t>(address_len));
53     const struct sockaddr_in6* sockaddr =
54         reinterpret_cast<const struct sockaddr_in6*>(address);
55     return &sockaddr->sin6_port;
56   } else {
57     NOTREACHED();
58     return nullptr;
59   }
60 }
61 
62 // Returns the value of port in |sockaddr| (in host byte ordering).
GetPortFromSockaddr(const struct sockaddr * address,socklen_t address_len)63 int GetPortFromSockaddr(const struct sockaddr* address, socklen_t address_len) {
64   const uint16_t* port_field = GetPortFieldFromSockaddr(address, address_len);
65   if (!port_field)
66     return -1;
67   return base::NetToHost16(*port_field);
68 }
69 
70 struct TestData {
71   std::string host;
72   std::string host_normalized;
73   bool ipv6;
74   IPAddress ip_address;
75 } tests[] = {
76     {"127.0.00.1", "127.0.0.1", false},
77     {"192.168.1.1", "192.168.1.1", false},
78     {"::1", "[::1]", true},
79     {"2001:db8:0::42", "[2001:db8::42]", true},
80 };
81 
82 class IPEndPointTest : public PlatformTest {
83  public:
SetUp()84   void SetUp() override {
85     // This is where we populate the TestData.
86     for (auto& test : tests) {
87       EXPECT_TRUE(test.ip_address.AssignFromIPLiteral(test.host));
88     }
89   }
90 };
91 
TEST_F(IPEndPointTest,Constructor)92 TEST_F(IPEndPointTest, Constructor) {
93   {
94     IPEndPoint endpoint;
95     EXPECT_EQ(0, endpoint.port());
96   }
97 
98   for (const auto& test : tests) {
99     IPEndPoint endpoint(test.ip_address, 80);
100     EXPECT_EQ(80, endpoint.port());
101     EXPECT_EQ(test.ip_address, endpoint.address());
102   }
103 }
104 
TEST_F(IPEndPointTest,Assignment)105 TEST_F(IPEndPointTest, Assignment) {
106   uint16_t port = 0;
107   for (const auto& test : tests) {
108     IPEndPoint src(test.ip_address, ++port);
109     IPEndPoint dest = src;
110 
111     EXPECT_EQ(src.port(), dest.port());
112     EXPECT_EQ(src.address(), dest.address());
113   }
114 }
115 
TEST_F(IPEndPointTest,Copy)116 TEST_F(IPEndPointTest, Copy) {
117   uint16_t port = 0;
118   for (const auto& test : tests) {
119     IPEndPoint src(test.ip_address, ++port);
120     IPEndPoint dest(src);
121 
122     EXPECT_EQ(src.port(), dest.port());
123     EXPECT_EQ(src.address(), dest.address());
124   }
125 }
126 
TEST_F(IPEndPointTest,ToFromSockAddr)127 TEST_F(IPEndPointTest, ToFromSockAddr) {
128   uint16_t port = 0;
129   for (const auto& test : tests) {
130     IPEndPoint ip_endpoint(test.ip_address, ++port);
131 
132     // Convert to a sockaddr.
133     SockaddrStorage storage;
134     EXPECT_TRUE(ip_endpoint.ToSockAddr(storage.addr, &storage.addr_len));
135 
136     // Basic verification.
137     socklen_t expected_size =
138         test.ipv6 ? sizeof(struct sockaddr_in6) : sizeof(struct sockaddr_in);
139     EXPECT_EQ(expected_size, storage.addr_len);
140     EXPECT_EQ(ip_endpoint.port(),
141               GetPortFromSockaddr(storage.addr, storage.addr_len));
142     // And convert back to an IPEndPoint.
143     IPEndPoint ip_endpoint2;
144     EXPECT_TRUE(ip_endpoint2.FromSockAddr(storage.addr, storage.addr_len));
145     EXPECT_EQ(ip_endpoint.port(), ip_endpoint2.port());
146     EXPECT_EQ(ip_endpoint.address(), ip_endpoint2.address());
147   }
148 }
149 
TEST_F(IPEndPointTest,ToSockAddrBufTooSmall)150 TEST_F(IPEndPointTest, ToSockAddrBufTooSmall) {
151   uint16_t port = 0;
152   for (const auto& test : tests) {
153     IPEndPoint ip_endpoint(test.ip_address, port);
154 
155     SockaddrStorage storage;
156     storage.addr_len = 3;  // size is too small!
157     EXPECT_FALSE(ip_endpoint.ToSockAddr(storage.addr, &storage.addr_len));
158   }
159 }
160 
TEST_F(IPEndPointTest,FromSockAddrBufTooSmall)161 TEST_F(IPEndPointTest, FromSockAddrBufTooSmall) {
162   struct sockaddr_in addr;
163   memset(&addr, 0, sizeof(addr));
164   addr.sin_family = AF_INET;
165   IPEndPoint ip_endpoint;
166   struct sockaddr* sockaddr = reinterpret_cast<struct sockaddr*>(&addr);
167   EXPECT_FALSE(ip_endpoint.FromSockAddr(sockaddr, sizeof(addr) - 1));
168 }
169 
170 #if BUILDFLAG(IS_WIN)
171 
172 namespace {
173 constexpr uint8_t kBluetoothAddrBytes[kBluetoothAddressSize] = {1, 2, 3,
174                                                                 4, 5, 6};
175 constexpr uint8_t kBluetoothAddrBytes2[kBluetoothAddressSize] = {1, 2, 3,
176                                                                  4, 5, 7};
177 const IPAddress kBluetoothAddress(kBluetoothAddrBytes);
178 const IPAddress kBluetoothAddress2(kBluetoothAddrBytes2);
179 
180 // Select a Bluetooth port that does not fit in a uint16_t.
181 constexpr uint32_t kBluetoothPort = std::numeric_limits<uint16_t>::max() + 1;
182 
BuildBluetoothSockAddr(const IPAddress & ip_address,uint32_t port)183 SOCKADDR_BTH BuildBluetoothSockAddr(const IPAddress& ip_address,
184                                     uint32_t port) {
185   SOCKADDR_BTH addr = {};
186   addr.addressFamily = AF_BTH;
187   DCHECK_LE(ip_address.bytes().size(), sizeof(addr.btAddr));
188   memcpy(&addr.btAddr, ip_address.bytes().data(), ip_address.bytes().size());
189   addr.port = port;
190   return addr;
191 }
192 }  // namespace
193 
TEST_F(IPEndPointTest,WinBluetoothSockAddrCompareWithSelf)194 TEST_F(IPEndPointTest, WinBluetoothSockAddrCompareWithSelf) {
195   IPEndPoint bt_endpoint;
196   SOCKADDR_BTH addr = BuildBluetoothSockAddr(kBluetoothAddress, kBluetoothPort);
197   EXPECT_TRUE(bt_endpoint.FromSockAddr(
198       reinterpret_cast<const struct sockaddr*>(&addr), sizeof(addr)));
199   EXPECT_EQ(bt_endpoint.address(), kBluetoothAddress);
200   EXPECT_EQ(bt_endpoint.GetFamily(), AddressFamily::ADDRESS_FAMILY_UNSPECIFIED);
201   EXPECT_EQ(bt_endpoint.GetSockAddrFamily(), AF_BTH);
202   // Comparison functions should agree that `bt_endpoint` equals itself.
203   EXPECT_FALSE(bt_endpoint < bt_endpoint);
204   EXPECT_FALSE(bt_endpoint != bt_endpoint);
205   EXPECT_TRUE(bt_endpoint == bt_endpoint);
206   // Test that IPv4/IPv6-only methods crash.
207   EXPECT_DCHECK_DEATH(bt_endpoint.port());
208   SockaddrStorage storage;
209   EXPECT_DCHECK_DEATH(
210       std::ignore = bt_endpoint.ToSockAddr(storage.addr, &storage.addr_len));
211   EXPECT_DCHECK_DEATH(bt_endpoint.ToString());
212   EXPECT_DCHECK_DEATH(bt_endpoint.ToStringWithoutPort());
213 }
214 
TEST_F(IPEndPointTest,WinBluetoothSockAddrCompareWithNonBluetooth)215 TEST_F(IPEndPointTest, WinBluetoothSockAddrCompareWithNonBluetooth) {
216   IPEndPoint bt_endpoint;
217   SOCKADDR_BTH addr = BuildBluetoothSockAddr(kBluetoothAddress, kBluetoothPort);
218   EXPECT_TRUE(bt_endpoint.FromSockAddr(
219       reinterpret_cast<const struct sockaddr*>(&addr), sizeof(addr)));
220 
221   // Compare `bt_endpoint` with non-Bluetooth endpoints.
222   for (const auto& test : tests) {
223     IPEndPoint endpoint(test.ip_address, 80);
224     if (test.ip_address.IsIPv4()) {
225       EXPECT_FALSE(bt_endpoint < endpoint);
226     } else {
227       EXPECT_TRUE(test.ip_address.IsIPv6());
228       EXPECT_TRUE(bt_endpoint < endpoint);
229     }
230     EXPECT_TRUE(bt_endpoint != endpoint);
231     EXPECT_FALSE(bt_endpoint == endpoint);
232   }
233 }
234 
TEST_F(IPEndPointTest,WinBluetoothSockAddrCompareWithCopy)235 TEST_F(IPEndPointTest, WinBluetoothSockAddrCompareWithCopy) {
236   IPEndPoint bt_endpoint;
237   SOCKADDR_BTH addr = BuildBluetoothSockAddr(kBluetoothAddress, kBluetoothPort);
238   EXPECT_TRUE(bt_endpoint.FromSockAddr(
239       reinterpret_cast<const struct sockaddr*>(&addr), sizeof(addr)));
240 
241   // Verify that a copy's accessors return the same values as the original's.
242   IPEndPoint bt_endpoint_other(bt_endpoint);
243   EXPECT_EQ(bt_endpoint.address(), bt_endpoint_other.address());
244   EXPECT_EQ(bt_endpoint.GetFamily(), bt_endpoint_other.GetFamily());
245   EXPECT_EQ(bt_endpoint.GetSockAddrFamily(),
246             bt_endpoint_other.GetSockAddrFamily());
247   // Comparison functions should agree that the endpoints are equal.
248   EXPECT_FALSE(bt_endpoint < bt_endpoint_other);
249   EXPECT_FALSE(bt_endpoint != bt_endpoint_other);
250   EXPECT_TRUE(bt_endpoint == bt_endpoint_other);
251   // Test that IPv4/IPv6-only methods crash.
252   EXPECT_DCHECK_DEATH(bt_endpoint_other.port());
253   SockaddrStorage storage;
254   EXPECT_DCHECK_DEATH(std::ignore = bt_endpoint_other.ToSockAddr(
255                           storage.addr, &storage.addr_len));
256   EXPECT_DCHECK_DEATH(bt_endpoint_other.ToString());
257   EXPECT_DCHECK_DEATH(bt_endpoint_other.ToStringWithoutPort());
258 }
259 
TEST_F(IPEndPointTest,WinBluetoothSockAddrCompareWithDifferentPort)260 TEST_F(IPEndPointTest, WinBluetoothSockAddrCompareWithDifferentPort) {
261   IPEndPoint bt_endpoint;
262   SOCKADDR_BTH addr = BuildBluetoothSockAddr(kBluetoothAddress, kBluetoothPort);
263   EXPECT_TRUE(bt_endpoint.FromSockAddr(
264       reinterpret_cast<const struct sockaddr*>(&addr), sizeof(addr)));
265 
266   // Compare with another IPEndPoint that has a different port.
267   IPEndPoint bt_endpoint_other;
268   SOCKADDR_BTH addr2 =
269       BuildBluetoothSockAddr(kBluetoothAddress, kBluetoothPort + 1);
270   EXPECT_TRUE(bt_endpoint_other.FromSockAddr(
271       reinterpret_cast<const struct sockaddr*>(&addr2), sizeof(addr2)));
272   EXPECT_EQ(bt_endpoint.address(), bt_endpoint_other.address());
273   EXPECT_EQ(bt_endpoint.GetFamily(), bt_endpoint_other.GetFamily());
274   EXPECT_EQ(bt_endpoint.GetSockAddrFamily(),
275             bt_endpoint_other.GetSockAddrFamily());
276   // Comparison functions should agree that `bt_endpoint == bt_endpoint_other`
277   // because they have the same address and Bluetooth ports are not considered
278   // by comparison functions.
279   EXPECT_FALSE(bt_endpoint < bt_endpoint_other);
280   EXPECT_FALSE(bt_endpoint != bt_endpoint_other);
281   EXPECT_TRUE(bt_endpoint == bt_endpoint_other);
282   // Test that IPv4/IPv6-only methods crash.
283   EXPECT_DCHECK_DEATH(bt_endpoint_other.port());
284   SockaddrStorage storage;
285   EXPECT_DCHECK_DEATH(std::ignore = bt_endpoint_other.ToSockAddr(
286                           storage.addr, &storage.addr_len));
287   EXPECT_DCHECK_DEATH(bt_endpoint_other.ToString());
288   EXPECT_DCHECK_DEATH(bt_endpoint_other.ToStringWithoutPort());
289 }
290 
TEST_F(IPEndPointTest,WinBluetoothSockAddrCompareWithDifferentAddress)291 TEST_F(IPEndPointTest, WinBluetoothSockAddrCompareWithDifferentAddress) {
292   IPEndPoint bt_endpoint;
293   SOCKADDR_BTH addr = BuildBluetoothSockAddr(kBluetoothAddress, kBluetoothPort);
294   EXPECT_TRUE(bt_endpoint.FromSockAddr(
295       reinterpret_cast<const struct sockaddr*>(&addr), sizeof(addr)));
296 
297   // Compare with another IPEndPoint that has a different address.
298   IPEndPoint bt_endpoint_other;
299   SOCKADDR_BTH addr2 =
300       BuildBluetoothSockAddr(kBluetoothAddress2, kBluetoothPort);
301   EXPECT_TRUE(bt_endpoint_other.FromSockAddr(
302       reinterpret_cast<const struct sockaddr*>(&addr2), sizeof(addr2)));
303   EXPECT_LT(bt_endpoint.address(), bt_endpoint_other.address());
304   EXPECT_EQ(bt_endpoint.GetFamily(), bt_endpoint_other.GetFamily());
305   EXPECT_EQ(bt_endpoint.GetSockAddrFamily(),
306             bt_endpoint_other.GetSockAddrFamily());
307   // Comparison functions should agree that `bt_endpoint < bt_endpoint_other`
308   // due to lexicographic comparison of the address bytes.
309   EXPECT_TRUE(bt_endpoint < bt_endpoint_other);
310   EXPECT_TRUE(bt_endpoint != bt_endpoint_other);
311   EXPECT_FALSE(bt_endpoint == bt_endpoint_other);
312   // Test that IPv4/IPv6-only methods crash.
313   EXPECT_DCHECK_DEATH(bt_endpoint_other.port());
314   SockaddrStorage storage;
315   EXPECT_DCHECK_DEATH(std::ignore = bt_endpoint_other.ToSockAddr(
316                           storage.addr, &storage.addr_len));
317   EXPECT_DCHECK_DEATH(bt_endpoint_other.ToString());
318   EXPECT_DCHECK_DEATH(bt_endpoint_other.ToStringWithoutPort());
319 }
320 #endif
321 
TEST_F(IPEndPointTest,Equality)322 TEST_F(IPEndPointTest, Equality) {
323   uint16_t port = 0;
324   for (const auto& test : tests) {
325     IPEndPoint src(test.ip_address, ++port);
326     IPEndPoint dest(src);
327     EXPECT_TRUE(src == dest);
328   }
329 }
330 
TEST_F(IPEndPointTest,LessThan)331 TEST_F(IPEndPointTest, LessThan) {
332   // Vary by port.
333   IPEndPoint ip_endpoint1(tests[0].ip_address, 100);
334   IPEndPoint ip_endpoint2(tests[0].ip_address, 1000);
335   EXPECT_TRUE(ip_endpoint1 < ip_endpoint2);
336   EXPECT_FALSE(ip_endpoint2 < ip_endpoint1);
337 
338   // IPv4 vs IPv6
339   ip_endpoint1 = IPEndPoint(tests[0].ip_address, 81);
340   ip_endpoint2 = IPEndPoint(tests[2].ip_address, 80);
341   EXPECT_TRUE(ip_endpoint1 < ip_endpoint2);
342   EXPECT_FALSE(ip_endpoint2 < ip_endpoint1);
343 
344   // IPv4 vs IPv4
345   ip_endpoint1 = IPEndPoint(tests[0].ip_address, 81);
346   ip_endpoint2 = IPEndPoint(tests[1].ip_address, 80);
347   EXPECT_TRUE(ip_endpoint1 < ip_endpoint2);
348   EXPECT_FALSE(ip_endpoint2 < ip_endpoint1);
349 
350   // IPv6 vs IPv6
351   ip_endpoint1 = IPEndPoint(tests[2].ip_address, 81);
352   ip_endpoint2 = IPEndPoint(tests[3].ip_address, 80);
353   EXPECT_TRUE(ip_endpoint1 < ip_endpoint2);
354   EXPECT_FALSE(ip_endpoint2 < ip_endpoint1);
355 
356   // Compare equivalent endpoints.
357   ip_endpoint1 = IPEndPoint(tests[0].ip_address, 80);
358   ip_endpoint2 = IPEndPoint(tests[0].ip_address, 80);
359   EXPECT_FALSE(ip_endpoint1 < ip_endpoint2);
360   EXPECT_FALSE(ip_endpoint2 < ip_endpoint1);
361 }
362 
TEST_F(IPEndPointTest,ToString)363 TEST_F(IPEndPointTest, ToString) {
364   {
365     IPEndPoint endpoint;
366     EXPECT_EQ(0, endpoint.port());
367   }
368 
369   uint16_t port = 100;
370   for (const auto& test : tests) {
371     ++port;
372     IPEndPoint endpoint(test.ip_address, port);
373     const std::string result = endpoint.ToString();
374     EXPECT_EQ(test.host_normalized + ":" + base::NumberToString(port), result);
375   }
376 
377   // ToString() shouldn't crash on invalid addresses.
378   IPAddress invalid_address;
379   IPEndPoint invalid_endpoint(invalid_address, 8080);
380   EXPECT_EQ("", invalid_endpoint.ToString());
381   EXPECT_EQ("", invalid_endpoint.ToStringWithoutPort());
382 }
383 
TEST_F(IPEndPointTest,RoundtripThroughValue)384 TEST_F(IPEndPointTest, RoundtripThroughValue) {
385   for (const auto& test : tests) {
386     IPEndPoint endpoint(test.ip_address, 1645);
387     base::Value value = endpoint.ToValue();
388 
389     EXPECT_THAT(IPEndPoint::FromValue(value), Optional(endpoint));
390   }
391 }
392 
TEST_F(IPEndPointTest,FromGarbageValue)393 TEST_F(IPEndPointTest, FromGarbageValue) {
394   base::Value value(123);
395   EXPECT_FALSE(IPEndPoint::FromValue(value).has_value());
396 }
397 
TEST_F(IPEndPointTest,FromMalformedValues)398 TEST_F(IPEndPointTest, FromMalformedValues) {
399   for (const auto& test : tests) {
400     base::Value valid_value = IPEndPoint(test.ip_address, 1111).ToValue();
401     ASSERT_TRUE(IPEndPoint::FromValue(valid_value).has_value());
402 
403     base::Value missing_address = valid_value.Clone();
404     ASSERT_TRUE(missing_address.GetDict().Remove("address"));
405     EXPECT_FALSE(IPEndPoint::FromValue(missing_address).has_value());
406 
407     base::Value missing_port = valid_value.Clone();
408     ASSERT_TRUE(missing_port.GetDict().Remove("port"));
409     EXPECT_FALSE(IPEndPoint::FromValue(missing_port).has_value());
410 
411     base::Value invalid_address = valid_value.Clone();
412     *invalid_address.GetDict().Find("address") = base::Value("1.2.3.4.5");
413     EXPECT_FALSE(IPEndPoint::FromValue(invalid_address).has_value());
414 
415     base::Value negative_port = valid_value.Clone();
416     *negative_port.GetDict().Find("port") = base::Value(-1);
417     EXPECT_FALSE(IPEndPoint::FromValue(negative_port).has_value());
418 
419     base::Value large_port = valid_value.Clone();
420     *large_port.GetDict().Find("port") = base::Value(66000);
421     EXPECT_FALSE(IPEndPoint::FromValue(large_port).has_value());
422   }
423 }
424 
425 }  // namespace
426 
427 }  // namespace net
428