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

Side by Side Diff: chrome/browser/browsing_data/browsing_data_remover_impl_unittest.cc

Issue 2827523003: Move BrowsingDataRemover to content/ (Closed)
Patch Set: Rebase over codereview.chromium.org/2815913005 Created 3 years, 7 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
(Empty)
1 // Copyright 2017 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.
4
5 #include <stddef.h>
6 #include <stdint.h>
7
8 #include <list>
9 #include <memory>
10 #include <set>
11 #include <string>
12 #include <utility>
13 #include <vector>
14
15 #include "base/bind.h"
16 #include "base/bind_helpers.h"
17 #include "base/files/file_path.h"
18 #include "base/files/file_util.h"
19 #include "base/location.h"
20 #include "base/logging.h"
21 #include "base/macros.h"
22 #include "base/memory/ptr_util.h"
23 #include "base/message_loop/message_loop.h"
24 #include "base/run_loop.h"
25 #include "base/single_thread_task_runner.h"
26 #include "base/strings/utf_string_conversions.h"
27 #include "base/task/cancelable_task_tracker.h"
28 #include "base/threading/thread_task_runner_handle.h"
29 #include "base/time/time.h"
30 #include "build/build_config.h"
31 #include "chrome/browser/browsing_data/browsing_data_helper.h"
32 #include "chrome/browser/browsing_data/browsing_data_remover.h"
33 #include "chrome/browser/browsing_data/browsing_data_remover_factory.h"
34 #include "chrome/browser/browsing_data/browsing_data_remover_impl.h"
35 #include "chrome/browser/browsing_data/browsing_data_remover_test_util.h"
36 #include "chrome/test/base/testing_profile.h"
37 #include "content/public/browser/browser_context.h"
38 #include "content/public/browser/browsing_data_filter_builder.h"
39 #include "content/public/browser/cookie_store_factory.h"
40 #include "content/public/browser/dom_storage_context.h"
41 #include "content/public/browser/local_storage_usage_info.h"
42 #include "content/public/browser/storage_partition.h"
43 #include "content/public/test/mock_download_manager.h"
44 #include "content/public/test/test_browser_thread.h"
45 #include "content/public/test/test_browser_thread_bundle.h"
46 #include "content/public/test/test_utils.h"
47 #include "extensions/features/features.h"
48 #include "net/cookies/cookie_store.h"
49 #include "net/http/http_network_session.h"
50 #include "net/http/http_transaction_factory.h"
51 #include "net/ssl/channel_id_service.h"
52 #include "net/ssl/channel_id_store.h"
53 #include "net/ssl/ssl_client_cert_type.h"
54 #include "net/url_request/url_request_context.h"
55 #include "net/url_request/url_request_context_getter.h"
56 #include "ppapi/features/features.h"
57 #include "storage/browser/test/mock_special_storage_policy.h"
58 #include "testing/gmock/include/gmock/gmock.h"
59 #include "testing/gtest/include/gtest/gtest.h"
60 #include "url/origin.h"
61
62 using content::BrowserThread;
63 using content::BrowserContext;
64 using content::BrowsingDataFilterBuilder;
65 using content::StoragePartition;
66 using testing::_;
67 using testing::ByRef;
68 using testing::Eq;
69 using testing::Invoke;
70 using testing::IsEmpty;
71 using testing::Matcher;
72 using testing::MakeMatcher;
73 using testing::MatcherInterface;
74 using testing::MatchResultListener;
75 using testing::Not;
76 using testing::Return;
77 using testing::SizeIs;
78 using testing::WithArgs;
79
80 namespace {
81
82 const char kTestOrigin1[] = "http://host1.com:1/";
83 const char kTestRegisterableDomain1[] = "host1.com";
84 const char kTestOrigin2[] = "http://host2.com:1/";
85 const char kTestOrigin3[] = "http://host3.com:1/";
86 const char kTestRegisterableDomain3[] = "host3.com";
87 const char kTestOrigin4[] = "https://host3.com:1/";
88 const char kTestOriginExt[] = "chrome-extension://abcdefghijklmnopqrstuvwxyz/";
89 const char kTestOriginDevTools[] = "chrome-devtools://abcdefghijklmnopqrstuvw/";
90
91 // For HTTP auth.
92 const char kTestRealm[] = "TestRealm";
93
94 const GURL kOrigin1(kTestOrigin1);
95 const GURL kOrigin2(kTestOrigin2);
96 const GURL kOrigin3(kTestOrigin3);
97 const GURL kOrigin4(kTestOrigin4);
98 const GURL kOriginExt(kTestOriginExt);
99 const GURL kOriginDevTools(kTestOriginDevTools);
100
101 const base::FilePath::CharType kDomStorageOrigin1[] =
102 FILE_PATH_LITERAL("http_host1_1.localstorage");
103
104 const base::FilePath::CharType kDomStorageOrigin2[] =
105 FILE_PATH_LITERAL("http_host2_1.localstorage");
106
107 const base::FilePath::CharType kDomStorageOrigin3[] =
108 FILE_PATH_LITERAL("http_host3_1.localstorage");
109
110 const base::FilePath::CharType kDomStorageExt[] = FILE_PATH_LITERAL(
111 "chrome-extension_abcdefghijklmnopqrstuvwxyz_0.localstorage");
112
113 struct StoragePartitionRemovalData {
114 uint32_t remove_mask = 0;
115 uint32_t quota_storage_remove_mask = 0;
116 base::Time remove_begin;
117 base::Time remove_end;
118 StoragePartition::OriginMatcherFunction origin_matcher;
119 StoragePartition::CookieMatcherFunction cookie_matcher;
120
121 StoragePartitionRemovalData() {}
122 };
123
124 net::CanonicalCookie CreateCookieWithHost(const GURL& source) {
125 std::unique_ptr<net::CanonicalCookie> cookie(net::CanonicalCookie::Create(
126 source, "A", "1", std::string(), "/", base::Time::Now(),
127 base::Time::Now(), false, false, net::CookieSameSite::DEFAULT_MODE,
128 net::COOKIE_PRIORITY_MEDIUM));
129 EXPECT_TRUE(cookie);
130 return *cookie;
131 }
132
133 class TestStoragePartition : public StoragePartition {
134 public:
135 TestStoragePartition() {}
136 ~TestStoragePartition() override {}
137
138 // content::StoragePartition implementation.
139 base::FilePath GetPath() override { return base::FilePath(); }
140 net::URLRequestContextGetter* GetURLRequestContext() override {
141 return nullptr;
142 }
143 net::URLRequestContextGetter* GetMediaURLRequestContext() override {
144 return nullptr;
145 }
146 storage::QuotaManager* GetQuotaManager() override { return nullptr; }
147 content::AppCacheService* GetAppCacheService() override { return nullptr; }
148 storage::FileSystemContext* GetFileSystemContext() override {
149 return nullptr;
150 }
151 storage::DatabaseTracker* GetDatabaseTracker() override { return nullptr; }
152 content::DOMStorageContext* GetDOMStorageContext() override {
153 return nullptr;
154 }
155 content::IndexedDBContext* GetIndexedDBContext() override { return nullptr; }
156 content::ServiceWorkerContext* GetServiceWorkerContext() override {
157 return nullptr;
158 }
159 content::CacheStorageContext* GetCacheStorageContext() override {
160 return nullptr;
161 }
162 content::PlatformNotificationContext* GetPlatformNotificationContext()
163 override {
164 return nullptr;
165 }
166 content::HostZoomMap* GetHostZoomMap() override { return nullptr; }
167 content::HostZoomLevelContext* GetHostZoomLevelContext() override {
168 return nullptr;
169 }
170 content::ZoomLevelDelegate* GetZoomLevelDelegate() override {
171 return nullptr;
172 }
173
174 void ClearDataForOrigin(uint32_t remove_mask,
175 uint32_t quota_storage_remove_mask,
176 const GURL& storage_origin,
177 net::URLRequestContextGetter* rq_context,
178 const base::Closure& callback) override {
179 BrowserThread::PostTask(
180 BrowserThread::UI, FROM_HERE,
181 base::BindOnce(&TestStoragePartition::AsyncRunCallback,
182 base::Unretained(this), callback));
183 }
184
185 void ClearData(uint32_t remove_mask,
186 uint32_t quota_storage_remove_mask,
187 const GURL& storage_origin,
188 const OriginMatcherFunction& origin_matcher,
189 const base::Time begin,
190 const base::Time end,
191 const base::Closure& callback) override {
192 // Store stuff to verify parameters' correctness later.
193 storage_partition_removal_data_.remove_mask = remove_mask;
194 storage_partition_removal_data_.quota_storage_remove_mask =
195 quota_storage_remove_mask;
196 storage_partition_removal_data_.remove_begin = begin;
197 storage_partition_removal_data_.remove_end = end;
198 storage_partition_removal_data_.origin_matcher = origin_matcher;
199
200 BrowserThread::PostTask(
201 BrowserThread::UI, FROM_HERE,
202 base::BindOnce(&TestStoragePartition::AsyncRunCallback,
203 base::Unretained(this), callback));
204 }
205
206 void ClearData(uint32_t remove_mask,
207 uint32_t quota_storage_remove_mask,
208 const OriginMatcherFunction& origin_matcher,
209 const CookieMatcherFunction& cookie_matcher,
210 const base::Time begin,
211 const base::Time end,
212 const base::Closure& callback) override {
213 // Store stuff to verify parameters' correctness later.
214 storage_partition_removal_data_.remove_mask = remove_mask;
215 storage_partition_removal_data_.quota_storage_remove_mask =
216 quota_storage_remove_mask;
217 storage_partition_removal_data_.remove_begin = begin;
218 storage_partition_removal_data_.remove_end = end;
219 storage_partition_removal_data_.origin_matcher = origin_matcher;
220 storage_partition_removal_data_.cookie_matcher = cookie_matcher;
221
222 BrowserThread::PostTask(
223 BrowserThread::UI, FROM_HERE,
224 base::BindOnce(&TestStoragePartition::AsyncRunCallback,
225 base::Unretained(this), callback));
226 }
227
228 void ClearHttpAndMediaCaches(
229 const base::Time begin,
230 const base::Time end,
231 const base::Callback<bool(const GURL&)>& url_matcher,
232 const base::Closure& callback) override {
233 // Not needed in this test.
234 }
235
236 void Flush() override {}
237
238 void ClearBluetoothAllowedDevicesMapForTesting() override {}
239
240 StoragePartitionRemovalData GetStoragePartitionRemovalData() {
241 return storage_partition_removal_data_;
242 }
243
244 private:
245 void AsyncRunCallback(const base::Closure& callback) {
246 callback.Run();
247 }
248
249 StoragePartitionRemovalData storage_partition_removal_data_;
250
251 DISALLOW_COPY_AND_ASSIGN(TestStoragePartition);
252 };
253
254 // Custom matcher to test the equivalence of two URL filters. Since those are
255 // blackbox predicates, we can only approximate the equivalence by testing
256 // whether the filter give the same answer for several URLs. This is currently
257 // good enough for our testing purposes, to distinguish whitelists
258 // and blacklists, empty and non-empty filters and such.
259 // TODO(msramek): BrowsingDataRemover and some of its backends support URL
260 // filters, but its constructor currently only takes a single URL and constructs
261 // its own url filter. If an url filter was directly passed to
262 // BrowsingDataRemover (what should eventually be the case), we can use the same
263 // instance in the test as well, and thus simply test base::Callback::Equals()
264 // in this matcher.
265 class ProbablySameFilterMatcher
266 : public MatcherInterface<const base::Callback<bool(const GURL&)>&> {
267 public:
268 explicit ProbablySameFilterMatcher(
269 const base::Callback<bool(const GURL&)>& filter)
270 : to_match_(filter) {
271 }
272
273 virtual bool MatchAndExplain(const base::Callback<bool(const GURL&)>& filter,
274 MatchResultListener* listener) const {
275 if (filter.is_null() && to_match_.is_null())
276 return true;
277 if (filter.is_null() != to_match_.is_null())
278 return false;
279
280 const GURL urls_to_test_[] =
281 {kOrigin1, kOrigin2, kOrigin3, GURL("invalid spec")};
282 for (GURL url : urls_to_test_) {
283 if (filter.Run(url) != to_match_.Run(url)) {
284 if (listener)
285 *listener << "The filters differ on the URL " << url;
286 return false;
287 }
288 }
289 return true;
290 }
291
292 virtual void DescribeTo(::std::ostream* os) const {
293 *os << "is probably the same url filter as " << &to_match_;
294 }
295
296 virtual void DescribeNegationTo(::std::ostream* os) const {
297 *os << "is definitely NOT the same url filter as " << &to_match_;
298 }
299
300 private:
301 const base::Callback<bool(const GURL&)>& to_match_;
302 };
303
304 inline Matcher<const base::Callback<bool(const GURL&)>&> ProbablySameFilter(
305 const base::Callback<bool(const GURL&)>& filter) {
306 return MakeMatcher(new ProbablySameFilterMatcher(filter));
307 }
308
309 base::Time AnHourAgo() {
310 return base::Time::Now() - base::TimeDelta::FromHours(1);
311 }
312
313 } // namespace
314
315 // Testers -------------------------------------------------------------------
316
317 class RemoveCookieTester {
318 public:
319 RemoveCookieTester() {}
320
321 // Returns true, if the given cookie exists in the cookie store.
322 bool ContainsCookie() {
323 scoped_refptr<content::MessageLoopRunner> message_loop_runner =
324 new content::MessageLoopRunner;
325 quit_closure_ = message_loop_runner->QuitClosure();
326 get_cookie_success_ = false;
327 cookie_store_->GetCookiesWithOptionsAsync(
328 kOrigin1, net::CookieOptions(),
329 base::Bind(&RemoveCookieTester::GetCookieCallback,
330 base::Unretained(this)));
331 message_loop_runner->Run();
332 return get_cookie_success_;
333 }
334
335 void AddCookie() {
336 scoped_refptr<content::MessageLoopRunner> message_loop_runner =
337 new content::MessageLoopRunner;
338 quit_closure_ = message_loop_runner->QuitClosure();
339 cookie_store_->SetCookieWithOptionsAsync(
340 kOrigin1, "A=1", net::CookieOptions(),
341 base::Bind(&RemoveCookieTester::SetCookieCallback,
342 base::Unretained(this)));
343 message_loop_runner->Run();
344 }
345
346 protected:
347 void SetCookieStore(net::CookieStore* cookie_store) {
348 cookie_store_ = cookie_store;
349 }
350
351 private:
352 void GetCookieCallback(const std::string& cookies) {
353 if (cookies == "A=1") {
354 get_cookie_success_ = true;
355 } else {
356 EXPECT_EQ("", cookies);
357 get_cookie_success_ = false;
358 }
359 quit_closure_.Run();
360 }
361
362 void SetCookieCallback(bool result) {
363 ASSERT_TRUE(result);
364 quit_closure_.Run();
365 }
366
367 bool get_cookie_success_ = false;
368 base::Closure quit_closure_;
369
370 // CookieStore must out live |this|.
371 net::CookieStore* cookie_store_ = nullptr;
372
373 DISALLOW_COPY_AND_ASSIGN(RemoveCookieTester);
374 };
375
376 class RemoveChannelIDTester : public net::SSLConfigService::Observer {
377 public:
378 explicit RemoveChannelIDTester(BrowserContext* browser_context) {
379 net::URLRequestContext* url_request_context =
380 content::BrowserContext::GetDefaultStoragePartition(browser_context)
381 ->GetURLRequestContext()->GetURLRequestContext();
382 channel_id_service_ = url_request_context-> channel_id_service();
383 ssl_config_service_ = url_request_context->ssl_config_service();
384 ssl_config_service_->AddObserver(this);
385 }
386
387 ~RemoveChannelIDTester() override {
388 ssl_config_service_->RemoveObserver(this);
389 }
390
391 int ChannelIDCount() { return channel_id_service_->channel_id_count(); }
392
393 // Add a server bound cert for |server| with specific creation and expiry
394 // times. The cert and key data will be filled with dummy values.
395 void AddChannelIDWithTimes(const std::string& server_identifier,
396 base::Time creation_time) {
397 GetChannelIDStore()->SetChannelID(
398 base::MakeUnique<net::ChannelIDStore::ChannelID>(
399 server_identifier, creation_time, crypto::ECPrivateKey::Create()));
400 }
401
402 // Add a server bound cert for |server|, with the current time as the
403 // creation time. The cert and key data will be filled with dummy values.
404 void AddChannelID(const std::string& server_identifier) {
405 base::Time now = base::Time::Now();
406 AddChannelIDWithTimes(server_identifier, now);
407 }
408
409 void GetChannelIDList(net::ChannelIDStore::ChannelIDList* channel_ids) {
410 GetChannelIDStore()->GetAllChannelIDs(
411 base::Bind(&RemoveChannelIDTester::GetAllChannelIDsCallback,
412 channel_ids));
413 }
414
415 net::ChannelIDStore* GetChannelIDStore() {
416 return channel_id_service_->GetChannelIDStore();
417 }
418
419 int ssl_config_changed_count() const {
420 return ssl_config_changed_count_;
421 }
422
423 // net::SSLConfigService::Observer implementation:
424 void OnSSLConfigChanged() override { ssl_config_changed_count_++; }
425
426 private:
427 static void GetAllChannelIDsCallback(
428 net::ChannelIDStore::ChannelIDList* dest,
429 const net::ChannelIDStore::ChannelIDList& result) {
430 *dest = result;
431 }
432
433 net::ChannelIDService* channel_id_service_;
434 scoped_refptr<net::SSLConfigService> ssl_config_service_;
435 int ssl_config_changed_count_ = 0;
436
437 DISALLOW_COPY_AND_ASSIGN(RemoveChannelIDTester);
438 };
439
440 class RemoveLocalStorageTester {
441 public:
442 explicit RemoveLocalStorageTester(BrowserContext* browser_context)
443 : browser_context_(browser_context) {
444 dom_storage_context_ =
445 content::BrowserContext::GetDefaultStoragePartition(browser_context_)->
446 GetDOMStorageContext();
447 }
448
449 // Returns true, if the given origin URL exists.
450 bool DOMStorageExistsForOrigin(const GURL& origin) {
451 scoped_refptr<content::MessageLoopRunner> message_loop_runner =
452 new content::MessageLoopRunner;
453 quit_closure_ = message_loop_runner->QuitClosure();
454 GetLocalStorageUsage();
455 message_loop_runner->Run();
456 for (size_t i = 0; i < infos_.size(); ++i) {
457 if (origin == infos_[i].origin)
458 return true;
459 }
460 return false;
461 }
462
463 void AddDOMStorageTestData() {
464 // Note: This test depends on details of how the dom_storage library
465 // stores data in the host file system.
466 base::FilePath storage_path =
467 browser_context_->GetPath().AppendASCII("Local Storage");
468 base::CreateDirectory(storage_path);
469
470 // Write some files.
471 base::WriteFile(storage_path.Append(kDomStorageOrigin1), nullptr, 0);
472 base::WriteFile(storage_path.Append(kDomStorageOrigin2), nullptr, 0);
473 base::WriteFile(storage_path.Append(kDomStorageOrigin3), nullptr, 0);
474 base::WriteFile(storage_path.Append(kDomStorageExt), nullptr, 0);
475
476 // Tweak their dates.
477 base::Time now = base::Time::Now();
478 base::TouchFile(storage_path.Append(kDomStorageOrigin1), now, now);
479
480 base::Time one_day_ago = now - base::TimeDelta::FromDays(1);
481 base::TouchFile(storage_path.Append(kDomStorageOrigin2),
482 one_day_ago, one_day_ago);
483
484 base::Time sixty_days_ago = now - base::TimeDelta::FromDays(60);
485 base::TouchFile(storage_path.Append(kDomStorageOrigin3),
486 sixty_days_ago, sixty_days_ago);
487
488 base::TouchFile(storage_path.Append(kDomStorageExt), now, now);
489 }
490
491 private:
492 void GetLocalStorageUsage() {
493 dom_storage_context_->GetLocalStorageUsage(
494 base::Bind(&RemoveLocalStorageTester::OnGotLocalStorageUsage,
495 base::Unretained(this)));
496 }
497 void OnGotLocalStorageUsage(
498 const std::vector<content::LocalStorageUsageInfo>& infos) {
499 infos_ = infos;
500 quit_closure_.Run();
501 }
502
503 // We don't own these pointers.
504 BrowserContext* browser_context_;
505 content::DOMStorageContext* dom_storage_context_ = nullptr;
506
507 std::vector<content::LocalStorageUsageInfo> infos_;
508 base::Closure quit_closure_;
509
510 DISALLOW_COPY_AND_ASSIGN(RemoveLocalStorageTester);
511 };
512
513 class RemoveDownloadsTester {
514 public:
515 explicit RemoveDownloadsTester(BrowserContext* browser_context)
516 : download_manager_(new content::MockDownloadManager()) {
517 content::BrowserContext::SetDownloadManagerForTesting(
518 browser_context, base::WrapUnique(download_manager_));
519 EXPECT_EQ(download_manager_,
520 content::BrowserContext::GetDownloadManager(browser_context));
521 EXPECT_CALL(*download_manager_, Shutdown());
522 }
523
524 ~RemoveDownloadsTester() {}
525
526 content::MockDownloadManager* download_manager() { return download_manager_; }
527
528 private:
529 content::MockDownloadManager* download_manager_; // Owned by browser context.
530
531 DISALLOW_COPY_AND_ASSIGN(RemoveDownloadsTester);
532 };
533
534 // Test Class ----------------------------------------------------------------
535
536 class BrowsingDataRemoverImplTest : public testing::Test {
537 public:
538 BrowsingDataRemoverImplTest()
539 : browser_context_(new TestingProfile()) {
540 // TODO(crbug.com/668114): To create a BrowsingDataRemoverImpl, we currently
541 // need a BrowsingDataRemoverFactory which only exists for a Profile.
542 // Therefore, this test must use a TestingProfile for now. Switch it to
543 // a BrowserContext or TestBrowserContext when BrowsingDataRemoverImpl
544 // moves to content/. Furthermore, when in content/, BrowsingDataRemoverImpl
545 // will have no delegate. For now, explicitly set it to nullptr.
546 remover_ = static_cast<BrowsingDataRemoverImpl*>(
547 BrowsingDataRemoverFactory::GetForBrowserContext(
548 browser_context_.get()));
549 remover_->SetEmbedderDelegate(nullptr);
550 }
551
552 ~BrowsingDataRemoverImplTest() override {}
553
554 void TearDown() override {
555 mock_policy_ = nullptr;
556
557 // BrowserContext contains a DOMStorageContext. BrowserContext's
558 // destructor posts a message to the WEBKIT thread to delete some of its
559 // member variables. We need to ensure that the browser context is
560 // destroyed, and that the message loop is cleared out, before destroying
561 // the threads and loop. Otherwise we leak memory.
562 browser_context_.reset();
563 base::RunLoop().RunUntilIdle();
564 }
565
566 void BlockUntilBrowsingDataRemoved(const base::Time& delete_begin,
567 const base::Time& delete_end,
568 int remove_mask,
569 bool include_protected_origins) {
570 TestStoragePartition storage_partition;
571 remover_->OverrideStoragePartitionForTesting(&storage_partition);
572
573 int origin_type_mask = BrowsingDataRemover::ORIGIN_TYPE_UNPROTECTED_WEB;
574 if (include_protected_origins)
575 origin_type_mask |= BrowsingDataRemover::ORIGIN_TYPE_PROTECTED_WEB;
576
577 BrowsingDataRemoverCompletionObserver completion_observer(remover_);
578 remover_->RemoveAndReply(
579 delete_begin, delete_end, remove_mask, origin_type_mask,
580 &completion_observer);
581 completion_observer.BlockUntilCompletion();
582
583 // Save so we can verify later.
584 storage_partition_removal_data_ =
585 storage_partition.GetStoragePartitionRemovalData();
586 }
587
588 void BlockUntilOriginDataRemoved(
589 const base::Time& delete_begin,
590 const base::Time& delete_end,
591 int remove_mask,
592 std::unique_ptr<BrowsingDataFilterBuilder> filter_builder) {
593 TestStoragePartition storage_partition;
594 remover_->OverrideStoragePartitionForTesting(&storage_partition);
595
596 BrowsingDataRemoverCompletionObserver completion_observer(remover_);
597 remover_->RemoveWithFilterAndReply(
598 delete_begin, delete_end, remove_mask,
599 BrowsingDataRemover::ORIGIN_TYPE_UNPROTECTED_WEB,
600 std::move(filter_builder), &completion_observer);
601 completion_observer.BlockUntilCompletion();
602
603 // Save so we can verify later.
604 storage_partition_removal_data_ =
605 storage_partition.GetStoragePartitionRemovalData();
606 }
607
608 BrowserContext* GetBrowserContext() {
609 return browser_context_.get();
610 }
611
612 void DestroyBrowserContext() { browser_context_.reset(); }
613
614 const base::Time& GetBeginTime() {
615 return remover_->GetLastUsedBeginTime();
616 }
617
618 int GetRemovalMask() {
619 return remover_->GetLastUsedRemovalMask();
620 }
621
622 int GetOriginTypeMask() {
623 return remover_->GetLastUsedOriginTypeMask();
624 }
625
626 StoragePartitionRemovalData GetStoragePartitionRemovalData() {
627 return storage_partition_removal_data_;
628 }
629
630 content::MockSpecialStoragePolicy* CreateMockPolicy() {
631 mock_policy_ = new content::MockSpecialStoragePolicy();
632 return mock_policy_.get();
633 }
634
635 content::MockSpecialStoragePolicy* mock_policy() {
636 return mock_policy_.get();
637 }
638
639 bool Match(const GURL& origin,
640 int mask,
641 storage::SpecialStoragePolicy* policy) {
642 return remover_->DoesOriginMatchMask(mask, origin, policy);
643 }
644
645 private:
646 // Cached pointer to BrowsingDataRemoverImpl for access to testing methods.
647 BrowsingDataRemoverImpl* remover_;
648
649 content::TestBrowserThreadBundle thread_bundle_;
650 std::unique_ptr<BrowserContext> browser_context_;
651
652 StoragePartitionRemovalData storage_partition_removal_data_;
653
654 scoped_refptr<content::MockSpecialStoragePolicy> mock_policy_;
655
656 DISALLOW_COPY_AND_ASSIGN(BrowsingDataRemoverImplTest);
657 };
658
659 // Tests ---------------------------------------------------------------------
660
661 TEST_F(BrowsingDataRemoverImplTest, RemoveCookieForever) {
662 BlockUntilBrowsingDataRemoved(base::Time(), base::Time::Max(),
663 BrowsingDataRemover::DATA_TYPE_COOKIES, false);
664
665 EXPECT_EQ(BrowsingDataRemover::DATA_TYPE_COOKIES, GetRemovalMask());
666 EXPECT_EQ(BrowsingDataRemover::ORIGIN_TYPE_UNPROTECTED_WEB,
667 GetOriginTypeMask());
668
669 // Verify that storage partition was instructed to remove the cookies.
670 StoragePartitionRemovalData removal_data = GetStoragePartitionRemovalData();
671 EXPECT_EQ(removal_data.remove_mask,
672 StoragePartition::REMOVE_DATA_MASK_COOKIES);
673 EXPECT_EQ(removal_data.quota_storage_remove_mask,
674 StoragePartition::QUOTA_MANAGED_STORAGE_MASK_ALL);
675 EXPECT_EQ(removal_data.remove_begin, GetBeginTime());
676 }
677
678 TEST_F(BrowsingDataRemoverImplTest, RemoveCookieLastHour) {
679 BlockUntilBrowsingDataRemoved(AnHourAgo(), base::Time::Max(),
680 BrowsingDataRemover::DATA_TYPE_COOKIES, false);
681
682 EXPECT_EQ(BrowsingDataRemover::DATA_TYPE_COOKIES, GetRemovalMask());
683 EXPECT_EQ(BrowsingDataRemover::ORIGIN_TYPE_UNPROTECTED_WEB,
684 GetOriginTypeMask());
685
686 // Verify that storage partition was instructed to remove the cookies.
687 StoragePartitionRemovalData removal_data = GetStoragePartitionRemovalData();
688 EXPECT_EQ(removal_data.remove_mask,
689 StoragePartition::REMOVE_DATA_MASK_COOKIES);
690 // Removing with time period other than all time should not clear
691 // persistent storage data.
692 EXPECT_EQ(removal_data.quota_storage_remove_mask,
693 ~StoragePartition::QUOTA_MANAGED_STORAGE_MASK_PERSISTENT);
694 EXPECT_EQ(removal_data.remove_begin, GetBeginTime());
695 }
696
697 TEST_F(BrowsingDataRemoverImplTest, RemoveCookiesDomainBlacklist) {
698 std::unique_ptr<BrowsingDataFilterBuilder> filter(
699 BrowsingDataFilterBuilder::Create(BrowsingDataFilterBuilder::BLACKLIST));
700 filter->AddRegisterableDomain(kTestRegisterableDomain1);
701 filter->AddRegisterableDomain(kTestRegisterableDomain3);
702 BlockUntilOriginDataRemoved(AnHourAgo(), base::Time::Max(),
703 BrowsingDataRemover::DATA_TYPE_COOKIES,
704 std::move(filter));
705
706 EXPECT_EQ(BrowsingDataRemover::DATA_TYPE_COOKIES, GetRemovalMask());
707 EXPECT_EQ(BrowsingDataRemover::ORIGIN_TYPE_UNPROTECTED_WEB,
708 GetOriginTypeMask());
709
710 // Verify that storage partition was instructed to remove the cookies.
711 StoragePartitionRemovalData removal_data = GetStoragePartitionRemovalData();
712 EXPECT_EQ(removal_data.remove_mask,
713 StoragePartition::REMOVE_DATA_MASK_COOKIES);
714 // Removing with time period other than all time should not clear
715 // persistent storage data.
716 EXPECT_EQ(removal_data.quota_storage_remove_mask,
717 ~StoragePartition::QUOTA_MANAGED_STORAGE_MASK_PERSISTENT);
718 EXPECT_EQ(removal_data.remove_begin, GetBeginTime());
719 EXPECT_FALSE(removal_data.origin_matcher.Run(kOrigin1, mock_policy()));
720 EXPECT_TRUE(removal_data.origin_matcher.Run(kOrigin2, mock_policy()));
721 EXPECT_FALSE(removal_data.origin_matcher.Run(kOrigin3, mock_policy()));
722 // Even though it's a different origin, it's the same domain.
723 EXPECT_FALSE(removal_data.origin_matcher.Run(kOrigin4, mock_policy()));
724
725 EXPECT_FALSE(removal_data.cookie_matcher.Run(CreateCookieWithHost(kOrigin1)));
726 EXPECT_TRUE(removal_data.cookie_matcher.Run(CreateCookieWithHost(kOrigin2)));
727 EXPECT_FALSE(removal_data.cookie_matcher.Run(CreateCookieWithHost(kOrigin3)));
728 // This is false, because this is the same domain as 3, just with a different
729 // scheme.
730 EXPECT_FALSE(removal_data.cookie_matcher.Run(CreateCookieWithHost(kOrigin4)));
731 }
732
733 // Test that removing cookies clears HTTP auth data.
734 TEST_F(BrowsingDataRemoverImplTest, ClearHttpAuthCache_RemoveCookies) {
735 net::HttpNetworkSession* http_session =
736 content::BrowserContext::GetDefaultStoragePartition(GetBrowserContext())
737 ->GetURLRequestContext()
738 ->GetURLRequestContext()
739 ->http_transaction_factory()
740 ->GetSession();
741 DCHECK(http_session);
742
743 net::HttpAuthCache* http_auth_cache = http_session->http_auth_cache();
744 http_auth_cache->Add(kOrigin1, kTestRealm, net::HttpAuth::AUTH_SCHEME_BASIC,
745 "test challenge",
746 net::AuthCredentials(base::ASCIIToUTF16("foo"),
747 base::ASCIIToUTF16("bar")),
748 "/");
749 CHECK(http_auth_cache->Lookup(kOrigin1, kTestRealm,
750 net::HttpAuth::AUTH_SCHEME_BASIC));
751
752 BlockUntilBrowsingDataRemoved(base::Time(), base::Time::Max(),
753 BrowsingDataRemover::DATA_TYPE_COOKIES, false);
754
755 EXPECT_EQ(nullptr, http_auth_cache->Lookup(kOrigin1, kTestRealm,
756 net::HttpAuth::AUTH_SCHEME_BASIC));
757 }
758
759 TEST_F(BrowsingDataRemoverImplTest, RemoveChannelIDForever) {
760 RemoveChannelIDTester tester(GetBrowserContext());
761
762 tester.AddChannelID(kTestOrigin1);
763 EXPECT_EQ(0, tester.ssl_config_changed_count());
764 EXPECT_EQ(1, tester.ChannelIDCount());
765
766 BlockUntilBrowsingDataRemoved(base::Time(), base::Time::Max(),
767 BrowsingDataRemover::DATA_TYPE_CHANNEL_IDS,
768 false);
769
770 EXPECT_EQ(BrowsingDataRemover::DATA_TYPE_CHANNEL_IDS, GetRemovalMask());
771 EXPECT_EQ(BrowsingDataRemover::ORIGIN_TYPE_UNPROTECTED_WEB,
772 GetOriginTypeMask());
773 EXPECT_EQ(1, tester.ssl_config_changed_count());
774 EXPECT_EQ(0, tester.ChannelIDCount());
775 }
776
777 TEST_F(BrowsingDataRemoverImplTest, RemoveChannelIDLastHour) {
778 RemoveChannelIDTester tester(GetBrowserContext());
779
780 base::Time now = base::Time::Now();
781 tester.AddChannelID(kTestOrigin1);
782 tester.AddChannelIDWithTimes(kTestOrigin2,
783 now - base::TimeDelta::FromHours(2));
784 EXPECT_EQ(0, tester.ssl_config_changed_count());
785 EXPECT_EQ(2, tester.ChannelIDCount());
786
787 BlockUntilBrowsingDataRemoved(AnHourAgo(), base::Time::Max(),
788 BrowsingDataRemover::DATA_TYPE_CHANNEL_IDS,
789 false);
790
791 EXPECT_EQ(BrowsingDataRemover::DATA_TYPE_CHANNEL_IDS, GetRemovalMask());
792 EXPECT_EQ(BrowsingDataRemover::ORIGIN_TYPE_UNPROTECTED_WEB,
793 GetOriginTypeMask());
794 EXPECT_EQ(1, tester.ssl_config_changed_count());
795 ASSERT_EQ(1, tester.ChannelIDCount());
796 net::ChannelIDStore::ChannelIDList channel_ids;
797 tester.GetChannelIDList(&channel_ids);
798 ASSERT_EQ(1U, channel_ids.size());
799 EXPECT_EQ(kTestOrigin2, channel_ids.front().server_identifier());
800 }
801
802 TEST_F(BrowsingDataRemoverImplTest, RemoveChannelIDsForServerIdentifiers) {
803 RemoveChannelIDTester tester(GetBrowserContext());
804
805 tester.AddChannelID(kTestRegisterableDomain1);
806 tester.AddChannelID(kTestRegisterableDomain3);
807 EXPECT_EQ(2, tester.ChannelIDCount());
808
809 std::unique_ptr<BrowsingDataFilterBuilder> filter_builder(
810 BrowsingDataFilterBuilder::Create(BrowsingDataFilterBuilder::WHITELIST));
811 filter_builder->AddRegisterableDomain(kTestRegisterableDomain1);
812
813 BlockUntilOriginDataRemoved(base::Time(), base::Time::Max(),
814 BrowsingDataRemover::DATA_TYPE_CHANNEL_IDS,
815 std::move(filter_builder));
816
817 EXPECT_EQ(1, tester.ChannelIDCount());
818 net::ChannelIDStore::ChannelIDList channel_ids;
819 tester.GetChannelIDList(&channel_ids);
820 EXPECT_EQ(kTestRegisterableDomain3, channel_ids.front().server_identifier());
821 }
822
823 TEST_F(BrowsingDataRemoverImplTest, RemoveUnprotectedLocalStorageForever) {
824 content::MockSpecialStoragePolicy* policy = CreateMockPolicy();
825 // Protect kOrigin1.
826 policy->AddProtected(kOrigin1.GetOrigin());
827
828 BlockUntilBrowsingDataRemoved(base::Time(), base::Time::Max(),
829 BrowsingDataRemover::DATA_TYPE_LOCAL_STORAGE,
830 false);
831
832 EXPECT_EQ(BrowsingDataRemover::DATA_TYPE_LOCAL_STORAGE, GetRemovalMask());
833 EXPECT_EQ(BrowsingDataRemover::ORIGIN_TYPE_UNPROTECTED_WEB,
834 GetOriginTypeMask());
835
836 // Verify that storage partition was instructed to remove the data correctly.
837 StoragePartitionRemovalData removal_data = GetStoragePartitionRemovalData();
838 EXPECT_EQ(removal_data.remove_mask,
839 StoragePartition::REMOVE_DATA_MASK_LOCAL_STORAGE);
840 EXPECT_EQ(removal_data.quota_storage_remove_mask,
841 StoragePartition::QUOTA_MANAGED_STORAGE_MASK_ALL);
842 EXPECT_EQ(removal_data.remove_begin, GetBeginTime());
843
844 // Check origin matcher.
845 EXPECT_FALSE(removal_data.origin_matcher.Run(kOrigin1, mock_policy()));
846 EXPECT_TRUE(removal_data.origin_matcher.Run(kOrigin2, mock_policy()));
847 EXPECT_TRUE(removal_data.origin_matcher.Run(kOrigin3, mock_policy()));
848 EXPECT_FALSE(removal_data.origin_matcher.Run(kOriginExt, mock_policy()));
849 }
850
851 TEST_F(BrowsingDataRemoverImplTest, RemoveProtectedLocalStorageForever) {
852 // Protect kOrigin1.
853 content::MockSpecialStoragePolicy* policy = CreateMockPolicy();
854 policy->AddProtected(kOrigin1.GetOrigin());
855
856 BlockUntilBrowsingDataRemoved(base::Time(), base::Time::Max(),
857 BrowsingDataRemover::DATA_TYPE_LOCAL_STORAGE,
858 true);
859
860 EXPECT_EQ(BrowsingDataRemover::DATA_TYPE_LOCAL_STORAGE, GetRemovalMask());
861 EXPECT_EQ(BrowsingDataRemover::ORIGIN_TYPE_UNPROTECTED_WEB |
862 BrowsingDataRemover::ORIGIN_TYPE_PROTECTED_WEB,
863 GetOriginTypeMask());
864
865 // Verify that storage partition was instructed to remove the data correctly.
866 StoragePartitionRemovalData removal_data = GetStoragePartitionRemovalData();
867 EXPECT_EQ(removal_data.remove_mask,
868 StoragePartition::REMOVE_DATA_MASK_LOCAL_STORAGE);
869 EXPECT_EQ(removal_data.quota_storage_remove_mask,
870 StoragePartition::QUOTA_MANAGED_STORAGE_MASK_ALL);
871 EXPECT_EQ(removal_data.remove_begin, GetBeginTime());
872
873 // Check origin matcher all http origin will match since we specified
874 // both protected and unprotected.
875 EXPECT_TRUE(removal_data.origin_matcher.Run(kOrigin1, mock_policy()));
876 EXPECT_TRUE(removal_data.origin_matcher.Run(kOrigin2, mock_policy()));
877 EXPECT_TRUE(removal_data.origin_matcher.Run(kOrigin3, mock_policy()));
878 EXPECT_FALSE(removal_data.origin_matcher.Run(kOriginExt, mock_policy()));
879 }
880
881 TEST_F(BrowsingDataRemoverImplTest, RemoveLocalStorageForLastWeek) {
882 #if BUILDFLAG(ENABLE_EXTENSIONS)
883 CreateMockPolicy();
884 #endif
885
886 BlockUntilBrowsingDataRemoved(
887 base::Time::Now() - base::TimeDelta::FromDays(7), base::Time::Max(),
888 BrowsingDataRemover::DATA_TYPE_LOCAL_STORAGE, false);
889
890 EXPECT_EQ(BrowsingDataRemover::DATA_TYPE_LOCAL_STORAGE, GetRemovalMask());
891 EXPECT_EQ(BrowsingDataRemover::ORIGIN_TYPE_UNPROTECTED_WEB,
892 GetOriginTypeMask());
893
894 // Verify that storage partition was instructed to remove the data correctly.
895 StoragePartitionRemovalData removal_data = GetStoragePartitionRemovalData();
896 EXPECT_EQ(removal_data.remove_mask,
897 StoragePartition::REMOVE_DATA_MASK_LOCAL_STORAGE);
898 // Persistent storage won't be deleted.
899 EXPECT_EQ(removal_data.quota_storage_remove_mask,
900 ~StoragePartition::QUOTA_MANAGED_STORAGE_MASK_PERSISTENT);
901 EXPECT_EQ(removal_data.remove_begin, GetBeginTime());
902
903 // Check origin matcher.
904 EXPECT_TRUE(removal_data.origin_matcher.Run(kOrigin1, mock_policy()));
905 EXPECT_TRUE(removal_data.origin_matcher.Run(kOrigin2, mock_policy()));
906 EXPECT_TRUE(removal_data.origin_matcher.Run(kOrigin3, mock_policy()));
907 EXPECT_FALSE(removal_data.origin_matcher.Run(kOriginExt, mock_policy()));
908 }
909
910 TEST_F(BrowsingDataRemoverImplTest, RemoveMultipleTypes) {
911 // Downloads should be deleted through the DownloadManager, assure it would
912 // be called.
913 RemoveDownloadsTester downloads_tester(GetBrowserContext());
914 EXPECT_CALL(*downloads_tester.download_manager(),
915 RemoveDownloadsByURLAndTime(_, _, _));
916
917 int removal_mask = BrowsingDataRemover::DATA_TYPE_DOWNLOADS |
918 BrowsingDataRemover::DATA_TYPE_COOKIES;
919
920 BlockUntilBrowsingDataRemoved(base::Time(), base::Time::Max(),
921 removal_mask, false);
922
923 EXPECT_EQ(removal_mask, GetRemovalMask());
924 EXPECT_EQ(BrowsingDataRemover::ORIGIN_TYPE_UNPROTECTED_WEB,
925 GetOriginTypeMask());
926
927 // The cookie would be deleted throught the StorageParition, check if the
928 // partition was requested to remove cookie.
929 StoragePartitionRemovalData removal_data = GetStoragePartitionRemovalData();
930 EXPECT_EQ(removal_data.remove_mask,
931 StoragePartition::REMOVE_DATA_MASK_COOKIES);
932 EXPECT_EQ(removal_data.quota_storage_remove_mask,
933 StoragePartition::QUOTA_MANAGED_STORAGE_MASK_ALL);
934 }
935
936 TEST_F(BrowsingDataRemoverImplTest, RemoveQuotaManagedDataForeverBoth) {
937 BlockUntilBrowsingDataRemoved(
938 base::Time(), base::Time::Max(),
939 BrowsingDataRemover::DATA_TYPE_FILE_SYSTEMS |
940 BrowsingDataRemover::DATA_TYPE_WEB_SQL |
941 BrowsingDataRemover::DATA_TYPE_APP_CACHE |
942 BrowsingDataRemover::DATA_TYPE_SERVICE_WORKERS |
943 BrowsingDataRemover::DATA_TYPE_CACHE_STORAGE |
944 BrowsingDataRemover::DATA_TYPE_INDEXED_DB,
945 false);
946
947 EXPECT_EQ(BrowsingDataRemover::DATA_TYPE_FILE_SYSTEMS |
948 BrowsingDataRemover::DATA_TYPE_WEB_SQL |
949 BrowsingDataRemover::DATA_TYPE_APP_CACHE |
950 BrowsingDataRemover::DATA_TYPE_SERVICE_WORKERS |
951 BrowsingDataRemover::DATA_TYPE_CACHE_STORAGE |
952 BrowsingDataRemover::DATA_TYPE_INDEXED_DB,
953 GetRemovalMask());
954 EXPECT_EQ(BrowsingDataRemover::ORIGIN_TYPE_UNPROTECTED_WEB,
955 GetOriginTypeMask());
956
957 // Verify storage partition related stuffs.
958 StoragePartitionRemovalData removal_data = GetStoragePartitionRemovalData();
959 EXPECT_EQ(removal_data.remove_mask,
960 StoragePartition::REMOVE_DATA_MASK_FILE_SYSTEMS |
961 StoragePartition::REMOVE_DATA_MASK_WEBSQL |
962 StoragePartition::REMOVE_DATA_MASK_APPCACHE |
963 StoragePartition::REMOVE_DATA_MASK_SERVICE_WORKERS |
964 StoragePartition::REMOVE_DATA_MASK_CACHE_STORAGE |
965 StoragePartition::REMOVE_DATA_MASK_INDEXEDDB);
966 EXPECT_EQ(removal_data.quota_storage_remove_mask,
967 StoragePartition::QUOTA_MANAGED_STORAGE_MASK_ALL);
968 }
969
970 TEST_F(BrowsingDataRemoverImplTest,
971 RemoveQuotaManagedDataForeverOnlyTemporary) {
972 #if BUILDFLAG(ENABLE_EXTENSIONS)
973 CreateMockPolicy();
974 #endif
975
976 BlockUntilBrowsingDataRemoved(
977 base::Time(), base::Time::Max(),
978 BrowsingDataRemover::DATA_TYPE_FILE_SYSTEMS |
979 BrowsingDataRemover::DATA_TYPE_WEB_SQL |
980 BrowsingDataRemover::DATA_TYPE_APP_CACHE |
981 BrowsingDataRemover::DATA_TYPE_SERVICE_WORKERS |
982 BrowsingDataRemover::DATA_TYPE_CACHE_STORAGE |
983 BrowsingDataRemover::DATA_TYPE_INDEXED_DB,
984 false);
985
986 EXPECT_EQ(BrowsingDataRemover::DATA_TYPE_FILE_SYSTEMS |
987 BrowsingDataRemover::DATA_TYPE_WEB_SQL |
988 BrowsingDataRemover::DATA_TYPE_APP_CACHE |
989 BrowsingDataRemover::DATA_TYPE_SERVICE_WORKERS |
990 BrowsingDataRemover::DATA_TYPE_CACHE_STORAGE |
991 BrowsingDataRemover::DATA_TYPE_INDEXED_DB,
992 GetRemovalMask());
993 EXPECT_EQ(BrowsingDataRemover::ORIGIN_TYPE_UNPROTECTED_WEB,
994 GetOriginTypeMask());
995
996 // Verify storage partition related stuffs.
997 StoragePartitionRemovalData removal_data = GetStoragePartitionRemovalData();
998
999 EXPECT_EQ(removal_data.remove_mask,
1000 StoragePartition::REMOVE_DATA_MASK_FILE_SYSTEMS |
1001 StoragePartition::REMOVE_DATA_MASK_WEBSQL |
1002 StoragePartition::REMOVE_DATA_MASK_APPCACHE |
1003 StoragePartition::REMOVE_DATA_MASK_SERVICE_WORKERS |
1004 StoragePartition::REMOVE_DATA_MASK_CACHE_STORAGE |
1005 StoragePartition::REMOVE_DATA_MASK_INDEXEDDB);
1006 EXPECT_EQ(removal_data.quota_storage_remove_mask,
1007 StoragePartition::QUOTA_MANAGED_STORAGE_MASK_ALL);
1008
1009 // Check that all related origin data would be removed, that is, origin
1010 // matcher would match these origin.
1011 EXPECT_TRUE(removal_data.origin_matcher.Run(kOrigin1, mock_policy()));
1012 EXPECT_TRUE(removal_data.origin_matcher.Run(kOrigin2, mock_policy()));
1013 EXPECT_TRUE(removal_data.origin_matcher.Run(kOrigin3, mock_policy()));
1014 }
1015
1016 TEST_F(BrowsingDataRemoverImplTest,
1017 RemoveQuotaManagedDataForeverOnlyPersistent) {
1018 #if BUILDFLAG(ENABLE_EXTENSIONS)
1019 CreateMockPolicy();
1020 #endif
1021
1022 BlockUntilBrowsingDataRemoved(
1023 base::Time(), base::Time::Max(),
1024 BrowsingDataRemover::DATA_TYPE_FILE_SYSTEMS |
1025 BrowsingDataRemover::DATA_TYPE_WEB_SQL |
1026 BrowsingDataRemover::DATA_TYPE_APP_CACHE |
1027 BrowsingDataRemover::DATA_TYPE_SERVICE_WORKERS |
1028 BrowsingDataRemover::DATA_TYPE_CACHE_STORAGE |
1029 BrowsingDataRemover::DATA_TYPE_INDEXED_DB,
1030 false);
1031
1032 EXPECT_EQ(BrowsingDataRemover::DATA_TYPE_FILE_SYSTEMS |
1033 BrowsingDataRemover::DATA_TYPE_WEB_SQL |
1034 BrowsingDataRemover::DATA_TYPE_APP_CACHE |
1035 BrowsingDataRemover::DATA_TYPE_SERVICE_WORKERS |
1036 BrowsingDataRemover::DATA_TYPE_CACHE_STORAGE |
1037 BrowsingDataRemover::DATA_TYPE_INDEXED_DB,
1038 GetRemovalMask());
1039 EXPECT_EQ(BrowsingDataRemover::ORIGIN_TYPE_UNPROTECTED_WEB,
1040 GetOriginTypeMask());
1041
1042 // Verify storage partition related stuffs.
1043 StoragePartitionRemovalData removal_data = GetStoragePartitionRemovalData();
1044
1045 EXPECT_EQ(removal_data.remove_mask,
1046 StoragePartition::REMOVE_DATA_MASK_FILE_SYSTEMS |
1047 StoragePartition::REMOVE_DATA_MASK_WEBSQL |
1048 StoragePartition::REMOVE_DATA_MASK_APPCACHE |
1049 StoragePartition::REMOVE_DATA_MASK_SERVICE_WORKERS |
1050 StoragePartition::REMOVE_DATA_MASK_CACHE_STORAGE |
1051 StoragePartition::REMOVE_DATA_MASK_INDEXEDDB);
1052 EXPECT_EQ(removal_data.quota_storage_remove_mask,
1053 StoragePartition::QUOTA_MANAGED_STORAGE_MASK_ALL);
1054
1055 // Check that all related origin data would be removed, that is, origin
1056 // matcher would match these origin.
1057 EXPECT_TRUE(removal_data.origin_matcher.Run(kOrigin1, mock_policy()));
1058 EXPECT_TRUE(removal_data.origin_matcher.Run(kOrigin2, mock_policy()));
1059 EXPECT_TRUE(removal_data.origin_matcher.Run(kOrigin3, mock_policy()));
1060 }
1061
1062 TEST_F(BrowsingDataRemoverImplTest, RemoveQuotaManagedDataForeverNeither) {
1063 #if BUILDFLAG(ENABLE_EXTENSIONS)
1064 CreateMockPolicy();
1065 #endif
1066
1067 BlockUntilBrowsingDataRemoved(
1068 base::Time(), base::Time::Max(),
1069 BrowsingDataRemover::DATA_TYPE_FILE_SYSTEMS |
1070 BrowsingDataRemover::DATA_TYPE_WEB_SQL |
1071 BrowsingDataRemover::DATA_TYPE_APP_CACHE |
1072 BrowsingDataRemover::DATA_TYPE_SERVICE_WORKERS |
1073 BrowsingDataRemover::DATA_TYPE_CACHE_STORAGE |
1074 BrowsingDataRemover::DATA_TYPE_INDEXED_DB,
1075 false);
1076
1077 EXPECT_EQ(BrowsingDataRemover::DATA_TYPE_FILE_SYSTEMS |
1078 BrowsingDataRemover::DATA_TYPE_WEB_SQL |
1079 BrowsingDataRemover::DATA_TYPE_APP_CACHE |
1080 BrowsingDataRemover::DATA_TYPE_SERVICE_WORKERS |
1081 BrowsingDataRemover::DATA_TYPE_CACHE_STORAGE |
1082 BrowsingDataRemover::DATA_TYPE_INDEXED_DB,
1083 GetRemovalMask());
1084 EXPECT_EQ(BrowsingDataRemover::ORIGIN_TYPE_UNPROTECTED_WEB,
1085 GetOriginTypeMask());
1086
1087 // Verify storage partition related stuffs.
1088 StoragePartitionRemovalData removal_data = GetStoragePartitionRemovalData();
1089
1090 EXPECT_EQ(removal_data.remove_mask,
1091 StoragePartition::REMOVE_DATA_MASK_FILE_SYSTEMS |
1092 StoragePartition::REMOVE_DATA_MASK_WEBSQL |
1093 StoragePartition::REMOVE_DATA_MASK_APPCACHE |
1094 StoragePartition::REMOVE_DATA_MASK_SERVICE_WORKERS |
1095 StoragePartition::REMOVE_DATA_MASK_CACHE_STORAGE |
1096 StoragePartition::REMOVE_DATA_MASK_INDEXEDDB);
1097 EXPECT_EQ(removal_data.quota_storage_remove_mask,
1098 StoragePartition::QUOTA_MANAGED_STORAGE_MASK_ALL);
1099
1100 // Check that all related origin data would be removed, that is, origin
1101 // matcher would match these origin.
1102 EXPECT_TRUE(removal_data.origin_matcher.Run(kOrigin1, mock_policy()));
1103 EXPECT_TRUE(removal_data.origin_matcher.Run(kOrigin2, mock_policy()));
1104 EXPECT_TRUE(removal_data.origin_matcher.Run(kOrigin3, mock_policy()));
1105 }
1106
1107 TEST_F(BrowsingDataRemoverImplTest,
1108 RemoveQuotaManagedDataForeverSpecificOrigin) {
1109 std::unique_ptr<BrowsingDataFilterBuilder> builder(
1110 BrowsingDataFilterBuilder::Create(BrowsingDataFilterBuilder::WHITELIST));
1111 builder->AddRegisterableDomain(kTestRegisterableDomain1);
1112 // Remove Origin 1.
1113 BlockUntilOriginDataRemoved(
1114 base::Time(), base::Time::Max(),
1115 BrowsingDataRemover::DATA_TYPE_APP_CACHE |
1116 BrowsingDataRemover::DATA_TYPE_SERVICE_WORKERS |
1117 BrowsingDataRemover::DATA_TYPE_CACHE_STORAGE |
1118 BrowsingDataRemover::DATA_TYPE_FILE_SYSTEMS |
1119 BrowsingDataRemover::DATA_TYPE_INDEXED_DB |
1120 BrowsingDataRemover::DATA_TYPE_WEB_SQL,
1121 std::move(builder));
1122
1123 EXPECT_EQ(BrowsingDataRemover::DATA_TYPE_APP_CACHE |
1124 BrowsingDataRemover::DATA_TYPE_SERVICE_WORKERS |
1125 BrowsingDataRemover::DATA_TYPE_CACHE_STORAGE |
1126 BrowsingDataRemover::DATA_TYPE_FILE_SYSTEMS |
1127 BrowsingDataRemover::DATA_TYPE_INDEXED_DB |
1128 BrowsingDataRemover::DATA_TYPE_WEB_SQL,
1129 GetRemovalMask());
1130 EXPECT_EQ(BrowsingDataRemover::ORIGIN_TYPE_UNPROTECTED_WEB,
1131 GetOriginTypeMask());
1132
1133 // Verify storage partition related stuffs.
1134 StoragePartitionRemovalData removal_data = GetStoragePartitionRemovalData();
1135
1136 EXPECT_EQ(removal_data.remove_mask,
1137 StoragePartition::REMOVE_DATA_MASK_FILE_SYSTEMS |
1138 StoragePartition::REMOVE_DATA_MASK_WEBSQL |
1139 StoragePartition::REMOVE_DATA_MASK_APPCACHE |
1140 StoragePartition::REMOVE_DATA_MASK_SERVICE_WORKERS |
1141 StoragePartition::REMOVE_DATA_MASK_CACHE_STORAGE |
1142 StoragePartition::REMOVE_DATA_MASK_INDEXEDDB);
1143 EXPECT_EQ(removal_data.quota_storage_remove_mask,
1144 StoragePartition::QUOTA_MANAGED_STORAGE_MASK_ALL);
1145 EXPECT_TRUE(removal_data.origin_matcher.Run(kOrigin1, mock_policy()));
1146 EXPECT_FALSE(removal_data.origin_matcher.Run(kOrigin2, mock_policy()));
1147 EXPECT_FALSE(removal_data.origin_matcher.Run(kOrigin3, mock_policy()));
1148 EXPECT_FALSE(removal_data.origin_matcher.Run(kOrigin4, mock_policy()));
1149 }
1150
1151 TEST_F(BrowsingDataRemoverImplTest, RemoveQuotaManagedDataForLastHour) {
1152 BlockUntilBrowsingDataRemoved(
1153 AnHourAgo(), base::Time::Max(),
1154 BrowsingDataRemover::DATA_TYPE_FILE_SYSTEMS |
1155 BrowsingDataRemover::DATA_TYPE_WEB_SQL |
1156 BrowsingDataRemover::DATA_TYPE_APP_CACHE |
1157 BrowsingDataRemover::DATA_TYPE_SERVICE_WORKERS |
1158 BrowsingDataRemover::DATA_TYPE_CACHE_STORAGE |
1159 BrowsingDataRemover::DATA_TYPE_INDEXED_DB,
1160 false);
1161
1162 EXPECT_EQ(BrowsingDataRemover::DATA_TYPE_FILE_SYSTEMS |
1163 BrowsingDataRemover::DATA_TYPE_WEB_SQL |
1164 BrowsingDataRemover::DATA_TYPE_APP_CACHE |
1165 BrowsingDataRemover::DATA_TYPE_SERVICE_WORKERS |
1166 BrowsingDataRemover::DATA_TYPE_CACHE_STORAGE |
1167 BrowsingDataRemover::DATA_TYPE_INDEXED_DB,
1168 GetRemovalMask());
1169 EXPECT_EQ(BrowsingDataRemover::ORIGIN_TYPE_UNPROTECTED_WEB,
1170 GetOriginTypeMask());
1171
1172 // Verify storage partition related stuffs.
1173 StoragePartitionRemovalData removal_data = GetStoragePartitionRemovalData();
1174
1175 EXPECT_EQ(removal_data.remove_mask,
1176 StoragePartition::REMOVE_DATA_MASK_FILE_SYSTEMS |
1177 StoragePartition::REMOVE_DATA_MASK_WEBSQL |
1178 StoragePartition::REMOVE_DATA_MASK_APPCACHE |
1179 StoragePartition::REMOVE_DATA_MASK_SERVICE_WORKERS |
1180 StoragePartition::REMOVE_DATA_MASK_CACHE_STORAGE |
1181 StoragePartition::REMOVE_DATA_MASK_INDEXEDDB);
1182
1183 // Persistent data would be left out since we are not removing from
1184 // beginning of time.
1185 uint32_t expected_quota_mask =
1186 ~StoragePartition::QUOTA_MANAGED_STORAGE_MASK_PERSISTENT;
1187 EXPECT_EQ(removal_data.quota_storage_remove_mask, expected_quota_mask);
1188 // Check removal begin time.
1189 EXPECT_EQ(removal_data.remove_begin, GetBeginTime());
1190 }
1191
1192 TEST_F(BrowsingDataRemoverImplTest, RemoveQuotaManagedDataForLastWeek) {
1193 BlockUntilBrowsingDataRemoved(
1194 base::Time::Now() - base::TimeDelta::FromDays(7), base::Time::Max(),
1195 BrowsingDataRemover::DATA_TYPE_FILE_SYSTEMS |
1196 BrowsingDataRemover::DATA_TYPE_WEB_SQL |
1197 BrowsingDataRemover::DATA_TYPE_APP_CACHE |
1198 BrowsingDataRemover::DATA_TYPE_SERVICE_WORKERS |
1199 BrowsingDataRemover::DATA_TYPE_CACHE_STORAGE |
1200 BrowsingDataRemover::DATA_TYPE_INDEXED_DB,
1201 false);
1202
1203 EXPECT_EQ(BrowsingDataRemover::DATA_TYPE_FILE_SYSTEMS |
1204 BrowsingDataRemover::DATA_TYPE_WEB_SQL |
1205 BrowsingDataRemover::DATA_TYPE_APP_CACHE |
1206 BrowsingDataRemover::DATA_TYPE_SERVICE_WORKERS |
1207 BrowsingDataRemover::DATA_TYPE_CACHE_STORAGE |
1208 BrowsingDataRemover::DATA_TYPE_INDEXED_DB,
1209 GetRemovalMask());
1210 EXPECT_EQ(BrowsingDataRemover::ORIGIN_TYPE_UNPROTECTED_WEB,
1211 GetOriginTypeMask());
1212
1213 // Verify storage partition related stuffs.
1214 StoragePartitionRemovalData removal_data = GetStoragePartitionRemovalData();
1215
1216 EXPECT_EQ(removal_data.remove_mask,
1217 StoragePartition::REMOVE_DATA_MASK_FILE_SYSTEMS |
1218 StoragePartition::REMOVE_DATA_MASK_WEBSQL |
1219 StoragePartition::REMOVE_DATA_MASK_APPCACHE |
1220 StoragePartition::REMOVE_DATA_MASK_SERVICE_WORKERS |
1221 StoragePartition::REMOVE_DATA_MASK_CACHE_STORAGE |
1222 StoragePartition::REMOVE_DATA_MASK_INDEXEDDB);
1223
1224 // Persistent data would be left out since we are not removing from
1225 // beginning of time.
1226 uint32_t expected_quota_mask =
1227 ~StoragePartition::QUOTA_MANAGED_STORAGE_MASK_PERSISTENT;
1228 EXPECT_EQ(removal_data.quota_storage_remove_mask, expected_quota_mask);
1229 // Check removal begin time.
1230 EXPECT_EQ(removal_data.remove_begin, GetBeginTime());
1231 }
1232
1233 TEST_F(BrowsingDataRemoverImplTest, RemoveQuotaManagedUnprotectedOrigins) {
1234 content::MockSpecialStoragePolicy* policy = CreateMockPolicy();
1235 // Protect kOrigin1.
1236 policy->AddProtected(kOrigin1.GetOrigin());
1237
1238 BlockUntilBrowsingDataRemoved(
1239 base::Time(), base::Time::Max(),
1240 BrowsingDataRemover::DATA_TYPE_FILE_SYSTEMS |
1241 BrowsingDataRemover::DATA_TYPE_WEB_SQL |
1242 BrowsingDataRemover::DATA_TYPE_APP_CACHE |
1243 BrowsingDataRemover::DATA_TYPE_SERVICE_WORKERS |
1244 BrowsingDataRemover::DATA_TYPE_CACHE_STORAGE |
1245 BrowsingDataRemover::DATA_TYPE_INDEXED_DB,
1246 false);
1247
1248 EXPECT_EQ(BrowsingDataRemover::DATA_TYPE_FILE_SYSTEMS |
1249 BrowsingDataRemover::DATA_TYPE_WEB_SQL |
1250 BrowsingDataRemover::DATA_TYPE_APP_CACHE |
1251 BrowsingDataRemover::DATA_TYPE_SERVICE_WORKERS |
1252 BrowsingDataRemover::DATA_TYPE_CACHE_STORAGE |
1253 BrowsingDataRemover::DATA_TYPE_INDEXED_DB,
1254 GetRemovalMask());
1255 EXPECT_EQ(BrowsingDataRemover::ORIGIN_TYPE_UNPROTECTED_WEB,
1256 GetOriginTypeMask());
1257
1258 // Verify storage partition related stuffs.
1259 StoragePartitionRemovalData removal_data = GetStoragePartitionRemovalData();
1260
1261 EXPECT_EQ(removal_data.remove_mask,
1262 StoragePartition::REMOVE_DATA_MASK_FILE_SYSTEMS |
1263 StoragePartition::REMOVE_DATA_MASK_WEBSQL |
1264 StoragePartition::REMOVE_DATA_MASK_APPCACHE |
1265 StoragePartition::REMOVE_DATA_MASK_SERVICE_WORKERS |
1266 StoragePartition::REMOVE_DATA_MASK_CACHE_STORAGE |
1267 StoragePartition::REMOVE_DATA_MASK_INDEXEDDB);
1268 EXPECT_EQ(removal_data.quota_storage_remove_mask,
1269 StoragePartition::QUOTA_MANAGED_STORAGE_MASK_ALL);
1270
1271 // Check OriginMatcherFunction.
1272 EXPECT_FALSE(removal_data.origin_matcher.Run(kOrigin1, mock_policy()));
1273 EXPECT_TRUE(removal_data.origin_matcher.Run(kOrigin2, mock_policy()));
1274 EXPECT_TRUE(removal_data.origin_matcher.Run(kOrigin3, mock_policy()));
1275 }
1276
1277 TEST_F(BrowsingDataRemoverImplTest, RemoveQuotaManagedProtectedSpecificOrigin) {
1278 content::MockSpecialStoragePolicy* policy = CreateMockPolicy();
1279 // Protect kOrigin1.
1280 policy->AddProtected(kOrigin1.GetOrigin());
1281
1282 std::unique_ptr<BrowsingDataFilterBuilder> builder(
1283 BrowsingDataFilterBuilder::Create(BrowsingDataFilterBuilder::WHITELIST));
1284 builder->AddRegisterableDomain(kTestRegisterableDomain1);
1285
1286 // Try to remove kOrigin1. Expect failure.
1287 BlockUntilOriginDataRemoved(
1288 base::Time(), base::Time::Max(),
1289 BrowsingDataRemover::DATA_TYPE_APP_CACHE |
1290 BrowsingDataRemover::DATA_TYPE_SERVICE_WORKERS |
1291 BrowsingDataRemover::DATA_TYPE_CACHE_STORAGE |
1292 BrowsingDataRemover::DATA_TYPE_FILE_SYSTEMS |
1293 BrowsingDataRemover::DATA_TYPE_INDEXED_DB |
1294 BrowsingDataRemover::DATA_TYPE_WEB_SQL,
1295 std::move(builder));
1296
1297 EXPECT_EQ(BrowsingDataRemover::DATA_TYPE_APP_CACHE |
1298 BrowsingDataRemover::DATA_TYPE_SERVICE_WORKERS |
1299 BrowsingDataRemover::DATA_TYPE_CACHE_STORAGE |
1300 BrowsingDataRemover::DATA_TYPE_FILE_SYSTEMS |
1301 BrowsingDataRemover::DATA_TYPE_INDEXED_DB |
1302 BrowsingDataRemover::DATA_TYPE_WEB_SQL,
1303 GetRemovalMask());
1304 EXPECT_EQ(BrowsingDataRemover::ORIGIN_TYPE_UNPROTECTED_WEB,
1305 GetOriginTypeMask());
1306
1307 // Verify storage partition related stuffs.
1308 StoragePartitionRemovalData removal_data = GetStoragePartitionRemovalData();
1309
1310 EXPECT_EQ(removal_data.remove_mask,
1311 StoragePartition::REMOVE_DATA_MASK_FILE_SYSTEMS |
1312 StoragePartition::REMOVE_DATA_MASK_WEBSQL |
1313 StoragePartition::REMOVE_DATA_MASK_APPCACHE |
1314 StoragePartition::REMOVE_DATA_MASK_SERVICE_WORKERS |
1315 StoragePartition::REMOVE_DATA_MASK_CACHE_STORAGE |
1316 StoragePartition::REMOVE_DATA_MASK_INDEXEDDB);
1317 EXPECT_EQ(removal_data.quota_storage_remove_mask,
1318 StoragePartition::QUOTA_MANAGED_STORAGE_MASK_ALL);
1319
1320 // Check OriginMatcherFunction.
1321 EXPECT_FALSE(removal_data.origin_matcher.Run(kOrigin1, mock_policy()));
1322 // Since we use the matcher function to validate origins now, this should
1323 // return false for the origins we're not trying to clear.
1324 EXPECT_FALSE(removal_data.origin_matcher.Run(kOrigin2, mock_policy()));
1325 EXPECT_FALSE(removal_data.origin_matcher.Run(kOrigin3, mock_policy()));
1326 }
1327
1328 TEST_F(BrowsingDataRemoverImplTest, RemoveQuotaManagedProtectedOrigins) {
1329 content::MockSpecialStoragePolicy* policy = CreateMockPolicy();
1330 // Protect kOrigin1.
1331 policy->AddProtected(kOrigin1.GetOrigin());
1332
1333 // Try to remove kOrigin1. Expect success.
1334 BlockUntilBrowsingDataRemoved(
1335 base::Time(), base::Time::Max(),
1336 BrowsingDataRemover::DATA_TYPE_APP_CACHE |
1337 BrowsingDataRemover::DATA_TYPE_SERVICE_WORKERS |
1338 BrowsingDataRemover::DATA_TYPE_CACHE_STORAGE |
1339 BrowsingDataRemover::DATA_TYPE_FILE_SYSTEMS |
1340 BrowsingDataRemover::DATA_TYPE_INDEXED_DB |
1341 BrowsingDataRemover::DATA_TYPE_WEB_SQL,
1342 true);
1343
1344 EXPECT_EQ(BrowsingDataRemover::DATA_TYPE_APP_CACHE |
1345 BrowsingDataRemover::DATA_TYPE_SERVICE_WORKERS |
1346 BrowsingDataRemover::DATA_TYPE_CACHE_STORAGE |
1347 BrowsingDataRemover::DATA_TYPE_FILE_SYSTEMS |
1348 BrowsingDataRemover::DATA_TYPE_INDEXED_DB |
1349 BrowsingDataRemover::DATA_TYPE_WEB_SQL,
1350 GetRemovalMask());
1351 EXPECT_EQ(BrowsingDataRemover::ORIGIN_TYPE_PROTECTED_WEB |
1352 BrowsingDataRemover::ORIGIN_TYPE_UNPROTECTED_WEB,
1353 GetOriginTypeMask());
1354
1355 // Verify storage partition related stuffs.
1356 StoragePartitionRemovalData removal_data = GetStoragePartitionRemovalData();
1357
1358 EXPECT_EQ(removal_data.remove_mask,
1359 StoragePartition::REMOVE_DATA_MASK_FILE_SYSTEMS |
1360 StoragePartition::REMOVE_DATA_MASK_WEBSQL |
1361 StoragePartition::REMOVE_DATA_MASK_APPCACHE |
1362 StoragePartition::REMOVE_DATA_MASK_SERVICE_WORKERS |
1363 StoragePartition::REMOVE_DATA_MASK_CACHE_STORAGE |
1364 StoragePartition::REMOVE_DATA_MASK_INDEXEDDB);
1365 EXPECT_EQ(removal_data.quota_storage_remove_mask,
1366 StoragePartition::QUOTA_MANAGED_STORAGE_MASK_ALL);
1367
1368 // Check OriginMatcherFunction, |kOrigin1| would match mask since we
1369 // would have 'protected' specified in origin_type_mask.
1370 EXPECT_TRUE(removal_data.origin_matcher.Run(kOrigin1, mock_policy()));
1371 EXPECT_TRUE(removal_data.origin_matcher.Run(kOrigin2, mock_policy()));
1372 EXPECT_TRUE(removal_data.origin_matcher.Run(kOrigin3, mock_policy()));
1373 }
1374
1375 TEST_F(BrowsingDataRemoverImplTest,
1376 RemoveQuotaManagedIgnoreExtensionsAndDevTools) {
1377 #if BUILDFLAG(ENABLE_EXTENSIONS)
1378 CreateMockPolicy();
1379 #endif
1380
1381 BlockUntilBrowsingDataRemoved(
1382 base::Time(), base::Time::Max(),
1383 BrowsingDataRemover::DATA_TYPE_APP_CACHE |
1384 BrowsingDataRemover::DATA_TYPE_SERVICE_WORKERS |
1385 BrowsingDataRemover::DATA_TYPE_CACHE_STORAGE |
1386 BrowsingDataRemover::DATA_TYPE_FILE_SYSTEMS |
1387 BrowsingDataRemover::DATA_TYPE_INDEXED_DB |
1388 BrowsingDataRemover::DATA_TYPE_WEB_SQL,
1389 false);
1390
1391 EXPECT_EQ(BrowsingDataRemover::DATA_TYPE_APP_CACHE |
1392 BrowsingDataRemover::DATA_TYPE_SERVICE_WORKERS |
1393 BrowsingDataRemover::DATA_TYPE_CACHE_STORAGE |
1394 BrowsingDataRemover::DATA_TYPE_FILE_SYSTEMS |
1395 BrowsingDataRemover::DATA_TYPE_INDEXED_DB |
1396 BrowsingDataRemover::DATA_TYPE_WEB_SQL,
1397 GetRemovalMask());
1398 EXPECT_EQ(BrowsingDataRemover::ORIGIN_TYPE_UNPROTECTED_WEB,
1399 GetOriginTypeMask());
1400
1401 // Verify storage partition related stuffs.
1402 StoragePartitionRemovalData removal_data = GetStoragePartitionRemovalData();
1403
1404 EXPECT_EQ(removal_data.remove_mask,
1405 StoragePartition::REMOVE_DATA_MASK_FILE_SYSTEMS |
1406 StoragePartition::REMOVE_DATA_MASK_WEBSQL |
1407 StoragePartition::REMOVE_DATA_MASK_APPCACHE |
1408 StoragePartition::REMOVE_DATA_MASK_SERVICE_WORKERS |
1409 StoragePartition::REMOVE_DATA_MASK_CACHE_STORAGE |
1410 StoragePartition::REMOVE_DATA_MASK_INDEXEDDB);
1411 EXPECT_EQ(removal_data.quota_storage_remove_mask,
1412 StoragePartition::QUOTA_MANAGED_STORAGE_MASK_ALL);
1413
1414 // Check that extension and devtools data wouldn't be removed, that is,
1415 // origin matcher would not match these origin.
1416 EXPECT_FALSE(removal_data.origin_matcher.Run(kOriginExt, mock_policy()));
1417 EXPECT_FALSE(removal_data.origin_matcher.Run(kOriginDevTools, mock_policy()));
1418 }
1419
1420 class InspectableCompletionObserver
1421 : public BrowsingDataRemoverCompletionObserver {
1422 public:
1423 explicit InspectableCompletionObserver(BrowsingDataRemover* remover)
1424 : BrowsingDataRemoverCompletionObserver(remover) {}
1425 ~InspectableCompletionObserver() override {}
1426
1427 bool called() { return called_; }
1428
1429 protected:
1430 void OnBrowsingDataRemoverDone() override {
1431 BrowsingDataRemoverCompletionObserver::OnBrowsingDataRemoverDone();
1432 called_ = true;
1433 }
1434
1435 private:
1436 bool called_ = false;
1437 };
1438
1439 TEST_F(BrowsingDataRemoverImplTest, CompletionInhibition) {
1440 BrowsingDataRemoverImpl* remover = static_cast<BrowsingDataRemoverImpl*>(
1441 BrowsingDataRemoverFactory::GetForBrowserContext(GetBrowserContext()));
1442
1443 // The |completion_inhibitor| on the stack should prevent removal sessions
1444 // from completing until after ContinueToCompletion() is called.
1445 BrowsingDataRemoverCompletionInhibitor completion_inhibitor(remover);
1446 InspectableCompletionObserver completion_observer(remover);
1447 remover->RemoveAndReply(
1448 base::Time(), base::Time::Max(), BrowsingDataRemover::DATA_TYPE_COOKIES,
1449 BrowsingDataRemover::ORIGIN_TYPE_UNPROTECTED_WEB, &completion_observer);
1450
1451 // Process messages until the inhibitor is notified, and then some, to make
1452 // sure we do not complete asynchronously before ContinueToCompletion() is
1453 // called.
1454 completion_inhibitor.BlockUntilNearCompletion();
1455 base::RunLoop().RunUntilIdle();
1456
1457 // Verify that the removal has not yet been completed and the observer has
1458 // not been called.
1459 EXPECT_TRUE(remover->is_removing());
1460 EXPECT_FALSE(completion_observer.called());
1461
1462 // Now run the removal process until completion, and verify that observers are
1463 // now notified, and the notifications is sent out.
1464 completion_inhibitor.ContinueToCompletion();
1465 completion_observer.BlockUntilCompletion();
1466
1467 EXPECT_FALSE(remover->is_removing());
1468 EXPECT_TRUE(completion_observer.called());
1469 }
1470
1471 TEST_F(BrowsingDataRemoverImplTest, EarlyShutdown) {
1472 BrowsingDataRemoverImpl* remover = static_cast<BrowsingDataRemoverImpl*>(
1473 BrowsingDataRemoverFactory::GetForBrowserContext(GetBrowserContext()));
1474 InspectableCompletionObserver completion_observer(remover);
1475 BrowsingDataRemoverCompletionInhibitor completion_inhibitor(remover);
1476 remover->RemoveAndReply(
1477 base::Time(), base::Time::Max(), BrowsingDataRemover::DATA_TYPE_COOKIES,
1478 BrowsingDataRemover::ORIGIN_TYPE_UNPROTECTED_WEB, &completion_observer);
1479
1480 completion_inhibitor.BlockUntilNearCompletion();
1481 completion_inhibitor.Reset();
1482
1483 // Verify that the deletion has not yet been completed and the observer has
1484 // not been called.
1485 EXPECT_TRUE(remover->is_removing());
1486 EXPECT_FALSE(completion_observer.called());
1487
1488 // Destroying the profile should trigger the notification.
1489 DestroyBrowserContext();
1490
1491 EXPECT_TRUE(completion_observer.called());
1492
1493 // Finishing after shutdown shouldn't break anything.
1494 completion_inhibitor.ContinueToCompletion();
1495 completion_observer.BlockUntilCompletion();
1496 }
1497
1498 TEST_F(BrowsingDataRemoverImplTest, RemoveDownloadsByTimeOnly) {
1499 RemoveDownloadsTester tester(GetBrowserContext());
1500 base::Callback<bool(const GURL&)> filter =
1501 BrowsingDataFilterBuilder::BuildNoopFilter();
1502
1503 EXPECT_CALL(
1504 *tester.download_manager(),
1505 RemoveDownloadsByURLAndTime(ProbablySameFilter(filter), _, _));
1506
1507 BlockUntilBrowsingDataRemoved(base::Time(), base::Time::Max(),
1508 BrowsingDataRemover::DATA_TYPE_DOWNLOADS,
1509 false);
1510 }
1511
1512 TEST_F(BrowsingDataRemoverImplTest, RemoveDownloadsByOrigin) {
1513 RemoveDownloadsTester tester(GetBrowserContext());
1514 std::unique_ptr<BrowsingDataFilterBuilder> builder(
1515 BrowsingDataFilterBuilder::Create(BrowsingDataFilterBuilder::WHITELIST));
1516 builder->AddRegisterableDomain(kTestRegisterableDomain1);
1517 base::Callback<bool(const GURL&)> filter = builder->BuildGeneralFilter();
1518
1519 EXPECT_CALL(
1520 *tester.download_manager(),
1521 RemoveDownloadsByURLAndTime(ProbablySameFilter(filter), _, _));
1522
1523 BlockUntilOriginDataRemoved(base::Time(), base::Time::Max(),
1524 BrowsingDataRemover::DATA_TYPE_DOWNLOADS,
1525 std::move(builder));
1526 }
1527
1528 class MultipleTasksObserver {
1529 public:
1530 // A simple implementation of BrowsingDataRemover::Observer.
1531 // MultipleTasksObserver will use several instances of Target to test
1532 // that completion callbacks are returned to the correct one.
1533 class Target : public BrowsingDataRemover::Observer {
1534 public:
1535 Target(MultipleTasksObserver* parent, BrowsingDataRemover* remover)
1536 : parent_(parent),
1537 observer_(this) {
1538 observer_.Add(remover);
1539 }
1540 ~Target() override {}
1541
1542 void OnBrowsingDataRemoverDone() override {
1543 parent_->SetLastCalledTarget(this);
1544 }
1545
1546 private:
1547 MultipleTasksObserver* parent_;
1548 ScopedObserver<BrowsingDataRemover, BrowsingDataRemover::Observer>
1549 observer_;
1550 };
1551
1552 explicit MultipleTasksObserver(BrowsingDataRemover* remover)
1553 : target_a_(this, remover),
1554 target_b_(this, remover),
1555 last_called_target_(nullptr) {}
1556 ~MultipleTasksObserver() {}
1557
1558 void ClearLastCalledTarget() {
1559 last_called_target_ = nullptr;
1560 }
1561
1562 Target* GetLastCalledTarget() {
1563 return last_called_target_;
1564 }
1565
1566 Target* target_a() { return &target_a_; }
1567 Target* target_b() { return &target_b_; }
1568
1569 private:
1570 void SetLastCalledTarget(Target* target) {
1571 DCHECK(!last_called_target_)
1572 << "Call ClearLastCalledTarget() before every removal task.";
1573 last_called_target_ = target;
1574 }
1575
1576 Target target_a_;
1577 Target target_b_;
1578 Target* last_called_target_;
1579 };
1580
1581 TEST_F(BrowsingDataRemoverImplTest, MultipleTasks) {
1582 BrowsingDataRemoverImpl* remover = static_cast<BrowsingDataRemoverImpl*>(
1583 BrowsingDataRemoverFactory::GetForBrowserContext(GetBrowserContext()));
1584 EXPECT_FALSE(remover->is_removing());
1585
1586 std::unique_ptr<BrowsingDataFilterBuilder> filter_builder_1(
1587 BrowsingDataFilterBuilder::Create(BrowsingDataFilterBuilder::WHITELIST));
1588 std::unique_ptr<BrowsingDataFilterBuilder> filter_builder_2(
1589 BrowsingDataFilterBuilder::Create(BrowsingDataFilterBuilder::BLACKLIST));
1590 filter_builder_2->AddRegisterableDomain("example.com");
1591
1592 MultipleTasksObserver observer(remover);
1593 BrowsingDataRemoverCompletionInhibitor completion_inhibitor(remover);
1594
1595 // Test several tasks with various configuration of masks, filters, and target
1596 // observers.
1597 std::list<BrowsingDataRemoverImpl::RemovalTask> tasks;
1598 tasks.emplace_back(
1599 base::Time(), base::Time::Max(), BrowsingDataRemover::DATA_TYPE_DOWNLOADS,
1600 BrowsingDataRemover::ORIGIN_TYPE_UNPROTECTED_WEB,
1601 BrowsingDataFilterBuilder::Create(BrowsingDataFilterBuilder::BLACKLIST),
1602 observer.target_a());
1603 tasks.emplace_back(
1604 base::Time(), base::Time::Max(), BrowsingDataRemover::DATA_TYPE_COOKIES,
1605 BrowsingDataRemover::ORIGIN_TYPE_PROTECTED_WEB,
1606 BrowsingDataFilterBuilder::Create(BrowsingDataFilterBuilder::BLACKLIST),
1607 nullptr);
1608 tasks.emplace_back(
1609 base::Time::Now(), base::Time::Max(),
1610 BrowsingDataRemover::DATA_TYPE_DOWNLOADS,
1611 BrowsingDataRemover::ORIGIN_TYPE_UNPROTECTED_WEB |
1612 BrowsingDataRemover::ORIGIN_TYPE_PROTECTED_WEB,
1613 BrowsingDataFilterBuilder::Create(BrowsingDataFilterBuilder::BLACKLIST),
1614 observer.target_b());
1615 tasks.emplace_back(base::Time(), base::Time::UnixEpoch(),
1616 BrowsingDataRemover::DATA_TYPE_WEB_SQL,
1617 BrowsingDataRemover::ORIGIN_TYPE_UNPROTECTED_WEB,
1618 std::move(filter_builder_1), observer.target_b());
1619 tasks.emplace_back(base::Time::UnixEpoch(), base::Time::Now(),
1620 BrowsingDataRemover::DATA_TYPE_CHANNEL_IDS,
1621 BrowsingDataRemover::ORIGIN_TYPE_UNPROTECTED_WEB |
1622 BrowsingDataRemover::ORIGIN_TYPE_PROTECTED_WEB,
1623 std::move(filter_builder_2), nullptr);
1624
1625 for (BrowsingDataRemoverImpl::RemovalTask& task : tasks) {
1626 // All tasks can be directly translated to a RemoveInternal() call. Since
1627 // that is a private method, we must call the four public versions of
1628 // Remove.* instead. This also serves as a test that those methods are all
1629 // correctly reduced to RemoveInternal().
1630 if (!task.observer && task.filter_builder->IsEmptyBlacklist()) {
1631 remover->Remove(task.delete_begin, task.delete_end,
1632 task.remove_mask, task.origin_type_mask);
1633 } else if (task.filter_builder->IsEmptyBlacklist()) {
1634 remover->RemoveAndReply(task.delete_begin, task.delete_end,
1635 task.remove_mask, task.origin_type_mask,
1636 task.observer);
1637 } else if (!task.observer) {
1638 remover->RemoveWithFilter(task.delete_begin, task.delete_end,
1639 task.remove_mask, task.origin_type_mask,
1640 std::move(task.filter_builder));
1641 } else {
1642 remover->RemoveWithFilterAndReply(task.delete_begin, task.delete_end,
1643 task.remove_mask, task.origin_type_mask,
1644 std::move(task.filter_builder),
1645 task.observer);
1646 }
1647 }
1648
1649 // Use the inhibitor to stop after every task and check the results.
1650 for (BrowsingDataRemoverImpl::RemovalTask& task : tasks) {
1651 EXPECT_TRUE(remover->is_removing());
1652 observer.ClearLastCalledTarget();
1653
1654 // Finish the task execution synchronously.
1655 completion_inhibitor.BlockUntilNearCompletion();
1656 completion_inhibitor.ContinueToCompletion();
1657
1658 // Observers, if any, should have been called by now (since we call
1659 // observers on the same thread).
1660 EXPECT_EQ(task.observer, observer.GetLastCalledTarget());
1661
1662 // TODO(msramek): If BrowsingDataRemover took ownership of the last used
1663 // filter builder and exposed it, we could also test it here. Make it so.
1664 EXPECT_EQ(task.remove_mask, GetRemovalMask());
1665 EXPECT_EQ(task.origin_type_mask, GetOriginTypeMask());
1666 EXPECT_EQ(task.delete_begin, GetBeginTime());
1667 }
1668
1669 EXPECT_FALSE(remover->is_removing());
1670 }
1671
1672 // The previous test, BrowsingDataRemoverTest.MultipleTasks, tests that the
1673 // tasks are not mixed up and they are executed in a correct order. However,
1674 // the completion inhibitor kept synchronizing the execution in order to verify
1675 // the parameters. This test demonstrates that even running the tasks without
1676 // inhibition is executed correctly and doesn't crash.
1677 TEST_F(BrowsingDataRemoverImplTest, MultipleTasksInQuickSuccession) {
1678 BrowsingDataRemoverImpl* remover = static_cast<BrowsingDataRemoverImpl*>(
1679 BrowsingDataRemoverFactory::GetForBrowserContext(GetBrowserContext()));
1680 EXPECT_FALSE(remover->is_removing());
1681
1682 int test_removal_masks[] = {
1683 BrowsingDataRemover::DATA_TYPE_COOKIES,
1684 BrowsingDataRemover::DATA_TYPE_LOCAL_STORAGE,
1685 BrowsingDataRemover::DATA_TYPE_COOKIES,
1686 BrowsingDataRemover::DATA_TYPE_COOKIES,
1687 BrowsingDataRemover::DATA_TYPE_COOKIES,
1688 BrowsingDataRemover::DATA_TYPE_DOWNLOADS,
1689 BrowsingDataRemover::DATA_TYPE_DOWNLOADS,
1690 BrowsingDataRemover::DATA_TYPE_DOWNLOADS,
1691 BrowsingDataRemover::DATA_TYPE_COOKIES |
1692 BrowsingDataRemover::DATA_TYPE_DOWNLOADS,
1693 BrowsingDataRemover::DATA_TYPE_COOKIES |
1694 BrowsingDataRemover::DATA_TYPE_DOWNLOADS,
1695 BrowsingDataRemover::DATA_TYPE_COOKIES |
1696 BrowsingDataRemover::DATA_TYPE_DOWNLOADS |
1697 BrowsingDataRemover::DATA_TYPE_LOCAL_STORAGE,
1698 BrowsingDataRemover::DATA_TYPE_LOCAL_STORAGE,
1699 BrowsingDataRemover::DATA_TYPE_LOCAL_STORAGE,
1700 };
1701
1702 for (int removal_mask : test_removal_masks) {
1703 remover->Remove(base::Time(), base::Time::Max(), removal_mask,
1704 BrowsingDataRemover::ORIGIN_TYPE_UNPROTECTED_WEB);
1705 }
1706
1707 EXPECT_TRUE(remover->is_removing());
1708
1709 // Add one more deletion and wait for it.
1710 BlockUntilBrowsingDataRemoved(
1711 base::Time(), base::Time::Max(), BrowsingDataRemover::DATA_TYPE_COOKIES,
1712 BrowsingDataRemover::ORIGIN_TYPE_UNPROTECTED_WEB);
1713
1714 EXPECT_FALSE(remover->is_removing());
1715 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698