[Sync] Rename PSS::IsSyncEnabled to PSS::IsSyncAllowedByFlag.
[chromium-blink-merge.git] / chrome / browser / browsing_data / browsing_data_remover_unittest.cc
blob0531d87453b35fed9d2d0d01766f3c960e71ad5c
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 "chrome/browser/browsing_data/browsing_data_remover.h"
7 #include <set>
8 #include <string>
9 #include <vector>
11 #include "base/bind.h"
12 #include "base/bind_helpers.h"
13 #include "base/files/file_path.h"
14 #include "base/files/file_util.h"
15 #include "base/guid.h"
16 #include "base/memory/scoped_ptr.h"
17 #include "base/message_loop/message_loop.h"
18 #include "base/prefs/testing_pref_service.h"
19 #include "base/run_loop.h"
20 #include "base/strings/utf_string_conversions.h"
21 #include "base/task/cancelable_task_tracker.h"
22 #include "chrome/browser/autofill/personal_data_manager_factory.h"
23 #include "chrome/browser/bookmarks/bookmark_model_factory.h"
24 #include "chrome/browser/browsing_data/browsing_data_helper.h"
25 #include "chrome/browser/browsing_data/browsing_data_remover_test_util.h"
26 #include "chrome/browser/domain_reliability/service_factory.h"
27 #include "chrome/browser/favicon/favicon_service_factory.h"
28 #include "chrome/browser/history/history_service_factory.h"
29 #include "chrome/browser/safe_browsing/safe_browsing_service.h"
30 #include "chrome/common/pref_names.h"
31 #include "chrome/test/base/testing_browser_process.h"
32 #include "chrome/test/base/testing_profile.h"
33 #include "components/autofill/core/browser/autofill_profile.h"
34 #include "components/autofill/core/browser/autofill_test_utils.h"
35 #include "components/autofill/core/browser/credit_card.h"
36 #include "components/autofill/core/browser/personal_data_manager.h"
37 #include "components/autofill/core/browser/personal_data_manager_observer.h"
38 #include "components/bookmarks/browser/bookmark_model.h"
39 #include "components/bookmarks/test/bookmark_test_helpers.h"
40 #include "components/domain_reliability/clear_mode.h"
41 #include "components/domain_reliability/monitor.h"
42 #include "components/domain_reliability/service.h"
43 #include "components/favicon/core/favicon_service.h"
44 #include "components/history/core/browser/history_service.h"
45 #include "content/public/browser/cookie_store_factory.h"
46 #include "content/public/browser/dom_storage_context.h"
47 #include "content/public/browser/local_storage_usage_info.h"
48 #include "content/public/browser/storage_partition.h"
49 #include "content/public/test/test_browser_thread.h"
50 #include "content/public/test/test_browser_thread_bundle.h"
51 #include "content/public/test/test_utils.h"
52 #include "net/cookies/cookie_store.h"
53 #include "net/ssl/channel_id_service.h"
54 #include "net/ssl/channel_id_store.h"
55 #include "net/ssl/ssl_client_cert_type.h"
56 #include "net/url_request/url_request_context.h"
57 #include "net/url_request/url_request_context_getter.h"
58 #include "testing/gmock/include/gmock/gmock.h"
59 #include "testing/gtest/include/gtest/gtest.h"
60 #include "third_party/skia/include/core/SkBitmap.h"
61 #include "ui/gfx/favicon_size.h"
63 #if defined(OS_CHROMEOS)
64 #include "chrome/browser/chromeos/login/users/mock_user_manager.h"
65 #include "chrome/browser/chromeos/login/users/scoped_user_manager_enabler.h"
66 #include "chrome/browser/chromeos/settings/cros_settings.h"
67 #include "chrome/browser/chromeos/settings/device_settings_service.h"
68 #include "chromeos/dbus/dbus_thread_manager.h"
69 #include "chromeos/dbus/mock_cryptohome_client.h"
70 #endif
72 #if defined(ENABLE_EXTENSIONS)
73 #include "chrome/browser/extensions/mock_extension_special_storage_policy.h"
74 #endif
76 class MockExtensionSpecialStoragePolicy;
78 using content::BrowserThread;
79 using content::StoragePartition;
80 using domain_reliability::CLEAR_BEACONS;
81 using domain_reliability::CLEAR_CONTEXTS;
82 using domain_reliability::DomainReliabilityClearMode;
83 using domain_reliability::DomainReliabilityMonitor;
84 using domain_reliability::DomainReliabilityService;
85 using domain_reliability::DomainReliabilityServiceFactory;
86 using testing::_;
87 using testing::Invoke;
88 using testing::WithArgs;
90 namespace {
92 const char kTestOrigin1[] = "http://host1:1/";
93 const char kTestOrigin2[] = "http://host2:1/";
94 const char kTestOrigin3[] = "http://host3:1/";
95 const char kTestOriginExt[] = "chrome-extension://abcdefghijklmnopqrstuvwxyz/";
96 const char kTestOriginDevTools[] = "chrome-devtools://abcdefghijklmnopqrstuvw/";
98 // For Autofill.
99 const char kChromeOrigin[] = "Chrome settings";
100 const char kWebOrigin[] = "https://www.example.com/";
102 const GURL kOrigin1(kTestOrigin1);
103 const GURL kOrigin2(kTestOrigin2);
104 const GURL kOrigin3(kTestOrigin3);
105 const GURL kOriginExt(kTestOriginExt);
106 const GURL kOriginDevTools(kTestOriginDevTools);
108 const base::FilePath::CharType kDomStorageOrigin1[] =
109 FILE_PATH_LITERAL("http_host1_1.localstorage");
111 const base::FilePath::CharType kDomStorageOrigin2[] =
112 FILE_PATH_LITERAL("http_host2_1.localstorage");
114 const base::FilePath::CharType kDomStorageOrigin3[] =
115 FILE_PATH_LITERAL("http_host3_1.localstorage");
117 const base::FilePath::CharType kDomStorageExt[] = FILE_PATH_LITERAL(
118 "chrome-extension_abcdefghijklmnopqrstuvwxyz_0.localstorage");
120 #if defined(OS_CHROMEOS)
121 void FakeDBusCall(const chromeos::BoolDBusMethodCallback& callback) {
122 base::MessageLoop::current()->PostTask(
123 FROM_HERE,
124 base::Bind(callback, chromeos::DBUS_METHOD_CALL_SUCCESS, true));
126 #endif
128 struct StoragePartitionRemovalData {
129 uint32 remove_mask;
130 uint32 quota_storage_remove_mask;
131 GURL remove_origin;
132 base::Time remove_begin;
133 base::Time remove_end;
134 StoragePartition::OriginMatcherFunction origin_matcher;
136 StoragePartitionRemovalData() : remove_mask(0),
137 quota_storage_remove_mask(0) {}
140 class TestStoragePartition : public StoragePartition {
141 public:
142 TestStoragePartition() {}
143 ~TestStoragePartition() override {}
145 // content::StoragePartition implementation.
146 base::FilePath GetPath() override { return base::FilePath(); }
147 net::URLRequestContextGetter* GetURLRequestContext() override { return NULL; }
148 net::URLRequestContextGetter* GetMediaURLRequestContext() override {
149 return NULL;
151 storage::QuotaManager* GetQuotaManager() override { return NULL; }
152 content::AppCacheService* GetAppCacheService() override { return NULL; }
153 storage::FileSystemContext* GetFileSystemContext() override { return NULL; }
154 storage::DatabaseTracker* GetDatabaseTracker() override { return NULL; }
155 content::DOMStorageContext* GetDOMStorageContext() override { return NULL; }
156 content::IndexedDBContext* GetIndexedDBContext() override { return NULL; }
157 content::ServiceWorkerContext* GetServiceWorkerContext() override {
158 return NULL;
160 content::GeofencingManager* GetGeofencingManager() override { return NULL; }
161 content::NavigatorConnectContext* GetNavigatorConnectContext() override {
162 return nullptr;
165 content::PlatformNotificationContext* GetPlatformNotificationContext()
166 override {
167 return nullptr;
170 content::HostZoomMap* GetHostZoomMap() override { return NULL; }
171 content::HostZoomLevelContext* GetHostZoomLevelContext() override {
172 return NULL;
174 content::ZoomLevelDelegate* GetZoomLevelDelegate() override { return NULL; }
176 void ClearDataForOrigin(uint32 remove_mask,
177 uint32 quota_storage_remove_mask,
178 const GURL& storage_origin,
179 net::URLRequestContextGetter* rq_context,
180 const base::Closure& callback) override {
181 BrowserThread::PostTask(BrowserThread::UI,
182 FROM_HERE,
183 base::Bind(&TestStoragePartition::AsyncRunCallback,
184 base::Unretained(this),
185 callback));
188 void ClearData(uint32 remove_mask,
189 uint32 quota_storage_remove_mask,
190 const GURL& storage_origin,
191 const OriginMatcherFunction& origin_matcher,
192 const base::Time begin,
193 const base::Time end,
194 const base::Closure& callback) override {
195 // Store stuff to verify parameters' correctness later.
196 storage_partition_removal_data_.remove_mask = remove_mask;
197 storage_partition_removal_data_.quota_storage_remove_mask =
198 quota_storage_remove_mask;
199 storage_partition_removal_data_.remove_origin = storage_origin;
200 storage_partition_removal_data_.remove_begin = begin;
201 storage_partition_removal_data_.remove_end = end;
202 storage_partition_removal_data_.origin_matcher = origin_matcher;
204 BrowserThread::PostTask(
205 BrowserThread::UI,
206 FROM_HERE,
207 base::Bind(&TestStoragePartition::AsyncRunCallback,
208 base::Unretained(this), callback));
211 void Flush() override {}
213 StoragePartitionRemovalData GetStoragePartitionRemovalData() {
214 return storage_partition_removal_data_;
217 private:
218 void AsyncRunCallback(const base::Closure& callback) {
219 callback.Run();
222 StoragePartitionRemovalData storage_partition_removal_data_;
224 DISALLOW_COPY_AND_ASSIGN(TestStoragePartition);
227 } // namespace
229 // Testers -------------------------------------------------------------------
231 class RemoveCookieTester {
232 public:
233 RemoveCookieTester() : get_cookie_success_(false), cookie_store_(NULL) {
236 // Returns true, if the given cookie exists in the cookie store.
237 bool ContainsCookie() {
238 scoped_refptr<content::MessageLoopRunner> message_loop_runner =
239 new content::MessageLoopRunner;
240 quit_closure_ = message_loop_runner->QuitClosure();
241 get_cookie_success_ = false;
242 cookie_store_->GetCookiesWithOptionsAsync(
243 kOrigin1, net::CookieOptions(),
244 base::Bind(&RemoveCookieTester::GetCookieCallback,
245 base::Unretained(this)));
246 message_loop_runner->Run();
247 return get_cookie_success_;
250 void AddCookie() {
251 scoped_refptr<content::MessageLoopRunner> message_loop_runner =
252 new content::MessageLoopRunner;
253 quit_closure_ = message_loop_runner->QuitClosure();
254 cookie_store_->SetCookieWithOptionsAsync(
255 kOrigin1, "A=1", net::CookieOptions(),
256 base::Bind(&RemoveCookieTester::SetCookieCallback,
257 base::Unretained(this)));
258 message_loop_runner->Run();
261 protected:
262 void SetMonster(net::CookieStore* monster) {
263 cookie_store_ = monster;
266 private:
267 void GetCookieCallback(const std::string& cookies) {
268 if (cookies == "A=1") {
269 get_cookie_success_ = true;
270 } else {
271 EXPECT_EQ("", cookies);
272 get_cookie_success_ = false;
274 quit_closure_.Run();
277 void SetCookieCallback(bool result) {
278 ASSERT_TRUE(result);
279 quit_closure_.Run();
282 bool get_cookie_success_;
283 base::Closure quit_closure_;
284 net::CookieStore* cookie_store_;
286 DISALLOW_COPY_AND_ASSIGN(RemoveCookieTester);
289 #if defined(SAFE_BROWSING_SERVICE)
290 class RemoveSafeBrowsingCookieTester : public RemoveCookieTester {
291 public:
292 RemoveSafeBrowsingCookieTester()
293 : browser_process_(TestingBrowserProcess::GetGlobal()) {
294 scoped_refptr<SafeBrowsingService> sb_service =
295 SafeBrowsingService::CreateSafeBrowsingService();
296 browser_process_->SetSafeBrowsingService(sb_service.get());
297 sb_service->Initialize();
298 base::MessageLoop::current()->RunUntilIdle();
300 // Create a cookiemonster that does not have persistant storage, and replace
301 // the SafeBrowsingService created one with it.
302 net::CookieStore* monster =
303 content::CreateCookieStore(content::CookieStoreConfig());
304 sb_service->url_request_context()->GetURLRequestContext()->
305 set_cookie_store(monster);
306 SetMonster(monster);
309 virtual ~RemoveSafeBrowsingCookieTester() {
310 browser_process_->safe_browsing_service()->ShutDown();
311 base::MessageLoop::current()->RunUntilIdle();
312 browser_process_->SetSafeBrowsingService(NULL);
315 private:
316 TestingBrowserProcess* browser_process_;
318 DISALLOW_COPY_AND_ASSIGN(RemoveSafeBrowsingCookieTester);
320 #endif
322 class RemoveChannelIDTester : public net::SSLConfigService::Observer {
323 public:
324 explicit RemoveChannelIDTester(TestingProfile* profile)
325 : ssl_config_changed_count_(0) {
326 channel_id_service_ = profile->GetRequestContext()->
327 GetURLRequestContext()->channel_id_service();
328 ssl_config_service_ = profile->GetSSLConfigService();
329 ssl_config_service_->AddObserver(this);
332 ~RemoveChannelIDTester() override {
333 ssl_config_service_->RemoveObserver(this);
336 int ChannelIDCount() { return channel_id_service_->channel_id_count(); }
338 // Add a server bound cert for |server| with specific creation and expiry
339 // times. The cert and key data will be filled with dummy values.
340 void AddChannelIDWithTimes(const std::string& server_identifier,
341 base::Time creation_time) {
342 GetChannelIDStore()->SetChannelID(
343 make_scoped_ptr(new net::ChannelIDStore::ChannelID(
344 server_identifier, creation_time,
345 make_scoped_ptr(crypto::ECPrivateKey::Create()))));
348 // Add a server bound cert for |server|, with the current time as the
349 // creation time. The cert and key data will be filled with dummy values.
350 void AddChannelID(const std::string& server_identifier) {
351 base::Time now = base::Time::Now();
352 AddChannelIDWithTimes(server_identifier, now);
355 void GetChannelIDList(net::ChannelIDStore::ChannelIDList* channel_ids) {
356 GetChannelIDStore()->GetAllChannelIDs(
357 base::Bind(&RemoveChannelIDTester::GetAllChannelIDsCallback,
358 channel_ids));
361 net::ChannelIDStore* GetChannelIDStore() {
362 return channel_id_service_->GetChannelIDStore();
365 int ssl_config_changed_count() const {
366 return ssl_config_changed_count_;
369 // net::SSLConfigService::Observer implementation:
370 void OnSSLConfigChanged() override { ssl_config_changed_count_++; }
372 private:
373 static void GetAllChannelIDsCallback(
374 net::ChannelIDStore::ChannelIDList* dest,
375 const net::ChannelIDStore::ChannelIDList& result) {
376 *dest = result;
379 net::ChannelIDService* channel_id_service_;
380 scoped_refptr<net::SSLConfigService> ssl_config_service_;
381 int ssl_config_changed_count_;
383 DISALLOW_COPY_AND_ASSIGN(RemoveChannelIDTester);
386 class RemoveHistoryTester {
387 public:
388 RemoveHistoryTester() : query_url_success_(false), history_service_(NULL) {}
390 bool Init(TestingProfile* profile) WARN_UNUSED_RESULT {
391 if (!profile->CreateHistoryService(true, false))
392 return false;
393 history_service_ = HistoryServiceFactory::GetForProfile(
394 profile, ServiceAccessType::EXPLICIT_ACCESS);
395 return true;
398 // Returns true, if the given URL exists in the history service.
399 bool HistoryContainsURL(const GURL& url) {
400 scoped_refptr<content::MessageLoopRunner> message_loop_runner =
401 new content::MessageLoopRunner;
402 quit_closure_ = message_loop_runner->QuitClosure();
403 history_service_->QueryURL(
404 url,
405 true,
406 base::Bind(&RemoveHistoryTester::SaveResultAndQuit,
407 base::Unretained(this)),
408 &tracker_);
409 message_loop_runner->Run();
410 return query_url_success_;
413 void AddHistory(const GURL& url, base::Time time) {
414 history_service_->AddPage(url, time, NULL, 0, GURL(),
415 history::RedirectList(), ui::PAGE_TRANSITION_LINK,
416 history::SOURCE_BROWSED, false);
419 private:
420 // Callback for HistoryService::QueryURL.
421 void SaveResultAndQuit(bool success,
422 const history::URLRow&,
423 const history::VisitVector&) {
424 query_url_success_ = success;
425 quit_closure_.Run();
428 // For History requests.
429 base::CancelableTaskTracker tracker_;
430 bool query_url_success_;
431 base::Closure quit_closure_;
433 // TestingProfile owns the history service; we shouldn't delete it.
434 history::HistoryService* history_service_;
436 DISALLOW_COPY_AND_ASSIGN(RemoveHistoryTester);
439 class RemoveFaviconTester {
440 public:
441 RemoveFaviconTester()
442 : got_favicon_(false),
443 got_expired_favicon_(false),
444 history_service_(nullptr),
445 favicon_service_(nullptr) {}
447 bool Init(TestingProfile* profile) WARN_UNUSED_RESULT {
448 // Create the history service if it has not been created yet.
449 history_service_ = HistoryServiceFactory::GetForProfile(
450 profile, ServiceAccessType::EXPLICIT_ACCESS);
451 if (!history_service_) {
452 if (!profile->CreateHistoryService(true, false))
453 return false;
454 history_service_ = HistoryServiceFactory::GetForProfile(
455 profile, ServiceAccessType::EXPLICIT_ACCESS);
458 profile->CreateFaviconService();
459 favicon_service_ = FaviconServiceFactory::GetForProfile(
460 profile, ServiceAccessType::EXPLICIT_ACCESS);
461 return true;
464 // Returns true if there is a favicon stored for |page_url| in the favicon
465 // database.
466 bool HasFaviconForPageURL(const GURL& page_url) {
467 RequestFaviconSyncForPageURL(page_url);
468 return got_favicon_;
471 // Returns true if:
472 // - There is a favicon stored for |page_url| in the favicon database.
473 // - The stored favicon is expired.
474 bool HasExpiredFaviconForPageURL(const GURL& page_url) {
475 RequestFaviconSyncForPageURL(page_url);
476 return got_expired_favicon_;
479 // Adds a visit to history and stores an arbitrary favicon bitmap for
480 // |page_url|.
481 void VisitAndAddFavicon(const GURL& page_url) {
482 history_service_->AddPage(page_url, base::Time::Now(), nullptr, 0, GURL(),
483 history::RedirectList(), ui::PAGE_TRANSITION_LINK,
484 history::SOURCE_BROWSED, false);
486 SkBitmap bitmap;
487 bitmap.allocN32Pixels(gfx::kFaviconSize, gfx::kFaviconSize);
488 bitmap.eraseColor(SK_ColorBLUE);
489 favicon_service_->SetFavicons(page_url, page_url, favicon_base::FAVICON,
490 gfx::Image::CreateFrom1xBitmap(bitmap));
493 private:
494 // Synchronously requests the favicon for |page_url| from the favicon
495 // database.
496 void RequestFaviconSyncForPageURL(const GURL& page_url) {
497 base::RunLoop run_loop;
498 quit_closure_ = run_loop.QuitClosure();
499 favicon_service_->GetRawFaviconForPageURL(
500 page_url,
501 favicon_base::FAVICON,
502 gfx::kFaviconSize,
503 base::Bind(&RemoveFaviconTester::SaveResultAndQuit,
504 base::Unretained(this)),
505 &tracker_);
506 run_loop.Run();
509 // Callback for HistoryService::QueryURL.
510 void SaveResultAndQuit(const favicon_base::FaviconRawBitmapResult& result) {
511 got_favicon_ = result.is_valid();
512 got_expired_favicon_ = result.is_valid() && result.expired;
513 quit_closure_.Run();
516 // For favicon requests.
517 base::CancelableTaskTracker tracker_;
518 bool got_favicon_;
519 bool got_expired_favicon_;
520 base::Closure quit_closure_;
522 // Owned by TestingProfile.
523 history::HistoryService* history_service_;
524 favicon::FaviconService* favicon_service_;
526 DISALLOW_COPY_AND_ASSIGN(RemoveFaviconTester);
529 class RemoveAutofillTester : public autofill::PersonalDataManagerObserver {
530 public:
531 explicit RemoveAutofillTester(TestingProfile* profile)
532 : personal_data_manager_(
533 autofill::PersonalDataManagerFactory::GetForProfile(profile)) {
534 autofill::test::DisableSystemServices(profile->GetPrefs());
535 personal_data_manager_->AddObserver(this);
538 ~RemoveAutofillTester() override {
539 personal_data_manager_->RemoveObserver(this);
542 // Returns true if there are autofill profiles.
543 bool HasProfile() {
544 return !personal_data_manager_->GetProfiles().empty() &&
545 !personal_data_manager_->GetCreditCards().empty();
548 bool HasOrigin(const std::string& origin) {
549 const std::vector<autofill::AutofillProfile*>& profiles =
550 personal_data_manager_->GetProfiles();
551 for (std::vector<autofill::AutofillProfile*>::const_iterator it =
552 profiles.begin();
553 it != profiles.end(); ++it) {
554 if ((*it)->origin() == origin)
555 return true;
558 const std::vector<autofill::CreditCard*>& credit_cards =
559 personal_data_manager_->GetCreditCards();
560 for (std::vector<autofill::CreditCard*>::const_iterator it =
561 credit_cards.begin();
562 it != credit_cards.end(); ++it) {
563 if ((*it)->origin() == origin)
564 return true;
567 return false;
570 // Add two profiles and two credit cards to the database. In each pair, one
571 // entry has a web origin and the other has a Chrome origin.
572 void AddProfilesAndCards() {
573 std::vector<autofill::AutofillProfile> profiles;
574 autofill::AutofillProfile profile;
575 profile.set_guid(base::GenerateGUID());
576 profile.set_origin(kWebOrigin);
577 profile.SetRawInfo(autofill::NAME_FIRST, base::ASCIIToUTF16("Bob"));
578 profile.SetRawInfo(autofill::NAME_LAST, base::ASCIIToUTF16("Smith"));
579 profile.SetRawInfo(autofill::ADDRESS_HOME_ZIP, base::ASCIIToUTF16("94043"));
580 profile.SetRawInfo(autofill::EMAIL_ADDRESS,
581 base::ASCIIToUTF16("sue@example.com"));
582 profile.SetRawInfo(autofill::COMPANY_NAME, base::ASCIIToUTF16("Company X"));
583 profiles.push_back(profile);
585 profile.set_guid(base::GenerateGUID());
586 profile.set_origin(kChromeOrigin);
587 profiles.push_back(profile);
589 personal_data_manager_->SetProfiles(&profiles);
590 base::MessageLoop::current()->Run();
592 std::vector<autofill::CreditCard> cards;
593 autofill::CreditCard card;
594 card.set_guid(base::GenerateGUID());
595 card.set_origin(kWebOrigin);
596 card.SetRawInfo(autofill::CREDIT_CARD_NUMBER,
597 base::ASCIIToUTF16("1234-5678-9012-3456"));
598 cards.push_back(card);
600 card.set_guid(base::GenerateGUID());
601 card.set_origin(kChromeOrigin);
602 cards.push_back(card);
604 personal_data_manager_->SetCreditCards(&cards);
605 base::MessageLoop::current()->Run();
608 private:
609 void OnPersonalDataChanged() override {
610 base::MessageLoop::current()->Quit();
613 autofill::PersonalDataManager* personal_data_manager_;
614 DISALLOW_COPY_AND_ASSIGN(RemoveAutofillTester);
617 class RemoveLocalStorageTester {
618 public:
619 explicit RemoveLocalStorageTester(TestingProfile* profile)
620 : profile_(profile), dom_storage_context_(NULL) {
621 dom_storage_context_ =
622 content::BrowserContext::GetDefaultStoragePartition(profile)->
623 GetDOMStorageContext();
626 // Returns true, if the given origin URL exists.
627 bool DOMStorageExistsForOrigin(const GURL& origin) {
628 scoped_refptr<content::MessageLoopRunner> message_loop_runner =
629 new content::MessageLoopRunner;
630 quit_closure_ = message_loop_runner->QuitClosure();
631 GetLocalStorageUsage();
632 message_loop_runner->Run();
633 for (size_t i = 0; i < infos_.size(); ++i) {
634 if (origin == infos_[i].origin)
635 return true;
637 return false;
640 void AddDOMStorageTestData() {
641 // Note: This test depends on details of how the dom_storage library
642 // stores data in the host file system.
643 base::FilePath storage_path =
644 profile_->GetPath().AppendASCII("Local Storage");
645 base::CreateDirectory(storage_path);
647 // Write some files.
648 base::WriteFile(storage_path.Append(kDomStorageOrigin1), NULL, 0);
649 base::WriteFile(storage_path.Append(kDomStorageOrigin2), NULL, 0);
650 base::WriteFile(storage_path.Append(kDomStorageOrigin3), NULL, 0);
651 base::WriteFile(storage_path.Append(kDomStorageExt), NULL, 0);
653 // Tweak their dates.
654 base::Time now = base::Time::Now();
655 base::TouchFile(storage_path.Append(kDomStorageOrigin1), now, now);
657 base::Time one_day_ago = now - base::TimeDelta::FromDays(1);
658 base::TouchFile(storage_path.Append(kDomStorageOrigin2),
659 one_day_ago, one_day_ago);
661 base::Time sixty_days_ago = now - base::TimeDelta::FromDays(60);
662 base::TouchFile(storage_path.Append(kDomStorageOrigin3),
663 sixty_days_ago, sixty_days_ago);
665 base::TouchFile(storage_path.Append(kDomStorageExt), now, now);
668 private:
669 void GetLocalStorageUsage() {
670 dom_storage_context_->GetLocalStorageUsage(
671 base::Bind(&RemoveLocalStorageTester::OnGotLocalStorageUsage,
672 base::Unretained(this)));
674 void OnGotLocalStorageUsage(
675 const std::vector<content::LocalStorageUsageInfo>& infos) {
676 infos_ = infos;
677 quit_closure_.Run();
680 // We don't own these pointers.
681 TestingProfile* profile_;
682 content::DOMStorageContext* dom_storage_context_;
684 std::vector<content::LocalStorageUsageInfo> infos_;
685 base::Closure quit_closure_;
687 DISALLOW_COPY_AND_ASSIGN(RemoveLocalStorageTester);
690 class MockDomainReliabilityService : public DomainReliabilityService {
691 public:
692 MockDomainReliabilityService() : clear_count_(0) {}
694 ~MockDomainReliabilityService() override {}
696 scoped_ptr<DomainReliabilityMonitor> CreateMonitor(
697 scoped_refptr<base::SingleThreadTaskRunner> network_task_runner)
698 override {
699 NOTREACHED();
700 return scoped_ptr<DomainReliabilityMonitor>();
703 void ClearBrowsingData(DomainReliabilityClearMode clear_mode,
704 const base::Closure& callback) override {
705 clear_count_++;
706 last_clear_mode_ = clear_mode;
707 callback.Run();
710 void GetWebUIData(const base::Callback<void(scoped_ptr<base::Value>)>&
711 callback) const override {
712 NOTREACHED();
715 int clear_count() const { return clear_count_; }
717 DomainReliabilityClearMode last_clear_mode() const {
718 return last_clear_mode_;
721 private:
722 unsigned clear_count_;
723 DomainReliabilityClearMode last_clear_mode_;
726 struct TestingDomainReliabilityServiceFactoryUserData
727 : public base::SupportsUserData::Data {
728 TestingDomainReliabilityServiceFactoryUserData(
729 content::BrowserContext* context,
730 MockDomainReliabilityService* service)
731 : context(context),
732 service(service),
733 attached(false) {}
734 ~TestingDomainReliabilityServiceFactoryUserData() override {}
736 content::BrowserContext* const context;
737 MockDomainReliabilityService* const service;
738 bool attached;
740 static const void* kKey;
743 // static
744 const void* TestingDomainReliabilityServiceFactoryUserData::kKey =
745 &TestingDomainReliabilityServiceFactoryUserData::kKey;
747 KeyedService* TestingDomainReliabilityServiceFactoryFunction(
748 content::BrowserContext* context) {
749 const void* kKey = TestingDomainReliabilityServiceFactoryUserData::kKey;
751 TestingDomainReliabilityServiceFactoryUserData* data =
752 static_cast<TestingDomainReliabilityServiceFactoryUserData*>(
753 context->GetUserData(kKey));
754 EXPECT_TRUE(data);
755 EXPECT_EQ(data->context, context);
756 EXPECT_FALSE(data->attached);
758 data->attached = true;
759 return data->service;
762 class ClearDomainReliabilityTester {
763 public:
764 explicit ClearDomainReliabilityTester(TestingProfile* profile) :
765 profile_(profile),
766 mock_service_(new MockDomainReliabilityService()) {
767 AttachService();
770 unsigned clear_count() const { return mock_service_->clear_count(); }
772 DomainReliabilityClearMode last_clear_mode() const {
773 return mock_service_->last_clear_mode();
776 private:
777 void AttachService() {
778 const void* kKey = TestingDomainReliabilityServiceFactoryUserData::kKey;
780 // Attach kludgey UserData struct to profile.
781 TestingDomainReliabilityServiceFactoryUserData* data =
782 new TestingDomainReliabilityServiceFactoryUserData(profile_,
783 mock_service_);
784 EXPECT_FALSE(profile_->GetUserData(kKey));
785 profile_->SetUserData(kKey, data);
787 // Set and use factory that will attach service stuffed in kludgey struct.
788 DomainReliabilityServiceFactory::GetInstance()->SetTestingFactoryAndUse(
789 profile_,
790 &TestingDomainReliabilityServiceFactoryFunction);
792 // Verify and detach kludgey struct.
793 EXPECT_EQ(data, profile_->GetUserData(kKey));
794 EXPECT_TRUE(data->attached);
795 profile_->RemoveUserData(kKey);
798 TestingProfile* profile_;
799 MockDomainReliabilityService* mock_service_;
802 // Test Class ----------------------------------------------------------------
804 class BrowsingDataRemoverTest : public testing::Test {
805 public:
806 BrowsingDataRemoverTest()
807 : profile_(new TestingProfile()),
808 clear_domain_reliability_tester_(GetProfile()) {
809 callback_subscription_ =
810 BrowsingDataRemover::RegisterOnBrowsingDataRemovedCallback(
811 base::Bind(&BrowsingDataRemoverTest::NotifyWithDetails,
812 base::Unretained(this)));
815 ~BrowsingDataRemoverTest() override {}
817 void TearDown() override {
818 #if defined(ENABLE_EXTENSIONS)
819 mock_policy_ = NULL;
820 #endif
822 // TestingProfile contains a DOMStorageContext. BrowserContext's destructor
823 // posts a message to the WEBKIT thread to delete some of its member
824 // variables. We need to ensure that the profile is destroyed, and that
825 // the message loop is cleared out, before destroying the threads and loop.
826 // Otherwise we leak memory.
827 profile_.reset();
828 base::MessageLoop::current()->RunUntilIdle();
830 TestingBrowserProcess::GetGlobal()->SetLocalState(NULL);
833 void BlockUntilBrowsingDataRemoved(BrowsingDataRemover::TimePeriod period,
834 int remove_mask,
835 bool include_protected_origins) {
836 BrowsingDataRemover* remover = BrowsingDataRemover::CreateForPeriod(
837 profile_.get(), period);
839 TestStoragePartition storage_partition;
840 remover->OverrideStoragePartitionForTesting(&storage_partition);
842 called_with_details_.reset(new BrowsingDataRemover::NotificationDetails());
844 // BrowsingDataRemover deletes itself when it completes.
845 int origin_set_mask = BrowsingDataHelper::UNPROTECTED_WEB;
846 if (include_protected_origins)
847 origin_set_mask |= BrowsingDataHelper::PROTECTED_WEB;
849 BrowsingDataRemoverCompletionObserver completion_observer(remover);
850 remover->Remove(remove_mask, origin_set_mask);
851 completion_observer.BlockUntilCompletion();
853 // Save so we can verify later.
854 storage_partition_removal_data_ =
855 storage_partition.GetStoragePartitionRemovalData();
858 void BlockUntilOriginDataRemoved(BrowsingDataRemover::TimePeriod period,
859 int remove_mask,
860 const GURL& remove_origin) {
861 BrowsingDataRemover* remover = BrowsingDataRemover::CreateForPeriod(
862 profile_.get(), period);
863 TestStoragePartition storage_partition;
864 remover->OverrideStoragePartitionForTesting(&storage_partition);
866 called_with_details_.reset(new BrowsingDataRemover::NotificationDetails());
868 // BrowsingDataRemover deletes itself when it completes.
869 BrowsingDataRemoverCompletionObserver completion_observer(remover);
870 remover->RemoveImpl(remove_mask, remove_origin,
871 BrowsingDataHelper::UNPROTECTED_WEB);
872 completion_observer.BlockUntilCompletion();
874 // Save so we can verify later.
875 storage_partition_removal_data_ =
876 storage_partition.GetStoragePartitionRemovalData();
879 TestingProfile* GetProfile() {
880 return profile_.get();
883 base::Time GetBeginTime() {
884 return called_with_details_->removal_begin;
887 int GetRemovalMask() {
888 return called_with_details_->removal_mask;
891 int GetOriginSetMask() {
892 return called_with_details_->origin_set_mask;
895 StoragePartitionRemovalData GetStoragePartitionRemovalData() {
896 return storage_partition_removal_data_;
899 // Callback for browsing data removal events.
900 void NotifyWithDetails(
901 const BrowsingDataRemover::NotificationDetails& details) {
902 // We're not taking ownership of the details object, but storing a copy of
903 // it locally.
904 called_with_details_.reset(
905 new BrowsingDataRemover::NotificationDetails(details));
907 callback_subscription_.reset();
910 MockExtensionSpecialStoragePolicy* CreateMockPolicy() {
911 #if defined(ENABLE_EXTENSIONS)
912 mock_policy_ = new MockExtensionSpecialStoragePolicy;
913 return mock_policy_.get();
914 #else
915 NOTREACHED();
916 return NULL;
917 #endif
920 storage::SpecialStoragePolicy* mock_policy() {
921 #if defined(ENABLE_EXTENSIONS)
922 return mock_policy_.get();
923 #else
924 return NULL;
925 #endif
928 // If |kOrigin1| is protected when extensions are enabled, the expected
929 // result for tests where the OriginMatcherFunction result is variable.
930 bool ShouldRemoveForProtectedOriginOne() const {
931 #if defined(ENABLE_EXTENSIONS)
932 return false;
933 #else
934 return true;
935 #endif
938 const ClearDomainReliabilityTester& clear_domain_reliability_tester() {
939 return clear_domain_reliability_tester_;
942 protected:
943 scoped_ptr<BrowsingDataRemover::NotificationDetails> called_with_details_;
945 private:
946 content::TestBrowserThreadBundle thread_bundle_;
947 scoped_ptr<TestingProfile> profile_;
949 StoragePartitionRemovalData storage_partition_removal_data_;
951 #if defined(ENABLE_EXTENSIONS)
952 scoped_refptr<MockExtensionSpecialStoragePolicy> mock_policy_;
953 #endif
955 BrowsingDataRemover::CallbackSubscription callback_subscription_;
957 // Needed to mock out DomainReliabilityService, even for unrelated tests.
958 ClearDomainReliabilityTester clear_domain_reliability_tester_;
960 DISALLOW_COPY_AND_ASSIGN(BrowsingDataRemoverTest);
963 // Tests ---------------------------------------------------------------------
965 TEST_F(BrowsingDataRemoverTest, RemoveCookieForever) {
966 BlockUntilBrowsingDataRemoved(BrowsingDataRemover::EVERYTHING,
967 BrowsingDataRemover::REMOVE_COOKIES,
968 false);
970 EXPECT_EQ(BrowsingDataRemover::REMOVE_COOKIES, GetRemovalMask());
971 EXPECT_EQ(BrowsingDataHelper::UNPROTECTED_WEB, GetOriginSetMask());
973 // Verify that storage partition was instructed to remove the cookies.
974 StoragePartitionRemovalData removal_data = GetStoragePartitionRemovalData();
975 EXPECT_EQ(removal_data.remove_mask,
976 StoragePartition::REMOVE_DATA_MASK_COOKIES);
977 EXPECT_EQ(removal_data.quota_storage_remove_mask,
978 StoragePartition::QUOTA_MANAGED_STORAGE_MASK_ALL);
979 EXPECT_TRUE(removal_data.remove_origin.is_empty());
980 EXPECT_EQ(removal_data.remove_begin, GetBeginTime());
983 TEST_F(BrowsingDataRemoverTest, RemoveCookieLastHour) {
984 BlockUntilBrowsingDataRemoved(BrowsingDataRemover::LAST_HOUR,
985 BrowsingDataRemover::REMOVE_COOKIES,
986 false);
988 EXPECT_EQ(BrowsingDataRemover::REMOVE_COOKIES, GetRemovalMask());
989 EXPECT_EQ(BrowsingDataHelper::UNPROTECTED_WEB, GetOriginSetMask());
991 // Verify that storage partition was instructed to remove the cookies.
992 StoragePartitionRemovalData removal_data = GetStoragePartitionRemovalData();
993 EXPECT_EQ(removal_data.remove_mask,
994 StoragePartition::REMOVE_DATA_MASK_COOKIES);
995 // Removing with time period other than EVERYTHING should not clear
996 // persistent storage data.
997 EXPECT_EQ(removal_data.quota_storage_remove_mask,
998 ~StoragePartition::QUOTA_MANAGED_STORAGE_MASK_PERSISTENT);
999 EXPECT_TRUE(removal_data.remove_origin.is_empty());
1000 EXPECT_EQ(removal_data.remove_begin, GetBeginTime());
1003 #if defined(SAFE_BROWSING_SERVICE)
1004 TEST_F(BrowsingDataRemoverTest, RemoveSafeBrowsingCookieForever) {
1005 RemoveSafeBrowsingCookieTester tester;
1007 tester.AddCookie();
1008 ASSERT_TRUE(tester.ContainsCookie());
1010 BlockUntilBrowsingDataRemoved(BrowsingDataRemover::EVERYTHING,
1011 BrowsingDataRemover::REMOVE_COOKIES, false);
1013 EXPECT_EQ(BrowsingDataRemover::REMOVE_COOKIES, GetRemovalMask());
1014 EXPECT_EQ(BrowsingDataHelper::UNPROTECTED_WEB, GetOriginSetMask());
1015 EXPECT_FALSE(tester.ContainsCookie());
1018 TEST_F(BrowsingDataRemoverTest, RemoveSafeBrowsingCookieLastHour) {
1019 RemoveSafeBrowsingCookieTester tester;
1021 tester.AddCookie();
1022 ASSERT_TRUE(tester.ContainsCookie());
1024 BlockUntilBrowsingDataRemoved(BrowsingDataRemover::LAST_HOUR,
1025 BrowsingDataRemover::REMOVE_COOKIES, false);
1027 EXPECT_EQ(BrowsingDataRemover::REMOVE_COOKIES, GetRemovalMask());
1028 EXPECT_EQ(BrowsingDataHelper::UNPROTECTED_WEB, GetOriginSetMask());
1029 // Removing with time period other than EVERYTHING should not clear safe
1030 // browsing cookies.
1031 EXPECT_TRUE(tester.ContainsCookie());
1033 #endif
1035 TEST_F(BrowsingDataRemoverTest, RemoveChannelIDForever) {
1036 RemoveChannelIDTester tester(GetProfile());
1038 tester.AddChannelID(kTestOrigin1);
1039 EXPECT_EQ(0, tester.ssl_config_changed_count());
1040 EXPECT_EQ(1, tester.ChannelIDCount());
1042 BlockUntilBrowsingDataRemoved(BrowsingDataRemover::EVERYTHING,
1043 BrowsingDataRemover::REMOVE_CHANNEL_IDS, false);
1045 EXPECT_EQ(BrowsingDataRemover::REMOVE_CHANNEL_IDS, GetRemovalMask());
1046 EXPECT_EQ(BrowsingDataHelper::UNPROTECTED_WEB, GetOriginSetMask());
1047 EXPECT_EQ(1, tester.ssl_config_changed_count());
1048 EXPECT_EQ(0, tester.ChannelIDCount());
1051 TEST_F(BrowsingDataRemoverTest, RemoveChannelIDLastHour) {
1052 RemoveChannelIDTester tester(GetProfile());
1054 base::Time now = base::Time::Now();
1055 tester.AddChannelID(kTestOrigin1);
1056 tester.AddChannelIDWithTimes(kTestOrigin2,
1057 now - base::TimeDelta::FromHours(2));
1058 EXPECT_EQ(0, tester.ssl_config_changed_count());
1059 EXPECT_EQ(2, tester.ChannelIDCount());
1061 BlockUntilBrowsingDataRemoved(BrowsingDataRemover::LAST_HOUR,
1062 BrowsingDataRemover::REMOVE_CHANNEL_IDS, false);
1064 EXPECT_EQ(BrowsingDataRemover::REMOVE_CHANNEL_IDS, GetRemovalMask());
1065 EXPECT_EQ(BrowsingDataHelper::UNPROTECTED_WEB, GetOriginSetMask());
1066 EXPECT_EQ(1, tester.ssl_config_changed_count());
1067 ASSERT_EQ(1, tester.ChannelIDCount());
1068 net::ChannelIDStore::ChannelIDList channel_ids;
1069 tester.GetChannelIDList(&channel_ids);
1070 ASSERT_EQ(1U, channel_ids.size());
1071 EXPECT_EQ(kTestOrigin2, channel_ids.front().server_identifier());
1074 TEST_F(BrowsingDataRemoverTest, RemoveUnprotectedLocalStorageForever) {
1075 #if defined(ENABLE_EXTENSIONS)
1076 MockExtensionSpecialStoragePolicy* policy = CreateMockPolicy();
1077 // Protect kOrigin1.
1078 policy->AddProtected(kOrigin1.GetOrigin());
1079 #endif
1081 BlockUntilBrowsingDataRemoved(BrowsingDataRemover::EVERYTHING,
1082 BrowsingDataRemover::REMOVE_LOCAL_STORAGE,
1083 false);
1085 EXPECT_EQ(BrowsingDataRemover::REMOVE_LOCAL_STORAGE, GetRemovalMask());
1086 EXPECT_EQ(BrowsingDataHelper::UNPROTECTED_WEB, GetOriginSetMask());
1088 // Verify that storage partition was instructed to remove the data correctly.
1089 StoragePartitionRemovalData removal_data = GetStoragePartitionRemovalData();
1090 EXPECT_EQ(removal_data.remove_mask,
1091 StoragePartition::REMOVE_DATA_MASK_LOCAL_STORAGE);
1092 EXPECT_EQ(removal_data.quota_storage_remove_mask,
1093 StoragePartition::QUOTA_MANAGED_STORAGE_MASK_ALL);
1094 EXPECT_TRUE(removal_data.remove_origin.is_empty());
1095 EXPECT_EQ(removal_data.remove_begin, GetBeginTime());
1097 // Check origin matcher.
1098 EXPECT_EQ(ShouldRemoveForProtectedOriginOne(),
1099 removal_data.origin_matcher.Run(kOrigin1, mock_policy()));
1100 EXPECT_TRUE(removal_data.origin_matcher.Run(kOrigin2, mock_policy()));
1101 EXPECT_TRUE(removal_data.origin_matcher.Run(kOrigin3, mock_policy()));
1102 EXPECT_FALSE(removal_data.origin_matcher.Run(kOriginExt, mock_policy()));
1105 TEST_F(BrowsingDataRemoverTest, RemoveProtectedLocalStorageForever) {
1106 #if defined(ENABLE_EXTENSIONS)
1107 // Protect kOrigin1.
1108 MockExtensionSpecialStoragePolicy* policy = CreateMockPolicy();
1109 policy->AddProtected(kOrigin1.GetOrigin());
1110 #endif
1112 BlockUntilBrowsingDataRemoved(BrowsingDataRemover::EVERYTHING,
1113 BrowsingDataRemover::REMOVE_LOCAL_STORAGE,
1114 true);
1116 EXPECT_EQ(BrowsingDataRemover::REMOVE_LOCAL_STORAGE, GetRemovalMask());
1117 EXPECT_EQ(BrowsingDataHelper::UNPROTECTED_WEB |
1118 BrowsingDataHelper::PROTECTED_WEB, GetOriginSetMask());
1120 // Verify that storage partition was instructed to remove the data correctly.
1121 StoragePartitionRemovalData removal_data = GetStoragePartitionRemovalData();
1122 EXPECT_EQ(removal_data.remove_mask,
1123 StoragePartition::REMOVE_DATA_MASK_LOCAL_STORAGE);
1124 EXPECT_EQ(removal_data.quota_storage_remove_mask,
1125 StoragePartition::QUOTA_MANAGED_STORAGE_MASK_ALL);
1126 EXPECT_TRUE(removal_data.remove_origin.is_empty());
1127 EXPECT_EQ(removal_data.remove_begin, GetBeginTime());
1129 // Check origin matcher all http origin will match since we specified
1130 // both protected and unprotected.
1131 EXPECT_TRUE(removal_data.origin_matcher.Run(kOrigin1, mock_policy()));
1132 EXPECT_TRUE(removal_data.origin_matcher.Run(kOrigin2, mock_policy()));
1133 EXPECT_TRUE(removal_data.origin_matcher.Run(kOrigin3, mock_policy()));
1134 EXPECT_FALSE(removal_data.origin_matcher.Run(kOriginExt, mock_policy()));
1137 TEST_F(BrowsingDataRemoverTest, RemoveLocalStorageForLastWeek) {
1138 #if defined(ENABLE_EXTENSIONS)
1139 CreateMockPolicy();
1140 #endif
1142 BlockUntilBrowsingDataRemoved(BrowsingDataRemover::LAST_WEEK,
1143 BrowsingDataRemover::REMOVE_LOCAL_STORAGE,
1144 false);
1146 EXPECT_EQ(BrowsingDataRemover::REMOVE_LOCAL_STORAGE, GetRemovalMask());
1147 EXPECT_EQ(BrowsingDataHelper::UNPROTECTED_WEB, GetOriginSetMask());
1149 // Verify that storage partition was instructed to remove the data correctly.
1150 StoragePartitionRemovalData removal_data = GetStoragePartitionRemovalData();
1151 EXPECT_EQ(removal_data.remove_mask,
1152 StoragePartition::REMOVE_DATA_MASK_LOCAL_STORAGE);
1153 // Persistent storage won't be deleted.
1154 EXPECT_EQ(removal_data.quota_storage_remove_mask,
1155 ~StoragePartition::QUOTA_MANAGED_STORAGE_MASK_PERSISTENT);
1156 EXPECT_TRUE(removal_data.remove_origin.is_empty());
1157 EXPECT_EQ(removal_data.remove_begin, GetBeginTime());
1159 // Check origin matcher.
1160 EXPECT_TRUE(removal_data.origin_matcher.Run(kOrigin1, mock_policy()));
1161 EXPECT_TRUE(removal_data.origin_matcher.Run(kOrigin2, mock_policy()));
1162 EXPECT_TRUE(removal_data.origin_matcher.Run(kOrigin3, mock_policy()));
1163 EXPECT_FALSE(removal_data.origin_matcher.Run(kOriginExt, mock_policy()));
1166 TEST_F(BrowsingDataRemoverTest, RemoveHistoryForever) {
1167 RemoveHistoryTester tester;
1168 ASSERT_TRUE(tester.Init(GetProfile()));
1170 tester.AddHistory(kOrigin1, base::Time::Now());
1171 ASSERT_TRUE(tester.HistoryContainsURL(kOrigin1));
1173 BlockUntilBrowsingDataRemoved(BrowsingDataRemover::EVERYTHING,
1174 BrowsingDataRemover::REMOVE_HISTORY, false);
1176 EXPECT_EQ(BrowsingDataRemover::REMOVE_HISTORY, GetRemovalMask());
1177 EXPECT_EQ(BrowsingDataHelper::UNPROTECTED_WEB, GetOriginSetMask());
1178 EXPECT_FALSE(tester.HistoryContainsURL(kOrigin1));
1181 TEST_F(BrowsingDataRemoverTest, RemoveHistoryForLastHour) {
1182 RemoveHistoryTester tester;
1183 ASSERT_TRUE(tester.Init(GetProfile()));
1185 base::Time two_hours_ago = base::Time::Now() - base::TimeDelta::FromHours(2);
1187 tester.AddHistory(kOrigin1, base::Time::Now());
1188 tester.AddHistory(kOrigin2, two_hours_ago);
1189 ASSERT_TRUE(tester.HistoryContainsURL(kOrigin1));
1190 ASSERT_TRUE(tester.HistoryContainsURL(kOrigin2));
1192 BlockUntilBrowsingDataRemoved(BrowsingDataRemover::LAST_HOUR,
1193 BrowsingDataRemover::REMOVE_HISTORY, false);
1195 EXPECT_EQ(BrowsingDataRemover::REMOVE_HISTORY, GetRemovalMask());
1196 EXPECT_EQ(BrowsingDataHelper::UNPROTECTED_WEB, GetOriginSetMask());
1197 EXPECT_FALSE(tester.HistoryContainsURL(kOrigin1));
1198 EXPECT_TRUE(tester.HistoryContainsURL(kOrigin2));
1201 // This should crash (DCHECK) in Debug, but death tests don't work properly
1202 // here.
1203 #if defined(NDEBUG) && !defined(DCHECK_ALWAYS_ON)
1204 TEST_F(BrowsingDataRemoverTest, RemoveHistoryProhibited) {
1205 RemoveHistoryTester tester;
1206 ASSERT_TRUE(tester.Init(GetProfile()));
1207 PrefService* prefs = GetProfile()->GetPrefs();
1208 prefs->SetBoolean(prefs::kAllowDeletingBrowserHistory, false);
1210 base::Time two_hours_ago = base::Time::Now() - base::TimeDelta::FromHours(2);
1212 tester.AddHistory(kOrigin1, base::Time::Now());
1213 tester.AddHistory(kOrigin2, two_hours_ago);
1214 ASSERT_TRUE(tester.HistoryContainsURL(kOrigin1));
1215 ASSERT_TRUE(tester.HistoryContainsURL(kOrigin2));
1217 BlockUntilBrowsingDataRemoved(BrowsingDataRemover::LAST_HOUR,
1218 BrowsingDataRemover::REMOVE_HISTORY, false);
1219 EXPECT_EQ(BrowsingDataRemover::REMOVE_HISTORY, GetRemovalMask());
1220 EXPECT_EQ(BrowsingDataHelper::UNPROTECTED_WEB, GetOriginSetMask());
1222 // Nothing should have been deleted.
1223 EXPECT_TRUE(tester.HistoryContainsURL(kOrigin1));
1224 EXPECT_TRUE(tester.HistoryContainsURL(kOrigin2));
1226 #endif
1228 TEST_F(BrowsingDataRemoverTest, RemoveMultipleTypes) {
1229 // Add some history.
1230 RemoveHistoryTester history_tester;
1231 ASSERT_TRUE(history_tester.Init(GetProfile()));
1232 history_tester.AddHistory(kOrigin1, base::Time::Now());
1233 ASSERT_TRUE(history_tester.HistoryContainsURL(kOrigin1));
1235 int removal_mask = BrowsingDataRemover::REMOVE_HISTORY |
1236 BrowsingDataRemover::REMOVE_COOKIES;
1238 BlockUntilBrowsingDataRemoved(BrowsingDataRemover::EVERYTHING,
1239 removal_mask, false);
1241 EXPECT_EQ(removal_mask, GetRemovalMask());
1242 EXPECT_EQ(BrowsingDataHelper::UNPROTECTED_WEB, GetOriginSetMask());
1243 EXPECT_FALSE(history_tester.HistoryContainsURL(kOrigin1));
1245 // The cookie would be deleted throught the StorageParition, check if the
1246 // partition was requested to remove cookie.
1247 StoragePartitionRemovalData removal_data = GetStoragePartitionRemovalData();
1248 EXPECT_EQ(removal_data.remove_mask,
1249 StoragePartition::REMOVE_DATA_MASK_COOKIES);
1250 EXPECT_EQ(removal_data.quota_storage_remove_mask,
1251 StoragePartition::QUOTA_MANAGED_STORAGE_MASK_ALL);
1254 // This should crash (DCHECK) in Debug, but death tests don't work properly
1255 // here.
1256 #if defined(NDEBUG) && !defined(DCHECK_ALWAYS_ON)
1257 TEST_F(BrowsingDataRemoverTest, RemoveMultipleTypesHistoryProhibited) {
1258 PrefService* prefs = GetProfile()->GetPrefs();
1259 prefs->SetBoolean(prefs::kAllowDeletingBrowserHistory, false);
1261 // Add some history.
1262 RemoveHistoryTester history_tester;
1263 ASSERT_TRUE(history_tester.Init(GetProfile()));
1264 history_tester.AddHistory(kOrigin1, base::Time::Now());
1265 ASSERT_TRUE(history_tester.HistoryContainsURL(kOrigin1));
1267 int removal_mask = BrowsingDataRemover::REMOVE_HISTORY |
1268 BrowsingDataRemover::REMOVE_COOKIES;
1270 BlockUntilBrowsingDataRemoved(BrowsingDataRemover::LAST_HOUR,
1271 removal_mask, false);
1272 EXPECT_EQ(removal_mask, GetRemovalMask());
1273 EXPECT_EQ(BrowsingDataHelper::UNPROTECTED_WEB, GetOriginSetMask());
1275 // 1/2. History should remain.
1276 EXPECT_TRUE(history_tester.HistoryContainsURL(kOrigin1));
1278 // 2/2. The cookie(s) would be deleted throught the StorageParition, check if
1279 // the partition was requested to remove cookie.
1280 StoragePartitionRemovalData removal_data = GetStoragePartitionRemovalData();
1281 EXPECT_EQ(removal_data.remove_mask,
1282 StoragePartition::REMOVE_DATA_MASK_COOKIES);
1283 // Persistent storage won't be deleted, since EVERYTHING was not specified.
1284 EXPECT_EQ(removal_data.quota_storage_remove_mask,
1285 ~StoragePartition::QUOTA_MANAGED_STORAGE_MASK_PERSISTENT);
1287 #endif
1289 // Test that clearing history deletes favicons not associated with bookmarks.
1290 TEST_F(BrowsingDataRemoverTest, RemoveFaviconsForever) {
1291 GURL page_url("http://a");
1293 RemoveFaviconTester favicon_tester;
1294 ASSERT_TRUE(favicon_tester.Init(GetProfile()));
1295 favicon_tester.VisitAndAddFavicon(page_url);
1296 ASSERT_TRUE(favicon_tester.HasFaviconForPageURL(page_url));
1298 BlockUntilBrowsingDataRemoved(BrowsingDataRemover::EVERYTHING,
1299 BrowsingDataRemover::REMOVE_HISTORY, false);
1300 EXPECT_EQ(BrowsingDataRemover::REMOVE_HISTORY, GetRemovalMask());
1301 EXPECT_FALSE(favicon_tester.HasFaviconForPageURL(page_url));
1304 // Test that a bookmark's favicon is expired and not deleted when clearing
1305 // history. Expiring the favicon causes the bookmark's favicon to be updated
1306 // when the user next visits the bookmarked page. Expiring the bookmark's
1307 // favicon is useful when the bookmark's favicon becomes incorrect (See
1308 // crbug.com/474421 for a sample bug which causes this).
1309 TEST_F(BrowsingDataRemoverTest, ExpireBookmarkFavicons) {
1310 GURL bookmarked_page("http://a");
1312 TestingProfile* profile = GetProfile();
1313 profile->CreateBookmarkModel(true);
1314 bookmarks::BookmarkModel* bookmark_model =
1315 BookmarkModelFactory::GetForProfile(profile);
1316 bookmarks::test::WaitForBookmarkModelToLoad(bookmark_model);
1317 bookmark_model->AddURL(bookmark_model->bookmark_bar_node(), 0,
1318 base::ASCIIToUTF16("a"), bookmarked_page);
1320 RemoveFaviconTester favicon_tester;
1321 ASSERT_TRUE(favicon_tester.Init(GetProfile()));
1322 favicon_tester.VisitAndAddFavicon(bookmarked_page);
1323 ASSERT_TRUE(favicon_tester.HasFaviconForPageURL(bookmarked_page));
1325 BlockUntilBrowsingDataRemoved(BrowsingDataRemover::EVERYTHING,
1326 BrowsingDataRemover::REMOVE_HISTORY, false);
1327 EXPECT_EQ(BrowsingDataRemover::REMOVE_HISTORY, GetRemovalMask());
1328 EXPECT_TRUE(favicon_tester.HasExpiredFaviconForPageURL(bookmarked_page));
1331 TEST_F(BrowsingDataRemoverTest, RemoveQuotaManagedDataForeverBoth) {
1332 BlockUntilBrowsingDataRemoved(BrowsingDataRemover::EVERYTHING,
1333 BrowsingDataRemover::REMOVE_FILE_SYSTEMS |
1334 BrowsingDataRemover::REMOVE_WEBSQL |
1335 BrowsingDataRemover::REMOVE_APPCACHE |
1336 BrowsingDataRemover::REMOVE_SERVICE_WORKERS |
1337 BrowsingDataRemover::REMOVE_INDEXEDDB,
1338 false);
1340 EXPECT_EQ(BrowsingDataRemover::REMOVE_FILE_SYSTEMS |
1341 BrowsingDataRemover::REMOVE_WEBSQL |
1342 BrowsingDataRemover::REMOVE_APPCACHE |
1343 BrowsingDataRemover::REMOVE_SERVICE_WORKERS |
1344 BrowsingDataRemover::REMOVE_INDEXEDDB, GetRemovalMask());
1345 EXPECT_EQ(BrowsingDataHelper::UNPROTECTED_WEB, GetOriginSetMask());
1347 // Verify storage partition related stuffs.
1348 StoragePartitionRemovalData removal_data = GetStoragePartitionRemovalData();
1349 EXPECT_EQ(removal_data.remove_mask,
1350 StoragePartition::REMOVE_DATA_MASK_FILE_SYSTEMS |
1351 StoragePartition::REMOVE_DATA_MASK_WEBSQL |
1352 StoragePartition::REMOVE_DATA_MASK_APPCACHE |
1353 StoragePartition::REMOVE_DATA_MASK_SERVICE_WORKERS |
1354 StoragePartition::REMOVE_DATA_MASK_INDEXEDDB);
1355 EXPECT_EQ(removal_data.quota_storage_remove_mask,
1356 StoragePartition::QUOTA_MANAGED_STORAGE_MASK_ALL);
1357 EXPECT_TRUE(removal_data.remove_origin.is_empty());
1360 TEST_F(BrowsingDataRemoverTest, RemoveQuotaManagedDataForeverOnlyTemporary) {
1361 #if defined(ENABLE_EXTENSIONS)
1362 CreateMockPolicy();
1363 #endif
1365 BlockUntilBrowsingDataRemoved(BrowsingDataRemover::EVERYTHING,
1366 BrowsingDataRemover::REMOVE_FILE_SYSTEMS |
1367 BrowsingDataRemover::REMOVE_WEBSQL |
1368 BrowsingDataRemover::REMOVE_APPCACHE |
1369 BrowsingDataRemover::REMOVE_SERVICE_WORKERS |
1370 BrowsingDataRemover::REMOVE_INDEXEDDB,
1371 false);
1373 EXPECT_EQ(BrowsingDataRemover::REMOVE_FILE_SYSTEMS |
1374 BrowsingDataRemover::REMOVE_WEBSQL |
1375 BrowsingDataRemover::REMOVE_APPCACHE |
1376 BrowsingDataRemover::REMOVE_SERVICE_WORKERS |
1377 BrowsingDataRemover::REMOVE_INDEXEDDB, GetRemovalMask());
1378 EXPECT_EQ(BrowsingDataHelper::UNPROTECTED_WEB, GetOriginSetMask());
1380 // Verify storage partition related stuffs.
1381 StoragePartitionRemovalData removal_data = GetStoragePartitionRemovalData();
1383 EXPECT_EQ(removal_data.remove_mask,
1384 StoragePartition::REMOVE_DATA_MASK_FILE_SYSTEMS |
1385 StoragePartition::REMOVE_DATA_MASK_WEBSQL |
1386 StoragePartition::REMOVE_DATA_MASK_APPCACHE |
1387 StoragePartition::REMOVE_DATA_MASK_SERVICE_WORKERS |
1388 StoragePartition::REMOVE_DATA_MASK_INDEXEDDB);
1389 EXPECT_EQ(removal_data.quota_storage_remove_mask,
1390 StoragePartition::QUOTA_MANAGED_STORAGE_MASK_ALL);
1391 EXPECT_TRUE(removal_data.remove_origin.is_empty());
1393 // Check that all related origin data would be removed, that is, origin
1394 // matcher would match these origin.
1395 EXPECT_TRUE(removal_data.origin_matcher.Run(kOrigin1, mock_policy()));
1396 EXPECT_TRUE(removal_data.origin_matcher.Run(kOrigin2, mock_policy()));
1397 EXPECT_TRUE(removal_data.origin_matcher.Run(kOrigin3, mock_policy()));
1400 TEST_F(BrowsingDataRemoverTest, RemoveQuotaManagedDataForeverOnlyPersistent) {
1401 #if defined(ENABLE_EXTENSIONS)
1402 CreateMockPolicy();
1403 #endif
1405 BlockUntilBrowsingDataRemoved(BrowsingDataRemover::EVERYTHING,
1406 BrowsingDataRemover::REMOVE_FILE_SYSTEMS |
1407 BrowsingDataRemover::REMOVE_WEBSQL |
1408 BrowsingDataRemover::REMOVE_APPCACHE |
1409 BrowsingDataRemover::REMOVE_SERVICE_WORKERS |
1410 BrowsingDataRemover::REMOVE_INDEXEDDB,
1411 false);
1413 EXPECT_EQ(BrowsingDataRemover::REMOVE_FILE_SYSTEMS |
1414 BrowsingDataRemover::REMOVE_WEBSQL |
1415 BrowsingDataRemover::REMOVE_APPCACHE |
1416 BrowsingDataRemover::REMOVE_SERVICE_WORKERS |
1417 BrowsingDataRemover::REMOVE_INDEXEDDB, GetRemovalMask());
1418 EXPECT_EQ(BrowsingDataHelper::UNPROTECTED_WEB, GetOriginSetMask());
1420 // Verify storage partition related stuffs.
1421 StoragePartitionRemovalData removal_data = GetStoragePartitionRemovalData();
1423 EXPECT_EQ(removal_data.remove_mask,
1424 StoragePartition::REMOVE_DATA_MASK_FILE_SYSTEMS |
1425 StoragePartition::REMOVE_DATA_MASK_WEBSQL |
1426 StoragePartition::REMOVE_DATA_MASK_APPCACHE |
1427 StoragePartition::REMOVE_DATA_MASK_SERVICE_WORKERS |
1428 StoragePartition::REMOVE_DATA_MASK_INDEXEDDB);
1429 EXPECT_EQ(removal_data.quota_storage_remove_mask,
1430 StoragePartition::QUOTA_MANAGED_STORAGE_MASK_ALL);
1431 EXPECT_TRUE(removal_data.remove_origin.is_empty());
1433 // Check that all related origin data would be removed, that is, origin
1434 // matcher would match these origin.
1435 EXPECT_TRUE(removal_data.origin_matcher.Run(kOrigin1, mock_policy()));
1436 EXPECT_TRUE(removal_data.origin_matcher.Run(kOrigin2, mock_policy()));
1437 EXPECT_TRUE(removal_data.origin_matcher.Run(kOrigin3, mock_policy()));
1440 TEST_F(BrowsingDataRemoverTest, RemoveQuotaManagedDataForeverNeither) {
1441 #if defined(ENABLE_EXTENSIONS)
1442 CreateMockPolicy();
1443 #endif
1446 BlockUntilBrowsingDataRemoved(BrowsingDataRemover::EVERYTHING,
1447 BrowsingDataRemover::REMOVE_FILE_SYSTEMS |
1448 BrowsingDataRemover::REMOVE_WEBSQL |
1449 BrowsingDataRemover::REMOVE_APPCACHE |
1450 BrowsingDataRemover::REMOVE_SERVICE_WORKERS |
1451 BrowsingDataRemover::REMOVE_INDEXEDDB,
1452 false);
1454 EXPECT_EQ(BrowsingDataRemover::REMOVE_FILE_SYSTEMS |
1455 BrowsingDataRemover::REMOVE_WEBSQL |
1456 BrowsingDataRemover::REMOVE_APPCACHE |
1457 BrowsingDataRemover::REMOVE_SERVICE_WORKERS |
1458 BrowsingDataRemover::REMOVE_INDEXEDDB, GetRemovalMask());
1459 EXPECT_EQ(BrowsingDataHelper::UNPROTECTED_WEB, GetOriginSetMask());
1461 // Verify storage partition related stuffs.
1462 StoragePartitionRemovalData removal_data = GetStoragePartitionRemovalData();
1464 EXPECT_EQ(removal_data.remove_mask,
1465 StoragePartition::REMOVE_DATA_MASK_FILE_SYSTEMS |
1466 StoragePartition::REMOVE_DATA_MASK_WEBSQL |
1467 StoragePartition::REMOVE_DATA_MASK_APPCACHE |
1468 StoragePartition::REMOVE_DATA_MASK_SERVICE_WORKERS |
1469 StoragePartition::REMOVE_DATA_MASK_INDEXEDDB);
1470 EXPECT_EQ(removal_data.quota_storage_remove_mask,
1471 StoragePartition::QUOTA_MANAGED_STORAGE_MASK_ALL);
1472 EXPECT_TRUE(removal_data.remove_origin.is_empty());
1474 // Check that all related origin data would be removed, that is, origin
1475 // matcher would match these origin.
1476 EXPECT_TRUE(removal_data.origin_matcher.Run(kOrigin1, mock_policy()));
1477 EXPECT_TRUE(removal_data.origin_matcher.Run(kOrigin2, mock_policy()));
1478 EXPECT_TRUE(removal_data.origin_matcher.Run(kOrigin3, mock_policy()));
1481 TEST_F(BrowsingDataRemoverTest, RemoveQuotaManagedDataForeverSpecificOrigin) {
1482 // Remove Origin 1.
1483 BlockUntilOriginDataRemoved(BrowsingDataRemover::EVERYTHING,
1484 BrowsingDataRemover::REMOVE_APPCACHE |
1485 BrowsingDataRemover::REMOVE_SERVICE_WORKERS |
1486 BrowsingDataRemover::REMOVE_FILE_SYSTEMS |
1487 BrowsingDataRemover::REMOVE_INDEXEDDB |
1488 BrowsingDataRemover::REMOVE_WEBSQL,
1489 kOrigin1);
1491 EXPECT_EQ(BrowsingDataRemover::REMOVE_APPCACHE |
1492 BrowsingDataRemover::REMOVE_SERVICE_WORKERS |
1493 BrowsingDataRemover::REMOVE_FILE_SYSTEMS |
1494 BrowsingDataRemover::REMOVE_INDEXEDDB |
1495 BrowsingDataRemover::REMOVE_WEBSQL, GetRemovalMask());
1496 EXPECT_EQ(BrowsingDataHelper::UNPROTECTED_WEB, GetOriginSetMask());
1498 // Verify storage partition related stuffs.
1499 StoragePartitionRemovalData removal_data = GetStoragePartitionRemovalData();
1501 EXPECT_EQ(removal_data.remove_mask,
1502 StoragePartition::REMOVE_DATA_MASK_FILE_SYSTEMS |
1503 StoragePartition::REMOVE_DATA_MASK_WEBSQL |
1504 StoragePartition::REMOVE_DATA_MASK_APPCACHE |
1505 StoragePartition::REMOVE_DATA_MASK_SERVICE_WORKERS |
1506 StoragePartition::REMOVE_DATA_MASK_INDEXEDDB);
1507 EXPECT_EQ(removal_data.quota_storage_remove_mask,
1508 StoragePartition::QUOTA_MANAGED_STORAGE_MASK_ALL);
1509 EXPECT_EQ(removal_data.remove_origin, kOrigin1);
1512 TEST_F(BrowsingDataRemoverTest, RemoveQuotaManagedDataForLastHour) {
1513 BlockUntilBrowsingDataRemoved(BrowsingDataRemover::LAST_HOUR,
1514 BrowsingDataRemover::REMOVE_FILE_SYSTEMS |
1515 BrowsingDataRemover::REMOVE_WEBSQL |
1516 BrowsingDataRemover::REMOVE_APPCACHE |
1517 BrowsingDataRemover::REMOVE_SERVICE_WORKERS |
1518 BrowsingDataRemover::REMOVE_INDEXEDDB,
1519 false);
1521 EXPECT_EQ(BrowsingDataRemover::REMOVE_FILE_SYSTEMS |
1522 BrowsingDataRemover::REMOVE_WEBSQL |
1523 BrowsingDataRemover::REMOVE_APPCACHE |
1524 BrowsingDataRemover::REMOVE_SERVICE_WORKERS |
1525 BrowsingDataRemover::REMOVE_INDEXEDDB, GetRemovalMask());
1526 EXPECT_EQ(BrowsingDataHelper::UNPROTECTED_WEB, GetOriginSetMask());
1528 // Verify storage partition related stuffs.
1529 StoragePartitionRemovalData removal_data = GetStoragePartitionRemovalData();
1531 EXPECT_EQ(removal_data.remove_mask,
1532 StoragePartition::REMOVE_DATA_MASK_FILE_SYSTEMS |
1533 StoragePartition::REMOVE_DATA_MASK_WEBSQL |
1534 StoragePartition::REMOVE_DATA_MASK_APPCACHE |
1535 StoragePartition::REMOVE_DATA_MASK_SERVICE_WORKERS |
1536 StoragePartition::REMOVE_DATA_MASK_INDEXEDDB);
1538 // Persistent data would be left out since we are not removing from
1539 // beginning of time.
1540 uint32 expected_quota_mask =
1541 ~StoragePartition::QUOTA_MANAGED_STORAGE_MASK_PERSISTENT;
1542 EXPECT_EQ(removal_data.quota_storage_remove_mask, expected_quota_mask);
1543 EXPECT_TRUE(removal_data.remove_origin.is_empty());
1544 // Check removal begin time.
1545 EXPECT_EQ(removal_data.remove_begin, GetBeginTime());
1548 TEST_F(BrowsingDataRemoverTest, RemoveQuotaManagedDataForLastWeek) {
1549 BlockUntilBrowsingDataRemoved(BrowsingDataRemover::LAST_WEEK,
1550 BrowsingDataRemover::REMOVE_FILE_SYSTEMS |
1551 BrowsingDataRemover::REMOVE_WEBSQL |
1552 BrowsingDataRemover::REMOVE_APPCACHE |
1553 BrowsingDataRemover::REMOVE_SERVICE_WORKERS |
1554 BrowsingDataRemover::REMOVE_INDEXEDDB,
1555 false);
1557 EXPECT_EQ(BrowsingDataRemover::REMOVE_FILE_SYSTEMS |
1558 BrowsingDataRemover::REMOVE_WEBSQL |
1559 BrowsingDataRemover::REMOVE_APPCACHE |
1560 BrowsingDataRemover::REMOVE_SERVICE_WORKERS |
1561 BrowsingDataRemover::REMOVE_INDEXEDDB, GetRemovalMask());
1562 EXPECT_EQ(BrowsingDataHelper::UNPROTECTED_WEB, GetOriginSetMask());
1564 // Verify storage partition related stuffs.
1565 StoragePartitionRemovalData removal_data = GetStoragePartitionRemovalData();
1567 EXPECT_EQ(removal_data.remove_mask,
1568 StoragePartition::REMOVE_DATA_MASK_FILE_SYSTEMS |
1569 StoragePartition::REMOVE_DATA_MASK_WEBSQL |
1570 StoragePartition::REMOVE_DATA_MASK_APPCACHE |
1571 StoragePartition::REMOVE_DATA_MASK_SERVICE_WORKERS |
1572 StoragePartition::REMOVE_DATA_MASK_INDEXEDDB);
1574 // Persistent data would be left out since we are not removing from
1575 // beginning of time.
1576 uint32 expected_quota_mask =
1577 ~StoragePartition::QUOTA_MANAGED_STORAGE_MASK_PERSISTENT;
1578 EXPECT_EQ(removal_data.quota_storage_remove_mask, expected_quota_mask);
1579 EXPECT_TRUE(removal_data.remove_origin.is_empty());
1580 // Check removal begin time.
1581 EXPECT_EQ(removal_data.remove_begin, GetBeginTime());
1584 TEST_F(BrowsingDataRemoverTest, RemoveQuotaManagedUnprotectedOrigins) {
1585 #if defined(ENABLE_EXTENSIONS)
1586 MockExtensionSpecialStoragePolicy* policy = CreateMockPolicy();
1587 // Protect kOrigin1.
1588 policy->AddProtected(kOrigin1.GetOrigin());
1589 #endif
1591 BlockUntilBrowsingDataRemoved(BrowsingDataRemover::EVERYTHING,
1592 BrowsingDataRemover::REMOVE_FILE_SYSTEMS |
1593 BrowsingDataRemover::REMOVE_WEBSQL |
1594 BrowsingDataRemover::REMOVE_APPCACHE |
1595 BrowsingDataRemover::REMOVE_SERVICE_WORKERS |
1596 BrowsingDataRemover::REMOVE_INDEXEDDB,
1597 false);
1599 EXPECT_EQ(BrowsingDataRemover::REMOVE_FILE_SYSTEMS |
1600 BrowsingDataRemover::REMOVE_WEBSQL |
1601 BrowsingDataRemover::REMOVE_APPCACHE |
1602 BrowsingDataRemover::REMOVE_SERVICE_WORKERS |
1603 BrowsingDataRemover::REMOVE_INDEXEDDB, GetRemovalMask());
1604 EXPECT_EQ(BrowsingDataHelper::UNPROTECTED_WEB, GetOriginSetMask());
1606 // Verify storage partition related stuffs.
1607 StoragePartitionRemovalData removal_data = GetStoragePartitionRemovalData();
1609 EXPECT_EQ(removal_data.remove_mask,
1610 StoragePartition::REMOVE_DATA_MASK_FILE_SYSTEMS |
1611 StoragePartition::REMOVE_DATA_MASK_WEBSQL |
1612 StoragePartition::REMOVE_DATA_MASK_APPCACHE |
1613 StoragePartition::REMOVE_DATA_MASK_SERVICE_WORKERS |
1614 StoragePartition::REMOVE_DATA_MASK_INDEXEDDB);
1615 EXPECT_EQ(removal_data.quota_storage_remove_mask,
1616 StoragePartition::QUOTA_MANAGED_STORAGE_MASK_ALL);
1617 EXPECT_TRUE(removal_data.remove_origin.is_empty());
1619 // Check OriginMatcherFunction.
1620 EXPECT_EQ(ShouldRemoveForProtectedOriginOne(),
1621 removal_data.origin_matcher.Run(kOrigin1, mock_policy()));
1622 EXPECT_TRUE(removal_data.origin_matcher.Run(kOrigin2, mock_policy()));
1623 EXPECT_TRUE(removal_data.origin_matcher.Run(kOrigin3, mock_policy()));
1626 TEST_F(BrowsingDataRemoverTest, RemoveQuotaManagedProtectedSpecificOrigin) {
1627 #if defined(ENABLE_EXTENSIONS)
1628 MockExtensionSpecialStoragePolicy* policy = CreateMockPolicy();
1629 // Protect kOrigin1.
1630 policy->AddProtected(kOrigin1.GetOrigin());
1631 #endif
1633 // Try to remove kOrigin1. Expect failure.
1634 BlockUntilOriginDataRemoved(BrowsingDataRemover::EVERYTHING,
1635 BrowsingDataRemover::REMOVE_APPCACHE |
1636 BrowsingDataRemover::REMOVE_SERVICE_WORKERS |
1637 BrowsingDataRemover::REMOVE_FILE_SYSTEMS |
1638 BrowsingDataRemover::REMOVE_INDEXEDDB |
1639 BrowsingDataRemover::REMOVE_WEBSQL,
1640 kOrigin1);
1642 EXPECT_EQ(BrowsingDataRemover::REMOVE_APPCACHE |
1643 BrowsingDataRemover::REMOVE_SERVICE_WORKERS |
1644 BrowsingDataRemover::REMOVE_FILE_SYSTEMS |
1645 BrowsingDataRemover::REMOVE_INDEXEDDB |
1646 BrowsingDataRemover::REMOVE_WEBSQL, GetRemovalMask());
1647 EXPECT_EQ(BrowsingDataHelper::UNPROTECTED_WEB, GetOriginSetMask());
1649 // Verify storage partition related stuffs.
1650 StoragePartitionRemovalData removal_data = GetStoragePartitionRemovalData();
1652 EXPECT_EQ(removal_data.remove_mask,
1653 StoragePartition::REMOVE_DATA_MASK_FILE_SYSTEMS |
1654 StoragePartition::REMOVE_DATA_MASK_WEBSQL |
1655 StoragePartition::REMOVE_DATA_MASK_APPCACHE |
1656 StoragePartition::REMOVE_DATA_MASK_SERVICE_WORKERS |
1657 StoragePartition::REMOVE_DATA_MASK_INDEXEDDB);
1658 EXPECT_EQ(removal_data.quota_storage_remove_mask,
1659 StoragePartition::QUOTA_MANAGED_STORAGE_MASK_ALL);
1660 EXPECT_EQ(removal_data.remove_origin, kOrigin1);
1662 // Check OriginMatcherFunction.
1663 EXPECT_EQ(ShouldRemoveForProtectedOriginOne(),
1664 removal_data.origin_matcher.Run(kOrigin1, mock_policy()));
1665 EXPECT_TRUE(removal_data.origin_matcher.Run(kOrigin2, mock_policy()));
1666 EXPECT_TRUE(removal_data.origin_matcher.Run(kOrigin3, mock_policy()));
1669 TEST_F(BrowsingDataRemoverTest, RemoveQuotaManagedProtectedOrigins) {
1670 #if defined(ENABLE_EXTENSIONS)
1671 MockExtensionSpecialStoragePolicy* policy = CreateMockPolicy();
1672 // Protect kOrigin1.
1673 policy->AddProtected(kOrigin1.GetOrigin());
1674 #endif
1676 // Try to remove kOrigin1. Expect success.
1677 BlockUntilBrowsingDataRemoved(BrowsingDataRemover::EVERYTHING,
1678 BrowsingDataRemover::REMOVE_APPCACHE |
1679 BrowsingDataRemover::REMOVE_SERVICE_WORKERS |
1680 BrowsingDataRemover::REMOVE_FILE_SYSTEMS |
1681 BrowsingDataRemover::REMOVE_INDEXEDDB |
1682 BrowsingDataRemover::REMOVE_WEBSQL,
1683 true);
1685 EXPECT_EQ(BrowsingDataRemover::REMOVE_APPCACHE |
1686 BrowsingDataRemover::REMOVE_SERVICE_WORKERS |
1687 BrowsingDataRemover::REMOVE_FILE_SYSTEMS |
1688 BrowsingDataRemover::REMOVE_INDEXEDDB |
1689 BrowsingDataRemover::REMOVE_WEBSQL, GetRemovalMask());
1690 EXPECT_EQ(BrowsingDataHelper::PROTECTED_WEB |
1691 BrowsingDataHelper::UNPROTECTED_WEB, GetOriginSetMask());
1693 // Verify storage partition related stuffs.
1694 StoragePartitionRemovalData removal_data = GetStoragePartitionRemovalData();
1696 EXPECT_EQ(removal_data.remove_mask,
1697 StoragePartition::REMOVE_DATA_MASK_FILE_SYSTEMS |
1698 StoragePartition::REMOVE_DATA_MASK_WEBSQL |
1699 StoragePartition::REMOVE_DATA_MASK_APPCACHE |
1700 StoragePartition::REMOVE_DATA_MASK_SERVICE_WORKERS |
1701 StoragePartition::REMOVE_DATA_MASK_INDEXEDDB);
1702 EXPECT_EQ(removal_data.quota_storage_remove_mask,
1703 StoragePartition::QUOTA_MANAGED_STORAGE_MASK_ALL);
1704 EXPECT_TRUE(removal_data.remove_origin.is_empty());
1706 // Check OriginMatcherFunction, |kOrigin1| would match mask since we
1707 // would have 'protected' specified in origin_set_mask.
1708 EXPECT_TRUE(removal_data.origin_matcher.Run(kOrigin1, mock_policy()));
1709 EXPECT_TRUE(removal_data.origin_matcher.Run(kOrigin2, mock_policy()));
1710 EXPECT_TRUE(removal_data.origin_matcher.Run(kOrigin3, mock_policy()));
1713 TEST_F(BrowsingDataRemoverTest, RemoveQuotaManagedIgnoreExtensionsAndDevTools) {
1714 #if defined(ENABLE_EXTENSIONS)
1715 CreateMockPolicy();
1716 #endif
1718 BlockUntilBrowsingDataRemoved(BrowsingDataRemover::EVERYTHING,
1719 BrowsingDataRemover::REMOVE_APPCACHE |
1720 BrowsingDataRemover::REMOVE_SERVICE_WORKERS |
1721 BrowsingDataRemover::REMOVE_FILE_SYSTEMS |
1722 BrowsingDataRemover::REMOVE_INDEXEDDB |
1723 BrowsingDataRemover::REMOVE_WEBSQL,
1724 false);
1726 EXPECT_EQ(BrowsingDataRemover::REMOVE_APPCACHE |
1727 BrowsingDataRemover::REMOVE_SERVICE_WORKERS |
1728 BrowsingDataRemover::REMOVE_FILE_SYSTEMS |
1729 BrowsingDataRemover::REMOVE_INDEXEDDB |
1730 BrowsingDataRemover::REMOVE_WEBSQL, GetRemovalMask());
1731 EXPECT_EQ(BrowsingDataHelper::UNPROTECTED_WEB, GetOriginSetMask());
1733 // Verify storage partition related stuffs.
1734 StoragePartitionRemovalData removal_data = GetStoragePartitionRemovalData();
1736 EXPECT_EQ(removal_data.remove_mask,
1737 StoragePartition::REMOVE_DATA_MASK_FILE_SYSTEMS |
1738 StoragePartition::REMOVE_DATA_MASK_WEBSQL |
1739 StoragePartition::REMOVE_DATA_MASK_APPCACHE |
1740 StoragePartition::REMOVE_DATA_MASK_SERVICE_WORKERS |
1741 StoragePartition::REMOVE_DATA_MASK_INDEXEDDB);
1742 EXPECT_EQ(removal_data.quota_storage_remove_mask,
1743 StoragePartition::QUOTA_MANAGED_STORAGE_MASK_ALL);
1744 EXPECT_TRUE(removal_data.remove_origin.is_empty());
1746 // Check that extension and devtools data wouldn't be removed, that is,
1747 // origin matcher would not match these origin.
1748 EXPECT_FALSE(removal_data.origin_matcher.Run(kOriginExt, mock_policy()));
1749 EXPECT_FALSE(removal_data.origin_matcher.Run(kOriginDevTools, mock_policy()));
1752 TEST_F(BrowsingDataRemoverTest, OriginBasedHistoryRemoval) {
1753 RemoveHistoryTester tester;
1754 ASSERT_TRUE(tester.Init(GetProfile()));
1756 base::Time two_hours_ago = base::Time::Now() - base::TimeDelta::FromHours(2);
1758 tester.AddHistory(kOrigin1, base::Time::Now());
1759 tester.AddHistory(kOrigin2, two_hours_ago);
1760 ASSERT_TRUE(tester.HistoryContainsURL(kOrigin1));
1761 ASSERT_TRUE(tester.HistoryContainsURL(kOrigin2));
1763 BlockUntilOriginDataRemoved(BrowsingDataRemover::EVERYTHING,
1764 BrowsingDataRemover::REMOVE_HISTORY, kOrigin2);
1766 EXPECT_EQ(BrowsingDataRemover::REMOVE_HISTORY, GetRemovalMask());
1767 EXPECT_EQ(BrowsingDataHelper::UNPROTECTED_WEB, GetOriginSetMask());
1769 // Nothing should have been deleted.
1770 EXPECT_TRUE(tester.HistoryContainsURL(kOrigin1));
1771 EXPECT_FALSE(tester.HistoryContainsURL(kOrigin2));
1774 TEST_F(BrowsingDataRemoverTest, OriginAndTimeBasedHistoryRemoval) {
1775 RemoveHistoryTester tester;
1776 ASSERT_TRUE(tester.Init(GetProfile()));
1778 base::Time two_hours_ago = base::Time::Now() - base::TimeDelta::FromHours(2);
1780 tester.AddHistory(kOrigin1, base::Time::Now());
1781 tester.AddHistory(kOrigin2, two_hours_ago);
1782 ASSERT_TRUE(tester.HistoryContainsURL(kOrigin1));
1783 ASSERT_TRUE(tester.HistoryContainsURL(kOrigin2));
1785 BlockUntilOriginDataRemoved(BrowsingDataRemover::LAST_HOUR,
1786 BrowsingDataRemover::REMOVE_HISTORY, kOrigin2);
1788 EXPECT_EQ(BrowsingDataRemover::REMOVE_HISTORY, GetRemovalMask());
1789 EXPECT_EQ(BrowsingDataHelper::UNPROTECTED_WEB, GetOriginSetMask());
1790 EXPECT_TRUE(tester.HistoryContainsURL(kOrigin1));
1791 EXPECT_TRUE(tester.HistoryContainsURL(kOrigin2));
1794 // Verify that clearing autofill form data works.
1795 TEST_F(BrowsingDataRemoverTest, AutofillRemovalLastHour) {
1796 GetProfile()->CreateWebDataService();
1797 RemoveAutofillTester tester(GetProfile());
1799 ASSERT_FALSE(tester.HasProfile());
1800 tester.AddProfilesAndCards();
1801 ASSERT_TRUE(tester.HasProfile());
1803 BlockUntilBrowsingDataRemoved(
1804 BrowsingDataRemover::LAST_HOUR,
1805 BrowsingDataRemover::REMOVE_FORM_DATA, false);
1807 EXPECT_EQ(BrowsingDataRemover::REMOVE_FORM_DATA, GetRemovalMask());
1808 EXPECT_EQ(BrowsingDataHelper::UNPROTECTED_WEB, GetOriginSetMask());
1809 ASSERT_FALSE(tester.HasProfile());
1812 TEST_F(BrowsingDataRemoverTest, AutofillRemovalEverything) {
1813 GetProfile()->CreateWebDataService();
1814 RemoveAutofillTester tester(GetProfile());
1816 ASSERT_FALSE(tester.HasProfile());
1817 tester.AddProfilesAndCards();
1818 ASSERT_TRUE(tester.HasProfile());
1820 BlockUntilBrowsingDataRemoved(
1821 BrowsingDataRemover::EVERYTHING,
1822 BrowsingDataRemover::REMOVE_FORM_DATA, false);
1824 EXPECT_EQ(BrowsingDataRemover::REMOVE_FORM_DATA, GetRemovalMask());
1825 EXPECT_EQ(BrowsingDataHelper::UNPROTECTED_WEB, GetOriginSetMask());
1826 ASSERT_FALSE(tester.HasProfile());
1829 // Verify that clearing autofill form data works.
1830 TEST_F(BrowsingDataRemoverTest, AutofillOriginsRemovedWithHistory) {
1831 GetProfile()->CreateWebDataService();
1832 RemoveAutofillTester tester(GetProfile());
1834 tester.AddProfilesAndCards();
1835 EXPECT_FALSE(tester.HasOrigin(std::string()));
1836 EXPECT_TRUE(tester.HasOrigin(kWebOrigin));
1837 EXPECT_TRUE(tester.HasOrigin(kChromeOrigin));
1839 BlockUntilBrowsingDataRemoved(
1840 BrowsingDataRemover::LAST_HOUR,
1841 BrowsingDataRemover::REMOVE_HISTORY, false);
1843 EXPECT_EQ(BrowsingDataRemover::REMOVE_HISTORY, GetRemovalMask());
1844 EXPECT_EQ(BrowsingDataHelper::UNPROTECTED_WEB, GetOriginSetMask());
1845 EXPECT_TRUE(tester.HasOrigin(std::string()));
1846 EXPECT_FALSE(tester.HasOrigin(kWebOrigin));
1847 EXPECT_TRUE(tester.HasOrigin(kChromeOrigin));
1850 TEST_F(BrowsingDataRemoverTest, CompletionInhibition) {
1851 // The |completion_inhibitor| on the stack should prevent removal sessions
1852 // from completing until after ContinueToCompletion() is called.
1853 BrowsingDataRemoverCompletionInhibitor completion_inhibitor;
1855 called_with_details_.reset(new BrowsingDataRemover::NotificationDetails());
1857 // BrowsingDataRemover deletes itself when it completes.
1858 BrowsingDataRemover* remover = BrowsingDataRemover::CreateForPeriod(
1859 GetProfile(), BrowsingDataRemover::EVERYTHING);
1860 remover->Remove(BrowsingDataRemover::REMOVE_HISTORY,
1861 BrowsingDataHelper::UNPROTECTED_WEB);
1863 // Process messages until the inhibitor is notified, and then some, to make
1864 // sure we do not complete asynchronously before ContinueToCompletion() is
1865 // called.
1866 completion_inhibitor.BlockUntilNearCompletion();
1867 base::RunLoop().RunUntilIdle();
1869 // Verify that the completion notification has not yet been broadcasted.
1870 EXPECT_EQ(-1, GetRemovalMask());
1871 EXPECT_EQ(-1, GetOriginSetMask());
1873 // Now run the removal process until completion, and verify that observers are
1874 // now notified, and the notifications is sent out.
1875 BrowsingDataRemoverCompletionObserver completion_observer(remover);
1876 completion_inhibitor.ContinueToCompletion();
1877 completion_observer.BlockUntilCompletion();
1879 EXPECT_EQ(BrowsingDataRemover::REMOVE_HISTORY, GetRemovalMask());
1880 EXPECT_EQ(BrowsingDataHelper::UNPROTECTED_WEB, GetOriginSetMask());
1883 TEST_F(BrowsingDataRemoverTest, ZeroSuggestCacheClear) {
1884 PrefService* prefs = GetProfile()->GetPrefs();
1885 prefs->SetString(prefs::kZeroSuggestCachedResults,
1886 "[\"\", [\"foo\", \"bar\"]]");
1887 BlockUntilBrowsingDataRemoved(BrowsingDataRemover::EVERYTHING,
1888 BrowsingDataRemover::REMOVE_COOKIES,
1889 false);
1891 // Expect the prefs to be cleared when cookies are removed.
1892 EXPECT_TRUE(prefs->GetString(prefs::kZeroSuggestCachedResults).empty());
1893 EXPECT_EQ(BrowsingDataRemover::REMOVE_COOKIES, GetRemovalMask());
1894 EXPECT_EQ(BrowsingDataHelper::UNPROTECTED_WEB, GetOriginSetMask());
1897 #if defined(OS_CHROMEOS)
1898 TEST_F(BrowsingDataRemoverTest, ContentProtectionPlatformKeysRemoval) {
1899 chromeos::ScopedTestDeviceSettingsService test_device_settings_service;
1900 chromeos::ScopedTestCrosSettings test_cros_settings;
1901 chromeos::MockUserManager* mock_user_manager =
1902 new testing::NiceMock<chromeos::MockUserManager>();
1903 mock_user_manager->SetActiveUser("test@example.com");
1904 chromeos::ScopedUserManagerEnabler user_manager_enabler(mock_user_manager);
1906 scoped_ptr<chromeos::DBusThreadManagerSetter> dbus_setter =
1907 chromeos::DBusThreadManager::GetSetterForTesting();
1908 chromeos::MockCryptohomeClient* cryptohome_client =
1909 new chromeos::MockCryptohomeClient;
1910 dbus_setter->SetCryptohomeClient(
1911 scoped_ptr<chromeos::CryptohomeClient>(cryptohome_client));
1913 // Expect exactly one call. No calls means no attempt to delete keys and more
1914 // than one call means a significant performance problem.
1915 EXPECT_CALL(*cryptohome_client, TpmAttestationDeleteKeys(_, _, _, _))
1916 .WillOnce(WithArgs<3>(Invoke(FakeDBusCall)));
1918 BlockUntilBrowsingDataRemoved(
1919 BrowsingDataRemover::EVERYTHING,
1920 BrowsingDataRemover::REMOVE_CONTENT_LICENSES, false);
1922 chromeos::DBusThreadManager::Shutdown();
1924 #endif
1926 TEST_F(BrowsingDataRemoverTest, DomainReliability_Null) {
1927 const ClearDomainReliabilityTester& tester =
1928 clear_domain_reliability_tester();
1930 EXPECT_EQ(0u, tester.clear_count());
1933 TEST_F(BrowsingDataRemoverTest, DomainReliability_Beacons) {
1934 const ClearDomainReliabilityTester& tester =
1935 clear_domain_reliability_tester();
1937 BlockUntilBrowsingDataRemoved(
1938 BrowsingDataRemover::EVERYTHING,
1939 BrowsingDataRemover::REMOVE_HISTORY, false);
1940 EXPECT_EQ(1u, tester.clear_count());
1941 EXPECT_EQ(CLEAR_BEACONS, tester.last_clear_mode());
1944 TEST_F(BrowsingDataRemoverTest, DomainReliability_Contexts) {
1945 const ClearDomainReliabilityTester& tester =
1946 clear_domain_reliability_tester();
1948 BlockUntilBrowsingDataRemoved(
1949 BrowsingDataRemover::EVERYTHING,
1950 BrowsingDataRemover::REMOVE_COOKIES, false);
1951 EXPECT_EQ(1u, tester.clear_count());
1952 EXPECT_EQ(CLEAR_CONTEXTS, tester.last_clear_mode());
1955 TEST_F(BrowsingDataRemoverTest, DomainReliability_ContextsWin) {
1956 const ClearDomainReliabilityTester& tester =
1957 clear_domain_reliability_tester();
1959 BlockUntilBrowsingDataRemoved(
1960 BrowsingDataRemover::EVERYTHING,
1961 BrowsingDataRemover::REMOVE_HISTORY |
1962 BrowsingDataRemover::REMOVE_COOKIES, false);
1963 EXPECT_EQ(1u, tester.clear_count());
1964 EXPECT_EQ(CLEAR_CONTEXTS, tester.last_clear_mode());
1967 TEST_F(BrowsingDataRemoverTest, DomainReliability_ProtectedOrigins) {
1968 const ClearDomainReliabilityTester& tester =
1969 clear_domain_reliability_tester();
1971 BlockUntilBrowsingDataRemoved(
1972 BrowsingDataRemover::EVERYTHING,
1973 BrowsingDataRemover::REMOVE_COOKIES, true);
1974 EXPECT_EQ(1u, tester.clear_count());
1975 EXPECT_EQ(CLEAR_CONTEXTS, tester.last_clear_mode());
1978 // TODO(ttuttle): This isn't actually testing the no-monitor case, since
1979 // BrowsingDataRemoverTest now creates one unconditionally, since it's needed
1980 // for some unrelated test cases. This should be fixed so it tests the no-
1981 // monitor case again.
1982 TEST_F(BrowsingDataRemoverTest, DISABLED_DomainReliability_NoMonitor) {
1983 BlockUntilBrowsingDataRemoved(
1984 BrowsingDataRemover::EVERYTHING,
1985 BrowsingDataRemover::REMOVE_HISTORY |
1986 BrowsingDataRemover::REMOVE_COOKIES, false);