[Android] Pass the blacklist file to android gtests run via testing/scripts/.
[chromium-blink-merge.git] / net / base / filename_util_unittest.cc
blob453106c6aadb15dd6e18b61951746cea48356e29
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"
13 #include "url/gurl.h"
15 namespace net {
17 namespace {
19 struct FileCase {
20 const wchar_t* file;
21 const char* url;
24 struct GenerateFilenameCase {
25 int lineno;
26 const char* url;
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) {
37 #if defined(OS_WIN)
38 return path.value();
39 #else
40 return base::UTF8ToWide(path.value());
41 #endif
43 base::FilePath WStringAsFilePath(const std::wstring& str) {
44 #if defined(OS_WIN)
45 return base::FilePath(str);
46 #else
47 return base::FilePath(base::WideToUTF8(str));
48 #endif
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
55 // fail.
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 " +
64 locale + ")";
65 #else
66 return "";
67 #endif
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();
81 } // namespace
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}"),
113 #if !defined(OS_WIN)
114 FILE_PATH_LITERAL("a\\a"),
115 #endif
118 static const base::FilePath::CharType* kUnsafePortableBasenamesForWindows[] = {
119 FILE_PATH_LITERAL("con"),
120 FILE_PATH_LITERAL("con.zip"),
121 FILE_PATH_LITERAL("NUL"),
122 FILE_PATH_LITERAL("NUL.zip"),
125 static const base::FilePath::CharType* kSafePortableRelativePaths[] = {
126 FILE_PATH_LITERAL("a/a"),
127 #if defined(OS_WIN)
128 FILE_PATH_LITERAL("a\\a"),
129 #endif
132 TEST(FilenameUtilTest, IsSafePortablePathComponent) {
133 for (size_t i = 0; i < arraysize(kSafePortableBasenames); ++i) {
134 EXPECT_TRUE(
135 IsSafePortablePathComponent(base::FilePath(kSafePortableBasenames[i])))
136 << kSafePortableBasenames[i];
138 for (size_t i = 0; i < arraysize(kUnsafePortableBasenames); ++i) {
139 EXPECT_FALSE(IsSafePortablePathComponent(
140 base::FilePath(kUnsafePortableBasenames[i])))
141 << kUnsafePortableBasenames[i];
143 for (size_t i = 0; i < arraysize(kSafePortableRelativePaths); ++i) {
144 EXPECT_FALSE(IsSafePortablePathComponent(
145 base::FilePath(kSafePortableRelativePaths[i])))
146 << kSafePortableRelativePaths[i];
150 TEST(FilenameUtilTest, IsSafePortableRelativePath) {
151 base::FilePath safe_dirname(FILE_PATH_LITERAL("a"));
152 for (size_t i = 0; i < arraysize(kSafePortableBasenames); ++i) {
153 EXPECT_TRUE(
154 IsSafePortableRelativePath(base::FilePath(kSafePortableBasenames[i])))
155 << kSafePortableBasenames[i];
156 EXPECT_TRUE(IsSafePortableRelativePath(
157 safe_dirname.Append(base::FilePath(kSafePortableBasenames[i]))))
158 << kSafePortableBasenames[i];
160 for (size_t i = 0; i < arraysize(kSafePortableRelativePaths); ++i) {
161 EXPECT_TRUE(IsSafePortableRelativePath(
162 base::FilePath(kSafePortableRelativePaths[i])))
163 << kSafePortableRelativePaths[i];
164 EXPECT_TRUE(IsSafePortableRelativePath(
165 safe_dirname.Append(base::FilePath(kSafePortableRelativePaths[i]))))
166 << kSafePortableRelativePaths[i];
168 for (size_t i = 0; i < arraysize(kUnsafePortableBasenames); ++i) {
169 EXPECT_FALSE(
170 IsSafePortableRelativePath(base::FilePath(kUnsafePortableBasenames[i])))
171 << kUnsafePortableBasenames[i];
172 if (!base::FilePath::StringType(kUnsafePortableBasenames[i]).empty()) {
173 EXPECT_FALSE(IsSafePortableRelativePath(
174 safe_dirname.Append(base::FilePath(kUnsafePortableBasenames[i]))))
175 << kUnsafePortableBasenames[i];
180 TEST(FilenameUtilTest, FileURLConversion) {
181 // a list of test file names and the corresponding URLs
182 const FileCase round_trip_cases[] = {
183 #if defined(OS_WIN)
184 {L"C:\\foo\\bar.txt", "file:///C:/foo/bar.txt"},
185 {L"\\\\some computer\\foo\\bar.txt",
186 "file://some%20computer/foo/bar.txt"}, // UNC
187 {L"D:\\Name;with%some symbols*#",
188 "file:///D:/Name%3Bwith%25some%20symbols*%23"},
189 // issue 14153: To be tested with the OS default codepage other than 1252.
190 {L"D:\\latin1\\caf\x00E9\x00DD.txt",
191 "file:///D:/latin1/caf%C3%A9%C3%9D.txt"},
192 {L"D:\\otherlatin\\caf\x0119.txt", "file:///D:/otherlatin/caf%C4%99.txt"},
193 {L"D:\\greek\\\x03B1\x03B2\x03B3.txt",
194 "file:///D:/greek/%CE%B1%CE%B2%CE%B3.txt"},
195 {L"D:\\Chinese\\\x6240\x6709\x4e2d\x6587\x7f51\x9875.doc",
196 "file:///D:/Chinese/%E6%89%80%E6%9C%89%E4%B8%AD%E6%96%87%E7%BD%91"
197 "%E9%A1%B5.doc"},
198 {L"D:\\plane1\\\xD835\xDC00\xD835\xDC01.txt", // Math alphabet "AB"
199 "file:///D:/plane1/%F0%9D%90%80%F0%9D%90%81.txt"},
200 #elif defined(OS_POSIX)
201 {L"/foo/bar.txt", "file:///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%3F.txt"},
205 {L"/some computer/foo/bar.txt", "file:///some%20computer/foo/bar.txt"},
206 {L"/Name;with%some symbols*#", "file:///Name%3Bwith%25some%20symbols*%23"},
207 {L"/latin1/caf\x00E9\x00DD.txt", "file:///latin1/caf%C3%A9%C3%9D.txt"},
208 {L"/otherlatin/caf\x0119.txt", "file:///otherlatin/caf%C4%99.txt"},
209 {L"/greek/\x03B1\x03B2\x03B3.txt", "file:///greek/%CE%B1%CE%B2%CE%B3.txt"},
210 {L"/Chinese/\x6240\x6709\x4e2d\x6587\x7f51\x9875.doc",
211 "file:///Chinese/%E6%89%80%E6%9C%89%E4%B8%AD%E6%96%87%E7%BD"
212 "%91%E9%A1%B5.doc"},
213 {L"/plane1/\x1D400\x1D401.txt", // Math alphabet "AB"
214 "file:///plane1/%F0%9D%90%80%F0%9D%90%81.txt"},
215 #endif
218 // First, we'll test that we can round-trip all of the above cases of URLs
219 base::FilePath output;
220 for (size_t i = 0; i < arraysize(round_trip_cases); i++) {
221 // convert to the file URL
222 GURL file_url(
223 FilePathToFileURL(WStringAsFilePath(round_trip_cases[i].file)));
224 EXPECT_EQ(round_trip_cases[i].url, file_url.spec());
226 // Back to the filename.
227 EXPECT_TRUE(FileURLToFilePath(file_url, &output));
228 EXPECT_EQ(round_trip_cases[i].file, FilePathAsWString(output));
231 // Test that various file: URLs get decoded into the correct file type
232 FileCase url_cases[] = {
233 #if defined(OS_WIN)
234 {L"C:\\foo\\bar.txt", "file:c|/foo\\bar.txt"},
235 {L"C:\\foo\\bar.txt", "file:/c:/foo/bar.txt"},
236 {L"\\\\foo\\bar.txt", "file://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"\\\\foo\\bar.txt", "file:/foo/bar.txt"},
240 {L"\\\\foo\\bar.txt", "file://foo\\bar.txt"},
241 {L"C:\\foo\\bar.txt", "file:\\\\\\c:/foo/bar.txt"},
242 #elif defined(OS_POSIX)
243 {L"/c:/foo/bar.txt", "file:/c:/foo/bar.txt"},
244 {L"/c:/foo/bar.txt", "file:///c:/foo/bar.txt"},
245 {L"/foo/bar.txt", "file:/foo/bar.txt"},
246 {L"/c:/foo/bar.txt", "file:\\\\\\c:/foo/bar.txt"},
247 {L"/foo/bar.txt", "file:foo/bar.txt"},
248 {L"/bar.txt", "file://foo/bar.txt"},
249 {L"/foo/bar.txt", "file:///foo/bar.txt"},
250 {L"/foo/bar.txt", "file:////foo/bar.txt"},
251 {L"/foo/bar.txt", "file:////foo//bar.txt"},
252 {L"/foo/bar.txt", "file:////foo///bar.txt"},
253 {L"/foo/bar.txt", "file:////foo////bar.txt"},
254 {L"/c:/foo/bar.txt", "file:\\\\\\c:/foo/bar.txt"},
255 {L"/c:/foo/bar.txt", "file:c:/foo/bar.txt"},
256 // We get these wrong because GURL turns back slashes into forward
257 // slashes.
258 // {L"/foo%5Cbar.txt", "file://foo\\bar.txt"},
259 // {L"/c|/foo%5Cbar.txt", "file:c|/foo\\bar.txt"},
260 // {L"/foo%5Cbar.txt", "file://foo\\bar.txt"},
261 // {L"/foo%5Cbar.txt", "file:////foo\\bar.txt"},
262 // {L"/foo%5Cbar.txt", "file://foo\\bar.txt"},
263 #endif
265 for (size_t i = 0; i < arraysize(url_cases); i++) {
266 FileURLToFilePath(GURL(url_cases[i].url), &output);
267 EXPECT_EQ(url_cases[i].file, FilePathAsWString(output));
270 // Unfortunately, UTF8ToWide discards invalid UTF8 input.
271 #ifdef BUG_878908_IS_FIXED
272 // Test that no conversion happens if the UTF-8 input is invalid, and that
273 // the input is preserved in UTF-8
274 const char invalid_utf8[] = "file:///d:/Blah/\xff.doc";
275 const wchar_t invalid_wide[] = L"D:\\Blah\\\xff.doc";
276 EXPECT_TRUE(FileURLToFilePath(GURL(std::string(invalid_utf8)), &output));
277 EXPECT_EQ(std::wstring(invalid_wide), output);
278 #endif
280 // Test that if a file URL is malformed, we get a failure
281 EXPECT_FALSE(FileURLToFilePath(GURL("filefoobar"), &output));
284 #if defined(OS_WIN)
285 #define JPEG_EXT L".jpg"
286 #define HTML_EXT L".htm"
287 #elif defined(OS_MACOSX)
288 #define JPEG_EXT L".jpeg"
289 #define HTML_EXT L".html"
290 #else
291 #define JPEG_EXT L".jpg"
292 #define HTML_EXT L".html"
293 #endif
294 #define TXT_EXT L".txt"
295 #define TAR_EXT L".tar"
297 TEST(FilenameUtilTest, GenerateSafeFileName) {
298 const struct {
299 const char* mime_type;
300 const base::FilePath::CharType* filename;
301 const base::FilePath::CharType* expected_filename;
302 } safe_tests[] = {
303 #if defined(OS_WIN)
304 {"text/html",
305 FILE_PATH_LITERAL("C:\\foo\\bar.htm"),
306 FILE_PATH_LITERAL("C:\\foo\\bar.htm")},
307 {"text/html",
308 FILE_PATH_LITERAL("C:\\foo\\bar.html"),
309 FILE_PATH_LITERAL("C:\\foo\\bar.html")},
310 {"text/html",
311 FILE_PATH_LITERAL("C:\\foo\\bar"),
312 FILE_PATH_LITERAL("C:\\foo\\bar.htm")},
313 {"image/png",
314 FILE_PATH_LITERAL("C:\\bar.html"),
315 FILE_PATH_LITERAL("C:\\bar.html")},
316 {"image/png",
317 FILE_PATH_LITERAL("C:\\bar"),
318 FILE_PATH_LITERAL("C:\\bar.png")},
319 {"text/html",
320 FILE_PATH_LITERAL("C:\\foo\\bar.exe"),
321 FILE_PATH_LITERAL("C:\\foo\\bar.exe")},
322 {"image/gif",
323 FILE_PATH_LITERAL("C:\\foo\\bar.exe"),
324 FILE_PATH_LITERAL("C:\\foo\\bar.exe")},
325 {"text/html",
326 FILE_PATH_LITERAL("C:\\foo\\google.com"),
327 FILE_PATH_LITERAL("C:\\foo\\google.com")},
328 {"text/html",
329 FILE_PATH_LITERAL("C:\\foo\\con.htm"),
330 FILE_PATH_LITERAL("C:\\foo\\_con.htm")},
331 {"text/html",
332 FILE_PATH_LITERAL("C:\\foo\\con"),
333 FILE_PATH_LITERAL("C:\\foo\\_con.htm")},
334 {"text/html",
335 FILE_PATH_LITERAL("C:\\foo\\harmless.{not-really-this-may-be-a-guid}"),
336 FILE_PATH_LITERAL("C:\\foo\\harmless.download")},
337 {"text/html",
338 FILE_PATH_LITERAL("C:\\foo\\harmless.local"),
339 FILE_PATH_LITERAL("C:\\foo\\harmless.download")},
340 {"text/html",
341 FILE_PATH_LITERAL("C:\\foo\\harmless.lnk"),
342 FILE_PATH_LITERAL("C:\\foo\\harmless.download")},
343 {"text/html",
344 FILE_PATH_LITERAL("C:\\foo\\harmless.{mismatched-"),
345 FILE_PATH_LITERAL("C:\\foo\\harmless.{mismatched-")},
346 // Allow extension synonyms.
347 {"image/jpeg",
348 FILE_PATH_LITERAL("C:\\foo\\bar.jpg"),
349 FILE_PATH_LITERAL("C:\\foo\\bar.jpg")},
350 {"image/jpeg",
351 FILE_PATH_LITERAL("C:\\foo\\bar.jpeg"),
352 FILE_PATH_LITERAL("C:\\foo\\bar.jpeg")},
353 #else // !defined(OS_WIN)
354 {"text/html",
355 FILE_PATH_LITERAL("/foo/bar.htm"),
356 FILE_PATH_LITERAL("/foo/bar.htm")},
357 {"text/html",
358 FILE_PATH_LITERAL("/foo/bar.html"),
359 FILE_PATH_LITERAL("/foo/bar.html")},
360 {"text/html",
361 FILE_PATH_LITERAL("/foo/bar"),
362 FILE_PATH_LITERAL("/foo/bar.html")},
363 {"image/png",
364 FILE_PATH_LITERAL("/bar.html"),
365 FILE_PATH_LITERAL("/bar.html")},
366 {"image/png", FILE_PATH_LITERAL("/bar"), FILE_PATH_LITERAL("/bar.png")},
367 {"image/gif",
368 FILE_PATH_LITERAL("/foo/bar.exe"),
369 FILE_PATH_LITERAL("/foo/bar.exe")},
370 {"text/html",
371 FILE_PATH_LITERAL("/foo/google.com"),
372 FILE_PATH_LITERAL("/foo/google.com")},
373 {"text/html",
374 FILE_PATH_LITERAL("/foo/con.htm"),
375 FILE_PATH_LITERAL("/foo/con.htm")},
376 {"text/html",
377 FILE_PATH_LITERAL("/foo/con"),
378 FILE_PATH_LITERAL("/foo/con.html")},
379 // Allow extension synonyms.
380 {"image/jpeg",
381 FILE_PATH_LITERAL("/bar.jpg"),
382 FILE_PATH_LITERAL("/bar.jpg")},
383 {"image/jpeg",
384 FILE_PATH_LITERAL("/bar.jpeg"),
385 FILE_PATH_LITERAL("/bar.jpeg")},
386 #endif // !defined(OS_WIN)
389 for (size_t i = 0; i < arraysize(safe_tests); ++i) {
390 base::FilePath file_path(safe_tests[i].filename);
391 GenerateSafeFileName(safe_tests[i].mime_type, false, &file_path);
392 EXPECT_EQ(safe_tests[i].expected_filename, file_path.value())
393 << "Iteration " << i;
397 TEST(FilenameUtilTest, GenerateFileName) {
398 // Tests whether the correct filename is selected from the the given
399 // parameters and that Content-Disposition headers are properly
400 // handled including failovers when the header is malformed.
401 const GenerateFilenameCase selection_tests[] = {
402 {__LINE__,
403 "http://www.google.com/",
404 "attachment; filename=test.html",
408 L"",
409 L"test.html"},
410 {__LINE__,
411 "http://www.google.com/",
412 "attachment; filename=\"test.html\"",
416 L"",
417 L"test.html"},
418 {__LINE__,
419 "http://www.google.com/",
420 "attachment; filename= \"test.html\"",
424 L"",
425 L"test.html"},
426 {__LINE__,
427 "http://www.google.com/",
428 "attachment; filename = \"test.html\"",
432 L"",
433 L"test.html"},
434 {// filename is whitespace. Should failover to URL host
435 __LINE__,
436 "http://www.google.com/",
437 "attachment; filename= ",
441 L"",
442 L"www.google.com"},
443 {// No filename.
444 __LINE__,
445 "http://www.google.com/path/test.html",
446 "attachment",
450 L"",
451 L"test.html"},
452 {// Ditto
453 __LINE__,
454 "http://www.google.com/path/test.html",
455 "attachment;",
459 L"",
460 L"test.html"},
461 {// No C-D
462 __LINE__,
463 "http://www.google.com/",
468 L"",
469 L"www.google.com"},
470 {__LINE__,
471 "http://www.google.com/test.html",
476 L"",
477 L"test.html"},
478 {// Now that we use src/url's ExtractFileName, this case falls back to
479 // the hostname. If this behavior is not desirable, we'd better change
480 // ExtractFileName (in url_parse.cc).
481 __LINE__,
482 "http://www.google.com/path/",
487 L"",
488 L"www.google.com"},
489 {__LINE__, "http://www.google.com/path", "", "", "", "", L"", L"path"},
490 {__LINE__, "file:///", "", "", "", "", L"", L"download"},
491 {__LINE__, "file:///path/testfile", "", "", "", "", L"", L"testfile"},
492 {__LINE__, "non-standard-scheme:", "", "", "", "", L"", L"download"},
493 {// C-D should override default
494 __LINE__,
495 "http://www.google.com/",
496 "attachment; filename =\"test.html\"",
500 L"download",
501 L"test.html"},
502 {// But the URL shouldn't
503 __LINE__,
504 "http://www.google.com/",
509 L"download",
510 L"download"},
511 {__LINE__,
512 "http://www.google.com/",
513 "attachment; filename=\"../test.html\"",
517 L"",
518 L"-test.html"},
519 {__LINE__,
520 "http://www.google.com/",
521 "attachment; filename=\"..\\test.html\"",
525 L"",
526 L"test.html"},
527 {__LINE__,
528 "http://www.google.com/",
529 "attachment; filename=\"..\\\\test.html\"",
533 L"",
534 L"-test.html"},
535 {// Filename disappears after leading and trailing periods are removed.
536 __LINE__,
537 "http://www.google.com/",
538 "attachment; filename=\"..\"",
542 L"default",
543 L"default"},
544 {// C-D specified filename disappears. Failover to final filename.
545 __LINE__,
546 "http://www.google.com/test.html",
547 "attachment; filename=\"..\"",
551 L"default",
552 L"default"},
553 // Below is a small subset of cases taken from HttpContentDisposition tests.
554 {__LINE__,
555 "http://www.google.com/",
556 "attachment; filename=\"%EC%98%88%EC%88%A0%20"
557 "%EC%98%88%EC%88%A0.jpg\"",
561 L"",
562 L"\uc608\uc220 \uc608\uc220.jpg"},
563 {__LINE__,
564 "http://www.google.com/%EC%98%88%EC%88%A0%20%EC%98%88%EC%88%A0.jpg",
569 L"download",
570 L"\uc608\uc220 \uc608\uc220.jpg"},
571 {__LINE__,
572 "http://www.google.com/",
573 "attachment;",
577 L"\uB2E4\uC6B4\uB85C\uB4DC",
578 L"\uB2E4\uC6B4\uB85C\uB4DC"},
579 {__LINE__,
580 "http://www.google.com/",
581 "attachment; filename=\"=?EUC-JP?Q?=B7=DD=BD="
582 "D13=2Epng?=\"",
586 L"download",
587 L"\u82b8\u88533.png"},
588 {__LINE__,
589 "http://www.example.com/images?id=3",
590 "attachment; filename=caf\xc3\xa9.png",
591 "iso-8859-1",
594 L"",
595 L"caf\u00e9.png"},
596 {__LINE__,
597 "http://www.example.com/images?id=3",
598 "attachment; filename=caf\xe5.png",
599 "windows-1253",
602 L"",
603 L"caf\u03b5.png"},
604 {// Invalid C-D header. Name value is skipped now.
605 __LINE__,
606 "http://www.example.com/file?id=3",
607 "attachment; name=\xcf\xc2\xd4\xd8.zip",
608 "GBK",
611 L"",
612 L"file"},
613 {// Invalid C-D header. Extracts filename from url.
614 __LINE__,
615 "http://www.google.com/test.html",
616 "attachment; filename==?iiso88591?Q?caf=EG?=",
620 L"",
621 L"test.html"},
622 // about: and data: URLs
623 {__LINE__, "about:chrome", "", "", "", "", L"", L"download"},
624 {__LINE__, "data:,looks/like/a.path", "", "", "", "", L"", L"download"},
625 {__LINE__,
626 "data:text/plain;base64,VG8gYmUgb3Igbm90IHRvIGJlLg=",
631 L"",
632 L"download"},
633 {__LINE__,
634 "data:,looks/like/a.path",
639 L"default_filename_is_given",
640 L"default_filename_is_given"},
641 {__LINE__,
642 "data:,looks/like/a.path",
647 L"\u65e5\u672c\u8a9e", // Japanese Kanji.
648 L"\u65e5\u672c\u8a9e"},
649 {// The filename encoding is specified by the referrer charset.
650 __LINE__,
651 "http://example.com/V%FDvojov%E1%20psychologie.doc",
653 "iso-8859-1",
656 L"",
657 L"V\u00fdvojov\u00e1 psychologie.doc"},
658 {// Suggested filename takes precedence over URL
659 __LINE__,
660 "http://www.google.com/test",
663 "suggested",
665 L"",
666 L"suggested"},
667 {// The content-disposition has higher precedence over the suggested name.
668 __LINE__,
669 "http://www.google.com/test",
670 "attachment; filename=test.html",
672 "suggested",
674 L"",
675 L"test.html"},
676 {__LINE__,
677 "http://www.google.com/test",
678 "attachment; filename=test",
679 "utf-8",
681 "image/png",
682 L"",
683 L"test"},
684 #if 0
685 { // The filename encoding doesn't match the referrer charset, the system
686 // charset, or UTF-8.
687 // TODO(jshin): we need to handle this case.
688 __LINE__,
689 "http://example.com/V%FDvojov%E1%20psychologie.doc",
691 "utf-8",
694 L"",
695 L"V\u00fdvojov\u00e1 psychologie.doc",
697 #endif
698 // Raw 8bit characters in C-D
699 {__LINE__,
700 "http://www.example.com/images?id=3",
701 "attachment; filename=caf\xc3\xa9.png",
702 "iso-8859-1",
704 "image/png",
705 L"",
706 L"caf\u00e9.png"},
707 {__LINE__,
708 "http://www.example.com/images?id=3",
709 "attachment; filename=caf\xe5.png",
710 "windows-1253",
712 "image/png",
713 L"",
714 L"caf\u03b5.png"},
715 {// No 'filename' keyword in the disposition, use the URL
716 __LINE__,
717 "http://www.evil.com/my_download.txt",
718 "a_file_name.txt",
721 "text/plain",
722 L"download",
723 L"my_download.txt"},
724 {// Spaces in the disposition file name
725 __LINE__,
726 "http://www.frontpagehacker.com/a_download.exe",
727 "filename=My Downloaded File.exe",
730 "application/octet-stream",
731 L"download",
732 L"My Downloaded File.exe"},
733 {// % encoded
734 __LINE__,
735 "http://www.examples.com/",
736 "attachment; "
737 "filename=\"%EC%98%88%EC%88%A0%20%EC%98%88%EC%88%A0.jpg\"",
740 "image/jpeg",
741 L"download",
742 L"\uc608\uc220 \uc608\uc220.jpg"},
743 {// Invalid C-D header. Name value is skipped now.
744 __LINE__,
745 "http://www.examples.com/q.cgi?id=abc",
746 "attachment; name=abc de.pdf",
749 "application/octet-stream",
750 L"download",
751 L"q.cgi"},
752 {__LINE__,
753 "http://www.example.com/path",
754 "filename=\"=?EUC-JP?Q?=B7=DD=BD=D13=2Epng?=\"",
757 "image/png",
758 L"download",
759 L"\x82b8\x8853"
760 L"3.png"},
761 {// The following two have invalid CD headers and filenames come from the
762 // URL.
763 __LINE__,
764 "http://www.example.com/test%20123",
765 "attachment; filename==?iiso88591?Q?caf=EG?=",
768 "image/jpeg",
769 L"download",
770 L"test 123" JPEG_EXT},
771 {__LINE__,
772 "http://www.google.com/%EC%98%88%EC%88%A0%20%EC%98%88%EC%88%A0.jpg",
773 "malformed_disposition",
776 "image/jpeg",
777 L"download",
778 L"\uc608\uc220 \uc608\uc220.jpg"},
779 {// Invalid C-D. No filename from URL. Falls back to 'download'.
780 __LINE__,
781 "http://www.google.com/path1/path2/",
782 "attachment; filename==?iso88591?Q?caf=E3?",
785 "image/jpeg",
786 L"download",
787 L"download" JPEG_EXT},
790 // Tests filename generation. Once the correct filename is
791 // selected, they should be passed through the validation steps and
792 // a correct extension should be added if necessary.
793 const GenerateFilenameCase generation_tests[] = {
794 // Dotfiles. Ensures preceeding period(s) stripped.
795 {__LINE__,
796 "http://www.google.com/.test.html",
801 L"",
802 L"test.html"},
803 {__LINE__, "http://www.google.com/.test", "", "", "", "", L"", L"test"},
804 {__LINE__, "http://www.google.com/..test", "", "", "", "", L"", L"test"},
805 {// Disposition has relative paths, remove directory separators
806 __LINE__,
807 "http://www.evil.com/my_download.txt",
808 "filename=../../../../././../a_file_name.txt",
811 "text/plain",
812 L"download",
813 L"-..-..-..-.-.-..-a_file_name.txt"},
814 {// Disposition has parent directories, remove directory separators
815 __LINE__,
816 "http://www.evil.com/my_download.txt",
817 "filename=dir1/dir2/a_file_name.txt",
820 "text/plain",
821 L"download",
822 L"dir1-dir2-a_file_name.txt"},
823 {// Disposition has relative paths, remove directory separators
824 __LINE__,
825 "http://www.evil.com/my_download.txt",
826 "filename=..\\..\\..\\..\\.\\.\\..\\a_file_name.txt",
829 "text/plain",
830 L"download",
831 L"-..-..-..-.-.-..-a_file_name.txt"},
832 {// Disposition has parent directories, remove directory separators
833 __LINE__,
834 "http://www.evil.com/my_download.txt",
835 "filename=dir1\\dir2\\a_file_name.txt",
838 "text/plain",
839 L"download",
840 L"dir1-dir2-a_file_name.txt"},
841 {// No useful information in disposition or URL, use default
842 __LINE__,
843 "http://www.truncated.com/path/",
847 "text/plain",
848 L"download",
849 L"download" TXT_EXT},
850 {// Filename looks like HTML?
851 __LINE__,
852 "http://www.evil.com/get/malware/here",
853 "filename=\"<blink>Hello kitty</blink>\"",
856 "text/plain",
857 L"default",
858 L"-blink-Hello kitty--blink-"},
859 {// A normal avi should get .avi and not .avi.avi
860 __LINE__,
861 "https://blah.google.com/misc/2.avi",
865 "video/x-msvideo",
866 L"download",
867 L"2.avi"},
868 {// Extension generation
869 __LINE__,
870 "http://www.example.com/my-cat",
871 "filename=my-cat",
874 "image/jpeg",
875 L"download",
876 L"my-cat"},
877 {__LINE__,
878 "http://www.example.com/my-cat",
879 "filename=my-cat",
882 "text/plain",
883 L"download",
884 L"my-cat"},
885 {__LINE__,
886 "http://www.example.com/my-cat",
887 "filename=my-cat",
890 "text/html",
891 L"download",
892 L"my-cat"},
893 {// Unknown MIME type
894 __LINE__,
895 "http://www.example.com/my-cat",
896 "filename=my-cat",
899 "dance/party",
900 L"download",
901 L"my-cat"},
902 {__LINE__,
903 "http://www.example.com/my-cat.jpg",
904 "filename=my-cat.jpg",
907 "text/plain",
908 L"download",
909 L"my-cat.jpg"},
910 // Windows specific tests
911 #if defined(OS_WIN)
912 {__LINE__,
913 "http://www.goodguy.com/evil.exe",
914 "filename=evil.exe",
917 "image/jpeg",
918 L"download",
919 L"evil.exe"},
920 {__LINE__,
921 "http://www.goodguy.com/ok.exe",
922 "filename=ok.exe",
925 "binary/octet-stream",
926 L"download",
927 L"ok.exe"},
928 {__LINE__,
929 "http://www.goodguy.com/evil.dll",
930 "filename=evil.dll",
933 "dance/party",
934 L"download",
935 L"evil.dll"},
936 {__LINE__,
937 "http://www.goodguy.com/evil.exe",
938 "filename=evil",
941 "application/rss+xml",
942 L"download",
943 L"evil"},
944 // Test truncation of trailing dots and spaces
945 {__LINE__,
946 "http://www.goodguy.com/evil.exe ",
947 "filename=evil.exe ",
950 "binary/octet-stream",
951 L"download",
952 L"evil.exe"},
953 {__LINE__,
954 "http://www.goodguy.com/evil.exe.",
955 "filename=evil.exe.",
958 "binary/octet-stream",
959 L"download",
960 L"evil.exe-"},
961 {__LINE__,
962 "http://www.goodguy.com/evil.exe. . .",
963 "filename=evil.exe. . .",
966 "binary/octet-stream",
967 L"download",
968 L"evil.exe-------"},
969 {__LINE__,
970 "http://www.goodguy.com/evil.",
971 "filename=evil.",
974 "binary/octet-stream",
975 L"download",
976 L"evil-"},
977 {__LINE__,
978 "http://www.goodguy.com/. . . . .",
979 "filename=. . . . .",
982 "binary/octet-stream",
983 L"download",
984 L"download"},
985 {__LINE__,
986 "http://www.badguy.com/attachment?name=meh.exe%C2%A0",
987 "attachment; filename=\"meh.exe\xC2\xA0\"",
990 "binary/octet-stream",
991 L"",
992 L"meh.exe-"},
993 #endif // OS_WIN
994 {__LINE__,
995 "http://www.goodguy.com/utils.js",
996 "filename=utils.js",
999 "application/x-javascript",
1000 L"download",
1001 L"utils.js"},
1002 {__LINE__,
1003 "http://www.goodguy.com/contacts.js",
1004 "filename=contacts.js",
1007 "application/json",
1008 L"download",
1009 L"contacts.js"},
1010 {__LINE__,
1011 "http://www.goodguy.com/utils.js",
1012 "filename=utils.js",
1015 "text/javascript",
1016 L"download",
1017 L"utils.js"},
1018 {__LINE__,
1019 "http://www.goodguy.com/utils.js",
1020 "filename=utils.js",
1023 "text/javascript;version=2",
1024 L"download",
1025 L"utils.js"},
1026 {__LINE__,
1027 "http://www.goodguy.com/utils.js",
1028 "filename=utils.js",
1031 "application/ecmascript",
1032 L"download",
1033 L"utils.js"},
1034 {__LINE__,
1035 "http://www.goodguy.com/utils.js",
1036 "filename=utils.js",
1039 "application/ecmascript;version=4",
1040 L"download",
1041 L"utils.js"},
1042 {__LINE__,
1043 "http://www.goodguy.com/program.exe",
1044 "filename=program.exe",
1047 "application/foo-bar",
1048 L"download",
1049 L"program.exe"},
1050 {__LINE__,
1051 "http://www.evil.com/../foo.txt",
1052 "filename=../foo.txt",
1055 "text/plain",
1056 L"download",
1057 L"-foo.txt"},
1058 {__LINE__,
1059 "http://www.evil.com/..\\foo.txt",
1060 "filename=..\\foo.txt",
1063 "text/plain",
1064 L"download",
1065 L"-foo.txt"},
1066 {__LINE__,
1067 "http://www.evil.com/.hidden",
1068 "filename=.hidden",
1071 "text/plain",
1072 L"download",
1073 L"hidden"},
1074 {__LINE__,
1075 "http://www.evil.com/trailing.",
1076 "filename=trailing.",
1079 "dance/party",
1080 L"download",
1081 #if defined(OS_WIN)
1082 L"trailing-"
1083 #else
1084 L"trailing"
1085 #endif
1087 {__LINE__,
1088 "http://www.evil.com/trailing.",
1089 "filename=trailing.",
1092 "text/plain",
1093 L"download",
1094 #if defined(OS_WIN)
1095 L"trailing-"
1096 #else
1097 L"trailing"
1098 #endif
1100 {__LINE__,
1101 "http://www.evil.com/.",
1102 "filename=.",
1105 "dance/party",
1106 L"download",
1107 L"download"},
1108 {__LINE__,
1109 "http://www.evil.com/..",
1110 "filename=..",
1113 "dance/party",
1114 L"download",
1115 L"download"},
1116 {__LINE__,
1117 "http://www.evil.com/...",
1118 "filename=...",
1121 "dance/party",
1122 L"download",
1123 L"download"},
1124 {// Note that this one doesn't have "filename=" on it.
1125 __LINE__,
1126 "http://www.evil.com/",
1127 "a_file_name.txt",
1130 "image/jpeg",
1131 L"download",
1132 L"download" JPEG_EXT},
1133 {__LINE__,
1134 "http://www.evil.com/",
1135 "filename=",
1138 "image/jpeg",
1139 L"download",
1140 L"download" JPEG_EXT},
1141 {__LINE__,
1142 "http://www.example.com/simple",
1143 "filename=simple",
1146 "application/octet-stream",
1147 L"download",
1148 L"simple"},
1149 // Reserved words on Windows
1150 {__LINE__,
1151 "http://www.goodguy.com/COM1",
1152 "filename=COM1",
1155 "application/foo-bar",
1156 L"download",
1157 #if defined(OS_WIN)
1158 L"_COM1"
1159 #else
1160 L"COM1"
1161 #endif
1163 {__LINE__,
1164 "http://www.goodguy.com/COM4.txt",
1165 "filename=COM4.txt",
1168 "text/plain",
1169 L"download",
1170 #if defined(OS_WIN)
1171 L"_COM4.txt"
1172 #else
1173 L"COM4.txt"
1174 #endif
1176 {__LINE__,
1177 "http://www.goodguy.com/lpt1.TXT",
1178 "filename=lpt1.TXT",
1181 "text/plain",
1182 L"download",
1183 #if defined(OS_WIN)
1184 L"_lpt1.TXT"
1185 #else
1186 L"lpt1.TXT"
1187 #endif
1189 {__LINE__,
1190 "http://www.goodguy.com/clock$.txt",
1191 "filename=clock$.txt",
1194 "text/plain",
1195 L"download",
1196 #if defined(OS_WIN)
1197 L"_clock$.txt"
1198 #else
1199 L"clock$.txt"
1200 #endif
1202 {// Validation should also apply to sugested name
1203 __LINE__,
1204 "http://www.goodguy.com/blah$.txt",
1205 "filename=clock$.txt",
1207 "clock$.txt",
1208 "text/plain",
1209 L"download",
1210 #if defined(OS_WIN)
1211 L"_clock$.txt"
1212 #else
1213 L"clock$.txt"
1214 #endif
1216 {__LINE__,
1217 "http://www.goodguy.com/mycom1.foo",
1218 "filename=mycom1.foo",
1221 "text/plain",
1222 L"download",
1223 L"mycom1.foo"},
1224 {__LINE__,
1225 "http://www.badguy.com/Setup.exe.local",
1226 "filename=Setup.exe.local",
1229 "application/foo-bar",
1230 L"download",
1231 #if defined(OS_WIN)
1232 L"Setup.exe.download"
1233 #else
1234 L"Setup.exe.local"
1235 #endif
1237 {__LINE__,
1238 "http://www.badguy.com/Setup.exe.local",
1239 "filename=Setup.exe.local.local",
1242 "application/foo-bar",
1243 L"download",
1244 #if defined(OS_WIN)
1245 L"Setup.exe.local.download"
1246 #else
1247 L"Setup.exe.local.local"
1248 #endif
1250 {__LINE__,
1251 "http://www.badguy.com/Setup.exe.lnk",
1252 "filename=Setup.exe.lnk",
1255 "application/foo-bar",
1256 L"download",
1257 #if defined(OS_WIN)
1258 L"Setup.exe.download"
1259 #else
1260 L"Setup.exe.lnk"
1261 #endif
1263 {__LINE__,
1264 "http://www.badguy.com/Desktop.ini",
1265 "filename=Desktop.ini",
1268 "application/foo-bar",
1269 L"download",
1270 #if defined(OS_WIN)
1271 L"_Desktop.ini"
1272 #else
1273 L"Desktop.ini"
1274 #endif
1276 {__LINE__,
1277 "http://www.badguy.com/Thumbs.db",
1278 "filename=Thumbs.db",
1281 "application/foo-bar",
1282 L"download",
1283 #if defined(OS_WIN)
1284 L"_Thumbs.db"
1285 #else
1286 L"Thumbs.db"
1287 #endif
1289 {__LINE__,
1290 "http://www.hotmail.com",
1291 "filename=source.jpg",
1294 "application/x-javascript",
1295 L"download",
1296 L"source.jpg"},
1297 {// http://crbug.com/5772.
1298 __LINE__,
1299 "http://www.example.com/foo.tar.gz",
1303 "application/x-tar",
1304 L"download",
1305 L"foo.tar.gz"},
1306 {// http://crbug.com/52250.
1307 __LINE__,
1308 "http://www.example.com/foo.tgz",
1312 "application/x-tar",
1313 L"download",
1314 L"foo.tgz"},
1315 {// http://crbug.com/7337.
1316 __LINE__,
1317 "http://maged.lordaeron.org/blank.reg",
1321 "text/x-registry",
1322 L"download",
1323 L"blank.reg"},
1324 {__LINE__,
1325 "http://www.example.com/bar.tar",
1329 "application/x-tar",
1330 L"download",
1331 L"bar.tar"},
1332 {__LINE__,
1333 "http://www.example.com/bar.bogus",
1337 "application/x-tar",
1338 L"download",
1339 L"bar.bogus"},
1340 {// http://crbug.com/20337
1341 __LINE__,
1342 "http://www.example.com/.download.txt",
1343 "filename=.download.txt",
1346 "text/plain",
1347 L"-download",
1348 L"download.txt"},
1349 {// http://crbug.com/56855.
1350 __LINE__,
1351 "http://www.example.com/bar.sh",
1355 "application/x-sh",
1356 L"download",
1357 L"bar.sh"},
1358 {// http://crbug.com/61571
1359 __LINE__,
1360 "http://www.example.com/npdf.php?fn=foobar.pdf",
1364 "text/plain",
1365 L"download",
1366 L"npdf" TXT_EXT},
1367 {// Shouldn't overwrite C-D specified extension.
1368 __LINE__,
1369 "http://www.example.com/npdf.php?fn=foobar.pdf",
1370 "filename=foobar.jpg",
1373 "text/plain",
1374 L"download",
1375 L"foobar.jpg"},
1376 {// http://crbug.com/87719
1377 __LINE__,
1378 "http://www.example.com/image.aspx?id=blargh",
1382 "image/jpeg",
1383 L"download",
1384 L"image" JPEG_EXT},
1385 {__LINE__,
1386 "http://www.example.com/image.aspx?id=blargh",
1389 " .foo",
1391 L"download",
1392 L"-.foo"},
1393 #if defined(OS_CHROMEOS)
1394 {// http://crosbug.com/26028
1395 __LINE__,
1396 "http://www.example.com/fooa%cc%88.txt",
1400 "image/jpeg",
1401 L"foo\xe4",
1402 L"foo\xe4.txt"},
1403 #endif
1406 for (size_t i = 0; i < arraysize(selection_tests); ++i)
1407 RunGenerateFileNameTestCase(&selection_tests[i]);
1409 for (size_t i = 0; i < arraysize(generation_tests); ++i)
1410 RunGenerateFileNameTestCase(&generation_tests[i]);
1412 for (size_t i = 0; i < arraysize(generation_tests); ++i) {
1413 GenerateFilenameCase test_case = generation_tests[i];
1414 test_case.referrer_charset = "GBK";
1415 RunGenerateFileNameTestCase(&test_case);
1419 TEST(FilenameUtilTest, IsReservedNameOnWindows) {
1420 for (size_t i = 0; i < arraysize(kSafePortableBasenames); ++i) {
1421 EXPECT_FALSE(IsReservedNameOnWindows(
1422 base::FilePath(kSafePortableBasenames[i]).value()))
1423 << kSafePortableBasenames[i];
1426 for (size_t i = 0; i < arraysize(kUnsafePortableBasenamesForWindows); ++i) {
1427 EXPECT_TRUE(IsReservedNameOnWindows(
1428 base::FilePath(kUnsafePortableBasenamesForWindows[i]).value()))
1429 << kUnsafePortableBasenamesForWindows[i];
1433 } // namespace net