Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(427)

Side by Side Diff: net/base/net_util_unittest.cc

Issue 885443002: Roll Chrome into Mojo. (Closed) Base URL: https://github.com/domokit/mojo.git@master
Patch Set: Rebase to ToT mojo Created 5 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « net/base/net_util_icu_unittest.cc ('k') | net/base/network_change_notifier.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "net/base/net_util.h" 5 #include "net/base/net_util.h"
6 6
7 #include <string.h> 7 #include <string.h>
8 8
9 #include <ostream> 9 #include <ostream>
10 10
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
54 #endif // OS_WIN 54 #endif // OS_WIN
55 55
56 using base::ASCIIToUTF16; 56 using base::ASCIIToUTF16;
57 using base::WideToUTF16; 57 using base::WideToUTF16;
58 58
59 namespace net { 59 namespace net {
60 60
61 namespace { 61 namespace {
62 62
63 struct HeaderCase { 63 struct HeaderCase {
64 const char* header_name; 64 const char* const header_name;
65 const char* expected; 65 const char* const expected;
66 }; 66 };
67 67
68 // Fills in sockaddr for the given 32-bit address (IPv4.) 68 // Fills in sockaddr for the given 32-bit address (IPv4.)
69 // |bytes| should be an array of length 4. 69 // |bytes| should be an array of length 4.
70 void MakeIPv4Address(const uint8* bytes, int port, SockaddrStorage* storage) { 70 void MakeIPv4Address(const uint8* bytes, int port, SockaddrStorage* storage) {
71 memset(&storage->addr_storage, 0, sizeof(storage->addr_storage)); 71 memset(&storage->addr_storage, 0, sizeof(storage->addr_storage));
72 storage->addr_len = sizeof(struct sockaddr_in); 72 storage->addr_len = sizeof(struct sockaddr_in);
73 struct sockaddr_in* addr4 = reinterpret_cast<sockaddr_in*>(storage->addr); 73 struct sockaddr_in* addr4 = reinterpret_cast<sockaddr_in*>(storage->addr);
74 addr4->sin_port = base::HostToNet16(port); 74 addr4->sin_port = base::HostToNet16(port);
75 addr4->sin_family = AF_INET; 75 addr4->sin_family = AF_INET;
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after
147 interfaces->ifa_netmask = reinterpret_cast<sockaddr*>(&sock_addrs[1]); 147 interfaces->ifa_netmask = reinterpret_cast<sockaddr*>(&sock_addrs[1]);
148 148
149 return true; 149 return true;
150 } 150 }
151 #endif // OS_MACOSX 151 #endif // OS_MACOSX
152 152
153 } // anonymous namespace 153 } // anonymous namespace
154 154
155 TEST(NetUtilTest, GetIdentityFromURL) { 155 TEST(NetUtilTest, GetIdentityFromURL) {
156 struct { 156 struct {
157 const char* input_url; 157 const char* const input_url;
158 const char* expected_username; 158 const char* const expected_username;
159 const char* expected_password; 159 const char* const expected_password;
160 } tests[] = { 160 } tests[] = {
161 { 161 {
162 "http://username:password@google.com", 162 "http://username:password@google.com",
163 "username", 163 "username",
164 "password", 164 "password",
165 }, 165 },
166 { // Test for http://crbug.com/19200 166 { // Test for http://crbug.com/19200
167 "http://username:p@ssword@google.com", 167 "http://username:p@ssword@google.com",
168 "username", 168 "username",
169 "p@ssword", 169 "p@ssword",
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
217 // Extract the unescaped identity. 217 // Extract the unescaped identity.
218 base::string16 username, password; 218 base::string16 username, password;
219 GetIdentityFromURL(url, &username, &password); 219 GetIdentityFromURL(url, &username, &password);
220 220
221 // Verify that it was decoded as UTF8. 221 // Verify that it was decoded as UTF8.
222 EXPECT_EQ(ASCIIToUTF16("foo"), username); 222 EXPECT_EQ(ASCIIToUTF16("foo"), username);
223 EXPECT_EQ(WideToUTF16(L"\x4f60\x597d"), password); 223 EXPECT_EQ(WideToUTF16(L"\x4f60\x597d"), password);
224 } 224 }
225 225
226 // Just a bunch of fake headers. 226 // Just a bunch of fake headers.
227 const char* google_headers = 227 const char google_headers[] =
228 "HTTP/1.1 200 OK\n" 228 "HTTP/1.1 200 OK\n"
229 "Content-TYPE: text/html; charset=utf-8\n" 229 "Content-TYPE: text/html; charset=utf-8\n"
230 "Content-disposition: attachment; filename=\"download.pdf\"\n" 230 "Content-disposition: attachment; filename=\"download.pdf\"\n"
231 "Content-Length: 378557\n" 231 "Content-Length: 378557\n"
232 "X-Google-Google1: 314159265\n" 232 "X-Google-Google1: 314159265\n"
233 "X-Google-Google2: aaaa2:7783,bbb21:9441\n" 233 "X-Google-Google2: aaaa2:7783,bbb21:9441\n"
234 "X-Google-Google4: home\n" 234 "X-Google-Google4: home\n"
235 "Transfer-Encoding: chunked\n" 235 "Transfer-Encoding: chunked\n"
236 "Set-Cookie: HEHE_AT=6666x66beef666x6-66xx6666x66; Path=/mail\n" 236 "Set-Cookie: HEHE_AT=6666x66beef666x6-66xx6666x66; Path=/mail\n"
237 "Set-Cookie: HEHE_HELP=owned:0;Path=/\n" 237 "Set-Cookie: HEHE_HELP=owned:0;Path=/\n"
(...skipping 26 matching lines...) Expand all
264 264
265 // Test again with empty headers. 265 // Test again with empty headers.
266 for (size_t i = 0; i < arraysize(tests); ++i) { 266 for (size_t i = 0; i < arraysize(tests); ++i) {
267 std::string result = GetSpecificHeader(std::string(), tests[i].header_name); 267 std::string result = GetSpecificHeader(std::string(), tests[i].header_name);
268 EXPECT_EQ(result, std::string()); 268 EXPECT_EQ(result, std::string());
269 } 269 }
270 } 270 }
271 271
272 TEST(NetUtilTest, CompliantHost) { 272 TEST(NetUtilTest, CompliantHost) {
273 struct CompliantHostCase { 273 struct CompliantHostCase {
274 const char* host; 274 const char* const host;
275 bool expected_output; 275 bool expected_output;
276 }; 276 };
277 277
278 const CompliantHostCase compliant_host_cases[] = { 278 const CompliantHostCase compliant_host_cases[] = {
279 {"", false}, 279 {"", false},
280 {"a", true}, 280 {"a", true},
281 {"-", false}, 281 {"-", false},
282 {".", false}, 282 {".", false},
283 {"9", true}, 283 {"9", true},
284 {"9a", true}, 284 {"9a", true},
(...skipping 16 matching lines...) Expand all
301 }; 301 };
302 302
303 for (size_t i = 0; i < arraysize(compliant_host_cases); ++i) { 303 for (size_t i = 0; i < arraysize(compliant_host_cases); ++i) {
304 EXPECT_EQ(compliant_host_cases[i].expected_output, 304 EXPECT_EQ(compliant_host_cases[i].expected_output,
305 IsCanonicalizedHostCompliant(compliant_host_cases[i].host)); 305 IsCanonicalizedHostCompliant(compliant_host_cases[i].host));
306 } 306 }
307 } 307 }
308 308
309 TEST(NetUtilTest, ParseHostAndPort) { 309 TEST(NetUtilTest, ParseHostAndPort) {
310 const struct { 310 const struct {
311 const char* input; 311 const char* const input;
312 bool success; 312 bool success;
313 const char* expected_host; 313 const char* const expected_host;
314 int expected_port; 314 int expected_port;
315 } tests[] = { 315 } tests[] = {
316 // Valid inputs: 316 // Valid inputs:
317 {"foo:10", true, "foo", 10}, 317 {"foo:10", true, "foo", 10},
318 {"foo", true, "foo", -1}, 318 {"foo", true, "foo", -1},
319 { 319 {
320 "[1080:0:0:0:8:800:200C:4171]:11", 320 "[1080:0:0:0:8:800:200C:4171]:11",
321 true, 321 true,
322 "1080:0:0:0:8:800:200C:4171", 322 "1080:0:0:0:8:800:200C:4171",
323 11 323 11
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
360 if (tests[i].success) { 360 if (tests[i].success) {
361 EXPECT_EQ(tests[i].expected_host, host); 361 EXPECT_EQ(tests[i].expected_host, host);
362 EXPECT_EQ(tests[i].expected_port, port); 362 EXPECT_EQ(tests[i].expected_port, port);
363 } 363 }
364 } 364 }
365 } 365 }
366 366
367 TEST(NetUtilTest, GetHostAndPort) { 367 TEST(NetUtilTest, GetHostAndPort) {
368 const struct { 368 const struct {
369 GURL url; 369 GURL url;
370 const char* expected_host_and_port; 370 const char* const expected_host_and_port;
371 } tests[] = { 371 } tests[] = {
372 { GURL("http://www.foo.com/x"), "www.foo.com:80"}, 372 { GURL("http://www.foo.com/x"), "www.foo.com:80"},
373 { GURL("http://www.foo.com:21/x"), "www.foo.com:21"}, 373 { GURL("http://www.foo.com:21/x"), "www.foo.com:21"},
374 374
375 // For IPv6 literals should always include the brackets. 375 // For IPv6 literals should always include the brackets.
376 { GURL("http://[1::2]/x"), "[1::2]:80"}, 376 { GURL("http://[1::2]/x"), "[1::2]:80"},
377 { GURL("http://[::a]:33/x"), "[::a]:33"}, 377 { GURL("http://[::a]:33/x"), "[::a]:33"},
378 }; 378 };
379 for (size_t i = 0; i < arraysize(tests); ++i) { 379 for (size_t i = 0; i < arraysize(tests); ++i) {
380 std::string host_and_port = GetHostAndPort(tests[i].url); 380 std::string host_and_port = GetHostAndPort(tests[i].url);
381 EXPECT_EQ(std::string(tests[i].expected_host_and_port), host_and_port); 381 EXPECT_EQ(std::string(tests[i].expected_host_and_port), host_and_port);
382 } 382 }
383 } 383 }
384 384
385 TEST(NetUtilTest, GetHostAndOptionalPort) { 385 TEST(NetUtilTest, GetHostAndOptionalPort) {
386 const struct { 386 const struct {
387 GURL url; 387 GURL url;
388 const char* expected_host_and_port; 388 const char* const expected_host_and_port;
389 } tests[] = { 389 } tests[] = {
390 { GURL("http://www.foo.com/x"), "www.foo.com"}, 390 { GURL("http://www.foo.com/x"), "www.foo.com"},
391 { GURL("http://www.foo.com:21/x"), "www.foo.com:21"}, 391 { GURL("http://www.foo.com:21/x"), "www.foo.com:21"},
392 392
393 // For IPv6 literals should always include the brackets. 393 // For IPv6 literals should always include the brackets.
394 { GURL("http://[1::2]/x"), "[1::2]"}, 394 { GURL("http://[1::2]/x"), "[1::2]"},
395 { GURL("http://[::a]:33/x"), "[::a]:33"}, 395 { GURL("http://[::a]:33/x"), "[::a]:33"},
396 }; 396 };
397 for (size_t i = 0; i < arraysize(tests); ++i) { 397 for (size_t i = 0; i < arraysize(tests); ++i) {
398 std::string host_and_port = GetHostAndOptionalPort(tests[i].url); 398 std::string host_and_port = GetHostAndOptionalPort(tests[i].url);
(...skipping 21 matching lines...) Expand all
420 IPAddressToStringWithPort(addr2, sizeof(addr2), 99)); 420 IPAddressToStringWithPort(addr2, sizeof(addr2), 99));
421 421
422 uint8 addr3[16] = {0xFE, 0xDC, 0xBA, 0x98}; 422 uint8 addr3[16] = {0xFE, 0xDC, 0xBA, 0x98};
423 EXPECT_EQ("[fedc:ba98::]:8080", 423 EXPECT_EQ("[fedc:ba98::]:8080",
424 IPAddressToStringWithPort(addr3, sizeof(addr3), 8080)); 424 IPAddressToStringWithPort(addr3, sizeof(addr3), 8080));
425 } 425 }
426 426
427 TEST(NetUtilTest, NetAddressToString_IPv4) { 427 TEST(NetUtilTest, NetAddressToString_IPv4) {
428 const struct { 428 const struct {
429 uint8 addr[4]; 429 uint8 addr[4];
430 const char* result; 430 const char* const result;
431 } tests[] = { 431 } tests[] = {
432 {{0, 0, 0, 0}, "0.0.0.0"}, 432 {{0, 0, 0, 0}, "0.0.0.0"},
433 {{127, 0, 0, 1}, "127.0.0.1"}, 433 {{127, 0, 0, 1}, "127.0.0.1"},
434 {{192, 168, 0, 1}, "192.168.0.1"}, 434 {{192, 168, 0, 1}, "192.168.0.1"},
435 }; 435 };
436 436
437 for (size_t i = 0; i < arraysize(tests); ++i) { 437 for (size_t i = 0; i < arraysize(tests); ++i) {
438 SockaddrStorage storage; 438 SockaddrStorage storage;
439 MakeIPv4Address(tests[i].addr, 80, &storage); 439 MakeIPv4Address(tests[i].addr, 80, &storage);
440 std::string result = NetAddressToString(storage.addr, storage.addr_len); 440 std::string result = NetAddressToString(storage.addr, storage.addr_len);
441 EXPECT_EQ(std::string(tests[i].result), result); 441 EXPECT_EQ(std::string(tests[i].result), result);
442 } 442 }
443 } 443 }
444 444
445 TEST(NetUtilTest, NetAddressToString_IPv6) { 445 TEST(NetUtilTest, NetAddressToString_IPv6) {
446 const struct { 446 const struct {
447 uint8 addr[16]; 447 uint8 addr[16];
448 const char* result; 448 const char* const result;
449 } tests[] = { 449 } tests[] = {
450 {{0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10, 0xFE, 0xDC, 0xBA, 450 {{0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10, 0xFE, 0xDC, 0xBA,
451 0x98, 0x76, 0x54, 0x32, 0x10}, 451 0x98, 0x76, 0x54, 0x32, 0x10},
452 "fedc:ba98:7654:3210:fedc:ba98:7654:3210"}, 452 "fedc:ba98:7654:3210:fedc:ba98:7654:3210"},
453 }; 453 };
454 454
455 for (size_t i = 0; i < arraysize(tests); ++i) { 455 for (size_t i = 0; i < arraysize(tests); ++i) {
456 SockaddrStorage storage; 456 SockaddrStorage storage;
457 MakeIPv6Address(tests[i].addr, 80, &storage); 457 MakeIPv6Address(tests[i].addr, 80, &storage);
458 EXPECT_EQ(std::string(tests[i].result), 458 EXPECT_EQ(std::string(tests[i].result),
(...skipping 28 matching lines...) Expand all
487 TEST(NetUtilTest, GetHostName) { 487 TEST(NetUtilTest, GetHostName) {
488 // We can't check the result of GetHostName() directly, since the result 488 // We can't check the result of GetHostName() directly, since the result
489 // will differ across machines. Our goal here is to simply exercise the 489 // will differ across machines. Our goal here is to simply exercise the
490 // code path, and check that things "look about right". 490 // code path, and check that things "look about right".
491 std::string hostname = GetHostName(); 491 std::string hostname = GetHostName();
492 EXPECT_FALSE(hostname.empty()); 492 EXPECT_FALSE(hostname.empty());
493 } 493 }
494 494
495 TEST(NetUtilTest, SimplifyUrlForRequest) { 495 TEST(NetUtilTest, SimplifyUrlForRequest) {
496 struct { 496 struct {
497 const char* input_url; 497 const char* const input_url;
498 const char* expected_simplified_url; 498 const char* const expected_simplified_url;
499 } tests[] = { 499 } tests[] = {
500 { 500 {
501 // Reference section should be stripped. 501 // Reference section should be stripped.
502 "http://www.google.com:78/foobar?query=1#hash", 502 "http://www.google.com:78/foobar?query=1#hash",
503 "http://www.google.com:78/foobar?query=1", 503 "http://www.google.com:78/foobar?query=1",
504 }, 504 },
505 { 505 {
506 // Reference section can itself contain #. 506 // Reference section can itself contain #.
507 "http://192.168.0.1?query=1#hash#10#11#13#14", 507 "http://192.168.0.1?query=1#hash#10#11#13#14",
508 "http://192.168.0.1?query=1", 508 "http://192.168.0.1?query=1",
(...skipping 145 matching lines...) Expand 10 before | Expand all | Expand 10 after
654 IPAddressNumber ipv4mapped_number; 654 IPAddressNumber ipv4mapped_number;
655 EXPECT_TRUE(ParseIPLiteralToNumber("::ffff:0101:1", &ipv4mapped_number)); 655 EXPECT_TRUE(ParseIPLiteralToNumber("::ffff:0101:1", &ipv4mapped_number));
656 IPAddressNumber expected; 656 IPAddressNumber expected;
657 EXPECT_TRUE(ParseIPLiteralToNumber("1.1.0.1", &expected)); 657 EXPECT_TRUE(ParseIPLiteralToNumber("1.1.0.1", &expected));
658 IPAddressNumber result = ConvertIPv4MappedToIPv4(ipv4mapped_number); 658 IPAddressNumber result = ConvertIPv4MappedToIPv4(ipv4mapped_number);
659 EXPECT_EQ(expected, result); 659 EXPECT_EQ(expected, result);
660 } 660 }
661 661
662 // Test parsing invalid CIDR notation literals. 662 // Test parsing invalid CIDR notation literals.
663 TEST(NetUtilTest, ParseCIDRBlock_Invalid) { 663 TEST(NetUtilTest, ParseCIDRBlock_Invalid) {
664 const char* bad_literals[] = { 664 const char* const bad_literals[] = {
665 "foobar", 665 "foobar",
666 "", 666 "",
667 "192.168.0.1", 667 "192.168.0.1",
668 "::1", 668 "::1",
669 "/", 669 "/",
670 "/1", 670 "/1",
671 "1", 671 "1",
672 "192.168.1.1/-1", 672 "192.168.1.1/-1",
673 "192.168.1.1/33", 673 "192.168.1.1/33",
674 "::1/-3", 674 "::1/-3",
(...skipping 20 matching lines...) Expand all
695 EXPECT_TRUE(ParseCIDRBlock("192.168.0.1/11", 695 EXPECT_TRUE(ParseCIDRBlock("192.168.0.1/11",
696 &ip_number, 696 &ip_number,
697 &prefix_length_in_bits)); 697 &prefix_length_in_bits));
698 698
699 EXPECT_EQ("192,168,0,1", DumpIPNumber(ip_number)); 699 EXPECT_EQ("192,168,0,1", DumpIPNumber(ip_number));
700 EXPECT_EQ(11u, prefix_length_in_bits); 700 EXPECT_EQ(11u, prefix_length_in_bits);
701 } 701 }
702 702
703 TEST(NetUtilTest, IPNumberMatchesPrefix) { 703 TEST(NetUtilTest, IPNumberMatchesPrefix) {
704 struct { 704 struct {
705 const char* cidr_literal; 705 const char* const cidr_literal;
706 const char* ip_literal; 706 const char* const ip_literal;
707 bool expected_to_match; 707 bool expected_to_match;
708 } tests[] = { 708 } tests[] = {
709 // IPv4 prefix with IPv4 inputs. 709 // IPv4 prefix with IPv4 inputs.
710 { 710 {
711 "10.10.1.32/27", 711 "10.10.1.32/27",
712 "10.10.1.44", 712 "10.10.1.44",
713 true 713 true
714 }, 714 },
715 { 715 {
716 "10.10.1.32/27", 716 "10.10.1.32/27",
(...skipping 693 matching lines...) Expand 10 before | Expand all | Expand 10 after
1410 TEST(NetUtilTest, SetWifiOptionsTest) { 1410 TEST(NetUtilTest, SetWifiOptionsTest) {
1411 TryChangeWifiOptions(0); 1411 TryChangeWifiOptions(0);
1412 TryChangeWifiOptions(WIFI_OPTIONS_DISABLE_SCAN); 1412 TryChangeWifiOptions(WIFI_OPTIONS_DISABLE_SCAN);
1413 TryChangeWifiOptions(WIFI_OPTIONS_MEDIA_STREAMING_MODE); 1413 TryChangeWifiOptions(WIFI_OPTIONS_MEDIA_STREAMING_MODE);
1414 TryChangeWifiOptions(WIFI_OPTIONS_DISABLE_SCAN | 1414 TryChangeWifiOptions(WIFI_OPTIONS_DISABLE_SCAN |
1415 WIFI_OPTIONS_MEDIA_STREAMING_MODE); 1415 WIFI_OPTIONS_MEDIA_STREAMING_MODE);
1416 } 1416 }
1417 1417
1418 struct NonUniqueNameTestData { 1418 struct NonUniqueNameTestData {
1419 bool is_unique; 1419 bool is_unique;
1420 const char* hostname; 1420 const char* const hostname;
1421 }; 1421 };
1422 1422
1423 // Google Test pretty-printer. 1423 // Google Test pretty-printer.
1424 void PrintTo(const NonUniqueNameTestData& data, std::ostream* os) { 1424 void PrintTo(const NonUniqueNameTestData& data, std::ostream* os) {
1425 ASSERT_TRUE(data.hostname); 1425 ASSERT_TRUE(data.hostname);
1426 *os << " hostname: " << testing::PrintToString(data.hostname) 1426 *os << " hostname: " << testing::PrintToString(data.hostname)
1427 << "; is_unique: " << testing::PrintToString(data.is_unique); 1427 << "; is_unique: " << testing::PrintToString(data.is_unique);
1428 } 1428 }
1429 1429
1430 const NonUniqueNameTestData kNonUniqueNameTestData[] = { 1430 const NonUniqueNameTestData kNonUniqueNameTestData[] = {
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after
1489 TEST_P(NetUtilNonUniqueNameTest, IsHostnameNonUnique) { 1489 TEST_P(NetUtilNonUniqueNameTest, IsHostnameNonUnique) {
1490 const NonUniqueNameTestData& test_data = GetParam(); 1490 const NonUniqueNameTestData& test_data = GetParam();
1491 1491
1492 EXPECT_EQ(test_data.is_unique, IsUnique(test_data.hostname)); 1492 EXPECT_EQ(test_data.is_unique, IsUnique(test_data.hostname));
1493 } 1493 }
1494 1494
1495 INSTANTIATE_TEST_CASE_P(, NetUtilNonUniqueNameTest, 1495 INSTANTIATE_TEST_CASE_P(, NetUtilNonUniqueNameTest,
1496 testing::ValuesIn(kNonUniqueNameTestData)); 1496 testing::ValuesIn(kNonUniqueNameTestData));
1497 1497
1498 } // namespace net 1498 } // namespace net
OLDNEW
« no previous file with comments | « net/base/net_util_icu_unittest.cc ('k') | net/base/network_change_notifier.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698