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 125 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
136 scoped_refptr<content::DownloadBuffer> download_buffer_; | 136 scoped_refptr<content::DownloadBuffer> download_buffer_; |
137 | 137 |
138 DownloadFileManager* file_manager() { | 138 DownloadFileManager* file_manager() { |
139 if (!file_manager_) { | 139 if (!file_manager_) { |
140 file_manager_ = new DownloadFileManager(NULL); | 140 file_manager_ = new DownloadFileManager(NULL); |
141 download_manager_->SetFileManager(file_manager_); | 141 download_manager_->SetFileManager(file_manager_); |
142 } | 142 } |
143 return file_manager_; | 143 return file_manager_; |
144 } | 144 } |
145 | 145 |
146 // Make sure download item |id| was set with correct safety state for | |
147 // given |is_dangerous_file| and |is_dangerous_url|. | |
148 bool VerifySafetyState(bool is_dangerous_file, | |
149 bool is_dangerous_url, | |
150 int id) { | |
151 DownloadItem::SafetyState safety_state = | |
152 download_manager_->GetDownloadItem(id)->safety_state(); | |
153 return (is_dangerous_file || is_dangerous_url) ? | |
154 safety_state != DownloadItem::SAFE : safety_state == DownloadItem::SAFE; | |
155 } | |
156 | |
157 DISALLOW_COPY_AND_ASSIGN(DownloadManagerTest); | 146 DISALLOW_COPY_AND_ASSIGN(DownloadManagerTest); |
158 }; | 147 }; |
159 | 148 |
160 const char* DownloadManagerTest::kTestData = "a;sdlfalsdfjalsdkfjad"; | 149 const char* DownloadManagerTest::kTestData = "a;sdlfalsdfjalsdkfjad"; |
161 const size_t DownloadManagerTest::kTestDataLen = | 150 const size_t DownloadManagerTest::kTestDataLen = |
162 strlen(DownloadManagerTest::kTestData); | 151 strlen(DownloadManagerTest::kTestData); |
163 | 152 |
164 // A DownloadFile that we can inject errors into. | 153 // A DownloadFile that we can inject errors into. |
165 class DownloadFileWithErrors : public DownloadFileImpl { | 154 class DownloadFileWithErrors : public DownloadFileImpl { |
166 public: | 155 public: |
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
255 false, }, | 244 false, }, |
256 { "http://www.foo.com/always_prompt.jar", | 245 { "http://www.foo.com/always_prompt.jar", |
257 "application/jar", | 246 "application/jar", |
258 false, | 247 false, |
259 true, | 248 true, |
260 true, }, | 249 true, }, |
261 }; | 250 }; |
262 | 251 |
263 const struct { | 252 const struct { |
264 FilePath::StringType suggested_path; | 253 FilePath::StringType suggested_path; |
265 bool is_dangerous_file; | 254 DownloadStateInfo::DangerType danger; |
266 bool is_dangerous_url; | |
267 bool finish_before_rename; | 255 bool finish_before_rename; |
268 int expected_rename_count; | 256 int expected_rename_count; |
269 } kDownloadRenameCases[] = { | 257 } kDownloadRenameCases[] = { |
270 // Safe download, download finishes BEFORE file name determined. | 258 // Safe download, download finishes BEFORE file name determined. |
271 // Renamed twice (linear path through UI). Crdownload file does not need | 259 // Renamed twice (linear path through UI). Crdownload file does not need |
272 // to be deleted. | 260 // to be deleted. |
273 { FILE_PATH_LITERAL("foo.zip"), false, false, true, 2, }, | 261 { FILE_PATH_LITERAL("foo.zip"), DownloadStateInfo::NOT_DANGEROUS, true, 2, }, |
274 // Dangerous download (file is dangerous or download URL is not safe or both), | 262 // Potentially dangerous download (e.g., file is dangerous), download finishes |
275 // download finishes BEFORE file name determined. Needs to be renamed only | 263 // BEFORE file name determined. Needs to be renamed only once. |
276 // once. | 264 { FILE_PATH_LITERAL("Unconfirmed xxx.crdownload"), |
277 { FILE_PATH_LITERAL("Unconfirmed xxx.crdownload"), true, false, true, 1, }, | 265 DownloadStateInfo::MAYBE_DANGEROUS_CONTENT, true, 1, }, |
278 { FILE_PATH_LITERAL("Unconfirmed xxx.crdownload"), false, true, true, 1, }, | 266 { FILE_PATH_LITERAL("Unconfirmed xxx.crdownload"), |
279 { FILE_PATH_LITERAL("Unconfirmed xxx.crdownload"), true, true, true, 1, }, | 267 DownloadStateInfo::DANGEROUS_FILE, true, 1, }, |
280 // Safe download, download finishes AFTER file name determined. | 268 // Safe download, download finishes AFTER file name determined. |
281 // Needs to be renamed twice. | 269 // Needs to be renamed twice. |
282 { FILE_PATH_LITERAL("foo.zip"), false, false, false, 2, }, | 270 { FILE_PATH_LITERAL("foo.zip"), DownloadStateInfo::NOT_DANGEROUS, false, 2, }, |
283 // Dangerous download, download finishes AFTER file name determined. | 271 // Potentially dangerous download, download finishes AFTER file name |
284 // Needs to be renamed only once. | 272 // determined. Needs to be renamed only once. |
285 { FILE_PATH_LITERAL("Unconfirmed xxx.crdownload"), true, false, false, 1, }, | 273 { FILE_PATH_LITERAL("Unconfirmed xxx.crdownload"), |
286 { FILE_PATH_LITERAL("Unconfirmed xxx.crdownload"), false, true, false, 1, }, | 274 DownloadStateInfo::MAYBE_DANGEROUS_CONTENT, false, 1, }, |
287 { FILE_PATH_LITERAL("Unconfirmed xxx.crdownload"), true, true, false, 1, }, | 275 { FILE_PATH_LITERAL("Unconfirmed xxx.crdownload"), |
| 276 DownloadStateInfo::DANGEROUS_FILE, false, 1, }, |
288 }; | 277 }; |
289 | 278 |
290 // This is an observer that records what download IDs have opened a select | 279 // This is an observer that records what download IDs have opened a select |
291 // file dialog. | 280 // file dialog. |
292 class SelectFileObserver : public DownloadManager::Observer { | 281 class SelectFileObserver : public DownloadManager::Observer { |
293 public: | 282 public: |
294 explicit SelectFileObserver(DownloadManager* download_manager) | 283 explicit SelectFileObserver(DownloadManager* download_manager) |
295 : download_manager_(download_manager) { | 284 : download_manager_(download_manager) { |
296 DCHECK(download_manager_.get()); | 285 DCHECK(download_manager_.get()); |
297 download_manager_->AddObserver(this); | 286 download_manager_->AddObserver(this); |
(...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
428 download_file->SetExpectedPath(0, new_path); | 417 download_file->SetExpectedPath(0, new_path); |
429 } else { | 418 } else { |
430 ASSERT_EQ(2, kDownloadRenameCases[i].expected_rename_count); | 419 ASSERT_EQ(2, kDownloadRenameCases[i].expected_rename_count); |
431 FilePath crdownload(download_util::GetCrDownloadPath(new_path)); | 420 FilePath crdownload(download_util::GetCrDownloadPath(new_path)); |
432 download_file->SetExpectedPath(0, crdownload); | 421 download_file->SetExpectedPath(0, crdownload); |
433 download_file->SetExpectedPath(1, new_path); | 422 download_file->SetExpectedPath(1, new_path); |
434 } | 423 } |
435 download_manager_->CreateDownloadItem(info.get(), DownloadRequestHandle()); | 424 download_manager_->CreateDownloadItem(info.get(), DownloadRequestHandle()); |
436 DownloadItem* download = GetActiveDownloadItem(i); | 425 DownloadItem* download = GetActiveDownloadItem(i); |
437 ASSERT_TRUE(download != NULL); | 426 ASSERT_TRUE(download != NULL); |
438 if (kDownloadRenameCases[i].is_dangerous_file) | 427 DownloadStateInfo state = download->state_info(); |
439 download->MarkFileDangerous(); | 428 state.danger = kDownloadRenameCases[i].danger; |
440 if (kDownloadRenameCases[i].is_dangerous_url) | 429 download->SetFileCheckResults(state); |
441 download->MarkUrlDangerous(); | |
442 | 430 |
443 int32* id_ptr = new int32; | 431 int32* id_ptr = new int32; |
444 *id_ptr = i; // Deleted in FileSelected(). | 432 *id_ptr = i; // Deleted in FileSelected(). |
445 if (kDownloadRenameCases[i].finish_before_rename) { | 433 if (kDownloadRenameCases[i].finish_before_rename) { |
446 OnResponseCompleted(i, 1024, std::string("fake_hash")); | 434 OnResponseCompleted(i, 1024, std::string("fake_hash")); |
447 message_loop_.RunAllPending(); | 435 message_loop_.RunAllPending(); |
448 FileSelected(new_path, id_ptr); | 436 FileSelected(new_path, id_ptr); |
449 } else { | 437 } else { |
450 FileSelected(new_path, id_ptr); | 438 FileSelected(new_path, id_ptr); |
451 message_loop_.RunAllPending(); | 439 message_loop_.RunAllPending(); |
452 OnResponseCompleted(i, 1024, std::string("fake_hash")); | 440 OnResponseCompleted(i, 1024, std::string("fake_hash")); |
453 } | 441 } |
454 | |
455 message_loop_.RunAllPending(); | 442 message_loop_.RunAllPending(); |
456 EXPECT_EQ( | 443 EXPECT_EQ( |
457 kDownloadRenameCases[i].expected_rename_count, | 444 kDownloadRenameCases[i].expected_rename_count, |
458 recorder.Count(MockDownloadFile::StatisticsRecorder::STAT_RENAME)); | 445 recorder.Count(MockDownloadFile::StatisticsRecorder::STAT_RENAME)); |
459 EXPECT_TRUE(VerifySafetyState(kDownloadRenameCases[i].is_dangerous_file, | |
460 kDownloadRenameCases[i].is_dangerous_url, | |
461 i)); | |
462 } | 446 } |
463 } | 447 } |
464 | 448 |
465 TEST_F(DownloadManagerTest, DownloadInterruptTest) { | 449 TEST_F(DownloadManagerTest, DownloadInterruptTest) { |
466 using ::testing::_; | 450 using ::testing::_; |
467 using ::testing::CreateFunctor; | 451 using ::testing::CreateFunctor; |
468 using ::testing::Invoke; | 452 using ::testing::Invoke; |
469 using ::testing::Return; | 453 using ::testing::Return; |
470 | 454 |
471 // Normally, the download system takes ownership of info, and is | 455 // Normally, the download system takes ownership of info, and is |
(...skipping 400 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
872 EXPECT_FALSE(observer->hit_state(DownloadItem::REMOVING)); | 856 EXPECT_FALSE(observer->hit_state(DownloadItem::REMOVING)); |
873 EXPECT_TRUE(observer->was_updated()); | 857 EXPECT_TRUE(observer->was_updated()); |
874 EXPECT_FALSE(observer->was_opened()); | 858 EXPECT_FALSE(observer->was_opened()); |
875 EXPECT_TRUE(download->file_externally_removed()); | 859 EXPECT_TRUE(download->file_externally_removed()); |
876 EXPECT_EQ(DownloadItem::COMPLETE, download->state()); | 860 EXPECT_EQ(DownloadItem::COMPLETE, download->state()); |
877 EXPECT_EQ(download_item_model->GetStatusText(), | 861 EXPECT_EQ(download_item_model->GetStatusText(), |
878 l10n_util::GetStringUTF16(IDS_DOWNLOAD_STATUS_REMOVED)); | 862 l10n_util::GetStringUTF16(IDS_DOWNLOAD_STATUS_REMOVED)); |
879 | 863 |
880 EXPECT_FALSE(file_util::PathExists(new_path)); | 864 EXPECT_FALSE(file_util::PathExists(new_path)); |
881 } | 865 } |
OLD | NEW |