| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 #include <set> | 6 #include <set> |
| 7 | 7 |
| 8 #include "base/file_util.h" | 8 #include "base/file_util.h" |
| 9 #include "base/memory/scoped_ptr.h" | 9 #include "base/memory/scoped_ptr.h" |
| 10 #include "base/stl_util-inl.h" | 10 #include "base/stl_util-inl.h" |
| (...skipping 154 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 165 const struct { | 165 const struct { |
| 166 FilePath::StringType suggested_path; | 166 FilePath::StringType suggested_path; |
| 167 bool is_dangerous_file; | 167 bool is_dangerous_file; |
| 168 bool is_dangerous_url; | 168 bool is_dangerous_url; |
| 169 bool finish_before_rename; | 169 bool finish_before_rename; |
| 170 int expected_rename_count; | 170 int expected_rename_count; |
| 171 } kDownloadRenameCases[] = { | 171 } kDownloadRenameCases[] = { |
| 172 // Safe download, download finishes BEFORE file name determined. | 172 // Safe download, download finishes BEFORE file name determined. |
| 173 // Renamed twice (linear path through UI). Crdownload file does not need | 173 // Renamed twice (linear path through UI). Crdownload file does not need |
| 174 // to be deleted. | 174 // to be deleted. |
| 175 { FILE_PATH_LITERAL("foo.zip"), | 175 { FILE_PATH_LITERAL("foo.zip"), false, false, true, 2, }, |
| 176 false, false, true, 2, }, | |
| 177 // Dangerous download (file is dangerous or download URL is not safe or both), | 176 // Dangerous download (file is dangerous or download URL is not safe or both), |
| 178 // download finishes BEFORE file name determined. Needs to be renamed only | 177 // download finishes BEFORE file name determined. Needs to be renamed only |
| 179 // once. | 178 // once. |
| 180 { FILE_PATH_LITERAL("Unconfirmed xxx.crdownload"), | 179 { FILE_PATH_LITERAL("Unconfirmed xxx.crdownload"), true, false, true, 1, }, |
| 181 true, false, true, 1, }, | 180 { FILE_PATH_LITERAL("Unconfirmed xxx.crdownload"), false, true, true, 1, }, |
| 182 { FILE_PATH_LITERAL("Unconfirmed xxx.crdownload"), | 181 { FILE_PATH_LITERAL("Unconfirmed xxx.crdownload"), true, true, true, 1, }, |
| 183 false, true, true, 1, }, | |
| 184 { FILE_PATH_LITERAL("Unconfirmed xxx.crdownload"), | |
| 185 true, true, true, 1, }, | |
| 186 // Safe download, download finishes AFTER file name determined. | 182 // Safe download, download finishes AFTER file name determined. |
| 187 // Needs to be renamed twice. | 183 // Needs to be renamed twice. |
| 188 { FILE_PATH_LITERAL("foo.zip"), | 184 { FILE_PATH_LITERAL("foo.zip"), false, false, false, 2, }, |
| 189 false, false, false, 2, }, | |
| 190 // Dangerous download, download finishes AFTER file name determined. | 185 // Dangerous download, download finishes AFTER file name determined. |
| 191 // Needs to be renamed only once. | 186 // Needs to be renamed only once. |
| 192 { FILE_PATH_LITERAL("Unconfirmed xxx.crdownload"), | 187 { FILE_PATH_LITERAL("Unconfirmed xxx.crdownload"), true, false, false, 1, }, |
| 193 true, false, false, 1, }, | 188 { FILE_PATH_LITERAL("Unconfirmed xxx.crdownload"), false, true, false, 1, }, |
| 194 { FILE_PATH_LITERAL("Unconfirmed xxx.crdownload"), | 189 { FILE_PATH_LITERAL("Unconfirmed xxx.crdownload"), true, true, false, 1, }, |
| 195 false, true, false, 1, }, | |
| 196 { FILE_PATH_LITERAL("Unconfirmed xxx.crdownload"), | |
| 197 true, true, false, 1, }, | |
| 198 }; | 190 }; |
| 199 | 191 |
| 200 class MockDownloadFile : public DownloadFile { | 192 class MockDownloadFile : public DownloadFile { |
| 201 public: | 193 public: |
| 202 MockDownloadFile(DownloadCreateInfo* info, DownloadManager* manager) | 194 MockDownloadFile(DownloadCreateInfo* info, DownloadManager* manager) |
| 203 : DownloadFile(info, manager), renamed_count_(0) { } | 195 : DownloadFile(info, manager), renamed_count_(0) { } |
| 204 virtual ~MockDownloadFile() { Destructed(); } | 196 virtual ~MockDownloadFile() { Destructed(); } |
| 205 MOCK_METHOD1(Rename, bool(const FilePath&)); | 197 MOCK_METHOD1(Rename, bool(const FilePath&)); |
| 206 MOCK_METHOD0(Destructed, void()); | 198 MOCK_METHOD0(Destructed, void()); |
| 207 | 199 |
| (...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 338 using ::testing::CreateFunctor; | 330 using ::testing::CreateFunctor; |
| 339 using ::testing::Invoke; | 331 using ::testing::Invoke; |
| 340 using ::testing::Return; | 332 using ::testing::Return; |
| 341 | 333 |
| 342 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kDownloadRenameCases); ++i) { | 334 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kDownloadRenameCases); ++i) { |
| 343 // |info| will be destroyed in download_manager_. | 335 // |info| will be destroyed in download_manager_. |
| 344 DownloadCreateInfo* info(new DownloadCreateInfo); | 336 DownloadCreateInfo* info(new DownloadCreateInfo); |
| 345 info->download_id = static_cast<int>(i); | 337 info->download_id = static_cast<int>(i); |
| 346 info->prompt_user_for_save_location = false; | 338 info->prompt_user_for_save_location = false; |
| 347 info->url_chain.push_back(GURL()); | 339 info->url_chain.push_back(GURL()); |
| 348 info->is_dangerous_file = kDownloadRenameCases[i].is_dangerous_file; | |
| 349 info->is_dangerous_url = kDownloadRenameCases[i].is_dangerous_url; | |
| 350 const FilePath new_path(kDownloadRenameCases[i].suggested_path); | 340 const FilePath new_path(kDownloadRenameCases[i].suggested_path); |
| 351 | 341 |
| 352 MockDownloadFile* download_file( | 342 MockDownloadFile* download_file( |
| 353 new MockDownloadFile(info, download_manager_)); | 343 new MockDownloadFile(info, download_manager_)); |
| 354 AddDownloadToFileManager(info->download_id, download_file); | 344 AddDownloadToFileManager(info->download_id, download_file); |
| 355 | 345 |
| 356 // |download_file| is owned by DownloadFileManager. | 346 // |download_file| is owned by DownloadFileManager. |
| 357 ::testing::Mock::AllowLeak(download_file); | 347 ::testing::Mock::AllowLeak(download_file); |
| 358 EXPECT_CALL(*download_file, Destructed()).Times(1); | 348 EXPECT_CALL(*download_file, Destructed()).Times(1); |
| 359 | 349 |
| 360 if (kDownloadRenameCases[i].expected_rename_count == 1) { | 350 if (kDownloadRenameCases[i].expected_rename_count == 1) { |
| 361 EXPECT_CALL(*download_file, Rename(new_path)).WillOnce(Return(true)); | 351 EXPECT_CALL(*download_file, Rename(new_path)).WillOnce(Return(true)); |
| 362 } else { | 352 } else { |
| 363 ASSERT_EQ(2, kDownloadRenameCases[i].expected_rename_count); | 353 ASSERT_EQ(2, kDownloadRenameCases[i].expected_rename_count); |
| 364 FilePath crdownload(download_util::GetCrDownloadPath(new_path)); | 354 FilePath crdownload(download_util::GetCrDownloadPath(new_path)); |
| 365 EXPECT_CALL(*download_file, Rename(_)) | 355 EXPECT_CALL(*download_file, Rename(_)) |
| 366 .WillOnce(testing::WithArgs<0>(Invoke(CreateFunctor( | 356 .WillOnce(testing::WithArgs<0>(Invoke(CreateFunctor( |
| 367 download_file, &MockDownloadFile::TestMultipleRename, | 357 download_file, &MockDownloadFile::TestMultipleRename, |
| 368 1, crdownload)))) | 358 1, crdownload)))) |
| 369 .WillOnce(testing::WithArgs<0>(Invoke(CreateFunctor( | 359 .WillOnce(testing::WithArgs<0>(Invoke(CreateFunctor( |
| 370 download_file, &MockDownloadFile::TestMultipleRename, | 360 download_file, &MockDownloadFile::TestMultipleRename, |
| 371 2, new_path)))); | 361 2, new_path)))); |
| 372 } | 362 } |
| 373 download_manager_->CreateDownloadItem(info); | 363 download_manager_->CreateDownloadItem(info); |
| 364 DownloadItem* download = download_manager_->GetDownloadItem(i); |
| 365 ASSERT_TRUE(download != NULL); |
| 366 download->SetFileDangerous(kDownloadRenameCases[i].is_dangerous_file); |
| 367 download->SetUrlDangerous(kDownloadRenameCases[i].is_dangerous_url); |
| 374 | 368 |
| 375 int32* id_ptr = new int32; | 369 int32* id_ptr = new int32; |
| 376 *id_ptr = i; // Deleted in FileSelected(). | 370 *id_ptr = i; // Deleted in FileSelected(). |
| 377 if (kDownloadRenameCases[i].finish_before_rename) { | 371 if (kDownloadRenameCases[i].finish_before_rename) { |
| 378 OnAllDataSaved(i, 1024, std::string("fake_hash")); | 372 OnAllDataSaved(i, 1024, std::string("fake_hash")); |
| 379 message_loop_.RunAllPending(); | 373 message_loop_.RunAllPending(); |
| 380 FileSelected(new_path, i, id_ptr); | 374 FileSelected(new_path, i, id_ptr); |
| 381 } else { | 375 } else { |
| 382 FileSelected(new_path, i, id_ptr); | 376 FileSelected(new_path, i, id_ptr); |
| 383 message_loop_.RunAllPending(); | 377 message_loop_.RunAllPending(); |
| (...skipping 11 matching lines...) Expand all Loading... |
| 395 using ::testing::_; | 389 using ::testing::_; |
| 396 using ::testing::CreateFunctor; | 390 using ::testing::CreateFunctor; |
| 397 using ::testing::Invoke; | 391 using ::testing::Invoke; |
| 398 using ::testing::Return; | 392 using ::testing::Return; |
| 399 | 393 |
| 400 // |info| will be destroyed in download_manager_. | 394 // |info| will be destroyed in download_manager_. |
| 401 DownloadCreateInfo* info(new DownloadCreateInfo); | 395 DownloadCreateInfo* info(new DownloadCreateInfo); |
| 402 info->download_id = static_cast<int>(0); | 396 info->download_id = static_cast<int>(0); |
| 403 info->prompt_user_for_save_location = false; | 397 info->prompt_user_for_save_location = false; |
| 404 info->url_chain.push_back(GURL()); | 398 info->url_chain.push_back(GURL()); |
| 405 info->is_dangerous_file = false; | |
| 406 info->is_dangerous_url = false; | |
| 407 const FilePath new_path(FILE_PATH_LITERAL("foo.zip")); | 399 const FilePath new_path(FILE_PATH_LITERAL("foo.zip")); |
| 408 const FilePath cr_path(download_util::GetCrDownloadPath(new_path)); | 400 const FilePath cr_path(download_util::GetCrDownloadPath(new_path)); |
| 409 | 401 |
| 410 MockDownloadFile* download_file( | 402 MockDownloadFile* download_file( |
| 411 new MockDownloadFile(info, download_manager_)); | 403 new MockDownloadFile(info, download_manager_)); |
| 412 AddDownloadToFileManager(info->download_id, download_file); | 404 AddDownloadToFileManager(info->download_id, download_file); |
| 413 | 405 |
| 414 // |download_file| is owned by DownloadFileManager. | 406 // |download_file| is owned by DownloadFileManager. |
| 415 ::testing::Mock::AllowLeak(download_file); | 407 ::testing::Mock::AllowLeak(download_file); |
| 416 EXPECT_CALL(*download_file, Destructed()).Times(1); | 408 EXPECT_CALL(*download_file, Destructed()).Times(1); |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 460 using ::testing::_; | 452 using ::testing::_; |
| 461 using ::testing::CreateFunctor; | 453 using ::testing::CreateFunctor; |
| 462 using ::testing::Invoke; | 454 using ::testing::Invoke; |
| 463 using ::testing::Return; | 455 using ::testing::Return; |
| 464 | 456 |
| 465 // |info| will be destroyed in download_manager_. | 457 // |info| will be destroyed in download_manager_. |
| 466 DownloadCreateInfo* info(new DownloadCreateInfo); | 458 DownloadCreateInfo* info(new DownloadCreateInfo); |
| 467 info->download_id = static_cast<int>(0); | 459 info->download_id = static_cast<int>(0); |
| 468 info->prompt_user_for_save_location = false; | 460 info->prompt_user_for_save_location = false; |
| 469 info->url_chain.push_back(GURL()); | 461 info->url_chain.push_back(GURL()); |
| 470 info->is_dangerous_file = false; | |
| 471 info->is_dangerous_url = false; | |
| 472 const FilePath new_path(FILE_PATH_LITERAL("foo.zip")); | 462 const FilePath new_path(FILE_PATH_LITERAL("foo.zip")); |
| 473 const FilePath cr_path(download_util::GetCrDownloadPath(new_path)); | 463 const FilePath cr_path(download_util::GetCrDownloadPath(new_path)); |
| 474 | 464 |
| 475 MockDownloadFile* download_file( | 465 MockDownloadFile* download_file( |
| 476 new MockDownloadFile(info, download_manager_)); | 466 new MockDownloadFile(info, download_manager_)); |
| 477 AddDownloadToFileManager(info->download_id, download_file); | 467 AddDownloadToFileManager(info->download_id, download_file); |
| 478 | 468 |
| 479 // |download_file| is owned by DownloadFileManager. | 469 // |download_file| is owned by DownloadFileManager. |
| 480 ::testing::Mock::AllowLeak(download_file); | 470 ::testing::Mock::AllowLeak(download_file); |
| 481 EXPECT_CALL(*download_file, Destructed()).Times(1); | 471 EXPECT_CALL(*download_file, Destructed()).Times(1); |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 538 int uniquifier = download_util::GetUniquePathNumber(new_path); | 528 int uniquifier = download_util::GetUniquePathNumber(new_path); |
| 539 FilePath unique_new_path = new_path; | 529 FilePath unique_new_path = new_path; |
| 540 EXPECT_NE(0, uniquifier); | 530 EXPECT_NE(0, uniquifier); |
| 541 download_util::AppendNumberToPath(&unique_new_path, uniquifier); | 531 download_util::AppendNumberToPath(&unique_new_path, uniquifier); |
| 542 | 532 |
| 543 // |info| will be destroyed in download_manager_. | 533 // |info| will be destroyed in download_manager_. |
| 544 DownloadCreateInfo* info(new DownloadCreateInfo); | 534 DownloadCreateInfo* info(new DownloadCreateInfo); |
| 545 info->download_id = static_cast<int>(0); | 535 info->download_id = static_cast<int>(0); |
| 546 info->prompt_user_for_save_location = true; | 536 info->prompt_user_for_save_location = true; |
| 547 info->url_chain.push_back(GURL()); | 537 info->url_chain.push_back(GURL()); |
| 548 info->is_dangerous_file = false; | |
| 549 info->is_dangerous_url = false; | |
| 550 | 538 |
| 551 download_manager_->CreateDownloadItem(info); | 539 download_manager_->CreateDownloadItem(info); |
| 552 | 540 |
| 553 DownloadItem* download = GetActiveDownloadItem(0); | 541 DownloadItem* download = GetActiveDownloadItem(0); |
| 554 ASSERT_TRUE(download != NULL); | 542 ASSERT_TRUE(download != NULL); |
| 555 | 543 |
| 556 EXPECT_EQ(DownloadItem::IN_PROGRESS, download->state()); | 544 EXPECT_EQ(DownloadItem::IN_PROGRESS, download->state()); |
| 557 scoped_ptr<ItemObserver> observer(new ItemObserver(download)); | 545 scoped_ptr<ItemObserver> observer(new ItemObserver(download)); |
| 558 | 546 |
| 559 // Create and initialize the download file. We're bypassing the first part | 547 // Create and initialize the download file. We're bypassing the first part |
| (...skipping 29 matching lines...) Expand all Loading... |
| 589 EXPECT_FALSE(observer->was_opened()); | 577 EXPECT_FALSE(observer->was_opened()); |
| 590 EXPECT_EQ(DownloadItem::COMPLETE, download->state()); | 578 EXPECT_EQ(DownloadItem::COMPLETE, download->state()); |
| 591 | 579 |
| 592 EXPECT_TRUE(file_util::PathExists(new_path)); | 580 EXPECT_TRUE(file_util::PathExists(new_path)); |
| 593 EXPECT_FALSE(file_util::PathExists(cr_path)); | 581 EXPECT_FALSE(file_util::PathExists(cr_path)); |
| 594 EXPECT_FALSE(file_util::PathExists(unique_new_path)); | 582 EXPECT_FALSE(file_util::PathExists(unique_new_path)); |
| 595 std::string file_contents; | 583 std::string file_contents; |
| 596 EXPECT_TRUE(file_util::ReadFileToString(new_path, &file_contents)); | 584 EXPECT_TRUE(file_util::ReadFileToString(new_path, &file_contents)); |
| 597 EXPECT_EQ(std::string(kTestData), file_contents); | 585 EXPECT_EQ(std::string(kTestData), file_contents); |
| 598 } | 586 } |
| OLD | NEW |