| 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 "base/file_util.h" | 5 #include "base/file_util.h" |
| 6 #include "base/files/file_path.h" | 6 #include "base/files/file_path.h" |
| 7 #include "base/files/scoped_temp_dir.h" | 7 #include "base/files/scoped_temp_dir.h" |
| 8 #include "base/message_loop.h" | 8 #include "base/message_loop.h" |
| 9 #include "base/observer_list.h" | 9 #include "base/observer_list.h" |
| 10 #include "base/prefs/pref_service.h" | 10 #include "base/prefs/pref_service.h" |
| (...skipping 14 matching lines...) Expand all Loading... |
| 25 #include "content/public/test/mock_download_item.h" | 25 #include "content/public/test/mock_download_item.h" |
| 26 #include "content/public/test/mock_download_manager.h" | 26 #include "content/public/test/mock_download_manager.h" |
| 27 #include "content/public/test/test_browser_thread.h" | 27 #include "content/public/test/test_browser_thread.h" |
| 28 #include "content/public/test/test_renderer_host.h" | 28 #include "content/public/test/test_renderer_host.h" |
| 29 #include "content/public/test/web_contents_tester.h" | 29 #include "content/public/test/web_contents_tester.h" |
| 30 #include "testing/gmock/include/gmock/gmock.h" | 30 #include "testing/gmock/include/gmock/gmock.h" |
| 31 #include "testing/gtest/include/gtest/gtest.h" | 31 #include "testing/gtest/include/gtest/gtest.h" |
| 32 | 32 |
| 33 using ::testing::AtMost; | 33 using ::testing::AtMost; |
| 34 using ::testing::Invoke; | 34 using ::testing::Invoke; |
| 35 using ::testing::Ref; |
| 35 using ::testing::Return; | 36 using ::testing::Return; |
| 36 using ::testing::ReturnPointee; | 37 using ::testing::ReturnPointee; |
| 37 using ::testing::ReturnRef; | 38 using ::testing::ReturnRef; |
| 38 using ::testing::ReturnRefOfCopy; | 39 using ::testing::ReturnRefOfCopy; |
| 39 using ::testing::SetArgPointee; | 40 using ::testing::SetArgPointee; |
| 40 using ::testing::WithArg; | 41 using ::testing::WithArg; |
| 41 using ::testing::_; | 42 using ::testing::_; |
| 42 using content::DownloadItem; | 43 using content::DownloadItem; |
| 43 using safe_browsing::DownloadProtectionService; | 44 using safe_browsing::DownloadProtectionService; |
| 44 | 45 |
| (...skipping 13 matching lines...) Expand all Loading... |
| 58 // public: | 59 // public: |
| 59 // virtual void Foo(base::Callback<void(bool)> callback); | 60 // virtual void Foo(base::Callback<void(bool)> callback); |
| 60 // }; | 61 // }; |
| 61 // ... | 62 // ... |
| 62 // EXPECT_CALL(mock_fooclass_instance, Foo(callback)) | 63 // EXPECT_CALL(mock_fooclass_instance, Foo(callback)) |
| 63 // .WillOnce(ScheduleCallback(false)); | 64 // .WillOnce(ScheduleCallback(false)); |
| 64 ACTION_P(ScheduleCallback, result) { | 65 ACTION_P(ScheduleCallback, result) { |
| 65 MessageLoop::current()->PostTask(FROM_HERE, base::Bind(arg0, result)); | 66 MessageLoop::current()->PostTask(FROM_HERE, base::Bind(arg0, result)); |
| 66 } | 67 } |
| 67 | 68 |
| 68 // Matches a safe_browsing::DownloadProtectionService::DownloadInfo that has | |
| 69 // |url| as the first URL in the |download_url_chain|. | |
| 70 // Example: | |
| 71 // EXPECT_CALL(Foo(InfoMatchinURL(url))) | |
| 72 MATCHER_P(InfoMatchingURL, url, "DownloadInfo matching URL " + url.spec()) { | |
| 73 return url == arg.download_url_chain.front(); | |
| 74 } | |
| 75 | |
| 76 // Used with DownloadTestCase. Indicates the type of test case. The expectations | 69 // Used with DownloadTestCase. Indicates the type of test case. The expectations |
| 77 // for the test is set based on the type. | 70 // for the test is set based on the type. |
| 78 enum TestCaseType { | 71 enum TestCaseType { |
| 79 SAVE_AS, | 72 SAVE_AS, |
| 80 AUTOMATIC, | 73 AUTOMATIC, |
| 81 FORCED // Requires that forced_file_path be non-empty. | 74 FORCED // Requires that forced_file_path be non-empty. |
| 82 }; | 75 }; |
| 83 | 76 |
| 84 // Used with DownloadTestCase. Indicates whether the a file should be | 77 // Used with DownloadTestCase. Indicates whether the a file should be |
| 85 // overwritten. | 78 // overwritten. |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 138 #if defined(FULL_SAFE_BROWSING) | 131 #if defined(FULL_SAFE_BROWSING) |
| 139 // DownloadProtectionService with mock methods. Since the SafeBrowsingService is | 132 // DownloadProtectionService with mock methods. Since the SafeBrowsingService is |
| 140 // set to NULL, it is not safe to call any non-mocked methods other than | 133 // set to NULL, it is not safe to call any non-mocked methods other than |
| 141 // SetEnabled() and enabled(). | 134 // SetEnabled() and enabled(). |
| 142 class TestDownloadProtectionService | 135 class TestDownloadProtectionService |
| 143 : public safe_browsing::DownloadProtectionService { | 136 : public safe_browsing::DownloadProtectionService { |
| 144 public: | 137 public: |
| 145 TestDownloadProtectionService() | 138 TestDownloadProtectionService() |
| 146 : safe_browsing::DownloadProtectionService(NULL, NULL) {} | 139 : safe_browsing::DownloadProtectionService(NULL, NULL) {} |
| 147 MOCK_METHOD2(CheckClientDownload, | 140 MOCK_METHOD2(CheckClientDownload, |
| 148 void(const DownloadProtectionService::DownloadInfo&, | 141 void(content::DownloadItem*, |
| 149 const DownloadProtectionService::CheckDownloadCallback&)); | 142 const DownloadProtectionService::CheckDownloadCallback&)); |
| 150 MOCK_METHOD2(CheckDownloadUrl, | 143 MOCK_METHOD2(CheckDownloadUrl, |
| 151 void(const DownloadProtectionService::DownloadInfo&, | 144 void(const content::DownloadItem&, |
| 152 const DownloadProtectionService::CheckDownloadCallback&)); | 145 const DownloadProtectionService::CheckDownloadCallback&)); |
| 153 MOCK_CONST_METHOD1(IsSupportedDownload, | 146 MOCK_CONST_METHOD2(IsSupportedDownload, |
| 154 bool(const DownloadProtectionService::DownloadInfo&)); | 147 bool(const content::DownloadItem&, |
| 148 const base::FilePath&)); |
| 155 }; | 149 }; |
| 156 #endif | 150 #endif |
| 157 | 151 |
| 158 // Subclass of the ChromeDownloadManagerDelegate that uses a mock | 152 // Subclass of the ChromeDownloadManagerDelegate that uses a mock |
| 159 // DownloadProtectionService and IsDangerousFile. | 153 // DownloadProtectionService and IsDangerousFile. |
| 160 class TestChromeDownloadManagerDelegate : public ChromeDownloadManagerDelegate { | 154 class TestChromeDownloadManagerDelegate : public ChromeDownloadManagerDelegate { |
| 161 public: | 155 public: |
| 162 explicit TestChromeDownloadManagerDelegate(Profile* profile) | 156 explicit TestChromeDownloadManagerDelegate(Profile* profile) |
| 163 : ChromeDownloadManagerDelegate(profile) { | 157 : ChromeDownloadManagerDelegate(profile) { |
| 164 #if defined(FULL_SAFE_BROWSING) | 158 #if defined(FULL_SAFE_BROWSING) |
| (...skipping 232 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 397 EXPECT_CALL(*download_manager_, GetAllDownloads(_)) | 391 EXPECT_CALL(*download_manager_, GetAllDownloads(_)) |
| 398 .WillRepeatedly(SetArgPointee<0>(items)); | 392 .WillRepeatedly(SetArgPointee<0>(items)); |
| 399 | 393 |
| 400 #if defined(FULL_SAFE_BROWSING) | 394 #if defined(FULL_SAFE_BROWSING) |
| 401 // Results of SafeBrowsing URL check. | 395 // Results of SafeBrowsing URL check. |
| 402 DownloadProtectionService::DownloadCheckResult url_check_result = | 396 DownloadProtectionService::DownloadCheckResult url_check_result = |
| 403 (test_case.danger_type == content::DOWNLOAD_DANGER_TYPE_DANGEROUS_URL) ? | 397 (test_case.danger_type == content::DOWNLOAD_DANGER_TYPE_DANGEROUS_URL) ? |
| 404 DownloadProtectionService::DANGEROUS : | 398 DownloadProtectionService::DANGEROUS : |
| 405 DownloadProtectionService::SAFE; | 399 DownloadProtectionService::SAFE; |
| 406 EXPECT_CALL(*delegate_->test_download_protection_service(), | 400 EXPECT_CALL(*delegate_->test_download_protection_service(), |
| 407 CheckDownloadUrl(InfoMatchingURL(download_url), _)) | 401 CheckDownloadUrl(Ref(*item), _)) |
| 408 .WillOnce(WithArg<1>(ScheduleCallback(url_check_result))); | 402 .WillOnce(WithArg<1>(ScheduleCallback(url_check_result))); |
| 409 | 403 |
| 410 // Downloads that are flagged as DANGEROUS_URL aren't checked for dangerous | 404 // Downloads that are flagged as DANGEROUS_URL aren't checked for dangerous |
| 411 // content. So we never end up calling IsSupportedDownload for them. | 405 // content. So we never end up calling IsSupportedDownload for them. |
| 412 if (test_case.danger_type != content::DOWNLOAD_DANGER_TYPE_DANGEROUS_URL) { | 406 if (test_case.danger_type != content::DOWNLOAD_DANGER_TYPE_DANGEROUS_URL) { |
| 413 bool maybe_dangerous = | 407 bool maybe_dangerous = |
| 414 (test_case.danger_type == | 408 (test_case.danger_type == |
| 415 content::DOWNLOAD_DANGER_TYPE_MAYBE_DANGEROUS_CONTENT); | 409 content::DOWNLOAD_DANGER_TYPE_MAYBE_DANGEROUS_CONTENT); |
| 416 EXPECT_CALL(*delegate_->test_download_protection_service(), | 410 EXPECT_CALL(*delegate_->test_download_protection_service(), |
| 417 IsSupportedDownload(InfoMatchingURL(download_url))) | 411 IsSupportedDownload(Ref(*item), _)) |
| 418 .WillOnce(Return(maybe_dangerous)); | 412 .WillOnce(Return(maybe_dangerous)); |
| 419 } | 413 } |
| 420 #else // FULL_SAFE_BROWSING | 414 #else // FULL_SAFE_BROWSING |
| 421 // If safe browsing is not enabled, then these tests would fail. If such a | 415 // If safe browsing is not enabled, then these tests would fail. If such a |
| 422 // test was added, then fail early. | 416 // test was added, then fail early. |
| 423 EXPECT_NE(content::DOWNLOAD_DANGER_TYPE_DANGEROUS_URL, test_case.danger_type); | 417 EXPECT_NE(content::DOWNLOAD_DANGER_TYPE_DANGEROUS_URL, test_case.danger_type); |
| 424 EXPECT_NE(content::DOWNLOAD_DANGER_TYPE_MAYBE_DANGEROUS_CONTENT, | 418 EXPECT_NE(content::DOWNLOAD_DANGER_TYPE_MAYBE_DANGEROUS_CONTENT, |
| 425 test_case.danger_type); | 419 test_case.danger_type); |
| 426 #endif // !FULL_SAFE_BROWSING | 420 #endif // !FULL_SAFE_BROWSING |
| 427 | 421 |
| (...skipping 495 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 923 // Run the first test case again. Since the last download path was cleared, | 917 // Run the first test case again. Since the last download path was cleared, |
| 924 // this test case should behave identically to the first time it was run. | 918 // this test case should behave identically to the first time it was run. |
| 925 RunTestCases(kLastSavePathTestCases, 1); | 919 RunTestCases(kLastSavePathTestCases, 1); |
| 926 } | 920 } |
| 927 | 921 |
| 928 // TODO(asanka): Add more tests. | 922 // TODO(asanka): Add more tests. |
| 929 // * Default download path is not writable. | 923 // * Default download path is not writable. |
| 930 // * Download path doesn't exist. | 924 // * Download path doesn't exist. |
| 931 // * IsDangerousFile(). | 925 // * IsDangerousFile(). |
| 932 // * Filename generation. | 926 // * Filename generation. |
| OLD | NEW |