| 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 <set> | 5 #include <set> |
| 6 #include <string> | 6 #include <string> |
| 7 | 7 |
| 8 #include "base/bind.h" | 8 #include "base/bind.h" |
| 9 #include "base/file_util.h" | 9 #include "base/file_util.h" |
| 10 #include "base/i18n/number_formatting.h" | 10 #include "base/i18n/number_formatting.h" |
| (...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 138 scoped_refptr<content::DownloadBuffer> download_buffer_; | 138 scoped_refptr<content::DownloadBuffer> download_buffer_; |
| 139 | 139 |
| 140 DownloadFileManager* file_manager() { | 140 DownloadFileManager* file_manager() { |
| 141 if (!file_manager_) { | 141 if (!file_manager_) { |
| 142 file_manager_ = new DownloadFileManager(NULL); | 142 file_manager_ = new DownloadFileManager(NULL); |
| 143 download_manager_->file_manager_ = file_manager_; | 143 download_manager_->file_manager_ = file_manager_; |
| 144 } | 144 } |
| 145 return file_manager_; | 145 return file_manager_; |
| 146 } | 146 } |
| 147 | 147 |
| 148 // Make sure download item |id| was set with correct safety state for | |
| 149 // given |is_dangerous_file| and |is_dangerous_url|. | |
| 150 bool VerifySafetyState(bool is_dangerous_file, | |
| 151 bool is_dangerous_url, | |
| 152 int id) { | |
| 153 DownloadItem::SafetyState safety_state = | |
| 154 download_manager_->GetDownloadItem(id)->safety_state(); | |
| 155 return (is_dangerous_file || is_dangerous_url) ? | |
| 156 safety_state != DownloadItem::SAFE : safety_state == DownloadItem::SAFE; | |
| 157 } | |
| 158 | |
| 159 DISALLOW_COPY_AND_ASSIGN(DownloadManagerTest); | 148 DISALLOW_COPY_AND_ASSIGN(DownloadManagerTest); |
| 160 }; | 149 }; |
| 161 | 150 |
| 162 const char* DownloadManagerTest::kTestData = "a;sdlfalsdfjalsdkfjad"; | 151 const char* DownloadManagerTest::kTestData = "a;sdlfalsdfjalsdkfjad"; |
| 163 const size_t DownloadManagerTest::kTestDataLen = | 152 const size_t DownloadManagerTest::kTestDataLen = |
| 164 strlen(DownloadManagerTest::kTestData); | 153 strlen(DownloadManagerTest::kTestData); |
| 165 | 154 |
| 166 // A DownloadFile that we can inject errors into. Uses MockFileStream. | 155 // A DownloadFile that we can inject errors into. Uses MockFileStream. |
| 167 // Note: This can't be in an anonymous namespace because it must be declared | 156 // Note: This can't be in an anonymous namespace because it must be declared |
| 168 // as a friend of |DownloadFile| in order to access its private members. | 157 // as a friend of |DownloadFile| in order to access its private members. |
| (...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 248 false, }, | 237 false, }, |
| 249 { "http://www.foo.com/always_prompt.jar", | 238 { "http://www.foo.com/always_prompt.jar", |
| 250 "application/jar", | 239 "application/jar", |
| 251 false, | 240 false, |
| 252 true, | 241 true, |
| 253 true, }, | 242 true, }, |
| 254 }; | 243 }; |
| 255 | 244 |
| 256 const struct { | 245 const struct { |
| 257 FilePath::StringType suggested_path; | 246 FilePath::StringType suggested_path; |
| 258 bool is_dangerous_file; | 247 DownloadStateInfo::DangerType danger; |
| 259 bool is_dangerous_url; | |
| 260 bool finish_before_rename; | 248 bool finish_before_rename; |
| 261 int expected_rename_count; | 249 int expected_rename_count; |
| 262 } kDownloadRenameCases[] = { | 250 } kDownloadRenameCases[] = { |
| 263 // Safe download, download finishes BEFORE file name determined. | 251 // Safe download, download finishes BEFORE file name determined. |
| 264 // Renamed twice (linear path through UI). Crdownload file does not need | 252 // Renamed twice (linear path through UI). Crdownload file does not need |
| 265 // to be deleted. | 253 // to be deleted. |
| 266 { FILE_PATH_LITERAL("foo.zip"), false, false, true, 2, }, | 254 { FILE_PATH_LITERAL("foo.zip"), DownloadStateInfo::NOT_DANGEROUS, true, 2, }, |
| 267 // Dangerous download (file is dangerous or download URL is not safe or both), | 255 // Potentially dangerous download (e.g., file is dangerous), download finishes |
| 268 // download finishes BEFORE file name determined. Needs to be renamed only | 256 // BEFORE file name determined. Needs to be renamed only once. |
| 269 // once. | 257 { FILE_PATH_LITERAL("Unconfirmed xxx.crdownload"), |
| 270 { FILE_PATH_LITERAL("Unconfirmed xxx.crdownload"), true, false, true, 1, }, | 258 DownloadStateInfo::MAYBE_DANGEROUS_CONTENT, true, 1, }, |
| 271 { FILE_PATH_LITERAL("Unconfirmed xxx.crdownload"), false, true, true, 1, }, | 259 { FILE_PATH_LITERAL("Unconfirmed xxx.crdownload"), |
| 272 { FILE_PATH_LITERAL("Unconfirmed xxx.crdownload"), true, true, true, 1, }, | 260 DownloadStateInfo::DANGEROUS_FILE, true, 1, }, |
| 273 // Safe download, download finishes AFTER file name determined. | 261 // Safe download, download finishes AFTER file name determined. |
| 274 // Needs to be renamed twice. | 262 // Needs to be renamed twice. |
| 275 { FILE_PATH_LITERAL("foo.zip"), false, false, false, 2, }, | 263 { FILE_PATH_LITERAL("foo.zip"), DownloadStateInfo::NOT_DANGEROUS, false, 2, }, |
| 276 // Dangerous download, download finishes AFTER file name determined. | 264 // Potentially dangerous download, download finishes AFTER file name |
| 277 // Needs to be renamed only once. | 265 // determined. Needs to be renamed only once. |
| 278 { FILE_PATH_LITERAL("Unconfirmed xxx.crdownload"), true, false, false, 1, }, | 266 { FILE_PATH_LITERAL("Unconfirmed xxx.crdownload"), |
| 279 { FILE_PATH_LITERAL("Unconfirmed xxx.crdownload"), false, true, false, 1, }, | 267 DownloadStateInfo::MAYBE_DANGEROUS_CONTENT, false, 1, }, |
| 280 { FILE_PATH_LITERAL("Unconfirmed xxx.crdownload"), true, true, false, 1, }, | 268 { FILE_PATH_LITERAL("Unconfirmed xxx.crdownload"), |
| 269 DownloadStateInfo::DANGEROUS_FILE, false, 1, }, |
| 281 }; | 270 }; |
| 282 | 271 |
| 283 class MockDownloadFile : public DownloadFile { | 272 class MockDownloadFile : public DownloadFile { |
| 284 public: | 273 public: |
| 285 MockDownloadFile(DownloadCreateInfo* info, DownloadManager* manager) | 274 MockDownloadFile(DownloadCreateInfo* info, DownloadManager* manager) |
| 286 : DownloadFile(info, new DownloadRequestHandle(), manager), | 275 : DownloadFile(info, new DownloadRequestHandle(), manager), |
| 287 renamed_count_(0) { } | 276 renamed_count_(0) { } |
| 288 virtual ~MockDownloadFile() { Destructed(); } | 277 virtual ~MockDownloadFile() { Destructed(); } |
| 289 MOCK_METHOD1(Rename, net::Error(const FilePath&)); | 278 MOCK_METHOD1(Rename, net::Error(const FilePath&)); |
| 290 MOCK_METHOD0(Destructed, void()); | 279 MOCK_METHOD0(Destructed, void()); |
| (...skipping 156 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 447 .WillOnce(testing::WithArgs<0>(Invoke(CreateFunctor( | 436 .WillOnce(testing::WithArgs<0>(Invoke(CreateFunctor( |
| 448 download_file, &MockDownloadFile::TestMultipleRename, | 437 download_file, &MockDownloadFile::TestMultipleRename, |
| 449 1, crdownload)))) | 438 1, crdownload)))) |
| 450 .WillOnce(testing::WithArgs<0>(Invoke(CreateFunctor( | 439 .WillOnce(testing::WithArgs<0>(Invoke(CreateFunctor( |
| 451 download_file, &MockDownloadFile::TestMultipleRename, | 440 download_file, &MockDownloadFile::TestMultipleRename, |
| 452 2, new_path)))); | 441 2, new_path)))); |
| 453 } | 442 } |
| 454 download_manager_->CreateDownloadItem(info.get(), DownloadRequestHandle()); | 443 download_manager_->CreateDownloadItem(info.get(), DownloadRequestHandle()); |
| 455 DownloadItem* download = GetActiveDownloadItem(i); | 444 DownloadItem* download = GetActiveDownloadItem(i); |
| 456 ASSERT_TRUE(download != NULL); | 445 ASSERT_TRUE(download != NULL); |
| 457 if (kDownloadRenameCases[i].is_dangerous_file) | 446 DownloadStateInfo state = download->state_info(); |
| 458 download->MarkFileDangerous(); | 447 state.danger = kDownloadRenameCases[i].danger; |
| 459 if (kDownloadRenameCases[i].is_dangerous_url) | 448 download->SetFileCheckResults(state); |
| 460 download->MarkUrlDangerous(); | |
| 461 | 449 |
| 462 int32* id_ptr = new int32; | 450 int32* id_ptr = new int32; |
| 463 *id_ptr = i; // Deleted in FileSelected(). | 451 *id_ptr = i; // Deleted in FileSelected(). |
| 464 if (kDownloadRenameCases[i].finish_before_rename) { | 452 if (kDownloadRenameCases[i].finish_before_rename) { |
| 465 OnResponseCompleted(i, 1024, std::string("fake_hash")); | 453 OnResponseCompleted(i, 1024, std::string("fake_hash")); |
| 466 message_loop_.RunAllPending(); | 454 message_loop_.RunAllPending(); |
| 467 FileSelected(new_path, id_ptr); | 455 FileSelected(new_path, id_ptr); |
| 468 } else { | 456 } else { |
| 469 FileSelected(new_path, id_ptr); | 457 FileSelected(new_path, id_ptr); |
| 470 message_loop_.RunAllPending(); | 458 message_loop_.RunAllPending(); |
| 471 OnResponseCompleted(i, 1024, std::string("fake_hash")); | 459 OnResponseCompleted(i, 1024, std::string("fake_hash")); |
| 472 } | 460 } |
| 473 | |
| 474 message_loop_.RunAllPending(); | 461 message_loop_.RunAllPending(); |
| 475 EXPECT_TRUE(VerifySafetyState(kDownloadRenameCases[i].is_dangerous_file, | |
| 476 kDownloadRenameCases[i].is_dangerous_url, | |
| 477 i)); | |
| 478 } | 462 } |
| 479 } | 463 } |
| 480 | 464 |
| 481 TEST_F(DownloadManagerTest, DownloadInterruptTest) { | 465 TEST_F(DownloadManagerTest, DownloadInterruptTest) { |
| 482 using ::testing::_; | 466 using ::testing::_; |
| 483 using ::testing::CreateFunctor; | 467 using ::testing::CreateFunctor; |
| 484 using ::testing::Invoke; | 468 using ::testing::Invoke; |
| 485 using ::testing::Return; | 469 using ::testing::Return; |
| 486 | 470 |
| 487 // Normally, the download system takes ownership of info, and is | 471 // Normally, the download system takes ownership of info, and is |
| (...skipping 396 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 884 EXPECT_FALSE(observer->hit_state(DownloadItem::REMOVING)); | 868 EXPECT_FALSE(observer->hit_state(DownloadItem::REMOVING)); |
| 885 EXPECT_TRUE(observer->was_updated()); | 869 EXPECT_TRUE(observer->was_updated()); |
| 886 EXPECT_FALSE(observer->was_opened()); | 870 EXPECT_FALSE(observer->was_opened()); |
| 887 EXPECT_TRUE(download->file_externally_removed()); | 871 EXPECT_TRUE(download->file_externally_removed()); |
| 888 EXPECT_EQ(DownloadItem::COMPLETE, download->state()); | 872 EXPECT_EQ(DownloadItem::COMPLETE, download->state()); |
| 889 EXPECT_EQ(download_item_model->GetStatusText(), | 873 EXPECT_EQ(download_item_model->GetStatusText(), |
| 890 l10n_util::GetStringUTF16(IDS_DOWNLOAD_STATUS_REMOVED)); | 874 l10n_util::GetStringUTF16(IDS_DOWNLOAD_STATUS_REMOVED)); |
| 891 | 875 |
| 892 EXPECT_FALSE(file_util::PathExists(new_path)); | 876 EXPECT_FALSE(file_util::PathExists(new_path)); |
| 893 } | 877 } |
| OLD | NEW |