Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(289)

Side by Side Diff: chrome/browser/chrome_content_browser_client_unittest.cc

Issue 2025683003: First experimental implementation of the Clear-Site-Data header (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: REMOVE_PLUGIN_DATA for eTLD+1 Created 4 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2013 The Chromium Authors. All rights reserved. 1 // Copyright 2013 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 "chrome/browser/chrome_content_browser_client.h" 5 #include "chrome/browser/chrome_content_browser_client.h"
6 6
7 #include <list>
7 #include <map> 8 #include <map>
9 #include <memory>
8 10
11 #include "base/bind.h"
9 #include "base/command_line.h" 12 #include "base/command_line.h"
10 #include "base/macros.h" 13 #include "base/macros.h"
14 #include "base/memory/ptr_util.h"
15 #include "base/message_loop/message_loop.h"
11 #include "base/metrics/field_trial.h" 16 #include "base/metrics/field_trial.h"
17 #include "base/strings/stringprintf.h"
12 #include "base/strings/utf_string_conversions.h" 18 #include "base/strings/utf_string_conversions.h"
13 #include "build/build_config.h" 19 #include "build/build_config.h"
20 #include "chrome/browser/browsing_data/browsing_data_filter_builder.h"
21 #include "chrome/browser/browsing_data/browsing_data_helper.h"
22 #include "chrome/browser/browsing_data/browsing_data_remover.h"
23 #include "chrome/browser/browsing_data/browsing_data_remover_factory.h"
24 #include "chrome/browser/browsing_data/origin_filter_builder.h"
25 #include "chrome/browser/browsing_data/registrable_domain_filter_builder.h"
14 #include "chrome/browser/search_engines/template_url_service_factory.h" 26 #include "chrome/browser/search_engines/template_url_service_factory.h"
27 #include "chrome/test/base/testing_profile.h"
15 #include "components/content_settings/core/browser/host_content_settings_map.h" 28 #include "components/content_settings/core/browser/host_content_settings_map.h"
16 #include "components/search_engines/template_url_service.h" 29 #include "components/search_engines/template_url_service.h"
17 #include "components/variations/entropy_provider.h" 30 #include "components/variations/entropy_provider.h"
18 #include "components/variations/variations_associated_data.h" 31 #include "components/variations/variations_associated_data.h"
19 #include "components/version_info/version_info.h" 32 #include "components/version_info/version_info.h"
20 #include "content/public/browser/navigation_controller.h" 33 #include "content/public/browser/navigation_controller.h"
21 #include "content/public/browser/navigation_entry.h" 34 #include "content/public/browser/navigation_entry.h"
22 #include "content/public/browser/web_contents.h" 35 #include "content/public/browser/web_contents.h"
23 #include "content/public/common/content_switches.h" 36 #include "content/public/common/content_switches.h"
24 #include "content/public/test/test_browser_thread_bundle.h" 37 #include "content/public/test/test_browser_thread_bundle.h"
38 #include "testing/gmock/include/gmock/gmock.h"
25 #include "testing/gtest/include/gtest/gtest.h" 39 #include "testing/gtest/include/gtest/gtest.h"
26 #include "url/gurl.h" 40 #include "url/gurl.h"
27 41
28 #if !defined(OS_ANDROID) 42 #if !defined(OS_ANDROID)
29 #include "chrome/browser/ui/browser.h" 43 #include "chrome/browser/ui/browser.h"
30 #include "chrome/browser/ui/tabs/tab_strip_model.h" 44 #include "chrome/browser/ui/tabs/tab_strip_model.h"
31 #include "chrome/common/pref_names.h" 45 #include "chrome/common/pref_names.h"
32 #include "chrome/test/base/browser_with_test_window_test.h" 46 #include "chrome/test/base/browser_with_test_window_test.h"
33 #include "chrome/test/base/search_test_utils.h" 47 #include "chrome/test/base/search_test_utils.h"
34 #endif 48 #endif
35 49
50 using testing::_;
36 using ChromeContentBrowserClientTest = testing::Test; 51 using ChromeContentBrowserClientTest = testing::Test;
37 52
38 TEST_F(ChromeContentBrowserClientTest, ShouldAssignSiteForURL) { 53 TEST_F(ChromeContentBrowserClientTest, ShouldAssignSiteForURL) {
39 ChromeContentBrowserClient client; 54 ChromeContentBrowserClient client;
40 EXPECT_FALSE(client.ShouldAssignSiteForURL(GURL("chrome-native://test"))); 55 EXPECT_FALSE(client.ShouldAssignSiteForURL(GURL("chrome-native://test")));
41 EXPECT_TRUE(client.ShouldAssignSiteForURL(GURL("http://www.google.com"))); 56 EXPECT_TRUE(client.ShouldAssignSiteForURL(GURL("http://www.google.com")));
42 EXPECT_TRUE(client.ShouldAssignSiteForURL(GURL("https://www.google.com"))); 57 EXPECT_TRUE(client.ShouldAssignSiteForURL(GURL("https://www.google.com")));
43 } 58 }
44 59
45 // BrowserWithTestWindowTest doesn't work on Android. 60 // BrowserWithTestWindowTest doesn't work on Android.
(...skipping 275 matching lines...) Expand 10 before | Expand all | Expand 10 after
321 336
322 NavigationEntry* entry = browser()->tab_strip_model()-> 337 NavigationEntry* entry = browser()->tab_strip_model()->
323 GetActiveWebContents()->GetController().GetLastCommittedEntry(); 338 GetActiveWebContents()->GetController().GetLastCommittedEntry();
324 ASSERT_TRUE(entry != NULL); 339 ASSERT_TRUE(entry != NULL);
325 EXPECT_EQ(url_rewritten, entry->GetURL()); 340 EXPECT_EQ(url_rewritten, entry->GetURL());
326 EXPECT_EQ(url_original, entry->GetVirtualURL()); 341 EXPECT_EQ(url_original, entry->GetVirtualURL());
327 } 342 }
328 343
329 } // namespace content 344 } // namespace content
330 #endif // !defined(OS_ANDROID) 345 #endif // !defined(OS_ANDROID)
346
347 namespace {
348
349 // A BrowsingDataRemover that only records calls.
350 class MockBrowsingDataRemover : public BrowsingDataRemover {
351 public:
352 explicit MockBrowsingDataRemover(content::BrowserContext* context)
353 : BrowsingDataRemover(context) {}
354
355 ~MockBrowsingDataRemover() override {
356 DCHECK(!expected_calls_.size())
357 << "Expectations were set but not verified.";
358 }
359
360 void RemoveInternal(const TimeRange& time_range,
361 int remove_mask,
362 int origin_type_mask,
363 std::unique_ptr<BrowsingDataFilterBuilder> filter_builder,
364 BrowsingDataRemover::Observer* observer) override {
365 actual_calls_.emplace_back(time_range, remove_mask, origin_type_mask,
366 std::move(filter_builder), UNKNOWN);
367
368 // |observer| is not recorded in |actual_calls_| to be compared with
369 // expectations, because it's created internally in ClearSiteData() and
370 // it's unknown to this. However, it is tested implicitly, because we use
371 // it for the completion callback, so an incorrect |observer| will fail
372 // the test by waiting for the callback forever.
373 DCHECK(observer);
374 observer->OnBrowsingDataRemoverDone();
375 }
376
377 void ExpectCall(
378 const TimeRange& time_range,
379 int remove_mask,
380 int origin_type_mask,
381 std::unique_ptr<RegistrableDomainFilterBuilder> filter_builder) {
382 expected_calls_.emplace_back(time_range, remove_mask, origin_type_mask,
383 std::move(filter_builder),
384 REGISTRABLE_DOMAIN_FILTER_BUILDER);
385 }
386
387 void ExpectCall(const TimeRange& time_range,
388 int remove_mask,
389 int origin_type_mask,
390 std::unique_ptr<OriginFilterBuilder> filter_builder) {
391 expected_calls_.emplace_back(time_range, remove_mask, origin_type_mask,
392 std::move(filter_builder),
393 ORIGIN_FILTER_BUILDER);
394 }
395
396 void ExpectCallDontCareAboutFilterBuilder(const TimeRange& time_range,
397 int remove_mask,
398 int origin_type_mask) {
399 expected_calls_.emplace_back(time_range, remove_mask, origin_type_mask,
400 std::unique_ptr<BrowsingDataFilterBuilder>(),
401 DONT_CARE);
402 }
403
404 void VerifyAndClearExpectations() {
405 EXPECT_EQ(expected_calls_, actual_calls_);
406 expected_calls_.clear();
407 actual_calls_.clear();
408 }
409
410 private:
411 // Used to further specify the type and intention behind the passed
412 // std::unique_ptr<BrowsingDataFilterBuilder>. This is needed for comparison
413 // between the expected and actual call parameters.
414 enum FilterBuilderType {
415 REGISTRABLE_DOMAIN_FILTER_BUILDER, // RegistrableDomainFilterBuilder
416 ORIGIN_FILTER_BUILDER, // OriginFilterBuilder
417 UNKNOWN, // can't static_cast<>
418 DONT_CARE // don't have to compare for equality
419 };
420
421 class CallParameters {
422 public:
423 CallParameters(const TimeRange& time_range,
424 int remove_mask,
425 int origin_type_mask,
426 std::unique_ptr<BrowsingDataFilterBuilder> filter_builder,
427 FilterBuilderType type)
428 : time_range_(time_range),
429 remove_mask_(remove_mask),
430 origin_type_mask_(origin_type_mask),
431 filter_builder_(std::move(filter_builder)),
432 type_(type) {}
433 ~CallParameters() {}
434
435 bool operator==(const CallParameters& other) const {
436 const CallParameters& a = *this;
437 const CallParameters& b = other;
438
439 if (!(a.time_range_ == b.time_range_) ||
440 a.remove_mask_ != b.remove_mask_ ||
441 a.origin_type_mask_ != b.origin_type_mask_) {
442 return false;
443 }
444
445 if (a.type_ == DONT_CARE || b.type_ == DONT_CARE)
446 return true;
447 if (a.type_ == UNKNOWN && b.type_ == UNKNOWN)
448 return false;
449 if (a.type_ != UNKNOWN && b.type_ != UNKNOWN && a.type_ != b.type_)
450 return false;
451
452 FilterBuilderType resolved_type =
453 (a.type_ != UNKNOWN) ? a.type_ : b.type_;
454
455 DCHECK(resolved_type == ORIGIN_FILTER_BUILDER ||
456 resolved_type == REGISTRABLE_DOMAIN_FILTER_BUILDER);
457
458 if (resolved_type == ORIGIN_FILTER_BUILDER) {
459 return *static_cast<OriginFilterBuilder*>(a.filter_builder_.get()) ==
460 *static_cast<OriginFilterBuilder*>(b.filter_builder_.get());
461 } else if (resolved_type == REGISTRABLE_DOMAIN_FILTER_BUILDER) {
462 return *static_cast<RegistrableDomainFilterBuilder*>(
463 a.filter_builder_.get()) ==
464 *static_cast<RegistrableDomainFilterBuilder*>(
465 b.filter_builder_.get());
466 }
467
468 NOTREACHED();
469 return false;
470 }
471
472 private:
473 TimeRange time_range_;
474 int remove_mask_;
475 int origin_type_mask_;
476 std::unique_ptr<BrowsingDataFilterBuilder> filter_builder_;
477 FilterBuilderType type_;
478 };
479
480 std::list<CallParameters> actual_calls_;
481 std::list<CallParameters> expected_calls_;
482 };
483
484 // Tests for ChromeContentBrowserClient::ClearSiteData().
485 class ChromeContentBrowserClientClearSiteDataTest : public testing::Test {
486 public:
487 void SetUp() override {
488 BrowsingDataRemoverFactory::GetInstance()->SetTestingFactoryAndUse(
489 &profile_, &ChromeContentBrowserClientClearSiteDataTest::GetRemover);
490 }
491
492 content::BrowserContext* profile() { return &profile_; }
493
494 MockBrowsingDataRemover* remover() {
495 return static_cast<MockBrowsingDataRemover*>(
496 BrowsingDataRemoverFactory::GetForBrowserContext(&profile_));
497 }
498
499 void SetClearingFinished(bool finished) { finished_ = finished; }
500
501 bool IsClearingFinished() { return finished_; }
502
503 private:
504 static std::unique_ptr<KeyedService> GetRemover(
505 content::BrowserContext* context) {
506 return base::WrapUnique(new MockBrowsingDataRemover(context));
507 }
508
509 base::MessageLoop loop_;
510 TestingProfile profile_;
511 bool finished_;
512 };
513
514 // Tests that the parameters to ClearBrowsingData() are translated to
515 // the correct BrowsingDataRemover::RemoveInternal() operation. The fourth
516 // parameter, |filter_builder|, is tested in detail in the RegistrableDomains
517 // test below.
518 TEST_F(ChromeContentBrowserClientClearSiteDataTest, Parameters) {
519 ChromeContentBrowserClient client;
520
521 struct TestCase {
522 bool cookies;
523 bool storage;
524 bool cache;
525 int mask;
526 } test_cases[] = {
527 {false, false, false, 0},
528 {true, false, false, BrowsingDataRemover::REMOVE_COOKIES |
529 BrowsingDataRemover::REMOVE_CHANNEL_IDS |
530 BrowsingDataRemover::REMOVE_PLUGIN_DATA},
531 {false, true, false, BrowsingDataRemover::REMOVE_SITE_DATA &
532 ~BrowsingDataRemover::REMOVE_COOKIES &
533 ~BrowsingDataRemover::REMOVE_CHANNEL_IDS &
534 ~BrowsingDataRemover::REMOVE_PLUGIN_DATA},
535 {false, false, true, BrowsingDataRemover::REMOVE_CACHE},
536 {true, true, false, BrowsingDataRemover::REMOVE_SITE_DATA},
537 {true, false, true, BrowsingDataRemover::REMOVE_COOKIES |
538 BrowsingDataRemover::REMOVE_CHANNEL_IDS |
539 BrowsingDataRemover::REMOVE_PLUGIN_DATA |
540 BrowsingDataRemover::REMOVE_CACHE},
541 {false, true, true, BrowsingDataRemover::REMOVE_CACHE |
542 (BrowsingDataRemover::REMOVE_SITE_DATA &
543 ~BrowsingDataRemover::REMOVE_COOKIES &
544 ~BrowsingDataRemover::REMOVE_CHANNEL_IDS &
545 ~BrowsingDataRemover::REMOVE_PLUGIN_DATA)},
546 {true, true, true, BrowsingDataRemover::REMOVE_SITE_DATA |
547 BrowsingDataRemover::REMOVE_CACHE},
548 };
549
550 for (unsigned int i = 0; i < arraysize(test_cases); ++i) {
551 SCOPED_TRACE(base::StringPrintf("Test case %d", i));
552 const TestCase& test_case = test_cases[i];
553
554 // We always delete data for all time and all origin types.
555 BrowsingDataRemover::TimeRange all_time(base::Time(), base::Time::Max());
556 BrowsingDataHelper::OriginTypeMask all_origin_types =
557 BrowsingDataHelper::ALL;
558
559 // Some data are deleted for the origin and some for the registrable domain.
560 // Depending on the chosen datatypes, this might result into one or two
561 // calls. In the latter case, the removal mask will be split into two
562 // parts - one for the origin deletion and one for the registrable domain.
563 const int domain_scoped_types = BrowsingDataRemover::REMOVE_COOKIES |
564 BrowsingDataRemover::REMOVE_CHANNEL_IDS |
565 BrowsingDataRemover::REMOVE_PLUGIN_DATA;
566 int registrable_domain_deletion_mask = test_case.mask & domain_scoped_types;
567 int origin_deletion_mask = test_case.mask & ~domain_scoped_types;
568
569 if (registrable_domain_deletion_mask) {
570 remover()->ExpectCallDontCareAboutFilterBuilder(
571 all_time, registrable_domain_deletion_mask, all_origin_types);
572 }
573
574 if (origin_deletion_mask) {
575 remover()->ExpectCallDontCareAboutFilterBuilder(
576 all_time, origin_deletion_mask, all_origin_types);
577 }
578
579 SetClearingFinished(false);
580 client.ClearSiteData(
581 profile(), url::Origin(GURL("https://www.example.com")),
582 test_case.cookies, test_case.storage, test_case.cache,
583 base::Bind(
584 &ChromeContentBrowserClientClearSiteDataTest::SetClearingFinished,
585 base::Unretained(this), true));
586 EXPECT_TRUE(IsClearingFinished());
587
588 remover()->VerifyAndClearExpectations();
589 }
590 }
591
592 // Tests that ClearBrowsingData() called for an origin deletes cookies in the
593 // scope of the registrable domain corresponding to that origin, while cache
594 // is deleted for that exact origin.
595 TEST_F(ChromeContentBrowserClientClearSiteDataTest, RegistrableDomains) {
596 ChromeContentBrowserClient client;
597
598 struct TestCase {
599 const char* origin; // origin on which ClearSiteData() is called.
600 const char* domain; // domain on which cookies will be deleted.
601 } test_cases[] = {
602 // TLD has no embedded dot.
603 {"https://example.com", "example.com"},
604 {"https://www.example.com", "example.com"},
605 {"https://www.fourth.third.second.com", "second.com"},
606
607 // TLD has one embedded dot.
608 {"https://www.example.co.uk", "example.co.uk"},
609 {"https://example.co.uk", "example.co.uk"},
610
611 // TLD has more embedded dots.
612 {"https://www.website.sp.nom.br", "website.sp.nom.br"},
613
614 // IP addresses.
615 {"http://127.0.0.1", "127.0.0.1"},
616 {"http://192.168.0.1", "192.168.0.1"},
617 {"http://192.168.0.1", "192.168.0.1"},
618
619 // Internal hostnames.
620 {"http://localhost", "localhost"},
621 {"http://fileserver", "fileserver"},
622
623 // These are not subdomains of internal hostnames, but subdomain of
624 // unknown TLDs.
625 {"http://subdomain.localhost", "subdomain.localhost"},
626 {"http://www.subdomain.localhost", "subdomain.localhost"},
627 {"http://documents.fileserver", "documents.fileserver"},
628
629 // Scheme and port don't matter.
630 {"http://example.com", "example.com"},
631 {"http://example.com:8080", "example.com"},
632 {"https://example.com:4433", "example.com"},
633 };
634
635 for (const TestCase& test_case : test_cases) {
636 SCOPED_TRACE(test_case.origin);
637
638 std::unique_ptr<RegistrableDomainFilterBuilder>
639 registrable_domain_filter_builder(new RegistrableDomainFilterBuilder(
640 BrowsingDataFilterBuilder::WHITELIST));
641 registrable_domain_filter_builder->AddRegisterableDomain(test_case.domain);
642
643 remover()->ExpectCall(
644 BrowsingDataRemover::Period(browsing_data::TimePeriod::ALL_TIME),
645 BrowsingDataRemover::REMOVE_COOKIES |
646 BrowsingDataRemover::REMOVE_CHANNEL_IDS |
647 BrowsingDataRemover::REMOVE_PLUGIN_DATA,
648 BrowsingDataHelper::ALL, std::move(registrable_domain_filter_builder));
649
650 std::unique_ptr<OriginFilterBuilder> origin_filter_builder(
651 new OriginFilterBuilder(BrowsingDataFilterBuilder::WHITELIST));
652 origin_filter_builder->AddOrigin(url::Origin(GURL(test_case.origin)));
653
654 remover()->ExpectCall(
655 BrowsingDataRemover::Period(browsing_data::TimePeriod::ALL_TIME),
656 BrowsingDataRemover::REMOVE_CACHE, BrowsingDataHelper::ALL,
657 std::move(origin_filter_builder));
658
659 SetClearingFinished(false);
660 client.ClearSiteData(
661 profile(), url::Origin(GURL(test_case.origin)), true /* cookies */,
662 false /* storage */, true /* cache */,
663 base::Bind(
664 &ChromeContentBrowserClientClearSiteDataTest::SetClearingFinished,
665 base::Unretained(this), true));
666 EXPECT_TRUE(IsClearingFinished());
667
668 remover()->VerifyAndClearExpectations();
669 }
670 }
671
672 // Tests that we always wait for all scheduled BrowsingDataRemover tasks and
673 // that BrowsingDataRemoverObserver never leaks.
674 TEST_F(ChromeContentBrowserClientClearSiteDataTest, Tasks) {
675 ChromeContentBrowserClient client;
676 url::Origin origin(GURL("https://www.example.com"));
677
678 // No removal tasks.
679 SetClearingFinished(false);
680 client.ClearSiteData(
681 profile(), origin, false /* cookies */, false /* storage */,
682 false /* cache */,
683 base::Bind(
684 &ChromeContentBrowserClientClearSiteDataTest::SetClearingFinished,
685 base::Unretained(this), true));
686 EXPECT_TRUE(IsClearingFinished());
687
688 // One removal task: deleting cookies with a domain filter.
689 SetClearingFinished(false);
690 client.ClearSiteData(
691 profile(), origin, true /* cookies */, false /* storage */,
692 false /* cache */,
693 base::Bind(
694 &ChromeContentBrowserClientClearSiteDataTest::SetClearingFinished,
695 base::Unretained(this), true));
696 EXPECT_TRUE(IsClearingFinished());
697
698 // One removal task: deleting cache with a domain filter.
699 SetClearingFinished(false);
700 client.ClearSiteData(
701 profile(), origin, false /* cookies */, false /* storage */,
702 true /* cache */,
703 base::Bind(
704 &ChromeContentBrowserClientClearSiteDataTest::SetClearingFinished,
705 base::Unretained(this), true));
706 EXPECT_TRUE(IsClearingFinished());
707
708 // Two removal tasks, with domain and origin filters respectively.
709 SetClearingFinished(false);
710 client.ClearSiteData(
711 profile(), origin, true /* cookies */, false /* storage */,
712 true /* cache */,
713 base::Bind(
714 &ChromeContentBrowserClientClearSiteDataTest::SetClearingFinished,
715 base::Unretained(this), true));
716 EXPECT_TRUE(IsClearingFinished());
717 }
718
719 } // namespace
OLDNEW
« no previous file with comments | « chrome/browser/chrome_content_browser_client.cc ('k') | content/browser/browsing_data/clear_site_data_throttle.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698