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 std::string
GetLocaleWarningString() {
52 #if defined(OS_POSIX) && !defined(OS_ANDROID)
53 // The generate filename tests can fail on certain OS_POSIX platforms when
54 // LC_CTYPE is not "utf8" or "utf-8" because some of the string conversions
56 // This warning text is appended to any test failures to save people time if
57 // this happens to be the cause of failure :)
58 // Note: some platforms (MACOSX, Chromecast) don't have this problem:
59 // setlocale returns "c" but it functions as utf8. And Android doesn't
60 // have setlocale at all.
61 std::string locale
= setlocale(LC_CTYPE
, NULL
);
62 return " this test may have failed because the current LC_CTYPE locale is "
63 "not utf8 (currently set to " +
70 void RunGenerateFileNameTestCase(const GenerateFilenameCase
* test_case
) {
71 std::string
default_filename(base::WideToUTF8(test_case
->default_filename
));
72 base::FilePath file_path
= GenerateFileName(
73 GURL(test_case
->url
), test_case
->content_disp_header
,
74 test_case
->referrer_charset
, test_case
->suggested_filename
,
75 test_case
->mime_type
, default_filename
);
76 EXPECT_EQ(test_case
->expected_filename
, FilePathAsWString(file_path
))
77 << "test case at line number: " << test_case
->lineno
<< "; "
78 << GetLocaleWarningString();
83 static const base::FilePath::CharType
* kSafePortableBasenames
[] = {
84 FILE_PATH_LITERAL("a"),
85 FILE_PATH_LITERAL("a.txt"),
86 FILE_PATH_LITERAL("a b.txt"),
87 FILE_PATH_LITERAL("a-b.txt"),
88 FILE_PATH_LITERAL("My Computer"),
91 static const base::FilePath::CharType
* kUnsafePortableBasenames
[] = {
92 FILE_PATH_LITERAL(""),
93 FILE_PATH_LITERAL("."),
94 FILE_PATH_LITERAL(".."),
95 FILE_PATH_LITERAL("..."),
96 FILE_PATH_LITERAL("con"),
97 FILE_PATH_LITERAL("con.zip"),
98 FILE_PATH_LITERAL("NUL"),
99 FILE_PATH_LITERAL("NUL.zip"),
100 FILE_PATH_LITERAL(".a"),
101 FILE_PATH_LITERAL("a."),
102 FILE_PATH_LITERAL("a\"a"),
103 FILE_PATH_LITERAL("a<a"),
104 FILE_PATH_LITERAL("a>a"),
105 FILE_PATH_LITERAL("a?a"),
106 FILE_PATH_LITERAL("a/"),
107 FILE_PATH_LITERAL("a\\"),
108 FILE_PATH_LITERAL("a "),
109 FILE_PATH_LITERAL("a . ."),
110 FILE_PATH_LITERAL(" Computer"),
111 FILE_PATH_LITERAL("My Computer.{a}"),
112 FILE_PATH_LITERAL("My Computer.{20D04FE0-3AEA-1069-A2D8-08002B30309D}"),
114 FILE_PATH_LITERAL("a\\a"),
118 static const base::FilePath::CharType
* kSafePortableRelativePaths
[] = {
119 FILE_PATH_LITERAL("a/a"),
121 FILE_PATH_LITERAL("a\\a"),
125 TEST(FilenameUtilTest
, IsSafePortablePathComponent
) {
126 for (size_t i
= 0; i
< arraysize(kSafePortableBasenames
); ++i
) {
128 IsSafePortablePathComponent(base::FilePath(kSafePortableBasenames
[i
])))
129 << kSafePortableBasenames
[i
];
131 for (size_t i
= 0; i
< arraysize(kUnsafePortableBasenames
); ++i
) {
132 EXPECT_FALSE(IsSafePortablePathComponent(
133 base::FilePath(kUnsafePortableBasenames
[i
])))
134 << kUnsafePortableBasenames
[i
];
136 for (size_t i
= 0; i
< arraysize(kSafePortableRelativePaths
); ++i
) {
137 EXPECT_FALSE(IsSafePortablePathComponent(
138 base::FilePath(kSafePortableRelativePaths
[i
])))
139 << kSafePortableRelativePaths
[i
];
143 TEST(FilenameUtilTest
, IsSafePortableRelativePath
) {
144 base::FilePath
safe_dirname(FILE_PATH_LITERAL("a"));
145 for (size_t i
= 0; i
< arraysize(kSafePortableBasenames
); ++i
) {
147 IsSafePortableRelativePath(base::FilePath(kSafePortableBasenames
[i
])))
148 << kSafePortableBasenames
[i
];
149 EXPECT_TRUE(IsSafePortableRelativePath(
150 safe_dirname
.Append(base::FilePath(kSafePortableBasenames
[i
]))))
151 << kSafePortableBasenames
[i
];
153 for (size_t i
= 0; i
< arraysize(kSafePortableRelativePaths
); ++i
) {
154 EXPECT_TRUE(IsSafePortableRelativePath(
155 base::FilePath(kSafePortableRelativePaths
[i
])))
156 << kSafePortableRelativePaths
[i
];
157 EXPECT_TRUE(IsSafePortableRelativePath(
158 safe_dirname
.Append(base::FilePath(kSafePortableRelativePaths
[i
]))))
159 << kSafePortableRelativePaths
[i
];
161 for (size_t i
= 0; i
< arraysize(kUnsafePortableBasenames
); ++i
) {
163 IsSafePortableRelativePath(base::FilePath(kUnsafePortableBasenames
[i
])))
164 << kUnsafePortableBasenames
[i
];
165 if (!base::FilePath::StringType(kUnsafePortableBasenames
[i
]).empty()) {
166 EXPECT_FALSE(IsSafePortableRelativePath(
167 safe_dirname
.Append(base::FilePath(kUnsafePortableBasenames
[i
]))))
168 << kUnsafePortableBasenames
[i
];
173 TEST(FilenameUtilTest
, FileURLConversion
) {
174 // a list of test file names and the corresponding URLs
175 const FileCase round_trip_cases
[] = {
177 {L
"C:\\foo\\bar.txt", "file:///C:/foo/bar.txt"},
178 {L
"\\\\some computer\\foo\\bar.txt",
179 "file://some%20computer/foo/bar.txt"}, // UNC
180 {L
"D:\\Name;with%some symbols*#",
181 "file:///D:/Name%3Bwith%25some%20symbols*%23"},
182 // issue 14153: To be tested with the OS default codepage other than 1252.
183 {L
"D:\\latin1\\caf\x00E9\x00DD.txt",
184 "file:///D:/latin1/caf%C3%A9%C3%9D.txt"},
185 {L
"D:\\otherlatin\\caf\x0119.txt", "file:///D:/otherlatin/caf%C4%99.txt"},
186 {L
"D:\\greek\\\x03B1\x03B2\x03B3.txt",
187 "file:///D:/greek/%CE%B1%CE%B2%CE%B3.txt"},
188 {L
"D:\\Chinese\\\x6240\x6709\x4e2d\x6587\x7f51\x9875.doc",
189 "file:///D:/Chinese/%E6%89%80%E6%9C%89%E4%B8%AD%E6%96%87%E7%BD%91"
191 {L
"D:\\plane1\\\xD835\xDC00\xD835\xDC01.txt", // Math alphabet "AB"
192 "file:///D:/plane1/%F0%9D%90%80%F0%9D%90%81.txt"},
193 #elif defined(OS_POSIX)
194 {L
"/foo/bar.txt", "file:///foo/bar.txt"},
195 {L
"/foo/BAR.txt", "file:///foo/BAR.txt"},
196 {L
"/C:/foo/bar.txt", "file:///C:/foo/bar.txt"},
197 {L
"/foo/bar?.txt", "file:///foo/bar%3F.txt"},
198 {L
"/some computer/foo/bar.txt", "file:///some%20computer/foo/bar.txt"},
199 {L
"/Name;with%some symbols*#", "file:///Name%3Bwith%25some%20symbols*%23"},
200 {L
"/latin1/caf\x00E9\x00DD.txt", "file:///latin1/caf%C3%A9%C3%9D.txt"},
201 {L
"/otherlatin/caf\x0119.txt", "file:///otherlatin/caf%C4%99.txt"},
202 {L
"/greek/\x03B1\x03B2\x03B3.txt", "file:///greek/%CE%B1%CE%B2%CE%B3.txt"},
203 {L
"/Chinese/\x6240\x6709\x4e2d\x6587\x7f51\x9875.doc",
204 "file:///Chinese/%E6%89%80%E6%9C%89%E4%B8%AD%E6%96%87%E7%BD"
206 {L
"/plane1/\x1D400\x1D401.txt", // Math alphabet "AB"
207 "file:///plane1/%F0%9D%90%80%F0%9D%90%81.txt"},
211 // First, we'll test that we can round-trip all of the above cases of URLs
212 base::FilePath output
;
213 for (size_t i
= 0; i
< arraysize(round_trip_cases
); i
++) {
214 // convert to the file URL
216 FilePathToFileURL(WStringAsFilePath(round_trip_cases
[i
].file
)));
217 EXPECT_EQ(round_trip_cases
[i
].url
, file_url
.spec());
219 // Back to the filename.
220 EXPECT_TRUE(FileURLToFilePath(file_url
, &output
));
221 EXPECT_EQ(round_trip_cases
[i
].file
, FilePathAsWString(output
));
224 // Test that various file: URLs get decoded into the correct file type
225 FileCase url_cases
[] = {
227 {L
"C:\\foo\\bar.txt", "file:c|/foo\\bar.txt"},
228 {L
"C:\\foo\\bar.txt", "file:/c:/foo/bar.txt"},
229 {L
"\\\\foo\\bar.txt", "file://foo\\bar.txt"},
230 {L
"C:\\foo\\bar.txt", "file:///c:/foo/bar.txt"},
231 {L
"\\\\foo\\bar.txt", "file:////foo\\bar.txt"},
232 {L
"\\\\foo\\bar.txt", "file:/foo/bar.txt"},
233 {L
"\\\\foo\\bar.txt", "file://foo\\bar.txt"},
234 {L
"C:\\foo\\bar.txt", "file:\\\\\\c:/foo/bar.txt"},
235 #elif defined(OS_POSIX)
236 {L
"/c:/foo/bar.txt", "file:/c:/foo/bar.txt"},
237 {L
"/c:/foo/bar.txt", "file:///c:/foo/bar.txt"},
238 {L
"/foo/bar.txt", "file:/foo/bar.txt"},
239 {L
"/c:/foo/bar.txt", "file:\\\\\\c:/foo/bar.txt"},
240 {L
"/foo/bar.txt", "file:foo/bar.txt"},
241 {L
"/bar.txt", "file://foo/bar.txt"},
242 {L
"/foo/bar.txt", "file:///foo/bar.txt"},
243 {L
"/foo/bar.txt", "file:////foo/bar.txt"},
244 {L
"/foo/bar.txt", "file:////foo//bar.txt"},
245 {L
"/foo/bar.txt", "file:////foo///bar.txt"},
246 {L
"/foo/bar.txt", "file:////foo////bar.txt"},
247 {L
"/c:/foo/bar.txt", "file:\\\\\\c:/foo/bar.txt"},
248 {L
"/c:/foo/bar.txt", "file:c:/foo/bar.txt"},
249 // We get these wrong because GURL turns back slashes into forward
251 // {L"/foo%5Cbar.txt", "file://foo\\bar.txt"},
252 // {L"/c|/foo%5Cbar.txt", "file:c|/foo\\bar.txt"},
253 // {L"/foo%5Cbar.txt", "file://foo\\bar.txt"},
254 // {L"/foo%5Cbar.txt", "file:////foo\\bar.txt"},
255 // {L"/foo%5Cbar.txt", "file://foo\\bar.txt"},
258 for (size_t i
= 0; i
< arraysize(url_cases
); i
++) {
259 FileURLToFilePath(GURL(url_cases
[i
].url
), &output
);
260 EXPECT_EQ(url_cases
[i
].file
, FilePathAsWString(output
));
263 // Unfortunately, UTF8ToWide discards invalid UTF8 input.
264 #ifdef BUG_878908_IS_FIXED
265 // Test that no conversion happens if the UTF-8 input is invalid, and that
266 // the input is preserved in UTF-8
267 const char invalid_utf8
[] = "file:///d:/Blah/\xff.doc";
268 const wchar_t invalid_wide
[] = L
"D:\\Blah\\\xff.doc";
269 EXPECT_TRUE(FileURLToFilePath(GURL(std::string(invalid_utf8
)), &output
));
270 EXPECT_EQ(std::wstring(invalid_wide
), output
);
273 // Test that if a file URL is malformed, we get a failure
274 EXPECT_FALSE(FileURLToFilePath(GURL("filefoobar"), &output
));
278 #define JPEG_EXT L".jpg"
279 #define HTML_EXT L".htm"
280 #elif defined(OS_MACOSX)
281 #define JPEG_EXT L".jpeg"
282 #define HTML_EXT L".html"
284 #define JPEG_EXT L".jpg"
285 #define HTML_EXT L".html"
287 #define TXT_EXT L".txt"
288 #define TAR_EXT L".tar"
290 TEST(FilenameUtilTest
, GenerateSafeFileName
) {
292 const char* mime_type
;
293 const base::FilePath::CharType
* filename
;
294 const base::FilePath::CharType
* expected_filename
;
298 FILE_PATH_LITERAL("C:\\foo\\bar.htm"),
299 FILE_PATH_LITERAL("C:\\foo\\bar.htm")},
301 FILE_PATH_LITERAL("C:\\foo\\bar.html"),
302 FILE_PATH_LITERAL("C:\\foo\\bar.html")},
304 FILE_PATH_LITERAL("C:\\foo\\bar"),
305 FILE_PATH_LITERAL("C:\\foo\\bar.htm")},
307 FILE_PATH_LITERAL("C:\\bar.html"),
308 FILE_PATH_LITERAL("C:\\bar.html")},
310 FILE_PATH_LITERAL("C:\\bar"),
311 FILE_PATH_LITERAL("C:\\bar.png")},
313 FILE_PATH_LITERAL("C:\\foo\\bar.exe"),
314 FILE_PATH_LITERAL("C:\\foo\\bar.exe")},
316 FILE_PATH_LITERAL("C:\\foo\\bar.exe"),
317 FILE_PATH_LITERAL("C:\\foo\\bar.exe")},
319 FILE_PATH_LITERAL("C:\\foo\\google.com"),
320 FILE_PATH_LITERAL("C:\\foo\\google.com")},
322 FILE_PATH_LITERAL("C:\\foo\\con.htm"),
323 FILE_PATH_LITERAL("C:\\foo\\_con.htm")},
325 FILE_PATH_LITERAL("C:\\foo\\con"),
326 FILE_PATH_LITERAL("C:\\foo\\_con.htm")},
328 FILE_PATH_LITERAL("C:\\foo\\harmless.{not-really-this-may-be-a-guid}"),
329 FILE_PATH_LITERAL("C:\\foo\\harmless.download")},
331 FILE_PATH_LITERAL("C:\\foo\\harmless.local"),
332 FILE_PATH_LITERAL("C:\\foo\\harmless.download")},
334 FILE_PATH_LITERAL("C:\\foo\\harmless.lnk"),
335 FILE_PATH_LITERAL("C:\\foo\\harmless.download")},
337 FILE_PATH_LITERAL("C:\\foo\\harmless.{mismatched-"),
338 FILE_PATH_LITERAL("C:\\foo\\harmless.{mismatched-")},
339 // Allow extension synonyms.
341 FILE_PATH_LITERAL("C:\\foo\\bar.jpg"),
342 FILE_PATH_LITERAL("C:\\foo\\bar.jpg")},
344 FILE_PATH_LITERAL("C:\\foo\\bar.jpeg"),
345 FILE_PATH_LITERAL("C:\\foo\\bar.jpeg")},
346 #else // !defined(OS_WIN)
348 FILE_PATH_LITERAL("/foo/bar.htm"),
349 FILE_PATH_LITERAL("/foo/bar.htm")},
351 FILE_PATH_LITERAL("/foo/bar.html"),
352 FILE_PATH_LITERAL("/foo/bar.html")},
354 FILE_PATH_LITERAL("/foo/bar"),
355 FILE_PATH_LITERAL("/foo/bar.html")},
357 FILE_PATH_LITERAL("/bar.html"),
358 FILE_PATH_LITERAL("/bar.html")},
359 {"image/png", FILE_PATH_LITERAL("/bar"), FILE_PATH_LITERAL("/bar.png")},
361 FILE_PATH_LITERAL("/foo/bar.exe"),
362 FILE_PATH_LITERAL("/foo/bar.exe")},
364 FILE_PATH_LITERAL("/foo/google.com"),
365 FILE_PATH_LITERAL("/foo/google.com")},
367 FILE_PATH_LITERAL("/foo/con.htm"),
368 FILE_PATH_LITERAL("/foo/con.htm")},
370 FILE_PATH_LITERAL("/foo/con"),
371 FILE_PATH_LITERAL("/foo/con.html")},
372 // Allow extension synonyms.
374 FILE_PATH_LITERAL("/bar.jpg"),
375 FILE_PATH_LITERAL("/bar.jpg")},
377 FILE_PATH_LITERAL("/bar.jpeg"),
378 FILE_PATH_LITERAL("/bar.jpeg")},
379 #endif // !defined(OS_WIN)
382 for (size_t i
= 0; i
< arraysize(safe_tests
); ++i
) {
383 base::FilePath
file_path(safe_tests
[i
].filename
);
384 GenerateSafeFileName(safe_tests
[i
].mime_type
, false, &file_path
);
385 EXPECT_EQ(safe_tests
[i
].expected_filename
, file_path
.value())
386 << "Iteration " << i
;
390 TEST(FilenameUtilTest
, GenerateFileName
) {
391 // Tests whether the correct filename is selected from the the given
392 // parameters and that Content-Disposition headers are properly
393 // handled including failovers when the header is malformed.
394 const GenerateFilenameCase selection_tests
[] = {
396 "http://www.google.com/",
397 "attachment; filename=test.html",
404 "http://www.google.com/",
405 "attachment; filename=\"test.html\"",
412 "http://www.google.com/",
413 "attachment; filename= \"test.html\"",
420 "http://www.google.com/",
421 "attachment; filename = \"test.html\"",
427 {// filename is whitespace. Should failover to URL host
429 "http://www.google.com/",
430 "attachment; filename= ",
438 "http://www.google.com/path/test.html",
447 "http://www.google.com/path/test.html",
456 "http://www.google.com/",
464 "http://www.google.com/test.html",
471 {// Now that we use src/url's ExtractFileName, this case falls back to
472 // the hostname. If this behavior is not desirable, we'd better change
473 // ExtractFileName (in url_parse.cc).
475 "http://www.google.com/path/",
482 {__LINE__
, "http://www.google.com/path", "", "", "", "", L
"", L
"path"},
483 {__LINE__
, "file:///", "", "", "", "", L
"", L
"download"},
484 {__LINE__
, "file:///path/testfile", "", "", "", "", L
"", L
"testfile"},
485 {__LINE__
, "non-standard-scheme:", "", "", "", "", L
"", L
"download"},
486 {// C-D should override default
488 "http://www.google.com/",
489 "attachment; filename =\"test.html\"",
495 {// But the URL shouldn't
497 "http://www.google.com/",
505 "http://www.google.com/",
506 "attachment; filename=\"../test.html\"",
513 "http://www.google.com/",
514 "attachment; filename=\"..\\test.html\"",
521 "http://www.google.com/",
522 "attachment; filename=\"..\\\\test.html\"",
528 {// Filename disappears after leading and trailing periods are removed.
530 "http://www.google.com/",
531 "attachment; filename=\"..\"",
537 {// C-D specified filename disappears. Failover to final filename.
539 "http://www.google.com/test.html",
540 "attachment; filename=\"..\"",
546 // Below is a small subset of cases taken from HttpContentDisposition tests.
548 "http://www.google.com/",
549 "attachment; filename=\"%EC%98%88%EC%88%A0%20"
550 "%EC%98%88%EC%88%A0.jpg\"",
555 L
"\uc608\uc220 \uc608\uc220.jpg"},
557 "http://www.google.com/%EC%98%88%EC%88%A0%20%EC%98%88%EC%88%A0.jpg",
563 L
"\uc608\uc220 \uc608\uc220.jpg"},
565 "http://www.google.com/",
570 L
"\uB2E4\uC6B4\uB85C\uB4DC",
571 L
"\uB2E4\uC6B4\uB85C\uB4DC"},
573 "http://www.google.com/",
574 "attachment; filename=\"=?EUC-JP?Q?=B7=DD=BD="
580 L
"\u82b8\u88533.png"},
582 "http://www.example.com/images?id=3",
583 "attachment; filename=caf\xc3\xa9.png",
590 "http://www.example.com/images?id=3",
591 "attachment; filename=caf\xe5.png",
598 "http://www.example.com/file?id=3",
599 "attachment; name=\xcf\xc2\xd4\xd8.zip",
604 L
"\u4e0b\u8f7d.zip"},
605 {// Invalid C-D header. Extracts filename from url.
607 "http://www.google.com/test.html",
608 "attachment; filename==?iiso88591?Q?caf=EG?=",
614 // about: and data: URLs
615 {__LINE__
, "about:chrome", "", "", "", "", L
"", L
"download"},
616 {__LINE__
, "data:,looks/like/a.path", "", "", "", "", L
"", L
"download"},
618 "data:text/plain;base64,VG8gYmUgb3Igbm90IHRvIGJlLg=",
626 "data:,looks/like/a.path",
631 L
"default_filename_is_given",
632 L
"default_filename_is_given"},
634 "data:,looks/like/a.path",
639 L
"\u65e5\u672c\u8a9e", // Japanese Kanji.
640 L
"\u65e5\u672c\u8a9e"},
641 {// The filename encoding is specified by the referrer charset.
643 "http://example.com/V%FDvojov%E1%20psychologie.doc",
649 L
"V\u00fdvojov\u00e1 psychologie.doc"},
650 {// Suggested filename takes precedence over URL
652 "http://www.google.com/test",
659 {// The content-disposition has higher precedence over the suggested name.
661 "http://www.google.com/test",
662 "attachment; filename=test.html",
669 "http://www.google.com/test",
670 "attachment; filename=test",
677 { // The filename encoding doesn't match the referrer charset, the system
678 // charset, or UTF-8.
679 // TODO(jshin): we need to handle this case.
681 "http://example.com/V%FDvojov%E1%20psychologie.doc",
687 L
"V\u00fdvojov\u00e1 psychologie.doc",
690 // Raw 8bit characters in C-D
692 "http://www.example.com/images?id=3",
693 "attachment; filename=caf\xc3\xa9.png",
700 "http://www.example.com/images?id=3",
701 "attachment; filename=caf\xe5.png",
707 {// No 'filename' keyword in the disposition, use the URL
709 "http://www.evil.com/my_download.txt",
716 {// Spaces in the disposition file name
718 "http://www.frontpagehacker.com/a_download.exe",
719 "filename=My Downloaded File.exe",
722 "application/octet-stream",
724 L
"My Downloaded File.exe"},
727 "http://www.examples.com/",
729 "filename=\"%EC%98%88%EC%88%A0%20%EC%98%88%EC%88%A0.jpg\"",
734 L
"\uc608\uc220 \uc608\uc220.jpg"},
737 "http://www.examples.com/q.cgi?id=abc",
738 "attachment; name=abc de.pdf",
741 "application/octet-stream",
745 "http://www.example.com/path",
746 "filename=\"=?EUC-JP?Q?=B7=DD=BD=D13=2Epng?=\"",
753 {// The following two have invalid CD headers and filenames come from the
756 "http://www.example.com/test%20123",
757 "attachment; filename==?iiso88591?Q?caf=EG?=",
762 L
"test 123" JPEG_EXT
},
764 "http://www.google.com/%EC%98%88%EC%88%A0%20%EC%98%88%EC%88%A0.jpg",
765 "malformed_disposition",
770 L
"\uc608\uc220 \uc608\uc220.jpg"},
771 {// Invalid C-D. No filename from URL. Falls back to 'download'.
773 "http://www.google.com/path1/path2/",
774 "attachment; filename==?iso88591?Q?caf=E3?",
779 L
"download" JPEG_EXT
},
782 // Tests filename generation. Once the correct filename is
783 // selected, they should be passed through the validation steps and
784 // a correct extension should be added if necessary.
785 const GenerateFilenameCase generation_tests
[] = {
786 // Dotfiles. Ensures preceeding period(s) stripped.
788 "http://www.google.com/.test.html",
795 {__LINE__
, "http://www.google.com/.test", "", "", "", "", L
"", L
"test"},
796 {__LINE__
, "http://www.google.com/..test", "", "", "", "", L
"", L
"test"},
797 {// Disposition has relative paths, remove directory separators
799 "http://www.evil.com/my_download.txt",
800 "filename=../../../../././../a_file_name.txt",
805 L
"-..-..-..-.-.-..-a_file_name.txt"},
806 {// Disposition has parent directories, remove directory separators
808 "http://www.evil.com/my_download.txt",
809 "filename=dir1/dir2/a_file_name.txt",
814 L
"dir1-dir2-a_file_name.txt"},
815 {// Disposition has relative paths, remove directory separators
817 "http://www.evil.com/my_download.txt",
818 "filename=..\\..\\..\\..\\.\\.\\..\\a_file_name.txt",
823 L
"-..-..-..-.-.-..-a_file_name.txt"},
824 {// Disposition has parent directories, remove directory separators
826 "http://www.evil.com/my_download.txt",
827 "filename=dir1\\dir2\\a_file_name.txt",
832 L
"dir1-dir2-a_file_name.txt"},
833 {// No useful information in disposition or URL, use default
835 "http://www.truncated.com/path/",
841 L
"download" TXT_EXT
},
842 {// Filename looks like HTML?
844 "http://www.evil.com/get/malware/here",
845 "filename=\"<blink>Hello kitty</blink>\"",
850 L
"-blink-Hello kitty--blink-"},
851 {// A normal avi should get .avi and not .avi.avi
853 "https://blah.google.com/misc/2.avi",
860 {// Extension generation
862 "http://www.example.com/my-cat",
870 "http://www.example.com/my-cat",
878 "http://www.example.com/my-cat",
885 {// Unknown MIME type
887 "http://www.example.com/my-cat",
895 "http://www.example.com/my-cat.jpg",
896 "filename=my-cat.jpg",
902 // Windows specific tests
905 "http://www.goodguy.com/evil.exe",
913 "http://www.goodguy.com/ok.exe",
917 "binary/octet-stream",
921 "http://www.goodguy.com/evil.dll",
929 "http://www.goodguy.com/evil.exe",
933 "application/rss+xml",
936 // Test truncation of trailing dots and spaces
938 "http://www.goodguy.com/evil.exe ",
939 "filename=evil.exe ",
942 "binary/octet-stream",
946 "http://www.goodguy.com/evil.exe.",
947 "filename=evil.exe.",
950 "binary/octet-stream",
954 "http://www.goodguy.com/evil.exe. . .",
955 "filename=evil.exe. . .",
958 "binary/octet-stream",
962 "http://www.goodguy.com/evil.",
966 "binary/octet-stream",
970 "http://www.goodguy.com/. . . . .",
971 "filename=. . . . .",
974 "binary/octet-stream",
978 "http://www.badguy.com/attachment?name=meh.exe%C2%A0",
979 "attachment; filename=\"meh.exe\xC2\xA0\"",
982 "binary/octet-stream",
987 "http://www.goodguy.com/utils.js",
991 "application/x-javascript",
995 "http://www.goodguy.com/contacts.js",
996 "filename=contacts.js",
1003 "http://www.goodguy.com/utils.js",
1004 "filename=utils.js",
1011 "http://www.goodguy.com/utils.js",
1012 "filename=utils.js",
1015 "text/javascript;version=2",
1019 "http://www.goodguy.com/utils.js",
1020 "filename=utils.js",
1023 "application/ecmascript",
1027 "http://www.goodguy.com/utils.js",
1028 "filename=utils.js",
1031 "application/ecmascript;version=4",
1035 "http://www.goodguy.com/program.exe",
1036 "filename=program.exe",
1039 "application/foo-bar",
1043 "http://www.evil.com/../foo.txt",
1044 "filename=../foo.txt",
1051 "http://www.evil.com/..\\foo.txt",
1052 "filename=..\\foo.txt",
1059 "http://www.evil.com/.hidden",
1067 "http://www.evil.com/trailing.",
1068 "filename=trailing.",
1080 "http://www.evil.com/trailing.",
1081 "filename=trailing.",
1093 "http://www.evil.com/.",
1101 "http://www.evil.com/..",
1109 "http://www.evil.com/...",
1116 {// Note that this one doesn't have "filename=" on it.
1118 "http://www.evil.com/",
1124 L
"download" JPEG_EXT
},
1126 "http://www.evil.com/",
1132 L
"download" JPEG_EXT
},
1134 "http://www.example.com/simple",
1138 "application/octet-stream",
1141 // Reserved words on Windows
1143 "http://www.goodguy.com/COM1",
1147 "application/foo-bar",
1156 "http://www.goodguy.com/COM4.txt",
1157 "filename=COM4.txt",
1169 "http://www.goodguy.com/lpt1.TXT",
1170 "filename=lpt1.TXT",
1182 "http://www.goodguy.com/clock$.txt",
1183 "filename=clock$.txt",
1194 {// Validation should also apply to sugested name
1196 "http://www.goodguy.com/blah$.txt",
1197 "filename=clock$.txt",
1209 "http://www.goodguy.com/mycom1.foo",
1210 "filename=mycom1.foo",
1217 "http://www.badguy.com/Setup.exe.local",
1218 "filename=Setup.exe.local",
1221 "application/foo-bar",
1224 L
"Setup.exe.download"
1230 "http://www.badguy.com/Setup.exe.local",
1231 "filename=Setup.exe.local.local",
1234 "application/foo-bar",
1237 L
"Setup.exe.local.download"
1239 L
"Setup.exe.local.local"
1243 "http://www.badguy.com/Setup.exe.lnk",
1244 "filename=Setup.exe.lnk",
1247 "application/foo-bar",
1250 L
"Setup.exe.download"
1256 "http://www.badguy.com/Desktop.ini",
1257 "filename=Desktop.ini",
1260 "application/foo-bar",
1269 "http://www.badguy.com/Thumbs.db",
1270 "filename=Thumbs.db",
1273 "application/foo-bar",
1282 "http://www.hotmail.com",
1283 "filename=source.jpg",
1286 "application/x-javascript",
1289 {// http://crbug.com/5772.
1291 "http://www.example.com/foo.tar.gz",
1295 "application/x-tar",
1298 {// http://crbug.com/52250.
1300 "http://www.example.com/foo.tgz",
1304 "application/x-tar",
1307 {// http://crbug.com/7337.
1309 "http://maged.lordaeron.org/blank.reg",
1317 "http://www.example.com/bar.tar",
1321 "application/x-tar",
1325 "http://www.example.com/bar.bogus",
1329 "application/x-tar",
1332 {// http://crbug.com/20337
1334 "http://www.example.com/.download.txt",
1335 "filename=.download.txt",
1341 {// http://crbug.com/56855.
1343 "http://www.example.com/bar.sh",
1350 {// http://crbug.com/61571
1352 "http://www.example.com/npdf.php?fn=foobar.pdf",
1359 {// Shouldn't overwrite C-D specified extension.
1361 "http://www.example.com/npdf.php?fn=foobar.pdf",
1362 "filename=foobar.jpg",
1368 {// http://crbug.com/87719
1370 "http://www.example.com/image.aspx?id=blargh",
1378 "http://www.example.com/image.aspx?id=blargh",
1385 #if defined(OS_CHROMEOS)
1386 {// http://crosbug.com/26028
1388 "http://www.example.com/fooa%cc%88.txt",
1398 for (size_t i
= 0; i
< arraysize(selection_tests
); ++i
)
1399 RunGenerateFileNameTestCase(&selection_tests
[i
]);
1401 for (size_t i
= 0; i
< arraysize(generation_tests
); ++i
)
1402 RunGenerateFileNameTestCase(&generation_tests
[i
]);
1404 for (size_t i
= 0; i
< arraysize(generation_tests
); ++i
) {
1405 GenerateFilenameCase test_case
= generation_tests
[i
];
1406 test_case
.referrer_charset
= "GBK";
1407 RunGenerateFileNameTestCase(&test_case
);