OLD | NEW |
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 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 <string> | 5 #include <string> |
6 | 6 |
7 #include "chrome/browser/download/download_manager.h" | 7 #include "chrome/browser/download/download_manager.h" |
8 #include "chrome/browser/download/download_util.h" | 8 #include "chrome/browser/download/download_util.h" |
9 #include "testing/gtest/include/gtest/gtest.h" | 9 #include "testing/gtest/include/gtest/gtest.h" |
10 | 10 |
11 class DownloadManagerTest : public testing::Test { | 11 class DownloadManagerTest : public testing::Test { |
12 public: | 12 public: |
13 DownloadManagerTest() { | 13 DownloadManagerTest() { |
14 download_manager_ = new DownloadManager(); | 14 download_manager_ = new DownloadManager(); |
15 download_util::InitializeExeTypes(&download_manager_->exe_types_); | 15 download_util::InitializeExeTypes(&download_manager_->exe_types_); |
16 } | 16 } |
17 | 17 |
18 void GetGeneratedFilename(const std::string& content_disposition, | 18 void GetGeneratedFilename(const std::string& content_disposition, |
19 const std::string& url, | 19 const std::string& url, |
20 const std::string& mime_type, | 20 const std::string& mime_type, |
| 21 const std::string& referrer_charset, |
21 std::wstring* generated_name_string) { | 22 std::wstring* generated_name_string) { |
22 DownloadCreateInfo info; | 23 DownloadCreateInfo info; |
23 info.content_disposition = content_disposition; | 24 info.content_disposition = content_disposition; |
24 info.url = GURL(url); | 25 info.url = GURL(url); |
25 info.mime_type = mime_type; | 26 info.mime_type = mime_type; |
| 27 info.referrer_charset = referrer_charset; |
26 FilePath generated_name; | 28 FilePath generated_name; |
27 download_manager_->GenerateFilename(&info, &generated_name); | 29 download_manager_->GenerateFilename(&info, &generated_name); |
28 *generated_name_string = generated_name.ToWStringHack(); | 30 *generated_name_string = generated_name.ToWStringHack(); |
29 } | 31 } |
30 | 32 |
31 protected: | 33 protected: |
32 scoped_refptr<DownloadManager> download_manager_; | 34 scoped_refptr<DownloadManager> download_manager_; |
33 MessageLoopForUI message_loop_; | 35 MessageLoopForUI message_loop_; |
34 | 36 |
35 DISALLOW_EVIL_CONSTRUCTORS(DownloadManagerTest); | 37 DISALLOW_EVIL_CONSTRUCTORS(DownloadManagerTest); |
(...skipping 292 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
328 {"", | 330 {"", |
329 "http://www.example.com/bar.tar", | 331 "http://www.example.com/bar.tar", |
330 "application/x-tar", | 332 "application/x-tar", |
331 L"bar.tar"}, | 333 L"bar.tar"}, |
332 | 334 |
333 {"", | 335 {"", |
334 "http://www.example.com/bar.bogus", | 336 "http://www.example.com/bar.bogus", |
335 "application/x-tar", | 337 "application/x-tar", |
336 L"bar.bogus.tar"}, | 338 L"bar.bogus.tar"}, |
337 | 339 |
338 // TODO(darin): Add some raw 8-bit Content-Disposition tests. | |
339 }; | 340 }; |
340 | 341 |
341 } // namespace | 342 } // namespace |
342 | 343 |
343 // Tests to ensure that the file names we generate from hints from the server | 344 // Tests to ensure that the file names we generate from hints from the server |
344 // (content-disposition, URL name, etc) don't cause security holes. | 345 // (content-disposition, URL name, etc) don't cause security holes. |
345 TEST_F(DownloadManagerTest, TestDownloadFilename) { | 346 TEST_F(DownloadManagerTest, TestDownloadFilename) { |
| 347 std::wstring file_name; |
346 for (int i = 0; i < arraysize(kGeneratedFiles); ++i) { | 348 for (int i = 0; i < arraysize(kGeneratedFiles); ++i) { |
347 std::wstring file_name; | |
348 GetGeneratedFilename(kGeneratedFiles[i].disposition, | 349 GetGeneratedFilename(kGeneratedFiles[i].disposition, |
349 kGeneratedFiles[i].url, | 350 kGeneratedFiles[i].url, |
350 kGeneratedFiles[i].mime_type, | 351 kGeneratedFiles[i].mime_type, |
| 352 "", |
| 353 &file_name); |
| 354 EXPECT_EQ(kGeneratedFiles[i].expected_name, file_name); |
| 355 GetGeneratedFilename(kGeneratedFiles[i].disposition, |
| 356 kGeneratedFiles[i].url, |
| 357 kGeneratedFiles[i].mime_type, |
| 358 "GBK", |
351 &file_name); | 359 &file_name); |
352 EXPECT_EQ(kGeneratedFiles[i].expected_name, file_name); | 360 EXPECT_EQ(kGeneratedFiles[i].expected_name, file_name); |
353 } | 361 } |
| 362 |
| 363 // A couple of cases with raw 8bit characters in C-D. |
| 364 GetGeneratedFilename("attachment; filename=caf\xc3\xa9.png", |
| 365 "http://www.example.com/images?id=3", |
| 366 "image/png", |
| 367 "iso-8859-1", |
| 368 &file_name); |
| 369 EXPECT_EQ(L"caf\u00e9.png", file_name); |
| 370 GetGeneratedFilename("attachment; filename=caf\xe5.png", |
| 371 "http://www.example.com/images?id=3", |
| 372 "image/png", |
| 373 "windows-1253", |
| 374 &file_name); |
| 375 EXPECT_EQ(L"caf\u03b5.png", file_name); |
354 } | 376 } |
355 | 377 |
356 namespace { | 378 namespace { |
357 | 379 |
358 const struct { | 380 const struct { |
359 const FilePath::CharType* path; | 381 const FilePath::CharType* path; |
360 const char* mime_type; | 382 const char* mime_type; |
361 const FilePath::CharType* expected_path; | 383 const FilePath::CharType* expected_path; |
362 } kSafeFilenameCases[] = { | 384 } kSafeFilenameCases[] = { |
363 { FILE_PATH_LITERAL("C:\\foo\\bar.htm"), | 385 { FILE_PATH_LITERAL("C:\\foo\\bar.htm"), |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
399 } // namespace | 421 } // namespace |
400 | 422 |
401 TEST_F(DownloadManagerTest, GetSafeFilename) { | 423 TEST_F(DownloadManagerTest, GetSafeFilename) { |
402 for (int i = 0; i < arraysize(kSafeFilenameCases); ++i) { | 424 for (int i = 0; i < arraysize(kSafeFilenameCases); ++i) { |
403 FilePath path(kSafeFilenameCases[i].path); | 425 FilePath path(kSafeFilenameCases[i].path); |
404 download_manager_->GenerateSafeFilename(kSafeFilenameCases[i].mime_type, | 426 download_manager_->GenerateSafeFilename(kSafeFilenameCases[i].mime_type, |
405 &path); | 427 &path); |
406 EXPECT_EQ(kSafeFilenameCases[i].expected_path, path.value()); | 428 EXPECT_EQ(kSafeFilenameCases[i].expected_path, path.value()); |
407 } | 429 } |
408 } | 430 } |
OLD | NEW |