Supervised user import: Listen for profile creation/deletion
[chromium-blink-merge.git] / content / browser / storage_partition_impl_unittest.cc
bloba2950c850aaf2d0a76de15621dc3a93f9081101e
1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "base/files/file_util.h"
6 #include "base/message_loop/message_loop_proxy.h"
7 #include "base/run_loop.h"
8 #include "base/threading/thread.h"
9 #include "content/browser/browser_thread_impl.h"
10 #include "content/browser/gpu/shader_disk_cache.h"
11 #include "content/browser/quota/mock_quota_manager.h"
12 #include "content/browser/storage_partition_impl.h"
13 #include "content/public/browser/local_storage_usage_info.h"
14 #include "content/public/browser/storage_partition.h"
15 #include "content/public/test/mock_special_storage_policy.h"
16 #include "content/public/test/test_browser_context.h"
17 #include "content/public/test/test_browser_thread.h"
18 #include "content/public/test/test_browser_thread_bundle.h"
19 #include "net/base/test_completion_callback.h"
20 #include "net/cookies/cookie_monster.h"
21 #include "net/url_request/url_request_context.h"
22 #include "net/url_request/url_request_context_getter.h"
23 #include "storage/browser/quota/quota_manager.h"
24 #include "testing/gtest/include/gtest/gtest.h"
26 namespace content {
27 namespace {
29 const int kDefaultClientId = 42;
30 const char kCacheKey[] = "key";
31 const char kCacheValue[] = "cached value";
33 const char kTestOrigin1[] = "http://host1:1/";
34 const char kTestOrigin2[] = "http://host2:1/";
35 const char kTestOrigin3[] = "http://host3:1/";
36 const char kTestOriginDevTools[] = "chrome-devtools://abcdefghijklmnopqrstuvw/";
38 const GURL kOrigin1(kTestOrigin1);
39 const GURL kOrigin2(kTestOrigin2);
40 const GURL kOrigin3(kTestOrigin3);
41 const GURL kOriginDevTools(kTestOriginDevTools);
43 const base::FilePath::CharType kDomStorageOrigin1[] =
44 FILE_PATH_LITERAL("http_host1_1.localstorage");
46 const base::FilePath::CharType kDomStorageOrigin2[] =
47 FILE_PATH_LITERAL("http_host2_1.localstorage");
49 const base::FilePath::CharType kDomStorageOrigin3[] =
50 FILE_PATH_LITERAL("http_host3_1.localstorage");
52 const storage::StorageType kTemporary = storage::kStorageTypeTemporary;
53 const storage::StorageType kPersistent = storage::kStorageTypePersistent;
55 const storage::QuotaClient::ID kClientFile = storage::QuotaClient::kFileSystem;
57 const uint32 kAllQuotaRemoveMask =
58 StoragePartition::REMOVE_DATA_MASK_APPCACHE |
59 StoragePartition::REMOVE_DATA_MASK_FILE_SYSTEMS |
60 StoragePartition::REMOVE_DATA_MASK_INDEXEDDB |
61 StoragePartition::REMOVE_DATA_MASK_WEBSQL;
63 class AwaitCompletionHelper {
64 public:
65 AwaitCompletionHelper() : start_(false), already_quit_(false) {}
66 virtual ~AwaitCompletionHelper() {}
68 void BlockUntilNotified() {
69 if (!already_quit_) {
70 DCHECK(!start_);
71 start_ = true;
72 base::MessageLoop::current()->Run();
73 } else {
74 DCHECK(!start_);
75 already_quit_ = false;
79 void Notify() {
80 if (start_) {
81 DCHECK(!already_quit_);
82 base::MessageLoop::current()->Quit();
83 start_ = false;
84 } else {
85 DCHECK(!already_quit_);
86 already_quit_ = true;
90 private:
91 // Helps prevent from running message_loop, if the callback invoked
92 // immediately.
93 bool start_;
94 bool already_quit_;
96 DISALLOW_COPY_AND_ASSIGN(AwaitCompletionHelper);
99 class RemoveCookieTester {
100 public:
101 explicit RemoveCookieTester(TestBrowserContext* context)
102 : get_cookie_success_(false), monster_(NULL) {
103 SetMonster(context->GetRequestContext()->GetURLRequestContext()->
104 cookie_store()->GetCookieMonster());
107 // Returns true, if the given cookie exists in the cookie store.
108 bool ContainsCookie() {
109 get_cookie_success_ = false;
110 monster_->GetCookiesWithOptionsAsync(
111 kOrigin1, net::CookieOptions(),
112 base::Bind(&RemoveCookieTester::GetCookieCallback,
113 base::Unretained(this)));
114 await_completion_.BlockUntilNotified();
115 return get_cookie_success_;
118 void AddCookie() {
119 monster_->SetCookieWithOptionsAsync(
120 kOrigin1, "A=1", net::CookieOptions(),
121 base::Bind(&RemoveCookieTester::SetCookieCallback,
122 base::Unretained(this)));
123 await_completion_.BlockUntilNotified();
126 protected:
127 void SetMonster(net::CookieStore* monster) {
128 monster_ = monster;
131 private:
132 void GetCookieCallback(const std::string& cookies) {
133 if (cookies == "A=1") {
134 get_cookie_success_ = true;
135 } else {
136 EXPECT_EQ("", cookies);
137 get_cookie_success_ = false;
139 await_completion_.Notify();
142 void SetCookieCallback(bool result) {
143 ASSERT_TRUE(result);
144 await_completion_.Notify();
147 bool get_cookie_success_;
148 AwaitCompletionHelper await_completion_;
149 net::CookieStore* monster_;
151 DISALLOW_COPY_AND_ASSIGN(RemoveCookieTester);
154 class RemoveLocalStorageTester {
155 public:
156 explicit RemoveLocalStorageTester(TestBrowserContext* profile)
157 : profile_(profile), dom_storage_context_(NULL) {
158 dom_storage_context_ =
159 content::BrowserContext::GetDefaultStoragePartition(profile)->
160 GetDOMStorageContext();
163 // Returns true, if the given origin URL exists.
164 bool DOMStorageExistsForOrigin(const GURL& origin) {
165 GetLocalStorageUsage();
166 await_completion_.BlockUntilNotified();
167 for (size_t i = 0; i < infos_.size(); ++i) {
168 if (origin == infos_[i].origin)
169 return true;
171 return false;
174 void AddDOMStorageTestData() {
175 // Note: This test depends on details of how the dom_storage library
176 // stores data in the host file system.
177 base::FilePath storage_path =
178 profile_->GetPath().AppendASCII("Local Storage");
179 base::CreateDirectory(storage_path);
181 // Write some files.
182 base::WriteFile(storage_path.Append(kDomStorageOrigin1), NULL, 0);
183 base::WriteFile(storage_path.Append(kDomStorageOrigin2), NULL, 0);
184 base::WriteFile(storage_path.Append(kDomStorageOrigin3), NULL, 0);
186 // Tweak their dates.
187 base::Time now = base::Time::Now();
188 base::TouchFile(storage_path.Append(kDomStorageOrigin1), now, now);
190 base::Time one_day_ago = now - base::TimeDelta::FromDays(1);
191 base::TouchFile(storage_path.Append(kDomStorageOrigin2),
192 one_day_ago, one_day_ago);
194 base::Time sixty_days_ago = now - base::TimeDelta::FromDays(60);
195 base::TouchFile(storage_path.Append(kDomStorageOrigin3),
196 sixty_days_ago, sixty_days_ago);
199 private:
200 void GetLocalStorageUsage() {
201 dom_storage_context_->GetLocalStorageUsage(
202 base::Bind(&RemoveLocalStorageTester::OnGotLocalStorageUsage,
203 base::Unretained(this)));
205 void OnGotLocalStorageUsage(
206 const std::vector<content::LocalStorageUsageInfo>& infos) {
207 infos_ = infos;
208 await_completion_.Notify();
211 // We don't own these pointers.
212 TestBrowserContext* profile_;
213 content::DOMStorageContext* dom_storage_context_;
215 std::vector<content::LocalStorageUsageInfo> infos_;
217 AwaitCompletionHelper await_completion_;
219 DISALLOW_COPY_AND_ASSIGN(RemoveLocalStorageTester);
222 bool IsWebSafeSchemeForTest(const std::string& scheme) {
223 return scheme == "http";
226 bool DoesOriginMatchForUnprotectedWeb(
227 const GURL& origin,
228 storage::SpecialStoragePolicy* special_storage_policy) {
229 if (IsWebSafeSchemeForTest(origin.scheme()))
230 return !special_storage_policy->IsStorageProtected(origin.GetOrigin());
232 return false;
235 bool DoesOriginMatchForBothProtectedAndUnprotectedWeb(
236 const GURL& origin,
237 storage::SpecialStoragePolicy* special_storage_policy) {
238 return true;
241 bool DoesOriginMatchUnprotected(
242 const GURL& origin,
243 storage::SpecialStoragePolicy* special_storage_policy) {
244 return origin.GetOrigin().scheme() != kOriginDevTools.scheme();
247 void ClearQuotaData(content::StoragePartition* partition,
248 base::RunLoop* loop_to_quit) {
249 partition->ClearData(kAllQuotaRemoveMask,
250 StoragePartition::QUOTA_MANAGED_STORAGE_MASK_ALL, GURL(),
251 StoragePartition::OriginMatcherFunction(), base::Time(),
252 base::Time::Max(), loop_to_quit->QuitClosure());
255 void ClearQuotaDataWithOriginMatcher(
256 content::StoragePartition* partition,
257 const GURL& remove_origin,
258 const StoragePartition::OriginMatcherFunction& origin_matcher,
259 const base::Time delete_begin,
260 base::RunLoop* loop_to_quit) {
261 partition->ClearData(kAllQuotaRemoveMask,
262 StoragePartition::QUOTA_MANAGED_STORAGE_MASK_ALL,
263 remove_origin, origin_matcher, delete_begin,
264 base::Time::Max(), loop_to_quit->QuitClosure());
267 void ClearQuotaDataForOrigin(
268 content::StoragePartition* partition,
269 const GURL& remove_origin,
270 const base::Time delete_begin,
271 base::RunLoop* loop_to_quit) {
272 ClearQuotaDataWithOriginMatcher(
273 partition, remove_origin,
274 StoragePartition::OriginMatcherFunction(), delete_begin,
275 loop_to_quit);
278 void ClearQuotaDataForNonPersistent(
279 content::StoragePartition* partition,
280 const base::Time delete_begin,
281 base::RunLoop* loop_to_quit) {
282 partition->ClearData(
283 kAllQuotaRemoveMask,
284 ~StoragePartition::QUOTA_MANAGED_STORAGE_MASK_PERSISTENT,
285 GURL(), StoragePartition::OriginMatcherFunction(), delete_begin,
286 base::Time::Max(), loop_to_quit->QuitClosure());
289 void ClearCookies(content::StoragePartition* partition,
290 const base::Time delete_begin,
291 const base::Time delete_end,
292 base::RunLoop* run_loop) {
293 partition->ClearData(
294 StoragePartition::REMOVE_DATA_MASK_COOKIES,
295 StoragePartition::QUOTA_MANAGED_STORAGE_MASK_ALL,
296 GURL(), StoragePartition::OriginMatcherFunction(),
297 delete_begin, delete_end, run_loop->QuitClosure());
300 void ClearStuff(uint32 remove_mask,
301 content::StoragePartition* partition,
302 const base::Time delete_begin,
303 const base::Time delete_end,
304 const StoragePartition::OriginMatcherFunction& origin_matcher,
305 base::RunLoop* run_loop) {
306 partition->ClearData(
307 remove_mask, StoragePartition::QUOTA_MANAGED_STORAGE_MASK_ALL,
308 GURL(), origin_matcher, delete_begin, delete_end,
309 run_loop->QuitClosure());
312 void ClearData(content::StoragePartition* partition,
313 base::RunLoop* run_loop) {
314 base::Time time;
315 partition->ClearData(
316 StoragePartition::REMOVE_DATA_MASK_SHADER_CACHE,
317 StoragePartition::QUOTA_MANAGED_STORAGE_MASK_ALL,
318 GURL(), StoragePartition::OriginMatcherFunction(),
319 time, time, run_loop->QuitClosure());
322 } // namespace
324 class StoragePartitionImplTest : public testing::Test {
325 public:
326 StoragePartitionImplTest()
327 : thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP),
328 browser_context_(new TestBrowserContext()) {
331 MockQuotaManager* GetMockManager() {
332 if (!quota_manager_.get()) {
333 quota_manager_ = new MockQuotaManager(
334 browser_context_->IsOffTheRecord(),
335 browser_context_->GetPath(),
336 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO).get(),
337 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::DB).get(),
338 browser_context_->GetSpecialStoragePolicy());
340 return quota_manager_.get();
343 TestBrowserContext* browser_context() {
344 return browser_context_.get();
347 private:
348 content::TestBrowserThreadBundle thread_bundle_;
349 scoped_ptr<TestBrowserContext> browser_context_;
350 scoped_refptr<MockQuotaManager> quota_manager_;
352 DISALLOW_COPY_AND_ASSIGN(StoragePartitionImplTest);
355 class StoragePartitionShaderClearTest : public testing::Test {
356 public:
357 StoragePartitionShaderClearTest()
358 : thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP),
359 browser_context_(new TestBrowserContext()) {
360 ShaderCacheFactory::GetInstance()->SetCacheInfo(
361 kDefaultClientId,
362 BrowserContext::GetDefaultStoragePartition(
363 browser_context())->GetPath());
364 cache_ = ShaderCacheFactory::GetInstance()->Get(kDefaultClientId);
367 ~StoragePartitionShaderClearTest() override {
368 cache_ = NULL;
369 ShaderCacheFactory::GetInstance()->RemoveCacheInfo(kDefaultClientId);
372 void InitCache() {
373 net::TestCompletionCallback available_cb;
374 int rv = cache_->SetAvailableCallback(available_cb.callback());
375 ASSERT_EQ(net::OK, available_cb.GetResult(rv));
376 EXPECT_EQ(0, cache_->Size());
378 cache_->Cache(kCacheKey, kCacheValue);
380 net::TestCompletionCallback complete_cb;
382 rv = cache_->SetCacheCompleteCallback(complete_cb.callback());
383 ASSERT_EQ(net::OK, complete_cb.GetResult(rv));
386 size_t Size() { return cache_->Size(); }
388 TestBrowserContext* browser_context() {
389 return browser_context_.get();
392 private:
393 content::TestBrowserThreadBundle thread_bundle_;
394 scoped_ptr<TestBrowserContext> browser_context_;
396 scoped_refptr<ShaderDiskCache> cache_;
399 // Tests ---------------------------------------------------------------------
401 TEST_F(StoragePartitionShaderClearTest, ClearShaderCache) {
402 InitCache();
403 EXPECT_EQ(1u, Size());
405 base::RunLoop run_loop;
406 base::MessageLoop::current()->PostTask(
407 FROM_HERE, base::Bind(
408 &ClearData,
409 BrowserContext::GetDefaultStoragePartition(browser_context()),
410 &run_loop));
411 run_loop.Run();
412 EXPECT_EQ(0u, Size());
415 TEST_F(StoragePartitionImplTest, QuotaClientMaskGeneration) {
416 EXPECT_EQ(storage::QuotaClient::kFileSystem,
417 StoragePartitionImpl::GenerateQuotaClientMask(
418 StoragePartition::REMOVE_DATA_MASK_FILE_SYSTEMS));
419 EXPECT_EQ(storage::QuotaClient::kDatabase,
420 StoragePartitionImpl::GenerateQuotaClientMask(
421 StoragePartition::REMOVE_DATA_MASK_WEBSQL));
422 EXPECT_EQ(storage::QuotaClient::kAppcache,
423 StoragePartitionImpl::GenerateQuotaClientMask(
424 StoragePartition::REMOVE_DATA_MASK_APPCACHE));
425 EXPECT_EQ(storage::QuotaClient::kIndexedDatabase,
426 StoragePartitionImpl::GenerateQuotaClientMask(
427 StoragePartition::REMOVE_DATA_MASK_INDEXEDDB));
428 EXPECT_EQ(storage::QuotaClient::kFileSystem |
429 storage::QuotaClient::kDatabase |
430 storage::QuotaClient::kAppcache |
431 storage::QuotaClient::kIndexedDatabase,
432 StoragePartitionImpl::GenerateQuotaClientMask(kAllQuotaRemoveMask));
435 void PopulateTestQuotaManagedPersistentData(MockQuotaManager* manager) {
436 manager->AddOrigin(kOrigin2, kPersistent, kClientFile, base::Time());
437 manager->AddOrigin(kOrigin3, kPersistent, kClientFile,
438 base::Time::Now() - base::TimeDelta::FromDays(1));
440 EXPECT_FALSE(manager->OriginHasData(kOrigin1, kPersistent, kClientFile));
441 EXPECT_TRUE(manager->OriginHasData(kOrigin2, kPersistent, kClientFile));
442 EXPECT_TRUE(manager->OriginHasData(kOrigin3, kPersistent, kClientFile));
445 void PopulateTestQuotaManagedTemporaryData(MockQuotaManager* manager) {
446 manager->AddOrigin(kOrigin1, kTemporary, kClientFile, base::Time::Now());
447 manager->AddOrigin(kOrigin3, kTemporary, kClientFile,
448 base::Time::Now() - base::TimeDelta::FromDays(1));
450 EXPECT_TRUE(manager->OriginHasData(kOrigin1, kTemporary, kClientFile));
451 EXPECT_FALSE(manager->OriginHasData(kOrigin2, kTemporary, kClientFile));
452 EXPECT_TRUE(manager->OriginHasData(kOrigin3, kTemporary, kClientFile));
455 void PopulateTestQuotaManagedData(MockQuotaManager* manager) {
456 // Set up kOrigin1 with a temporary quota, kOrigin2 with a persistent
457 // quota, and kOrigin3 with both. kOrigin1 is modified now, kOrigin2
458 // is modified at the beginning of time, and kOrigin3 is modified one day
459 // ago.
460 PopulateTestQuotaManagedPersistentData(manager);
461 PopulateTestQuotaManagedTemporaryData(manager);
464 void PopulateTestQuotaManagedNonBrowsingData(MockQuotaManager* manager) {
465 manager->AddOrigin(kOriginDevTools, kTemporary, kClientFile, base::Time());
466 manager->AddOrigin(kOriginDevTools, kPersistent, kClientFile, base::Time());
469 TEST_F(StoragePartitionImplTest, RemoveQuotaManagedDataForeverBoth) {
470 PopulateTestQuotaManagedData(GetMockManager());
472 StoragePartitionImpl* partition = static_cast<StoragePartitionImpl*>(
473 BrowserContext::GetDefaultStoragePartition(browser_context()));
474 partition->OverrideQuotaManagerForTesting(
475 GetMockManager());
477 base::RunLoop run_loop;
478 base::MessageLoop::current()->PostTask(
479 FROM_HERE, base::Bind(&ClearQuotaData, partition, &run_loop));
480 run_loop.Run();
482 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin1, kTemporary,
483 kClientFile));
484 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin2, kTemporary,
485 kClientFile));
486 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin3, kTemporary,
487 kClientFile));
488 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin1, kPersistent,
489 kClientFile));
490 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin2, kPersistent,
491 kClientFile));
492 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin3, kPersistent,
493 kClientFile));
496 TEST_F(StoragePartitionImplTest, RemoveQuotaManagedDataForeverOnlyTemporary) {
497 PopulateTestQuotaManagedTemporaryData(GetMockManager());
499 StoragePartitionImpl* partition = static_cast<StoragePartitionImpl*>(
500 BrowserContext::GetDefaultStoragePartition(browser_context()));
501 partition->OverrideQuotaManagerForTesting(
502 GetMockManager());
504 base::RunLoop run_loop;
505 base::MessageLoop::current()->PostTask(
506 FROM_HERE, base::Bind(&ClearQuotaData, partition, &run_loop));
507 run_loop.Run();
509 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin1, kTemporary,
510 kClientFile));
511 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin2, kTemporary,
512 kClientFile));
513 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin3, kTemporary,
514 kClientFile));
515 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin1, kPersistent,
516 kClientFile));
517 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin2, kPersistent,
518 kClientFile));
519 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin3, kPersistent,
520 kClientFile));
523 TEST_F(StoragePartitionImplTest, RemoveQuotaManagedDataForeverOnlyPersistent) {
524 PopulateTestQuotaManagedPersistentData(GetMockManager());
526 StoragePartitionImpl* partition = static_cast<StoragePartitionImpl*>(
527 BrowserContext::GetDefaultStoragePartition(browser_context()));
528 partition->OverrideQuotaManagerForTesting(
529 GetMockManager());
531 base::RunLoop run_loop;
532 base::MessageLoop::current()->PostTask(
533 FROM_HERE, base::Bind(&ClearQuotaData, partition, &run_loop));
534 run_loop.Run();
536 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin1, kTemporary,
537 kClientFile));
538 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin2, kTemporary,
539 kClientFile));
540 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin3, kTemporary,
541 kClientFile));
542 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin1, kPersistent,
543 kClientFile));
544 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin2, kPersistent,
545 kClientFile));
546 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin3, kPersistent,
547 kClientFile));
550 TEST_F(StoragePartitionImplTest, RemoveQuotaManagedDataForeverNeither) {
551 StoragePartitionImpl* partition = static_cast<StoragePartitionImpl*>(
552 BrowserContext::GetDefaultStoragePartition(browser_context()));
553 partition->OverrideQuotaManagerForTesting(
554 GetMockManager());
556 base::RunLoop run_loop;
557 base::MessageLoop::current()->PostTask(
558 FROM_HERE, base::Bind(&ClearQuotaData, partition, &run_loop));
559 run_loop.Run();
561 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin1, kTemporary,
562 kClientFile));
563 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin2, kTemporary,
564 kClientFile));
565 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin3, kTemporary,
566 kClientFile));
567 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin1, kPersistent,
568 kClientFile));
569 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin2, kPersistent,
570 kClientFile));
571 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin3, kPersistent,
572 kClientFile));
575 TEST_F(StoragePartitionImplTest, RemoveQuotaManagedDataForeverSpecificOrigin) {
576 PopulateTestQuotaManagedData(GetMockManager());
578 StoragePartitionImpl* partition = static_cast<StoragePartitionImpl*>(
579 BrowserContext::GetDefaultStoragePartition(browser_context()));
580 partition->OverrideQuotaManagerForTesting(
581 GetMockManager());
583 base::RunLoop run_loop;
584 base::MessageLoop::current()->PostTask(
585 FROM_HERE, base::Bind(&ClearQuotaDataForOrigin,
586 partition, kOrigin1, base::Time(),
587 &run_loop));
588 run_loop.Run();
590 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin1, kTemporary,
591 kClientFile));
592 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin2, kTemporary,
593 kClientFile));
594 EXPECT_TRUE(GetMockManager()->OriginHasData(kOrigin3, kTemporary,
595 kClientFile));
596 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin1, kPersistent,
597 kClientFile));
598 EXPECT_TRUE(GetMockManager()->OriginHasData(kOrigin2, kPersistent,
599 kClientFile));
600 EXPECT_TRUE(GetMockManager()->OriginHasData(kOrigin3, kPersistent,
601 kClientFile));
604 TEST_F(StoragePartitionImplTest, RemoveQuotaManagedDataForLastHour) {
605 PopulateTestQuotaManagedData(GetMockManager());
607 StoragePartitionImpl* partition = static_cast<StoragePartitionImpl*>(
608 BrowserContext::GetDefaultStoragePartition(browser_context()));
609 partition->OverrideQuotaManagerForTesting(
610 GetMockManager());
612 base::RunLoop run_loop;
613 base::MessageLoop::current()->PostTask(
614 FROM_HERE, base::Bind(&ClearQuotaDataForOrigin,
615 partition, GURL(),
616 base::Time::Now() - base::TimeDelta::FromHours(1),
617 &run_loop));
618 run_loop.Run();
620 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin1, kTemporary,
621 kClientFile));
622 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin2, kTemporary,
623 kClientFile));
624 EXPECT_TRUE(GetMockManager()->OriginHasData(kOrigin3, kTemporary,
625 kClientFile));
626 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin1, kPersistent,
627 kClientFile));
628 EXPECT_TRUE(GetMockManager()->OriginHasData(kOrigin2, kPersistent,
629 kClientFile));
630 EXPECT_TRUE(GetMockManager()->OriginHasData(kOrigin3, kPersistent,
631 kClientFile));
634 TEST_F(StoragePartitionImplTest, RemoveQuotaManagedDataForLastWeek) {
635 PopulateTestQuotaManagedData(GetMockManager());
637 base::RunLoop run_loop;
638 StoragePartitionImpl* partition = static_cast<StoragePartitionImpl*>(
639 BrowserContext::GetDefaultStoragePartition(browser_context()));
640 partition->OverrideQuotaManagerForTesting(
641 GetMockManager());
642 base::MessageLoop::current()->PostTask(
643 FROM_HERE, base::Bind(&ClearQuotaDataForNonPersistent,
644 partition,
645 base::Time::Now() - base::TimeDelta::FromDays(7),
646 &run_loop));
647 run_loop.Run();
649 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin1, kTemporary,
650 kClientFile));
651 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin2, kTemporary,
652 kClientFile));
653 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin3, kTemporary,
654 kClientFile));
655 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin1, kPersistent,
656 kClientFile));
657 EXPECT_TRUE(GetMockManager()->OriginHasData(kOrigin2, kPersistent,
658 kClientFile));
659 EXPECT_TRUE(GetMockManager()->OriginHasData(kOrigin3, kPersistent,
660 kClientFile));
663 TEST_F(StoragePartitionImplTest, RemoveQuotaManagedUnprotectedOrigins) {
664 // Protect kOrigin1.
665 scoped_refptr<MockSpecialStoragePolicy> mock_policy =
666 new MockSpecialStoragePolicy;
667 mock_policy->AddProtected(kOrigin1.GetOrigin());
669 PopulateTestQuotaManagedData(GetMockManager());
671 StoragePartitionImpl* partition = static_cast<StoragePartitionImpl*>(
672 BrowserContext::GetDefaultStoragePartition(browser_context()));
673 partition->OverrideQuotaManagerForTesting(
674 GetMockManager());
675 partition->OverrideSpecialStoragePolicyForTesting(mock_policy.get());
677 base::RunLoop run_loop;
678 base::MessageLoop::current()->PostTask(
679 FROM_HERE, base::Bind(&ClearQuotaDataWithOriginMatcher,
680 partition, GURL(),
681 base::Bind(&DoesOriginMatchForUnprotectedWeb),
682 base::Time(), &run_loop));
683 run_loop.Run();
685 EXPECT_TRUE(GetMockManager()->OriginHasData(kOrigin1, kTemporary,
686 kClientFile));
687 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin2, kTemporary,
688 kClientFile));
689 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin3, kTemporary,
690 kClientFile));
691 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin1, kPersistent,
692 kClientFile));
693 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin2, kPersistent,
694 kClientFile));
695 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin3, kPersistent,
696 kClientFile));
699 TEST_F(StoragePartitionImplTest, RemoveQuotaManagedProtectedSpecificOrigin) {
700 // Protect kOrigin1.
701 scoped_refptr<MockSpecialStoragePolicy> mock_policy =
702 new MockSpecialStoragePolicy;
703 mock_policy->AddProtected(kOrigin1.GetOrigin());
705 PopulateTestQuotaManagedData(GetMockManager());
707 StoragePartitionImpl* partition = static_cast<StoragePartitionImpl*>(
708 BrowserContext::GetDefaultStoragePartition(browser_context()));
709 partition->OverrideQuotaManagerForTesting(
710 GetMockManager());
711 partition->OverrideSpecialStoragePolicyForTesting(mock_policy.get());
713 // Try to remove kOrigin1. Expect failure.
714 base::RunLoop run_loop;
715 base::MessageLoop::current()->PostTask(
716 FROM_HERE, base::Bind(&ClearQuotaDataWithOriginMatcher,
717 partition, kOrigin1,
718 base::Bind(&DoesOriginMatchForUnprotectedWeb),
719 base::Time(), &run_loop));
720 run_loop.Run();
722 EXPECT_TRUE(GetMockManager()->OriginHasData(kOrigin1, kTemporary,
723 kClientFile));
724 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin2, kTemporary,
725 kClientFile));
726 EXPECT_TRUE(GetMockManager()->OriginHasData(kOrigin3, kTemporary,
727 kClientFile));
728 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin1, kPersistent,
729 kClientFile));
730 EXPECT_TRUE(GetMockManager()->OriginHasData(kOrigin2, kPersistent,
731 kClientFile));
732 EXPECT_TRUE(GetMockManager()->OriginHasData(kOrigin3, kPersistent,
733 kClientFile));
736 TEST_F(StoragePartitionImplTest, RemoveQuotaManagedProtectedOrigins) {
737 // Protect kOrigin1.
738 scoped_refptr<MockSpecialStoragePolicy> mock_policy =
739 new MockSpecialStoragePolicy;
740 mock_policy->AddProtected(kOrigin1.GetOrigin());
742 PopulateTestQuotaManagedData(GetMockManager());
744 // Try to remove kOrigin1. Expect success.
745 base::RunLoop run_loop;
746 StoragePartitionImpl* partition = static_cast<StoragePartitionImpl*>(
747 BrowserContext::GetDefaultStoragePartition(browser_context()));
748 partition->OverrideQuotaManagerForTesting(
749 GetMockManager());
750 partition->OverrideSpecialStoragePolicyForTesting(mock_policy.get());
751 base::MessageLoop::current()->PostTask(
752 FROM_HERE,
753 base::Bind(&ClearQuotaDataWithOriginMatcher,
754 partition, GURL(),
755 base::Bind(&DoesOriginMatchForBothProtectedAndUnprotectedWeb),
756 base::Time(), &run_loop));
757 run_loop.Run();
759 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin1, kTemporary,
760 kClientFile));
761 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin2, kTemporary,
762 kClientFile));
763 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin3, kTemporary,
764 kClientFile));
765 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin1, kPersistent,
766 kClientFile));
767 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin2, kPersistent,
768 kClientFile));
769 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin3, kPersistent,
770 kClientFile));
773 TEST_F(StoragePartitionImplTest, RemoveQuotaManagedIgnoreDevTools) {
774 PopulateTestQuotaManagedNonBrowsingData(GetMockManager());
776 base::RunLoop run_loop;
777 StoragePartitionImpl* partition = static_cast<StoragePartitionImpl*>(
778 BrowserContext::GetDefaultStoragePartition(browser_context()));
779 partition->OverrideQuotaManagerForTesting(
780 GetMockManager());
781 base::MessageLoop::current()->PostTask(
782 FROM_HERE, base::Bind(&ClearQuotaDataWithOriginMatcher,
783 partition, GURL(),
784 base::Bind(&DoesOriginMatchUnprotected),
785 base::Time(), &run_loop));
786 run_loop.Run();
788 // Check that devtools data isn't removed.
789 EXPECT_TRUE(GetMockManager()->OriginHasData(kOriginDevTools, kTemporary,
790 kClientFile));
791 EXPECT_TRUE(GetMockManager()->OriginHasData(kOriginDevTools, kPersistent,
792 kClientFile));
795 TEST_F(StoragePartitionImplTest, RemoveCookieForever) {
796 RemoveCookieTester tester(browser_context());
798 tester.AddCookie();
799 ASSERT_TRUE(tester.ContainsCookie());
801 StoragePartitionImpl* partition = static_cast<StoragePartitionImpl*>(
802 BrowserContext::GetDefaultStoragePartition(browser_context()));
803 partition->SetURLRequestContext(browser_context()->GetRequestContext());
805 base::RunLoop run_loop;
806 base::MessageLoop::current()->PostTask(
807 FROM_HERE, base::Bind(&ClearCookies,
808 partition, base::Time(), base::Time::Max(),
809 &run_loop));
810 run_loop.Run();
812 EXPECT_FALSE(tester.ContainsCookie());
815 TEST_F(StoragePartitionImplTest, RemoveCookieLastHour) {
816 RemoveCookieTester tester(browser_context());
818 tester.AddCookie();
819 ASSERT_TRUE(tester.ContainsCookie());
821 StoragePartitionImpl* partition = static_cast<StoragePartitionImpl*>(
822 BrowserContext::GetDefaultStoragePartition(browser_context()));
823 base::Time an_hour_ago = base::Time::Now() - base::TimeDelta::FromHours(1);
824 partition->SetURLRequestContext(browser_context()->GetRequestContext());
826 base::RunLoop run_loop;
827 base::MessageLoop::current()->PostTask(
828 FROM_HERE, base::Bind(&ClearCookies,
829 partition, an_hour_ago, base::Time::Max(),
830 &run_loop));
831 run_loop.Run();
833 EXPECT_FALSE(tester.ContainsCookie());
836 TEST_F(StoragePartitionImplTest, RemoveUnprotectedLocalStorageForever) {
837 // Protect kOrigin1.
838 scoped_refptr<MockSpecialStoragePolicy> mock_policy =
839 new MockSpecialStoragePolicy;
840 mock_policy->AddProtected(kOrigin1.GetOrigin());
842 RemoveLocalStorageTester tester(browser_context());
844 tester.AddDOMStorageTestData();
845 EXPECT_TRUE(tester.DOMStorageExistsForOrigin(kOrigin1));
846 EXPECT_TRUE(tester.DOMStorageExistsForOrigin(kOrigin2));
847 EXPECT_TRUE(tester.DOMStorageExistsForOrigin(kOrigin3));
849 StoragePartitionImpl* partition = static_cast<StoragePartitionImpl*>(
850 BrowserContext::GetDefaultStoragePartition(browser_context()));
851 partition->OverrideSpecialStoragePolicyForTesting(mock_policy.get());
853 base::RunLoop run_loop;
854 base::MessageLoop::current()->PostTask(
855 FROM_HERE,
856 base::Bind(&ClearStuff,
857 StoragePartitionImpl::REMOVE_DATA_MASK_LOCAL_STORAGE,
858 partition, base::Time(), base::Time::Max(),
859 base::Bind(&DoesOriginMatchForUnprotectedWeb),
860 &run_loop));
861 run_loop.Run();
863 EXPECT_TRUE(tester.DOMStorageExistsForOrigin(kOrigin1));
864 EXPECT_FALSE(tester.DOMStorageExistsForOrigin(kOrigin2));
865 EXPECT_FALSE(tester.DOMStorageExistsForOrigin(kOrigin3));
868 TEST_F(StoragePartitionImplTest, RemoveProtectedLocalStorageForever) {
869 // Protect kOrigin1.
870 scoped_refptr<MockSpecialStoragePolicy> mock_policy =
871 new MockSpecialStoragePolicy;
872 mock_policy->AddProtected(kOrigin1.GetOrigin());
874 RemoveLocalStorageTester tester(browser_context());
876 tester.AddDOMStorageTestData();
877 EXPECT_TRUE(tester.DOMStorageExistsForOrigin(kOrigin1));
878 EXPECT_TRUE(tester.DOMStorageExistsForOrigin(kOrigin2));
879 EXPECT_TRUE(tester.DOMStorageExistsForOrigin(kOrigin3));
881 StoragePartitionImpl* partition = static_cast<StoragePartitionImpl*>(
882 BrowserContext::GetDefaultStoragePartition(browser_context()));
883 partition->OverrideSpecialStoragePolicyForTesting(mock_policy.get());
885 base::RunLoop run_loop;
886 base::MessageLoop::current()->PostTask(
887 FROM_HERE,
888 base::Bind(&ClearStuff,
889 StoragePartitionImpl::REMOVE_DATA_MASK_LOCAL_STORAGE,
890 partition, base::Time(), base::Time::Max(),
891 base::Bind(&DoesOriginMatchForBothProtectedAndUnprotectedWeb),
892 &run_loop));
893 run_loop.Run();
895 // Even if kOrigin1 is protected, it will be deleted since we specify
896 // ClearData to delete protected data.
897 EXPECT_FALSE(tester.DOMStorageExistsForOrigin(kOrigin1));
898 EXPECT_FALSE(tester.DOMStorageExistsForOrigin(kOrigin2));
899 EXPECT_FALSE(tester.DOMStorageExistsForOrigin(kOrigin3));
902 TEST_F(StoragePartitionImplTest, RemoveLocalStorageForLastWeek) {
903 RemoveLocalStorageTester tester(browser_context());
905 tester.AddDOMStorageTestData();
906 EXPECT_TRUE(tester.DOMStorageExistsForOrigin(kOrigin1));
907 EXPECT_TRUE(tester.DOMStorageExistsForOrigin(kOrigin2));
908 EXPECT_TRUE(tester.DOMStorageExistsForOrigin(kOrigin3));
910 StoragePartitionImpl* partition = static_cast<StoragePartitionImpl*>(
911 BrowserContext::GetDefaultStoragePartition(browser_context()));
912 base::Time a_week_ago = base::Time::Now() - base::TimeDelta::FromDays(7);
914 base::RunLoop run_loop;
915 base::MessageLoop::current()->PostTask(
916 FROM_HERE,
917 base::Bind(&ClearStuff,
918 StoragePartitionImpl::REMOVE_DATA_MASK_LOCAL_STORAGE,
919 partition, a_week_ago, base::Time::Max(),
920 base::Bind(&DoesOriginMatchForBothProtectedAndUnprotectedWeb),
921 &run_loop));
922 run_loop.Run();
924 // kOrigin1 and kOrigin2 do not have age more than a week.
925 EXPECT_FALSE(tester.DOMStorageExistsForOrigin(kOrigin1));
926 EXPECT_FALSE(tester.DOMStorageExistsForOrigin(kOrigin2));
927 EXPECT_TRUE(tester.DOMStorageExistsForOrigin(kOrigin3));
930 } // namespace content