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_number_conversions.h" | 9 #include "base/string_number_conversions.h" |
10 #include "base/string_util.h" | 10 #include "base/string_util.h" |
(...skipping 693 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
704 {L"Bad-Header", L"", L""}, | 704 {L"Bad-Header", L"", L""}, |
705 {L"", L"badparam", L""}, | 705 {L"", L"badparam", L""}, |
706 {L"", L"", L""}, | 706 {L"", L"", L""}, |
707 }; | 707 }; |
708 // TODO(mpcomplete): add tests for other formats of headers. | 708 // TODO(mpcomplete): add tests for other formats of headers. |
709 | 709 |
710 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) { | 710 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) { |
711 std::wstring header_value = | 711 std::wstring header_value = |
712 net::GetSpecificHeader(google_headers, tests[i].header_name); | 712 net::GetSpecificHeader(google_headers, tests[i].header_name); |
713 std::wstring result = | 713 std::wstring result = |
714 net::GetHeaderParamValue(header_value, tests[i].param_name); | 714 net::GetHeaderParamValue(header_value, tests[i].param_name, |
| 715 net::QuoteRule::REMOVE_OUTER_QUOTES); |
715 EXPECT_EQ(result, tests[i].expected); | 716 EXPECT_EQ(result, tests[i].expected); |
716 } | 717 } |
717 | 718 |
718 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) { | 719 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) { |
719 std::wstring header_value = | 720 std::wstring header_value = |
720 net::GetSpecificHeader(L"", tests[i].header_name); | 721 net::GetSpecificHeader(L"", tests[i].header_name); |
721 std::wstring result = | 722 std::wstring result = |
722 net::GetHeaderParamValue(header_value, tests[i].param_name); | 723 net::GetHeaderParamValue(header_value, tests[i].param_name, |
| 724 net::QuoteRule::REMOVE_OUTER_QUOTES); |
723 EXPECT_EQ(result, std::wstring()); | 725 EXPECT_EQ(result, std::wstring()); |
724 } | 726 } |
725 } | 727 } |
726 | 728 |
| 729 TEST(NetUtilTest, GetHeaderParamValueQuotes) { |
| 730 struct { |
| 731 const char* header; |
| 732 const char* expected_with_quotes; |
| 733 const char* expected_without_quotes; |
| 734 } tests[] = { |
| 735 {"filename=foo", "foo", "foo"}, |
| 736 {"filename=\"foo\"", "\"foo\"", "foo"}, |
| 737 {"filename=foo\"", "foo\"", "foo\""}, |
| 738 {"filename=fo\"o", "fo\"o", "fo\"o"}, |
| 739 }; |
| 740 |
| 741 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) { |
| 742 std::string actual_with_quotes = |
| 743 net::GetHeaderParamValue(tests[i].header, "filename", |
| 744 net::QuoteRule::KEEP_OUTER_QUOTES); |
| 745 std::string actual_without_quotes = |
| 746 net::GetHeaderParamValue(tests[i].header, "filename", |
| 747 net::QuoteRule::REMOVE_OUTER_QUOTES); |
| 748 EXPECT_EQ(tests[i].expected_with_quotes, actual_with_quotes) |
| 749 << "Failed while processing: " << tests[i].header; |
| 750 EXPECT_EQ(tests[i].expected_without_quotes, actual_without_quotes) |
| 751 << "Failed while processing: " << tests[i].header; |
| 752 } |
| 753 } |
| 754 |
727 TEST(NetUtilTest, GetFileNameFromCD) { | 755 TEST(NetUtilTest, GetFileNameFromCD) { |
728 const FileNameCDCase tests[] = { | 756 const FileNameCDCase tests[] = { |
729 // Test various forms of C-D header fields emitted by web servers. | 757 // Test various forms of C-D header fields emitted by web servers. |
730 {"content-disposition: inline; filename=\"abcde.pdf\"", "", L"abcde.pdf"}, | 758 {"content-disposition: inline; filename=\"abcde.pdf\"", "", L"abcde.pdf"}, |
731 {"content-disposition: inline; name=\"abcde.pdf\"", "", L"abcde.pdf"}, | 759 {"content-disposition: inline; name=\"abcde.pdf\"", "", L"abcde.pdf"}, |
732 {"content-disposition: attachment; filename=abcde.pdf", "", L"abcde.pdf"}, | 760 {"content-disposition: attachment; filename=abcde.pdf", "", L"abcde.pdf"}, |
733 {"content-disposition: attachment; name=abcde.pdf", "", L"abcde.pdf"}, | 761 {"content-disposition: attachment; name=abcde.pdf", "", L"abcde.pdf"}, |
734 {"content-disposition: attachment; filename=abc,de.pdf", "", L"abc,de.pdf"}, | 762 {"content-disposition: attachment; filename=abc,de.pdf", "", L"abc,de.pdf"}, |
735 {"content-disposition: filename=abcde.pdf", "", L"abcde.pdf"}, | 763 {"content-disposition: filename=abcde.pdf", "", L"abcde.pdf"}, |
736 {"content-disposition: filename= abcde.pdf", "", L"abcde.pdf"}, | 764 {"content-disposition: filename= abcde.pdf", "", L"abcde.pdf"}, |
(...skipping 18 matching lines...) Expand all Loading... |
755 {"Content-Disposition: attachment; filename=\"=?EUC-JP?Q?=B7=DD=BD=" | 783 {"Content-Disposition: attachment; filename=\"=?EUC-JP?Q?=B7=DD=BD=" |
756 "D13=2Epng?=\"", "", L"\x82b8\x8853" L"3.png"}, | 784 "D13=2Epng?=\"", "", L"\x82b8\x8853" L"3.png"}, |
757 {"Content-Disposition: attachment; filename==?eUc-Kr?b?v7m8+iAzLnBuZw==?=", | 785 {"Content-Disposition: attachment; filename==?eUc-Kr?b?v7m8+iAzLnBuZw==?=", |
758 "", L"\xc608\xc220 3.png"}, | 786 "", L"\xc608\xc220 3.png"}, |
759 {"Content-Disposition: attachment; filename==?utf-8?Q?=E8=8A=B8=E8" | 787 {"Content-Disposition: attachment; filename==?utf-8?Q?=E8=8A=B8=E8" |
760 "=A1=93_3=2Epng?=", "", L"\x82b8\x8853 3.png"}, | 788 "=A1=93_3=2Epng?=", "", L"\x82b8\x8853 3.png"}, |
761 {"Content-Disposition: attachment; filename==?utf-8?Q?=F0=90=8C=B0" | 789 {"Content-Disposition: attachment; filename==?utf-8?Q?=F0=90=8C=B0" |
762 "_3=2Epng?=", "", L"\U00010330 3.png"}, | 790 "_3=2Epng?=", "", L"\U00010330 3.png"}, |
763 {"Content-Disposition: inline; filename=\"=?iso88591?Q?caf=e9_=2epng?=\"", | 791 {"Content-Disposition: inline; filename=\"=?iso88591?Q?caf=e9_=2epng?=\"", |
764 "", L"caf\x00e9 .png"}, | 792 "", L"caf\x00e9 .png"}, |
765 // Space after an encode word should be removed. | 793 // Space after an encoded word should be removed. |
766 {"Content-Disposition: inline; filename=\"=?iso88591?Q?caf=E9_?= .png\"", | 794 {"Content-Disposition: inline; filename=\"=?iso88591?Q?caf=E9_?= .png\"", |
767 "", L"caf\x00e9 .png"}, | 795 "", L"caf\x00e9 .png"}, |
768 // Two encoded words with different charsets (not very likely to be emitted | 796 // Two encoded words with different charsets (not very likely to be emitted |
769 // by web servers in the wild). Spaces between them are removed. | 797 // by web servers in the wild). Spaces between them are removed. |
770 {"Content-Disposition: inline; filename=\"=?euc-kr?b?v7m8+iAz?=" | 798 {"Content-Disposition: inline; filename=\"=?euc-kr?b?v7m8+iAz?=" |
771 " =?ksc5601?q?=BF=B9=BC=FA=2Epng?=\"", "", | 799 " =?ksc5601?q?=BF=B9=BC=FA=2Epng?=\"", "", |
772 L"\xc608\xc220 3\xc608\xc220.png"}, | 800 L"\xc608\xc220 3\xc608\xc220.png"}, |
773 {"Content-Disposition: attachment; filename=\"=?windows-1252?Q?caf=E9?=" | 801 {"Content-Disposition: attachment; filename=\"=?windows-1252?Q?caf=E9?=" |
774 " =?iso-8859-7?b?4eI=?= .png\"", "", L"caf\x00e9\x03b1\x03b2.png"}, | 802 " =?iso-8859-7?b?4eI=?= .png\"", "", L"caf\x00e9\x03b1\x03b2.png"}, |
775 // Non-ASCII string is passed through and treated as UTF-8 as long as | 803 // Non-ASCII string is passed through and treated as UTF-8 as long as |
(...skipping 29 matching lines...) Expand all Loading... |
805 "", L""}, | 833 "", L""}, |
806 // Incomplete hex-escaped chars | 834 // Incomplete hex-escaped chars |
807 {"Content-Disposition: attachment; filename==?windows-1252?Q?=63=61=E?=", | 835 {"Content-Disposition: attachment; filename==?windows-1252?Q?=63=61=E?=", |
808 "", L""}, | 836 "", L""}, |
809 {"Content-Disposition: attachment; filename=%EC%98%88%EC%88%A", "", L""}, | 837 {"Content-Disposition: attachment; filename=%EC%98%88%EC%88%A", "", L""}, |
810 // %-escaped non-UTF-8 encoding is an "error" | 838 // %-escaped non-UTF-8 encoding is an "error" |
811 {"Content-Disposition: attachment; filename=%B7%DD%BD%D1.png", "", L""}, | 839 {"Content-Disposition: attachment; filename=%B7%DD%BD%D1.png", "", L""}, |
812 // Two RFC 2047 encoded words in a row without a space is an error. | 840 // Two RFC 2047 encoded words in a row without a space is an error. |
813 {"Content-Disposition: attachment; filename==?windows-1252?Q?caf=E3?=" | 841 {"Content-Disposition: attachment; filename==?windows-1252?Q?caf=E3?=" |
814 "=?iso-8859-7?b?4eIucG5nCg==?=", "", L""}, | 842 "=?iso-8859-7?b?4eIucG5nCg==?=", "", L""}, |
| 843 |
| 844 // RFC 5987 tests with Filename* : see http://tools.ietf.org/html/rfc5987 |
| 845 {"Content-Disposition: attachment; filename*=foo.html", "", L""}, |
| 846 {"Content-Disposition: attachment; filename*=foo'.html", "", L""}, |
| 847 {"Content-Disposition: attachment; filename*=''foo'.html", "", L""}, |
| 848 {"Content-Disposition: attachment; filename*=''foo.html'", "", L""}, |
| 849 {"Content-Disposition: attachment; filename*=''f\"oo\".html'", "", L""}, |
| 850 {"Content-Disposition: attachment; filename*=bogus_charset''foo.html'", |
| 851 "", L""}, |
| 852 {"Content-Disposition: attachment; filename*='en'foo.html'", "", L""}, |
| 853 {"Content-Disposition: attachment; filename*=iso-8859-1'en'foo.html", "", |
| 854 L"foo.html"}, |
| 855 {"Content-Disposition: attachment; filename*=utf-8'en'foo.html", "", |
| 856 L"foo.html"}, |
| 857 // charset cannot be omitted. |
| 858 {"Content-Disposition: attachment; filename*='es'f\xfa.html'", "", L""}, |
| 859 // Non-ASCII bytes are not allowed. |
| 860 {"Content-Disposition: attachment; filename*=iso-8859-1'es'f\xfa.html", "", |
| 861 L""}, |
| 862 {"Content-Disposition: attachment; filename*=utf-8'es'f\xce\xba.html", "", |
| 863 L""}, |
| 864 // TODO(jshin): Space should be %-encoded, but currently, we allow |
| 865 // spaces. |
| 866 {"Content-Disposition: inline; filename*=iso88591''cafe foo.png", "", |
| 867 L"cafe foo.png"}, |
| 868 |
| 869 // Filename* tests converted from Q-encoded tests above. |
| 870 {"Content-Disposition: attachment; filename*=EUC-JP''%B7%DD%BD%D13%2Epng", |
| 871 "", L"\x82b8\x8853" L"3.png"}, |
| 872 {"Content-Disposition: attachment; filename*=utf-8''" |
| 873 "%E8%8A%B8%E8%A1%93%203%2Epng", "", L"\x82b8\x8853 3.png"}, |
| 874 {"Content-Disposition: attachment; filename*=utf-8''%F0%90%8C%B0 3.png", "", |
| 875 L"\U00010330 3.png"}, |
| 876 {"Content-Disposition: inline; filename*=Euc-Kr'ko'%BF%B9%BC%FA%2Epng", "", |
| 877 L"\xc608\xc220.png"}, |
| 878 {"Content-Disposition: attachment; filename*=windows-1252''caf%E9.png", "", |
| 879 L"caf\x00e9.png"}, |
| 880 |
| 881 // http://greenbytes.de/tech/tc2231/ filename* test cases. |
| 882 // attwithisofn2231iso |
| 883 {"Content-Disposition: attachment; filename*=iso-8859-1''foo-%E4.html", "", |
| 884 L"foo-\xe4.html"}, |
| 885 // attwithfn2231utf8 |
| 886 {"Content-Disposition: attachment; filename*=" |
| 887 "UTF-8''foo-%c3%a4-%e2%82%ac.html", "", L"foo-\xe4-\x20ac.html"}, |
| 888 // attwithfn2231noc : no encoding specified but UTF-8 is used. |
| 889 {"Content-Disposition: attachment; filename*=''foo-%c3%a4-%e2%82%ac.html", |
| 890 "", L""}, |
| 891 // attwithfn2231utf8comp |
| 892 {"Content-Disposition: attachment; filename*=UTF-8''foo-a%cc%88.html", "", |
| 893 L"foo-\xe4.html"}, |
| 894 #ifdef ICU_SHOULD_FAIL_CONVERSION_ON_INVALID_CHARACTER |
| 895 // attwithfn2231utf8-bad |
| 896 {"Content-Disposition: attachment; filename*=" |
| 897 "iso-8859-1''foo-%c3%a4-%e2%82%ac.html", "", L""}, |
| 898 #endif |
| 899 // attwithfn2231ws1 |
| 900 {"Content-Disposition: attachment; filename *=UTF-8''foo-%c3%a4.html", "", |
| 901 L""}, |
| 902 // attwithfn2231ws2 |
| 903 {"Content-Disposition: attachment; filename*= UTF-8''foo-%c3%a4.html", "", |
| 904 L"foo-\xe4.html"}, |
| 905 // attwithfn2231ws3 |
| 906 {"Content-Disposition: attachment; filename* =UTF-8''foo-%c3%a4.html", "", |
| 907 L"foo-\xe4.html"}, |
| 908 // attwithfn2231quot |
| 909 {"Content-Disposition: attachment; filename*=\"UTF-8''foo-%c3%a4.html\"", |
| 910 "", L""}, |
| 911 // attfnboth |
| 912 {"Content-Disposition: attachment; filename=\"foo-ae.html\"; " |
| 913 "filename*=UTF-8''foo-%c3%a4.html", "", L"foo-\xe4.html"}, |
| 914 // attfnboth2 |
| 915 {"Content-Disposition: attachment; filename*=UTF-8''foo-%c3%a4.html; " |
| 916 "filename=\"foo-ae.html\"", "", L"foo-\xe4.html"}, |
| 917 // attnewandfn |
| 918 {"Content-Disposition: attachment; foobar=x; filename=\"foo.html\"", "", |
| 919 L"foo.html"}, |
815 }; | 920 }; |
816 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) { | 921 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) { |
817 EXPECT_EQ(tests[i].expected, | 922 EXPECT_EQ(tests[i].expected, |
818 UTF8ToWide(net::GetFileNameFromCD(tests[i].header_field, | 923 UTF8ToWide(net::GetFileNameFromCD(tests[i].header_field, |
819 tests[i].referrer_charset))); | 924 tests[i].referrer_charset))) |
| 925 << "Failed on input: " << tests[i].header_field; |
820 } | 926 } |
821 } | 927 } |
822 | 928 |
823 TEST(NetUtilTest, IDNToUnicodeFast) { | 929 TEST(NetUtilTest, IDNToUnicodeFast) { |
824 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(idn_cases); i++) { | 930 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(idn_cases); i++) { |
825 for (size_t j = 0; j < arraysize(kLanguages); j++) { | 931 for (size_t j = 0; j < arraysize(kLanguages); j++) { |
826 // ja || zh-TW,en || ko,ja -> IDNToUnicodeSlow | 932 // ja || zh-TW,en || ko,ja -> IDNToUnicodeSlow |
827 if (j == 3 || j == 17 || j == 18) | 933 if (j == 3 || j == 17 || j == 18) |
828 continue; | 934 continue; |
829 std::wstring output(net::IDNToUnicode(idn_cases[i].input, | 935 std::wstring output(net::IDNToUnicode(idn_cases[i].input, |
(...skipping 1223 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2053 EXPECT_TRUE(net::ParseCIDRBlock(tests[i].cidr_literal, | 2159 EXPECT_TRUE(net::ParseCIDRBlock(tests[i].cidr_literal, |
2054 &ip_prefix, | 2160 &ip_prefix, |
2055 &prefix_length_in_bits)); | 2161 &prefix_length_in_bits)); |
2056 | 2162 |
2057 EXPECT_EQ(tests[i].expected_to_match, | 2163 EXPECT_EQ(tests[i].expected_to_match, |
2058 net::IPNumberMatchesPrefix(ip_number, | 2164 net::IPNumberMatchesPrefix(ip_number, |
2059 ip_prefix, | 2165 ip_prefix, |
2060 prefix_length_in_bits)); | 2166 prefix_length_in_bits)); |
2061 } | 2167 } |
2062 } | 2168 } |
OLD | NEW |