Clean up URLFetcher unit tests, part 8.
[chromium-blink-merge.git] / net / base / filename_util_unittest.cc
bloba251a153022df3ba67a1973f042880854734c7e7
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* kSafePortableRelativePaths[] = {
119 FILE_PATH_LITERAL("a/a"),
120 #if defined(OS_WIN)
121 FILE_PATH_LITERAL("a\\a"),
122 #endif
125 TEST(FilenameUtilTest, IsSafePortablePathComponent) {
126 for (size_t i = 0; i < arraysize(kSafePortableBasenames); ++i) {
127 EXPECT_TRUE(
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) {
146 EXPECT_TRUE(
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) {
162 EXPECT_FALSE(
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[] = {
176 #if defined(OS_WIN)
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"
190 "%E9%A1%B5.doc"},
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"
205 "%91%E9%A1%B5.doc"},
206 {L"/plane1/\x1D400\x1D401.txt", // Math alphabet "AB"
207 "file:///plane1/%F0%9D%90%80%F0%9D%90%81.txt"},
208 #endif
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
215 GURL 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[] = {
226 #if defined(OS_WIN)
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
250 // slashes.
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"},
256 #endif
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);
271 #endif
273 // Test that if a file URL is malformed, we get a failure
274 EXPECT_FALSE(FileURLToFilePath(GURL("filefoobar"), &output));
277 #if defined(OS_WIN)
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"
283 #else
284 #define JPEG_EXT L".jpg"
285 #define HTML_EXT L".html"
286 #endif
287 #define TXT_EXT L".txt"
288 #define TAR_EXT L".tar"
290 TEST(FilenameUtilTest, GenerateSafeFileName) {
291 const struct {
292 const char* mime_type;
293 const base::FilePath::CharType* filename;
294 const base::FilePath::CharType* expected_filename;
295 } safe_tests[] = {
296 #if defined(OS_WIN)
297 {"text/html",
298 FILE_PATH_LITERAL("C:\\foo\\bar.htm"),
299 FILE_PATH_LITERAL("C:\\foo\\bar.htm")},
300 {"text/html",
301 FILE_PATH_LITERAL("C:\\foo\\bar.html"),
302 FILE_PATH_LITERAL("C:\\foo\\bar.html")},
303 {"text/html",
304 FILE_PATH_LITERAL("C:\\foo\\bar"),
305 FILE_PATH_LITERAL("C:\\foo\\bar.htm")},
306 {"image/png",
307 FILE_PATH_LITERAL("C:\\bar.html"),
308 FILE_PATH_LITERAL("C:\\bar.html")},
309 {"image/png",
310 FILE_PATH_LITERAL("C:\\bar"),
311 FILE_PATH_LITERAL("C:\\bar.png")},
312 {"text/html",
313 FILE_PATH_LITERAL("C:\\foo\\bar.exe"),
314 FILE_PATH_LITERAL("C:\\foo\\bar.exe")},
315 {"image/gif",
316 FILE_PATH_LITERAL("C:\\foo\\bar.exe"),
317 FILE_PATH_LITERAL("C:\\foo\\bar.exe")},
318 {"text/html",
319 FILE_PATH_LITERAL("C:\\foo\\google.com"),
320 FILE_PATH_LITERAL("C:\\foo\\google.com")},
321 {"text/html",
322 FILE_PATH_LITERAL("C:\\foo\\con.htm"),
323 FILE_PATH_LITERAL("C:\\foo\\_con.htm")},
324 {"text/html",
325 FILE_PATH_LITERAL("C:\\foo\\con"),
326 FILE_PATH_LITERAL("C:\\foo\\_con.htm")},
327 {"text/html",
328 FILE_PATH_LITERAL("C:\\foo\\harmless.{not-really-this-may-be-a-guid}"),
329 FILE_PATH_LITERAL("C:\\foo\\harmless.download")},
330 {"text/html",
331 FILE_PATH_LITERAL("C:\\foo\\harmless.local"),
332 FILE_PATH_LITERAL("C:\\foo\\harmless.download")},
333 {"text/html",
334 FILE_PATH_LITERAL("C:\\foo\\harmless.lnk"),
335 FILE_PATH_LITERAL("C:\\foo\\harmless.download")},
336 {"text/html",
337 FILE_PATH_LITERAL("C:\\foo\\harmless.{mismatched-"),
338 FILE_PATH_LITERAL("C:\\foo\\harmless.{mismatched-")},
339 // Allow extension synonyms.
340 {"image/jpeg",
341 FILE_PATH_LITERAL("C:\\foo\\bar.jpg"),
342 FILE_PATH_LITERAL("C:\\foo\\bar.jpg")},
343 {"image/jpeg",
344 FILE_PATH_LITERAL("C:\\foo\\bar.jpeg"),
345 FILE_PATH_LITERAL("C:\\foo\\bar.jpeg")},
346 #else // !defined(OS_WIN)
347 {"text/html",
348 FILE_PATH_LITERAL("/foo/bar.htm"),
349 FILE_PATH_LITERAL("/foo/bar.htm")},
350 {"text/html",
351 FILE_PATH_LITERAL("/foo/bar.html"),
352 FILE_PATH_LITERAL("/foo/bar.html")},
353 {"text/html",
354 FILE_PATH_LITERAL("/foo/bar"),
355 FILE_PATH_LITERAL("/foo/bar.html")},
356 {"image/png",
357 FILE_PATH_LITERAL("/bar.html"),
358 FILE_PATH_LITERAL("/bar.html")},
359 {"image/png", FILE_PATH_LITERAL("/bar"), FILE_PATH_LITERAL("/bar.png")},
360 {"image/gif",
361 FILE_PATH_LITERAL("/foo/bar.exe"),
362 FILE_PATH_LITERAL("/foo/bar.exe")},
363 {"text/html",
364 FILE_PATH_LITERAL("/foo/google.com"),
365 FILE_PATH_LITERAL("/foo/google.com")},
366 {"text/html",
367 FILE_PATH_LITERAL("/foo/con.htm"),
368 FILE_PATH_LITERAL("/foo/con.htm")},
369 {"text/html",
370 FILE_PATH_LITERAL("/foo/con"),
371 FILE_PATH_LITERAL("/foo/con.html")},
372 // Allow extension synonyms.
373 {"image/jpeg",
374 FILE_PATH_LITERAL("/bar.jpg"),
375 FILE_PATH_LITERAL("/bar.jpg")},
376 {"image/jpeg",
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[] = {
395 {__LINE__,
396 "http://www.google.com/",
397 "attachment; filename=test.html",
401 L"",
402 L"test.html"},
403 {__LINE__,
404 "http://www.google.com/",
405 "attachment; filename=\"test.html\"",
409 L"",
410 L"test.html"},
411 {__LINE__,
412 "http://www.google.com/",
413 "attachment; filename= \"test.html\"",
417 L"",
418 L"test.html"},
419 {__LINE__,
420 "http://www.google.com/",
421 "attachment; filename = \"test.html\"",
425 L"",
426 L"test.html"},
427 {// filename is whitespace. Should failover to URL host
428 __LINE__,
429 "http://www.google.com/",
430 "attachment; filename= ",
434 L"",
435 L"www.google.com"},
436 {// No filename.
437 __LINE__,
438 "http://www.google.com/path/test.html",
439 "attachment",
443 L"",
444 L"test.html"},
445 {// Ditto
446 __LINE__,
447 "http://www.google.com/path/test.html",
448 "attachment;",
452 L"",
453 L"test.html"},
454 {// No C-D
455 __LINE__,
456 "http://www.google.com/",
461 L"",
462 L"www.google.com"},
463 {__LINE__,
464 "http://www.google.com/test.html",
469 L"",
470 L"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).
474 __LINE__,
475 "http://www.google.com/path/",
480 L"",
481 L"www.google.com"},
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
487 __LINE__,
488 "http://www.google.com/",
489 "attachment; filename =\"test.html\"",
493 L"download",
494 L"test.html"},
495 {// But the URL shouldn't
496 __LINE__,
497 "http://www.google.com/",
502 L"download",
503 L"download"},
504 {__LINE__,
505 "http://www.google.com/",
506 "attachment; filename=\"../test.html\"",
510 L"",
511 L"-test.html"},
512 {__LINE__,
513 "http://www.google.com/",
514 "attachment; filename=\"..\\test.html\"",
518 L"",
519 L"test.html"},
520 {__LINE__,
521 "http://www.google.com/",
522 "attachment; filename=\"..\\\\test.html\"",
526 L"",
527 L"-test.html"},
528 {// Filename disappears after leading and trailing periods are removed.
529 __LINE__,
530 "http://www.google.com/",
531 "attachment; filename=\"..\"",
535 L"default",
536 L"default"},
537 {// C-D specified filename disappears. Failover to final filename.
538 __LINE__,
539 "http://www.google.com/test.html",
540 "attachment; filename=\"..\"",
544 L"default",
545 L"default"},
546 // Below is a small subset of cases taken from HttpContentDisposition tests.
547 {__LINE__,
548 "http://www.google.com/",
549 "attachment; filename=\"%EC%98%88%EC%88%A0%20"
550 "%EC%98%88%EC%88%A0.jpg\"",
554 L"",
555 L"\uc608\uc220 \uc608\uc220.jpg"},
556 {__LINE__,
557 "http://www.google.com/%EC%98%88%EC%88%A0%20%EC%98%88%EC%88%A0.jpg",
562 L"download",
563 L"\uc608\uc220 \uc608\uc220.jpg"},
564 {__LINE__,
565 "http://www.google.com/",
566 "attachment;",
570 L"\uB2E4\uC6B4\uB85C\uB4DC",
571 L"\uB2E4\uC6B4\uB85C\uB4DC"},
572 {__LINE__,
573 "http://www.google.com/",
574 "attachment; filename=\"=?EUC-JP?Q?=B7=DD=BD="
575 "D13=2Epng?=\"",
579 L"download",
580 L"\u82b8\u88533.png"},
581 {__LINE__,
582 "http://www.example.com/images?id=3",
583 "attachment; filename=caf\xc3\xa9.png",
584 "iso-8859-1",
587 L"",
588 L"caf\u00e9.png"},
589 {__LINE__,
590 "http://www.example.com/images?id=3",
591 "attachment; filename=caf\xe5.png",
592 "windows-1253",
595 L"",
596 L"caf\u03b5.png"},
597 {// Invalid C-D header. Name value is skipped now.
598 __LINE__,
599 "http://www.example.com/file?id=3",
600 "attachment; name=\xcf\xc2\xd4\xd8.zip",
601 "GBK",
604 L"",
605 L"file"},
606 {// Invalid C-D header. Extracts filename from url.
607 __LINE__,
608 "http://www.google.com/test.html",
609 "attachment; filename==?iiso88591?Q?caf=EG?=",
613 L"",
614 L"test.html"},
615 // about: and data: URLs
616 {__LINE__, "about:chrome", "", "", "", "", L"", L"download"},
617 {__LINE__, "data:,looks/like/a.path", "", "", "", "", L"", L"download"},
618 {__LINE__,
619 "data:text/plain;base64,VG8gYmUgb3Igbm90IHRvIGJlLg=",
624 L"",
625 L"download"},
626 {__LINE__,
627 "data:,looks/like/a.path",
632 L"default_filename_is_given",
633 L"default_filename_is_given"},
634 {__LINE__,
635 "data:,looks/like/a.path",
640 L"\u65e5\u672c\u8a9e", // Japanese Kanji.
641 L"\u65e5\u672c\u8a9e"},
642 {// The filename encoding is specified by the referrer charset.
643 __LINE__,
644 "http://example.com/V%FDvojov%E1%20psychologie.doc",
646 "iso-8859-1",
649 L"",
650 L"V\u00fdvojov\u00e1 psychologie.doc"},
651 {// Suggested filename takes precedence over URL
652 __LINE__,
653 "http://www.google.com/test",
656 "suggested",
658 L"",
659 L"suggested"},
660 {// The content-disposition has higher precedence over the suggested name.
661 __LINE__,
662 "http://www.google.com/test",
663 "attachment; filename=test.html",
665 "suggested",
667 L"",
668 L"test.html"},
669 {__LINE__,
670 "http://www.google.com/test",
671 "attachment; filename=test",
672 "utf-8",
674 "image/png",
675 L"",
676 L"test"},
677 #if 0
678 { // The filename encoding doesn't match the referrer charset, the system
679 // charset, or UTF-8.
680 // TODO(jshin): we need to handle this case.
681 __LINE__,
682 "http://example.com/V%FDvojov%E1%20psychologie.doc",
684 "utf-8",
687 L"",
688 L"V\u00fdvojov\u00e1 psychologie.doc",
690 #endif
691 // Raw 8bit characters in C-D
692 {__LINE__,
693 "http://www.example.com/images?id=3",
694 "attachment; filename=caf\xc3\xa9.png",
695 "iso-8859-1",
697 "image/png",
698 L"",
699 L"caf\u00e9.png"},
700 {__LINE__,
701 "http://www.example.com/images?id=3",
702 "attachment; filename=caf\xe5.png",
703 "windows-1253",
705 "image/png",
706 L"",
707 L"caf\u03b5.png"},
708 {// No 'filename' keyword in the disposition, use the URL
709 __LINE__,
710 "http://www.evil.com/my_download.txt",
711 "a_file_name.txt",
714 "text/plain",
715 L"download",
716 L"my_download.txt"},
717 {// Spaces in the disposition file name
718 __LINE__,
719 "http://www.frontpagehacker.com/a_download.exe",
720 "filename=My Downloaded File.exe",
723 "application/octet-stream",
724 L"download",
725 L"My Downloaded File.exe"},
726 {// % encoded
727 __LINE__,
728 "http://www.examples.com/",
729 "attachment; "
730 "filename=\"%EC%98%88%EC%88%A0%20%EC%98%88%EC%88%A0.jpg\"",
733 "image/jpeg",
734 L"download",
735 L"\uc608\uc220 \uc608\uc220.jpg"},
736 {// Invalid C-D header. Name value is skipped now.
737 __LINE__,
738 "http://www.examples.com/q.cgi?id=abc",
739 "attachment; name=abc de.pdf",
742 "application/octet-stream",
743 L"download",
744 L"q.cgi"},
745 {__LINE__,
746 "http://www.example.com/path",
747 "filename=\"=?EUC-JP?Q?=B7=DD=BD=D13=2Epng?=\"",
750 "image/png",
751 L"download",
752 L"\x82b8\x8853"
753 L"3.png"},
754 {// The following two have invalid CD headers and filenames come from the
755 // URL.
756 __LINE__,
757 "http://www.example.com/test%20123",
758 "attachment; filename==?iiso88591?Q?caf=EG?=",
761 "image/jpeg",
762 L"download",
763 L"test 123" JPEG_EXT},
764 {__LINE__,
765 "http://www.google.com/%EC%98%88%EC%88%A0%20%EC%98%88%EC%88%A0.jpg",
766 "malformed_disposition",
769 "image/jpeg",
770 L"download",
771 L"\uc608\uc220 \uc608\uc220.jpg"},
772 {// Invalid C-D. No filename from URL. Falls back to 'download'.
773 __LINE__,
774 "http://www.google.com/path1/path2/",
775 "attachment; filename==?iso88591?Q?caf=E3?",
778 "image/jpeg",
779 L"download",
780 L"download" JPEG_EXT},
783 // Tests filename generation. Once the correct filename is
784 // selected, they should be passed through the validation steps and
785 // a correct extension should be added if necessary.
786 const GenerateFilenameCase generation_tests[] = {
787 // Dotfiles. Ensures preceeding period(s) stripped.
788 {__LINE__,
789 "http://www.google.com/.test.html",
794 L"",
795 L"test.html"},
796 {__LINE__, "http://www.google.com/.test", "", "", "", "", L"", L"test"},
797 {__LINE__, "http://www.google.com/..test", "", "", "", "", L"", L"test"},
798 {// Disposition has relative paths, remove directory separators
799 __LINE__,
800 "http://www.evil.com/my_download.txt",
801 "filename=../../../../././../a_file_name.txt",
804 "text/plain",
805 L"download",
806 L"-..-..-..-.-.-..-a_file_name.txt"},
807 {// Disposition has parent directories, remove directory separators
808 __LINE__,
809 "http://www.evil.com/my_download.txt",
810 "filename=dir1/dir2/a_file_name.txt",
813 "text/plain",
814 L"download",
815 L"dir1-dir2-a_file_name.txt"},
816 {// Disposition has relative paths, remove directory separators
817 __LINE__,
818 "http://www.evil.com/my_download.txt",
819 "filename=..\\..\\..\\..\\.\\.\\..\\a_file_name.txt",
822 "text/plain",
823 L"download",
824 L"-..-..-..-.-.-..-a_file_name.txt"},
825 {// Disposition has parent directories, remove directory separators
826 __LINE__,
827 "http://www.evil.com/my_download.txt",
828 "filename=dir1\\dir2\\a_file_name.txt",
831 "text/plain",
832 L"download",
833 L"dir1-dir2-a_file_name.txt"},
834 {// No useful information in disposition or URL, use default
835 __LINE__,
836 "http://www.truncated.com/path/",
840 "text/plain",
841 L"download",
842 L"download" TXT_EXT},
843 {// Filename looks like HTML?
844 __LINE__,
845 "http://www.evil.com/get/malware/here",
846 "filename=\"<blink>Hello kitty</blink>\"",
849 "text/plain",
850 L"default",
851 L"-blink-Hello kitty--blink-"},
852 {// A normal avi should get .avi and not .avi.avi
853 __LINE__,
854 "https://blah.google.com/misc/2.avi",
858 "video/x-msvideo",
859 L"download",
860 L"2.avi"},
861 {// Extension generation
862 __LINE__,
863 "http://www.example.com/my-cat",
864 "filename=my-cat",
867 "image/jpeg",
868 L"download",
869 L"my-cat"},
870 {__LINE__,
871 "http://www.example.com/my-cat",
872 "filename=my-cat",
875 "text/plain",
876 L"download",
877 L"my-cat"},
878 {__LINE__,
879 "http://www.example.com/my-cat",
880 "filename=my-cat",
883 "text/html",
884 L"download",
885 L"my-cat"},
886 {// Unknown MIME type
887 __LINE__,
888 "http://www.example.com/my-cat",
889 "filename=my-cat",
892 "dance/party",
893 L"download",
894 L"my-cat"},
895 {__LINE__,
896 "http://www.example.com/my-cat.jpg",
897 "filename=my-cat.jpg",
900 "text/plain",
901 L"download",
902 L"my-cat.jpg"},
903 // Windows specific tests
904 #if defined(OS_WIN)
905 {__LINE__,
906 "http://www.goodguy.com/evil.exe",
907 "filename=evil.exe",
910 "image/jpeg",
911 L"download",
912 L"evil.exe"},
913 {__LINE__,
914 "http://www.goodguy.com/ok.exe",
915 "filename=ok.exe",
918 "binary/octet-stream",
919 L"download",
920 L"ok.exe"},
921 {__LINE__,
922 "http://www.goodguy.com/evil.dll",
923 "filename=evil.dll",
926 "dance/party",
927 L"download",
928 L"evil.dll"},
929 {__LINE__,
930 "http://www.goodguy.com/evil.exe",
931 "filename=evil",
934 "application/rss+xml",
935 L"download",
936 L"evil"},
937 // Test truncation of trailing dots and spaces
938 {__LINE__,
939 "http://www.goodguy.com/evil.exe ",
940 "filename=evil.exe ",
943 "binary/octet-stream",
944 L"download",
945 L"evil.exe"},
946 {__LINE__,
947 "http://www.goodguy.com/evil.exe.",
948 "filename=evil.exe.",
951 "binary/octet-stream",
952 L"download",
953 L"evil.exe-"},
954 {__LINE__,
955 "http://www.goodguy.com/evil.exe. . .",
956 "filename=evil.exe. . .",
959 "binary/octet-stream",
960 L"download",
961 L"evil.exe-------"},
962 {__LINE__,
963 "http://www.goodguy.com/evil.",
964 "filename=evil.",
967 "binary/octet-stream",
968 L"download",
969 L"evil-"},
970 {__LINE__,
971 "http://www.goodguy.com/. . . . .",
972 "filename=. . . . .",
975 "binary/octet-stream",
976 L"download",
977 L"download"},
978 {__LINE__,
979 "http://www.badguy.com/attachment?name=meh.exe%C2%A0",
980 "attachment; filename=\"meh.exe\xC2\xA0\"",
983 "binary/octet-stream",
984 L"",
985 L"meh.exe-"},
986 #endif // OS_WIN
987 {__LINE__,
988 "http://www.goodguy.com/utils.js",
989 "filename=utils.js",
992 "application/x-javascript",
993 L"download",
994 L"utils.js"},
995 {__LINE__,
996 "http://www.goodguy.com/contacts.js",
997 "filename=contacts.js",
1000 "application/json",
1001 L"download",
1002 L"contacts.js"},
1003 {__LINE__,
1004 "http://www.goodguy.com/utils.js",
1005 "filename=utils.js",
1008 "text/javascript",
1009 L"download",
1010 L"utils.js"},
1011 {__LINE__,
1012 "http://www.goodguy.com/utils.js",
1013 "filename=utils.js",
1016 "text/javascript;version=2",
1017 L"download",
1018 L"utils.js"},
1019 {__LINE__,
1020 "http://www.goodguy.com/utils.js",
1021 "filename=utils.js",
1024 "application/ecmascript",
1025 L"download",
1026 L"utils.js"},
1027 {__LINE__,
1028 "http://www.goodguy.com/utils.js",
1029 "filename=utils.js",
1032 "application/ecmascript;version=4",
1033 L"download",
1034 L"utils.js"},
1035 {__LINE__,
1036 "http://www.goodguy.com/program.exe",
1037 "filename=program.exe",
1040 "application/foo-bar",
1041 L"download",
1042 L"program.exe"},
1043 {__LINE__,
1044 "http://www.evil.com/../foo.txt",
1045 "filename=../foo.txt",
1048 "text/plain",
1049 L"download",
1050 L"-foo.txt"},
1051 {__LINE__,
1052 "http://www.evil.com/..\\foo.txt",
1053 "filename=..\\foo.txt",
1056 "text/plain",
1057 L"download",
1058 L"-foo.txt"},
1059 {__LINE__,
1060 "http://www.evil.com/.hidden",
1061 "filename=.hidden",
1064 "text/plain",
1065 L"download",
1066 L"hidden"},
1067 {__LINE__,
1068 "http://www.evil.com/trailing.",
1069 "filename=trailing.",
1072 "dance/party",
1073 L"download",
1074 #if defined(OS_WIN)
1075 L"trailing-"
1076 #else
1077 L"trailing"
1078 #endif
1080 {__LINE__,
1081 "http://www.evil.com/trailing.",
1082 "filename=trailing.",
1085 "text/plain",
1086 L"download",
1087 #if defined(OS_WIN)
1088 L"trailing-"
1089 #else
1090 L"trailing"
1091 #endif
1093 {__LINE__,
1094 "http://www.evil.com/.",
1095 "filename=.",
1098 "dance/party",
1099 L"download",
1100 L"download"},
1101 {__LINE__,
1102 "http://www.evil.com/..",
1103 "filename=..",
1106 "dance/party",
1107 L"download",
1108 L"download"},
1109 {__LINE__,
1110 "http://www.evil.com/...",
1111 "filename=...",
1114 "dance/party",
1115 L"download",
1116 L"download"},
1117 {// Note that this one doesn't have "filename=" on it.
1118 __LINE__,
1119 "http://www.evil.com/",
1120 "a_file_name.txt",
1123 "image/jpeg",
1124 L"download",
1125 L"download" JPEG_EXT},
1126 {__LINE__,
1127 "http://www.evil.com/",
1128 "filename=",
1131 "image/jpeg",
1132 L"download",
1133 L"download" JPEG_EXT},
1134 {__LINE__,
1135 "http://www.example.com/simple",
1136 "filename=simple",
1139 "application/octet-stream",
1140 L"download",
1141 L"simple"},
1142 // Reserved words on Windows
1143 {__LINE__,
1144 "http://www.goodguy.com/COM1",
1145 "filename=COM1",
1148 "application/foo-bar",
1149 L"download",
1150 #if defined(OS_WIN)
1151 L"_COM1"
1152 #else
1153 L"COM1"
1154 #endif
1156 {__LINE__,
1157 "http://www.goodguy.com/COM4.txt",
1158 "filename=COM4.txt",
1161 "text/plain",
1162 L"download",
1163 #if defined(OS_WIN)
1164 L"_COM4.txt"
1165 #else
1166 L"COM4.txt"
1167 #endif
1169 {__LINE__,
1170 "http://www.goodguy.com/lpt1.TXT",
1171 "filename=lpt1.TXT",
1174 "text/plain",
1175 L"download",
1176 #if defined(OS_WIN)
1177 L"_lpt1.TXT"
1178 #else
1179 L"lpt1.TXT"
1180 #endif
1182 {__LINE__,
1183 "http://www.goodguy.com/clock$.txt",
1184 "filename=clock$.txt",
1187 "text/plain",
1188 L"download",
1189 #if defined(OS_WIN)
1190 L"_clock$.txt"
1191 #else
1192 L"clock$.txt"
1193 #endif
1195 {// Validation should also apply to sugested name
1196 __LINE__,
1197 "http://www.goodguy.com/blah$.txt",
1198 "filename=clock$.txt",
1200 "clock$.txt",
1201 "text/plain",
1202 L"download",
1203 #if defined(OS_WIN)
1204 L"_clock$.txt"
1205 #else
1206 L"clock$.txt"
1207 #endif
1209 {__LINE__,
1210 "http://www.goodguy.com/mycom1.foo",
1211 "filename=mycom1.foo",
1214 "text/plain",
1215 L"download",
1216 L"mycom1.foo"},
1217 {__LINE__,
1218 "http://www.badguy.com/Setup.exe.local",
1219 "filename=Setup.exe.local",
1222 "application/foo-bar",
1223 L"download",
1224 #if defined(OS_WIN)
1225 L"Setup.exe.download"
1226 #else
1227 L"Setup.exe.local"
1228 #endif
1230 {__LINE__,
1231 "http://www.badguy.com/Setup.exe.local",
1232 "filename=Setup.exe.local.local",
1235 "application/foo-bar",
1236 L"download",
1237 #if defined(OS_WIN)
1238 L"Setup.exe.local.download"
1239 #else
1240 L"Setup.exe.local.local"
1241 #endif
1243 {__LINE__,
1244 "http://www.badguy.com/Setup.exe.lnk",
1245 "filename=Setup.exe.lnk",
1248 "application/foo-bar",
1249 L"download",
1250 #if defined(OS_WIN)
1251 L"Setup.exe.download"
1252 #else
1253 L"Setup.exe.lnk"
1254 #endif
1256 {__LINE__,
1257 "http://www.badguy.com/Desktop.ini",
1258 "filename=Desktop.ini",
1261 "application/foo-bar",
1262 L"download",
1263 #if defined(OS_WIN)
1264 L"_Desktop.ini"
1265 #else
1266 L"Desktop.ini"
1267 #endif
1269 {__LINE__,
1270 "http://www.badguy.com/Thumbs.db",
1271 "filename=Thumbs.db",
1274 "application/foo-bar",
1275 L"download",
1276 #if defined(OS_WIN)
1277 L"_Thumbs.db"
1278 #else
1279 L"Thumbs.db"
1280 #endif
1282 {__LINE__,
1283 "http://www.hotmail.com",
1284 "filename=source.jpg",
1287 "application/x-javascript",
1288 L"download",
1289 L"source.jpg"},
1290 {// http://crbug.com/5772.
1291 __LINE__,
1292 "http://www.example.com/foo.tar.gz",
1296 "application/x-tar",
1297 L"download",
1298 L"foo.tar.gz"},
1299 {// http://crbug.com/52250.
1300 __LINE__,
1301 "http://www.example.com/foo.tgz",
1305 "application/x-tar",
1306 L"download",
1307 L"foo.tgz"},
1308 {// http://crbug.com/7337.
1309 __LINE__,
1310 "http://maged.lordaeron.org/blank.reg",
1314 "text/x-registry",
1315 L"download",
1316 L"blank.reg"},
1317 {__LINE__,
1318 "http://www.example.com/bar.tar",
1322 "application/x-tar",
1323 L"download",
1324 L"bar.tar"},
1325 {__LINE__,
1326 "http://www.example.com/bar.bogus",
1330 "application/x-tar",
1331 L"download",
1332 L"bar.bogus"},
1333 {// http://crbug.com/20337
1334 __LINE__,
1335 "http://www.example.com/.download.txt",
1336 "filename=.download.txt",
1339 "text/plain",
1340 L"-download",
1341 L"download.txt"},
1342 {// http://crbug.com/56855.
1343 __LINE__,
1344 "http://www.example.com/bar.sh",
1348 "application/x-sh",
1349 L"download",
1350 L"bar.sh"},
1351 {// http://crbug.com/61571
1352 __LINE__,
1353 "http://www.example.com/npdf.php?fn=foobar.pdf",
1357 "text/plain",
1358 L"download",
1359 L"npdf" TXT_EXT},
1360 {// Shouldn't overwrite C-D specified extension.
1361 __LINE__,
1362 "http://www.example.com/npdf.php?fn=foobar.pdf",
1363 "filename=foobar.jpg",
1366 "text/plain",
1367 L"download",
1368 L"foobar.jpg"},
1369 {// http://crbug.com/87719
1370 __LINE__,
1371 "http://www.example.com/image.aspx?id=blargh",
1375 "image/jpeg",
1376 L"download",
1377 L"image" JPEG_EXT},
1378 {__LINE__,
1379 "http://www.example.com/image.aspx?id=blargh",
1382 " .foo",
1384 L"download",
1385 L"-.foo"},
1386 #if defined(OS_CHROMEOS)
1387 {// http://crosbug.com/26028
1388 __LINE__,
1389 "http://www.example.com/fooa%cc%88.txt",
1393 "image/jpeg",
1394 L"foo\xe4",
1395 L"foo\xe4.txt"},
1396 #endif
1399 for (size_t i = 0; i < arraysize(selection_tests); ++i)
1400 RunGenerateFileNameTestCase(&selection_tests[i]);
1402 for (size_t i = 0; i < arraysize(generation_tests); ++i)
1403 RunGenerateFileNameTestCase(&generation_tests[i]);
1405 for (size_t i = 0; i < arraysize(generation_tests); ++i) {
1406 GenerateFilenameCase test_case = generation_tests[i];
1407 test_case.referrer_charset = "GBK";
1408 RunGenerateFileNameTestCase(&test_case);
1412 } // namespace net