• 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 
30 #include <ws2bth.h>
31 
32 #include "base/test/gtest_util.h"   // For EXPECT_DCHECK_DEATH
33 #include "net/base/winsock_util.h"  // For kBluetoothAddressSize
34 #elif BUILDFLAG(IS_POSIX)
35 #include <netinet/in.h>
36 #endif
37 
38 using testing::Optional;
39 
40 namespace net {
41 
42 namespace {
43 
44 // Retuns the port field of the |sockaddr|.
GetPortFieldFromSockaddr(const struct sockaddr * address,socklen_t address_len)45 const uint16_t* GetPortFieldFromSockaddr(const struct sockaddr* address,
46                                          socklen_t address_len) {
47   if (address->sa_family == AF_INET) {
48     DCHECK_LE(sizeof(sockaddr_in), static_cast<size_t>(address_len));
49     const struct sockaddr_in* sockaddr =
50         reinterpret_cast<const struct sockaddr_in*>(address);
51     return &sockaddr->sin_port;
52   } else if (address->sa_family == AF_INET6) {
53     DCHECK_LE(sizeof(sockaddr_in6), static_cast<size_t>(address_len));
54     const struct sockaddr_in6* sockaddr =
55         reinterpret_cast<const struct sockaddr_in6*>(address);
56     return &sockaddr->sin6_port;
57   } else {
58     NOTREACHED();
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 constexpr uint32_t kMaxFakeInterfaceIndex = 10;
71 
FakeNameToIndexFunc(const char * name)72 uint32_t FakeNameToIndexFunc(const char* name) {
73   uint32_t index = 0;
74   const bool ok = base::StringToUint(name, &index);
75   if (!ok || index > kMaxFakeInterfaceIndex) {
76     return 0;
77   }
78   return index;
79 }
80 
FakeIndexToNameFunc(unsigned int index,char * ifname)81 char* FakeIndexToNameFunc(unsigned int index, char* ifname) {
82   if (index > kMaxFakeInterfaceIndex) {
83     return nullptr;
84   }
85   std::string name = base::NumberToString(index);
86   ifname[0] = name[0];
87   return ifname;
88 }
89 
90 struct TestData {
91   std::string host;
92   std::string host_normalized;
93   bool ipv6;
94   IPAddress ip_address;
95   std::optional<uint32_t> scope_id = std::nullopt;
96 } tests[] = {
97     {"127.0.00.1", "127.0.0.1", false},
98     {"192.168.1.1", "192.168.1.1", false},
99     {"::1", "[::1]", true},
100     {"2001:db8:0::42", "[2001:db8::42]", true},
101     {"fe80::1", "[fe80::1]", true, IPAddress(), /*scope_id=*/1},
102 };
103 
104 class IPEndPointTest : public PlatformTest {
105  public:
SetUp()106   void SetUp() override {
107     IPEndPoint::SetNameToIndexFuncForTesting(FakeNameToIndexFunc);
108     IPEndPoint::SetIndexToNameFuncForTesting(FakeIndexToNameFunc);
109 
110     // This is where we populate the TestData.
111     for (auto& test : tests) {
112       EXPECT_TRUE(test.ip_address.AssignFromIPLiteral(test.host));
113     }
114   }
115 
TearDown()116   void TearDown() override {
117     IPEndPoint::SetNameToIndexFuncForTesting(nullptr);
118     IPEndPoint::SetIndexToNameFuncForTesting(nullptr);
119   }
120 };
121 
TEST_F(IPEndPointTest,Constructor)122 TEST_F(IPEndPointTest, Constructor) {
123   {
124     IPEndPoint endpoint;
125     EXPECT_EQ(0, endpoint.port());
126   }
127 
128   for (const auto& test : tests) {
129     IPEndPoint endpoint(test.ip_address, 80, test.scope_id);
130     EXPECT_EQ(80, endpoint.port());
131     EXPECT_EQ(test.ip_address, endpoint.address());
132     EXPECT_EQ(test.scope_id, endpoint.scope_id());
133   }
134 }
135 
TEST_F(IPEndPointTest,Assignment)136 TEST_F(IPEndPointTest, Assignment) {
137   uint16_t port = 0;
138   for (const auto& test : tests) {
139     IPEndPoint src(test.ip_address, ++port, test.scope_id);
140     IPEndPoint dest = src;
141 
142     EXPECT_EQ(src.port(), dest.port());
143     EXPECT_EQ(src.address(), dest.address());
144     EXPECT_EQ(src.scope_id(), dest.scope_id());
145   }
146 }
147 
TEST_F(IPEndPointTest,Copy)148 TEST_F(IPEndPointTest, Copy) {
149   uint16_t port = 0;
150   for (const auto& test : tests) {
151     IPEndPoint src(test.ip_address, ++port, test.scope_id);
152     IPEndPoint dest(src);
153 
154     EXPECT_EQ(src.port(), dest.port());
155     EXPECT_EQ(src.address(), dest.address());
156     EXPECT_EQ(src.scope_id(), dest.scope_id());
157   }
158 }
159 
TEST_F(IPEndPointTest,ToFromSockAddr)160 TEST_F(IPEndPointTest, ToFromSockAddr) {
161   uint16_t port = 0;
162   for (const auto& test : tests) {
163     IPEndPoint ip_endpoint(test.ip_address, ++port, test.scope_id);
164 
165     // Convert to a sockaddr.
166     SockaddrStorage storage;
167     EXPECT_TRUE(ip_endpoint.ToSockAddr(storage.addr, &storage.addr_len));
168 
169     // Basic verification.
170     socklen_t expected_size =
171         test.ipv6 ? sizeof(struct sockaddr_in6) : sizeof(struct sockaddr_in);
172     EXPECT_EQ(expected_size, storage.addr_len);
173     EXPECT_EQ(ip_endpoint.port(),
174               GetPortFromSockaddr(storage.addr, storage.addr_len));
175     if (test.ipv6) {
176       uint32_t scope_id =
177           reinterpret_cast<struct sockaddr_in6*>(storage.addr)->sin6_scope_id;
178       EXPECT_EQ(scope_id, test.scope_id.value_or(0));
179     }
180     // And convert back to an IPEndPoint.
181     IPEndPoint ip_endpoint2;
182     EXPECT_TRUE(ip_endpoint2.FromSockAddr(storage.addr, storage.addr_len));
183     EXPECT_EQ(ip_endpoint.port(), ip_endpoint2.port());
184     EXPECT_EQ(ip_endpoint.address(), ip_endpoint2.address());
185     EXPECT_EQ(ip_endpoint.scope_id(), ip_endpoint2.scope_id());
186   }
187 }
188 
TEST_F(IPEndPointTest,ToSockAddrBufTooSmall)189 TEST_F(IPEndPointTest, ToSockAddrBufTooSmall) {
190   uint16_t port = 0;
191   for (const auto& test : tests) {
192     IPEndPoint ip_endpoint(test.ip_address, port);
193 
194     SockaddrStorage storage;
195     storage.addr_len = 3;  // size is too small!
196     EXPECT_FALSE(ip_endpoint.ToSockAddr(storage.addr, &storage.addr_len));
197   }
198 }
199 
TEST_F(IPEndPointTest,FromSockAddrBufTooSmall)200 TEST_F(IPEndPointTest, FromSockAddrBufTooSmall) {
201   struct sockaddr_in addr;
202   memset(&addr, 0, sizeof(addr));
203   addr.sin_family = AF_INET;
204   IPEndPoint ip_endpoint;
205   struct sockaddr* sockaddr = reinterpret_cast<struct sockaddr*>(&addr);
206   EXPECT_FALSE(ip_endpoint.FromSockAddr(sockaddr, sizeof(addr) - 1));
207 }
208 
209 #if BUILDFLAG(IS_WIN)
210 
211 namespace {
212 constexpr uint8_t kBluetoothAddrBytes[kBluetoothAddressSize] = {1, 2, 3,
213                                                                 4, 5, 6};
214 constexpr uint8_t kBluetoothAddrBytes2[kBluetoothAddressSize] = {1, 2, 3,
215                                                                  4, 5, 7};
216 const IPAddress kBluetoothAddress(kBluetoothAddrBytes);
217 const IPAddress kBluetoothAddress2(kBluetoothAddrBytes2);
218 
219 // Select a Bluetooth port that does not fit in a uint16_t.
220 constexpr uint32_t kBluetoothPort = std::numeric_limits<uint16_t>::max() + 1;
221 
BuildBluetoothSockAddr(const IPAddress & ip_address,uint32_t port)222 SOCKADDR_BTH BuildBluetoothSockAddr(const IPAddress& ip_address,
223                                     uint32_t port) {
224   SOCKADDR_BTH addr = {};
225   addr.addressFamily = AF_BTH;
226   DCHECK_LE(ip_address.bytes().size(), sizeof(addr.btAddr));
227   memcpy(&addr.btAddr, ip_address.bytes().data(), ip_address.bytes().size());
228   addr.port = port;
229   return addr;
230 }
231 }  // namespace
232 
TEST_F(IPEndPointTest,WinBluetoothSockAddrCompareWithSelf)233 TEST_F(IPEndPointTest, WinBluetoothSockAddrCompareWithSelf) {
234   IPEndPoint bt_endpoint;
235   SOCKADDR_BTH addr = BuildBluetoothSockAddr(kBluetoothAddress, kBluetoothPort);
236   EXPECT_TRUE(bt_endpoint.FromSockAddr(
237       reinterpret_cast<const struct sockaddr*>(&addr), sizeof(addr)));
238   EXPECT_EQ(bt_endpoint.address(), kBluetoothAddress);
239   EXPECT_EQ(bt_endpoint.GetFamily(), AddressFamily::ADDRESS_FAMILY_UNSPECIFIED);
240   EXPECT_EQ(bt_endpoint.GetSockAddrFamily(), AF_BTH);
241   // Comparison functions should agree that `bt_endpoint` equals itself.
242   EXPECT_FALSE(bt_endpoint < bt_endpoint);
243   EXPECT_FALSE(bt_endpoint != bt_endpoint);
244   EXPECT_TRUE(bt_endpoint == bt_endpoint);
245   // Test that IPv4/IPv6-only methods crash.
246   EXPECT_DCHECK_DEATH(bt_endpoint.port());
247   SockaddrStorage storage;
248   EXPECT_DCHECK_DEATH(
249       std::ignore = bt_endpoint.ToSockAddr(storage.addr, &storage.addr_len));
250   EXPECT_DCHECK_DEATH(bt_endpoint.ToString());
251   EXPECT_DCHECK_DEATH(bt_endpoint.ToStringWithoutPort());
252 }
253 
TEST_F(IPEndPointTest,WinBluetoothSockAddrCompareWithNonBluetooth)254 TEST_F(IPEndPointTest, WinBluetoothSockAddrCompareWithNonBluetooth) {
255   IPEndPoint bt_endpoint;
256   SOCKADDR_BTH addr = BuildBluetoothSockAddr(kBluetoothAddress, kBluetoothPort);
257   EXPECT_TRUE(bt_endpoint.FromSockAddr(
258       reinterpret_cast<const struct sockaddr*>(&addr), sizeof(addr)));
259 
260   // Compare `bt_endpoint` with non-Bluetooth endpoints.
261   for (const auto& test : tests) {
262     IPEndPoint endpoint(test.ip_address, 80);
263     if (test.ip_address.IsIPv4()) {
264       EXPECT_FALSE(bt_endpoint < endpoint);
265     } else {
266       EXPECT_TRUE(test.ip_address.IsIPv6());
267       EXPECT_TRUE(bt_endpoint < endpoint);
268     }
269     EXPECT_TRUE(bt_endpoint != endpoint);
270     EXPECT_FALSE(bt_endpoint == endpoint);
271   }
272 }
273 
TEST_F(IPEndPointTest,WinBluetoothSockAddrCompareWithCopy)274 TEST_F(IPEndPointTest, WinBluetoothSockAddrCompareWithCopy) {
275   IPEndPoint bt_endpoint;
276   SOCKADDR_BTH addr = BuildBluetoothSockAddr(kBluetoothAddress, kBluetoothPort);
277   EXPECT_TRUE(bt_endpoint.FromSockAddr(
278       reinterpret_cast<const struct sockaddr*>(&addr), sizeof(addr)));
279 
280   // Verify that a copy's accessors return the same values as the original's.
281   IPEndPoint bt_endpoint_other(bt_endpoint);
282   EXPECT_EQ(bt_endpoint.address(), bt_endpoint_other.address());
283   EXPECT_EQ(bt_endpoint.GetFamily(), bt_endpoint_other.GetFamily());
284   EXPECT_EQ(bt_endpoint.GetSockAddrFamily(),
285             bt_endpoint_other.GetSockAddrFamily());
286   // Comparison functions should agree that the endpoints are equal.
287   EXPECT_FALSE(bt_endpoint < bt_endpoint_other);
288   EXPECT_FALSE(bt_endpoint != bt_endpoint_other);
289   EXPECT_TRUE(bt_endpoint == bt_endpoint_other);
290   // Test that IPv4/IPv6-only methods crash.
291   EXPECT_DCHECK_DEATH(bt_endpoint_other.port());
292   SockaddrStorage storage;
293   EXPECT_DCHECK_DEATH(std::ignore = bt_endpoint_other.ToSockAddr(
294                           storage.addr, &storage.addr_len));
295   EXPECT_DCHECK_DEATH(bt_endpoint_other.ToString());
296   EXPECT_DCHECK_DEATH(bt_endpoint_other.ToStringWithoutPort());
297 }
298 
TEST_F(IPEndPointTest,WinBluetoothSockAddrCompareWithDifferentPort)299 TEST_F(IPEndPointTest, WinBluetoothSockAddrCompareWithDifferentPort) {
300   IPEndPoint bt_endpoint;
301   SOCKADDR_BTH addr = BuildBluetoothSockAddr(kBluetoothAddress, kBluetoothPort);
302   EXPECT_TRUE(bt_endpoint.FromSockAddr(
303       reinterpret_cast<const struct sockaddr*>(&addr), sizeof(addr)));
304 
305   // Compare with another IPEndPoint that has a different port.
306   IPEndPoint bt_endpoint_other;
307   SOCKADDR_BTH addr2 =
308       BuildBluetoothSockAddr(kBluetoothAddress, kBluetoothPort + 1);
309   EXPECT_TRUE(bt_endpoint_other.FromSockAddr(
310       reinterpret_cast<const struct sockaddr*>(&addr2), sizeof(addr2)));
311   EXPECT_EQ(bt_endpoint.address(), bt_endpoint_other.address());
312   EXPECT_EQ(bt_endpoint.GetFamily(), bt_endpoint_other.GetFamily());
313   EXPECT_EQ(bt_endpoint.GetSockAddrFamily(),
314             bt_endpoint_other.GetSockAddrFamily());
315   // Comparison functions should agree that `bt_endpoint == bt_endpoint_other`
316   // because they have the same address and Bluetooth ports are not considered
317   // by comparison functions.
318   EXPECT_FALSE(bt_endpoint < bt_endpoint_other);
319   EXPECT_FALSE(bt_endpoint != bt_endpoint_other);
320   EXPECT_TRUE(bt_endpoint == bt_endpoint_other);
321   // Test that IPv4/IPv6-only methods crash.
322   EXPECT_DCHECK_DEATH(bt_endpoint_other.port());
323   SockaddrStorage storage;
324   EXPECT_DCHECK_DEATH(std::ignore = bt_endpoint_other.ToSockAddr(
325                           storage.addr, &storage.addr_len));
326   EXPECT_DCHECK_DEATH(bt_endpoint_other.ToString());
327   EXPECT_DCHECK_DEATH(bt_endpoint_other.ToStringWithoutPort());
328 }
329 
TEST_F(IPEndPointTest,WinBluetoothSockAddrCompareWithDifferentAddress)330 TEST_F(IPEndPointTest, WinBluetoothSockAddrCompareWithDifferentAddress) {
331   IPEndPoint bt_endpoint;
332   SOCKADDR_BTH addr = BuildBluetoothSockAddr(kBluetoothAddress, kBluetoothPort);
333   EXPECT_TRUE(bt_endpoint.FromSockAddr(
334       reinterpret_cast<const struct sockaddr*>(&addr), sizeof(addr)));
335 
336   // Compare with another IPEndPoint that has a different address.
337   IPEndPoint bt_endpoint_other;
338   SOCKADDR_BTH addr2 =
339       BuildBluetoothSockAddr(kBluetoothAddress2, kBluetoothPort);
340   EXPECT_TRUE(bt_endpoint_other.FromSockAddr(
341       reinterpret_cast<const struct sockaddr*>(&addr2), sizeof(addr2)));
342   EXPECT_LT(bt_endpoint.address(), bt_endpoint_other.address());
343   EXPECT_EQ(bt_endpoint.GetFamily(), bt_endpoint_other.GetFamily());
344   EXPECT_EQ(bt_endpoint.GetSockAddrFamily(),
345             bt_endpoint_other.GetSockAddrFamily());
346   // Comparison functions should agree that `bt_endpoint < bt_endpoint_other`
347   // due to lexicographic comparison of the address bytes.
348   EXPECT_TRUE(bt_endpoint < bt_endpoint_other);
349   EXPECT_TRUE(bt_endpoint != bt_endpoint_other);
350   EXPECT_FALSE(bt_endpoint == bt_endpoint_other);
351   // Test that IPv4/IPv6-only methods crash.
352   EXPECT_DCHECK_DEATH(bt_endpoint_other.port());
353   SockaddrStorage storage;
354   EXPECT_DCHECK_DEATH(std::ignore = bt_endpoint_other.ToSockAddr(
355                           storage.addr, &storage.addr_len));
356   EXPECT_DCHECK_DEATH(bt_endpoint_other.ToString());
357   EXPECT_DCHECK_DEATH(bt_endpoint_other.ToStringWithoutPort());
358 }
359 #endif
360 
TEST_F(IPEndPointTest,Equality)361 TEST_F(IPEndPointTest, Equality) {
362   uint16_t port = 0;
363   for (const auto& test : tests) {
364     IPEndPoint src(test.ip_address, ++port, test.scope_id);
365     IPEndPoint dest(src);
366     EXPECT_TRUE(src == dest);
367   }
368 
369   // Compare scope_id.
370   const auto v6_link_local_address = *IPAddress::FromIPLiteral("fe80::1");
371   IPEndPoint ip_endpoint1 =
372       IPEndPoint(v6_link_local_address, 80, /*scope_id=*/1);
373   IPEndPoint ip_endpoint2 =
374       IPEndPoint(v6_link_local_address, 80, /*scope_id=*/1);
375   EXPECT_EQ(ip_endpoint1, ip_endpoint2);
376   ip_endpoint2 = IPEndPoint(v6_link_local_address, 80, /*scope_id=*/2);
377   EXPECT_NE(ip_endpoint1, ip_endpoint2);
378 }
379 
TEST_F(IPEndPointTest,LessThan)380 TEST_F(IPEndPointTest, LessThan) {
381   // Vary by port.
382   IPEndPoint ip_endpoint1(tests[0].ip_address, 100);
383   IPEndPoint ip_endpoint2(tests[0].ip_address, 1000);
384   EXPECT_TRUE(ip_endpoint1 < ip_endpoint2);
385   EXPECT_FALSE(ip_endpoint2 < ip_endpoint1);
386 
387   // IPv4 vs IPv6
388   ip_endpoint1 = IPEndPoint(tests[0].ip_address, 81);
389   ip_endpoint2 = IPEndPoint(tests[2].ip_address, 80);
390   EXPECT_TRUE(ip_endpoint1 < ip_endpoint2);
391   EXPECT_FALSE(ip_endpoint2 < ip_endpoint1);
392 
393   // IPv4 vs IPv4
394   ip_endpoint1 = IPEndPoint(tests[0].ip_address, 81);
395   ip_endpoint2 = IPEndPoint(tests[1].ip_address, 80);
396   EXPECT_TRUE(ip_endpoint1 < ip_endpoint2);
397   EXPECT_FALSE(ip_endpoint2 < ip_endpoint1);
398 
399   // IPv6 vs IPv6
400   ip_endpoint1 = IPEndPoint(tests[2].ip_address, 81);
401   ip_endpoint2 = IPEndPoint(tests[3].ip_address, 80);
402   EXPECT_TRUE(ip_endpoint1 < ip_endpoint2);
403   EXPECT_FALSE(ip_endpoint2 < ip_endpoint1);
404 
405   // Compare equivalent endpoints.
406   ip_endpoint1 = IPEndPoint(tests[0].ip_address, 80);
407   ip_endpoint2 = IPEndPoint(tests[0].ip_address, 80);
408   EXPECT_FALSE(ip_endpoint1 < ip_endpoint2);
409   EXPECT_FALSE(ip_endpoint2 < ip_endpoint1);
410 }
411 
TEST_F(IPEndPointTest,ToString)412 TEST_F(IPEndPointTest, ToString) {
413   {
414     IPEndPoint endpoint;
415     EXPECT_EQ(0, endpoint.port());
416   }
417 
418   uint16_t port = 100;
419   for (const auto& test : tests) {
420     ++port;
421     IPEndPoint endpoint(test.ip_address, port, test.scope_id);
422     const std::string result = endpoint.ToString();
423     EXPECT_EQ(test.host_normalized + ":" + base::NumberToString(port), result);
424   }
425 
426   // ToString() shouldn't crash on invalid addresses.
427   IPAddress invalid_address;
428   IPEndPoint invalid_endpoint(invalid_address, 8080);
429   EXPECT_EQ("", invalid_endpoint.ToString());
430   EXPECT_EQ("", invalid_endpoint.ToStringWithoutPort());
431 }
432 
TEST_F(IPEndPointTest,RoundtripThroughValue)433 TEST_F(IPEndPointTest, RoundtripThroughValue) {
434   for (const auto& test : tests) {
435     IPEndPoint endpoint(test.ip_address, 1645, test.scope_id);
436     base::Value value = endpoint.ToValue();
437 
438     EXPECT_THAT(IPEndPoint::FromValue(value), Optional(endpoint));
439   }
440 }
441 
TEST_F(IPEndPointTest,FromGarbageValue)442 TEST_F(IPEndPointTest, FromGarbageValue) {
443   base::Value value(123);
444   EXPECT_FALSE(IPEndPoint::FromValue(value).has_value());
445 }
446 
TEST_F(IPEndPointTest,FromMalformedValues)447 TEST_F(IPEndPointTest, FromMalformedValues) {
448   for (const auto& test : tests) {
449     base::Value valid_value =
450         IPEndPoint(test.ip_address, 1111, test.scope_id).ToValue();
451     ASSERT_TRUE(IPEndPoint::FromValue(valid_value).has_value());
452 
453     base::Value missing_address = valid_value.Clone();
454     ASSERT_TRUE(missing_address.GetDict().Remove("address"));
455     EXPECT_FALSE(IPEndPoint::FromValue(missing_address).has_value());
456 
457     base::Value missing_port = valid_value.Clone();
458     ASSERT_TRUE(missing_port.GetDict().Remove("port"));
459     EXPECT_FALSE(IPEndPoint::FromValue(missing_port).has_value());
460 
461     base::Value invalid_address = valid_value.Clone();
462     *invalid_address.GetDict().Find("address") = base::Value("1.2.3.4.5");
463     EXPECT_FALSE(IPEndPoint::FromValue(invalid_address).has_value());
464 
465     base::Value negative_port = valid_value.Clone();
466     *negative_port.GetDict().Find("port") = base::Value(-1);
467     EXPECT_FALSE(IPEndPoint::FromValue(negative_port).has_value());
468 
469     base::Value large_port = valid_value.Clone();
470     *large_port.GetDict().Find("port") = base::Value(66000);
471     EXPECT_FALSE(IPEndPoint::FromValue(large_port).has_value());
472   }
473 
474   // Invalid values for scope id.
475   const auto v6_link_local_address = *IPAddress::FromIPLiteral("fe80::1");
476   base::Value valid_value =
477       IPEndPoint(v6_link_local_address, /*port=*/80, /*scope_id=*/1).ToValue();
478 
479   base::Value invalid_scope_id = valid_value.Clone();
480   *invalid_scope_id.GetDict().Find("interface_name") = base::Value("-1");
481   EXPECT_FALSE(IPEndPoint::FromValue(invalid_scope_id).has_value());
482 
483   base::Value invalid_scope_id2 = valid_value.Clone();
484   *invalid_scope_id2.GetDict().Find("interface_name") = base::Value("0");
485   EXPECT_FALSE(IPEndPoint::FromValue(invalid_scope_id2).has_value());
486 
487   base::Value invalid_address_v4 = valid_value.Clone();
488   *invalid_address_v4.GetDict().Find("address") = base::Value("169.254.0.1");
489   EXPECT_FALSE(IPEndPoint::FromValue(invalid_scope_id).has_value());
490 
491   base::Value invalid_address_v6 = valid_value.Clone();
492   *invalid_address_v4.GetDict().Find("address") = base::Value("2001:db8:0::42");
493   EXPECT_FALSE(IPEndPoint::FromValue(invalid_scope_id).has_value());
494 
495   base::Value invalid_ipv4_mapped_v6_address = valid_value.Clone();
496   *invalid_address_v4.GetDict().Find("address") =
497       base::Value("::ffff:169.254.0.1");
498   EXPECT_FALSE(IPEndPoint::FromValue(invalid_scope_id).has_value());
499 }
500 
501 }  // namespace
502 
503 }  // namespace net
504