| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 <stddef.h> | 5 #include <stddef.h> |
| 6 #include <stdint.h> | 6 #include <stdint.h> |
| 7 | 7 |
| 8 #include <string> |
| 9 #include <vector> |
| 10 |
| 8 #include "base/at_exit.h" | 11 #include "base/at_exit.h" |
| 9 #include "base/files/file_path.h" | 12 #include "base/files/file_path.h" |
| 10 #include "base/files/scoped_temp_dir.h" | 13 #include "base/files/scoped_temp_dir.h" |
| 11 #include "base/location.h" | 14 #include "base/location.h" |
| 12 #include "base/macros.h" | 15 #include "base/macros.h" |
| 16 #include "base/memory/ptr_util.h" |
| 13 #include "base/observer_list.h" | 17 #include "base/observer_list.h" |
| 14 #include "base/run_loop.h" | 18 #include "base/run_loop.h" |
| 15 #include "base/single_thread_task_runner.h" | 19 #include "base/single_thread_task_runner.h" |
| 16 #include "base/strings/string_util.h" | 20 #include "base/strings/string_util.h" |
| 17 #include "base/threading/thread_task_runner_handle.h" | 21 #include "base/threading/thread_task_runner_handle.h" |
| 18 #include "base/value_conversions.h" | 22 #include "base/value_conversions.h" |
| 19 #include "build/build_config.h" | 23 #include "build/build_config.h" |
| 20 #include "chrome/browser/download/chrome_download_manager_delegate.h" | 24 #include "chrome/browser/download/chrome_download_manager_delegate.h" |
| 25 #include "chrome/browser/download/download_confirmation_result.h" |
| 21 #include "chrome/browser/download/download_prefs.h" | 26 #include "chrome/browser/download/download_prefs.h" |
| 22 #include "chrome/browser/download/download_target_determiner.h" | 27 #include "chrome/browser/download/download_target_determiner.h" |
| 23 #include "chrome/browser/download/download_target_info.h" | 28 #include "chrome/browser/download/download_target_info.h" |
| 24 #include "chrome/browser/history/history_service_factory.h" | 29 #include "chrome/browser/history/history_service_factory.h" |
| 30 #include "chrome/common/features.h" |
| 25 #include "chrome/common/pref_names.h" | 31 #include "chrome/common/pref_names.h" |
| 26 #include "chrome/common/safe_browsing/file_type_policies.h" | 32 #include "chrome/common/safe_browsing/file_type_policies.h" |
| 33 #include "chrome/common/safe_browsing/file_type_policies_test_util.h" |
| 27 #include "chrome/test/base/chrome_render_view_host_test_harness.h" | 34 #include "chrome/test/base/chrome_render_view_host_test_harness.h" |
| 28 #include "chrome/test/base/testing_profile.h" | 35 #include "chrome/test/base/testing_profile.h" |
| 29 #include "components/history/core/browser/history_service.h" | 36 #include "components/history/core/browser/history_service.h" |
| 30 #include "components/history/core/browser/history_types.h" | 37 #include "components/history/core/browser/history_types.h" |
| 31 #include "components/prefs/pref_service.h" | 38 #include "components/prefs/pref_service.h" |
| 32 #include "components/sync_preferences/testing_pref_service_syncable.h" | 39 #include "components/sync_preferences/testing_pref_service_syncable.h" |
| 33 #include "content/public/browser/download_interrupt_reasons.h" | 40 #include "content/public/browser/download_interrupt_reasons.h" |
| 34 #include "content/public/browser/render_process_host.h" | 41 #include "content/public/browser/render_process_host.h" |
| 35 #include "content/public/browser/web_contents.h" | 42 #include "content/public/browser/web_contents.h" |
| 36 #include "content/public/browser/web_contents_delegate.h" | 43 #include "content/public/browser/web_contents_delegate.h" |
| (...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 105 SAVE_AS, | 112 SAVE_AS, |
| 106 AUTOMATIC, | 113 AUTOMATIC, |
| 107 FORCED // Requires that forced_file_path be non-empty. | 114 FORCED // Requires that forced_file_path be non-empty. |
| 108 }; | 115 }; |
| 109 | 116 |
| 110 // Used with DownloadTestCase. Type of intermediate filename to expect. | 117 // Used with DownloadTestCase. Type of intermediate filename to expect. |
| 111 enum TestCaseExpectIntermediate { | 118 enum TestCaseExpectIntermediate { |
| 112 EXPECT_CRDOWNLOAD, // Expect path/to/target.crdownload. | 119 EXPECT_CRDOWNLOAD, // Expect path/to/target.crdownload. |
| 113 EXPECT_UNCONFIRMED, // Expect path/to/Unconfirmed xxx.crdownload. | 120 EXPECT_UNCONFIRMED, // Expect path/to/Unconfirmed xxx.crdownload. |
| 114 EXPECT_LOCAL_PATH, // Expect target path. | 121 EXPECT_LOCAL_PATH, // Expect target path. |
| 122 EXPECT_EMPTY, // Expect empty path. Only for downloads which will be |
| 123 // marked interrupted or cancelled. |
| 115 }; | 124 }; |
| 116 | 125 |
| 117 // Typical download test case. Used with | 126 // Typical download test case. Used with |
| 118 // DownloadTargetDeterminerTest::RunTestCase(). | 127 // DownloadTargetDeterminerTest::RunTestCase(). |
| 119 struct DownloadTestCase { | 128 struct DownloadTestCase { |
| 120 // Type of test. | 129 // Type of test. |
| 121 TestCaseType test_type; | 130 TestCaseType test_type; |
| 122 | 131 |
| 123 // Expected danger type. Verified at the end of target determination. | 132 // Expected danger type. Verified at the end of target determination. |
| 124 content::DownloadDangerType expected_danger_type; | 133 content::DownloadDangerType expected_danger_type; |
| (...skipping 24 matching lines...) Expand all Loading... |
| 149 | 158 |
| 150 class MockDownloadTargetDeterminerDelegate | 159 class MockDownloadTargetDeterminerDelegate |
| 151 : public DownloadTargetDeterminerDelegate { | 160 : public DownloadTargetDeterminerDelegate { |
| 152 public: | 161 public: |
| 153 MOCK_METHOD3(CheckDownloadUrl, | 162 MOCK_METHOD3(CheckDownloadUrl, |
| 154 void(content::DownloadItem*, const base::FilePath&, | 163 void(content::DownloadItem*, const base::FilePath&, |
| 155 const CheckDownloadUrlCallback&)); | 164 const CheckDownloadUrlCallback&)); |
| 156 MOCK_METHOD3(NotifyExtensions, | 165 MOCK_METHOD3(NotifyExtensions, |
| 157 void(content::DownloadItem*, const base::FilePath&, | 166 void(content::DownloadItem*, const base::FilePath&, |
| 158 const NotifyExtensionsCallback&)); | 167 const NotifyExtensionsCallback&)); |
| 159 MOCK_METHOD3(PromptUserForDownloadPath, | 168 MOCK_METHOD4(RequestConfirmation, |
| 160 void(content::DownloadItem*, const base::FilePath&, | 169 void(content::DownloadItem*, |
| 161 const FileSelectedCallback&)); | 170 const base::FilePath&, |
| 171 DownloadConfirmationReason, |
| 172 const ConfirmationCallback&)); |
| 162 MOCK_METHOD3(DetermineLocalPath, | 173 MOCK_METHOD3(DetermineLocalPath, |
| 163 void(DownloadItem*, const base::FilePath&, | 174 void(DownloadItem*, const base::FilePath&, |
| 164 const LocalPathCallback&)); | 175 const LocalPathCallback&)); |
| 165 MOCK_METHOD5(ReserveVirtualPath, | 176 MOCK_METHOD5(ReserveVirtualPath, |
| 166 void(DownloadItem*, const base::FilePath&, bool, | 177 void(DownloadItem*, const base::FilePath&, bool, |
| 167 DownloadPathReservationTracker::FilenameConflictAction, | 178 DownloadPathReservationTracker::FilenameConflictAction, |
| 168 const ReservedPathCallback&)); | 179 const ReservedPathCallback&)); |
| 169 MOCK_METHOD2(GetFileMimeType, | 180 MOCK_METHOD2(GetFileMimeType, |
| 170 void(const base::FilePath&, | 181 void(const base::FilePath&, |
| 171 const GetFileMimeTypeCallback&)); | 182 const GetFileMimeTypeCallback&)); |
| 172 | 183 |
| 173 void SetupDefaults() { | 184 void SetupDefaults() { |
| 174 ON_CALL(*this, CheckDownloadUrl(_, _, _)) | 185 ON_CALL(*this, CheckDownloadUrl(_, _, _)) |
| 175 .WillByDefault(WithArg<2>( | 186 .WillByDefault(WithArg<2>( |
| 176 ScheduleCallback(content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS))); | 187 ScheduleCallback(content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS))); |
| 177 ON_CALL(*this, NotifyExtensions(_, _, _)) | 188 ON_CALL(*this, NotifyExtensions(_, _, _)) |
| 178 .WillByDefault(WithArg<2>( | 189 .WillByDefault(WithArg<2>( |
| 179 ScheduleCallback2(base::FilePath(), | 190 ScheduleCallback2(base::FilePath(), |
| 180 DownloadPathReservationTracker::UNIQUIFY))); | 191 DownloadPathReservationTracker::UNIQUIFY))); |
| 181 ON_CALL(*this, ReserveVirtualPath(_, _, _, _, _)) | 192 ON_CALL(*this, ReserveVirtualPath(_, _, _, _, _)) |
| 182 .WillByDefault(Invoke( | 193 .WillByDefault(Invoke( |
| 183 &MockDownloadTargetDeterminerDelegate::NullReserveVirtualPath)); | 194 &MockDownloadTargetDeterminerDelegate::NullReserveVirtualPath)); |
| 184 ON_CALL(*this, PromptUserForDownloadPath(_, _, _)) | 195 ON_CALL(*this, RequestConfirmation(_, _, _, _)) |
| 185 .WillByDefault(Invoke( | 196 .WillByDefault( |
| 186 &MockDownloadTargetDeterminerDelegate::NullPromptUser)); | 197 Invoke(&MockDownloadTargetDeterminerDelegate::NullPromptUser)); |
| 187 ON_CALL(*this, DetermineLocalPath(_, _, _)) | 198 ON_CALL(*this, DetermineLocalPath(_, _, _)) |
| 188 .WillByDefault(Invoke( | 199 .WillByDefault(Invoke( |
| 189 &MockDownloadTargetDeterminerDelegate::NullDetermineLocalPath)); | 200 &MockDownloadTargetDeterminerDelegate::NullDetermineLocalPath)); |
| 190 ON_CALL(*this, GetFileMimeType(_, _)) | 201 ON_CALL(*this, GetFileMimeType(_, _)) |
| 191 .WillByDefault(WithArg<1>( | 202 .WillByDefault(WithArg<1>( |
| 192 ScheduleCallback(""))); | 203 ScheduleCallback(""))); |
| 193 } | 204 } |
| 194 private: | 205 private: |
| 195 static void NullReserveVirtualPath( | 206 static void NullReserveVirtualPath( |
| 196 DownloadItem* download, | 207 DownloadItem* download, |
| 197 const base::FilePath& virtual_path, | 208 const base::FilePath& virtual_path, |
| 198 bool create_directory, | 209 bool create_directory, |
| 199 DownloadPathReservationTracker::FilenameConflictAction conflict_action, | 210 DownloadPathReservationTracker::FilenameConflictAction conflict_action, |
| 200 const DownloadTargetDeterminerDelegate::ReservedPathCallback& callback); | 211 const DownloadTargetDeterminerDelegate::ReservedPathCallback& callback); |
| 201 static void NullPromptUser( | 212 static void NullPromptUser(DownloadItem* download, |
| 202 DownloadItem* download, const base::FilePath& suggested_path, | 213 const base::FilePath& suggested_path, |
| 203 const FileSelectedCallback& callback); | 214 DownloadConfirmationReason reason, |
| 215 const ConfirmationCallback& callback); |
| 204 static void NullDetermineLocalPath( | 216 static void NullDetermineLocalPath( |
| 205 DownloadItem* download, const base::FilePath& virtual_path, | 217 DownloadItem* download, const base::FilePath& virtual_path, |
| 206 const LocalPathCallback& callback); | 218 const LocalPathCallback& callback); |
| 207 }; | 219 }; |
| 208 | 220 |
| 209 class DownloadTargetDeterminerTest : public ChromeRenderViewHostTestHarness { | 221 class DownloadTargetDeterminerTest : public ChromeRenderViewHostTestHarness { |
| 210 public: | 222 public: |
| 211 // ::testing::Test | 223 // ::testing::Test |
| 212 void SetUp() override; | 224 void SetUp() override; |
| 213 void TearDown() override; | 225 void TearDown() override; |
| 214 | 226 |
| 215 // Creates MockDownloadItem and sets up default expectations. | 227 // Creates MockDownloadItem and sets up default expectations. |
| 216 content::MockDownloadItem* CreateActiveDownloadItem( | 228 std::unique_ptr<content::MockDownloadItem> CreateActiveDownloadItem( |
| 217 int32_t id, | 229 int32_t id, |
| 218 const DownloadTestCase& test_case); | 230 const DownloadTestCase& test_case); |
| 219 | 231 |
| 220 // Sets the AutoOpenBasedOnExtension user preference for |path|. | 232 // Sets the AutoOpenBasedOnExtension user preference for |path|. |
| 221 void EnableAutoOpenBasedOnExtension(const base::FilePath& path); | 233 void EnableAutoOpenBasedOnExtension(const base::FilePath& path); |
| 222 | 234 |
| 223 // Set the kDownloadDefaultDirectory managed preference to |path|. | 235 // Set the kDownloadDefaultDirectory managed preference to |path|. |
| 224 void SetManagedDownloadPath(const base::FilePath& path); | 236 void SetManagedDownloadPath(const base::FilePath& path); |
| 225 | 237 |
| 226 // Set the kPromptForDownload user preference to |prompt|. | 238 // Set the kPromptForDownload user preference to |prompt|. |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 262 } | 274 } |
| 263 | 275 |
| 264 MockDownloadTargetDeterminerDelegate* delegate() { | 276 MockDownloadTargetDeterminerDelegate* delegate() { |
| 265 return &delegate_; | 277 return &delegate_; |
| 266 } | 278 } |
| 267 | 279 |
| 268 DownloadPrefs* download_prefs() { | 280 DownloadPrefs* download_prefs() { |
| 269 return download_prefs_.get(); | 281 return download_prefs_.get(); |
| 270 } | 282 } |
| 271 | 283 |
| 272 // Shortcut | 284 private: |
| 273 const FileTypePolicies* Policies() const { | 285 void SetUpFileTypePolicies(); |
| 274 return FileTypePolicies::GetInstance(); | |
| 275 } | |
| 276 | 286 |
| 277 private: | |
| 278 std::unique_ptr<DownloadPrefs> download_prefs_; | 287 std::unique_ptr<DownloadPrefs> download_prefs_; |
| 279 ::testing::NiceMock<MockDownloadTargetDeterminerDelegate> delegate_; | 288 ::testing::NiceMock<MockDownloadTargetDeterminerDelegate> delegate_; |
| 280 NullWebContentsDelegate web_contents_delegate_; | 289 NullWebContentsDelegate web_contents_delegate_; |
| 281 base::ScopedTempDir test_download_dir_; | 290 base::ScopedTempDir test_download_dir_; |
| 282 base::FilePath test_virtual_dir_; | 291 base::FilePath test_virtual_dir_; |
| 292 safe_browsing::FileTypePoliciesTestOverlay file_type_configuration_; |
| 283 }; | 293 }; |
| 284 | 294 |
| 285 void DownloadTargetDeterminerTest::SetUp() { | 295 void DownloadTargetDeterminerTest::SetUp() { |
| 286 ChromeRenderViewHostTestHarness::SetUp(); | 296 ChromeRenderViewHostTestHarness::SetUp(); |
| 287 CHECK(profile()); | 297 CHECK(profile()); |
| 288 download_prefs_.reset(new DownloadPrefs(profile())); | 298 download_prefs_.reset(new DownloadPrefs(profile())); |
| 289 web_contents()->SetDelegate(&web_contents_delegate_); | 299 web_contents()->SetDelegate(&web_contents_delegate_); |
| 290 ASSERT_TRUE(test_download_dir_.CreateUniqueTempDir()); | 300 ASSERT_TRUE(test_download_dir_.CreateUniqueTempDir()); |
| 291 test_virtual_dir_ = test_download_dir().Append(FILE_PATH_LITERAL("virtual")); | 301 test_virtual_dir_ = test_download_dir().Append(FILE_PATH_LITERAL("virtual")); |
| 292 download_prefs_->SetDownloadPath(test_download_dir()); | 302 download_prefs_->SetDownloadPath(test_download_dir()); |
| 293 delegate_.SetupDefaults(); | 303 delegate_.SetupDefaults(); |
| 304 SetUpFileTypePolicies(); |
| 294 } | 305 } |
| 295 | 306 |
| 296 void DownloadTargetDeterminerTest::TearDown() { | 307 void DownloadTargetDeterminerTest::TearDown() { |
| 297 download_prefs_.reset(); | 308 download_prefs_.reset(); |
| 298 ChromeRenderViewHostTestHarness::TearDown(); | 309 ChromeRenderViewHostTestHarness::TearDown(); |
| 299 } | 310 } |
| 300 | 311 |
| 301 content::MockDownloadItem* | 312 std::unique_ptr<content::MockDownloadItem> |
| 302 DownloadTargetDeterminerTest::CreateActiveDownloadItem( | 313 DownloadTargetDeterminerTest::CreateActiveDownloadItem( |
| 303 int32_t id, | 314 int32_t id, |
| 304 const DownloadTestCase& test_case) { | 315 const DownloadTestCase& test_case) { |
| 305 content::MockDownloadItem* item = | 316 std::unique_ptr<content::MockDownloadItem> item = |
| 306 new ::testing::NiceMock<content::MockDownloadItem>(); | 317 base::MakeUnique<::testing::NiceMock<content::MockDownloadItem>>(); |
| 307 GURL download_url(test_case.url); | 318 GURL download_url(test_case.url); |
| 308 std::vector<GURL> url_chain; | 319 std::vector<GURL> url_chain; |
| 309 url_chain.push_back(download_url); | 320 url_chain.push_back(download_url); |
| 310 base::FilePath forced_file_path = | 321 base::FilePath forced_file_path = |
| 311 GetPathInDownloadDir(test_case.forced_file_path); | 322 GetPathInDownloadDir(test_case.forced_file_path); |
| 312 DownloadItem::TargetDisposition initial_disposition = | 323 DownloadItem::TargetDisposition initial_disposition = |
| 313 (test_case.test_type == SAVE_AS) ? | 324 (test_case.test_type == SAVE_AS) ? |
| 314 DownloadItem::TARGET_DISPOSITION_PROMPT : | 325 DownloadItem::TARGET_DISPOSITION_PROMPT : |
| 315 DownloadItem::TARGET_DISPOSITION_OVERWRITE; | 326 DownloadItem::TARGET_DISPOSITION_OVERWRITE; |
| 316 EXPECT_EQ(test_case.test_type == FORCED, | 327 EXPECT_EQ(test_case.test_type == FORCED, |
| (...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 400 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, closure); | 411 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, closure); |
| 401 } | 412 } |
| 402 | 413 |
| 403 std::unique_ptr<DownloadTargetInfo> | 414 std::unique_ptr<DownloadTargetInfo> |
| 404 DownloadTargetDeterminerTest::RunDownloadTargetDeterminer( | 415 DownloadTargetDeterminerTest::RunDownloadTargetDeterminer( |
| 405 const base::FilePath& initial_virtual_path, | 416 const base::FilePath& initial_virtual_path, |
| 406 content::MockDownloadItem* item) { | 417 content::MockDownloadItem* item) { |
| 407 std::unique_ptr<DownloadTargetInfo> target_info; | 418 std::unique_ptr<DownloadTargetInfo> target_info; |
| 408 base::RunLoop run_loop; | 419 base::RunLoop run_loop; |
| 409 DownloadTargetDeterminer::Start( | 420 DownloadTargetDeterminer::Start( |
| 410 item, initial_virtual_path, download_prefs_.get(), delegate(), | 421 item, initial_virtual_path, DownloadPathReservationTracker::UNIQUIFY, |
| 411 base::Bind(&CompletionCallbackWrapper, | 422 download_prefs_.get(), delegate(), |
| 412 run_loop.QuitClosure(), | 423 base::Bind(&CompletionCallbackWrapper, run_loop.QuitClosure(), |
| 413 &target_info)); | 424 &target_info)); |
| 414 run_loop.Run(); | 425 run_loop.Run(); |
| 415 ::testing::Mock::VerifyAndClearExpectations(delegate()); | 426 ::testing::Mock::VerifyAndClearExpectations(delegate()); |
| 416 return target_info; | 427 return target_info; |
| 417 } | 428 } |
| 418 | 429 |
| 419 void DownloadTargetDeterminerTest::RunTestCasesWithActiveItem( | 430 void DownloadTargetDeterminerTest::RunTestCasesWithActiveItem( |
| 420 const DownloadTestCase test_cases[], | 431 const DownloadTestCase test_cases[], |
| 421 size_t test_case_count) { | 432 size_t test_case_count) { |
| 422 for (size_t i = 0; i < test_case_count; ++i) { | 433 for (size_t i = 0; i < test_case_count; ++i) { |
| 423 std::unique_ptr<content::MockDownloadItem> item( | 434 std::unique_ptr<content::MockDownloadItem> item = |
| 424 CreateActiveDownloadItem(i, test_cases[i])); | 435 CreateActiveDownloadItem(i, test_cases[i]); |
| 425 SCOPED_TRACE(testing::Message() << "Running test case " << i); | 436 SCOPED_TRACE(testing::Message() << "Running test case " << i); |
| 426 RunTestCase(test_cases[i], base::FilePath(), item.get()); | 437 RunTestCase(test_cases[i], base::FilePath(), item.get()); |
| 427 } | 438 } |
| 428 } | 439 } |
| 429 | 440 |
| 430 void DownloadTargetDeterminerTest::VerifyDownloadTarget( | 441 void DownloadTargetDeterminerTest::VerifyDownloadTarget( |
| 431 const DownloadTestCase& test_case, | 442 const DownloadTestCase& test_case, |
| 432 const DownloadTargetInfo* target_info) { | 443 const DownloadTargetInfo* target_info) { |
| 433 base::FilePath expected_local_path( | 444 base::FilePath expected_local_path( |
| 434 GetPathInDownloadDir(test_case.expected_local_path)); | 445 GetPathInDownloadDir(test_case.expected_local_path)); |
| (...skipping 25 matching lines...) Expand all Loading... |
| 460 FILE_PATH_LITERAL(".crdownload"))); | 471 FILE_PATH_LITERAL(".crdownload"))); |
| 461 EXPECT_EQ(0u, | 472 EXPECT_EQ(0u, |
| 462 target_info->intermediate_path.BaseName().value().find( | 473 target_info->intermediate_path.BaseName().value().find( |
| 463 FILE_PATH_LITERAL("Unconfirmed "))); | 474 FILE_PATH_LITERAL("Unconfirmed "))); |
| 464 break; | 475 break; |
| 465 | 476 |
| 466 case EXPECT_LOCAL_PATH: | 477 case EXPECT_LOCAL_PATH: |
| 467 EXPECT_EQ(expected_local_path.value(), | 478 EXPECT_EQ(expected_local_path.value(), |
| 468 target_info->intermediate_path.value()); | 479 target_info->intermediate_path.value()); |
| 469 break; | 480 break; |
| 481 |
| 482 case EXPECT_EMPTY: |
| 483 EXPECT_TRUE(target_info->intermediate_path.empty()); |
| 484 break; |
| 470 } | 485 } |
| 471 } | 486 } |
| 472 | 487 |
| 488 void DownloadTargetDeterminerTest::SetUpFileTypePolicies() { |
| 489 std::unique_ptr<safe_browsing::DownloadFileTypeConfig> fake_file_type_config = |
| 490 base::MakeUnique<safe_browsing::DownloadFileTypeConfig>(); |
| 491 auto* file_type = fake_file_type_config->mutable_default_file_type(); |
| 492 file_type->set_uma_value(-1); |
| 493 auto* platform_settings = file_type->add_platform_settings(); |
| 494 platform_settings->set_danger_level(DownloadFileType::NOT_DANGEROUS); |
| 495 platform_settings->set_auto_open_hint(DownloadFileType::ALLOW_AUTO_OPEN); |
| 496 |
| 497 file_type = fake_file_type_config->add_file_types(); |
| 498 file_type->set_extension("kindabad"); |
| 499 file_type->set_uma_value(-1); |
| 500 platform_settings = file_type->add_platform_settings(); |
| 501 platform_settings->set_danger_level(DownloadFileType::ALLOW_ON_USER_GESTURE); |
| 502 platform_settings->set_auto_open_hint(DownloadFileType::ALLOW_AUTO_OPEN); |
| 503 |
| 504 file_type = fake_file_type_config->add_file_types(); |
| 505 file_type->set_extension("bad"); |
| 506 file_type->set_uma_value(-1); |
| 507 platform_settings = file_type->add_platform_settings(); |
| 508 platform_settings->set_danger_level(DownloadFileType::DANGEROUS); |
| 509 platform_settings->set_auto_open_hint(DownloadFileType::DISALLOW_AUTO_OPEN); |
| 510 |
| 511 file_type_configuration_.SwapConfig(fake_file_type_config); |
| 512 } |
| 513 |
| 473 // static | 514 // static |
| 474 void MockDownloadTargetDeterminerDelegate::NullReserveVirtualPath( | 515 void MockDownloadTargetDeterminerDelegate::NullReserveVirtualPath( |
| 475 DownloadItem* download, | 516 DownloadItem* download, |
| 476 const base::FilePath& virtual_path, | 517 const base::FilePath& virtual_path, |
| 477 bool create_directory, | 518 bool create_directory, |
| 478 DownloadPathReservationTracker::FilenameConflictAction conflict_action, | 519 DownloadPathReservationTracker::FilenameConflictAction conflict_action, |
| 479 const DownloadTargetDeterminerDelegate::ReservedPathCallback& callback) { | 520 const DownloadTargetDeterminerDelegate::ReservedPathCallback& callback) { |
| 480 callback.Run(virtual_path, true); | 521 callback.Run(PathValidationResult::SUCCESS, virtual_path); |
| 481 } | 522 } |
| 482 | 523 |
| 483 // static | 524 // static |
| 484 void MockDownloadTargetDeterminerDelegate::NullPromptUser( | 525 void MockDownloadTargetDeterminerDelegate::NullPromptUser( |
| 485 DownloadItem* download, const base::FilePath& suggested_path, | 526 DownloadItem* download, |
| 486 const FileSelectedCallback& callback) { | 527 const base::FilePath& suggested_path, |
| 487 callback.Run(suggested_path); | 528 DownloadConfirmationReason reason, |
| 529 const ConfirmationCallback& callback) { |
| 530 callback.Run(DownloadConfirmationResult::CONFIRMED, suggested_path); |
| 488 } | 531 } |
| 489 | 532 |
| 490 // static | 533 // static |
| 491 void MockDownloadTargetDeterminerDelegate::NullDetermineLocalPath( | 534 void MockDownloadTargetDeterminerDelegate::NullDetermineLocalPath( |
| 492 DownloadItem* download, const base::FilePath& virtual_path, | 535 DownloadItem* download, const base::FilePath& virtual_path, |
| 493 const LocalPathCallback& callback) { | 536 const LocalPathCallback& callback) { |
| 494 callback.Run(virtual_path); | 537 callback.Run(virtual_path); |
| 495 } | 538 } |
| 496 | 539 |
| 497 // NotifyExtensions implementation that overrides the path so that the target | 540 // NotifyExtensions implementation that overrides the path so that the target |
| 498 // file is in a subdirectory called 'overridden'. If the extension is '.remove', | 541 // file is in a subdirectory called 'overridden'. If the extension is '.remove', |
| 499 // the extension is removed. | 542 // the extension is removed. |
| 500 void NotifyExtensionsOverridePath( | 543 void NotifyExtensionsOverridePath( |
| 501 content::DownloadItem* download, | 544 content::DownloadItem* download, |
| 502 const base::FilePath& path, | 545 const base::FilePath& path, |
| 503 const DownloadTargetDeterminerDelegate::NotifyExtensionsCallback& | 546 const DownloadTargetDeterminerDelegate::NotifyExtensionsCallback& |
| 504 callback) { | 547 callback) { |
| 505 base::FilePath new_path = | 548 base::FilePath new_path = |
| 506 base::FilePath() | 549 base::FilePath() |
| 507 .AppendASCII("overridden") | 550 .AppendASCII("overridden") |
| 508 .Append(path.BaseName()); | 551 .Append(path.BaseName()); |
| 509 if (new_path.MatchesExtension(FILE_PATH_LITERAL(".remove"))) | 552 if (new_path.MatchesExtension(FILE_PATH_LITERAL(".remove"))) |
| 510 new_path = new_path.RemoveExtension(); | 553 new_path = new_path.RemoveExtension(); |
| 511 callback.Run(new_path, DownloadPathReservationTracker::UNIQUIFY); | 554 callback.Run(new_path, DownloadPathReservationTracker::UNIQUIFY); |
| 512 } | 555 } |
| 513 | 556 |
| 514 TEST_F(DownloadTargetDeterminerTest, Basic) { | 557 TEST_F(DownloadTargetDeterminerTest, Basic) { |
| 515 const DownloadTestCase kBasicTestCases[] = { | 558 const DownloadTestCase kBasicTestCases[] = { |
| 516 {// 0: Automatic Safe | 559 {// Automatic Safe |
| 517 AUTOMATIC, content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, | 560 AUTOMATIC, content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, |
| 518 DownloadFileType::NOT_DANGEROUS, "http://example.com/foo.txt", | 561 DownloadFileType::NOT_DANGEROUS, "http://example.com/foo.txt", |
| 519 "text/plain", FILE_PATH_LITERAL(""), | 562 "text/plain", FILE_PATH_LITERAL(""), |
| 520 | 563 |
| 521 FILE_PATH_LITERAL("foo.txt"), DownloadItem::TARGET_DISPOSITION_OVERWRITE, | 564 FILE_PATH_LITERAL("foo.txt"), DownloadItem::TARGET_DISPOSITION_OVERWRITE, |
| 522 | 565 |
| 523 EXPECT_CRDOWNLOAD}, | 566 EXPECT_CRDOWNLOAD}, |
| 524 | 567 |
| 525 #if !defined(OS_ANDROID) | 568 {// Save_As Safe |
| 526 {// 1: Save_As Safe | 569 SAVE_AS, content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, |
| 527 SAVE_AS, content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, | 570 DownloadFileType::NOT_DANGEROUS, "http://example.com/foo.txt", |
| 528 DownloadFileType::NOT_DANGEROUS, "http://example.com/foo.txt", | 571 "text/plain", FILE_PATH_LITERAL(""), |
| 529 "text/plain", FILE_PATH_LITERAL(""), | |
| 530 | 572 |
| 531 FILE_PATH_LITERAL("foo.txt"), DownloadItem::TARGET_DISPOSITION_PROMPT, | 573 FILE_PATH_LITERAL("foo.txt"), DownloadItem::TARGET_DISPOSITION_PROMPT, |
| 532 | 574 |
| 533 EXPECT_CRDOWNLOAD}, | 575 EXPECT_CRDOWNLOAD}, |
| 534 #endif // !defined(OS_ANDROID) | |
| 535 | 576 |
| 536 {// 2: Automatic Dangerous | 577 {// Automatic Dangerous |
| 537 AUTOMATIC, content::DOWNLOAD_DANGER_TYPE_DANGEROUS_FILE, | 578 AUTOMATIC, content::DOWNLOAD_DANGER_TYPE_DANGEROUS_FILE, |
| 538 DownloadFileType::ALLOW_ON_USER_GESTURE, "http://example.com/foo.crx", "", | 579 DownloadFileType::ALLOW_ON_USER_GESTURE, |
| 539 FILE_PATH_LITERAL(""), | 580 "http://example.com/foo.kindabad", "", FILE_PATH_LITERAL(""), |
| 540 | 581 |
| 541 FILE_PATH_LITERAL("foo.crx"), DownloadItem::TARGET_DISPOSITION_OVERWRITE, | 582 FILE_PATH_LITERAL("foo.kindabad"), |
| 583 DownloadItem::TARGET_DISPOSITION_OVERWRITE, |
| 542 | 584 |
| 543 EXPECT_UNCONFIRMED}, | 585 EXPECT_UNCONFIRMED}, |
| 544 | 586 |
| 545 {// 3: Forced Safe | 587 {// Forced Safe |
| 546 FORCED, content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, | 588 FORCED, content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, |
| 547 DownloadFileType::NOT_DANGEROUS, "http://example.com/foo.txt", "", | 589 DownloadFileType::NOT_DANGEROUS, "http://example.com/foo.txt", "", |
| 548 FILE_PATH_LITERAL("forced-foo.txt"), | 590 FILE_PATH_LITERAL("forced-foo.txt"), |
| 549 | 591 |
| 550 FILE_PATH_LITERAL("forced-foo.txt"), | 592 FILE_PATH_LITERAL("forced-foo.txt"), |
| 551 DownloadItem::TARGET_DISPOSITION_OVERWRITE, | 593 DownloadItem::TARGET_DISPOSITION_OVERWRITE, |
| 552 | 594 |
| 553 EXPECT_LOCAL_PATH}, | 595 EXPECT_LOCAL_PATH}, |
| 554 }; | 596 }; |
| 555 | 597 |
| 556 // The test assumes that .crx files have a danger level of | 598 // The test assumes that .kindabad files have a danger level of |
| 557 // ALLOW_ON_USER_GESTURE. | 599 // ALLOW_ON_USER_GESTURE. |
| 558 ASSERT_EQ(DownloadFileType::ALLOW_ON_USER_GESTURE, | 600 ASSERT_EQ(DownloadFileType::ALLOW_ON_USER_GESTURE, |
| 559 Policies()->GetFileDangerLevel( | 601 safe_browsing::FileTypePolicies::GetInstance()->GetFileDangerLevel( |
| 560 base::FilePath(FILE_PATH_LITERAL("foo.crx")))); | 602 base::FilePath(FILE_PATH_LITERAL("foo.kindabad")))); |
| 561 RunTestCasesWithActiveItem(kBasicTestCases, arraysize(kBasicTestCases)); | 603 RunTestCasesWithActiveItem(kBasicTestCases, arraysize(kBasicTestCases)); |
| 562 } | 604 } |
| 563 | 605 |
| 564 #if !defined(OS_ANDROID) | |
| 565 TEST_F(DownloadTargetDeterminerTest, CancelSaveAs) { | 606 TEST_F(DownloadTargetDeterminerTest, CancelSaveAs) { |
| 566 const DownloadTestCase kCancelSaveAsTestCases[] = { | 607 const DownloadTestCase kCancelSaveAsTestCases[] = { |
| 567 {// 0: Save_As Safe, Cancelled. | 608 {// 0: Save_As Safe, Cancelled. |
| 568 SAVE_AS, content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, | 609 SAVE_AS, content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, |
| 569 DownloadFileType::NOT_DANGEROUS, "http://example.com/foo.txt", | 610 DownloadFileType::NOT_DANGEROUS, "http://example.com/foo.txt", |
| 570 "text/plain", FILE_PATH_LITERAL(""), | 611 "text/plain", FILE_PATH_LITERAL(""), |
| 571 | 612 |
| 572 FILE_PATH_LITERAL(""), DownloadItem::TARGET_DISPOSITION_PROMPT, | 613 FILE_PATH_LITERAL(""), DownloadItem::TARGET_DISPOSITION_PROMPT, |
| 573 | 614 |
| 574 EXPECT_LOCAL_PATH}}; | 615 EXPECT_LOCAL_PATH}}; |
| 575 ON_CALL(*delegate(), PromptUserForDownloadPath(_, _, _)) | 616 ON_CALL(*delegate(), RequestConfirmation(_, _, _, _)) |
| 576 .WillByDefault(WithArg<2>(ScheduleCallback(base::FilePath()))); | 617 .WillByDefault(WithArg<3>(ScheduleCallback2( |
| 618 DownloadConfirmationResult::CANCELED, base::FilePath()))); |
| 577 RunTestCasesWithActiveItem(kCancelSaveAsTestCases, | 619 RunTestCasesWithActiveItem(kCancelSaveAsTestCases, |
| 578 arraysize(kCancelSaveAsTestCases)); | 620 arraysize(kCancelSaveAsTestCases)); |
| 579 } | 621 } |
| 580 #endif // !defined(OS_ANDROID) | |
| 581 | 622 |
| 582 // The SafeBrowsing check is performed early. Make sure that a download item | 623 // The SafeBrowsing check is performed early. Make sure that a download item |
| 583 // that has been marked as DANGEROUS_URL behaves correctly. | 624 // that has been marked as DANGEROUS_URL behaves correctly. |
| 584 TEST_F(DownloadTargetDeterminerTest, DangerousUrl) { | 625 TEST_F(DownloadTargetDeterminerTest, DangerousUrl) { |
| 585 const DownloadTestCase kSafeBrowsingTestCases[] = { | 626 const DownloadTestCase kSafeBrowsingTestCases[] = { |
| 586 {// 0: Automatic Dangerous URL | 627 {// 0: Automatic Dangerous URL |
| 587 AUTOMATIC, content::DOWNLOAD_DANGER_TYPE_DANGEROUS_URL, | 628 AUTOMATIC, content::DOWNLOAD_DANGER_TYPE_DANGEROUS_URL, |
| 588 DownloadFileType::NOT_DANGEROUS, "http://phishing.example.com/foo.txt", "", | 629 DownloadFileType::NOT_DANGEROUS, "http://phishing.example.com/foo.txt", |
| 589 FILE_PATH_LITERAL(""), | 630 "", FILE_PATH_LITERAL(""), |
| 590 | 631 |
| 591 FILE_PATH_LITERAL("foo.txt"), DownloadItem::TARGET_DISPOSITION_OVERWRITE, | 632 FILE_PATH_LITERAL("foo.txt"), DownloadItem::TARGET_DISPOSITION_OVERWRITE, |
| 592 | 633 |
| 593 EXPECT_UNCONFIRMED}, | 634 EXPECT_UNCONFIRMED}, |
| 594 | 635 |
| 595 #if !defined(OS_ANDROID) | 636 {// 1: Save As Dangerous URL |
| 596 {// 1: Save As Dangerous URL | 637 SAVE_AS, content::DOWNLOAD_DANGER_TYPE_DANGEROUS_URL, |
| 597 SAVE_AS, content::DOWNLOAD_DANGER_TYPE_DANGEROUS_URL, | 638 DownloadFileType::NOT_DANGEROUS, "http://phishing.example.com/foo.txt", |
| 598 DownloadFileType::NOT_DANGEROUS, "http://phishing.example.com/foo.txt", "", | 639 "", FILE_PATH_LITERAL(""), |
| 599 FILE_PATH_LITERAL(""), | |
| 600 | 640 |
| 601 FILE_PATH_LITERAL("foo.txt"), DownloadItem::TARGET_DISPOSITION_PROMPT, | 641 FILE_PATH_LITERAL("foo.txt"), DownloadItem::TARGET_DISPOSITION_PROMPT, |
| 602 | 642 |
| 603 EXPECT_UNCONFIRMED}, | 643 EXPECT_UNCONFIRMED}, |
| 604 #endif // !defined(OS_ANDROID) | |
| 605 | 644 |
| 606 {// 2: Forced Dangerous URL | 645 {// 2: Forced Dangerous URL |
| 607 FORCED, content::DOWNLOAD_DANGER_TYPE_DANGEROUS_URL, | 646 FORCED, content::DOWNLOAD_DANGER_TYPE_DANGEROUS_URL, |
| 608 DownloadFileType::NOT_DANGEROUS, "http://phishing.example.com/foo.txt", "", | 647 DownloadFileType::NOT_DANGEROUS, "http://phishing.example.com/foo.txt", |
| 609 FILE_PATH_LITERAL("forced-foo.txt"), | 648 "", FILE_PATH_LITERAL("forced-foo.txt"), |
| 610 | 649 |
| 611 FILE_PATH_LITERAL("forced-foo.txt"), | 650 FILE_PATH_LITERAL("forced-foo.txt"), |
| 612 DownloadItem::TARGET_DISPOSITION_OVERWRITE, | 651 DownloadItem::TARGET_DISPOSITION_OVERWRITE, |
| 613 | 652 |
| 614 EXPECT_UNCONFIRMED}, | 653 EXPECT_UNCONFIRMED}, |
| 615 | 654 |
| 616 {// 3: Automatic Dangerous URL + Dangerous file. Dangerous URL takes | 655 {// 3: Automatic Dangerous URL + Dangerous file. Dangerous URL takes |
| 617 // precedence. | 656 // precedence. |
| 618 AUTOMATIC, content::DOWNLOAD_DANGER_TYPE_DANGEROUS_URL, | 657 AUTOMATIC, content::DOWNLOAD_DANGER_TYPE_DANGEROUS_URL, |
| 619 DownloadFileType::NOT_DANGEROUS, "http://phishing.example.com/foo.html", | 658 DownloadFileType::NOT_DANGEROUS, "http://phishing.example.com/foo.html", |
| 620 "", FILE_PATH_LITERAL(""), | 659 "", FILE_PATH_LITERAL(""), |
| 621 | 660 |
| 622 FILE_PATH_LITERAL("foo.html"), DownloadItem::TARGET_DISPOSITION_OVERWRITE, | 661 FILE_PATH_LITERAL("foo.html"), |
| 662 DownloadItem::TARGET_DISPOSITION_OVERWRITE, |
| 623 | 663 |
| 624 EXPECT_UNCONFIRMED}, | 664 EXPECT_UNCONFIRMED}, |
| 625 | 665 |
| 626 #if !defined(OS_ANDROID) | 666 {// 4: Save As Dangerous URL + Dangerous file |
| 627 {// 4: Save As Dangerous URL + Dangerous file | 667 SAVE_AS, content::DOWNLOAD_DANGER_TYPE_DANGEROUS_URL, |
| 628 SAVE_AS, content::DOWNLOAD_DANGER_TYPE_DANGEROUS_URL, | 668 DownloadFileType::NOT_DANGEROUS, "http://phishing.example.com/foo.html", |
| 629 DownloadFileType::NOT_DANGEROUS, "http://phishing.example.com/foo.html", | 669 "", FILE_PATH_LITERAL(""), |
| 630 "", FILE_PATH_LITERAL(""), | |
| 631 | 670 |
| 632 FILE_PATH_LITERAL("foo.html"), DownloadItem::TARGET_DISPOSITION_PROMPT, | 671 FILE_PATH_LITERAL("foo.html"), DownloadItem::TARGET_DISPOSITION_PROMPT, |
| 633 | 672 |
| 634 EXPECT_UNCONFIRMED}, | 673 EXPECT_UNCONFIRMED}, |
| 635 #endif // !defined(OS_ANDROID) | |
| 636 | 674 |
| 637 {// 5: Forced Dangerous URL + Dangerous file | 675 {// 5: Forced Dangerous URL + Dangerous file |
| 638 FORCED, content::DOWNLOAD_DANGER_TYPE_DANGEROUS_URL, | 676 FORCED, content::DOWNLOAD_DANGER_TYPE_DANGEROUS_URL, |
| 639 DownloadFileType::NOT_DANGEROUS, "http://phishing.example.com/foo.html", | 677 DownloadFileType::NOT_DANGEROUS, "http://phishing.example.com/foo.html", |
| 640 "", FILE_PATH_LITERAL("forced-foo.html"), | 678 "", FILE_PATH_LITERAL("forced-foo.html"), |
| 641 | 679 |
| 642 FILE_PATH_LITERAL("forced-foo.html"), | 680 FILE_PATH_LITERAL("forced-foo.html"), |
| 643 DownloadItem::TARGET_DISPOSITION_OVERWRITE, | 681 DownloadItem::TARGET_DISPOSITION_OVERWRITE, |
| 644 | 682 |
| 645 EXPECT_UNCONFIRMED}, | 683 EXPECT_UNCONFIRMED}, |
| 646 }; | 684 }; |
| 647 | 685 |
| 648 ON_CALL(*delegate(), CheckDownloadUrl(_, _, _)) | 686 ON_CALL(*delegate(), CheckDownloadUrl(_, _, _)) |
| 649 .WillByDefault(WithArg<2>(ScheduleCallback( | 687 .WillByDefault(WithArg<2>(ScheduleCallback( |
| 650 content::DOWNLOAD_DANGER_TYPE_DANGEROUS_URL))); | 688 content::DOWNLOAD_DANGER_TYPE_DANGEROUS_URL))); |
| 651 RunTestCasesWithActiveItem(kSafeBrowsingTestCases, | 689 RunTestCasesWithActiveItem(kSafeBrowsingTestCases, |
| 652 arraysize(kSafeBrowsingTestCases)); | 690 arraysize(kSafeBrowsingTestCases)); |
| 653 } | 691 } |
| 654 | 692 |
| 655 // The SafeBrowsing check is performed early. Make sure that a download item | 693 // The SafeBrowsing check is performed early. Make sure that a download item |
| 656 // that has been marked as MAYBE_DANGEROUS_CONTENT behaves correctly. | 694 // that has been marked as MAYBE_DANGEROUS_CONTENT behaves correctly. |
| 657 TEST_F(DownloadTargetDeterminerTest, MaybeDangerousContent) { | 695 TEST_F(DownloadTargetDeterminerTest, MaybeDangerousContent) { |
| 658 const DownloadTestCase kSafeBrowsingTestCases[] = { | 696 const DownloadTestCase kSafeBrowsingTestCases[] = { |
| 659 {// 0: Automatic Maybe dangerous content | 697 {// 0: Automatic Maybe dangerous content |
| 660 AUTOMATIC, content::DOWNLOAD_DANGER_TYPE_MAYBE_DANGEROUS_CONTENT, | 698 AUTOMATIC, content::DOWNLOAD_DANGER_TYPE_MAYBE_DANGEROUS_CONTENT, |
| 661 DownloadFileType::ALLOW_ON_USER_GESTURE, | 699 DownloadFileType::ALLOW_ON_USER_GESTURE, |
| 662 "http://phishing.example.com/foo.crx", "", FILE_PATH_LITERAL(""), | 700 "http://phishing.example.com/foo.kindabad", "", FILE_PATH_LITERAL(""), |
| 663 | 701 |
| 664 FILE_PATH_LITERAL("foo.crx"), DownloadItem::TARGET_DISPOSITION_OVERWRITE, | 702 FILE_PATH_LITERAL("foo.kindabad"), |
| 703 DownloadItem::TARGET_DISPOSITION_OVERWRITE, EXPECT_UNCONFIRMED}, |
| 665 | 704 |
| 666 EXPECT_UNCONFIRMED}, | 705 {// 1: Automatic Maybe dangerous content with DANGEROUS type. |
| 706 AUTOMATIC, content::DOWNLOAD_DANGER_TYPE_MAYBE_DANGEROUS_CONTENT, |
| 707 DownloadFileType::DANGEROUS, "http://phishing.example.com/foo.bad", "", |
| 708 FILE_PATH_LITERAL(""), |
| 667 | 709 |
| 668 {// 1: Automatic Maybe dangerous content with DANGEROUS type. | 710 FILE_PATH_LITERAL("foo.bad"), DownloadItem::TARGET_DISPOSITION_OVERWRITE, |
| 669 AUTOMATIC, content::DOWNLOAD_DANGER_TYPE_MAYBE_DANGEROUS_CONTENT, | 711 EXPECT_UNCONFIRMED}, |
| 670 DownloadFileType::DANGEROUS, "http://phishing.example.com/foo.swf", "", | |
| 671 FILE_PATH_LITERAL(""), | |
| 672 | 712 |
| 673 FILE_PATH_LITERAL("foo.swf"), DownloadItem::TARGET_DISPOSITION_OVERWRITE, | 713 {// 2: Save As Maybe dangerous content |
| 714 SAVE_AS, content::DOWNLOAD_DANGER_TYPE_MAYBE_DANGEROUS_CONTENT, |
| 715 DownloadFileType::NOT_DANGEROUS, |
| 716 "http://phishing.example.com/foo.kindabad", "", FILE_PATH_LITERAL(""), |
| 674 | 717 |
| 675 EXPECT_UNCONFIRMED}, | 718 FILE_PATH_LITERAL("foo.kindabad"), |
| 719 DownloadItem::TARGET_DISPOSITION_PROMPT, |
| 676 | 720 |
| 677 #if !defined(OS_ANDROID) | 721 EXPECT_UNCONFIRMED}, |
| 678 {// 2: Save As Maybe dangerous content | |
| 679 SAVE_AS, content::DOWNLOAD_DANGER_TYPE_MAYBE_DANGEROUS_CONTENT, | |
| 680 DownloadFileType::NOT_DANGEROUS, "http://phishing.example.com/foo.crx", "", | |
| 681 FILE_PATH_LITERAL(""), | |
| 682 | 722 |
| 683 FILE_PATH_LITERAL("foo.crx"), DownloadItem::TARGET_DISPOSITION_PROMPT, | 723 {// 3: Forced Maybe dangerous content |
| 724 FORCED, content::DOWNLOAD_DANGER_TYPE_MAYBE_DANGEROUS_CONTENT, |
| 725 DownloadFileType::NOT_DANGEROUS, |
| 726 "http://phishing.example.com/foo.kindabad", "", |
| 727 FILE_PATH_LITERAL("forced-foo.kindabad"), |
| 684 | 728 |
| 685 EXPECT_UNCONFIRMED}, | 729 FILE_PATH_LITERAL("forced-foo.kindabad"), |
| 686 #endif // !defined(OS_ANDROID) | 730 DownloadItem::TARGET_DISPOSITION_OVERWRITE, |
| 687 | 731 |
| 688 {// 3: Forced Maybe dangerous content | 732 EXPECT_UNCONFIRMED}}; |
| 689 FORCED, content::DOWNLOAD_DANGER_TYPE_MAYBE_DANGEROUS_CONTENT, | |
| 690 DownloadFileType::NOT_DANGEROUS, "http://phishing.example.com/foo.crx", "", | |
| 691 FILE_PATH_LITERAL("forced-foo.crx"), | |
| 692 | |
| 693 FILE_PATH_LITERAL("forced-foo.crx"), | |
| 694 DownloadItem::TARGET_DISPOSITION_OVERWRITE, | |
| 695 | |
| 696 EXPECT_UNCONFIRMED}, | |
| 697 }; | |
| 698 | 733 |
| 699 // Test assumptions: | 734 // Test assumptions: |
| 700 ASSERT_EQ(DownloadFileType::ALLOW_ON_USER_GESTURE, | 735 ASSERT_EQ(DownloadFileType::ALLOW_ON_USER_GESTURE, |
| 701 Policies()->GetFileDangerLevel( | 736 safe_browsing::FileTypePolicies::GetInstance()->GetFileDangerLevel( |
| 702 base::FilePath(FILE_PATH_LITERAL("foo.crx")))); | 737 base::FilePath(FILE_PATH_LITERAL("foo.kindabad")))); |
| 703 ASSERT_EQ(DownloadFileType::DANGEROUS, | 738 ASSERT_EQ(DownloadFileType::DANGEROUS, |
| 704 Policies()->GetFileDangerLevel( | 739 safe_browsing::FileTypePolicies::GetInstance()->GetFileDangerLevel( |
| 705 base::FilePath(FILE_PATH_LITERAL("foo.swf")))); | 740 base::FilePath(FILE_PATH_LITERAL("foo.bad")))); |
| 706 | 741 |
| 707 ON_CALL(*delegate(), CheckDownloadUrl(_, _, _)) | 742 ON_CALL(*delegate(), CheckDownloadUrl(_, _, _)) |
| 708 .WillByDefault(WithArg<2>(ScheduleCallback( | 743 .WillByDefault(WithArg<2>(ScheduleCallback( |
| 709 content::DOWNLOAD_DANGER_TYPE_MAYBE_DANGEROUS_CONTENT))); | 744 content::DOWNLOAD_DANGER_TYPE_MAYBE_DANGEROUS_CONTENT))); |
| 710 RunTestCasesWithActiveItem(kSafeBrowsingTestCases, | 745 RunTestCasesWithActiveItem(kSafeBrowsingTestCases, |
| 711 arraysize(kSafeBrowsingTestCases)); | 746 arraysize(kSafeBrowsingTestCases)); |
| 712 } | 747 } |
| 713 | 748 |
| 714 #if !defined(OS_ANDROID) | |
| 715 // Test whether the last saved directory is used for 'Save As' downloads. | 749 // Test whether the last saved directory is used for 'Save As' downloads. |
| 716 TEST_F(DownloadTargetDeterminerTest, LastSavePath) { | 750 TEST_F(DownloadTargetDeterminerTest, LastSavePath) { |
| 717 const DownloadTestCase kLastSavePathTestCasesPre[] = { | 751 const DownloadTestCase kLastSavePathTestCasesPre[] = { |
| 718 {// 0: If the last save path is empty, then the default download directory | 752 {// 0: If the last save path is empty, then the default download directory |
| 719 // should be used. | 753 // should be used. |
| 720 SAVE_AS, content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, | 754 SAVE_AS, content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, |
| 721 DownloadFileType::NOT_DANGEROUS, "http://example.com/foo.txt", | 755 DownloadFileType::NOT_DANGEROUS, "http://example.com/foo.txt", |
| 722 "text/plain", FILE_PATH_LITERAL(""), | 756 "text/plain", FILE_PATH_LITERAL(""), |
| 723 | 757 |
| 724 FILE_PATH_LITERAL("foo.txt"), DownloadItem::TARGET_DISPOSITION_PROMPT, | 758 FILE_PATH_LITERAL("foo.txt"), DownloadItem::TARGET_DISPOSITION_PROMPT, |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 760 FILE_PATH_LITERAL("bar.txt"), DownloadItem::TARGET_DISPOSITION_PROMPT, | 794 FILE_PATH_LITERAL("bar.txt"), DownloadItem::TARGET_DISPOSITION_PROMPT, |
| 761 | 795 |
| 762 EXPECT_LOCAL_PATH}, | 796 EXPECT_LOCAL_PATH}, |
| 763 }; | 797 }; |
| 764 | 798 |
| 765 { | 799 { |
| 766 SCOPED_TRACE(testing::Message() | 800 SCOPED_TRACE(testing::Message() |
| 767 << "Running with default download path"); | 801 << "Running with default download path"); |
| 768 base::FilePath prompt_path = | 802 base::FilePath prompt_path = |
| 769 GetPathInDownloadDir(FILE_PATH_LITERAL("foo.txt")); | 803 GetPathInDownloadDir(FILE_PATH_LITERAL("foo.txt")); |
| 770 EXPECT_CALL(*delegate(), PromptUserForDownloadPath(_, prompt_path, _)); | 804 EXPECT_CALL(*delegate(), |
| 805 RequestConfirmation(_, prompt_path, |
| 806 DownloadConfirmationReason::SAVE_AS, _)); |
| 771 RunTestCasesWithActiveItem(kLastSavePathTestCasesPre, | 807 RunTestCasesWithActiveItem(kLastSavePathTestCasesPre, |
| 772 arraysize(kLastSavePathTestCasesPre)); | 808 arraysize(kLastSavePathTestCasesPre)); |
| 773 } | 809 } |
| 774 | 810 |
| 775 // Try with a non-empty last save path. | 811 // Try with a non-empty last save path. |
| 776 { | 812 { |
| 777 SCOPED_TRACE(testing::Message() | 813 SCOPED_TRACE(testing::Message() |
| 778 << "Running with local last_selected_directory"); | 814 << "Running with local last_selected_directory"); |
| 779 download_prefs()->SetSaveFilePath(test_download_dir().AppendASCII("foo")); | 815 download_prefs()->SetSaveFilePath(test_download_dir().AppendASCII("foo")); |
| 780 base::FilePath prompt_path = | 816 base::FilePath prompt_path = |
| 781 GetPathInDownloadDir(FILE_PATH_LITERAL("foo/foo.txt")); | 817 GetPathInDownloadDir(FILE_PATH_LITERAL("foo/foo.txt")); |
| 782 EXPECT_CALL(*delegate(), PromptUserForDownloadPath(_, prompt_path, _)); | 818 EXPECT_CALL(*delegate(), |
| 819 RequestConfirmation(_, prompt_path, |
| 820 DownloadConfirmationReason::SAVE_AS, _)); |
| 783 RunTestCasesWithActiveItem(kLastSavePathTestCasesPost, | 821 RunTestCasesWithActiveItem(kLastSavePathTestCasesPost, |
| 784 arraysize(kLastSavePathTestCasesPost)); | 822 arraysize(kLastSavePathTestCasesPost)); |
| 785 } | 823 } |
| 786 | 824 |
| 787 // And again, but this time use a virtual directory. | 825 // And again, but this time use a virtual directory. |
| 788 { | 826 { |
| 789 SCOPED_TRACE(testing::Message() | 827 SCOPED_TRACE(testing::Message() |
| 790 << "Running with virtual last_selected_directory"); | 828 << "Running with virtual last_selected_directory"); |
| 791 base::FilePath last_selected_dir = test_virtual_dir().AppendASCII("foo"); | 829 base::FilePath last_selected_dir = test_virtual_dir().AppendASCII("foo"); |
| 792 base::FilePath virtual_path = last_selected_dir.AppendASCII("foo.txt"); | 830 base::FilePath virtual_path = last_selected_dir.AppendASCII("foo.txt"); |
| 793 download_prefs()->SetSaveFilePath(last_selected_dir); | 831 download_prefs()->SetSaveFilePath(last_selected_dir); |
| 794 EXPECT_CALL(*delegate(), PromptUserForDownloadPath( | 832 EXPECT_CALL(*delegate(), |
| 795 _, last_selected_dir.AppendASCII("foo.txt"), _)); | 833 RequestConfirmation(_, last_selected_dir.AppendASCII("foo.txt"), |
| 834 DownloadConfirmationReason::SAVE_AS, _)); |
| 796 EXPECT_CALL(*delegate(), DetermineLocalPath(_, virtual_path, _)) | 835 EXPECT_CALL(*delegate(), DetermineLocalPath(_, virtual_path, _)) |
| 797 .WillOnce(WithArg<2>(ScheduleCallback( | 836 .WillOnce(WithArg<2>(ScheduleCallback( |
| 798 GetPathInDownloadDir(FILE_PATH_LITERAL("bar.txt"))))); | 837 GetPathInDownloadDir(FILE_PATH_LITERAL("bar.txt"))))); |
| 799 RunTestCasesWithActiveItem(kLastSavePathTestCasesVirtual, | 838 RunTestCasesWithActiveItem(kLastSavePathTestCasesVirtual, |
| 800 arraysize(kLastSavePathTestCasesVirtual)); | 839 arraysize(kLastSavePathTestCasesVirtual)); |
| 801 } | 840 } |
| 802 } | 841 } |
| 803 #endif // !defined(OS_ANDROID) | |
| 804 | 842 |
| 805 // These tests are run with the default downloads folder set to a virtual | 843 // These tests are run with the default downloads folder set to a virtual |
| 806 // directory. | 844 // directory. |
| 807 TEST_F(DownloadTargetDeterminerTest, DefaultVirtual) { | 845 TEST_F(DownloadTargetDeterminerTest, DefaultVirtual) { |
| 808 // The default download directory is the virutal path. | 846 // The default download directory is the virutal path. |
| 809 download_prefs()->SetDownloadPath(test_virtual_dir()); | 847 download_prefs()->SetDownloadPath(test_virtual_dir()); |
| 810 | 848 |
| 811 { | 849 { |
| 812 SCOPED_TRACE(testing::Message() << "Automatic Safe Download"); | 850 SCOPED_TRACE(testing::Message() << "Automatic Safe Download"); |
| 813 const DownloadTestCase kAutomaticDownloadToVirtualDir = { | 851 const DownloadTestCase kAutomaticDownloadToVirtualDir = { |
| 814 AUTOMATIC, | 852 AUTOMATIC, |
| 815 content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, | 853 content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, |
| 816 DownloadFileType::NOT_DANGEROUS, | 854 DownloadFileType::NOT_DANGEROUS, |
| 817 "http://example.com/foo.txt", | 855 "http://example.com/foo.txt", |
| 818 "text/plain", | 856 "text/plain", |
| 819 FILE_PATH_LITERAL(""), | 857 FILE_PATH_LITERAL(""), |
| 820 | 858 |
| 821 FILE_PATH_LITERAL("foo-local.txt"), | 859 FILE_PATH_LITERAL("foo-local.txt"), |
| 822 DownloadItem::TARGET_DISPOSITION_OVERWRITE, | 860 DownloadItem::TARGET_DISPOSITION_OVERWRITE, |
| 823 | 861 |
| 824 EXPECT_LOCAL_PATH}; | 862 EXPECT_LOCAL_PATH}; |
| 825 EXPECT_CALL(*delegate(), DetermineLocalPath(_, _, _)) | 863 EXPECT_CALL(*delegate(), DetermineLocalPath(_, _, _)) |
| 826 .WillOnce(WithArg<2>(ScheduleCallback( | 864 .WillOnce(WithArg<2>(ScheduleCallback( |
| 827 GetPathInDownloadDir(FILE_PATH_LITERAL("foo-local.txt"))))); | 865 GetPathInDownloadDir(FILE_PATH_LITERAL("foo-local.txt"))))); |
| 828 RunTestCasesWithActiveItem(&kAutomaticDownloadToVirtualDir, 1); | 866 RunTestCasesWithActiveItem(&kAutomaticDownloadToVirtualDir, 1); |
| 829 } | 867 } |
| 830 | 868 |
| 831 #if !defined(OS_ANDROID) | |
| 832 { | 869 { |
| 833 SCOPED_TRACE(testing::Message() << "Save As to virtual directory"); | 870 SCOPED_TRACE(testing::Message() << "Save As to virtual directory"); |
| 834 const DownloadTestCase kSaveAsToVirtualDir = { | 871 const DownloadTestCase kSaveAsToVirtualDir = { |
| 835 SAVE_AS, | 872 SAVE_AS, |
| 836 content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, | 873 content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, |
| 837 DownloadFileType::NOT_DANGEROUS, | 874 DownloadFileType::NOT_DANGEROUS, |
| 838 "http://example.com/bar.txt", | 875 "http://example.com/bar.txt", |
| 839 "text/plain", | 876 "text/plain", |
| 840 FILE_PATH_LITERAL(""), | 877 FILE_PATH_LITERAL(""), |
| 841 | 878 |
| 842 FILE_PATH_LITERAL("foo-local.txt"), | 879 FILE_PATH_LITERAL("foo-local.txt"), |
| 843 DownloadItem::TARGET_DISPOSITION_PROMPT, | 880 DownloadItem::TARGET_DISPOSITION_PROMPT, |
| 844 | 881 |
| 845 EXPECT_LOCAL_PATH}; | 882 EXPECT_LOCAL_PATH}; |
| 846 EXPECT_CALL(*delegate(), DetermineLocalPath(_, _, _)) | 883 EXPECT_CALL(*delegate(), DetermineLocalPath(_, _, _)) |
| 847 .WillOnce(WithArg<2>(ScheduleCallback( | 884 .WillOnce(WithArg<2>(ScheduleCallback( |
| 848 GetPathInDownloadDir(FILE_PATH_LITERAL("foo-local.txt"))))); | 885 GetPathInDownloadDir(FILE_PATH_LITERAL("foo-local.txt"))))); |
| 849 EXPECT_CALL(*delegate(), PromptUserForDownloadPath( | 886 EXPECT_CALL(*delegate(), RequestConfirmation( |
| 850 _, test_virtual_dir().AppendASCII("bar.txt"), _)) | 887 _, test_virtual_dir().AppendASCII("bar.txt"), |
| 851 .WillOnce(WithArg<2>(ScheduleCallback( | 888 DownloadConfirmationReason::SAVE_AS, _)) |
| 852 test_virtual_dir().AppendASCII("prompted.txt")))); | 889 .WillOnce(WithArg<3>( |
| 890 ScheduleCallback2(DownloadConfirmationResult::CONFIRMED, |
| 891 test_virtual_dir().AppendASCII("prompted.txt")))); |
| 853 RunTestCasesWithActiveItem(&kSaveAsToVirtualDir, 1); | 892 RunTestCasesWithActiveItem(&kSaveAsToVirtualDir, 1); |
| 854 } | 893 } |
| 855 | 894 |
| 856 { | 895 { |
| 857 SCOPED_TRACE(testing::Message() << "Save As to local directory"); | 896 SCOPED_TRACE(testing::Message() << "Save As to local directory"); |
| 858 const DownloadTestCase kSaveAsToLocalDir = { | 897 const DownloadTestCase kSaveAsToLocalDir = { |
| 859 SAVE_AS, | 898 SAVE_AS, |
| 860 content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, | 899 content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, |
| 861 DownloadFileType::NOT_DANGEROUS, | 900 DownloadFileType::NOT_DANGEROUS, |
| 862 "http://example.com/bar.txt", | 901 "http://example.com/bar.txt", |
| 863 "text/plain", | 902 "text/plain", |
| 864 FILE_PATH_LITERAL(""), | 903 FILE_PATH_LITERAL(""), |
| 865 | 904 |
| 866 FILE_PATH_LITERAL("foo-x.txt"), | 905 FILE_PATH_LITERAL("foo-x.txt"), |
| 867 DownloadItem::TARGET_DISPOSITION_PROMPT, | 906 DownloadItem::TARGET_DISPOSITION_PROMPT, |
| 868 | 907 |
| 869 EXPECT_CRDOWNLOAD}; | 908 EXPECT_CRDOWNLOAD}; |
| 870 EXPECT_CALL(*delegate(), PromptUserForDownloadPath( | 909 EXPECT_CALL(*delegate(), RequestConfirmation( |
| 871 _, test_virtual_dir().AppendASCII("bar.txt"), _)) | 910 _, test_virtual_dir().AppendASCII("bar.txt"), |
| 872 .WillOnce(WithArg<2>(ScheduleCallback( | 911 DownloadConfirmationReason::SAVE_AS, _)) |
| 912 .WillOnce(WithArg<3>(ScheduleCallback2( |
| 913 DownloadConfirmationResult::CONFIRMED, |
| 873 GetPathInDownloadDir(FILE_PATH_LITERAL("foo-x.txt"))))); | 914 GetPathInDownloadDir(FILE_PATH_LITERAL("foo-x.txt"))))); |
| 874 RunTestCasesWithActiveItem(&kSaveAsToLocalDir, 1); | 915 RunTestCasesWithActiveItem(&kSaveAsToLocalDir, 1); |
| 875 } | 916 } |
| 876 #endif // !defined(OS_ANDROID) | |
| 877 | 917 |
| 878 { | 918 { |
| 879 SCOPED_TRACE(testing::Message() << "Forced safe download"); | 919 SCOPED_TRACE(testing::Message() << "Forced safe download"); |
| 880 const DownloadTestCase kForcedSafe = { | 920 const DownloadTestCase kForcedSafe = { |
| 881 FORCED, | 921 FORCED, |
| 882 content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, | 922 content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, |
| 883 DownloadFileType::NOT_DANGEROUS, | 923 DownloadFileType::NOT_DANGEROUS, |
| 884 "http://example.com/foo.txt", | 924 "http://example.com/foo.txt", |
| 885 "", | 925 "", |
| 886 FILE_PATH_LITERAL("forced-foo.txt"), | 926 FILE_PATH_LITERAL("forced-foo.txt"), |
| 887 | 927 |
| 888 FILE_PATH_LITERAL("forced-foo.txt"), | 928 FILE_PATH_LITERAL("forced-foo.txt"), |
| 889 DownloadItem::TARGET_DISPOSITION_OVERWRITE, | 929 DownloadItem::TARGET_DISPOSITION_OVERWRITE, |
| 890 | 930 |
| 891 EXPECT_LOCAL_PATH}; | 931 EXPECT_LOCAL_PATH}; |
| 892 RunTestCasesWithActiveItem(&kForcedSafe, 1); | 932 RunTestCasesWithActiveItem(&kForcedSafe, 1); |
| 893 } | 933 } |
| 894 } | 934 } |
| 895 | 935 |
| 896 // Test that an inactive download will still get a virtual or local download | 936 // Test that an inactive download will still get a virtual or local download |
| 897 // path. | 937 // path. |
| 898 TEST_F(DownloadTargetDeterminerTest, InactiveDownload) { | 938 TEST_F(DownloadTargetDeterminerTest, InactiveDownload) { |
| 899 const DownloadTestCase kInactiveTestCases[] = { | 939 const DownloadTestCase kBaseTestCase = { |
| 900 {AUTOMATIC, content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, | 940 AUTOMATIC, |
| 901 DownloadFileType::NOT_DANGEROUS, "http://example.com/foo.txt", | 941 content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, |
| 902 "text/plain", FILE_PATH_LITERAL(""), | 942 DownloadFileType::NOT_DANGEROUS, |
| 943 "http://example.com/foo.txt", |
| 944 "text/plain", |
| 945 FILE_PATH_LITERAL(""), |
| 946 FILE_PATH_LITERAL("foo.txt"), |
| 947 DownloadItem::TARGET_DISPOSITION_OVERWRITE, |
| 948 EXPECT_CRDOWNLOAD}; |
| 903 | 949 |
| 904 FILE_PATH_LITERAL("foo.txt"), DownloadItem::TARGET_DISPOSITION_OVERWRITE, | 950 const struct { |
| 951 TestCaseType type; |
| 952 DownloadItem::TargetDisposition disposition; |
| 953 } kTestCases[] = {{AUTOMATIC, DownloadItem::TARGET_DISPOSITION_OVERWRITE}, |
| 954 {SAVE_AS, DownloadItem::TARGET_DISPOSITION_PROMPT}}; |
| 905 | 955 |
| 906 EXPECT_CRDOWNLOAD}, | 956 for (const auto& test_case : kTestCases) { |
| 907 | 957 DownloadTestCase download_test_case = kBaseTestCase; |
| 908 #if !defined(OS_ANDROID) | 958 download_test_case.test_type = test_case.type; |
| 909 {SAVE_AS, content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, | 959 download_test_case.expected_disposition = test_case.disposition; |
| 910 DownloadFileType::NOT_DANGEROUS, "http://example.com/foo.txt", | 960 std::unique_ptr<content::MockDownloadItem> item = |
| 911 "text/plain", FILE_PATH_LITERAL(""), | 961 CreateActiveDownloadItem(1, download_test_case); |
| 912 | |
| 913 FILE_PATH_LITERAL("foo.txt"), DownloadItem::TARGET_DISPOSITION_PROMPT, | |
| 914 | |
| 915 EXPECT_CRDOWNLOAD} | |
| 916 #endif // !defined(OS_ANDROID) | |
| 917 }; | |
| 918 | |
| 919 for (size_t i = 0; i < arraysize(kInactiveTestCases); ++i) { | |
| 920 SCOPED_TRACE(testing::Message() << "Running test case " << i); | |
| 921 const DownloadTestCase& test_case = kInactiveTestCases[i]; | |
| 922 std::unique_ptr<content::MockDownloadItem> item( | |
| 923 CreateActiveDownloadItem(i, test_case)); | |
| 924 EXPECT_CALL(*item.get(), GetState()) | 962 EXPECT_CALL(*item.get(), GetState()) |
| 925 .WillRepeatedly(Return(content::DownloadItem::CANCELLED)); | 963 .WillRepeatedly(Return(content::DownloadItem::CANCELLED)); |
| 926 | 964 |
| 927 EXPECT_CALL(*delegate(), PromptUserForDownloadPath(_, _, _)).Times(0); | 965 EXPECT_CALL(*delegate(), RequestConfirmation(_, _, _, _)).Times(0); |
| 928 EXPECT_CALL(*delegate(), NotifyExtensions(_, _, _)).Times(0); | 966 EXPECT_CALL(*delegate(), NotifyExtensions(_, _, _)).Times(0); |
| 929 EXPECT_CALL(*delegate(), ReserveVirtualPath(_, _, _, _, _)).Times(0); | 967 EXPECT_CALL(*delegate(), ReserveVirtualPath(_, _, _, _, _)).Times(0); |
| 930 EXPECT_CALL(*delegate(), DetermineLocalPath(_, _, _)).Times(1); | 968 EXPECT_CALL(*delegate(), DetermineLocalPath(_, _, _)).Times(1); |
| 931 RunTestCase(test_case, base::FilePath(), item.get()); | 969 |
| 970 // Each test case has a non-empty target path. The test will fail if the |
| 971 // target determination doesn't result in that path. |
| 972 RunTestCase(download_test_case, base::FilePath(), item.get()); |
| 932 } | 973 } |
| 933 } | 974 } |
| 934 | 975 |
| 935 // If the reserved path could not be verified, then the user should see a | 976 // If the reserved path could not be verified, then the user should see a |
| 936 // prompt. | 977 // prompt. |
| 937 TEST_F(DownloadTargetDeterminerTest, ReservationFailed) { | 978 TEST_F(DownloadTargetDeterminerTest, ReservationFailed_Confirmation) { |
| 938 const DownloadTestCase kReservationFailedCases[] = { | 979 DownloadTestCase download_test_case = { |
| 939 {// 0: Automatic download. Since the reservation fails, the disposition of | 980 // 0: Automatic download. Since the reservation fails, the disposition of |
| 940 // the target is to prompt, but the returned path is used. | 981 // the target is to prompt, but the returned path is used. |
| 941 AUTOMATIC, content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, | 982 AUTOMATIC, |
| 942 DownloadFileType::NOT_DANGEROUS, "http://example.com/foo.txt", | 983 content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, |
| 943 "text/plain", FILE_PATH_LITERAL(""), | 984 DownloadFileType::NOT_DANGEROUS, |
| 985 "http://example.com/foo.txt", |
| 986 "text/plain", |
| 987 FILE_PATH_LITERAL(""), |
| 944 | 988 |
| 945 FILE_PATH_LITERAL("bar.txt"), DownloadItem::TARGET_DISPOSITION_PROMPT, | 989 FILE_PATH_LITERAL("bar.txt"), |
| 990 DownloadItem::TARGET_DISPOSITION_PROMPT, |
| 946 | 991 |
| 947 EXPECT_CRDOWNLOAD}, | 992 EXPECT_CRDOWNLOAD}; |
| 948 }; | |
| 949 | 993 |
| 950 // Setup ReserveVirtualPath() to fail. | 994 struct TestCase { |
| 951 ON_CALL(*delegate(), ReserveVirtualPath(_, _, _, _, _)) | 995 PathValidationResult result; |
| 952 .WillByDefault(WithArg<4>(ScheduleCallback2( | 996 DownloadConfirmationReason expected_confirmation_reason; |
| 953 GetPathInDownloadDir(FILE_PATH_LITERAL("bar.txt")), false))); | 997 } kTestCases[] = { |
| 954 RunTestCasesWithActiveItem(kReservationFailedCases, | 998 {PathValidationResult::SUCCESS, DownloadConfirmationReason::NONE}, |
| 955 arraysize(kReservationFailedCases)); | 999 {PathValidationResult::CONFLICT, |
| 1000 DownloadConfirmationReason::TARGET_CONFLICT}, |
| 1001 {PathValidationResult::PATH_NOT_WRITABLE, |
| 1002 DownloadConfirmationReason::TARGET_PATH_NOT_WRITEABLE}, |
| 1003 {PathValidationResult::NAME_TOO_LONG, |
| 1004 DownloadConfirmationReason::NAME_TOO_LONG}}; |
| 1005 |
| 1006 for (const auto& test_case : kTestCases) { |
| 1007 SCOPED_TRACE(::testing::Message() << "PathValidationResult " |
| 1008 << static_cast<int>(test_case.result)); |
| 1009 ON_CALL(*delegate(), ReserveVirtualPath(_, _, _, _, _)) |
| 1010 .WillByDefault(WithArg<4>(ScheduleCallback2( |
| 1011 test_case.result, |
| 1012 GetPathInDownloadDir(FILE_PATH_LITERAL("bar.txt"))))); |
| 1013 if (test_case.expected_confirmation_reason != |
| 1014 DownloadConfirmationReason::NONE) { |
| 1015 EXPECT_CALL( |
| 1016 *delegate(), |
| 1017 RequestConfirmation(_, _, test_case.expected_confirmation_reason, _)); |
| 1018 } else { |
| 1019 EXPECT_CALL(*delegate(), RequestConfirmation(_, _, _, _)).Times(0); |
| 1020 } |
| 1021 |
| 1022 download_test_case.expected_disposition = |
| 1023 test_case.expected_confirmation_reason == |
| 1024 DownloadConfirmationReason::NONE |
| 1025 ? content::DownloadItem::TARGET_DISPOSITION_OVERWRITE |
| 1026 : content::DownloadItem::TARGET_DISPOSITION_PROMPT; |
| 1027 std::unique_ptr<content::MockDownloadItem> item = CreateActiveDownloadItem( |
| 1028 static_cast<int>(test_case.result), download_test_case); |
| 1029 RunTestCase(download_test_case, base::FilePath(), item.get()); |
| 1030 } |
| 956 } | 1031 } |
| 957 | 1032 |
| 958 // If the local path could not be determined, the download should be cancelled. | 1033 // If the local path could not be determined, the download should be cancelled. |
| 959 TEST_F(DownloadTargetDeterminerTest, LocalPathFailed) { | 1034 TEST_F(DownloadTargetDeterminerTest, LocalPathFailed) { |
| 960 const DownloadTestCase kLocalPathFailedCases[] = { | 1035 const DownloadTestCase kLocalPathFailedCases[] = { |
| 961 {// 0: Automatic download. | 1036 {// 0: Automatic download. |
| 962 AUTOMATIC, content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, | 1037 AUTOMATIC, content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, |
| 963 DownloadFileType::NOT_DANGEROUS, "http://example.com/foo.txt", | 1038 DownloadFileType::NOT_DANGEROUS, "http://example.com/foo.txt", |
| 964 "text/plain", FILE_PATH_LITERAL(""), | 1039 "text/plain", FILE_PATH_LITERAL(""), |
| 965 | 1040 |
| (...skipping 10 matching lines...) Expand all Loading... |
| 976 .WillOnce(WithArg<2>(ScheduleCallback(base::FilePath()))); | 1051 .WillOnce(WithArg<2>(ScheduleCallback(base::FilePath()))); |
| 977 RunTestCasesWithActiveItem(kLocalPathFailedCases, | 1052 RunTestCasesWithActiveItem(kLocalPathFailedCases, |
| 978 arraysize(kLocalPathFailedCases)); | 1053 arraysize(kLocalPathFailedCases)); |
| 979 } | 1054 } |
| 980 | 1055 |
| 981 // Downloads that have a danger level of ALLOW_ON_USER_GESTURE should be marked | 1056 // Downloads that have a danger level of ALLOW_ON_USER_GESTURE should be marked |
| 982 // as safe depending on whether there was a user gesture associated with the | 1057 // as safe depending on whether there was a user gesture associated with the |
| 983 // download and whether the referrer was visited prior to today. | 1058 // download and whether the referrer was visited prior to today. |
| 984 TEST_F(DownloadTargetDeterminerTest, VisitedReferrer) { | 1059 TEST_F(DownloadTargetDeterminerTest, VisitedReferrer) { |
| 985 const DownloadTestCase kVisitedReferrerCases[] = { | 1060 const DownloadTestCase kVisitedReferrerCases[] = { |
| 986 // http://visited.example.com/ is added to the history as a visit that | 1061 // http://visited.example.com/ is added to the history as a visit that |
| 987 // happened prior to today. | 1062 // happened prior to today. |
| 988 {// 0: Safe download due to visiting referrer before. | 1063 {// 0: Safe download due to visiting referrer before. |
| 989 AUTOMATIC, content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, | 1064 AUTOMATIC, content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, |
| 990 DownloadFileType::NOT_DANGEROUS, "http://visited.example.com/foo.crx", | 1065 DownloadFileType::NOT_DANGEROUS, |
| 991 "application/xml", FILE_PATH_LITERAL(""), | 1066 "http://visited.example.com/foo.kindabad", "application/xml", |
| 1067 FILE_PATH_LITERAL(""), |
| 992 | 1068 |
| 993 FILE_PATH_LITERAL("foo.crx"), DownloadItem::TARGET_DISPOSITION_OVERWRITE, | 1069 FILE_PATH_LITERAL("foo.kindabad"), |
| 1070 DownloadItem::TARGET_DISPOSITION_OVERWRITE, |
| 994 | 1071 |
| 995 EXPECT_CRDOWNLOAD}, | 1072 EXPECT_CRDOWNLOAD}, |
| 996 | 1073 |
| 997 {// 1: Dangerous due to not having visited referrer before. | 1074 {// 1: Dangerous due to not having visited referrer before. |
| 998 AUTOMATIC, content::DOWNLOAD_DANGER_TYPE_DANGEROUS_FILE, | 1075 AUTOMATIC, content::DOWNLOAD_DANGER_TYPE_DANGEROUS_FILE, |
| 999 DownloadFileType::ALLOW_ON_USER_GESTURE, | 1076 DownloadFileType::ALLOW_ON_USER_GESTURE, |
| 1000 "http://not-visited.example.com/foo.crx", "application/xml", | 1077 "http://not-visited.example.com/foo.kindabad", "application/xml", |
| 1001 FILE_PATH_LITERAL(""), | 1078 FILE_PATH_LITERAL(""), |
| 1002 | 1079 |
| 1003 FILE_PATH_LITERAL("foo.crx"), DownloadItem::TARGET_DISPOSITION_OVERWRITE, | 1080 FILE_PATH_LITERAL("foo.kindabad"), |
| 1081 DownloadItem::TARGET_DISPOSITION_OVERWRITE, |
| 1004 | 1082 |
| 1005 EXPECT_UNCONFIRMED}, | 1083 EXPECT_UNCONFIRMED}, |
| 1006 | 1084 |
| 1007 #if !defined(OS_ANDROID) | 1085 {// 2: Safe because the user is being prompted. |
| 1008 {// 2: Safe because the user is being prompted. | 1086 SAVE_AS, content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, |
| 1009 SAVE_AS, content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, | 1087 DownloadFileType::NOT_DANGEROUS, |
| 1010 DownloadFileType::NOT_DANGEROUS, "http://not-visited.example.com/foo.crx", | 1088 "http://not-visited.example.com/foo.kindabad", "application/xml", |
| 1011 "application/xml", FILE_PATH_LITERAL(""), | 1089 FILE_PATH_LITERAL(""), |
| 1012 | 1090 |
| 1013 FILE_PATH_LITERAL("foo.crx"), DownloadItem::TARGET_DISPOSITION_PROMPT, | 1091 FILE_PATH_LITERAL("foo.kindabad"), |
| 1092 DownloadItem::TARGET_DISPOSITION_PROMPT, |
| 1014 | 1093 |
| 1015 EXPECT_CRDOWNLOAD}, | 1094 EXPECT_CRDOWNLOAD}, |
| 1016 #endif // !defined(OS_ANDROID) | |
| 1017 | 1095 |
| 1018 {// 3: Safe because of forced path. | 1096 {// 3: Safe because of forced path. |
| 1019 FORCED, content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, | 1097 FORCED, content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, |
| 1020 DownloadFileType::NOT_DANGEROUS, "http://not-visited.example.com/foo.crx", | 1098 DownloadFileType::NOT_DANGEROUS, |
| 1021 "application/xml", FILE_PATH_LITERAL("foo.crx"), | 1099 "http://not-visited.example.com/foo.kindabad", "application/xml", |
| 1100 FILE_PATH_LITERAL("foo.kindabad"), |
| 1022 | 1101 |
| 1023 FILE_PATH_LITERAL("foo.crx"), DownloadItem::TARGET_DISPOSITION_OVERWRITE, | 1102 FILE_PATH_LITERAL("foo.kindabad"), |
| 1103 DownloadItem::TARGET_DISPOSITION_OVERWRITE, |
| 1024 | 1104 |
| 1025 EXPECT_LOCAL_PATH}, | 1105 EXPECT_LOCAL_PATH}, |
| 1026 }; | 1106 }; |
| 1027 | 1107 |
| 1028 // This test assumes that the danger level of .crx files is | 1108 // This test assumes that the danger level of .kindabad files is |
| 1029 // ALLOW_ON_USER_GESTURE. | 1109 // ALLOW_ON_USER_GESTURE. |
| 1030 ASSERT_EQ(DownloadFileType::ALLOW_ON_USER_GESTURE, | 1110 ASSERT_EQ(DownloadFileType::ALLOW_ON_USER_GESTURE, |
| 1031 Policies()->GetFileDangerLevel( | 1111 safe_browsing::FileTypePolicies::GetInstance()->GetFileDangerLevel( |
| 1032 base::FilePath(FILE_PATH_LITERAL("foo.crx")))); | 1112 base::FilePath(FILE_PATH_LITERAL("foo.kindabad")))); |
| 1033 | 1113 |
| 1034 // First the history service must exist. | 1114 // First the history service must exist. |
| 1035 ASSERT_TRUE(profile()->CreateHistoryService(false, false)); | 1115 ASSERT_TRUE(profile()->CreateHistoryService(false, false)); |
| 1036 | 1116 |
| 1037 GURL url("http://visited.example.com/visited-link.html"); | 1117 GURL url("http://visited.example.com/visited-link.html"); |
| 1038 // The time of visit is picked to be several seconds prior to the most recent | 1118 // The time of visit is picked to be several seconds prior to the most recent |
| 1039 // midnight. | 1119 // midnight. |
| 1040 base::Time time_of_visit( | 1120 base::Time time_of_visit( |
| 1041 base::Time::Now().LocalMidnight() - base::TimeDelta::FromSeconds(10)); | 1121 base::Time::Now().LocalMidnight() - base::TimeDelta::FromSeconds(10)); |
| 1042 history::HistoryService* history_service = | 1122 history::HistoryService* history_service = |
| (...skipping 17 matching lines...) Expand all Loading... |
| 1060 | 1140 |
| 1061 FILE_PATH_LITERAL("foo.txt"), | 1141 FILE_PATH_LITERAL("foo.txt"), |
| 1062 DownloadItem::TARGET_DISPOSITION_OVERWRITE, | 1142 DownloadItem::TARGET_DISPOSITION_OVERWRITE, |
| 1063 | 1143 |
| 1064 EXPECT_CRDOWNLOAD}; | 1144 EXPECT_CRDOWNLOAD}; |
| 1065 | 1145 |
| 1066 const DownloadTestCase kAllowOnUserGesture = { | 1146 const DownloadTestCase kAllowOnUserGesture = { |
| 1067 AUTOMATIC, | 1147 AUTOMATIC, |
| 1068 content::DOWNLOAD_DANGER_TYPE_DANGEROUS_FILE, | 1148 content::DOWNLOAD_DANGER_TYPE_DANGEROUS_FILE, |
| 1069 DownloadFileType::ALLOW_ON_USER_GESTURE, | 1149 DownloadFileType::ALLOW_ON_USER_GESTURE, |
| 1070 "http://example.com/foo.crx", | 1150 "http://example.com/foo.kindabad", |
| 1071 "application/octet-stream", | 1151 "application/octet-stream", |
| 1072 FILE_PATH_LITERAL(""), | 1152 FILE_PATH_LITERAL(""), |
| 1073 | 1153 |
| 1074 FILE_PATH_LITERAL("foo.crx"), | 1154 FILE_PATH_LITERAL("foo.kindabad"), |
| 1075 DownloadItem::TARGET_DISPOSITION_OVERWRITE, | 1155 DownloadItem::TARGET_DISPOSITION_OVERWRITE, |
| 1076 | 1156 |
| 1077 EXPECT_UNCONFIRMED}; | 1157 EXPECT_UNCONFIRMED}; |
| 1078 | 1158 |
| 1079 const DownloadTestCase kDangerousFile = { | 1159 const DownloadTestCase kDangerousFile = { |
| 1080 AUTOMATIC, | 1160 AUTOMATIC, |
| 1081 content::DOWNLOAD_DANGER_TYPE_DANGEROUS_FILE, | 1161 content::DOWNLOAD_DANGER_TYPE_DANGEROUS_FILE, |
| 1082 DownloadFileType::DANGEROUS, | 1162 DownloadFileType::DANGEROUS, |
| 1083 "http://example.com/foo.swf", | 1163 "http://example.com/foo.bad", |
| 1084 "application/octet-stream", | 1164 "application/octet-stream", |
| 1085 FILE_PATH_LITERAL(""), | 1165 FILE_PATH_LITERAL(""), |
| 1086 | 1166 |
| 1087 FILE_PATH_LITERAL("foo.swf"), | 1167 FILE_PATH_LITERAL("foo.bad"), |
| 1088 DownloadItem::TARGET_DISPOSITION_OVERWRITE, | 1168 DownloadItem::TARGET_DISPOSITION_OVERWRITE, |
| 1089 | 1169 |
| 1090 EXPECT_UNCONFIRMED}; | 1170 EXPECT_UNCONFIRMED}; |
| 1091 | 1171 |
| 1092 const struct { | 1172 const struct { |
| 1093 ui::PageTransition page_transition; | 1173 ui::PageTransition page_transition; |
| 1094 content::DownloadDangerType expected_danger_type; | 1174 content::DownloadDangerType expected_danger_type; |
| 1095 const DownloadTestCase& template_download_test_case; | 1175 const DownloadTestCase& template_download_test_case; |
| 1096 } kTestCases[] = { | 1176 } kTestCases[] = { |
| 1097 {// Benign file type. Results in a danger type of NOT_DANGEROUS. Page | 1177 {// Benign file type. Results in a danger type of NOT_DANGEROUS. Page |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1130 content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, kAllowOnUserGesture}, | 1210 content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, kAllowOnUserGesture}, |
| 1131 | 1211 |
| 1132 {// File type is DANGEROUS. PageTransition is irrelevant. | 1212 {// File type is DANGEROUS. PageTransition is irrelevant. |
| 1133 static_cast<ui::PageTransition>(ui::PAGE_TRANSITION_TYPED | | 1213 static_cast<ui::PageTransition>(ui::PAGE_TRANSITION_TYPED | |
| 1134 ui::PAGE_TRANSITION_FROM_ADDRESS_BAR), | 1214 ui::PAGE_TRANSITION_FROM_ADDRESS_BAR), |
| 1135 content::DOWNLOAD_DANGER_TYPE_DANGEROUS_FILE, kDangerousFile}, | 1215 content::DOWNLOAD_DANGER_TYPE_DANGEROUS_FILE, kDangerousFile}, |
| 1136 }; | 1216 }; |
| 1137 | 1217 |
| 1138 // Test assumptions: | 1218 // Test assumptions: |
| 1139 ASSERT_EQ(DownloadFileType::ALLOW_ON_USER_GESTURE, | 1219 ASSERT_EQ(DownloadFileType::ALLOW_ON_USER_GESTURE, |
| 1140 Policies()->GetFileDangerLevel( | 1220 safe_browsing::FileTypePolicies::GetInstance()->GetFileDangerLevel( |
| 1141 base::FilePath(FILE_PATH_LITERAL("foo.crx")))); | 1221 base::FilePath(FILE_PATH_LITERAL("foo.kindabad")))); |
| 1142 ASSERT_EQ(DownloadFileType::DANGEROUS, | 1222 ASSERT_EQ(DownloadFileType::DANGEROUS, |
| 1143 Policies()->GetFileDangerLevel( | 1223 safe_browsing::FileTypePolicies::GetInstance()->GetFileDangerLevel( |
| 1144 base::FilePath(FILE_PATH_LITERAL("foo.swf")))); | 1224 base::FilePath(FILE_PATH_LITERAL("foo.bad")))); |
| 1145 ASSERT_EQ(DownloadFileType::NOT_DANGEROUS, | 1225 ASSERT_EQ(DownloadFileType::NOT_DANGEROUS, |
| 1146 Policies()->GetFileDangerLevel( | 1226 safe_browsing::FileTypePolicies::GetInstance()->GetFileDangerLevel( |
| 1147 base::FilePath(FILE_PATH_LITERAL("foo.txt")))); | 1227 base::FilePath(FILE_PATH_LITERAL("foo.txt")))); |
| 1148 | 1228 |
| 1149 for (const auto& test_case : kTestCases) { | 1229 for (const auto& test_case : kTestCases) { |
| 1150 // The template download test case describes what to expect if the page | 1230 // The template download test case describes what to expect if the page |
| 1151 // transition was LINK. If the expectation is that the page transition type | 1231 // transition was LINK. If the expectation is that the page transition type |
| 1152 // causes the download to be considered safe, then download_test_case needs | 1232 // causes the download to be considered safe, then download_test_case needs |
| 1153 // to be adjusted accordingly. | 1233 // to be adjusted accordingly. |
| 1154 DownloadTestCase download_test_case = test_case.template_download_test_case; | 1234 DownloadTestCase download_test_case = test_case.template_download_test_case; |
| 1155 download_test_case.expected_danger_type = test_case.expected_danger_type; | 1235 download_test_case.expected_danger_type = test_case.expected_danger_type; |
| 1156 if (test_case.expected_danger_type == | 1236 if (test_case.expected_danger_type == |
| 1157 content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS) { | 1237 content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS) { |
| 1158 download_test_case.expected_danger_level = | 1238 download_test_case.expected_danger_level = |
| 1159 DownloadFileType::NOT_DANGEROUS; | 1239 DownloadFileType::NOT_DANGEROUS; |
| 1160 download_test_case.expected_intermediate = EXPECT_CRDOWNLOAD; | 1240 download_test_case.expected_intermediate = EXPECT_CRDOWNLOAD; |
| 1161 } | 1241 } |
| 1162 | 1242 |
| 1163 std::unique_ptr<content::MockDownloadItem> item( | 1243 std::unique_ptr<content::MockDownloadItem> item = |
| 1164 CreateActiveDownloadItem(1, download_test_case)); | 1244 CreateActiveDownloadItem(1, download_test_case); |
| 1165 EXPECT_CALL(*item, GetTransitionType()) | 1245 EXPECT_CALL(*item, GetTransitionType()) |
| 1166 .WillRepeatedly(Return(test_case.page_transition)); | 1246 .WillRepeatedly(Return(test_case.page_transition)); |
| 1167 RunTestCase(download_test_case, base::FilePath(), item.get()); | 1247 RunTestCase(download_test_case, base::FilePath(), item.get()); |
| 1168 } | 1248 } |
| 1169 } | 1249 } |
| 1170 | 1250 |
| 1171 #if !defined(OS_ANDROID) | |
| 1172 // These test cases are run with "Prompt for download" user preference set to | 1251 // These test cases are run with "Prompt for download" user preference set to |
| 1173 // true. | 1252 // true. |
| 1174 TEST_F(DownloadTargetDeterminerTest, PromptAlways) { | 1253 TEST_F(DownloadTargetDeterminerTest, PromptAlways_SafeAutomatic) { |
| 1175 const DownloadTestCase kPromptingTestCases[] = { | 1254 const DownloadTestCase kSafeAutomatic = { |
| 1176 {// 0: Safe Automatic - Should prompt because of "Prompt for download" | 1255 // 0: Safe Automatic - Should prompt because of "Prompt for download" |
| 1177 // preference setting. | 1256 // preference setting. |
| 1178 AUTOMATIC, content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, | 1257 AUTOMATIC, |
| 1179 DownloadFileType::NOT_DANGEROUS, "http://example.com/foo.txt", | 1258 content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, |
| 1180 "text/plain", FILE_PATH_LITERAL(""), | 1259 DownloadFileType::NOT_DANGEROUS, |
| 1260 "http://example.com/automatic.txt", |
| 1261 "text/plain", |
| 1262 FILE_PATH_LITERAL(""), |
| 1181 | 1263 |
| 1182 FILE_PATH_LITERAL("foo.txt"), DownloadItem::TARGET_DISPOSITION_PROMPT, | 1264 FILE_PATH_LITERAL("automatic.txt"), |
| 1265 DownloadItem::TARGET_DISPOSITION_PROMPT, |
| 1183 | 1266 |
| 1184 EXPECT_CRDOWNLOAD}, | 1267 EXPECT_CRDOWNLOAD}; |
| 1185 | 1268 |
| 1186 {// 1: Safe Forced - Shouldn't prompt. | 1269 SetPromptForDownload(true); |
| 1187 FORCED, content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, | 1270 EXPECT_CALL(*delegate(), |
| 1188 DownloadFileType::NOT_DANGEROUS, "http://example.com/foo.txt", | 1271 RequestConfirmation( |
| 1189 "text/plain", FILE_PATH_LITERAL("foo.txt"), | 1272 _, GetPathInDownloadDir(FILE_PATH_LITERAL("automatic.txt")), |
| 1273 DownloadConfirmationReason::PREFERENCE, _)); |
| 1274 RunTestCasesWithActiveItem(&kSafeAutomatic, 1); |
| 1275 } |
| 1190 | 1276 |
| 1191 FILE_PATH_LITERAL("foo.txt"), DownloadItem::TARGET_DISPOSITION_OVERWRITE, | 1277 TEST_F(DownloadTargetDeterminerTest, PromptAlways_SafeSaveAs) { |
| 1278 const DownloadTestCase kSafeSaveAs = { |
| 1279 // 1: Safe Save As - Should prompt because of "Save as" invocation. |
| 1280 SAVE_AS, |
| 1281 content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, |
| 1282 DownloadFileType::NOT_DANGEROUS, |
| 1283 "http://example.com/save-as.txt", |
| 1284 "text/plain", |
| 1285 FILE_PATH_LITERAL(""), |
| 1192 | 1286 |
| 1193 EXPECT_LOCAL_PATH}, | 1287 FILE_PATH_LITERAL("save-as.txt"), |
| 1288 DownloadItem::TARGET_DISPOSITION_PROMPT, |
| 1194 | 1289 |
| 1195 {// 2: Automatic - The filename extension is marked as one that we will | 1290 EXPECT_CRDOWNLOAD}; |
| 1196 // open automatically. Shouldn't prompt. | |
| 1197 AUTOMATIC, content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, | |
| 1198 DownloadFileType::NOT_DANGEROUS, "http://example.com/foo.dummy", "", | |
| 1199 FILE_PATH_LITERAL(""), | |
| 1200 | 1291 |
| 1201 FILE_PATH_LITERAL("foo.dummy"), | 1292 SetPromptForDownload(true); |
| 1202 DownloadItem::TARGET_DISPOSITION_OVERWRITE, | 1293 EXPECT_CALL(*delegate(), |
| 1294 RequestConfirmation( |
| 1295 _, GetPathInDownloadDir(FILE_PATH_LITERAL("save-as.txt")), |
| 1296 DownloadConfirmationReason::SAVE_AS, _)); |
| 1297 RunTestCasesWithActiveItem(&kSafeSaveAs, 1); |
| 1298 } |
| 1203 | 1299 |
| 1204 EXPECT_CRDOWNLOAD}, | 1300 TEST_F(DownloadTargetDeterminerTest, PromptAlways_SafeForced) { |
| 1205 }; | 1301 const DownloadTestCase kSafeForced = { |
| 1302 // 2: Safe Forced - Shouldn't prompt. |
| 1303 FORCED, |
| 1304 content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, |
| 1305 DownloadFileType::NOT_DANGEROUS, |
| 1306 "http://example.com/foo.txt", |
| 1307 "text/plain", |
| 1308 FILE_PATH_LITERAL("foo.txt"), |
| 1206 | 1309 |
| 1310 FILE_PATH_LITERAL("foo.txt"), |
| 1311 DownloadItem::TARGET_DISPOSITION_OVERWRITE, |
| 1312 |
| 1313 EXPECT_LOCAL_PATH}; |
| 1314 |
| 1315 SetPromptForDownload(true); |
| 1316 RunTestCasesWithActiveItem(&kSafeForced, 1); |
| 1317 } |
| 1318 |
| 1319 TEST_F(DownloadTargetDeterminerTest, PromptAlways_AutoOpen) { |
| 1320 const DownloadTestCase kAutoOpen = { |
| 1321 // 3: Automatic - The filename extension is marked as one that we will |
| 1322 // open automatically. Shouldn't prompt. |
| 1323 AUTOMATIC, |
| 1324 content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, |
| 1325 DownloadFileType::NOT_DANGEROUS, |
| 1326 "http://example.com/foo.dummy", |
| 1327 "", |
| 1328 FILE_PATH_LITERAL(""), |
| 1329 |
| 1330 FILE_PATH_LITERAL("foo.dummy"), |
| 1331 DownloadItem::TARGET_DISPOSITION_OVERWRITE, |
| 1332 |
| 1333 EXPECT_CRDOWNLOAD}; |
| 1207 SetPromptForDownload(true); | 1334 SetPromptForDownload(true); |
| 1208 EnableAutoOpenBasedOnExtension( | 1335 EnableAutoOpenBasedOnExtension( |
| 1209 base::FilePath(FILE_PATH_LITERAL("dummy.dummy"))); | 1336 base::FilePath(FILE_PATH_LITERAL("dummy.dummy"))); |
| 1210 RunTestCasesWithActiveItem(kPromptingTestCases, | 1337 RunTestCasesWithActiveItem(&kAutoOpen, 1); |
| 1211 arraysize(kPromptingTestCases)); | |
| 1212 } | 1338 } |
| 1213 #endif // !defined(OS_ANDROID) | 1339 |
| 1340 // If an embedder responds to a RequestConfirmation with a new path and a |
| 1341 // CONTINUE_WITHOUT_CONFIRMATION, then we shouldn't consider the file as safe. |
| 1342 TEST_F(DownloadTargetDeterminerTest, ContinueWithoutConfirmation_SaveAs) { |
| 1343 const DownloadTestCase kTestCase = { |
| 1344 SAVE_AS, |
| 1345 content::DOWNLOAD_DANGER_TYPE_DANGEROUS_FILE, |
| 1346 DownloadFileType::ALLOW_ON_USER_GESTURE, |
| 1347 "http://example.com/save-as.kindabad", |
| 1348 "text/plain", |
| 1349 FILE_PATH_LITERAL(""), |
| 1350 |
| 1351 FILE_PATH_LITERAL("foo.kindabad"), |
| 1352 DownloadItem::TARGET_DISPOSITION_OVERWRITE, |
| 1353 |
| 1354 EXPECT_UNCONFIRMED}; |
| 1355 |
| 1356 EXPECT_CALL( |
| 1357 *delegate(), |
| 1358 RequestConfirmation( |
| 1359 _, GetPathInDownloadDir(FILE_PATH_LITERAL("save-as.kindabad")), |
| 1360 DownloadConfirmationReason::SAVE_AS, _)) |
| 1361 .WillOnce(WithArg<3>(ScheduleCallback2( |
| 1362 DownloadConfirmationResult::CONTINUE_WITHOUT_CONFIRMATION, |
| 1363 GetPathInDownloadDir(FILE_PATH_LITERAL("foo.kindabad"))))); |
| 1364 RunTestCasesWithActiveItem(&kTestCase, 1); |
| 1365 } |
| 1366 |
| 1367 // Same as ContinueWithoutConfirmation_SaveAs, but the embedder response |
| 1368 // indicates that the user confirmed the path. Hence the danger level of the |
| 1369 // download and the disposition should be updated accordingly. |
| 1370 TEST_F(DownloadTargetDeterminerTest, ContinueWithConfirmation_SaveAs) { |
| 1371 const DownloadTestCase kTestCase = { |
| 1372 SAVE_AS, |
| 1373 content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, |
| 1374 DownloadFileType::NOT_DANGEROUS, |
| 1375 "http://example.com/save-as.kindabad", |
| 1376 "text/plain", |
| 1377 FILE_PATH_LITERAL(""), |
| 1378 |
| 1379 FILE_PATH_LITERAL("foo.kindabad"), |
| 1380 DownloadItem::TARGET_DISPOSITION_PROMPT, |
| 1381 |
| 1382 EXPECT_CRDOWNLOAD}; |
| 1383 |
| 1384 EXPECT_CALL( |
| 1385 *delegate(), |
| 1386 RequestConfirmation( |
| 1387 _, GetPathInDownloadDir(FILE_PATH_LITERAL("save-as.kindabad")), |
| 1388 DownloadConfirmationReason::SAVE_AS, _)) |
| 1389 .WillOnce(WithArg<3>(ScheduleCallback2( |
| 1390 DownloadConfirmationResult::CONFIRMED, |
| 1391 GetPathInDownloadDir(FILE_PATH_LITERAL("foo.kindabad"))))); |
| 1392 RunTestCasesWithActiveItem(&kTestCase, 1); |
| 1393 } |
| 1214 | 1394 |
| 1215 #if BUILDFLAG(ENABLE_EXTENSIONS) | 1395 #if BUILDFLAG(ENABLE_EXTENSIONS) |
| 1216 // These test cases are run with "Prompt for download" user preference set to | 1396 // These test cases are run with "Prompt for download" user preference set to |
| 1217 // true. Automatic extension downloads shouldn't cause prompting. | 1397 // true. Automatic extension downloads shouldn't cause prompting. |
| 1218 // Android doesn't support extensions. | 1398 // Android doesn't support extensions. |
| 1219 TEST_F(DownloadTargetDeterminerTest, PromptAlways_Extension) { | 1399 TEST_F(DownloadTargetDeterminerTest, PromptAlways_Extension) { |
| 1220 const DownloadTestCase kPromptingTestCases[] = { | 1400 const DownloadTestCase kPromptingTestCases[] = { |
| 1221 {// 0: Automatic Browser Extension download. - Shouldn't prompt for browser | 1401 {// 0: Automatic Browser Extension download. - Shouldn't prompt for |
| 1222 // extension downloads even if "Prompt for download" preference is set. | 1402 // browser extension downloads even if "Prompt for download" |
| 1223 AUTOMATIC, content::DOWNLOAD_DANGER_TYPE_DANGEROUS_FILE, | 1403 // preference is set. |
| 1224 DownloadFileType::ALLOW_ON_USER_GESTURE, "http://example.com/foo.crx", | 1404 AUTOMATIC, content::DOWNLOAD_DANGER_TYPE_DANGEROUS_FILE, |
| 1225 extensions::Extension::kMimeType, FILE_PATH_LITERAL(""), | 1405 DownloadFileType::ALLOW_ON_USER_GESTURE, |
| 1406 "http://example.com/foo.kindabad", extensions::Extension::kMimeType, |
| 1407 FILE_PATH_LITERAL(""), |
| 1226 | 1408 |
| 1227 FILE_PATH_LITERAL("foo.crx"), DownloadItem::TARGET_DISPOSITION_OVERWRITE, | 1409 FILE_PATH_LITERAL("foo.kindabad"), |
| 1410 DownloadItem::TARGET_DISPOSITION_OVERWRITE, |
| 1228 | 1411 |
| 1229 EXPECT_UNCONFIRMED}, | 1412 EXPECT_UNCONFIRMED}, |
| 1230 | 1413 |
| 1231 #if defined(OS_WIN) | 1414 {// 1: Automatic User Script - Shouldn't prompt for user script downloads |
| 1232 {// 1: Automatic User Script - Shouldn't prompt for user script downloads | 1415 // even if "Prompt for download" preference is set. |
| 1233 // even if "Prompt for download" preference is set. ".js" files are | 1416 AUTOMATIC, content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, |
| 1234 // considered dangerous on Windows. | 1417 DownloadFileType::NOT_DANGEROUS, "http://example.com/foo.user.js", "", |
| 1235 AUTOMATIC, content::DOWNLOAD_DANGER_TYPE_DANGEROUS_FILE, | 1418 FILE_PATH_LITERAL(""), |
| 1236 DownloadFileType::ALLOW_ON_USER_GESTURE, "http://example.com/foo.user.js",
"", | |
| 1237 FILE_PATH_LITERAL(""), | |
| 1238 | 1419 |
| 1239 FILE_PATH_LITERAL("foo.user.js"), | 1420 FILE_PATH_LITERAL("foo.user.js"), |
| 1240 DownloadItem::TARGET_DISPOSITION_OVERWRITE, | 1421 DownloadItem::TARGET_DISPOSITION_OVERWRITE, |
| 1241 | 1422 |
| 1242 EXPECT_UNCONFIRMED}, | 1423 EXPECT_CRDOWNLOAD}, |
| 1243 #else | |
| 1244 {// 1: Automatic User Script - Shouldn't prompt for user script downloads | |
| 1245 // even if "Prompt for download" preference is set. | |
| 1246 AUTOMATIC, content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, | |
| 1247 DownloadFileType::NOT_DANGEROUS, "http://example.com/foo.user.js", "", | |
| 1248 FILE_PATH_LITERAL(""), | |
| 1249 | |
| 1250 FILE_PATH_LITERAL("foo.user.js"), | |
| 1251 DownloadItem::TARGET_DISPOSITION_OVERWRITE, | |
| 1252 | |
| 1253 EXPECT_CRDOWNLOAD}, | |
| 1254 #endif | |
| 1255 }; | 1424 }; |
| 1256 | 1425 |
| 1257 SetPromptForDownload(true); | 1426 SetPromptForDownload(true); |
| 1258 RunTestCasesWithActiveItem(kPromptingTestCases, | 1427 RunTestCasesWithActiveItem(kPromptingTestCases, |
| 1259 arraysize(kPromptingTestCases)); | 1428 arraysize(kPromptingTestCases)); |
| 1260 } | 1429 } |
| 1261 #endif // BUILDFLAG(ENABLE_EXTENSIONS) | 1430 #endif // BUILDFLAG(ENABLE_EXTENSIONS) |
| 1262 | 1431 |
| 1263 // If the download path is managed, then we don't show any prompts. | 1432 // If the download path is managed, then we don't show any prompts. |
| 1264 // Note that if the download path is managed, then PromptForDownload() is false. | 1433 // Note that if the download path is managed, then PromptForDownload() is false. |
| (...skipping 21 matching lines...) Expand all Loading... |
| 1286 SetManagedDownloadPath(test_download_dir()); | 1455 SetManagedDownloadPath(test_download_dir()); |
| 1287 ASSERT_TRUE(download_prefs()->IsDownloadPathManaged()); | 1456 ASSERT_TRUE(download_prefs()->IsDownloadPathManaged()); |
| 1288 RunTestCasesWithActiveItem(kManagedPathTestCases, | 1457 RunTestCasesWithActiveItem(kManagedPathTestCases, |
| 1289 arraysize(kManagedPathTestCases)); | 1458 arraysize(kManagedPathTestCases)); |
| 1290 } | 1459 } |
| 1291 | 1460 |
| 1292 // Test basic functionality supporting extensions that want to override download | 1461 // Test basic functionality supporting extensions that want to override download |
| 1293 // filenames. | 1462 // filenames. |
| 1294 TEST_F(DownloadTargetDeterminerTest, NotifyExtensionsSafe) { | 1463 TEST_F(DownloadTargetDeterminerTest, NotifyExtensionsSafe) { |
| 1295 const DownloadTestCase kNotifyExtensionsTestCases[] = { | 1464 const DownloadTestCase kNotifyExtensionsTestCases[] = { |
| 1296 {// 0: Automatic Safe | 1465 {// 0: Automatic Safe |
| 1297 AUTOMATIC, content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, | 1466 AUTOMATIC, content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, |
| 1298 DownloadFileType::NOT_DANGEROUS, "http://example.com/foo.txt", | 1467 DownloadFileType::NOT_DANGEROUS, "http://example.com/foo.txt", |
| 1299 "text/plain", FILE_PATH_LITERAL(""), | 1468 "text/plain", FILE_PATH_LITERAL(""), |
| 1300 | 1469 |
| 1301 FILE_PATH_LITERAL("overridden/foo.txt"), | 1470 FILE_PATH_LITERAL("overridden/foo.txt"), |
| 1302 DownloadItem::TARGET_DISPOSITION_OVERWRITE, | 1471 DownloadItem::TARGET_DISPOSITION_OVERWRITE, |
| 1303 | 1472 |
| 1304 EXPECT_CRDOWNLOAD}, | 1473 EXPECT_CRDOWNLOAD}, |
| 1305 | 1474 |
| 1306 #if !defined(OS_ANDROID) | 1475 {// 1: Save_As Safe |
| 1307 {// 1: Save_As Safe | 1476 SAVE_AS, content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, |
| 1308 SAVE_AS, content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, | 1477 DownloadFileType::NOT_DANGEROUS, "http://example.com/foo.txt", |
| 1309 DownloadFileType::NOT_DANGEROUS, "http://example.com/foo.txt", | 1478 "text/plain", FILE_PATH_LITERAL(""), |
| 1310 "text/plain", FILE_PATH_LITERAL(""), | |
| 1311 | 1479 |
| 1312 FILE_PATH_LITERAL("overridden/foo.txt"), | 1480 FILE_PATH_LITERAL("overridden/foo.txt"), |
| 1313 DownloadItem::TARGET_DISPOSITION_PROMPT, | 1481 DownloadItem::TARGET_DISPOSITION_PROMPT, |
| 1314 | 1482 |
| 1315 EXPECT_CRDOWNLOAD}, | 1483 EXPECT_CRDOWNLOAD}, |
| 1316 #endif // !defined(OS_ANDROID) | |
| 1317 | 1484 |
| 1318 {// 2: Automatic Dangerous | 1485 {// 2: Automatic Dangerous |
| 1319 AUTOMATIC, content::DOWNLOAD_DANGER_TYPE_DANGEROUS_FILE, | 1486 AUTOMATIC, content::DOWNLOAD_DANGER_TYPE_DANGEROUS_FILE, |
| 1320 DownloadFileType::ALLOW_ON_USER_GESTURE, "http://example.com/foo.crx", "", | 1487 DownloadFileType::ALLOW_ON_USER_GESTURE, |
| 1321 FILE_PATH_LITERAL(""), | 1488 "http://example.com/foo.kindabad", "", FILE_PATH_LITERAL(""), |
| 1322 | 1489 |
| 1323 FILE_PATH_LITERAL("overridden/foo.crx"), | 1490 FILE_PATH_LITERAL("overridden/foo.kindabad"), |
| 1324 DownloadItem::TARGET_DISPOSITION_OVERWRITE, | 1491 DownloadItem::TARGET_DISPOSITION_OVERWRITE, |
| 1325 | 1492 |
| 1326 EXPECT_UNCONFIRMED}, | 1493 EXPECT_UNCONFIRMED}, |
| 1327 | 1494 |
| 1328 {// 3: Forced Safe | 1495 {// 3: Forced Safe |
| 1329 FORCED, content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, | 1496 FORCED, content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, |
| 1330 DownloadFileType::NOT_DANGEROUS, "http://example.com/foo.txt", "", | 1497 DownloadFileType::NOT_DANGEROUS, "http://example.com/foo.txt", "", |
| 1331 FILE_PATH_LITERAL("forced-foo.txt"), | 1498 FILE_PATH_LITERAL("forced-foo.txt"), |
| 1332 | 1499 |
| 1333 FILE_PATH_LITERAL("forced-foo.txt"), | 1500 FILE_PATH_LITERAL("forced-foo.txt"), |
| 1334 DownloadItem::TARGET_DISPOSITION_OVERWRITE, | 1501 DownloadItem::TARGET_DISPOSITION_OVERWRITE, |
| 1335 | 1502 |
| 1336 EXPECT_LOCAL_PATH}, | 1503 EXPECT_LOCAL_PATH}, |
| 1337 }; | 1504 }; |
| 1338 | 1505 |
| 1339 ON_CALL(*delegate(), NotifyExtensions(_, _, _)) | 1506 ON_CALL(*delegate(), NotifyExtensions(_, _, _)) |
| 1340 .WillByDefault(Invoke(&NotifyExtensionsOverridePath)); | 1507 .WillByDefault(Invoke(&NotifyExtensionsOverridePath)); |
| 1341 RunTestCasesWithActiveItem(kNotifyExtensionsTestCases, | 1508 RunTestCasesWithActiveItem(kNotifyExtensionsTestCases, |
| 1342 arraysize(kNotifyExtensionsTestCases)); | 1509 arraysize(kNotifyExtensionsTestCases)); |
| 1343 } | 1510 } |
| 1344 | 1511 |
| 1345 // Test that filenames provided by extensions are passed into SafeBrowsing | 1512 // Test that filenames provided by extensions are passed into SafeBrowsing |
| 1346 // checks and dangerous download checks. | 1513 // checks and dangerous download checks. |
| 1347 TEST_F(DownloadTargetDeterminerTest, NotifyExtensionsUnsafe) { | 1514 TEST_F(DownloadTargetDeterminerTest, NotifyExtensionsUnsafe) { |
| 1348 const DownloadTestCase kNotHandledBySafeBrowsing = { | 1515 const DownloadTestCase kNotHandledBySafeBrowsing = { |
| 1349 AUTOMATIC, | 1516 AUTOMATIC, |
| 1350 content::DOWNLOAD_DANGER_TYPE_DANGEROUS_FILE, | 1517 content::DOWNLOAD_DANGER_TYPE_DANGEROUS_FILE, |
| 1351 DownloadFileType::ALLOW_ON_USER_GESTURE, | 1518 DownloadFileType::ALLOW_ON_USER_GESTURE, |
| 1352 "http://example.com/foo.crx.remove", | 1519 "http://example.com/foo.kindabad.remove", |
| 1353 "text/plain", | 1520 "text/plain", |
| 1354 FILE_PATH_LITERAL(""), | 1521 FILE_PATH_LITERAL(""), |
| 1355 | 1522 |
| 1356 FILE_PATH_LITERAL("overridden/foo.crx"), | 1523 FILE_PATH_LITERAL("overridden/foo.kindabad"), |
| 1357 DownloadItem::TARGET_DISPOSITION_OVERWRITE, | 1524 DownloadItem::TARGET_DISPOSITION_OVERWRITE, |
| 1358 | 1525 |
| 1359 EXPECT_UNCONFIRMED}; | 1526 EXPECT_UNCONFIRMED}; |
| 1360 | 1527 |
| 1361 const DownloadTestCase kHandledBySafeBrowsing = { | 1528 const DownloadTestCase kHandledBySafeBrowsing = { |
| 1362 AUTOMATIC, | 1529 AUTOMATIC, |
| 1363 content::DOWNLOAD_DANGER_TYPE_MAYBE_DANGEROUS_CONTENT, | 1530 content::DOWNLOAD_DANGER_TYPE_MAYBE_DANGEROUS_CONTENT, |
| 1364 DownloadFileType::ALLOW_ON_USER_GESTURE, | 1531 DownloadFileType::ALLOW_ON_USER_GESTURE, |
| 1365 "http://example.com/foo.crx.remove", | 1532 "http://example.com/foo.kindabad.remove", |
| 1366 "text/plain", | 1533 "text/plain", |
| 1367 FILE_PATH_LITERAL(""), | 1534 FILE_PATH_LITERAL(""), |
| 1368 | 1535 |
| 1369 FILE_PATH_LITERAL("overridden/foo.crx"), | 1536 FILE_PATH_LITERAL("overridden/foo.kindabad"), |
| 1370 DownloadItem::TARGET_DISPOSITION_OVERWRITE, | 1537 DownloadItem::TARGET_DISPOSITION_OVERWRITE, |
| 1371 | 1538 |
| 1372 EXPECT_UNCONFIRMED}; | 1539 EXPECT_UNCONFIRMED}; |
| 1373 | 1540 |
| 1374 ON_CALL(*delegate(), NotifyExtensions(_, _, _)) | 1541 ON_CALL(*delegate(), NotifyExtensions(_, _, _)) |
| 1375 .WillByDefault(Invoke(&NotifyExtensionsOverridePath)); | 1542 .WillByDefault(Invoke(&NotifyExtensionsOverridePath)); |
| 1376 RunTestCasesWithActiveItem(&kNotHandledBySafeBrowsing, 1); | 1543 RunTestCasesWithActiveItem(&kNotHandledBySafeBrowsing, 1); |
| 1377 | 1544 |
| 1378 ON_CALL(*delegate(), CheckDownloadUrl(_, _, _)) | 1545 ON_CALL(*delegate(), CheckDownloadUrl(_, _, _)) |
| 1379 .WillByDefault(WithArg<2>(ScheduleCallback( | 1546 .WillByDefault(WithArg<2>(ScheduleCallback( |
| (...skipping 11 matching lines...) Expand all Loading... |
| 1391 "http://example.com/foo.txt", | 1558 "http://example.com/foo.txt", |
| 1392 "text/plain", | 1559 "text/plain", |
| 1393 FILE_PATH_LITERAL(""), | 1560 FILE_PATH_LITERAL(""), |
| 1394 | 1561 |
| 1395 FILE_PATH_LITERAL("overridden/foo.txt"), | 1562 FILE_PATH_LITERAL("overridden/foo.txt"), |
| 1396 DownloadItem::TARGET_DISPOSITION_OVERWRITE, | 1563 DownloadItem::TARGET_DISPOSITION_OVERWRITE, |
| 1397 | 1564 |
| 1398 EXPECT_CRDOWNLOAD}; | 1565 EXPECT_CRDOWNLOAD}; |
| 1399 | 1566 |
| 1400 const DownloadTestCase& test_case = kNotifyExtensionsTestCase; | 1567 const DownloadTestCase& test_case = kNotifyExtensionsTestCase; |
| 1401 std::unique_ptr<content::MockDownloadItem> item( | 1568 std::unique_ptr<content::MockDownloadItem> item = |
| 1402 CreateActiveDownloadItem(0, test_case)); | 1569 CreateActiveDownloadItem(0, test_case); |
| 1403 base::FilePath overridden_path(FILE_PATH_LITERAL("overridden/foo.txt")); | 1570 base::FilePath overridden_path(FILE_PATH_LITERAL("overridden/foo.txt")); |
| 1404 base::FilePath full_overridden_path = | 1571 base::FilePath full_overridden_path = |
| 1405 GetPathInDownloadDir(overridden_path.value()); | 1572 GetPathInDownloadDir(overridden_path.value()); |
| 1406 | 1573 |
| 1407 // First case: An extension sets the conflict_action to OVERWRITE. | 1574 // First case: An extension sets the conflict_action to OVERWRITE. |
| 1408 EXPECT_CALL(*delegate(), NotifyExtensions(_, _, _)) | 1575 EXPECT_CALL(*delegate(), NotifyExtensions(_, _, _)) |
| 1409 .WillOnce(WithArg<2>( | 1576 .WillOnce(WithArg<2>( |
| 1410 ScheduleCallback2(overridden_path, | 1577 ScheduleCallback2(overridden_path, |
| 1411 DownloadPathReservationTracker::OVERWRITE))); | 1578 DownloadPathReservationTracker::OVERWRITE))); |
| 1412 EXPECT_CALL(*delegate(), ReserveVirtualPath( | 1579 EXPECT_CALL(*delegate(), |
| 1413 _, full_overridden_path, true, DownloadPathReservationTracker::OVERWRITE, | 1580 ReserveVirtualPath(_, full_overridden_path, true, |
| 1414 _)).WillOnce(WithArg<4>( | 1581 DownloadPathReservationTracker::OVERWRITE, _)) |
| 1415 ScheduleCallback2(full_overridden_path, true))); | 1582 .WillOnce(WithArg<4>(ScheduleCallback2(PathValidationResult::SUCCESS, |
| 1583 full_overridden_path))); |
| 1416 | 1584 |
| 1417 RunTestCase(test_case, base::FilePath(), item.get()); | 1585 RunTestCase(test_case, base::FilePath(), item.get()); |
| 1418 | 1586 |
| 1419 // Second case: An extension sets the conflict_action to PROMPT. | 1587 // Second case: An extension sets the conflict_action to PROMPT. |
| 1420 EXPECT_CALL(*delegate(), NotifyExtensions(_, _, _)) | 1588 EXPECT_CALL(*delegate(), NotifyExtensions(_, _, _)) |
| 1421 .WillOnce(WithArg<2>( | 1589 .WillOnce(WithArg<2>( |
| 1422 ScheduleCallback2(overridden_path, | 1590 ScheduleCallback2(overridden_path, |
| 1423 DownloadPathReservationTracker::PROMPT))); | 1591 DownloadPathReservationTracker::PROMPT))); |
| 1424 EXPECT_CALL(*delegate(), ReserveVirtualPath( | 1592 EXPECT_CALL(*delegate(), |
| 1425 _, full_overridden_path, true, DownloadPathReservationTracker::PROMPT, _)) | 1593 ReserveVirtualPath(_, full_overridden_path, true, |
| 1426 .WillOnce(WithArg<4>( | 1594 DownloadPathReservationTracker::PROMPT, _)) |
| 1427 ScheduleCallback2(full_overridden_path, true))); | 1595 .WillOnce(WithArg<4>(ScheduleCallback2(PathValidationResult::SUCCESS, |
| 1596 full_overridden_path))); |
| 1428 RunTestCase(test_case, base::FilePath(), item.get()); | 1597 RunTestCase(test_case, base::FilePath(), item.get()); |
| 1429 } | 1598 } |
| 1430 | 1599 |
| 1431 #if !defined(OS_ANDROID) | |
| 1432 // Test that relative paths returned by extensions are always relative to the | 1600 // Test that relative paths returned by extensions are always relative to the |
| 1433 // default downloads path. | 1601 // default downloads path. |
| 1434 TEST_F(DownloadTargetDeterminerTest, NotifyExtensionsDefaultPath) { | 1602 TEST_F(DownloadTargetDeterminerTest, NotifyExtensionsDefaultPath) { |
| 1435 const DownloadTestCase kNotifyExtensionsTestCase = { | 1603 const DownloadTestCase kNotifyExtensionsTestCase = { |
| 1436 SAVE_AS, | 1604 AUTOMATIC, |
| 1437 content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, | 1605 content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, |
| 1438 DownloadFileType::NOT_DANGEROUS, | 1606 DownloadFileType::NOT_DANGEROUS, |
| 1439 "http://example.com/foo.txt", | 1607 "http://example.com/foo.txt", |
| 1440 "text/plain", | 1608 "text/plain", |
| 1441 FILE_PATH_LITERAL(""), | 1609 FILE_PATH_LITERAL(""), |
| 1442 | 1610 |
| 1443 FILE_PATH_LITERAL("overridden/foo.txt"), | 1611 FILE_PATH_LITERAL("overridden/foo.txt"), |
| 1444 DownloadItem::TARGET_DISPOSITION_PROMPT, | 1612 DownloadItem::TARGET_DISPOSITION_OVERWRITE, |
| 1445 | 1613 |
| 1446 EXPECT_CRDOWNLOAD}; | 1614 EXPECT_CRDOWNLOAD}; |
| 1447 | 1615 |
| 1448 const DownloadTestCase& test_case = kNotifyExtensionsTestCase; | 1616 const DownloadTestCase& test_case = kNotifyExtensionsTestCase; |
| 1449 std::unique_ptr<content::MockDownloadItem> item( | 1617 std::unique_ptr<content::MockDownloadItem> item = |
| 1450 CreateActiveDownloadItem(0, test_case)); | 1618 CreateActiveDownloadItem(0, test_case); |
| 1451 base::FilePath overridden_path(FILE_PATH_LITERAL("overridden/foo.txt")); | 1619 base::FilePath overridden_path(FILE_PATH_LITERAL("overridden/foo.txt")); |
| 1452 base::FilePath full_overridden_path = | 1620 base::FilePath full_overridden_path = |
| 1453 GetPathInDownloadDir(overridden_path.value()); | 1621 GetPathInDownloadDir(overridden_path.value()); |
| 1454 | 1622 |
| 1455 download_prefs()->SetSaveFilePath(GetPathInDownloadDir( | 1623 download_prefs()->SetSaveFilePath(GetPathInDownloadDir( |
| 1456 FILE_PATH_LITERAL("last_selected"))); | 1624 FILE_PATH_LITERAL("last_selected"))); |
| 1457 | 1625 |
| 1458 EXPECT_CALL(*delegate(), NotifyExtensions(_, _, _)) | 1626 EXPECT_CALL(*delegate(), NotifyExtensions(_, _, _)) |
| 1459 .WillOnce(WithArg<2>( | 1627 .WillOnce(WithArg<2>( |
| 1460 ScheduleCallback2(overridden_path, | 1628 ScheduleCallback2(overridden_path, |
| 1461 DownloadPathReservationTracker::UNIQUIFY))); | 1629 DownloadPathReservationTracker::UNIQUIFY))); |
| 1462 EXPECT_CALL(*delegate(), | |
| 1463 PromptUserForDownloadPath(_, full_overridden_path, _)) | |
| 1464 .WillOnce(WithArg<2>( | |
| 1465 ScheduleCallback(full_overridden_path))); | |
| 1466 RunTestCase(test_case, base::FilePath(), item.get()); | 1630 RunTestCase(test_case, base::FilePath(), item.get()); |
| 1467 } | 1631 } |
| 1468 #endif // !defined(OS_ANDROID) | |
| 1469 | 1632 |
| 1470 TEST_F(DownloadTargetDeterminerTest, InitialVirtualPathUnsafe) { | 1633 TEST_F(DownloadTargetDeterminerTest, InitialVirtualPathUnsafe) { |
| 1471 const base::FilePath::CharType* kInitialPath = | 1634 const base::FilePath::CharType* kInitialPath = |
| 1472 FILE_PATH_LITERAL("some_path/bar.html"); | 1635 FILE_PATH_LITERAL("some_path/bar.html"); |
| 1473 | 1636 |
| 1474 const DownloadTestCase kInitialPathTestCase = { | 1637 const DownloadTestCase kInitialPathTestCase = { |
| 1475 // 0: Save As Save. The path generated based on the DownloadItem is safe, | 1638 // 0: Save As Save. The path generated based on the DownloadItem is safe, |
| 1476 // but the initial path is unsafe. However, the download is not considered | 1639 // but the initial path is unsafe. However, the download is not considered |
| 1477 // dangerous since the user has been prompted. | 1640 // dangerous since the user has been prompted. |
| 1478 SAVE_AS, | 1641 SAVE_AS, |
| 1479 content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, | 1642 content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, |
| 1480 DownloadFileType::NOT_DANGEROUS, | 1643 DownloadFileType::NOT_DANGEROUS, |
| 1481 "http://example.com/foo.txt", | 1644 "http://example.com/foo.txt", |
| 1482 "text/plain", | 1645 "text/plain", |
| 1483 FILE_PATH_LITERAL(""), | 1646 FILE_PATH_LITERAL(""), |
| 1484 | 1647 |
| 1485 kInitialPath, | 1648 kInitialPath, |
| 1486 DownloadItem::TARGET_DISPOSITION_PROMPT, | 1649 DownloadItem::TARGET_DISPOSITION_PROMPT, |
| 1487 | 1650 |
| 1488 EXPECT_CRDOWNLOAD}; | 1651 EXPECT_CRDOWNLOAD}; |
| 1489 | 1652 |
| 1490 const DownloadTestCase& test_case = kInitialPathTestCase; | 1653 const DownloadTestCase& test_case = kInitialPathTestCase; |
| 1491 std::unique_ptr<content::MockDownloadItem> item( | 1654 std::unique_ptr<content::MockDownloadItem> item = |
| 1492 CreateActiveDownloadItem(1, test_case)); | 1655 CreateActiveDownloadItem(1, test_case); |
| 1493 EXPECT_CALL(*item, GetLastReason()) | 1656 EXPECT_CALL(*item, GetLastReason()) |
| 1494 .WillRepeatedly(Return( | 1657 .WillRepeatedly(Return( |
| 1495 content::DOWNLOAD_INTERRUPT_REASON_NETWORK_FAILED)); | 1658 content::DOWNLOAD_INTERRUPT_REASON_NETWORK_FAILED)); |
| 1496 EXPECT_CALL(*item, GetTargetDisposition()) | 1659 EXPECT_CALL(*item, GetTargetDisposition()) |
| 1497 .WillRepeatedly(Return(DownloadItem::TARGET_DISPOSITION_PROMPT)); | 1660 .WillRepeatedly(Return(DownloadItem::TARGET_DISPOSITION_PROMPT)); |
| 1498 RunTestCase(test_case, GetPathInDownloadDir(kInitialPath), item.get()); | 1661 RunTestCase(test_case, GetPathInDownloadDir(kInitialPath), item.get()); |
| 1499 } | 1662 } |
| 1500 | 1663 |
| 1501 // Prompting behavior for resumed downloads is based on the last interrupt | 1664 // Prompting behavior for resumed downloads is based on the last interrupt |
| 1502 // reason. If the reason indicates that the target path may not be suitable for | 1665 // reason. If the reason indicates that the target path may not be suitable for |
| 1503 // the download (ACCESS_DENIED, NO_SPACE, etc..), then the user should be | 1666 // the download (ACCESS_DENIED, NO_SPACE, etc..), then the user should be |
| 1504 // prompted, and not otherwise. These test cases shouldn't result in prompting | 1667 // prompted, and not otherwise. These test cases shouldn't result in prompting |
| 1505 // since the error is set to NETWORK_FAILED. | 1668 // since the error is set to NETWORK_FAILED. |
| 1506 TEST_F(DownloadTargetDeterminerTest, ResumedNoPrompt) { | 1669 TEST_F(DownloadTargetDeterminerTest, ResumedNoPrompt) { |
| 1507 // All test cases run with GetPathInDownloadDir(kInitialPath) as the inital | 1670 // All test cases run with GetPathInDownloadDir(kInitialPath) as the inital |
| 1508 // path. | 1671 // path. |
| 1509 const base::FilePath::CharType* kInitialPath = | 1672 const base::FilePath::CharType* kInitialPath = |
| 1510 FILE_PATH_LITERAL("some_path/bar.txt"); | 1673 FILE_PATH_LITERAL("some_path/bar.txt"); |
| 1511 | 1674 |
| 1512 const DownloadTestCase kResumedTestCases[] = { | 1675 const DownloadTestCase kResumedTestCases[] = { |
| 1513 {// 0: Automatic Safe: Initial path is ignored since the user has not been | 1676 {// 0: Automatic Safe: Initial path is ignored since the user has not been |
| 1514 // prompted before. | 1677 // prompted before. |
| 1515 AUTOMATIC, content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, | 1678 AUTOMATIC, content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, |
| 1516 DownloadFileType::NOT_DANGEROUS, "http://example.com/foo.txt", "text/plai
n", | 1679 DownloadFileType::NOT_DANGEROUS, "http://example.com/foo.txt", |
| 1517 FILE_PATH_LITERAL(""), | 1680 "text/plain", FILE_PATH_LITERAL(""), |
| 1518 | 1681 |
| 1519 FILE_PATH_LITERAL("foo.txt"), DownloadItem::TARGET_DISPOSITION_OVERWRITE, | 1682 FILE_PATH_LITERAL("foo.txt"), DownloadItem::TARGET_DISPOSITION_OVERWRITE, |
| 1520 | 1683 |
| 1521 EXPECT_CRDOWNLOAD}, | 1684 EXPECT_CRDOWNLOAD}, |
| 1522 | 1685 |
| 1523 {// 1: Save_As Safe: Initial path used. | 1686 {// 1: Save_As Safe: Initial path used. |
| 1524 SAVE_AS, content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, | 1687 SAVE_AS, content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, |
| 1525 DownloadFileType::NOT_DANGEROUS, "http://example.com/foo.txt", "text/plai
n", | 1688 DownloadFileType::NOT_DANGEROUS, "http://example.com/foo.txt", |
| 1526 FILE_PATH_LITERAL(""), | 1689 "text/plain", FILE_PATH_LITERAL(""), |
| 1527 | 1690 |
| 1528 kInitialPath, DownloadItem::TARGET_DISPOSITION_PROMPT, | 1691 kInitialPath, DownloadItem::TARGET_DISPOSITION_PROMPT, |
| 1529 | 1692 |
| 1530 EXPECT_CRDOWNLOAD}, | 1693 EXPECT_CRDOWNLOAD}, |
| 1531 | 1694 |
| 1532 {// 2: Automatic Dangerous: Initial path is ignored since the user hasn't | 1695 {// 2: Automatic Dangerous: Initial path is ignored since the user hasn't |
| 1533 // been prompted before. | 1696 // been prompted before. |
| 1534 AUTOMATIC, content::DOWNLOAD_DANGER_TYPE_DANGEROUS_FILE, | 1697 AUTOMATIC, content::DOWNLOAD_DANGER_TYPE_DANGEROUS_FILE, |
| 1535 DownloadFileType::ALLOW_ON_USER_GESTURE, "http://example.com/foo.crx", ""
, | 1698 DownloadFileType::ALLOW_ON_USER_GESTURE, |
| 1536 FILE_PATH_LITERAL(""), | 1699 "http://example.com/foo.kindabad", "", FILE_PATH_LITERAL(""), |
| 1537 | 1700 |
| 1538 FILE_PATH_LITERAL("foo.crx"), DownloadItem::TARGET_DISPOSITION_OVERWRITE, | 1701 FILE_PATH_LITERAL("foo.kindabad"), |
| 1702 DownloadItem::TARGET_DISPOSITION_OVERWRITE, |
| 1539 | 1703 |
| 1540 EXPECT_UNCONFIRMED}, | 1704 EXPECT_UNCONFIRMED}, |
| 1541 | 1705 |
| 1542 {// 3: Forced Safe: Initial path is ignored due to the forced path. | 1706 {// 3: Forced Safe: Initial path is ignored due to the forced path. |
| 1543 FORCED, content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, | 1707 FORCED, content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, |
| 1544 DownloadFileType::NOT_DANGEROUS, "http://example.com/foo.txt", "", | 1708 DownloadFileType::NOT_DANGEROUS, "http://example.com/foo.txt", "", |
| 1545 FILE_PATH_LITERAL("forced-foo.txt"), | 1709 FILE_PATH_LITERAL("forced-foo.txt"), |
| 1546 | 1710 |
| 1547 FILE_PATH_LITERAL("forced-foo.txt"), | 1711 FILE_PATH_LITERAL("forced-foo.txt"), |
| 1548 DownloadItem::TARGET_DISPOSITION_OVERWRITE, | 1712 DownloadItem::TARGET_DISPOSITION_OVERWRITE, |
| 1549 | 1713 |
| 1550 EXPECT_LOCAL_PATH}, | 1714 EXPECT_LOCAL_PATH}, |
| 1551 }; | 1715 }; |
| 1552 | 1716 |
| 1553 // The test assumes that .crx files have a danger level of | 1717 // The test assumes that .kindabad files have a danger level of |
| 1554 // ALLOW_ON_USER_GESTURE. | 1718 // ALLOW_ON_USER_GESTURE. |
| 1555 ASSERT_EQ(DownloadFileType::ALLOW_ON_USER_GESTURE, | 1719 ASSERT_EQ(DownloadFileType::ALLOW_ON_USER_GESTURE, |
| 1556 Policies()->GetFileDangerLevel( | 1720 safe_browsing::FileTypePolicies::GetInstance()->GetFileDangerLevel( |
| 1557 base::FilePath(FILE_PATH_LITERAL("foo.crx")))); | 1721 base::FilePath(FILE_PATH_LITERAL("foo.kindabad")))); |
| 1558 for (size_t i = 0; i < arraysize(kResumedTestCases); ++i) { | 1722 for (size_t i = 0; i < arraysize(kResumedTestCases); ++i) { |
| 1559 SCOPED_TRACE(testing::Message() << "Running test case " << i); | 1723 SCOPED_TRACE(testing::Message() << "Running test case " << i); |
| 1560 const DownloadTestCase& test_case = kResumedTestCases[i]; | 1724 const DownloadTestCase& test_case = kResumedTestCases[i]; |
| 1561 std::unique_ptr<content::MockDownloadItem> item( | 1725 std::unique_ptr<content::MockDownloadItem> item = |
| 1562 CreateActiveDownloadItem(i, test_case)); | 1726 CreateActiveDownloadItem(i, test_case); |
| 1563 base::FilePath expected_path = | 1727 base::FilePath expected_path = |
| 1564 GetPathInDownloadDir(test_case.expected_local_path); | 1728 GetPathInDownloadDir(test_case.expected_local_path); |
| 1565 ON_CALL(*item.get(), GetLastReason()) | 1729 ON_CALL(*item.get(), GetLastReason()) |
| 1566 .WillByDefault(Return( | 1730 .WillByDefault(Return( |
| 1567 content::DOWNLOAD_INTERRUPT_REASON_NETWORK_FAILED)); | 1731 content::DOWNLOAD_INTERRUPT_REASON_NETWORK_FAILED)); |
| 1568 // Extensions should be notified if a new path is being generated and there | 1732 // Extensions should be notified if a new path is being generated and there |
| 1569 // is no forced path. In the test cases above, this is true for tests with | 1733 // is no forced path. In the test cases above, this is true for tests with |
| 1570 // type == AUTOMATIC. | 1734 // type == AUTOMATIC. |
| 1571 EXPECT_CALL(*delegate(), NotifyExtensions(_, _, _)) | 1735 EXPECT_CALL(*delegate(), NotifyExtensions(_, _, _)) |
| 1572 .Times(test_case.test_type == AUTOMATIC ? 1 : 0); | 1736 .Times(test_case.test_type == AUTOMATIC ? 1 : 0); |
| 1573 // When resuming an AUTOMATIC download with non-empty initial path, the file | 1737 EXPECT_CALL(*delegate(), ReserveVirtualPath(_, expected_path, false, _, _)); |
| 1574 // name conflict action should be UNIQUIFY. | 1738 EXPECT_CALL(*delegate(), RequestConfirmation(_, expected_path, _, _)) |
| 1575 DownloadPathReservationTracker::FilenameConflictAction action = | |
| 1576 test_case.test_type == AUTOMATIC ? | |
| 1577 DownloadPathReservationTracker::UNIQUIFY : | |
| 1578 DownloadPathReservationTracker::OVERWRITE; | |
| 1579 EXPECT_CALL(*delegate(), ReserveVirtualPath( | |
| 1580 _, expected_path, false, action, _)); | |
| 1581 EXPECT_CALL(*delegate(), PromptUserForDownloadPath(_, expected_path, _)) | |
| 1582 .Times(0); | 1739 .Times(0); |
| 1583 EXPECT_CALL(*delegate(), DetermineLocalPath(_, expected_path, _)); | 1740 EXPECT_CALL(*delegate(), DetermineLocalPath(_, expected_path, _)); |
| 1584 EXPECT_CALL(*delegate(), CheckDownloadUrl(_, expected_path, _)); | 1741 EXPECT_CALL(*delegate(), CheckDownloadUrl(_, expected_path, _)); |
| 1585 RunTestCase(test_case, GetPathInDownloadDir(kInitialPath), item.get()); | 1742 RunTestCase(test_case, GetPathInDownloadDir(kInitialPath), item.get()); |
| 1586 } | 1743 } |
| 1587 } | 1744 } |
| 1588 | 1745 |
| 1589 // Test that a forced download doesn't prompt, even if the interrupt reason | 1746 // Test that a forced download doesn't prompt, even if the interrupt reason |
| 1590 // suggests that the target path may not be suitable for downloads. | 1747 // suggests that the target path may not be suitable for downloads. |
| 1591 TEST_F(DownloadTargetDeterminerTest, ResumedForcedDownload) { | 1748 TEST_F(DownloadTargetDeterminerTest, ResumedForcedDownload) { |
| 1592 const base::FilePath::CharType* kInitialPath = | 1749 const base::FilePath::CharType* kInitialPath = |
| 1593 FILE_PATH_LITERAL("some_path/bar.txt"); | 1750 FILE_PATH_LITERAL("some_path/bar.txt"); |
| 1594 const DownloadTestCase kResumedForcedDownload = { | 1751 const DownloadTestCase kResumedForcedDownload = { |
| 1595 // 3: Forced Safe | 1752 // 3: Forced Safe |
| 1596 FORCED, | 1753 FORCED, |
| 1597 content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, | 1754 content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, |
| 1598 DownloadFileType::NOT_DANGEROUS, | 1755 DownloadFileType::NOT_DANGEROUS, |
| 1599 "http://example.com/foo.txt", | 1756 "http://example.com/foo.txt", |
| 1600 "", | 1757 "", |
| 1601 FILE_PATH_LITERAL("forced-foo.txt"), | 1758 FILE_PATH_LITERAL("forced-foo.txt"), |
| 1602 | 1759 |
| 1603 FILE_PATH_LITERAL("forced-foo.txt"), | 1760 FILE_PATH_LITERAL("forced-foo.txt"), |
| 1604 DownloadItem::TARGET_DISPOSITION_OVERWRITE, | 1761 DownloadItem::TARGET_DISPOSITION_OVERWRITE, |
| 1605 | 1762 |
| 1606 EXPECT_LOCAL_PATH}; | 1763 EXPECT_LOCAL_PATH}; |
| 1607 | 1764 |
| 1608 const DownloadTestCase& test_case = kResumedForcedDownload; | 1765 const DownloadTestCase& test_case = kResumedForcedDownload; |
| 1609 base::FilePath expected_path = | 1766 base::FilePath expected_path = |
| 1610 GetPathInDownloadDir(test_case.expected_local_path); | 1767 GetPathInDownloadDir(test_case.expected_local_path); |
| 1611 std::unique_ptr<content::MockDownloadItem> item( | 1768 std::unique_ptr<content::MockDownloadItem> item = |
| 1612 CreateActiveDownloadItem(0, test_case)); | 1769 CreateActiveDownloadItem(0, test_case); |
| 1613 ON_CALL(*item.get(), GetLastReason()) | 1770 ON_CALL(*item.get(), GetLastReason()) |
| 1614 .WillByDefault(Return(content::DOWNLOAD_INTERRUPT_REASON_FILE_NO_SPACE)); | 1771 .WillByDefault(Return(content::DOWNLOAD_INTERRUPT_REASON_FILE_NO_SPACE)); |
| 1615 EXPECT_CALL(*delegate(), NotifyExtensions(_, _, _)) | 1772 EXPECT_CALL(*delegate(), NotifyExtensions(_, _, _)) |
| 1616 .Times(test_case.test_type == AUTOMATIC ? 1 : 0); | 1773 .Times(test_case.test_type == AUTOMATIC ? 1 : 0); |
| 1617 EXPECT_CALL(*delegate(), ReserveVirtualPath(_, expected_path, false, _, _)); | 1774 EXPECT_CALL(*delegate(), ReserveVirtualPath(_, expected_path, false, _, _)); |
| 1618 EXPECT_CALL(*delegate(), PromptUserForDownloadPath(_, _, _)) | 1775 EXPECT_CALL(*delegate(), RequestConfirmation(_, _, _, _)).Times(0); |
| 1619 .Times(0); | |
| 1620 EXPECT_CALL(*delegate(), DetermineLocalPath(_, expected_path, _)); | 1776 EXPECT_CALL(*delegate(), DetermineLocalPath(_, expected_path, _)); |
| 1621 EXPECT_CALL(*delegate(), CheckDownloadUrl(_, expected_path, _)); | 1777 EXPECT_CALL(*delegate(), CheckDownloadUrl(_, expected_path, _)); |
| 1622 RunTestCase(test_case, GetPathInDownloadDir(kInitialPath), item.get()); | 1778 RunTestCase(test_case, GetPathInDownloadDir(kInitialPath), item.get()); |
| 1623 } | 1779 } |
| 1624 | 1780 |
| 1625 // Prompting behavior for resumed downloads is based on the last interrupt | 1781 // Prompting behavior for resumed downloads is based on the last interrupt |
| 1626 // reason. If the reason indicates that the target path may not be suitable for | 1782 // reason. If the reason indicates that the target path may not be suitable for |
| 1627 // the download (ACCESS_DENIED, NO_SPACE, etc..), then the user should be | 1783 // the download (ACCESS_DENIED, NO_SPACE, etc..), then the user should be |
| 1628 // prompted, and not otherwise. These test cases result in prompting since the | 1784 // prompted, and not otherwise. These test cases result in prompting since the |
| 1629 // error is set to NO_SPACE. | 1785 // error is set to NO_SPACE. |
| 1630 TEST_F(DownloadTargetDeterminerTest, ResumedWithPrompt) { | 1786 TEST_F(DownloadTargetDeterminerTest, ResumedWithPrompt) { |
| 1631 // All test cases run with GetPathInDownloadDir(kInitialPath) as the inital | 1787 // All test cases run with GetPathInDownloadDir(kInitialPath) as the inital |
| 1632 // path. | 1788 // path. |
| 1633 const base::FilePath::CharType* kInitialPath = | 1789 const base::FilePath::CharType* kInitialPath = |
| 1634 FILE_PATH_LITERAL("some_path/bar.txt"); | 1790 FILE_PATH_LITERAL("some_path/bar.txt"); |
| 1635 | 1791 |
| 1636 const DownloadTestCase kResumedTestCases[] = { | 1792 const DownloadTestCase kResumedTestCases[] = { |
| 1637 {// 0: Automatic Safe | 1793 {// 0: Automatic Safe |
| 1638 AUTOMATIC, content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, | 1794 AUTOMATIC, content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, |
| 1639 DownloadFileType::NOT_DANGEROUS, "http://example.com/foo.txt", | 1795 DownloadFileType::NOT_DANGEROUS, "http://example.com/foo.txt", |
| 1640 "text/plain", FILE_PATH_LITERAL(""), | 1796 "text/plain", FILE_PATH_LITERAL(""), |
| 1641 | 1797 |
| 1642 FILE_PATH_LITERAL("foo.txt"), | 1798 FILE_PATH_LITERAL("foo.txt"), DownloadItem::TARGET_DISPOSITION_PROMPT, |
| 1643 #if defined(OS_ANDROID) | |
| 1644 DownloadItem::TARGET_DISPOSITION_OVERWRITE, | |
| 1645 #else | |
| 1646 DownloadItem::TARGET_DISPOSITION_PROMPT, | |
| 1647 #endif | |
| 1648 EXPECT_CRDOWNLOAD}, | 1799 EXPECT_CRDOWNLOAD}, |
| 1649 | 1800 |
| 1650 {// 1: Save_As Safe | 1801 {// 1: Save_As Safe |
| 1651 SAVE_AS, content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, | 1802 SAVE_AS, content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, |
| 1652 DownloadFileType::NOT_DANGEROUS, "http://example.com/foo.txt", | 1803 DownloadFileType::NOT_DANGEROUS, "http://example.com/foo.txt", |
| 1653 "text/plain", FILE_PATH_LITERAL(""), | 1804 "text/plain", FILE_PATH_LITERAL(""), |
| 1654 | 1805 |
| 1655 kInitialPath, DownloadItem::TARGET_DISPOSITION_PROMPT, | 1806 kInitialPath, DownloadItem::TARGET_DISPOSITION_PROMPT, |
| 1656 | 1807 |
| 1657 EXPECT_CRDOWNLOAD}, | 1808 EXPECT_CRDOWNLOAD}, |
| 1658 | 1809 |
| 1659 { | 1810 { |
| 1660 // 2: Automatic Dangerous | 1811 // 2: Automatic Dangerous |
| 1661 AUTOMATIC, | 1812 AUTOMATIC, content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, |
| 1662 #if defined(OS_ANDROID) | 1813 DownloadFileType::NOT_DANGEROUS, "http://example.com/foo.kindabad", |
| 1663 // If we don't prompt user, the file will be treated as dangerous. | 1814 "", FILE_PATH_LITERAL(""), |
| 1664 content::DOWNLOAD_DANGER_TYPE_DANGEROUS_FILE, | |
| 1665 DownloadFileType::ALLOW_ON_USER_GESTURE, | |
| 1666 #else | |
| 1667 content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, | |
| 1668 DownloadFileType::NOT_DANGEROUS, | |
| 1669 #endif | |
| 1670 "http://example.com/foo.crx", "", FILE_PATH_LITERAL(""), | |
| 1671 | 1815 |
| 1672 FILE_PATH_LITERAL("foo.crx"), | 1816 FILE_PATH_LITERAL("foo.kindabad"), |
| 1673 #if defined(OS_ANDROID) | |
| 1674 DownloadItem::TARGET_DISPOSITION_OVERWRITE, | |
| 1675 // Dangerous download will have an unconfirmed intermediate file name. | |
| 1676 EXPECT_UNCONFIRMED, | |
| 1677 #else | |
| 1678 DownloadItem::TARGET_DISPOSITION_PROMPT, EXPECT_CRDOWNLOAD, | 1817 DownloadItem::TARGET_DISPOSITION_PROMPT, EXPECT_CRDOWNLOAD, |
| 1679 #endif | 1818 }, |
| 1819 |
| 1820 { |
| 1821 // 3: Automatic Dangerous |
| 1822 AUTOMATIC, content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, |
| 1823 DownloadFileType::NOT_DANGEROUS, "http://example.com/foo.bad", "", |
| 1824 FILE_PATH_LITERAL(""), |
| 1825 |
| 1826 FILE_PATH_LITERAL("foo.bad"), DownloadItem::TARGET_DISPOSITION_PROMPT, |
| 1827 EXPECT_CRDOWNLOAD, |
| 1680 }, | 1828 }, |
| 1681 }; | 1829 }; |
| 1682 | 1830 |
| 1683 // The test assumes that .xml files have a danger level of | |
| 1684 // ALLOW_ON_USER_GESTURE. | |
| 1685 ASSERT_EQ(DownloadFileType::ALLOW_ON_USER_GESTURE, | 1831 ASSERT_EQ(DownloadFileType::ALLOW_ON_USER_GESTURE, |
| 1686 Policies()->GetFileDangerLevel( | 1832 safe_browsing::FileTypePolicies::GetInstance()->GetFileDangerLevel( |
| 1687 base::FilePath(FILE_PATH_LITERAL("foo.crx")))); | 1833 base::FilePath(FILE_PATH_LITERAL("foo.kindabad")))); |
| 1688 for (size_t i = 0; i < arraysize(kResumedTestCases); ++i) { | 1834 for (size_t i = 0; i < arraysize(kResumedTestCases); ++i) { |
| 1689 SCOPED_TRACE(testing::Message() << "Running test case " << i); | 1835 SCOPED_TRACE(testing::Message() << "Running test case " << i); |
| 1690 download_prefs()->SetSaveFilePath(test_download_dir()); | 1836 download_prefs()->SetSaveFilePath(test_download_dir()); |
| 1691 const DownloadTestCase& test_case = kResumedTestCases[i]; | 1837 const DownloadTestCase& test_case = kResumedTestCases[i]; |
| 1692 base::FilePath expected_path = | 1838 base::FilePath expected_path = |
| 1693 GetPathInDownloadDir(test_case.expected_local_path); | 1839 GetPathInDownloadDir(test_case.expected_local_path); |
| 1694 std::unique_ptr<content::MockDownloadItem> item( | 1840 std::unique_ptr<content::MockDownloadItem> item = |
| 1695 CreateActiveDownloadItem(i, test_case)); | 1841 CreateActiveDownloadItem(i, test_case); |
| 1696 ON_CALL(*item.get(), GetLastReason()) | 1842 ON_CALL(*item.get(), GetLastReason()) |
| 1697 .WillByDefault(Return( | 1843 .WillByDefault(Return( |
| 1698 content::DOWNLOAD_INTERRUPT_REASON_FILE_NO_SPACE)); | 1844 content::DOWNLOAD_INTERRUPT_REASON_FILE_NO_SPACE)); |
| 1699 EXPECT_CALL(*delegate(), NotifyExtensions(_, _, _)) | 1845 EXPECT_CALL(*delegate(), NotifyExtensions(_, _, _)) |
| 1700 .Times(test_case.test_type == AUTOMATIC ? 1 : 0); | 1846 .Times(test_case.test_type == AUTOMATIC ? 1 : 0); |
| 1701 EXPECT_CALL(*delegate(), ReserveVirtualPath(_, expected_path, false, _, _)); | 1847 EXPECT_CALL(*delegate(), ReserveVirtualPath(_, expected_path, false, _, _)); |
| 1702 #if defined(OS_ANDROID) | 1848 EXPECT_CALL(*delegate(), RequestConfirmation(_, expected_path, _, _)); |
| 1703 EXPECT_CALL(*delegate(), PromptUserForDownloadPath(_, expected_path, _)) | |
| 1704 .Times(0); | |
| 1705 #else | |
| 1706 EXPECT_CALL(*delegate(), PromptUserForDownloadPath(_, expected_path, _)); | |
| 1707 #endif | |
| 1708 EXPECT_CALL(*delegate(), DetermineLocalPath(_, expected_path, _)); | 1849 EXPECT_CALL(*delegate(), DetermineLocalPath(_, expected_path, _)); |
| 1709 EXPECT_CALL(*delegate(), CheckDownloadUrl(_, expected_path, _)); | 1850 EXPECT_CALL(*delegate(), CheckDownloadUrl(_, expected_path, _)); |
| 1710 RunTestCase(test_case, GetPathInDownloadDir(kInitialPath), item.get()); | 1851 RunTestCase(test_case, GetPathInDownloadDir(kInitialPath), item.get()); |
| 1711 } | 1852 } |
| 1712 } | 1853 } |
| 1713 | 1854 |
| 1714 // Test intermediate filename generation for resumed downloads. | 1855 // Test intermediate filename generation for resumed downloads. |
| 1715 TEST_F(DownloadTargetDeterminerTest, IntermediateNameForResumed) { | 1856 TEST_F(DownloadTargetDeterminerTest, IntermediateNameForResumed) { |
| 1716 // All test cases run with GetPathInDownloadDir(kInitialPath) as the inital | 1857 // All test cases run with GetPathInDownloadDir(kInitialPath) as the inital |
| 1717 // path. | 1858 // path. |
| (...skipping 30 matching lines...) Expand all Loading... |
| 1748 "text/plain", FILE_PATH_LITERAL(""), | 1889 "text/plain", FILE_PATH_LITERAL(""), |
| 1749 | 1890 |
| 1750 kInitialPath, DownloadItem::TARGET_DISPOSITION_PROMPT, | 1891 kInitialPath, DownloadItem::TARGET_DISPOSITION_PROMPT, |
| 1751 | 1892 |
| 1752 EXPECT_CRDOWNLOAD}, | 1893 EXPECT_CRDOWNLOAD}, |
| 1753 FILE_PATH_LITERAL("foo.txt.crdownload"), | 1894 FILE_PATH_LITERAL("foo.txt.crdownload"), |
| 1754 FILE_PATH_LITERAL("some_path/bar.txt.crdownload")}, | 1895 FILE_PATH_LITERAL("some_path/bar.txt.crdownload")}, |
| 1755 | 1896 |
| 1756 {{// 2: Automatic Dangerous | 1897 {{// 2: Automatic Dangerous |
| 1757 AUTOMATIC, content::DOWNLOAD_DANGER_TYPE_DANGEROUS_FILE, | 1898 AUTOMATIC, content::DOWNLOAD_DANGER_TYPE_DANGEROUS_FILE, |
| 1758 DownloadFileType::ALLOW_ON_USER_GESTURE, "http://example.com/foo.crx", | 1899 DownloadFileType::ALLOW_ON_USER_GESTURE, |
| 1759 "", FILE_PATH_LITERAL(""), | 1900 "http://example.com/foo.kindabad", "", FILE_PATH_LITERAL(""), |
| 1760 | 1901 |
| 1761 FILE_PATH_LITERAL("foo.crx"), | 1902 FILE_PATH_LITERAL("foo.kindabad"), |
| 1762 DownloadItem::TARGET_DISPOSITION_OVERWRITE, | 1903 DownloadItem::TARGET_DISPOSITION_OVERWRITE, |
| 1763 | 1904 |
| 1764 EXPECT_UNCONFIRMED}, | 1905 EXPECT_UNCONFIRMED}, |
| 1765 FILE_PATH_LITERAL("Unconfirmed abcd.crdownload"), | 1906 FILE_PATH_LITERAL("Unconfirmed abcd.crdownload"), |
| 1766 FILE_PATH_LITERAL("Unconfirmed abcd.crdownload")}, | 1907 FILE_PATH_LITERAL("Unconfirmed abcd.crdownload")}, |
| 1767 | 1908 |
| 1768 {{// 3: Automatic Dangerous | 1909 {{// 3: Automatic Dangerous |
| 1769 AUTOMATIC, content::DOWNLOAD_DANGER_TYPE_DANGEROUS_FILE, | 1910 AUTOMATIC, content::DOWNLOAD_DANGER_TYPE_DANGEROUS_FILE, |
| 1770 DownloadFileType::ALLOW_ON_USER_GESTURE, "http://example.com/foo.crx", | 1911 DownloadFileType::ALLOW_ON_USER_GESTURE, |
| 1771 "", FILE_PATH_LITERAL(""), | 1912 "http://example.com/foo.kindabad", "", FILE_PATH_LITERAL(""), |
| 1772 | 1913 |
| 1773 FILE_PATH_LITERAL("foo.crx"), | 1914 FILE_PATH_LITERAL("foo.kindabad"), |
| 1774 DownloadItem::TARGET_DISPOSITION_OVERWRITE, | 1915 DownloadItem::TARGET_DISPOSITION_OVERWRITE, |
| 1775 | 1916 |
| 1776 EXPECT_UNCONFIRMED}, | 1917 EXPECT_UNCONFIRMED}, |
| 1777 FILE_PATH_LITERAL("other_path/Unconfirmed abcd.crdownload"), | 1918 FILE_PATH_LITERAL("other_path/Unconfirmed abcd.crdownload"), |
| 1778 // Rely on the EXPECT_UNCONFIRMED check in the general test settings. A | 1919 // Rely on the EXPECT_UNCONFIRMED check in the general test settings. A |
| 1779 // new intermediate path of the form "Unconfirmed <number>.crdownload" | 1920 // new intermediate path of the form "Unconfirmed <number>.crdownload" |
| 1780 // should be generated for this case since the initial intermediate path | 1921 // should be generated for this case since the initial intermediate path |
| 1781 // is in the wrong directory. | 1922 // is in the wrong directory. |
| 1782 FILE_PATH_LITERAL("")}, | 1923 FILE_PATH_LITERAL("")}, |
| 1783 | 1924 |
| 1784 {{// 3: Forced Safe: Initial path is ignored due to the forced path. | 1925 {{// 3: Forced Safe: Initial path is ignored due to the forced path. |
| 1785 FORCED, content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, | 1926 FORCED, content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, |
| 1786 DownloadFileType::NOT_DANGEROUS, "http://example.com/foo.txt", "", | 1927 DownloadFileType::NOT_DANGEROUS, "http://example.com/foo.txt", "", |
| 1787 FILE_PATH_LITERAL("forced-foo.txt"), | 1928 FILE_PATH_LITERAL("forced-foo.txt"), |
| 1788 | 1929 |
| 1789 FILE_PATH_LITERAL("forced-foo.txt"), | 1930 FILE_PATH_LITERAL("forced-foo.txt"), |
| 1790 DownloadItem::TARGET_DISPOSITION_OVERWRITE, | 1931 DownloadItem::TARGET_DISPOSITION_OVERWRITE, |
| 1791 | 1932 |
| 1792 EXPECT_LOCAL_PATH}, | 1933 EXPECT_LOCAL_PATH}, |
| 1793 FILE_PATH_LITERAL("forced-foo.txt"), | 1934 FILE_PATH_LITERAL("forced-foo.txt"), |
| 1794 FILE_PATH_LITERAL("forced-foo.txt")}, | 1935 FILE_PATH_LITERAL("forced-foo.txt")}, |
| 1795 }; | 1936 }; |
| 1796 | 1937 |
| 1797 // The test assumes that .crx files have a danger level of | 1938 // The test assumes that .kindabad files have a danger level of |
| 1798 // ALLOW_ON_USER_GESTURE. | 1939 // ALLOW_ON_USER_GESTURE. |
| 1799 ASSERT_EQ(DownloadFileType::ALLOW_ON_USER_GESTURE, | 1940 ASSERT_EQ(DownloadFileType::ALLOW_ON_USER_GESTURE, |
| 1800 Policies()->GetFileDangerLevel( | 1941 safe_browsing::FileTypePolicies::GetInstance()->GetFileDangerLevel( |
| 1801 base::FilePath(FILE_PATH_LITERAL("foo.crx")))); | 1942 base::FilePath(FILE_PATH_LITERAL("foo.kindabad")))); |
| 1802 | 1943 |
| 1803 for (size_t i = 0; i < arraysize(kIntermediateNameTestCases); ++i) { | 1944 for (size_t i = 0; i < arraysize(kIntermediateNameTestCases); ++i) { |
| 1804 SCOPED_TRACE(testing::Message() << "Running test case " << i); | 1945 SCOPED_TRACE(testing::Message() << "Running test case " << i); |
| 1805 const IntermediateNameTestCase& test_case = kIntermediateNameTestCases[i]; | 1946 const IntermediateNameTestCase& test_case = kIntermediateNameTestCases[i]; |
| 1806 std::unique_ptr<content::MockDownloadItem> item( | 1947 std::unique_ptr<content::MockDownloadItem> item = |
| 1807 CreateActiveDownloadItem(i, test_case.general)); | 1948 CreateActiveDownloadItem(i, test_case.general); |
| 1808 | 1949 |
| 1809 ON_CALL(*item.get(), GetLastReason()) | 1950 ON_CALL(*item.get(), GetLastReason()) |
| 1810 .WillByDefault(Return( | 1951 .WillByDefault(Return( |
| 1811 content::DOWNLOAD_INTERRUPT_REASON_NETWORK_FAILED)); | 1952 content::DOWNLOAD_INTERRUPT_REASON_NETWORK_FAILED)); |
| 1812 ON_CALL(*item.get(), GetFullPath()) | 1953 ON_CALL(*item.get(), GetFullPath()) |
| 1813 .WillByDefault(ReturnRefOfCopy( | 1954 .WillByDefault(ReturnRefOfCopy( |
| 1814 GetPathInDownloadDir(test_case.initial_intermediate_path))); | 1955 GetPathInDownloadDir(test_case.initial_intermediate_path))); |
| 1815 ON_CALL(*item.get(), GetDangerType()) | 1956 ON_CALL(*item.get(), GetDangerType()) |
| 1816 .WillByDefault(Return(test_case.general.expected_danger_type)); | 1957 .WillByDefault(Return(test_case.general.expected_danger_type)); |
| 1817 | 1958 |
| (...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1893 {{// 5: x-x509-user-cert mime-type. | 2034 {{// 5: x-x509-user-cert mime-type. |
| 1894 AUTOMATIC, content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, | 2035 AUTOMATIC, content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, |
| 1895 DownloadFileType::NOT_DANGEROUS, "http://example.com/foo.notarealext", | 2036 DownloadFileType::NOT_DANGEROUS, "http://example.com/foo.notarealext", |
| 1896 "application/x-x509-user-cert", FILE_PATH_LITERAL(""), | 2037 "application/x-x509-user-cert", FILE_PATH_LITERAL(""), |
| 1897 | 2038 |
| 1898 FILE_PATH_LITERAL("user.crt"), | 2039 FILE_PATH_LITERAL("user.crt"), |
| 1899 DownloadItem::TARGET_DISPOSITION_OVERWRITE, | 2040 DownloadItem::TARGET_DISPOSITION_OVERWRITE, |
| 1900 | 2041 |
| 1901 EXPECT_CRDOWNLOAD}, | 2042 EXPECT_CRDOWNLOAD}, |
| 1902 ""}, | 2043 ""}, |
| 1903 | |
| 1904 }; | 2044 }; |
| 1905 | 2045 |
| 1906 ON_CALL(*delegate(), GetFileMimeType( | 2046 ON_CALL(*delegate(), GetFileMimeType( |
| 1907 GetPathInDownloadDir(FILE_PATH_LITERAL("foo.png")), _)) | 2047 GetPathInDownloadDir(FILE_PATH_LITERAL("foo.png")), _)) |
| 1908 .WillByDefault(WithArg<1>( | 2048 .WillByDefault(WithArg<1>( |
| 1909 ScheduleCallback("image/png"))); | 2049 ScheduleCallback("image/png"))); |
| 1910 | 2050 |
| 1911 for (size_t i = 0; i < arraysize(kMIMETypeTestCases); ++i) { | 2051 for (size_t i = 0; i < arraysize(kMIMETypeTestCases); ++i) { |
| 1912 SCOPED_TRACE(testing::Message() << "Running test case " << i); | 2052 SCOPED_TRACE(testing::Message() << "Running test case " << i); |
| 1913 const MIMETypeTestCase& test_case = kMIMETypeTestCases[i]; | 2053 const MIMETypeTestCase& test_case = kMIMETypeTestCases[i]; |
| 1914 std::unique_ptr<content::MockDownloadItem> item( | 2054 std::unique_ptr<content::MockDownloadItem> item = |
| 1915 CreateActiveDownloadItem(i, test_case.general)); | 2055 CreateActiveDownloadItem(i, test_case.general); |
| 1916 std::unique_ptr<DownloadTargetInfo> target_info = | 2056 std::unique_ptr<DownloadTargetInfo> target_info = |
| 1917 RunDownloadTargetDeterminer(GetPathInDownloadDir(kInitialPath), | 2057 RunDownloadTargetDeterminer(GetPathInDownloadDir(kInitialPath), |
| 1918 item.get()); | 2058 item.get()); |
| 1919 EXPECT_EQ(test_case.expected_mime_type, target_info->mime_type); | 2059 EXPECT_EQ(test_case.expected_mime_type, target_info->mime_type); |
| 1920 } | 2060 } |
| 1921 } | 2061 } |
| 1922 | 2062 |
| 1923 // Test that a user validated download won't be treated as dangerous. | 2063 // Test that a user validated download won't be treated as dangerous. |
| 1924 TEST_F(DownloadTargetDeterminerTest, ResumedWithUserValidatedDownload) { | 2064 TEST_F(DownloadTargetDeterminerTest, ResumedWithUserValidatedDownload) { |
| 1925 const base::FilePath::CharType kInitialPath[] = | 2065 const base::FilePath::CharType kInitialPath[] = |
| (...skipping 18 matching lines...) Expand all Loading... |
| 1944 base::FilePath expected_path = | 2084 base::FilePath expected_path = |
| 1945 GetPathInDownloadDir(test_case.expected_local_path); | 2085 GetPathInDownloadDir(test_case.expected_local_path); |
| 1946 ON_CALL(*item.get(), GetDangerType()) | 2086 ON_CALL(*item.get(), GetDangerType()) |
| 1947 .WillByDefault(Return(content::DOWNLOAD_DANGER_TYPE_USER_VALIDATED)); | 2087 .WillByDefault(Return(content::DOWNLOAD_DANGER_TYPE_USER_VALIDATED)); |
| 1948 ON_CALL(*item.get(), GetFullPath()) | 2088 ON_CALL(*item.get(), GetFullPath()) |
| 1949 .WillByDefault(ReturnRefOfCopy(GetPathInDownloadDir(kIntermediatePath))); | 2089 .WillByDefault(ReturnRefOfCopy(GetPathInDownloadDir(kIntermediatePath))); |
| 1950 ON_CALL(*item.get(), GetLastReason()) | 2090 ON_CALL(*item.get(), GetLastReason()) |
| 1951 .WillByDefault(Return(content::DOWNLOAD_INTERRUPT_REASON_NETWORK_FAILED)); | 2091 .WillByDefault(Return(content::DOWNLOAD_INTERRUPT_REASON_NETWORK_FAILED)); |
| 1952 EXPECT_CALL(*delegate(), NotifyExtensions(_, _, _)); | 2092 EXPECT_CALL(*delegate(), NotifyExtensions(_, _, _)); |
| 1953 EXPECT_CALL(*delegate(), ReserveVirtualPath(_, expected_path, false, _, _)); | 2093 EXPECT_CALL(*delegate(), ReserveVirtualPath(_, expected_path, false, _, _)); |
| 1954 EXPECT_CALL(*delegate(), PromptUserForDownloadPath(_, expected_path, _)) | |
| 1955 .Times(0); | |
| 1956 EXPECT_CALL(*delegate(), DetermineLocalPath(_, expected_path, _)); | 2094 EXPECT_CALL(*delegate(), DetermineLocalPath(_, expected_path, _)); |
| 1957 EXPECT_CALL(*delegate(), CheckDownloadUrl(_, expected_path, _)).Times(0); | 2095 EXPECT_CALL(*delegate(), CheckDownloadUrl(_, expected_path, _)).Times(0); |
| 2096 EXPECT_CALL(*delegate(), RequestConfirmation(_, _, _, _)).Times(0); |
| 1958 RunTestCase(test_case, GetPathInDownloadDir(kInitialPath), item.get()); | 2097 RunTestCase(test_case, GetPathInDownloadDir(kInitialPath), item.get()); |
| 1959 } | 2098 } |
| 1960 | 2099 |
| 1961 #if BUILDFLAG(ENABLE_PLUGINS) | 2100 #if BUILDFLAG(ENABLE_PLUGINS) |
| 1962 | 2101 |
| 1963 void DummyGetPluginsCallback( | 2102 void DummyGetPluginsCallback( |
| 1964 const base::Closure& closure, | 2103 const base::Closure& closure, |
| 1965 const std::vector<content::WebPluginInfo>& plugins) { | 2104 const std::vector<content::WebPluginInfo>& plugins) { |
| 1966 closure.Run(); | 2105 closure.Run(); |
| 1967 } | 2106 } |
| (...skipping 133 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2101 ASSERT_FALSE(plugin_service->GetPluginInfoArray( | 2240 ASSERT_FALSE(plugin_service->GetPluginInfoArray( |
| 2102 GURL(), kTestMIMEType, false, &info, NULL)); | 2241 GURL(), kTestMIMEType, false, &info, NULL)); |
| 2103 ASSERT_EQ(0u, info.size()) | 2242 ASSERT_EQ(0u, info.size()) |
| 2104 << "Name: " << info[0].name << ", Path: " << info[0].path.value(); | 2243 << "Name: " << info[0].name << ", Path: " << info[0].path.value(); |
| 2105 } | 2244 } |
| 2106 | 2245 |
| 2107 ON_CALL(*delegate(), GetFileMimeType( | 2246 ON_CALL(*delegate(), GetFileMimeType( |
| 2108 GetPathInDownloadDir(FILE_PATH_LITERAL("foo.fakeext")), _)) | 2247 GetPathInDownloadDir(FILE_PATH_LITERAL("foo.fakeext")), _)) |
| 2109 .WillByDefault(WithArg<1>( | 2248 .WillByDefault(WithArg<1>( |
| 2110 ScheduleCallback(kTestMIMEType))); | 2249 ScheduleCallback(kTestMIMEType))); |
| 2111 std::unique_ptr<content::MockDownloadItem> item( | 2250 std::unique_ptr<content::MockDownloadItem> item = |
| 2112 CreateActiveDownloadItem(1, kSecureHandlingTestCase)); | 2251 CreateActiveDownloadItem(1, kSecureHandlingTestCase); |
| 2113 std::unique_ptr<DownloadTargetInfo> target_info = RunDownloadTargetDeterminer( | 2252 std::unique_ptr<DownloadTargetInfo> target_info = RunDownloadTargetDeterminer( |
| 2114 GetPathInDownloadDir(kInitialPath), item.get()); | 2253 GetPathInDownloadDir(kInitialPath), item.get()); |
| 2115 EXPECT_FALSE(target_info->is_filetype_handled_safely); | 2254 EXPECT_FALSE(target_info->is_filetype_handled_safely); |
| 2116 | 2255 |
| 2117 // Register a PPAPI plugin. This should count as handling the filetype | 2256 // Register a PPAPI plugin. This should count as handling the filetype |
| 2118 // securely. | 2257 // securely. |
| 2119 ScopedRegisterInternalPlugin ppapi_plugin( | 2258 ScopedRegisterInternalPlugin ppapi_plugin( |
| 2120 plugin_service, | 2259 plugin_service, |
| 2121 content::WebPluginInfo::PLUGIN_TYPE_PEPPER_OUT_OF_PROCESS, | 2260 content::WebPluginInfo::PLUGIN_TYPE_PEPPER_OUT_OF_PROCESS, |
| 2122 test_download_dir().AppendASCII("ppapi"), | 2261 test_download_dir().AppendASCII("ppapi"), |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2170 ASSERT_FALSE(plugin_service->GetPluginInfoArray( | 2309 ASSERT_FALSE(plugin_service->GetPluginInfoArray( |
| 2171 GURL(), kTestMIMEType, false, &info, NULL)); | 2310 GURL(), kTestMIMEType, false, &info, NULL)); |
| 2172 ASSERT_EQ(0u, info.size()) | 2311 ASSERT_EQ(0u, info.size()) |
| 2173 << "Name: " << info[0].name << ", Path: " << info[0].path.value(); | 2312 << "Name: " << info[0].name << ", Path: " << info[0].path.value(); |
| 2174 } | 2313 } |
| 2175 | 2314 |
| 2176 ON_CALL(*delegate(), GetFileMimeType( | 2315 ON_CALL(*delegate(), GetFileMimeType( |
| 2177 GetPathInDownloadDir(FILE_PATH_LITERAL("foo.fakeext")), _)) | 2316 GetPathInDownloadDir(FILE_PATH_LITERAL("foo.fakeext")), _)) |
| 2178 .WillByDefault(WithArg<1>( | 2317 .WillByDefault(WithArg<1>( |
| 2179 ScheduleCallback(kTestMIMEType))); | 2318 ScheduleCallback(kTestMIMEType))); |
| 2180 std::unique_ptr<content::MockDownloadItem> item( | 2319 std::unique_ptr<content::MockDownloadItem> item = |
| 2181 CreateActiveDownloadItem(1, kSecureHandlingTestCase)); | 2320 CreateActiveDownloadItem(1, kSecureHandlingTestCase); |
| 2182 std::unique_ptr<DownloadTargetInfo> target_info = RunDownloadTargetDeterminer( | 2321 std::unique_ptr<DownloadTargetInfo> target_info = RunDownloadTargetDeterminer( |
| 2183 GetPathInDownloadDir(kInitialPath), item.get()); | 2322 GetPathInDownloadDir(kInitialPath), item.get()); |
| 2184 EXPECT_FALSE(target_info->is_filetype_handled_safely); | 2323 EXPECT_FALSE(target_info->is_filetype_handled_safely); |
| 2185 | 2324 |
| 2186 // Register a BrowserPlugin. This should count as handling the filetype | 2325 // Register a BrowserPlugin. This should count as handling the filetype |
| 2187 // securely. | 2326 // securely. |
| 2188 ScopedRegisterInternalPlugin browser_plugin( | 2327 ScopedRegisterInternalPlugin browser_plugin( |
| 2189 plugin_service, | 2328 plugin_service, |
| 2190 content::WebPluginInfo::PLUGIN_TYPE_BROWSER_PLUGIN, | 2329 content::WebPluginInfo::PLUGIN_TYPE_BROWSER_PLUGIN, |
| 2191 test_download_dir().AppendASCII("browser_plugin"), | 2330 test_download_dir().AppendASCII("browser_plugin"), |
| (...skipping 10 matching lines...) Expand all Loading... |
| 2202 EXPECT_CALL(mock_plugin_filter_, MockPluginAvailable(browser_plugin.path())) | 2341 EXPECT_CALL(mock_plugin_filter_, MockPluginAvailable(browser_plugin.path())) |
| 2203 .WillRepeatedly(Return(false)); | 2342 .WillRepeatedly(Return(false)); |
| 2204 target_info = RunDownloadTargetDeterminer( | 2343 target_info = RunDownloadTargetDeterminer( |
| 2205 GetPathInDownloadDir(kInitialPath), item.get()); | 2344 GetPathInDownloadDir(kInitialPath), item.get()); |
| 2206 EXPECT_FALSE(target_info->is_filetype_handled_safely); | 2345 EXPECT_FALSE(target_info->is_filetype_handled_safely); |
| 2207 } | 2346 } |
| 2208 | 2347 |
| 2209 #endif // BUILDFLAG(ENABLE_PLUGINS) | 2348 #endif // BUILDFLAG(ENABLE_PLUGINS) |
| 2210 | 2349 |
| 2211 } // namespace | 2350 } // namespace |
| OLD | NEW |