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

Side by Side Diff: chrome/renderer/safe_browsing/phishing_classifier_delegate_browsertest.cc

Issue 1585003002: Rewrite PhishingClassifierDelegateTest using ChromeRenderViewTest to run with multi-process (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: fix compilation problem on win Created 4 years, 11 months 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
« no previous file with comments | « no previous file | chrome/renderer/safe_browsing/threat_dom_details.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 "chrome/renderer/safe_browsing/phishing_classifier_delegate.h" 5 #include "chrome/renderer/safe_browsing/phishing_classifier_delegate.h"
6 6
7 #include <stdint.h>
8 #include <utility>
9
10 #include "base/command_line.h"
11 #include "base/location.h"
12 #include "base/macros.h"
13 #include "base/memory/scoped_ptr.h" 7 #include "base/memory/scoped_ptr.h"
14 #include "base/run_loop.h"
15 #include "base/single_thread_task_runner.h"
16 #include "base/strings/string_number_conversions.h"
17 #include "base/strings/utf_string_conversions.h" 8 #include "base/strings/utf_string_conversions.h"
18 #include "build/build_config.h"
19 #include "chrome/browser/ui/browser.h"
20 #include "chrome/browser/ui/browser_commands.h"
21 #include "chrome/browser/ui/tabs/tab_strip_model.h"
22 #include "chrome/common/chrome_switches.h"
23 #include "chrome/common/safe_browsing/csd.pb.h" 9 #include "chrome/common/safe_browsing/csd.pb.h"
24 #include "chrome/common/safe_browsing/safebrowsing_messages.h" 10 #include "chrome/common/safe_browsing/safebrowsing_messages.h"
25 #include "chrome/renderer/safe_browsing/features.h" 11 #include "chrome/renderer/safe_browsing/features.h"
26 #include "chrome/renderer/safe_browsing/phishing_classifier.h" 12 #include "chrome/renderer/safe_browsing/phishing_classifier.h"
27 #include "chrome/renderer/safe_browsing/scorer.h" 13 #include "chrome/renderer/safe_browsing/scorer.h"
28 #include "chrome/test/base/in_process_browser_test.h" 14 #include "chrome/test/base/chrome_render_view_test.h"
29 #include "chrome/test/base/ui_test_utils.h" 15 #include "chrome/test/base/chrome_unit_test_suite.h"
30 #include "content/public/browser/browser_message_filter.h"
31 #include "content/public/browser/browser_thread.h"
32 #include "content/public/browser/render_frame_host.h"
33 #include "content/public/browser/render_process_host.h"
34 #include "content/public/browser/web_contents.h"
35 #include "content/public/common/content_switches.h"
36 #include "content/public/renderer/render_frame.h" 16 #include "content/public/renderer/render_frame.h"
37 #include "content/public/test/browser_test_utils.h" 17 #include "content/public/renderer/render_view.h"
38 #include "content/public/test/test_navigation_observer.h"
39 #include "content/public/test/test_utils.h"
40 #include "net/dns/mock_host_resolver.h"
41 #include "net/test/embedded_test_server/embedded_test_server.h"
42 #include "net/test/embedded_test_server/http_request.h"
43 #include "net/test/embedded_test_server/http_response.h"
44 #include "testing/gmock/include/gmock/gmock.h" 18 #include "testing/gmock/include/gmock/gmock.h"
45 #include "third_party/WebKit/public/platform/WebURL.h" 19 #include "third_party/WebKit/public/platform/WebURL.h"
46 #include "third_party/WebKit/public/platform/WebURLRequest.h" 20 #include "third_party/WebKit/public/platform/WebURLRequest.h"
47 #include "url/gurl.h" 21 #include "url/gurl.h"
48 22
49 using base::ASCIIToUTF16; 23 using base::ASCIIToUTF16;
50 using ::testing::_; 24 using ::testing::_;
51 using ::testing::InSequence; 25 using ::testing::InSequence;
52 using ::testing::Mock; 26 using ::testing::Mock;
53 using ::testing::Pointee; 27 using ::testing::Pointee;
(...skipping 22 matching lines...) Expand all
76 public: 50 public:
77 MockScorer() : Scorer() {} 51 MockScorer() : Scorer() {}
78 virtual ~MockScorer() {} 52 virtual ~MockScorer() {}
79 53
80 MOCK_CONST_METHOD1(ComputeScore, double(const FeatureMap&)); 54 MOCK_CONST_METHOD1(ComputeScore, double(const FeatureMap&));
81 55
82 private: 56 private:
83 DISALLOW_COPY_AND_ASSIGN(MockScorer); 57 DISALLOW_COPY_AND_ASSIGN(MockScorer);
84 }; 58 };
85 59
86 class InterceptingMessageFilter : public content::BrowserMessageFilter { 60 } // namespace
61
62 class FakeRenderThread : public ChromeMockRenderThread {
87 public: 63 public:
88 InterceptingMessageFilter() 64 // Instead of sending this message, we verify its content here.
89 : BrowserMessageFilter(SafeBrowsingMsgStart), 65 bool Send(IPC::Message* msg) override {
90 waiting_message_loop_(NULL) { 66 // handle and verify message here.
67 IPC_BEGIN_MESSAGE_MAP(FakeRenderThread, *msg)
68 IPC_MESSAGE_HANDLER(SafeBrowsingHostMsg_PhishingDetectionDone,
69 VerifyMessageContent)
70 IPC_END_MESSAGE_MAP()
71 if (msg) { // Prevent memory leak.
72 delete msg;
73 msg = nullptr;
74 }
75 // Return ture anyway, since we don't want to block other IPC.
Nathan Parker 2016/01/26 16:49:19 s/ture/true
Jialiu Lin 2016/01/28 19:55:23 Done.
76 return true;
91 } 77 }
92 78
93 const ClientPhishingRequest* verdict() const { return verdict_.get(); } 79 void VerifyMessageContent(const std::string& verdict_str) {
94 bool OnMessageReceived(const IPC::Message& message) override { 80 scoped_ptr<ClientPhishingRequest> verdict(new ClientPhishingRequest);
Nathan Parker 2016/01/26 16:49:19 nit: Does this need to be a ptr? Could be just an
Jialiu Lin 2016/01/28 19:55:23 regular point will do too.
95 bool handled = true; 81 if (verdict->ParseFromString(verdict_str) && verdict->IsInitialized()) {
Nathan Parker 2016/01/26 16:49:19 Do you want to catch if the msg is not parsable?
Jialiu Lin 2016/01/28 19:55:23 yes, it makes more sense. Done
96 IPC_BEGIN_MESSAGE_MAP(InterceptingMessageFilter, message) 82 EXPECT_EQ("http://host.com/", verdict->url());
97 IPC_MESSAGE_HANDLER(SafeBrowsingHostMsg_PhishingDetectionDone, 83 EXPECT_EQ(0.8f, verdict->client_score());
98 OnPhishingDetectionDone) 84 EXPECT_EQ(false, verdict->is_phishing());
99 IPC_MESSAGE_UNHANDLED(handled = false); 85 }
100 IPC_END_MESSAGE_MAP() 86 }
101 return handled; 87 };
88
89 class PhishingClassifierDelegateTest : public ChromeRenderViewTest {
Nathan Parker 2016/01/26 16:49:19 Q: How does this fit with rv->rf refactoring?
Jialiu Lin 2016/01/28 19:55:23 ah, this class name (chromeRenderViewTest) is inde
90 protected:
91 void SetUp() override {
92 ChromeUnitTestSuite::InitializeProviders();
93 ChromeUnitTestSuite::InitializeResourceBundle();
94
95 // Plug-in FakeRenderThread.
96 chrome_render_thread_ = new FakeRenderThread();
97 render_thread_.reset(chrome_render_thread_);
98
99 content::RenderViewTest::SetUp();
100
101 content::RenderFrame* render_frame = view_->GetMainRenderFrame();
102 classifier_ = new StrictMock<MockPhishingClassifier>(render_frame);
103 delegate_ = PhishingClassifierDelegate::Create(render_frame, classifier_);
102 } 104 }
103 105
104 void Reset() { 106 // Runs the ClassificationDone callback, then verify if message sent
105 run_loop_.reset(new base::RunLoop()); 107 // by FakeRenderThread is correct.
106 waiting_message_loop_ = base::MessageLoop::current(); 108 void RunAndVerifyClassificationDone(const ClientPhishingRequest& verdict) {
107 quit_closure_ = run_loop_->QuitClosure(); 109 delegate_->ClassificationDone(verdict);
108 }
109
110 void RunUntilVerdictReceived() {
111 content::RunThisRunLoop(run_loop_.get());
112
113 // Clear out the synchronization state just in case.
114 waiting_message_loop_ = NULL;
115 quit_closure_.Reset();
116 run_loop_.reset();
117 }
118
119 void OnPhishingDetectionDone(const std::string& verdict_str) {
120 scoped_ptr<ClientPhishingRequest> verdict(new ClientPhishingRequest);
121 if (verdict->ParseFromString(verdict_str) &&
122 verdict->IsInitialized()) {
123 verdict_.swap(verdict);
124 }
125 waiting_message_loop_->task_runner()->PostTask(FROM_HERE, quit_closure_);
126 }
127
128 private:
129 ~InterceptingMessageFilter() override {}
130
131 scoped_ptr<ClientPhishingRequest> verdict_;
132 base::MessageLoop* waiting_message_loop_;
133 base::Closure quit_closure_;
134 scoped_ptr<base::RunLoop> run_loop_;
135 };
136 } // namespace
137
138 class PhishingClassifierDelegateTest : public InProcessBrowserTest {
139 public:
140 void CancelCalled() {
141 if (runner_.get()) {
142 content::BrowserThread::PostTask(
143 content::BrowserThread::UI, FROM_HERE, runner_->QuitClosure());
144 }
145 }
146
147 protected:
148 void SetUpCommandLine(base::CommandLine* command_line) override {
149 command_line->AppendSwitch(switches::kSingleProcess);
150 #if defined(OS_WIN)
151 // Don't want to try to create a GPU process.
152 command_line->AppendSwitch(switches::kDisableGpu);
153 #endif
154 }
155
156 void SetUpOnMainThread() override {
157 intercepting_filter_ = new InterceptingMessageFilter();
158
159 GetWebContents()->GetRenderProcessHost()->AddFilter(
160 intercepting_filter_.get());
161 content::RenderFrame* render_frame = GetRenderFrame();
162 classifier_ = new StrictMock<MockPhishingClassifier>(render_frame);
163 PostTaskToInProcessRendererAndWait(
164 base::Bind(&PhishingClassifierDelegateTest::SetUpOnRendererThread,
165 base::Unretained(this)));
166
167 embedded_test_server()->RegisterRequestHandler(
168 base::Bind(&PhishingClassifierDelegateTest::HandleRequest,
169 base::Unretained(this)));
170 ASSERT_TRUE(embedded_test_server()->Start());
171 host_resolver()->AddRule("*", "127.0.0.1");
172 }
173
174 // Runs the ClassificationDone callback, then waits for the
175 // PhishingDetectionDone IPC to arrive.
176 void RunClassificationDone(const ClientPhishingRequest& verdict) {
177 // Clear out any previous state.
178 intercepting_filter_->Reset();
179 PostTaskToInProcessRendererAndWait(
180 base::Bind(&PhishingClassifierDelegate::ClassificationDone,
181 base::Unretained(delegate_),
182 verdict));
183 intercepting_filter_->RunUntilVerdictReceived();
184 } 110 }
185 111
186 void OnStartPhishingDetection(const GURL& url) { 112 void OnStartPhishingDetection(const GURL& url) {
187 PostTaskToInProcessRendererAndWait( 113 delegate_->OnStartPhishingDetection(url);
188 base::Bind(&PhishingClassifierDelegate::OnStartPhishingDetection,
189 base::Unretained(delegate_), url));
190 } 114 }
191 115
192 void PageCaptured(base::string16* page_text, bool preliminary_capture) { 116 void PageCaptured(base::string16* page_text,
193 PostTaskToInProcessRendererAndWait( 117 bool preliminary_capture,
194 base::Bind(&PhishingClassifierDelegate::PageCaptured, 118 const GURL& page_url) {
195 base::Unretained(delegate_), page_text, 119 if (preliminary_capture)
196 preliminary_capture)); 120 return;
121
122 delegate_->CancelPendingClassification(
123 PhishingClassifierDelegate::PAGE_RECAPTURED);
124 delegate_->last_finished_load_url_ = page_url;
125 delegate_->classifier_page_text_.swap(*page_text);
126 delegate_->have_page_text_ = true;
127 delegate_->MaybeStartClassification();
197 } 128 }
198 129
199 scoped_ptr<net::test_server::HttpResponse> HandleRequest( 130 void SimulateRedirection(const GURL& redir_url) {
200 const net::test_server::HttpRequest& request) { 131 delegate_->last_url_received_from_browser_ = redir_url;
201 auto host_it = request.headers.find("Host");
202 if (host_it == request.headers.end())
203 return scoped_ptr<net::test_server::HttpResponse>();
204
205 std::string url =
206 std::string("http://") + host_it->second + request.relative_url;
207 if (response_url_.spec() != url)
208 return scoped_ptr<net::test_server::HttpResponse>();
209
210 scoped_ptr<net::test_server::BasicHttpResponse> http_response(
211 new net::test_server::BasicHttpResponse());
212 http_response->set_code(net::HTTP_OK);
213 http_response->set_content_type("text/html");
214 http_response->set_content(response_content_);
215 return std::move(http_response);
216 } 132 }
217 133
218 content::WebContents* GetWebContents() { 134 void SimulatePageTrantitionForwardOrBack(const char* html, const char* url) {
219 return browser()->tab_strip_model()->GetActiveWebContents(); 135 LoadHTMLWithUrlOverride(html, url);
136 delegate_->last_main_frame_transition_ = ui::PAGE_TRANSITION_FORWARD_BACK;
220 } 137 }
221 138
222 content::RenderFrame* GetRenderFrame() {
223 int render_frame_routing_id =
224 GetWebContents()->GetMainFrame()->GetRoutingID();
225 content::RenderFrame* render_frame =
226 content::RenderFrame::FromRoutingID(render_frame_routing_id);
227 return render_frame;
228 }
229
230 // Returns the URL that was loaded.
231 GURL LoadHtml(const std::string& host, const std::string& content) {
232 GURL::Replacements replace_host;
233 replace_host.SetHostStr(host);
234 response_content_ = content;
235 response_url_ =
236 embedded_test_server()->base_url().ReplaceComponents(replace_host);
237 ui_test_utils::NavigateToURL(browser(), response_url_);
238 return response_url_;
239 }
240
241 void GoBack() {
242 GetWebContents()->GetController().GoBack();
243 content::WaitForLoadStop(GetWebContents());
244 }
245
246 void GoForward() {
247 GetWebContents()->GetController().GoForward();
248 content::WaitForLoadStop(GetWebContents());
249 }
250
251 scoped_refptr<InterceptingMessageFilter> intercepting_filter_;
252 GURL response_url_;
253 std::string response_content_;
254 scoped_ptr<ClientPhishingRequest> verdict_;
255 StrictMock<MockPhishingClassifier>* classifier_; // Owned by |delegate_|. 139 StrictMock<MockPhishingClassifier>* classifier_; // Owned by |delegate_|.
256 PhishingClassifierDelegate* delegate_; // Owned by the RenderView. 140 PhishingClassifierDelegate* delegate_; // Owned by the RenderFrame.
257 scoped_refptr<content::MessageLoopRunner> runner_;
258
259 private:
260 void SetUpOnRendererThread() {
261 content::RenderFrame* render_frame = GetRenderFrame();
262 // PhishingClassifierDelegate is a RenderFrameObserver and therefore has to
263 // be created on the renderer thread, which is not the main thread in an
264 // in-process-browser-test.
265 delegate_ = PhishingClassifierDelegate::Create(render_frame, classifier_);
266 }
267
268 }; 141 };
269 142
270 IN_PROC_BROWSER_TEST_F(PhishingClassifierDelegateTest, Navigation) { 143 TEST_F(PhishingClassifierDelegateTest, Navigation) {
271 MockScorer scorer; 144 MockScorer scorer;
272 delegate_->SetPhishingScorer(&scorer); 145 delegate_->SetPhishingScorer(&scorer);
273 ASSERT_TRUE(classifier_->is_ready()); 146 ASSERT_TRUE(classifier_->is_ready());
274 147
275 // Test an initial load. We expect classification to happen normally. 148 // Test an initial load. We expect classification to happen normally.
276 EXPECT_CALL(*classifier_, CancelPendingClassification()); 149 EXPECT_CALL(*classifier_, CancelPendingClassification());
277 GURL iframe_url = embedded_test_server()->GetURL("/"); 150 std::string html = "<html><body>dummy</body></html>";
278 GURL::Replacements replace_host; 151 GURL url("http://host.com/index.html");
279 replace_host.SetHostStr("sub1.com"); 152 LoadHTMLWithUrlOverride(html.c_str(), url.spec().c_str());
280 std::string html = "<html><body><iframe src=\"";
281 html += iframe_url.ReplaceComponents(replace_host).spec();
282 html += "\"></iframe></body></html>";
283 GURL url = LoadHtml("host.com", html);
284 Mock::VerifyAndClearExpectations(classifier_); 153 Mock::VerifyAndClearExpectations(classifier_);
Nathan Parker 2016/01/26 16:49:19 nit: newline before VerifyAndClearExpectations (or
Jialiu Lin 2016/01/28 19:55:23 Done.
285 OnStartPhishingDetection(url); 154 OnStartPhishingDetection(url);
286 base::string16 page_text = ASCIIToUTF16("dummy"); 155 base::string16 page_text = ASCIIToUTF16("dummy");
287 { 156 {
288 InSequence s; 157 InSequence s;
289 EXPECT_CALL(*classifier_, CancelPendingClassification()); 158 EXPECT_CALL(*classifier_, CancelPendingClassification());
290 EXPECT_CALL(*classifier_, BeginClassification(Pointee(page_text), _)); 159 EXPECT_CALL(*classifier_, BeginClassification(Pointee(page_text), _));
291 PageCaptured(&page_text, false); 160 PageCaptured(&page_text, false, url);
292 Mock::VerifyAndClearExpectations(classifier_); 161 Mock::VerifyAndClearExpectations(classifier_);
293 } 162 }
294 163
295 // Reloading the same page should not trigger a reclassification. 164 // Reloading the same page should not trigger a reclassification.
296 // However, it will cancel any pending classification since the 165 // However, it will cancel any pending classification since the
297 // content is being replaced. 166 // content is being replaced.
298 EXPECT_CALL(*classifier_, CancelPendingClassification()); 167 EXPECT_CALL(*classifier_, CancelPendingClassification());
299 168 LoadHTMLWithUrlOverride(html.c_str(), url.spec().c_str());
300 content::TestNavigationObserver observer(GetWebContents()); 169 content::PageState back_state = GetCurrentPageState();
Nathan Parker 2016/01/26 16:49:19 What does this do?
Jialiu Lin 2016/01/28 19:55:23 oops, forgot to delete this line...
301 chrome::Reload(browser(), CURRENT_TAB);
302 observer.Wait();
303 170
304 Mock::VerifyAndClearExpectations(classifier_); 171 Mock::VerifyAndClearExpectations(classifier_);
305 OnStartPhishingDetection(url); 172 OnStartPhishingDetection(url);
306 page_text = ASCIIToUTF16("dummy"); 173 page_text = ASCIIToUTF16("dummy");
307 EXPECT_CALL(*classifier_, CancelPendingClassification()); 174 EXPECT_CALL(*classifier_, CancelPendingClassification());
308 PageCaptured(&page_text, false); 175 PageCaptured(&page_text, false, url);
309 Mock::VerifyAndClearExpectations(classifier_); 176 Mock::VerifyAndClearExpectations(classifier_);
310 177
311 OnStartPhishingDetection(url); 178 OnStartPhishingDetection(url);
312 page_text = ASCIIToUTF16("dummy"); 179 page_text = ASCIIToUTF16("dummy");
313 EXPECT_CALL(*classifier_, CancelPendingClassification()); 180 EXPECT_CALL(*classifier_, CancelPendingClassification());
314 PageCaptured(&page_text, false); 181 PageCaptured(&page_text, false, url);
315 Mock::VerifyAndClearExpectations(classifier_); 182 Mock::VerifyAndClearExpectations(classifier_);
316 183
317 // Scrolling to an anchor works similarly to a subframe navigation, but 184 // Navigating within page works similarly to a subframe navigation, but
318 // see the TODO in PhishingClassifierDelegate::DidCommitProvisionalLoad. 185 // see the TODO in PhishingClassifierDelegate::DidCommitProvisionalLoad.
319 EXPECT_CALL(*classifier_, CancelPendingClassification()); 186 EXPECT_CALL(*classifier_, CancelPendingClassification());
320 GURL foo_url = GURL(url.spec() + "#foo"); 187 DidNavigateWithinPage(GetMainFrame(), true);
321 ui_test_utils::NavigateToURL(browser(), foo_url);
322 Mock::VerifyAndClearExpectations(classifier_); 188 Mock::VerifyAndClearExpectations(classifier_);
323 OnStartPhishingDetection(url); 189 OnStartPhishingDetection(url);
324 page_text = ASCIIToUTF16("dummy"); 190 page_text = ASCIIToUTF16("dummy");
325 EXPECT_CALL(*classifier_, CancelPendingClassification()); 191 EXPECT_CALL(*classifier_, CancelPendingClassification());
326 PageCaptured(&page_text, false); 192 PageCaptured(&page_text, false, url);
327 Mock::VerifyAndClearExpectations(classifier_); 193 Mock::VerifyAndClearExpectations(classifier_);
328 194
329 // Now load a new toplevel page, which should trigger another classification. 195 // Now load a new toplevel page, which should trigger another classification.
330 EXPECT_CALL(*classifier_, CancelPendingClassification()) 196 EXPECT_CALL(*classifier_, CancelPendingClassification());
331 .WillOnce(Invoke(this, &PhishingClassifierDelegateTest::CancelCalled)); 197 GURL new_url("http://host2.com");
332 198 LoadHTMLWithUrlOverride("dummy2", new_url.spec().c_str());
333 runner_ = new content::MessageLoopRunner;
334 url = LoadHtml("host2.com", "dummy2");
335 runner_->Run();
336 runner_ = NULL;
337
338 Mock::VerifyAndClearExpectations(classifier_); 199 Mock::VerifyAndClearExpectations(classifier_);
339 page_text = ASCIIToUTF16("dummy2"); 200 page_text = ASCIIToUTF16("dummy2");
340 OnStartPhishingDetection(url); 201 OnStartPhishingDetection(new_url);
341 { 202 {
342 InSequence s; 203 InSequence s;
343 EXPECT_CALL(*classifier_, CancelPendingClassification()); 204 EXPECT_CALL(*classifier_, CancelPendingClassification());
344 EXPECT_CALL(*classifier_, BeginClassification(Pointee(page_text), _)); 205 EXPECT_CALL(*classifier_, BeginClassification(Pointee(page_text), _));
345 PageCaptured(&page_text, false); 206 PageCaptured(&page_text, false, new_url);
346 Mock::VerifyAndClearExpectations(classifier_); 207 Mock::VerifyAndClearExpectations(classifier_);
347 } 208 }
348 209
349 // No classification should happen on back/forward navigation. 210 // No classification should happen on back/forward navigation.
350 // Note: in practice, the browser will not send a StartPhishingDetection IPC 211 // Note: in practice, the browser will not send a StartPhishingDetection IPC
351 // in this case. However, we want to make sure that the delegate behaves 212 // in this case. However, we want to make sure that the delegate behaves
352 // correctly regardless. 213 // correctly regardless.
353 EXPECT_CALL(*classifier_, CancelPendingClassification()).Times(1); 214 EXPECT_CALL(*classifier_, CancelPendingClassification()).Times(1);
354 GoBack(); 215 // Simulate a go back navigation, i.e. back to http://host.com/index.html.
216 SimulatePageTrantitionForwardOrBack(html.c_str(), url.spec().c_str());
355 Mock::VerifyAndClearExpectations(classifier_); 217 Mock::VerifyAndClearExpectations(classifier_);
356
357 page_text = ASCIIToUTF16("dummy"); 218 page_text = ASCIIToUTF16("dummy");
358 OnStartPhishingDetection(url); 219 OnStartPhishingDetection(new_url);
359 EXPECT_CALL(*classifier_, CancelPendingClassification()); 220 EXPECT_CALL(*classifier_, CancelPendingClassification());
360 PageCaptured(&page_text, false); 221 PageCaptured(&page_text, false, new_url);
361 Mock::VerifyAndClearExpectations(classifier_); 222 Mock::VerifyAndClearExpectations(classifier_);
362 223
363 EXPECT_CALL(*classifier_, CancelPendingClassification()); 224 EXPECT_CALL(*classifier_, CancelPendingClassification());
364 GoForward(); 225 // Simulate a go forward navigation, i.e. forward to http://host.com
226 SimulatePageTrantitionForwardOrBack("dummy2", new_url.spec().c_str());
365 Mock::VerifyAndClearExpectations(classifier_); 227 Mock::VerifyAndClearExpectations(classifier_);
366 228
367 page_text = ASCIIToUTF16("dummy2"); 229 page_text = ASCIIToUTF16("dummy2");
368 OnStartPhishingDetection(url); 230 OnStartPhishingDetection(url);
369 EXPECT_CALL(*classifier_, CancelPendingClassification()); 231 EXPECT_CALL(*classifier_, CancelPendingClassification());
370 PageCaptured(&page_text, false); 232 PageCaptured(&page_text, false, url);
371 Mock::VerifyAndClearExpectations(classifier_); 233 Mock::VerifyAndClearExpectations(classifier_);
372 234
373 // Now go back again and scroll to a different anchor. 235 // Now go back again and navigate to a different place within
374 // No classification should happen. 236 // the same page. No classification should happen.
375 EXPECT_CALL(*classifier_, CancelPendingClassification()); 237 EXPECT_CALL(*classifier_, CancelPendingClassification());
376 GoBack(); 238 // Simulate a go back again to http://host.com/index.html
239 SimulatePageTrantitionForwardOrBack(html.c_str(), url.spec().c_str());
377 Mock::VerifyAndClearExpectations(classifier_); 240 Mock::VerifyAndClearExpectations(classifier_);
378 page_text = ASCIIToUTF16("dummy"); 241 page_text = ASCIIToUTF16("dummy");
379 242
380 OnStartPhishingDetection(url); 243 OnStartPhishingDetection(url);
381 EXPECT_CALL(*classifier_, CancelPendingClassification()); 244 EXPECT_CALL(*classifier_, CancelPendingClassification());
382 PageCaptured(&page_text, false); 245 PageCaptured(&page_text, false, url);
383 Mock::VerifyAndClearExpectations(classifier_); 246 Mock::VerifyAndClearExpectations(classifier_);
384 247
385 EXPECT_CALL(*classifier_, CancelPendingClassification()); 248 EXPECT_CALL(*classifier_, CancelPendingClassification());
386 GURL foo2_url = GURL(foo_url.spec() + "2"); 249 // Inp page navigation.
Nathan Parker 2016/01/26 16:49:19 "In-page"?
Jialiu Lin 2016/01/28 19:55:23 Done.
387 ui_test_utils::NavigateToURL(browser(), foo2_url); 250 DidNavigateWithinPage(GetMainFrame(), true);
388 Mock::VerifyAndClearExpectations(classifier_); 251 Mock::VerifyAndClearExpectations(classifier_);
389 252
390 OnStartPhishingDetection(url); 253 OnStartPhishingDetection(url);
391 page_text = ASCIIToUTF16("dummy"); 254 page_text = ASCIIToUTF16("dummy");
392 EXPECT_CALL(*classifier_, CancelPendingClassification()); 255 EXPECT_CALL(*classifier_, CancelPendingClassification());
393 PageCaptured(&page_text, false); 256 PageCaptured(&page_text, false, url);
394 Mock::VerifyAndClearExpectations(classifier_); 257 Mock::VerifyAndClearExpectations(classifier_);
395 258
396 // The delegate will cancel pending classification on destruction. 259 // The delegate will cancel pending classification on destruction.
397 EXPECT_CALL(*classifier_, CancelPendingClassification()); 260 EXPECT_CALL(*classifier_, CancelPendingClassification());
398 } 261 }
399 262
400 // Flaky: crbug.com/479757 263 TEST_F(PhishingClassifierDelegateTest, NoScorer) {
401 #if defined(LEAK_SANITIZER)
402 #define MAYBE_NoScorer DISABLED_NoScorer
403 #else
404 #define MAYBE_NoScorer NoScorer
405 #endif
406
407 IN_PROC_BROWSER_TEST_F(PhishingClassifierDelegateTest, MAYBE_NoScorer) {
408 // For this test, we'll create the delegate with no scorer available yet. 264 // For this test, we'll create the delegate with no scorer available yet.
Nathan Parker 2016/01/26 16:49:19 Do you know when this could happen? Is if we have
Jialiu Lin 2016/01/28 19:55:23 yes, this tests if we haven't received the model y
409 ASSERT_FALSE(classifier_->is_ready()); 265 ASSERT_FALSE(classifier_->is_ready());
410 266
411 // Queue up a pending classification, cancel it, then queue up another one. 267 // Queue up a pending classification, cancel it, then queue up another one.
412 GURL url = LoadHtml("host.com", "dummy"); 268 GURL url("http://host.com");
413 base::string16 page_text = ASCIIToUTF16("dummy"); 269 base::string16 page_text = ASCIIToUTF16("dummy");
270 LoadHTMLWithUrlOverride("dummy", url.spec().c_str());
414 OnStartPhishingDetection(url); 271 OnStartPhishingDetection(url);
415 PageCaptured(&page_text, false); 272 PageCaptured(&page_text, false, url);
416 273
417 url = LoadHtml("host2.com", "dummy2"); 274 GURL url2("http://host2.com");
418 page_text = ASCIIToUTF16("dummy2"); 275 page_text = ASCIIToUTF16("dummy");
419 OnStartPhishingDetection(url); 276 LoadHTMLWithUrlOverride("dummy", url2.spec().c_str());
420 PageCaptured(&page_text, false); 277 OnStartPhishingDetection(url2);
278 PageCaptured(&page_text, false, url2);
421 279
422 // Now set a scorer, which should cause a classifier to be created and 280 // Now set a scorer, which should cause a classifier to be created and
423 // the classification to proceed. 281 // the classification to proceed.
424 page_text = ASCIIToUTF16("dummy2"); 282 page_text = ASCIIToUTF16("dummy");
425 EXPECT_CALL(*classifier_, BeginClassification(Pointee(page_text), _)); 283 EXPECT_CALL(*classifier_, BeginClassification(Pointee(page_text), _));
426 MockScorer scorer; 284 MockScorer scorer;
427 delegate_->SetPhishingScorer(&scorer); 285 delegate_->SetPhishingScorer(&scorer);
428 Mock::VerifyAndClearExpectations(classifier_); 286 Mock::VerifyAndClearExpectations(classifier_);
429 287
430 // If we set a new scorer while a classification is going on the 288 // If we set a new scorer while a classification is going on the
431 // classification should be cancelled. 289 // classification should be cancelled.
432 EXPECT_CALL(*classifier_, CancelPendingClassification()); 290 EXPECT_CALL(*classifier_, CancelPendingClassification());
433 delegate_->SetPhishingScorer(&scorer); 291 delegate_->SetPhishingScorer(&scorer);
434 Mock::VerifyAndClearExpectations(classifier_); 292 Mock::VerifyAndClearExpectations(classifier_);
435 293
436 // The delegate will cancel pending classification on destruction. 294 // The delegate will cancel pending classification on destruction.
437 EXPECT_CALL(*classifier_, CancelPendingClassification()); 295 EXPECT_CALL(*classifier_, CancelPendingClassification());
438 } 296 }
439 297
440 // Flaky: crbug.com/435719 298 TEST_F(PhishingClassifierDelegateTest, NoScorer_Ref) {
441 #if defined(LEAK_SANITIZER)
442 #define MAYBE_NoScorer_Ref DISABLED_NoScorer_Ref
443 #else
444 #define MAYBE_NoScorer_Ref NoScorer_Ref
445 #endif
446
447 IN_PROC_BROWSER_TEST_F(PhishingClassifierDelegateTest, MAYBE_NoScorer_Ref) {
448 // Similar to the last test, but navigates within the page before 299 // Similar to the last test, but navigates within the page before
449 // setting the scorer. 300 // setting the scorer.
450 ASSERT_FALSE(classifier_->is_ready()); 301 ASSERT_FALSE(classifier_->is_ready());
451 302
452 // Queue up a pending classification, cancel it, then queue up another one. 303 // Queue up a pending classification, cancel it, then queue up another one.
453 GURL url = LoadHtml("host.com", "dummy"); 304 GURL url("http://host.com");
454 base::string16 page_text = ASCIIToUTF16("dummy"); 305 base::string16 page_text = ASCIIToUTF16("dummy");
306 LoadHTMLWithUrlOverride("dummy", url.spec().c_str());
455 OnStartPhishingDetection(url); 307 OnStartPhishingDetection(url);
456 PageCaptured(&page_text, false); 308 PageCaptured(&page_text, false, url);
457 309
310 page_text = ASCIIToUTF16("dummy");
458 OnStartPhishingDetection(url); 311 OnStartPhishingDetection(url);
459 page_text = ASCIIToUTF16("dummy"); 312 PageCaptured(&page_text, false, url);
460 PageCaptured(&page_text, false);
461 313
462 // Now set a scorer, which should cause a classifier to be created and 314 // Now set a scorer, which should cause a classifier to be created and
463 // the classification to proceed. 315 // the classification to proceed.
464 page_text = ASCIIToUTF16("dummy"); 316 page_text = ASCIIToUTF16("dummy");
465 EXPECT_CALL(*classifier_, BeginClassification(Pointee(page_text), _)); 317 EXPECT_CALL(*classifier_, BeginClassification(Pointee(page_text), _));
466 MockScorer scorer; 318 MockScorer scorer;
467 delegate_->SetPhishingScorer(&scorer); 319 delegate_->SetPhishingScorer(&scorer);
468 Mock::VerifyAndClearExpectations(classifier_); 320 Mock::VerifyAndClearExpectations(classifier_);
469 321
322 // If we set a new scorer while a classification is going on the
323 // classification should be cancelled.
324 EXPECT_CALL(*classifier_, CancelPendingClassification());
325 delegate_->SetPhishingScorer(&scorer);
326 Mock::VerifyAndClearExpectations(classifier_);
327
470 // The delegate will cancel pending classification on destruction. 328 // The delegate will cancel pending classification on destruction.
471 EXPECT_CALL(*classifier_, CancelPendingClassification()); 329 EXPECT_CALL(*classifier_, CancelPendingClassification());
472 } 330 }
473 331
474 IN_PROC_BROWSER_TEST_F(PhishingClassifierDelegateTest, 332 TEST_F(PhishingClassifierDelegateTest, NoStartPhishingDetection) {
475 NoStartPhishingDetection) {
476 // Tests the behavior when OnStartPhishingDetection has not yet been called 333 // Tests the behavior when OnStartPhishingDetection has not yet been called
477 // when the page load finishes. 334 // when the page load finishes.
478 MockScorer scorer; 335 MockScorer scorer;
479 delegate_->SetPhishingScorer(&scorer); 336 delegate_->SetPhishingScorer(&scorer);
480 ASSERT_TRUE(classifier_->is_ready()); 337 ASSERT_TRUE(classifier_->is_ready());
481 338
482 EXPECT_CALL(*classifier_, CancelPendingClassification()); 339 EXPECT_CALL(*classifier_, CancelPendingClassification());
483 GURL url = LoadHtml("host.com", "<html><body>phish</body></html>"); 340 GURL url("http://host.com");
341 LoadHTMLWithUrlOverride("<html><body>phish</body></html>",
342 url.spec().c_str());
484 Mock::VerifyAndClearExpectations(classifier_); 343 Mock::VerifyAndClearExpectations(classifier_);
485 base::string16 page_text = ASCIIToUTF16("phish"); 344 base::string16 page_text = ASCIIToUTF16("phish");
486 EXPECT_CALL(*classifier_, CancelPendingClassification()); 345 EXPECT_CALL(*classifier_, CancelPendingClassification());
487 PageCaptured(&page_text, false); 346 PageCaptured(&page_text, false, url);
488 Mock::VerifyAndClearExpectations(classifier_); 347 Mock::VerifyAndClearExpectations(classifier_);
489 // Now simulate the StartPhishingDetection IPC. We expect classification 348 // Now simulate the StartPhishingDetection IPC. We expect classification
490 // to begin. 349 // to begin.
491 page_text = ASCIIToUTF16("phish"); 350 page_text = ASCIIToUTF16("phish");
492 EXPECT_CALL(*classifier_, BeginClassification(Pointee(page_text), _)); 351 EXPECT_CALL(*classifier_, BeginClassification(Pointee(page_text), _));
493 OnStartPhishingDetection(url); 352 OnStartPhishingDetection(url);
494 Mock::VerifyAndClearExpectations(classifier_); 353 Mock::VerifyAndClearExpectations(classifier_);
495 354
496 // Now try again, but this time we will navigate the page away before 355 // Now try again, but this time we will navigate the page away before
497 // the IPC is sent. 356 // the IPC is sent.
498 EXPECT_CALL(*classifier_, CancelPendingClassification()); 357 EXPECT_CALL(*classifier_, CancelPendingClassification());
499 LoadHtml("host2.com", "<html><body>phish</body></html>"); 358 GURL url2("http://host2.com");
359 LoadHTMLWithUrlOverride("<html><body>phish</body></html>",
360 url2.spec().c_str());
500 Mock::VerifyAndClearExpectations(classifier_); 361 Mock::VerifyAndClearExpectations(classifier_);
501 page_text = ASCIIToUTF16("phish"); 362 page_text = ASCIIToUTF16("phish");
502 EXPECT_CALL(*classifier_, CancelPendingClassification()); 363 EXPECT_CALL(*classifier_, CancelPendingClassification());
503 PageCaptured(&page_text, false); 364 PageCaptured(&page_text, false, url2);
504 Mock::VerifyAndClearExpectations(classifier_); 365 Mock::VerifyAndClearExpectations(classifier_);
505 366
506 EXPECT_CALL(*classifier_, CancelPendingClassification()); 367 EXPECT_CALL(*classifier_, CancelPendingClassification());
507 LoadHtml("host3.com", "<html><body>phish</body></html>"); 368 GURL url3("http://host3.com");
369 LoadHTMLWithUrlOverride("<html><body>phish</body></html>",
370 url3.spec().c_str());
508 Mock::VerifyAndClearExpectations(classifier_); 371 Mock::VerifyAndClearExpectations(classifier_);
509 OnStartPhishingDetection(url); 372 OnStartPhishingDetection(url);
510 373
511 // In this test, the original page is a redirect, which we do not get a 374 // In this test, the original page is a redirect, which we do not get a
512 // StartPhishingDetection IPC for. We use location.replace() to load a 375 // StartPhishingDetection IPC for. We simulate the redirection event to load
376 // a
Nathan Parker 2016/01/26 16:49:19 nit: paragraph flow.
Jialiu Lin 2016/01/28 19:55:23 Done.
513 // new page while reusing the original session history entry, and check that 377 // new page while reusing the original session history entry, and check that
514 // classification begins correctly for the landing page. 378 // classification begins correctly for the landing page.
515 EXPECT_CALL(*classifier_, CancelPendingClassification()); 379 EXPECT_CALL(*classifier_, CancelPendingClassification());
516 LoadHtml("host4.com", "<html><body>abc</body></html>"); 380 GURL url4("http://host4.com");
381 LoadHTMLWithUrlOverride("<html><body>phish</body></html>",
382 url4.spec().c_str());
517 Mock::VerifyAndClearExpectations(classifier_); 383 Mock::VerifyAndClearExpectations(classifier_);
518 page_text = ASCIIToUTF16("abc"); 384 page_text = ASCIIToUTF16("abc");
519 EXPECT_CALL(*classifier_, CancelPendingClassification()); 385 EXPECT_CALL(*classifier_, CancelPendingClassification());
520 PageCaptured(&page_text, false); 386 PageCaptured(&page_text, false, url4);
521 Mock::VerifyAndClearExpectations(classifier_); 387 Mock::VerifyAndClearExpectations(classifier_);
522 EXPECT_CALL(*classifier_, CancelPendingClassification()); 388 EXPECT_CALL(*classifier_, CancelPendingClassification());
523 389
524 ui_test_utils::NavigateToURL( 390 GURL redir_url("http://host4.com/redir");
525 browser(), GURL("javascript:location.replace(\'redir\');")); 391 LoadHTMLWithUrlOverride("123", redir_url.spec().c_str());
526
527 Mock::VerifyAndClearExpectations(classifier_); 392 Mock::VerifyAndClearExpectations(classifier_);
528 393 OnStartPhishingDetection(url4);
529 GURL redir_url = embedded_test_server()->GetURL("/redir");
530 GURL::Replacements replace_host;
531 replace_host.SetHostStr("host4.com");
532 OnStartPhishingDetection(redir_url.ReplaceComponents(replace_host));
533 page_text = ASCIIToUTF16("123"); 394 page_text = ASCIIToUTF16("123");
534 { 395 {
535 InSequence s; 396 InSequence s;
536 EXPECT_CALL(*classifier_, CancelPendingClassification()); 397 EXPECT_CALL(*classifier_, CancelPendingClassification());
537 EXPECT_CALL(*classifier_, BeginClassification(Pointee(page_text), _)); 398 EXPECT_CALL(*classifier_, BeginClassification(Pointee(page_text), _));
538 PageCaptured(&page_text, false); 399 SimulateRedirection(redir_url);
400 PageCaptured(&page_text, false, redir_url);
539 Mock::VerifyAndClearExpectations(classifier_); 401 Mock::VerifyAndClearExpectations(classifier_);
540 } 402 }
541 403
542 // The delegate will cancel pending classification on destruction. 404 // The delegate will cancel pending classification on destruction.
543 EXPECT_CALL(*classifier_, CancelPendingClassification()); 405 EXPECT_CALL(*classifier_, CancelPendingClassification());
544 } 406 }
545 407
546 // Test flakes with LSAN enabled. See http://crbug.com/373155. 408 TEST_F(PhishingClassifierDelegateTest, IgnorePreliminaryCapture) {
547 #if defined(LEAK_SANITIZER)
548 #define MAYBE_IgnorePreliminaryCapture DISABLED_IgnorePreliminaryCapture
549 #else
550 #define MAYBE_IgnorePreliminaryCapture IgnorePreliminaryCapture
551 #endif
552 IN_PROC_BROWSER_TEST_F(PhishingClassifierDelegateTest,
553 MAYBE_IgnorePreliminaryCapture) {
554 // Tests that preliminary PageCaptured notifications are ignored. 409 // Tests that preliminary PageCaptured notifications are ignored.
555 MockScorer scorer; 410 MockScorer scorer;
556 delegate_->SetPhishingScorer(&scorer); 411 delegate_->SetPhishingScorer(&scorer);
557 ASSERT_TRUE(classifier_->is_ready()); 412 ASSERT_TRUE(classifier_->is_ready());
558 413
559 EXPECT_CALL(*classifier_, CancelPendingClassification()); 414 EXPECT_CALL(*classifier_, CancelPendingClassification());
560 GURL url = LoadHtml("host.com", "<html><body>phish</body></html>"); 415 GURL url("http://host.com");
416 LoadHTMLWithUrlOverride("<html><body>phish</body></html>",
417 url.spec().c_str());
561 Mock::VerifyAndClearExpectations(classifier_); 418 Mock::VerifyAndClearExpectations(classifier_);
562 OnStartPhishingDetection(url); 419 OnStartPhishingDetection(url);
563 base::string16 page_text = ASCIIToUTF16("phish"); 420 base::string16 page_text = ASCIIToUTF16("phish");
564 PageCaptured(&page_text, true); 421 PageCaptured(&page_text, true, url);
565 422
566 // Once the non-preliminary capture happens, classification should begin. 423 // Once the non-preliminary capture happens, classification should begin.
567 page_text = ASCIIToUTF16("phish"); 424 page_text = ASCIIToUTF16("phish");
568 { 425 {
569 InSequence s; 426 InSequence s;
570 EXPECT_CALL(*classifier_, CancelPendingClassification()); 427 EXPECT_CALL(*classifier_, CancelPendingClassification());
571 EXPECT_CALL(*classifier_, BeginClassification(Pointee(page_text), _)); 428 EXPECT_CALL(*classifier_, BeginClassification(Pointee(page_text), _));
572 PageCaptured(&page_text, false); 429 PageCaptured(&page_text, false, url);
573 Mock::VerifyAndClearExpectations(classifier_); 430 Mock::VerifyAndClearExpectations(classifier_);
574 } 431 }
575 432
576 // The delegate will cancel pending classification on destruction. 433 // The delegate will cancel pending classification on destruction.
577 EXPECT_CALL(*classifier_, CancelPendingClassification()); 434 EXPECT_CALL(*classifier_, CancelPendingClassification());
578 } 435 }
579 436
580 #if defined(ADDRESS_SANITIZER) 437 TEST_F(PhishingClassifierDelegateTest, DuplicatePageCapture) {
581 #define Maybe_DuplicatePageCapture DISABLED_DuplicatePageCapture
582 #else
583 #define Maybe_DuplicatePageCapture DuplicatePageCapture
584 #endif
585 IN_PROC_BROWSER_TEST_F(PhishingClassifierDelegateTest,
586 Maybe_DuplicatePageCapture) {
587 // Tests that a second PageCaptured notification causes classification to 438 // Tests that a second PageCaptured notification causes classification to
588 // be cancelled. 439 // be cancelled.
589 MockScorer scorer; 440 MockScorer scorer;
590 delegate_->SetPhishingScorer(&scorer); 441 delegate_->SetPhishingScorer(&scorer);
591 ASSERT_TRUE(classifier_->is_ready()); 442 ASSERT_TRUE(classifier_->is_ready());
592 443
593 EXPECT_CALL(*classifier_, CancelPendingClassification()); 444 EXPECT_CALL(*classifier_, CancelPendingClassification());
594 GURL url = LoadHtml("host.com", "<html><body>phish</body></html>"); 445 GURL url("http://host.com");
446 LoadHTMLWithUrlOverride("<html><body>phish</body></html>",
447 url.spec().c_str());
595 Mock::VerifyAndClearExpectations(classifier_); 448 Mock::VerifyAndClearExpectations(classifier_);
596 OnStartPhishingDetection(url); 449 OnStartPhishingDetection(url);
597 base::string16 page_text = ASCIIToUTF16("phish"); 450 base::string16 page_text = ASCIIToUTF16("phish");
598 { 451 {
599 InSequence s; 452 InSequence s;
600 EXPECT_CALL(*classifier_, CancelPendingClassification()); 453 EXPECT_CALL(*classifier_, CancelPendingClassification());
601 EXPECT_CALL(*classifier_, BeginClassification(Pointee(page_text), _)); 454 EXPECT_CALL(*classifier_, BeginClassification(Pointee(page_text), _));
602 PageCaptured(&page_text, false); 455 PageCaptured(&page_text, false, url);
603 Mock::VerifyAndClearExpectations(classifier_); 456 Mock::VerifyAndClearExpectations(classifier_);
604 } 457 }
605 458
606 page_text = ASCIIToUTF16("phish"); 459 page_text = ASCIIToUTF16("phish");
607 EXPECT_CALL(*classifier_, CancelPendingClassification()); 460 EXPECT_CALL(*classifier_, CancelPendingClassification());
608 PageCaptured(&page_text, false); 461 PageCaptured(&page_text, false, url);
609 Mock::VerifyAndClearExpectations(classifier_); 462 Mock::VerifyAndClearExpectations(classifier_);
610 463
611 // The delegate will cancel pending classification on destruction. 464 // The delegate will cancel pending classification on destruction.
612 EXPECT_CALL(*classifier_, CancelPendingClassification()); 465 EXPECT_CALL(*classifier_, CancelPendingClassification());
613 } 466 }
614 467
615 // Test flakes with LSAN enabled. See http://crbug.com/373155. 468 TEST_F(PhishingClassifierDelegateTest, PhishingDetectionDone) {
616 #if defined(LEAK_SANITIZER) 469 // Tests that a SafeBrowsingHostMsg_PhishingDetectionDone IPC is
617 #define MAYBE_PhishingDetectionDone DISABLED_PhishingDetectionDone 470 // sent to the browser whenever we finish classification.
618 #else
619 #define MAYBE_PhishingDetectionDone PhishingDetectionDone
620 #endif
621 IN_PROC_BROWSER_TEST_F(PhishingClassifierDelegateTest,
622 MAYBE_PhishingDetectionDone) {
623 // Tests that a PhishingDetectionDone IPC is sent to the browser
624 // whenever we finish classification.
625 MockScorer scorer; 471 MockScorer scorer;
626 delegate_->SetPhishingScorer(&scorer); 472 delegate_->SetPhishingScorer(&scorer);
627 ASSERT_TRUE(classifier_->is_ready()); 473 ASSERT_TRUE(classifier_->is_ready());
628 474
629 // Start by loading a page to populate the delegate's state. 475 // Start by loading a page to populate the delegate's state.
630 EXPECT_CALL(*classifier_, CancelPendingClassification()); 476 EXPECT_CALL(*classifier_, CancelPendingClassification());
631 GURL url = LoadHtml("host.com", "<html><body>phish</body></html>"); 477 GURL url("http://host.com");
478 LoadHTMLWithUrlOverride("<html><body>phish</body></html>",
479 url.spec().c_str());
632 Mock::VerifyAndClearExpectations(classifier_); 480 Mock::VerifyAndClearExpectations(classifier_);
633 base::string16 page_text = ASCIIToUTF16("phish"); 481 base::string16 page_text = ASCIIToUTF16("phish");
634 OnStartPhishingDetection(url); 482 OnStartPhishingDetection(url);
635 { 483 {
636 InSequence s; 484 InSequence s;
637 EXPECT_CALL(*classifier_, CancelPendingClassification()); 485 EXPECT_CALL(*classifier_, CancelPendingClassification());
638 EXPECT_CALL(*classifier_, BeginClassification(Pointee(page_text), _)); 486 EXPECT_CALL(*classifier_, BeginClassification(Pointee(page_text), _));
639 PageCaptured(&page_text, false); 487 PageCaptured(&page_text, false, url);
640 Mock::VerifyAndClearExpectations(classifier_); 488 Mock::VerifyAndClearExpectations(classifier_);
641 } 489 }
642 490
643 // Now run the callback to simulate the classifier finishing. 491 // Now run the callback to simulate the classifier finishing.
644 ClientPhishingRequest verdict; 492 ClientPhishingRequest verdict;
645 verdict.set_url(url.spec()); 493 verdict.set_url(url.spec());
646 verdict.set_client_score(0.8f); 494 verdict.set_client_score(0.8f);
647 verdict.set_is_phishing(false); // Send IPC even if site is not phishing. 495 verdict.set_is_phishing(false); // Send IPC even if site is not phishing.
648 RunClassificationDone(verdict); 496 RunAndVerifyClassificationDone(verdict);
649 ASSERT_TRUE(intercepting_filter_->verdict());
650 EXPECT_EQ(verdict.SerializeAsString(),
651 intercepting_filter_->verdict()->SerializeAsString());
652 497
653 // The delegate will cancel pending classification on destruction. 498 // The delegate will cancel pending classification on destruction.
654 EXPECT_CALL(*classifier_, CancelPendingClassification()); 499 EXPECT_CALL(*classifier_, CancelPendingClassification());
655 } 500 }
656 501
657 } // namespace safe_browsing 502 } // namespace safe_browsing
OLDNEW
« no previous file with comments | « no previous file | chrome/renderer/safe_browsing/threat_dom_details.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698