OLD | NEW |
---|---|
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "net/base/filename_util.h" | 5 #include "net/base/filename_util.h" |
6 | 6 |
7 #include "base/files/file_path.h" | 7 #include "base/files/file_path.h" |
8 #include "base/files/file_util.h" | 8 #include "base/files/file_util.h" |
9 #include "base/strings/string_util.h" | 9 #include "base/strings/string_util.h" |
10 #include "base/strings/utf_string_conversions.h" | 10 #include "base/strings/utf_string_conversions.h" |
(...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
109 FILE_PATH_LITERAL("a . ."), | 109 FILE_PATH_LITERAL("a . ."), |
110 FILE_PATH_LITERAL(" Computer"), | 110 FILE_PATH_LITERAL(" Computer"), |
111 FILE_PATH_LITERAL("My Computer.{a}"), | 111 FILE_PATH_LITERAL("My Computer.{a}"), |
112 FILE_PATH_LITERAL("My Computer.{20D04FE0-3AEA-1069-A2D8-08002B30309D}"), | 112 FILE_PATH_LITERAL("My Computer.{20D04FE0-3AEA-1069-A2D8-08002B30309D}"), |
113 #if !defined(OS_WIN) | 113 #if !defined(OS_WIN) |
114 FILE_PATH_LITERAL("a\\a"), | 114 FILE_PATH_LITERAL("a\\a"), |
115 #endif | 115 #endif |
116 }; | 116 }; |
117 | 117 |
118 static const base::FilePath::CharType* kSafePortableRelativePaths[] = { | 118 static const base::FilePath::CharType* kSafePortableRelativePaths[] = { |
119 FILE_PATH_LITERAL("a/a"), | 119 FILE_PATH_LITERAL("a/a"), |
120 #if defined(OS_WIN) | 120 #if defined(OS_WIN) |
121 FILE_PATH_LITERAL("a\\a"), | 121 FILE_PATH_LITERAL("a\\a"), |
122 #endif | 122 #endif |
123 }; | 123 }; |
124 | 124 |
125 TEST(FilenameUtilTest, IsSafePortablePathComponent) { | 125 TEST(FilenameUtilTest, IsSafePortablePathComponent) { |
126 for (size_t i = 0 ; i < arraysize(kSafePortableBasenames); ++i) { | 126 for (size_t i = 0; i < arraysize(kSafePortableBasenames); ++i) { |
127 EXPECT_TRUE(IsSafePortablePathComponent(base::FilePath( | 127 EXPECT_TRUE( |
128 kSafePortableBasenames[i]))) << kSafePortableBasenames[i]; | 128 IsSafePortablePathComponent(base::FilePath(kSafePortableBasenames[i]))) |
129 << kSafePortableBasenames[i]; | |
129 } | 130 } |
130 for (size_t i = 0 ; i < arraysize(kUnsafePortableBasenames); ++i) { | 131 for (size_t i = 0; i < arraysize(kUnsafePortableBasenames); ++i) { |
131 EXPECT_FALSE(IsSafePortablePathComponent(base::FilePath( | 132 EXPECT_FALSE(IsSafePortablePathComponent( |
132 kUnsafePortableBasenames[i]))) << kUnsafePortableBasenames[i]; | 133 base::FilePath(kUnsafePortableBasenames[i]))) |
134 << kUnsafePortableBasenames[i]; | |
133 } | 135 } |
134 for (size_t i = 0 ; i < arraysize(kSafePortableRelativePaths); ++i) { | 136 for (size_t i = 0; i < arraysize(kSafePortableRelativePaths); ++i) { |
135 EXPECT_FALSE(IsSafePortablePathComponent(base::FilePath( | 137 EXPECT_FALSE(IsSafePortablePathComponent( |
136 kSafePortableRelativePaths[i]))) << kSafePortableRelativePaths[i]; | 138 base::FilePath(kSafePortableRelativePaths[i]))) |
139 << kSafePortableRelativePaths[i]; | |
137 } | 140 } |
138 } | 141 } |
139 | 142 |
140 TEST(FilenameUtilTest, IsSafePortableRelativePath) { | 143 TEST(FilenameUtilTest, IsSafePortableRelativePath) { |
141 base::FilePath safe_dirname(FILE_PATH_LITERAL("a")); | 144 base::FilePath safe_dirname(FILE_PATH_LITERAL("a")); |
142 for (size_t i = 0 ; i < arraysize(kSafePortableBasenames); ++i) { | 145 for (size_t i = 0; i < arraysize(kSafePortableBasenames); ++i) { |
143 EXPECT_TRUE(IsSafePortableRelativePath(base::FilePath( | 146 EXPECT_TRUE( |
144 kSafePortableBasenames[i]))) << kSafePortableBasenames[i]; | 147 IsSafePortableRelativePath(base::FilePath(kSafePortableBasenames[i]))) |
145 EXPECT_TRUE(IsSafePortableRelativePath(safe_dirname.Append(base::FilePath( | 148 << kSafePortableBasenames[i]; |
146 kSafePortableBasenames[i])))) << kSafePortableBasenames[i]; | 149 EXPECT_TRUE(IsSafePortableRelativePath( |
150 safe_dirname.Append(base::FilePath(kSafePortableBasenames[i])))) | |
151 << kSafePortableBasenames[i]; | |
147 } | 152 } |
148 for (size_t i = 0 ; i < arraysize(kSafePortableRelativePaths); ++i) { | 153 for (size_t i = 0; i < arraysize(kSafePortableRelativePaths); ++i) { |
149 EXPECT_TRUE(IsSafePortableRelativePath(base::FilePath( | 154 EXPECT_TRUE(IsSafePortableRelativePath( |
150 kSafePortableRelativePaths[i]))) << kSafePortableRelativePaths[i]; | 155 base::FilePath(kSafePortableRelativePaths[i]))) |
151 EXPECT_TRUE(IsSafePortableRelativePath(safe_dirname.Append(base::FilePath( | 156 << kSafePortableRelativePaths[i]; |
152 kSafePortableRelativePaths[i])))) << kSafePortableRelativePaths[i]; | 157 EXPECT_TRUE(IsSafePortableRelativePath( |
158 safe_dirname.Append(base::FilePath(kSafePortableRelativePaths[i])))) | |
159 << kSafePortableRelativePaths[i]; | |
153 } | 160 } |
154 for (size_t i = 0 ; i < arraysize(kUnsafePortableBasenames); ++i) { | 161 for (size_t i = 0; i < arraysize(kUnsafePortableBasenames); ++i) { |
155 EXPECT_FALSE(IsSafePortableRelativePath(base::FilePath( | 162 EXPECT_FALSE( |
156 kUnsafePortableBasenames[i]))) << kUnsafePortableBasenames[i]; | 163 IsSafePortableRelativePath(base::FilePath(kUnsafePortableBasenames[i]))) |
164 << kUnsafePortableBasenames[i]; | |
157 if (!base::FilePath::StringType(kUnsafePortableBasenames[i]).empty()) { | 165 if (!base::FilePath::StringType(kUnsafePortableBasenames[i]).empty()) { |
158 EXPECT_FALSE(IsSafePortableRelativePath(safe_dirname.Append( | 166 EXPECT_FALSE(IsSafePortableRelativePath( |
159 base::FilePath(kUnsafePortableBasenames[i])))) | 167 safe_dirname.Append(base::FilePath(kUnsafePortableBasenames[i])))) |
160 << kUnsafePortableBasenames[i]; | 168 << kUnsafePortableBasenames[i]; |
161 } | 169 } |
162 } | 170 } |
163 } | 171 } |
164 | 172 |
165 TEST(FilenameUtilTest, FileURLConversion) { | 173 TEST(FilenameUtilTest, FileURLConversion) { |
166 // a list of test file names and the corresponding URLs | 174 // a list of test file names and the corresponding URLs |
167 const FileCase round_trip_cases[] = { | 175 const FileCase round_trip_cases[] = { |
168 #if defined(OS_WIN) | 176 #if defined(OS_WIN) |
169 {L"C:\\foo\\bar.txt", "file:///C:/foo/bar.txt"}, | 177 {L"C:\\foo\\bar.txt", "file:///C:/foo/bar.txt"}, |
170 {L"\\\\some computer\\foo\\bar.txt", | 178 {L"\\\\some computer\\foo\\bar.txt", |
171 "file://some%20computer/foo/bar.txt"}, // UNC | 179 "file://some%20computer/foo/bar.txt"}, // UNC |
172 {L"D:\\Name;with%some symbols*#", | 180 {L"D:\\Name;with%some symbols*#", |
173 "file:///D:/Name%3Bwith%25some%20symbols*%23"}, | 181 "file:///D:/Name%3Bwith%25some%20symbols*%23"}, |
174 // issue 14153: To be tested with the OS default codepage other than 1252. | 182 // issue 14153: To be tested with the OS default codepage other than 1252. |
175 {L"D:\\latin1\\caf\x00E9\x00DD.txt", | 183 {L"D:\\latin1\\caf\x00E9\x00DD.txt", |
176 "file:///D:/latin1/caf%C3%A9%C3%9D.txt"}, | 184 "file:///D:/latin1/caf%C3%A9%C3%9D.txt"}, |
177 {L"D:\\otherlatin\\caf\x0119.txt", | 185 {L"D:\\otherlatin\\caf\x0119.txt", "file:///D:/otherlatin/caf%C4%99.txt"}, |
178 "file:///D:/otherlatin/caf%C4%99.txt"}, | |
179 {L"D:\\greek\\\x03B1\x03B2\x03B3.txt", | 186 {L"D:\\greek\\\x03B1\x03B2\x03B3.txt", |
180 "file:///D:/greek/%CE%B1%CE%B2%CE%B3.txt"}, | 187 "file:///D:/greek/%CE%B1%CE%B2%CE%B3.txt"}, |
181 {L"D:\\Chinese\\\x6240\x6709\x4e2d\x6587\x7f51\x9875.doc", | 188 {L"D:\\Chinese\\\x6240\x6709\x4e2d\x6587\x7f51\x9875.doc", |
182 "file:///D:/Chinese/%E6%89%80%E6%9C%89%E4%B8%AD%E6%96%87%E7%BD%91" | 189 "file:///D:/Chinese/%E6%89%80%E6%9C%89%E4%B8%AD%E6%96%87%E7%BD%91" |
183 "%E9%A1%B5.doc"}, | 190 "%E9%A1%B5.doc"}, |
184 {L"D:\\plane1\\\xD835\xDC00\xD835\xDC01.txt", // Math alphabet "AB" | 191 {L"D:\\plane1\\\xD835\xDC00\xD835\xDC01.txt", // Math alphabet "AB" |
185 "file:///D:/plane1/%F0%9D%90%80%F0%9D%90%81.txt"}, | 192 "file:///D:/plane1/%F0%9D%90%80%F0%9D%90%81.txt"}, |
186 #elif defined(OS_POSIX) | 193 #elif defined(OS_POSIX) |
187 {L"/foo/bar.txt", "file:///foo/bar.txt"}, | 194 {L"/foo/bar.txt", "file:///foo/bar.txt"}, |
188 {L"/foo/BAR.txt", "file:///foo/BAR.txt"}, | 195 {L"/foo/BAR.txt", "file:///foo/BAR.txt"}, |
189 {L"/C:/foo/bar.txt", "file:///C:/foo/bar.txt"}, | 196 {L"/C:/foo/bar.txt", "file:///C:/foo/bar.txt"}, |
190 {L"/foo/bar?.txt", "file:///foo/bar%3F.txt"}, | 197 {L"/foo/bar?.txt", "file:///foo/bar%3F.txt"}, |
191 {L"/some computer/foo/bar.txt", "file:///some%20computer/foo/bar.txt"}, | 198 {L"/some computer/foo/bar.txt", "file:///some%20computer/foo/bar.txt"}, |
192 {L"/Name;with%some symbols*#", "file:///Name%3Bwith%25some%20symbols*%23"}, | 199 {L"/Name;with%some symbols*#", "file:///Name%3Bwith%25some%20symbols*%23"}, |
193 {L"/latin1/caf\x00E9\x00DD.txt", "file:///latin1/caf%C3%A9%C3%9D.txt"}, | 200 {L"/latin1/caf\x00E9\x00DD.txt", "file:///latin1/caf%C3%A9%C3%9D.txt"}, |
194 {L"/otherlatin/caf\x0119.txt", "file:///otherlatin/caf%C4%99.txt"}, | 201 {L"/otherlatin/caf\x0119.txt", "file:///otherlatin/caf%C4%99.txt"}, |
195 {L"/greek/\x03B1\x03B2\x03B3.txt", "file:///greek/%CE%B1%CE%B2%CE%B3.txt"}, | 202 {L"/greek/\x03B1\x03B2\x03B3.txt", "file:///greek/%CE%B1%CE%B2%CE%B3.txt"}, |
196 {L"/Chinese/\x6240\x6709\x4e2d\x6587\x7f51\x9875.doc", | 203 {L"/Chinese/\x6240\x6709\x4e2d\x6587\x7f51\x9875.doc", |
197 "file:///Chinese/%E6%89%80%E6%9C%89%E4%B8%AD%E6%96%87%E7%BD" | 204 "file:///Chinese/%E6%89%80%E6%9C%89%E4%B8%AD%E6%96%87%E7%BD" |
198 "%91%E9%A1%B5.doc"}, | 205 "%91%E9%A1%B5.doc"}, |
199 {L"/plane1/\x1D400\x1D401.txt", // Math alphabet "AB" | 206 {L"/plane1/\x1D400\x1D401.txt", // Math alphabet "AB" |
200 "file:///plane1/%F0%9D%90%80%F0%9D%90%81.txt"}, | 207 "file:///plane1/%F0%9D%90%80%F0%9D%90%81.txt"}, |
201 #endif | 208 #endif |
202 }; | 209 }; |
203 | 210 |
204 // First, we'll test that we can round-trip all of the above cases of URLs | 211 // First, we'll test that we can round-trip all of the above cases of URLs |
205 base::FilePath output; | 212 base::FilePath output; |
206 for (size_t i = 0; i < arraysize(round_trip_cases); i++) { | 213 for (size_t i = 0; i < arraysize(round_trip_cases); i++) { |
207 // convert to the file URL | 214 // convert to the file URL |
208 GURL file_url(FilePathToFileURL( | 215 GURL file_url( |
209 WStringAsFilePath(round_trip_cases[i].file))); | 216 FilePathToFileURL(WStringAsFilePath(round_trip_cases[i].file))); |
210 EXPECT_EQ(round_trip_cases[i].url, file_url.spec()); | 217 EXPECT_EQ(round_trip_cases[i].url, file_url.spec()); |
211 | 218 |
212 // Back to the filename. | 219 // Back to the filename. |
213 EXPECT_TRUE(FileURLToFilePath(file_url, &output)); | 220 EXPECT_TRUE(FileURLToFilePath(file_url, &output)); |
214 EXPECT_EQ(round_trip_cases[i].file, FilePathAsWString(output)); | 221 EXPECT_EQ(round_trip_cases[i].file, FilePathAsWString(output)); |
215 } | 222 } |
216 | 223 |
217 // Test that various file: URLs get decoded into the correct file type | 224 // Test that various file: URLs get decoded into the correct file type |
218 FileCase url_cases[] = { | 225 FileCase url_cases[] = { |
219 #if defined(OS_WIN) | 226 #if defined(OS_WIN) |
(...skipping 12 matching lines...) Expand all Loading... | |
232 {L"/c:/foo/bar.txt", "file:\\\\\\c:/foo/bar.txt"}, | 239 {L"/c:/foo/bar.txt", "file:\\\\\\c:/foo/bar.txt"}, |
233 {L"/foo/bar.txt", "file:foo/bar.txt"}, | 240 {L"/foo/bar.txt", "file:foo/bar.txt"}, |
234 {L"/bar.txt", "file://foo/bar.txt"}, | 241 {L"/bar.txt", "file://foo/bar.txt"}, |
235 {L"/foo/bar.txt", "file:///foo/bar.txt"}, | 242 {L"/foo/bar.txt", "file:///foo/bar.txt"}, |
236 {L"/foo/bar.txt", "file:////foo/bar.txt"}, | 243 {L"/foo/bar.txt", "file:////foo/bar.txt"}, |
237 {L"/foo/bar.txt", "file:////foo//bar.txt"}, | 244 {L"/foo/bar.txt", "file:////foo//bar.txt"}, |
238 {L"/foo/bar.txt", "file:////foo///bar.txt"}, | 245 {L"/foo/bar.txt", "file:////foo///bar.txt"}, |
239 {L"/foo/bar.txt", "file:////foo////bar.txt"}, | 246 {L"/foo/bar.txt", "file:////foo////bar.txt"}, |
240 {L"/c:/foo/bar.txt", "file:\\\\\\c:/foo/bar.txt"}, | 247 {L"/c:/foo/bar.txt", "file:\\\\\\c:/foo/bar.txt"}, |
241 {L"/c:/foo/bar.txt", "file:c:/foo/bar.txt"}, | 248 {L"/c:/foo/bar.txt", "file:c:/foo/bar.txt"}, |
242 // We get these wrong because GURL turns back slashes into forward | 249 // We get these wrong because GURL turns back slashes into forward |
243 // slashes. | 250 // slashes. |
244 //{L"/foo%5Cbar.txt", "file://foo\\bar.txt"}, | 251 //{L"/foo%5Cbar.txt", "file://foo\\bar.txt"}, |
jkarlin
2015/01/31 00:59:43
This indentation level seems unfortunate, but is s
asanka
2015/02/03 19:49:46
Done.
| |
245 //{L"/c|/foo%5Cbar.txt", "file:c|/foo\\bar.txt"}, | 252 //{L"/c|/foo%5Cbar.txt", "file:c|/foo\\bar.txt"}, |
246 //{L"/foo%5Cbar.txt", "file://foo\\bar.txt"}, | 253 //{L"/foo%5Cbar.txt", "file://foo\\bar.txt"}, |
247 //{L"/foo%5Cbar.txt", "file:////foo\\bar.txt"}, | 254 //{L"/foo%5Cbar.txt", "file:////foo\\bar.txt"}, |
248 //{L"/foo%5Cbar.txt", "file://foo\\bar.txt"}, | 255 //{L"/foo%5Cbar.txt", "file://foo\\bar.txt"}, |
249 #endif | 256 #endif |
250 }; | 257 }; |
251 for (size_t i = 0; i < arraysize(url_cases); i++) { | 258 for (size_t i = 0; i < arraysize(url_cases); i++) { |
252 FileURLToFilePath(GURL(url_cases[i].url), &output); | 259 FileURLToFilePath(GURL(url_cases[i].url), &output); |
253 EXPECT_EQ(url_cases[i].file, FilePathAsWString(output)); | 260 EXPECT_EQ(url_cases[i].file, FilePathAsWString(output)); |
254 } | 261 } |
255 | 262 |
256 // Unfortunately, UTF8ToWide discards invalid UTF8 input. | 263 // Unfortunately, UTF8ToWide discards invalid UTF8 input. |
257 #ifdef BUG_878908_IS_FIXED | 264 #ifdef BUG_878908_IS_FIXED |
258 // Test that no conversion happens if the UTF-8 input is invalid, and that | 265 // Test that no conversion happens if the UTF-8 input is invalid, and that |
259 // the input is preserved in UTF-8 | 266 // the input is preserved in UTF-8 |
260 const char invalid_utf8[] = "file:///d:/Blah/\xff.doc"; | 267 const char invalid_utf8[] = "file:///d:/Blah/\xff.doc"; |
261 const wchar_t invalid_wide[] = L"D:\\Blah\\\xff.doc"; | 268 const wchar_t invalid_wide[] = L"D:\\Blah\\\xff.doc"; |
262 EXPECT_TRUE(FileURLToFilePath( | 269 EXPECT_TRUE(FileURLToFilePath(GURL(std::string(invalid_utf8)), &output)); |
263 GURL(std::string(invalid_utf8)), &output)); | |
264 EXPECT_EQ(std::wstring(invalid_wide), output); | 270 EXPECT_EQ(std::wstring(invalid_wide), output); |
265 #endif | 271 #endif |
266 | 272 |
267 // Test that if a file URL is malformed, we get a failure | 273 // Test that if a file URL is malformed, we get a failure |
268 EXPECT_FALSE(FileURLToFilePath(GURL("filefoobar"), &output)); | 274 EXPECT_FALSE(FileURLToFilePath(GURL("filefoobar"), &output)); |
269 } | 275 } |
270 | 276 |
271 #if defined(OS_WIN) | 277 #if defined(OS_WIN) |
272 #define JPEG_EXT L".jpg" | 278 #define JPEG_EXT L".jpg" |
273 #define HTML_EXT L".htm" | 279 #define HTML_EXT L".htm" |
274 #elif defined(OS_MACOSX) | 280 #elif defined(OS_MACOSX) |
275 #define JPEG_EXT L".jpeg" | 281 #define JPEG_EXT L".jpeg" |
276 #define HTML_EXT L".html" | 282 #define HTML_EXT L".html" |
277 #else | 283 #else |
278 #define JPEG_EXT L".jpg" | 284 #define JPEG_EXT L".jpg" |
279 #define HTML_EXT L".html" | 285 #define HTML_EXT L".html" |
280 #endif | 286 #endif |
281 #define TXT_EXT L".txt" | 287 #define TXT_EXT L".txt" |
282 #define TAR_EXT L".tar" | 288 #define TAR_EXT L".tar" |
283 | 289 |
284 TEST(FilenameUtilTest, GenerateSafeFileName) { | 290 TEST(FilenameUtilTest, GenerateSafeFileName) { |
285 const struct { | 291 const struct { |
286 const char* mime_type; | 292 const char* mime_type; |
287 const base::FilePath::CharType* filename; | 293 const base::FilePath::CharType* filename; |
288 const base::FilePath::CharType* expected_filename; | 294 const base::FilePath::CharType* expected_filename; |
289 } safe_tests[] = { | 295 } safe_tests[] = { |
290 #if defined(OS_WIN) | 296 #if defined(OS_WIN) |
291 { | 297 {"text/html", |
292 "text/html", | 298 FILE_PATH_LITERAL("C:\\foo\\bar.htm"), |
293 FILE_PATH_LITERAL("C:\\foo\\bar.htm"), | 299 FILE_PATH_LITERAL("C:\\foo\\bar.htm")}, |
294 FILE_PATH_LITERAL("C:\\foo\\bar.htm") | 300 {"text/html", |
295 }, | 301 FILE_PATH_LITERAL("C:\\foo\\bar.html"), |
296 { | 302 FILE_PATH_LITERAL("C:\\foo\\bar.html")}, |
297 "text/html", | 303 {"text/html", |
298 FILE_PATH_LITERAL("C:\\foo\\bar.html"), | 304 FILE_PATH_LITERAL("C:\\foo\\bar"), |
299 FILE_PATH_LITERAL("C:\\foo\\bar.html") | 305 FILE_PATH_LITERAL("C:\\foo\\bar.htm")}, |
300 }, | 306 {"image/png", |
301 { | 307 FILE_PATH_LITERAL("C:\\bar.html"), |
302 "text/html", | 308 FILE_PATH_LITERAL("C:\\bar.html")}, |
303 FILE_PATH_LITERAL("C:\\foo\\bar"), | 309 {"image/png", |
304 FILE_PATH_LITERAL("C:\\foo\\bar.htm") | 310 FILE_PATH_LITERAL("C:\\bar"), |
305 }, | 311 FILE_PATH_LITERAL("C:\\bar.png")}, |
306 { | 312 {"text/html", |
307 "image/png", | 313 FILE_PATH_LITERAL("C:\\foo\\bar.exe"), |
308 FILE_PATH_LITERAL("C:\\bar.html"), | 314 FILE_PATH_LITERAL("C:\\foo\\bar.exe")}, |
309 FILE_PATH_LITERAL("C:\\bar.html") | 315 {"image/gif", |
310 }, | 316 FILE_PATH_LITERAL("C:\\foo\\bar.exe"), |
311 { | 317 FILE_PATH_LITERAL("C:\\foo\\bar.exe")}, |
312 "image/png", | 318 {"text/html", |
313 FILE_PATH_LITERAL("C:\\bar"), | 319 FILE_PATH_LITERAL("C:\\foo\\google.com"), |
314 FILE_PATH_LITERAL("C:\\bar.png") | 320 FILE_PATH_LITERAL("C:\\foo\\google.com")}, |
315 }, | 321 {"text/html", |
316 { | 322 FILE_PATH_LITERAL("C:\\foo\\con.htm"), |
317 "text/html", | 323 FILE_PATH_LITERAL("C:\\foo\\_con.htm")}, |
318 FILE_PATH_LITERAL("C:\\foo\\bar.exe"), | 324 {"text/html", |
319 FILE_PATH_LITERAL("C:\\foo\\bar.exe") | 325 FILE_PATH_LITERAL("C:\\foo\\con"), |
320 }, | 326 FILE_PATH_LITERAL("C:\\foo\\_con.htm")}, |
321 { | 327 {"text/html", |
322 "image/gif", | 328 FILE_PATH_LITERAL("C:\\foo\\harmless.{not-really-this-may-be-a-guid}"), |
323 FILE_PATH_LITERAL("C:\\foo\\bar.exe"), | 329 FILE_PATH_LITERAL("C:\\foo\\harmless.download")}, |
324 FILE_PATH_LITERAL("C:\\foo\\bar.exe") | 330 {"text/html", |
325 }, | 331 FILE_PATH_LITERAL("C:\\foo\\harmless.local"), |
326 { | 332 FILE_PATH_LITERAL("C:\\foo\\harmless.download")}, |
327 "text/html", | 333 {"text/html", |
328 FILE_PATH_LITERAL("C:\\foo\\google.com"), | 334 FILE_PATH_LITERAL("C:\\foo\\harmless.lnk"), |
329 FILE_PATH_LITERAL("C:\\foo\\google.com") | 335 FILE_PATH_LITERAL("C:\\foo\\harmless.download")}, |
330 }, | 336 {"text/html", |
331 { | 337 FILE_PATH_LITERAL("C:\\foo\\harmless.{mismatched-"), |
332 "text/html", | 338 FILE_PATH_LITERAL("C:\\foo\\harmless.{mismatched-")}, |
333 FILE_PATH_LITERAL("C:\\foo\\con.htm"), | |
334 FILE_PATH_LITERAL("C:\\foo\\_con.htm") | |
335 }, | |
336 { | |
337 "text/html", | |
338 FILE_PATH_LITERAL("C:\\foo\\con"), | |
339 FILE_PATH_LITERAL("C:\\foo\\_con.htm") | |
340 }, | |
341 { | |
342 "text/html", | |
343 FILE_PATH_LITERAL("C:\\foo\\harmless.{not-really-this-may-be-a-guid}"), | |
344 FILE_PATH_LITERAL("C:\\foo\\harmless.download") | |
345 }, | |
346 { | |
347 "text/html", | |
348 FILE_PATH_LITERAL("C:\\foo\\harmless.local"), | |
349 FILE_PATH_LITERAL("C:\\foo\\harmless.download") | |
350 }, | |
351 { | |
352 "text/html", | |
353 FILE_PATH_LITERAL("C:\\foo\\harmless.lnk"), | |
354 FILE_PATH_LITERAL("C:\\foo\\harmless.download") | |
355 }, | |
356 { | |
357 "text/html", | |
358 FILE_PATH_LITERAL("C:\\foo\\harmless.{mismatched-"), | |
359 FILE_PATH_LITERAL("C:\\foo\\harmless.{mismatched-") | |
360 }, | |
361 // Allow extension synonyms. | 339 // Allow extension synonyms. |
362 { | 340 {"image/jpeg", |
363 "image/jpeg", | 341 FILE_PATH_LITERAL("C:\\foo\\bar.jpg"), |
364 FILE_PATH_LITERAL("C:\\foo\\bar.jpg"), | 342 FILE_PATH_LITERAL("C:\\foo\\bar.jpg")}, |
365 FILE_PATH_LITERAL("C:\\foo\\bar.jpg") | 343 {"image/jpeg", |
366 }, | 344 FILE_PATH_LITERAL("C:\\foo\\bar.jpeg"), |
367 { | 345 FILE_PATH_LITERAL("C:\\foo\\bar.jpeg")}, |
368 "image/jpeg", | 346 #else // !defined(OS_WIN) |
jkarlin
2015/01/31 00:59:43
I wonder why it added a third space. That seems li
asanka
2015/02/03 19:49:46
The extra space appears to be to keep this comment
| |
369 FILE_PATH_LITERAL("C:\\foo\\bar.jpeg"), | 347 {"text/html", |
370 FILE_PATH_LITERAL("C:\\foo\\bar.jpeg") | 348 FILE_PATH_LITERAL("/foo/bar.htm"), |
371 }, | 349 FILE_PATH_LITERAL("/foo/bar.htm")}, |
372 #else // !defined(OS_WIN) | 350 {"text/html", |
373 { | 351 FILE_PATH_LITERAL("/foo/bar.html"), |
374 "text/html", | 352 FILE_PATH_LITERAL("/foo/bar.html")}, |
375 FILE_PATH_LITERAL("/foo/bar.htm"), | 353 {"text/html", |
376 FILE_PATH_LITERAL("/foo/bar.htm") | 354 FILE_PATH_LITERAL("/foo/bar"), |
377 }, | 355 FILE_PATH_LITERAL("/foo/bar.html")}, |
378 { | 356 {"image/png", |
379 "text/html", | 357 FILE_PATH_LITERAL("/bar.html"), |
380 FILE_PATH_LITERAL("/foo/bar.html"), | 358 FILE_PATH_LITERAL("/bar.html")}, |
381 FILE_PATH_LITERAL("/foo/bar.html") | 359 {"image/png", FILE_PATH_LITERAL("/bar"), FILE_PATH_LITERAL("/bar.png")}, |
382 }, | 360 {"image/gif", |
383 { | 361 FILE_PATH_LITERAL("/foo/bar.exe"), |
384 "text/html", | 362 FILE_PATH_LITERAL("/foo/bar.exe")}, |
385 FILE_PATH_LITERAL("/foo/bar"), | 363 {"text/html", |
386 FILE_PATH_LITERAL("/foo/bar.html") | 364 FILE_PATH_LITERAL("/foo/google.com"), |
387 }, | 365 FILE_PATH_LITERAL("/foo/google.com")}, |
388 { | 366 {"text/html", |
389 "image/png", | 367 FILE_PATH_LITERAL("/foo/con.htm"), |
390 FILE_PATH_LITERAL("/bar.html"), | 368 FILE_PATH_LITERAL("/foo/con.htm")}, |
391 FILE_PATH_LITERAL("/bar.html") | 369 {"text/html", |
392 }, | 370 FILE_PATH_LITERAL("/foo/con"), |
393 { | 371 FILE_PATH_LITERAL("/foo/con.html")}, |
394 "image/png", | |
395 FILE_PATH_LITERAL("/bar"), | |
396 FILE_PATH_LITERAL("/bar.png") | |
397 }, | |
398 { | |
399 "image/gif", | |
400 FILE_PATH_LITERAL("/foo/bar.exe"), | |
401 FILE_PATH_LITERAL("/foo/bar.exe") | |
402 }, | |
403 { | |
404 "text/html", | |
405 FILE_PATH_LITERAL("/foo/google.com"), | |
406 FILE_PATH_LITERAL("/foo/google.com") | |
407 }, | |
408 { | |
409 "text/html", | |
410 FILE_PATH_LITERAL("/foo/con.htm"), | |
411 FILE_PATH_LITERAL("/foo/con.htm") | |
412 }, | |
413 { | |
414 "text/html", | |
415 FILE_PATH_LITERAL("/foo/con"), | |
416 FILE_PATH_LITERAL("/foo/con.html") | |
417 }, | |
418 // Allow extension synonyms. | 372 // Allow extension synonyms. |
419 { | 373 {"image/jpeg", |
420 "image/jpeg", | 374 FILE_PATH_LITERAL("/bar.jpg"), |
421 FILE_PATH_LITERAL("/bar.jpg"), | 375 FILE_PATH_LITERAL("/bar.jpg")}, |
422 FILE_PATH_LITERAL("/bar.jpg") | 376 {"image/jpeg", |
423 }, | 377 FILE_PATH_LITERAL("/bar.jpeg"), |
424 { | 378 FILE_PATH_LITERAL("/bar.jpeg")}, |
425 "image/jpeg", | |
426 FILE_PATH_LITERAL("/bar.jpeg"), | |
427 FILE_PATH_LITERAL("/bar.jpeg") | |
428 }, | |
429 #endif // !defined(OS_WIN) | 379 #endif // !defined(OS_WIN) |
430 }; | 380 }; |
431 | 381 |
432 for (size_t i = 0; i < arraysize(safe_tests); ++i) { | 382 for (size_t i = 0; i < arraysize(safe_tests); ++i) { |
433 base::FilePath file_path(safe_tests[i].filename); | 383 base::FilePath file_path(safe_tests[i].filename); |
434 GenerateSafeFileName(safe_tests[i].mime_type, false, &file_path); | 384 GenerateSafeFileName(safe_tests[i].mime_type, false, &file_path); |
435 EXPECT_EQ(safe_tests[i].expected_filename, file_path.value()) | 385 EXPECT_EQ(safe_tests[i].expected_filename, file_path.value()) |
436 << "Iteration " << i; | 386 << "Iteration " << i; |
437 } | 387 } |
438 } | 388 } |
439 | 389 |
440 TEST(FilenameUtilTest, GenerateFileName) { | 390 TEST(FilenameUtilTest, GenerateFileName) { |
441 // Tests whether the correct filename is selected from the the given | 391 // Tests whether the correct filename is selected from the the given |
442 // parameters and that Content-Disposition headers are properly | 392 // parameters and that Content-Disposition headers are properly |
443 // handled including failovers when the header is malformed. | 393 // handled including failovers when the header is malformed. |
444 const GenerateFilenameCase selection_tests[] = { | 394 const GenerateFilenameCase selection_tests[] = { |
445 { | 395 {__LINE__, |
446 __LINE__, | 396 "http://www.google.com/", |
447 "http://www.google.com/", | 397 "attachment; filename=test.html", |
448 "attachment; filename=test.html", | 398 "", |
449 "", | 399 "", |
450 "", | 400 "", |
451 "", | 401 L"", |
452 L"", | 402 L"test.html"}, |
453 L"test.html" | 403 {__LINE__, |
454 }, | 404 "http://www.google.com/", |
455 { | 405 "attachment; filename=\"test.html\"", |
456 __LINE__, | 406 "", |
457 "http://www.google.com/", | 407 "", |
458 "attachment; filename=\"test.html\"", | 408 "", |
459 "", | 409 L"", |
460 "", | 410 L"test.html"}, |
461 "", | 411 {__LINE__, |
462 L"", | 412 "http://www.google.com/", |
463 L"test.html" | 413 "attachment; filename= \"test.html\"", |
464 }, | 414 "", |
465 { | 415 "", |
466 __LINE__, | 416 "", |
467 "http://www.google.com/", | 417 L"", |
468 "attachment; filename= \"test.html\"", | 418 L"test.html"}, |
469 "", | 419 {__LINE__, |
470 "", | 420 "http://www.google.com/", |
471 "", | 421 "attachment; filename = \"test.html\"", |
472 L"", | 422 "", |
473 L"test.html" | 423 "", |
474 }, | 424 "", |
475 { | 425 L"", |
476 __LINE__, | 426 L"test.html"}, |
477 "http://www.google.com/", | 427 {// filename is whitespace. Should failover to URL host |
478 "attachment; filename = \"test.html\"", | 428 __LINE__, |
479 "", | 429 "http://www.google.com/", |
480 "", | 430 "attachment; filename= ", |
481 "", | 431 "", |
482 L"", | 432 "", |
483 L"test.html" | 433 "", |
484 }, | 434 L"", |
485 { // filename is whitespace. Should failover to URL host | 435 L"www.google.com"}, |
486 __LINE__, | 436 {// No filename. |
487 "http://www.google.com/", | 437 __LINE__, |
488 "attachment; filename= ", | 438 "http://www.google.com/path/test.html", |
489 "", | 439 "attachment", |
490 "", | 440 "", |
491 "", | 441 "", |
492 L"", | 442 "", |
493 L"www.google.com" | 443 L"", |
494 }, | 444 L"test.html"}, |
495 { // No filename. | 445 {// Ditto |
496 __LINE__, | 446 __LINE__, |
497 "http://www.google.com/path/test.html", | 447 "http://www.google.com/path/test.html", |
498 "attachment", | 448 "attachment;", |
499 "", | 449 "", |
500 "", | 450 "", |
501 "", | 451 "", |
502 L"", | 452 L"", |
503 L"test.html" | 453 L"test.html"}, |
504 }, | 454 {// No C-D |
505 { // Ditto | 455 __LINE__, |
506 __LINE__, | 456 "http://www.google.com/", |
507 "http://www.google.com/path/test.html", | 457 "", |
508 "attachment;", | 458 "", |
509 "", | 459 "", |
510 "", | 460 "", |
511 "", | 461 L"", |
512 L"", | 462 L"www.google.com"}, |
513 L"test.html" | 463 {__LINE__, |
514 }, | 464 "http://www.google.com/test.html", |
515 { // No C-D | 465 "", |
516 __LINE__, | 466 "", |
517 "http://www.google.com/", | 467 "", |
518 "", | 468 "", |
519 "", | 469 L"", |
520 "", | 470 L"test.html"}, |
521 "", | 471 {// Now that we use src/url's ExtractFileName, this case falls back to |
522 L"", | 472 // the hostname. If this behavior is not desirable, we'd better change |
523 L"www.google.com" | 473 // ExtractFileName (in url_parse.cc). |
524 }, | 474 __LINE__, |
525 { | 475 "http://www.google.com/path/", |
526 __LINE__, | 476 "", |
527 "http://www.google.com/test.html", | 477 "", |
528 "", | 478 "", |
529 "", | 479 "", |
530 "", | 480 L"", |
531 "", | 481 L"www.google.com"}, |
532 L"", | 482 {__LINE__, "http://www.google.com/path", "", "", "", "", L"", L"path"}, |
533 L"test.html" | 483 {__LINE__, "file:///", "", "", "", "", L"", L"download"}, |
534 }, | 484 {__LINE__, "file:///path/testfile", "", "", "", "", L"", L"testfile"}, |
535 { // Now that we use src/url's ExtractFileName, this case falls back to | 485 {__LINE__, "non-standard-scheme:", "", "", "", "", L"", L"download"}, |
536 // the hostname. If this behavior is not desirable, we'd better change | 486 {// C-D should override default |
537 // ExtractFileName (in url_parse.cc). | 487 __LINE__, |
538 __LINE__, | 488 "http://www.google.com/", |
539 "http://www.google.com/path/", | 489 "attachment; filename =\"test.html\"", |
540 "", | 490 "", |
541 "", | 491 "", |
542 "", | 492 "", |
543 "", | 493 L"download", |
544 L"", | 494 L"test.html"}, |
545 L"www.google.com" | 495 {// But the URL shouldn't |
546 }, | 496 __LINE__, |
547 { | 497 "http://www.google.com/", |
548 __LINE__, | 498 "", |
549 "http://www.google.com/path", | 499 "", |
550 "", | 500 "", |
551 "", | 501 "", |
552 "", | 502 L"download", |
553 "", | 503 L"download"}, |
554 L"", | 504 {__LINE__, |
555 L"path" | 505 "http://www.google.com/", |
556 }, | 506 "attachment; filename=\"../test.html\"", |
557 { | 507 "", |
558 __LINE__, | 508 "", |
559 "file:///", | 509 "", |
560 "", | 510 L"", |
561 "", | 511 L"-test.html"}, |
562 "", | 512 {__LINE__, |
563 "", | 513 "http://www.google.com/", |
564 L"", | 514 "attachment; filename=\"..\\test.html\"", |
565 L"download" | 515 "", |
566 }, | 516 "", |
567 { | 517 "", |
568 __LINE__, | 518 L"", |
569 "file:///path/testfile", | 519 L"test.html"}, |
570 "", | 520 {__LINE__, |
571 "", | 521 "http://www.google.com/", |
572 "", | 522 "attachment; filename=\"..\\\\test.html\"", |
573 "", | 523 "", |
574 L"", | 524 "", |
575 L"testfile" | 525 "", |
576 }, | 526 L"", |
577 { | 527 L"-test.html"}, |
578 __LINE__, | 528 {// Filename disappears after leading and trailing periods are removed. |
579 "non-standard-scheme:", | 529 __LINE__, |
580 "", | 530 "http://www.google.com/", |
581 "", | 531 "attachment; filename=\"..\"", |
582 "", | 532 "", |
583 "", | 533 "", |
584 L"", | 534 "", |
585 L"download" | 535 L"default", |
586 }, | 536 L"default"}, |
587 { // C-D should override default | 537 {// C-D specified filename disappears. Failover to final filename. |
588 __LINE__, | 538 __LINE__, |
589 "http://www.google.com/", | 539 "http://www.google.com/test.html", |
590 "attachment; filename =\"test.html\"", | 540 "attachment; filename=\"..\"", |
591 "", | 541 "", |
592 "", | 542 "", |
593 "", | 543 "", |
594 L"download", | 544 L"default", |
595 L"test.html" | 545 L"default"}, |
596 }, | |
597 { // But the URL shouldn't | |
598 __LINE__, | |
599 "http://www.google.com/", | |
600 "", | |
601 "", | |
602 "", | |
603 "", | |
604 L"download", | |
605 L"download" | |
606 }, | |
607 { | |
608 __LINE__, | |
609 "http://www.google.com/", | |
610 "attachment; filename=\"../test.html\"", | |
611 "", | |
612 "", | |
613 "", | |
614 L"", | |
615 L"-test.html" | |
616 }, | |
617 { | |
618 __LINE__, | |
619 "http://www.google.com/", | |
620 "attachment; filename=\"..\\test.html\"", | |
621 "", | |
622 "", | |
623 "", | |
624 L"", | |
625 L"test.html" | |
626 }, | |
627 { | |
628 __LINE__, | |
629 "http://www.google.com/", | |
630 "attachment; filename=\"..\\\\test.html\"", | |
631 "", | |
632 "", | |
633 "", | |
634 L"", | |
635 L"-test.html" | |
636 }, | |
637 { // Filename disappears after leading and trailing periods are removed. | |
638 __LINE__, | |
639 "http://www.google.com/", | |
640 "attachment; filename=\"..\"", | |
641 "", | |
642 "", | |
643 "", | |
644 L"default", | |
645 L"default" | |
646 }, | |
647 { // C-D specified filename disappears. Failover to final filename. | |
648 __LINE__, | |
649 "http://www.google.com/test.html", | |
650 "attachment; filename=\"..\"", | |
651 "", | |
652 "", | |
653 "", | |
654 L"default", | |
655 L"default" | |
656 }, | |
657 // Below is a small subset of cases taken from HttpContentDisposition tests. | 546 // Below is a small subset of cases taken from HttpContentDisposition tests. |
658 { | 547 {__LINE__, |
659 __LINE__, | 548 "http://www.google.com/", |
660 "http://www.google.com/", | 549 "attachment; filename=\"%EC%98%88%EC%88%A0%20" |
661 "attachment; filename=\"%EC%98%88%EC%88%A0%20" | 550 "%EC%98%88%EC%88%A0.jpg\"", |
662 "%EC%98%88%EC%88%A0.jpg\"", | 551 "", |
663 "", | 552 "", |
664 "", | 553 "", |
665 "", | 554 L"", |
666 L"", | 555 L"\uc608\uc220 \uc608\uc220.jpg"}, |
667 L"\uc608\uc220 \uc608\uc220.jpg" | 556 {__LINE__, |
668 }, | 557 "http://www.google.com/%EC%98%88%EC%88%A0%20%EC%98%88%EC%88%A0.jpg", |
669 { | 558 "", |
670 __LINE__, | 559 "", |
671 "http://www.google.com/%EC%98%88%EC%88%A0%20%EC%98%88%EC%88%A0.jpg", | 560 "", |
672 "", | 561 "", |
673 "", | 562 L"download", |
674 "", | 563 L"\uc608\uc220 \uc608\uc220.jpg"}, |
675 "", | 564 {__LINE__, |
676 L"download", | 565 "http://www.google.com/", |
677 L"\uc608\uc220 \uc608\uc220.jpg" | 566 "attachment;", |
678 }, | 567 "", |
679 { | 568 "", |
680 __LINE__, | 569 "", |
681 "http://www.google.com/", | 570 L"\uB2E4\uC6B4\uB85C\uB4DC", |
682 "attachment;", | 571 L"\uB2E4\uC6B4\uB85C\uB4DC"}, |
683 "", | 572 {__LINE__, |
684 "", | 573 "http://www.google.com/", |
685 "", | 574 "attachment; filename=\"=?EUC-JP?Q?=B7=DD=BD=" |
686 L"\uB2E4\uC6B4\uB85C\uB4DC", | 575 "D13=2Epng?=\"", |
687 L"\uB2E4\uC6B4\uB85C\uB4DC" | 576 "", |
688 }, | 577 "", |
689 { | 578 "", |
690 __LINE__, | 579 L"download", |
691 "http://www.google.com/", | 580 L"\u82b8\u88533.png"}, |
692 "attachment; filename=\"=?EUC-JP?Q?=B7=DD=BD=" | 581 {__LINE__, |
693 "D13=2Epng?=\"", | 582 "http://www.example.com/images?id=3", |
694 "", | 583 "attachment; filename=caf\xc3\xa9.png", |
695 "", | 584 "iso-8859-1", |
696 "", | 585 "", |
697 L"download", | 586 "", |
698 L"\u82b8\u88533.png" | 587 L"", |
699 }, | 588 L"caf\u00e9.png"}, |
700 { | 589 {__LINE__, |
701 __LINE__, | 590 "http://www.example.com/images?id=3", |
702 "http://www.example.com/images?id=3", | 591 "attachment; filename=caf\xe5.png", |
703 "attachment; filename=caf\xc3\xa9.png", | 592 "windows-1253", |
704 "iso-8859-1", | 593 "", |
705 "", | 594 "", |
706 "", | 595 L"", |
707 L"", | 596 L"caf\u03b5.png"}, |
708 L"caf\u00e9.png" | 597 {__LINE__, |
709 }, | 598 "http://www.example.com/file?id=3", |
710 { | 599 "attachment; name=\xcf\xc2\xd4\xd8.zip", |
711 __LINE__, | 600 "GBK", |
712 "http://www.example.com/images?id=3", | 601 "", |
713 "attachment; filename=caf\xe5.png", | 602 "", |
714 "windows-1253", | 603 L"", |
715 "", | 604 L"\u4e0b\u8f7d.zip"}, |
716 "", | 605 {// Invalid C-D header. Extracts filename from url. |
717 L"", | 606 __LINE__, |
718 L"caf\u03b5.png" | 607 "http://www.google.com/test.html", |
719 }, | 608 "attachment; filename==?iiso88591?Q?caf=EG?=", |
720 { | 609 "", |
721 __LINE__, | 610 "", |
722 "http://www.example.com/file?id=3", | 611 "", |
723 "attachment; name=\xcf\xc2\xd4\xd8.zip", | 612 L"", |
724 "GBK", | 613 L"test.html"}, |
725 "", | |
726 "", | |
727 L"", | |
728 L"\u4e0b\u8f7d.zip" | |
729 }, | |
730 { // Invalid C-D header. Extracts filename from url. | |
731 __LINE__, | |
732 "http://www.google.com/test.html", | |
733 "attachment; filename==?iiso88591?Q?caf=EG?=", | |
734 "", | |
735 "", | |
736 "", | |
737 L"", | |
738 L"test.html" | |
739 }, | |
740 // about: and data: URLs | 614 // about: and data: URLs |
741 { | 615 {__LINE__, "about:chrome", "", "", "", "", L"", L"download"}, |
742 __LINE__, | 616 {__LINE__, "data:,looks/like/a.path", "", "", "", "", L"", L"download"}, |
743 "about:chrome", | 617 {__LINE__, |
744 "", | 618 "data:text/plain;base64,VG8gYmUgb3Igbm90IHRvIGJlLg=", |
745 "", | 619 "", |
746 "", | 620 "", |
747 "", | 621 "", |
748 L"", | 622 "", |
749 L"download" | 623 L"", |
750 }, | 624 L"download"}, |
751 { | 625 {__LINE__, |
752 __LINE__, | 626 "data:,looks/like/a.path", |
753 "data:,looks/like/a.path", | 627 "", |
754 "", | 628 "", |
755 "", | 629 "", |
756 "", | 630 "", |
757 "", | 631 L"default_filename_is_given", |
758 L"", | 632 L"default_filename_is_given"}, |
759 L"download" | 633 {__LINE__, |
760 }, | 634 "data:,looks/like/a.path", |
761 { | 635 "", |
762 __LINE__, | 636 "", |
763 "data:text/plain;base64,VG8gYmUgb3Igbm90IHRvIGJlLg=", | 637 "", |
764 "", | 638 "", |
765 "", | 639 L"\u65e5\u672c\u8a9e", // Japanese Kanji. |
766 "", | 640 L"\u65e5\u672c\u8a9e"}, |
767 "", | 641 {// The filename encoding is specified by the referrer charset. |
768 L"", | 642 __LINE__, |
769 L"download" | 643 "http://example.com/V%FDvojov%E1%20psychologie.doc", |
770 }, | 644 "", |
771 { | 645 "iso-8859-1", |
772 __LINE__, | 646 "", |
773 "data:,looks/like/a.path", | 647 "", |
774 "", | 648 L"", |
775 "", | 649 L"V\u00fdvojov\u00e1 psychologie.doc"}, |
776 "", | 650 {// Suggested filename takes precedence over URL |
777 "", | 651 __LINE__, |
778 L"default_filename_is_given", | 652 "http://www.google.com/test", |
779 L"default_filename_is_given" | 653 "", |
780 }, | 654 "", |
781 { | 655 "suggested", |
782 __LINE__, | 656 "", |
783 "data:,looks/like/a.path", | 657 L"", |
784 "", | 658 L"suggested"}, |
785 "", | 659 {// The content-disposition has higher precedence over the suggested name. |
786 "", | 660 __LINE__, |
787 "", | 661 "http://www.google.com/test", |
788 L"\u65e5\u672c\u8a9e", // Japanese Kanji. | 662 "attachment; filename=test.html", |
789 L"\u65e5\u672c\u8a9e" | 663 "", |
790 }, | 664 "suggested", |
791 { // The filename encoding is specified by the referrer charset. | 665 "", |
792 __LINE__, | 666 L"", |
793 "http://example.com/V%FDvojov%E1%20psychologie.doc", | 667 L"test.html"}, |
794 "", | 668 {__LINE__, |
795 "iso-8859-1", | 669 "http://www.google.com/test", |
796 "", | 670 "attachment; filename=test", |
797 "", | 671 "utf-8", |
798 L"", | 672 "", |
799 L"V\u00fdvojov\u00e1 psychologie.doc" | 673 "image/png", |
800 }, | 674 L"", |
801 { // Suggested filename takes precedence over URL | 675 L"test"}, |
802 __LINE__, | |
803 "http://www.google.com/test", | |
804 "", | |
805 "", | |
806 "suggested", | |
807 "", | |
808 L"", | |
809 L"suggested" | |
810 }, | |
811 { // The content-disposition has higher precedence over the suggested name. | |
812 __LINE__, | |
813 "http://www.google.com/test", | |
814 "attachment; filename=test.html", | |
815 "", | |
816 "suggested", | |
817 "", | |
818 L"", | |
819 L"test.html" | |
820 }, | |
821 { | |
822 __LINE__, | |
823 "http://www.google.com/test", | |
824 "attachment; filename=test", | |
825 "utf-8", | |
826 "", | |
827 "image/png", | |
828 L"", | |
829 L"test" | |
830 }, | |
831 #if 0 | 676 #if 0 |
832 { // The filename encoding doesn't match the referrer charset, the system | 677 { // The filename encoding doesn't match the referrer charset, the system |
833 // charset, or UTF-8. | 678 // charset, or UTF-8. |
834 // TODO(jshin): we need to handle this case. | 679 // TODO(jshin): we need to handle this case. |
835 __LINE__, | 680 __LINE__, |
836 "http://example.com/V%FDvojov%E1%20psychologie.doc", | 681 "http://example.com/V%FDvojov%E1%20psychologie.doc", |
837 "", | 682 "", |
838 "utf-8", | 683 "utf-8", |
839 "", | 684 "", |
840 "", | 685 "", |
841 L"", | 686 L"", |
842 L"V\u00fdvojov\u00e1 psychologie.doc", | 687 L"V\u00fdvojov\u00e1 psychologie.doc", |
843 }, | 688 }, |
844 #endif | 689 #endif |
845 // Raw 8bit characters in C-D | 690 // Raw 8bit characters in C-D |
846 { | 691 {__LINE__, |
847 __LINE__, | 692 "http://www.example.com/images?id=3", |
848 "http://www.example.com/images?id=3", | 693 "attachment; filename=caf\xc3\xa9.png", |
849 "attachment; filename=caf\xc3\xa9.png", | 694 "iso-8859-1", |
850 "iso-8859-1", | 695 "", |
851 "", | 696 "image/png", |
852 "image/png", | 697 L"", |
853 L"", | 698 L"caf\u00e9.png"}, |
854 L"caf\u00e9.png" | 699 {__LINE__, |
855 }, | 700 "http://www.example.com/images?id=3", |
856 { | 701 "attachment; filename=caf\xe5.png", |
857 __LINE__, | 702 "windows-1253", |
858 "http://www.example.com/images?id=3", | 703 "", |
859 "attachment; filename=caf\xe5.png", | 704 "image/png", |
860 "windows-1253", | 705 L"", |
861 "", | 706 L"caf\u03b5.png"}, |
862 "image/png", | 707 {// No 'filename' keyword in the disposition, use the URL |
863 L"", | 708 __LINE__, |
864 L"caf\u03b5.png" | 709 "http://www.evil.com/my_download.txt", |
865 }, | 710 "a_file_name.txt", |
866 { // No 'filename' keyword in the disposition, use the URL | 711 "", |
867 __LINE__, | 712 "", |
868 "http://www.evil.com/my_download.txt", | 713 "text/plain", |
869 "a_file_name.txt", | 714 L"download", |
870 "", | 715 L"my_download.txt"}, |
871 "", | 716 {// Spaces in the disposition file name |
872 "text/plain", | 717 __LINE__, |
873 L"download", | 718 "http://www.frontpagehacker.com/a_download.exe", |
874 L"my_download.txt" | 719 "filename=My Downloaded File.exe", |
875 }, | 720 "", |
876 { // Spaces in the disposition file name | 721 "", |
877 __LINE__, | 722 "application/octet-stream", |
878 "http://www.frontpagehacker.com/a_download.exe", | 723 L"download", |
879 "filename=My Downloaded File.exe", | 724 L"My Downloaded File.exe"}, |
880 "", | 725 {// % encoded |
881 "", | 726 __LINE__, |
882 "application/octet-stream", | 727 "http://www.examples.com/", |
883 L"download", | 728 "attachment; " |
884 L"My Downloaded File.exe" | 729 "filename=\"%EC%98%88%EC%88%A0%20%EC%98%88%EC%88%A0.jpg\"", |
885 }, | 730 "", |
886 { // % encoded | 731 "", |
887 __LINE__, | 732 "image/jpeg", |
888 "http://www.examples.com/", | 733 L"download", |
889 "attachment; " | 734 L"\uc608\uc220 \uc608\uc220.jpg"}, |
890 "filename=\"%EC%98%88%EC%88%A0%20%EC%98%88%EC%88%A0.jpg\"", | 735 {// name= parameter |
891 "", | 736 __LINE__, |
892 "", | 737 "http://www.examples.com/q.cgi?id=abc", |
893 "image/jpeg", | 738 "attachment; name=abc de.pdf", |
894 L"download", | 739 "", |
895 L"\uc608\uc220 \uc608\uc220.jpg" | 740 "", |
896 }, | 741 "application/octet-stream", |
897 { // name= parameter | 742 L"download", |
898 __LINE__, | 743 L"abc de.pdf"}, |
899 "http://www.examples.com/q.cgi?id=abc", | 744 {__LINE__, |
900 "attachment; name=abc de.pdf", | 745 "http://www.example.com/path", |
901 "", | 746 "filename=\"=?EUC-JP?Q?=B7=DD=BD=D13=2Epng?=\"", |
902 "", | 747 "", |
903 "application/octet-stream", | 748 "", |
904 L"download", | 749 "image/png", |
905 L"abc de.pdf" | 750 L"download", |
906 }, | 751 L"\x82b8\x8853" |
907 { | 752 L"3.png"}, |
908 __LINE__, | 753 {// The following two have invalid CD headers and filenames come from the |
909 "http://www.example.com/path", | 754 // URL. |
910 "filename=\"=?EUC-JP?Q?=B7=DD=BD=D13=2Epng?=\"", | 755 __LINE__, |
911 "", | 756 "http://www.example.com/test%20123", |
912 "", | 757 "attachment; filename==?iiso88591?Q?caf=EG?=", |
913 "image/png", | 758 "", |
914 L"download", | 759 "", |
915 L"\x82b8\x8853" L"3.png" | 760 "image/jpeg", |
916 }, | 761 L"download", |
917 { // The following two have invalid CD headers and filenames come from the | 762 L"test 123" JPEG_EXT}, |
918 // URL. | 763 {__LINE__, |
919 __LINE__, | 764 "http://www.google.com/%EC%98%88%EC%88%A0%20%EC%98%88%EC%88%A0.jpg", |
920 "http://www.example.com/test%20123", | 765 "malformed_disposition", |
921 "attachment; filename==?iiso88591?Q?caf=EG?=", | 766 "", |
922 "", | 767 "", |
923 "", | 768 "image/jpeg", |
924 "image/jpeg", | 769 L"download", |
925 L"download", | 770 L"\uc608\uc220 \uc608\uc220.jpg"}, |
926 L"test 123" JPEG_EXT | 771 {// Invalid C-D. No filename from URL. Falls back to 'download'. |
927 }, | 772 __LINE__, |
928 { | 773 "http://www.google.com/path1/path2/", |
929 __LINE__, | 774 "attachment; filename==?iso88591?Q?caf=E3?", |
930 "http://www.google.com/%EC%98%88%EC%88%A0%20%EC%98%88%EC%88%A0.jpg", | 775 "", |
931 "malformed_disposition", | 776 "", |
932 "", | 777 "image/jpeg", |
933 "", | 778 L"download", |
934 "image/jpeg", | 779 L"download" JPEG_EXT}, |
935 L"download", | |
936 L"\uc608\uc220 \uc608\uc220.jpg" | |
937 }, | |
938 { // Invalid C-D. No filename from URL. Falls back to 'download'. | |
939 __LINE__, | |
940 "http://www.google.com/path1/path2/", | |
941 "attachment; filename==?iso88591?Q?caf=E3?", | |
942 "", | |
943 "", | |
944 "image/jpeg", | |
945 L"download", | |
946 L"download" JPEG_EXT | |
947 }, | |
948 }; | 780 }; |
949 | 781 |
950 // Tests filename generation. Once the correct filename is | 782 // Tests filename generation. Once the correct filename is |
951 // selected, they should be passed through the validation steps and | 783 // selected, they should be passed through the validation steps and |
952 // a correct extension should be added if necessary. | 784 // a correct extension should be added if necessary. |
953 const GenerateFilenameCase generation_tests[] = { | 785 const GenerateFilenameCase generation_tests[] = { |
954 // Dotfiles. Ensures preceeding period(s) stripped. | 786 // Dotfiles. Ensures preceeding period(s) stripped. |
955 {__LINE__, | 787 {__LINE__, |
956 "http://www.google.com/.test.html", | 788 "http://www.google.com/.test.html", |
957 "", | 789 "", |
(...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1060 L"download", | 892 L"download", |
1061 L"my-cat"}, | 893 L"my-cat"}, |
1062 {__LINE__, | 894 {__LINE__, |
1063 "http://www.example.com/my-cat.jpg", | 895 "http://www.example.com/my-cat.jpg", |
1064 "filename=my-cat.jpg", | 896 "filename=my-cat.jpg", |
1065 "", | 897 "", |
1066 "", | 898 "", |
1067 "text/plain", | 899 "text/plain", |
1068 L"download", | 900 L"download", |
1069 L"my-cat.jpg"}, | 901 L"my-cat.jpg"}, |
1070 // Windows specific tests | 902 // Windows specific tests |
1071 #if defined(OS_WIN) | 903 #if defined(OS_WIN) |
1072 {__LINE__, | 904 {__LINE__, |
1073 "http://www.goodguy.com/evil.exe", | 905 "http://www.goodguy.com/evil.exe", |
1074 "filename=evil.exe", | 906 "filename=evil.exe", |
1075 "", | 907 "", |
1076 "", | 908 "", |
1077 "image/jpeg", | 909 "image/jpeg", |
1078 L"download", | 910 L"download", |
1079 L"evil.exe"}, | 911 L"evil.exe"}, |
1080 {__LINE__, | 912 {__LINE__, |
(...skipping 489 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1570 RunGenerateFileNameTestCase(&generation_tests[i]); | 1402 RunGenerateFileNameTestCase(&generation_tests[i]); |
1571 | 1403 |
1572 for (size_t i = 0; i < arraysize(generation_tests); ++i) { | 1404 for (size_t i = 0; i < arraysize(generation_tests); ++i) { |
1573 GenerateFilenameCase test_case = generation_tests[i]; | 1405 GenerateFilenameCase test_case = generation_tests[i]; |
1574 test_case.referrer_charset = "GBK"; | 1406 test_case.referrer_charset = "GBK"; |
1575 RunGenerateFileNameTestCase(&test_case); | 1407 RunGenerateFileNameTestCase(&test_case); |
1576 } | 1408 } |
1577 } | 1409 } |
1578 | 1410 |
1579 } // namespace net | 1411 } // namespace net |
OLD | NEW |