Chromium Code Reviews| 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/bind.h" | 5 #include "base/bind.h" |
| 6 #include "base/files/file_path.h" | 6 #include "base/files/file_path.h" |
| 7 #include "base/memory/weak_ptr.h" | |
|
Lei Zhang
2015/11/24 18:55:27
Not used?
Jialiu Lin
2015/11/25 23:43:21
Thanks for catching this. Done.
| |
| 7 #include "chrome/browser/download/download_danger_prompt.h" | 8 #include "chrome/browser/download/download_danger_prompt.h" |
| 8 #include "chrome/browser/profiles/profile.h" | 9 #include "chrome/browser/profiles/profile.h" |
| 10 #include "chrome/browser/safe_browsing/safe_browsing_service.h" | |
| 9 #include "chrome/browser/ui/browser.h" | 11 #include "chrome/browser/ui/browser.h" |
| 10 #include "chrome/browser/ui/browser_commands.h" | 12 #include "chrome/browser/ui/browser_commands.h" |
| 11 #include "chrome/browser/ui/browser_tabstrip.h" | 13 #include "chrome/browser/ui/browser_tabstrip.h" |
| 12 #include "chrome/browser/ui/tabs/tab_strip_model.h" | 14 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
| 15 #include "chrome/common/safe_browsing/csd.pb.h" | |
| 13 #include "chrome/test/base/in_process_browser_test.h" | 16 #include "chrome/test/base/in_process_browser_test.h" |
| 14 #include "chrome/test/base/ui_test_utils.h" | 17 #include "chrome/test/base/ui_test_utils.h" |
| 15 #include "content/public/test/mock_download_item.h" | 18 #include "content/public/test/mock_download_item.h" |
| 16 #include "testing/gmock/include/gmock/gmock.h" | 19 #include "testing/gmock/include/gmock/gmock.h" |
| 17 #include "testing/gtest/include/gtest/gtest.h" | 20 #include "testing/gtest/include/gtest/gtest.h" |
| 18 #include "url/gurl.h" | 21 #include "url/gurl.h" |
| 19 | 22 |
| 20 using ::testing::_; | 23 using ::testing::_; |
| 21 using ::testing::ByRef; | 24 using ::testing::ByRef; |
| 22 using ::testing::Eq; | 25 using ::testing::Eq; |
| 23 using ::testing::Return; | 26 using ::testing::Return; |
| 24 using ::testing::ReturnRef; | 27 using ::testing::ReturnRef; |
| 25 using ::testing::SaveArg; | 28 using ::testing::SaveArg; |
| 29 using safe_browsing::ClientSafeBrowsingReportRequest; | |
| 30 using safe_browsing::SafeBrowsingService; | |
| 31 | |
| 32 static const char* kTestDownloadUrl = "http://evildownload.com"; | |
|
asanka
2015/11/24 18:48:14
Nit: You want 'static const char* const kTestDownl
Lei Zhang
2015/11/24 18:55:27
Or just const char kFoo[]
Jialiu Lin
2015/11/25 23:43:21
Done.
Jialiu Lin
2015/11/25 23:43:21
Thanks, good to know.
Lei Zhang
2015/11/25 23:48:15
In general, prefer const char kFoo[] unless you re
Jialiu Lin
2015/11/26 00:58:58
Done.
| |
| 33 | |
| 34 class FakeSafeBrowsingService : public SafeBrowsingService { | |
| 35 public: | |
| 36 FakeSafeBrowsingService() {} | |
| 37 | |
| 38 void SendDownloadRecoveryReport(const std::string& report) override { | |
| 39 report_ = report; | |
| 40 } | |
| 41 | |
| 42 std::string GetDownloadRecoveryReport() { return report_; } | |
|
Lei Zhang
2015/11/24 18:55:27
const method
Jialiu Lin
2015/11/25 23:43:21
Done.
| |
| 43 | |
| 44 protected: | |
| 45 ~FakeSafeBrowsingService() override {} | |
| 46 | |
| 47 private: | |
| 48 std::string report_; | |
| 49 }; | |
| 50 | |
| 51 // Factory that creates FakeSafeBrowsingService instances. | |
| 52 class TestSafeBrowsingServiceFactory | |
| 53 : public safe_browsing::SafeBrowsingServiceFactory { | |
| 54 public: | |
| 55 TestSafeBrowsingServiceFactory() : fake_safe_browsing_service_(NULL) {} | |
|
asanka
2015/11/24 18:48:14
NULL -> nullptr for new code.
Jialiu Lin
2015/11/25 23:43:21
Done.
| |
| 56 ~TestSafeBrowsingServiceFactory() override {} | |
| 57 | |
| 58 SafeBrowsingService* CreateSafeBrowsingService() override { | |
| 59 fake_safe_browsing_service_ = new FakeSafeBrowsingService(); | |
|
asanka
2015/11/24 18:48:14
Check that fake_safe_browsing_service_ isn't set a
Jialiu Lin
2015/11/25 23:43:21
Added check to see if fake_safe_browsing_service_
| |
| 60 return fake_safe_browsing_service_.get(); | |
| 61 } | |
| 62 | |
| 63 scoped_refptr<FakeSafeBrowsingService> fake_safe_browsing_service() { | |
| 64 return fake_safe_browsing_service_; | |
| 65 } | |
| 66 | |
| 67 private: | |
| 68 scoped_refptr<FakeSafeBrowsingService> fake_safe_browsing_service_; | |
| 69 }; | |
| 26 | 70 |
| 27 class DownloadDangerPromptTest : public InProcessBrowserTest { | 71 class DownloadDangerPromptTest : public InProcessBrowserTest { |
| 28 public: | 72 public: |
| 29 DownloadDangerPromptTest() | 73 DownloadDangerPromptTest() |
| 30 : prompt_(NULL), | 74 : prompt_(NULL), |
|
Lei Zhang
2015/11/24 18:55:27
Also nullptr while you are here?
Jialiu Lin
2015/11/25 23:43:21
Done.
| |
| 31 expected_action_(DownloadDangerPrompt::CANCEL), | 75 expected_action_(DownloadDangerPrompt::CANCEL), |
| 32 did_receive_callback_(false) { | 76 did_receive_callback_(false), |
| 77 safe_browsing_factory_(new TestSafeBrowsingServiceFactory()), | |
| 78 report_sent_(false) { | |
| 79 safe_browsing::SafeBrowsingService::RegisterFactory( | |
|
asanka
2015/11/24 18:48:14
Do this in SetUp().
Jialiu Lin
2015/11/25 23:43:21
Moved to SetUp()
| |
| 80 safe_browsing_factory_.get()); | |
| 33 } | 81 } |
| 34 | 82 |
| 35 ~DownloadDangerPromptTest() override {} | 83 ~DownloadDangerPromptTest() override {} |
| 36 | 84 |
| 37 // Opens a new tab and waits for navigations to finish. If there are pending | 85 // Opens a new tab and waits for navigations to finish. If there are pending |
| 38 // navigations, the constrained prompt might be dismissed when the navigation | 86 // navigations, the constrained prompt might be dismissed when the navigation |
| 39 // completes. | 87 // completes. |
| 40 void OpenNewTab() { | 88 void OpenNewTab() { |
| 41 ui_test_utils::NavigateToURLWithDisposition( | 89 ui_test_utils::NavigateToURLWithDisposition( |
| 42 browser(), GURL("about:blank"), | 90 browser(), GURL("about:blank"), |
| 43 NEW_FOREGROUND_TAB, | 91 NEW_FOREGROUND_TAB, |
| 44 ui_test_utils::BROWSER_TEST_WAIT_FOR_TAB | | 92 ui_test_utils::BROWSER_TEST_WAIT_FOR_TAB | |
| 45 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION); | 93 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION); |
| 46 } | 94 } |
| 47 | 95 |
| 48 void SetUpExpectations(DownloadDangerPrompt::Action expected_action) { | 96 void SetUpExpectations(DownloadDangerPrompt::Action expected_action) { |
| 49 did_receive_callback_ = false; | 97 did_receive_callback_ = false; |
| 50 expected_action_ = expected_action; | 98 expected_action_ = expected_action; |
| 51 SetUpDownloadItemExpectations(); | 99 SetUpDownloadItemExpectations(); |
| 100 SetUpSafeBrowsingReportExpectations(expected_action == | |
| 101 DownloadDangerPrompt::ACCEPT); | |
| 52 CreatePrompt(); | 102 CreatePrompt(); |
| 103 report_sent_ = false; | |
| 53 } | 104 } |
| 54 | 105 |
| 55 void VerifyExpectations() { | 106 void VerifyExpectations() { |
| 56 content::RunAllPendingInMessageLoop(); | 107 content::RunAllPendingInMessageLoop(); |
| 57 // At the end of each test, we expect no more activity from the prompt. The | 108 // At the end of each test, we expect no more activity from the prompt. The |
| 58 // prompt shouldn't exist anymore either. | 109 // prompt shouldn't exist anymore either. |
| 59 EXPECT_TRUE(did_receive_callback_); | 110 EXPECT_TRUE(did_receive_callback_); |
| 60 EXPECT_FALSE(prompt_); | 111 EXPECT_FALSE(prompt_); |
| 61 testing::Mock::VerifyAndClearExpectations(&download_); | 112 testing::Mock::VerifyAndClearExpectations(&download_); |
| 113 if (report_sent_) { | |
| 114 EXPECT_EQ(expected_serialized_report_, | |
| 115 safe_browsing_factory_->fake_safe_browsing_service() | |
| 116 ->GetDownloadRecoveryReport()); | |
| 117 } | |
| 62 } | 118 } |
| 63 | 119 |
| 64 void SimulatePromptAction(DownloadDangerPrompt::Action action) { | 120 void SimulatePromptAction(DownloadDangerPrompt::Action action) { |
| 65 prompt_->InvokeActionForTesting(action); | 121 prompt_->InvokeActionForTesting(action); |
| 122 report_sent_ = true; | |
| 66 } | 123 } |
| 67 | 124 |
| 68 content::MockDownloadItem& download() { return download_; } | 125 content::MockDownloadItem& download() { return download_; } |
| 69 | 126 |
| 70 DownloadDangerPrompt* prompt() { return prompt_; } | 127 DownloadDangerPrompt* prompt() { return prompt_; } |
| 71 | 128 |
| 72 private: | 129 private: |
| 73 void SetUpDownloadItemExpectations() { | 130 void SetUpDownloadItemExpectations() { |
| 74 EXPECT_CALL(download_, GetFileNameToReportUser()).WillRepeatedly(Return( | 131 EXPECT_CALL(download_, GetFileNameToReportUser()).WillRepeatedly(Return( |
| 75 base::FilePath(FILE_PATH_LITERAL("evil.exe")))); | 132 base::FilePath(FILE_PATH_LITERAL("evil.exe")))); |
| 76 EXPECT_CALL(download_, GetDangerType()) | 133 EXPECT_CALL(download_, GetDangerType()) |
| 77 .WillRepeatedly(Return(content::DOWNLOAD_DANGER_TYPE_DANGEROUS_URL)); | 134 .WillRepeatedly(Return(content::DOWNLOAD_DANGER_TYPE_DANGEROUS_URL)); |
| 78 } | 135 } |
| 79 | 136 |
| 137 void SetUpSafeBrowsingReportExpectations(bool did_proceed) { | |
| 138 ClientSafeBrowsingReportRequest expected_report; | |
| 139 expected_report.set_url(GURL(kTestDownloadUrl).spec()); | |
| 140 expected_report.set_type( | |
| 141 ClientSafeBrowsingReportRequest::MALICIOUS_DOWNLOAD_RECOVERY); | |
| 142 expected_report.set_did_proceed(did_proceed); | |
| 143 expected_report.SerializeToString(&expected_serialized_report_); | |
| 144 } | |
| 145 | |
| 80 void CreatePrompt() { | 146 void CreatePrompt() { |
| 81 prompt_ = DownloadDangerPrompt::Create( | 147 prompt_ = DownloadDangerPrompt::Create( |
| 82 &download_, | 148 &download_, |
| 83 browser()->tab_strip_model()->GetActiveWebContents(), | 149 browser()->tab_strip_model()->GetActiveWebContents(), |
| 84 false, | 150 false, |
| 85 base::Bind(&DownloadDangerPromptTest::PromptCallback, this)); | 151 base::Bind(&DownloadDangerPromptTest::PromptCallback, this)); |
| 86 content::RunAllPendingInMessageLoop(); | 152 content::RunAllPendingInMessageLoop(); |
| 87 } | 153 } |
| 88 | 154 |
| 89 void PromptCallback(DownloadDangerPrompt::Action action) { | 155 void PromptCallback(DownloadDangerPrompt::Action action) { |
| 90 EXPECT_FALSE(did_receive_callback_); | 156 EXPECT_FALSE(did_receive_callback_); |
| 91 EXPECT_EQ(expected_action_, action); | 157 EXPECT_EQ(expected_action_, action); |
| 92 did_receive_callback_ = true; | 158 did_receive_callback_ = true; |
| 93 prompt_ = NULL; | 159 prompt_ = NULL; |
| 94 } | 160 } |
| 95 | 161 |
| 96 content::MockDownloadItem download_; | 162 content::MockDownloadItem download_; |
| 97 DownloadDangerPrompt* prompt_; | 163 DownloadDangerPrompt* prompt_; |
| 98 DownloadDangerPrompt::Action expected_action_; | 164 DownloadDangerPrompt::Action expected_action_; |
| 99 bool did_receive_callback_; | 165 bool did_receive_callback_; |
| 166 scoped_ptr<TestSafeBrowsingServiceFactory> safe_browsing_factory_; | |
| 167 std::string expected_serialized_report_; | |
| 168 bool report_sent_; | |
| 100 | 169 |
| 101 DISALLOW_COPY_AND_ASSIGN(DownloadDangerPromptTest); | 170 DISALLOW_COPY_AND_ASSIGN(DownloadDangerPromptTest); |
| 102 }; | 171 }; |
| 103 | 172 |
| 104 // Disabled for flaky timeouts on Windows. crbug.com/446696 | 173 // Disabled for flaky timeouts on Windows. crbug.com/446696 |
| 105 #if defined(OS_WIN) | 174 #if defined(OS_WIN) |
| 106 #define MAYBE_TestAll DISABLED_TestAll | 175 #define MAYBE_TestAll DISABLED_TestAll |
| 107 #else | 176 #else |
| 108 #define MAYBE_TestAll TestAll | 177 #define MAYBE_TestAll TestAll |
| 109 #endif | 178 #endif |
| 110 IN_PROC_BROWSER_TEST_F(DownloadDangerPromptTest, MAYBE_TestAll) { | 179 IN_PROC_BROWSER_TEST_F(DownloadDangerPromptTest, MAYBE_TestAll) { |
| 111 // ExperienceSampling: Set default actions for DownloadItem methods we need. | 180 // ExperienceSampling: Set default actions for DownloadItem methods we need. |
| 112 ON_CALL(download(), GetURL()).WillByDefault(ReturnRef(GURL::EmptyGURL())); | 181 GURL download_url(kTestDownloadUrl); |
| 182 ON_CALL(download(), GetURL()).WillByDefault(ReturnRef(download_url)); | |
| 113 ON_CALL(download(), GetReferrerUrl()) | 183 ON_CALL(download(), GetReferrerUrl()) |
| 114 .WillByDefault(ReturnRef(GURL::EmptyGURL())); | 184 .WillByDefault(ReturnRef(GURL::EmptyGURL())); |
| 115 ON_CALL(download(), GetBrowserContext()) | 185 ON_CALL(download(), GetBrowserContext()) |
| 116 .WillByDefault(Return(browser()->profile())); | 186 .WillByDefault(Return(browser()->profile())); |
| 117 | 187 |
| 118 OpenNewTab(); | 188 OpenNewTab(); |
| 119 | 189 |
| 120 // Clicking the Accept button should invoke the ACCEPT action. | 190 // Clicking the Accept button should invoke the ACCEPT action. |
| 121 SetUpExpectations(DownloadDangerPrompt::ACCEPT); | 191 SetUpExpectations(DownloadDangerPrompt::ACCEPT); |
| 122 SimulatePromptAction(DownloadDangerPrompt::ACCEPT); | 192 SimulatePromptAction(DownloadDangerPrompt::ACCEPT); |
| (...skipping 27 matching lines...) Expand all Loading... | |
| 150 download().NotifyObserversDownloadUpdated(); | 220 download().NotifyObserversDownloadUpdated(); |
| 151 SimulatePromptAction(DownloadDangerPrompt::ACCEPT); | 221 SimulatePromptAction(DownloadDangerPrompt::ACCEPT); |
| 152 VerifyExpectations(); | 222 VerifyExpectations(); |
| 153 | 223 |
| 154 // If the containing tab is closed, the dialog should DISMISS itself. | 224 // If the containing tab is closed, the dialog should DISMISS itself. |
| 155 OpenNewTab(); | 225 OpenNewTab(); |
| 156 SetUpExpectations(DownloadDangerPrompt::DISMISS); | 226 SetUpExpectations(DownloadDangerPrompt::DISMISS); |
| 157 chrome::CloseTab(browser()); | 227 chrome::CloseTab(browser()); |
| 158 VerifyExpectations(); | 228 VerifyExpectations(); |
| 159 } | 229 } |
| OLD | NEW |