Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(313)

Side by Side Diff: chrome/browser/download/download_danger_prompt_browsertest.cc

Issue 1436273002: Send safe browsing ThreatDetails to track download CTR when user tries to recover blocked downloads (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: addressing asanka and Lei's comments Created 5 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 "chrome/browser/download/download_danger_prompt.h" 7 #include "chrome/browser/download/download_danger_prompt.h"
8 #include "chrome/browser/profiles/profile.h" 8 #include "chrome/browser/profiles/profile.h"
9 #include "chrome/browser/safe_browsing/database_manager.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* const kTestDownloadUrl = "http://evildownload.com";
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() const { return report_; }
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_(nullptr) {}
56 ~TestSafeBrowsingServiceFactory() override {}
57
58 SafeBrowsingService* CreateSafeBrowsingService() override {
59 if (fake_safe_browsing_service_ == nullptr) {
Lei Zhang 2015/11/25 23:48:15 Just: if (!fake_safe_browsing_service_) ?
Jialiu Lin 2015/11/26 00:59:00 Done.
60 fake_safe_browsing_service_ = new FakeSafeBrowsingService();
61 }
62 return fake_safe_browsing_service_.get();
63 }
64
65 scoped_refptr<FakeSafeBrowsingService> fake_safe_browsing_service() {
66 return fake_safe_browsing_service_;
67 }
68
69 private:
70 scoped_refptr<FakeSafeBrowsingService> fake_safe_browsing_service_;
71 };
26 72
27 class DownloadDangerPromptTest : public InProcessBrowserTest { 73 class DownloadDangerPromptTest : public InProcessBrowserTest {
28 public: 74 public:
29 DownloadDangerPromptTest() 75 DownloadDangerPromptTest()
30 : prompt_(NULL), 76 : prompt_(nullptr),
31 expected_action_(DownloadDangerPrompt::CANCEL), 77 expected_action_(DownloadDangerPrompt::CANCEL),
32 did_receive_callback_(false) { 78 did_receive_callback_(false),
79 test_safe_browsing_factory_(new TestSafeBrowsingServiceFactory()),
80 report_sent_(false) {}
81
82 ~DownloadDangerPromptTest() override {}
83
84 void SetUp() override {
85 SafeBrowsingService::RegisterFactory(test_safe_browsing_factory_.get());
86 InProcessBrowserTest::SetUp();
33 } 87 }
34 88
35 ~DownloadDangerPromptTest() override {} 89 void TearDown() override {
90 SafeBrowsingService::RegisterFactory(nullptr);
Lei Zhang 2015/11/25 23:48:15 Also call InProcessBrowserTest::TearDown() ?
Jialiu Lin 2015/11/26 00:59:00 Done.
91 }
36 92
37 // Opens a new tab and waits for navigations to finish. If there are pending 93 // 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 94 // navigations, the constrained prompt might be dismissed when the navigation
39 // completes. 95 // completes.
40 void OpenNewTab() { 96 void OpenNewTab() {
41 ui_test_utils::NavigateToURLWithDisposition( 97 ui_test_utils::NavigateToURLWithDisposition(
42 browser(), GURL("about:blank"), 98 browser(), GURL("about:blank"),
43 NEW_FOREGROUND_TAB, 99 NEW_FOREGROUND_TAB,
44 ui_test_utils::BROWSER_TEST_WAIT_FOR_TAB | 100 ui_test_utils::BROWSER_TEST_WAIT_FOR_TAB |
45 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION); 101 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION);
46 } 102 }
47 103
48 void SetUpExpectations(DownloadDangerPrompt::Action expected_action) { 104 void SetUpExpectations(DownloadDangerPrompt::Action expected_action) {
49 did_receive_callback_ = false; 105 did_receive_callback_ = false;
50 expected_action_ = expected_action; 106 expected_action_ = expected_action;
51 SetUpDownloadItemExpectations(); 107 SetUpDownloadItemExpectations();
108 SetUpSafeBrowsingReportExpectations(expected_action ==
109 DownloadDangerPrompt::ACCEPT);
52 CreatePrompt(); 110 CreatePrompt();
111 report_sent_ = false;
53 } 112 }
54 113
55 void VerifyExpectations() { 114 void VerifyExpectations() {
56 content::RunAllPendingInMessageLoop(); 115 content::RunAllPendingInMessageLoop();
57 // At the end of each test, we expect no more activity from the prompt. The 116 // At the end of each test, we expect no more activity from the prompt. The
58 // prompt shouldn't exist anymore either. 117 // prompt shouldn't exist anymore either.
59 EXPECT_TRUE(did_receive_callback_); 118 EXPECT_TRUE(did_receive_callback_);
60 EXPECT_FALSE(prompt_); 119 EXPECT_FALSE(prompt_);
61 testing::Mock::VerifyAndClearExpectations(&download_); 120 testing::Mock::VerifyAndClearExpectations(&download_);
121 if (report_sent_) {
122 EXPECT_EQ(expected_serialized_report_,
123 test_safe_browsing_factory_->fake_safe_browsing_service()
124 ->GetDownloadRecoveryReport());
125 }
62 } 126 }
63 127
64 void SimulatePromptAction(DownloadDangerPrompt::Action action) { 128 void SimulatePromptAction(DownloadDangerPrompt::Action action) {
65 prompt_->InvokeActionForTesting(action); 129 prompt_->InvokeActionForTesting(action);
130 report_sent_ = true;
66 } 131 }
67 132
68 content::MockDownloadItem& download() { return download_; } 133 content::MockDownloadItem& download() { return download_; }
69 134
70 DownloadDangerPrompt* prompt() { return prompt_; } 135 DownloadDangerPrompt* prompt() { return prompt_; }
71 136
72 private: 137 private:
73 void SetUpDownloadItemExpectations() { 138 void SetUpDownloadItemExpectations() {
74 EXPECT_CALL(download_, GetFileNameToReportUser()).WillRepeatedly(Return( 139 EXPECT_CALL(download_, GetFileNameToReportUser()).WillRepeatedly(Return(
75 base::FilePath(FILE_PATH_LITERAL("evil.exe")))); 140 base::FilePath(FILE_PATH_LITERAL("evil.exe"))));
76 EXPECT_CALL(download_, GetDangerType()) 141 EXPECT_CALL(download_, GetDangerType())
77 .WillRepeatedly(Return(content::DOWNLOAD_DANGER_TYPE_DANGEROUS_URL)); 142 .WillRepeatedly(Return(content::DOWNLOAD_DANGER_TYPE_DANGEROUS_URL));
78 } 143 }
79 144
145 void SetUpSafeBrowsingReportExpectations(bool did_proceed) {
146 ClientSafeBrowsingReportRequest expected_report;
147 expected_report.set_url(GURL(kTestDownloadUrl).spec());
148 expected_report.set_type(
149 ClientSafeBrowsingReportRequest::MALICIOUS_DOWNLOAD_RECOVERY);
150 expected_report.set_did_proceed(did_proceed);
151 expected_report.SerializeToString(&expected_serialized_report_);
152 }
153
80 void CreatePrompt() { 154 void CreatePrompt() {
81 prompt_ = DownloadDangerPrompt::Create( 155 prompt_ = DownloadDangerPrompt::Create(
82 &download_, 156 &download_,
83 browser()->tab_strip_model()->GetActiveWebContents(), 157 browser()->tab_strip_model()->GetActiveWebContents(),
84 false, 158 false,
85 base::Bind(&DownloadDangerPromptTest::PromptCallback, this)); 159 base::Bind(&DownloadDangerPromptTest::PromptCallback, this));
86 content::RunAllPendingInMessageLoop(); 160 content::RunAllPendingInMessageLoop();
87 } 161 }
88 162
89 void PromptCallback(DownloadDangerPrompt::Action action) { 163 void PromptCallback(DownloadDangerPrompt::Action action) {
90 EXPECT_FALSE(did_receive_callback_); 164 EXPECT_FALSE(did_receive_callback_);
91 EXPECT_EQ(expected_action_, action); 165 EXPECT_EQ(expected_action_, action);
92 did_receive_callback_ = true; 166 did_receive_callback_ = true;
93 prompt_ = NULL; 167 prompt_ = nullptr;
94 } 168 }
95 169
96 content::MockDownloadItem download_; 170 content::MockDownloadItem download_;
97 DownloadDangerPrompt* prompt_; 171 DownloadDangerPrompt* prompt_;
98 DownloadDangerPrompt::Action expected_action_; 172 DownloadDangerPrompt::Action expected_action_;
99 bool did_receive_callback_; 173 bool did_receive_callback_;
174 scoped_ptr<TestSafeBrowsingServiceFactory> test_safe_browsing_factory_;
175 std::string expected_serialized_report_;
176 bool report_sent_;
100 177
101 DISALLOW_COPY_AND_ASSIGN(DownloadDangerPromptTest); 178 DISALLOW_COPY_AND_ASSIGN(DownloadDangerPromptTest);
102 }; 179 };
103 180
104 // Disabled for flaky timeouts on Windows. crbug.com/446696 181 // Disabled for flaky timeouts on Windows. crbug.com/446696
105 #if defined(OS_WIN) 182 #if defined(OS_WIN)
106 #define MAYBE_TestAll DISABLED_TestAll 183 #define MAYBE_TestAll DISABLED_TestAll
107 #else 184 #else
108 #define MAYBE_TestAll TestAll 185 #define MAYBE_TestAll TestAll
109 #endif 186 #endif
110 IN_PROC_BROWSER_TEST_F(DownloadDangerPromptTest, MAYBE_TestAll) { 187 IN_PROC_BROWSER_TEST_F(DownloadDangerPromptTest, MAYBE_TestAll) {
111 // ExperienceSampling: Set default actions for DownloadItem methods we need. 188 // ExperienceSampling: Set default actions for DownloadItem methods we need.
112 ON_CALL(download(), GetURL()).WillByDefault(ReturnRef(GURL::EmptyGURL())); 189 GURL download_url(kTestDownloadUrl);
190 ON_CALL(download(), GetURL()).WillByDefault(ReturnRef(download_url));
113 ON_CALL(download(), GetReferrerUrl()) 191 ON_CALL(download(), GetReferrerUrl())
114 .WillByDefault(ReturnRef(GURL::EmptyGURL())); 192 .WillByDefault(ReturnRef(GURL::EmptyGURL()));
115 ON_CALL(download(), GetBrowserContext()) 193 ON_CALL(download(), GetBrowserContext())
116 .WillByDefault(Return(browser()->profile())); 194 .WillByDefault(Return(browser()->profile()));
117 195
118 OpenNewTab(); 196 OpenNewTab();
119 197
120 // Clicking the Accept button should invoke the ACCEPT action. 198 // Clicking the Accept button should invoke the ACCEPT action.
121 SetUpExpectations(DownloadDangerPrompt::ACCEPT); 199 SetUpExpectations(DownloadDangerPrompt::ACCEPT);
122 SimulatePromptAction(DownloadDangerPrompt::ACCEPT); 200 SimulatePromptAction(DownloadDangerPrompt::ACCEPT);
(...skipping 27 matching lines...) Expand all
150 download().NotifyObserversDownloadUpdated(); 228 download().NotifyObserversDownloadUpdated();
151 SimulatePromptAction(DownloadDangerPrompt::ACCEPT); 229 SimulatePromptAction(DownloadDangerPrompt::ACCEPT);
152 VerifyExpectations(); 230 VerifyExpectations();
153 231
154 // If the containing tab is closed, the dialog should DISMISS itself. 232 // If the containing tab is closed, the dialog should DISMISS itself.
155 OpenNewTab(); 233 OpenNewTab();
156 SetUpExpectations(DownloadDangerPrompt::DISMISS); 234 SetUpExpectations(DownloadDangerPrompt::DISMISS);
157 chrome::CloseTab(browser()); 235 chrome::CloseTab(browser());
158 VerifyExpectations(); 236 VerifyExpectations();
159 } 237 }
OLDNEW
« no previous file with comments | « chrome/browser/download/download_danger_prompt.cc ('k') | chrome/browser/safe_browsing/safe_browsing_service.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698