1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "net/base/filename_util.h"
7 #include "base/files/file_path.h"
8 #include "base/files/file_util.h"
9 #include "base/strings/string_util.h"
10 #include "base/strings/utf_string_conversions.h"
11 #include "base/test/test_file_util.h"
12 #include "testing/gtest/include/gtest/gtest.h"
24 struct GenerateFilenameCase
{
27 const char* content_disp_header
;
28 const char* referrer_charset
;
29 const char* suggested_filename
;
30 const char* mime_type
;
31 const wchar_t* default_filename
;
32 const wchar_t* expected_filename
;
35 // The expected filenames are coded as wchar_t for convenience.
36 std::wstring
FilePathAsWString(const base::FilePath
& path
) {
40 return base::UTF8ToWide(path
.value());
43 base::FilePath
WStringAsFilePath(const std::wstring
& str
) {
45 return base::FilePath(str
);
47 return base::FilePath(base::WideToUTF8(str
));
51 void RunGenerateFileNameTestCase(const GenerateFilenameCase
* test_case
) {
52 std::string
default_filename(base::WideToUTF8(test_case
->default_filename
));
53 base::FilePath file_path
= GenerateFileName(
54 GURL(test_case
->url
), test_case
->content_disp_header
,
55 test_case
->referrer_charset
, test_case
->suggested_filename
,
56 test_case
->mime_type
, default_filename
);
57 EXPECT_EQ(test_case
->expected_filename
, FilePathAsWString(file_path
))
58 << "test case at line number: " << test_case
->lineno
;
63 static const base::FilePath::CharType
* kSafePortableBasenames
[] = {
64 FILE_PATH_LITERAL("a"),
65 FILE_PATH_LITERAL("a.txt"),
66 FILE_PATH_LITERAL("a b.txt"),
67 FILE_PATH_LITERAL("a-b.txt"),
68 FILE_PATH_LITERAL("My Computer"),
69 FILE_PATH_LITERAL(" Computer"),
72 static const base::FilePath::CharType
* kUnsafePortableBasenames
[] = {
73 FILE_PATH_LITERAL(""),
74 FILE_PATH_LITERAL("."),
75 FILE_PATH_LITERAL(".."),
76 FILE_PATH_LITERAL("..."),
77 FILE_PATH_LITERAL("con"),
78 FILE_PATH_LITERAL("con.zip"),
79 FILE_PATH_LITERAL("NUL"),
80 FILE_PATH_LITERAL("NUL.zip"),
81 FILE_PATH_LITERAL(".a"),
82 FILE_PATH_LITERAL("a."),
83 FILE_PATH_LITERAL("a\"a"),
84 FILE_PATH_LITERAL("a<a"),
85 FILE_PATH_LITERAL("a>a"),
86 FILE_PATH_LITERAL("a?a"),
87 FILE_PATH_LITERAL("a/"),
88 FILE_PATH_LITERAL("a\\"),
89 FILE_PATH_LITERAL("a "),
90 FILE_PATH_LITERAL("a . ."),
91 FILE_PATH_LITERAL("My Computer.{a}"),
92 FILE_PATH_LITERAL("My Computer.{20D04FE0-3AEA-1069-A2D8-08002B30309D}"),
94 FILE_PATH_LITERAL("a\\a"),
98 static const base::FilePath::CharType
* kSafePortableRelativePaths
[] = {
99 FILE_PATH_LITERAL("a/a"),
101 FILE_PATH_LITERAL("a\\a"),
105 TEST(FilenameUtilTest
, IsSafePortablePathComponent
) {
106 for (size_t i
= 0 ; i
< arraysize(kSafePortableBasenames
); ++i
) {
107 EXPECT_TRUE(IsSafePortablePathComponent(base::FilePath(
108 kSafePortableBasenames
[i
]))) << kSafePortableBasenames
[i
];
110 for (size_t i
= 0 ; i
< arraysize(kUnsafePortableBasenames
); ++i
) {
111 EXPECT_FALSE(IsSafePortablePathComponent(base::FilePath(
112 kUnsafePortableBasenames
[i
]))) << kUnsafePortableBasenames
[i
];
114 for (size_t i
= 0 ; i
< arraysize(kSafePortableRelativePaths
); ++i
) {
115 EXPECT_FALSE(IsSafePortablePathComponent(base::FilePath(
116 kSafePortableRelativePaths
[i
]))) << kSafePortableRelativePaths
[i
];
120 TEST(FilenameUtilTest
, IsSafePortableRelativePath
) {
121 base::FilePath
safe_dirname(FILE_PATH_LITERAL("a"));
122 for (size_t i
= 0 ; i
< arraysize(kSafePortableBasenames
); ++i
) {
123 EXPECT_TRUE(IsSafePortableRelativePath(base::FilePath(
124 kSafePortableBasenames
[i
]))) << kSafePortableBasenames
[i
];
125 EXPECT_TRUE(IsSafePortableRelativePath(safe_dirname
.Append(base::FilePath(
126 kSafePortableBasenames
[i
])))) << kSafePortableBasenames
[i
];
128 for (size_t i
= 0 ; i
< arraysize(kSafePortableRelativePaths
); ++i
) {
129 EXPECT_TRUE(IsSafePortableRelativePath(base::FilePath(
130 kSafePortableRelativePaths
[i
]))) << kSafePortableRelativePaths
[i
];
131 EXPECT_TRUE(IsSafePortableRelativePath(safe_dirname
.Append(base::FilePath(
132 kSafePortableRelativePaths
[i
])))) << kSafePortableRelativePaths
[i
];
134 for (size_t i
= 0 ; i
< arraysize(kUnsafePortableBasenames
); ++i
) {
135 EXPECT_FALSE(IsSafePortableRelativePath(base::FilePath(
136 kUnsafePortableBasenames
[i
]))) << kUnsafePortableBasenames
[i
];
137 if (!base::FilePath::StringType(kUnsafePortableBasenames
[i
]).empty()) {
138 EXPECT_FALSE(IsSafePortableRelativePath(safe_dirname
.Append(
139 base::FilePath(kUnsafePortableBasenames
[i
]))))
140 << kUnsafePortableBasenames
[i
];
145 TEST(FilenameUtilTest
, FileURLConversion
) {
146 // a list of test file names and the corresponding URLs
147 const FileCase round_trip_cases
[] = {
149 {L
"C:\\foo\\bar.txt", "file:///C:/foo/bar.txt"},
150 {L
"\\\\some computer\\foo\\bar.txt",
151 "file://some%20computer/foo/bar.txt"}, // UNC
152 {L
"D:\\Name;with%some symbols*#",
153 "file:///D:/Name%3Bwith%25some%20symbols*%23"},
154 // issue 14153: To be tested with the OS default codepage other than 1252.
155 {L
"D:\\latin1\\caf\x00E9\x00DD.txt",
156 "file:///D:/latin1/caf%C3%A9%C3%9D.txt"},
157 {L
"D:\\otherlatin\\caf\x0119.txt",
158 "file:///D:/otherlatin/caf%C4%99.txt"},
159 {L
"D:\\greek\\\x03B1\x03B2\x03B3.txt",
160 "file:///D:/greek/%CE%B1%CE%B2%CE%B3.txt"},
161 {L
"D:\\Chinese\\\x6240\x6709\x4e2d\x6587\x7f51\x9875.doc",
162 "file:///D:/Chinese/%E6%89%80%E6%9C%89%E4%B8%AD%E6%96%87%E7%BD%91"
164 {L
"D:\\plane1\\\xD835\xDC00\xD835\xDC01.txt", // Math alphabet "AB"
165 "file:///D:/plane1/%F0%9D%90%80%F0%9D%90%81.txt"},
166 #elif defined(OS_POSIX)
167 {L
"/foo/bar.txt", "file:///foo/bar.txt"},
168 {L
"/foo/BAR.txt", "file:///foo/BAR.txt"},
169 {L
"/C:/foo/bar.txt", "file:///C:/foo/bar.txt"},
170 {L
"/foo/bar?.txt", "file:///foo/bar%3F.txt"},
171 {L
"/some computer/foo/bar.txt", "file:///some%20computer/foo/bar.txt"},
172 {L
"/Name;with%some symbols*#", "file:///Name%3Bwith%25some%20symbols*%23"},
173 {L
"/latin1/caf\x00E9\x00DD.txt", "file:///latin1/caf%C3%A9%C3%9D.txt"},
174 {L
"/otherlatin/caf\x0119.txt", "file:///otherlatin/caf%C4%99.txt"},
175 {L
"/greek/\x03B1\x03B2\x03B3.txt", "file:///greek/%CE%B1%CE%B2%CE%B3.txt"},
176 {L
"/Chinese/\x6240\x6709\x4e2d\x6587\x7f51\x9875.doc",
177 "file:///Chinese/%E6%89%80%E6%9C%89%E4%B8%AD%E6%96%87%E7%BD"
179 {L
"/plane1/\x1D400\x1D401.txt", // Math alphabet "AB"
180 "file:///plane1/%F0%9D%90%80%F0%9D%90%81.txt"},
184 // First, we'll test that we can round-trip all of the above cases of URLs
185 base::FilePath output
;
186 for (size_t i
= 0; i
< arraysize(round_trip_cases
); i
++) {
187 // convert to the file URL
188 GURL
file_url(FilePathToFileURL(
189 WStringAsFilePath(round_trip_cases
[i
].file
)));
190 EXPECT_EQ(round_trip_cases
[i
].url
, file_url
.spec());
192 // Back to the filename.
193 EXPECT_TRUE(FileURLToFilePath(file_url
, &output
));
194 EXPECT_EQ(round_trip_cases
[i
].file
, FilePathAsWString(output
));
197 // Test that various file: URLs get decoded into the correct file type
198 FileCase url_cases
[] = {
200 {L
"C:\\foo\\bar.txt", "file:c|/foo\\bar.txt"},
201 {L
"C:\\foo\\bar.txt", "file:/c:/foo/bar.txt"},
202 {L
"\\\\foo\\bar.txt", "file://foo\\bar.txt"},
203 {L
"C:\\foo\\bar.txt", "file:///c:/foo/bar.txt"},
204 {L
"\\\\foo\\bar.txt", "file:////foo\\bar.txt"},
205 {L
"\\\\foo\\bar.txt", "file:/foo/bar.txt"},
206 {L
"\\\\foo\\bar.txt", "file://foo\\bar.txt"},
207 {L
"C:\\foo\\bar.txt", "file:\\\\\\c:/foo/bar.txt"},
208 #elif defined(OS_POSIX)
209 {L
"/c:/foo/bar.txt", "file:/c:/foo/bar.txt"},
210 {L
"/c:/foo/bar.txt", "file:///c:/foo/bar.txt"},
211 {L
"/foo/bar.txt", "file:/foo/bar.txt"},
212 {L
"/c:/foo/bar.txt", "file:\\\\\\c:/foo/bar.txt"},
213 {L
"/foo/bar.txt", "file:foo/bar.txt"},
214 {L
"/bar.txt", "file://foo/bar.txt"},
215 {L
"/foo/bar.txt", "file:///foo/bar.txt"},
216 {L
"/foo/bar.txt", "file:////foo/bar.txt"},
217 {L
"/foo/bar.txt", "file:////foo//bar.txt"},
218 {L
"/foo/bar.txt", "file:////foo///bar.txt"},
219 {L
"/foo/bar.txt", "file:////foo////bar.txt"},
220 {L
"/c:/foo/bar.txt", "file:\\\\\\c:/foo/bar.txt"},
221 {L
"/c:/foo/bar.txt", "file:c:/foo/bar.txt"},
222 // We get these wrong because GURL turns back slashes into forward
224 //{L"/foo%5Cbar.txt", "file://foo\\bar.txt"},
225 //{L"/c|/foo%5Cbar.txt", "file:c|/foo\\bar.txt"},
226 //{L"/foo%5Cbar.txt", "file://foo\\bar.txt"},
227 //{L"/foo%5Cbar.txt", "file:////foo\\bar.txt"},
228 //{L"/foo%5Cbar.txt", "file://foo\\bar.txt"},
231 for (size_t i
= 0; i
< arraysize(url_cases
); i
++) {
232 FileURLToFilePath(GURL(url_cases
[i
].url
), &output
);
233 EXPECT_EQ(url_cases
[i
].file
, FilePathAsWString(output
));
236 // Unfortunately, UTF8ToWide discards invalid UTF8 input.
237 #ifdef BUG_878908_IS_FIXED
238 // Test that no conversion happens if the UTF-8 input is invalid, and that
239 // the input is preserved in UTF-8
240 const char invalid_utf8
[] = "file:///d:/Blah/\xff.doc";
241 const wchar_t invalid_wide
[] = L
"D:\\Blah\\\xff.doc";
242 EXPECT_TRUE(FileURLToFilePath(
243 GURL(std::string(invalid_utf8
)), &output
));
244 EXPECT_EQ(std::wstring(invalid_wide
), output
);
247 // Test that if a file URL is malformed, we get a failure
248 EXPECT_FALSE(FileURLToFilePath(GURL("filefoobar"), &output
));
252 #define JPEG_EXT L".jpg"
253 #define HTML_EXT L".htm"
254 #elif defined(OS_MACOSX)
255 #define JPEG_EXT L".jpeg"
256 #define HTML_EXT L".html"
258 #define JPEG_EXT L".jpg"
259 #define HTML_EXT L".html"
261 #define TXT_EXT L".txt"
262 #define TAR_EXT L".tar"
264 TEST(FilenameUtilTest
, GenerateSafeFileName
) {
266 const char* mime_type
;
267 const base::FilePath::CharType
* filename
;
268 const base::FilePath::CharType
* expected_filename
;
273 FILE_PATH_LITERAL("C:\\foo\\bar.htm"),
274 FILE_PATH_LITERAL("C:\\foo\\bar.htm")
278 FILE_PATH_LITERAL("C:\\foo\\bar.html"),
279 FILE_PATH_LITERAL("C:\\foo\\bar.html")
283 FILE_PATH_LITERAL("C:\\foo\\bar"),
284 FILE_PATH_LITERAL("C:\\foo\\bar.htm")
288 FILE_PATH_LITERAL("C:\\bar.html"),
289 FILE_PATH_LITERAL("C:\\bar.html")
293 FILE_PATH_LITERAL("C:\\bar"),
294 FILE_PATH_LITERAL("C:\\bar.png")
298 FILE_PATH_LITERAL("C:\\foo\\bar.exe"),
299 FILE_PATH_LITERAL("C:\\foo\\bar.exe")
303 FILE_PATH_LITERAL("C:\\foo\\bar.exe"),
304 FILE_PATH_LITERAL("C:\\foo\\bar.exe")
308 FILE_PATH_LITERAL("C:\\foo\\google.com"),
309 FILE_PATH_LITERAL("C:\\foo\\google.com")
313 FILE_PATH_LITERAL("C:\\foo\\con.htm"),
314 FILE_PATH_LITERAL("C:\\foo\\_con.htm")
318 FILE_PATH_LITERAL("C:\\foo\\con"),
319 FILE_PATH_LITERAL("C:\\foo\\_con.htm")
323 FILE_PATH_LITERAL("C:\\foo\\harmless.{not-really-this-may-be-a-guid}"),
324 FILE_PATH_LITERAL("C:\\foo\\harmless.download")
328 FILE_PATH_LITERAL("C:\\foo\\harmless.local"),
329 FILE_PATH_LITERAL("C:\\foo\\harmless.download")
333 FILE_PATH_LITERAL("C:\\foo\\harmless.lnk"),
334 FILE_PATH_LITERAL("C:\\foo\\harmless.download")
338 FILE_PATH_LITERAL("C:\\foo\\harmless.{mismatched-"),
339 FILE_PATH_LITERAL("C:\\foo\\harmless.{mismatched-")
341 // Allow extension synonyms.
344 FILE_PATH_LITERAL("C:\\foo\\bar.jpg"),
345 FILE_PATH_LITERAL("C:\\foo\\bar.jpg")
349 FILE_PATH_LITERAL("C:\\foo\\bar.jpeg"),
350 FILE_PATH_LITERAL("C:\\foo\\bar.jpeg")
352 #else // !defined(OS_WIN)
355 FILE_PATH_LITERAL("/foo/bar.htm"),
356 FILE_PATH_LITERAL("/foo/bar.htm")
360 FILE_PATH_LITERAL("/foo/bar.html"),
361 FILE_PATH_LITERAL("/foo/bar.html")
365 FILE_PATH_LITERAL("/foo/bar"),
366 FILE_PATH_LITERAL("/foo/bar.html")
370 FILE_PATH_LITERAL("/bar.html"),
371 FILE_PATH_LITERAL("/bar.html")
375 FILE_PATH_LITERAL("/bar"),
376 FILE_PATH_LITERAL("/bar.png")
380 FILE_PATH_LITERAL("/foo/bar.exe"),
381 FILE_PATH_LITERAL("/foo/bar.exe")
385 FILE_PATH_LITERAL("/foo/google.com"),
386 FILE_PATH_LITERAL("/foo/google.com")
390 FILE_PATH_LITERAL("/foo/con.htm"),
391 FILE_PATH_LITERAL("/foo/con.htm")
395 FILE_PATH_LITERAL("/foo/con"),
396 FILE_PATH_LITERAL("/foo/con.html")
398 // Allow extension synonyms.
401 FILE_PATH_LITERAL("/bar.jpg"),
402 FILE_PATH_LITERAL("/bar.jpg")
406 FILE_PATH_LITERAL("/bar.jpeg"),
407 FILE_PATH_LITERAL("/bar.jpeg")
409 #endif // !defined(OS_WIN)
412 for (size_t i
= 0; i
< arraysize(safe_tests
); ++i
) {
413 base::FilePath
file_path(safe_tests
[i
].filename
);
414 GenerateSafeFileName(safe_tests
[i
].mime_type
, false, &file_path
);
415 EXPECT_EQ(safe_tests
[i
].expected_filename
, file_path
.value())
416 << "Iteration " << i
;
420 TEST(FilenameUtilTest
, GenerateFileName
) {
421 #if defined(OS_POSIX) && !defined(OS_MACOSX) && !defined(OS_ANDROID)
422 // This test doesn't run when the locale is not UTF-8 because some of the
423 // string conversions fail. This is OK (we have the default value) but they
424 // don't match our expectations.
425 std::string locale
= setlocale(LC_CTYPE
, NULL
);
426 base::StringToLowerASCII(&locale
);
427 EXPECT_TRUE(locale
.find("utf-8") != std::string::npos
||
428 locale
.find("utf8") != std::string::npos
)
429 << "Your locale (" << locale
<< ") must be set to UTF-8 "
430 << "for this test to pass!";
433 // Tests whether the correct filename is selected from the the given
434 // parameters and that Content-Disposition headers are properly
435 // handled including failovers when the header is malformed.
436 const GenerateFilenameCase selection_tests
[] = {
439 "http://www.google.com/",
440 "attachment; filename=test.html",
449 "http://www.google.com/",
450 "attachment; filename=\"test.html\"",
459 "http://www.google.com/",
460 "attachment; filename= \"test.html\"",
469 "http://www.google.com/",
470 "attachment; filename = \"test.html\"",
477 { // filename is whitespace. Should failover to URL host
479 "http://www.google.com/",
480 "attachment; filename= ",
489 "http://www.google.com/path/test.html",
499 "http://www.google.com/path/test.html",
509 "http://www.google.com/",
519 "http://www.google.com/test.html",
527 { // Now that we use src/url's ExtractFileName, this case falls back to
528 // the hostname. If this behavior is not desirable, we'd better change
529 // ExtractFileName (in url_parse.cc).
531 "http://www.google.com/path/",
541 "http://www.google.com/path",
561 "file:///path/testfile",
571 "non-standard-scheme:",
579 { // C-D should override default
581 "http://www.google.com/",
582 "attachment; filename =\"test.html\"",
589 { // But the URL shouldn't
591 "http://www.google.com/",
601 "http://www.google.com/",
602 "attachment; filename=\"../test.html\"",
611 "http://www.google.com/",
612 "attachment; filename=\"..\\test.html\"",
621 "http://www.google.com/",
622 "attachment; filename=\"..\\\\test.html\"",
629 { // Filename disappears after leading and trailing periods are removed.
631 "http://www.google.com/",
632 "attachment; filename=\"..\"",
639 { // C-D specified filename disappears. Failover to final filename.
641 "http://www.google.com/test.html",
642 "attachment; filename=\"..\"",
649 // Below is a small subset of cases taken from HttpContentDisposition tests.
652 "http://www.google.com/",
653 "attachment; filename=\"%EC%98%88%EC%88%A0%20"
654 "%EC%98%88%EC%88%A0.jpg\"",
659 L
"\uc608\uc220 \uc608\uc220.jpg"
663 "http://www.google.com/%EC%98%88%EC%88%A0%20%EC%98%88%EC%88%A0.jpg",
669 L
"\uc608\uc220 \uc608\uc220.jpg"
673 "http://www.google.com/",
678 L
"\uB2E4\uC6B4\uB85C\uB4DC",
679 L
"\uB2E4\uC6B4\uB85C\uB4DC"
683 "http://www.google.com/",
684 "attachment; filename=\"=?EUC-JP?Q?=B7=DD=BD="
694 "http://www.example.com/images?id=3",
695 "attachment; filename=caf\xc3\xa9.png",
704 "http://www.example.com/images?id=3",
705 "attachment; filename=caf\xe5.png",
714 "http://www.example.com/file?id=3",
715 "attachment; name=\xcf\xc2\xd4\xd8.zip",
722 { // Invalid C-D header. Extracts filename from url.
724 "http://www.google.com/test.html",
725 "attachment; filename==?iiso88591?Q?caf=EG?=",
732 // about: and data: URLs
745 "data:,looks/like/a.path",
755 "data:text/plain;base64,VG8gYmUgb3Igbm90IHRvIGJlLg=",
765 "data:,looks/like/a.path",
770 L
"default_filename_is_given",
771 L
"default_filename_is_given"
775 "data:,looks/like/a.path",
780 L
"\u65e5\u672c\u8a9e", // Japanese Kanji.
781 L
"\u65e5\u672c\u8a9e"
783 { // The filename encoding is specified by the referrer charset.
785 "http://example.com/V%FDvojov%E1%20psychologie.doc",
791 L
"V\u00fdvojov\u00e1 psychologie.doc"
793 { // Suggested filename takes precedence over URL
795 "http://www.google.com/test",
803 { // The content-disposition has higher precedence over the suggested name.
805 "http://www.google.com/test",
806 "attachment; filename=test.html",
815 "http://www.google.com/test",
816 "attachment; filename=test",
824 { // The filename encoding doesn't match the referrer charset, the system
825 // charset, or UTF-8.
826 // TODO(jshin): we need to handle this case.
828 "http://example.com/V%FDvojov%E1%20psychologie.doc",
834 L
"V\u00fdvojov\u00e1 psychologie.doc",
837 // Raw 8bit characters in C-D
840 "http://www.example.com/images?id=3",
841 "attachment; filename=caf\xc3\xa9.png",
850 "http://www.example.com/images?id=3",
851 "attachment; filename=caf\xe5.png",
858 { // No 'filename' keyword in the disposition, use the URL
860 "http://www.evil.com/my_download.txt",
868 { // Spaces in the disposition file name
870 "http://www.frontpagehacker.com/a_download.exe",
871 "filename=My Downloaded File.exe",
874 "application/octet-stream",
876 L
"My Downloaded File.exe"
880 "http://www.examples.com/",
882 "filename=\"%EC%98%88%EC%88%A0%20%EC%98%88%EC%88%A0.jpg\"",
887 L
"\uc608\uc220 \uc608\uc220.jpg"
891 "http://www.examples.com/q.cgi?id=abc",
892 "attachment; name=abc de.pdf",
895 "application/octet-stream",
901 "http://www.example.com/path",
902 "filename=\"=?EUC-JP?Q?=B7=DD=BD=D13=2Epng?=\"",
907 L
"\x82b8\x8853" L
"3.png"
909 { // The following two have invalid CD headers and filenames come from the
912 "http://www.example.com/test%20123",
913 "attachment; filename==?iiso88591?Q?caf=EG?=",
922 "http://www.google.com/%EC%98%88%EC%88%A0%20%EC%98%88%EC%88%A0.jpg",
923 "malformed_disposition",
928 L
"\uc608\uc220 \uc608\uc220.jpg"
930 { // Invalid C-D. No filename from URL. Falls back to 'download'.
932 "http://www.google.com/path1/path2/",
933 "attachment; filename==?iso88591?Q?caf=E3?",
942 // Tests filename generation. Once the correct filename is
943 // selected, they should be passed through the validation steps and
944 // a correct extension should be added if necessary.
945 const GenerateFilenameCase generation_tests
[] = {
946 // Dotfiles. Ensures preceeding period(s) stripped.
949 "http://www.google.com/.test.html",
959 "http://www.google.com/.test",
969 "http://www.google.com/..test",
977 { // Disposition has relative paths, remove directory separators
979 "http://www.evil.com/my_download.txt",
980 "filename=../../../../././../a_file_name.txt",
985 L
"-..-..-..-.-.-..-a_file_name.txt"
987 { // Disposition has parent directories, remove directory separators
989 "http://www.evil.com/my_download.txt",
990 "filename=dir1/dir2/a_file_name.txt",
995 L
"dir1-dir2-a_file_name.txt"
997 { // Disposition has relative paths, remove directory separators
999 "http://www.evil.com/my_download.txt",
1000 "filename=..\\..\\..\\..\\.\\.\\..\\a_file_name.txt",
1005 L
"-..-..-..-.-.-..-a_file_name.txt"
1007 { // Disposition has parent directories, remove directory separators
1009 "http://www.evil.com/my_download.txt",
1010 "filename=dir1\\dir2\\a_file_name.txt",
1015 L
"dir1-dir2-a_file_name.txt"
1017 { // No useful information in disposition or URL, use default
1019 "http://www.truncated.com/path/",
1027 { // Filename looks like HTML?
1029 "http://www.evil.com/get/malware/here",
1030 "filename=\"<blink>Hello kitty</blink>\"",
1035 L
"-blink-Hello kitty--blink-"
1037 { // A normal avi should get .avi and not .avi.avi
1039 "https://blah.google.com/misc/2.avi",
1047 { // Extension generation
1049 "http://www.example.com/my-cat",
1059 "http://www.example.com/my-cat",
1069 "http://www.example.com/my-cat",
1077 { // Unknown MIME type
1079 "http://www.example.com/my-cat",
1089 "http://www.example.com/my-cat.jpg",
1090 "filename=my-cat.jpg",
1097 // Windows specific tests
1101 "http://www.goodguy.com/evil.exe",
1102 "filename=evil.exe",
1111 "http://www.goodguy.com/ok.exe",
1115 "binary/octet-stream",
1121 "http://www.goodguy.com/evil.dll",
1122 "filename=evil.dll",
1131 "http://www.goodguy.com/evil.exe",
1135 "application/rss+xml",
1139 // Test truncation of trailing dots and spaces
1142 "http://www.goodguy.com/evil.exe ",
1143 "filename=evil.exe ",
1146 "binary/octet-stream",
1152 "http://www.goodguy.com/evil.exe.",
1153 "filename=evil.exe.",
1156 "binary/octet-stream",
1162 "http://www.goodguy.com/evil.exe. . .",
1163 "filename=evil.exe. . .",
1166 "binary/octet-stream",
1172 "http://www.goodguy.com/evil.",
1176 "binary/octet-stream",
1182 "http://www.goodguy.com/. . . . .",
1183 "filename=. . . . .",
1186 "binary/octet-stream",
1192 "http://www.badguy.com/attachment?name=meh.exe%C2%A0",
1193 "attachment; filename=\"meh.exe\xC2\xA0\"",
1196 "binary/octet-stream",
1203 "http://www.goodguy.com/utils.js",
1204 "filename=utils.js",
1207 "application/x-javascript",
1213 "http://www.goodguy.com/contacts.js",
1214 "filename=contacts.js",
1223 "http://www.goodguy.com/utils.js",
1224 "filename=utils.js",
1233 "http://www.goodguy.com/utils.js",
1234 "filename=utils.js",
1237 "text/javascript;version=2",
1243 "http://www.goodguy.com/utils.js",
1244 "filename=utils.js",
1247 "application/ecmascript",
1253 "http://www.goodguy.com/utils.js",
1254 "filename=utils.js",
1257 "application/ecmascript;version=4",
1263 "http://www.goodguy.com/program.exe",
1264 "filename=program.exe",
1267 "application/foo-bar",
1273 "http://www.evil.com/../foo.txt",
1274 "filename=../foo.txt",
1283 "http://www.evil.com/..\\foo.txt",
1284 "filename=..\\foo.txt",
1293 "http://www.evil.com/.hidden",
1303 "http://www.evil.com/trailing.",
1304 "filename=trailing.",
1317 "http://www.evil.com/trailing.",
1318 "filename=trailing.",
1331 "http://www.evil.com/.",
1341 "http://www.evil.com/..",
1351 "http://www.evil.com/...",
1359 { // Note that this one doesn't have "filename=" on it.
1361 "http://www.evil.com/",
1367 L
"download" JPEG_EXT
1371 "http://www.evil.com/",
1377 L
"download" JPEG_EXT
1381 "http://www.example.com/simple",
1385 "application/octet-stream",
1389 // Reserved words on Windows
1392 "http://www.goodguy.com/COM1",
1396 "application/foo-bar",
1406 "http://www.goodguy.com/COM4.txt",
1407 "filename=COM4.txt",
1420 "http://www.goodguy.com/lpt1.TXT",
1421 "filename=lpt1.TXT",
1434 "http://www.goodguy.com/clock$.txt",
1435 "filename=clock$.txt",
1446 { // Validation should also apply to sugested name
1448 "http://www.goodguy.com/blah$.txt",
1449 "filename=clock$.txt",
1462 "http://www.goodguy.com/mycom1.foo",
1463 "filename=mycom1.foo",
1472 "http://www.badguy.com/Setup.exe.local",
1473 "filename=Setup.exe.local",
1476 "application/foo-bar",
1479 L
"Setup.exe.download"
1486 "http://www.badguy.com/Setup.exe.local",
1487 "filename=Setup.exe.local.local",
1490 "application/foo-bar",
1493 L
"Setup.exe.local.download"
1495 L
"Setup.exe.local.local"
1500 "http://www.badguy.com/Setup.exe.lnk",
1501 "filename=Setup.exe.lnk",
1504 "application/foo-bar",
1507 L
"Setup.exe.download"
1514 "http://www.badguy.com/Desktop.ini",
1515 "filename=Desktop.ini",
1518 "application/foo-bar",
1528 "http://www.badguy.com/Thumbs.db",
1529 "filename=Thumbs.db",
1532 "application/foo-bar",
1542 "http://www.hotmail.com",
1543 "filename=source.jpg",
1546 "application/x-javascript",
1550 { // http://crbug.com/5772.
1552 "http://www.example.com/foo.tar.gz",
1556 "application/x-tar",
1560 { // http://crbug.com/52250.
1562 "http://www.example.com/foo.tgz",
1566 "application/x-tar",
1570 { // http://crbug.com/7337.
1572 "http://maged.lordaeron.org/blank.reg",
1582 "http://www.example.com/bar.tar",
1586 "application/x-tar",
1592 "http://www.example.com/bar.bogus",
1596 "application/x-tar",
1600 { // http://crbug.com/20337
1602 "http://www.example.com/.download.txt",
1603 "filename=.download.txt",
1610 { // http://crbug.com/56855.
1612 "http://www.example.com/bar.sh",
1620 { // http://crbug.com/61571
1622 "http://www.example.com/npdf.php?fn=foobar.pdf",
1630 { // Shouldn't overwrite C-D specified extension.
1632 "http://www.example.com/npdf.php?fn=foobar.pdf",
1633 "filename=foobar.jpg",
1640 { // http://crbug.com/87719
1642 "http://www.example.com/image.aspx?id=blargh",
1650 #if defined(OS_CHROMEOS)
1651 { // http://crosbug.com/26028
1653 "http://www.example.com/fooa%cc%88.txt",
1664 for (size_t i
= 0; i
< arraysize(selection_tests
); ++i
)
1665 RunGenerateFileNameTestCase(&selection_tests
[i
]);
1667 for (size_t i
= 0; i
< arraysize(generation_tests
); ++i
)
1668 RunGenerateFileNameTestCase(&generation_tests
[i
]);
1670 for (size_t i
= 0; i
< arraysize(generation_tests
); ++i
) {
1671 GenerateFilenameCase test_case
= generation_tests
[i
];
1672 test_case
.referrer_charset
= "GBK";
1673 RunGenerateFileNameTestCase(&test_case
);