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 bool needs_quarantine_file; |
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"), false, 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"), true, true, 1, }, |
270 { FILE_PATH_LITERAL("Unconfirmed xxx.crdownload"), true, false, true, 1, }, | |
271 { FILE_PATH_LITERAL("Unconfirmed xxx.crdownload"), false, true, true, 1, }, | |
272 { FILE_PATH_LITERAL("Unconfirmed xxx.crdownload"), true, true, true, 1, }, | |
273 // Safe download, download finishes AFTER file name determined. | 258 // Safe download, download finishes AFTER file name determined. |
274 // Needs to be renamed twice. | 259 // Needs to be renamed twice. |
275 { FILE_PATH_LITERAL("foo.zip"), false, false, false, 2, }, | 260 { FILE_PATH_LITERAL("foo.zip"), false, false, 2, }, |
276 // Dangerous download, download finishes AFTER file name determined. | 261 // Potentially dangerous download, download finishes AFTER file name |
277 // Needs to be renamed only once. | 262 // determined. Needs to be renamed only once. |
278 { FILE_PATH_LITERAL("Unconfirmed xxx.crdownload"), true, false, false, 1, }, | 263 { FILE_PATH_LITERAL("Unconfirmed xxx.crdownload"), true, false, 1, }, |
279 { FILE_PATH_LITERAL("Unconfirmed xxx.crdownload"), false, true, false, 1, }, | |
280 { FILE_PATH_LITERAL("Unconfirmed xxx.crdownload"), true, true, false, 1, }, | |
281 }; | 264 }; |
282 | 265 |
283 class MockDownloadFile : public DownloadFile { | 266 class MockDownloadFile : public DownloadFile { |
284 public: | 267 public: |
285 MockDownloadFile(DownloadCreateInfo* info, DownloadManager* manager) | 268 MockDownloadFile(DownloadCreateInfo* info, DownloadManager* manager) |
286 : DownloadFile(info, new DownloadRequestHandle(), manager), | 269 : DownloadFile(info, new DownloadRequestHandle(), manager), |
287 renamed_count_(0) { } | 270 renamed_count_(0) { } |
288 virtual ~MockDownloadFile() { Destructed(); } | 271 virtual ~MockDownloadFile() { Destructed(); } |
289 MOCK_METHOD1(Rename, net::Error(const FilePath&)); | 272 MOCK_METHOD1(Rename, net::Error(const FilePath&)); |
290 MOCK_METHOD0(Destructed, void()); | 273 MOCK_METHOD0(Destructed, void()); |
(...skipping 156 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
447 .WillOnce(testing::WithArgs<0>(Invoke(CreateFunctor( | 430 .WillOnce(testing::WithArgs<0>(Invoke(CreateFunctor( |
448 download_file, &MockDownloadFile::TestMultipleRename, | 431 download_file, &MockDownloadFile::TestMultipleRename, |
449 1, crdownload)))) | 432 1, crdownload)))) |
450 .WillOnce(testing::WithArgs<0>(Invoke(CreateFunctor( | 433 .WillOnce(testing::WithArgs<0>(Invoke(CreateFunctor( |
451 download_file, &MockDownloadFile::TestMultipleRename, | 434 download_file, &MockDownloadFile::TestMultipleRename, |
452 2, new_path)))); | 435 2, new_path)))); |
453 } | 436 } |
454 download_manager_->CreateDownloadItem(info.get(), DownloadRequestHandle()); | 437 download_manager_->CreateDownloadItem(info.get(), DownloadRequestHandle()); |
455 DownloadItem* download = GetActiveDownloadItem(i); | 438 DownloadItem* download = GetActiveDownloadItem(i); |
456 ASSERT_TRUE(download != NULL); | 439 ASSERT_TRUE(download != NULL); |
457 if (kDownloadRenameCases[i].is_dangerous_file) | 440 if (kDownloadRenameCases[i].needs_quarantine_file) { |
458 download->MarkFileDangerous(); | 441 DownloadStateInfo info = download->state_info(); |
459 if (kDownloadRenameCases[i].is_dangerous_url) | 442 info.needs_quarantine_file = true; |
460 download->MarkUrlDangerous(); | 443 download->SetFileCheckResults(info); |
| 444 } |
461 | 445 |
462 int32* id_ptr = new int32; | 446 int32* id_ptr = new int32; |
463 *id_ptr = i; // Deleted in FileSelected(). | 447 *id_ptr = i; // Deleted in FileSelected(). |
464 if (kDownloadRenameCases[i].finish_before_rename) { | 448 if (kDownloadRenameCases[i].finish_before_rename) { |
465 OnResponseCompleted(i, 1024, std::string("fake_hash")); | 449 OnResponseCompleted(i, 1024, std::string("fake_hash")); |
466 message_loop_.RunAllPending(); | 450 message_loop_.RunAllPending(); |
467 FileSelected(new_path, id_ptr); | 451 FileSelected(new_path, id_ptr); |
468 } else { | 452 } else { |
469 FileSelected(new_path, id_ptr); | 453 FileSelected(new_path, id_ptr); |
470 message_loop_.RunAllPending(); | 454 message_loop_.RunAllPending(); |
471 OnResponseCompleted(i, 1024, std::string("fake_hash")); | 455 OnResponseCompleted(i, 1024, std::string("fake_hash")); |
472 } | 456 } |
473 | |
474 message_loop_.RunAllPending(); | 457 message_loop_.RunAllPending(); |
475 EXPECT_TRUE(VerifySafetyState(kDownloadRenameCases[i].is_dangerous_file, | |
476 kDownloadRenameCases[i].is_dangerous_url, | |
477 i)); | |
478 } | 458 } |
479 } | 459 } |
480 | 460 |
481 TEST_F(DownloadManagerTest, DownloadInterruptTest) { | 461 TEST_F(DownloadManagerTest, DownloadInterruptTest) { |
482 using ::testing::_; | 462 using ::testing::_; |
483 using ::testing::CreateFunctor; | 463 using ::testing::CreateFunctor; |
484 using ::testing::Invoke; | 464 using ::testing::Invoke; |
485 using ::testing::Return; | 465 using ::testing::Return; |
486 | 466 |
487 // Normally, the download system takes ownership of info, and is | 467 // 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)); | 864 EXPECT_FALSE(observer->hit_state(DownloadItem::REMOVING)); |
885 EXPECT_TRUE(observer->was_updated()); | 865 EXPECT_TRUE(observer->was_updated()); |
886 EXPECT_FALSE(observer->was_opened()); | 866 EXPECT_FALSE(observer->was_opened()); |
887 EXPECT_TRUE(download->file_externally_removed()); | 867 EXPECT_TRUE(download->file_externally_removed()); |
888 EXPECT_EQ(DownloadItem::COMPLETE, download->state()); | 868 EXPECT_EQ(DownloadItem::COMPLETE, download->state()); |
889 EXPECT_EQ(download_item_model->GetStatusText(), | 869 EXPECT_EQ(download_item_model->GetStatusText(), |
890 l10n_util::GetStringUTF16(IDS_DOWNLOAD_STATUS_REMOVED)); | 870 l10n_util::GetStringUTF16(IDS_DOWNLOAD_STATUS_REMOVED)); |
891 | 871 |
892 EXPECT_FALSE(file_util::PathExists(new_path)); | 872 EXPECT_FALSE(file_util::PathExists(new_path)); |
893 } | 873 } |
OLD | NEW |