[Android WebView] Fix webview perf bot switchover to use org.chromium.webview_shell...
[chromium-blink-merge.git] / net / base / ip_address_number_unittest.cc
blob2ee7bed264e28a1a476a4981266cc2d1292d8a15
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"
13 namespace net {
15 namespace {
17 // Helper to strignize an IP number (used to define expectations).
18 std::string DumpIPNumber(const IPAddressNumber& v) {
19 std::string out;
20 for (size_t i = 0; i < v.size(); ++i) {
21 if (i != 0)
22 out.append(",");
23 out.append(base::IntToString(static_cast<int>(v[i])));
25 return out;
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);
88 // ::ffff:192.168.0.1
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[] = {
145 "foobar",
147 "192.168.0.1",
148 "::1",
149 "/",
150 "/1",
151 "1",
152 "192.168.1.1/-1",
153 "192.168.1.1/33",
154 "::1/-3",
155 "a::3/129",
156 "::1/x",
157 "192.168.0.1//11"
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],
165 &ip_number,
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",
176 &ip_number,
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) {
184 struct {
185 const char* const cidr_literal;
186 const char* const ip_literal;
187 bool expected_to_match;
188 } tests[] = {
189 // IPv4 prefix with IPv4 inputs.
191 "10.10.1.32/27",
192 "10.10.1.44",
193 true
196 "10.10.1.32/27",
197 "10.10.1.90",
198 false
201 "10.10.1.32/27",
202 "10.10.1.90",
203 false
206 // IPv6 prefix with IPv6 inputs.
208 "2001:db8::/32",
209 "2001:DB8:3:4::5",
210 true
213 "2001:db8::/32",
214 "2001:c8::",
215 false
218 // IPv6 prefix with IPv4 inputs.
220 "2001:db8::/33",
221 "192.168.0.1",
222 false
225 "::ffff:192.168.0.1/112",
226 "192.168.33.77",
227 true
230 // IPv4 prefix with IPv6 inputs.
232 "10.11.33.44/16",
233 "::ffff:0a0b:89",
234 true
237 "10.11.33.44/16",
238 "::ffff:10.12.33.44",
239 false
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,
254 &ip_prefix,
255 &prefix_length_in_bits));
257 EXPECT_EQ(tests[i].expected_to_match,
258 IPNumberMatchesPrefix(ip_number,
259 ip_prefix,
260 prefix_length_in_bits));
264 } // anonymous namespace
266 } // namespace net