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 = GetActiveDownloadItem(i); |
| 365 ASSERT_TRUE(download != NULL); |
| 366 if (kDownloadRenameCases[i].is_dangerous_file) |
| 367 download->MarkFileDangerous(); |
| 368 if (kDownloadRenameCases[i].is_dangerous_url) |
| 369 download->MarkUrlDangerous(); |
374 | 370 |
375 int32* id_ptr = new int32; | 371 int32* id_ptr = new int32; |
376 *id_ptr = i; // Deleted in FileSelected(). | 372 *id_ptr = i; // Deleted in FileSelected(). |
377 if (kDownloadRenameCases[i].finish_before_rename) { | 373 if (kDownloadRenameCases[i].finish_before_rename) { |
378 OnAllDataSaved(i, 1024, std::string("fake_hash")); | 374 OnAllDataSaved(i, 1024, std::string("fake_hash")); |
379 message_loop_.RunAllPending(); | 375 message_loop_.RunAllPending(); |
380 FileSelected(new_path, i, id_ptr); | 376 FileSelected(new_path, i, id_ptr); |
381 } else { | 377 } else { |
382 FileSelected(new_path, i, id_ptr); | 378 FileSelected(new_path, i, id_ptr); |
383 message_loop_.RunAllPending(); | 379 message_loop_.RunAllPending(); |
(...skipping 11 matching lines...) Expand all Loading... |
395 using ::testing::_; | 391 using ::testing::_; |
396 using ::testing::CreateFunctor; | 392 using ::testing::CreateFunctor; |
397 using ::testing::Invoke; | 393 using ::testing::Invoke; |
398 using ::testing::Return; | 394 using ::testing::Return; |
399 | 395 |
400 // |info| will be destroyed in download_manager_. | 396 // |info| will be destroyed in download_manager_. |
401 DownloadCreateInfo* info(new DownloadCreateInfo); | 397 DownloadCreateInfo* info(new DownloadCreateInfo); |
402 info->download_id = static_cast<int>(0); | 398 info->download_id = static_cast<int>(0); |
403 info->prompt_user_for_save_location = false; | 399 info->prompt_user_for_save_location = false; |
404 info->url_chain.push_back(GURL()); | 400 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")); | 401 const FilePath new_path(FILE_PATH_LITERAL("foo.zip")); |
408 const FilePath cr_path(download_util::GetCrDownloadPath(new_path)); | 402 const FilePath cr_path(download_util::GetCrDownloadPath(new_path)); |
409 | 403 |
410 MockDownloadFile* download_file( | 404 MockDownloadFile* download_file( |
411 new MockDownloadFile(info, download_manager_)); | 405 new MockDownloadFile(info, download_manager_)); |
412 AddDownloadToFileManager(info->download_id, download_file); | 406 AddDownloadToFileManager(info->download_id, download_file); |
413 | 407 |
414 // |download_file| is owned by DownloadFileManager. | 408 // |download_file| is owned by DownloadFileManager. |
415 ::testing::Mock::AllowLeak(download_file); | 409 ::testing::Mock::AllowLeak(download_file); |
416 EXPECT_CALL(*download_file, Destructed()).Times(1); | 410 EXPECT_CALL(*download_file, Destructed()).Times(1); |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
460 using ::testing::_; | 454 using ::testing::_; |
461 using ::testing::CreateFunctor; | 455 using ::testing::CreateFunctor; |
462 using ::testing::Invoke; | 456 using ::testing::Invoke; |
463 using ::testing::Return; | 457 using ::testing::Return; |
464 | 458 |
465 // |info| will be destroyed in download_manager_. | 459 // |info| will be destroyed in download_manager_. |
466 DownloadCreateInfo* info(new DownloadCreateInfo); | 460 DownloadCreateInfo* info(new DownloadCreateInfo); |
467 info->download_id = static_cast<int>(0); | 461 info->download_id = static_cast<int>(0); |
468 info->prompt_user_for_save_location = false; | 462 info->prompt_user_for_save_location = false; |
469 info->url_chain.push_back(GURL()); | 463 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")); | 464 const FilePath new_path(FILE_PATH_LITERAL("foo.zip")); |
473 const FilePath cr_path(download_util::GetCrDownloadPath(new_path)); | 465 const FilePath cr_path(download_util::GetCrDownloadPath(new_path)); |
474 | 466 |
475 MockDownloadFile* download_file( | 467 MockDownloadFile* download_file( |
476 new MockDownloadFile(info, download_manager_)); | 468 new MockDownloadFile(info, download_manager_)); |
477 AddDownloadToFileManager(info->download_id, download_file); | 469 AddDownloadToFileManager(info->download_id, download_file); |
478 | 470 |
479 // |download_file| is owned by DownloadFileManager. | 471 // |download_file| is owned by DownloadFileManager. |
480 ::testing::Mock::AllowLeak(download_file); | 472 ::testing::Mock::AllowLeak(download_file); |
481 EXPECT_CALL(*download_file, Destructed()).Times(1); | 473 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); | 530 int uniquifier = download_util::GetUniquePathNumber(new_path); |
539 FilePath unique_new_path = new_path; | 531 FilePath unique_new_path = new_path; |
540 EXPECT_NE(0, uniquifier); | 532 EXPECT_NE(0, uniquifier); |
541 download_util::AppendNumberToPath(&unique_new_path, uniquifier); | 533 download_util::AppendNumberToPath(&unique_new_path, uniquifier); |
542 | 534 |
543 // |info| will be destroyed in download_manager_. | 535 // |info| will be destroyed in download_manager_. |
544 DownloadCreateInfo* info(new DownloadCreateInfo); | 536 DownloadCreateInfo* info(new DownloadCreateInfo); |
545 info->download_id = static_cast<int>(0); | 537 info->download_id = static_cast<int>(0); |
546 info->prompt_user_for_save_location = true; | 538 info->prompt_user_for_save_location = true; |
547 info->url_chain.push_back(GURL()); | 539 info->url_chain.push_back(GURL()); |
548 info->is_dangerous_file = false; | |
549 info->is_dangerous_url = false; | |
550 | 540 |
551 download_manager_->CreateDownloadItem(info); | 541 download_manager_->CreateDownloadItem(info); |
552 | 542 |
553 DownloadItem* download = GetActiveDownloadItem(0); | 543 DownloadItem* download = GetActiveDownloadItem(0); |
554 ASSERT_TRUE(download != NULL); | 544 ASSERT_TRUE(download != NULL); |
555 | 545 |
556 EXPECT_EQ(DownloadItem::IN_PROGRESS, download->state()); | 546 EXPECT_EQ(DownloadItem::IN_PROGRESS, download->state()); |
557 scoped_ptr<ItemObserver> observer(new ItemObserver(download)); | 547 scoped_ptr<ItemObserver> observer(new ItemObserver(download)); |
558 | 548 |
559 // Create and initialize the download file. We're bypassing the first part | 549 // 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()); | 579 EXPECT_FALSE(observer->was_opened()); |
590 EXPECT_EQ(DownloadItem::COMPLETE, download->state()); | 580 EXPECT_EQ(DownloadItem::COMPLETE, download->state()); |
591 | 581 |
592 EXPECT_TRUE(file_util::PathExists(new_path)); | 582 EXPECT_TRUE(file_util::PathExists(new_path)); |
593 EXPECT_FALSE(file_util::PathExists(cr_path)); | 583 EXPECT_FALSE(file_util::PathExists(cr_path)); |
594 EXPECT_FALSE(file_util::PathExists(unique_new_path)); | 584 EXPECT_FALSE(file_util::PathExists(unique_new_path)); |
595 std::string file_contents; | 585 std::string file_contents; |
596 EXPECT_TRUE(file_util::ReadFileToString(new_path, &file_contents)); | 586 EXPECT_TRUE(file_util::ReadFileToString(new_path, &file_contents)); |
597 EXPECT_EQ(std::string(kTestData), file_contents); | 587 EXPECT_EQ(std::string(kTestData), file_contents); |
598 } | 588 } |
OLD | NEW |