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

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

Issue 367003: Enable localization of default downloaded filename. (Closed)
Patch Set: Created 11 years, 1 month 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.cc ('k') | webkit/glue/weburlloader_impl.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) 2006-2008 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2006-2008 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 "base/basictypes.h" 5 #include "base/basictypes.h"
6 #include "base/file_path.h" 6 #include "base/file_path.h"
7 #include "base/string_util.h" 7 #include "base/string_util.h"
8 #include "base/sys_string_conversions.h"
8 #include "base/time.h" 9 #include "base/time.h"
9 #include "googleurl/src/gurl.h" 10 #include "googleurl/src/gurl.h"
10 #include "net/base/escape.h" 11 #include "net/base/escape.h"
11 #include "net/base/net_util.h" 12 #include "net/base/net_util.h"
12 #include "testing/gtest/include/gtest/gtest.h" 13 #include "testing/gtest/include/gtest/gtest.h"
13 14
14 #if defined(OS_WIN) 15 #if defined(OS_WIN)
15 #include <ws2tcpip.h> 16 #include <ws2tcpip.h>
16 #else 17 #else
17 #include <netdb.h> 18 #include <netdb.h>
(...skipping 328 matching lines...) Expand 10 before | Expand all | Expand 10 after
346 347
347 struct CompliantHostCase { 348 struct CompliantHostCase {
348 const char* host; 349 const char* host;
349 bool expected_output; 350 bool expected_output;
350 }; 351 };
351 352
352 struct SuggestedFilenameCase { 353 struct SuggestedFilenameCase {
353 const char* url; 354 const char* url;
354 const char* content_disp_header; 355 const char* content_disp_header;
355 const char* referrer_charset; 356 const char* referrer_charset;
356 const char* default_filename; 357 const wchar_t* default_filename;
357 const wchar_t* expected_filename; 358 const wchar_t* expected_filename;
358 }; 359 };
359 360
360 struct UrlTestData { 361 struct UrlTestData {
361 const char* description; 362 const char* description;
362 const char* input; 363 const char* input;
363 const std::wstring languages; 364 const std::wstring languages;
364 bool omit; 365 bool omit;
365 UnescapeRule::Type escape_rules; 366 UnescapeRule::Type escape_rules;
366 const std::wstring output; 367 const std::wstring output;
(...skipping 482 matching lines...) Expand 10 before | Expand all | Expand 10 after
849 EXPECT_EQ(L"", net::StripWWW(L"www.")); 850 EXPECT_EQ(L"", net::StripWWW(L"www."));
850 EXPECT_EQ(L"blah", net::StripWWW(L"www.blah")); 851 EXPECT_EQ(L"blah", net::StripWWW(L"www.blah"));
851 EXPECT_EQ(L"blah", net::StripWWW(L"blah")); 852 EXPECT_EQ(L"blah", net::StripWWW(L"blah"));
852 } 853 }
853 854
854 TEST(NetUtilTest, GetSuggestedFilename) { 855 TEST(NetUtilTest, GetSuggestedFilename) {
855 const SuggestedFilenameCase test_cases[] = { 856 const SuggestedFilenameCase test_cases[] = {
856 {"http://www.google.com/", 857 {"http://www.google.com/",
857 "Content-disposition: attachment; filename=test.html", 858 "Content-disposition: attachment; filename=test.html",
858 "", 859 "",
859 "", 860 L"",
860 L"test.html"}, 861 L"test.html"},
861 {"http://www.google.com/", 862 {"http://www.google.com/",
862 "Content-disposition: attachment; filename=\"test.html\"", 863 "Content-disposition: attachment; filename=\"test.html\"",
863 "", 864 "",
864 "", 865 L"",
865 L"test.html"}, 866 L"test.html"},
866 {"http://www.google.com/path/test.html", 867 {"http://www.google.com/path/test.html",
867 "Content-disposition: attachment", 868 "Content-disposition: attachment",
868 "", 869 "",
869 "", 870 L"",
870 L"test.html"}, 871 L"test.html"},
871 {"http://www.google.com/path/test.html", 872 {"http://www.google.com/path/test.html",
872 "Content-disposition: attachment;", 873 "Content-disposition: attachment;",
873 "", 874 "",
874 "", 875 L"",
875 L"test.html"}, 876 L"test.html"},
876 {"http://www.google.com/", 877 {"http://www.google.com/",
877 "", 878 "",
878 "", 879 "",
879 "", 880 L"",
880 L"www.google.com"}, 881 L"www.google.com"},
881 {"http://www.google.com/test.html", 882 {"http://www.google.com/test.html",
882 "", 883 "",
883 "", 884 "",
884 "", 885 L"",
885 L"test.html"}, 886 L"test.html"},
886 // Now that we use googleurl's ExtractFileName, this case falls back 887 // Now that we use googleurl's ExtractFileName, this case falls back
887 // to the hostname. If this behavior is not desirable, we'd better 888 // to the hostname. If this behavior is not desirable, we'd better
888 // change ExtractFileName (in url_parse). 889 // change ExtractFileName (in url_parse).
889 {"http://www.google.com/path/", 890 {"http://www.google.com/path/",
890 "", 891 "",
891 "", 892 "",
892 "", 893 L"",
893 L"www.google.com"}, 894 L"www.google.com"},
894 {"http://www.google.com/path", 895 {"http://www.google.com/path",
895 "", 896 "",
896 "", 897 "",
897 "", 898 L"",
898 L"path"}, 899 L"path"},
899 {"file:///", 900 {"file:///",
900 "", 901 "",
901 "", 902 "",
902 "", 903 L"",
903 L"download"}, 904 L"download"},
904 {"non-standard-scheme:", 905 {"non-standard-scheme:",
905 "", 906 "",
906 "", 907 "",
907 "", 908 L"",
908 L"download"}, 909 L"download"},
909 {"http://www.google.com/", 910 {"http://www.google.com/",
910 "Content-disposition: attachment; filename =\"test.html\"", 911 "Content-disposition: attachment; filename =\"test.html\"",
911 "", 912 "",
912 "download", 913 L"download",
913 L"test.html"}, 914 L"test.html"},
914 {"http://www.google.com/", 915 {"http://www.google.com/",
915 "", 916 "",
916 "", 917 "",
917 "download", 918 L"download",
918 L"download"}, 919 L"download"},
919 {"http://www.google.com/", 920 {"http://www.google.com/",
920 "Content-disposition: attachment; filename=\"../test.html\"", 921 "Content-disposition: attachment; filename=\"../test.html\"",
921 "", 922 "",
922 "", 923 L"",
923 L"test.html"}, 924 L"test.html"},
924 {"http://www.google.com/", 925 {"http://www.google.com/",
925 "Content-disposition: attachment; filename=\"..\"", 926 "Content-disposition: attachment; filename=\"..\"",
926 "", 927 "",
927 "download", 928 L"download",
928 L"download"}, 929 L"download"},
929 {"http://www.google.com/test.html", 930 {"http://www.google.com/test.html",
930 "Content-disposition: attachment; filename=\"..\"", 931 "Content-disposition: attachment; filename=\"..\"",
931 "", 932 "",
932 "download", 933 L"download",
933 L"test.html"}, 934 L"test.html"},
934 // Below is a small subset of cases taken from GetFileNameFromCD test above. 935 // Below is a small subset of cases taken from GetFileNameFromCD test above.
935 {"http://www.google.com/", 936 {"http://www.google.com/",
936 "Content-Disposition: attachment; filename=\"%EC%98%88%EC%88%A0%20" 937 "Content-Disposition: attachment; filename=\"%EC%98%88%EC%88%A0%20"
937 "%EC%98%88%EC%88%A0.jpg\"", 938 "%EC%98%88%EC%88%A0.jpg\"",
938 "", 939 "",
939 "", 940 L"",
940 L"\uc608\uc220 \uc608\uc220.jpg"}, 941 L"\uc608\uc220 \uc608\uc220.jpg"},
941 {"http://www.google.com/%EC%98%88%EC%88%A0%20%EC%98%88%EC%88%A0.jpg", 942 {"http://www.google.com/%EC%98%88%EC%88%A0%20%EC%98%88%EC%88%A0.jpg",
942 "", 943 "",
943 "", 944 "",
944 "download", 945 L"download",
945 L"\uc608\uc220 \uc608\uc220.jpg"}, 946 L"\uc608\uc220 \uc608\uc220.jpg"},
946 {"http://www.google.com/", 947 {"http://www.google.com/",
947 "Content-disposition: attachment;", 948 "Content-disposition: attachment;",
948 "", 949 "",
949 "\xEB\x8B\xA4\xEC\x9A\xB4\xEB\xA1\x9C\xEB\x93\x9C", 950 L"\uB2E4\uC6B4\uB85C\uB4DC",
950 L"\uB2E4\uC6B4\uB85C\uB4DC"}, 951 L"\uB2E4\uC6B4\uB85C\uB4DC"},
951 {"http://www.google.com/", 952 {"http://www.google.com/",
952 "Content-Disposition: attachment; filename=\"=?EUC-JP?Q?=B7=DD=BD=" 953 "Content-Disposition: attachment; filename=\"=?EUC-JP?Q?=B7=DD=BD="
953 "D13=2Epng?=\"", 954 "D13=2Epng?=\"",
954 "", 955 "",
955 "download", 956 L"download",
956 L"\u82b8\u88533.png"}, 957 L"\u82b8\u88533.png"},
957 {"http://www.example.com/images?id=3", 958 {"http://www.example.com/images?id=3",
958 "Content-Disposition: attachment; filename=caf\xc3\xa9.png", 959 "Content-Disposition: attachment; filename=caf\xc3\xa9.png",
959 "iso-8859-1", 960 "iso-8859-1",
960 "", 961 L"",
961 L"caf\u00e9.png"}, 962 L"caf\u00e9.png"},
962 {"http://www.example.com/images?id=3", 963 {"http://www.example.com/images?id=3",
963 "Content-Disposition: attachment; filename=caf\xe5.png", 964 "Content-Disposition: attachment; filename=caf\xe5.png",
964 "windows-1253", 965 "windows-1253",
965 "", 966 L"",
966 L"caf\u03b5.png"}, 967 L"caf\u03b5.png"},
967 {"http://www.example.com/file?id=3", 968 {"http://www.example.com/file?id=3",
968 "Content-Disposition: attachment; name=\xcf\xc2\xd4\xd8.zip", 969 "Content-Disposition: attachment; name=\xcf\xc2\xd4\xd8.zip",
969 "GBK", 970 "GBK",
970 "", 971 L"",
971 L"\u4e0b\u8f7d.zip"}, 972 L"\u4e0b\u8f7d.zip"},
972 // Invalid C-D header. Extracts filename from url. 973 // Invalid C-D header. Extracts filename from url.
973 {"http://www.google.com/test.html", 974 {"http://www.google.com/test.html",
974 "Content-Disposition: attachment; filename==?iiso88591?Q?caf=EG?=", 975 "Content-Disposition: attachment; filename==?iiso88591?Q?caf=EG?=",
975 "", 976 "",
976 "", 977 L"",
977 L"test.html"}, 978 L"test.html"},
978 // about: and data: URLs 979 // about: and data: URLs
979 {"about:chrome", 980 {"about:chrome",
980 "", 981 "",
981 "", 982 "",
982 "", 983 L"",
983 L"download"}, 984 L"download"},
984 {"data:,looks/like/a.path", 985 {"data:,looks/like/a.path",
985 "", 986 "",
986 "", 987 "",
987 "", 988 L"",
988 L"download"}, 989 L"download"},
989 {"data:text/plain;base64,VG8gYmUgb3Igbm90IHRvIGJlLg=", 990 {"data:text/plain;base64,VG8gYmUgb3Igbm90IHRvIGJlLg=",
990 "", 991 "",
991 "", 992 "",
993 L"",
994 L"download"},
995 {"data:,looks/like/a.path",
992 "", 996 "",
993 L"download"}, 997 "",
998 L"default_filename_is_given",
999 L"default_filename_is_given"},
1000 {"data:,looks/like/a.path",
1001 "",
1002 "",
1003 L"\u65e5\u672c\u8a9e", // Japanese Kanji.
1004 L"\u65e5\u672c\u8a9e"},
994 }; 1005 };
995 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(test_cases); ++i) { 1006 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(test_cases); ++i) {
1007 #if defined(OS_WIN)
1008 FilePath default_name(test_cases[i].default_filename);
1009 #else
1010 FilePath default_name(
1011 base::SysWideToNativeMB(test_cases[i].default_filename));
1012 #endif
996 FilePath filename = net::GetSuggestedFilename( 1013 FilePath filename = net::GetSuggestedFilename(
997 GURL(test_cases[i].url), test_cases[i].content_disp_header, 1014 GURL(test_cases[i].url), test_cases[i].content_disp_header,
998 test_cases[i].referrer_charset, test_cases[i].default_filename); 1015 test_cases[i].referrer_charset, default_name);
999 #if defined(OS_WIN) 1016 #if defined(OS_WIN)
1000 EXPECT_EQ(std::wstring(test_cases[i].expected_filename), filename.value()) 1017 EXPECT_EQ(std::wstring(test_cases[i].expected_filename), filename.value())
1001 #else 1018 #else
1002 EXPECT_EQ(WideToUTF8(test_cases[i].expected_filename), filename.value()) 1019 EXPECT_EQ(base::SysWideToNativeMB(test_cases[i].expected_filename),
1020 filename.value())
1003 #endif 1021 #endif
1004 << "Iteration " << i << ": " << test_cases[i].url; 1022 << "Iteration " << i << ": " << test_cases[i].url;
1005 } 1023 }
1006 } 1024 }
1007 1025
1008 // This is currently a windows specific function. 1026 // This is currently a windows specific function.
1009 #if defined(OS_WIN) 1027 #if defined(OS_WIN)
1010 namespace { 1028 namespace {
1011 1029
1012 struct GetDirectoryListingEntryCase { 1030 struct GetDirectoryListingEntryCase {
(...skipping 429 matching lines...) Expand 10 before | Expand all | Expand 10 after
1442 net::SetExplicitlyAllowedPorts(invalid[i]); 1460 net::SetExplicitlyAllowedPorts(invalid[i]);
1443 EXPECT_EQ(0, static_cast<int>(net::explicitly_allowed_ports.size())); 1461 EXPECT_EQ(0, static_cast<int>(net::explicitly_allowed_ports.size()));
1444 } 1462 }
1445 1463
1446 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(valid); ++i) { 1464 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(valid); ++i) {
1447 net::SetExplicitlyAllowedPorts(valid[i]); 1465 net::SetExplicitlyAllowedPorts(valid[i]);
1448 EXPECT_EQ(i, net::explicitly_allowed_ports.size()); 1466 EXPECT_EQ(i, net::explicitly_allowed_ports.size());
1449 } 1467 }
1450 } 1468 }
1451 1469
OLDNEW
« no previous file with comments | « net/base/net_util.cc ('k') | webkit/glue/weburlloader_impl.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698