Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(396)

Side by Side Diff: net/base/ip_address_unittest.cc

Issue 2881673002: Avoid heap allocations in IPAddress (Closed)
Patch Set: New constructor Created 3 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « net/base/ip_address.cc ('k') | net/cert/internal/name_constraints.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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"
11 #include "base/strings/stringprintf.h" 11 #include "base/strings/stringprintf.h"
12 #include "testing/gtest/include/gtest/gtest.h" 12 #include "testing/gtest/include/gtest/gtest.h"
13 13
14 namespace net { 14 namespace net {
15 15
16 namespace { 16 namespace {
17 17
18 // Helper to stringize an IP address (used to define expectations). 18 // Helper to stringize an IP address (used to define expectations).
19 std::string DumpIPAddress(const IPAddress& v) { 19 std::string DumpIPAddress(const IPAddress& v) {
20 std::string out; 20 std::string out;
21 for (size_t i = 0; i < v.bytes().size(); ++i) { 21 for (size_t i = 0; i < v.bytes().size(); ++i) {
22 if (i != 0) 22 if (i != 0)
23 out.append(","); 23 out.append(",");
24 out.append(base::UintToString(v.bytes()[i])); 24 out.append(base::UintToString(v.bytes()[i]));
25 } 25 }
26 return out; 26 return out;
27 } 27 }
28 28
29 TEST(IPAddressBytesTest, ConstructEmpty) {
30 IPAddressBytes bytes;
31 ASSERT_EQ(0u, bytes.size());
32 }
33
34 TEST(IPAddressBytesTest, ConstructIPv4) {
35 uint8_t data[] = {192, 168, 1, 1};
36 IPAddressBytes bytes(data, arraysize(data));
37 ASSERT_EQ(arraysize(data), bytes.size());
38 size_t i = 0;
39 for (uint8_t byte : bytes)
40 EXPECT_EQ(data[i++], byte);
41 ASSERT_EQ(arraysize(data), i);
42 }
43
44 TEST(IPAddressBytesTest, ConstructIPv6) {
45 uint8_t data[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16};
46 IPAddressBytes bytes(data, arraysize(data));
47 ASSERT_EQ(arraysize(data), bytes.size());
48 size_t i = 0;
49 for (uint8_t byte : bytes)
50 EXPECT_EQ(data[i++], byte);
51 ASSERT_EQ(arraysize(data), i);
52 }
53
54 TEST(IPAddressBytesTest, Assign) {
55 uint8_t data[] = {192, 168, 1, 1};
56 IPAddressBytes copy;
57 copy.Assign(data, arraysize(data));
58 EXPECT_EQ(IPAddressBytes(data, arraysize(data)), copy);
59 }
60
29 TEST(IPAddressTest, ConstructIPv4) { 61 TEST(IPAddressTest, ConstructIPv4) {
30 EXPECT_EQ("127.0.0.1", IPAddress::IPv4Localhost().ToString()); 62 EXPECT_EQ("127.0.0.1", IPAddress::IPv4Localhost().ToString());
31 63
32 IPAddress ipv4_ctor(192, 168, 1, 1); 64 IPAddress ipv4_ctor(192, 168, 1, 1);
33 EXPECT_EQ("192.168.1.1", ipv4_ctor.ToString()); 65 EXPECT_EQ("192.168.1.1", ipv4_ctor.ToString());
34 } 66 }
35 67
36 TEST(IPAddressTest, ConstructIPv6) {
37 EXPECT_EQ("::1", IPAddress::IPv6Localhost().ToString());
38
39 IPAddress ipv6_ctor(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16);
40 EXPECT_EQ("102:304:506:708:90a:b0c:d0e:f10", ipv6_ctor.ToString());
41 }
42
43 TEST(IPAddressTest, IsIPVersion) { 68 TEST(IPAddressTest, IsIPVersion) {
44 uint8_t addr1[4] = {192, 168, 0, 1}; 69 uint8_t addr1[4] = {192, 168, 0, 1};
45 IPAddress ip_address1(addr1); 70 IPAddress ip_address1(addr1);
46 EXPECT_TRUE(ip_address1.IsIPv4()); 71 EXPECT_TRUE(ip_address1.IsIPv4());
47 EXPECT_FALSE(ip_address1.IsIPv6()); 72 EXPECT_FALSE(ip_address1.IsIPv6());
48 73
49 uint8_t addr2[16] = {0xFE, 0xDC, 0xBA, 0x98}; 74 uint8_t addr2[16] = {0xFE, 0xDC, 0xBA, 0x98};
50 IPAddress ip_address2(addr2); 75 IPAddress ip_address2(addr2);
51 EXPECT_TRUE(ip_address2.IsIPv6()); 76 EXPECT_TRUE(ip_address2.IsIPv6());
52 EXPECT_FALSE(ip_address2.IsIPv4()); 77 EXPECT_FALSE(ip_address2.IsIPv4());
(...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after
183 // Unreserved block(s) 208 // Unreserved block(s)
184 {"203.0.114.0", NOT_RESERVED}, 209 {"203.0.114.0", NOT_RESERVED},
185 {"223.255.255.255", NOT_RESERVED}, 210 {"223.255.255.255", NOT_RESERVED},
186 // 224.0.0.0/8 - 255.0.0.0/8 211 // 224.0.0.0/8 - 255.0.0.0/8
187 {"224.0.0.0", RESERVED}, 212 {"224.0.0.0", RESERVED},
188 {"255.255.255.255", RESERVED}}; 213 {"255.255.255.255", RESERVED}};
189 214
190 IPAddress address; 215 IPAddress address;
191 for (const auto& test : tests) { 216 for (const auto& test : tests) {
192 EXPECT_TRUE(address.AssignFromIPLiteral(test.address)); 217 EXPECT_TRUE(address.AssignFromIPLiteral(test.address));
218 ASSERT_TRUE(address.IsValid());
193 EXPECT_EQ(!!test.is_reserved, address.IsReserved()); 219 EXPECT_EQ(!!test.is_reserved, address.IsReserved());
194 } 220 }
195 } 221 }
196 222
197 // Tests for the reserved IPv6 ranges and the (unreserved) blocks in between. 223 // 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 224 // 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 225 // range. The unreserved blocks are tested similarly. These tests cover the
200 // entire IPv6 address range. 226 // entire IPv6 address range.
201 TEST(IPAddressTest, IsReservedIPv6) { 227 TEST(IPAddressTest, IsReservedIPv6) {
202 struct { 228 struct {
(...skipping 210 matching lines...) Expand 10 before | Expand all | Expand 10 after
413 IPAddress ip_address2; 439 IPAddress ip_address2;
414 EXPECT_TRUE(ip_address2.AssignFromIPLiteral("2001:db8:0::42")); 440 EXPECT_TRUE(ip_address2.AssignFromIPLiteral("2001:db8:0::42"));
415 EXPECT_TRUE(ip_address1 < ip_address2); 441 EXPECT_TRUE(ip_address1 < ip_address2);
416 EXPECT_FALSE(ip_address2 < ip_address1); 442 EXPECT_FALSE(ip_address2 < ip_address1);
417 443
418 // Compare equivalent addresses. 444 // Compare equivalent addresses.
419 IPAddress ip_address3; 445 IPAddress ip_address3;
420 EXPECT_TRUE(ip_address3.AssignFromIPLiteral("127.0.0.1")); 446 EXPECT_TRUE(ip_address3.AssignFromIPLiteral("127.0.0.1"));
421 EXPECT_FALSE(ip_address1 < ip_address3); 447 EXPECT_FALSE(ip_address1 < ip_address3);
422 EXPECT_FALSE(ip_address3 < ip_address1); 448 EXPECT_FALSE(ip_address3 < ip_address1);
449
450 IPAddress ip_address4;
451 EXPECT_TRUE(ip_address4.AssignFromIPLiteral("128.0.0.0"));
452 EXPECT_TRUE(ip_address1 < ip_address4);
453 EXPECT_FALSE(ip_address4 < ip_address1);
423 } 454 }
424 455
425 // Test mapping an IPv4 address to an IPv6 address. 456 // Test mapping an IPv4 address to an IPv6 address.
426 TEST(IPAddressTest, ConvertIPv4ToIPv4MappedIPv6) { 457 TEST(IPAddressTest, ConvertIPv4ToIPv4MappedIPv6) {
427 IPAddress ipv4_address(192, 168, 0, 1); 458 IPAddress ipv4_address(192, 168, 0, 1);
428 IPAddress ipv6_address = ConvertIPv4ToIPv4MappedIPv6(ipv4_address); 459 IPAddress ipv6_address = ConvertIPv4ToIPv4MappedIPv6(ipv4_address);
429 460
430 // ::ffff:192.168.0.1 461 // ::ffff:192.168.0.1
431 EXPECT_EQ("0,0,0,0,0,0,0,0,0,0,255,255,192,168,0,1", 462 EXPECT_EQ("0,0,0,0,0,0,0,0,0,0,255,255,192,168,0,1",
432 DumpIPAddress(ipv6_address)); 463 DumpIPAddress(ipv6_address));
(...skipping 160 matching lines...) Expand 10 before | Expand all | Expand 10 after
593 624
594 // Prefix is longer than the address. 625 // Prefix is longer than the address.
595 uint8_t ipv6_prefix5[] = {42, 0, 20, 80, 64, 12, 12, 9, 0, 626 uint8_t ipv6_prefix5[] = {42, 0, 20, 80, 64, 12, 12, 9, 0,
596 0, 0, 0, 0, 0, 0, 0, 10}; 627 0, 0, 0, 0, 0, 0, 0, 10};
597 EXPECT_FALSE(IPAddressStartsWith(ipv6_address, ipv6_prefix5)); 628 EXPECT_FALSE(IPAddressStartsWith(ipv6_address, ipv6_prefix5));
598 } 629 }
599 630
600 } // anonymous namespace 631 } // anonymous namespace
601 632
602 } // namespace net 633 } // namespace net
OLDNEW
« no previous file with comments | « net/base/ip_address.cc ('k') | net/cert/internal/name_constraints.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698