OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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 "base/file_path.h" | 7 #include "base/file_path.h" |
8 #include "base/format_macros.h" | 8 #include "base/format_macros.h" |
9 #include "base/string_util.h" | 9 #include "base/string_util.h" |
10 #include "base/sys_string_conversions.h" | 10 #include "base/sys_string_conversions.h" |
(...skipping 557 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
568 EXPECT_EQ(std::wstring(invalid_wide), output); | 568 EXPECT_EQ(std::wstring(invalid_wide), output); |
569 #endif | 569 #endif |
570 | 570 |
571 // Test that if a file URL is malformed, we get a failure | 571 // Test that if a file URL is malformed, we get a failure |
572 EXPECT_FALSE(net::FileURLToFilePath(GURL("filefoobar"), &output)); | 572 EXPECT_FALSE(net::FileURLToFilePath(GURL("filefoobar"), &output)); |
573 } | 573 } |
574 | 574 |
575 TEST(NetUtilTest, GetIdentityFromURL) { | 575 TEST(NetUtilTest, GetIdentityFromURL) { |
576 struct { | 576 struct { |
577 const char* input_url; | 577 const char* input_url; |
578 const wchar_t* expected_username; | 578 const char* expected_username; |
579 const wchar_t* expected_password; | 579 const char* expected_password; |
580 } tests[] = { | 580 } tests[] = { |
581 { | 581 { |
582 "http://username:password@google.com", | 582 "http://username:password@google.com", |
583 L"username", | 583 "username", |
584 L"password", | 584 "password", |
585 }, | 585 }, |
586 { // Test for http://crbug.com/19200 | 586 { // Test for http://crbug.com/19200 |
587 "http://username:p@ssword@google.com", | 587 "http://username:p@ssword@google.com", |
588 L"username", | 588 "username", |
589 L"p@ssword", | 589 "p@ssword", |
590 }, | 590 }, |
591 { // Special URL characters should be unescaped. | 591 { // Special URL characters should be unescaped. |
592 "http://username:p%3fa%26s%2fs%23@google.com", | 592 "http://username:p%3fa%26s%2fs%23@google.com", |
593 L"username", | 593 "username", |
594 L"p?a&s/s#", | 594 "p?a&s/s#", |
595 }, | 595 }, |
596 { // Username contains %20. | 596 { // Username contains %20. |
597 "http://use rname:password@google.com", | 597 "http://use rname:password@google.com", |
598 L"use rname", | 598 "use rname", |
599 L"password", | 599 "password", |
600 }, | 600 }, |
601 { // Keep %00 as is. | 601 { // Keep %00 as is. |
602 "http://use%00rname:password@google.com", | 602 "http://use%00rname:password@google.com", |
603 L"use%00rname", | 603 "use%00rname", |
604 L"password", | 604 "password", |
605 }, | 605 }, |
606 { // Use a '+' in the username. | 606 { // Use a '+' in the username. |
607 "http://use+rname:password@google.com", | 607 "http://use+rname:password@google.com", |
608 L"use+rname", | 608 "use+rname", |
609 L"password", | 609 "password", |
610 }, | 610 }, |
611 { // Use a '&' in the password. | 611 { // Use a '&' in the password. |
612 "http://username:p&ssword@google.com", | 612 "http://username:p&ssword@google.com", |
613 L"username", | 613 "username", |
614 L"p&ssword", | 614 "p&ssword", |
615 }, | 615 }, |
616 }; | 616 }; |
617 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) { | 617 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) { |
618 SCOPED_TRACE(StringPrintf("Test[%" PRIuS "]: %s", i, tests[i].input_url)); | 618 SCOPED_TRACE(StringPrintf("Test[%" PRIuS "]: %s", i, tests[i].input_url)); |
619 GURL url(tests[i].input_url); | 619 GURL url(tests[i].input_url); |
620 | 620 |
621 std::wstring username, password; | 621 string16 username, password; |
622 net::GetIdentityFromURL(url, &username, &password); | 622 net::GetIdentityFromURL(url, &username, &password); |
623 | 623 |
624 EXPECT_EQ(tests[i].expected_username, username); | 624 EXPECT_EQ(ASCIIToUTF16(tests[i].expected_username), username); |
625 EXPECT_EQ(tests[i].expected_password, password); | 625 EXPECT_EQ(ASCIIToUTF16(tests[i].expected_password), password); |
626 } | 626 } |
627 } | 627 } |
628 | 628 |
629 // Try extracting a username which was encoded with UTF8. | 629 // Try extracting a username which was encoded with UTF8. |
630 TEST(NetUtilTest, GetIdentityFromURL_UTF8) { | 630 TEST(NetUtilTest, GetIdentityFromURL_UTF8) { |
631 GURL url(WideToUTF16(L"http://foo:\x4f60\x597d@blah.com")); | 631 GURL url(WideToUTF16(L"http://foo:\x4f60\x597d@blah.com")); |
632 | 632 |
633 EXPECT_EQ("foo", url.username()); | 633 EXPECT_EQ("foo", url.username()); |
634 EXPECT_EQ("%E4%BD%A0%E5%A5%BD", url.password()); | 634 EXPECT_EQ("%E4%BD%A0%E5%A5%BD", url.password()); |
635 | 635 |
636 // Extract the unescaped identity. | 636 // Extract the unescaped identity. |
637 std::wstring username, password; | 637 string16 username, password; |
638 net::GetIdentityFromURL(url, &username, &password); | 638 net::GetIdentityFromURL(url, &username, &password); |
639 | 639 |
640 // Verify that it was decoded as UTF8. | 640 // Verify that it was decoded as UTF8. |
641 EXPECT_EQ(L"foo", username); | 641 EXPECT_EQ(ASCIIToUTF16("foo"), username); |
642 EXPECT_EQ(L"\x4f60\x597d", password); | 642 EXPECT_EQ(WideToUTF16(L"\x4f60\x597d"), password); |
643 } | 643 } |
644 | 644 |
645 // Just a bunch of fake headers. | 645 // Just a bunch of fake headers. |
646 const wchar_t* google_headers = | 646 const wchar_t* google_headers = |
647 L"HTTP/1.1 200 OK\n" | 647 L"HTTP/1.1 200 OK\n" |
648 L"Content-TYPE: text/html; charset=utf-8\n" | 648 L"Content-TYPE: text/html; charset=utf-8\n" |
649 L"Content-disposition: attachment; filename=\"download.pdf\"\n" | 649 L"Content-disposition: attachment; filename=\"download.pdf\"\n" |
650 L"Content-Length: 378557\n" | 650 L"Content-Length: 378557\n" |
651 L"X-Google-Google1: 314159265\n" | 651 L"X-Google-Google1: 314159265\n" |
652 L"X-Google-Google2: aaaa2:7783,bbb21:9441\n" | 652 L"X-Google-Google2: aaaa2:7783,bbb21:9441\n" |
(...skipping 1368 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2021 EXPECT_TRUE(net::ParseCIDRBlock(tests[i].cidr_literal, | 2021 EXPECT_TRUE(net::ParseCIDRBlock(tests[i].cidr_literal, |
2022 &ip_prefix, | 2022 &ip_prefix, |
2023 &prefix_length_in_bits)); | 2023 &prefix_length_in_bits)); |
2024 | 2024 |
2025 EXPECT_EQ(tests[i].expected_to_match, | 2025 EXPECT_EQ(tests[i].expected_to_match, |
2026 net::IPNumberMatchesPrefix(ip_number, | 2026 net::IPNumberMatchesPrefix(ip_number, |
2027 ip_prefix, | 2027 ip_prefix, |
2028 prefix_length_in_bits)); | 2028 prefix_length_in_bits)); |
2029 } | 2029 } |
2030 } | 2030 } |
OLD | NEW |