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

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

Issue 2368923003: Support the Clear-Site-Data header on resource requests (Closed)
Patch Set: Addressed comments, formatted. Created 3 years, 6 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 <list>
8 #include <map> 8 #include <map>
9 #include <memory> 9 #include <memory>
10 10
11 #include "base/bind.h" 11 #include "base/bind.h"
12 #include "base/command_line.h" 12 #include "base/command_line.h"
13 #include "base/macros.h" 13 #include "base/macros.h"
14 #include "base/memory/ptr_util.h" 14 #include "base/memory/ptr_util.h"
15 #include "base/message_loop/message_loop.h" 15 #include "base/message_loop/message_loop.h"
16 #include "base/metrics/field_trial.h" 16 #include "base/metrics/field_trial.h"
17 #include "base/run_loop.h" 17 #include "base/run_loop.h"
18 #include "base/strings/stringprintf.h" 18 #include "base/strings/stringprintf.h"
19 #include "base/strings/utf_string_conversions.h" 19 #include "base/strings/utf_string_conversions.h"
20 #include "build/build_config.h" 20 #include "build/build_config.h"
21 #include "chrome/browser/browsing_data/browsing_data_helper.h" 21 #include "chrome/browser/browsing_data/browsing_data_helper.h"
22 #include "chrome/browser/browsing_data/chrome_browsing_data_remover_delegate.h" 22 #include "chrome/browser/browsing_data/chrome_browsing_data_remover_delegate.h"
23 #include "chrome/browser/browsing_data/mock_browsing_data_remover_delegate.h"
24 #include "chrome/browser/search_engines/template_url_service_factory.h" 23 #include "chrome/browser/search_engines/template_url_service_factory.h"
25 #include "chrome/test/base/testing_profile.h" 24 #include "chrome/test/base/testing_profile.h"
26 #include "components/content_settings/core/browser/host_content_settings_map.h" 25 #include "components/content_settings/core/browser/host_content_settings_map.h"
27 #include "components/search_engines/template_url_service.h" 26 #include "components/search_engines/template_url_service.h"
28 #include "components/variations/entropy_provider.h" 27 #include "components/variations/entropy_provider.h"
29 #include "components/variations/variations_associated_data.h" 28 #include "components/variations/variations_associated_data.h"
30 #include "components/version_info/version_info.h" 29 #include "components/version_info/version_info.h"
31 #include "content/public/browser/browsing_data_filter_builder.h" 30 #include "content/public/browser/browsing_data_filter_builder.h"
32 #include "content/public/browser/browsing_data_remover.h" 31 #include "content/public/browser/browsing_data_remover.h"
33 #include "content/public/browser/navigation_controller.h" 32 #include "content/public/browser/navigation_controller.h"
(...skipping 302 matching lines...) Expand 10 before | Expand all | Expand 10 after
336 335
337 NavigationEntry* entry = browser()->tab_strip_model()-> 336 NavigationEntry* entry = browser()->tab_strip_model()->
338 GetActiveWebContents()->GetController().GetLastCommittedEntry(); 337 GetActiveWebContents()->GetController().GetLastCommittedEntry();
339 ASSERT_TRUE(entry != NULL); 338 ASSERT_TRUE(entry != NULL);
340 EXPECT_EQ(url_rewritten, entry->GetURL()); 339 EXPECT_EQ(url_rewritten, entry->GetURL());
341 EXPECT_EQ(url_original, entry->GetVirtualURL()); 340 EXPECT_EQ(url_original, entry->GetVirtualURL());
342 } 341 }
343 342
344 } // namespace content 343 } // namespace content
345 #endif // !defined(OS_ANDROID) 344 #endif // !defined(OS_ANDROID)
346
347 namespace {
348
349 // Tests for ChromeContentBrowserClient::ClearSiteData().
350 class ChromeContentBrowserClientClearSiteDataTest : public testing::Test {
351 public:
352 void SetUp() override {
353 content::BrowserContext::GetBrowsingDataRemover(profile())
354 ->SetEmbedderDelegate(&mock_delegate_);
355 run_loop_.reset(new base::RunLoop());
356 }
357
358 content::BrowserContext* profile() { return &profile_; }
359
360 MockBrowsingDataRemoverDelegate* delegate() { return &mock_delegate_; }
361
362 void OnClearingFinished() { run_loop_->Quit(); }
363
364 void WaitForClearingFinished() {
365 run_loop_->Run();
366 run_loop_.reset(new base::RunLoop());
367 }
368
369 private:
370 std::unique_ptr<base::RunLoop> run_loop_;
371 MockBrowsingDataRemoverDelegate mock_delegate_;
372 content::TestBrowserThreadBundle thread_bundle_;
373 TestingProfile profile_;
374 };
375
376 // Tests that the parameters to ClearBrowsingData() are translated to
377 // the correct BrowsingDataRemover::RemoveInternal() operation. The fourth
378 // parameter, |filter_builder|, is tested in detail in the RegistrableDomains
379 // test below.
380 TEST_F(ChromeContentBrowserClientClearSiteDataTest, Parameters) {
381 ChromeContentBrowserClient client;
382
383 struct TestCase {
384 bool cookies;
385 bool storage;
386 bool cache;
387 int mask;
388 } test_cases[] = {
389 {false, false, false, 0},
390 {true, false, false,
391 content::BrowsingDataRemover::DATA_TYPE_COOKIES |
392 content::BrowsingDataRemover::DATA_TYPE_CHANNEL_IDS |
393 ChromeBrowsingDataRemoverDelegate::DATA_TYPE_PLUGIN_DATA},
394 {false, true, false, content::BrowsingDataRemover::DATA_TYPE_DOM_STORAGE},
395 {false, false, true, content::BrowsingDataRemover::DATA_TYPE_CACHE},
396 {true, true, false,
397 content::BrowsingDataRemover::DATA_TYPE_COOKIES |
398 content::BrowsingDataRemover::DATA_TYPE_CHANNEL_IDS |
399 ChromeBrowsingDataRemoverDelegate::DATA_TYPE_PLUGIN_DATA |
400 content::BrowsingDataRemover::DATA_TYPE_DOM_STORAGE},
401 {true, false, true,
402 content::BrowsingDataRemover::DATA_TYPE_COOKIES |
403 content::BrowsingDataRemover::DATA_TYPE_CHANNEL_IDS |
404 ChromeBrowsingDataRemoverDelegate::DATA_TYPE_PLUGIN_DATA |
405 content::BrowsingDataRemover::DATA_TYPE_CACHE},
406 {false, true, true,
407 content::BrowsingDataRemover::DATA_TYPE_DOM_STORAGE |
408 content::BrowsingDataRemover::DATA_TYPE_CACHE},
409 {true, true, true,
410 content::BrowsingDataRemover::DATA_TYPE_COOKIES |
411 content::BrowsingDataRemover::DATA_TYPE_CHANNEL_IDS |
412 ChromeBrowsingDataRemoverDelegate::DATA_TYPE_PLUGIN_DATA |
413 content::BrowsingDataRemover::DATA_TYPE_DOM_STORAGE |
414 content::BrowsingDataRemover::DATA_TYPE_CACHE},
415 };
416
417 for (unsigned int i = 0; i < arraysize(test_cases); ++i) {
418 SCOPED_TRACE(base::StringPrintf("Test case %d", i));
419 const TestCase& test_case = test_cases[i];
420
421 // We always delete data for all time and all origin types.
422 int all_origin_types = ChromeBrowsingDataRemoverDelegate::ALL_ORIGIN_TYPES;
423
424 // Some data are deleted for the origin and some for the registrable domain.
425 // Depending on the chosen datatypes, this might result into one or two
426 // calls. In the latter case, the removal mask will be split into two
427 // parts - one for the origin deletion and one for the registrable domain.
428 const int domain_scoped_types =
429 content::BrowsingDataRemover::DATA_TYPE_COOKIES |
430 content::BrowsingDataRemover::DATA_TYPE_CHANNEL_IDS |
431 ChromeBrowsingDataRemoverDelegate::DATA_TYPE_PLUGIN_DATA;
432 int registrable_domain_deletion_mask = test_case.mask & domain_scoped_types;
433 int origin_deletion_mask = test_case.mask & ~domain_scoped_types;
434
435 if (registrable_domain_deletion_mask) {
436 delegate()->ExpectCallDontCareAboutFilterBuilder(
437 base::Time(), base::Time::Max(), registrable_domain_deletion_mask,
438 all_origin_types);
439 }
440
441 if (origin_deletion_mask) {
442 delegate()->ExpectCallDontCareAboutFilterBuilder(
443 base::Time(), base::Time::Max(), origin_deletion_mask,
444 all_origin_types);
445 }
446
447 client.ClearSiteData(
448 profile(), url::Origin(GURL("https://www.example.com")),
449 test_case.cookies, test_case.storage, test_case.cache,
450 base::Bind(
451 &ChromeContentBrowserClientClearSiteDataTest::OnClearingFinished,
452 base::Unretained(this)));
453 WaitForClearingFinished();
454
455 delegate()->VerifyAndClearExpectations();
456 }
457 }
458
459 // Tests that ClearBrowsingData() called for an origin deletes cookies in the
460 // scope of the registrable domain corresponding to that origin, while cache
461 // is deleted for that exact origin.
462 TEST_F(ChromeContentBrowserClientClearSiteDataTest, RegistrableDomains) {
463 ChromeContentBrowserClient client;
464
465 struct TestCase {
466 const char* origin; // origin on which ClearSiteData() is called.
467 const char* domain; // domain on which cookies will be deleted.
468 } test_cases[] = {
469 // TLD has no embedded dot.
470 {"https://example.com", "example.com"},
471 {"https://www.example.com", "example.com"},
472 {"https://www.fourth.third.second.com", "second.com"},
473
474 // TLD has one embedded dot.
475 {"https://www.example.co.uk", "example.co.uk"},
476 {"https://example.co.uk", "example.co.uk"},
477
478 // TLD has more embedded dots.
479 {"https://www.website.sp.nom.br", "website.sp.nom.br"},
480
481 // IP addresses.
482 {"http://127.0.0.1", "127.0.0.1"},
483 {"http://192.168.0.1", "192.168.0.1"},
484 {"http://192.168.0.1", "192.168.0.1"},
485
486 // Internal hostnames.
487 {"http://localhost", "localhost"},
488 {"http://fileserver", "fileserver"},
489
490 // These are not subdomains of internal hostnames, but subdomain of
491 // unknown TLDs.
492 {"http://subdomain.localhost", "subdomain.localhost"},
493 {"http://www.subdomain.localhost", "subdomain.localhost"},
494 {"http://documents.fileserver", "documents.fileserver"},
495
496 // Scheme and port don't matter.
497 {"http://example.com", "example.com"},
498 {"http://example.com:8080", "example.com"},
499 {"https://example.com:4433", "example.com"},
500 };
501
502 for (const TestCase& test_case : test_cases) {
503 SCOPED_TRACE(test_case.origin);
504
505 std::unique_ptr<BrowsingDataFilterBuilder>
506 registrable_domain_filter_builder(BrowsingDataFilterBuilder::Create(
507 BrowsingDataFilterBuilder::WHITELIST));
508 registrable_domain_filter_builder->AddRegisterableDomain(test_case.domain);
509
510 delegate()->ExpectCall(
511 base::Time(), base::Time::Max(),
512 content::BrowsingDataRemover::DATA_TYPE_COOKIES |
513 content::BrowsingDataRemover::DATA_TYPE_CHANNEL_IDS |
514 ChromeBrowsingDataRemoverDelegate::DATA_TYPE_PLUGIN_DATA,
515 ChromeBrowsingDataRemoverDelegate::ALL_ORIGIN_TYPES,
516 *registrable_domain_filter_builder);
517
518 std::unique_ptr<BrowsingDataFilterBuilder> origin_filter_builder(
519 BrowsingDataFilterBuilder::Create(
520 BrowsingDataFilterBuilder::WHITELIST));
521 origin_filter_builder->AddOrigin(url::Origin(GURL(test_case.origin)));
522
523 delegate()->ExpectCall(base::Time(), base::Time::Max(),
524 content::BrowsingDataRemover::DATA_TYPE_CACHE,
525 ChromeBrowsingDataRemoverDelegate::ALL_ORIGIN_TYPES,
526 *origin_filter_builder);
527
528 client.ClearSiteData(
529 profile(), url::Origin(GURL(test_case.origin)), true /* cookies */,
530 false /* storage */, true /* cache */,
531 base::Bind(
532 &ChromeContentBrowserClientClearSiteDataTest::OnClearingFinished,
533 base::Unretained(this)));
534 WaitForClearingFinished();
535
536 delegate()->VerifyAndClearExpectations();
537 }
538 }
539
540 // Tests that we always wait for all scheduled BrowsingDataRemover tasks and
541 // that BrowsingDataRemoverObserver never leaks.
542 TEST_F(ChromeContentBrowserClientClearSiteDataTest, Tasks) {
543 ChromeContentBrowserClient client;
544 url::Origin origin(GURL("https://www.example.com"));
545
546 // No removal tasks.
547 client.ClearSiteData(
548 profile(), origin, false /* cookies */, false /* storage */,
549 false /* cache */,
550 base::Bind(
551 &ChromeContentBrowserClientClearSiteDataTest::OnClearingFinished,
552 base::Unretained(this)));
553 WaitForClearingFinished();
554
555 // One removal task: deleting cookies with a domain filter.
556 client.ClearSiteData(
557 profile(), origin, true /* cookies */, false /* storage */,
558 false /* cache */,
559 base::Bind(
560 &ChromeContentBrowserClientClearSiteDataTest::OnClearingFinished,
561 base::Unretained(this)));
562 WaitForClearingFinished();
563
564 // One removal task: deleting cache with a domain filter.
565 client.ClearSiteData(
566 profile(), origin, false /* cookies */, false /* storage */,
567 true /* cache */,
568 base::Bind(
569 &ChromeContentBrowserClientClearSiteDataTest::OnClearingFinished,
570 base::Unretained(this)));
571 WaitForClearingFinished();
572
573 // Two removal tasks, with domain and origin filters respectively.
574 client.ClearSiteData(
575 profile(), origin, true /* cookies */, false /* storage */,
576 true /* cache */,
577 base::Bind(
578 &ChromeContentBrowserClientClearSiteDataTest::OnClearingFinished,
579 base::Unretained(this)));
580 WaitForClearingFinished();
581 }
582
583 } // 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