Delete the Safe Browsing V1 interstitial
[chromium-blink-merge.git] / chrome / browser / safe_browsing / safe_browsing_blocking_page_unittest.cc
blob582020d3eaab5e779f2939c08592958d4f2175cf
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
3 // found in the LICENSE file.
5 #include <list>
7 #include "base/prefs/pref_service.h"
8 #include "base/run_loop.h"
9 #include "chrome/browser/profiles/profile.h"
10 #include "chrome/browser/safe_browsing/malware_details.h"
11 #include "chrome/browser/safe_browsing/safe_browsing_blocking_page.h"
12 #include "chrome/browser/safe_browsing/safe_browsing_service.h"
13 #include "chrome/browser/safe_browsing/ui_manager.h"
14 #include "chrome/common/pref_names.h"
15 #include "chrome/test/base/chrome_render_view_host_test_harness.h"
16 #include "content/public/browser/interstitial_page.h"
17 #include "content/public/browser/navigation_entry.h"
18 #include "content/public/browser/render_process_host.h"
19 #include "content/public/browser/web_contents.h"
20 #include "content/public/test/web_contents_tester.h"
22 using content::InterstitialPage;
23 using content::NavigationEntry;
24 using content::WebContents;
25 using content::WebContentsTester;
27 static const char* kGoogleURL = "http://www.google.com/";
28 static const char* kGoodURL = "http://www.goodguys.com/";
29 static const char* kBadURL = "http://www.badguys.com/";
30 static const char* kBadURL2 = "http://www.badguys2.com/";
31 static const char* kBadURL3 = "http://www.badguys3.com/";
33 namespace {
35 // A SafeBrowingBlockingPage class that does not create windows.
36 class TestSafeBrowsingBlockingPageV2 : public SafeBrowsingBlockingPageV2 {
37 public:
38 TestSafeBrowsingBlockingPageV2(SafeBrowsingUIManager* manager,
39 WebContents* web_contents,
40 const UnsafeResourceList& unsafe_resources)
41 : SafeBrowsingBlockingPageV2(manager, web_contents, unsafe_resources) {
42 // Don't delay details at all for the unittest.
43 malware_details_proceed_delay_ms_ = 0;
45 // Don't create a view.
46 interstitial_page()->DontCreateViewForTesting();
50 // A SafeBrowingBlockingPage class that does not create windows.
51 class TestSafeBrowsingBlockingPageV3 : public SafeBrowsingBlockingPageV3 {
52 public:
53 TestSafeBrowsingBlockingPageV3(SafeBrowsingUIManager* manager,
54 WebContents* web_contents,
55 const UnsafeResourceList& unsafe_resources)
56 : SafeBrowsingBlockingPageV3(manager, web_contents, unsafe_resources) {
57 // Don't delay details at all for the unittest.
58 malware_details_proceed_delay_ms_ = 0;
60 // Don't create a view.
61 interstitial_page()->DontCreateViewForTesting();
65 class TestSafeBrowsingUIManager: public SafeBrowsingUIManager {
66 public:
67 explicit TestSafeBrowsingUIManager(SafeBrowsingService* service)
68 : SafeBrowsingUIManager(service) {
71 virtual void SendSerializedMalwareDetails(
72 const std::string& serialized) OVERRIDE {
73 details_.push_back(serialized);
76 std::list<std::string>* GetDetails() {
77 return &details_;
80 private:
81 virtual ~TestSafeBrowsingUIManager() {}
83 std::list<std::string> details_;
86 template <class SBInterstitialPage>
87 class TestSafeBrowsingBlockingPageFactory
88 : public SafeBrowsingBlockingPageFactory {
89 public:
90 TestSafeBrowsingBlockingPageFactory() { }
91 virtual ~TestSafeBrowsingBlockingPageFactory() { }
93 virtual SafeBrowsingBlockingPage* CreateSafeBrowsingPage(
94 SafeBrowsingUIManager* manager,
95 WebContents* web_contents,
96 const SafeBrowsingBlockingPage::UnsafeResourceList& unsafe_resources)
97 OVERRIDE {
98 // The V2 version doesn't handle multiple threats.
99 if (unsafe_resources.size() == 1 &&
100 (unsafe_resources[0].threat_type == SB_THREAT_TYPE_URL_MALWARE ||
101 unsafe_resources[0].threat_type == SB_THREAT_TYPE_URL_PHISHING)) {
102 return new SBInterstitialPage(manager, web_contents, unsafe_resources);
104 return new TestSafeBrowsingBlockingPageV3(manager, web_contents,
105 unsafe_resources);
109 } // namespace
111 template <class SBInterstitialPage>
112 class SafeBrowsingBlockingPageTest : public ChromeRenderViewHostTestHarness {
113 public:
114 // The decision the user made.
115 enum UserResponse {
116 PENDING,
118 CANCEL
121 SafeBrowsingBlockingPageTest() {
122 ResetUserResponse();
123 // The safe browsing UI manager does not need a service for this test.
124 ui_manager_ = new TestSafeBrowsingUIManager(NULL);
127 virtual void SetUp() OVERRIDE {
128 ChromeRenderViewHostTestHarness::SetUp();
129 SafeBrowsingBlockingPage::RegisterFactory(&factory_);
130 ResetUserResponse();
133 virtual void TearDown() OVERRIDE {
134 // Release the UI manager before the BrowserThreads are destroyed.
135 ui_manager_ = NULL;
136 SafeBrowsingBlockingPage::RegisterFactory(NULL);
137 // Clean up singleton reference (crbug.com/110594).
138 MalwareDetails::RegisterFactory(NULL);
139 ChromeRenderViewHostTestHarness::TearDown();
142 void OnBlockingPageComplete(bool proceed) {
143 if (proceed)
144 user_response_ = OK;
145 else
146 user_response_ = CANCEL;
149 void Navigate(const char* url, int page_id) {
150 WebContentsTester::For(web_contents())->TestDidNavigate(
151 web_contents()->GetRenderViewHost(), page_id, GURL(url),
152 content::PAGE_TRANSITION_TYPED);
155 void GoBack(bool is_cross_site) {
156 NavigationEntry* entry =
157 web_contents()->GetController().GetEntryAtOffset(-1);
158 ASSERT_TRUE(entry);
159 web_contents()->GetController().GoBack();
161 // The pending RVH should commit for cross-site navigations.
162 content::RenderViewHost* rvh = is_cross_site ?
163 WebContentsTester::For(web_contents())->GetPendingRenderViewHost() :
164 web_contents()->GetRenderViewHost();
165 WebContentsTester::For(web_contents())->TestDidNavigate(
166 rvh,
167 entry->GetPageID(),
168 GURL(entry->GetURL()),
169 content::PAGE_TRANSITION_TYPED);
172 void ShowInterstitial(bool is_subresource, const char* url) {
173 SafeBrowsingUIManager::UnsafeResource resource;
174 InitResource(&resource, is_subresource, GURL(url));
175 SafeBrowsingBlockingPage::ShowBlockingPage(ui_manager_.get(), resource);
178 // Returns the SafeBrowsingBlockingPage currently showing or NULL if none is
179 // showing.
180 SafeBrowsingBlockingPage* GetSafeBrowsingBlockingPage() {
181 InterstitialPage* interstitial =
182 InterstitialPage::GetInterstitialPage(web_contents());
183 if (!interstitial)
184 return NULL;
185 return static_cast<SafeBrowsingBlockingPage*>(
186 interstitial->GetDelegateForTesting());
189 UserResponse user_response() const { return user_response_; }
190 void ResetUserResponse() { user_response_ = PENDING; }
192 static void ProceedThroughInterstitial(
193 SafeBrowsingBlockingPage* sb_interstitial) {
194 sb_interstitial->interstitial_page_->Proceed();
195 // Proceed() posts a task to update the SafeBrowsingService::Client.
196 base::RunLoop().RunUntilIdle();
199 static void DontProceedThroughInterstitial(
200 SafeBrowsingBlockingPage* sb_interstitial) {
201 sb_interstitial->interstitial_page_->DontProceed();
202 // DontProceed() posts a task to update the SafeBrowsingService::Client.
203 base::RunLoop().RunUntilIdle();
206 void DontProceedThroughSubresourceInterstitial(
207 SafeBrowsingBlockingPage* sb_interstitial) {
208 // CommandReceived(kTakeMeBackCommand) does a back navigation for
209 // subresource interstitials.
210 GoBack(false);
211 // DontProceed() posts a task to update the SafeBrowsingService::Client.
212 base::RunLoop().RunUntilIdle();
215 scoped_refptr<TestSafeBrowsingUIManager> ui_manager_;
217 private:
218 void InitResource(SafeBrowsingUIManager::UnsafeResource* resource,
219 bool is_subresource,
220 const GURL& url) {
221 resource->callback =
222 base::Bind(&SafeBrowsingBlockingPageTest::OnBlockingPageComplete,
223 base::Unretained(this));
224 resource->url = url;
225 resource->is_subresource = is_subresource;
226 resource->threat_type = SB_THREAT_TYPE_URL_MALWARE;
227 resource->render_process_host_id =
228 web_contents()->GetRenderProcessHost()->GetID();
229 resource->render_view_id =
230 web_contents()->GetRenderViewHost()->GetRoutingID();
233 UserResponse user_response_;
234 TestSafeBrowsingBlockingPageFactory<SBInterstitialPage> factory_;
237 typedef ::testing::Types<TestSafeBrowsingBlockingPageV2,
238 TestSafeBrowsingBlockingPageV3> InterstitialTestTypes;
239 TYPED_TEST_CASE(SafeBrowsingBlockingPageTest, InterstitialTestTypes);
241 // Tests showing a blocking page for a malware page and not proceeding.
242 TYPED_TEST(SafeBrowsingBlockingPageTest, MalwarePageDontProceed) {
243 // Enable malware details.
244 Profile* profile = Profile::FromBrowserContext(
245 this->web_contents()->GetBrowserContext());
246 profile->GetPrefs()->SetBoolean(
247 prefs::kSafeBrowsingExtendedReportingEnabled, true);
249 // Start a load.
250 this->controller().LoadURL(GURL(kBadURL), content::Referrer(),
251 content::PAGE_TRANSITION_TYPED, std::string());
254 // Simulate the load causing a safe browsing interstitial to be shown.
255 this->ShowInterstitial(false, kBadURL);
256 SafeBrowsingBlockingPage* sb_interstitial =
257 this->GetSafeBrowsingBlockingPage();
258 ASSERT_TRUE(sb_interstitial);
260 base::RunLoop().RunUntilIdle();
262 // Simulate the user clicking "don't proceed".
263 this->DontProceedThroughInterstitial(sb_interstitial);
265 // The interstitial should be gone.
266 EXPECT_EQ(this->CANCEL, this->user_response());
267 EXPECT_FALSE(this->GetSafeBrowsingBlockingPage());
269 // We did not proceed, the pending entry should be gone.
270 EXPECT_FALSE(this->controller().GetPendingEntry());
272 // A report should have been sent.
273 EXPECT_EQ(1u, this->ui_manager_->GetDetails()->size());
274 this->ui_manager_->GetDetails()->clear();
277 // Tests showing a blocking page for a malware page and then proceeding.
278 TYPED_TEST(SafeBrowsingBlockingPageTest, MalwarePageProceed) {
279 // Enable malware reports.
280 Profile* profile = Profile::FromBrowserContext(
281 this->web_contents()->GetBrowserContext());
282 profile->GetPrefs()->SetBoolean(
283 prefs::kSafeBrowsingExtendedReportingEnabled, true);
285 // Start a load.
286 this->controller().LoadURL(GURL(kBadURL), content::Referrer(),
287 content::PAGE_TRANSITION_TYPED, std::string());
289 // Simulate the load causing a safe browsing interstitial to be shown.
290 this->ShowInterstitial(false, kBadURL);
291 SafeBrowsingBlockingPage* sb_interstitial =
292 this->GetSafeBrowsingBlockingPage();
293 ASSERT_TRUE(sb_interstitial);
295 // Simulate the user clicking "proceed".
296 this->ProceedThroughInterstitial(sb_interstitial);
298 // The interstitial is shown until the navigation commits.
299 ASSERT_TRUE(InterstitialPage::GetInterstitialPage(this->web_contents()));
300 // Commit the navigation.
301 this->Navigate(kBadURL, 1);
302 // The interstitial should be gone now.
303 ASSERT_FALSE(InterstitialPage::GetInterstitialPage(this->web_contents()));
305 // A report should have been sent.
306 EXPECT_EQ(1u, this->ui_manager_->GetDetails()->size());
307 this->ui_manager_->GetDetails()->clear();
310 // Tests showing a blocking page for a page that contains malware subresources
311 // and not proceeding.
312 TYPED_TEST(SafeBrowsingBlockingPageTest, PageWithMalwareResourceDontProceed) {
313 // Enable malware reports.
314 Profile* profile = Profile::FromBrowserContext(
315 this->web_contents()->GetBrowserContext());
316 profile->GetPrefs()->SetBoolean(
317 prefs::kSafeBrowsingExtendedReportingEnabled, true);
319 // Navigate somewhere.
320 this->Navigate(kGoogleURL, 1);
322 // Navigate somewhere else.
323 this->Navigate(kGoodURL, 2);
325 // Simulate that page loading a bad-resource triggering an interstitial.
326 this->ShowInterstitial(true, kBadURL);
328 SafeBrowsingBlockingPage* sb_interstitial =
329 this->GetSafeBrowsingBlockingPage();
330 ASSERT_TRUE(sb_interstitial);
332 // Simulate the user clicking "don't proceed".
333 this->DontProceedThroughSubresourceInterstitial(sb_interstitial);
334 EXPECT_EQ(this->CANCEL, this->user_response());
335 EXPECT_FALSE(this->GetSafeBrowsingBlockingPage());
337 // We did not proceed, we should be back to the first page, the 2nd one should
338 // have been removed from the navigation controller.
339 ASSERT_EQ(1, this->controller().GetEntryCount());
340 EXPECT_EQ(kGoogleURL, this->controller().GetActiveEntry()->GetURL().spec());
342 // A report should have been sent.
343 EXPECT_EQ(1u, this->ui_manager_->GetDetails()->size());
344 this->ui_manager_->GetDetails()->clear();
347 // Tests showing a blocking page for a page that contains malware subresources
348 // and proceeding.
349 TYPED_TEST(SafeBrowsingBlockingPageTest, PageWithMalwareResourceProceed) {
350 // Enable malware reports.
351 Profile* profile = Profile::FromBrowserContext(
352 this->web_contents()->GetBrowserContext());
353 profile->GetPrefs()->SetBoolean(
354 prefs::kSafeBrowsingExtendedReportingEnabled, true);
356 // Navigate somewhere.
357 this->Navigate(kGoodURL, 1);
359 // Simulate that page loading a bad-resource triggering an interstitial.
360 this->ShowInterstitial(true, kBadURL);
362 SafeBrowsingBlockingPage* sb_interstitial =
363 this->GetSafeBrowsingBlockingPage();
364 ASSERT_TRUE(sb_interstitial);
366 // Simulate the user clicking "proceed".
367 this->ProceedThroughInterstitial(sb_interstitial);
368 EXPECT_EQ(this->OK, this->user_response());
369 EXPECT_FALSE(this->GetSafeBrowsingBlockingPage());
371 // We did proceed, we should be back to showing the page.
372 ASSERT_EQ(1, this->controller().GetEntryCount());
373 EXPECT_EQ(kGoodURL, this->controller().GetActiveEntry()->GetURL().spec());
375 // A report should have been sent.
376 EXPECT_EQ(1u, this->ui_manager_->GetDetails()->size());
377 this->ui_manager_->GetDetails()->clear();
380 // Tests showing a blocking page for a page that contains multiple malware
381 // subresources and not proceeding. This just tests that the extra malware
382 // subresources (which trigger queued interstitial pages) do not break anything.
383 TYPED_TEST(SafeBrowsingBlockingPageTest,
384 PageWithMultipleMalwareResourceDontProceed) {
385 // Enable malware reports.
386 Profile* profile = Profile::FromBrowserContext(
387 this->web_contents()->GetBrowserContext());
388 profile->GetPrefs()->SetBoolean(
389 prefs::kSafeBrowsingExtendedReportingEnabled, true);
391 // Navigate somewhere.
392 this->Navigate(kGoogleURL, 1);
394 // Navigate somewhere else.
395 this->Navigate(kGoodURL, 2);
397 // Simulate that page loading a bad-resource triggering an interstitial.
398 this->ShowInterstitial(true, kBadURL);
400 // More bad resources loading causing more interstitials. The new
401 // interstitials should be queued.
402 this->ShowInterstitial(true, kBadURL2);
403 this->ShowInterstitial(true, kBadURL3);
405 SafeBrowsingBlockingPage* sb_interstitial =
406 this->GetSafeBrowsingBlockingPage();
407 ASSERT_TRUE(sb_interstitial);
409 // Simulate the user clicking "don't proceed".
410 this->DontProceedThroughSubresourceInterstitial(sb_interstitial);
411 EXPECT_EQ(this->CANCEL, this->user_response());
412 EXPECT_FALSE(this->GetSafeBrowsingBlockingPage());
414 // We did not proceed, we should be back to the first page, the 2nd one should
415 // have been removed from the navigation controller.
416 ASSERT_EQ(1, this->controller().GetEntryCount());
417 EXPECT_EQ(kGoogleURL, this->controller().GetActiveEntry()->GetURL().spec());
419 // A report should have been sent.
420 EXPECT_EQ(1u, this->ui_manager_->GetDetails()->size());
421 this->ui_manager_->GetDetails()->clear();
424 // Tests showing a blocking page for a page that contains multiple malware
425 // subresources and proceeding through the first interstitial, but not the next.
426 TYPED_TEST(SafeBrowsingBlockingPageTest,
427 PageWithMultipleMalwareResourceProceedThenDontProceed) {
428 // Enable malware reports.
429 Profile* profile = Profile::FromBrowserContext(
430 this->web_contents()->GetBrowserContext());
431 profile->GetPrefs()->SetBoolean(
432 prefs::kSafeBrowsingExtendedReportingEnabled, true);
434 // Navigate somewhere.
435 this->Navigate(kGoogleURL, 1);
437 // Navigate somewhere else.
438 this->Navigate(kGoodURL, 2);
440 // Simulate that page loading a bad-resource triggering an interstitial.
441 this->ShowInterstitial(true, kBadURL);
443 // More bad resources loading causing more interstitials. The new
444 // interstitials should be queued.
445 this->ShowInterstitial(true, kBadURL2);
446 this->ShowInterstitial(true, kBadURL3);
448 SafeBrowsingBlockingPage* sb_interstitial =
449 this->GetSafeBrowsingBlockingPage();
450 ASSERT_TRUE(sb_interstitial);
452 // Proceed through the 1st interstitial.
453 this->ProceedThroughInterstitial(sb_interstitial);
454 EXPECT_EQ(this->OK, this->user_response());
456 // A report should have been sent.
457 EXPECT_EQ(1u, this->ui_manager_->GetDetails()->size());
458 this->ui_manager_->GetDetails()->clear();
460 this->ResetUserResponse();
462 // We should land to a 2nd interstitial (aggregating all the malware resources
463 // loaded while the 1st interstitial was showing).
464 sb_interstitial = this->GetSafeBrowsingBlockingPage();
465 ASSERT_TRUE(sb_interstitial);
467 // Don't proceed through the 2nd interstitial.
468 this->DontProceedThroughSubresourceInterstitial(sb_interstitial);
469 EXPECT_EQ(this->CANCEL, this->user_response());
470 EXPECT_FALSE(this->GetSafeBrowsingBlockingPage());
472 // We did not proceed, we should be back to the first page, the 2nd one should
473 // have been removed from the navigation controller.
474 ASSERT_EQ(1, this->controller().GetEntryCount());
475 EXPECT_EQ(kGoogleURL, this->controller().GetActiveEntry()->GetURL().spec());
477 // No report should have been sent -- we don't create a report the
478 // second time.
479 EXPECT_EQ(0u, this->ui_manager_->GetDetails()->size());
480 this->ui_manager_->GetDetails()->clear();
483 // Tests showing a blocking page for a page that contains multiple malware
484 // subresources and proceeding through the multiple interstitials.
485 TYPED_TEST(SafeBrowsingBlockingPageTest,
486 PageWithMultipleMalwareResourceProceed) {
487 // Enable malware reports.
488 Profile* profile = Profile::FromBrowserContext(
489 this->web_contents()->GetBrowserContext());
490 profile->GetPrefs()->SetBoolean(
491 prefs::kSafeBrowsingExtendedReportingEnabled, true);
493 // Navigate somewhere else.
494 this->Navigate(kGoodURL, 1);
496 // Simulate that page loading a bad-resource triggering an interstitial.
497 this->ShowInterstitial(true, kBadURL);
499 // More bad resources loading causing more interstitials. The new
500 // interstitials should be queued.
501 this->ShowInterstitial(true, kBadURL2);
502 this->ShowInterstitial(true, kBadURL3);
504 SafeBrowsingBlockingPage* sb_interstitial =
505 this->GetSafeBrowsingBlockingPage();
506 ASSERT_TRUE(sb_interstitial);
508 // Proceed through the 1st interstitial.
509 this->ProceedThroughInterstitial(sb_interstitial);
510 EXPECT_EQ(this->OK, this->user_response());
512 // A report should have been sent.
513 EXPECT_EQ(1u, this->ui_manager_->GetDetails()->size());
514 this->ui_manager_->GetDetails()->clear();
516 this->ResetUserResponse();
518 // We should land to a 2nd interstitial (aggregating all the malware resources
519 // loaded while the 1st interstitial was showing).
520 sb_interstitial = this->GetSafeBrowsingBlockingPage();
521 ASSERT_TRUE(sb_interstitial);
523 // Proceed through the 2nd interstitial.
524 this->ProceedThroughInterstitial(sb_interstitial);
525 EXPECT_EQ(this->OK, this->user_response());
527 // We did proceed, we should be back to the initial page.
528 ASSERT_EQ(1, this->controller().GetEntryCount());
529 EXPECT_EQ(kGoodURL, this->controller().GetActiveEntry()->GetURL().spec());
531 // No report should have been sent -- we don't create a report the
532 // second time.
533 EXPECT_EQ(0u, this->ui_manager_->GetDetails()->size());
534 this->ui_manager_->GetDetails()->clear();
537 // Tests showing a blocking page then navigating back and forth to make sure the
538 // controller entries are OK. http://crbug.com/17627
539 TYPED_TEST(SafeBrowsingBlockingPageTest, NavigatingBackAndForth) {
540 // Enable malware reports.
541 Profile* profile = Profile::FromBrowserContext(
542 this->web_contents()->GetBrowserContext());
543 profile->GetPrefs()->SetBoolean(
544 prefs::kSafeBrowsingExtendedReportingEnabled, true);
546 // Navigate somewhere.
547 this->Navigate(kGoodURL, 1);
549 // Now navigate to a bad page triggerring an interstitial.
550 this->controller().LoadURL(GURL(kBadURL), content::Referrer(),
551 content::PAGE_TRANSITION_TYPED, std::string());
552 this->ShowInterstitial(false, kBadURL);
553 SafeBrowsingBlockingPage* sb_interstitial =
554 this->GetSafeBrowsingBlockingPage();
555 ASSERT_TRUE(sb_interstitial);
557 // Proceed, then navigate back.
558 this->ProceedThroughInterstitial(sb_interstitial);
559 this->Navigate(kBadURL, 2); // Commit the navigation.
560 this->GoBack(true);
562 // We are back on the good page.
563 sb_interstitial = this->GetSafeBrowsingBlockingPage();
564 ASSERT_FALSE(sb_interstitial);
565 ASSERT_EQ(2, this->controller().GetEntryCount());
566 EXPECT_EQ(kGoodURL, this->controller().GetActiveEntry()->GetURL().spec());
568 // Navigate forward to the malware URL.
569 this->web_contents()->GetController().GoForward();
570 this->ShowInterstitial(false, kBadURL);
571 sb_interstitial = this->GetSafeBrowsingBlockingPage();
572 ASSERT_TRUE(sb_interstitial);
574 // Let's proceed and make sure everything is OK (bug 17627).
575 this->ProceedThroughInterstitial(sb_interstitial);
576 this->Navigate(kBadURL, 2); // Commit the navigation.
577 sb_interstitial = this->GetSafeBrowsingBlockingPage();
578 ASSERT_FALSE(sb_interstitial);
579 ASSERT_EQ(2, this->controller().GetEntryCount());
580 EXPECT_EQ(kBadURL, this->controller().GetActiveEntry()->GetURL().spec());
582 // Two reports should have been sent.
583 EXPECT_EQ(2u, this->ui_manager_->GetDetails()->size());
584 this->ui_manager_->GetDetails()->clear();
587 // Tests that calling "don't proceed" after "proceed" has been called doesn't
588 // cause problems. http://crbug.com/30079
589 TYPED_TEST(SafeBrowsingBlockingPageTest, ProceedThenDontProceed) {
590 // Enable malware reports.
591 Profile* profile = Profile::FromBrowserContext(
592 this->web_contents()->GetBrowserContext());
593 profile->GetPrefs()->SetBoolean(
594 prefs::kSafeBrowsingExtendedReportingEnabled, true);
596 // Start a load.
597 this->controller().LoadURL(GURL(kBadURL), content::Referrer(),
598 content::PAGE_TRANSITION_TYPED, std::string());
600 // Simulate the load causing a safe browsing interstitial to be shown.
601 this->ShowInterstitial(false, kBadURL);
602 SafeBrowsingBlockingPage* sb_interstitial =
603 this->GetSafeBrowsingBlockingPage();
604 ASSERT_TRUE(sb_interstitial);
606 base::RunLoop().RunUntilIdle();
608 // Simulate the user clicking "proceed" then "don't proceed" (before the
609 // interstitial is shown).
610 sb_interstitial->interstitial_page_->Proceed();
611 sb_interstitial->interstitial_page_->DontProceed();
612 // Proceed() and DontProceed() post a task to update the
613 // SafeBrowsingService::Client.
614 base::RunLoop().RunUntilIdle();
616 // The interstitial should be gone.
617 EXPECT_EQ(this->OK, this->user_response());
618 EXPECT_FALSE(this->GetSafeBrowsingBlockingPage());
620 // Only one report should have been sent.
621 EXPECT_EQ(1u, this->ui_manager_->GetDetails()->size());
622 this->ui_manager_->GetDetails()->clear();
625 // Tests showing a blocking page for a malware page with reports disabled.
626 TYPED_TEST(SafeBrowsingBlockingPageTest, MalwareReportsDisabled) {
627 // Disable malware reports.
628 Profile* profile = Profile::FromBrowserContext(
629 this->web_contents()->GetBrowserContext());
630 profile->GetPrefs()->SetBoolean(
631 prefs::kSafeBrowsingExtendedReportingEnabled, false);
633 // Start a load.
634 this->controller().LoadURL(GURL(kBadURL), content::Referrer(),
635 content::PAGE_TRANSITION_TYPED, std::string());
637 // Simulate the load causing a safe browsing interstitial to be shown.
638 this->ShowInterstitial(false, kBadURL);
639 SafeBrowsingBlockingPage* sb_interstitial =
640 this->GetSafeBrowsingBlockingPage();
641 ASSERT_TRUE(sb_interstitial);
643 base::RunLoop().RunUntilIdle();
645 // Simulate the user clicking "don't proceed".
646 this->DontProceedThroughInterstitial(sb_interstitial);
648 // The interstitial should be gone.
649 EXPECT_EQ(this->CANCEL, this->user_response());
650 EXPECT_FALSE(this->GetSafeBrowsingBlockingPage());
652 // We did not proceed, the pending entry should be gone.
653 EXPECT_FALSE(this->controller().GetPendingEntry());
655 // No report should have been sent.
656 EXPECT_EQ(0u, this->ui_manager_->GetDetails()->size());
657 this->ui_manager_->GetDetails()->clear();
660 // Test that toggling the checkbox has the anticipated effects.
661 TYPED_TEST(SafeBrowsingBlockingPageTest, MalwareReportsToggling) {
662 // Disable malware reports.
663 Profile* profile = Profile::FromBrowserContext(
664 this->web_contents()->GetBrowserContext());
665 profile->GetPrefs()->SetBoolean(
666 prefs::kSafeBrowsingExtendedReportingEnabled, false);
668 // Start a load.
669 this->controller().LoadURL(GURL(kBadURL), content::Referrer(),
670 content::PAGE_TRANSITION_TYPED, std::string());
672 // Simulate the load causing a safe browsing interstitial to be shown.
673 this->ShowInterstitial(false, kBadURL);
674 SafeBrowsingBlockingPage* sb_interstitial =
675 this->GetSafeBrowsingBlockingPage();
676 ASSERT_TRUE(sb_interstitial);
678 base::RunLoop().RunUntilIdle();
680 EXPECT_FALSE(profile->GetPrefs()->GetBoolean(
681 prefs::kSafeBrowsingExtendedReportingEnabled));
683 // Simulate the user check the report agreement checkbox.
684 sb_interstitial->SetReportingPreference(true);
686 EXPECT_TRUE(profile->GetPrefs()->GetBoolean(
687 prefs::kSafeBrowsingExtendedReportingEnabled));
689 // Simulate the user uncheck the report agreement checkbox.
690 sb_interstitial->SetReportingPreference(false);
692 EXPECT_FALSE(profile->GetPrefs()->GetBoolean(
693 prefs::kSafeBrowsingExtendedReportingEnabled));
696 // Test that the transition from old to new preference works.
697 TYPED_TEST(SafeBrowsingBlockingPageTest, MalwareReportsTransitionEnabled) {
698 // The old pref is enabled.
699 Profile* profile = Profile::FromBrowserContext(
700 this->web_contents()->GetBrowserContext());
701 profile->GetPrefs()->SetBoolean(prefs::kSafeBrowsingReportingEnabled, true);
703 // Start a load.
704 this->controller().LoadURL(GURL(kBadURL), content::Referrer(),
705 content::PAGE_TRANSITION_TYPED, std::string());
707 // Simulate the load causing a safe browsing interstitial to be shown.
708 this->ShowInterstitial(false, kBadURL);
709 SafeBrowsingBlockingPage* sb_interstitial =
710 this->GetSafeBrowsingBlockingPage();
711 ASSERT_TRUE(sb_interstitial);
713 base::RunLoop().RunUntilIdle();
715 // At this point, nothing should have changed yet.
716 EXPECT_FALSE(profile->GetPrefs()->GetBoolean(
717 prefs::kSafeBrowsingExtendedReportingEnabled));
719 this->ProceedThroughInterstitial(sb_interstitial);
721 // Since the user has proceeded without changing the checkbox, the new pref
722 // has been updated.
723 EXPECT_TRUE(profile->GetPrefs()->GetBoolean(
724 prefs::kSafeBrowsingExtendedReportingEnabled));
727 // Test that the transition from old to new preference still respects the
728 // user's checkbox preferences.
729 TYPED_TEST(SafeBrowsingBlockingPageTest, MalwareReportsTransitionDisabled) {
730 // The old pref is enabled.
731 Profile* profile = Profile::FromBrowserContext(
732 this->web_contents()->GetBrowserContext());
733 profile->GetPrefs()->SetBoolean(prefs::kSafeBrowsingReportingEnabled, true);
735 // Start a load.
736 this->controller().LoadURL(GURL(kBadURL), content::Referrer(),
737 content::PAGE_TRANSITION_TYPED, std::string());
739 // Simulate the load causing a safe browsing interstitial to be shown.
740 this->ShowInterstitial(false, kBadURL);
741 SafeBrowsingBlockingPage* sb_interstitial =
742 this->GetSafeBrowsingBlockingPage();
743 ASSERT_TRUE(sb_interstitial);
745 base::RunLoop().RunUntilIdle();
747 // At this point, nothing should have changed yet.
748 EXPECT_FALSE(profile->GetPrefs()->GetBoolean(
749 prefs::kSafeBrowsingExtendedReportingEnabled));
751 // Simulate the user uncheck the report agreement checkbox.
752 sb_interstitial->SetReportingPreference(false);
754 this->ProceedThroughInterstitial(sb_interstitial);
756 // The new pref is turned off.
757 EXPECT_FALSE(profile->GetPrefs()->GetBoolean(
758 prefs::kSafeBrowsingExtendedReportingEnabled));
759 EXPECT_FALSE(profile->GetPrefs()->GetBoolean(
760 prefs::kSafeBrowsingReportingEnabled));
761 EXPECT_FALSE(profile->GetPrefs()->GetBoolean(
762 prefs::kSafeBrowsingDownloadFeedbackEnabled));