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 "net/base/ip_endpoint.h"
22 #if !defined(OS_NACL) && !defined(OS_WIN)
24 #include <netinet/in.h>
25 #if defined(OS_MACOSX)
28 #include <netinet/in_var.h>
31 #endif // !OS_NACL && !OS_WIN
32 #include "testing/gtest/include/gtest/gtest.h"
38 #include "base/win/windows_version.h"
41 #if !defined(OS_MACOSX) && !defined(OS_NACL) && !defined(OS_WIN)
42 #include "net/base/address_tracker_linux.h"
43 #endif // !OS_MACOSX && !OS_NACL && !OS_WIN
45 using base::ASCIIToUTF16
;
46 using base::WideToUTF16
;
53 const char* const header_name
;
54 const char* const expected
;
57 // Fills in sockaddr for the given 32-bit address (IPv4.)
58 // |bytes| should be an array of length 4.
59 void MakeIPv4Address(const uint8_t* bytes
, int port
, SockaddrStorage
* storage
) {
60 memset(&storage
->addr_storage
, 0, sizeof(storage
->addr_storage
));
61 storage
->addr_len
= sizeof(struct sockaddr_in
);
62 struct sockaddr_in
* addr4
= reinterpret_cast<sockaddr_in
*>(storage
->addr
);
63 addr4
->sin_port
= base::HostToNet16(port
);
64 addr4
->sin_family
= AF_INET
;
65 memcpy(&addr4
->sin_addr
, bytes
, 4);
68 // Fills in sockaddr for the given 128-bit address (IPv6.)
69 // |bytes| should be an array of length 16.
70 void MakeIPv6Address(const uint8_t* bytes
, int port
, SockaddrStorage
* storage
) {
71 memset(&storage
->addr_storage
, 0, sizeof(storage
->addr_storage
));
72 storage
->addr_len
= sizeof(struct sockaddr_in6
);
73 struct sockaddr_in6
* addr6
= reinterpret_cast<sockaddr_in6
*>(storage
->addr
);
74 addr6
->sin6_port
= base::HostToNet16(port
);
75 addr6
->sin6_family
= AF_INET6
;
76 memcpy(&addr6
->sin6_addr
, bytes
, 16);
79 } // anonymous namespace
81 TEST(NetUtilTest
, GetIdentityFromURL
) {
83 const char* const input_url
;
84 const char* const expected_username
;
85 const char* const expected_password
;
88 "http://username:password@google.com",
92 { // Test for http://crbug.com/19200
93 "http://username:p@ssword@google.com",
97 { // Special URL characters should be unescaped.
98 "http://username:p%3fa%26s%2fs%23@google.com",
102 { // Username contains %20.
103 "http://use rname:password@google.com",
108 "http://use%00rname:password@google.com",
112 { // Use a '+' in the username.
113 "http://use+rname:password@google.com",
117 { // Use a '&' in the password.
118 "http://username:p&ssword@google.com",
123 for (size_t i
= 0; i
< arraysize(tests
); ++i
) {
124 SCOPED_TRACE(base::StringPrintf("Test[%" PRIuS
"]: %s", i
,
125 tests
[i
].input_url
));
126 GURL
url(tests
[i
].input_url
);
128 base::string16 username
, password
;
129 GetIdentityFromURL(url
, &username
, &password
);
131 EXPECT_EQ(ASCIIToUTF16(tests
[i
].expected_username
), username
);
132 EXPECT_EQ(ASCIIToUTF16(tests
[i
].expected_password
), password
);
136 // Try extracting a username which was encoded with UTF8.
137 TEST(NetUtilTest
, GetIdentityFromURL_UTF8
) {
138 GURL
url(WideToUTF16(L
"http://foo:\x4f60\x597d@blah.com"));
140 EXPECT_EQ("foo", url
.username());
141 EXPECT_EQ("%E4%BD%A0%E5%A5%BD", url
.password());
143 // Extract the unescaped identity.
144 base::string16 username
, password
;
145 GetIdentityFromURL(url
, &username
, &password
);
147 // Verify that it was decoded as UTF8.
148 EXPECT_EQ(ASCIIToUTF16("foo"), username
);
149 EXPECT_EQ(WideToUTF16(L
"\x4f60\x597d"), password
);
152 // Just a bunch of fake headers.
153 const char google_headers
[] =
155 "Content-TYPE: text/html; charset=utf-8\n"
156 "Content-disposition: attachment; filename=\"download.pdf\"\n"
157 "Content-Length: 378557\n"
158 "X-Google-Google1: 314159265\n"
159 "X-Google-Google2: aaaa2:7783,bbb21:9441\n"
160 "X-Google-Google4: home\n"
161 "Transfer-Encoding: chunked\n"
162 "Set-Cookie: HEHE_AT=6666x66beef666x6-66xx6666x66; Path=/mail\n"
163 "Set-Cookie: HEHE_HELP=owned:0;Path=/\n"
164 "Set-Cookie: S=gmail=Xxx-beefbeefbeef_beefb:gmail_yj=beefbeef000beefbee"
165 "fbee:gmproxy=bee-fbeefbe; Domain=.google.com; Path=/\n"
166 "X-Google-Google2: /one/two/three/four/five/six/seven-height/nine:9411\n"
168 "Transfer-Encoding: chunked\n"
169 "Date: Mon, 13 Nov 2006 21:38:09 GMT\n"
170 "Expires: Tue, 14 Nov 2006 19:23:58 GMT\n"
171 "X-Malformed: bla; arg=test\"\n"
172 "X-Malformed2: bla; arg=\n"
173 "X-Test: bla; arg1=val1; arg2=val2";
175 TEST(NetUtilTest
, GetSpecificHeader
) {
176 const HeaderCase tests
[] = {
177 {"content-type", "text/html; charset=utf-8"},
178 {"CONTENT-LENGTH", "378557"},
179 {"Date", "Mon, 13 Nov 2006 21:38:09 GMT"},
184 // Test first with google_headers.
185 for (size_t i
= 0; i
< arraysize(tests
); ++i
) {
187 GetSpecificHeader(google_headers
, tests
[i
].header_name
);
188 EXPECT_EQ(result
, tests
[i
].expected
);
191 // Test again with empty headers.
192 for (size_t i
= 0; i
< arraysize(tests
); ++i
) {
193 std::string result
= GetSpecificHeader(std::string(), tests
[i
].header_name
);
194 EXPECT_EQ(result
, std::string());
198 TEST(NetUtilTest
, CompliantHost
) {
199 struct CompliantHostCase
{
200 const char* const host
;
201 bool expected_output
;
204 const CompliantHostCase compliant_host_cases
[] = {
230 {"1.2.3.4.5.", true},
233 for (size_t i
= 0; i
< arraysize(compliant_host_cases
); ++i
) {
234 EXPECT_EQ(compliant_host_cases
[i
].expected_output
,
235 IsCanonicalizedHostCompliant(compliant_host_cases
[i
].host
));
239 TEST(NetUtilTest
, ParseHostAndPort
) {
241 const char* const input
;
243 const char* const expected_host
;
247 {"foo:10", true, "foo", 10},
248 {"foo", true, "foo", -1},
250 "[1080:0:0:0:8:800:200C:4171]:11",
252 "1080:0:0:0:8:800:200C:4171",
256 "[1080:0:0:0:8:800:200C:4171]",
258 "1080:0:0:0:8:800:200C:4171",
262 // Because no validation is done on the host, the following are accepted,
263 // even though they are invalid names.
264 {"]", true, "]", -1},
265 {"::1", true, ":", 1},
267 {"foo:bar", false, "", -1},
268 {"foo:", false, "", -1},
269 {":", false, "", -1},
270 {":80", false, "", -1},
272 {"porttoolong:300000", false, "", -1},
273 {"usrname@host", false, "", -1},
274 {"usrname:password@host", false, "", -1},
275 {":password@host", false, "", -1},
276 {":password@host:80", false, "", -1},
277 {":password@host", false, "", -1},
278 {"@host", false, "", -1},
279 {"[", false, "", -1},
280 {"[]", false, "", -1},
283 for (size_t i
= 0; i
< arraysize(tests
); ++i
) {
286 bool ok
= ParseHostAndPort(tests
[i
].input
, &host
, &port
);
288 EXPECT_EQ(tests
[i
].success
, ok
);
290 if (tests
[i
].success
) {
291 EXPECT_EQ(tests
[i
].expected_host
, host
);
292 EXPECT_EQ(tests
[i
].expected_port
, port
);
297 TEST(NetUtilTest
, GetHostAndPort
) {
300 const char* const expected_host_and_port
;
302 { GURL("http://www.foo.com/x"), "www.foo.com:80"},
303 { GURL("http://www.foo.com:21/x"), "www.foo.com:21"},
305 // For IPv6 literals should always include the brackets.
306 { GURL("http://[1::2]/x"), "[1::2]:80"},
307 { GURL("http://[::a]:33/x"), "[::a]:33"},
309 for (size_t i
= 0; i
< arraysize(tests
); ++i
) {
310 std::string host_and_port
= GetHostAndPort(tests
[i
].url
);
311 EXPECT_EQ(std::string(tests
[i
].expected_host_and_port
), host_and_port
);
315 TEST(NetUtilTest
, GetHostAndOptionalPort
) {
318 const char* const expected_host_and_port
;
320 { GURL("http://www.foo.com/x"), "www.foo.com"},
321 { GURL("http://www.foo.com:21/x"), "www.foo.com:21"},
323 // For IPv6 literals should always include the brackets.
324 { GURL("http://[1::2]/x"), "[1::2]"},
325 { GURL("http://[::a]:33/x"), "[::a]:33"},
327 for (size_t i
= 0; i
< arraysize(tests
); ++i
) {
328 std::string host_and_port
= GetHostAndOptionalPort(tests
[i
].url
);
329 EXPECT_EQ(std::string(tests
[i
].expected_host_and_port
), host_and_port
);
333 TEST(NetUtilTest
, NetAddressToString_IPv4
) {
336 const char* const result
;
338 {{0, 0, 0, 0}, "0.0.0.0"},
339 {{127, 0, 0, 1}, "127.0.0.1"},
340 {{192, 168, 0, 1}, "192.168.0.1"},
343 for (size_t i
= 0; i
< arraysize(tests
); ++i
) {
344 SockaddrStorage storage
;
345 MakeIPv4Address(tests
[i
].addr
, 80, &storage
);
346 std::string result
= NetAddressToString(storage
.addr
, storage
.addr_len
);
347 EXPECT_EQ(std::string(tests
[i
].result
), result
);
351 TEST(NetUtilTest
, NetAddressToString_IPv6
) {
354 const char* const result
;
356 {{0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10, 0xFE, 0xDC, 0xBA,
357 0x98, 0x76, 0x54, 0x32, 0x10},
358 "fedc:ba98:7654:3210:fedc:ba98:7654:3210"},
361 for (size_t i
= 0; i
< arraysize(tests
); ++i
) {
362 SockaddrStorage storage
;
363 MakeIPv6Address(tests
[i
].addr
, 80, &storage
);
364 EXPECT_EQ(std::string(tests
[i
].result
),
365 NetAddressToString(storage
.addr
, storage
.addr_len
));
369 TEST(NetUtilTest
, NetAddressToStringWithPort_IPv4
) {
370 uint8_t addr
[] = {127, 0, 0, 1};
371 SockaddrStorage storage
;
372 MakeIPv4Address(addr
, 166, &storage
);
373 std::string result
= NetAddressToStringWithPort(storage
.addr
,
375 EXPECT_EQ("127.0.0.1:166", result
);
378 TEST(NetUtilTest
, NetAddressToStringWithPort_IPv6
) {
380 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10, 0xFE, 0xDC, 0xBA,
381 0x98, 0x76, 0x54, 0x32, 0x10
383 SockaddrStorage storage
;
384 MakeIPv6Address(addr
, 361, &storage
);
385 std::string result
= NetAddressToStringWithPort(storage
.addr
,
388 // May fail on systems that don't support IPv6.
390 EXPECT_EQ("[fedc:ba98:7654:3210:fedc:ba98:7654:3210]:361", result
);
393 TEST(NetUtilTest
, GetHostName
) {
394 // We can't check the result of GetHostName() directly, since the result
395 // will differ across machines. Our goal here is to simply exercise the
396 // code path, and check that things "look about right".
397 std::string hostname
= GetHostName();
398 EXPECT_FALSE(hostname
.empty());
401 TEST(NetUtilTest
, SimplifyUrlForRequest
) {
403 const char* const input_url
;
404 const char* const expected_simplified_url
;
407 // Reference section should be stripped.
408 "http://www.google.com:78/foobar?query=1#hash",
409 "http://www.google.com:78/foobar?query=1",
412 // Reference section can itself contain #.
413 "http://192.168.0.1?query=1#hash#10#11#13#14",
414 "http://192.168.0.1?query=1",
416 { // Strip username/password.
417 "http://user:pass@google.com",
418 "http://google.com/",
420 { // Strip both the reference and the username/password.
421 "http://user:pass@google.com:80/sup?yo#X#X",
422 "http://google.com/sup?yo",
424 { // Try an HTTPS URL -- strip both the reference and the username/password.
425 "https://user:pass@google.com:80/sup?yo#X#X",
426 "https://google.com:80/sup?yo",
428 { // Try an FTP URL -- strip both the reference and the username/password.
429 "ftp://user:pass@google.com:80/sup?yo#X#X",
430 "ftp://google.com:80/sup?yo",
432 { // Try a nonstandard URL
433 "foobar://user:pass@google.com:80/sup?yo#X#X",
434 "foobar://user:pass@google.com:80/sup?yo",
437 for (size_t i
= 0; i
< arraysize(tests
); ++i
) {
438 SCOPED_TRACE(base::StringPrintf("Test[%" PRIuS
"]: %s", i
,
439 tests
[i
].input_url
));
440 GURL
input_url(GURL(tests
[i
].input_url
));
441 GURL
expected_url(GURL(tests
[i
].expected_simplified_url
));
442 EXPECT_EQ(expected_url
, SimplifyUrlForRequest(input_url
));
446 TEST(NetUtilTest
, SetExplicitlyAllowedPortsTest
) {
447 std::string invalid
[] = { "1,2,a", "'1','2'", "1, 2, 3", "1 0,11,12" };
448 std::string valid
[] = { "", "1", "1,2", "1,2,3", "10,11,12,13" };
450 for (size_t i
= 0; i
< arraysize(invalid
); ++i
) {
451 SetExplicitlyAllowedPorts(invalid
[i
]);
452 EXPECT_EQ(0, static_cast<int>(GetCountOfExplicitlyAllowedPorts()));
455 for (size_t i
= 0; i
< arraysize(valid
); ++i
) {
456 SetExplicitlyAllowedPorts(valid
[i
]);
457 EXPECT_EQ(i
, GetCountOfExplicitlyAllowedPorts());
461 TEST(NetUtilTest
, GetHostOrSpecFromURL
) {
462 EXPECT_EQ("example.com",
463 GetHostOrSpecFromURL(GURL("http://example.com/test")));
464 EXPECT_EQ("example.com",
465 GetHostOrSpecFromURL(GURL("http://example.com./test")));
466 EXPECT_EQ("file:///tmp/test.html",
467 GetHostOrSpecFromURL(GURL("file:///tmp/test.html")));
470 TEST(NetUtilTest
, GetAddressFamily
) {
471 IPAddressNumber number
;
472 EXPECT_TRUE(ParseIPLiteralToNumber("192.168.0.1", &number
));
473 EXPECT_EQ(ADDRESS_FAMILY_IPV4
, GetAddressFamily(number
));
474 EXPECT_TRUE(ParseIPLiteralToNumber("1:abcd::3:4:ff", &number
));
475 EXPECT_EQ(ADDRESS_FAMILY_IPV6
, GetAddressFamily(number
));
478 TEST(NetUtilTest
, IsLocalhost
) {
479 EXPECT_TRUE(IsLocalhost("localhost"));
480 EXPECT_TRUE(IsLocalhost("localhost.localdomain"));
481 EXPECT_TRUE(IsLocalhost("localhost6"));
482 EXPECT_TRUE(IsLocalhost("localhost6.localdomain6"));
483 EXPECT_TRUE(IsLocalhost("127.0.0.1"));
484 EXPECT_TRUE(IsLocalhost("127.0.1.0"));
485 EXPECT_TRUE(IsLocalhost("127.1.0.0"));
486 EXPECT_TRUE(IsLocalhost("127.0.0.255"));
487 EXPECT_TRUE(IsLocalhost("127.0.255.0"));
488 EXPECT_TRUE(IsLocalhost("127.255.0.0"));
489 EXPECT_TRUE(IsLocalhost("::1"));
490 EXPECT_TRUE(IsLocalhost("0:0:0:0:0:0:0:1"));
491 EXPECT_TRUE(IsLocalhost("foo.localhost"));
493 EXPECT_FALSE(IsLocalhost("localhostx"));
494 EXPECT_FALSE(IsLocalhost("foo.localdomain"));
495 EXPECT_FALSE(IsLocalhost("localhost6x"));
496 EXPECT_FALSE(IsLocalhost("localhost.localdomain6"));
497 EXPECT_FALSE(IsLocalhost("localhost6.localdomain"));
498 EXPECT_FALSE(IsLocalhost("127.0.0.1.1"));
499 EXPECT_FALSE(IsLocalhost(".127.0.0.255"));
500 EXPECT_FALSE(IsLocalhost("::2"));
501 EXPECT_FALSE(IsLocalhost("::1:1"));
502 EXPECT_FALSE(IsLocalhost("0:0:0:0:1:0:0:1"));
503 EXPECT_FALSE(IsLocalhost("::1:1"));
504 EXPECT_FALSE(IsLocalhost("0:0:0:0:0:0:0:0:1"));
505 EXPECT_FALSE(IsLocalhost("foo.localhost.com"));
506 EXPECT_FALSE(IsLocalhost("foo.localhoste"));
509 TEST(NetUtilTest
, IsLocalhostTLD
) {
510 EXPECT_TRUE(IsLocalhostTLD("foo.localhost"));
511 EXPECT_TRUE(IsLocalhostTLD("foo.localhost."));
512 EXPECT_FALSE(IsLocalhostTLD("foo.localhos"));
513 EXPECT_FALSE(IsLocalhostTLD("foo.localhost.com"));
514 EXPECT_FALSE(IsLocalhost("foo.localhoste"));
517 TEST(NetUtilTest
, GoogleHost
) {
518 struct GoogleHostCase
{
520 bool expected_output
;
523 const GoogleHostCase google_host_cases
[] = {
524 {GURL("http://.google.com"), true},
525 {GURL("http://.youtube.com"), true},
526 {GURL("http://.gmail.com"), true},
527 {GURL("http://.doubleclick.net"), true},
528 {GURL("http://.gstatic.com"), true},
529 {GURL("http://.googlevideo.com"), true},
530 {GURL("http://.googleusercontent.com"), true},
531 {GURL("http://.googlesyndication.com"), true},
532 {GURL("http://.google-analytics.com"), true},
533 {GURL("http://.googleadservices.com"), true},
534 {GURL("http://.googleapis.com"), true},
535 {GURL("http://a.google.com"), true},
536 {GURL("http://b.youtube.com"), true},
537 {GURL("http://c.gmail.com"), true},
538 {GURL("http://google.com"), false},
539 {GURL("http://youtube.com"), false},
540 {GURL("http://gmail.com"), false},
541 {GURL("http://google.coma"), false},
542 {GURL("http://agoogle.com"), false},
543 {GURL("http://oogle.com"), false},
544 {GURL("http://google.co"), false},
545 {GURL("http://oggole.com"), false},
548 for (size_t i
= 0; i
< arraysize(google_host_cases
); ++i
) {
549 EXPECT_EQ(google_host_cases
[i
].expected_output
,
550 HasGoogleHost(google_host_cases
[i
].url
));
554 struct NonUniqueNameTestData
{
556 const char* const hostname
;
559 // Google Test pretty-printer.
560 void PrintTo(const NonUniqueNameTestData
& data
, std::ostream
* os
) {
561 ASSERT_TRUE(data
.hostname
);
562 *os
<< " hostname: " << testing::PrintToString(data
.hostname
)
563 << "; is_unique: " << testing::PrintToString(data
.is_unique
);
566 const NonUniqueNameTestData kNonUniqueNameTestData
[] = {
567 // Domains under ICANN-assigned domains.
568 { true, "google.com" },
569 { true, "google.co.uk" },
570 // Domains under private registries.
571 { true, "appspot.com" },
572 { true, "test.appspot.com" },
573 // Unreserved IPv4 addresses (in various forms).
575 { true, "99.64.0.0" },
576 { true, "212.15.0.0" },
578 { true, "212.15.0" },
579 { true, "3557752832" },
580 // Reserved IPv4 addresses (in various forms).
581 { false, "192.168.0.0" },
582 { false, "192.168.0.6" },
583 { false, "10.0.0.5" },
586 { false, "3232235526" },
587 // Unreserved IPv6 addresses.
588 { true, "FFC0:ba98:7654:3210:FEDC:BA98:7654:3210" },
589 { true, "2000:ba98:7654:2301:EFCD:BA98:7654:3210" },
590 // Reserved IPv6 addresses.
591 { false, "::192.9.5.5" },
592 { false, "FEED::BEEF" },
593 { false, "FEC0:ba98:7654:3210:FEDC:BA98:7654:3210" },
594 // 'internal'/non-IANA assigned domains.
595 { false, "intranet" },
596 { false, "intranet." },
597 { false, "intranet.example" },
598 { false, "host.intranet.example" },
599 // gTLDs under discussion, but not yet assigned.
600 { false, "intranet.corp" },
601 { false, "intranet.internal" },
602 // Invalid host names are treated as unique - but expected to be
603 // filtered out before then.
604 { true, "junk)(£)$*!@~#" },
605 { true, "w$w.example.com" },
606 { true, "nocolonsallowed:example" },
607 { true, "[::4.5.6.9]" },
610 class NetUtilNonUniqueNameTest
611 : public testing::TestWithParam
<NonUniqueNameTestData
> {
613 virtual ~NetUtilNonUniqueNameTest() {}
616 bool IsUnique(const std::string
& hostname
) {
617 return !IsHostnameNonUnique(hostname
);
621 // Test that internal/non-unique names are properly identified as such, but
622 // that IP addresses and hosts beneath registry-controlled domains are flagged
624 TEST_P(NetUtilNonUniqueNameTest
, IsHostnameNonUnique
) {
625 const NonUniqueNameTestData
& test_data
= GetParam();
627 EXPECT_EQ(test_data
.is_unique
, IsUnique(test_data
.hostname
));
630 INSTANTIATE_TEST_CASE_P(, NetUtilNonUniqueNameTest
,
631 testing::ValuesIn(kNonUniqueNameTestData
));