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/location.h"
7 #include "base/run_loop.h"
8 #include "base/single_thread_task_runner.h"
9 #include "base/thread_task_runner_handle.h"
10 #include "base/threading/thread.h"
11 #include "content/browser/browser_thread_impl.h"
12 #include "content/browser/gpu/shader_disk_cache.h"
13 #include "content/browser/quota/mock_quota_manager.h"
14 #include "content/browser/storage_partition_impl.h"
15 #include "content/public/browser/local_storage_usage_info.h"
16 #include "content/public/browser/storage_partition.h"
17 #include "content/public/test/mock_special_storage_policy.h"
18 #include "content/public/test/test_browser_context.h"
19 #include "content/public/test/test_browser_thread.h"
20 #include "content/public/test/test_browser_thread_bundle.h"
21 #include "net/base/test_completion_callback.h"
22 #include "net/cookies/cookie_monster.h"
23 #include "net/url_request/url_request_context.h"
24 #include "net/url_request/url_request_context_getter.h"
25 #include "storage/browser/quota/quota_manager.h"
26 #include "testing/gtest/include/gtest/gtest.h"
31 const int kDefaultClientId
= 42;
32 const char kCacheKey
[] = "key";
33 const char kCacheValue
[] = "cached value";
35 const char kTestOrigin1
[] = "http://host1:1/";
36 const char kTestOrigin2
[] = "http://host2:1/";
37 const char kTestOrigin3
[] = "http://host3:1/";
38 const char kTestOriginDevTools
[] = "chrome-devtools://abcdefghijklmnopqrstuvw/";
40 const GURL
kOrigin1(kTestOrigin1
);
41 const GURL
kOrigin2(kTestOrigin2
);
42 const GURL
kOrigin3(kTestOrigin3
);
43 const GURL
kOriginDevTools(kTestOriginDevTools
);
45 const base::FilePath::CharType kDomStorageOrigin1
[] =
46 FILE_PATH_LITERAL("http_host1_1.localstorage");
48 const base::FilePath::CharType kDomStorageOrigin2
[] =
49 FILE_PATH_LITERAL("http_host2_1.localstorage");
51 const base::FilePath::CharType kDomStorageOrigin3
[] =
52 FILE_PATH_LITERAL("http_host3_1.localstorage");
54 const storage::StorageType kTemporary
= storage::kStorageTypeTemporary
;
55 const storage::StorageType kPersistent
= storage::kStorageTypePersistent
;
57 const storage::QuotaClient::ID kClientFile
= storage::QuotaClient::kFileSystem
;
59 const uint32 kAllQuotaRemoveMask
=
60 StoragePartition::REMOVE_DATA_MASK_APPCACHE
|
61 StoragePartition::REMOVE_DATA_MASK_FILE_SYSTEMS
|
62 StoragePartition::REMOVE_DATA_MASK_INDEXEDDB
|
63 StoragePartition::REMOVE_DATA_MASK_WEBSQL
;
65 class AwaitCompletionHelper
{
67 AwaitCompletionHelper() : start_(false), already_quit_(false) {}
68 virtual ~AwaitCompletionHelper() {}
70 void BlockUntilNotified() {
74 base::MessageLoop::current()->Run();
77 already_quit_
= false;
83 DCHECK(!already_quit_
);
84 base::MessageLoop::current()->Quit();
87 DCHECK(!already_quit_
);
93 // Helps prevent from running message_loop, if the callback invoked
98 DISALLOW_COPY_AND_ASSIGN(AwaitCompletionHelper
);
101 class RemoveCookieTester
{
103 explicit RemoveCookieTester(TestBrowserContext
* context
)
104 : get_cookie_success_(false), monster_(NULL
) {
105 SetMonster(context
->GetRequestContext()->GetURLRequestContext()->
106 cookie_store()->GetCookieMonster());
109 // Returns true, if the given cookie exists in the cookie store.
110 bool ContainsCookie() {
111 get_cookie_success_
= false;
112 monster_
->GetCookiesWithOptionsAsync(
113 kOrigin1
, net::CookieOptions(),
114 base::Bind(&RemoveCookieTester::GetCookieCallback
,
115 base::Unretained(this)));
116 await_completion_
.BlockUntilNotified();
117 return get_cookie_success_
;
121 monster_
->SetCookieWithOptionsAsync(
122 kOrigin1
, "A=1", net::CookieOptions(),
123 base::Bind(&RemoveCookieTester::SetCookieCallback
,
124 base::Unretained(this)));
125 await_completion_
.BlockUntilNotified();
129 void SetMonster(net::CookieStore
* monster
) {
134 void GetCookieCallback(const std::string
& cookies
) {
135 if (cookies
== "A=1") {
136 get_cookie_success_
= true;
138 EXPECT_EQ("", cookies
);
139 get_cookie_success_
= false;
141 await_completion_
.Notify();
144 void SetCookieCallback(bool result
) {
146 await_completion_
.Notify();
149 bool get_cookie_success_
;
150 AwaitCompletionHelper await_completion_
;
151 net::CookieStore
* monster_
;
153 DISALLOW_COPY_AND_ASSIGN(RemoveCookieTester
);
156 class RemoveLocalStorageTester
{
158 explicit RemoveLocalStorageTester(TestBrowserContext
* profile
)
159 : profile_(profile
), dom_storage_context_(NULL
) {
160 dom_storage_context_
=
161 content::BrowserContext::GetDefaultStoragePartition(profile
)->
162 GetDOMStorageContext();
165 // Returns true, if the given origin URL exists.
166 bool DOMStorageExistsForOrigin(const GURL
& origin
) {
167 GetLocalStorageUsage();
168 await_completion_
.BlockUntilNotified();
169 for (size_t i
= 0; i
< infos_
.size(); ++i
) {
170 if (origin
== infos_
[i
].origin
)
176 void AddDOMStorageTestData() {
177 // Note: This test depends on details of how the dom_storage library
178 // stores data in the host file system.
179 base::FilePath storage_path
=
180 profile_
->GetPath().AppendASCII("Local Storage");
181 base::CreateDirectory(storage_path
);
184 base::WriteFile(storage_path
.Append(kDomStorageOrigin1
), NULL
, 0);
185 base::WriteFile(storage_path
.Append(kDomStorageOrigin2
), NULL
, 0);
186 base::WriteFile(storage_path
.Append(kDomStorageOrigin3
), NULL
, 0);
188 // Tweak their dates.
189 base::Time now
= base::Time::Now();
190 base::TouchFile(storage_path
.Append(kDomStorageOrigin1
), now
, now
);
192 base::Time one_day_ago
= now
- base::TimeDelta::FromDays(1);
193 base::TouchFile(storage_path
.Append(kDomStorageOrigin2
),
194 one_day_ago
, one_day_ago
);
196 base::Time sixty_days_ago
= now
- base::TimeDelta::FromDays(60);
197 base::TouchFile(storage_path
.Append(kDomStorageOrigin3
),
198 sixty_days_ago
, sixty_days_ago
);
202 void GetLocalStorageUsage() {
203 dom_storage_context_
->GetLocalStorageUsage(
204 base::Bind(&RemoveLocalStorageTester::OnGotLocalStorageUsage
,
205 base::Unretained(this)));
207 void OnGotLocalStorageUsage(
208 const std::vector
<content::LocalStorageUsageInfo
>& infos
) {
210 await_completion_
.Notify();
213 // We don't own these pointers.
214 TestBrowserContext
* profile_
;
215 content::DOMStorageContext
* dom_storage_context_
;
217 std::vector
<content::LocalStorageUsageInfo
> infos_
;
219 AwaitCompletionHelper await_completion_
;
221 DISALLOW_COPY_AND_ASSIGN(RemoveLocalStorageTester
);
224 bool IsWebSafeSchemeForTest(const std::string
& scheme
) {
225 return scheme
== "http";
228 bool DoesOriginMatchForUnprotectedWeb(
230 storage::SpecialStoragePolicy
* special_storage_policy
) {
231 if (IsWebSafeSchemeForTest(origin
.scheme()))
232 return !special_storage_policy
->IsStorageProtected(origin
.GetOrigin());
237 bool DoesOriginMatchForBothProtectedAndUnprotectedWeb(
239 storage::SpecialStoragePolicy
* special_storage_policy
) {
243 bool DoesOriginMatchUnprotected(
245 storage::SpecialStoragePolicy
* special_storage_policy
) {
246 return origin
.GetOrigin().scheme() != kOriginDevTools
.scheme();
249 void ClearQuotaData(content::StoragePartition
* partition
,
250 base::RunLoop
* loop_to_quit
) {
251 partition
->ClearData(kAllQuotaRemoveMask
,
252 StoragePartition::QUOTA_MANAGED_STORAGE_MASK_ALL
, GURL(),
253 StoragePartition::OriginMatcherFunction(), base::Time(),
254 base::Time::Max(), loop_to_quit
->QuitClosure());
257 void ClearQuotaDataWithOriginMatcher(
258 content::StoragePartition
* partition
,
259 const GURL
& remove_origin
,
260 const StoragePartition::OriginMatcherFunction
& origin_matcher
,
261 const base::Time delete_begin
,
262 base::RunLoop
* loop_to_quit
) {
263 partition
->ClearData(kAllQuotaRemoveMask
,
264 StoragePartition::QUOTA_MANAGED_STORAGE_MASK_ALL
,
265 remove_origin
, origin_matcher
, delete_begin
,
266 base::Time::Max(), loop_to_quit
->QuitClosure());
269 void ClearQuotaDataForOrigin(
270 content::StoragePartition
* partition
,
271 const GURL
& remove_origin
,
272 const base::Time delete_begin
,
273 base::RunLoop
* loop_to_quit
) {
274 ClearQuotaDataWithOriginMatcher(
275 partition
, remove_origin
,
276 StoragePartition::OriginMatcherFunction(), delete_begin
,
280 void ClearQuotaDataForNonPersistent(
281 content::StoragePartition
* partition
,
282 const base::Time delete_begin
,
283 base::RunLoop
* loop_to_quit
) {
284 partition
->ClearData(
286 ~StoragePartition::QUOTA_MANAGED_STORAGE_MASK_PERSISTENT
,
287 GURL(), StoragePartition::OriginMatcherFunction(), delete_begin
,
288 base::Time::Max(), loop_to_quit
->QuitClosure());
291 void ClearCookies(content::StoragePartition
* partition
,
292 const base::Time delete_begin
,
293 const base::Time delete_end
,
294 base::RunLoop
* run_loop
) {
295 partition
->ClearData(
296 StoragePartition::REMOVE_DATA_MASK_COOKIES
,
297 StoragePartition::QUOTA_MANAGED_STORAGE_MASK_ALL
,
298 GURL(), StoragePartition::OriginMatcherFunction(),
299 delete_begin
, delete_end
, run_loop
->QuitClosure());
302 void ClearStuff(uint32 remove_mask
,
303 content::StoragePartition
* partition
,
304 const base::Time delete_begin
,
305 const base::Time delete_end
,
306 const StoragePartition::OriginMatcherFunction
& origin_matcher
,
307 base::RunLoop
* run_loop
) {
308 partition
->ClearData(
309 remove_mask
, StoragePartition::QUOTA_MANAGED_STORAGE_MASK_ALL
,
310 GURL(), origin_matcher
, delete_begin
, delete_end
,
311 run_loop
->QuitClosure());
314 void ClearData(content::StoragePartition
* partition
,
315 base::RunLoop
* run_loop
) {
317 partition
->ClearData(
318 StoragePartition::REMOVE_DATA_MASK_SHADER_CACHE
,
319 StoragePartition::QUOTA_MANAGED_STORAGE_MASK_ALL
,
320 GURL(), StoragePartition::OriginMatcherFunction(),
321 time
, time
, run_loop
->QuitClosure());
326 class StoragePartitionImplTest
: public testing::Test
{
328 StoragePartitionImplTest()
329 : thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP
),
330 browser_context_(new TestBrowserContext()) {
333 MockQuotaManager
* GetMockManager() {
334 if (!quota_manager_
.get()) {
335 quota_manager_
= new MockQuotaManager(
336 browser_context_
->IsOffTheRecord(),
337 browser_context_
->GetPath(),
338 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO
).get(),
339 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::DB
).get(),
340 browser_context_
->GetSpecialStoragePolicy());
342 return quota_manager_
.get();
345 TestBrowserContext
* browser_context() {
346 return browser_context_
.get();
350 content::TestBrowserThreadBundle thread_bundle_
;
351 scoped_ptr
<TestBrowserContext
> browser_context_
;
352 scoped_refptr
<MockQuotaManager
> quota_manager_
;
354 DISALLOW_COPY_AND_ASSIGN(StoragePartitionImplTest
);
357 class StoragePartitionShaderClearTest
: public testing::Test
{
359 StoragePartitionShaderClearTest()
360 : thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP
),
361 browser_context_(new TestBrowserContext()) {
362 ShaderCacheFactory::GetInstance()->SetCacheInfo(
364 BrowserContext::GetDefaultStoragePartition(
365 browser_context())->GetPath());
366 cache_
= ShaderCacheFactory::GetInstance()->Get(kDefaultClientId
);
369 ~StoragePartitionShaderClearTest() override
{
371 ShaderCacheFactory::GetInstance()->RemoveCacheInfo(kDefaultClientId
);
375 net::TestCompletionCallback available_cb
;
376 int rv
= cache_
->SetAvailableCallback(available_cb
.callback());
377 ASSERT_EQ(net::OK
, available_cb
.GetResult(rv
));
378 EXPECT_EQ(0, cache_
->Size());
380 cache_
->Cache(kCacheKey
, kCacheValue
);
382 net::TestCompletionCallback complete_cb
;
384 rv
= cache_
->SetCacheCompleteCallback(complete_cb
.callback());
385 ASSERT_EQ(net::OK
, complete_cb
.GetResult(rv
));
388 size_t Size() { return cache_
->Size(); }
390 TestBrowserContext
* browser_context() {
391 return browser_context_
.get();
395 content::TestBrowserThreadBundle thread_bundle_
;
396 scoped_ptr
<TestBrowserContext
> browser_context_
;
398 scoped_refptr
<ShaderDiskCache
> cache_
;
401 // Tests ---------------------------------------------------------------------
403 TEST_F(StoragePartitionShaderClearTest
, ClearShaderCache
) {
405 EXPECT_EQ(1u, Size());
407 base::RunLoop run_loop
;
408 base::ThreadTaskRunnerHandle::Get()->PostTask(
410 base::Bind(&ClearData
,
411 BrowserContext::GetDefaultStoragePartition(browser_context()),
414 EXPECT_EQ(0u, Size());
417 TEST_F(StoragePartitionImplTest
, QuotaClientMaskGeneration
) {
418 EXPECT_EQ(storage::QuotaClient::kFileSystem
,
419 StoragePartitionImpl::GenerateQuotaClientMask(
420 StoragePartition::REMOVE_DATA_MASK_FILE_SYSTEMS
));
421 EXPECT_EQ(storage::QuotaClient::kDatabase
,
422 StoragePartitionImpl::GenerateQuotaClientMask(
423 StoragePartition::REMOVE_DATA_MASK_WEBSQL
));
424 EXPECT_EQ(storage::QuotaClient::kAppcache
,
425 StoragePartitionImpl::GenerateQuotaClientMask(
426 StoragePartition::REMOVE_DATA_MASK_APPCACHE
));
427 EXPECT_EQ(storage::QuotaClient::kIndexedDatabase
,
428 StoragePartitionImpl::GenerateQuotaClientMask(
429 StoragePartition::REMOVE_DATA_MASK_INDEXEDDB
));
430 EXPECT_EQ(storage::QuotaClient::kFileSystem
|
431 storage::QuotaClient::kDatabase
|
432 storage::QuotaClient::kAppcache
|
433 storage::QuotaClient::kIndexedDatabase
,
434 StoragePartitionImpl::GenerateQuotaClientMask(kAllQuotaRemoveMask
));
437 void PopulateTestQuotaManagedPersistentData(MockQuotaManager
* manager
) {
438 manager
->AddOrigin(kOrigin2
, kPersistent
, kClientFile
, base::Time());
439 manager
->AddOrigin(kOrigin3
, kPersistent
, kClientFile
,
440 base::Time::Now() - base::TimeDelta::FromDays(1));
442 EXPECT_FALSE(manager
->OriginHasData(kOrigin1
, kPersistent
, kClientFile
));
443 EXPECT_TRUE(manager
->OriginHasData(kOrigin2
, kPersistent
, kClientFile
));
444 EXPECT_TRUE(manager
->OriginHasData(kOrigin3
, kPersistent
, kClientFile
));
447 void PopulateTestQuotaManagedTemporaryData(MockQuotaManager
* manager
) {
448 manager
->AddOrigin(kOrigin1
, kTemporary
, kClientFile
, base::Time::Now());
449 manager
->AddOrigin(kOrigin3
, kTemporary
, kClientFile
,
450 base::Time::Now() - base::TimeDelta::FromDays(1));
452 EXPECT_TRUE(manager
->OriginHasData(kOrigin1
, kTemporary
, kClientFile
));
453 EXPECT_FALSE(manager
->OriginHasData(kOrigin2
, kTemporary
, kClientFile
));
454 EXPECT_TRUE(manager
->OriginHasData(kOrigin3
, kTemporary
, kClientFile
));
457 void PopulateTestQuotaManagedData(MockQuotaManager
* manager
) {
458 // Set up kOrigin1 with a temporary quota, kOrigin2 with a persistent
459 // quota, and kOrigin3 with both. kOrigin1 is modified now, kOrigin2
460 // is modified at the beginning of time, and kOrigin3 is modified one day
462 PopulateTestQuotaManagedPersistentData(manager
);
463 PopulateTestQuotaManagedTemporaryData(manager
);
466 void PopulateTestQuotaManagedNonBrowsingData(MockQuotaManager
* manager
) {
467 manager
->AddOrigin(kOriginDevTools
, kTemporary
, kClientFile
, base::Time());
468 manager
->AddOrigin(kOriginDevTools
, kPersistent
, kClientFile
, base::Time());
471 TEST_F(StoragePartitionImplTest
, RemoveQuotaManagedDataForeverBoth
) {
472 PopulateTestQuotaManagedData(GetMockManager());
474 StoragePartitionImpl
* partition
= static_cast<StoragePartitionImpl
*>(
475 BrowserContext::GetDefaultStoragePartition(browser_context()));
476 partition
->OverrideQuotaManagerForTesting(
479 base::RunLoop run_loop
;
480 base::ThreadTaskRunnerHandle::Get()->PostTask(
481 FROM_HERE
, base::Bind(&ClearQuotaData
, partition
, &run_loop
));
484 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin1
, kTemporary
,
486 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin2
, kTemporary
,
488 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin3
, kTemporary
,
490 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin1
, kPersistent
,
492 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin2
, kPersistent
,
494 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin3
, kPersistent
,
498 TEST_F(StoragePartitionImplTest
, RemoveQuotaManagedDataForeverOnlyTemporary
) {
499 PopulateTestQuotaManagedTemporaryData(GetMockManager());
501 StoragePartitionImpl
* partition
= static_cast<StoragePartitionImpl
*>(
502 BrowserContext::GetDefaultStoragePartition(browser_context()));
503 partition
->OverrideQuotaManagerForTesting(
506 base::RunLoop run_loop
;
507 base::ThreadTaskRunnerHandle::Get()->PostTask(
508 FROM_HERE
, base::Bind(&ClearQuotaData
, partition
, &run_loop
));
511 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin1
, kTemporary
,
513 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin2
, kTemporary
,
515 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin3
, kTemporary
,
517 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin1
, kPersistent
,
519 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin2
, kPersistent
,
521 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin3
, kPersistent
,
525 TEST_F(StoragePartitionImplTest
, RemoveQuotaManagedDataForeverOnlyPersistent
) {
526 PopulateTestQuotaManagedPersistentData(GetMockManager());
528 StoragePartitionImpl
* partition
= static_cast<StoragePartitionImpl
*>(
529 BrowserContext::GetDefaultStoragePartition(browser_context()));
530 partition
->OverrideQuotaManagerForTesting(
533 base::RunLoop run_loop
;
534 base::ThreadTaskRunnerHandle::Get()->PostTask(
535 FROM_HERE
, base::Bind(&ClearQuotaData
, partition
, &run_loop
));
538 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin1
, kTemporary
,
540 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin2
, kTemporary
,
542 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin3
, kTemporary
,
544 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin1
, kPersistent
,
546 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin2
, kPersistent
,
548 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin3
, kPersistent
,
552 TEST_F(StoragePartitionImplTest
, RemoveQuotaManagedDataForeverNeither
) {
553 StoragePartitionImpl
* partition
= static_cast<StoragePartitionImpl
*>(
554 BrowserContext::GetDefaultStoragePartition(browser_context()));
555 partition
->OverrideQuotaManagerForTesting(
558 base::RunLoop run_loop
;
559 base::ThreadTaskRunnerHandle::Get()->PostTask(
560 FROM_HERE
, base::Bind(&ClearQuotaData
, partition
, &run_loop
));
563 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin1
, kTemporary
,
565 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin2
, kTemporary
,
567 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin3
, kTemporary
,
569 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin1
, kPersistent
,
571 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin2
, kPersistent
,
573 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin3
, kPersistent
,
577 TEST_F(StoragePartitionImplTest
, RemoveQuotaManagedDataForeverSpecificOrigin
) {
578 PopulateTestQuotaManagedData(GetMockManager());
580 StoragePartitionImpl
* partition
= static_cast<StoragePartitionImpl
*>(
581 BrowserContext::GetDefaultStoragePartition(browser_context()));
582 partition
->OverrideQuotaManagerForTesting(
585 base::RunLoop run_loop
;
586 base::ThreadTaskRunnerHandle::Get()->PostTask(
587 FROM_HERE
, base::Bind(&ClearQuotaDataForOrigin
, partition
, kOrigin1
,
588 base::Time(), &run_loop
));
591 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin1
, kTemporary
,
593 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin2
, kTemporary
,
595 EXPECT_TRUE(GetMockManager()->OriginHasData(kOrigin3
, kTemporary
,
597 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin1
, kPersistent
,
599 EXPECT_TRUE(GetMockManager()->OriginHasData(kOrigin2
, kPersistent
,
601 EXPECT_TRUE(GetMockManager()->OriginHasData(kOrigin3
, kPersistent
,
605 TEST_F(StoragePartitionImplTest
, RemoveQuotaManagedDataForLastHour
) {
606 PopulateTestQuotaManagedData(GetMockManager());
608 StoragePartitionImpl
* partition
= static_cast<StoragePartitionImpl
*>(
609 BrowserContext::GetDefaultStoragePartition(browser_context()));
610 partition
->OverrideQuotaManagerForTesting(
613 base::RunLoop run_loop
;
614 base::ThreadTaskRunnerHandle::Get()->PostTask(
616 base::Bind(&ClearQuotaDataForOrigin
, partition
, GURL(),
617 base::Time::Now() - base::TimeDelta::FromHours(1), &run_loop
));
620 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin1
, kTemporary
,
622 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin2
, kTemporary
,
624 EXPECT_TRUE(GetMockManager()->OriginHasData(kOrigin3
, kTemporary
,
626 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin1
, kPersistent
,
628 EXPECT_TRUE(GetMockManager()->OriginHasData(kOrigin2
, kPersistent
,
630 EXPECT_TRUE(GetMockManager()->OriginHasData(kOrigin3
, kPersistent
,
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(
642 base::ThreadTaskRunnerHandle::Get()->PostTask(
644 base::Bind(&ClearQuotaDataForNonPersistent
, partition
,
645 base::Time::Now() - base::TimeDelta::FromDays(7), &run_loop
));
648 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin1
, kTemporary
,
650 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin2
, kTemporary
,
652 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin3
, kTemporary
,
654 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin1
, kPersistent
,
656 EXPECT_TRUE(GetMockManager()->OriginHasData(kOrigin2
, kPersistent
,
658 EXPECT_TRUE(GetMockManager()->OriginHasData(kOrigin3
, kPersistent
,
662 TEST_F(StoragePartitionImplTest
, RemoveQuotaManagedUnprotectedOrigins
) {
664 scoped_refptr
<MockSpecialStoragePolicy
> mock_policy
=
665 new MockSpecialStoragePolicy
;
666 mock_policy
->AddProtected(kOrigin1
.GetOrigin());
668 PopulateTestQuotaManagedData(GetMockManager());
670 StoragePartitionImpl
* partition
= static_cast<StoragePartitionImpl
*>(
671 BrowserContext::GetDefaultStoragePartition(browser_context()));
672 partition
->OverrideQuotaManagerForTesting(
674 partition
->OverrideSpecialStoragePolicyForTesting(mock_policy
.get());
676 base::RunLoop run_loop
;
677 base::ThreadTaskRunnerHandle::Get()->PostTask(
678 FROM_HERE
, base::Bind(&ClearQuotaDataWithOriginMatcher
, partition
, GURL(),
679 base::Bind(&DoesOriginMatchForUnprotectedWeb
),
680 base::Time(), &run_loop
));
683 EXPECT_TRUE(GetMockManager()->OriginHasData(kOrigin1
, kTemporary
,
685 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin2
, kTemporary
,
687 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin3
, kTemporary
,
689 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin1
, kPersistent
,
691 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin2
, kPersistent
,
693 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin3
, kPersistent
,
697 TEST_F(StoragePartitionImplTest
, RemoveQuotaManagedProtectedSpecificOrigin
) {
699 scoped_refptr
<MockSpecialStoragePolicy
> mock_policy
=
700 new MockSpecialStoragePolicy
;
701 mock_policy
->AddProtected(kOrigin1
.GetOrigin());
703 PopulateTestQuotaManagedData(GetMockManager());
705 StoragePartitionImpl
* partition
= static_cast<StoragePartitionImpl
*>(
706 BrowserContext::GetDefaultStoragePartition(browser_context()));
707 partition
->OverrideQuotaManagerForTesting(
709 partition
->OverrideSpecialStoragePolicyForTesting(mock_policy
.get());
711 // Try to remove kOrigin1. Expect failure.
712 base::RunLoop run_loop
;
713 base::ThreadTaskRunnerHandle::Get()->PostTask(
715 base::Bind(&ClearQuotaDataWithOriginMatcher
, partition
, kOrigin1
,
716 base::Bind(&DoesOriginMatchForUnprotectedWeb
), base::Time(),
720 EXPECT_TRUE(GetMockManager()->OriginHasData(kOrigin1
, kTemporary
,
722 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin2
, kTemporary
,
724 EXPECT_TRUE(GetMockManager()->OriginHasData(kOrigin3
, kTemporary
,
726 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin1
, kPersistent
,
728 EXPECT_TRUE(GetMockManager()->OriginHasData(kOrigin2
, kPersistent
,
730 EXPECT_TRUE(GetMockManager()->OriginHasData(kOrigin3
, kPersistent
,
734 TEST_F(StoragePartitionImplTest
, RemoveQuotaManagedProtectedOrigins
) {
736 scoped_refptr
<MockSpecialStoragePolicy
> mock_policy
=
737 new MockSpecialStoragePolicy
;
738 mock_policy
->AddProtected(kOrigin1
.GetOrigin());
740 PopulateTestQuotaManagedData(GetMockManager());
742 // Try to remove kOrigin1. Expect success.
743 base::RunLoop run_loop
;
744 StoragePartitionImpl
* partition
= static_cast<StoragePartitionImpl
*>(
745 BrowserContext::GetDefaultStoragePartition(browser_context()));
746 partition
->OverrideQuotaManagerForTesting(
748 partition
->OverrideSpecialStoragePolicyForTesting(mock_policy
.get());
749 base::ThreadTaskRunnerHandle::Get()->PostTask(
751 base::Bind(&ClearQuotaDataWithOriginMatcher
, partition
, GURL(),
752 base::Bind(&DoesOriginMatchForBothProtectedAndUnprotectedWeb
),
753 base::Time(), &run_loop
));
756 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin1
, kTemporary
,
758 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin2
, kTemporary
,
760 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin3
, kTemporary
,
762 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin1
, kPersistent
,
764 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin2
, kPersistent
,
766 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin3
, kPersistent
,
770 TEST_F(StoragePartitionImplTest
, RemoveQuotaManagedIgnoreDevTools
) {
771 PopulateTestQuotaManagedNonBrowsingData(GetMockManager());
773 base::RunLoop run_loop
;
774 StoragePartitionImpl
* partition
= static_cast<StoragePartitionImpl
*>(
775 BrowserContext::GetDefaultStoragePartition(browser_context()));
776 partition
->OverrideQuotaManagerForTesting(
778 base::ThreadTaskRunnerHandle::Get()->PostTask(
779 FROM_HERE
, base::Bind(&ClearQuotaDataWithOriginMatcher
, partition
, GURL(),
780 base::Bind(&DoesOriginMatchUnprotected
),
781 base::Time(), &run_loop
));
784 // Check that devtools data isn't removed.
785 EXPECT_TRUE(GetMockManager()->OriginHasData(kOriginDevTools
, kTemporary
,
787 EXPECT_TRUE(GetMockManager()->OriginHasData(kOriginDevTools
, kPersistent
,
791 TEST_F(StoragePartitionImplTest
, RemoveCookieForever
) {
792 RemoveCookieTester
tester(browser_context());
795 ASSERT_TRUE(tester
.ContainsCookie());
797 StoragePartitionImpl
* partition
= static_cast<StoragePartitionImpl
*>(
798 BrowserContext::GetDefaultStoragePartition(browser_context()));
799 partition
->SetURLRequestContext(browser_context()->GetRequestContext());
801 base::RunLoop run_loop
;
802 base::ThreadTaskRunnerHandle::Get()->PostTask(
803 FROM_HERE
, base::Bind(&ClearCookies
, partition
, base::Time(),
804 base::Time::Max(), &run_loop
));
807 EXPECT_FALSE(tester
.ContainsCookie());
810 TEST_F(StoragePartitionImplTest
, RemoveCookieLastHour
) {
811 RemoveCookieTester
tester(browser_context());
814 ASSERT_TRUE(tester
.ContainsCookie());
816 StoragePartitionImpl
* partition
= static_cast<StoragePartitionImpl
*>(
817 BrowserContext::GetDefaultStoragePartition(browser_context()));
818 base::Time an_hour_ago
= base::Time::Now() - base::TimeDelta::FromHours(1);
819 partition
->SetURLRequestContext(browser_context()->GetRequestContext());
821 base::RunLoop run_loop
;
822 base::ThreadTaskRunnerHandle::Get()->PostTask(
823 FROM_HERE
, base::Bind(&ClearCookies
, partition
, an_hour_ago
,
824 base::Time::Max(), &run_loop
));
827 EXPECT_FALSE(tester
.ContainsCookie());
830 TEST_F(StoragePartitionImplTest
, RemoveUnprotectedLocalStorageForever
) {
832 scoped_refptr
<MockSpecialStoragePolicy
> mock_policy
=
833 new MockSpecialStoragePolicy
;
834 mock_policy
->AddProtected(kOrigin1
.GetOrigin());
836 RemoveLocalStorageTester
tester(browser_context());
838 tester
.AddDOMStorageTestData();
839 EXPECT_TRUE(tester
.DOMStorageExistsForOrigin(kOrigin1
));
840 EXPECT_TRUE(tester
.DOMStorageExistsForOrigin(kOrigin2
));
841 EXPECT_TRUE(tester
.DOMStorageExistsForOrigin(kOrigin3
));
843 StoragePartitionImpl
* partition
= static_cast<StoragePartitionImpl
*>(
844 BrowserContext::GetDefaultStoragePartition(browser_context()));
845 partition
->OverrideSpecialStoragePolicyForTesting(mock_policy
.get());
847 base::RunLoop run_loop
;
848 base::ThreadTaskRunnerHandle::Get()->PostTask(
850 base::Bind(&ClearStuff
,
851 StoragePartitionImpl::REMOVE_DATA_MASK_LOCAL_STORAGE
,
852 partition
, base::Time(), base::Time::Max(),
853 base::Bind(&DoesOriginMatchForUnprotectedWeb
), &run_loop
));
856 EXPECT_TRUE(tester
.DOMStorageExistsForOrigin(kOrigin1
));
857 EXPECT_FALSE(tester
.DOMStorageExistsForOrigin(kOrigin2
));
858 EXPECT_FALSE(tester
.DOMStorageExistsForOrigin(kOrigin3
));
861 TEST_F(StoragePartitionImplTest
, RemoveProtectedLocalStorageForever
) {
863 scoped_refptr
<MockSpecialStoragePolicy
> mock_policy
=
864 new MockSpecialStoragePolicy
;
865 mock_policy
->AddProtected(kOrigin1
.GetOrigin());
867 RemoveLocalStorageTester
tester(browser_context());
869 tester
.AddDOMStorageTestData();
870 EXPECT_TRUE(tester
.DOMStorageExistsForOrigin(kOrigin1
));
871 EXPECT_TRUE(tester
.DOMStorageExistsForOrigin(kOrigin2
));
872 EXPECT_TRUE(tester
.DOMStorageExistsForOrigin(kOrigin3
));
874 StoragePartitionImpl
* partition
= static_cast<StoragePartitionImpl
*>(
875 BrowserContext::GetDefaultStoragePartition(browser_context()));
876 partition
->OverrideSpecialStoragePolicyForTesting(mock_policy
.get());
878 base::RunLoop run_loop
;
879 base::ThreadTaskRunnerHandle::Get()->PostTask(
881 base::Bind(&ClearStuff
,
882 StoragePartitionImpl::REMOVE_DATA_MASK_LOCAL_STORAGE
,
883 partition
, base::Time(), base::Time::Max(),
884 base::Bind(&DoesOriginMatchForBothProtectedAndUnprotectedWeb
),
888 // Even if kOrigin1 is protected, it will be deleted since we specify
889 // ClearData to delete protected data.
890 EXPECT_FALSE(tester
.DOMStorageExistsForOrigin(kOrigin1
));
891 EXPECT_FALSE(tester
.DOMStorageExistsForOrigin(kOrigin2
));
892 EXPECT_FALSE(tester
.DOMStorageExistsForOrigin(kOrigin3
));
895 TEST_F(StoragePartitionImplTest
, RemoveLocalStorageForLastWeek
) {
896 RemoveLocalStorageTester
tester(browser_context());
898 tester
.AddDOMStorageTestData();
899 EXPECT_TRUE(tester
.DOMStorageExistsForOrigin(kOrigin1
));
900 EXPECT_TRUE(tester
.DOMStorageExistsForOrigin(kOrigin2
));
901 EXPECT_TRUE(tester
.DOMStorageExistsForOrigin(kOrigin3
));
903 StoragePartitionImpl
* partition
= static_cast<StoragePartitionImpl
*>(
904 BrowserContext::GetDefaultStoragePartition(browser_context()));
905 base::Time a_week_ago
= base::Time::Now() - base::TimeDelta::FromDays(7);
907 base::RunLoop run_loop
;
908 base::ThreadTaskRunnerHandle::Get()->PostTask(
910 base::Bind(&ClearStuff
,
911 StoragePartitionImpl::REMOVE_DATA_MASK_LOCAL_STORAGE
,
912 partition
, a_week_ago
, base::Time::Max(),
913 base::Bind(&DoesOriginMatchForBothProtectedAndUnprotectedWeb
),
917 // kOrigin1 and kOrigin2 do not have age more than a week.
918 EXPECT_FALSE(tester
.DOMStorageExistsForOrigin(kOrigin1
));
919 EXPECT_FALSE(tester
.DOMStorageExistsForOrigin(kOrigin2
));
920 EXPECT_TRUE(tester
.DOMStorageExistsForOrigin(kOrigin3
));
923 } // namespace content