| OLD | NEW |
| (Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "base/file_util.h" |
| 6 #include "base/files/file_path.h" |
| 7 #include "base/files/scoped_temp_dir.h" |
| 8 #include "base/message_loop.h" |
| 9 #include "base/observer_list.h" |
| 10 #include "base/prefs/pref_service.h" |
| 11 #include "base/stl_util.h" |
| 12 #include "base/string_util.h" |
| 13 #include "base/value_conversions.h" |
| 14 #include "chrome/browser/download/chrome_download_manager_delegate.h" |
| 15 #include "chrome/browser/download/download_extensions.h" |
| 16 #include "chrome/browser/download/download_prefs.h" |
| 17 #include "chrome/browser/download/download_target_determiner.h" |
| 18 #include "chrome/browser/download/download_util.h" |
| 19 #include "chrome/browser/history/history_service.h" |
| 20 #include "chrome/browser/history/history_service_factory.h" |
| 21 #include "chrome/browser/history/history_types.h" |
| 22 #include "chrome/common/extensions/extension.h" |
| 23 #include "chrome/common/pref_names.h" |
| 24 #include "chrome/test/base/chrome_render_view_host_test_harness.h" |
| 25 #include "chrome/test/base/testing_pref_service_syncable.h" |
| 26 #include "chrome/test/base/testing_profile.h" |
| 27 #include "content/public/browser/web_contents.h" |
| 28 #include "content/public/browser/web_contents_delegate.h" |
| 29 #include "content/public/test/mock_download_item.h" |
| 30 #include "content/public/test/test_browser_thread.h" |
| 31 #include "content/public/test/test_renderer_host.h" |
| 32 #include "content/public/test/web_contents_tester.h" |
| 33 #include "testing/gmock/include/gmock/gmock.h" |
| 34 #include "testing/gtest/include/gtest/gtest.h" |
| 35 |
| 36 #if defined(FULL_SAFE_BROWSING) |
| 37 #include "chrome/browser/safe_browsing/download_protection_service.h" |
| 38 #endif |
| 39 |
| 40 using ::testing::AnyNumber; |
| 41 using ::testing::Invoke; |
| 42 using ::testing::Ref; |
| 43 using ::testing::Return; |
| 44 using ::testing::ReturnRef; |
| 45 using ::testing::ReturnRefOfCopy; |
| 46 using ::testing::Truly; |
| 47 using ::testing::WithArg; |
| 48 using ::testing::_; |
| 49 using content::DownloadItem; |
| 50 |
| 51 namespace { |
| 52 |
| 53 // No-op delegate. |
| 54 class NullWebContentsDelegate : public content::WebContentsDelegate { |
| 55 public: |
| 56 NullWebContentsDelegate() {} |
| 57 ~NullWebContentsDelegate() {} |
| 58 }; |
| 59 |
| 60 // Google Mock action that posts a task to the current message loop that invokes |
| 61 // the first argument of the mocked method as a callback. Said argument must be |
| 62 // a base::Callback<void(ParamType)>. |result| must be of |ParamType| and is |
| 63 // bound as that parameter. |
| 64 // Example: |
| 65 // class FooClass { |
| 66 // public: |
| 67 // virtual void Foo(base::Callback<void(bool)> callback); |
| 68 // }; |
| 69 // ... |
| 70 // EXPECT_CALL(mock_fooclass_instance, Foo(callback)) |
| 71 // .WillOnce(ScheduleCallback(false)); |
| 72 ACTION_P(ScheduleCallback, result0) { |
| 73 MessageLoop::current()->PostTask(FROM_HERE, base::Bind(arg0, result0)); |
| 74 } |
| 75 |
| 76 // Similar to ScheduleCallback, but binds 2 arguments. |
| 77 ACTION_P2(ScheduleCallback2, result0, result1) { |
| 78 MessageLoop::current()->PostTask( |
| 79 FROM_HERE, base::Bind(arg0, result0, result1)); |
| 80 } |
| 81 |
| 82 // Used with DownloadTestCase. Indicates the type of test case. The expectations |
| 83 // for the test is set based on the type. |
| 84 enum TestCaseType { |
| 85 SAVE_AS, |
| 86 AUTOMATIC, |
| 87 FORCED // Requires that forced_file_path be non-empty. |
| 88 }; |
| 89 |
| 90 // Used with DownloadTestCase. Type of intermediate filename to expect. |
| 91 enum TestCaseExpectIntermediate { |
| 92 EXPECT_CRDOWNLOAD, // Expect path/to/target.crdownload. |
| 93 EXPECT_UNCONFIRMED, // Expect path/to/Unconfirmed xxx.crdownload. |
| 94 EXPECT_LOCAL_PATH, // Expect target path. |
| 95 }; |
| 96 |
| 97 // Typical download test case. Used with |
| 98 // DownloadTargetDeterminerTest::RunTestCase(). |
| 99 struct DownloadTestCase { |
| 100 // Type of test. |
| 101 TestCaseType test_type; |
| 102 |
| 103 // The |expected_danger_type| is the expected danger type for the download as |
| 104 // determined by CDMD. This value is also used to determine the behavior of |
| 105 // DownloadProtectionService::IsSupportedDownload(), CDMD::CheckDownloadUrl() |
| 106 // as necessary for flagging the download with as a dangerous download of type |
| 107 // |expected_danger_type|. |
| 108 content::DownloadDangerType expected_danger_type; |
| 109 |
| 110 // Value of DownloadItem::GetURL() |
| 111 const char* url; |
| 112 |
| 113 // Value of DownloadItem::GetMimeType() |
| 114 const char* mime_type; |
| 115 |
| 116 // Should be non-empty if |test_type| == FORCED. Value of GetForcedFilePath(). |
| 117 const base::FilePath::CharType* forced_file_path; |
| 118 |
| 119 // Expected virtual path. Specified relative to the virtual download path. If |
| 120 // empty, assumed to be the same as |expected_local_path|. |
| 121 const base::FilePath::CharType* expected_virtual_path; |
| 122 |
| 123 // Expected local path. Specified relative to the test download path. |
| 124 const base::FilePath::CharType* expected_local_path; |
| 125 |
| 126 // The path that DownloadTargetDeterminer will generate before handing off to |
| 127 // the ReserveVirtualPath(). |
| 128 const base::FilePath::CharType* expected_generated_path; |
| 129 |
| 130 // Expected target disposition. If this is TARGET_DISPOSITION_PROMPT, then the |
| 131 // test run will expect ChromeDownloadManagerDelegate to prompt the user for a |
| 132 // download location. |
| 133 DownloadItem::TargetDisposition expected_disposition; |
| 134 |
| 135 // Type of intermediate path to expect. |
| 136 TestCaseExpectIntermediate expected_intermediate; |
| 137 }; |
| 138 |
| 139 #if defined(FULL_SAFE_BROWSING) |
| 140 // DownloadProtectionService with mock methods. Since the SafeBrowsingService is |
| 141 // set to NULL, it is not safe to call any non-mocked methods other than |
| 142 // SetEnabled() and enabled(). |
| 143 class TestDownloadProtectionService |
| 144 : public safe_browsing::DownloadProtectionService { |
| 145 public: |
| 146 TestDownloadProtectionService() |
| 147 : safe_browsing::DownloadProtectionService(NULL, NULL) {} |
| 148 MOCK_METHOD2(CheckClientDownload, |
| 149 void(DownloadItem*, |
| 150 const CheckDownloadCallback&)); |
| 151 MOCK_METHOD2(CheckDownloadUrl, |
| 152 void(const DownloadItem&, |
| 153 const CheckDownloadCallback&)); |
| 154 MOCK_CONST_METHOD2(IsSupportedDownload, |
| 155 bool(const DownloadItem&, |
| 156 const base::FilePath&)); |
| 157 |
| 158 void SetupDefaults() { |
| 159 ON_CALL(*this, CheckDownloadUrl(_, _)) |
| 160 .WillByDefault(WithArg<1>(ScheduleCallback(SAFE))); |
| 161 ON_CALL(*this, IsSupportedDownload(_, _)) |
| 162 .WillByDefault(Return(false)); |
| 163 } |
| 164 }; |
| 165 #endif |
| 166 |
| 167 class MockDownloadTargetDeterminerDelegate : |
| 168 public DownloadTargetDeterminerDelegate { |
| 169 public: |
| 170 MOCK_METHOD0(GetDownloadProtectionService, |
| 171 safe_browsing::DownloadProtectionService*()); |
| 172 MOCK_METHOD0(GetExtensionEventRouter, ExtensionDownloadsEventRouter*()); |
| 173 MOCK_METHOD3(PromptUserForDownloadPath, |
| 174 void(content::DownloadItem*, const base::FilePath&, |
| 175 const DownloadTargetDeterminerDelegate::FileSelectedCallback&)); |
| 176 MOCK_METHOD3(DetermineLocalPath, |
| 177 void(DownloadItem*, const base::FilePath&, |
| 178 const DownloadTargetDeterminerDelegate::LocalPathCallback&)); |
| 179 MOCK_METHOD4(ReserveVirtualPath, |
| 180 void(DownloadItem*, const base::FilePath&, bool, |
| 181 const DownloadTargetDeterminerDelegate::ReservedPathCallback&)); |
| 182 |
| 183 void SetupDefaults() { |
| 184 ON_CALL(*this, GetExtensionEventRouter()) |
| 185 .WillByDefault(Return( |
| 186 static_cast<ExtensionDownloadsEventRouter*>(NULL))); |
| 187 ON_CALL(*this, GetDownloadProtectionService()) |
| 188 .WillByDefault(Return( |
| 189 static_cast<safe_browsing::DownloadProtectionService*>(NULL))); |
| 190 ON_CALL(*this, ReserveVirtualPath(_, _, _, _)) |
| 191 .WillByDefault(Invoke( |
| 192 &MockDownloadTargetDeterminerDelegate::NullReserveVirtualPath)); |
| 193 } |
| 194 private: |
| 195 static void NullReserveVirtualPath( |
| 196 DownloadItem* download, |
| 197 const base::FilePath& virtual_path, |
| 198 bool override, |
| 199 const DownloadTargetDeterminerDelegate::ReservedPathCallback& callback); |
| 200 }; |
| 201 |
| 202 class DownloadTargetDeterminerTest : public ChromeRenderViewHostTestHarness { |
| 203 public: |
| 204 DownloadTargetDeterminerTest(); |
| 205 |
| 206 // ::testing::Test |
| 207 virtual void SetUp() OVERRIDE; |
| 208 virtual void TearDown() OVERRIDE; |
| 209 |
| 210 // Creates MockDownloadItem and sets up default expectations. |
| 211 content::MockDownloadItem* CreateActiveDownloadItem(int32 id); |
| 212 |
| 213 // Sets the AutoOpenBasedOnExtension user preference for |path|. |
| 214 void EnableAutoOpenBasedOnExtension(const base::FilePath& path); |
| 215 |
| 216 // Set the kDownloadDefaultDirectory user preference to |path|. |
| 217 void SetDefaultDownloadPath(const base::FilePath& path); |
| 218 |
| 219 // Set the kDownloadDefaultDirectory managed preference to |path|. |
| 220 void SetManagedDownloadPath(const base::FilePath& path); |
| 221 |
| 222 // Set the kPromptForDownload user preference to |prompt|. |
| 223 void SetPromptForDownload(bool prompt); |
| 224 |
| 225 // Given the relative path |path|, returns the full path under the temporary |
| 226 // downloads directory. |
| 227 base::FilePath GetPathInDownloadDir(const base::FilePath::StringType& path); |
| 228 |
| 229 // Run |test_case| using |item|. |
| 230 void RunTestCaseWithDownloadItem(const DownloadTestCase& test_case, |
| 231 content::MockDownloadItem* item); |
| 232 |
| 233 // Run through |test_case_count| tests in |test_cases|. A new MockDownloadItem |
| 234 // will be created for each test case and destroyed when the test case is |
| 235 // complete. |
| 236 void RunTestCases(const DownloadTestCase test_cases[], |
| 237 size_t test_case_count); |
| 238 |
| 239 const base::FilePath& test_download_dir() const { |
| 240 return test_download_dir_.path(); |
| 241 } |
| 242 |
| 243 const base::FilePath& test_virtual_dir() const { |
| 244 return test_virtual_dir_; |
| 245 } |
| 246 |
| 247 MockDownloadTargetDeterminerDelegate* delegate() { |
| 248 return &delegate_; |
| 249 } |
| 250 |
| 251 DownloadPrefs* download_prefs() { |
| 252 return download_prefs_.get(); |
| 253 } |
| 254 |
| 255 void set_last_selected_directory(const base::FilePath& path) { |
| 256 last_selected_directory_ = path; |
| 257 } |
| 258 |
| 259 private: |
| 260 // Verifies that |target_path|, |disposition|, |expected_danger_type| and |
| 261 // |intermediate_path| matches the expectations of |test_case|. |
| 262 void DownloadTargetVerifier(const DownloadTestCase& test_case, |
| 263 const base::FilePath& virtual_path, |
| 264 const base::FilePath& local_path, |
| 265 const base::FilePath& intermediate_path, |
| 266 DownloadItem::TargetDisposition disposition, |
| 267 content::DownloadDangerType danger_type); |
| 268 |
| 269 scoped_ptr<DownloadPrefs> download_prefs_; |
| 270 ::testing::StrictMock<MockDownloadTargetDeterminerDelegate> delegate_; |
| 271 NullWebContentsDelegate web_contents_delegate_; |
| 272 base::ScopedTempDir test_download_dir_; |
| 273 base::FilePath test_virtual_dir_; |
| 274 base::FilePath last_selected_directory_; |
| 275 content::TestBrowserThread ui_thread_; |
| 276 content::TestBrowserThread file_thread_; |
| 277 }; |
| 278 |
| 279 DownloadTargetDeterminerTest::DownloadTargetDeterminerTest() |
| 280 : ChromeRenderViewHostTestHarness(), |
| 281 ui_thread_(content::BrowserThread::UI, &message_loop_), |
| 282 file_thread_(content::BrowserThread::FILE, &message_loop_) { |
| 283 } |
| 284 |
| 285 void DownloadTargetDeterminerTest::SetUp() { |
| 286 ChromeRenderViewHostTestHarness::SetUp(); |
| 287 CHECK(profile()); |
| 288 download_prefs_.reset(new DownloadPrefs(profile())); |
| 289 web_contents()->SetDelegate(&web_contents_delegate_); |
| 290 ASSERT_TRUE(test_download_dir_.CreateUniqueTempDir()); |
| 291 test_virtual_dir_ = test_download_dir().Append(FILE_PATH_LITERAL("virtual")); |
| 292 SetDefaultDownloadPath(test_download_dir()); |
| 293 delegate_.SetupDefaults(); |
| 294 } |
| 295 |
| 296 void DownloadTargetDeterminerTest::TearDown() { |
| 297 download_prefs_.reset(); |
| 298 message_loop_.RunUntilIdle(); |
| 299 ChromeRenderViewHostTestHarness::TearDown(); |
| 300 } |
| 301 |
| 302 content::MockDownloadItem* |
| 303 DownloadTargetDeterminerTest::CreateActiveDownloadItem(int32 id) { |
| 304 content::MockDownloadItem* item = |
| 305 new ::testing::NiceMock<content::MockDownloadItem>(); |
| 306 ON_CALL(*item, GetDangerType()) |
| 307 .WillByDefault(Return(content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS)); |
| 308 ON_CALL(*item, GetFullPath()) |
| 309 .WillByDefault(ReturnRefOfCopy(base::FilePath())); |
| 310 ON_CALL(*item, GetHash()) |
| 311 .WillByDefault(ReturnRefOfCopy(std::string())); |
| 312 ON_CALL(*item, GetReferrerUrl()) |
| 313 .WillByDefault(ReturnRefOfCopy(GURL())); |
| 314 ON_CALL(*item, GetTargetFilePath()) |
| 315 .WillByDefault(ReturnRefOfCopy(base::FilePath())); |
| 316 ON_CALL(*item, GetTransitionType()) |
| 317 .WillByDefault(Return(content::PAGE_TRANSITION_LINK)); |
| 318 ON_CALL(*item, GetWebContents()) |
| 319 .WillByDefault(Return(web_contents())); |
| 320 ON_CALL(*item, HasUserGesture()) |
| 321 .WillByDefault(Return(false)); |
| 322 ON_CALL(*item, IsDangerous()) |
| 323 .WillByDefault(Return(false)); |
| 324 ON_CALL(*item, IsInProgress()) |
| 325 .WillByDefault(Return(true)); |
| 326 ON_CALL(*item, IsTemporary()) |
| 327 .WillByDefault(Return(false)); |
| 328 EXPECT_CALL(*item, GetBrowserContext()) |
| 329 .WillRepeatedly(Return(profile())); |
| 330 EXPECT_CALL(*item, GetId()) |
| 331 .WillRepeatedly(Return(id)); |
| 332 EXPECT_CALL(*item, GetState()) |
| 333 .WillRepeatedly(Return(DownloadItem::IN_PROGRESS)); |
| 334 EXPECT_CALL(*item, AddObserver(_)).WillRepeatedly(Return()); |
| 335 EXPECT_CALL(*item, RemoveObserver(_)).WillRepeatedly(Return()); |
| 336 return item; |
| 337 } |
| 338 |
| 339 void DownloadTargetDeterminerTest::EnableAutoOpenBasedOnExtension( |
| 340 const base::FilePath& path) { |
| 341 EXPECT_TRUE(download_prefs_->EnableAutoOpenBasedOnExtension(path)); |
| 342 } |
| 343 |
| 344 void DownloadTargetDeterminerTest::SetDefaultDownloadPath( |
| 345 const base::FilePath& path) { |
| 346 profile()->GetTestingPrefService()-> |
| 347 SetFilePath(prefs::kDownloadDefaultDirectory, path); |
| 348 } |
| 349 |
| 350 void DownloadTargetDeterminerTest::SetManagedDownloadPath( |
| 351 const base::FilePath& path) { |
| 352 profile()->GetTestingPrefService()-> |
| 353 SetManagedPref(prefs::kDownloadDefaultDirectory, |
| 354 base::CreateFilePathValue(path)); |
| 355 } |
| 356 |
| 357 void DownloadTargetDeterminerTest::SetPromptForDownload(bool prompt) { |
| 358 profile()->GetTestingPrefService()-> |
| 359 SetBoolean(prefs::kPromptForDownload, prompt); |
| 360 } |
| 361 |
| 362 base::FilePath DownloadTargetDeterminerTest::GetPathInDownloadDir( |
| 363 const base::FilePath::StringType& relative_path) { |
| 364 if (relative_path.empty()) |
| 365 return base::FilePath(); |
| 366 base::FilePath full_path(test_download_dir().Append(relative_path)); |
| 367 return full_path.NormalizePathSeparators(); |
| 368 } |
| 369 |
| 370 void DownloadTargetDeterminerTest::RunTestCaseWithDownloadItem( |
| 371 const DownloadTestCase& test_case, |
| 372 content::MockDownloadItem* item) { |
| 373 // SetUp DownloadItem |
| 374 GURL download_url(test_case.url); |
| 375 std::vector<GURL> url_chain; |
| 376 url_chain.push_back(download_url); |
| 377 base::FilePath expected_generated_path = |
| 378 GetPathInDownloadDir(test_case.expected_generated_path); |
| 379 base::FilePath forced_file_path = |
| 380 GetPathInDownloadDir(test_case.forced_file_path); |
| 381 base::FilePath expected_virtual_path = |
| 382 GetPathInDownloadDir(test_case.expected_virtual_path); |
| 383 base::FilePath expected_local_path = |
| 384 GetPathInDownloadDir(test_case.expected_local_path); |
| 385 // If the virtual path is empty, we assume it is the same as the local path. |
| 386 if (expected_virtual_path.empty()) |
| 387 expected_virtual_path = expected_local_path; |
| 388 if (test_case.test_type == FORCED) |
| 389 ASSERT_FALSE(forced_file_path.empty()); |
| 390 |
| 391 // Expectations for the DownloadItem: |
| 392 EXPECT_CALL(*item, GetReferrerUrl()) |
| 393 .WillRepeatedly(ReturnRef(download_url)); |
| 394 EXPECT_CALL(*item, GetURL()) |
| 395 .WillRepeatedly(ReturnRef(download_url)); |
| 396 EXPECT_CALL(*item, GetUrlChain()) |
| 397 .WillRepeatedly(ReturnRef(url_chain)); |
| 398 EXPECT_CALL(*item, GetForcedFilePath()) |
| 399 .WillRepeatedly(ReturnRef(forced_file_path)); |
| 400 EXPECT_CALL(*item, GetMimeType()) |
| 401 .WillRepeatedly(Return(test_case.mime_type)); |
| 402 DownloadItem::TargetDisposition initial_disposition = |
| 403 (test_case.test_type == SAVE_AS) ? |
| 404 DownloadItem::TARGET_DISPOSITION_PROMPT : |
| 405 DownloadItem::TARGET_DISPOSITION_OVERWRITE; |
| 406 EXPECT_CALL(*item, GetTargetDisposition()) |
| 407 .WillRepeatedly(Return(initial_disposition)); |
| 408 |
| 409 // Expectations for the delegate: |
| 410 if (item->IsInProgress()) { |
| 411 bool will_prompt = (test_case.expected_disposition == |
| 412 DownloadItem::TARGET_DISPOSITION_PROMPT); |
| 413 bool has_virtual_path = !expected_virtual_path.empty(); |
| 414 bool needs_local_path = !will_prompt && has_virtual_path; |
| 415 if (will_prompt) { |
| 416 EXPECT_CALL(*delegate(), |
| 417 PromptUserForDownloadPath(item, expected_generated_path, _)) |
| 418 .WillOnce(WithArg<2>(ScheduleCallback2(expected_virtual_path, |
| 419 expected_local_path))); |
| 420 } else { |
| 421 EXPECT_CALL(*delegate(), PromptUserForDownloadPath(_, _, _)) |
| 422 .Times(0); |
| 423 } |
| 424 if (needs_local_path) { |
| 425 EXPECT_CALL(*delegate(), |
| 426 DetermineLocalPath(item, expected_virtual_path, _)) |
| 427 .WillOnce(WithArg<2>(ScheduleCallback(expected_local_path))); |
| 428 } |
| 429 EXPECT_CALL(*delegate(), |
| 430 ReserveVirtualPath(item, expected_generated_path, |
| 431 forced_file_path.empty(), _)); |
| 432 EXPECT_CALL(*delegate(), GetExtensionEventRouter()); |
| 433 } |
| 434 EXPECT_CALL(*delegate(), GetDownloadProtectionService()) |
| 435 .Times(AnyNumber()); |
| 436 |
| 437 // Kick off the test. |
| 438 base::WeakPtrFactory<DownloadTargetDeterminerTest> factory(this); |
| 439 DownloadTargetDeterminer::Start( |
| 440 item, download_prefs_.get(), last_selected_directory_, delegate(), |
| 441 base::Bind(&DownloadTargetDeterminerTest::DownloadTargetVerifier, |
| 442 factory.GetWeakPtr(), test_case)); |
| 443 message_loop_.RunUntilIdle(); |
| 444 ::testing::Mock::VerifyAndClearExpectations(delegate()); |
| 445 } |
| 446 |
| 447 void DownloadTargetDeterminerTest::RunTestCases( |
| 448 const DownloadTestCase test_cases[], |
| 449 size_t test_case_count) { |
| 450 for (size_t i = 0; i < test_case_count; ++i) { |
| 451 scoped_ptr<content::MockDownloadItem> item(CreateActiveDownloadItem(i)); |
| 452 SCOPED_TRACE(testing::Message() << "Running test case " << i); |
| 453 RunTestCaseWithDownloadItem(test_cases[i], item.get()); |
| 454 } |
| 455 } |
| 456 |
| 457 void DownloadTargetDeterminerTest::DownloadTargetVerifier( |
| 458 const DownloadTestCase& test_case, |
| 459 const base::FilePath& virtual_path, |
| 460 const base::FilePath& local_path, |
| 461 const base::FilePath& intermediate_path, |
| 462 DownloadItem::TargetDisposition disposition, |
| 463 content::DownloadDangerType danger_type) { |
| 464 base::FilePath expected_local_path( |
| 465 GetPathInDownloadDir(test_case.expected_local_path)); |
| 466 base::FilePath expected_virtual_path( |
| 467 GetPathInDownloadDir(test_case.expected_virtual_path)); |
| 468 if (expected_virtual_path.empty()) |
| 469 expected_virtual_path = expected_local_path; |
| 470 EXPECT_EQ(expected_virtual_path.value(), virtual_path.value()); |
| 471 EXPECT_EQ(expected_local_path.value(), local_path.value()); |
| 472 EXPECT_EQ(test_case.expected_disposition, disposition); |
| 473 EXPECT_EQ(test_case.expected_danger_type, danger_type); |
| 474 |
| 475 switch (test_case.expected_intermediate) { |
| 476 case EXPECT_CRDOWNLOAD: |
| 477 EXPECT_EQ(download_util::GetCrDownloadPath(local_path).value(), |
| 478 intermediate_path.value()); |
| 479 break; |
| 480 |
| 481 case EXPECT_UNCONFIRMED: |
| 482 // The paths (in English) look like: /path/Unconfirmed xxx.crdownload. |
| 483 // Of this, we only check that the path is: |
| 484 // 1. Not "/path/target.crdownload", |
| 485 // 2. Points to the same directory as the target. |
| 486 // 3. Has extension ".crdownload". |
| 487 EXPECT_NE(download_util::GetCrDownloadPath(expected_local_path).value(), |
| 488 intermediate_path.value()); |
| 489 EXPECT_EQ(expected_local_path.DirName().value(), |
| 490 intermediate_path.DirName().value()); |
| 491 EXPECT_TRUE(intermediate_path.MatchesExtension( |
| 492 FILE_PATH_LITERAL(".crdownload"))); |
| 493 break; |
| 494 |
| 495 case EXPECT_LOCAL_PATH: |
| 496 EXPECT_EQ(expected_local_path.value(), intermediate_path.value()); |
| 497 break; |
| 498 } |
| 499 } |
| 500 |
| 501 // static |
| 502 void MockDownloadTargetDeterminerDelegate::NullReserveVirtualPath( |
| 503 DownloadItem* download, |
| 504 const base::FilePath& virtual_path, |
| 505 bool override, |
| 506 const DownloadTargetDeterminerDelegate::ReservedPathCallback& callback) { |
| 507 callback.Run(virtual_path, true); |
| 508 } |
| 509 |
| 510 TEST_F(DownloadTargetDeterminerTest, TargetDeterminer_Basic) { |
| 511 const DownloadTestCase kBasicTestCases[] = { |
| 512 { |
| 513 // 0: Automatic Safe |
| 514 AUTOMATIC, |
| 515 content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, |
| 516 "http://example.com/foo.txt", "text/plain", |
| 517 FILE_PATH_LITERAL(""), |
| 518 |
| 519 FILE_PATH_LITERAL(""), |
| 520 FILE_PATH_LITERAL("foo.txt"), |
| 521 FILE_PATH_LITERAL("foo.txt"), |
| 522 DownloadItem::TARGET_DISPOSITION_OVERWRITE, |
| 523 |
| 524 EXPECT_CRDOWNLOAD |
| 525 }, |
| 526 |
| 527 { |
| 528 // 1: Save_As Safe |
| 529 SAVE_AS, |
| 530 content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, |
| 531 "http://example.com/foo.txt", "text/plain", |
| 532 FILE_PATH_LITERAL(""), |
| 533 |
| 534 FILE_PATH_LITERAL(""), |
| 535 FILE_PATH_LITERAL("foo.txt"), |
| 536 FILE_PATH_LITERAL("foo.txt"), |
| 537 DownloadItem::TARGET_DISPOSITION_PROMPT, |
| 538 |
| 539 EXPECT_CRDOWNLOAD |
| 540 }, |
| 541 |
| 542 { |
| 543 // 2: Automatic Dangerous |
| 544 AUTOMATIC, |
| 545 content::DOWNLOAD_DANGER_TYPE_DANGEROUS_FILE, |
| 546 "http://example.com/foo.html", "", |
| 547 FILE_PATH_LITERAL(""), |
| 548 |
| 549 FILE_PATH_LITERAL(""), |
| 550 FILE_PATH_LITERAL("foo.html"), |
| 551 FILE_PATH_LITERAL("foo.html"), |
| 552 DownloadItem::TARGET_DISPOSITION_OVERWRITE, |
| 553 |
| 554 EXPECT_UNCONFIRMED |
| 555 }, |
| 556 |
| 557 { |
| 558 // 3: Forced Safe |
| 559 FORCED, |
| 560 content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, |
| 561 "http://example.com/foo.txt", "", |
| 562 FILE_PATH_LITERAL("forced-foo.txt"), |
| 563 |
| 564 FILE_PATH_LITERAL(""), |
| 565 FILE_PATH_LITERAL("forced-foo.txt"), |
| 566 FILE_PATH_LITERAL("forced-foo.txt"), |
| 567 DownloadItem::TARGET_DISPOSITION_OVERWRITE, |
| 568 |
| 569 EXPECT_LOCAL_PATH |
| 570 }, |
| 571 }; |
| 572 |
| 573 // The test assumes that .html files have a danger level of |
| 574 // AllowOnUserGesture. |
| 575 ASSERT_EQ(download_util::AllowOnUserGesture, |
| 576 download_util::GetFileDangerLevel( |
| 577 base::FilePath(FILE_PATH_LITERAL("foo.html")))); |
| 578 RunTestCases(kBasicTestCases, arraysize(kBasicTestCases)); |
| 579 } |
| 580 |
| 581 TEST_F(DownloadTargetDeterminerTest, TargetDeterminer_CancelSaveAs) { |
| 582 const DownloadTestCase kCancelSaveAsTestCases[] = { |
| 583 { |
| 584 // 2: Save_As Safe, Cancelled. |
| 585 SAVE_AS, |
| 586 content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, |
| 587 "http://example.com/foo.txt", "text/plain", |
| 588 FILE_PATH_LITERAL(""), |
| 589 |
| 590 FILE_PATH_LITERAL(""), |
| 591 FILE_PATH_LITERAL(""), |
| 592 FILE_PATH_LITERAL("foo.txt"), |
| 593 DownloadItem::TARGET_DISPOSITION_PROMPT, |
| 594 |
| 595 EXPECT_LOCAL_PATH |
| 596 } |
| 597 }; |
| 598 RunTestCases(kCancelSaveAsTestCases, arraysize(kCancelSaveAsTestCases)); |
| 599 } |
| 600 |
| 601 #if defined(FULL_SAFE_BROWSING) |
| 602 TEST_F(DownloadTargetDeterminerTest, TargetDeterminer_MaybeDangerous) { |
| 603 const DownloadTestCase kMaybeDangerousTestCases[] = { |
| 604 // These test cases are run with foo.txt being considered a filetype |
| 605 // supported by safe browsing. |
| 606 { |
| 607 // 0: Forced. |
| 608 FORCED, |
| 609 content::DOWNLOAD_DANGER_TYPE_MAYBE_DANGEROUS_CONTENT, |
| 610 "http://example.com/foo.txt", "", |
| 611 FILE_PATH_LITERAL("foo.txt"), |
| 612 |
| 613 FILE_PATH_LITERAL(""), |
| 614 FILE_PATH_LITERAL("foo.txt"), |
| 615 FILE_PATH_LITERAL("foo.txt"), |
| 616 DownloadItem::TARGET_DISPOSITION_OVERWRITE, |
| 617 |
| 618 EXPECT_UNCONFIRMED |
| 619 }, |
| 620 |
| 621 { |
| 622 // 1: Save_As. |
| 623 SAVE_AS, |
| 624 content::DOWNLOAD_DANGER_TYPE_MAYBE_DANGEROUS_CONTENT, |
| 625 "http://example.com/foo.txt", "", |
| 626 FILE_PATH_LITERAL(""), |
| 627 |
| 628 FILE_PATH_LITERAL(""), |
| 629 FILE_PATH_LITERAL("foo.txt"), |
| 630 FILE_PATH_LITERAL("foo.txt"), |
| 631 DownloadItem::TARGET_DISPOSITION_PROMPT, |
| 632 |
| 633 EXPECT_UNCONFIRMED |
| 634 }, |
| 635 |
| 636 { |
| 637 // 2: Automatic. |
| 638 AUTOMATIC, |
| 639 content::DOWNLOAD_DANGER_TYPE_MAYBE_DANGEROUS_CONTENT, |
| 640 "http://example.com/foo.txt", "", |
| 641 FILE_PATH_LITERAL("foo.txt"), |
| 642 |
| 643 FILE_PATH_LITERAL(""), |
| 644 FILE_PATH_LITERAL("foo.txt"), |
| 645 FILE_PATH_LITERAL("foo.txt"), |
| 646 DownloadItem::TARGET_DISPOSITION_OVERWRITE, |
| 647 |
| 648 EXPECT_UNCONFIRMED |
| 649 }, |
| 650 |
| 651 { |
| 652 // 3: Unsupported filetype. |
| 653 AUTOMATIC, |
| 654 content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, |
| 655 "http://example.com/foo-unsupported.txt", "", |
| 656 FILE_PATH_LITERAL(""), |
| 657 |
| 658 FILE_PATH_LITERAL(""), |
| 659 FILE_PATH_LITERAL("foo-unsupported.txt"), |
| 660 FILE_PATH_LITERAL("foo-unsupported.txt"), |
| 661 DownloadItem::TARGET_DISPOSITION_OVERWRITE, |
| 662 |
| 663 EXPECT_CRDOWNLOAD |
| 664 }, |
| 665 }; |
| 666 |
| 667 base::FilePath supported_path( |
| 668 GetPathInDownloadDir(FILE_PATH_LITERAL("foo.txt"))); |
| 669 base::FilePath unsupported_path( |
| 670 GetPathInDownloadDir(FILE_PATH_LITERAL("foo-unsupported.txt"))); |
| 671 scoped_ptr<TestDownloadProtectionService> service( |
| 672 new TestDownloadProtectionService); |
| 673 service->SetupDefaults(); |
| 674 ON_CALL(*delegate(), GetDownloadProtectionService()) |
| 675 .WillByDefault(Return(service.get())); |
| 676 EXPECT_CALL(*service.get(), IsSupportedDownload(_, supported_path)) |
| 677 .WillRepeatedly(Return(true)); |
| 678 EXPECT_CALL(*service.get(), IsSupportedDownload(_, unsupported_path)) |
| 679 .WillRepeatedly(Return(false)); |
| 680 EXPECT_CALL(*service.get(), CheckDownloadUrl(_, _)) |
| 681 .Times(arraysize(kMaybeDangerousTestCases)); |
| 682 RunTestCases(kMaybeDangerousTestCases, arraysize(kMaybeDangerousTestCases)); |
| 683 } |
| 684 |
| 685 // The SafeBrowsing URL check is performed early. Make sure that a download item |
| 686 // that has been marked as DANGEROUS_URL behaves correctly. |
| 687 TEST_F(DownloadTargetDeterminerTest, TargetDeterminer_DangerousURL) { |
| 688 const DownloadTestCase kDangerousURLTestCases[] = { |
| 689 { |
| 690 // 0: Automatic Dangerous URL |
| 691 AUTOMATIC, |
| 692 content::DOWNLOAD_DANGER_TYPE_DANGEROUS_URL, |
| 693 "http://phishing.example.com/foo.txt", "", |
| 694 FILE_PATH_LITERAL(""), |
| 695 |
| 696 FILE_PATH_LITERAL(""), |
| 697 FILE_PATH_LITERAL("foo.txt"), |
| 698 FILE_PATH_LITERAL("foo.txt"), |
| 699 DownloadItem::TARGET_DISPOSITION_OVERWRITE, |
| 700 |
| 701 EXPECT_UNCONFIRMED |
| 702 }, |
| 703 |
| 704 { |
| 705 // 1: Save As Dangerous URL |
| 706 SAVE_AS, |
| 707 content::DOWNLOAD_DANGER_TYPE_DANGEROUS_URL, |
| 708 "http://phishing.example.com/foo.txt", "", |
| 709 FILE_PATH_LITERAL(""), |
| 710 |
| 711 FILE_PATH_LITERAL(""), |
| 712 FILE_PATH_LITERAL("foo.txt"), |
| 713 FILE_PATH_LITERAL("foo.txt"), |
| 714 DownloadItem::TARGET_DISPOSITION_PROMPT, |
| 715 |
| 716 EXPECT_UNCONFIRMED |
| 717 }, |
| 718 |
| 719 { |
| 720 // 2: Forced Dangerous URL |
| 721 FORCED, |
| 722 content::DOWNLOAD_DANGER_TYPE_DANGEROUS_URL, |
| 723 "http://phishing.example.com/foo.txt", "", |
| 724 FILE_PATH_LITERAL("forced-foo.txt"), |
| 725 |
| 726 FILE_PATH_LITERAL(""), |
| 727 FILE_PATH_LITERAL("forced-foo.txt"), |
| 728 FILE_PATH_LITERAL("forced-foo.txt"), |
| 729 DownloadItem::TARGET_DISPOSITION_OVERWRITE, |
| 730 |
| 731 EXPECT_UNCONFIRMED |
| 732 }, |
| 733 |
| 734 { |
| 735 // 3: Automatic Dangerous URL + Dangerous file. Dangerous URL takes |
| 736 // precendence. |
| 737 AUTOMATIC, |
| 738 content::DOWNLOAD_DANGER_TYPE_DANGEROUS_URL, |
| 739 "http://phishing.example.com/foo.html", "", |
| 740 FILE_PATH_LITERAL(""), |
| 741 |
| 742 FILE_PATH_LITERAL(""), |
| 743 FILE_PATH_LITERAL("foo.html"), |
| 744 FILE_PATH_LITERAL("foo.html"), |
| 745 DownloadItem::TARGET_DISPOSITION_OVERWRITE, |
| 746 |
| 747 EXPECT_UNCONFIRMED |
| 748 }, |
| 749 |
| 750 { |
| 751 // 4: Save As Dangerous URL + Dangerous file |
| 752 SAVE_AS, |
| 753 content::DOWNLOAD_DANGER_TYPE_DANGEROUS_URL, |
| 754 "http://phishing.example.com/foo.html", "", |
| 755 FILE_PATH_LITERAL(""), |
| 756 |
| 757 FILE_PATH_LITERAL(""), |
| 758 FILE_PATH_LITERAL("foo.html"), |
| 759 FILE_PATH_LITERAL("foo.html"), |
| 760 DownloadItem::TARGET_DISPOSITION_PROMPT, |
| 761 |
| 762 EXPECT_UNCONFIRMED |
| 763 }, |
| 764 |
| 765 { |
| 766 // 5: Forced Dangerous URL + Dangerous file |
| 767 FORCED, |
| 768 content::DOWNLOAD_DANGER_TYPE_DANGEROUS_URL, |
| 769 "http://phishing.example.com/foo.html", "", |
| 770 FILE_PATH_LITERAL("forced-foo.html"), |
| 771 |
| 772 FILE_PATH_LITERAL(""), |
| 773 FILE_PATH_LITERAL("forced-foo.html"), |
| 774 FILE_PATH_LITERAL("forced-foo.html"), |
| 775 DownloadItem::TARGET_DISPOSITION_OVERWRITE, |
| 776 |
| 777 EXPECT_UNCONFIRMED |
| 778 }, |
| 779 }; |
| 780 |
| 781 scoped_ptr<TestDownloadProtectionService> service( |
| 782 new ::testing::StrictMock<TestDownloadProtectionService>()); |
| 783 service->SetupDefaults(); |
| 784 ON_CALL(*delegate(), GetDownloadProtectionService()) |
| 785 .WillByDefault(Return(service.get())); |
| 786 EXPECT_CALL(*service.get(), IsSupportedDownload(_, _)) |
| 787 .Times(AnyNumber()); |
| 788 EXPECT_CALL(*service.get(), CheckDownloadUrl(_, _)) |
| 789 .Times(AnyNumber()) |
| 790 .WillRepeatedly(WithArg<1>(ScheduleCallback( |
| 791 safe_browsing::DownloadProtectionService::DANGEROUS))); |
| 792 RunTestCases(kDangerousURLTestCases, arraysize(kDangerousURLTestCases)); |
| 793 } |
| 794 #endif // FULL_SAFE_BROWSING |
| 795 |
| 796 // Test whether the last saved directory is used for 'Save As' downloads. |
| 797 TEST_F(DownloadTargetDeterminerTest, TargetDeterminer_LastSavePath) { |
| 798 const DownloadTestCase kLastSavePathTestCasesPre[] = { |
| 799 { |
| 800 // 0: If the last save path is empty, then the default download directory |
| 801 // should be used. |
| 802 SAVE_AS, |
| 803 content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, |
| 804 "http://example.com/foo.txt", "text/plain", |
| 805 FILE_PATH_LITERAL(""), |
| 806 |
| 807 FILE_PATH_LITERAL(""), |
| 808 FILE_PATH_LITERAL("bar.txt"), |
| 809 FILE_PATH_LITERAL("foo.txt"), |
| 810 DownloadItem::TARGET_DISPOSITION_PROMPT, |
| 811 |
| 812 EXPECT_CRDOWNLOAD |
| 813 } |
| 814 }; |
| 815 |
| 816 // These test cases are run with a last save path set to a non-emtpy local |
| 817 // download directory. |
| 818 const DownloadTestCase kLastSavePathTestCasesPost[] = { |
| 819 { |
| 820 // 0: This test case is run with the last download directory set to |
| 821 // '<test_download_dir()>/foo'. |
| 822 SAVE_AS, |
| 823 content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, |
| 824 "http://example.com/foo.txt", "text/plain", |
| 825 FILE_PATH_LITERAL(""), |
| 826 |
| 827 FILE_PATH_LITERAL(""), |
| 828 FILE_PATH_LITERAL("foo/bar.txt"), |
| 829 FILE_PATH_LITERAL("foo/foo.txt"), |
| 830 DownloadItem::TARGET_DISPOSITION_PROMPT, |
| 831 |
| 832 EXPECT_CRDOWNLOAD |
| 833 }, |
| 834 |
| 835 { |
| 836 // 1: Start an automatic download. This should be saved to the user's |
| 837 // default download directory and not the last used Save As directory. |
| 838 AUTOMATIC, |
| 839 content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, |
| 840 "http://example.com/foo.txt", "text/plain", |
| 841 FILE_PATH_LITERAL(""), |
| 842 |
| 843 FILE_PATH_LITERAL(""), |
| 844 FILE_PATH_LITERAL("foo.txt"), |
| 845 FILE_PATH_LITERAL("foo.txt"), |
| 846 DownloadItem::TARGET_DISPOSITION_OVERWRITE, |
| 847 |
| 848 EXPECT_CRDOWNLOAD |
| 849 }, |
| 850 }; |
| 851 |
| 852 // This test case is run with the last save path set to a non-empty virtual |
| 853 // directory. |
| 854 const DownloadTestCase kLastSavePathTestCasesVirtual[] = { |
| 855 { |
| 856 SAVE_AS, |
| 857 content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, |
| 858 "http://example.com/foo.txt", "text/plain", |
| 859 FILE_PATH_LITERAL(""), |
| 860 |
| 861 FILE_PATH_LITERAL("virtual/foo/foo.txt"), |
| 862 FILE_PATH_LITERAL("foo/bar.txt"), |
| 863 FILE_PATH_LITERAL("virtual/foo/foo.txt"), |
| 864 DownloadItem::TARGET_DISPOSITION_PROMPT, |
| 865 |
| 866 EXPECT_LOCAL_PATH |
| 867 }, |
| 868 }; |
| 869 |
| 870 { |
| 871 SCOPED_TRACE(testing::Message() |
| 872 << "Running with empty last_selected_directory"); |
| 873 RunTestCases(kLastSavePathTestCasesPre, |
| 874 arraysize(kLastSavePathTestCasesPre)); |
| 875 } |
| 876 |
| 877 // Try with a non-empty last save path. |
| 878 { |
| 879 SCOPED_TRACE(testing::Message() |
| 880 << "Running with local last_selected_directory"); |
| 881 set_last_selected_directory(test_download_dir().AppendASCII("foo")); |
| 882 RunTestCases(kLastSavePathTestCasesPost, |
| 883 arraysize(kLastSavePathTestCasesPost)); |
| 884 } |
| 885 |
| 886 // And again, but this time use a virtual directory. |
| 887 { |
| 888 SCOPED_TRACE(testing::Message() |
| 889 << "Running with virtual last_selected_directory"); |
| 890 set_last_selected_directory(test_virtual_dir().AppendASCII("foo")); |
| 891 RunTestCases(kLastSavePathTestCasesVirtual, |
| 892 arraysize(kLastSavePathTestCasesVirtual)); |
| 893 } |
| 894 } |
| 895 |
| 896 // These tests are run with the default downloads folder set to a virtual |
| 897 // directory. |
| 898 TEST_F(DownloadTargetDeterminerTest, TargetDeterminer_DefaultVirtual) { |
| 899 const DownloadTestCase kDefaultVirtualTestCases[] = { |
| 900 { |
| 901 // 0: Automatic Safe |
| 902 AUTOMATIC, |
| 903 content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, |
| 904 "http://example.com/foo.txt", "text/plain", |
| 905 FILE_PATH_LITERAL(""), |
| 906 |
| 907 FILE_PATH_LITERAL("virtual/foo.txt"), |
| 908 FILE_PATH_LITERAL("foo-local.txt"), |
| 909 FILE_PATH_LITERAL("virtual/foo.txt"), |
| 910 DownloadItem::TARGET_DISPOSITION_OVERWRITE, |
| 911 |
| 912 EXPECT_LOCAL_PATH |
| 913 }, |
| 914 |
| 915 { |
| 916 // 1: Save_As Safe. |
| 917 SAVE_AS, |
| 918 content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, |
| 919 "http://example.com/foo.txt", "text/plain", |
| 920 FILE_PATH_LITERAL(""), |
| 921 |
| 922 // The response to the download prompt is to choose the 'foo.txt' virtual |
| 923 // path. However, the virtual path shouldn't be passed through |
| 924 // DetermineLocalPath(). So the 'foo-x.txt' local path should make it |
| 925 // through without being modified. |
| 926 FILE_PATH_LITERAL("virtual/foo.txt"), |
| 927 FILE_PATH_LITERAL("foo-x.txt"), |
| 928 |
| 929 // The last selected download path is empty. So 'Save As' should default |
| 930 // to using the virtual path for the suggested path. |
| 931 FILE_PATH_LITERAL("virtual/foo.txt"), |
| 932 |
| 933 DownloadItem::TARGET_DISPOSITION_PROMPT, |
| 934 |
| 935 EXPECT_LOCAL_PATH |
| 936 }, |
| 937 |
| 938 { |
| 939 // 2: Save_As Safe. |
| 940 SAVE_AS, |
| 941 content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, |
| 942 "http://example.com/foo.txt", "text/plain", |
| 943 FILE_PATH_LITERAL(""), |
| 944 |
| 945 // Response to the 'Save As' is to choose the local path for 'foo-x.txt'. |
| 946 FILE_PATH_LITERAL(""), |
| 947 FILE_PATH_LITERAL("foo-x.txt"), |
| 948 |
| 949 // The last selected download path is empty. So 'Save As' should default |
| 950 // to using the virtual path for the suggested path. |
| 951 FILE_PATH_LITERAL("virtual/foo.txt"), |
| 952 |
| 953 DownloadItem::TARGET_DISPOSITION_PROMPT, |
| 954 |
| 955 EXPECT_CRDOWNLOAD |
| 956 }, |
| 957 |
| 958 { |
| 959 // 3: Forced Safe. Doesn't override forced paths. |
| 960 FORCED, |
| 961 content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, |
| 962 "http://example.com/foo.txt", "", |
| 963 FILE_PATH_LITERAL("forced-foo.txt"), |
| 964 |
| 965 FILE_PATH_LITERAL(""), |
| 966 FILE_PATH_LITERAL("forced-foo.txt"), |
| 967 FILE_PATH_LITERAL("forced-foo.txt"), |
| 968 DownloadItem::TARGET_DISPOSITION_OVERWRITE, |
| 969 |
| 970 EXPECT_LOCAL_PATH |
| 971 }, |
| 972 }; |
| 973 |
| 974 for (size_t i = 0; i < arraysize(kDefaultVirtualTestCases); ++i) { |
| 975 scoped_ptr<content::MockDownloadItem> item(CreateActiveDownloadItem(i)); |
| 976 const DownloadTestCase& test_case = kDefaultVirtualTestCases[i]; |
| 977 SCOPED_TRACE(testing::Message() << "Running test case " << i); |
| 978 |
| 979 // The default download directory is the virtual path. |
| 980 SetDefaultDownloadPath(test_virtual_dir()); |
| 981 |
| 982 // The result of calling DetermineLocalPath is 'foo-local.txt' in the local |
| 983 // downloads directory. It should only be called for automatic downloads. |
| 984 if (test_case.test_type == AUTOMATIC) { |
| 985 base::FilePath expected_virtual_path = GetPathInDownloadDir( |
| 986 test_case.expected_virtual_path); |
| 987 EXPECT_CALL(*delegate(), DetermineLocalPath( |
| 988 item.get(), expected_virtual_path, _)) |
| 989 .WillRepeatedly(WithArg<2>(ScheduleCallback( |
| 990 GetPathInDownloadDir(FILE_PATH_LITERAL("foo-local.txt"))))); |
| 991 } else { |
| 992 EXPECT_CALL(*delegate(), DetermineLocalPath(_, _, _)) |
| 993 .Times(0); |
| 994 } |
| 995 RunTestCaseWithDownloadItem(test_case, item.get()); |
| 996 } |
| 997 } |
| 998 |
| 999 // Test that an inactive download will still get a virtual or local download |
| 1000 // path. |
| 1001 TEST_F(DownloadTargetDeterminerTest, TargetDeterminer_InactiveDownload) { |
| 1002 const DownloadTestCase kInactiveTestCases[] = { |
| 1003 { |
| 1004 AUTOMATIC, |
| 1005 content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, |
| 1006 "http://example.com/foo.txt", "text/plain", |
| 1007 FILE_PATH_LITERAL(""), |
| 1008 |
| 1009 FILE_PATH_LITERAL("foo.txt"), |
| 1010 FILE_PATH_LITERAL(""), |
| 1011 FILE_PATH_LITERAL(""), |
| 1012 DownloadItem::TARGET_DISPOSITION_OVERWRITE, |
| 1013 |
| 1014 EXPECT_LOCAL_PATH |
| 1015 }, |
| 1016 |
| 1017 { |
| 1018 SAVE_AS, |
| 1019 content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, |
| 1020 "http://example.com/foo.txt", "text/plain", |
| 1021 FILE_PATH_LITERAL(""), |
| 1022 |
| 1023 FILE_PATH_LITERAL("foo.txt"), |
| 1024 FILE_PATH_LITERAL(""), |
| 1025 // Even though this is a SAVE_AS download, no prompt will be displayed to |
| 1026 // the user because the download is inactive. |
| 1027 FILE_PATH_LITERAL(""), |
| 1028 DownloadItem::TARGET_DISPOSITION_PROMPT, |
| 1029 |
| 1030 EXPECT_LOCAL_PATH |
| 1031 } |
| 1032 }; |
| 1033 |
| 1034 for (size_t i = 0; i < arraysize(kInactiveTestCases); ++i) { |
| 1035 scoped_ptr<content::MockDownloadItem> item(CreateActiveDownloadItem(i)); |
| 1036 SCOPED_TRACE(testing::Message() << "Running test case " << i); |
| 1037 EXPECT_CALL(*item.get(), IsInProgress()) |
| 1038 .WillRepeatedly(Return(false)); |
| 1039 RunTestCaseWithDownloadItem(kInactiveTestCases[i], item.get()); |
| 1040 } |
| 1041 } |
| 1042 |
| 1043 // If the reserved path could not be verified, then the user should see a |
| 1044 // prompt. |
| 1045 TEST_F(DownloadTargetDeterminerTest, TargetDeterminer_ReservationFailed) { |
| 1046 const DownloadTestCase kReservationFailedCases[] = { |
| 1047 { |
| 1048 // 0: Automatic download. Since the reservation fails, the disposition of |
| 1049 // the target is to prompt. Also the returned reserved path is ignored. |
| 1050 AUTOMATIC, |
| 1051 content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, |
| 1052 "http://example.com/foo.txt", "text/plain", |
| 1053 FILE_PATH_LITERAL(""), |
| 1054 |
| 1055 FILE_PATH_LITERAL(""), |
| 1056 FILE_PATH_LITERAL("foo.txt"), |
| 1057 FILE_PATH_LITERAL("foo.txt"), |
| 1058 DownloadItem::TARGET_DISPOSITION_PROMPT, |
| 1059 |
| 1060 EXPECT_CRDOWNLOAD |
| 1061 }, |
| 1062 }; |
| 1063 |
| 1064 // Setup ReserveVirtualPath() to fail. |
| 1065 ON_CALL(*delegate(), ReserveVirtualPath(_, _, _, _)) |
| 1066 .WillByDefault(WithArg<3>(ScheduleCallback2( |
| 1067 GetPathInDownloadDir(FILE_PATH_LITERAL("bar.txt")), false))); |
| 1068 RunTestCases(kReservationFailedCases, arraysize(kReservationFailedCases)); |
| 1069 } |
| 1070 |
| 1071 // If the local path could not be determined, the download should be cancelled. |
| 1072 TEST_F(DownloadTargetDeterminerTest, TargetDeterminer_LocalPathFailed) { |
| 1073 const DownloadTestCase kLocalPathFailedCases[] = { |
| 1074 { |
| 1075 // 0: Automatic download. |
| 1076 AUTOMATIC, |
| 1077 content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, |
| 1078 "http://example.com/foo.txt", "text/plain", |
| 1079 FILE_PATH_LITERAL(""), |
| 1080 |
| 1081 FILE_PATH_LITERAL(""), |
| 1082 FILE_PATH_LITERAL(""), |
| 1083 FILE_PATH_LITERAL("virtual/foo.txt"), |
| 1084 DownloadItem::TARGET_DISPOSITION_OVERWRITE, |
| 1085 |
| 1086 EXPECT_LOCAL_PATH |
| 1087 }, |
| 1088 }; |
| 1089 |
| 1090 base::FilePath expected_virtual_path( |
| 1091 GetPathInDownloadDir(FILE_PATH_LITERAL("virtual/foo.txt"))); |
| 1092 // The default download directory is the virtual path. |
| 1093 SetDefaultDownloadPath(test_virtual_dir()); |
| 1094 // Simulate failed call to DetermineLocalPath. |
| 1095 EXPECT_CALL(*delegate(), DetermineLocalPath( |
| 1096 _, GetPathInDownloadDir(FILE_PATH_LITERAL("virtual/foo.txt")), _)) |
| 1097 .WillOnce(WithArg<2>(ScheduleCallback(base::FilePath()))); |
| 1098 RunTestCases(kLocalPathFailedCases, arraysize(kLocalPathFailedCases)); |
| 1099 } |
| 1100 |
| 1101 // Downloads that have a danger level of AllowOnUserGesture should be marked as |
| 1102 // safe depending on whether there was a user gesture associated with the |
| 1103 // download and whether the referrer was visited prior to today. |
| 1104 TEST_F(DownloadTargetDeterminerTest, TargetDeterminer_VisitedReferrer) { |
| 1105 const DownloadTestCase kVisitedReferrerCases[] = { |
| 1106 // http://visited.example.com/ is added to the history as a visit that |
| 1107 // happened prior to today. |
| 1108 { |
| 1109 // 0: Safe download due to visiting referrer before. |
| 1110 AUTOMATIC, |
| 1111 content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, |
| 1112 "http://visited.example.com/foo.html", "text/html", |
| 1113 FILE_PATH_LITERAL(""), |
| 1114 |
| 1115 FILE_PATH_LITERAL(""), |
| 1116 FILE_PATH_LITERAL("foo.html"), |
| 1117 FILE_PATH_LITERAL("foo.html"), |
| 1118 DownloadItem::TARGET_DISPOSITION_OVERWRITE, |
| 1119 |
| 1120 EXPECT_CRDOWNLOAD |
| 1121 }, |
| 1122 |
| 1123 { |
| 1124 // 1: Dangerous due to not having visited referrer before. |
| 1125 AUTOMATIC, |
| 1126 content::DOWNLOAD_DANGER_TYPE_DANGEROUS_FILE, |
| 1127 "http://not-visited.example.com/foo.html", "text/html", |
| 1128 FILE_PATH_LITERAL(""), |
| 1129 |
| 1130 FILE_PATH_LITERAL(""), |
| 1131 FILE_PATH_LITERAL("foo.html"), |
| 1132 FILE_PATH_LITERAL("foo.html"), |
| 1133 DownloadItem::TARGET_DISPOSITION_OVERWRITE, |
| 1134 |
| 1135 EXPECT_UNCONFIRMED |
| 1136 }, |
| 1137 |
| 1138 { |
| 1139 // 2: Safe because the user is being prompted. |
| 1140 SAVE_AS, |
| 1141 content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, |
| 1142 "http://not-visited.example.com/foo.html", "text/html", |
| 1143 FILE_PATH_LITERAL(""), |
| 1144 |
| 1145 FILE_PATH_LITERAL(""), |
| 1146 FILE_PATH_LITERAL("foo.html"), |
| 1147 FILE_PATH_LITERAL("foo.html"), |
| 1148 DownloadItem::TARGET_DISPOSITION_PROMPT, |
| 1149 |
| 1150 EXPECT_CRDOWNLOAD |
| 1151 }, |
| 1152 |
| 1153 { |
| 1154 // 3: Safe because of forced path. |
| 1155 FORCED, |
| 1156 content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, |
| 1157 "http://not-visited.example.com/foo.html", "text/html", |
| 1158 FILE_PATH_LITERAL("foo.html"), |
| 1159 |
| 1160 FILE_PATH_LITERAL(""), |
| 1161 FILE_PATH_LITERAL("foo.html"), |
| 1162 FILE_PATH_LITERAL("foo.html"), |
| 1163 DownloadItem::TARGET_DISPOSITION_OVERWRITE, |
| 1164 |
| 1165 EXPECT_LOCAL_PATH |
| 1166 }, |
| 1167 }; |
| 1168 |
| 1169 // This test assumes that the danger level of .html files is |
| 1170 // AllowOnUserGesture. |
| 1171 ASSERT_EQ(download_util::AllowOnUserGesture, |
| 1172 download_util::GetFileDangerLevel( |
| 1173 base::FilePath(FILE_PATH_LITERAL("foo.html")))); |
| 1174 |
| 1175 // First the history service must exist. |
| 1176 profile()->CreateHistoryService(false, false); |
| 1177 |
| 1178 GURL url("http://visited.example.com/visited-link.html"); |
| 1179 // The time of visit is picked to be several seconds prior to the most recent |
| 1180 // midnight. |
| 1181 base::Time time_of_visit( |
| 1182 base::Time::Now().LocalMidnight() - base::TimeDelta::FromSeconds(10)); |
| 1183 HistoryService* history_service = |
| 1184 HistoryServiceFactory::GetForProfile(profile(), Profile::EXPLICIT_ACCESS); |
| 1185 ASSERT_TRUE(history_service); |
| 1186 history_service->AddPage(url, time_of_visit, history::SOURCE_BROWSED); |
| 1187 |
| 1188 RunTestCases(kVisitedReferrerCases, arraysize(kVisitedReferrerCases)); |
| 1189 } |
| 1190 |
| 1191 // These test cases are run with "Prompt for download" user preference set to |
| 1192 // true. Even with the preference set, some of these downloads should not cause |
| 1193 // a prompt to appear. |
| 1194 TEST_F(DownloadTargetDeterminerTest, TargetDeterminer_PromptAlways) { |
| 1195 const DownloadTestCase kPromptingTestCases[] = { |
| 1196 { |
| 1197 // 0: Safe Automatic - Should prompt because of "Prompt for download" |
| 1198 // preference setting. |
| 1199 AUTOMATIC, |
| 1200 content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, |
| 1201 "http://example.com/foo.txt", "text/plain", |
| 1202 FILE_PATH_LITERAL(""), |
| 1203 |
| 1204 FILE_PATH_LITERAL(""), |
| 1205 FILE_PATH_LITERAL("foo.txt"), |
| 1206 FILE_PATH_LITERAL("foo.txt"), |
| 1207 DownloadItem::TARGET_DISPOSITION_PROMPT, |
| 1208 |
| 1209 EXPECT_CRDOWNLOAD |
| 1210 }, |
| 1211 |
| 1212 { |
| 1213 // 1: Automatic Browser Extension download. - Shouldn't prompt for browser |
| 1214 // extension downloads even if "Prompt for download" preference is set. |
| 1215 AUTOMATIC, |
| 1216 content::DOWNLOAD_DANGER_TYPE_DANGEROUS_FILE, |
| 1217 "http://example.com/foo.crx", |
| 1218 extensions::Extension::kMimeType, |
| 1219 FILE_PATH_LITERAL(""), |
| 1220 |
| 1221 FILE_PATH_LITERAL(""), |
| 1222 FILE_PATH_LITERAL("foo.crx"), |
| 1223 FILE_PATH_LITERAL("foo.crx"), |
| 1224 DownloadItem::TARGET_DISPOSITION_OVERWRITE, |
| 1225 |
| 1226 EXPECT_UNCONFIRMED |
| 1227 }, |
| 1228 |
| 1229 { |
| 1230 // 2: Safe Forced - Shouldn't prompt. |
| 1231 FORCED, |
| 1232 content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, |
| 1233 "http://example.com/foo.txt", "text/plain", |
| 1234 FILE_PATH_LITERAL("foo.txt"), |
| 1235 |
| 1236 FILE_PATH_LITERAL(""), |
| 1237 FILE_PATH_LITERAL("foo.txt"), |
| 1238 FILE_PATH_LITERAL("foo.txt"), |
| 1239 DownloadItem::TARGET_DISPOSITION_OVERWRITE, |
| 1240 |
| 1241 EXPECT_LOCAL_PATH |
| 1242 }, |
| 1243 |
| 1244 { |
| 1245 // 3: Automatic User Script - Shouldn't prompt for user script downloads |
| 1246 // even if "Prompt for download" preference is set. |
| 1247 AUTOMATIC, |
| 1248 content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, |
| 1249 "http://example.com/foo.user.js", "", |
| 1250 FILE_PATH_LITERAL(""), |
| 1251 |
| 1252 FILE_PATH_LITERAL(""), |
| 1253 FILE_PATH_LITERAL("foo.user.js"), |
| 1254 FILE_PATH_LITERAL("foo.user.js"), |
| 1255 DownloadItem::TARGET_DISPOSITION_OVERWRITE, |
| 1256 |
| 1257 EXPECT_CRDOWNLOAD |
| 1258 }, |
| 1259 |
| 1260 { |
| 1261 // 4: Automatic - The filename extension is marked as one that we will |
| 1262 // open automatically. Shouldn't prompt. |
| 1263 AUTOMATIC, |
| 1264 content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, |
| 1265 "http://example.com/foo.dummy", "", |
| 1266 FILE_PATH_LITERAL(""), |
| 1267 |
| 1268 FILE_PATH_LITERAL(""), |
| 1269 FILE_PATH_LITERAL("foo.dummy"), |
| 1270 FILE_PATH_LITERAL("foo.dummy"), |
| 1271 DownloadItem::TARGET_DISPOSITION_OVERWRITE, |
| 1272 |
| 1273 EXPECT_CRDOWNLOAD |
| 1274 }, |
| 1275 }; |
| 1276 |
| 1277 SetPromptForDownload(true); |
| 1278 EnableAutoOpenBasedOnExtension( |
| 1279 base::FilePath(FILE_PATH_LITERAL("dummy.dummy"))); |
| 1280 RunTestCases(kPromptingTestCases, arraysize(kPromptingTestCases)); |
| 1281 } |
| 1282 |
| 1283 // If the download path is managed, then we don't show any prompts. |
| 1284 // Note that if the download path is managed, then PromptForDownload() is false. |
| 1285 TEST_F(DownloadTargetDeterminerTest, TargetDeterminer_ManagedPath) { |
| 1286 const DownloadTestCase kManagedPathTestCases[] = { |
| 1287 { |
| 1288 // 0: Automatic Safe |
| 1289 AUTOMATIC, |
| 1290 content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, |
| 1291 "http://example.com/foo.txt", "text/plain", |
| 1292 FILE_PATH_LITERAL(""), |
| 1293 |
| 1294 FILE_PATH_LITERAL(""), |
| 1295 FILE_PATH_LITERAL("foo.txt"), |
| 1296 FILE_PATH_LITERAL("foo.txt"), |
| 1297 DownloadItem::TARGET_DISPOSITION_OVERWRITE, |
| 1298 |
| 1299 EXPECT_CRDOWNLOAD |
| 1300 }, |
| 1301 |
| 1302 { |
| 1303 // 1: Save_As Safe |
| 1304 SAVE_AS, |
| 1305 content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, |
| 1306 "http://example.com/foo.txt", "text/plain", |
| 1307 FILE_PATH_LITERAL(""), |
| 1308 |
| 1309 FILE_PATH_LITERAL(""), |
| 1310 FILE_PATH_LITERAL("foo.txt"), |
| 1311 FILE_PATH_LITERAL("foo.txt"), |
| 1312 DownloadItem::TARGET_DISPOSITION_OVERWRITE, |
| 1313 |
| 1314 EXPECT_CRDOWNLOAD |
| 1315 }, |
| 1316 }; |
| 1317 |
| 1318 SetManagedDownloadPath(test_download_dir()); |
| 1319 ASSERT_TRUE(download_prefs()->IsDownloadPathManaged()); |
| 1320 RunTestCases(kManagedPathTestCases, arraysize(kManagedPathTestCases)); |
| 1321 } |
| 1322 |
| 1323 } // namespace |
| OLD | NEW |