| OLD | NEW |
| 1 // Copyright (c) 2015 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "net/base/ip_address.h" | 5 #include "net/base/ip_address.h" |
| 6 | 6 |
| 7 #include <vector> | 7 #include <vector> |
| 8 | 8 |
| 9 #include "base/format_macros.h" | 9 #include "base/format_macros.h" |
| 10 #include "base/strings/string_number_conversions.h" | 10 #include "base/strings/string_number_conversions.h" |
| (...skipping 172 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 183 // Unreserved block(s) | 183 // Unreserved block(s) |
| 184 {"203.0.114.0", NOT_RESERVED}, | 184 {"203.0.114.0", NOT_RESERVED}, |
| 185 {"223.255.255.255", NOT_RESERVED}, | 185 {"223.255.255.255", NOT_RESERVED}, |
| 186 // 224.0.0.0/8 - 255.0.0.0/8 | 186 // 224.0.0.0/8 - 255.0.0.0/8 |
| 187 {"224.0.0.0", RESERVED}, | 187 {"224.0.0.0", RESERVED}, |
| 188 {"255.255.255.255", RESERVED}}; | 188 {"255.255.255.255", RESERVED}}; |
| 189 | 189 |
| 190 IPAddress address; | 190 IPAddress address; |
| 191 for (const auto& test : tests) { | 191 for (const auto& test : tests) { |
| 192 EXPECT_TRUE(address.AssignFromIPLiteral(test.address)); | 192 EXPECT_TRUE(address.AssignFromIPLiteral(test.address)); |
| 193 ASSERT_TRUE(address.IsValid()); |
| 193 EXPECT_EQ(!!test.is_reserved, address.IsReserved()); | 194 EXPECT_EQ(!!test.is_reserved, address.IsReserved()); |
| 194 } | 195 } |
| 195 } | 196 } |
| 196 | 197 |
| 197 // Tests for the reserved IPv6 ranges and the (unreserved) blocks in between. | 198 // Tests for the reserved IPv6 ranges and the (unreserved) blocks in between. |
| 198 // The reserved ranges are tested by checking the first and last address of each | 199 // The reserved ranges are tested by checking the first and last address of each |
| 199 // range. The unreserved blocks are tested similarly. These tests cover the | 200 // range. The unreserved blocks are tested similarly. These tests cover the |
| 200 // entire IPv6 address range. | 201 // entire IPv6 address range. |
| 201 TEST(IPAddressTest, IsReservedIPv6) { | 202 TEST(IPAddressTest, IsReservedIPv6) { |
| 202 struct { | 203 struct { |
| (...skipping 210 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 413 IPAddress ip_address2; | 414 IPAddress ip_address2; |
| 414 EXPECT_TRUE(ip_address2.AssignFromIPLiteral("2001:db8:0::42")); | 415 EXPECT_TRUE(ip_address2.AssignFromIPLiteral("2001:db8:0::42")); |
| 415 EXPECT_TRUE(ip_address1 < ip_address2); | 416 EXPECT_TRUE(ip_address1 < ip_address2); |
| 416 EXPECT_FALSE(ip_address2 < ip_address1); | 417 EXPECT_FALSE(ip_address2 < ip_address1); |
| 417 | 418 |
| 418 // Compare equivalent addresses. | 419 // Compare equivalent addresses. |
| 419 IPAddress ip_address3; | 420 IPAddress ip_address3; |
| 420 EXPECT_TRUE(ip_address3.AssignFromIPLiteral("127.0.0.1")); | 421 EXPECT_TRUE(ip_address3.AssignFromIPLiteral("127.0.0.1")); |
| 421 EXPECT_FALSE(ip_address1 < ip_address3); | 422 EXPECT_FALSE(ip_address1 < ip_address3); |
| 422 EXPECT_FALSE(ip_address3 < ip_address1); | 423 EXPECT_FALSE(ip_address3 < ip_address1); |
| 424 |
| 425 IPAddress ip_address4; |
| 426 EXPECT_TRUE(ip_address4.AssignFromIPLiteral("128.0.0.0")); |
| 427 EXPECT_TRUE(ip_address1 < ip_address4); |
| 428 EXPECT_FALSE(ip_address4 < ip_address1); |
| 423 } | 429 } |
| 424 | 430 |
| 425 // Test mapping an IPv4 address to an IPv6 address. | 431 // Test mapping an IPv4 address to an IPv6 address. |
| 426 TEST(IPAddressTest, ConvertIPv4ToIPv4MappedIPv6) { | 432 TEST(IPAddressTest, ConvertIPv4ToIPv4MappedIPv6) { |
| 427 IPAddress ipv4_address(192, 168, 0, 1); | 433 IPAddress ipv4_address(192, 168, 0, 1); |
| 428 IPAddress ipv6_address = ConvertIPv4ToIPv4MappedIPv6(ipv4_address); | 434 IPAddress ipv6_address = ConvertIPv4ToIPv4MappedIPv6(ipv4_address); |
| 429 | 435 |
| 430 // ::ffff:192.168.0.1 | 436 // ::ffff:192.168.0.1 |
| 431 EXPECT_EQ("0,0,0,0,0,0,0,0,0,0,255,255,192,168,0,1", | 437 EXPECT_EQ("0,0,0,0,0,0,0,0,0,0,255,255,192,168,0,1", |
| 432 DumpIPAddress(ipv6_address)); | 438 DumpIPAddress(ipv6_address)); |
| (...skipping 160 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 593 | 599 |
| 594 // Prefix is longer than the address. | 600 // Prefix is longer than the address. |
| 595 uint8_t ipv6_prefix5[] = {42, 0, 20, 80, 64, 12, 12, 9, 0, | 601 uint8_t ipv6_prefix5[] = {42, 0, 20, 80, 64, 12, 12, 9, 0, |
| 596 0, 0, 0, 0, 0, 0, 0, 10}; | 602 0, 0, 0, 0, 0, 0, 0, 10}; |
| 597 EXPECT_FALSE(IPAddressStartsWith(ipv6_address, ipv6_prefix5)); | 603 EXPECT_FALSE(IPAddressStartsWith(ipv6_address, ipv6_prefix5)); |
| 598 } | 604 } |
| 599 | 605 |
| 600 } // anonymous namespace | 606 } // anonymous namespace |
| 601 | 607 |
| 602 } // namespace net | 608 } // namespace net |
| OLD | NEW |