This source file includes following definitions.
- SendSerializedMalwareDetails
- GetDetails
- CreateSafeBrowsingPage
- SetUp
- TearDown
- OnBlockingPageComplete
- Navigate
- GoBack
- ShowInterstitial
- GetSafeBrowsingBlockingPage
- user_response
- ResetUserResponse
- ProceedThroughInterstitial
- DontProceedThroughInterstitial
- DontProceedThroughSubresourceInterstitial
- InitResource
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
#include <list>
#include "base/prefs/pref_service.h"
#include "base/run_loop.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/safe_browsing/malware_details.h"
#include "chrome/browser/safe_browsing/safe_browsing_blocking_page.h"
#include "chrome/browser/safe_browsing/safe_browsing_service.h"
#include "chrome/browser/safe_browsing/ui_manager.h"
#include "chrome/common/pref_names.h"
#include "chrome/test/base/chrome_render_view_host_test_harness.h"
#include "content/public/browser/interstitial_page.h"
#include "content/public/browser/navigation_entry.h"
#include "content/public/browser/render_process_host.h"
#include "content/public/browser/web_contents.h"
#include "content/public/browser/web_contents_view.h"
#include "content/public/test/web_contents_tester.h"
using content::InterstitialPage;
using content::NavigationEntry;
using content::WebContents;
using content::WebContentsTester;
using content::WebContentsView;
static const char* kGoogleURL = "http://www.google.com/";
static const char* kGoodURL = "http://www.goodguys.com/";
static const char* kBadURL = "http://www.badguys.com/";
static const char* kBadURL2 = "http://www.badguys2.com/";
static const char* kBadURL3 = "http://www.badguys3.com/";
namespace {
class TestSafeBrowsingBlockingPageV1 : public SafeBrowsingBlockingPageV1 {
public:
TestSafeBrowsingBlockingPageV1(SafeBrowsingUIManager* manager,
WebContents* web_contents,
const UnsafeResourceList& unsafe_resources)
: SafeBrowsingBlockingPageV1(manager, web_contents, unsafe_resources) {
malware_details_proceed_delay_ms_ = 0;
interstitial_page()->DontCreateViewForTesting();
}
};
class TestSafeBrowsingBlockingPageV2 : public SafeBrowsingBlockingPageV2 {
public:
TestSafeBrowsingBlockingPageV2(SafeBrowsingUIManager* manager,
WebContents* web_contents,
const UnsafeResourceList& unsafe_resources)
: SafeBrowsingBlockingPageV2(manager, web_contents, unsafe_resources) {
malware_details_proceed_delay_ms_ = 0;
interstitial_page()->DontCreateViewForTesting();
}
};
class TestSafeBrowsingUIManager: public SafeBrowsingUIManager {
public:
explicit TestSafeBrowsingUIManager(SafeBrowsingService* service)
: SafeBrowsingUIManager(service) {
}
virtual void SendSerializedMalwareDetails(
const std::string& serialized) OVERRIDE {
details_.push_back(serialized);
}
std::list<std::string>* GetDetails() {
return &details_;
}
private:
virtual ~TestSafeBrowsingUIManager() {}
std::list<std::string> details_;
};
class TestSafeBrowsingBlockingPageFactory
: public SafeBrowsingBlockingPageFactory {
public:
TestSafeBrowsingBlockingPageFactory() { }
virtual ~TestSafeBrowsingBlockingPageFactory() { }
virtual SafeBrowsingBlockingPage* CreateSafeBrowsingPage(
SafeBrowsingUIManager* manager,
WebContents* web_contents,
const SafeBrowsingBlockingPage::UnsafeResourceList& unsafe_resources)
OVERRIDE {
if (unsafe_resources.size() == 1 &&
(unsafe_resources[0].threat_type == SB_THREAT_TYPE_URL_MALWARE ||
unsafe_resources[0].threat_type == SB_THREAT_TYPE_URL_PHISHING)) {
return new TestSafeBrowsingBlockingPageV2(manager, web_contents,
unsafe_resources);
}
return new TestSafeBrowsingBlockingPageV1(manager, web_contents,
unsafe_resources);
}
};
}
class SafeBrowsingBlockingPageTest : public ChromeRenderViewHostTestHarness {
public:
enum UserResponse {
PENDING,
OK,
CANCEL
};
SafeBrowsingBlockingPageTest() {
ResetUserResponse();
ui_manager_ = new TestSafeBrowsingUIManager(NULL);
}
virtual void SetUp() OVERRIDE {
ChromeRenderViewHostTestHarness::SetUp();
SafeBrowsingBlockingPage::RegisterFactory(&factory_);
ResetUserResponse();
}
virtual void TearDown() OVERRIDE {
ui_manager_ = NULL;
SafeBrowsingBlockingPage::RegisterFactory(NULL);
MalwareDetails::RegisterFactory(NULL);
ChromeRenderViewHostTestHarness::TearDown();
}
void OnBlockingPageComplete(bool proceed) {
if (proceed)
user_response_ = OK;
else
user_response_ = CANCEL;
}
void Navigate(const char* url, int page_id) {
WebContentsTester::For(web_contents())->TestDidNavigate(
web_contents()->GetRenderViewHost(), page_id, GURL(url),
content::PAGE_TRANSITION_TYPED);
}
void GoBack(bool is_cross_site) {
NavigationEntry* entry =
web_contents()->GetController().GetEntryAtOffset(-1);
ASSERT_TRUE(entry);
web_contents()->GetController().GoBack();
content::RenderViewHost* rvh = is_cross_site ?
WebContentsTester::For(web_contents())->GetPendingRenderViewHost() :
web_contents()->GetRenderViewHost();
WebContentsTester::For(web_contents())->TestDidNavigate(
rvh,
entry->GetPageID(),
GURL(entry->GetURL()),
content::PAGE_TRANSITION_TYPED);
}
void ShowInterstitial(bool is_subresource, const char* url) {
SafeBrowsingUIManager::UnsafeResource resource;
InitResource(&resource, is_subresource, GURL(url));
SafeBrowsingBlockingPage::ShowBlockingPage(ui_manager_.get(), resource);
}
SafeBrowsingBlockingPage* GetSafeBrowsingBlockingPage() {
InterstitialPage* interstitial =
InterstitialPage::GetInterstitialPage(web_contents());
if (!interstitial)
return NULL;
return static_cast<SafeBrowsingBlockingPage*>(
interstitial->GetDelegateForTesting());
}
UserResponse user_response() const { return user_response_; }
void ResetUserResponse() { user_response_ = PENDING; }
static void ProceedThroughInterstitial(
SafeBrowsingBlockingPage* sb_interstitial) {
sb_interstitial->interstitial_page_->Proceed();
base::RunLoop().RunUntilIdle();
}
static void DontProceedThroughInterstitial(
SafeBrowsingBlockingPage* sb_interstitial) {
sb_interstitial->interstitial_page_->DontProceed();
base::RunLoop().RunUntilIdle();
}
void DontProceedThroughSubresourceInterstitial(
SafeBrowsingBlockingPage* sb_interstitial) {
GoBack(false);
base::RunLoop().RunUntilIdle();
}
scoped_refptr<TestSafeBrowsingUIManager> ui_manager_;
private:
void InitResource(SafeBrowsingUIManager::UnsafeResource* resource,
bool is_subresource,
const GURL& url) {
resource->callback =
base::Bind(&SafeBrowsingBlockingPageTest::OnBlockingPageComplete,
base::Unretained(this));
resource->url = url;
resource->is_subresource = is_subresource;
resource->threat_type = SB_THREAT_TYPE_URL_MALWARE;
resource->render_process_host_id =
web_contents()->GetRenderProcessHost()->GetID();
resource->render_view_id =
web_contents()->GetRenderViewHost()->GetRoutingID();
}
UserResponse user_response_;
TestSafeBrowsingBlockingPageFactory factory_;
};
TEST_F(SafeBrowsingBlockingPageTest, MalwarePageDontProceed) {
Profile* profile = Profile::FromBrowserContext(
web_contents()->GetBrowserContext());
profile->GetPrefs()->SetBoolean(prefs::kSafeBrowsingReportingEnabled, true);
controller().LoadURL(GURL(kBadURL), content::Referrer(),
content::PAGE_TRANSITION_TYPED, std::string());
ShowInterstitial(false, kBadURL);
SafeBrowsingBlockingPage* sb_interstitial = GetSafeBrowsingBlockingPage();
ASSERT_TRUE(sb_interstitial);
base::RunLoop().RunUntilIdle();
DontProceedThroughInterstitial(sb_interstitial);
EXPECT_EQ(CANCEL, user_response());
EXPECT_FALSE(GetSafeBrowsingBlockingPage());
EXPECT_FALSE(controller().GetPendingEntry());
EXPECT_EQ(1u, ui_manager_->GetDetails()->size());
ui_manager_->GetDetails()->clear();
}
TEST_F(SafeBrowsingBlockingPageTest, MalwarePageProceed) {
Profile* profile = Profile::FromBrowserContext(
web_contents()->GetBrowserContext());
profile->GetPrefs()->SetBoolean(prefs::kSafeBrowsingReportingEnabled, true);
controller().LoadURL(GURL(kBadURL), content::Referrer(),
content::PAGE_TRANSITION_TYPED, std::string());
ShowInterstitial(false, kBadURL);
SafeBrowsingBlockingPage* sb_interstitial = GetSafeBrowsingBlockingPage();
ASSERT_TRUE(sb_interstitial);
ProceedThroughInterstitial(sb_interstitial);
ASSERT_TRUE(InterstitialPage::GetInterstitialPage(web_contents()));
Navigate(kBadURL, 1);
ASSERT_FALSE(InterstitialPage::GetInterstitialPage(web_contents()));
EXPECT_EQ(1u, ui_manager_->GetDetails()->size());
ui_manager_->GetDetails()->clear();
}
TEST_F(SafeBrowsingBlockingPageTest, PageWithMalwareResourceDontProceed) {
Profile* profile = Profile::FromBrowserContext(
web_contents()->GetBrowserContext());
profile->GetPrefs()->SetBoolean(prefs::kSafeBrowsingReportingEnabled, true);
Navigate(kGoogleURL, 1);
Navigate(kGoodURL, 2);
ShowInterstitial(true, kBadURL);
SafeBrowsingBlockingPage* sb_interstitial = GetSafeBrowsingBlockingPage();
ASSERT_TRUE(sb_interstitial);
DontProceedThroughSubresourceInterstitial(sb_interstitial);
EXPECT_EQ(CANCEL, user_response());
EXPECT_FALSE(GetSafeBrowsingBlockingPage());
ASSERT_EQ(1, controller().GetEntryCount());
EXPECT_EQ(kGoogleURL, controller().GetActiveEntry()->GetURL().spec());
EXPECT_EQ(1u, ui_manager_->GetDetails()->size());
ui_manager_->GetDetails()->clear();
}
TEST_F(SafeBrowsingBlockingPageTest, PageWithMalwareResourceProceed) {
Profile* profile = Profile::FromBrowserContext(
web_contents()->GetBrowserContext());
profile->GetPrefs()->SetBoolean(prefs::kSafeBrowsingReportingEnabled, true);
Navigate(kGoodURL, 1);
ShowInterstitial(true, kBadURL);
SafeBrowsingBlockingPage* sb_interstitial = GetSafeBrowsingBlockingPage();
ASSERT_TRUE(sb_interstitial);
ProceedThroughInterstitial(sb_interstitial);
EXPECT_EQ(OK, user_response());
EXPECT_FALSE(GetSafeBrowsingBlockingPage());
ASSERT_EQ(1, controller().GetEntryCount());
EXPECT_EQ(kGoodURL, controller().GetActiveEntry()->GetURL().spec());
EXPECT_EQ(1u, ui_manager_->GetDetails()->size());
ui_manager_->GetDetails()->clear();
}
TEST_F(SafeBrowsingBlockingPageTest,
PageWithMultipleMalwareResourceDontProceed) {
Profile* profile = Profile::FromBrowserContext(
web_contents()->GetBrowserContext());
profile->GetPrefs()->SetBoolean(prefs::kSafeBrowsingReportingEnabled, true);
Navigate(kGoogleURL, 1);
Navigate(kGoodURL, 2);
ShowInterstitial(true, kBadURL);
ShowInterstitial(true, kBadURL2);
ShowInterstitial(true, kBadURL3);
SafeBrowsingBlockingPage* sb_interstitial = GetSafeBrowsingBlockingPage();
ASSERT_TRUE(sb_interstitial);
DontProceedThroughSubresourceInterstitial(sb_interstitial);
EXPECT_EQ(CANCEL, user_response());
EXPECT_FALSE(GetSafeBrowsingBlockingPage());
ASSERT_EQ(1, controller().GetEntryCount());
EXPECT_EQ(kGoogleURL, controller().GetActiveEntry()->GetURL().spec());
EXPECT_EQ(1u, ui_manager_->GetDetails()->size());
ui_manager_->GetDetails()->clear();
}
TEST_F(SafeBrowsingBlockingPageTest,
PageWithMultipleMalwareResourceProceedThenDontProceed) {
Profile* profile = Profile::FromBrowserContext(
web_contents()->GetBrowserContext());
profile->GetPrefs()->SetBoolean(prefs::kSafeBrowsingReportingEnabled, true);
Navigate(kGoogleURL, 1);
Navigate(kGoodURL, 2);
ShowInterstitial(true, kBadURL);
ShowInterstitial(true, kBadURL2);
ShowInterstitial(true, kBadURL3);
SafeBrowsingBlockingPage* sb_interstitial = GetSafeBrowsingBlockingPage();
ASSERT_TRUE(sb_interstitial);
ProceedThroughInterstitial(sb_interstitial);
EXPECT_EQ(OK, user_response());
EXPECT_EQ(1u, ui_manager_->GetDetails()->size());
ui_manager_->GetDetails()->clear();
ResetUserResponse();
sb_interstitial = GetSafeBrowsingBlockingPage();
ASSERT_TRUE(sb_interstitial);
DontProceedThroughSubresourceInterstitial(sb_interstitial);
EXPECT_EQ(CANCEL, user_response());
EXPECT_FALSE(GetSafeBrowsingBlockingPage());
ASSERT_EQ(1, controller().GetEntryCount());
EXPECT_EQ(kGoogleURL, controller().GetActiveEntry()->GetURL().spec());
EXPECT_EQ(0u, ui_manager_->GetDetails()->size());
ui_manager_->GetDetails()->clear();
}
TEST_F(SafeBrowsingBlockingPageTest, PageWithMultipleMalwareResourceProceed) {
Profile* profile = Profile::FromBrowserContext(
web_contents()->GetBrowserContext());
profile->GetPrefs()->SetBoolean(prefs::kSafeBrowsingReportingEnabled, true);
Navigate(kGoodURL, 1);
ShowInterstitial(true, kBadURL);
ShowInterstitial(true, kBadURL2);
ShowInterstitial(true, kBadURL3);
SafeBrowsingBlockingPage* sb_interstitial = GetSafeBrowsingBlockingPage();
ASSERT_TRUE(sb_interstitial);
ProceedThroughInterstitial(sb_interstitial);
EXPECT_EQ(OK, user_response());
EXPECT_EQ(1u, ui_manager_->GetDetails()->size());
ui_manager_->GetDetails()->clear();
ResetUserResponse();
sb_interstitial = GetSafeBrowsingBlockingPage();
ASSERT_TRUE(sb_interstitial);
ProceedThroughInterstitial(sb_interstitial);
EXPECT_EQ(OK, user_response());
ASSERT_EQ(1, controller().GetEntryCount());
EXPECT_EQ(kGoodURL, controller().GetActiveEntry()->GetURL().spec());
EXPECT_EQ(0u, ui_manager_->GetDetails()->size());
ui_manager_->GetDetails()->clear();
}
TEST_F(SafeBrowsingBlockingPageTest, NavigatingBackAndForth) {
Profile* profile = Profile::FromBrowserContext(
web_contents()->GetBrowserContext());
profile->GetPrefs()->SetBoolean(prefs::kSafeBrowsingReportingEnabled, true);
Navigate(kGoodURL, 1);
controller().LoadURL(GURL(kBadURL), content::Referrer(),
content::PAGE_TRANSITION_TYPED, std::string());
ShowInterstitial(false, kBadURL);
SafeBrowsingBlockingPage* sb_interstitial = GetSafeBrowsingBlockingPage();
ASSERT_TRUE(sb_interstitial);
ProceedThroughInterstitial(sb_interstitial);
Navigate(kBadURL, 2);
GoBack(true);
sb_interstitial = GetSafeBrowsingBlockingPage();
ASSERT_FALSE(sb_interstitial);
ASSERT_EQ(2, controller().GetEntryCount());
EXPECT_EQ(kGoodURL, controller().GetActiveEntry()->GetURL().spec());
web_contents()->GetController().GoForward();
ShowInterstitial(false, kBadURL);
sb_interstitial = GetSafeBrowsingBlockingPage();
ASSERT_TRUE(sb_interstitial);
ProceedThroughInterstitial(sb_interstitial);
Navigate(kBadURL, 2);
sb_interstitial = GetSafeBrowsingBlockingPage();
ASSERT_FALSE(sb_interstitial);
ASSERT_EQ(2, controller().GetEntryCount());
EXPECT_EQ(kBadURL, controller().GetActiveEntry()->GetURL().spec());
EXPECT_EQ(2u, ui_manager_->GetDetails()->size());
ui_manager_->GetDetails()->clear();
}
TEST_F(SafeBrowsingBlockingPageTest, ProceedThenDontProceed) {
Profile* profile = Profile::FromBrowserContext(
web_contents()->GetBrowserContext());
profile->GetPrefs()->SetBoolean(prefs::kSafeBrowsingReportingEnabled, true);
controller().LoadURL(GURL(kBadURL), content::Referrer(),
content::PAGE_TRANSITION_TYPED, std::string());
ShowInterstitial(false, kBadURL);
SafeBrowsingBlockingPage* sb_interstitial = GetSafeBrowsingBlockingPage();
ASSERT_TRUE(sb_interstitial);
base::RunLoop().RunUntilIdle();
sb_interstitial->interstitial_page_->Proceed();
sb_interstitial->interstitial_page_->DontProceed();
base::RunLoop().RunUntilIdle();
EXPECT_EQ(OK, user_response());
EXPECT_FALSE(GetSafeBrowsingBlockingPage());
EXPECT_EQ(1u, ui_manager_->GetDetails()->size());
ui_manager_->GetDetails()->clear();
}
TEST_F(SafeBrowsingBlockingPageTest, MalwareReportsDisabled) {
Profile* profile = Profile::FromBrowserContext(
web_contents()->GetBrowserContext());
profile->GetPrefs()->SetBoolean(prefs::kSafeBrowsingReportingEnabled, false);
controller().LoadURL(GURL(kBadURL), content::Referrer(),
content::PAGE_TRANSITION_TYPED, std::string());
ShowInterstitial(false, kBadURL);
SafeBrowsingBlockingPage* sb_interstitial = GetSafeBrowsingBlockingPage();
ASSERT_TRUE(sb_interstitial);
base::RunLoop().RunUntilIdle();
DontProceedThroughInterstitial(sb_interstitial);
EXPECT_EQ(CANCEL, user_response());
EXPECT_FALSE(GetSafeBrowsingBlockingPage());
EXPECT_FALSE(controller().GetPendingEntry());
EXPECT_EQ(0u, ui_manager_->GetDetails()->size());
ui_manager_->GetDetails()->clear();
}
TEST_F(SafeBrowsingBlockingPageTest, MalwareReports) {
Profile* profile = Profile::FromBrowserContext(
web_contents()->GetBrowserContext());
profile->GetPrefs()->SetBoolean(prefs::kSafeBrowsingReportingEnabled, false);
controller().LoadURL(GURL(kBadURL), content::Referrer(),
content::PAGE_TRANSITION_TYPED, std::string());
ShowInterstitial(false, kBadURL);
SafeBrowsingBlockingPage* sb_interstitial = GetSafeBrowsingBlockingPage();
ASSERT_TRUE(sb_interstitial);
base::RunLoop().RunUntilIdle();
EXPECT_FALSE(profile->GetPrefs()->GetBoolean(
prefs::kSafeBrowsingReportingEnabled));
sb_interstitial->SetReportingPreference(true);
EXPECT_TRUE(profile->GetPrefs()->GetBoolean(
prefs::kSafeBrowsingReportingEnabled));
sb_interstitial->SetReportingPreference(false);
EXPECT_FALSE(profile->GetPrefs()->GetBoolean(
prefs::kSafeBrowsingReportingEnabled));
}