OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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_path.h" | 5 #include "base/file_path.h" |
6 #include "base/memory/ref_counted.h" | 6 #include "base/memory/ref_counted.h" |
7 #include "base/memory/scoped_ptr.h" | 7 #include "base/memory/scoped_ptr.h" |
8 #include "base/task.h" | 8 #include "base/task.h" |
9 #include "chrome/browser/safe_browsing/browser_feature_extractor.h" | 9 #include "chrome/browser/safe_browsing/browser_feature_extractor.h" |
10 #include "chrome/browser/safe_browsing/client_side_detection_host.h" | 10 #include "chrome/browser/safe_browsing/client_side_detection_host.h" |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
50 // (ClientPhishingRequest) which is passed to SendClientReportPhishingRequest | 50 // (ClientPhishingRequest) which is passed to SendClientReportPhishingRequest |
51 // has the expected fields set. Note: we can't simply compare the protocol | 51 // has the expected fields set. Note: we can't simply compare the protocol |
52 // buffer strings because the BrowserFeatureExtractor might add features to the | 52 // buffer strings because the BrowserFeatureExtractor might add features to the |
53 // verdict object before calling SendClientReportPhishingRequest. | 53 // verdict object before calling SendClientReportPhishingRequest. |
54 MATCHER_P(PartiallyEqualVerdict, other, "") { | 54 MATCHER_P(PartiallyEqualVerdict, other, "") { |
55 return (other.url() == arg.url() && | 55 return (other.url() == arg.url() && |
56 other.client_score() == arg.client_score() && | 56 other.client_score() == arg.client_score() && |
57 other.is_phishing() == arg.is_phishing()); | 57 other.is_phishing() == arg.is_phishing()); |
58 } | 58 } |
59 | 59 |
| 60 // Test that the callback is NULL when the verdict is not phishing. |
| 61 MATCHER(CallbackIsNull, "") { |
| 62 return arg.is_null(); |
| 63 } |
| 64 |
60 ACTION(QuitUIMessageLoop) { | 65 ACTION(QuitUIMessageLoop) { |
61 EXPECT_TRUE(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 66 EXPECT_TRUE(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
62 MessageLoopForUI::current()->Quit(); | 67 MessageLoopForUI::current()->Quit(); |
63 } | 68 } |
64 | 69 |
65 // It's kind of insane that InvokeArgument doesn't work with callbacks, but it | 70 // It's kind of insane that InvokeArgument doesn't work with callbacks, but it |
66 // doesn't seem like it. | 71 // doesn't seem like it. |
67 ACTION_TEMPLATE(InvokeCallbackArgument, | 72 ACTION_TEMPLATE(InvokeCallbackArgument, |
68 HAS_1_TEMPLATE_PARAMS(int, k), | 73 HAS_1_TEMPLATE_PARAMS(int, k), |
69 AND_2_VALUE_PARAMS(p0, p1)) { | 74 AND_2_VALUE_PARAMS(p0, p1)) { |
70 ::std::tr1::get<k>(args)->Run(p0, p1); | 75 ::std::tr1::get<k>(args).Run(p0, p1); |
71 // This is an old callback, so it needs to be deleted explicitly. | |
72 delete ::std::tr1::get<k>(args); | |
73 } | 76 } |
74 | 77 |
75 class MockClientSideDetectionService : public ClientSideDetectionService { | 78 class MockClientSideDetectionService : public ClientSideDetectionService { |
76 public: | 79 public: |
77 MockClientSideDetectionService() : ClientSideDetectionService(NULL) {} | 80 MockClientSideDetectionService() : ClientSideDetectionService(NULL) {} |
78 virtual ~MockClientSideDetectionService() {}; | 81 virtual ~MockClientSideDetectionService() {}; |
79 | 82 |
80 MOCK_METHOD2(SendClientReportPhishingRequest, | 83 MOCK_METHOD2(SendClientReportPhishingRequest, |
81 void(ClientPhishingRequest*, | 84 void(ClientPhishingRequest*, |
82 ClientReportPhishingRequestCallback*)); | 85 const ClientReportPhishingRequestCallback&)); |
83 MOCK_CONST_METHOD1(IsPrivateIPAddress, bool(const std::string&)); | 86 MOCK_CONST_METHOD1(IsPrivateIPAddress, bool(const std::string&)); |
84 MOCK_METHOD2(GetValidCachedResult, bool(const GURL&, bool*)); | 87 MOCK_METHOD2(GetValidCachedResult, bool(const GURL&, bool*)); |
85 MOCK_METHOD1(IsInCache, bool(const GURL&)); | 88 MOCK_METHOD1(IsInCache, bool(const GURL&)); |
86 MOCK_METHOD0(OverReportLimit, bool()); | 89 MOCK_METHOD0(OverReportLimit, bool()); |
87 | 90 |
88 private: | 91 private: |
89 DISALLOW_COPY_AND_ASSIGN(MockClientSideDetectionService); | 92 DISALLOW_COPY_AND_ASSIGN(MockClientSideDetectionService); |
90 }; | 93 }; |
91 | 94 |
92 class MockSafeBrowsingService : public SafeBrowsingService { | 95 class MockSafeBrowsingService : public SafeBrowsingService { |
(...skipping 27 matching lines...) Expand all Loading... |
120 }; | 123 }; |
121 | 124 |
122 class MockBrowserFeatureExtractor : public BrowserFeatureExtractor { | 125 class MockBrowserFeatureExtractor : public BrowserFeatureExtractor { |
123 public: | 126 public: |
124 explicit MockBrowserFeatureExtractor( | 127 explicit MockBrowserFeatureExtractor( |
125 TabContents* tab, | 128 TabContents* tab, |
126 ClientSideDetectionService* service) | 129 ClientSideDetectionService* service) |
127 : BrowserFeatureExtractor(tab, service) {} | 130 : BrowserFeatureExtractor(tab, service) {} |
128 virtual ~MockBrowserFeatureExtractor() {} | 131 virtual ~MockBrowserFeatureExtractor() {} |
129 | 132 |
130 MOCK_METHOD3(ExtractFeatures, void(const BrowseInfo* info, | 133 MOCK_METHOD3(ExtractFeatures, |
131 ClientPhishingRequest*, | 134 void(const BrowseInfo* info, |
132 BrowserFeatureExtractor::DoneCallback*)); | 135 ClientPhishingRequest*, |
| 136 const BrowserFeatureExtractor::DoneCallback&)); |
133 }; | 137 }; |
134 | 138 |
135 // Helper function which quits the UI message loop from the IO message loop. | 139 // Helper function which quits the UI message loop from the IO message loop. |
136 void QuitUIMessageLoopFromIO() { | 140 void QuitUIMessageLoopFromIO() { |
137 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 141 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
138 BrowserThread::PostTask(BrowserThread::UI, | 142 BrowserThread::PostTask(BrowserThread::UI, |
139 FROM_HERE, | 143 FROM_HERE, |
140 new MessageLoop::QuitTask()); | 144 new MessageLoop::QuitTask()); |
141 } | 145 } |
142 } // namespace | 146 } // namespace |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
188 void OnPhishingDetectionDone(const std::string& verdict_str) { | 192 void OnPhishingDetectionDone(const std::string& verdict_str) { |
189 csd_host_->OnPhishingDetectionDone(verdict_str); | 193 csd_host_->OnPhishingDetectionDone(verdict_str); |
190 } | 194 } |
191 | 195 |
192 void FlushIOMessageLoop() { | 196 void FlushIOMessageLoop() { |
193 // If there was a message posted on the IO thread to display the | 197 // If there was a message posted on the IO thread to display the |
194 // interstitial page we know that it would have been posted before | 198 // interstitial page we know that it would have been posted before |
195 // we put the quit message there. | 199 // we put the quit message there. |
196 BrowserThread::PostTask(BrowserThread::IO, | 200 BrowserThread::PostTask(BrowserThread::IO, |
197 FROM_HERE, | 201 FROM_HERE, |
198 NewRunnableFunction(&QuitUIMessageLoopFromIO)); | 202 base::Bind(&QuitUIMessageLoopFromIO)); |
199 MessageLoop::current()->Run(); | 203 MessageLoop::current()->Run(); |
200 } | 204 } |
201 | 205 |
202 void ExpectPreClassificationChecks(const GURL& url, | 206 void ExpectPreClassificationChecks(const GURL& url, |
203 const bool* is_private, | 207 const bool* is_private, |
204 const bool* is_incognito, | 208 const bool* is_incognito, |
205 const bool* match_csd_whitelist, | 209 const bool* match_csd_whitelist, |
206 const bool* get_valid_cached_result, | 210 const bool* get_valid_cached_result, |
207 const bool* is_in_cache, | 211 const bool* is_in_cache, |
208 const bool* over_report_limit) { | 212 const bool* over_report_limit) { |
(...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
308 } | 312 } |
309 | 313 |
310 TEST_F(ClientSideDetectionHostTest, OnPhishingDetectionDoneNotPhishing) { | 314 TEST_F(ClientSideDetectionHostTest, OnPhishingDetectionDoneNotPhishing) { |
311 // Case 1: client thinks the page is phishing. The server does not agree. | 315 // Case 1: client thinks the page is phishing. The server does not agree. |
312 // No interstitial is shown. | 316 // No interstitial is shown. |
313 MockBrowserFeatureExtractor* mock_extractor = new MockBrowserFeatureExtractor( | 317 MockBrowserFeatureExtractor* mock_extractor = new MockBrowserFeatureExtractor( |
314 contents(), | 318 contents(), |
315 csd_service_.get()); | 319 csd_service_.get()); |
316 SetFeatureExtractor(mock_extractor); // The host class takes ownership. | 320 SetFeatureExtractor(mock_extractor); // The host class takes ownership. |
317 | 321 |
318 ClientSideDetectionService::ClientReportPhishingRequestCallback* cb; | 322 ClientSideDetectionService::ClientReportPhishingRequestCallback cb; |
319 ClientPhishingRequest verdict; | 323 ClientPhishingRequest verdict; |
320 verdict.set_url("http://phishingurl.com/"); | 324 verdict.set_url("http://phishingurl.com/"); |
321 verdict.set_client_score(1.0f); | 325 verdict.set_client_score(1.0f); |
322 verdict.set_is_phishing(true); | 326 verdict.set_is_phishing(true); |
323 | 327 |
324 EXPECT_CALL(*mock_extractor, ExtractFeatures(_, _, _)) | 328 EXPECT_CALL(*mock_extractor, ExtractFeatures(_, _, _)) |
325 .WillOnce(DoAll(DeleteArg<1>(), | 329 .WillOnce(DoAll(DeleteArg<1>(), |
326 InvokeCallbackArgument<2>(true, &verdict))); | 330 InvokeCallbackArgument<2>(true, &verdict))); |
327 EXPECT_CALL(*csd_service_, | 331 EXPECT_CALL(*csd_service_, |
328 SendClientReportPhishingRequest( | 332 SendClientReportPhishingRequest( |
329 Pointee(PartiallyEqualVerdict(verdict)), _)) | 333 Pointee(PartiallyEqualVerdict(verdict)), _)) |
330 .WillOnce(SaveArg<1>(&cb)); | 334 .WillOnce(SaveArg<1>(&cb)); |
331 OnPhishingDetectionDone(verdict.SerializeAsString()); | 335 OnPhishingDetectionDone(verdict.SerializeAsString()); |
332 EXPECT_TRUE(Mock::VerifyAndClear(csd_service_.get())); | 336 EXPECT_TRUE(Mock::VerifyAndClear(csd_service_.get())); |
333 ASSERT_TRUE(cb); | 337 ASSERT_FALSE(cb.is_null()); |
334 | 338 |
335 // Make sure DoDisplayBlockingPage is not going to be called. | 339 // Make sure DoDisplayBlockingPage is not going to be called. |
336 EXPECT_CALL(*sb_service_, DoDisplayBlockingPage(_)).Times(0); | 340 EXPECT_CALL(*sb_service_, DoDisplayBlockingPage(_)).Times(0); |
337 cb->Run(GURL(verdict.url()), false); | 341 cb.Run(GURL(verdict.url()), false); |
338 delete cb; | |
339 MessageLoop::current()->RunAllPending(); | 342 MessageLoop::current()->RunAllPending(); |
340 EXPECT_TRUE(Mock::VerifyAndClear(sb_service_.get())); | 343 EXPECT_TRUE(Mock::VerifyAndClear(sb_service_.get())); |
341 } | 344 } |
342 | 345 |
343 TEST_F(ClientSideDetectionHostTest, OnPhishingDetectionDoneDisabled) { | 346 TEST_F(ClientSideDetectionHostTest, OnPhishingDetectionDoneDisabled) { |
344 // Case 2: client thinks the page is phishing and so does the server but | 347 // Case 2: client thinks the page is phishing and so does the server but |
345 // showing the interstitial is disabled => no interstitial is shown. | 348 // showing the interstitial is disabled => no interstitial is shown. |
346 MockBrowserFeatureExtractor* mock_extractor = new MockBrowserFeatureExtractor( | 349 MockBrowserFeatureExtractor* mock_extractor = new MockBrowserFeatureExtractor( |
347 contents(), | 350 contents(), |
348 csd_service_.get()); | 351 csd_service_.get()); |
349 SetFeatureExtractor(mock_extractor); // The host class takes ownership. | 352 SetFeatureExtractor(mock_extractor); // The host class takes ownership. |
350 | 353 |
351 ClientSideDetectionService::ClientReportPhishingRequestCallback* cb; | 354 ClientSideDetectionService::ClientReportPhishingRequestCallback cb; |
352 ClientPhishingRequest verdict; | 355 ClientPhishingRequest verdict; |
353 verdict.set_url("http://phishingurl.com/"); | 356 verdict.set_url("http://phishingurl.com/"); |
354 verdict.set_client_score(1.0f); | 357 verdict.set_client_score(1.0f); |
355 verdict.set_is_phishing(true); | 358 verdict.set_is_phishing(true); |
356 | 359 |
357 EXPECT_CALL(*mock_extractor, ExtractFeatures(_, _, _)) | 360 EXPECT_CALL(*mock_extractor, ExtractFeatures(_, _, _)) |
358 .WillOnce(DoAll(DeleteArg<1>(), | 361 .WillOnce(DoAll(DeleteArg<1>(), |
359 InvokeCallbackArgument<2>(true, &verdict))); | 362 InvokeCallbackArgument<2>(true, &verdict))); |
360 EXPECT_CALL(*csd_service_, | 363 EXPECT_CALL(*csd_service_, |
361 SendClientReportPhishingRequest( | 364 SendClientReportPhishingRequest( |
362 Pointee(PartiallyEqualVerdict(verdict)), _)) | 365 Pointee(PartiallyEqualVerdict(verdict)), _)) |
363 .WillOnce(SaveArg<1>(&cb)); | 366 .WillOnce(SaveArg<1>(&cb)); |
364 OnPhishingDetectionDone(verdict.SerializeAsString()); | 367 OnPhishingDetectionDone(verdict.SerializeAsString()); |
365 EXPECT_TRUE(Mock::VerifyAndClear(csd_service_.get())); | 368 EXPECT_TRUE(Mock::VerifyAndClear(csd_service_.get())); |
366 ASSERT_TRUE(cb); | 369 ASSERT_FALSE(cb.is_null()); |
367 | 370 |
368 // Make sure DoDisplayBlockingPage is not going to be called. | 371 // Make sure DoDisplayBlockingPage is not going to be called. |
369 EXPECT_CALL(*sb_service_, DoDisplayBlockingPage(_)).Times(0); | 372 EXPECT_CALL(*sb_service_, DoDisplayBlockingPage(_)).Times(0); |
370 cb->Run(GURL(verdict.url()), false); | 373 cb.Run(GURL(verdict.url()), false); |
371 delete cb; | |
372 MessageLoop::current()->RunAllPending(); | 374 MessageLoop::current()->RunAllPending(); |
373 EXPECT_TRUE(Mock::VerifyAndClear(sb_service_.get())); | 375 EXPECT_TRUE(Mock::VerifyAndClear(sb_service_.get())); |
374 } | 376 } |
375 | 377 |
376 TEST_F(ClientSideDetectionHostTest, OnPhishingDetectionDoneShowInterstitial) { | 378 TEST_F(ClientSideDetectionHostTest, OnPhishingDetectionDoneShowInterstitial) { |
377 // Case 3: client thinks the page is phishing and so does the server. | 379 // Case 3: client thinks the page is phishing and so does the server. |
378 // We show an interstitial. | 380 // We show an interstitial. |
379 MockBrowserFeatureExtractor* mock_extractor = new MockBrowserFeatureExtractor( | 381 MockBrowserFeatureExtractor* mock_extractor = new MockBrowserFeatureExtractor( |
380 contents(), | 382 contents(), |
381 csd_service_.get()); | 383 csd_service_.get()); |
382 SetFeatureExtractor(mock_extractor); // The host class takes ownership. | 384 SetFeatureExtractor(mock_extractor); // The host class takes ownership. |
383 | 385 |
384 ClientSideDetectionService::ClientReportPhishingRequestCallback* cb; | 386 ClientSideDetectionService::ClientReportPhishingRequestCallback cb; |
385 GURL phishing_url("http://phishingurl.com/"); | 387 GURL phishing_url("http://phishingurl.com/"); |
386 ClientPhishingRequest verdict; | 388 ClientPhishingRequest verdict; |
387 verdict.set_url(phishing_url.spec()); | 389 verdict.set_url(phishing_url.spec()); |
388 verdict.set_client_score(1.0f); | 390 verdict.set_client_score(1.0f); |
389 verdict.set_is_phishing(true); | 391 verdict.set_is_phishing(true); |
390 | 392 |
391 EXPECT_CALL(*mock_extractor, ExtractFeatures(_, _, _)) | 393 EXPECT_CALL(*mock_extractor, ExtractFeatures(_, _, _)) |
392 .WillOnce(DoAll(DeleteArg<1>(), | 394 .WillOnce(DoAll(DeleteArg<1>(), |
393 InvokeCallbackArgument<2>(true, &verdict))); | 395 InvokeCallbackArgument<2>(true, &verdict))); |
394 EXPECT_CALL(*csd_service_, | 396 EXPECT_CALL(*csd_service_, |
395 SendClientReportPhishingRequest( | 397 SendClientReportPhishingRequest( |
396 Pointee(PartiallyEqualVerdict(verdict)), _)) | 398 Pointee(PartiallyEqualVerdict(verdict)), _)) |
397 .WillOnce(SaveArg<1>(&cb)); | 399 .WillOnce(SaveArg<1>(&cb)); |
398 OnPhishingDetectionDone(verdict.SerializeAsString()); | 400 OnPhishingDetectionDone(verdict.SerializeAsString()); |
399 EXPECT_TRUE(Mock::VerifyAndClear(csd_service_.get())); | 401 EXPECT_TRUE(Mock::VerifyAndClear(csd_service_.get())); |
400 ASSERT_TRUE(cb); | 402 ASSERT_FALSE(cb.is_null()); |
401 | 403 |
402 SafeBrowsingService::UnsafeResource resource; | 404 SafeBrowsingService::UnsafeResource resource; |
403 EXPECT_CALL(*sb_service_, DoDisplayBlockingPage(_)) | 405 EXPECT_CALL(*sb_service_, DoDisplayBlockingPage(_)) |
404 .WillOnce(SaveArg<0>(&resource)); | 406 .WillOnce(SaveArg<0>(&resource)); |
405 cb->Run(phishing_url, true); | 407 cb.Run(phishing_url, true); |
406 delete cb; | |
407 | 408 |
408 MessageLoop::current()->RunAllPending(); | 409 MessageLoop::current()->RunAllPending(); |
409 EXPECT_TRUE(Mock::VerifyAndClear(sb_service_.get())); | 410 EXPECT_TRUE(Mock::VerifyAndClear(sb_service_.get())); |
410 EXPECT_EQ(phishing_url, resource.url); | 411 EXPECT_EQ(phishing_url, resource.url); |
411 EXPECT_EQ(phishing_url, resource.original_url); | 412 EXPECT_EQ(phishing_url, resource.original_url); |
412 EXPECT_FALSE(resource.is_subresource); | 413 EXPECT_FALSE(resource.is_subresource); |
413 EXPECT_EQ(SafeBrowsingService::CLIENT_SIDE_PHISHING_URL, | 414 EXPECT_EQ(SafeBrowsingService::CLIENT_SIDE_PHISHING_URL, |
414 resource.threat_type); | 415 resource.threat_type); |
415 EXPECT_EQ(contents()->GetRenderProcessHost()->id(), | 416 EXPECT_EQ(contents()->GetRenderProcessHost()->id(), |
416 resource.render_process_host_id); | 417 resource.render_process_host_id); |
417 EXPECT_EQ(contents()->render_view_host()->routing_id(), | 418 EXPECT_EQ(contents()->render_view_host()->routing_id(), |
418 resource.render_view_id); | 419 resource.render_view_id); |
419 | 420 |
420 // Make sure the client object will be deleted. | 421 // Make sure the client object will be deleted. |
421 BrowserThread::PostTask( | 422 BrowserThread::PostTask( |
422 BrowserThread::IO, | 423 BrowserThread::IO, |
423 FROM_HERE, | 424 FROM_HERE, |
424 NewRunnableMethod( | 425 base::Bind(&MockSafeBrowsingService::InvokeOnBlockingPageComplete, |
425 sb_service_.get(), | 426 sb_service_.get(), resource.client)); |
426 &MockSafeBrowsingService::InvokeOnBlockingPageComplete, | |
427 resource.client)); | |
428 // Since the CsdClient object will be deleted on the UI thread I need | 427 // Since the CsdClient object will be deleted on the UI thread I need |
429 // to run the UI message loop. Post a task to stop the UI message loop | 428 // to run the UI message loop. Post a task to stop the UI message loop |
430 // after the client object destructor is called. | 429 // after the client object destructor is called. |
431 FlushIOMessageLoop(); | 430 FlushIOMessageLoop(); |
432 } | 431 } |
433 | 432 |
434 TEST_F(ClientSideDetectionHostTest, OnPhishingDetectionDoneMultiplePings) { | 433 TEST_F(ClientSideDetectionHostTest, OnPhishingDetectionDoneMultiplePings) { |
435 // Case 4 & 5: client thinks a page is phishing then navigates to | 434 // Case 4 & 5: client thinks a page is phishing then navigates to |
436 // another page which is also considered phishing by the client | 435 // another page which is also considered phishing by the client |
437 // before the server responds with a verdict. After a while the | 436 // before the server responds with a verdict. After a while the |
438 // server responds for both requests with a phishing verdict. Only | 437 // server responds for both requests with a phishing verdict. Only |
439 // a single interstitial is shown for the second URL. | 438 // a single interstitial is shown for the second URL. |
440 MockBrowserFeatureExtractor* mock_extractor = new MockBrowserFeatureExtractor( | 439 MockBrowserFeatureExtractor* mock_extractor = new MockBrowserFeatureExtractor( |
441 contents(), | 440 contents(), |
442 csd_service_.get()); | 441 csd_service_.get()); |
443 SetFeatureExtractor(mock_extractor); // The host class takes ownership. | 442 SetFeatureExtractor(mock_extractor); // The host class takes ownership. |
444 | 443 |
445 ClientSideDetectionService::ClientReportPhishingRequestCallback* cb; | 444 ClientSideDetectionService::ClientReportPhishingRequestCallback cb; |
446 GURL phishing_url("http://phishingurl.com/"); | 445 GURL phishing_url("http://phishingurl.com/"); |
447 ClientPhishingRequest verdict; | 446 ClientPhishingRequest verdict; |
448 verdict.set_url(phishing_url.spec()); | 447 verdict.set_url(phishing_url.spec()); |
449 verdict.set_client_score(1.0f); | 448 verdict.set_client_score(1.0f); |
450 verdict.set_is_phishing(true); | 449 verdict.set_is_phishing(true); |
451 | 450 |
452 EXPECT_CALL(*mock_extractor, ExtractFeatures(_, _, _)) | 451 EXPECT_CALL(*mock_extractor, ExtractFeatures(_, _, _)) |
453 .WillOnce(DoAll(DeleteArg<1>(), | 452 .WillOnce(DoAll(DeleteArg<1>(), |
454 InvokeCallbackArgument<2>(true, &verdict))); | 453 InvokeCallbackArgument<2>(true, &verdict))); |
455 EXPECT_CALL(*csd_service_, | 454 EXPECT_CALL(*csd_service_, |
456 SendClientReportPhishingRequest( | 455 SendClientReportPhishingRequest( |
457 Pointee(PartiallyEqualVerdict(verdict)), _)) | 456 Pointee(PartiallyEqualVerdict(verdict)), _)) |
458 .WillOnce(SaveArg<1>(&cb)); | 457 .WillOnce(SaveArg<1>(&cb)); |
459 OnPhishingDetectionDone(verdict.SerializeAsString()); | 458 OnPhishingDetectionDone(verdict.SerializeAsString()); |
460 EXPECT_TRUE(Mock::VerifyAndClear(csd_service_.get())); | 459 EXPECT_TRUE(Mock::VerifyAndClear(csd_service_.get())); |
461 ASSERT_TRUE(cb); | 460 ASSERT_FALSE(cb.is_null()); |
462 | 461 |
463 // Set this back to a normal browser feature extractor since we're using | 462 // Set this back to a normal browser feature extractor since we're using |
464 // NavigateAndCommit() and it's easier to use the real thing than setting up | 463 // NavigateAndCommit() and it's easier to use the real thing than setting up |
465 // mock expectations. | 464 // mock expectations. |
466 SetFeatureExtractor(new BrowserFeatureExtractor(contents(), | 465 SetFeatureExtractor(new BrowserFeatureExtractor(contents(), |
467 csd_service_.get())); | 466 csd_service_.get())); |
468 GURL other_phishing_url("http://other_phishing_url.com/bla"); | 467 GURL other_phishing_url("http://other_phishing_url.com/bla"); |
469 ExpectPreClassificationChecks(other_phishing_url, &kFalse, &kFalse, &kFalse, | 468 ExpectPreClassificationChecks(other_phishing_url, &kFalse, &kFalse, &kFalse, |
470 &kFalse, &kFalse, &kFalse); | 469 &kFalse, &kFalse, &kFalse); |
471 // We navigate away. The callback cb should be revoked. | 470 // We navigate away. The callback cb should be revoked. |
472 NavigateAndCommit(other_phishing_url); | 471 NavigateAndCommit(other_phishing_url); |
473 // Wait for the pre-classification checks to finish for other_phishing_url. | 472 // Wait for the pre-classification checks to finish for other_phishing_url. |
474 WaitAndCheckPreClassificationChecks(); | 473 WaitAndCheckPreClassificationChecks(); |
475 | 474 |
476 ClientSideDetectionService::ClientReportPhishingRequestCallback* cb_other; | 475 ClientSideDetectionService::ClientReportPhishingRequestCallback cb_other; |
477 verdict.set_url(other_phishing_url.spec()); | 476 verdict.set_url(other_phishing_url.spec()); |
478 verdict.set_client_score(0.8f); | 477 verdict.set_client_score(0.8f); |
479 EXPECT_CALL(*csd_service_, | 478 EXPECT_CALL(*csd_service_, |
480 SendClientReportPhishingRequest( | 479 SendClientReportPhishingRequest( |
481 Pointee(PartiallyEqualVerdict(verdict)), _)) | 480 Pointee(PartiallyEqualVerdict(verdict)), _)) |
482 .WillOnce(DoAll(DeleteArg<0>(), | 481 .WillOnce(DoAll(DeleteArg<0>(), |
483 SaveArg<1>(&cb_other), | 482 SaveArg<1>(&cb_other), |
484 QuitUIMessageLoop())); | 483 QuitUIMessageLoop())); |
485 std::vector<GURL> redirect_chain; | 484 std::vector<GURL> redirect_chain; |
486 redirect_chain.push_back(other_phishing_url); | 485 redirect_chain.push_back(other_phishing_url); |
487 SetRedirectChain(redirect_chain); | 486 SetRedirectChain(redirect_chain); |
488 OnPhishingDetectionDone(verdict.SerializeAsString()); | 487 OnPhishingDetectionDone(verdict.SerializeAsString()); |
489 MessageLoop::current()->Run(); | 488 MessageLoop::current()->Run(); |
490 EXPECT_TRUE(Mock::VerifyAndClear(csd_service_.get())); | 489 EXPECT_TRUE(Mock::VerifyAndClear(csd_service_.get())); |
491 ASSERT_TRUE(cb_other); | 490 ASSERT_FALSE(cb_other.is_null()); |
492 | 491 |
493 // We expect that the interstitial is shown for the second phishing URL and | 492 // We expect that the interstitial is shown for the second phishing URL and |
494 // not for the first phishing URL. | 493 // not for the first phishing URL. |
495 SafeBrowsingService::UnsafeResource resource; | 494 SafeBrowsingService::UnsafeResource resource; |
496 EXPECT_CALL(*sb_service_, DoDisplayBlockingPage(_)) | 495 EXPECT_CALL(*sb_service_, DoDisplayBlockingPage(_)) |
497 .WillOnce(SaveArg<0>(&resource)); | 496 .WillOnce(SaveArg<0>(&resource)); |
498 | 497 |
499 cb->Run(phishing_url, true); // Should have no effect. | 498 cb.Run(phishing_url, true); // Should have no effect. |
500 delete cb; | 499 cb_other.Run(other_phishing_url, true); // Should show interstitial. |
501 cb_other->Run(other_phishing_url, true); // Should show interstitial. | |
502 delete cb_other; | |
503 | 500 |
504 MessageLoop::current()->RunAllPending(); | 501 MessageLoop::current()->RunAllPending(); |
505 EXPECT_TRUE(Mock::VerifyAndClear(sb_service_.get())); | 502 EXPECT_TRUE(Mock::VerifyAndClear(sb_service_.get())); |
506 EXPECT_EQ(other_phishing_url, resource.url); | 503 EXPECT_EQ(other_phishing_url, resource.url); |
507 EXPECT_EQ(other_phishing_url, resource.original_url); | 504 EXPECT_EQ(other_phishing_url, resource.original_url); |
508 EXPECT_FALSE(resource.is_subresource); | 505 EXPECT_FALSE(resource.is_subresource); |
509 EXPECT_EQ(SafeBrowsingService::CLIENT_SIDE_PHISHING_URL, | 506 EXPECT_EQ(SafeBrowsingService::CLIENT_SIDE_PHISHING_URL, |
510 resource.threat_type); | 507 resource.threat_type); |
511 EXPECT_EQ(contents()->GetRenderProcessHost()->id(), | 508 EXPECT_EQ(contents()->GetRenderProcessHost()->id(), |
512 resource.render_process_host_id); | 509 resource.render_process_host_id); |
513 EXPECT_EQ(contents()->render_view_host()->routing_id(), | 510 EXPECT_EQ(contents()->render_view_host()->routing_id(), |
514 resource.render_view_id); | 511 resource.render_view_id); |
515 | 512 |
516 // Make sure the client object will be deleted. | 513 // Make sure the client object will be deleted. |
517 BrowserThread::PostTask( | 514 BrowserThread::PostTask( |
518 BrowserThread::IO, | 515 BrowserThread::IO, |
519 FROM_HERE, | 516 FROM_HERE, |
520 NewRunnableMethod( | 517 base::Bind(&MockSafeBrowsingService::InvokeOnBlockingPageComplete, |
521 sb_service_.get(), | 518 sb_service_.get(), resource.client)); |
522 &MockSafeBrowsingService::InvokeOnBlockingPageComplete, | |
523 resource.client)); | |
524 // Since the CsdClient object will be deleted on the UI thread I need | 519 // Since the CsdClient object will be deleted on the UI thread I need |
525 // to run the UI message loop. Post a task to stop the UI message loop | 520 // to run the UI message loop. Post a task to stop the UI message loop |
526 // after the client object destructor is called. | 521 // after the client object destructor is called. |
527 FlushIOMessageLoop(); | 522 FlushIOMessageLoop(); |
528 } | 523 } |
529 | 524 |
530 TEST_F(ClientSideDetectionHostTest, OnPhishingDetectionDoneVerdictNotPhishing) { | 525 TEST_F(ClientSideDetectionHostTest, OnPhishingDetectionDoneVerdictNotPhishing) { |
531 // Case 6: renderer sends a verdict string that isn't phishing. | 526 // Case 6: renderer sends a verdict string that isn't phishing. |
532 MockBrowserFeatureExtractor* mock_extractor = new MockBrowserFeatureExtractor( | 527 MockBrowserFeatureExtractor* mock_extractor = new MockBrowserFeatureExtractor( |
533 contents(), | 528 contents(), |
(...skipping 22 matching lines...) Expand all Loading... |
556 | 551 |
557 // First we have to navigate to the URL to set the unique page ID. | 552 // First we have to navigate to the URL to set the unique page ID. |
558 ExpectPreClassificationChecks(url, &kFalse, &kFalse, &kFalse, &kFalse, | 553 ExpectPreClassificationChecks(url, &kFalse, &kFalse, &kFalse, &kFalse, |
559 &kFalse, &kFalse); | 554 &kFalse, &kFalse); |
560 NavigateAndCommit(url); | 555 NavigateAndCommit(url); |
561 WaitAndCheckPreClassificationChecks(); | 556 WaitAndCheckPreClassificationChecks(); |
562 SetUnsafeResourceToCurrent(); | 557 SetUnsafeResourceToCurrent(); |
563 | 558 |
564 EXPECT_CALL(*csd_service_, | 559 EXPECT_CALL(*csd_service_, |
565 SendClientReportPhishingRequest( | 560 SendClientReportPhishingRequest( |
566 Pointee(PartiallyEqualVerdict(verdict)), IsNull())) | 561 Pointee(PartiallyEqualVerdict(verdict)), CallbackIsNull())) |
567 .WillOnce(DoAll(DeleteArg<0>(), QuitUIMessageLoop())); | 562 .WillOnce(DoAll(DeleteArg<0>(), QuitUIMessageLoop())); |
568 std::vector<GURL> redirect_chain; | 563 std::vector<GURL> redirect_chain; |
569 redirect_chain.push_back(url); | 564 redirect_chain.push_back(url); |
570 SetRedirectChain(redirect_chain); | 565 SetRedirectChain(redirect_chain); |
571 OnPhishingDetectionDone(verdict.SerializeAsString()); | 566 OnPhishingDetectionDone(verdict.SerializeAsString()); |
572 MessageLoop::current()->Run(); | 567 MessageLoop::current()->Run(); |
573 EXPECT_TRUE(Mock::VerifyAndClear(csd_service_.get())); | 568 EXPECT_TRUE(Mock::VerifyAndClear(csd_service_.get())); |
574 } | 569 } |
575 | 570 |
576 TEST_F(ClientSideDetectionHostTest, NavigationCancelsShouldClassifyUrl) { | 571 TEST_F(ClientSideDetectionHostTest, NavigationCancelsShouldClassifyUrl) { |
(...skipping 164 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
741 EXPECT_TRUE(Mock::VerifyAndClear(sb_service_.get())); | 736 EXPECT_TRUE(Mock::VerifyAndClear(sb_service_.get())); |
742 EXPECT_EQ(url, resource.url); | 737 EXPECT_EQ(url, resource.url); |
743 EXPECT_EQ(url, resource.original_url); | 738 EXPECT_EQ(url, resource.original_url); |
744 delete resource.client; | 739 delete resource.client; |
745 msg = process()->sink().GetFirstMessageMatching( | 740 msg = process()->sink().GetFirstMessageMatching( |
746 SafeBrowsingMsg_StartPhishingDetection::ID); | 741 SafeBrowsingMsg_StartPhishingDetection::ID); |
747 ASSERT_FALSE(msg); | 742 ASSERT_FALSE(msg); |
748 } | 743 } |
749 | 744 |
750 } // namespace safe_browsing | 745 } // namespace safe_browsing |
OLD | NEW |