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

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

Issue 6898026: Eliminate wstring from base/utf_offset_string_conversions.h, net/base/escape.h, and net/base/net_... (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: '' Created 9 years, 8 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 | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 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 <algorithm> 7 #include <algorithm>
8 8
9 #include "base/file_path.h" 9 #include "base/file_path.h"
10 #include "base/format_macros.h" 10 #include "base/format_macros.h"
(...skipping 13 matching lines...) Expand all
24 namespace { 24 namespace {
25 25
26 static const size_t kNpos = string16::npos; 26 static const size_t kNpos = string16::npos;
27 27
28 struct FileCase { 28 struct FileCase {
29 const wchar_t* file; 29 const wchar_t* file;
30 const char* url; 30 const char* url;
31 }; 31 };
32 32
33 struct HeaderCase { 33 struct HeaderCase {
34 const wchar_t* header_name; 34 const char* header_name;
35 const wchar_t* expected; 35 const char* expected;
36 }; 36 };
37 37
38 struct HeaderParamCase { 38 struct HeaderParamCase {
39 const wchar_t* header_name; 39 const char* header_name;
40 const wchar_t* param_name; 40 const char* param_name;
41 const wchar_t* expected; 41 const char* expected;
42 }; 42 };
43 43
44 struct FileNameCDCase { 44 struct FileNameCDCase {
45 const char* header_field; 45 const char* header_field;
46 const char* referrer_charset; 46 const char* referrer_charset;
47 const wchar_t* expected; 47 const wchar_t* expected;
48 }; 48 };
49 49
50 const wchar_t* kLanguages[] = { 50 const char* kLanguages[] = {
51 L"", L"en", L"zh-CN", L"ja", L"ko", 51 "", "en", "zh-CN", "ja", "ko",
52 L"he", L"ar", L"ru", L"el", L"fr", 52 "he", "ar", "ru", "el", "fr",
53 L"de", L"pt", L"sv", L"th", L"hi", 53 "de", "pt", "sv", "th", "hi",
54 L"de,en", L"el,en", L"zh-TW,en", L"ko,ja", L"he,ru,en", 54 "de,en", "el,en", "zh-TW,en", "ko,ja", "he,ru,en",
55 L"zh,ru,en" 55 "zh,ru,en"
56 }; 56 };
57 57
58 struct IDNTestCase { 58 struct IDNTestCase {
59 const char* input; 59 const char* input;
60 const wchar_t* unicode_output; 60 const wchar_t* unicode_output;
61 const bool unicode_allowed[arraysize(kLanguages)]; 61 const bool unicode_allowed[arraysize(kLanguages)];
62 }; 62 };
63 63
64 // TODO(jungshik) This is just a random sample of languages and is far 64 // TODO(jungshik) This is just a random sample of languages and is far
65 // from exhaustive. We may have to generate all the combinations 65 // from exhaustive. We may have to generate all the combinations
(...skipping 378 matching lines...) Expand 10 before | Expand all | Expand 10 after
444 addr6->sin6_family = ai->ai_family; 444 addr6->sin6_family = ai->ai_family;
445 memcpy(&addr6->sin6_addr, bytes, 16); 445 memcpy(&addr6->sin6_addr, bytes, 16);
446 446
447 ai->ai_addr = (sockaddr*)addr6; 447 ai->ai_addr = (sockaddr*)addr6;
448 return ai; 448 return ai;
449 } 449 }
450 450
451 // A helper for IDN*{Fast,Slow}. 451 // A helper for IDN*{Fast,Slow}.
452 // Append "::<language list>" to |expected| and |actual| to make it 452 // Append "::<language list>" to |expected| and |actual| to make it
453 // easy to tell which sub-case fails without debugging. 453 // easy to tell which sub-case fails without debugging.
454 void AppendLanguagesToOutputs(const wchar_t* languages, 454 void AppendLanguagesToOutputs(const char* languages,
455 std::wstring* expected, 455 string16* expected,
456 std::wstring* actual) { 456 string16* actual) {
457 expected->append(L"::"); 457 string16 to_append = ASCIIToUTF16("::") + ASCIIToUTF16(languages);
458 expected->append(languages); 458 expected->append(to_append);
459 actual->append(L"::"); 459 actual->append(to_append);
460 actual->append(languages); 460 }
461
462 // A pair of helpers for the FormatUrlWithOffsets() test.
463 void VerboseExpect(size_t expected,
464 size_t actual,
465 const std::string& original_url,
466 size_t position,
467 const string16& formatted_url) {
468 EXPECT_EQ(expected, actual) << "Original URL: " << original_url
469 << " (at char " << position << ")\nFormatted URL: " << formatted_url;
470 }
471
472 void CheckAdjustedOffsets(const std::string& url_string,
473 const std::string& languages,
474 FormatUrlTypes format_types,
475 UnescapeRule::Type unescape_rules,
476 const AdjustOffsetCase* cases,
477 size_t num_cases,
478 const size_t* all_offsets) {
479 GURL url(url_string);
480 for (size_t i = 0; i < num_cases; ++i) {
481 size_t offset = cases[i].input_offset;
482 string16 formatted_url = FormatUrl(url, languages, format_types,
483 unescape_rules, NULL, NULL, &offset);
484 VerboseExpect(cases[i].output_offset, offset, url_string, i, formatted_url);
485 }
486
487 size_t url_size = url_string.length();
488 std::vector<size_t> offsets;
489 for (size_t i = 0; i < url_size + 1; ++i)
490 offsets.push_back(i);
491 string16 formatted_url = FormatUrlWithOffsets(url, languages, format_types,
492 unescape_rules, NULL, NULL, &offsets);
493 for (size_t i = 0; i < url_size; ++i)
494 VerboseExpect(all_offsets[i], offsets[i], url_string, i, formatted_url);
495 VerboseExpect(kNpos, offsets[url_size], url_string, url_size, formatted_url);
461 } 496 }
462 497
463 // Helper to strignize an IP number (used to define expectations). 498 // Helper to strignize an IP number (used to define expectations).
464 std::string DumpIPNumber(const IPAddressNumber& v) { 499 std::string DumpIPNumber(const IPAddressNumber& v) {
465 std::string out; 500 std::string out;
466 for (size_t i = 0; i < v.size(); ++i) { 501 for (size_t i = 0; i < v.size(); ++i) {
467 if (i != 0) 502 if (i != 0)
468 out.append(","); 503 out.append(",");
469 out.append(base::IntToString(static_cast<int>(v[i]))); 504 out.append(base::IntToString(static_cast<int>(v[i])));
470 } 505 }
(...skipping 172 matching lines...) Expand 10 before | Expand all | Expand 10 after
643 // Extract the unescaped identity. 678 // Extract the unescaped identity.
644 string16 username, password; 679 string16 username, password;
645 GetIdentityFromURL(url, &username, &password); 680 GetIdentityFromURL(url, &username, &password);
646 681
647 // Verify that it was decoded as UTF8. 682 // Verify that it was decoded as UTF8.
648 EXPECT_EQ(ASCIIToUTF16("foo"), username); 683 EXPECT_EQ(ASCIIToUTF16("foo"), username);
649 EXPECT_EQ(WideToUTF16(L"\x4f60\x597d"), password); 684 EXPECT_EQ(WideToUTF16(L"\x4f60\x597d"), password);
650 } 685 }
651 686
652 // Just a bunch of fake headers. 687 // Just a bunch of fake headers.
653 const wchar_t* google_headers = 688 const char* google_headers =
654 L"HTTP/1.1 200 OK\n" 689 "HTTP/1.1 200 OK\n"
655 L"Content-TYPE: text/html; charset=utf-8\n" 690 "Content-TYPE: text/html; charset=utf-8\n"
656 L"Content-disposition: attachment; filename=\"download.pdf\"\n" 691 "Content-disposition: attachment; filename=\"download.pdf\"\n"
657 L"Content-Length: 378557\n" 692 "Content-Length: 378557\n"
658 L"X-Google-Google1: 314159265\n" 693 "X-Google-Google1: 314159265\n"
659 L"X-Google-Google2: aaaa2:7783,bbb21:9441\n" 694 "X-Google-Google2: aaaa2:7783,bbb21:9441\n"
660 L"X-Google-Google4: home\n" 695 "X-Google-Google4: home\n"
661 L"Transfer-Encoding: chunked\n" 696 "Transfer-Encoding: chunked\n"
662 L"Set-Cookie: HEHE_AT=6666x66beef666x6-66xx6666x66; Path=/mail\n" 697 "Set-Cookie: HEHE_AT=6666x66beef666x6-66xx6666x66; Path=/mail\n"
663 L"Set-Cookie: HEHE_HELP=owned:0;Path=/\n" 698 "Set-Cookie: HEHE_HELP=owned:0;Path=/\n"
664 L"Set-Cookie: S=gmail=Xxx-beefbeefbeef_beefb:gmail_yj=beefbeef000beefbee" 699 "Set-Cookie: S=gmail=Xxx-beefbeefbeef_beefb:gmail_yj=beefbeef000beefbee"
665 L"fbee:gmproxy=bee-fbeefbe; Domain=.google.com; Path=/\n" 700 "fbee:gmproxy=bee-fbeefbe; Domain=.google.com; Path=/\n"
666 L"X-Google-Google2: /one/two/three/four/five/six/seven-height/nine:9411\n" 701 "X-Google-Google2: /one/two/three/four/five/six/seven-height/nine:9411\n"
667 L"Server: GFE/1.3\n" 702 "Server: GFE/1.3\n"
668 L"Transfer-Encoding: chunked\n" 703 "Transfer-Encoding: chunked\n"
669 L"Date: Mon, 13 Nov 2006 21:38:09 GMT\n" 704 "Date: Mon, 13 Nov 2006 21:38:09 GMT\n"
670 L"Expires: Tue, 14 Nov 2006 19:23:58 GMT\n" 705 "Expires: Tue, 14 Nov 2006 19:23:58 GMT\n"
671 L"X-Malformed: bla; arg=test\"\n" 706 "X-Malformed: bla; arg=test\"\n"
672 L"X-Malformed2: bla; arg=\n" 707 "X-Malformed2: bla; arg=\n"
673 L"X-Test: bla; arg1=val1; arg2=val2"; 708 "X-Test: bla; arg1=val1; arg2=val2";
674 709
675 TEST(NetUtilTest, GetSpecificHeader) { 710 TEST(NetUtilTest, GetSpecificHeader) {
676 const HeaderCase tests[] = { 711 const HeaderCase tests[] = {
677 {L"content-type", L"text/html; charset=utf-8"}, 712 {"content-type", "text/html; charset=utf-8"},
678 {L"CONTENT-LENGTH", L"378557"}, 713 {"CONTENT-LENGTH", "378557"},
679 {L"Date", L"Mon, 13 Nov 2006 21:38:09 GMT"}, 714 {"Date", "Mon, 13 Nov 2006 21:38:09 GMT"},
680 {L"Bad-Header", L""}, 715 {"Bad-Header", ""},
681 {L"", L""}, 716 {"", ""},
682 }; 717 };
683 718
684 // Test first with google_headers. 719 // Test first with google_headers.
685 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) { 720 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) {
686 std::wstring result = GetSpecificHeader(google_headers, 721 std::string result =
687 tests[i].header_name); 722 GetSpecificHeader(google_headers, tests[i].header_name);
688 EXPECT_EQ(result, tests[i].expected); 723 EXPECT_EQ(result, tests[i].expected);
689 } 724 }
690 725
691 // Test again with empty headers. 726 // Test again with empty headers.
692 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) { 727 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) {
693 std::wstring result = GetSpecificHeader(L"", tests[i].header_name); 728 std::string result = GetSpecificHeader(std::string(), tests[i].header_name);
694 EXPECT_EQ(result, std::wstring()); 729 EXPECT_EQ(result, std::string());
695 } 730 }
696 } 731 }
697 732
698 TEST(NetUtilTest, GetHeaderParamValue) { 733 TEST(NetUtilTest, GetHeaderParamValue) {
699 const HeaderParamCase tests[] = { 734 const HeaderParamCase tests[] = {
700 {L"Content-type", L"charset", L"utf-8"}, 735 {"Content-type", "charset", "utf-8"},
701 {L"content-disposition", L"filename", L"download.pdf"}, 736 {"content-disposition", "filename", "download.pdf"},
702 {L"Content-Type", L"badparam", L""}, 737 {"Content-Type", "badparam", ""},
703 {L"X-Malformed", L"arg", L"test\""}, 738 {"X-Malformed", "arg", "test\""},
704 {L"X-Malformed2", L"arg", L""}, 739 {"X-Malformed2", "arg", ""},
705 {L"X-Test", L"arg1", L"val1"}, 740 {"X-Test", "arg1", "val1"},
706 {L"X-Test", L"arg2", L"val2"}, 741 {"X-Test", "arg2", "val2"},
707 {L"Bad-Header", L"badparam", L""}, 742 {"Bad-Header", "badparam", ""},
708 {L"Bad-Header", L"", L""}, 743 {"Bad-Header", "", ""},
709 {L"", L"badparam", L""}, 744 {"", "badparam", ""},
710 {L"", L"", L""}, 745 {"", "", ""},
711 }; 746 };
712 // TODO(mpcomplete): add tests for other formats of headers. 747 // TODO(mpcomplete): add tests for other formats of headers.
713 748
714 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) { 749 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) {
715 std::wstring header_value = 750 std::string header_value =
716 GetSpecificHeader(google_headers, tests[i].header_name); 751 GetSpecificHeader(google_headers, tests[i].header_name);
717 std::wstring result = 752 std::string result =
718 GetHeaderParamValue(header_value, tests[i].param_name, 753 GetHeaderParamValue(header_value, tests[i].param_name,
719 QuoteRule::REMOVE_OUTER_QUOTES); 754 QuoteRule::REMOVE_OUTER_QUOTES);
720 EXPECT_EQ(result, tests[i].expected); 755 EXPECT_EQ(result, tests[i].expected);
721 } 756 }
722 757
723 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) { 758 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) {
724 std::wstring header_value = 759 std::string header_value =
725 GetSpecificHeader(L"", tests[i].header_name); 760 GetSpecificHeader(std::string(), tests[i].header_name);
726 std::wstring result = 761 std::string result =
727 GetHeaderParamValue(header_value, tests[i].param_name, 762 GetHeaderParamValue(header_value, tests[i].param_name,
728 QuoteRule::REMOVE_OUTER_QUOTES); 763 QuoteRule::REMOVE_OUTER_QUOTES);
729 EXPECT_EQ(result, std::wstring()); 764 EXPECT_EQ(result, std::string());
730 } 765 }
731 } 766 }
732 767
733 TEST(NetUtilTest, GetHeaderParamValueQuotes) { 768 TEST(NetUtilTest, GetHeaderParamValueQuotes) {
734 struct { 769 struct {
735 const char* header; 770 const char* header;
736 const char* expected_with_quotes; 771 const char* expected_with_quotes;
737 const char* expected_without_quotes; 772 const char* expected_without_quotes;
738 } tests[] = { 773 } tests[] = {
739 {"filename=foo", "foo", "foo"}, 774 {"filename=foo", "foo", "foo"},
(...skipping 194 matching lines...) Expand 10 before | Expand all | Expand 10 after
934 << "Failed on input: " << tests[i].header_field; 969 << "Failed on input: " << tests[i].header_field;
935 } 970 }
936 } 971 }
937 972
938 TEST(NetUtilTest, IDNToUnicodeFast) { 973 TEST(NetUtilTest, IDNToUnicodeFast) {
939 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(idn_cases); i++) { 974 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(idn_cases); i++) {
940 for (size_t j = 0; j < arraysize(kLanguages); j++) { 975 for (size_t j = 0; j < arraysize(kLanguages); j++) {
941 // ja || zh-TW,en || ko,ja -> IDNToUnicodeSlow 976 // ja || zh-TW,en || ko,ja -> IDNToUnicodeSlow
942 if (j == 3 || j == 17 || j == 18) 977 if (j == 3 || j == 17 || j == 18)
943 continue; 978 continue;
944 std::wstring output(IDNToUnicode(idn_cases[i].input, 979 string16 output(IDNToUnicode(idn_cases[i].input, kLanguages[j]));
945 strlen(idn_cases[i].input), kLanguages[j], NULL)); 980 string16 expected(idn_cases[i].unicode_allowed[j] ?
946 std::wstring expected(idn_cases[i].unicode_allowed[j] ? 981 WideToUTF16(idn_cases[i].unicode_output) :
947 idn_cases[i].unicode_output : ASCIIToWide(idn_cases[i].input)); 982 ASCIIToUTF16(idn_cases[i].input));
948 AppendLanguagesToOutputs(kLanguages[j], &expected, &output); 983 AppendLanguagesToOutputs(kLanguages[j], &expected, &output);
949 EXPECT_EQ(expected, output); 984 EXPECT_EQ(expected, output);
950 } 985 }
951 } 986 }
952 } 987 }
953 988
954 TEST(NetUtilTest, IDNToUnicodeSlow) { 989 TEST(NetUtilTest, IDNToUnicodeSlow) {
955 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(idn_cases); i++) { 990 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(idn_cases); i++) {
956 for (size_t j = 0; j < arraysize(kLanguages); j++) { 991 for (size_t j = 0; j < arraysize(kLanguages); j++) {
957 // !(ja || zh-TW,en || ko,ja) -> IDNToUnicodeFast 992 // !(ja || zh-TW,en || ko,ja) -> IDNToUnicodeFast
958 if (!(j == 3 || j == 17 || j == 18)) 993 if (!(j == 3 || j == 17 || j == 18))
959 continue; 994 continue;
960 std::wstring output(IDNToUnicode(idn_cases[i].input, 995 string16 output(IDNToUnicode(idn_cases[i].input, kLanguages[j]));
961 strlen(idn_cases[i].input), kLanguages[j], NULL)); 996 string16 expected(idn_cases[i].unicode_allowed[j] ?
962 std::wstring expected(idn_cases[i].unicode_allowed[j] ? 997 WideToUTF16(idn_cases[i].unicode_output) :
963 idn_cases[i].unicode_output : ASCIIToWide(idn_cases[i].input)); 998 ASCIIToUTF16(idn_cases[i].input));
964 AppendLanguagesToOutputs(kLanguages[j], &expected, &output); 999 AppendLanguagesToOutputs(kLanguages[j], &expected, &output);
965 EXPECT_EQ(expected, output); 1000 EXPECT_EQ(expected, output);
966 } 1001 }
967 } 1002 }
968 } 1003 }
969 1004
970 TEST(NetUtilTest, IDNToUnicodeAdjustOffset) {
971 const AdjustOffsetCase adjust_cases[] = {
972 {0, 0},
973 {2, 2},
974 {4, 4},
975 {5, 5},
976 {6, string16::npos},
977 {16, string16::npos},
978 {17, 7},
979 {18, 8},
980 {19, string16::npos},
981 {25, string16::npos},
982 {34, 12},
983 {35, 13},
984 {38, 16},
985 {39, string16::npos},
986 {string16::npos, string16::npos},
987 };
988 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(adjust_cases); ++i) {
989 size_t offset = adjust_cases[i].input_offset;
990 // "test.\x89c6\x9891.\x5317\x4eac\x5927\x5b78.test"
991 IDNToUnicode("test.xn--cy2a840a.xn--1lq90ic7f1rc.test", 39, L"zh-CN",
992 &offset);
993 EXPECT_EQ(adjust_cases[i].output_offset, offset);
994 }
995
996 std::vector<size_t> offsets;
997 for (size_t i = 0; i < 40; ++i)
998 offsets.push_back(i);
999 IDNToUnicodeWithOffsets("test.xn--cy2a840a.xn--1lq90ic7f1rc.test", 39,
1000 L"zh-CN", &offsets);
1001 size_t expected[] = {0, 1, 2, 3, 4, 5, kNpos, kNpos, kNpos, kNpos, kNpos,
1002 kNpos, kNpos, kNpos, kNpos, kNpos, kNpos, 7, 8, kNpos,
1003 kNpos, kNpos, kNpos, kNpos, kNpos, kNpos, kNpos, kNpos,
1004 kNpos, kNpos, kNpos, kNpos, kNpos, kNpos, 12, 13, 14, 15,
1005 16, kNpos};
1006 ASSERT_EQ(40U, arraysize(expected));
1007 for (size_t i = 0; i < 40; ++i)
1008 EXPECT_EQ(expected[i], offsets[i]);
1009 }
1010
1011 TEST(NetUtilTest, CompliantHost) { 1005 TEST(NetUtilTest, CompliantHost) {
1012 const CompliantHostCase compliant_host_cases[] = { 1006 const CompliantHostCase compliant_host_cases[] = {
1013 {"", "", false}, 1007 {"", "", false},
1014 {"a", "", true}, 1008 {"a", "", true},
1015 {"-", "", false}, 1009 {"-", "", false},
1016 {".", "", false}, 1010 {".", "", false},
1017 {"9", "", false}, 1011 {"9", "", false},
1018 {"9", "a", true}, 1012 {"9", "a", true},
1019 {"9a", "", false}, 1013 {"9a", "", false},
1020 {"9a", "a", true}, 1014 {"9a", "a", true},
(...skipping 766 matching lines...) Expand 10 before | Expand all | Expand 10 after
1787 EXPECT_FALSE(parsed.port.is_valid()); 1781 EXPECT_FALSE(parsed.port.is_valid());
1788 EXPECT_TRUE(parsed.path.is_valid()); 1782 EXPECT_TRUE(parsed.path.is_valid());
1789 EXPECT_FALSE(parsed.query.is_valid()); 1783 EXPECT_FALSE(parsed.query.is_valid());
1790 EXPECT_FALSE(parsed.ref.is_valid()); 1784 EXPECT_FALSE(parsed.ref.is_valid());
1791 EXPECT_EQ(WideToUTF16(L"f"), 1785 EXPECT_EQ(WideToUTF16(L"f"),
1792 formatted.substr(parsed.host.begin, parsed.host.len)); 1786 formatted.substr(parsed.host.begin, parsed.host.len));
1793 EXPECT_EQ(WideToUTF16(L"/"), 1787 EXPECT_EQ(WideToUTF16(L"/"),
1794 formatted.substr(parsed.path.begin, parsed.path.len)); 1788 formatted.substr(parsed.path.begin, parsed.path.len));
1795 } 1789 }
1796 1790
1797 TEST(NetUtilTest, FormatUrlAdjustOffset) { 1791 TEST(NetUtilTest, FormatUrlWithOffsets) {
1792 const AdjustOffsetCase null_cases[] = {
1793 {0, string16::npos},
1794 };
1795 CheckAdjustedOffsets(std::string(), "en", kFormatUrlOmitNothing,
1796 UnescapeRule::NORMAL, null_cases, arraysize(null_cases), NULL);
1797
1798 const AdjustOffsetCase basic_cases[] = { 1798 const AdjustOffsetCase basic_cases[] = {
1799 {0, 0}, 1799 {0, 0},
1800 {3, 3}, 1800 {3, 3},
1801 {5, 5}, 1801 {5, 5},
1802 {6, 6}, 1802 {6, 6},
1803 {13, 13}, 1803 {13, 13},
1804 {21, 21}, 1804 {21, 21},
1805 {22, 22}, 1805 {22, 22},
1806 {23, 23}, 1806 {23, 23},
1807 {25, 25}, 1807 {25, 25},
1808 {26, string16::npos}, 1808 {26, string16::npos},
1809 {500000, string16::npos}, 1809 {500000, string16::npos},
1810 {string16::npos, string16::npos}, 1810 {string16::npos, string16::npos},
1811 }; 1811 };
1812 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(basic_cases); ++i) { 1812 const size_t basic_offsets[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13,
1813 size_t offset = basic_cases[i].input_offset; 1813 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25};
1814 FormatUrl(GURL("http://www.google.com/foo/"), "en", 1814 CheckAdjustedOffsets("http://www.google.com/foo/", "en",
1815 kFormatUrlOmitUsernamePassword, UnescapeRule::NORMAL, 1815 kFormatUrlOmitNothing, UnescapeRule::NORMAL, basic_cases,
1816 NULL, NULL, &offset); 1816 arraysize(basic_cases), basic_offsets);
1817 EXPECT_EQ(basic_cases[i].output_offset, offset);
1818 }
1819 1817
1820 size_t url_size = 26; 1818 const AdjustOffsetCase omit_auth_cases_1[] = {
1821 std::vector<size_t> offsets; 1819 {6, 6},
1822 for (size_t i = 0; i < url_size + 1; ++i) 1820 {7, string16::npos},
1823 offsets.push_back(i); 1821 {8, string16::npos},
1824 FormatUrlWithOffsets(GURL("http://www.google.com/foo/"), "en", 1822 {10, string16::npos},
1825 kFormatUrlOmitUsernamePassword, UnescapeRule::NORMAL, 1823 {12, string16::npos},
1826 NULL, NULL, &offsets); 1824 {14, string16::npos},
1827 for (size_t i = 0; i < url_size; ++i) 1825 {15, 7},
1828 EXPECT_EQ(i, offsets[i]); 1826 {25, 17},
1829 EXPECT_EQ(kNpos, offsets[url_size]); 1827 };
1828 const size_t omit_auth_offsets_1[] = {0, 1, 2, 3, 4, 5, 6, kNpos, kNpos,
1829 kNpos, kNpos, kNpos, kNpos, kNpos, kNpos, 7, 8, 9, 10, 11, 12, 13, 14, 15,
1830 16, 17, 18, 19, 20, 21};
1831 CheckAdjustedOffsets("http://foo:bar@www.google.com/", "en",
1832 kFormatUrlOmitUsernamePassword, UnescapeRule::NORMAL, omit_auth_cases_1,
1833 arraysize(omit_auth_cases_1), omit_auth_offsets_1);
1830 1834
1831 const struct { 1835 const AdjustOffsetCase omit_auth_cases_2[] = {
1832 const char* input_url; 1836 {9, string16::npos},
1833 size_t input_offset; 1837 {11, 7},
1834 size_t output_offset;
1835 } omit_auth_cases[] = {
1836 {"http://foo:bar@www.google.com/", 6, 6},
1837 {"http://foo:bar@www.google.com/", 7, string16::npos},
1838 {"http://foo:bar@www.google.com/", 8, string16::npos},
1839 {"http://foo:bar@www.google.com/", 10, string16::npos},
1840 {"http://foo:bar@www.google.com/", 11, string16::npos},
1841 {"http://foo:bar@www.google.com/", 14, string16::npos},
1842 {"http://foo:bar@www.google.com/", 15, 7},
1843 {"http://foo:bar@www.google.com/", 25, 17},
1844 {"http://foo@www.google.com/", 9, string16::npos},
1845 {"http://foo@www.google.com/", 11, 7},
1846 }; 1838 };
1847 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(omit_auth_cases); ++i) { 1839 const size_t omit_auth_offsets_2[] = {0, 1, 2, 3, 4, 5, 6, kNpos, kNpos,
1848 size_t offset = omit_auth_cases[i].input_offset; 1840 kNpos, kNpos, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21};
1849 FormatUrl(GURL(omit_auth_cases[i].input_url), "en", 1841 CheckAdjustedOffsets("http://foo@www.google.com/", "en",
1850 kFormatUrlOmitUsernamePassword, UnescapeRule::NORMAL, 1842 kFormatUrlOmitUsernamePassword, UnescapeRule::NORMAL, omit_auth_cases_2,
1851 NULL, NULL, &offset); 1843 arraysize(omit_auth_cases_2), omit_auth_offsets_2);
1852 EXPECT_EQ(omit_auth_cases[i].output_offset, offset);
1853 }
1854 1844
1855 url_size = 30; 1845 // "http://foo\x30B0:\x30B0bar@www.google.com"
1856 offsets.clear(); 1846 const AdjustOffsetCase dont_omit_auth_cases[] = {
1857 for (size_t i = 0; i < url_size; ++i) 1847 {0, 0},
1858 offsets.push_back(i); 1848 /*{3, string16::npos},
1859 FormatUrlWithOffsets(GURL("http://foo:bar@www.google.com/"), "en", 1849 {7, 0},
1860 kFormatUrlOmitUsernamePassword, UnescapeRule::NORMAL, 1850 {11, 4},
1861 NULL, NULL, &offsets); 1851 {12, string16::npos},
1862 for (size_t i = 0; i < 7; ++i) 1852 {20, 5},
1863 EXPECT_EQ(i, offsets[i]); 1853 {24, 9},*/
1864 for (size_t i = 7; i < 15; ++i) 1854 };
1865 EXPECT_EQ(kNpos, offsets[i]); 1855 const size_t dont_omit_auth_offsets[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
1866 for (size_t i = 16; i < url_size; ++i) 1856 kNpos, kNpos, kNpos, kNpos, kNpos, kNpos, kNpos, kNpos, 11, 12, kNpos,
1867 EXPECT_EQ(i - 8 , offsets[i]); 1857 kNpos, kNpos, kNpos, kNpos, kNpos, kNpos, kNpos, 13, 14, 15, 16, 17, 18,
1858 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31};
1859 CheckAdjustedOffsets("http://foo%E3%82%B0:%E3%82%B0bar@www.google.com/", "en",
1860 kFormatUrlOmitNothing, UnescapeRule::NORMAL, dont_omit_auth_cases,
1861 arraysize(dont_omit_auth_cases), dont_omit_auth_offsets);
1868 1862
1869 const AdjustOffsetCase view_source_cases[] = { 1863 const AdjustOffsetCase view_source_cases[] = {
1870 {0, 0}, 1864 {0, 0},
1871 {3, 3}, 1865 {3, 3},
1872 {11, 11}, 1866 {11, 11},
1873 {12, 12}, 1867 {12, 12},
1874 {13, 13}, 1868 {13, 13},
1875 {18, 18}, 1869 {18, 18},
1876 {19, string16::npos}, 1870 {19, string16::npos},
1877 {20, string16::npos}, 1871 {20, string16::npos},
1878 {23, 19}, 1872 {23, 19},
1879 {26, 22}, 1873 {26, 22},
1880 {string16::npos, string16::npos}, 1874 {string16::npos, string16::npos},
1881 }; 1875 };
1882 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(view_source_cases); ++i) { 1876 const size_t view_source_offsets[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11,
1883 size_t offset = view_source_cases[i].input_offset; 1877 12, 13, 14, 15, 16, 17, 18, kNpos, kNpos, kNpos, kNpos, 19, 20, 21, 22,
1884 FormatUrl(GURL("view-source:http://foo@www.google.com/"), "en", 1878 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33};
1885 kFormatUrlOmitUsernamePassword, UnescapeRule::NORMAL, 1879 CheckAdjustedOffsets("view-source:http://foo@www.google.com/", "en",
1886 NULL, NULL, &offset); 1880 kFormatUrlOmitUsernamePassword, UnescapeRule::NORMAL, view_source_cases,
1887 EXPECT_EQ(view_source_cases[i].output_offset, offset); 1881 arraysize(view_source_cases), view_source_offsets);
1888 }
1889 1882
1890 url_size = 38; 1883 // "http://\x671d\x65e5\x3042\x3055\x3072.jp/foo/"
1891 offsets.clear(); 1884 const AdjustOffsetCase idn_hostname_cases_1[] = {
1892 for (size_t i = 0; i < url_size; ++i)
1893 offsets.push_back(i);
1894 FormatUrlWithOffsets(GURL("view-source:http://foo@www.google.com/"), "en",
1895 kFormatUrlOmitUsernamePassword, UnescapeRule::NORMAL,
1896 NULL, NULL, &offsets);
1897 size_t expected[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
1898 17, 18, kNpos, kNpos, kNpos, kNpos, 19, 20, 21, 22, 23,
1899 24, 25, 26, 27, 28, 29, 30, 31, 32, 33};
1900 ASSERT_EQ(url_size, arraysize(expected));
1901 for (size_t i = 0; i < url_size; ++i)
1902 EXPECT_EQ(expected[i], offsets[i]);
1903
1904 const AdjustOffsetCase idn_hostname_cases[] = {
1905 {8, string16::npos}, 1885 {8, string16::npos},
1906 {16, string16::npos}, 1886 {16, string16::npos},
1907 {24, string16::npos}, 1887 {24, string16::npos},
1908 {25, 12}, 1888 {25, 12},
1909 {30, 17}, 1889 {30, 17},
1910 }; 1890 };
1911 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(idn_hostname_cases); ++i) { 1891 const size_t idn_hostname_offsets_1[] = {0, 1, 2, 3, 4, 5, 6, 7, kNpos, kNpos,
1912 size_t offset = idn_hostname_cases[i].input_offset; 1892 kNpos, kNpos, kNpos, kNpos, kNpos, kNpos, kNpos, kNpos, kNpos, kNpos,
1913 // "http://\x671d\x65e5\x3042\x3055\x3072.jp/foo/" 1893 kNpos, kNpos, kNpos, kNpos, kNpos, 12, 13, 14, 15, 16, 17, 18, 19};
1914 FormatUrl(GURL("http://xn--l8jvb1ey91xtjb.jp/foo/"), "ja", 1894 CheckAdjustedOffsets("http://xn--l8jvb1ey91xtjb.jp/foo/", "ja",
1915 kFormatUrlOmitUsernamePassword, UnescapeRule::NORMAL, 1895 kFormatUrlOmitNothing, UnescapeRule::NORMAL, idn_hostname_cases_1,
1916 NULL, NULL, &offset); 1896 arraysize(idn_hostname_cases_1), idn_hostname_offsets_1);
1917 EXPECT_EQ(idn_hostname_cases[i].output_offset, offset);
1918 }
1919 1897
1920 url_size = 33; 1898 // "http://test.\x89c6\x9891.\x5317\x4eac\x5927\x5b78.test/"
1921 offsets.clear(); 1899 const AdjustOffsetCase idn_hostname_cases_2[] = {
1922 for (size_t i = 0; i < url_size; ++i) 1900 {7, 7},
1923 offsets.push_back(i); 1901 {9, 9},
1924 FormatUrlWithOffsets(GURL("http://xn--l8jvb1ey91xtjb.jp/foo/"), "ja", 1902 {11, 11},
1925 kFormatUrlOmitUsernamePassword, UnescapeRule::NORMAL, 1903 {12, 12},
1926 NULL, NULL, &offsets); 1904 {13, string16::npos},
1927 size_t expected_1[] = {0, 1, 2, 3, 4, 5, 6, 7, kNpos, kNpos, kNpos, kNpos, 1905 {23, string16::npos},
1928 kNpos, kNpos, kNpos, kNpos, kNpos, kNpos, kNpos, kNpos, 1906 {24, 14},
1929 kNpos, kNpos, kNpos, kNpos, kNpos, 12, 13, 14, 15, 16, 1907 {25, 15},
1930 17, 18, 19}; 1908 {26, string16::npos},
1931 ASSERT_EQ(url_size, arraysize(expected_1)); 1909 {32, string16::npos},
1932 for (size_t i = 0; i < url_size; ++i) 1910 {41, 19},
1933 EXPECT_EQ(expected_1[i], offsets[i]); 1911 {42, 20},
1912 {45, 23},
1913 {46, 24},
1914 {47, string16::npos},
1915 {string16::npos, string16::npos},
1916 };
1917 const size_t idn_hostname_offsets_2[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11,
1918 12, kNpos, kNpos, kNpos, kNpos, kNpos, kNpos, kNpos, kNpos, kNpos, kNpos,
1919 kNpos, 14, 15, kNpos, kNpos, kNpos, kNpos, kNpos, kNpos, kNpos, kNpos,
1920 kNpos, kNpos, kNpos, kNpos, kNpos, kNpos, kNpos, 19, 20, 21, 22, 23, 24};
1921 CheckAdjustedOffsets("http://test.xn--cy2a840a.xn--1lq90ic7f1rc.test/",
1922 "zh-CN", kFormatUrlOmitNothing, UnescapeRule::NORMAL,
1923 idn_hostname_cases_2, arraysize(idn_hostname_cases_2),
1924 idn_hostname_offsets_2);
1934 1925
1926 // "http://www.google.com/foo bar/\x30B0\x30FC\x30B0\x30EB"
1935 const AdjustOffsetCase unescape_cases[] = { 1927 const AdjustOffsetCase unescape_cases[] = {
1936 {25, 25}, 1928 {25, 25},
1937 {26, string16::npos}, 1929 {26, string16::npos},
1938 {27, string16::npos}, 1930 {27, string16::npos},
1939 {28, 26}, 1931 {28, 26},
1940 {35, string16::npos}, 1932 {35, string16::npos},
1941 {41, 31}, 1933 {41, 31},
1942 {59, 33}, 1934 {59, 33},
1943 {60, string16::npos}, 1935 {60, string16::npos},
1944 {67, string16::npos}, 1936 {67, string16::npos},
1945 {68, string16::npos}, 1937 {68, string16::npos},
1946 }; 1938 };
1947 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(unescape_cases); ++i) { 1939 const size_t unescape_offsets[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12,
1948 size_t offset = unescape_cases[i].input_offset; 1940 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, kNpos, kNpos, 26, 27,
1949 // "http://www.google.com/foo bar/\x30B0\x30FC\x30B0\x30EB" 1941 28, 29, 30, kNpos, kNpos, kNpos, kNpos, kNpos, kNpos, kNpos, kNpos, 31,
1950 FormatUrl(GURL( 1942 kNpos, kNpos, kNpos, kNpos, kNpos, kNpos, kNpos, kNpos, 32, kNpos, kNpos,
1951 "http://www.google.com/foo%20bar/%E3%82%B0%E3%83%BC%E3%82%B0%E3%83%AB"), 1943 kNpos, kNpos, kNpos, kNpos, kNpos, kNpos, 33, kNpos, kNpos, kNpos, kNpos,
1952 "en", kFormatUrlOmitUsernamePassword, UnescapeRule::SPACES, NULL, 1944 kNpos, kNpos, kNpos, kNpos};
1953 NULL, &offset); 1945 CheckAdjustedOffsets(
1954 EXPECT_EQ(unescape_cases[i].output_offset, offset); 1946 "http://www.google.com/foo%20bar/%E3%82%B0%E3%83%BC%E3%82%B0%E3%83%AB",
1955 } 1947 "en", kFormatUrlOmitNothing, UnescapeRule::SPACES, unescape_cases,
1948 arraysize(unescape_cases), unescape_offsets);
1956 1949
1957 url_size = 68; 1950 // "http://www.google.com/foo.html#\x30B0\x30B0z"
1958 offsets.clear();
1959 for (size_t i = 0; i < url_size; ++i)
1960 offsets.push_back(i);
1961 FormatUrlWithOffsets(GURL(
1962 "http://www.google.com/foo%20bar/%E3%82%B0%E3%83%BC%E3%82%B0%E3%83%AB"),
1963 "en", kFormatUrlOmitUsernamePassword, UnescapeRule::SPACES, NULL, NULL,
1964 &offsets);
1965 size_t expected_2[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
1966 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, kNpos, kNpos,
1967 26, 27, 28, 29, 30, kNpos, kNpos, kNpos, kNpos, kNpos,
1968 kNpos, kNpos, kNpos, 31, kNpos, kNpos, kNpos, kNpos,
1969 kNpos, kNpos, kNpos, kNpos, 32, kNpos, kNpos, kNpos,
1970 kNpos, kNpos, kNpos, kNpos, kNpos, 33, kNpos, kNpos,
1971 kNpos, kNpos, kNpos, kNpos, kNpos, kNpos};
1972 ASSERT_EQ(url_size, arraysize(expected_2));
1973 for (size_t i = 0; i < url_size; ++i)
1974 EXPECT_EQ(expected_2[i], offsets[i]);
1975
1976 const AdjustOffsetCase ref_cases[] = { 1951 const AdjustOffsetCase ref_cases[] = {
1977 {30, 30}, 1952 {30, 30},
1978 {31, 31}, 1953 {31, 31},
1979 {32, string16::npos}, 1954 {32, string16::npos},
1980 {34, 32}, 1955 {34, 32},
1981 {35, string16::npos}, 1956 {35, string16::npos},
1982 {37, 33}, 1957 {37, 33},
1983 {38, string16::npos}, 1958 {38, string16::npos},
1984 }; 1959 };
1985 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(ref_cases); ++i) { 1960 const size_t ref_offsets[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13,
1986 size_t offset = ref_cases[i].input_offset; 1961 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31,
1987 // "http://www.google.com/foo.html#\x30B0\x30B0z" 1962 kNpos, kNpos, 32, kNpos, kNpos, 33};
1988 FormatUrl(GURL( 1963 CheckAdjustedOffsets(
1989 "http://www.google.com/foo.html#\xE3\x82\xB0\xE3\x82\xB0z"), "en", 1964 "http://www.google.com/foo.html#\xE3\x82\xB0\xE3\x82\xB0z", "en",
1990 kFormatUrlOmitUsernamePassword, UnescapeRule::NORMAL, NULL, NULL, 1965 kFormatUrlOmitNothing, UnescapeRule::NORMAL, ref_cases,
1991 &offset); 1966 arraysize(ref_cases), ref_offsets);
1992 EXPECT_EQ(ref_cases[i].output_offset, offset);
1993 }
1994
1995 url_size = 38;
1996 offsets.clear();
1997 for (size_t i = 0; i < url_size; ++i)
1998 offsets.push_back(i);
1999 // "http://www.google.com/foo.html#\x30B0\x30B0z"
2000 FormatUrlWithOffsets(GURL(
2001 "http://www.google.com/foo.html#\xE3\x82\xB0\xE3\x82\xB0z"), "en",
2002 kFormatUrlOmitUsernamePassword, UnescapeRule::NORMAL, NULL, NULL,
2003 &offsets);
2004 size_t expected_3[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
2005 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29,
2006 30, 31, kNpos, kNpos, 32, kNpos, kNpos, 33};
2007 ASSERT_EQ(url_size, arraysize(expected_3));
2008 for (size_t i = 0; i < url_size; ++i)
2009 EXPECT_EQ(expected_3[i], offsets[i]);
2010 1967
2011 const AdjustOffsetCase omit_http_cases[] = { 1968 const AdjustOffsetCase omit_http_cases[] = {
2012 {0, string16::npos}, 1969 {0, string16::npos},
2013 {3, string16::npos}, 1970 {3, string16::npos},
2014 {7, 0}, 1971 {7, 0},
2015 {8, 1}, 1972 {8, 1},
2016 }; 1973 };
2017 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(omit_http_cases); ++i) { 1974 const size_t omit_http_offsets[] = {kNpos, kNpos, kNpos, kNpos, kNpos, kNpos,
2018 size_t offset = omit_http_cases[i].input_offset; 1975 kNpos, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14};
2019 FormatUrl(GURL("http://www.google.com"), "en", 1976 CheckAdjustedOffsets("http://www.google.com/", "en",
2020 kFormatUrlOmitHTTP, UnescapeRule::NORMAL, NULL, NULL, &offset); 1977 kFormatUrlOmitHTTP, UnescapeRule::NORMAL, omit_http_cases,
2021 EXPECT_EQ(omit_http_cases[i].output_offset, offset); 1978 arraysize(omit_http_cases), omit_http_offsets);
2022 }
2023 1979
2024 url_size = 23; 1980 const AdjustOffsetCase omit_http_start_with_ftp_cases[] = {
2025 offsets.clear();
2026 for (size_t i = 0; i < url_size; ++i)
2027 offsets.push_back(i);
2028 FormatUrlWithOffsets(GURL("http://www.google.com"), "en",
2029 kFormatUrlOmitHTTP, UnescapeRule::NORMAL, NULL, NULL, &offsets);
2030 size_t expected_4[] = {kNpos, kNpos, kNpos, kNpos, kNpos, kNpos, kNpos, 0, 1,
2031 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, kNpos};
2032 ASSERT_EQ(url_size, arraysize(expected_4));
2033 for (size_t i = 0; i < url_size; ++i)
2034 EXPECT_EQ(expected_4[i], offsets[i]);
2035
2036 const AdjustOffsetCase omit_http_start_with_ftp[] = {
2037 {0, 0}, 1981 {0, 0},
2038 {3, 3}, 1982 {3, 3},
2039 {8, 8}, 1983 {8, 8},
2040 }; 1984 };
2041 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(omit_http_start_with_ftp); ++i) { 1985 const size_t omit_http_start_with_ftp_offsets[] = {0, 1, 2, 3, 4, 5, 6, 7, 8,
2042 size_t offset = omit_http_start_with_ftp[i].input_offset; 1986 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21};
2043 FormatUrl(GURL("http://ftp.google.com"), "en", 1987 CheckAdjustedOffsets("http://ftp.google.com/", "en", kFormatUrlOmitHTTP,
2044 kFormatUrlOmitHTTP, UnescapeRule::NORMAL, NULL, NULL, &offset); 1988 UnescapeRule::NORMAL, omit_http_start_with_ftp_cases,
2045 EXPECT_EQ(omit_http_start_with_ftp[i].output_offset, offset); 1989 arraysize(omit_http_start_with_ftp_cases),
2046 } 1990 omit_http_start_with_ftp_offsets);
2047
2048 url_size = 23;
2049 offsets.clear();
2050 for (size_t i = 0; i < url_size; ++i)
2051 offsets.push_back(i);
2052 FormatUrlWithOffsets(GURL("http://ftp.google.com"), "en",
2053 kFormatUrlOmitHTTP, UnescapeRule::NORMAL, NULL, NULL, &offsets);
2054 size_t expected_5[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
2055 16, 17, 18, 19, 20, 21, kNpos};
2056 ASSERT_EQ(url_size, arraysize(expected_5));
2057 for (size_t i = 0; i < url_size; ++i)
2058 EXPECT_EQ(expected_5[i], offsets[i]);
2059 1991
2060 const AdjustOffsetCase omit_all_cases[] = { 1992 const AdjustOffsetCase omit_all_cases[] = {
2061 {12, 0}, 1993 {12, 0},
2062 {13, 1}, 1994 {13, 1},
2063 {0, string16::npos}, 1995 {0, string16::npos},
2064 {3, string16::npos}, 1996 {3, string16::npos},
2065 }; 1997 };
2066 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(omit_all_cases); ++i) { 1998 const size_t omit_all_offsets[] = {kNpos, kNpos, kNpos, kNpos, kNpos, kNpos,
2067 size_t offset = omit_all_cases[i].input_offset; 1999 kNpos, kNpos, kNpos, kNpos, kNpos, kNpos, 0, 1, 2, 3, 4, 5, 6, kNpos};
2068 FormatUrl(GURL("http://user@foo.com/"), "en", kFormatUrlOmitAll, 2000 CheckAdjustedOffsets("http://user@foo.com/", "en", kFormatUrlOmitAll,
2069 UnescapeRule::NORMAL, NULL, NULL, &offset); 2001 UnescapeRule::NORMAL, omit_all_cases,
2070 EXPECT_EQ(omit_all_cases[i].output_offset, offset); 2002 arraysize(omit_all_cases), omit_all_offsets);
2071 }
2072
2073 url_size = 21;
2074 offsets.clear();
2075 for (size_t i = 0; i < url_size; ++i)
2076 offsets.push_back(i);
2077 FormatUrlWithOffsets(GURL("http://user@foo.com/"), "en", kFormatUrlOmitAll,
2078 UnescapeRule::NORMAL, NULL, NULL, &offsets);
2079 size_t expected_6[] = {kNpos, kNpos, kNpos, kNpos, kNpos, kNpos, kNpos, kNpos,
2080 kNpos, kNpos, kNpos, kNpos, 0, 1, 2, 3, 4, 5, 6, 7,
2081 kNpos};
2082 ASSERT_EQ(url_size, arraysize(expected_6));
2083 for (size_t i = 0; i < url_size; ++i)
2084 EXPECT_EQ(expected_6[i], offsets[i]);
2085 } 2003 }
2086 2004
2087 TEST(NetUtilTest, SimplifyUrlForRequest) { 2005 TEST(NetUtilTest, SimplifyUrlForRequest) {
2088 struct { 2006 struct {
2089 const char* input_url; 2007 const char* input_url;
2090 const char* expected_simplified_url; 2008 const char* expected_simplified_url;
2091 } tests[] = { 2009 } tests[] = {
2092 { 2010 {
2093 // Reference section should be stripped. 2011 // Reference section should be stripped.
2094 "http://www.google.com:78/foobar?query=1#hash", 2012 "http://www.google.com:78/foobar?query=1#hash",
(...skipping 265 matching lines...) Expand 10 before | Expand all | Expand 10 after
2360 for (size_t i = 0; i < it->address.size(); ++i) { 2278 for (size_t i = 0; i < it->address.size(); ++i) {
2361 if (it->address[i] != 0) { 2279 if (it->address[i] != 0) {
2362 all_zeroes = false; 2280 all_zeroes = false;
2363 break; 2281 break;
2364 } 2282 }
2365 } 2283 }
2366 EXPECT_FALSE(all_zeroes); 2284 EXPECT_FALSE(all_zeroes);
2367 } 2285 }
2368 } 2286 }
2369 2287
2370 TEST(NetUtilTest, AdjustComponentOffset) {
2371 std::vector<size_t> old_offsets;
2372 for (size_t i = 0; i < 10; ++i)
2373 old_offsets.push_back(i);
2374 std::vector<size_t> new_offsets;
2375 std::transform(old_offsets.begin(),
2376 old_offsets.end(),
2377 std::back_inserter(new_offsets),
2378 ClampComponentOffset(5));
2379 size_t expected_1[] = {kNpos, kNpos, kNpos, kNpos, kNpos, 5, 6, 7, 8, 9};
2380 EXPECT_EQ(new_offsets.size(), arraysize(expected_1));
2381 EXPECT_EQ(new_offsets.size(), old_offsets.size());
2382 for (size_t i = 0; i < arraysize(expected_1); ++i)
2383 EXPECT_EQ(expected_1[i], new_offsets[i]);
2384 }
2385
2386 } // namespace net 2288 } // namespace net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698