1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "net/base/ip_address_number.h"
7 #include "base/format_macros.h"
8 #include "base/strings/string_number_conversions.h"
9 #include "base/strings/stringprintf.h"
10 #include "net/base/net_util.h"
11 #include "testing/gtest/include/gtest/gtest.h"
17 // Helper to strignize an IP number (used to define expectations).
18 std::string
DumpIPNumber(const IPAddressNumber
& v
) {
20 for (size_t i
= 0; i
< v
.size(); ++i
) {
23 out
.append(base::IntToString(static_cast<int>(v
[i
])));
28 TEST(IpAddressNumberTest
, IPAddressToString
) {
29 uint8_t addr1
[4] = {0, 0, 0, 0};
30 EXPECT_EQ("0.0.0.0", IPAddressToString(addr1
, sizeof(addr1
)));
32 uint8_t addr2
[4] = {192, 168, 0, 1};
33 EXPECT_EQ("192.168.0.1", IPAddressToString(addr2
, sizeof(addr2
)));
35 uint8_t addr3
[16] = {0xFE, 0xDC, 0xBA, 0x98};
36 EXPECT_EQ("fedc:ba98::", IPAddressToString(addr3
, sizeof(addr3
)));
39 TEST(IpAddressNumberTest
, IPAddressToStringWithPort
) {
40 uint8_t addr1
[4] = {0, 0, 0, 0};
41 EXPECT_EQ("0.0.0.0:3", IPAddressToStringWithPort(addr1
, sizeof(addr1
), 3));
43 uint8_t addr2
[4] = {192, 168, 0, 1};
44 EXPECT_EQ("192.168.0.1:99",
45 IPAddressToStringWithPort(addr2
, sizeof(addr2
), 99));
47 uint8_t addr3
[16] = {0xFE, 0xDC, 0xBA, 0x98};
48 EXPECT_EQ("[fedc:ba98::]:8080",
49 IPAddressToStringWithPort(addr3
, sizeof(addr3
), 8080));
52 // Test that invalid IP literals fail to parse.
53 TEST(IpAddressNumberTest
, ParseIPLiteralToNumber_FailParse
) {
54 IPAddressNumber number
;
56 EXPECT_FALSE(ParseIPLiteralToNumber("bad value", &number
));
57 EXPECT_FALSE(ParseIPLiteralToNumber("bad:value", &number
));
58 EXPECT_FALSE(ParseIPLiteralToNumber(std::string(), &number
));
59 EXPECT_FALSE(ParseIPLiteralToNumber("192.168.0.1:30", &number
));
60 EXPECT_FALSE(ParseIPLiteralToNumber(" 192.168.0.1 ", &number
));
61 EXPECT_FALSE(ParseIPLiteralToNumber("[::1]", &number
));
64 // Test parsing an IPv4 literal.
65 TEST(IpAddressNumberTest
, ParseIPLiteralToNumber_IPv4
) {
66 IPAddressNumber number
;
67 EXPECT_TRUE(ParseIPLiteralToNumber("192.168.0.1", &number
));
68 EXPECT_EQ("192,168,0,1", DumpIPNumber(number
));
69 EXPECT_EQ("192.168.0.1", IPAddressToString(number
));
72 // Test parsing an IPv6 literal.
73 TEST(IpAddressNumberTest
, ParseIPLiteralToNumber_IPv6
) {
74 IPAddressNumber number
;
75 EXPECT_TRUE(ParseIPLiteralToNumber("1:abcd::3:4:ff", &number
));
76 EXPECT_EQ("0,1,171,205,0,0,0,0,0,0,0,3,0,4,0,255", DumpIPNumber(number
));
77 EXPECT_EQ("1:abcd::3:4:ff", IPAddressToString(number
));
80 // Test mapping an IPv4 address to an IPv6 address.
81 TEST(IpAddressNumberTest
, ConvertIPv4NumberToIPv6Number
) {
82 IPAddressNumber ipv4_number
;
83 EXPECT_TRUE(ParseIPLiteralToNumber("192.168.0.1", &ipv4_number
));
85 IPAddressNumber ipv6_number
=
86 ConvertIPv4NumberToIPv6Number(ipv4_number
);
89 EXPECT_EQ("0,0,0,0,0,0,0,0,0,0,255,255,192,168,0,1",
90 DumpIPNumber(ipv6_number
));
91 EXPECT_EQ("::ffff:c0a8:1", IPAddressToString(ipv6_number
));
94 TEST(IpAddressNumberTest
, ParseURLHostnameToNumber_FailParse
) {
95 IPAddressNumber number
;
97 EXPECT_FALSE(ParseURLHostnameToNumber("bad value", &number
));
98 EXPECT_FALSE(ParseURLHostnameToNumber("bad:value", &number
));
99 EXPECT_FALSE(ParseURLHostnameToNumber(std::string(), &number
));
100 EXPECT_FALSE(ParseURLHostnameToNumber("192.168.0.1:30", &number
));
101 EXPECT_FALSE(ParseURLHostnameToNumber(" 192.168.0.1 ", &number
));
102 EXPECT_FALSE(ParseURLHostnameToNumber("::1", &number
));
105 TEST(IpAddressNumberTest
, ParseURLHostnameToNumber_IPv4
) {
106 IPAddressNumber number
;
107 EXPECT_TRUE(ParseURLHostnameToNumber("192.168.0.1", &number
));
108 EXPECT_EQ("192,168,0,1", DumpIPNumber(number
));
109 EXPECT_EQ("192.168.0.1", IPAddressToString(number
));
112 TEST(IpAddressNumberTest
, ParseURLHostnameToNumber_IPv6
) {
113 IPAddressNumber number
;
114 EXPECT_TRUE(ParseURLHostnameToNumber("[1:abcd::3:4:ff]", &number
));
115 EXPECT_EQ("0,1,171,205,0,0,0,0,0,0,0,3,0,4,0,255", DumpIPNumber(number
));
116 EXPECT_EQ("1:abcd::3:4:ff", IPAddressToString(number
));
119 TEST(IpAddressNumberTest
, IsIPv4Mapped
) {
120 IPAddressNumber ipv4_number
;
121 EXPECT_TRUE(ParseIPLiteralToNumber("192.168.0.1", &ipv4_number
));
122 EXPECT_FALSE(IsIPv4Mapped(ipv4_number
));
124 IPAddressNumber ipv6_number
;
125 EXPECT_TRUE(ParseIPLiteralToNumber("::1", &ipv4_number
));
126 EXPECT_FALSE(IsIPv4Mapped(ipv6_number
));
128 IPAddressNumber ipv4mapped_number
;
129 EXPECT_TRUE(ParseIPLiteralToNumber("::ffff:0101:1", &ipv4mapped_number
));
130 EXPECT_TRUE(IsIPv4Mapped(ipv4mapped_number
));
133 TEST(IpAddressNumberTest
, ConvertIPv4MappedToIPv4
) {
134 IPAddressNumber ipv4mapped_number
;
135 EXPECT_TRUE(ParseIPLiteralToNumber("::ffff:0101:1", &ipv4mapped_number
));
136 IPAddressNumber expected
;
137 EXPECT_TRUE(ParseIPLiteralToNumber("1.1.0.1", &expected
));
138 IPAddressNumber result
= ConvertIPv4MappedToIPv4(ipv4mapped_number
);
139 EXPECT_EQ(expected
, result
);
142 // Test parsing invalid CIDR notation literals.
143 TEST(IpAddressNumberTest
, ParseCIDRBlock_Invalid
) {
144 const char* const bad_literals
[] = {
160 for (size_t i
= 0; i
< arraysize(bad_literals
); ++i
) {
161 IPAddressNumber ip_number
;
162 size_t prefix_length_in_bits
;
164 EXPECT_FALSE(ParseCIDRBlock(bad_literals
[i
],
166 &prefix_length_in_bits
));
170 // Test parsing a valid CIDR notation literal.
171 TEST(IpAddressNumberTest
, ParseCIDRBlock_Valid
) {
172 IPAddressNumber ip_number
;
173 size_t prefix_length_in_bits
;
175 EXPECT_TRUE(ParseCIDRBlock("192.168.0.1/11",
177 &prefix_length_in_bits
));
179 EXPECT_EQ("192,168,0,1", DumpIPNumber(ip_number
));
180 EXPECT_EQ(11u, prefix_length_in_bits
);
183 TEST(IpAddressNumberTest
, IPNumberMatchesPrefix
) {
185 const char* const cidr_literal
;
186 const char* const ip_literal
;
187 bool expected_to_match
;
189 // IPv4 prefix with IPv4 inputs.
206 // IPv6 prefix with IPv6 inputs.
218 // IPv6 prefix with IPv4 inputs.
225 "::ffff:192.168.0.1/112",
230 // IPv4 prefix with IPv6 inputs.
238 "::ffff:10.12.33.44",
242 for (size_t i
= 0; i
< arraysize(tests
); ++i
) {
243 SCOPED_TRACE(base::StringPrintf("Test[%" PRIuS
"]: %s, %s", i
,
244 tests
[i
].cidr_literal
,
245 tests
[i
].ip_literal
));
247 IPAddressNumber ip_number
;
248 EXPECT_TRUE(ParseIPLiteralToNumber(tests
[i
].ip_literal
, &ip_number
));
250 IPAddressNumber ip_prefix
;
251 size_t prefix_length_in_bits
;
253 EXPECT_TRUE(ParseCIDRBlock(tests
[i
].cidr_literal
,
255 &prefix_length_in_bits
));
257 EXPECT_EQ(tests
[i
].expected_to_match
,
258 IPNumberMatchesPrefix(ip_number
,
260 prefix_length_in_bits
));
264 } // anonymous namespace