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/net_util.h"
11 #include "base/files/file_path.h"
12 #include "base/format_macros.h"
13 #include "base/scoped_native_library.h"
14 #include "base/strings/string_number_conversions.h"
15 #include "base/strings/string_util.h"
16 #include "base/strings/stringprintf.h"
17 #include "base/strings/utf_string_conversions.h"
18 #include "base/sys_byteorder.h"
19 #include "base/time/time.h"
20 #include "testing/gtest/include/gtest/gtest.h"
26 #include "base/win/windows_version.h"
27 #elif !defined(OS_ANDROID)
31 using base::ASCIIToUTF16
;
32 using base::WideToUTF16
;
39 const char* header_name
;
43 struct CompliantHostCase
{
45 const char* desired_tld
;
49 // Fills in sockaddr for the given 32-bit address (IPv4.)
50 // |bytes| should be an array of length 4.
51 void MakeIPv4Address(const uint8
* bytes
, int port
, SockaddrStorage
* storage
) {
52 memset(&storage
->addr_storage
, 0, sizeof(storage
->addr_storage
));
53 storage
->addr_len
= sizeof(struct sockaddr_in
);
54 struct sockaddr_in
* addr4
= reinterpret_cast<sockaddr_in
*>(storage
->addr
);
55 addr4
->sin_port
= base::HostToNet16(port
);
56 addr4
->sin_family
= AF_INET
;
57 memcpy(&addr4
->sin_addr
, bytes
, 4);
60 // Fills in sockaddr for the given 128-bit address (IPv6.)
61 // |bytes| should be an array of length 16.
62 void MakeIPv6Address(const uint8
* bytes
, int port
, SockaddrStorage
* storage
) {
63 memset(&storage
->addr_storage
, 0, sizeof(storage
->addr_storage
));
64 storage
->addr_len
= sizeof(struct sockaddr_in6
);
65 struct sockaddr_in6
* addr6
= reinterpret_cast<sockaddr_in6
*>(storage
->addr
);
66 addr6
->sin6_port
= base::HostToNet16(port
);
67 addr6
->sin6_family
= AF_INET6
;
68 memcpy(&addr6
->sin6_addr
, bytes
, 16);
71 // Helper to strignize an IP number (used to define expectations).
72 std::string
DumpIPNumber(const IPAddressNumber
& v
) {
74 for (size_t i
= 0; i
< v
.size(); ++i
) {
77 out
.append(base::IntToString(static_cast<int>(v
[i
])));
82 } // anonymous namespace
84 TEST(NetUtilTest
, GetIdentityFromURL
) {
86 const char* input_url
;
87 const char* expected_username
;
88 const char* expected_password
;
91 "http://username:password@google.com",
95 { // Test for http://crbug.com/19200
96 "http://username:p@ssword@google.com",
100 { // Special URL characters should be unescaped.
101 "http://username:p%3fa%26s%2fs%23@google.com",
105 { // Username contains %20.
106 "http://use rname:password@google.com",
111 "http://use%00rname:password@google.com",
115 { // Use a '+' in the username.
116 "http://use+rname:password@google.com",
120 { // Use a '&' in the password.
121 "http://username:p&ssword@google.com",
126 for (size_t i
= 0; i
< ARRAYSIZE_UNSAFE(tests
); ++i
) {
127 SCOPED_TRACE(base::StringPrintf("Test[%" PRIuS
"]: %s", i
,
128 tests
[i
].input_url
));
129 GURL
url(tests
[i
].input_url
);
131 base::string16 username
, password
;
132 GetIdentityFromURL(url
, &username
, &password
);
134 EXPECT_EQ(ASCIIToUTF16(tests
[i
].expected_username
), username
);
135 EXPECT_EQ(ASCIIToUTF16(tests
[i
].expected_password
), password
);
139 // Try extracting a username which was encoded with UTF8.
140 TEST(NetUtilTest
, GetIdentityFromURL_UTF8
) {
141 GURL
url(WideToUTF16(L
"http://foo:\x4f60\x597d@blah.com"));
143 EXPECT_EQ("foo", url
.username());
144 EXPECT_EQ("%E4%BD%A0%E5%A5%BD", url
.password());
146 // Extract the unescaped identity.
147 base::string16 username
, password
;
148 GetIdentityFromURL(url
, &username
, &password
);
150 // Verify that it was decoded as UTF8.
151 EXPECT_EQ(ASCIIToUTF16("foo"), username
);
152 EXPECT_EQ(WideToUTF16(L
"\x4f60\x597d"), password
);
155 // Just a bunch of fake headers.
156 const char* google_headers
=
158 "Content-TYPE: text/html; charset=utf-8\n"
159 "Content-disposition: attachment; filename=\"download.pdf\"\n"
160 "Content-Length: 378557\n"
161 "X-Google-Google1: 314159265\n"
162 "X-Google-Google2: aaaa2:7783,bbb21:9441\n"
163 "X-Google-Google4: home\n"
164 "Transfer-Encoding: chunked\n"
165 "Set-Cookie: HEHE_AT=6666x66beef666x6-66xx6666x66; Path=/mail\n"
166 "Set-Cookie: HEHE_HELP=owned:0;Path=/\n"
167 "Set-Cookie: S=gmail=Xxx-beefbeefbeef_beefb:gmail_yj=beefbeef000beefbee"
168 "fbee:gmproxy=bee-fbeefbe; Domain=.google.com; Path=/\n"
169 "X-Google-Google2: /one/two/three/four/five/six/seven-height/nine:9411\n"
171 "Transfer-Encoding: chunked\n"
172 "Date: Mon, 13 Nov 2006 21:38:09 GMT\n"
173 "Expires: Tue, 14 Nov 2006 19:23:58 GMT\n"
174 "X-Malformed: bla; arg=test\"\n"
175 "X-Malformed2: bla; arg=\n"
176 "X-Test: bla; arg1=val1; arg2=val2";
178 TEST(NetUtilTest
, GetSpecificHeader
) {
179 const HeaderCase tests
[] = {
180 {"content-type", "text/html; charset=utf-8"},
181 {"CONTENT-LENGTH", "378557"},
182 {"Date", "Mon, 13 Nov 2006 21:38:09 GMT"},
187 // Test first with google_headers.
188 for (size_t i
= 0; i
< ARRAYSIZE_UNSAFE(tests
); ++i
) {
190 GetSpecificHeader(google_headers
, tests
[i
].header_name
);
191 EXPECT_EQ(result
, tests
[i
].expected
);
194 // Test again with empty headers.
195 for (size_t i
= 0; i
< ARRAYSIZE_UNSAFE(tests
); ++i
) {
196 std::string result
= GetSpecificHeader(std::string(), tests
[i
].header_name
);
197 EXPECT_EQ(result
, std::string());
201 TEST(NetUtilTest
, CompliantHost
) {
202 const CompliantHostCase compliant_host_cases
[] = {
217 {"a.-a9", "", false},
220 {"1-.a-b", "", true},
221 {"1_.a-b", "", false},
222 {"1-2.a_b", "", true},
223 {"a.b.c.d.e", "", true},
224 {"1.2.3.4.5", "", true},
225 {"1.2.3.4.5.", "", true},
228 for (size_t i
= 0; i
< ARRAYSIZE_UNSAFE(compliant_host_cases
); ++i
) {
229 EXPECT_EQ(compliant_host_cases
[i
].expected_output
,
230 IsCanonicalizedHostCompliant(compliant_host_cases
[i
].host
,
231 compliant_host_cases
[i
].desired_tld
));
235 TEST(NetUtilTest
, ParseHostAndPort
) {
239 const char* expected_host
;
243 {"foo:10", true, "foo", 10},
244 {"foo", true, "foo", -1},
246 "[1080:0:0:0:8:800:200C:4171]:11",
248 "[1080:0:0:0:8:800:200C:4171]",
252 {"foo:bar", false, "", -1},
253 {"foo:", false, "", -1},
254 {":", false, "", -1},
255 {":80", false, "", -1},
257 {"porttoolong:300000", false, "", -1},
258 {"usrname@host", false, "", -1},
259 {"usrname:password@host", false, "", -1},
260 {":password@host", false, "", -1},
261 {":password@host:80", false, "", -1},
262 {":password@host", false, "", -1},
263 {"@host", false, "", -1},
266 for (size_t i
= 0; i
< ARRAYSIZE_UNSAFE(tests
); ++i
) {
269 bool ok
= ParseHostAndPort(tests
[i
].input
, &host
, &port
);
271 EXPECT_EQ(tests
[i
].success
, ok
);
273 if (tests
[i
].success
) {
274 EXPECT_EQ(tests
[i
].expected_host
, host
);
275 EXPECT_EQ(tests
[i
].expected_port
, port
);
280 TEST(NetUtilTest
, GetHostAndPort
) {
283 const char* expected_host_and_port
;
285 { GURL("http://www.foo.com/x"), "www.foo.com:80"},
286 { GURL("http://www.foo.com:21/x"), "www.foo.com:21"},
288 // For IPv6 literals should always include the brackets.
289 { GURL("http://[1::2]/x"), "[1::2]:80"},
290 { GURL("http://[::a]:33/x"), "[::a]:33"},
292 for (size_t i
= 0; i
< ARRAYSIZE_UNSAFE(tests
); ++i
) {
293 std::string host_and_port
= GetHostAndPort(tests
[i
].url
);
294 EXPECT_EQ(std::string(tests
[i
].expected_host_and_port
), host_and_port
);
298 TEST(NetUtilTest
, GetHostAndOptionalPort
) {
301 const char* expected_host_and_port
;
303 { GURL("http://www.foo.com/x"), "www.foo.com"},
304 { GURL("http://www.foo.com:21/x"), "www.foo.com:21"},
306 // For IPv6 literals should always include the brackets.
307 { GURL("http://[1::2]/x"), "[1::2]"},
308 { GURL("http://[::a]:33/x"), "[::a]:33"},
310 for (size_t i
= 0; i
< ARRAYSIZE_UNSAFE(tests
); ++i
) {
311 std::string host_and_port
= GetHostAndOptionalPort(tests
[i
].url
);
312 EXPECT_EQ(std::string(tests
[i
].expected_host_and_port
), host_and_port
);
316 TEST(NetUtilTest
, IPAddressToString
) {
317 uint8 addr1
[4] = {0, 0, 0, 0};
318 EXPECT_EQ("0.0.0.0", IPAddressToString(addr1
, sizeof(addr1
)));
320 uint8 addr2
[4] = {192, 168, 0, 1};
321 EXPECT_EQ("192.168.0.1", IPAddressToString(addr2
, sizeof(addr2
)));
323 uint8 addr3
[16] = {0xFE, 0xDC, 0xBA, 0x98};
324 EXPECT_EQ("fedc:ba98::", IPAddressToString(addr3
, sizeof(addr3
)));
327 TEST(NetUtilTest
, IPAddressToStringWithPort
) {
328 uint8 addr1
[4] = {0, 0, 0, 0};
329 EXPECT_EQ("0.0.0.0:3", IPAddressToStringWithPort(addr1
, sizeof(addr1
), 3));
331 uint8 addr2
[4] = {192, 168, 0, 1};
332 EXPECT_EQ("192.168.0.1:99",
333 IPAddressToStringWithPort(addr2
, sizeof(addr2
), 99));
335 uint8 addr3
[16] = {0xFE, 0xDC, 0xBA, 0x98};
336 EXPECT_EQ("[fedc:ba98::]:8080",
337 IPAddressToStringWithPort(addr3
, sizeof(addr3
), 8080));
340 TEST(NetUtilTest
, NetAddressToString_IPv4
) {
345 {{0, 0, 0, 0}, "0.0.0.0"},
346 {{127, 0, 0, 1}, "127.0.0.1"},
347 {{192, 168, 0, 1}, "192.168.0.1"},
350 for (size_t i
= 0; i
< ARRAYSIZE_UNSAFE(tests
); ++i
) {
351 SockaddrStorage storage
;
352 MakeIPv4Address(tests
[i
].addr
, 80, &storage
);
353 std::string result
= NetAddressToString(storage
.addr
, storage
.addr_len
);
354 EXPECT_EQ(std::string(tests
[i
].result
), result
);
358 TEST(NetUtilTest
, NetAddressToString_IPv6
) {
363 {{0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10, 0xFE, 0xDC, 0xBA,
364 0x98, 0x76, 0x54, 0x32, 0x10},
365 "fedc:ba98:7654:3210:fedc:ba98:7654:3210"},
368 for (size_t i
= 0; i
< ARRAYSIZE_UNSAFE(tests
); ++i
) {
369 SockaddrStorage storage
;
370 MakeIPv6Address(tests
[i
].addr
, 80, &storage
);
371 EXPECT_EQ(std::string(tests
[i
].result
),
372 NetAddressToString(storage
.addr
, storage
.addr_len
));
376 TEST(NetUtilTest
, NetAddressToStringWithPort_IPv4
) {
377 uint8 addr
[] = {127, 0, 0, 1};
378 SockaddrStorage storage
;
379 MakeIPv4Address(addr
, 166, &storage
);
380 std::string result
= NetAddressToStringWithPort(storage
.addr
,
382 EXPECT_EQ("127.0.0.1:166", result
);
385 TEST(NetUtilTest
, NetAddressToStringWithPort_IPv6
) {
387 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10, 0xFE, 0xDC, 0xBA,
388 0x98, 0x76, 0x54, 0x32, 0x10
390 SockaddrStorage storage
;
391 MakeIPv6Address(addr
, 361, &storage
);
392 std::string result
= NetAddressToStringWithPort(storage
.addr
,
395 // May fail on systems that don't support IPv6.
397 EXPECT_EQ("[fedc:ba98:7654:3210:fedc:ba98:7654:3210]:361", result
);
400 TEST(NetUtilTest
, GetHostName
) {
401 // We can't check the result of GetHostName() directly, since the result
402 // will differ across machines. Our goal here is to simply exercise the
403 // code path, and check that things "look about right".
404 std::string hostname
= GetHostName();
405 EXPECT_FALSE(hostname
.empty());
408 TEST(NetUtilTest
, SimplifyUrlForRequest
) {
410 const char* input_url
;
411 const char* expected_simplified_url
;
414 // Reference section should be stripped.
415 "http://www.google.com:78/foobar?query=1#hash",
416 "http://www.google.com:78/foobar?query=1",
419 // Reference section can itself contain #.
420 "http://192.168.0.1?query=1#hash#10#11#13#14",
421 "http://192.168.0.1?query=1",
423 { // Strip username/password.
424 "http://user:pass@google.com",
425 "http://google.com/",
427 { // Strip both the reference and the username/password.
428 "http://user:pass@google.com:80/sup?yo#X#X",
429 "http://google.com/sup?yo",
431 { // Try an HTTPS URL -- strip both the reference and the username/password.
432 "https://user:pass@google.com:80/sup?yo#X#X",
433 "https://google.com:80/sup?yo",
435 { // Try an FTP URL -- strip both the reference and the username/password.
436 "ftp://user:pass@google.com:80/sup?yo#X#X",
437 "ftp://google.com:80/sup?yo",
439 { // Try a nonstandard URL
440 "foobar://user:pass@google.com:80/sup?yo#X#X",
441 "foobar://user:pass@google.com:80/sup?yo",
444 for (size_t i
= 0; i
< ARRAYSIZE_UNSAFE(tests
); ++i
) {
445 SCOPED_TRACE(base::StringPrintf("Test[%" PRIuS
"]: %s", i
,
446 tests
[i
].input_url
));
447 GURL
input_url(GURL(tests
[i
].input_url
));
448 GURL
expected_url(GURL(tests
[i
].expected_simplified_url
));
449 EXPECT_EQ(expected_url
, SimplifyUrlForRequest(input_url
));
453 TEST(NetUtilTest
, SetExplicitlyAllowedPortsTest
) {
454 std::string invalid
[] = { "1,2,a", "'1','2'", "1, 2, 3", "1 0,11,12" };
455 std::string valid
[] = { "", "1", "1,2", "1,2,3", "10,11,12,13" };
457 for (size_t i
= 0; i
< ARRAYSIZE_UNSAFE(invalid
); ++i
) {
458 SetExplicitlyAllowedPorts(invalid
[i
]);
459 EXPECT_EQ(0, static_cast<int>(GetCountOfExplicitlyAllowedPorts()));
462 for (size_t i
= 0; i
< ARRAYSIZE_UNSAFE(valid
); ++i
) {
463 SetExplicitlyAllowedPorts(valid
[i
]);
464 EXPECT_EQ(i
, GetCountOfExplicitlyAllowedPorts());
468 TEST(NetUtilTest
, GetHostOrSpecFromURL
) {
469 EXPECT_EQ("example.com",
470 GetHostOrSpecFromURL(GURL("http://example.com/test")));
471 EXPECT_EQ("example.com",
472 GetHostOrSpecFromURL(GURL("http://example.com./test")));
473 EXPECT_EQ("file:///tmp/test.html",
474 GetHostOrSpecFromURL(GURL("file:///tmp/test.html")));
477 TEST(NetUtilTest
, GetAddressFamily
) {
478 IPAddressNumber number
;
479 EXPECT_TRUE(ParseIPLiteralToNumber("192.168.0.1", &number
));
480 EXPECT_EQ(ADDRESS_FAMILY_IPV4
, GetAddressFamily(number
));
481 EXPECT_TRUE(ParseIPLiteralToNumber("1:abcd::3:4:ff", &number
));
482 EXPECT_EQ(ADDRESS_FAMILY_IPV6
, GetAddressFamily(number
));
485 // Test that invalid IP literals fail to parse.
486 TEST(NetUtilTest
, ParseIPLiteralToNumber_FailParse
) {
487 IPAddressNumber number
;
489 EXPECT_FALSE(ParseIPLiteralToNumber("bad value", &number
));
490 EXPECT_FALSE(ParseIPLiteralToNumber("bad:value", &number
));
491 EXPECT_FALSE(ParseIPLiteralToNumber(std::string(), &number
));
492 EXPECT_FALSE(ParseIPLiteralToNumber("192.168.0.1:30", &number
));
493 EXPECT_FALSE(ParseIPLiteralToNumber(" 192.168.0.1 ", &number
));
494 EXPECT_FALSE(ParseIPLiteralToNumber("[::1]", &number
));
497 // Test parsing an IPv4 literal.
498 TEST(NetUtilTest
, ParseIPLiteralToNumber_IPv4
) {
499 IPAddressNumber number
;
500 EXPECT_TRUE(ParseIPLiteralToNumber("192.168.0.1", &number
));
501 EXPECT_EQ("192,168,0,1", DumpIPNumber(number
));
502 EXPECT_EQ("192.168.0.1", IPAddressToString(number
));
505 // Test parsing an IPv6 literal.
506 TEST(NetUtilTest
, ParseIPLiteralToNumber_IPv6
) {
507 IPAddressNumber number
;
508 EXPECT_TRUE(ParseIPLiteralToNumber("1:abcd::3:4:ff", &number
));
509 EXPECT_EQ("0,1,171,205,0,0,0,0,0,0,0,3,0,4,0,255", DumpIPNumber(number
));
510 EXPECT_EQ("1:abcd::3:4:ff", IPAddressToString(number
));
513 // Test mapping an IPv4 address to an IPv6 address.
514 TEST(NetUtilTest
, ConvertIPv4NumberToIPv6Number
) {
515 IPAddressNumber ipv4_number
;
516 EXPECT_TRUE(ParseIPLiteralToNumber("192.168.0.1", &ipv4_number
));
518 IPAddressNumber ipv6_number
=
519 ConvertIPv4NumberToIPv6Number(ipv4_number
);
521 // ::ffff:192.168.0.1
522 EXPECT_EQ("0,0,0,0,0,0,0,0,0,0,255,255,192,168,0,1",
523 DumpIPNumber(ipv6_number
));
524 EXPECT_EQ("::ffff:c0a8:1", IPAddressToString(ipv6_number
));
527 TEST(NetUtilTest
, ParseURLHostnameToNumber_FailParse
) {
528 IPAddressNumber number
;
530 EXPECT_FALSE(ParseURLHostnameToNumber("bad value", &number
));
531 EXPECT_FALSE(ParseURLHostnameToNumber("bad:value", &number
));
532 EXPECT_FALSE(ParseURLHostnameToNumber(std::string(), &number
));
533 EXPECT_FALSE(ParseURLHostnameToNumber("192.168.0.1:30", &number
));
534 EXPECT_FALSE(ParseURLHostnameToNumber(" 192.168.0.1 ", &number
));
535 EXPECT_FALSE(ParseURLHostnameToNumber("::1", &number
));
538 TEST(NetUtilTest
, ParseURLHostnameToNumber_IPv4
) {
539 IPAddressNumber number
;
540 EXPECT_TRUE(ParseURLHostnameToNumber("192.168.0.1", &number
));
541 EXPECT_EQ("192,168,0,1", DumpIPNumber(number
));
542 EXPECT_EQ("192.168.0.1", IPAddressToString(number
));
545 TEST(NetUtilTest
, ParseURLHostnameToNumber_IPv6
) {
546 IPAddressNumber number
;
547 EXPECT_TRUE(ParseURLHostnameToNumber("[1:abcd::3:4:ff]", &number
));
548 EXPECT_EQ("0,1,171,205,0,0,0,0,0,0,0,3,0,4,0,255", DumpIPNumber(number
));
549 EXPECT_EQ("1:abcd::3:4:ff", IPAddressToString(number
));
552 TEST(NetUtilTest
, IsIPv4Mapped
) {
553 IPAddressNumber ipv4_number
;
554 EXPECT_TRUE(ParseIPLiteralToNumber("192.168.0.1", &ipv4_number
));
555 EXPECT_FALSE(IsIPv4Mapped(ipv4_number
));
557 IPAddressNumber ipv6_number
;
558 EXPECT_TRUE(ParseIPLiteralToNumber("::1", &ipv4_number
));
559 EXPECT_FALSE(IsIPv4Mapped(ipv6_number
));
561 IPAddressNumber ipv4mapped_number
;
562 EXPECT_TRUE(ParseIPLiteralToNumber("::ffff:0101:1", &ipv4mapped_number
));
563 EXPECT_TRUE(IsIPv4Mapped(ipv4mapped_number
));
566 TEST(NetUtilTest
, ConvertIPv4MappedToIPv4
) {
567 IPAddressNumber ipv4mapped_number
;
568 EXPECT_TRUE(ParseIPLiteralToNumber("::ffff:0101:1", &ipv4mapped_number
));
569 IPAddressNumber expected
;
570 EXPECT_TRUE(ParseIPLiteralToNumber("1.1.0.1", &expected
));
571 IPAddressNumber result
= ConvertIPv4MappedToIPv4(ipv4mapped_number
);
572 EXPECT_EQ(expected
, result
);
575 // Test parsing invalid CIDR notation literals.
576 TEST(NetUtilTest
, ParseCIDRBlock_Invalid
) {
577 const char* bad_literals
[] = {
593 for (size_t i
= 0; i
< arraysize(bad_literals
); ++i
) {
594 IPAddressNumber ip_number
;
595 size_t prefix_length_in_bits
;
597 EXPECT_FALSE(ParseCIDRBlock(bad_literals
[i
],
599 &prefix_length_in_bits
));
603 // Test parsing a valid CIDR notation literal.
604 TEST(NetUtilTest
, ParseCIDRBlock_Valid
) {
605 IPAddressNumber ip_number
;
606 size_t prefix_length_in_bits
;
608 EXPECT_TRUE(ParseCIDRBlock("192.168.0.1/11",
610 &prefix_length_in_bits
));
612 EXPECT_EQ("192,168,0,1", DumpIPNumber(ip_number
));
613 EXPECT_EQ(11u, prefix_length_in_bits
);
616 TEST(NetUtilTest
, IPNumberMatchesPrefix
) {
618 const char* cidr_literal
;
619 const char* ip_literal
;
620 bool expected_to_match
;
622 // IPv4 prefix with IPv4 inputs.
639 // IPv6 prefix with IPv6 inputs.
651 // IPv6 prefix with IPv4 inputs.
658 "::ffff:192.168.0.1/112",
663 // IPv4 prefix with IPv6 inputs.
671 "::ffff:10.12.33.44",
675 for (size_t i
= 0; i
< ARRAYSIZE_UNSAFE(tests
); ++i
) {
676 SCOPED_TRACE(base::StringPrintf("Test[%" PRIuS
"]: %s, %s", i
,
677 tests
[i
].cidr_literal
,
678 tests
[i
].ip_literal
));
680 IPAddressNumber ip_number
;
681 EXPECT_TRUE(ParseIPLiteralToNumber(tests
[i
].ip_literal
, &ip_number
));
683 IPAddressNumber ip_prefix
;
684 size_t prefix_length_in_bits
;
686 EXPECT_TRUE(ParseCIDRBlock(tests
[i
].cidr_literal
,
688 &prefix_length_in_bits
));
690 EXPECT_EQ(tests
[i
].expected_to_match
,
691 IPNumberMatchesPrefix(ip_number
,
693 prefix_length_in_bits
));
697 TEST(NetUtilTest
, IsLocalhost
) {
698 EXPECT_TRUE(net::IsLocalhost("localhost"));
699 EXPECT_TRUE(net::IsLocalhost("localhost.localdomain"));
700 EXPECT_TRUE(net::IsLocalhost("localhost6"));
701 EXPECT_TRUE(net::IsLocalhost("localhost6.localdomain6"));
702 EXPECT_TRUE(net::IsLocalhost("127.0.0.1"));
703 EXPECT_TRUE(net::IsLocalhost("127.0.1.0"));
704 EXPECT_TRUE(net::IsLocalhost("127.1.0.0"));
705 EXPECT_TRUE(net::IsLocalhost("127.0.0.255"));
706 EXPECT_TRUE(net::IsLocalhost("127.0.255.0"));
707 EXPECT_TRUE(net::IsLocalhost("127.255.0.0"));
708 EXPECT_TRUE(net::IsLocalhost("::1"));
709 EXPECT_TRUE(net::IsLocalhost("0:0:0:0:0:0:0:1"));
711 EXPECT_FALSE(net::IsLocalhost("localhostx"));
712 EXPECT_FALSE(net::IsLocalhost("foo.localdomain"));
713 EXPECT_FALSE(net::IsLocalhost("localhost6x"));
714 EXPECT_FALSE(net::IsLocalhost("localhost.localdomain6"));
715 EXPECT_FALSE(net::IsLocalhost("localhost6.localdomain"));
716 EXPECT_FALSE(net::IsLocalhost("127.0.0.1.1"));
717 EXPECT_FALSE(net::IsLocalhost(".127.0.0.255"));
718 EXPECT_FALSE(net::IsLocalhost("::2"));
719 EXPECT_FALSE(net::IsLocalhost("::1:1"));
720 EXPECT_FALSE(net::IsLocalhost("0:0:0:0:1:0:0:1"));
721 EXPECT_FALSE(net::IsLocalhost("::1:1"));
722 EXPECT_FALSE(net::IsLocalhost("0:0:0:0:0:0:0:0:1"));
725 // Verify GetNetworkList().
726 TEST(NetUtilTest
, GetNetworkList
) {
727 NetworkInterfaceList list
;
728 ASSERT_TRUE(GetNetworkList(&list
, INCLUDE_HOST_SCOPE_VIRTUAL_INTERFACES
));
729 for (NetworkInterfaceList::iterator it
= list
.begin();
730 it
!= list
.end(); ++it
) {
731 // Verify that the names are not empty.
732 EXPECT_FALSE(it
->name
.empty());
733 EXPECT_FALSE(it
->friendly_name
.empty());
735 // Verify that the address is correct.
736 EXPECT_TRUE(it
->address
.size() == kIPv4AddressSize
||
737 it
->address
.size() == kIPv6AddressSize
)
738 << "Invalid address of size " << it
->address
.size();
739 bool all_zeroes
= true;
740 for (size_t i
= 0; i
< it
->address
.size(); ++i
) {
741 if (it
->address
[i
] != 0) {
746 EXPECT_FALSE(all_zeroes
);
747 EXPECT_GT(it
->network_prefix
, 1u);
748 EXPECT_LE(it
->network_prefix
, it
->address
.size() * 8);
751 // On Windows |name| is NET_LUID.
752 base::ScopedNativeLibrary
phlpapi_lib(
753 base::FilePath(FILE_PATH_LITERAL("iphlpapi.dll")));
754 ASSERT_TRUE(phlpapi_lib
.is_valid());
755 typedef NETIO_STATUS (WINAPI
* ConvertInterfaceIndexToLuid
)(NET_IFINDEX
,
757 ConvertInterfaceIndexToLuid interface_to_luid
=
758 reinterpret_cast<ConvertInterfaceIndexToLuid
>(
759 phlpapi_lib
.GetFunctionPointer("ConvertInterfaceIndexToLuid"));
761 typedef NETIO_STATUS (WINAPI
* ConvertInterfaceLuidToGuid
)(NET_LUID
*,
763 ConvertInterfaceLuidToGuid luid_to_guid
=
764 reinterpret_cast<ConvertInterfaceLuidToGuid
>(
765 phlpapi_lib
.GetFunctionPointer("ConvertInterfaceLuidToGuid"));
767 if (interface_to_luid
&& luid_to_guid
) {
769 EXPECT_EQ(interface_to_luid(it
->interface_index
, &luid
), NO_ERROR
);
771 EXPECT_EQ(luid_to_guid(&luid
, &guid
), NO_ERROR
);
773 StringFromCLSID(guid
, &name
);
774 EXPECT_STREQ(base::UTF8ToWide(it
->name
).c_str(), name
);
778 EXPECT_LT(base::win::GetVersion(), base::win::VERSION_VISTA
);
779 EXPECT_LT(it
->interface_index
, 1u << 24u); // Must fit 0.x.x.x.
780 EXPECT_NE(it
->interface_index
, 0u); // 0 means to use default.
782 if (it
->type
== NetworkChangeNotifier::CONNECTION_WIFI
) {
783 EXPECT_NE(WIFI_PHY_LAYER_PROTOCOL_NONE
, GetWifiPHYLayerProtocol());
785 #elif !defined(OS_ANDROID)
786 char name
[IF_NAMESIZE
];
787 EXPECT_TRUE(if_indextoname(it
->interface_index
, name
));
788 EXPECT_STREQ(it
->name
.c_str(), name
);
793 struct NonUniqueNameTestData
{
795 const char* hostname
;
798 // Google Test pretty-printer.
799 void PrintTo(const NonUniqueNameTestData
& data
, std::ostream
* os
) {
800 ASSERT_TRUE(data
.hostname
);
801 *os
<< " hostname: " << testing::PrintToString(data
.hostname
)
802 << "; is_unique: " << testing::PrintToString(data
.is_unique
);
805 const NonUniqueNameTestData kNonUniqueNameTestData
[] = {
806 // Domains under ICANN-assigned domains.
807 { true, "google.com" },
808 { true, "google.co.uk" },
809 // Domains under private registries.
810 { true, "appspot.com" },
811 { true, "test.appspot.com" },
812 // Unreserved IPv4 addresses (in various forms).
814 { true, "99.64.0.0" },
815 { true, "212.15.0.0" },
817 { true, "212.15.0" },
818 { true, "3557752832" },
819 // Reserved IPv4 addresses (in various forms).
820 { false, "192.168.0.0" },
821 { false, "192.168.0.6" },
822 { false, "10.0.0.5" },
825 { false, "3232235526" },
826 // Unreserved IPv6 addresses.
827 { true, "FFC0:ba98:7654:3210:FEDC:BA98:7654:3210" },
828 { true, "2000:ba98:7654:2301:EFCD:BA98:7654:3210" },
829 // Reserved IPv6 addresses.
830 { false, "::192.9.5.5" },
831 { false, "FEED::BEEF" },
832 { false, "FEC0:ba98:7654:3210:FEDC:BA98:7654:3210" },
833 // 'internal'/non-IANA assigned domains.
834 { false, "intranet" },
835 { false, "intranet." },
836 { false, "intranet.example" },
837 { false, "host.intranet.example" },
838 // gTLDs under discussion, but not yet assigned.
839 { false, "intranet.corp" },
840 { false, "example.tech" },
841 { false, "intranet.internal" },
842 // Invalid host names are treated as unique - but expected to be
843 // filtered out before then.
844 { true, "junk)(£)$*!@~#" },
845 { true, "w$w.example.com" },
846 { true, "nocolonsallowed:example" },
847 { true, "[::4.5.6.9]" },
850 class NetUtilNonUniqueNameTest
851 : public testing::TestWithParam
<NonUniqueNameTestData
> {
853 virtual ~NetUtilNonUniqueNameTest() {}
856 bool IsUnique(const std::string
& hostname
) {
857 return !IsHostnameNonUnique(hostname
);
861 // Test that internal/non-unique names are properly identified as such, but
862 // that IP addresses and hosts beneath registry-controlled domains are flagged
864 TEST_P(NetUtilNonUniqueNameTest
, IsHostnameNonUnique
) {
865 const NonUniqueNameTestData
& test_data
= GetParam();
867 EXPECT_EQ(test_data
.is_unique
, IsUnique(test_data
.hostname
));
870 INSTANTIATE_TEST_CASE_P(, NetUtilNonUniqueNameTest
,
871 testing::ValuesIn(kNonUniqueNameTestData
));