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/address_list.h"
21 #include "net/base/ip_endpoint.h"
23 #if !defined(OS_NACL) && !defined(OS_WIN)
25 #include <netinet/in.h>
26 #if defined(OS_MACOSX)
29 #include <netinet/in_var.h>
32 #endif // !OS_NACL && !OS_WIN
33 #include "testing/gtest/include/gtest/gtest.h"
39 #include "base/win/windows_version.h"
42 #if !defined(OS_MACOSX) && !defined(OS_NACL) && !defined(OS_WIN)
43 #include "net/base/address_tracker_linux.h"
44 #endif // !OS_MACOSX && !OS_NACL && !OS_WIN
46 using base::ASCIIToUTF16
;
47 using base::WideToUTF16
;
54 const char* const header_name
;
55 const char* const expected
;
58 const unsigned char kLocalhostIPv4
[] = {127, 0, 0, 1};
59 const unsigned char kLocalhostIPv6
[] =
60 {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1};
61 const uint16_t kLocalhostLookupPort
= 80;
63 // Fills in sockaddr for the given 32-bit address (IPv4.)
64 // |bytes| should be an array of length 4.
65 void MakeIPv4Address(const uint8_t* bytes
, int port
, SockaddrStorage
* storage
) {
66 memset(&storage
->addr_storage
, 0, sizeof(storage
->addr_storage
));
67 storage
->addr_len
= sizeof(struct sockaddr_in
);
68 struct sockaddr_in
* addr4
= reinterpret_cast<sockaddr_in
*>(storage
->addr
);
69 addr4
->sin_port
= base::HostToNet16(port
);
70 addr4
->sin_family
= AF_INET
;
71 memcpy(&addr4
->sin_addr
, bytes
, 4);
74 // Fills in sockaddr for the given 128-bit address (IPv6.)
75 // |bytes| should be an array of length 16.
76 void MakeIPv6Address(const uint8_t* bytes
, int port
, SockaddrStorage
* storage
) {
77 memset(&storage
->addr_storage
, 0, sizeof(storage
->addr_storage
));
78 storage
->addr_len
= sizeof(struct sockaddr_in6
);
79 struct sockaddr_in6
* addr6
= reinterpret_cast<sockaddr_in6
*>(storage
->addr
);
80 addr6
->sin6_port
= base::HostToNet16(port
);
81 addr6
->sin6_family
= AF_INET6
;
82 memcpy(&addr6
->sin6_addr
, bytes
, 16);
85 bool HasEndpoint(const IPEndPoint
& endpoint
, const AddressList
& addresses
) {
86 for (const auto& address
: addresses
) {
87 if (endpoint
== address
)
93 void TestBothLoopbackIPs(const std::string
& host
) {
94 IPEndPoint
localhost_ipv4(
95 IPAddressNumber(kLocalhostIPv4
,
96 kLocalhostIPv4
+ arraysize(kLocalhostIPv4
)),
97 kLocalhostLookupPort
);
98 IPEndPoint
localhost_ipv6(
99 IPAddressNumber(kLocalhostIPv6
,
100 kLocalhostIPv6
+ arraysize(kLocalhostIPv6
)),
101 kLocalhostLookupPort
);
103 AddressList addresses
;
104 EXPECT_TRUE(ResolveLocalHostname(host
, kLocalhostLookupPort
, &addresses
));
105 EXPECT_EQ(2u, addresses
.size());
106 EXPECT_TRUE(HasEndpoint(localhost_ipv4
, addresses
));
107 EXPECT_TRUE(HasEndpoint(localhost_ipv6
, addresses
));
110 void TestIPv6LoopbackOnly(const std::string
& host
) {
111 IPEndPoint
localhost_ipv6(
112 IPAddressNumber(kLocalhostIPv6
,
113 kLocalhostIPv6
+ arraysize(kLocalhostIPv6
)),
114 kLocalhostLookupPort
);
116 AddressList addresses
;
117 EXPECT_TRUE(ResolveLocalHostname(host
, kLocalhostLookupPort
, &addresses
));
118 EXPECT_EQ(1u, addresses
.size());
119 EXPECT_TRUE(HasEndpoint(localhost_ipv6
, addresses
));
122 } // anonymous namespace
124 TEST(NetUtilTest
, GetIdentityFromURL
) {
126 const char* const input_url
;
127 const char* const expected_username
;
128 const char* const expected_password
;
131 "http://username:password@google.com",
135 { // Test for http://crbug.com/19200
136 "http://username:p@ssword@google.com",
140 { // Special URL characters should be unescaped.
141 "http://username:p%3fa%26s%2fs%23@google.com",
145 { // Username contains %20.
146 "http://use rname:password@google.com",
151 "http://use%00rname:password@google.com",
155 { // Use a '+' in the username.
156 "http://use+rname:password@google.com",
160 { // Use a '&' in the password.
161 "http://username:p&ssword@google.com",
166 for (size_t i
= 0; i
< arraysize(tests
); ++i
) {
167 SCOPED_TRACE(base::StringPrintf("Test[%" PRIuS
"]: %s", i
,
168 tests
[i
].input_url
));
169 GURL
url(tests
[i
].input_url
);
171 base::string16 username
, password
;
172 GetIdentityFromURL(url
, &username
, &password
);
174 EXPECT_EQ(ASCIIToUTF16(tests
[i
].expected_username
), username
);
175 EXPECT_EQ(ASCIIToUTF16(tests
[i
].expected_password
), password
);
179 // Try extracting a username which was encoded with UTF8.
180 TEST(NetUtilTest
, GetIdentityFromURL_UTF8
) {
181 GURL
url(WideToUTF16(L
"http://foo:\x4f60\x597d@blah.com"));
183 EXPECT_EQ("foo", url
.username());
184 EXPECT_EQ("%E4%BD%A0%E5%A5%BD", url
.password());
186 // Extract the unescaped identity.
187 base::string16 username
, password
;
188 GetIdentityFromURL(url
, &username
, &password
);
190 // Verify that it was decoded as UTF8.
191 EXPECT_EQ(ASCIIToUTF16("foo"), username
);
192 EXPECT_EQ(WideToUTF16(L
"\x4f60\x597d"), password
);
195 // Just a bunch of fake headers.
196 const char google_headers
[] =
198 "Content-TYPE: text/html; charset=utf-8\n"
199 "Content-disposition: attachment; filename=\"download.pdf\"\n"
200 "Content-Length: 378557\n"
201 "X-Google-Google1: 314159265\n"
202 "X-Google-Google2: aaaa2:7783,bbb21:9441\n"
203 "X-Google-Google4: home\n"
204 "Transfer-Encoding: chunked\n"
205 "Set-Cookie: HEHE_AT=6666x66beef666x6-66xx6666x66; Path=/mail\n"
206 "Set-Cookie: HEHE_HELP=owned:0;Path=/\n"
207 "Set-Cookie: S=gmail=Xxx-beefbeefbeef_beefb:gmail_yj=beefbeef000beefbee"
208 "fbee:gmproxy=bee-fbeefbe; Domain=.google.com; Path=/\n"
209 "X-Google-Google2: /one/two/three/four/five/six/seven-height/nine:9411\n"
211 "Transfer-Encoding: chunked\n"
212 "Date: Mon, 13 Nov 2006 21:38:09 GMT\n"
213 "Expires: Tue, 14 Nov 2006 19:23:58 GMT\n"
214 "X-Malformed: bla; arg=test\"\n"
215 "X-Malformed2: bla; arg=\n"
216 "X-Test: bla; arg1=val1; arg2=val2";
218 TEST(NetUtilTest
, GetSpecificHeader
) {
219 const HeaderCase tests
[] = {
220 {"content-type", "text/html; charset=utf-8"},
221 {"CONTENT-LENGTH", "378557"},
222 {"Date", "Mon, 13 Nov 2006 21:38:09 GMT"},
227 // Test first with google_headers.
228 for (size_t i
= 0; i
< arraysize(tests
); ++i
) {
230 GetSpecificHeader(google_headers
, tests
[i
].header_name
);
231 EXPECT_EQ(result
, tests
[i
].expected
);
234 // Test again with empty headers.
235 for (size_t i
= 0; i
< arraysize(tests
); ++i
) {
236 std::string result
= GetSpecificHeader(std::string(), tests
[i
].header_name
);
237 EXPECT_EQ(result
, std::string());
241 TEST(NetUtilTest
, CompliantHost
) {
242 struct CompliantHostCase
{
243 const char* const host
;
244 bool expected_output
;
247 const CompliantHostCase compliant_host_cases
[] = {
273 {"1.2.3.4.5.", true},
276 for (size_t i
= 0; i
< arraysize(compliant_host_cases
); ++i
) {
277 EXPECT_EQ(compliant_host_cases
[i
].expected_output
,
278 IsCanonicalizedHostCompliant(compliant_host_cases
[i
].host
));
282 TEST(NetUtilTest
, ParseHostAndPort
) {
284 const char* const input
;
286 const char* const expected_host
;
290 {"foo:10", true, "foo", 10},
291 {"foo", true, "foo", -1},
293 "[1080:0:0:0:8:800:200C:4171]:11",
295 "1080:0:0:0:8:800:200C:4171",
299 "[1080:0:0:0:8:800:200C:4171]",
301 "1080:0:0:0:8:800:200C:4171",
305 // Because no validation is done on the host, the following are accepted,
306 // even though they are invalid names.
307 {"]", true, "]", -1},
308 {"::1", true, ":", 1},
310 {"foo:bar", false, "", -1},
311 {"foo:", false, "", -1},
312 {":", false, "", -1},
313 {":80", false, "", -1},
315 {"porttoolong:300000", false, "", -1},
316 {"usrname@host", false, "", -1},
317 {"usrname:password@host", false, "", -1},
318 {":password@host", false, "", -1},
319 {":password@host:80", false, "", -1},
320 {":password@host", false, "", -1},
321 {"@host", false, "", -1},
322 {"[", false, "", -1},
323 {"[]", false, "", -1},
326 for (size_t i
= 0; i
< arraysize(tests
); ++i
) {
329 bool ok
= ParseHostAndPort(tests
[i
].input
, &host
, &port
);
331 EXPECT_EQ(tests
[i
].success
, ok
);
333 if (tests
[i
].success
) {
334 EXPECT_EQ(tests
[i
].expected_host
, host
);
335 EXPECT_EQ(tests
[i
].expected_port
, port
);
340 TEST(NetUtilTest
, GetHostAndPort
) {
343 const char* const expected_host_and_port
;
345 { GURL("http://www.foo.com/x"), "www.foo.com:80"},
346 { GURL("http://www.foo.com:21/x"), "www.foo.com:21"},
348 // For IPv6 literals should always include the brackets.
349 { GURL("http://[1::2]/x"), "[1::2]:80"},
350 { GURL("http://[::a]:33/x"), "[::a]:33"},
352 for (size_t i
= 0; i
< arraysize(tests
); ++i
) {
353 std::string host_and_port
= GetHostAndPort(tests
[i
].url
);
354 EXPECT_EQ(std::string(tests
[i
].expected_host_and_port
), host_and_port
);
358 TEST(NetUtilTest
, GetHostAndOptionalPort
) {
361 const char* const expected_host_and_port
;
363 { GURL("http://www.foo.com/x"), "www.foo.com"},
364 { GURL("http://www.foo.com:21/x"), "www.foo.com:21"},
366 // For IPv6 literals should always include the brackets.
367 { GURL("http://[1::2]/x"), "[1::2]"},
368 { GURL("http://[::a]:33/x"), "[::a]:33"},
370 for (size_t i
= 0; i
< arraysize(tests
); ++i
) {
371 std::string host_and_port
= GetHostAndOptionalPort(tests
[i
].url
);
372 EXPECT_EQ(std::string(tests
[i
].expected_host_and_port
), host_and_port
);
376 TEST(NetUtilTest
, NetAddressToString_IPv4
) {
379 const char* const result
;
381 {{0, 0, 0, 0}, "0.0.0.0"},
382 {{127, 0, 0, 1}, "127.0.0.1"},
383 {{192, 168, 0, 1}, "192.168.0.1"},
386 for (size_t i
= 0; i
< arraysize(tests
); ++i
) {
387 SockaddrStorage storage
;
388 MakeIPv4Address(tests
[i
].addr
, 80, &storage
);
389 std::string result
= NetAddressToString(storage
.addr
, storage
.addr_len
);
390 EXPECT_EQ(std::string(tests
[i
].result
), result
);
394 TEST(NetUtilTest
, NetAddressToString_IPv6
) {
397 const char* const result
;
399 {{0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10, 0xFE, 0xDC, 0xBA,
400 0x98, 0x76, 0x54, 0x32, 0x10},
401 "fedc:ba98:7654:3210:fedc:ba98:7654:3210"},
404 for (size_t i
= 0; i
< arraysize(tests
); ++i
) {
405 SockaddrStorage storage
;
406 MakeIPv6Address(tests
[i
].addr
, 80, &storage
);
407 EXPECT_EQ(std::string(tests
[i
].result
),
408 NetAddressToString(storage
.addr
, storage
.addr_len
));
412 TEST(NetUtilTest
, NetAddressToStringWithPort_IPv4
) {
413 uint8_t addr
[] = {127, 0, 0, 1};
414 SockaddrStorage storage
;
415 MakeIPv4Address(addr
, 166, &storage
);
416 std::string result
= NetAddressToStringWithPort(storage
.addr
,
418 EXPECT_EQ("127.0.0.1:166", result
);
421 TEST(NetUtilTest
, NetAddressToStringWithPort_IPv6
) {
423 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10, 0xFE, 0xDC, 0xBA,
424 0x98, 0x76, 0x54, 0x32, 0x10
426 SockaddrStorage storage
;
427 MakeIPv6Address(addr
, 361, &storage
);
428 std::string result
= NetAddressToStringWithPort(storage
.addr
,
431 // May fail on systems that don't support IPv6.
433 EXPECT_EQ("[fedc:ba98:7654:3210:fedc:ba98:7654:3210]:361", result
);
436 TEST(NetUtilTest
, GetHostName
) {
437 // We can't check the result of GetHostName() directly, since the result
438 // will differ across machines. Our goal here is to simply exercise the
439 // code path, and check that things "look about right".
440 std::string hostname
= GetHostName();
441 EXPECT_FALSE(hostname
.empty());
444 TEST(NetUtilTest
, SimplifyUrlForRequest
) {
446 const char* const input_url
;
447 const char* const expected_simplified_url
;
450 // Reference section should be stripped.
451 "http://www.google.com:78/foobar?query=1#hash",
452 "http://www.google.com:78/foobar?query=1",
455 // Reference section can itself contain #.
456 "http://192.168.0.1?query=1#hash#10#11#13#14",
457 "http://192.168.0.1?query=1",
459 { // Strip username/password.
460 "http://user:pass@google.com",
461 "http://google.com/",
463 { // Strip both the reference and the username/password.
464 "http://user:pass@google.com:80/sup?yo#X#X",
465 "http://google.com/sup?yo",
467 { // Try an HTTPS URL -- strip both the reference and the username/password.
468 "https://user:pass@google.com:80/sup?yo#X#X",
469 "https://google.com:80/sup?yo",
471 { // Try an FTP URL -- strip both the reference and the username/password.
472 "ftp://user:pass@google.com:80/sup?yo#X#X",
473 "ftp://google.com:80/sup?yo",
475 { // Try a nonstandard URL
476 "foobar://user:pass@google.com:80/sup?yo#X#X",
477 "foobar://user:pass@google.com:80/sup?yo",
480 for (size_t i
= 0; i
< arraysize(tests
); ++i
) {
481 SCOPED_TRACE(base::StringPrintf("Test[%" PRIuS
"]: %s", i
,
482 tests
[i
].input_url
));
483 GURL
input_url(GURL(tests
[i
].input_url
));
484 GURL
expected_url(GURL(tests
[i
].expected_simplified_url
));
485 EXPECT_EQ(expected_url
, SimplifyUrlForRequest(input_url
));
489 TEST(NetUtilTest
, SetExplicitlyAllowedPortsTest
) {
490 std::string invalid
[] = { "1,2,a", "'1','2'", "1, 2, 3", "1 0,11,12" };
491 std::string valid
[] = { "", "1", "1,2", "1,2,3", "10,11,12,13" };
493 for (size_t i
= 0; i
< arraysize(invalid
); ++i
) {
494 SetExplicitlyAllowedPorts(invalid
[i
]);
495 EXPECT_EQ(0, static_cast<int>(GetCountOfExplicitlyAllowedPorts()));
498 for (size_t i
= 0; i
< arraysize(valid
); ++i
) {
499 SetExplicitlyAllowedPorts(valid
[i
]);
500 EXPECT_EQ(i
, GetCountOfExplicitlyAllowedPorts());
504 TEST(NetUtilTest
, GetHostOrSpecFromURL
) {
505 EXPECT_EQ("example.com",
506 GetHostOrSpecFromURL(GURL("http://example.com/test")));
507 EXPECT_EQ("example.com",
508 GetHostOrSpecFromURL(GURL("http://example.com./test")));
509 EXPECT_EQ("file:///tmp/test.html",
510 GetHostOrSpecFromURL(GURL("file:///tmp/test.html")));
513 TEST(NetUtilTest
, GetAddressFamily
) {
514 IPAddressNumber number
;
515 EXPECT_TRUE(ParseIPLiteralToNumber("192.168.0.1", &number
));
516 EXPECT_EQ(ADDRESS_FAMILY_IPV4
, GetAddressFamily(number
));
517 EXPECT_TRUE(ParseIPLiteralToNumber("1:abcd::3:4:ff", &number
));
518 EXPECT_EQ(ADDRESS_FAMILY_IPV6
, GetAddressFamily(number
));
521 TEST(NetUtilTest
, IsLocalhost
) {
522 EXPECT_TRUE(IsLocalhost("localhost"));
523 EXPECT_TRUE(IsLocalhost("localHosT"));
524 EXPECT_TRUE(IsLocalhost("localhost."));
525 EXPECT_TRUE(IsLocalhost("localHost."));
526 EXPECT_TRUE(IsLocalhost("localhost.localdomain"));
527 EXPECT_TRUE(IsLocalhost("localhost.localDOMain"));
528 EXPECT_TRUE(IsLocalhost("localhost.localdomain."));
529 EXPECT_TRUE(IsLocalhost("localhost6"));
530 EXPECT_TRUE(IsLocalhost("localhost6."));
531 EXPECT_TRUE(IsLocalhost("localhost6.localdomain6"));
532 EXPECT_TRUE(IsLocalhost("localhost6.localdomain6."));
533 EXPECT_TRUE(IsLocalhost("127.0.0.1"));
534 EXPECT_TRUE(IsLocalhost("127.0.1.0"));
535 EXPECT_TRUE(IsLocalhost("127.1.0.0"));
536 EXPECT_TRUE(IsLocalhost("127.0.0.255"));
537 EXPECT_TRUE(IsLocalhost("127.0.255.0"));
538 EXPECT_TRUE(IsLocalhost("127.255.0.0"));
539 EXPECT_TRUE(IsLocalhost("::1"));
540 EXPECT_TRUE(IsLocalhost("0:0:0:0:0:0:0:1"));
541 EXPECT_TRUE(IsLocalhost("foo.localhost"));
542 EXPECT_TRUE(IsLocalhost("foo.localhost."));
544 EXPECT_FALSE(IsLocalhost("localhostx"));
545 EXPECT_FALSE(IsLocalhost("localhost.x"));
546 EXPECT_FALSE(IsLocalhost("foo.localdomain"));
547 EXPECT_FALSE(IsLocalhost("foo.localdomain.x"));
548 EXPECT_FALSE(IsLocalhost("localhost6x"));
549 EXPECT_FALSE(IsLocalhost("localhost.localdomain6"));
550 EXPECT_FALSE(IsLocalhost("localhost6.localdomain"));
551 EXPECT_FALSE(IsLocalhost("127.0.0.1.1"));
552 EXPECT_FALSE(IsLocalhost(".127.0.0.255"));
553 EXPECT_FALSE(IsLocalhost("::2"));
554 EXPECT_FALSE(IsLocalhost("::1:1"));
555 EXPECT_FALSE(IsLocalhost("0:0:0:0:1:0:0:1"));
556 EXPECT_FALSE(IsLocalhost("::1:1"));
557 EXPECT_FALSE(IsLocalhost("0:0:0:0:0:0:0:0:1"));
558 EXPECT_FALSE(IsLocalhost("foo.localhost.com"));
559 EXPECT_FALSE(IsLocalhost("foo.localhoste"));
562 TEST(NetUtilTest
, ResolveLocalHostname
) {
563 AddressList addresses
;
565 TestBothLoopbackIPs("localhost");
566 TestBothLoopbackIPs("localhoST");
567 TestBothLoopbackIPs("localhost.");
568 TestBothLoopbackIPs("localhoST.");
569 TestBothLoopbackIPs("localhost.localdomain");
570 TestBothLoopbackIPs("localhost.localdomAIn");
571 TestBothLoopbackIPs("localhost.localdomain.");
572 TestBothLoopbackIPs("localhost.localdomAIn.");
573 TestBothLoopbackIPs("foo.localhost");
574 TestBothLoopbackIPs("foo.localhOSt");
575 TestBothLoopbackIPs("foo.localhost.");
576 TestBothLoopbackIPs("foo.localhOSt.");
578 TestIPv6LoopbackOnly("localhost6");
579 TestIPv6LoopbackOnly("localhoST6");
580 TestIPv6LoopbackOnly("localhost6.");
581 TestIPv6LoopbackOnly("localhost6.localdomain6");
582 TestIPv6LoopbackOnly("localhost6.localdomain6.");
585 ResolveLocalHostname("127.0.0.1", kLocalhostLookupPort
, &addresses
));
586 EXPECT_FALSE(ResolveLocalHostname("::1", kLocalhostLookupPort
, &addresses
));
587 EXPECT_FALSE(ResolveLocalHostname("0:0:0:0:0:0:0:1", kLocalhostLookupPort
,
590 ResolveLocalHostname("localhostx", kLocalhostLookupPort
, &addresses
));
592 ResolveLocalHostname("localhost.x", kLocalhostLookupPort
, &addresses
));
593 EXPECT_FALSE(ResolveLocalHostname("foo.localdomain", kLocalhostLookupPort
,
595 EXPECT_FALSE(ResolveLocalHostname("foo.localdomain.x", kLocalhostLookupPort
,
598 ResolveLocalHostname("localhost6x", kLocalhostLookupPort
, &addresses
));
599 EXPECT_FALSE(ResolveLocalHostname("localhost.localdomain6",
600 kLocalhostLookupPort
, &addresses
));
601 EXPECT_FALSE(ResolveLocalHostname("localhost6.localdomain",
602 kLocalhostLookupPort
, &addresses
));
604 ResolveLocalHostname("127.0.0.1.1", kLocalhostLookupPort
, &addresses
));
606 ResolveLocalHostname(".127.0.0.255", kLocalhostLookupPort
, &addresses
));
607 EXPECT_FALSE(ResolveLocalHostname("::2", kLocalhostLookupPort
, &addresses
));
608 EXPECT_FALSE(ResolveLocalHostname("::1:1", kLocalhostLookupPort
, &addresses
));
609 EXPECT_FALSE(ResolveLocalHostname("0:0:0:0:1:0:0:1", kLocalhostLookupPort
,
611 EXPECT_FALSE(ResolveLocalHostname("::1:1", kLocalhostLookupPort
, &addresses
));
612 EXPECT_FALSE(ResolveLocalHostname("0:0:0:0:0:0:0:0:1", kLocalhostLookupPort
,
614 EXPECT_FALSE(ResolveLocalHostname("foo.localhost.com", kLocalhostLookupPort
,
617 ResolveLocalHostname("foo.localhoste", kLocalhostLookupPort
, &addresses
));
620 TEST(NetUtilTest
, IsLocalhostTLD
) {
621 EXPECT_TRUE(IsLocalhostTLD("foo.localhost"));
622 EXPECT_TRUE(IsLocalhostTLD("foo.localhoST"));
623 EXPECT_TRUE(IsLocalhostTLD("foo.localhost."));
624 EXPECT_TRUE(IsLocalhostTLD("foo.localhoST."));
625 EXPECT_FALSE(IsLocalhostTLD("foo.localhos"));
626 EXPECT_FALSE(IsLocalhostTLD("foo.localhost.com"));
627 EXPECT_FALSE(IsLocalhost("foo.localhoste"));
630 TEST(NetUtilTest
, GoogleHost
) {
631 struct GoogleHostCase
{
633 bool expected_output
;
636 const GoogleHostCase google_host_cases
[] = {
637 {GURL("http://.google.com"), true},
638 {GURL("http://.youtube.com"), true},
639 {GURL("http://.gmail.com"), true},
640 {GURL("http://.doubleclick.net"), true},
641 {GURL("http://.gstatic.com"), true},
642 {GURL("http://.googlevideo.com"), true},
643 {GURL("http://.googleusercontent.com"), true},
644 {GURL("http://.googlesyndication.com"), true},
645 {GURL("http://.google-analytics.com"), true},
646 {GURL("http://.googleadservices.com"), true},
647 {GURL("http://.googleapis.com"), true},
648 {GURL("http://a.google.com"), true},
649 {GURL("http://b.youtube.com"), true},
650 {GURL("http://c.gmail.com"), true},
651 {GURL("http://google.com"), false},
652 {GURL("http://youtube.com"), false},
653 {GURL("http://gmail.com"), false},
654 {GURL("http://google.coma"), false},
655 {GURL("http://agoogle.com"), false},
656 {GURL("http://oogle.com"), false},
657 {GURL("http://google.co"), false},
658 {GURL("http://oggole.com"), false},
661 for (size_t i
= 0; i
< arraysize(google_host_cases
); ++i
) {
662 EXPECT_EQ(google_host_cases
[i
].expected_output
,
663 HasGoogleHost(google_host_cases
[i
].url
));
667 struct NonUniqueNameTestData
{
669 const char* const hostname
;
672 // Google Test pretty-printer.
673 void PrintTo(const NonUniqueNameTestData
& data
, std::ostream
* os
) {
674 ASSERT_TRUE(data
.hostname
);
675 *os
<< " hostname: " << testing::PrintToString(data
.hostname
)
676 << "; is_unique: " << testing::PrintToString(data
.is_unique
);
679 const NonUniqueNameTestData kNonUniqueNameTestData
[] = {
680 // Domains under ICANN-assigned domains.
681 { true, "google.com" },
682 { true, "google.co.uk" },
683 // Domains under private registries.
684 { true, "appspot.com" },
685 { true, "test.appspot.com" },
686 // Unreserved IPv4 addresses (in various forms).
688 { true, "99.64.0.0" },
689 { true, "212.15.0.0" },
691 { true, "212.15.0" },
692 { true, "3557752832" },
693 // Reserved IPv4 addresses (in various forms).
694 { false, "192.168.0.0" },
695 { false, "192.168.0.6" },
696 { false, "10.0.0.5" },
699 { false, "3232235526" },
700 // Unreserved IPv6 addresses.
701 { true, "FFC0:ba98:7654:3210:FEDC:BA98:7654:3210" },
702 { true, "2000:ba98:7654:2301:EFCD:BA98:7654:3210" },
703 // Reserved IPv6 addresses.
704 { false, "::192.9.5.5" },
705 { false, "FEED::BEEF" },
706 { false, "FEC0:ba98:7654:3210:FEDC:BA98:7654:3210" },
707 // 'internal'/non-IANA assigned domains.
708 { false, "intranet" },
709 { false, "intranet." },
710 { false, "intranet.example" },
711 { false, "host.intranet.example" },
712 // gTLDs under discussion, but not yet assigned.
713 { false, "intranet.corp" },
714 { false, "intranet.internal" },
715 // Invalid host names are treated as unique - but expected to be
716 // filtered out before then.
717 { true, "junk)(£)$*!@~#" },
718 { true, "w$w.example.com" },
719 { true, "nocolonsallowed:example" },
720 { true, "[::4.5.6.9]" },
723 class NetUtilNonUniqueNameTest
724 : public testing::TestWithParam
<NonUniqueNameTestData
> {
726 virtual ~NetUtilNonUniqueNameTest() {}
729 bool IsUnique(const std::string
& hostname
) {
730 return !IsHostnameNonUnique(hostname
);
734 // Test that internal/non-unique names are properly identified as such, but
735 // that IP addresses and hosts beneath registry-controlled domains are flagged
737 TEST_P(NetUtilNonUniqueNameTest
, IsHostnameNonUnique
) {
738 const NonUniqueNameTestData
& test_data
= GetParam();
740 EXPECT_EQ(test_data
.is_unique
, IsUnique(test_data
.hostname
));
743 INSTANTIATE_TEST_CASE_P(, NetUtilNonUniqueNameTest
,
744 testing::ValuesIn(kNonUniqueNameTestData
));