Avi Drissman | 6459548 | 2022-09-14 20:52:29 | [diff] [blame] | 1 | // Copyright 2014 The Chromium Authors |
[email protected] | d96cf75 | 2014-04-09 04:05:28 | [diff] [blame] | 2 | // 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] | d96cf75 | 2014-04-09 04:05:28 | [diff] [blame] | 7 | #include "base/files/file_path.h" |
thestig | d8df033 | 2014-09-04 06:33:29 | [diff] [blame] | 8 | #include "base/files/file_util.h" |
[email protected] | d96cf75 | 2014-04-09 04:05:28 | [diff] [blame] | 9 | #include "base/strings/string_util.h" |
| 10 | #include "base/strings/utf_string_conversions.h" |
| 11 | #include "base/test/test_file_util.h" |
Asanka Herath | 090f40e | 2018-04-05 20:46:50 | [diff] [blame] | 12 | #include "build/build_config.h" |
Yuta Hijikata | 101ed2a | 2020-11-18 07:50:39 | [diff] [blame] | 13 | #include "build/chromeos_buildflags.h" |
Asanka Herath | 27b7af9f | 2018-04-03 14:52:19 | [diff] [blame] | 14 | #include "net/base/mime_util.h" |
[email protected] | d96cf75 | 2014-04-09 04:05:28 | [diff] [blame] | 15 | #include "testing/gtest/include/gtest/gtest.h" |
| 16 | #include "url/gurl.h" |
| 17 | |
| 18 | namespace net { |
| 19 | |
| 20 | namespace { |
| 21 | |
| 22 | struct FileCase { |
Matt Giuca | ee5e87f | 2018-06-07 04:18:41 | [diff] [blame] | 23 | const wchar_t* file; // nullptr indicates expected to fail. |
[email protected] | d96cf75 | 2014-04-09 04:05:28 | [diff] [blame] | 24 | const char* url; |
| 25 | }; |
| 26 | |
| 27 | struct 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] | b7a95ad6 | 2014-08-07 16:53:01 | [diff] [blame] | 38 | // The expected filenames are coded as wchar_t for convenience. |
Jan Wilken Dörrie | 739ccc21 | 2021-03-11 18:13:05 | [diff] [blame] | 39 | // TODO(https://crbug.com/911896): Make these char16_t once std::u16string is |
jdoerrie | 6312bf6 | 2019-02-01 22:03:42 | [diff] [blame] | 40 | // std::u16string. |
[email protected] | b7a95ad6 | 2014-08-07 16:53:01 | [diff] [blame] | 41 | std::wstring FilePathAsWString(const base::FilePath& path) { |
Xiaohan Wang | 2a6845b | 2022-01-08 04:40:57 | [diff] [blame] | 42 | #if BUILDFLAG(IS_WIN) |
Jan Wilken Dörrie | 9720dce | 2020-07-21 17:14:23 | [diff] [blame] | 43 | return path.value(); |
Xiaohan Wang | 2a6845b | 2022-01-08 04:40:57 | [diff] [blame] | 44 | #elif BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA) |
[email protected] | b7a95ad6 | 2014-08-07 16:53:01 | [diff] [blame] | 45 | return base::UTF8ToWide(path.value()); |
| 46 | #endif |
| 47 | } |
| 48 | base::FilePath WStringAsFilePath(const std::wstring& str) { |
Xiaohan Wang | 2a6845b | 2022-01-08 04:40:57 | [diff] [blame] | 49 | #if BUILDFLAG(IS_WIN) |
Jan Wilken Dörrie | 9720dce | 2020-07-21 17:14:23 | [diff] [blame] | 50 | return base::FilePath(str); |
Xiaohan Wang | 2a6845b | 2022-01-08 04:40:57 | [diff] [blame] | 51 | #elif BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA) |
[email protected] | b7a95ad6 | 2014-08-07 16:53:01 | [diff] [blame] | 52 | return base::FilePath(base::WideToUTF8(str)); |
| 53 | #endif |
| 54 | } |
| 55 | |
halliwell | bd52a8a1 | 2014-12-12 17:21:04 | [diff] [blame] | 56 | std::string GetLocaleWarningString() { |
Xiaohan Wang | 2a6845b | 2022-01-08 04:40:57 | [diff] [blame] | 57 | #if BUILDFLAG(IS_WIN) || BUILDFLAG(IS_ANDROID) |
Fabrice de Gans-Riberi | 7de4737 | 2018-05-08 20:23:47 | [diff] [blame] | 58 | return ""; |
Xiaohan Wang | 2a6845b | 2022-01-08 04:40:57 | [diff] [blame] | 59 | #elif BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA) |
halliwell | bd52a8a1 | 2014-12-12 17:21:04 | [diff] [blame] | 60 | // 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 Horo | 291961af | 2022-06-16 08:51:27 | [diff] [blame] | 68 | std::string locale = setlocale(LC_CTYPE, nullptr); |
halliwell | bd52a8a1 | 2014-12-12 17:21:04 | [diff] [blame] | 69 | return " this test may have failed because the current LC_CTYPE locale is " |
| 70 | "not utf8 (currently set to " + |
| 71 | locale + ")"; |
halliwell | bd52a8a1 | 2014-12-12 17:21:04 | [diff] [blame] | 72 | #endif |
| 73 | } |
| 74 | |
[email protected] | d96cf75 | 2014-04-09 04:05:28 | [diff] [blame] | 75 | void 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] | b7a95ad6 | 2014-08-07 16:53:01 | [diff] [blame] | 81 | EXPECT_EQ(test_case->expected_filename, FilePathAsWString(file_path)) |
halliwell | bd52a8a1 | 2014-12-12 17:21:04 | [diff] [blame] | 82 | << "test case at line number: " << test_case->lineno << "; " |
| 83 | << GetLocaleWarningString(); |
[email protected] | d96cf75 | 2014-04-09 04:05:28 | [diff] [blame] | 84 | } |
| 85 | |
Lei Zhang | eb9b341 | 2017-09-25 20:57:38 | [diff] [blame] | 86 | constexpr 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"), |
asanka | 92a354f | 2015-01-31 00:37:40 | [diff] [blame] | 89 | FILE_PATH_LITERAL("My Computer"), |
[email protected] | d96cf75 | 2014-04-09 04:05:28 | [diff] [blame] | 90 | }; |
| 91 | |
Lei Zhang | eb9b341 | 2017-09-25 20:57:38 | [diff] [blame] | 92 | constexpr const base::FilePath::CharType* kUnsafePortableBasenames[] = { |
asanka | 92a354f | 2015-01-31 00:37:40 | [diff] [blame] | 93 | 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 Wang | 2a6845b | 2022-01-08 04:40:57 | [diff] [blame] | 114 | #if BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA) |
asanka | 92a354f | 2015-01-31 00:37:40 | [diff] [blame] | 115 | FILE_PATH_LITERAL("a\\a"), |
[email protected] | d96cf75 | 2014-04-09 04:05:28 | [diff] [blame] | 116 | #endif |
| 117 | }; |
| 118 | |
Lei Zhang | eb9b341 | 2017-09-25 20:57:38 | [diff] [blame] | 119 | constexpr 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"), |
dhnishi | c28dfc3 | 2015-07-08 02:21:31 | [diff] [blame] | 122 | }; |
| 123 | |
Lei Zhang | eb9b341 | 2017-09-25 20:57:38 | [diff] [blame] | 124 | constexpr const base::FilePath::CharType* kSafePortableRelativePaths[] = { |
asanka | 13cc94a | 2015-02-03 20:31:58 | [diff] [blame] | 125 | FILE_PATH_LITERAL("a/a"), |
Xiaohan Wang | 2a6845b | 2022-01-08 04:40:57 | [diff] [blame] | 126 | #if BUILDFLAG(IS_WIN) |
asanka | 13cc94a | 2015-02-03 20:31:58 | [diff] [blame] | 127 | FILE_PATH_LITERAL("a\\a"), |
[email protected] | d96cf75 | 2014-04-09 04:05:28 | [diff] [blame] | 128 | #endif |
| 129 | }; |
| 130 | |
Lei Zhang | eb9b341 | 2017-09-25 20:57:38 | [diff] [blame] | 131 | } // namespace |
| 132 | |
[email protected] | d96cf75 | 2014-04-09 04:05:28 | [diff] [blame] | 133 | TEST(FilenameUtilTest, IsSafePortablePathComponent) { |
Ryan Sleevi | 4625214fa | 2018-05-10 16:42:45 | [diff] [blame] | 134 | for (auto* basename : kSafePortableBasenames) { |
| 135 | EXPECT_TRUE(IsSafePortablePathComponent(base::FilePath(basename))) |
| 136 | << basename; |
[email protected] | d96cf75 | 2014-04-09 04:05:28 | [diff] [blame] | 137 | } |
Ryan Sleevi | 4625214fa | 2018-05-10 16:42:45 | [diff] [blame] | 138 | for (auto* basename : kUnsafePortableBasenames) { |
| 139 | EXPECT_FALSE(IsSafePortablePathComponent(base::FilePath(basename))) |
| 140 | << basename; |
[email protected] | d96cf75 | 2014-04-09 04:05:28 | [diff] [blame] | 141 | } |
Ryan Sleevi | 4625214fa | 2018-05-10 16:42:45 | [diff] [blame] | 142 | for (auto* path : kSafePortableRelativePaths) { |
| 143 | EXPECT_FALSE(IsSafePortablePathComponent(base::FilePath(path))) << path; |
[email protected] | d96cf75 | 2014-04-09 04:05:28 | [diff] [blame] | 144 | } |
| 145 | } |
| 146 | |
| 147 | TEST(FilenameUtilTest, IsSafePortableRelativePath) { |
| 148 | base::FilePath safe_dirname(FILE_PATH_LITERAL("a")); |
Ryan Sleevi | 4625214fa | 2018-05-10 16:42:45 | [diff] [blame] | 149 | 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; |
asanka | 13cc94a | 2015-02-03 20:31:58 | [diff] [blame] | 158 | EXPECT_TRUE( |
Ryan Sleevi | 4625214fa | 2018-05-10 16:42:45 | [diff] [blame] | 159 | IsSafePortableRelativePath(safe_dirname.Append(base::FilePath(path)))) |
| 160 | << path; |
[email protected] | d96cf75 | 2014-04-09 04:05:28 | [diff] [blame] | 161 | } |
Ryan Sleevi | 4625214fa | 2018-05-10 16:42:45 | [diff] [blame] | 162 | for (auto* basename : kUnsafePortableBasenames) { |
| 163 | EXPECT_FALSE(IsSafePortableRelativePath(base::FilePath(basename))) |
| 164 | << basename; |
| 165 | if (!base::FilePath::StringType(basename).empty()) { |
asanka | 13cc94a | 2015-02-03 20:31:58 | [diff] [blame] | 166 | EXPECT_FALSE(IsSafePortableRelativePath( |
Ryan Sleevi | 4625214fa | 2018-05-10 16:42:45 | [diff] [blame] | 167 | safe_dirname.Append(base::FilePath(basename)))) |
| 168 | << basename; |
[email protected] | d96cf75 | 2014-04-09 04:05:28 | [diff] [blame] | 169 | } |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | TEST(FilenameUtilTest, FileURLConversion) { |
| 174 | // a list of test file names and the corresponding URLs |
| 175 | const FileCase round_trip_cases[] = { |
Xiaohan Wang | 2a6845b | 2022-01-08 04:40:57 | [diff] [blame] | 176 | #if BUILDFLAG(IS_WIN) |
[email protected] | d96cf75 | 2014-04-09 04:05:28 | [diff] [blame] | 177 | {L"C:\\foo\\bar.txt", "file:///C:/foo/bar.txt"}, |
| 178 | {L"\\\\some computer\\foo\\bar.txt", |
asanka | 13cc94a | 2015-02-03 20:31:58 | [diff] [blame] | 179 | "file://some%20computer/foo/bar.txt"}, // UNC |
[email protected] | d96cf75 | 2014-04-09 04:05:28 | [diff] [blame] | 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"}, |
asanka | 13cc94a | 2015-02-03 20:31:58 | [diff] [blame] | 185 | {L"D:\\otherlatin\\caf\x0119.txt", "file:///D:/otherlatin/caf%C4%99.txt"}, |
[email protected] | d96cf75 | 2014-04-09 04:05:28 | [diff] [blame] | 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" |
asanka | 13cc94a | 2015-02-03 20:31:58 | [diff] [blame] | 190 | "%E9%A1%B5.doc"}, |
[email protected] | d96cf75 | 2014-04-09 04:05:28 | [diff] [blame] | 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"}, |
Matt Giuca | ee5e87f | 2018-06-07 04:18:41 | [diff] [blame] | 193 | // 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 Hockey | 6f7283f8 | 2020-11-10 01:11:00 | [diff] [blame] | 196 | "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 Wang | 2a6845b | 2022-01-08 04:40:57 | [diff] [blame] | 199 | #elif BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA) |
[email protected] | d96cf75 | 2014-04-09 04:05:28 | [diff] [blame] | 200 | {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 Hockey | 6f7283f8 | 2020-11-10 01:11:00 | [diff] [blame] | 204 | {L"/foo/\a\tbar\n ", "file:///foo/%07%09bar%0A%20"}, |
Matt Giuca | ee5e87f | 2018-06-07 04:18:41 | [diff] [blame] | 205 | // %5C ('\\') is not special on POSIX, and is therefore decoded as normal. |
| 206 | {L"/foo/..\\bar", "file:///foo/..%5Cbar"}, |
[email protected] | d96cf75 | 2014-04-09 04:05:28 | [diff] [blame] | 207 | {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" |
asanka | 13cc94a | 2015-02-03 20:31:58 | [diff] [blame] | 214 | "%91%E9%A1%B5.doc"}, |
[email protected] | d96cf75 | 2014-04-09 04:05:28 | [diff] [blame] | 215 | {L"/plane1/\x1D400\x1D401.txt", // Math alphabet "AB" |
| 216 | "file:///plane1/%F0%9D%90%80%F0%9D%90%81.txt"}, |
Matt Giuca | ee5e87f | 2018-06-07 04:18:41 | [diff] [blame] | 217 | // Other percent-encoded characters that are left alone when displaying a |
| 218 | // URL are decoded in a file path (https://crbug.com/585422). |
Ryan Sleevi | a9d6aa6 | 2019-07-26 13:32:18 | [diff] [blame] | 219 | {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] | d96cf75 | 2014-04-09 04:05:28 | [diff] [blame] | 221 | #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 Sleevi | 4625214fa | 2018-05-10 16:42:45 | [diff] [blame] | 226 | for (const auto& test_case : round_trip_cases) { |
[email protected] | d96cf75 | 2014-04-09 04:05:28 | [diff] [blame] | 227 | // convert to the file URL |
Ryan Sleevi | 4625214fa | 2018-05-10 16:42:45 | [diff] [blame] | 228 | GURL file_url(FilePathToFileURL(WStringAsFilePath(test_case.file))); |
| 229 | EXPECT_EQ(test_case.url, file_url.spec()); |
[email protected] | d96cf75 | 2014-04-09 04:05:28 | [diff] [blame] | 230 | |
| 231 | // Back to the filename. |
| 232 | EXPECT_TRUE(FileURLToFilePath(file_url, &output)); |
Ryan Sleevi | 4625214fa | 2018-05-10 16:42:45 | [diff] [blame] | 233 | EXPECT_EQ(test_case.file, FilePathAsWString(output)); |
[email protected] | d96cf75 | 2014-04-09 04:05:28 | [diff] [blame] | 234 | } |
| 235 | |
| 236 | // Test that various file: URLs get decoded into the correct file type |
| 237 | FileCase url_cases[] = { |
Tommy Li | 3187fae | 2019-11-14 20:04:22 | [diff] [blame] | 238 | {nullptr, "http://foo/bar.txt"}, |
| 239 | {nullptr, "http://localhost/foo/bar.txt"}, |
| 240 | {nullptr, "https://localhost/foo/bar.txt"}, |
Xiaohan Wang | 2a6845b | 2022-01-08 04:40:57 | [diff] [blame] | 241 | #if BUILDFLAG(IS_WIN) |
[email protected] | d96cf75 | 2014-04-09 04:05:28 | [diff] [blame] | 242 | {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 Giuca | ee5e87f | 2018-06-07 04:18:41 | [diff] [blame] | 250 | // %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 Giuca | 29b1b56 | 2018-05-16 04:06:16 | [diff] [blame] | 258 | // Other percent-encoded characters that are left alone when displaying a |
Matt Giuca | ee5e87f | 2018-06-07 04:18:41 | [diff] [blame] | 259 | // 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 Giuca | 29b1b56 | 2018-05-16 04:06:16 | [diff] [blame] | 262 | // Make sure that '+' isn't converted into ' '. |
| 263 | {L"C:\\foo\\romeo+juliet.txt", "file:/c:/foo/romeo+juliet.txt"}, |
Tommy Li | 3187fae | 2019-11-14 20:04:22 | [diff] [blame] | 264 | // SAMBA share case. |
| 265 | {L"\\\\computername\\ShareName\\Path\\Foo.txt", |
| 266 | "file://computername/ShareName/Path/Foo.txt"}, |
Xiaohan Wang | 2a6845b | 2022-01-08 04:40:57 | [diff] [blame] | 267 | #elif BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA) |
[email protected] | d96cf75 | 2014-04-09 04:05:28 | [diff] [blame] | 268 | {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] | d96cf75 | 2014-04-09 04:05:28 | [diff] [blame] | 273 | {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 Giuca | ee5e87f | 2018-06-07 04:18:41 | [diff] [blame] | 280 | // %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 Giuca | 29b1b56 | 2018-05-16 04:06:16 | [diff] [blame] | 285 | // Other percent-encoded characters that are left alone when displaying a |
Matt Giuca | ee5e87f | 2018-06-07 04:18:41 | [diff] [blame] | 286 | // 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 Giuca | 29b1b56 | 2018-05-16 04:06:16 | [diff] [blame] | 289 | // 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 Li | 3187fae | 2019-11-14 20:04:22 | [diff] [blame] | 292 | {L"/bar.txt", "file://\\bar.txt"}, |
Matt Giuca | 29b1b56 | 2018-05-16 04:06:16 | [diff] [blame] | 293 | {L"/c|/foo/bar.txt", "file:c|/foo\\bar.txt"}, |
| 294 | {L"/foo/bar.txt", "file:////foo\\bar.txt"}, |
Tommy Li | 3187fae | 2019-11-14 20:04:22 | [diff] [blame] | 295 | // 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] | d96cf75 | 2014-04-09 04:05:28 | [diff] [blame] | 305 | #endif |
| 306 | }; |
Ryan Sleevi | 4625214fa | 2018-05-10 16:42:45 | [diff] [blame] | 307 | for (const auto& test_case : url_cases) { |
Matt Giuca | ee5e87f | 2018-06-07 04:18:41 | [diff] [blame] | 308 | 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] | d96cf75 | 2014-04-09 04:05:28 | [diff] [blame] | 315 | } |
| 316 | |
Matt Giuca | 29b1b56 | 2018-05-16 04:06:16 | [diff] [blame] | 317 | // 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 Wang | 2a6845b | 2022-01-08 04:40:57 | [diff] [blame] | 324 | #if BUILDFLAG(IS_WIN) |
Matt Giuca | 29b1b56 | 2018-05-16 04:06:16 | [diff] [blame] | 325 | // On Windows, invalid UTF-8 bytes are interpreted using the default ANSI |
| 326 | // code page. This defaults to Windows-1252 (which we assume here). |
jdoerrie | 6312bf6 | 2019-02-01 22:03:42 | [diff] [blame] | 327 | const base::FilePath::CharType expected_output[] = |
| 328 | FILE_PATH_LITERAL("D:\\Blah\\\u2026\u2122.doc"); |
| 329 | EXPECT_EQ(expected_output, output.value()); |
Xiaohan Wang | 2a6845b | 2022-01-08 04:40:57 | [diff] [blame] | 330 | #elif BUILDFLAG(IS_POSIX) |
Matt Giuca | 29b1b56 | 2018-05-16 04:06:16 | [diff] [blame] | 331 | // 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] | d96cf75 | 2014-04-09 04:05:28 | [diff] [blame] | 334 | #endif |
Matt Giuca | 29b1b56 | 2018-05-16 04:06:16 | [diff] [blame] | 335 | } |
| 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 Wang | 2a6845b | 2022-01-08 04:40:57 | [diff] [blame] | 341 | #if BUILDFLAG(IS_WIN) |
Matt Giuca | 29b1b56 | 2018-05-16 04:06:16 | [diff] [blame] | 342 | // On Windows, invalid UTF-8 bytes are interpreted using the default ANSI |
| 343 | // code page. This defaults to Windows-1252 (which we assume here). |
jdoerrie | 6312bf6 | 2019-02-01 22:03:42 | [diff] [blame] | 344 | const base::FilePath::CharType expected_output[] = |
| 345 | FILE_PATH_LITERAL("D:\\Blah\\\u2026\u2122.doc"); |
| 346 | EXPECT_EQ(expected_output, output.value()); |
Xiaohan Wang | 2a6845b | 2022-01-08 04:40:57 | [diff] [blame] | 347 | #elif BUILDFLAG(IS_POSIX) |
Matt Giuca | 29b1b56 | 2018-05-16 04:06:16 | [diff] [blame] | 348 | // 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] | d96cf75 | 2014-04-09 04:05:28 | [diff] [blame] | 353 | |
| 354 | // Test that if a file URL is malformed, we get a failure |
| 355 | EXPECT_FALSE(FileURLToFilePath(GURL("filefoobar"), &output)); |
| 356 | } |
| 357 | |
Asanka Herath | 090f40e | 2018-04-05 20:46:50 | [diff] [blame] | 358 | TEST(FilenameUtilTest, GenerateSafeFileName) { |
[email protected] | d96cf75 | 2014-04-09 04:05:28 | [diff] [blame] | 359 | const struct { |
Asanka Herath | 090f40e | 2018-04-05 20:46:50 | [diff] [blame] | 360 | int line; |
[email protected] | d96cf75 | 2014-04-09 04:05:28 | [diff] [blame] | 361 | const char* mime_type; |
Asanka Herath | 090f40e | 2018-04-05 20:46:50 | [diff] [blame] | 362 | const char* filename; |
| 363 | const char* expected_filename; |
[email protected] | d96cf75 | 2014-04-09 04:05:28 | [diff] [blame] | 364 | } safe_tests[] = { |
Asanka Herath | 090f40e | 2018-04-05 20:46:50 | [diff] [blame] | 365 | {__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 Wang | 2a6845b | 2022-01-08 04:40:57 | [diff] [blame] | 376 | #if BUILDFLAG(IS_WIN) |
Asanka Herath | 090f40e | 2018-04-05 20:46:50 | [diff] [blame] | 377 | // 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 Wang | 2a6845b | 2022-01-08 04:40:57 | [diff] [blame] | 390 | #elif BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA) |
Asanka Herath | 090f40e | 2018-04-05 20:46:50 | [diff] [blame] | 391 | // 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 Wang | 2a6845b | 2022-01-08 04:40:57 | [diff] [blame] | 400 | #endif // BUILDFLAG(IS_WIN) |
[email protected] | d96cf75 | 2014-04-09 04:05:28 | [diff] [blame] | 401 | }; |
| 402 | |
Xiaohan Wang | 2a6845b | 2022-01-08 04:40:57 | [diff] [blame] | 403 | #if BUILDFLAG(IS_WIN) |
jdoerrie | 6312bf6 | 2019-02-01 22:03:42 | [diff] [blame] | 404 | base::FilePath base_path(FILE_PATH_LITERAL("C:\\foo")); |
Xiaohan Wang | 2a6845b | 2022-01-08 04:40:57 | [diff] [blame] | 405 | #elif BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA) |
Asanka Herath | 090f40e | 2018-04-05 20:46:50 | [diff] [blame] | 406 | 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] | d96cf75 | 2014-04-09 04:05:28 | [diff] [blame] | 416 | } |
| 417 | } |
| 418 | |
Asanka Herath | 27b7af9f | 2018-04-03 14:52:19 | [diff] [blame] | 419 | TEST(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] | d96cf75 | 2014-04-09 04:05:28 | [diff] [blame] | 426 | TEST(FilenameUtilTest, GenerateFileName) { |
[email protected] | d96cf75 | 2014-04-09 04:05:28 | [diff] [blame] | 427 | // 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 Herath | 97ae36c | 2018-04-03 14:47:07 | [diff] [blame] | 431 | {// 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 Herath | 27b7af9f | 2018-04-03 14:52:19 | [diff] [blame] | 545 | "", "", "application/x-chrome-extension", L"download", |
| 546 | L"\uc608\uc220 \uc608\uc220.jpg"}, |
Asanka Herath | 97ae36c | 2018-04-03 14:47:07 | [diff] [blame] | 547 | {// 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] | d96cf75 | 2014-04-09 04:05:28 | [diff] [blame] | 569 | }; |
| 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. |
mmenke | b00bf0a | 2016-01-05 20:11:18 | [diff] [blame] | 576 | {__LINE__, "http://www.google.com/.test.html", "", "", "", "", L"", |
asanka | 92a354f | 2015-01-31 00:37:40 | [diff] [blame] | 577 | 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 Herath | 97ae36c | 2018-04-03 14:47:07 | [diff] [blame] | 581 | __LINE__, "", "filename=../../../../././../a_file_name.txt", "", "", |
| 582 | "text/plain", L"download", L"_.._.._.._._._.._a_file_name.txt"}, |
asanka | 92a354f | 2015-01-31 00:37:40 | [diff] [blame] | 583 | {// Disposition has parent directories, remove directory separators |
Asanka Herath | 97ae36c | 2018-04-03 14:47:07 | [diff] [blame] | 584 | __LINE__, "", "filename=dir1/dir2/a_file_name.txt", "", "", "text/plain", |
| 585 | L"download", L"dir1_dir2_a_file_name.txt"}, |
asanka | 92a354f | 2015-01-31 00:37:40 | [diff] [blame] | 586 | {// Disposition has relative paths, remove directory separators |
Asanka Herath | 97ae36c | 2018-04-03 14:47:07 | [diff] [blame] | 587 | __LINE__, "", "filename=..\\..\\..\\..\\.\\.\\..\\a_file_name.txt", "", "", |
| 588 | "text/plain", L"download", L"_.._.._.._._._.._a_file_name.txt"}, |
asanka | 92a354f | 2015-01-31 00:37:40 | [diff] [blame] | 589 | {// Disposition has parent directories, remove directory separators |
Asanka Herath | 97ae36c | 2018-04-03 14:47:07 | [diff] [blame] | 590 | __LINE__, "", "filename=dir1\\dir2\\a_file_name.txt", "", "", "text/plain", |
| 591 | L"download", L"dir1_dir2_a_file_name.txt"}, |
asanka | 92a354f | 2015-01-31 00:37:40 | [diff] [blame] | 592 | {// Filename looks like HTML? |
Asanka Herath | 97ae36c | 2018-04-03 14:47:07 | [diff] [blame] | 593 | __LINE__, "", "filename=\"<blink>Hello kitty</blink>\"", "", "", |
| 594 | "text/plain", L"default", L"_blink_Hello kitty__blink_"}, |
asanka | 92a354f | 2015-01-31 00:37:40 | [diff] [blame] | 595 | {// A normal avi should get .avi and not .avi.avi |
Asanka Herath | 97ae36c | 2018-04-03 14:47:07 | [diff] [blame] | 596 | __LINE__, "https://example.com/misc/2.avi", "", "", "", "video/x-msvideo", |
| 597 | L"download", L"2.avi"}, |
Matt Giuca | 25c6a34f | 2018-09-07 00:48:17 | [diff] [blame] | 598 | {// Slashes are illegal, and should be replaced with underscores. |
Asanka Herath | 97ae36c | 2018-04-03 14:47:07 | [diff] [blame] | 599 | __LINE__, "http://example.com/foo%2f..%2fbar.jpg", "", "", "", |
Matt Giuca | 25c6a34f | 2018-09-07 00:48:17 | [diff] [blame] | 600 | "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 Herath | 97ae36c | 2018-04-03 14:47:07 | [diff] [blame] | 606 | {// Extension generation for C-D derived filenames. |
| 607 | __LINE__, "", "filename=my-cat", "", "", "image/jpeg", L"download", |
| 608 | L"my-cat"}, |
asanka | 92a354f | 2015-01-31 00:37:40 | [diff] [blame] | 609 | {// Unknown MIME type |
Asanka Herath | 97ae36c | 2018-04-03 14:47:07 | [diff] [blame] | 610 | __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 Wang | 2a6845b | 2022-01-08 04:40:57 | [diff] [blame] | 615 | #if BUILDFLAG(IS_WIN) |
Asanka Herath | 97ae36c | 2018-04-03 14:47:07 | [diff] [blame] | 616 | // 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 Wang | 2a6845b | 2022-01-08 04:40:57 | [diff] [blame] | 627 | #elif BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA) |
Asanka Herath | 97ae36c | 2018-04-03 14:47:07 | [diff] [blame] | 628 | // 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] | d96cf75 | 2014-04-09 04:05:28 | [diff] [blame] | 639 | #endif |
Asanka Herath | 97ae36c | 2018-04-03 14:47:07 | [diff] [blame] | 640 | {__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] | d96cf75 | 2014-04-09 04:05:28 | [diff] [blame] | 649 | // Reserved words on Windows |
Asanka Herath | 97ae36c | 2018-04-03 14:47:07 | [diff] [blame] | 650 | {__LINE__, "", "filename=COM1", "", "", "application/foo-bar", L"download", |
Xiaohan Wang | 2a6845b | 2022-01-08 04:40:57 | [diff] [blame] | 651 | #if BUILDFLAG(IS_WIN) |
asanka | 92a354f | 2015-01-31 00:37:40 | [diff] [blame] | 652 | L"_COM1" |
[email protected] | d96cf75 | 2014-04-09 04:05:28 | [diff] [blame] | 653 | #else |
asanka | 92a354f | 2015-01-31 00:37:40 | [diff] [blame] | 654 | L"COM1" |
[email protected] | d96cf75 | 2014-04-09 04:05:28 | [diff] [blame] | 655 | #endif |
| 656 | }, |
Asanka Herath | 97ae36c | 2018-04-03 14:47:07 | [diff] [blame] | 657 | {__LINE__, "", "filename=COM4.txt", "", "", "text/plain", L"download", |
Xiaohan Wang | 2a6845b | 2022-01-08 04:40:57 | [diff] [blame] | 658 | #if BUILDFLAG(IS_WIN) |
asanka | 92a354f | 2015-01-31 00:37:40 | [diff] [blame] | 659 | L"_COM4.txt" |
[email protected] | d96cf75 | 2014-04-09 04:05:28 | [diff] [blame] | 660 | #else |
asanka | 92a354f | 2015-01-31 00:37:40 | [diff] [blame] | 661 | L"COM4.txt" |
[email protected] | d96cf75 | 2014-04-09 04:05:28 | [diff] [blame] | 662 | #endif |
| 663 | }, |
Asanka Herath | 97ae36c | 2018-04-03 14:47:07 | [diff] [blame] | 664 | {__LINE__, "", "filename=lpt1.TXT", "", "", "text/plain", L"download", |
Xiaohan Wang | 2a6845b | 2022-01-08 04:40:57 | [diff] [blame] | 665 | #if BUILDFLAG(IS_WIN) |
asanka | 92a354f | 2015-01-31 00:37:40 | [diff] [blame] | 666 | L"_lpt1.TXT" |
[email protected] | d96cf75 | 2014-04-09 04:05:28 | [diff] [blame] | 667 | #else |
asanka | 92a354f | 2015-01-31 00:37:40 | [diff] [blame] | 668 | L"lpt1.TXT" |
[email protected] | d96cf75 | 2014-04-09 04:05:28 | [diff] [blame] | 669 | #endif |
| 670 | }, |
Asanka Herath | 97ae36c | 2018-04-03 14:47:07 | [diff] [blame] | 671 | {__LINE__, "", "filename=clock$.txt", "", "", "text/plain", L"download", |
Xiaohan Wang | 2a6845b | 2022-01-08 04:40:57 | [diff] [blame] | 672 | #if BUILDFLAG(IS_WIN) |
asanka | 92a354f | 2015-01-31 00:37:40 | [diff] [blame] | 673 | L"_clock$.txt" |
[email protected] | d96cf75 | 2014-04-09 04:05:28 | [diff] [blame] | 674 | #else |
asanka | 92a354f | 2015-01-31 00:37:40 | [diff] [blame] | 675 | L"clock$.txt" |
[email protected] | d96cf75 | 2014-04-09 04:05:28 | [diff] [blame] | 676 | #endif |
| 677 | }, |
asanka | 92a354f | 2015-01-31 00:37:40 | [diff] [blame] | 678 | {// Validation should also apply to sugested name |
Asanka Herath | 97ae36c | 2018-04-03 14:47:07 | [diff] [blame] | 679 | __LINE__, "", "", "", "clock$.txt", "text/plain", L"download", |
Xiaohan Wang | 2a6845b | 2022-01-08 04:40:57 | [diff] [blame] | 680 | #if BUILDFLAG(IS_WIN) |
asanka | 92a354f | 2015-01-31 00:37:40 | [diff] [blame] | 681 | L"_clock$.txt" |
[email protected] | d96cf75 | 2014-04-09 04:05:28 | [diff] [blame] | 682 | #else |
asanka | 92a354f | 2015-01-31 00:37:40 | [diff] [blame] | 683 | L"clock$.txt" |
[email protected] | d96cf75 | 2014-04-09 04:05:28 | [diff] [blame] | 684 | #endif |
| 685 | }, |
Asanka Herath | 97ae36c | 2018-04-03 14:47:07 | [diff] [blame] | 686 | {// 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 Wang | 2a6845b | 2022-01-08 04:40:57 | [diff] [blame] | 690 | #if BUILDFLAG(IS_WIN) |
asanka | 92a354f | 2015-01-31 00:37:40 | [diff] [blame] | 691 | L"Setup.exe.download" |
[email protected] | d96cf75 | 2014-04-09 04:05:28 | [diff] [blame] | 692 | #else |
asanka | 92a354f | 2015-01-31 00:37:40 | [diff] [blame] | 693 | L"Setup.exe.local" |
[email protected] | d96cf75 | 2014-04-09 04:05:28 | [diff] [blame] | 694 | #endif |
| 695 | }, |
Asanka Herath | 97ae36c | 2018-04-03 14:47:07 | [diff] [blame] | 696 | {__LINE__, "", "filename=Setup.exe.local.local", "", "", "", L"download", |
Xiaohan Wang | 2a6845b | 2022-01-08 04:40:57 | [diff] [blame] | 697 | #if BUILDFLAG(IS_WIN) |
asanka | 92a354f | 2015-01-31 00:37:40 | [diff] [blame] | 698 | L"Setup.exe.local.download" |
[email protected] | d96cf75 | 2014-04-09 04:05:28 | [diff] [blame] | 699 | #else |
asanka | 92a354f | 2015-01-31 00:37:40 | [diff] [blame] | 700 | L"Setup.exe.local.local" |
[email protected] | d96cf75 | 2014-04-09 04:05:28 | [diff] [blame] | 701 | #endif |
| 702 | }, |
Asanka Herath | 97ae36c | 2018-04-03 14:47:07 | [diff] [blame] | 703 | {__LINE__, "", "filename=Setup.exe.lnk", "", "", "", L"download", |
Xiaohan Wang | 2a6845b | 2022-01-08 04:40:57 | [diff] [blame] | 704 | #if BUILDFLAG(IS_WIN) |
asanka | 92a354f | 2015-01-31 00:37:40 | [diff] [blame] | 705 | L"Setup.exe.download" |
[email protected] | d96cf75 | 2014-04-09 04:05:28 | [diff] [blame] | 706 | #else |
asanka | 92a354f | 2015-01-31 00:37:40 | [diff] [blame] | 707 | L"Setup.exe.lnk" |
[email protected] | d96cf75 | 2014-04-09 04:05:28 | [diff] [blame] | 708 | #endif |
| 709 | }, |
Asanka Herath | 97ae36c | 2018-04-03 14:47:07 | [diff] [blame] | 710 | {__LINE__, "", "filename=Desktop.ini", "", "", "", L"download", |
Xiaohan Wang | 2a6845b | 2022-01-08 04:40:57 | [diff] [blame] | 711 | #if BUILDFLAG(IS_WIN) |
asanka | 92a354f | 2015-01-31 00:37:40 | [diff] [blame] | 712 | L"_Desktop.ini" |
[email protected] | d96cf75 | 2014-04-09 04:05:28 | [diff] [blame] | 713 | #else |
asanka | 92a354f | 2015-01-31 00:37:40 | [diff] [blame] | 714 | L"Desktop.ini" |
[email protected] | d96cf75 | 2014-04-09 04:05:28 | [diff] [blame] | 715 | #endif |
| 716 | }, |
Asanka Herath | 97ae36c | 2018-04-03 14:47:07 | [diff] [blame] | 717 | {__LINE__, "", "filename=Thumbs.db", "", "", "", L"download", |
Xiaohan Wang | 2a6845b | 2022-01-08 04:40:57 | [diff] [blame] | 718 | #if BUILDFLAG(IS_WIN) |
asanka | 92a354f | 2015-01-31 00:37:40 | [diff] [blame] | 719 | L"_Thumbs.db" |
[email protected] | d96cf75 | 2014-04-09 04:05:28 | [diff] [blame] | 720 | #else |
asanka | 92a354f | 2015-01-31 00:37:40 | [diff] [blame] | 721 | L"Thumbs.db" |
[email protected] | d96cf75 | 2014-04-09 04:05:28 | [diff] [blame] | 722 | #endif |
| 723 | }, |
Asanka Herath | 27b7af9f | 2018-04-03 14:52:19 | [diff] [blame] | 724 | |
| 725 | // Regression tests for older issues: |
asanka | 92a354f | 2015-01-31 00:37:40 | [diff] [blame] | 726 | {// http://crbug.com/5772. |
mmenke | b00bf0a | 2016-01-05 20:11:18 | [diff] [blame] | 727 | __LINE__, "http://www.example.com/foo.tar.gz", "", "", "", |
| 728 | "application/x-tar", L"download", L"foo.tar.gz"}, |
asanka | 92a354f | 2015-01-31 00:37:40 | [diff] [blame] | 729 | {// http://crbug.com/52250. |
mmenke | b00bf0a | 2016-01-05 20:11:18 | [diff] [blame] | 730 | __LINE__, "http://www.example.com/foo.tgz", "", "", "", |
| 731 | "application/x-tar", L"download", L"foo.tgz"}, |
asanka | 92a354f | 2015-01-31 00:37:40 | [diff] [blame] | 732 | {// http://crbug.com/7337. |
mmenke | b00bf0a | 2016-01-05 20:11:18 | [diff] [blame] | 733 | __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"}, |
asanka | 92a354f | 2015-01-31 00:37:40 | [diff] [blame] | 739 | {// http://crbug.com/20337 |
mmenke | b00bf0a | 2016-01-05 20:11:18 | [diff] [blame] | 740 | __LINE__, "http://www.example.com/.download.txt", "filename=.download.txt", |
| 741 | "", "", "text/plain", L"-download", L"download.txt"}, |
asanka | 92a354f | 2015-01-31 00:37:40 | [diff] [blame] | 742 | {// http://crbug.com/56855. |
mmenke | b00bf0a | 2016-01-05 20:11:18 | [diff] [blame] | 743 | __LINE__, "http://www.example.com/bar.sh", "", "", "", "application/x-sh", |
| 744 | L"download", L"bar.sh"}, |
asanka | 92a354f | 2015-01-31 00:37:40 | [diff] [blame] | 745 | {// http://crbug.com/61571 |
mmenke | b00bf0a | 2016-01-05 20:11:18 | [diff] [blame] | 746 | __LINE__, "http://www.example.com/npdf.php?fn=foobar.pdf", "", "", "", |
Asanka Herath | 27b7af9f | 2018-04-03 14:52:19 | [diff] [blame] | 747 | "application/x-chrome-extension", L"download", L"npdf.crx"}, |
asanka | 92a354f | 2015-01-31 00:37:40 | [diff] [blame] | 748 | {// Shouldn't overwrite C-D specified extension. |
mmenke | b00bf0a | 2016-01-05 20:11:18 | [diff] [blame] | 749 | __LINE__, "http://www.example.com/npdf.php?fn=foobar.pdf", |
| 750 | "filename=foobar.jpg", "", "", "text/plain", L"download", L"foobar.jpg"}, |
asanka | 92a354f | 2015-01-31 00:37:40 | [diff] [blame] | 751 | {// http://crbug.com/87719 |
mmenke | b00bf0a | 2016-01-05 20:11:18 | [diff] [blame] | 752 | __LINE__, "http://www.example.com/image.aspx?id=blargh", "", "", "", |
Asanka Herath | 27b7af9f | 2018-04-03 14:52:19 | [diff] [blame] | 753 | "application/x-chrome-extension", L"download", L"image.crx"}, |
mmenke | b00bf0a | 2016-01-05 20:11:18 | [diff] [blame] | 754 | {__LINE__, "http://www.example.com/image.aspx?id=blargh", "", "", " .foo", |
Xing Liu | f5d0556 | 2017-08-10 20:49:24 | [diff] [blame] | 755 | "", L"download", L"_.foo"}, |
mmenke | 35cc8801 | 2016-01-06 17:28:43 | [diff] [blame] | 756 | |
| 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). |
mmenke | b00bf0a | 2016-01-05 20:11:18 | [diff] [blame] | 760 | {__LINE__, "http://www.example.com/goat.tar.gz?wearing_hat=true", "", "", |
| 761 | "", "application/gzip", L"", L"goat.tar.gz"}, |
mmenke | 35cc8801 | 2016-01-06 17:28:43 | [diff] [blame] | 762 | {__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 Hijikata | 101ed2a | 2020-11-18 07:50:39 | [diff] [blame] | 769 | #if BUILDFLAG(IS_CHROMEOS_ASH) |
asanka | 92a354f | 2015-01-31 00:37:40 | [diff] [blame] | 770 | {// http://crosbug.com/26028 |
mmenke | b00bf0a | 2016-01-05 20:11:18 | [diff] [blame] | 771 | __LINE__, "http://www.example.com/fooa%cc%88.txt", "", "", "", |
| 772 | "image/jpeg", L"foo\xe4", L"foo\xe4.txt"}, |
[email protected] | d96cf75 | 2014-04-09 04:05:28 | [diff] [blame] | 773 | #endif |
Matt Giuca | 25c6a34f | 2018-09-07 00:48:17 | [diff] [blame] | 774 | |
| 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] | d96cf75 | 2014-04-09 04:05:28 | [diff] [blame] | 783 | }; |
| 784 | |
Ryan Sleevi | 4625214fa | 2018-05-10 16:42:45 | [diff] [blame] | 785 | for (const auto& selection_test : selection_tests) |
| 786 | RunGenerateFileNameTestCase(&selection_test); |
[email protected] | d96cf75 | 2014-04-09 04:05:28 | [diff] [blame] | 787 | |
Ryan Sleevi | 4625214fa | 2018-05-10 16:42:45 | [diff] [blame] | 788 | for (const auto& generation_test : generation_tests) |
| 789 | RunGenerateFileNameTestCase(&generation_test); |
[email protected] | d96cf75 | 2014-04-09 04:05:28 | [diff] [blame] | 790 | |
Ryan Sleevi | 4625214fa | 2018-05-10 16:42:45 | [diff] [blame] | 791 | for (const auto& generation_test : generation_tests) { |
| 792 | GenerateFilenameCase test_case = generation_test; |
[email protected] | d96cf75 | 2014-04-09 04:05:28 | [diff] [blame] | 793 | test_case.referrer_charset = "GBK"; |
| 794 | RunGenerateFileNameTestCase(&test_case); |
| 795 | } |
| 796 | } |
| 797 | |
dhnishi | c28dfc3 | 2015-07-08 02:21:31 | [diff] [blame] | 798 | TEST(FilenameUtilTest, IsReservedNameOnWindows) { |
Ryan Sleevi | 4625214fa | 2018-05-10 16:42:45 | [diff] [blame] | 799 | for (auto* basename : kSafePortableBasenames) { |
| 800 | EXPECT_FALSE(IsReservedNameOnWindows(base::FilePath(basename).value())) |
| 801 | << basename; |
dhnishi | c28dfc3 | 2015-07-08 02:21:31 | [diff] [blame] | 802 | } |
| 803 | |
Ryan Sleevi | 4625214fa | 2018-05-10 16:42:45 | [diff] [blame] | 804 | for (auto* basename : kUnsafePortableBasenamesForWin) { |
| 805 | EXPECT_TRUE(IsReservedNameOnWindows(base::FilePath(basename).value())) |
| 806 | << basename; |
dhnishi | c28dfc3 | 2015-07-08 02:21:31 | [diff] [blame] | 807 | } |
| 808 | } |
| 809 | |
[email protected] | d96cf75 | 2014-04-09 04:05:28 | [diff] [blame] | 810 | } // namespace net |