blob: 664b8bea79cf57ce705788fc32f525f90bde3491 [file] [log] [blame]
Avi Drissman64595482022-09-14 20:52:291// Copyright 2014 The Chromium Authors
[email protected]d96cf752014-04-09 04:05:282// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "net/base/filename_util.h"
6
[email protected]d96cf752014-04-09 04:05:287#include "base/files/file_path.h"
thestigd8df0332014-09-04 06:33:298#include "base/files/file_util.h"
[email protected]d96cf752014-04-09 04:05:289#include "base/strings/string_util.h"
10#include "base/strings/utf_string_conversions.h"
11#include "base/test/test_file_util.h"
Asanka Herath090f40e2018-04-05 20:46:5012#include "build/build_config.h"
Yuta Hijikata101ed2a2020-11-18 07:50:3913#include "build/chromeos_buildflags.h"
Asanka Herath27b7af9f2018-04-03 14:52:1914#include "net/base/mime_util.h"
[email protected]d96cf752014-04-09 04:05:2815#include "testing/gtest/include/gtest/gtest.h"
16#include "url/gurl.h"
17
18namespace net {
19
20namespace {
21
22struct FileCase {
Matt Giucaee5e87f2018-06-07 04:18:4123 const wchar_t* file; // nullptr indicates expected to fail.
[email protected]d96cf752014-04-09 04:05:2824 const char* url;
25};
26
27struct GenerateFilenameCase {
28 int lineno;
29 const char* url;
30 const char* content_disp_header;
31 const char* referrer_charset;
32 const char* suggested_filename;
33 const char* mime_type;
34 const wchar_t* default_filename;
35 const wchar_t* expected_filename;
36};
37
[email protected]b7a95ad62014-08-07 16:53:0138// The expected filenames are coded as wchar_t for convenience.
Jan Wilken Dörrie739ccc212021-03-11 18:13:0539// TODO(https://crbug.com/911896): Make these char16_t once std::u16string is
jdoerrie6312bf62019-02-01 22:03:4240// std::u16string.
[email protected]b7a95ad62014-08-07 16:53:0141std::wstring FilePathAsWString(const base::FilePath& path) {
Xiaohan Wang2a6845b2022-01-08 04:40:5742#if BUILDFLAG(IS_WIN)
Jan Wilken Dörrie9720dce2020-07-21 17:14:2343 return path.value();
Xiaohan Wang2a6845b2022-01-08 04:40:5744#elif BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA)
[email protected]b7a95ad62014-08-07 16:53:0145 return base::UTF8ToWide(path.value());
46#endif
47}
48base::FilePath WStringAsFilePath(const std::wstring& str) {
Xiaohan Wang2a6845b2022-01-08 04:40:5749#if BUILDFLAG(IS_WIN)
Jan Wilken Dörrie9720dce2020-07-21 17:14:2350 return base::FilePath(str);
Xiaohan Wang2a6845b2022-01-08 04:40:5751#elif BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA)
[email protected]b7a95ad62014-08-07 16:53:0152 return base::FilePath(base::WideToUTF8(str));
53#endif
54}
55
halliwellbd52a8a12014-12-12 17:21:0456std::string GetLocaleWarningString() {
Xiaohan Wang2a6845b2022-01-08 04:40:5757#if BUILDFLAG(IS_WIN) || BUILDFLAG(IS_ANDROID)
Fabrice de Gans-Riberi7de47372018-05-08 20:23:4758 return "";
Xiaohan Wang2a6845b2022-01-08 04:40:5759#elif BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA)
halliwellbd52a8a12014-12-12 17:21:0460 // The generate filename tests can fail on certain OS_POSIX platforms when
61 // LC_CTYPE is not "utf8" or "utf-8" because some of the string conversions
62 // fail.
63 // This warning text is appended to any test failures to save people time if
64 // this happens to be the cause of failure :)
65 // Note: some platforms (MACOSX, Chromecast) don't have this problem:
66 // setlocale returns "c" but it functions as utf8. And Android doesn't
67 // have setlocale at all.
Tsuyoshi Horo291961af2022-06-16 08:51:2768 std::string locale = setlocale(LC_CTYPE, nullptr);
halliwellbd52a8a12014-12-12 17:21:0469 return " this test may have failed because the current LC_CTYPE locale is "
70 "not utf8 (currently set to " +
71 locale + ")";
halliwellbd52a8a12014-12-12 17:21:0472#endif
73}
74
[email protected]d96cf752014-04-09 04:05:2875void RunGenerateFileNameTestCase(const GenerateFilenameCase* test_case) {
76 std::string default_filename(base::WideToUTF8(test_case->default_filename));
77 base::FilePath file_path = GenerateFileName(
78 GURL(test_case->url), test_case->content_disp_header,
79 test_case->referrer_charset, test_case->suggested_filename,
80 test_case->mime_type, default_filename);
[email protected]b7a95ad62014-08-07 16:53:0181 EXPECT_EQ(test_case->expected_filename, FilePathAsWString(file_path))
halliwellbd52a8a12014-12-12 17:21:0482 << "test case at line number: " << test_case->lineno << "; "
83 << GetLocaleWarningString();
[email protected]d96cf752014-04-09 04:05:2884}
85
Lei Zhangeb9b3412017-09-25 20:57:3886constexpr const base::FilePath::CharType* kSafePortableBasenames[] = {
87 FILE_PATH_LITERAL("a"), FILE_PATH_LITERAL("a.txt"),
88 FILE_PATH_LITERAL("a b.txt"), FILE_PATH_LITERAL("a-b.txt"),
asanka92a354f2015-01-31 00:37:4089 FILE_PATH_LITERAL("My Computer"),
[email protected]d96cf752014-04-09 04:05:2890};
91
Lei Zhangeb9b3412017-09-25 20:57:3892constexpr const base::FilePath::CharType* kUnsafePortableBasenames[] = {
asanka92a354f2015-01-31 00:37:4093 FILE_PATH_LITERAL(""),
94 FILE_PATH_LITERAL("."),
95 FILE_PATH_LITERAL(".."),
96 FILE_PATH_LITERAL("..."),
97 FILE_PATH_LITERAL("con"),
98 FILE_PATH_LITERAL("con.zip"),
99 FILE_PATH_LITERAL("NUL"),
100 FILE_PATH_LITERAL("NUL.zip"),
101 FILE_PATH_LITERAL(".a"),
102 FILE_PATH_LITERAL("a."),
103 FILE_PATH_LITERAL("a\"a"),
104 FILE_PATH_LITERAL("a<a"),
105 FILE_PATH_LITERAL("a>a"),
106 FILE_PATH_LITERAL("a?a"),
107 FILE_PATH_LITERAL("a/"),
108 FILE_PATH_LITERAL("a\\"),
109 FILE_PATH_LITERAL("a "),
110 FILE_PATH_LITERAL("a . ."),
111 FILE_PATH_LITERAL(" Computer"),
112 FILE_PATH_LITERAL("My Computer.{a}"),
113 FILE_PATH_LITERAL("My Computer.{20D04FE0-3AEA-1069-A2D8-08002B30309D}"),
Xiaohan Wang2a6845b2022-01-08 04:40:57114#if BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA)
asanka92a354f2015-01-31 00:37:40115 FILE_PATH_LITERAL("a\\a"),
[email protected]d96cf752014-04-09 04:05:28116#endif
117};
118
Lei Zhangeb9b3412017-09-25 20:57:38119constexpr const base::FilePath::CharType* kUnsafePortableBasenamesForWin[] = {
120 FILE_PATH_LITERAL("con"), FILE_PATH_LITERAL("con.zip"),
121 FILE_PATH_LITERAL("NUL"), FILE_PATH_LITERAL("NUL.zip"),
dhnishic28dfc32015-07-08 02:21:31122};
123
Lei Zhangeb9b3412017-09-25 20:57:38124constexpr const base::FilePath::CharType* kSafePortableRelativePaths[] = {
asanka13cc94a2015-02-03 20:31:58125 FILE_PATH_LITERAL("a/a"),
Xiaohan Wang2a6845b2022-01-08 04:40:57126#if BUILDFLAG(IS_WIN)
asanka13cc94a2015-02-03 20:31:58127 FILE_PATH_LITERAL("a\\a"),
[email protected]d96cf752014-04-09 04:05:28128#endif
129};
130
Lei Zhangeb9b3412017-09-25 20:57:38131} // namespace
132
[email protected]d96cf752014-04-09 04:05:28133TEST(FilenameUtilTest, IsSafePortablePathComponent) {
Ryan Sleevi4625214fa2018-05-10 16:42:45134 for (auto* basename : kSafePortableBasenames) {
135 EXPECT_TRUE(IsSafePortablePathComponent(base::FilePath(basename)))
136 << basename;
[email protected]d96cf752014-04-09 04:05:28137 }
Ryan Sleevi4625214fa2018-05-10 16:42:45138 for (auto* basename : kUnsafePortableBasenames) {
139 EXPECT_FALSE(IsSafePortablePathComponent(base::FilePath(basename)))
140 << basename;
[email protected]d96cf752014-04-09 04:05:28141 }
Ryan Sleevi4625214fa2018-05-10 16:42:45142 for (auto* path : kSafePortableRelativePaths) {
143 EXPECT_FALSE(IsSafePortablePathComponent(base::FilePath(path))) << path;
[email protected]d96cf752014-04-09 04:05:28144 }
145}
146
147TEST(FilenameUtilTest, IsSafePortableRelativePath) {
148 base::FilePath safe_dirname(FILE_PATH_LITERAL("a"));
Ryan Sleevi4625214fa2018-05-10 16:42:45149 for (auto* basename : kSafePortableBasenames) {
150 EXPECT_TRUE(IsSafePortableRelativePath(base::FilePath(basename)))
151 << basename;
152 EXPECT_TRUE(IsSafePortableRelativePath(
153 safe_dirname.Append(base::FilePath(basename))))
154 << basename;
155 }
156 for (auto* path : kSafePortableRelativePaths) {
157 EXPECT_TRUE(IsSafePortableRelativePath(base::FilePath(path))) << path;
asanka13cc94a2015-02-03 20:31:58158 EXPECT_TRUE(
Ryan Sleevi4625214fa2018-05-10 16:42:45159 IsSafePortableRelativePath(safe_dirname.Append(base::FilePath(path))))
160 << path;
[email protected]d96cf752014-04-09 04:05:28161 }
Ryan Sleevi4625214fa2018-05-10 16:42:45162 for (auto* basename : kUnsafePortableBasenames) {
163 EXPECT_FALSE(IsSafePortableRelativePath(base::FilePath(basename)))
164 << basename;
165 if (!base::FilePath::StringType(basename).empty()) {
asanka13cc94a2015-02-03 20:31:58166 EXPECT_FALSE(IsSafePortableRelativePath(
Ryan Sleevi4625214fa2018-05-10 16:42:45167 safe_dirname.Append(base::FilePath(basename))))
168 << basename;
[email protected]d96cf752014-04-09 04:05:28169 }
170 }
171}
172
173TEST(FilenameUtilTest, FileURLConversion) {
174 // a list of test file names and the corresponding URLs
175 const FileCase round_trip_cases[] = {
Xiaohan Wang2a6845b2022-01-08 04:40:57176#if BUILDFLAG(IS_WIN)
[email protected]d96cf752014-04-09 04:05:28177 {L"C:\\foo\\bar.txt", "file:///C:/foo/bar.txt"},
178 {L"\\\\some computer\\foo\\bar.txt",
asanka13cc94a2015-02-03 20:31:58179 "file://some%20computer/foo/bar.txt"}, // UNC
[email protected]d96cf752014-04-09 04:05:28180 {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"},
asanka13cc94a2015-02-03 20:31:58185 {L"D:\\otherlatin\\caf\x0119.txt", "file:///D:/otherlatin/caf%C4%99.txt"},
[email protected]d96cf752014-04-09 04:05:28186 {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"
asanka13cc94a2015-02-03 20:31:58190 "%E9%A1%B5.doc"},
[email protected]d96cf752014-04-09 04:05:28191 {L"D:\\plane1\\\xD835\xDC00\xD835\xDC01.txt", // Math alphabet "AB"
192 "file:///D:/plane1/%F0%9D%90%80%F0%9D%90%81.txt"},
Matt Giucaee5e87f2018-06-07 04:18:41193 // Other percent-encoded characters that are left alone when displaying a
194 // URL are decoded in a file path (https://crbug.com/585422).
195 {L"C:\\foo\\\U0001F512.txt",
Joel Hockey6f7283f82020-11-10 01:11:00196 "file:///C:/foo/%F0%9F%94%92.txt"}, // Blocked.
197 {L"C:\\foo\\\u2001.txt", "file:///C:/foo/%E2%80%81.txt"}, // Blocked.
198 {L"C:\\foo\\\a\tbar\n ", "file:///C:/foo/%07%09bar%0A%20"}, // Blocked.
Xiaohan Wang2a6845b2022-01-08 04:40:57199#elif BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA)
[email protected]d96cf752014-04-09 04:05:28200 {L"/foo/bar.txt", "file:///foo/bar.txt"},
201 {L"/foo/BAR.txt", "file:///foo/BAR.txt"},
202 {L"/C:/foo/bar.txt", "file:///C:/foo/bar.txt"},
203 {L"/foo/bar?.txt", "file:///foo/bar%3F.txt"},
Joel Hockey6f7283f82020-11-10 01:11:00204 {L"/foo/\a\tbar\n ", "file:///foo/%07%09bar%0A%20"},
Matt Giucaee5e87f2018-06-07 04:18:41205 // %5C ('\\') is not special on POSIX, and is therefore decoded as normal.
206 {L"/foo/..\\bar", "file:///foo/..%5Cbar"},
[email protected]d96cf752014-04-09 04:05:28207 {L"/some computer/foo/bar.txt", "file:///some%20computer/foo/bar.txt"},
208 {L"/Name;with%some symbols*#", "file:///Name%3Bwith%25some%20symbols*%23"},
209 {L"/latin1/caf\x00E9\x00DD.txt", "file:///latin1/caf%C3%A9%C3%9D.txt"},
210 {L"/otherlatin/caf\x0119.txt", "file:///otherlatin/caf%C4%99.txt"},
211 {L"/greek/\x03B1\x03B2\x03B3.txt", "file:///greek/%CE%B1%CE%B2%CE%B3.txt"},
212 {L"/Chinese/\x6240\x6709\x4e2d\x6587\x7f51\x9875.doc",
213 "file:///Chinese/%E6%89%80%E6%9C%89%E4%B8%AD%E6%96%87%E7%BD"
asanka13cc94a2015-02-03 20:31:58214 "%91%E9%A1%B5.doc"},
[email protected]d96cf752014-04-09 04:05:28215 {L"/plane1/\x1D400\x1D401.txt", // Math alphabet "AB"
216 "file:///plane1/%F0%9D%90%80%F0%9D%90%81.txt"},
Matt Giucaee5e87f2018-06-07 04:18:41217 // Other percent-encoded characters that are left alone when displaying a
218 // URL are decoded in a file path (https://crbug.com/585422).
Ryan Sleevia9d6aa62019-07-26 13:32:18219 {L"/foo/\U0001F512.txt", "file:///foo/%F0%9F%94%92.txt"}, // Blocked.
220 {L"/foo/\u2001.txt", "file:///foo/%E2%80%81.txt"}, // Blocked.
[email protected]d96cf752014-04-09 04:05:28221#endif
222 };
223
224 // First, we'll test that we can round-trip all of the above cases of URLs
225 base::FilePath output;
Ryan Sleevi4625214fa2018-05-10 16:42:45226 for (const auto& test_case : round_trip_cases) {
[email protected]d96cf752014-04-09 04:05:28227 // convert to the file URL
Ryan Sleevi4625214fa2018-05-10 16:42:45228 GURL file_url(FilePathToFileURL(WStringAsFilePath(test_case.file)));
229 EXPECT_EQ(test_case.url, file_url.spec());
[email protected]d96cf752014-04-09 04:05:28230
231 // Back to the filename.
232 EXPECT_TRUE(FileURLToFilePath(file_url, &output));
Ryan Sleevi4625214fa2018-05-10 16:42:45233 EXPECT_EQ(test_case.file, FilePathAsWString(output));
[email protected]d96cf752014-04-09 04:05:28234 }
235
236 // Test that various file: URLs get decoded into the correct file type
237 FileCase url_cases[] = {
Tommy Li3187fae2019-11-14 20:04:22238 {nullptr, "http://foo/bar.txt"},
239 {nullptr, "http://localhost/foo/bar.txt"},
240 {nullptr, "https://localhost/foo/bar.txt"},
Xiaohan Wang2a6845b2022-01-08 04:40:57241#if BUILDFLAG(IS_WIN)
[email protected]d96cf752014-04-09 04:05:28242 {L"C:\\foo\\bar.txt", "file:c|/foo\\bar.txt"},
243 {L"C:\\foo\\bar.txt", "file:/c:/foo/bar.txt"},
244 {L"\\\\foo\\bar.txt", "file://foo\\bar.txt"},
245 {L"C:\\foo\\bar.txt", "file:///c:/foo/bar.txt"},
246 {L"\\\\foo\\bar.txt", "file:////foo\\bar.txt"},
247 {L"\\\\foo\\bar.txt", "file:/foo/bar.txt"},
248 {L"\\\\foo\\bar.txt", "file://foo\\bar.txt"},
249 {L"C:\\foo\\bar.txt", "file:\\\\\\c:/foo/bar.txt"},
Matt Giucaee5e87f2018-06-07 04:18:41250 // %2F ('/') should fail, because it might otherwise be interpreted as a
251 // path separator on Windows.
252 {nullptr, "file:///C:\\foo%2f..\\bar"},
253 // %5C ('\\') should fail, because it can't be represented in a Windows
254 // filename (and should not be considered a path separator).
255 {nullptr, "file:///foo\\..%5cbar"},
256 // %00 should fail, because it represents a null byte in a filename.
257 {nullptr, "file:///foo/%00bar.txt"},
Matt Giuca29b1b562018-05-16 04:06:16258 // Other percent-encoded characters that are left alone when displaying a
Matt Giucaee5e87f2018-06-07 04:18:41259 // URL are decoded in a file path (https://crbug.com/585422).
260 {L"C:\\foo\\\n.txt", "file:///c:/foo/%0A.txt"}, // Control char.
261 {L"C:\\foo\\a=$b.txt", "file:///c:/foo/a%3D%24b.txt"}, // Reserved.
Matt Giuca29b1b562018-05-16 04:06:16262 // Make sure that '+' isn't converted into ' '.
263 {L"C:\\foo\\romeo+juliet.txt", "file:/c:/foo/romeo+juliet.txt"},
Tommy Li3187fae2019-11-14 20:04:22264 // SAMBA share case.
265 {L"\\\\computername\\ShareName\\Path\\Foo.txt",
266 "file://computername/ShareName/Path/Foo.txt"},
Xiaohan Wang2a6845b2022-01-08 04:40:57267#elif BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA)
[email protected]d96cf752014-04-09 04:05:28268 {L"/c:/foo/bar.txt", "file:/c:/foo/bar.txt"},
269 {L"/c:/foo/bar.txt", "file:///c:/foo/bar.txt"},
270 {L"/foo/bar.txt", "file:/foo/bar.txt"},
271 {L"/c:/foo/bar.txt", "file:\\\\\\c:/foo/bar.txt"},
272 {L"/foo/bar.txt", "file:foo/bar.txt"},
[email protected]d96cf752014-04-09 04:05:28273 {L"/foo/bar.txt", "file:///foo/bar.txt"},
274 {L"/foo/bar.txt", "file:////foo/bar.txt"},
275 {L"/foo/bar.txt", "file:////foo//bar.txt"},
276 {L"/foo/bar.txt", "file:////foo///bar.txt"},
277 {L"/foo/bar.txt", "file:////foo////bar.txt"},
278 {L"/c:/foo/bar.txt", "file:\\\\\\c:/foo/bar.txt"},
279 {L"/c:/foo/bar.txt", "file:c:/foo/bar.txt"},
Matt Giucaee5e87f2018-06-07 04:18:41280 // %2F ('/') should fail, because it can't be represented in a POSIX
281 // filename (and should not be considered a path separator).
282 {nullptr, "file:///foo%2f../bar"},
283 // %00 should fail, because it represents a null byte in a filename.
284 {nullptr, "file:///foo/%00bar.txt"},
Matt Giuca29b1b562018-05-16 04:06:16285 // Other percent-encoded characters that are left alone when displaying a
Matt Giucaee5e87f2018-06-07 04:18:41286 // URL are decoded in a file path (https://crbug.com/585422).
287 {L"/foo/\n.txt", "file:///foo/%0A.txt"}, // Control char.
288 {L"/foo/a=$b.txt", "file:///foo/a%3D%24b.txt"}, // Reserved.
Matt Giuca29b1b562018-05-16 04:06:16289 // Make sure that '+' isn't converted into ' '.
290 {L"/foo/romeo+juliet.txt", "file:///foo/romeo+juliet.txt"},
291 // Backslashes in a file URL are normalized as forward slashes.
Tommy Li3187fae2019-11-14 20:04:22292 {L"/bar.txt", "file://\\bar.txt"},
Matt Giuca29b1b562018-05-16 04:06:16293 {L"/c|/foo/bar.txt", "file:c|/foo\\bar.txt"},
294 {L"/foo/bar.txt", "file:////foo\\bar.txt"},
Tommy Li3187fae2019-11-14 20:04:22295 // Accept obviously-local file URLs.
296 {L"/foo/bar.txt", "file:///foo/bar.txt"},
297 {L"/foo/bar.txt", "file://localhost/foo/bar.txt"},
298 {L"/foo/bar.txt", "file://127.0.0.1/foo/bar.txt"},
299 {L"/foo/bar.txt", "file://[::1]/foo/bar.txt"},
300 // Reject non-local file URLs.
301 {nullptr, "file://foo/bar.txt"},
302 {nullptr, "file://example.com/bar.txt"},
303 {nullptr, "file://192.168.1.1/foo/bar.txt"},
304 {nullptr, "file://[2001:0db8:85a3:0000:0000:8a2e:0370:7334]/foo/bar.txt"},
[email protected]d96cf752014-04-09 04:05:28305#endif
306 };
Ryan Sleevi4625214fa2018-05-10 16:42:45307 for (const auto& test_case : url_cases) {
Matt Giucaee5e87f2018-06-07 04:18:41308 EXPECT_EQ(test_case.file != nullptr,
309 FileURLToFilePath(GURL(test_case.url), &output));
310 if (test_case.file) {
311 EXPECT_EQ(test_case.file, FilePathAsWString(output));
312 } else {
313 EXPECT_EQ(L"", FilePathAsWString(output));
314 }
[email protected]d96cf752014-04-09 04:05:28315 }
316
Matt Giuca29b1b562018-05-16 04:06:16317 // Invalid UTF-8 tests can't be tested above because FilePathAsWString assumes
318 // the output is valid UTF-8.
319
320 // Invalid UTF-8 bytes in input.
321 {
322 const char invalid_utf8[] = "file:///d:/Blah/\x85\x99.doc";
323 EXPECT_TRUE(FileURLToFilePath(GURL(invalid_utf8), &output));
Xiaohan Wang2a6845b2022-01-08 04:40:57324#if BUILDFLAG(IS_WIN)
Matt Giuca29b1b562018-05-16 04:06:16325 // On Windows, invalid UTF-8 bytes are interpreted using the default ANSI
326 // code page. This defaults to Windows-1252 (which we assume here).
jdoerrie6312bf62019-02-01 22:03:42327 const base::FilePath::CharType expected_output[] =
328 FILE_PATH_LITERAL("D:\\Blah\\\u2026\u2122.doc");
329 EXPECT_EQ(expected_output, output.value());
Xiaohan Wang2a6845b2022-01-08 04:40:57330#elif BUILDFLAG(IS_POSIX)
Matt Giuca29b1b562018-05-16 04:06:16331 // No conversion should happen, and the invalid UTF-8 should be preserved.
332 const char expected_output[] = "/d:/Blah/\x85\x99.doc";
333 EXPECT_EQ(expected_output, output.value());
[email protected]d96cf752014-04-09 04:05:28334#endif
Matt Giuca29b1b562018-05-16 04:06:16335 }
336
337 // Invalid UTF-8 percent-encoded bytes in input.
338 {
339 const char invalid_utf8[] = "file:///d:/Blah/%85%99.doc";
340 EXPECT_TRUE(FileURLToFilePath(GURL(invalid_utf8), &output));
Xiaohan Wang2a6845b2022-01-08 04:40:57341#if BUILDFLAG(IS_WIN)
Matt Giuca29b1b562018-05-16 04:06:16342 // On Windows, invalid UTF-8 bytes are interpreted using the default ANSI
343 // code page. This defaults to Windows-1252 (which we assume here).
jdoerrie6312bf62019-02-01 22:03:42344 const base::FilePath::CharType expected_output[] =
345 FILE_PATH_LITERAL("D:\\Blah\\\u2026\u2122.doc");
346 EXPECT_EQ(expected_output, output.value());
Xiaohan Wang2a6845b2022-01-08 04:40:57347#elif BUILDFLAG(IS_POSIX)
Matt Giuca29b1b562018-05-16 04:06:16348 // No conversion should happen, and the invalid UTF-8 should be preserved.
349 const char expected_output[] = "/d:/Blah/\x85\x99.doc";
350 EXPECT_EQ(expected_output, output.value());
351#endif
352 }
[email protected]d96cf752014-04-09 04:05:28353
354 // Test that if a file URL is malformed, we get a failure
355 EXPECT_FALSE(FileURLToFilePath(GURL("filefoobar"), &output));
356}
357
Asanka Herath090f40e2018-04-05 20:46:50358TEST(FilenameUtilTest, GenerateSafeFileName) {
[email protected]d96cf752014-04-09 04:05:28359 const struct {
Asanka Herath090f40e2018-04-05 20:46:50360 int line;
[email protected]d96cf752014-04-09 04:05:28361 const char* mime_type;
Asanka Herath090f40e2018-04-05 20:46:50362 const char* filename;
363 const char* expected_filename;
[email protected]d96cf752014-04-09 04:05:28364 } safe_tests[] = {
Asanka Herath090f40e2018-04-05 20:46:50365 {__LINE__, "text/html", "bar.htm", "bar.htm"},
366 {__LINE__, "text/html", "bar.html", "bar.html"},
367 {__LINE__, "application/x-chrome-extension", "bar", "bar.crx"},
368 {__LINE__, "image/png", "bar.html", "bar.html"},
369 {__LINE__, "text/html", "bar.exe", "bar.exe"},
370 {__LINE__, "image/gif", "bar.exe", "bar.exe"},
371 {__LINE__, "text/html", "google.com", "google.com"},
372 // Allow extension synonyms.
373 {__LINE__, "image/jpeg", "bar.jpg", "bar.jpg"},
374 {__LINE__, "image/jpeg", "bar.jpeg", "bar.jpeg"},
375
Xiaohan Wang2a6845b2022-01-08 04:40:57376#if BUILDFLAG(IS_WIN)
Asanka Herath090f40e2018-04-05 20:46:50377 // Device names
378 {__LINE__, "text/html", "con.htm", "_con.htm"},
379 {__LINE__, "text/html", "lpt1.htm", "_lpt1.htm"},
380 {__LINE__, "application/x-chrome-extension", "con", "_con.crx"},
381
382 // Looks like foo.{GUID} which get treated as namespace mounts on Windows.
383 {__LINE__, "text/html", "harmless.{not-really-this-may-be-a-guid}",
384 "harmless.download"},
385 {__LINE__, "text/html", "harmless.{mismatched-", "harmless.{mismatched-"},
386
387 // Dangerous extensions
388 {__LINE__, "text/html", "harmless.local", "harmless.download"},
389 {__LINE__, "text/html", "harmless.lnk", "harmless.download"},
Xiaohan Wang2a6845b2022-01-08 04:40:57390#elif BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA)
Asanka Herath090f40e2018-04-05 20:46:50391 // On Posix, none of the above set is particularly dangerous.
392 {__LINE__, "text/html", "con.htm", "con.htm"},
393 {__LINE__, "text/html", "lpt1.htm", "lpt1.htm"},
394 {__LINE__, "application/x-chrome-extension", "con", "con.crx"},
395 {__LINE__, "text/html", "harmless.{not-really-this-may-be-a-guid}",
396 "harmless.{not-really-this-may-be-a-guid}"},
397 {__LINE__, "text/html", "harmless.{mismatched-", "harmless.{mismatched-"},
398 {__LINE__, "text/html", "harmless.local", "harmless.local"},
399 {__LINE__, "text/html", "harmless.lnk", "harmless.lnk"},
Xiaohan Wang2a6845b2022-01-08 04:40:57400#endif // BUILDFLAG(IS_WIN)
[email protected]d96cf752014-04-09 04:05:28401 };
402
Xiaohan Wang2a6845b2022-01-08 04:40:57403#if BUILDFLAG(IS_WIN)
jdoerrie6312bf62019-02-01 22:03:42404 base::FilePath base_path(FILE_PATH_LITERAL("C:\\foo"));
Xiaohan Wang2a6845b2022-01-08 04:40:57405#elif BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA)
Asanka Herath090f40e2018-04-05 20:46:50406 base::FilePath base_path("/foo");
407#endif
408
409 for (const auto& test : safe_tests) {
410 base::FilePath file_path = base_path.AppendASCII(test.filename);
411 base::FilePath expected_path =
412 base_path.AppendASCII(test.expected_filename);
413 GenerateSafeFileName(test.mime_type, false, &file_path);
414 EXPECT_EQ(expected_path.value(), file_path.value())
415 << "Test case at line " << test.line;
[email protected]d96cf752014-04-09 04:05:28416 }
417}
418
Asanka Herath27b7af9f2018-04-03 14:52:19419TEST(FilenameUtilTest, GenerateFileName_Assumptions) {
420 base::FilePath::StringType extension;
421 EXPECT_TRUE(GetPreferredExtensionForMimeType("application/x-chrome-extension",
422 &extension));
423 EXPECT_EQ(base::FilePath::StringType(FILE_PATH_LITERAL("crx")), extension);
424}
425
[email protected]d96cf752014-04-09 04:05:28426TEST(FilenameUtilTest, GenerateFileName) {
[email protected]d96cf752014-04-09 04:05:28427 // Tests whether the correct filename is selected from the the given
428 // parameters and that Content-Disposition headers are properly
429 // handled including failovers when the header is malformed.
430 const GenerateFilenameCase selection_tests[] = {
Asanka Herath97ae36c2018-04-03 14:47:07431 {// Picks the filename from the C-D header.
432 __LINE__, "http://www.google.com/", "attachment; filename=test.html", "",
433 "", "", L"", L"test.html"},
434 {// Ditto. The C-D header uses a quoted string.
435 __LINE__, "http://www.google.com/", "attachment; filename=\"test.html\"",
436 "", "", "", L"", L"test.html"},
437 {// Ditto. Extra whilespace after the '=' sign.
438 __LINE__, "http://www.google.com/",
439 "attachment; filename= \"test.html\"", "", "", "", L"", L"test.html"},
440 {// Ditto. Whitespace before and after '=' sign.
441 __LINE__, "http://www.google.com/",
442 "attachment; filename = \"test.html\"", "", "", "", L"",
443 L"test.html"},
444 {// Filename is whitespace. Should failover to URL host
445 __LINE__, "http://www.google.com/", "attachment; filename= ", "", "",
446 "", L"", L"www.google.com"},
447 {// No filename.
448 __LINE__, "http://www.google.com/path/test.html", "attachment", "", "",
449 "", L"", L"test.html"},
450 {// Ditto
451 __LINE__, "http://www.google.com/path/test.html", "attachment;", "", "",
452 "", L"", L"test.html"},
453 {// No C-D, and no URL path.
454 __LINE__, "http://www.google.com/", "", "", "", "", L"",
455 L"www.google.com"},
456 {// No C-D. URL has a path.
457 __LINE__, "http://www.google.com/test.html", "", "", "", "", L"",
458 L"test.html"},
459 {// No C-D. URL's path ends in a slash which results in an empty final
460 // component.
461 __LINE__, "http://www.google.com/path/", "", "", "", "", L"",
462 L"www.google.com"},
463 {// No C-D. URL has a path, but the path has no extension.
464 __LINE__, "http://www.google.com/path", "", "", "", "", L"", L"path"},
465 {// No C-D. URL gives no filename hints.
466 __LINE__, "file:///", "", "", "", "", L"", L"download"},
467 {// file:// URL.
468 __LINE__, "file:///path/testfile", "", "", "", "", L"", L"testfile"},
469 {// Unknown scheme.
470 __LINE__, "non-standard-scheme:", "", "", "", "", L"", L"download"},
471 {// C-D overrides default
472 __LINE__, "http://www.google.com/",
473 "attachment; filename =\"test.html\"", "", "", "", L"download",
474 L"test.html"},
475 {// But the URL doesn't
476 __LINE__, "http://www.google.com/", "", "", "", "", L"download",
477 L"download"},
478 // Below is a small subset of cases taken from HttpContentDisposition
479 // tests.
480 {__LINE__, "http://www.google.com/",
481 "attachment; filename=\"%EC%98%88%EC%88%A0%20"
482 "%EC%98%88%EC%88%A0.jpg\"",
483 "", "", "", L"", L"\uc608\uc220 \uc608\uc220.jpg"},
484 {__LINE__,
485 "http://www.google.com/%EC%98%88%EC%88%A0%20%EC%98%88%EC%88%A0.jpg", "",
486 "", "", "", L"download", L"\uc608\uc220 \uc608\uc220.jpg"},
487 {__LINE__, "http://www.google.com/", "attachment;", "", "", "",
488 L"\uB2E4\uC6B4\uB85C\uB4DC", L"\uB2E4\uC6B4\uB85C\uB4DC"},
489 {__LINE__, "http://www.google.com/",
490 "attachment; filename=\"=?EUC-JP?Q?=B7=DD=BD="
491 "D13=2Epng?=\"",
492 "", "", "", L"download", L"\u82b8\u88533.png"},
493 {__LINE__, "http://www.example.com/images?id=3",
494 "attachment; filename=caf\xc3\xa9.png", "iso-8859-1", "", "", L"",
495 L"caf\u00e9.png"},
496 {__LINE__, "http://www.example.com/images?id=3",
497 "attachment; filename=caf\xe5.png", "windows-1253", "", "", L"",
498 L"caf\u03b5.png"},
499 {// Invalid C-D header. Name value is skipped now.
500 __LINE__, "http://www.example.com/file?id=3",
501 "attachment; name=\xcf\xc2\xd4\xd8.zip", "GBK", "", "", L"", L"file"},
502 {// Invalid C-D header. Extracts filename from url.
503 __LINE__, "http://www.google.com/test.html",
504 "attachment; filename==?iiso88591?Q?caf=EG?=", "", "", "", L"",
505 L"test.html"},
506 // about: and data: URLs
507 {__LINE__, "about:chrome", "", "", "", "", L"", L"download"},
508 {__LINE__, "data:,looks/like/a.path", "", "", "", "", L"", L"download"},
509 {__LINE__, "data:text/plain;base64,VG8gYmUgb3Igbm90IHRvIGJlLg=", "", "",
510 "", "", L"", L"download"},
511 {__LINE__, "data:,looks/like/a.path", "", "", "", "",
512 L"default_filename_is_given", L"default_filename_is_given"},
513 {__LINE__, "data:,looks/like/a.path", "", "", "", "",
514 L"\u65e5\u672c\u8a9e", // Japanese Kanji.
515 L"\u65e5\u672c\u8a9e"},
516 {// The filename encoding is specified by the referrer charset.
517 __LINE__, "http://example.com/V%FDvojov%E1%20psychologie.doc", "",
518 "iso-8859-1", "", "", L"", L"V\u00fdvojov\u00e1 psychologie.doc"},
519 {// Suggested filename takes precedence over URL
520 __LINE__, "http://www.google.com/test", "", "", "suggested", "", L"",
521 L"suggested"},
522 {// The content-disposition has higher precedence over the suggested name.
523 __LINE__, "http://www.google.com/test", "attachment; filename=test.html",
524 "", "suggested", "", L"", L"test.html"},
525 {__LINE__, "http://www.google.com/test", "attachment; filename=test",
526 "utf-8", "", "image/png", L"", L"test"},
527 // Raw 8bit characters in C-D
528 {__LINE__, "http://www.example.com/images?id=3",
529 "attachment; filename=caf\xc3\xa9.png", "iso-8859-1", "", "image/png",
530 L"", L"caf\u00e9.png"},
531 {__LINE__, "http://www.example.com/images?id=3",
532 "attachment; filename=caf\xe5.png", "windows-1253", "", "image/png", L"",
533 L"caf\u03b5.png"},
534 {// No 'filename' keyword in the disposition, use the URL
535 __LINE__, "http://www.evil.com/my_download.txt", "a_file_name.txt", "",
536 "", "text/plain", L"download", L"my_download.txt"},
537 {// Spaces in the disposition file name
538 __LINE__, "http://www.frontpagehacker.com/a_download.exe",
539 "filename=My Downloaded File.exe", "", "", "application/octet-stream",
540 L"download", L"My Downloaded File.exe"},
541 {// % encoded
542 __LINE__, "http://www.examples.com/",
543 "attachment; "
544 "filename=\"%EC%98%88%EC%88%A0%20%EC%98%88%EC%88%A0.jpg\"",
Asanka Herath27b7af9f2018-04-03 14:52:19545 "", "", "application/x-chrome-extension", L"download",
546 L"\uc608\uc220 \uc608\uc220.jpg"},
Asanka Herath97ae36c2018-04-03 14:47:07547 {// Invalid C-D header. Name value is skipped now.
548 __LINE__, "http://www.examples.com/q.cgi?id=abc",
549 "attachment; name=abc de.pdf", "", "", "application/octet-stream",
550 L"download", L"q.cgi"},
551 {__LINE__, "http://www.example.com/path",
552 "filename=\"=?EUC-JP?Q?=B7=DD=BD=D13=2Epng?=\"", "", "", "image/png",
553 L"download",
554 L"\x82b8\x8853"
555 L"3.png"},
556 {// The following two have invalid CD headers and filenames come from the
557 // URL.
558 __LINE__, "http://www.example.com/test%20123",
559 "attachment; filename==?iiso88591?Q?caf=EG?=", "", "", "", L"download",
560 L"test 123"},
561 {__LINE__,
562 "http://www.google.com/%EC%98%88%EC%88%A0%20%EC%98%88%EC%88%A0.jpg",
563 "malformed_disposition", "", "", "", L"download",
564 L"\uc608\uc220 \uc608\uc220.jpg"},
565 {// Invalid C-D. No filename from URL. Falls back to 'download'.
566 __LINE__, "http://www.google.com/path1/path2/",
567 "attachment; filename==?iso88591?Q?caf=E3?", "", "", "", L"download",
568 L"download"},
[email protected]d96cf752014-04-09 04:05:28569 };
570
571 // Tests filename generation. Once the correct filename is
572 // selected, they should be passed through the validation steps and
573 // a correct extension should be added if necessary.
574 const GenerateFilenameCase generation_tests[] = {
575 // Dotfiles. Ensures preceeding period(s) stripped.
mmenkeb00bf0a2016-01-05 20:11:18576 {__LINE__, "http://www.google.com/.test.html", "", "", "", "", L"",
asanka92a354f2015-01-31 00:37:40577 L"test.html"},
578 {__LINE__, "http://www.google.com/.test", "", "", "", "", L"", L"test"},
579 {__LINE__, "http://www.google.com/..test", "", "", "", "", L"", L"test"},
580 {// Disposition has relative paths, remove directory separators
Asanka Herath97ae36c2018-04-03 14:47:07581 __LINE__, "", "filename=../../../../././../a_file_name.txt", "", "",
582 "text/plain", L"download", L"_.._.._.._._._.._a_file_name.txt"},
asanka92a354f2015-01-31 00:37:40583 {// Disposition has parent directories, remove directory separators
Asanka Herath97ae36c2018-04-03 14:47:07584 __LINE__, "", "filename=dir1/dir2/a_file_name.txt", "", "", "text/plain",
585 L"download", L"dir1_dir2_a_file_name.txt"},
asanka92a354f2015-01-31 00:37:40586 {// Disposition has relative paths, remove directory separators
Asanka Herath97ae36c2018-04-03 14:47:07587 __LINE__, "", "filename=..\\..\\..\\..\\.\\.\\..\\a_file_name.txt", "", "",
588 "text/plain", L"download", L"_.._.._.._._._.._a_file_name.txt"},
asanka92a354f2015-01-31 00:37:40589 {// Disposition has parent directories, remove directory separators
Asanka Herath97ae36c2018-04-03 14:47:07590 __LINE__, "", "filename=dir1\\dir2\\a_file_name.txt", "", "", "text/plain",
591 L"download", L"dir1_dir2_a_file_name.txt"},
asanka92a354f2015-01-31 00:37:40592 {// Filename looks like HTML?
Asanka Herath97ae36c2018-04-03 14:47:07593 __LINE__, "", "filename=\"<blink>Hello kitty</blink>\"", "", "",
594 "text/plain", L"default", L"_blink_Hello kitty__blink_"},
asanka92a354f2015-01-31 00:37:40595 {// A normal avi should get .avi and not .avi.avi
Asanka Herath97ae36c2018-04-03 14:47:07596 __LINE__, "https://example.com/misc/2.avi", "", "", "", "video/x-msvideo",
597 L"download", L"2.avi"},
Matt Giuca25c6a34f2018-09-07 00:48:17598 {// Slashes are illegal, and should be replaced with underscores.
Asanka Herath97ae36c2018-04-03 14:47:07599 __LINE__, "http://example.com/foo%2f..%2fbar.jpg", "", "", "",
Matt Giuca25c6a34f2018-09-07 00:48:17600 "text/plain", L"download", L"foo_.._bar.jpg"},
601 {// "%00" decodes to the NUL byte, which is illegal and should be replaced
602 // with an underscore. (Note: This can't be tested with a URL, since "%00"
603 // is illegal in a URL. Only applies to Content-Disposition.)
604 __LINE__, "http://example.com/download.py", "filename=foo%00bar.jpg", "",
605 "", "text/plain", L"download", L"foo_bar.jpg"},
Asanka Herath97ae36c2018-04-03 14:47:07606 {// Extension generation for C-D derived filenames.
607 __LINE__, "", "filename=my-cat", "", "", "image/jpeg", L"download",
608 L"my-cat"},
asanka92a354f2015-01-31 00:37:40609 {// Unknown MIME type
Asanka Herath97ae36c2018-04-03 14:47:07610 __LINE__, "", "filename=my-cat", "", "", "dance/party", L"download",
611 L"my-cat"},
612 {// Known MIME type.
613 __LINE__, "", "filename=my-cat.jpg", "", "", "text/plain", L"download",
614 L"my-cat.jpg"},
Xiaohan Wang2a6845b2022-01-08 04:40:57615#if BUILDFLAG(IS_WIN)
Asanka Herath97ae36c2018-04-03 14:47:07616 // Test truncation of trailing dots and spaces (Windows)
617 {__LINE__, "", "filename=evil.exe ", "", "", "binary/octet-stream",
618 L"download", L"evil.exe"},
619 {__LINE__, "", "filename=evil.exe.", "", "", "binary/octet-stream",
620 L"download", L"evil.exe_"},
621 {__LINE__, "", "filename=evil.exe. . .", "", "", "binary/octet-stream",
622 L"download", L"evil.exe_______"},
623 {__LINE__, "", "filename=evil.", "", "", "binary/octet-stream", L"download",
624 L"evil_"},
625 {__LINE__, "", "filename=. . . . .", "", "", "binary/octet-stream",
626 L"download", L"download"},
Xiaohan Wang2a6845b2022-01-08 04:40:57627#elif BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA)
Asanka Herath97ae36c2018-04-03 14:47:07628 // Test truncation of trailing dots and spaces (non-Windows)
629 {__LINE__, "", "filename=evil.exe ", "", "", "binary/octet-stream",
630 L"download", L"evil.exe"},
631 {__LINE__, "", "filename=evil.exe.", "", "", "binary/octet-stream",
632 L"download", L"evil.exe"},
633 {__LINE__, "", "filename=evil.exe. . .", "", "", "binary/octet-stream",
634 L"download", L"evil.exe. . _"},
635 {__LINE__, "", "filename=evil.", "", "", "binary/octet-stream", L"download",
636 L"evil"},
637 {__LINE__, "", "filename=. . . . .", "", "", "binary/octet-stream",
638 L"download", L"_. . ._"},
[email protected]d96cf752014-04-09 04:05:28639#endif
Asanka Herath97ae36c2018-04-03 14:47:07640 {__LINE__, "", "attachment; filename=\"meh.exe\xC2\xA0\"", "", "",
641 "binary/octet-stream", L"", L"meh.exe_"},
642 // Disappearing directory references:
643 {__LINE__, "", "filename=.", "", "", "dance/party", L"download",
644 L"download"},
645 {__LINE__, "", "filename=..", "", "", "dance/party", L"download",
646 L"download"},
647 {__LINE__, "", "filename=...", "", "", "dance/party", L"download",
648 L"download"},
[email protected]d96cf752014-04-09 04:05:28649 // Reserved words on Windows
Asanka Herath97ae36c2018-04-03 14:47:07650 {__LINE__, "", "filename=COM1", "", "", "application/foo-bar", L"download",
Xiaohan Wang2a6845b2022-01-08 04:40:57651#if BUILDFLAG(IS_WIN)
asanka92a354f2015-01-31 00:37:40652 L"_COM1"
[email protected]d96cf752014-04-09 04:05:28653#else
asanka92a354f2015-01-31 00:37:40654 L"COM1"
[email protected]d96cf752014-04-09 04:05:28655#endif
656 },
Asanka Herath97ae36c2018-04-03 14:47:07657 {__LINE__, "", "filename=COM4.txt", "", "", "text/plain", L"download",
Xiaohan Wang2a6845b2022-01-08 04:40:57658#if BUILDFLAG(IS_WIN)
asanka92a354f2015-01-31 00:37:40659 L"_COM4.txt"
[email protected]d96cf752014-04-09 04:05:28660#else
asanka92a354f2015-01-31 00:37:40661 L"COM4.txt"
[email protected]d96cf752014-04-09 04:05:28662#endif
663 },
Asanka Herath97ae36c2018-04-03 14:47:07664 {__LINE__, "", "filename=lpt1.TXT", "", "", "text/plain", L"download",
Xiaohan Wang2a6845b2022-01-08 04:40:57665#if BUILDFLAG(IS_WIN)
asanka92a354f2015-01-31 00:37:40666 L"_lpt1.TXT"
[email protected]d96cf752014-04-09 04:05:28667#else
asanka92a354f2015-01-31 00:37:40668 L"lpt1.TXT"
[email protected]d96cf752014-04-09 04:05:28669#endif
670 },
Asanka Herath97ae36c2018-04-03 14:47:07671 {__LINE__, "", "filename=clock$.txt", "", "", "text/plain", L"download",
Xiaohan Wang2a6845b2022-01-08 04:40:57672#if BUILDFLAG(IS_WIN)
asanka92a354f2015-01-31 00:37:40673 L"_clock$.txt"
[email protected]d96cf752014-04-09 04:05:28674#else
asanka92a354f2015-01-31 00:37:40675 L"clock$.txt"
[email protected]d96cf752014-04-09 04:05:28676#endif
677 },
asanka92a354f2015-01-31 00:37:40678 {// Validation should also apply to sugested name
Asanka Herath97ae36c2018-04-03 14:47:07679 __LINE__, "", "", "", "clock$.txt", "text/plain", L"download",
Xiaohan Wang2a6845b2022-01-08 04:40:57680#if BUILDFLAG(IS_WIN)
asanka92a354f2015-01-31 00:37:40681 L"_clock$.txt"
[email protected]d96cf752014-04-09 04:05:28682#else
asanka92a354f2015-01-31 00:37:40683 L"clock$.txt"
[email protected]d96cf752014-04-09 04:05:28684#endif
685 },
Asanka Herath97ae36c2018-04-03 14:47:07686 {// Device names only work when present at the start of the string.
687 __LINE__, "", "filename=mycom1.foo", "", "", "", L"download",
688 L"mycom1.foo"},
689 {__LINE__, "", "filename=Setup.exe.local", "", "", "", L"download",
Xiaohan Wang2a6845b2022-01-08 04:40:57690#if BUILDFLAG(IS_WIN)
asanka92a354f2015-01-31 00:37:40691 L"Setup.exe.download"
[email protected]d96cf752014-04-09 04:05:28692#else
asanka92a354f2015-01-31 00:37:40693 L"Setup.exe.local"
[email protected]d96cf752014-04-09 04:05:28694#endif
695 },
Asanka Herath97ae36c2018-04-03 14:47:07696 {__LINE__, "", "filename=Setup.exe.local.local", "", "", "", L"download",
Xiaohan Wang2a6845b2022-01-08 04:40:57697#if BUILDFLAG(IS_WIN)
asanka92a354f2015-01-31 00:37:40698 L"Setup.exe.local.download"
[email protected]d96cf752014-04-09 04:05:28699#else
asanka92a354f2015-01-31 00:37:40700 L"Setup.exe.local.local"
[email protected]d96cf752014-04-09 04:05:28701#endif
702 },
Asanka Herath97ae36c2018-04-03 14:47:07703 {__LINE__, "", "filename=Setup.exe.lnk", "", "", "", L"download",
Xiaohan Wang2a6845b2022-01-08 04:40:57704#if BUILDFLAG(IS_WIN)
asanka92a354f2015-01-31 00:37:40705 L"Setup.exe.download"
[email protected]d96cf752014-04-09 04:05:28706#else
asanka92a354f2015-01-31 00:37:40707 L"Setup.exe.lnk"
[email protected]d96cf752014-04-09 04:05:28708#endif
709 },
Asanka Herath97ae36c2018-04-03 14:47:07710 {__LINE__, "", "filename=Desktop.ini", "", "", "", L"download",
Xiaohan Wang2a6845b2022-01-08 04:40:57711#if BUILDFLAG(IS_WIN)
asanka92a354f2015-01-31 00:37:40712 L"_Desktop.ini"
[email protected]d96cf752014-04-09 04:05:28713#else
asanka92a354f2015-01-31 00:37:40714 L"Desktop.ini"
[email protected]d96cf752014-04-09 04:05:28715#endif
716 },
Asanka Herath97ae36c2018-04-03 14:47:07717 {__LINE__, "", "filename=Thumbs.db", "", "", "", L"download",
Xiaohan Wang2a6845b2022-01-08 04:40:57718#if BUILDFLAG(IS_WIN)
asanka92a354f2015-01-31 00:37:40719 L"_Thumbs.db"
[email protected]d96cf752014-04-09 04:05:28720#else
asanka92a354f2015-01-31 00:37:40721 L"Thumbs.db"
[email protected]d96cf752014-04-09 04:05:28722#endif
723 },
Asanka Herath27b7af9f2018-04-03 14:52:19724
725 // Regression tests for older issues:
asanka92a354f2015-01-31 00:37:40726 {// http://crbug.com/5772.
mmenkeb00bf0a2016-01-05 20:11:18727 __LINE__, "http://www.example.com/foo.tar.gz", "", "", "",
728 "application/x-tar", L"download", L"foo.tar.gz"},
asanka92a354f2015-01-31 00:37:40729 {// http://crbug.com/52250.
mmenkeb00bf0a2016-01-05 20:11:18730 __LINE__, "http://www.example.com/foo.tgz", "", "", "",
731 "application/x-tar", L"download", L"foo.tgz"},
asanka92a354f2015-01-31 00:37:40732 {// http://crbug.com/7337.
mmenkeb00bf0a2016-01-05 20:11:18733 __LINE__, "http://maged.lordaeron.org/blank.reg", "", "", "",
734 "text/x-registry", L"download", L"blank.reg"},
735 {__LINE__, "http://www.example.com/bar.tar", "", "", "",
736 "application/x-tar", L"download", L"bar.tar"},
737 {__LINE__, "http://www.example.com/bar.bogus", "", "", "",
738 "application/x-tar", L"download", L"bar.bogus"},
asanka92a354f2015-01-31 00:37:40739 {// http://crbug.com/20337
mmenkeb00bf0a2016-01-05 20:11:18740 __LINE__, "http://www.example.com/.download.txt", "filename=.download.txt",
741 "", "", "text/plain", L"-download", L"download.txt"},
asanka92a354f2015-01-31 00:37:40742 {// http://crbug.com/56855.
mmenkeb00bf0a2016-01-05 20:11:18743 __LINE__, "http://www.example.com/bar.sh", "", "", "", "application/x-sh",
744 L"download", L"bar.sh"},
asanka92a354f2015-01-31 00:37:40745 {// http://crbug.com/61571
mmenkeb00bf0a2016-01-05 20:11:18746 __LINE__, "http://www.example.com/npdf.php?fn=foobar.pdf", "", "", "",
Asanka Herath27b7af9f2018-04-03 14:52:19747 "application/x-chrome-extension", L"download", L"npdf.crx"},
asanka92a354f2015-01-31 00:37:40748 {// Shouldn't overwrite C-D specified extension.
mmenkeb00bf0a2016-01-05 20:11:18749 __LINE__, "http://www.example.com/npdf.php?fn=foobar.pdf",
750 "filename=foobar.jpg", "", "", "text/plain", L"download", L"foobar.jpg"},
asanka92a354f2015-01-31 00:37:40751 {// http://crbug.com/87719
mmenkeb00bf0a2016-01-05 20:11:18752 __LINE__, "http://www.example.com/image.aspx?id=blargh", "", "", "",
Asanka Herath27b7af9f2018-04-03 14:52:19753 "application/x-chrome-extension", L"download", L"image.crx"},
mmenkeb00bf0a2016-01-05 20:11:18754 {__LINE__, "http://www.example.com/image.aspx?id=blargh", "", "", " .foo",
Xing Liuf5d05562017-08-10 20:49:24755 "", L"download", L"_.foo"},
mmenke35cc88012016-01-06 17:28:43756
757 // Note that the next 4 tests will not fail on all platforms on regression.
758 // They only fail if application/[x-]gzip has a default extension, which
759 // can vary across platforms (And even by OS install).
mmenkeb00bf0a2016-01-05 20:11:18760 {__LINE__, "http://www.example.com/goat.tar.gz?wearing_hat=true", "", "",
761 "", "application/gzip", L"", L"goat.tar.gz"},
mmenke35cc88012016-01-06 17:28:43762 {__LINE__, "http://www.example.com/goat.tar.gz?wearing_hat=true", "", "",
763 "", "application/x-gzip", L"", L"goat.tar.gz"},
764 {__LINE__, "http://www.example.com/goat.tgz?wearing_hat=true", "", "", "",
765 "application/gzip", L"", L"goat.tgz"},
766 {__LINE__, "http://www.example.com/goat.tgz?wearing_hat=true", "", "", "",
767 "application/x-gzip", L"", L"goat.tgz"},
768
Yuta Hijikata101ed2a2020-11-18 07:50:39769#if BUILDFLAG(IS_CHROMEOS_ASH)
asanka92a354f2015-01-31 00:37:40770 {// http://crosbug.com/26028
mmenkeb00bf0a2016-01-05 20:11:18771 __LINE__, "http://www.example.com/fooa%cc%88.txt", "", "", "",
772 "image/jpeg", L"foo\xe4", L"foo\xe4.txt"},
[email protected]d96cf752014-04-09 04:05:28773#endif
Matt Giuca25c6a34f2018-09-07 00:48:17774
775 // U+3000 IDEOGRAPHIC SPACE (http://crbug.com/849794): In URL file name.
776 {__LINE__, "http://www.example.com/%E5%B2%A1%E3%80%80%E5%B2%A1.txt", "", "",
777 "", "text/plain", L"", L"\u5ca1\u3000\u5ca1.txt"},
778 // U+3000 IDEOGRAPHIC SPACE (http://crbug.com/849794): In
779 // Content-Disposition filename.
780 {__LINE__, "http://www.example.com/download.py",
781 "filename=%E5%B2%A1%E3%80%80%E5%B2%A1.txt", "utf-8", "", "text/plain", L"",
782 L"\u5ca1\u3000\u5ca1.txt"},
[email protected]d96cf752014-04-09 04:05:28783 };
784
Ryan Sleevi4625214fa2018-05-10 16:42:45785 for (const auto& selection_test : selection_tests)
786 RunGenerateFileNameTestCase(&selection_test);
[email protected]d96cf752014-04-09 04:05:28787
Ryan Sleevi4625214fa2018-05-10 16:42:45788 for (const auto& generation_test : generation_tests)
789 RunGenerateFileNameTestCase(&generation_test);
[email protected]d96cf752014-04-09 04:05:28790
Ryan Sleevi4625214fa2018-05-10 16:42:45791 for (const auto& generation_test : generation_tests) {
792 GenerateFilenameCase test_case = generation_test;
[email protected]d96cf752014-04-09 04:05:28793 test_case.referrer_charset = "GBK";
794 RunGenerateFileNameTestCase(&test_case);
795 }
796}
797
dhnishic28dfc32015-07-08 02:21:31798TEST(FilenameUtilTest, IsReservedNameOnWindows) {
Ryan Sleevi4625214fa2018-05-10 16:42:45799 for (auto* basename : kSafePortableBasenames) {
800 EXPECT_FALSE(IsReservedNameOnWindows(base::FilePath(basename).value()))
801 << basename;
dhnishic28dfc32015-07-08 02:21:31802 }
803
Ryan Sleevi4625214fa2018-05-10 16:42:45804 for (auto* basename : kUnsafePortableBasenamesForWin) {
805 EXPECT_TRUE(IsReservedNameOnWindows(base::FilePath(basename).value()))
806 << basename;
dhnishic28dfc32015-07-08 02:21:31807 }
808}
809
[email protected]d96cf752014-04-09 04:05:28810} // namespace net