| 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/prefs/pref_service.h" | 5 #include "base/prefs/pref_service.h" |
| 6 #include "base/run_loop.h" |
| 6 #include "chrome/browser/profiles/profile.h" | 7 #include "chrome/browser/profiles/profile.h" |
| 7 #include "chrome/browser/safe_browsing/malware_details.h" | 8 #include "chrome/browser/safe_browsing/malware_details.h" |
| 8 #include "chrome/browser/safe_browsing/safe_browsing_blocking_page.h" | 9 #include "chrome/browser/safe_browsing/safe_browsing_blocking_page.h" |
| 9 #include "chrome/browser/safe_browsing/safe_browsing_service.h" | 10 #include "chrome/browser/safe_browsing/safe_browsing_service.h" |
| 10 #include "chrome/browser/safe_browsing/ui_manager.h" | 11 #include "chrome/browser/safe_browsing/ui_manager.h" |
| 11 #include "chrome/common/pref_names.h" | 12 #include "chrome/common/pref_names.h" |
| 12 #include "chrome/test/base/chrome_render_view_host_test_harness.h" | 13 #include "chrome/test/base/chrome_render_view_host_test_harness.h" |
| 13 #include "content/public/browser/interstitial_page.h" | 14 #include "content/public/browser/interstitial_page.h" |
| 14 #include "content/public/browser/navigation_entry.h" | 15 #include "content/public/browser/navigation_entry.h" |
| 15 #include "content/public/browser/render_process_host.h" | 16 #include "content/public/browser/render_process_host.h" |
| 16 #include "content/public/browser/web_contents.h" | 17 #include "content/public/browser/web_contents.h" |
| 17 #include "content/public/browser/web_contents_view.h" | 18 #include "content/public/browser/web_contents_view.h" |
| 18 #include "content/public/test/test_browser_thread.h" | |
| 19 #include "content/public/test/web_contents_tester.h" | 19 #include "content/public/test/web_contents_tester.h" |
| 20 | 20 |
| 21 using content::BrowserThread; | |
| 22 using content::InterstitialPage; | 21 using content::InterstitialPage; |
| 23 using content::NavigationEntry; | 22 using content::NavigationEntry; |
| 24 using content::WebContents; | 23 using content::WebContents; |
| 25 using content::WebContentsTester; | 24 using content::WebContentsTester; |
| 26 using content::WebContentsView; | 25 using content::WebContentsView; |
| 27 | 26 |
| 28 static const char* kGoogleURL = "http://www.google.com/"; | 27 static const char* kGoogleURL = "http://www.google.com/"; |
| 29 static const char* kGoodURL = "http://www.goodguys.com/"; | 28 static const char* kGoodURL = "http://www.goodguys.com/"; |
| 30 static const char* kBadURL = "http://www.badguys.com/"; | 29 static const char* kBadURL = "http://www.badguys.com/"; |
| 31 static const char* kBadURL2 = "http://www.badguys2.com/"; | 30 static const char* kBadURL2 = "http://www.badguys2.com/"; |
| (...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 112 | 111 |
| 113 class SafeBrowsingBlockingPageTest : public ChromeRenderViewHostTestHarness { | 112 class SafeBrowsingBlockingPageTest : public ChromeRenderViewHostTestHarness { |
| 114 public: | 113 public: |
| 115 // The decision the user made. | 114 // The decision the user made. |
| 116 enum UserResponse { | 115 enum UserResponse { |
| 117 PENDING, | 116 PENDING, |
| 118 OK, | 117 OK, |
| 119 CANCEL | 118 CANCEL |
| 120 }; | 119 }; |
| 121 | 120 |
| 122 SafeBrowsingBlockingPageTest() | 121 SafeBrowsingBlockingPageTest() { |
| 123 : ui_thread_(BrowserThread::UI, base::MessageLoop::current()), | |
| 124 file_user_blocking_thread_( | |
| 125 BrowserThread::FILE_USER_BLOCKING, base::MessageLoop::current()), | |
| 126 io_thread_(BrowserThread::IO, base::MessageLoop::current()) { | |
| 127 ResetUserResponse(); | 122 ResetUserResponse(); |
| 128 // The safe browsing UI manager does not need a service for this test. | 123 // The safe browsing UI manager does not need a service for this test. |
| 129 ui_manager_ = new TestSafeBrowsingUIManager(NULL); | 124 ui_manager_ = new TestSafeBrowsingUIManager(NULL); |
| 130 } | 125 } |
| 131 | 126 |
| 132 virtual void SetUp() { | 127 virtual void SetUp() { |
| 133 ChromeRenderViewHostTestHarness::SetUp(); | 128 ChromeRenderViewHostTestHarness::SetUp(); |
| 134 SafeBrowsingBlockingPage::RegisterFactory(&factory_); | 129 SafeBrowsingBlockingPage::RegisterFactory(&factory_); |
| 135 MalwareDetails::RegisterFactory(NULL); // Create it fresh each time. | 130 MalwareDetails::RegisterFactory(NULL); // Create it fresh each time. |
| 136 ResetUserResponse(); | 131 ResetUserResponse(); |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 189 interstitial->GetDelegateForTesting()); | 184 interstitial->GetDelegateForTesting()); |
| 190 } | 185 } |
| 191 | 186 |
| 192 UserResponse user_response() const { return user_response_; } | 187 UserResponse user_response() const { return user_response_; } |
| 193 void ResetUserResponse() { user_response_ = PENDING; } | 188 void ResetUserResponse() { user_response_ = PENDING; } |
| 194 | 189 |
| 195 static void ProceedThroughInterstitial( | 190 static void ProceedThroughInterstitial( |
| 196 SafeBrowsingBlockingPage* sb_interstitial) { | 191 SafeBrowsingBlockingPage* sb_interstitial) { |
| 197 sb_interstitial->interstitial_page_->Proceed(); | 192 sb_interstitial->interstitial_page_->Proceed(); |
| 198 // Proceed() posts a task to update the SafeBrowsingService::Client. | 193 // Proceed() posts a task to update the SafeBrowsingService::Client. |
| 199 base::MessageLoop::current()->RunUntilIdle(); | 194 base::RunLoop().RunUntilIdle(); |
| 200 } | 195 } |
| 201 | 196 |
| 202 static void DontProceedThroughInterstitial( | 197 static void DontProceedThroughInterstitial( |
| 203 SafeBrowsingBlockingPage* sb_interstitial) { | 198 SafeBrowsingBlockingPage* sb_interstitial) { |
| 204 sb_interstitial->interstitial_page_->DontProceed(); | 199 sb_interstitial->interstitial_page_->DontProceed(); |
| 205 // DontProceed() posts a task to update the SafeBrowsingService::Client. | 200 // DontProceed() posts a task to update the SafeBrowsingService::Client. |
| 206 base::MessageLoop::current()->RunUntilIdle(); | 201 base::RunLoop().RunUntilIdle(); |
| 207 } | 202 } |
| 208 | 203 |
| 209 void DontProceedThroughSubresourceInterstitial( | 204 void DontProceedThroughSubresourceInterstitial( |
| 210 SafeBrowsingBlockingPage* sb_interstitial) { | 205 SafeBrowsingBlockingPage* sb_interstitial) { |
| 211 // CommandReceived(kTakeMeBackCommand) does a back navigation for | 206 // CommandReceived(kTakeMeBackCommand) does a back navigation for |
| 212 // subresource interstitials. | 207 // subresource interstitials. |
| 213 GoBack(false); | 208 GoBack(false); |
| 214 // DontProceed() posts a task to update the SafeBrowsingService::Client. | 209 // DontProceed() posts a task to update the SafeBrowsingService::Client. |
| 215 base::MessageLoop::current()->RunUntilIdle(); | 210 base::RunLoop().RunUntilIdle(); |
| 216 } | 211 } |
| 217 | 212 |
| 218 scoped_refptr<TestSafeBrowsingUIManager> ui_manager_; | 213 scoped_refptr<TestSafeBrowsingUIManager> ui_manager_; |
| 219 | 214 |
| 220 private: | 215 private: |
| 221 void InitResource(SafeBrowsingUIManager::UnsafeResource* resource, | 216 void InitResource(SafeBrowsingUIManager::UnsafeResource* resource, |
| 222 bool is_subresource, | 217 bool is_subresource, |
| 223 const GURL& url) { | 218 const GURL& url) { |
| 224 resource->callback = | 219 resource->callback = |
| 225 base::Bind(&SafeBrowsingBlockingPageTest::OnBlockingPageComplete, | 220 base::Bind(&SafeBrowsingBlockingPageTest::OnBlockingPageComplete, |
| 226 base::Unretained(this)); | 221 base::Unretained(this)); |
| 227 resource->url = url; | 222 resource->url = url; |
| 228 resource->is_subresource = is_subresource; | 223 resource->is_subresource = is_subresource; |
| 229 resource->threat_type = SB_THREAT_TYPE_URL_MALWARE; | 224 resource->threat_type = SB_THREAT_TYPE_URL_MALWARE; |
| 230 resource->render_process_host_id = | 225 resource->render_process_host_id = |
| 231 web_contents()->GetRenderProcessHost()->GetID(); | 226 web_contents()->GetRenderProcessHost()->GetID(); |
| 232 resource->render_view_id = | 227 resource->render_view_id = |
| 233 web_contents()->GetRenderViewHost()->GetRoutingID(); | 228 web_contents()->GetRenderViewHost()->GetRoutingID(); |
| 234 } | 229 } |
| 235 | 230 |
| 236 UserResponse user_response_; | 231 UserResponse user_response_; |
| 237 TestSafeBrowsingBlockingPageFactory factory_; | 232 TestSafeBrowsingBlockingPageFactory factory_; |
| 238 content::TestBrowserThread ui_thread_; | |
| 239 content::TestBrowserThread file_user_blocking_thread_; | |
| 240 content::TestBrowserThread io_thread_; | |
| 241 }; | 233 }; |
| 242 | 234 |
| 243 // Tests showing a blocking page for a malware page and not proceeding. | 235 // Tests showing a blocking page for a malware page and not proceeding. |
| 244 TEST_F(SafeBrowsingBlockingPageTest, MalwarePageDontProceed) { | 236 TEST_F(SafeBrowsingBlockingPageTest, MalwarePageDontProceed) { |
| 245 // Enable malware details. | 237 // Enable malware details. |
| 246 Profile* profile = Profile::FromBrowserContext( | 238 Profile* profile = Profile::FromBrowserContext( |
| 247 web_contents()->GetBrowserContext()); | 239 web_contents()->GetBrowserContext()); |
| 248 profile->GetPrefs()->SetBoolean(prefs::kSafeBrowsingReportingEnabled, true); | 240 profile->GetPrefs()->SetBoolean(prefs::kSafeBrowsingReportingEnabled, true); |
| 249 | 241 |
| 250 // Start a load. | 242 // Start a load. |
| 251 controller().LoadURL(GURL(kBadURL), content::Referrer(), | 243 controller().LoadURL(GURL(kBadURL), content::Referrer(), |
| 252 content::PAGE_TRANSITION_TYPED, std::string()); | 244 content::PAGE_TRANSITION_TYPED, std::string()); |
| 253 | 245 |
| 254 | 246 |
| 255 // Simulate the load causing a safe browsing interstitial to be shown. | 247 // Simulate the load causing a safe browsing interstitial to be shown. |
| 256 ShowInterstitial(false, kBadURL); | 248 ShowInterstitial(false, kBadURL); |
| 257 SafeBrowsingBlockingPage* sb_interstitial = GetSafeBrowsingBlockingPage(); | 249 SafeBrowsingBlockingPage* sb_interstitial = GetSafeBrowsingBlockingPage(); |
| 258 ASSERT_TRUE(sb_interstitial); | 250 ASSERT_TRUE(sb_interstitial); |
| 259 | 251 |
| 260 base::MessageLoop::current()->RunUntilIdle(); | 252 base::RunLoop().RunUntilIdle(); |
| 261 | 253 |
| 262 // Simulate the user clicking "don't proceed". | 254 // Simulate the user clicking "don't proceed". |
| 263 DontProceedThroughInterstitial(sb_interstitial); | 255 DontProceedThroughInterstitial(sb_interstitial); |
| 264 | 256 |
| 265 // The interstitial should be gone. | 257 // The interstitial should be gone. |
| 266 EXPECT_EQ(CANCEL, user_response()); | 258 EXPECT_EQ(CANCEL, user_response()); |
| 267 EXPECT_FALSE(GetSafeBrowsingBlockingPage()); | 259 EXPECT_FALSE(GetSafeBrowsingBlockingPage()); |
| 268 | 260 |
| 269 // We did not proceed, the pending entry should be gone. | 261 // We did not proceed, the pending entry should be gone. |
| 270 EXPECT_FALSE(controller().GetPendingEntry()); | 262 EXPECT_FALSE(controller().GetPendingEntry()); |
| (...skipping 308 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 579 | 571 |
| 580 // Start a load. | 572 // Start a load. |
| 581 controller().LoadURL(GURL(kBadURL), content::Referrer(), | 573 controller().LoadURL(GURL(kBadURL), content::Referrer(), |
| 582 content::PAGE_TRANSITION_TYPED, std::string()); | 574 content::PAGE_TRANSITION_TYPED, std::string()); |
| 583 | 575 |
| 584 // Simulate the load causing a safe browsing interstitial to be shown. | 576 // Simulate the load causing a safe browsing interstitial to be shown. |
| 585 ShowInterstitial(false, kBadURL); | 577 ShowInterstitial(false, kBadURL); |
| 586 SafeBrowsingBlockingPage* sb_interstitial = GetSafeBrowsingBlockingPage(); | 578 SafeBrowsingBlockingPage* sb_interstitial = GetSafeBrowsingBlockingPage(); |
| 587 ASSERT_TRUE(sb_interstitial); | 579 ASSERT_TRUE(sb_interstitial); |
| 588 | 580 |
| 589 base::MessageLoop::current()->RunUntilIdle(); | 581 base::RunLoop().RunUntilIdle(); |
| 590 | 582 |
| 591 // Simulate the user clicking "proceed" then "don't proceed" (before the | 583 // Simulate the user clicking "proceed" then "don't proceed" (before the |
| 592 // interstitial is shown). | 584 // interstitial is shown). |
| 593 sb_interstitial->interstitial_page_->Proceed(); | 585 sb_interstitial->interstitial_page_->Proceed(); |
| 594 sb_interstitial->interstitial_page_->DontProceed(); | 586 sb_interstitial->interstitial_page_->DontProceed(); |
| 595 // Proceed() and DontProceed() post a task to update the | 587 // Proceed() and DontProceed() post a task to update the |
| 596 // SafeBrowsingService::Client. | 588 // SafeBrowsingService::Client. |
| 597 base::MessageLoop::current()->RunUntilIdle(); | 589 base::RunLoop().RunUntilIdle(); |
| 598 | 590 |
| 599 // The interstitial should be gone. | 591 // The interstitial should be gone. |
| 600 EXPECT_EQ(OK, user_response()); | 592 EXPECT_EQ(OK, user_response()); |
| 601 EXPECT_FALSE(GetSafeBrowsingBlockingPage()); | 593 EXPECT_FALSE(GetSafeBrowsingBlockingPage()); |
| 602 | 594 |
| 603 // Only one report should have been sent. | 595 // Only one report should have been sent. |
| 604 EXPECT_EQ(1u, ui_manager_->GetDetails()->size()); | 596 EXPECT_EQ(1u, ui_manager_->GetDetails()->size()); |
| 605 ui_manager_->GetDetails()->clear(); | 597 ui_manager_->GetDetails()->clear(); |
| 606 } | 598 } |
| 607 | 599 |
| 608 // Tests showing a blocking page for a malware page with reports disabled. | 600 // Tests showing a blocking page for a malware page with reports disabled. |
| 609 TEST_F(SafeBrowsingBlockingPageTest, MalwareReportsDisabled) { | 601 TEST_F(SafeBrowsingBlockingPageTest, MalwareReportsDisabled) { |
| 610 // Disable malware reports. | 602 // Disable malware reports. |
| 611 Profile* profile = Profile::FromBrowserContext( | 603 Profile* profile = Profile::FromBrowserContext( |
| 612 web_contents()->GetBrowserContext()); | 604 web_contents()->GetBrowserContext()); |
| 613 profile->GetPrefs()->SetBoolean(prefs::kSafeBrowsingReportingEnabled, false); | 605 profile->GetPrefs()->SetBoolean(prefs::kSafeBrowsingReportingEnabled, false); |
| 614 | 606 |
| 615 // Start a load. | 607 // Start a load. |
| 616 controller().LoadURL(GURL(kBadURL), content::Referrer(), | 608 controller().LoadURL(GURL(kBadURL), content::Referrer(), |
| 617 content::PAGE_TRANSITION_TYPED, std::string()); | 609 content::PAGE_TRANSITION_TYPED, std::string()); |
| 618 | 610 |
| 619 // Simulate the load causing a safe browsing interstitial to be shown. | 611 // Simulate the load causing a safe browsing interstitial to be shown. |
| 620 ShowInterstitial(false, kBadURL); | 612 ShowInterstitial(false, kBadURL); |
| 621 SafeBrowsingBlockingPage* sb_interstitial = GetSafeBrowsingBlockingPage(); | 613 SafeBrowsingBlockingPage* sb_interstitial = GetSafeBrowsingBlockingPage(); |
| 622 ASSERT_TRUE(sb_interstitial); | 614 ASSERT_TRUE(sb_interstitial); |
| 623 | 615 |
| 624 base::MessageLoop::current()->RunUntilIdle(); | 616 base::RunLoop().RunUntilIdle(); |
| 625 | 617 |
| 626 // Simulate the user clicking "don't proceed". | 618 // Simulate the user clicking "don't proceed". |
| 627 DontProceedThroughInterstitial(sb_interstitial); | 619 DontProceedThroughInterstitial(sb_interstitial); |
| 628 | 620 |
| 629 // The interstitial should be gone. | 621 // The interstitial should be gone. |
| 630 EXPECT_EQ(CANCEL, user_response()); | 622 EXPECT_EQ(CANCEL, user_response()); |
| 631 EXPECT_FALSE(GetSafeBrowsingBlockingPage()); | 623 EXPECT_FALSE(GetSafeBrowsingBlockingPage()); |
| 632 | 624 |
| 633 // We did not proceed, the pending entry should be gone. | 625 // We did not proceed, the pending entry should be gone. |
| 634 EXPECT_FALSE(controller().GetPendingEntry()); | 626 EXPECT_FALSE(controller().GetPendingEntry()); |
| (...skipping 12 matching lines...) Expand all Loading... |
| 647 | 639 |
| 648 // Start a load. | 640 // Start a load. |
| 649 controller().LoadURL(GURL(kBadURL), content::Referrer(), | 641 controller().LoadURL(GURL(kBadURL), content::Referrer(), |
| 650 content::PAGE_TRANSITION_TYPED, std::string()); | 642 content::PAGE_TRANSITION_TYPED, std::string()); |
| 651 | 643 |
| 652 // Simulate the load causing a safe browsing interstitial to be shown. | 644 // Simulate the load causing a safe browsing interstitial to be shown. |
| 653 ShowInterstitial(false, kBadURL); | 645 ShowInterstitial(false, kBadURL); |
| 654 SafeBrowsingBlockingPage* sb_interstitial = GetSafeBrowsingBlockingPage(); | 646 SafeBrowsingBlockingPage* sb_interstitial = GetSafeBrowsingBlockingPage(); |
| 655 ASSERT_TRUE(sb_interstitial); | 647 ASSERT_TRUE(sb_interstitial); |
| 656 | 648 |
| 657 base::MessageLoop::current()->RunUntilIdle(); | 649 base::RunLoop().RunUntilIdle(); |
| 658 | 650 |
| 659 EXPECT_FALSE(profile->GetPrefs()->GetBoolean( | 651 EXPECT_FALSE(profile->GetPrefs()->GetBoolean( |
| 660 prefs::kSafeBrowsingReportingEnabled)); | 652 prefs::kSafeBrowsingReportingEnabled)); |
| 661 | 653 |
| 662 // Simulate the user check the report agreement checkbox. | 654 // Simulate the user check the report agreement checkbox. |
| 663 sb_interstitial->SetReportingPreference(true); | 655 sb_interstitial->SetReportingPreference(true); |
| 664 | 656 |
| 665 EXPECT_TRUE(profile->GetPrefs()->GetBoolean( | 657 EXPECT_TRUE(profile->GetPrefs()->GetBoolean( |
| 666 prefs::kSafeBrowsingReportingEnabled)); | 658 prefs::kSafeBrowsingReportingEnabled)); |
| 667 | 659 |
| 668 // Simulate the user uncheck the report agreement checkbox. | 660 // Simulate the user uncheck the report agreement checkbox. |
| 669 sb_interstitial->SetReportingPreference(false); | 661 sb_interstitial->SetReportingPreference(false); |
| 670 | 662 |
| 671 EXPECT_FALSE(profile->GetPrefs()->GetBoolean( | 663 EXPECT_FALSE(profile->GetPrefs()->GetBoolean( |
| 672 prefs::kSafeBrowsingReportingEnabled)); | 664 prefs::kSafeBrowsingReportingEnabled)); |
| 673 } | 665 } |
| OLD | NEW |