| Index: chrome/browser/safe_browsing/safe_browsing_blocking_page_unittest.cc
|
| ===================================================================
|
| --- chrome/browser/safe_browsing/safe_browsing_blocking_page_unittest.cc (revision 66642)
|
| +++ chrome/browser/safe_browsing/safe_browsing_blocking_page_unittest.cc (working copy)
|
| @@ -5,9 +5,13 @@
|
| #include "chrome/browser/renderer_host/test/test_render_view_host.h"
|
|
|
| #include "chrome/browser/browser_thread.h"
|
| +#include "chrome/browser/prefs/pref_service.h"
|
| +#include "chrome/browser/profile.h"
|
| +#include "chrome/browser/safe_browsing/malware_report.h"
|
| #include "chrome/browser/safe_browsing/safe_browsing_blocking_page.h"
|
| #include "chrome/browser/tab_contents/navigation_entry.h"
|
| #include "chrome/browser/tab_contents/test_tab_contents.h"
|
| +#include "chrome/common/pref_names.h"
|
| #include "chrome/common/render_messages.h"
|
| #include "chrome/common/render_messages_params.h"
|
|
|
| @@ -32,6 +36,22 @@
|
| }
|
| };
|
|
|
| +class TestSafeBrowsingService: public SafeBrowsingService {
|
| + public:
|
| + virtual ~TestSafeBrowsingService() {}
|
| + virtual void SendMalwareReport(
|
| + scoped_refptr<SafeBrowsingMalwareReport> report) {
|
| + LOG(INFO) << "SendMalwareReport";
|
| + reports_.push_back(report);
|
| + }
|
| +
|
| + std::list<scoped_refptr<SafeBrowsingMalwareReport> >* GetReports() {
|
| + return &reports_;
|
| + }
|
| +
|
| + std::list<scoped_refptr<SafeBrowsingMalwareReport> > reports_;
|
| +};
|
| +
|
| class TestSafeBrowsingBlockingPageFactory
|
| : public SafeBrowsingBlockingPageFactory {
|
| public:
|
| @@ -61,7 +81,7 @@
|
| : ui_thread_(BrowserThread::UI, MessageLoop::current()),
|
| io_thread_(BrowserThread::IO, MessageLoop::current()) {
|
| ResetUserResponse();
|
| - service_ = new SafeBrowsingService();
|
| + service_ = new TestSafeBrowsingService();
|
| }
|
|
|
| virtual void SetUp() {
|
| @@ -128,6 +148,8 @@
|
| MessageLoop::current()->RunAllPending();
|
| }
|
|
|
| + scoped_refptr<TestSafeBrowsingService> service_;
|
| +
|
| private:
|
| void InitResource(SafeBrowsingService::UnsafeResource* resource,
|
| ResourceType::Type resource_type,
|
| @@ -141,7 +163,6 @@
|
| }
|
|
|
| UserResponse user_response_;
|
| - scoped_refptr<SafeBrowsingService> service_;
|
| TestSafeBrowsingBlockingPageFactory factory_;
|
| BrowserThread ui_thread_;
|
| BrowserThread io_thread_;
|
| @@ -149,9 +170,14 @@
|
|
|
| // Tests showing a blocking page for a malware page and not proceeding.
|
| TEST_F(SafeBrowsingBlockingPageTest, MalwarePageDontProceed) {
|
| + // Enable malware reports.
|
| + contents()->profile()->GetPrefs()->SetBoolean(
|
| + prefs::kSafeBrowsingReportingEnabled, true);
|
| +
|
| // Start a load.
|
| controller().LoadURL(GURL(kBadURL), GURL(), PageTransition::TYPED);
|
|
|
| +
|
| // Simulate the load causing a safe browsing interstitial to be shown.
|
| ShowInterstitial(ResourceType::MAIN_FRAME, kBadURL);
|
| SafeBrowsingBlockingPage* sb_interstitial = GetSafeBrowsingBlockingPage();
|
| @@ -168,10 +194,18 @@
|
|
|
| // We did not proceed, the pending entry should be gone.
|
| EXPECT_FALSE(controller().pending_entry());
|
| +
|
| + // A report should have been sent.
|
| + EXPECT_EQ(1u, service_->GetReports()->size());
|
| + service_->GetReports()->clear();
|
| }
|
|
|
| // Tests showing a blocking page for a malware page and then proceeding.
|
| TEST_F(SafeBrowsingBlockingPageTest, MalwarePageProceed) {
|
| + // Enable malware reports.
|
| + contents()->profile()->GetPrefs()->SetBoolean(
|
| + prefs::kSafeBrowsingReportingEnabled, true);
|
| +
|
| // Start a load.
|
| controller().LoadURL(GURL(kBadURL), GURL(), PageTransition::TYPED);
|
|
|
| @@ -189,11 +223,19 @@
|
| Navigate(kBadURL, 1);
|
| // The interstitial should be gone now.
|
| ASSERT_FALSE(InterstitialPage::GetInterstitialPage(contents()));
|
| +
|
| + // A report should have been sent.
|
| + EXPECT_EQ(1u, service_->GetReports()->size());
|
| + service_->GetReports()->clear();
|
| }
|
|
|
| // Tests showing a blocking page for a page that contains malware subresources
|
| // and not proceeding.
|
| TEST_F(SafeBrowsingBlockingPageTest, PageWithMalwareResourceDontProceed) {
|
| + // Enable malware reports.
|
| + contents()->profile()->GetPrefs()->SetBoolean(
|
| + prefs::kSafeBrowsingReportingEnabled, true);
|
| +
|
| // Navigate somewhere.
|
| Navigate(kGoogleURL, 1);
|
|
|
| @@ -215,11 +257,19 @@
|
| // have been removed from the navigation controller.
|
| ASSERT_EQ(1, controller().entry_count());
|
| EXPECT_EQ(kGoogleURL, controller().GetActiveEntry()->url().spec());
|
| +
|
| + // A report should have been sent.
|
| + EXPECT_EQ(1u, service_->GetReports()->size());
|
| + service_->GetReports()->clear();
|
| }
|
|
|
| // Tests showing a blocking page for a page that contains malware subresources
|
| // and proceeding.
|
| TEST_F(SafeBrowsingBlockingPageTest, PageWithMalwareResourceProceed) {
|
| + // Enable malware reports.
|
| + contents()->profile()->GetPrefs()->SetBoolean(
|
| + prefs::kSafeBrowsingReportingEnabled, true);
|
| +
|
| // Navigate somewhere.
|
| Navigate(kGoodURL, 1);
|
|
|
| @@ -237,6 +287,10 @@
|
| // We did proceed, we should be back to showing the page.
|
| ASSERT_EQ(1, controller().entry_count());
|
| EXPECT_EQ(kGoodURL, controller().GetActiveEntry()->url().spec());
|
| +
|
| + // A report should have been sent.
|
| + EXPECT_EQ(1u, service_->GetReports()->size());
|
| + service_->GetReports()->clear();
|
| }
|
|
|
| // Tests showing a blocking page for a page that contains multiple malware
|
| @@ -244,6 +298,10 @@
|
| // subresources (which trigger queued interstitial pages) do not break anything.
|
| TEST_F(SafeBrowsingBlockingPageTest,
|
| PageWithMultipleMalwareResourceDontProceed) {
|
| + // Enable malware reports.
|
| + contents()->profile()->GetPrefs()->SetBoolean(
|
| + prefs::kSafeBrowsingReportingEnabled, true);
|
| +
|
| // Navigate somewhere.
|
| Navigate(kGoogleURL, 1);
|
|
|
| @@ -270,12 +328,20 @@
|
| // have been removed from the navigation controller.
|
| ASSERT_EQ(1, controller().entry_count());
|
| EXPECT_EQ(kGoogleURL, controller().GetActiveEntry()->url().spec());
|
| +
|
| + // A report should have been sent.
|
| + EXPECT_EQ(1u, service_->GetReports()->size());
|
| + service_->GetReports()->clear();
|
| }
|
|
|
| // Tests showing a blocking page for a page that contains multiple malware
|
| // subresources and proceeding through the first interstitial, but not the next.
|
| TEST_F(SafeBrowsingBlockingPageTest,
|
| PageWithMultipleMalwareResourceProceedThenDontProceed) {
|
| + // Enable malware reports.
|
| + contents()->profile()->GetPrefs()->SetBoolean(
|
| + prefs::kSafeBrowsingReportingEnabled, true);
|
| +
|
| // Navigate somewhere.
|
| Navigate(kGoogleURL, 1);
|
|
|
| @@ -297,6 +363,10 @@
|
| ProceedThroughInterstitial(sb_interstitial);
|
| EXPECT_EQ(OK, user_response());
|
|
|
| + // A report should have been sent.
|
| + EXPECT_EQ(1u, service_->GetReports()->size());
|
| + service_->GetReports()->clear();
|
| +
|
| ResetUserResponse();
|
|
|
| // We should land to a 2nd interstitial (aggregating all the malware resources
|
| @@ -313,11 +383,20 @@
|
| // have been removed from the navigation controller.
|
| ASSERT_EQ(1, controller().entry_count());
|
| EXPECT_EQ(kGoogleURL, controller().GetActiveEntry()->url().spec());
|
| +
|
| + // No report should have been sent -- we don't create a report the
|
| + // second time.
|
| + EXPECT_EQ(0u, service_->GetReports()->size());
|
| + service_->GetReports()->clear();
|
| }
|
|
|
| // Tests showing a blocking page for a page that contains multiple malware
|
| // subresources and proceeding through the multiple interstitials.
|
| TEST_F(SafeBrowsingBlockingPageTest, PageWithMultipleMalwareResourceProceed) {
|
| + // Enable malware reports.
|
| + contents()->profile()->GetPrefs()->SetBoolean(
|
| + prefs::kSafeBrowsingReportingEnabled, true);
|
| +
|
| // Navigate somewhere else.
|
| Navigate(kGoodURL, 1);
|
|
|
| @@ -336,6 +415,10 @@
|
| ProceedThroughInterstitial(sb_interstitial);
|
| EXPECT_EQ(OK, user_response());
|
|
|
| + // A report should have been sent.
|
| + EXPECT_EQ(1u, service_->GetReports()->size());
|
| + service_->GetReports()->clear();
|
| +
|
| ResetUserResponse();
|
|
|
| // We should land to a 2nd interstitial (aggregating all the malware resources
|
| @@ -350,11 +433,20 @@
|
| // We did proceed, we should be back to the initial page.
|
| ASSERT_EQ(1, controller().entry_count());
|
| EXPECT_EQ(kGoodURL, controller().GetActiveEntry()->url().spec());
|
| +
|
| + // No report should have been sent -- we don't create a report the
|
| + // second time.
|
| + EXPECT_EQ(0u, service_->GetReports()->size());
|
| + service_->GetReports()->clear();
|
| }
|
|
|
| // Tests showing a blocking page then navigating back and forth to make sure the
|
| // controller entries are OK. http://crbug.com/17627
|
| TEST_F(SafeBrowsingBlockingPageTest, NavigatingBackAndForth) {
|
| + // Enable malware reports.
|
| + contents()->profile()->GetPrefs()->SetBoolean(
|
| + prefs::kSafeBrowsingReportingEnabled, true);
|
| +
|
| // Navigate somewhere.
|
| Navigate(kGoodURL, 1);
|
|
|
| @@ -388,11 +480,19 @@
|
| ASSERT_FALSE(sb_interstitial);
|
| ASSERT_EQ(2, controller().entry_count());
|
| EXPECT_EQ(kBadURL, controller().GetActiveEntry()->url().spec());
|
| +
|
| + // Two reports should have been sent.
|
| + EXPECT_EQ(2u, service_->GetReports()->size());
|
| + service_->GetReports()->clear();
|
| }
|
|
|
| // Tests that calling "don't proceed" after "proceed" has been called doesn't
|
| // cause problems. http://crbug.com/30079
|
| TEST_F(SafeBrowsingBlockingPageTest, ProceedThenDontProceed) {
|
| + // Enable malware reports.
|
| + contents()->profile()->GetPrefs()->SetBoolean(
|
| + prefs::kSafeBrowsingReportingEnabled, true);
|
| +
|
| // Start a load.
|
| controller().LoadURL(GURL(kBadURL), GURL(), PageTransition::TYPED);
|
|
|
| @@ -414,4 +514,39 @@
|
| // The interstitial should be gone.
|
| EXPECT_EQ(OK, user_response());
|
| EXPECT_FALSE(GetSafeBrowsingBlockingPage());
|
| +
|
| + // Only one report should have been sent.
|
| + EXPECT_EQ(1u, service_->GetReports()->size());
|
| + service_->GetReports()->clear();
|
| }
|
| +
|
| +// Tests showing a blocking page for a malware page with reports disabled.
|
| +TEST_F(SafeBrowsingBlockingPageTest, MalwareReportsDisabled) {
|
| + // Disable malware reports.
|
| + contents()->profile()->GetPrefs()->SetBoolean(
|
| + prefs::kSafeBrowsingReportingEnabled, false);
|
| +
|
| + // Start a load.
|
| + controller().LoadURL(GURL(kBadURL), GURL(), PageTransition::TYPED);
|
| +
|
| + // Simulate the load causing a safe browsing interstitial to be shown.
|
| + ShowInterstitial(ResourceType::MAIN_FRAME, kBadURL);
|
| + SafeBrowsingBlockingPage* sb_interstitial = GetSafeBrowsingBlockingPage();
|
| + ASSERT_TRUE(sb_interstitial);
|
| +
|
| + MessageLoop::current()->RunAllPending();
|
| +
|
| + // Simulate the user clicking "don't proceed".
|
| + DontProceedThroughInterstitial(sb_interstitial);
|
| +
|
| + // The interstitial should be gone.
|
| + EXPECT_EQ(CANCEL, user_response());
|
| + EXPECT_FALSE(GetSafeBrowsingBlockingPage());
|
| +
|
| + // We did not proceed, the pending entry should be gone.
|
| + EXPECT_FALSE(controller().pending_entry());
|
| +
|
| + // No report should have been sent.
|
| + EXPECT_EQ(0u, service_->GetReports()->size());
|
| + service_->GetReports()->clear();
|
| +}
|
|
|