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

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: Fixed tests, extracted c/b/browsing_data Created 4 years, 5 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 <map> 7 #include <map>
8 8
9 #include "base/command_line.h" 9 #include "base/command_line.h"
10 #include "base/macros.h" 10 #include "base/macros.h"
11 #include "base/memory/ptr_util.h"
12 #include "base/message_loop/message_loop.h"
11 #include "base/metrics/field_trial.h" 13 #include "base/metrics/field_trial.h"
12 #include "base/strings/utf_string_conversions.h" 14 #include "base/strings/utf_string_conversions.h"
13 #include "build/build_config.h" 15 #include "build/build_config.h"
16 #include "chrome/browser/browsing_data/browsing_data_filter_builder.h"
17 #include "chrome/browser/browsing_data/browsing_data_helper.h"
18 #include "chrome/browser/browsing_data/browsing_data_remover.h"
19 #include "chrome/browser/browsing_data/browsing_data_remover_factory.h"
20 #include "chrome/browser/browsing_data/origin_filter_builder.h"
21 #include "chrome/browser/browsing_data/registrable_domain_filter_builder.h"
14 #include "chrome/browser/search_engines/template_url_service_factory.h" 22 #include "chrome/browser/search_engines/template_url_service_factory.h"
23 #include "chrome/test/base/testing_profile.h"
15 #include "components/content_settings/core/browser/host_content_settings_map.h" 24 #include "components/content_settings/core/browser/host_content_settings_map.h"
16 #include "components/search_engines/template_url_service.h" 25 #include "components/search_engines/template_url_service.h"
17 #include "components/variations/entropy_provider.h" 26 #include "components/variations/entropy_provider.h"
18 #include "components/variations/variations_associated_data.h" 27 #include "components/variations/variations_associated_data.h"
19 #include "components/version_info/version_info.h" 28 #include "components/version_info/version_info.h"
20 #include "content/public/browser/navigation_controller.h" 29 #include "content/public/browser/navigation_controller.h"
21 #include "content/public/browser/navigation_entry.h" 30 #include "content/public/browser/navigation_entry.h"
22 #include "content/public/browser/web_contents.h" 31 #include "content/public/browser/web_contents.h"
23 #include "content/public/common/content_switches.h" 32 #include "content/public/common/content_switches.h"
24 #include "content/public/test/test_browser_thread_bundle.h" 33 #include "content/public/test/test_browser_thread_bundle.h"
34 #include "testing/gmock/include/gmock/gmock.h"
25 #include "testing/gtest/include/gtest/gtest.h" 35 #include "testing/gtest/include/gtest/gtest.h"
26 #include "url/gurl.h" 36 #include "url/gurl.h"
27 37
28 #if !defined(OS_ANDROID) 38 #if !defined(OS_ANDROID)
29 #include "chrome/browser/ui/browser.h" 39 #include "chrome/browser/ui/browser.h"
30 #include "chrome/browser/ui/tabs/tab_strip_model.h" 40 #include "chrome/browser/ui/tabs/tab_strip_model.h"
31 #include "chrome/common/pref_names.h" 41 #include "chrome/common/pref_names.h"
32 #include "chrome/test/base/browser_with_test_window_test.h" 42 #include "chrome/test/base/browser_with_test_window_test.h"
33 #include "chrome/test/base/search_test_utils.h" 43 #include "chrome/test/base/search_test_utils.h"
34 #endif 44 #endif
35 45
46 using testing::_;
36 using ChromeContentBrowserClientTest = testing::Test; 47 using ChromeContentBrowserClientTest = testing::Test;
37 48
38 TEST_F(ChromeContentBrowserClientTest, ShouldAssignSiteForURL) { 49 TEST_F(ChromeContentBrowserClientTest, ShouldAssignSiteForURL) {
39 ChromeContentBrowserClient client; 50 ChromeContentBrowserClient client;
40 EXPECT_FALSE(client.ShouldAssignSiteForURL(GURL("chrome-native://test"))); 51 EXPECT_FALSE(client.ShouldAssignSiteForURL(GURL("chrome-native://test")));
41 EXPECT_TRUE(client.ShouldAssignSiteForURL(GURL("http://www.google.com"))); 52 EXPECT_TRUE(client.ShouldAssignSiteForURL(GURL("http://www.google.com")));
42 EXPECT_TRUE(client.ShouldAssignSiteForURL(GURL("https://www.google.com"))); 53 EXPECT_TRUE(client.ShouldAssignSiteForURL(GURL("https://www.google.com")));
43 } 54 }
44 55
45 // BrowserWithTestWindowTest doesn't work on Android. 56 // BrowserWithTestWindowTest doesn't work on Android.
(...skipping 275 matching lines...) Expand 10 before | Expand all | Expand 10 after
321 332
322 NavigationEntry* entry = browser()->tab_strip_model()-> 333 NavigationEntry* entry = browser()->tab_strip_model()->
323 GetActiveWebContents()->GetController().GetLastCommittedEntry(); 334 GetActiveWebContents()->GetController().GetLastCommittedEntry();
324 ASSERT_TRUE(entry != NULL); 335 ASSERT_TRUE(entry != NULL);
325 EXPECT_EQ(url_rewritten, entry->GetURL()); 336 EXPECT_EQ(url_rewritten, entry->GetURL());
326 EXPECT_EQ(url_original, entry->GetVirtualURL()); 337 EXPECT_EQ(url_original, entry->GetVirtualURL());
327 } 338 }
328 339
329 } // namespace content 340 } // namespace content
330 #endif // !defined(OS_ANDROID) 341 #endif // !defined(OS_ANDROID)
342
343 namespace {
344
345 // Tests for ChromeContentBrowserClient::ClearSiteData().
346 class ChromeContentBrowserClientClearSiteDataTest : public testing::Test {
347 public:
348 class MockBrowsingDataRemover : public BrowsingDataRemover {
349 public:
350 explicit MockBrowsingDataRemover(content::BrowserContext* context)
351 : BrowsingDataRemover(context) {}
352 ~MockBrowsingDataRemover() {}
353
354 MOCK_METHOD4(RemoveWithFilter, void(
355 const TimeRange& time_range, int remove_mask, int origin_type_mask,
356 const BrowsingDataFilterBuilder& filter_builder));
357 };
358
359 void SetUp() override {
360 BrowsingDataRemoverFactory::GetInstance()->SetTestingFactoryAndUse(
361 &profile_, &ChromeContentBrowserClientClearSiteDataTest::GetRemover);
362 }
363
364 content::BrowserContext* profile() {
365 return &profile_;
366 }
367
368 MockBrowsingDataRemover* remover() {
369 return static_cast<MockBrowsingDataRemover*>(
370 BrowsingDataRemoverFactory::GetForBrowserContext(&profile_));
371 }
372
373 private:
374 static std::unique_ptr<KeyedService> GetRemover(
375 content::BrowserContext* context) {
376 return base::WrapUnique(new MockBrowsingDataRemover(context));
377 }
378
379 base::MessageLoop loop_;
380 TestingProfile profile_;
381 };
382
383 // Matcher for BrowsingDataFilterBuilder. As it is not possible to compare
384 // two BrowsingDataFilterBuilder-s of different kinds, it can only be used
385 // when we know which subclass of BrowsingDataFilterBuilder we expect:
386 // OriginFilterBuilder or RegistrableDomainFilterBuilder.
387 class SameBuilderMatcher :
388 public testing::MatcherInterface<const BrowsingDataFilterBuilder&> {
389 public:
390 explicit SameBuilderMatcher(const OriginFilterBuilder& builder)
391 : origin_filter_builder_(&builder), domain_filter_builder_(nullptr) {
392 }
393
394 explicit SameBuilderMatcher(const RegistrableDomainFilterBuilder& builder)
395 : origin_filter_builder_(nullptr), domain_filter_builder_(&builder) {
396 }
397
398 virtual bool MatchAndExplain(const BrowsingDataFilterBuilder& builder,
399 testing::MatchResultListener* listener) const {
400 if (origin_filter_builder_) {
Mike West 2016/07/18 10:20:34 Nit: DCHECK_FALSE(origin_filter_builder_ && domain
msramek 2016/07/18 13:16:07 Done. I actually made it a XOR.
401 return *origin_filter_builder_ ==
402 static_cast<const OriginFilterBuilder&>(builder);
403 }
404
405 if (domain_filter_builder_) {
406 return *domain_filter_builder_ ==
407 static_cast<const RegistrableDomainFilterBuilder&>(builder);
408 }
409
410 NOTREACHED();
411 return false;
412 }
413
414 void DescribeTo(std::ostream* os) const override {
415 *os << "has the same scope, mode, and added entries as the expected "
416 << "filter builder.";
417 }
418
419 void DescribeNegationTo(std::ostream* os) const override {
420 *os << "differs from the expected filter builder in scope, mode, "
421 << "or added entries.";
422 }
423
424 private:
425 const OriginFilterBuilder* origin_filter_builder_;
426 const RegistrableDomainFilterBuilder* domain_filter_builder_;
427 };
428
429 // Tests that the parameters to ClearBrowsingData() are translated to
430 // the correct BrowsingDataRemover::RemoveWithFilter() operation. The fourth
431 // parameter, |filter_builder|, is tested in detail in the RegistrableDomains
432 // test below.
433 TEST_F(ChromeContentBrowserClientClearSiteDataTest, Parameters) {
434 ChromeContentBrowserClient client;
435
436 struct TestCase {
437 bool cookies;
438 bool storage;
439 bool cache;
440 int mask;
441 } test_cases[] = {
442 { false, false, false, 0 },
443 { true, false, false, BrowsingDataRemover::REMOVE_COOKIES },
444 { false, true, false,
445 BrowsingDataRemover::REMOVE_SITE_DATA &
446 ~BrowsingDataRemover::REMOVE_COOKIES },
447 { false, false, true, BrowsingDataRemover::REMOVE_CACHE },
448 { true, true, false, BrowsingDataRemover::REMOVE_SITE_DATA },
449 { true, false, true,
450 BrowsingDataRemover::REMOVE_COOKIES |
451 BrowsingDataRemover::REMOVE_CACHE },
452 { false, true, true,
453 BrowsingDataRemover::REMOVE_CACHE |
454 (BrowsingDataRemover::REMOVE_SITE_DATA &
455 ~BrowsingDataRemover::REMOVE_COOKIES) },
456 { true, true, true,
457 BrowsingDataRemover::REMOVE_SITE_DATA |
458 BrowsingDataRemover::REMOVE_CACHE },
459 };
460
461 for (const TestCase& test_case : test_cases) {
462 SCOPED_TRACE(test_case.mask);
463
464 // We always delete data for all time and all origin types.
465 BrowsingDataRemover::TimeRange all_time(base::Time(), base::Time::Max());
466 BrowsingDataHelper::OriginTypeMask all_origin_types =
467 BrowsingDataHelper::ALL;
468
469 // Some data are deleted for the origin and some for the registrable domain.
470 // Depending on the chosen datatypes, this might result into one or two
471 // calls. In the latter case, the removal mask will be split into two
472 // parts - one for the origin deletion and one for the registrable domain.
473 const int domain_scoped_types =
474 BrowsingDataRemover::REMOVE_COOKIES |
475 BrowsingDataRemover::REMOVE_CHANNEL_IDS;
476 int origin_deletion_mask =
477 test_case.mask & ~domain_scoped_types;
478 int registrable_domain_deletion_mask =
479 test_case.mask & domain_scoped_types;
480
481 EXPECT_CALL(
482 *remover(),
483 RemoveWithFilter(
484 all_time,
485 origin_deletion_mask,
486 all_origin_types,
487 _)).Times(origin_deletion_mask != 0);
488
489 EXPECT_CALL(
490 *remover(),
491 RemoveWithFilter(
492 all_time,
493 registrable_domain_deletion_mask,
494 all_origin_types,
495 _)).Times(registrable_domain_deletion_mask != 0);
496
497 client.ClearSiteData(
498 profile(), url::Origin(GURL("https://www.example.com")),
499 test_case.cookies, test_case.storage, test_case.cache);
500
501 testing::Mock::VerifyAndClearExpectations(remover());
502 }
503 }
504
505 // Tests that ClearBrowsingData() called for an origin deletes cookies in the
506 // scope of the registrable domain corresponding to that origin, while cache
507 // is deleted for that exact origin.
508 TEST_F(ChromeContentBrowserClientClearSiteDataTest, RegistrableDomains) {
509 ChromeContentBrowserClient client;
510
511 struct TestCase {
512 const char* origin; // origin on which ClearSiteData() is called.
513 const char* domain; // domain on which cookies will be deleted.
514 } test_cases[] = {
515 // TLD has no embedded dot.
516 { "https://example.com", "example.com" },
517 { "https://www.example.com", "example.com" },
518 { "https://www.fourth.third.second.com", "second.com" },
519
520 // TLD has one embedded dot.
521 { "https://www.example.co.uk", "example.co.uk" },
522 { "https://example.co.uk", "example.co.uk" },
523
524 // TLD has more embedded dots.
525 { "https://www.website.sp.nom.br", "website.sp.nom.br" },
526
527 // IP addresses.
528 { "http://127.0.0.1", "127.0.0.1" },
529 { "http://192.168.0.1", "192.168.0.1" },
530 { "http://192.168.0.1", "192.168.0.1" },
531
532 // Internal hostnames.
533 { "http://localhost", "localhost" },
534 { "http://fileserver", "fileserver" },
535
536 // These are not subdomains of internal hostnames, but subdomain of
537 // unknown TLDs.
538 { "http://subdomain.localhost", "subdomain.localhost" },
539 { "http://www.subdomain.localhost", "subdomain.localhost" },
540 { "http://documents.fileserver", "documents.fileserver" },
541
542 // Scheme and port don't matter.
543 { "http://example.com", "example.com" },
544 { "http://example.com:8080", "example.com" },
545 { "https://example.com:4433", "example.com" },
546 };
547
548 for (const TestCase& test_case : test_cases) {
549 SCOPED_TRACE(test_case.origin);
550
551 OriginFilterBuilder origin_filter_builder(
552 BrowsingDataFilterBuilder::WHITELIST);
553 origin_filter_builder.AddOrigin(url::Origin(GURL(test_case.origin)));
554
555 EXPECT_CALL(
556 *remover(),
557 RemoveWithFilter(
558 _,
559 BrowsingDataRemover::REMOVE_CACHE,
560 _,
561 MakeMatcher(new SameBuilderMatcher(origin_filter_builder))));
562
563 RegistrableDomainFilterBuilder registrable_domain_filter_builder(
564 BrowsingDataFilterBuilder::WHITELIST);
565 registrable_domain_filter_builder.AddRegisterableDomain(test_case.domain);
566
567 EXPECT_CALL(*remover(),
568 RemoveWithFilter(
569 _,
570 BrowsingDataRemover::REMOVE_COOKIES,
571 _,
572 MakeMatcher(new SameBuilderMatcher(
573 registrable_domain_filter_builder))));
574
575 client.ClearSiteData(
576 profile(), url::Origin(GURL(test_case.origin)),
577 true /* cookies */, false /* storage */, true /* cache */);
578
579 testing::Mock::VerifyAndClearExpectations(remover());
580 }
581 }
582
583 } // namespace
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698