OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/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/synchronization/waitable_event.h" | 8 #include "base/synchronization/waitable_event.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 169 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
180 csd_host_.reset(safe_browsing::ClientSideDetectionHost::Create( | 180 csd_host_.reset(safe_browsing::ClientSideDetectionHost::Create( |
181 web_contents())); | 181 web_contents())); |
182 csd_host_->set_client_side_detection_service(csd_service_.get()); | 182 csd_host_->set_client_side_detection_service(csd_service_.get()); |
183 csd_host_->set_safe_browsing_service(sb_service_.get()); | 183 csd_host_->set_safe_browsing_service(sb_service_.get()); |
184 // We need to create this here since we don't call | 184 // We need to create this here since we don't call |
185 // DidNavigateMainFramePostCommit in this test. | 185 // DidNavigateMainFramePostCommit in this test. |
186 csd_host_->browse_info_.reset(new BrowseInfo); | 186 csd_host_->browse_info_.reset(new BrowseInfo); |
187 } | 187 } |
188 | 188 |
189 static void RunAllPendingOnIO(base::WaitableEvent* event) { | 189 static void RunAllPendingOnIO(base::WaitableEvent* event) { |
190 MessageLoop::current()->RunAllPending(); | 190 MessageLoop::current()->RunUntilIdle(); |
191 event->Signal(); | 191 event->Signal(); |
192 } | 192 } |
193 | 193 |
194 virtual void TearDown() { | 194 virtual void TearDown() { |
195 // Delete the host object on the UI thread and release the | 195 // Delete the host object on the UI thread and release the |
196 // SafeBrowsingService. | 196 // SafeBrowsingService. |
197 BrowserThread::DeleteSoon(BrowserThread::UI, FROM_HERE, | 197 BrowserThread::DeleteSoon(BrowserThread::UI, FROM_HERE, |
198 csd_host_.release()); | 198 csd_host_.release()); |
199 sb_service_ = NULL; | 199 sb_service_ = NULL; |
200 message_loop_.RunAllPending(); | 200 message_loop_.RunUntilIdle(); |
201 ChromeRenderViewHostTestHarness::TearDown(); | 201 ChromeRenderViewHostTestHarness::TearDown(); |
202 | 202 |
203 // Let the tasks on the IO thread run to avoid memory leaks. | 203 // Let the tasks on the IO thread run to avoid memory leaks. |
204 base::WaitableEvent done(false, false); | 204 base::WaitableEvent done(false, false); |
205 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, | 205 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, |
206 base::Bind(RunAllPendingOnIO, &done)); | 206 base::Bind(RunAllPendingOnIO, &done)); |
207 done.Wait(); | 207 done.Wait(); |
208 io_thread_.reset(); | 208 io_thread_.reset(); |
209 message_loop_.RunAllPending(); | 209 message_loop_.RunUntilIdle(); |
210 file_user_blocking_thread_.reset(); | 210 file_user_blocking_thread_.reset(); |
211 ui_thread_.reset(); | 211 ui_thread_.reset(); |
212 } | 212 } |
213 | 213 |
214 void OnPhishingDetectionDone(const std::string& verdict_str) { | 214 void OnPhishingDetectionDone(const std::string& verdict_str) { |
215 csd_host_->OnPhishingDetectionDone(verdict_str); | 215 csd_host_->OnPhishingDetectionDone(verdict_str); |
216 } | 216 } |
217 | 217 |
218 void FlushIOMessageLoop() { | 218 void FlushIOMessageLoop() { |
219 // If there was a message posted on the IO thread to display the | 219 // If there was a message posted on the IO thread to display the |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
255 if (over_report_limit) { | 255 if (over_report_limit) { |
256 EXPECT_CALL(*csd_service_, OverReportLimit()) | 256 EXPECT_CALL(*csd_service_, OverReportLimit()) |
257 .WillOnce(Return(*over_report_limit)); | 257 .WillOnce(Return(*over_report_limit)); |
258 } | 258 } |
259 } | 259 } |
260 | 260 |
261 void WaitAndCheckPreClassificationChecks() { | 261 void WaitAndCheckPreClassificationChecks() { |
262 // Wait for CheckCsdWhitelist to be called if at all. | 262 // Wait for CheckCsdWhitelist to be called if at all. |
263 FlushIOMessageLoop(); | 263 FlushIOMessageLoop(); |
264 // Checks for CheckCache() to be called if at all. | 264 // Checks for CheckCache() to be called if at all. |
265 MessageLoop::current()->RunAllPending(); | 265 MessageLoop::current()->RunUntilIdle(); |
266 EXPECT_TRUE(Mock::VerifyAndClear(csd_service_.get())); | 266 EXPECT_TRUE(Mock::VerifyAndClear(csd_service_.get())); |
267 EXPECT_TRUE(Mock::VerifyAndClear(sb_service_.get())); | 267 EXPECT_TRUE(Mock::VerifyAndClear(sb_service_.get())); |
268 EXPECT_TRUE(Mock::VerifyAndClear(mock_profile_)); | 268 EXPECT_TRUE(Mock::VerifyAndClear(mock_profile_)); |
269 } | 269 } |
270 | 270 |
271 void SetFeatureExtractor(BrowserFeatureExtractor* extractor) { | 271 void SetFeatureExtractor(BrowserFeatureExtractor* extractor) { |
272 csd_host_->feature_extractor_.reset(extractor); | 272 csd_host_->feature_extractor_.reset(extractor); |
273 } | 273 } |
274 | 274 |
275 void SetRedirectChain(const std::vector<GURL>& redirect_chain) { | 275 void SetRedirectChain(const std::vector<GURL>& redirect_chain) { |
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
349 SendClientReportPhishingRequest( | 349 SendClientReportPhishingRequest( |
350 Pointee(PartiallyEqualVerdict(verdict)), _)) | 350 Pointee(PartiallyEqualVerdict(verdict)), _)) |
351 .WillOnce(SaveArg<1>(&cb)); | 351 .WillOnce(SaveArg<1>(&cb)); |
352 OnPhishingDetectionDone(verdict.SerializeAsString()); | 352 OnPhishingDetectionDone(verdict.SerializeAsString()); |
353 EXPECT_TRUE(Mock::VerifyAndClear(csd_service_.get())); | 353 EXPECT_TRUE(Mock::VerifyAndClear(csd_service_.get())); |
354 ASSERT_FALSE(cb.is_null()); | 354 ASSERT_FALSE(cb.is_null()); |
355 | 355 |
356 // Make sure DoDisplayBlockingPage is not going to be called. | 356 // Make sure DoDisplayBlockingPage is not going to be called. |
357 EXPECT_CALL(*sb_service_, DoDisplayBlockingPage(_)).Times(0); | 357 EXPECT_CALL(*sb_service_, DoDisplayBlockingPage(_)).Times(0); |
358 cb.Run(GURL(verdict.url()), false); | 358 cb.Run(GURL(verdict.url()), false); |
359 MessageLoop::current()->RunAllPending(); | 359 MessageLoop::current()->RunUntilIdle(); |
360 EXPECT_TRUE(Mock::VerifyAndClear(sb_service_.get())); | 360 EXPECT_TRUE(Mock::VerifyAndClear(sb_service_.get())); |
361 } | 361 } |
362 | 362 |
363 TEST_F(ClientSideDetectionHostTest, OnPhishingDetectionDoneDisabled) { | 363 TEST_F(ClientSideDetectionHostTest, OnPhishingDetectionDoneDisabled) { |
364 // Case 2: client thinks the page is phishing and so does the server but | 364 // Case 2: client thinks the page is phishing and so does the server but |
365 // showing the interstitial is disabled => no interstitial is shown. | 365 // showing the interstitial is disabled => no interstitial is shown. |
366 MockBrowserFeatureExtractor* mock_extractor = new MockBrowserFeatureExtractor( | 366 MockBrowserFeatureExtractor* mock_extractor = new MockBrowserFeatureExtractor( |
367 web_contents(), | 367 web_contents(), |
368 csd_service_.get()); | 368 csd_service_.get()); |
369 SetFeatureExtractor(mock_extractor); // The host class takes ownership. | 369 SetFeatureExtractor(mock_extractor); // The host class takes ownership. |
(...skipping 11 matching lines...) Expand all Loading... |
381 SendClientReportPhishingRequest( | 381 SendClientReportPhishingRequest( |
382 Pointee(PartiallyEqualVerdict(verdict)), _)) | 382 Pointee(PartiallyEqualVerdict(verdict)), _)) |
383 .WillOnce(SaveArg<1>(&cb)); | 383 .WillOnce(SaveArg<1>(&cb)); |
384 OnPhishingDetectionDone(verdict.SerializeAsString()); | 384 OnPhishingDetectionDone(verdict.SerializeAsString()); |
385 EXPECT_TRUE(Mock::VerifyAndClear(csd_service_.get())); | 385 EXPECT_TRUE(Mock::VerifyAndClear(csd_service_.get())); |
386 ASSERT_FALSE(cb.is_null()); | 386 ASSERT_FALSE(cb.is_null()); |
387 | 387 |
388 // Make sure DoDisplayBlockingPage is not going to be called. | 388 // Make sure DoDisplayBlockingPage is not going to be called. |
389 EXPECT_CALL(*sb_service_, DoDisplayBlockingPage(_)).Times(0); | 389 EXPECT_CALL(*sb_service_, DoDisplayBlockingPage(_)).Times(0); |
390 cb.Run(GURL(verdict.url()), false); | 390 cb.Run(GURL(verdict.url()), false); |
391 MessageLoop::current()->RunAllPending(); | 391 MessageLoop::current()->RunUntilIdle(); |
392 EXPECT_TRUE(Mock::VerifyAndClear(sb_service_.get())); | 392 EXPECT_TRUE(Mock::VerifyAndClear(sb_service_.get())); |
393 } | 393 } |
394 | 394 |
395 TEST_F(ClientSideDetectionHostTest, OnPhishingDetectionDoneShowInterstitial) { | 395 TEST_F(ClientSideDetectionHostTest, OnPhishingDetectionDoneShowInterstitial) { |
396 // Case 3: client thinks the page is phishing and so does the server. | 396 // Case 3: client thinks the page is phishing and so does the server. |
397 // We show an interstitial. | 397 // We show an interstitial. |
398 MockBrowserFeatureExtractor* mock_extractor = new MockBrowserFeatureExtractor( | 398 MockBrowserFeatureExtractor* mock_extractor = new MockBrowserFeatureExtractor( |
399 web_contents(), | 399 web_contents(), |
400 csd_service_.get()); | 400 csd_service_.get()); |
401 SetFeatureExtractor(mock_extractor); // The host class takes ownership. | 401 SetFeatureExtractor(mock_extractor); // The host class takes ownership. |
(...skipping 14 matching lines...) Expand all Loading... |
416 .WillOnce(SaveArg<1>(&cb)); | 416 .WillOnce(SaveArg<1>(&cb)); |
417 OnPhishingDetectionDone(verdict.SerializeAsString()); | 417 OnPhishingDetectionDone(verdict.SerializeAsString()); |
418 EXPECT_TRUE(Mock::VerifyAndClear(csd_service_.get())); | 418 EXPECT_TRUE(Mock::VerifyAndClear(csd_service_.get())); |
419 ASSERT_FALSE(cb.is_null()); | 419 ASSERT_FALSE(cb.is_null()); |
420 | 420 |
421 SafeBrowsingService::UnsafeResource resource; | 421 SafeBrowsingService::UnsafeResource resource; |
422 EXPECT_CALL(*sb_service_, DoDisplayBlockingPage(_)) | 422 EXPECT_CALL(*sb_service_, DoDisplayBlockingPage(_)) |
423 .WillOnce(SaveArg<0>(&resource)); | 423 .WillOnce(SaveArg<0>(&resource)); |
424 cb.Run(phishing_url, true); | 424 cb.Run(phishing_url, true); |
425 | 425 |
426 MessageLoop::current()->RunAllPending(); | 426 MessageLoop::current()->RunUntilIdle(); |
427 EXPECT_TRUE(Mock::VerifyAndClear(sb_service_.get())); | 427 EXPECT_TRUE(Mock::VerifyAndClear(sb_service_.get())); |
428 EXPECT_EQ(phishing_url, resource.url); | 428 EXPECT_EQ(phishing_url, resource.url); |
429 EXPECT_EQ(phishing_url, resource.original_url); | 429 EXPECT_EQ(phishing_url, resource.original_url); |
430 EXPECT_FALSE(resource.is_subresource); | 430 EXPECT_FALSE(resource.is_subresource); |
431 EXPECT_EQ(SB_THREAT_TYPE_CLIENT_SIDE_PHISHING_URL, resource.threat_type); | 431 EXPECT_EQ(SB_THREAT_TYPE_CLIENT_SIDE_PHISHING_URL, resource.threat_type); |
432 EXPECT_EQ(web_contents()->GetRenderProcessHost()->GetID(), | 432 EXPECT_EQ(web_contents()->GetRenderProcessHost()->GetID(), |
433 resource.render_process_host_id); | 433 resource.render_process_host_id); |
434 EXPECT_EQ(web_contents()->GetRenderViewHost()->GetRoutingID(), | 434 EXPECT_EQ(web_contents()->GetRenderViewHost()->GetRoutingID(), |
435 resource.render_view_id); | 435 resource.render_view_id); |
436 | 436 |
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
507 | 507 |
508 // We expect that the interstitial is shown for the second phishing URL and | 508 // We expect that the interstitial is shown for the second phishing URL and |
509 // not for the first phishing URL. | 509 // not for the first phishing URL. |
510 SafeBrowsingService::UnsafeResource resource; | 510 SafeBrowsingService::UnsafeResource resource; |
511 EXPECT_CALL(*sb_service_, DoDisplayBlockingPage(_)) | 511 EXPECT_CALL(*sb_service_, DoDisplayBlockingPage(_)) |
512 .WillOnce(SaveArg<0>(&resource)); | 512 .WillOnce(SaveArg<0>(&resource)); |
513 | 513 |
514 cb.Run(phishing_url, true); // Should have no effect. | 514 cb.Run(phishing_url, true); // Should have no effect. |
515 cb_other.Run(other_phishing_url, true); // Should show interstitial. | 515 cb_other.Run(other_phishing_url, true); // Should show interstitial. |
516 | 516 |
517 MessageLoop::current()->RunAllPending(); | 517 MessageLoop::current()->RunUntilIdle(); |
518 EXPECT_TRUE(Mock::VerifyAndClear(sb_service_.get())); | 518 EXPECT_TRUE(Mock::VerifyAndClear(sb_service_.get())); |
519 EXPECT_EQ(other_phishing_url, resource.url); | 519 EXPECT_EQ(other_phishing_url, resource.url); |
520 EXPECT_EQ(other_phishing_url, resource.original_url); | 520 EXPECT_EQ(other_phishing_url, resource.original_url); |
521 EXPECT_FALSE(resource.is_subresource); | 521 EXPECT_FALSE(resource.is_subresource); |
522 EXPECT_EQ(SB_THREAT_TYPE_CLIENT_SIDE_PHISHING_URL, resource.threat_type); | 522 EXPECT_EQ(SB_THREAT_TYPE_CLIENT_SIDE_PHISHING_URL, resource.threat_type); |
523 EXPECT_EQ(web_contents()->GetRenderProcessHost()->GetID(), | 523 EXPECT_EQ(web_contents()->GetRenderProcessHost()->GetID(), |
524 resource.render_process_host_id); | 524 resource.render_process_host_id); |
525 EXPECT_EQ(web_contents()->GetRenderViewHost()->GetRoutingID(), | 525 EXPECT_EQ(web_contents()->GetRenderViewHost()->GetRoutingID(), |
526 resource.render_view_id); | 526 resource.render_view_id); |
527 | 527 |
(...skipping 216 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
744 NULL); | 744 NULL); |
745 | 745 |
746 SafeBrowsingService::UnsafeResource resource; | 746 SafeBrowsingService::UnsafeResource resource; |
747 EXPECT_CALL(*sb_service_, DoDisplayBlockingPage(_)) | 747 EXPECT_CALL(*sb_service_, DoDisplayBlockingPage(_)) |
748 .WillOnce(SaveArg<0>(&resource)); | 748 .WillOnce(SaveArg<0>(&resource)); |
749 | 749 |
750 NavigateAndCommit(url); | 750 NavigateAndCommit(url); |
751 // Wait for CheckCsdWhitelist to be called on the IO thread. | 751 // Wait for CheckCsdWhitelist to be called on the IO thread. |
752 FlushIOMessageLoop(); | 752 FlushIOMessageLoop(); |
753 // Wait for CheckCache() to be called on the UI thread. | 753 // Wait for CheckCache() to be called on the UI thread. |
754 MessageLoop::current()->RunAllPending(); | 754 MessageLoop::current()->RunUntilIdle(); |
755 // Now we check that all expected functions were indeed called on the two | 755 // Now we check that all expected functions were indeed called on the two |
756 // service objects. | 756 // service objects. |
757 EXPECT_TRUE(Mock::VerifyAndClear(csd_service_.get())); | 757 EXPECT_TRUE(Mock::VerifyAndClear(csd_service_.get())); |
758 EXPECT_TRUE(Mock::VerifyAndClear(sb_service_.get())); | 758 EXPECT_TRUE(Mock::VerifyAndClear(sb_service_.get())); |
759 EXPECT_EQ(url, resource.url); | 759 EXPECT_EQ(url, resource.url); |
760 EXPECT_EQ(url, resource.original_url); | 760 EXPECT_EQ(url, resource.original_url); |
761 resource.callback.Reset(); | 761 resource.callback.Reset(); |
762 msg = process()->sink().GetFirstMessageMatching( | 762 msg = process()->sink().GetFirstMessageMatching( |
763 SafeBrowsingMsg_StartPhishingDetection::ID); | 763 SafeBrowsingMsg_StartPhishingDetection::ID); |
764 ASSERT_FALSE(msg); | 764 ASSERT_FALSE(msg); |
765 } | 765 } |
766 | 766 |
767 } // namespace safe_browsing | 767 } // namespace safe_browsing |
OLD | NEW |