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 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
309 } | 313 } |
310 | 314 |
311 TEST_F(ClientSideDetectionHostTest, OnPhishingDetectionDoneNotPhishing) { | 315 TEST_F(ClientSideDetectionHostTest, OnPhishingDetectionDoneNotPhishing) { |
312 // Case 1: client thinks the page is phishing. The server does not agree. | 316 // Case 1: client thinks the page is phishing. The server does not agree. |
313 // No interstitial is shown. | 317 // No interstitial is shown. |
314 MockBrowserFeatureExtractor* mock_extractor = new MockBrowserFeatureExtractor( | 318 MockBrowserFeatureExtractor* mock_extractor = new MockBrowserFeatureExtractor( |
315 contents(), | 319 contents(), |
316 csd_service_.get()); | 320 csd_service_.get()); |
317 SetFeatureExtractor(mock_extractor); // The host class takes ownership. | 321 SetFeatureExtractor(mock_extractor); // The host class takes ownership. |
318 | 322 |
319 ClientSideDetectionService::ClientReportPhishingRequestCallback* cb; | 323 ClientSideDetectionService::ClientReportPhishingRequestCallback cb; |
320 ClientPhishingRequest verdict; | 324 ClientPhishingRequest verdict; |
321 verdict.set_url("http://phishingurl.com/"); | 325 verdict.set_url("http://phishingurl.com/"); |
322 verdict.set_client_score(1.0f); | 326 verdict.set_client_score(1.0f); |
323 verdict.set_is_phishing(true); | 327 verdict.set_is_phishing(true); |
324 | 328 |
325 EXPECT_CALL(*mock_extractor, ExtractFeatures(_, _, _)) | 329 EXPECT_CALL(*mock_extractor, ExtractFeatures(_, _, _)) |
326 .WillOnce(DoAll(DeleteArg<1>(), | 330 .WillOnce(DoAll(DeleteArg<1>(), |
327 InvokeCallbackArgument<2>(true, &verdict))); | 331 InvokeCallbackArgument<2>(true, &verdict))); |
328 EXPECT_CALL(*csd_service_, | 332 EXPECT_CALL(*csd_service_, |
329 SendClientReportPhishingRequest( | 333 SendClientReportPhishingRequest( |
330 Pointee(PartiallyEqualVerdict(verdict)), _)) | 334 Pointee(PartiallyEqualVerdict(verdict)), _)) |
331 .WillOnce(SaveArg<1>(&cb)); | 335 .WillOnce(SaveArg<1>(&cb)); |
332 OnPhishingDetectionDone(verdict.SerializeAsString()); | 336 OnPhishingDetectionDone(verdict.SerializeAsString()); |
333 EXPECT_TRUE(Mock::VerifyAndClear(csd_service_.get())); | 337 EXPECT_TRUE(Mock::VerifyAndClear(csd_service_.get())); |
334 ASSERT_TRUE(cb); | 338 ASSERT_FALSE(cb.is_null()); |
335 | 339 |
336 // Make sure DoDisplayBlockingPage is not going to be called. | 340 // Make sure DoDisplayBlockingPage is not going to be called. |
337 EXPECT_CALL(*sb_service_, DoDisplayBlockingPage(_)).Times(0); | 341 EXPECT_CALL(*sb_service_, DoDisplayBlockingPage(_)).Times(0); |
338 cb->Run(GURL(verdict.url()), false); | 342 cb.Run(GURL(verdict.url()), false); |
339 delete cb; | |
340 MessageLoop::current()->RunAllPending(); | 343 MessageLoop::current()->RunAllPending(); |
341 EXPECT_TRUE(Mock::VerifyAndClear(sb_service_.get())); | 344 EXPECT_TRUE(Mock::VerifyAndClear(sb_service_.get())); |
342 } | 345 } |
343 | 346 |
344 TEST_F(ClientSideDetectionHostTest, OnPhishingDetectionDoneDisabled) { | 347 TEST_F(ClientSideDetectionHostTest, OnPhishingDetectionDoneDisabled) { |
345 // Case 2: client thinks the page is phishing and so does the server but | 348 // Case 2: client thinks the page is phishing and so does the server but |
346 // showing the interstitial is disabled => no interstitial is shown. | 349 // showing the interstitial is disabled => no interstitial is shown. |
347 MockBrowserFeatureExtractor* mock_extractor = new MockBrowserFeatureExtractor( | 350 MockBrowserFeatureExtractor* mock_extractor = new MockBrowserFeatureExtractor( |
348 contents(), | 351 contents(), |
349 csd_service_.get()); | 352 csd_service_.get()); |
350 SetFeatureExtractor(mock_extractor); // The host class takes ownership. | 353 SetFeatureExtractor(mock_extractor); // The host class takes ownership. |
351 | 354 |
352 ClientSideDetectionService::ClientReportPhishingRequestCallback* cb; | 355 ClientSideDetectionService::ClientReportPhishingRequestCallback cb; |
353 ClientPhishingRequest verdict; | 356 ClientPhishingRequest verdict; |
354 verdict.set_url("http://phishingurl.com/"); | 357 verdict.set_url("http://phishingurl.com/"); |
355 verdict.set_client_score(1.0f); | 358 verdict.set_client_score(1.0f); |
356 verdict.set_is_phishing(true); | 359 verdict.set_is_phishing(true); |
357 | 360 |
358 EXPECT_CALL(*mock_extractor, ExtractFeatures(_, _, _)) | 361 EXPECT_CALL(*mock_extractor, ExtractFeatures(_, _, _)) |
359 .WillOnce(DoAll(DeleteArg<1>(), | 362 .WillOnce(DoAll(DeleteArg<1>(), |
360 InvokeCallbackArgument<2>(true, &verdict))); | 363 InvokeCallbackArgument<2>(true, &verdict))); |
361 EXPECT_CALL(*csd_service_, | 364 EXPECT_CALL(*csd_service_, |
362 SendClientReportPhishingRequest( | 365 SendClientReportPhishingRequest( |
363 Pointee(PartiallyEqualVerdict(verdict)), _)) | 366 Pointee(PartiallyEqualVerdict(verdict)), _)) |
364 .WillOnce(SaveArg<1>(&cb)); | 367 .WillOnce(SaveArg<1>(&cb)); |
365 OnPhishingDetectionDone(verdict.SerializeAsString()); | 368 OnPhishingDetectionDone(verdict.SerializeAsString()); |
366 EXPECT_TRUE(Mock::VerifyAndClear(csd_service_.get())); | 369 EXPECT_TRUE(Mock::VerifyAndClear(csd_service_.get())); |
367 ASSERT_TRUE(cb); | 370 ASSERT_FALSE(cb.is_null()); |
368 | 371 |
369 // Make sure DoDisplayBlockingPage is not going to be called. | 372 // Make sure DoDisplayBlockingPage is not going to be called. |
370 EXPECT_CALL(*sb_service_, DoDisplayBlockingPage(_)).Times(0); | 373 EXPECT_CALL(*sb_service_, DoDisplayBlockingPage(_)).Times(0); |
371 cb->Run(GURL(verdict.url()), false); | 374 cb.Run(GURL(verdict.url()), false); |
372 delete cb; | |
373 MessageLoop::current()->RunAllPending(); | 375 MessageLoop::current()->RunAllPending(); |
374 EXPECT_TRUE(Mock::VerifyAndClear(sb_service_.get())); | 376 EXPECT_TRUE(Mock::VerifyAndClear(sb_service_.get())); |
375 } | 377 } |
376 | 378 |
377 TEST_F(ClientSideDetectionHostTest, OnPhishingDetectionDoneShowInterstitial) { | 379 TEST_F(ClientSideDetectionHostTest, OnPhishingDetectionDoneShowInterstitial) { |
378 // Case 3: client thinks the page is phishing and so does the server. | 380 // Case 3: client thinks the page is phishing and so does the server. |
379 // We show an interstitial. | 381 // We show an interstitial. |
380 MockBrowserFeatureExtractor* mock_extractor = new MockBrowserFeatureExtractor( | 382 MockBrowserFeatureExtractor* mock_extractor = new MockBrowserFeatureExtractor( |
381 contents(), | 383 contents(), |
382 csd_service_.get()); | 384 csd_service_.get()); |
383 SetFeatureExtractor(mock_extractor); // The host class takes ownership. | 385 SetFeatureExtractor(mock_extractor); // The host class takes ownership. |
384 | 386 |
385 ClientSideDetectionService::ClientReportPhishingRequestCallback* cb; | 387 ClientSideDetectionService::ClientReportPhishingRequestCallback cb; |
386 GURL phishing_url("http://phishingurl.com/"); | 388 GURL phishing_url("http://phishingurl.com/"); |
387 ClientPhishingRequest verdict; | 389 ClientPhishingRequest verdict; |
388 verdict.set_url(phishing_url.spec()); | 390 verdict.set_url(phishing_url.spec()); |
389 verdict.set_client_score(1.0f); | 391 verdict.set_client_score(1.0f); |
390 verdict.set_is_phishing(true); | 392 verdict.set_is_phishing(true); |
391 | 393 |
392 EXPECT_CALL(*mock_extractor, ExtractFeatures(_, _, _)) | 394 EXPECT_CALL(*mock_extractor, ExtractFeatures(_, _, _)) |
393 .WillOnce(DoAll(DeleteArg<1>(), | 395 .WillOnce(DoAll(DeleteArg<1>(), |
394 InvokeCallbackArgument<2>(true, &verdict))); | 396 InvokeCallbackArgument<2>(true, &verdict))); |
395 EXPECT_CALL(*csd_service_, | 397 EXPECT_CALL(*csd_service_, |
396 SendClientReportPhishingRequest( | 398 SendClientReportPhishingRequest( |
397 Pointee(PartiallyEqualVerdict(verdict)), _)) | 399 Pointee(PartiallyEqualVerdict(verdict)), _)) |
398 .WillOnce(SaveArg<1>(&cb)); | 400 .WillOnce(SaveArg<1>(&cb)); |
399 OnPhishingDetectionDone(verdict.SerializeAsString()); | 401 OnPhishingDetectionDone(verdict.SerializeAsString()); |
400 EXPECT_TRUE(Mock::VerifyAndClear(csd_service_.get())); | 402 EXPECT_TRUE(Mock::VerifyAndClear(csd_service_.get())); |
401 ASSERT_TRUE(cb); | 403 ASSERT_FALSE(cb.is_null()); |
402 | 404 |
403 SafeBrowsingService::UnsafeResource resource; | 405 SafeBrowsingService::UnsafeResource resource; |
404 EXPECT_CALL(*sb_service_, DoDisplayBlockingPage(_)) | 406 EXPECT_CALL(*sb_service_, DoDisplayBlockingPage(_)) |
405 .WillOnce(SaveArg<0>(&resource)); | 407 .WillOnce(SaveArg<0>(&resource)); |
406 cb->Run(phishing_url, true); | 408 cb.Run(phishing_url, true); |
407 delete cb; | |
408 | 409 |
409 MessageLoop::current()->RunAllPending(); | 410 MessageLoop::current()->RunAllPending(); |
410 EXPECT_TRUE(Mock::VerifyAndClear(sb_service_.get())); | 411 EXPECT_TRUE(Mock::VerifyAndClear(sb_service_.get())); |
411 EXPECT_EQ(phishing_url, resource.url); | 412 EXPECT_EQ(phishing_url, resource.url); |
412 EXPECT_EQ(phishing_url, resource.original_url); | 413 EXPECT_EQ(phishing_url, resource.original_url); |
413 EXPECT_FALSE(resource.is_subresource); | 414 EXPECT_FALSE(resource.is_subresource); |
414 EXPECT_EQ(SafeBrowsingService::CLIENT_SIDE_PHISHING_URL, | 415 EXPECT_EQ(SafeBrowsingService::CLIENT_SIDE_PHISHING_URL, |
415 resource.threat_type); | 416 resource.threat_type); |
416 EXPECT_EQ(contents()->GetRenderProcessHost()->GetID(), | 417 EXPECT_EQ(contents()->GetRenderProcessHost()->GetID(), |
417 resource.render_process_host_id); | 418 resource.render_process_host_id); |
418 EXPECT_EQ(contents()->render_view_host()->routing_id(), | 419 EXPECT_EQ(contents()->render_view_host()->routing_id(), |
419 resource.render_view_id); | 420 resource.render_view_id); |
420 | 421 |
421 // Make sure the client object will be deleted. | 422 // Make sure the client object will be deleted. |
422 BrowserThread::PostTask( | 423 BrowserThread::PostTask( |
423 BrowserThread::IO, | 424 BrowserThread::IO, |
424 FROM_HERE, | 425 FROM_HERE, |
425 NewRunnableMethod( | 426 base::Bind(&MockSafeBrowsingService::InvokeOnBlockingPageComplete, |
426 sb_service_.get(), | 427 sb_service_.get(), resource.client)); |
427 &MockSafeBrowsingService::InvokeOnBlockingPageComplete, | |
428 resource.client)); | |
429 // Since the CsdClient object will be deleted on the UI thread I need | 428 // Since the CsdClient object will be deleted on the UI thread I need |
430 // to run the UI message loop. Post a task to stop the UI message loop | 429 // to run the UI message loop. Post a task to stop the UI message loop |
431 // after the client object destructor is called. | 430 // after the client object destructor is called. |
432 FlushIOMessageLoop(); | 431 FlushIOMessageLoop(); |
433 } | 432 } |
434 | 433 |
435 TEST_F(ClientSideDetectionHostTest, OnPhishingDetectionDoneMultiplePings) { | 434 TEST_F(ClientSideDetectionHostTest, OnPhishingDetectionDoneMultiplePings) { |
436 // Case 4 & 5: client thinks a page is phishing then navigates to | 435 // Case 4 & 5: client thinks a page is phishing then navigates to |
437 // another page which is also considered phishing by the client | 436 // another page which is also considered phishing by the client |
438 // before the server responds with a verdict. After a while the | 437 // before the server responds with a verdict. After a while the |
439 // server responds for both requests with a phishing verdict. Only | 438 // server responds for both requests with a phishing verdict. Only |
440 // a single interstitial is shown for the second URL. | 439 // a single interstitial is shown for the second URL. |
441 MockBrowserFeatureExtractor* mock_extractor = new MockBrowserFeatureExtractor( | 440 MockBrowserFeatureExtractor* mock_extractor = new MockBrowserFeatureExtractor( |
442 contents(), | 441 contents(), |
443 csd_service_.get()); | 442 csd_service_.get()); |
444 SetFeatureExtractor(mock_extractor); // The host class takes ownership. | 443 SetFeatureExtractor(mock_extractor); // The host class takes ownership. |
445 | 444 |
446 ClientSideDetectionService::ClientReportPhishingRequestCallback* cb; | 445 ClientSideDetectionService::ClientReportPhishingRequestCallback cb; |
447 GURL phishing_url("http://phishingurl.com/"); | 446 GURL phishing_url("http://phishingurl.com/"); |
448 ClientPhishingRequest verdict; | 447 ClientPhishingRequest verdict; |
449 verdict.set_url(phishing_url.spec()); | 448 verdict.set_url(phishing_url.spec()); |
450 verdict.set_client_score(1.0f); | 449 verdict.set_client_score(1.0f); |
451 verdict.set_is_phishing(true); | 450 verdict.set_is_phishing(true); |
452 | 451 |
453 EXPECT_CALL(*mock_extractor, ExtractFeatures(_, _, _)) | 452 EXPECT_CALL(*mock_extractor, ExtractFeatures(_, _, _)) |
454 .WillOnce(DoAll(DeleteArg<1>(), | 453 .WillOnce(DoAll(DeleteArg<1>(), |
455 InvokeCallbackArgument<2>(true, &verdict))); | 454 InvokeCallbackArgument<2>(true, &verdict))); |
456 EXPECT_CALL(*csd_service_, | 455 EXPECT_CALL(*csd_service_, |
457 SendClientReportPhishingRequest( | 456 SendClientReportPhishingRequest( |
458 Pointee(PartiallyEqualVerdict(verdict)), _)) | 457 Pointee(PartiallyEqualVerdict(verdict)), _)) |
459 .WillOnce(SaveArg<1>(&cb)); | 458 .WillOnce(SaveArg<1>(&cb)); |
460 OnPhishingDetectionDone(verdict.SerializeAsString()); | 459 OnPhishingDetectionDone(verdict.SerializeAsString()); |
461 EXPECT_TRUE(Mock::VerifyAndClear(csd_service_.get())); | 460 EXPECT_TRUE(Mock::VerifyAndClear(csd_service_.get())); |
462 ASSERT_TRUE(cb); | 461 ASSERT_FALSE(cb.is_null()); |
463 | 462 |
464 // Set this back to a normal browser feature extractor since we're using | 463 // Set this back to a normal browser feature extractor since we're using |
465 // NavigateAndCommit() and it's easier to use the real thing than setting up | 464 // NavigateAndCommit() and it's easier to use the real thing than setting up |
466 // mock expectations. | 465 // mock expectations. |
467 SetFeatureExtractor(new BrowserFeatureExtractor(contents(), | 466 SetFeatureExtractor(new BrowserFeatureExtractor(contents(), |
468 csd_service_.get())); | 467 csd_service_.get())); |
469 GURL other_phishing_url("http://other_phishing_url.com/bla"); | 468 GURL other_phishing_url("http://other_phishing_url.com/bla"); |
470 ExpectPreClassificationChecks(other_phishing_url, &kFalse, &kFalse, &kFalse, | 469 ExpectPreClassificationChecks(other_phishing_url, &kFalse, &kFalse, &kFalse, |
471 &kFalse, &kFalse, &kFalse); | 470 &kFalse, &kFalse, &kFalse); |
472 // We navigate away. The callback cb should be revoked. | 471 // We navigate away. The callback cb should be revoked. |
473 NavigateAndCommit(other_phishing_url); | 472 NavigateAndCommit(other_phishing_url); |
474 // Wait for the pre-classification checks to finish for other_phishing_url. | 473 // Wait for the pre-classification checks to finish for other_phishing_url. |
475 WaitAndCheckPreClassificationChecks(); | 474 WaitAndCheckPreClassificationChecks(); |
476 | 475 |
477 ClientSideDetectionService::ClientReportPhishingRequestCallback* cb_other; | 476 ClientSideDetectionService::ClientReportPhishingRequestCallback cb_other; |
478 verdict.set_url(other_phishing_url.spec()); | 477 verdict.set_url(other_phishing_url.spec()); |
479 verdict.set_client_score(0.8f); | 478 verdict.set_client_score(0.8f); |
480 EXPECT_CALL(*csd_service_, | 479 EXPECT_CALL(*csd_service_, |
481 SendClientReportPhishingRequest( | 480 SendClientReportPhishingRequest( |
482 Pointee(PartiallyEqualVerdict(verdict)), _)) | 481 Pointee(PartiallyEqualVerdict(verdict)), _)) |
483 .WillOnce(DoAll(DeleteArg<0>(), | 482 .WillOnce(DoAll(DeleteArg<0>(), |
484 SaveArg<1>(&cb_other), | 483 SaveArg<1>(&cb_other), |
485 QuitUIMessageLoop())); | 484 QuitUIMessageLoop())); |
486 std::vector<GURL> redirect_chain; | 485 std::vector<GURL> redirect_chain; |
487 redirect_chain.push_back(other_phishing_url); | 486 redirect_chain.push_back(other_phishing_url); |
488 SetRedirectChain(redirect_chain); | 487 SetRedirectChain(redirect_chain); |
489 OnPhishingDetectionDone(verdict.SerializeAsString()); | 488 OnPhishingDetectionDone(verdict.SerializeAsString()); |
490 MessageLoop::current()->Run(); | 489 MessageLoop::current()->Run(); |
491 EXPECT_TRUE(Mock::VerifyAndClear(csd_service_.get())); | 490 EXPECT_TRUE(Mock::VerifyAndClear(csd_service_.get())); |
492 ASSERT_TRUE(cb_other); | 491 ASSERT_FALSE(cb_other.is_null()); |
493 | 492 |
494 // We expect that the interstitial is shown for the second phishing URL and | 493 // We expect that the interstitial is shown for the second phishing URL and |
495 // not for the first phishing URL. | 494 // not for the first phishing URL. |
496 SafeBrowsingService::UnsafeResource resource; | 495 SafeBrowsingService::UnsafeResource resource; |
497 EXPECT_CALL(*sb_service_, DoDisplayBlockingPage(_)) | 496 EXPECT_CALL(*sb_service_, DoDisplayBlockingPage(_)) |
498 .WillOnce(SaveArg<0>(&resource)); | 497 .WillOnce(SaveArg<0>(&resource)); |
499 | 498 |
500 cb->Run(phishing_url, true); // Should have no effect. | 499 cb.Run(phishing_url, true); // Should have no effect. |
501 delete cb; | 500 cb_other.Run(other_phishing_url, true); // Should show interstitial. |
502 cb_other->Run(other_phishing_url, true); // Should show interstitial. | |
503 delete cb_other; | |
504 | 501 |
505 MessageLoop::current()->RunAllPending(); | 502 MessageLoop::current()->RunAllPending(); |
506 EXPECT_TRUE(Mock::VerifyAndClear(sb_service_.get())); | 503 EXPECT_TRUE(Mock::VerifyAndClear(sb_service_.get())); |
507 EXPECT_EQ(other_phishing_url, resource.url); | 504 EXPECT_EQ(other_phishing_url, resource.url); |
508 EXPECT_EQ(other_phishing_url, resource.original_url); | 505 EXPECT_EQ(other_phishing_url, resource.original_url); |
509 EXPECT_FALSE(resource.is_subresource); | 506 EXPECT_FALSE(resource.is_subresource); |
510 EXPECT_EQ(SafeBrowsingService::CLIENT_SIDE_PHISHING_URL, | 507 EXPECT_EQ(SafeBrowsingService::CLIENT_SIDE_PHISHING_URL, |
511 resource.threat_type); | 508 resource.threat_type); |
512 EXPECT_EQ(contents()->GetRenderProcessHost()->GetID(), | 509 EXPECT_EQ(contents()->GetRenderProcessHost()->GetID(), |
513 resource.render_process_host_id); | 510 resource.render_process_host_id); |
514 EXPECT_EQ(contents()->render_view_host()->routing_id(), | 511 EXPECT_EQ(contents()->render_view_host()->routing_id(), |
515 resource.render_view_id); | 512 resource.render_view_id); |
516 | 513 |
517 // Make sure the client object will be deleted. | 514 // Make sure the client object will be deleted. |
518 BrowserThread::PostTask( | 515 BrowserThread::PostTask( |
519 BrowserThread::IO, | 516 BrowserThread::IO, |
520 FROM_HERE, | 517 FROM_HERE, |
521 NewRunnableMethod( | 518 base::Bind(&MockSafeBrowsingService::InvokeOnBlockingPageComplete, |
522 sb_service_.get(), | 519 sb_service_.get(), resource.client)); |
523 &MockSafeBrowsingService::InvokeOnBlockingPageComplete, | |
524 resource.client)); | |
525 // Since the CsdClient object will be deleted on the UI thread I need | 520 // Since the CsdClient object will be deleted on the UI thread I need |
526 // to run the UI message loop. Post a task to stop the UI message loop | 521 // to run the UI message loop. Post a task to stop the UI message loop |
527 // after the client object destructor is called. | 522 // after the client object destructor is called. |
528 FlushIOMessageLoop(); | 523 FlushIOMessageLoop(); |
529 } | 524 } |
530 | 525 |
531 TEST_F(ClientSideDetectionHostTest, OnPhishingDetectionDoneVerdictNotPhishing) { | 526 TEST_F(ClientSideDetectionHostTest, OnPhishingDetectionDoneVerdictNotPhishing) { |
532 // Case 6: renderer sends a verdict string that isn't phishing. | 527 // Case 6: renderer sends a verdict string that isn't phishing. |
533 MockBrowserFeatureExtractor* mock_extractor = new MockBrowserFeatureExtractor( | 528 MockBrowserFeatureExtractor* mock_extractor = new MockBrowserFeatureExtractor( |
534 contents(), | 529 contents(), |
(...skipping 22 matching lines...) Expand all Loading... |
557 | 552 |
558 // First we have to navigate to the URL to set the unique page ID. | 553 // First we have to navigate to the URL to set the unique page ID. |
559 ExpectPreClassificationChecks(url, &kFalse, &kFalse, &kFalse, &kFalse, | 554 ExpectPreClassificationChecks(url, &kFalse, &kFalse, &kFalse, &kFalse, |
560 &kFalse, &kFalse); | 555 &kFalse, &kFalse); |
561 NavigateAndCommit(url); | 556 NavigateAndCommit(url); |
562 WaitAndCheckPreClassificationChecks(); | 557 WaitAndCheckPreClassificationChecks(); |
563 SetUnsafeResourceToCurrent(); | 558 SetUnsafeResourceToCurrent(); |
564 | 559 |
565 EXPECT_CALL(*csd_service_, | 560 EXPECT_CALL(*csd_service_, |
566 SendClientReportPhishingRequest( | 561 SendClientReportPhishingRequest( |
567 Pointee(PartiallyEqualVerdict(verdict)), IsNull())) | 562 Pointee(PartiallyEqualVerdict(verdict)), CallbackIsNull())) |
568 .WillOnce(DoAll(DeleteArg<0>(), QuitUIMessageLoop())); | 563 .WillOnce(DoAll(DeleteArg<0>(), QuitUIMessageLoop())); |
569 std::vector<GURL> redirect_chain; | 564 std::vector<GURL> redirect_chain; |
570 redirect_chain.push_back(url); | 565 redirect_chain.push_back(url); |
571 SetRedirectChain(redirect_chain); | 566 SetRedirectChain(redirect_chain); |
572 OnPhishingDetectionDone(verdict.SerializeAsString()); | 567 OnPhishingDetectionDone(verdict.SerializeAsString()); |
573 MessageLoop::current()->Run(); | 568 MessageLoop::current()->Run(); |
574 EXPECT_TRUE(Mock::VerifyAndClear(csd_service_.get())); | 569 EXPECT_TRUE(Mock::VerifyAndClear(csd_service_.get())); |
575 } | 570 } |
576 | 571 |
577 TEST_F(ClientSideDetectionHostTest, NavigationCancelsShouldClassifyUrl) { | 572 TEST_F(ClientSideDetectionHostTest, NavigationCancelsShouldClassifyUrl) { |
(...skipping 164 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
742 EXPECT_TRUE(Mock::VerifyAndClear(sb_service_.get())); | 737 EXPECT_TRUE(Mock::VerifyAndClear(sb_service_.get())); |
743 EXPECT_EQ(url, resource.url); | 738 EXPECT_EQ(url, resource.url); |
744 EXPECT_EQ(url, resource.original_url); | 739 EXPECT_EQ(url, resource.original_url); |
745 delete resource.client; | 740 delete resource.client; |
746 msg = process()->sink().GetFirstMessageMatching( | 741 msg = process()->sink().GetFirstMessageMatching( |
747 SafeBrowsingMsg_StartPhishingDetection::ID); | 742 SafeBrowsingMsg_StartPhishingDetection::ID); |
748 ASSERT_FALSE(msg); | 743 ASSERT_FALSE(msg); |
749 } | 744 } |
750 | 745 |
751 } // namespace safe_browsing | 746 } // namespace safe_browsing |
OLD | NEW |