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/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/storage_partition_impl.h"
12 #include "content/public/browser/local_storage_usage_info.h"
13 #include "content/public/browser/storage_partition.h"
14 #include "content/public/test/test_browser_context.h"
15 #include "content/public/test/test_browser_thread.h"
16 #include "content/public/test/test_browser_thread_bundle.h"
17 #include "net/base/test_completion_callback.h"
18 #include "net/cookies/cookie_monster.h"
19 #include "net/url_request/url_request_context.h"
20 #include "net/url_request/url_request_context_getter.h"
21 #include "testing/gtest/include/gtest/gtest.h"
22 #include "webkit/browser/quota/mock_quota_manager.h"
23 #include "webkit/browser/quota/mock_special_storage_policy.h"
24 #include "webkit/browser/quota/quota_manager.h"
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 quota::StorageType kTemporary
= quota::kStorageTypeTemporary
;
53 const quota::StorageType kPersistent
= quota::kStorageTypePersistent
;
55 const quota::QuotaClient::ID kClientFile
= quota::QuotaClient::kFileSystem
;
57 const uint32 kAllQuotaRemoveMask
=
58 StoragePartition::REMOVE_DATA_MASK_INDEXEDDB
|
59 StoragePartition::REMOVE_DATA_MASK_WEBSQL
|
60 StoragePartition::REMOVE_DATA_MASK_FILE_SYSTEMS
|
61 StoragePartition::REMOVE_DATA_MASK_APPCACHE
;
63 class AwaitCompletionHelper
{
65 AwaitCompletionHelper() : start_(false), already_quit_(false) {}
66 virtual ~AwaitCompletionHelper() {}
68 void BlockUntilNotified() {
72 base::MessageLoop::current()->Run();
75 already_quit_
= false;
81 DCHECK(!already_quit_
);
82 base::MessageLoop::current()->Quit();
85 DCHECK(!already_quit_
);
91 // Helps prevent from running message_loop, if the callback invoked
96 DISALLOW_COPY_AND_ASSIGN(AwaitCompletionHelper
);
99 class RemoveCookieTester
{
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_
;
119 monster_
->SetCookieWithOptionsAsync(
120 kOrigin1
, "A=1", net::CookieOptions(),
121 base::Bind(&RemoveCookieTester::SetCookieCallback
,
122 base::Unretained(this)));
123 await_completion_
.BlockUntilNotified();
127 void SetMonster(net::CookieStore
* monster
) {
132 void GetCookieCallback(const std::string
& cookies
) {
133 if (cookies
== "A=1") {
134 get_cookie_success_
= true;
136 EXPECT_EQ("", cookies
);
137 get_cookie_success_
= false;
139 await_completion_
.Notify();
142 void SetCookieCallback(bool 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
{
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
)
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
);
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
);
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
) {
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(
228 quota::SpecialStoragePolicy
* special_storage_policy
) {
229 if (IsWebSafeSchemeForTest(origin
.scheme()))
230 return !special_storage_policy
->IsStorageProtected(origin
.GetOrigin());
235 bool DoesOriginMatchForBothProtectedAndUnprotectedWeb(
237 quota::SpecialStoragePolicy
* special_storage_policy
) {
241 bool DoesOriginMatchUnprotected(
243 quota::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(
251 StoragePartition::QUOTA_MANAGED_STORAGE_MASK_ALL
,
252 GURL(), StoragePartition::OriginMatcherFunction(),
253 base::Time(), base::Time::Max(), loop_to_quit
->QuitClosure());
256 void ClearQuotaDataWithOriginMatcher(
257 content::StoragePartition
* partition
,
258 const GURL
& remove_origin
,
259 const StoragePartition::OriginMatcherFunction
& origin_matcher
,
260 const base::Time delete_begin
,
261 base::RunLoop
* loop_to_quit
) {
262 partition
->ClearData(kAllQuotaRemoveMask
,
263 StoragePartition::QUOTA_MANAGED_STORAGE_MASK_ALL
,
264 remove_origin
, origin_matcher
, delete_begin
,
265 base::Time::Max(), loop_to_quit
->QuitClosure());
268 void ClearQuotaDataForOrigin(
269 content::StoragePartition
* partition
,
270 const GURL
& remove_origin
,
271 const base::Time delete_begin
,
272 base::RunLoop
* loop_to_quit
) {
273 ClearQuotaDataWithOriginMatcher(
274 partition
, remove_origin
,
275 StoragePartition::OriginMatcherFunction(), delete_begin
,
279 void ClearQuotaDataForNonPersistent(
280 content::StoragePartition
* partition
,
281 const base::Time delete_begin
,
282 base::RunLoop
* loop_to_quit
) {
283 uint32 quota_storage_remove_mask_no_persistent
=
284 StoragePartition::QUOTA_MANAGED_STORAGE_MASK_ALL
&
285 ~StoragePartition::QUOTA_MANAGED_STORAGE_MASK_PERSISTENT
;
286 partition
->ClearData(
287 kAllQuotaRemoveMask
, quota_storage_remove_mask_no_persistent
,
288 GURL(), StoragePartition::OriginMatcherFunction(),
289 delete_begin
, base::Time::Max(), loop_to_quit
->QuitClosure());
292 void ClearCookies(content::StoragePartition
* partition
,
293 const base::Time delete_begin
,
294 const base::Time delete_end
,
295 base::RunLoop
* run_loop
) {
296 partition
->ClearData(
297 StoragePartition::REMOVE_DATA_MASK_COOKIES
,
298 StoragePartition::QUOTA_MANAGED_STORAGE_MASK_ALL
,
299 GURL(), StoragePartition::OriginMatcherFunction(),
300 delete_begin
, delete_end
, run_loop
->QuitClosure());
303 void ClearStuff(uint32 remove_mask
,
304 content::StoragePartition
* partition
,
305 const base::Time delete_begin
,
306 const base::Time delete_end
,
307 const StoragePartition::OriginMatcherFunction
& origin_matcher
,
308 base::RunLoop
* run_loop
) {
309 partition
->ClearData(
310 remove_mask
, StoragePartition::QUOTA_MANAGED_STORAGE_MASK_ALL
,
311 GURL(), origin_matcher
, delete_begin
, delete_end
,
312 run_loop
->QuitClosure());
315 void ClearData(content::StoragePartition
* partition
,
316 base::RunLoop
* run_loop
) {
318 partition
->ClearData(
319 StoragePartition::REMOVE_DATA_MASK_SHADER_CACHE
,
320 StoragePartition::QUOTA_MANAGED_STORAGE_MASK_ALL
,
321 GURL(), StoragePartition::OriginMatcherFunction(),
322 time
, time
, run_loop
->QuitClosure());
327 class StoragePartitionImplTest
: public testing::Test
{
329 StoragePartitionImplTest()
330 : thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP
),
331 browser_context_(new TestBrowserContext()) {
334 quota::MockQuotaManager
* GetMockManager() {
335 if (!quota_manager_
.get()) {
336 quota_manager_
= new quota::MockQuotaManager(
337 browser_context_
->IsOffTheRecord(),
338 browser_context_
->GetPath(),
339 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO
).get(),
340 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::DB
).get(),
341 browser_context_
->GetSpecialStoragePolicy());
343 return quota_manager_
.get();
346 TestBrowserContext
* browser_context() {
347 return browser_context_
.get();
351 content::TestBrowserThreadBundle thread_bundle_
;
352 scoped_ptr
<TestBrowserContext
> browser_context_
;
353 scoped_refptr
<quota::MockQuotaManager
> quota_manager_
;
355 DISALLOW_COPY_AND_ASSIGN(StoragePartitionImplTest
);
358 class StoragePartitionShaderClearTest
: public testing::Test
{
360 StoragePartitionShaderClearTest()
361 : thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP
),
362 browser_context_(new TestBrowserContext()) {
363 ShaderCacheFactory::GetInstance()->SetCacheInfo(
365 BrowserContext::GetDefaultStoragePartition(
366 browser_context())->GetPath());
367 cache_
= ShaderCacheFactory::GetInstance()->Get(kDefaultClientId
);
370 virtual ~StoragePartitionShaderClearTest() {
372 ShaderCacheFactory::GetInstance()->RemoveCacheInfo(kDefaultClientId
);
376 net::TestCompletionCallback available_cb
;
377 int rv
= cache_
->SetAvailableCallback(available_cb
.callback());
378 ASSERT_EQ(net::OK
, available_cb
.GetResult(rv
));
379 EXPECT_EQ(0, cache_
->Size());
381 cache_
->Cache(kCacheKey
, kCacheValue
);
383 net::TestCompletionCallback complete_cb
;
385 rv
= cache_
->SetCacheCompleteCallback(complete_cb
.callback());
386 ASSERT_EQ(net::OK
, complete_cb
.GetResult(rv
));
389 size_t Size() { return cache_
->Size(); }
391 TestBrowserContext
* browser_context() {
392 return browser_context_
.get();
396 content::TestBrowserThreadBundle thread_bundle_
;
397 scoped_ptr
<TestBrowserContext
> browser_context_
;
399 scoped_refptr
<ShaderDiskCache
> cache_
;
402 // Tests ---------------------------------------------------------------------
404 TEST_F(StoragePartitionShaderClearTest
, ClearShaderCache
) {
406 EXPECT_EQ(1u, Size());
408 base::RunLoop run_loop
;
409 base::MessageLoop::current()->PostTask(
410 FROM_HERE
, base::Bind(
412 BrowserContext::GetDefaultStoragePartition(browser_context()),
415 EXPECT_EQ(0u, Size());
418 TEST_F(StoragePartitionImplTest
, QuotaClientMaskGeneration
) {
419 EXPECT_EQ(quota::QuotaClient::kFileSystem
,
420 StoragePartitionImpl::GenerateQuotaClientMask(
421 StoragePartition::REMOVE_DATA_MASK_FILE_SYSTEMS
));
422 EXPECT_EQ(quota::QuotaClient::kDatabase
,
423 StoragePartitionImpl::GenerateQuotaClientMask(
424 StoragePartition::REMOVE_DATA_MASK_WEBSQL
));
425 EXPECT_EQ(quota::QuotaClient::kAppcache
,
426 StoragePartitionImpl::GenerateQuotaClientMask(
427 StoragePartition::REMOVE_DATA_MASK_APPCACHE
));
428 EXPECT_EQ(quota::QuotaClient::kIndexedDatabase
,
429 StoragePartitionImpl::GenerateQuotaClientMask(
430 StoragePartition::REMOVE_DATA_MASK_INDEXEDDB
));
431 EXPECT_EQ(quota::QuotaClient::kFileSystem
|
432 quota::QuotaClient::kDatabase
|
433 quota::QuotaClient::kAppcache
|
434 quota::QuotaClient::kIndexedDatabase
,
435 StoragePartitionImpl::GenerateQuotaClientMask(
436 StoragePartition::REMOVE_DATA_MASK_FILE_SYSTEMS
|
437 StoragePartition::REMOVE_DATA_MASK_WEBSQL
|
438 StoragePartition::REMOVE_DATA_MASK_APPCACHE
|
439 StoragePartition::REMOVE_DATA_MASK_INDEXEDDB
));
442 void PopulateTestQuotaManagedPersistentData(quota::MockQuotaManager
* manager
) {
443 manager
->AddOrigin(kOrigin2
, kPersistent
, kClientFile
, base::Time());
444 manager
->AddOrigin(kOrigin3
, kPersistent
, kClientFile
,
445 base::Time::Now() - base::TimeDelta::FromDays(1));
447 EXPECT_FALSE(manager
->OriginHasData(kOrigin1
, kPersistent
, kClientFile
));
448 EXPECT_TRUE(manager
->OriginHasData(kOrigin2
, kPersistent
, kClientFile
));
449 EXPECT_TRUE(manager
->OriginHasData(kOrigin3
, kPersistent
, kClientFile
));
452 void PopulateTestQuotaManagedTemporaryData(quota::MockQuotaManager
* manager
) {
453 manager
->AddOrigin(kOrigin1
, kTemporary
, kClientFile
, base::Time::Now());
454 manager
->AddOrigin(kOrigin3
, kTemporary
, kClientFile
,
455 base::Time::Now() - base::TimeDelta::FromDays(1));
457 EXPECT_TRUE(manager
->OriginHasData(kOrigin1
, kTemporary
, kClientFile
));
458 EXPECT_FALSE(manager
->OriginHasData(kOrigin2
, kTemporary
, kClientFile
));
459 EXPECT_TRUE(manager
->OriginHasData(kOrigin3
, kTemporary
, kClientFile
));
462 void PopulateTestQuotaManagedData(quota::MockQuotaManager
* manager
) {
463 // Set up kOrigin1 with a temporary quota, kOrigin2 with a persistent
464 // quota, and kOrigin3 with both. kOrigin1 is modified now, kOrigin2
465 // is modified at the beginning of time, and kOrigin3 is modified one day
467 PopulateTestQuotaManagedPersistentData(manager
);
468 PopulateTestQuotaManagedTemporaryData(manager
);
471 void PopulateTestQuotaManagedNonBrowsingData(quota::MockQuotaManager
* manager
) {
472 manager
->AddOrigin(kOriginDevTools
, kTemporary
, kClientFile
, base::Time());
473 manager
->AddOrigin(kOriginDevTools
, kPersistent
, kClientFile
, base::Time());
476 TEST_F(StoragePartitionImplTest
, RemoveQuotaManagedDataForeverBoth
) {
477 PopulateTestQuotaManagedData(GetMockManager());
479 StoragePartitionImpl
* partition
= static_cast<StoragePartitionImpl
*>(
480 BrowserContext::GetDefaultStoragePartition(browser_context()));
481 partition
->OverrideQuotaManagerForTesting(
484 base::RunLoop run_loop
;
485 base::MessageLoop::current()->PostTask(
486 FROM_HERE
, base::Bind(&ClearQuotaData
, partition
, &run_loop
));
489 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin1
, kTemporary
,
491 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin2
, kTemporary
,
493 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin3
, kTemporary
,
495 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin1
, kPersistent
,
497 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin2
, kPersistent
,
499 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin3
, kPersistent
,
503 TEST_F(StoragePartitionImplTest
, RemoveQuotaManagedDataForeverOnlyTemporary
) {
504 PopulateTestQuotaManagedTemporaryData(GetMockManager());
506 StoragePartitionImpl
* partition
= static_cast<StoragePartitionImpl
*>(
507 BrowserContext::GetDefaultStoragePartition(browser_context()));
508 partition
->OverrideQuotaManagerForTesting(
511 base::RunLoop run_loop
;
512 base::MessageLoop::current()->PostTask(
513 FROM_HERE
, base::Bind(&ClearQuotaData
, partition
, &run_loop
));
516 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin1
, kTemporary
,
518 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin2
, kTemporary
,
520 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin3
, kTemporary
,
522 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin1
, kPersistent
,
524 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin2
, kPersistent
,
526 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin3
, kPersistent
,
530 TEST_F(StoragePartitionImplTest
, RemoveQuotaManagedDataForeverOnlyPersistent
) {
531 PopulateTestQuotaManagedPersistentData(GetMockManager());
533 StoragePartitionImpl
* partition
= static_cast<StoragePartitionImpl
*>(
534 BrowserContext::GetDefaultStoragePartition(browser_context()));
535 partition
->OverrideQuotaManagerForTesting(
538 base::RunLoop run_loop
;
539 base::MessageLoop::current()->PostTask(
540 FROM_HERE
, base::Bind(&ClearQuotaData
, partition
, &run_loop
));
543 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin1
, kTemporary
,
545 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin2
, kTemporary
,
547 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin3
, kTemporary
,
549 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin1
, kPersistent
,
551 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin2
, kPersistent
,
553 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin3
, kPersistent
,
557 TEST_F(StoragePartitionImplTest
, RemoveQuotaManagedDataForeverNeither
) {
558 StoragePartitionImpl
* partition
= static_cast<StoragePartitionImpl
*>(
559 BrowserContext::GetDefaultStoragePartition(browser_context()));
560 partition
->OverrideQuotaManagerForTesting(
563 base::RunLoop run_loop
;
564 base::MessageLoop::current()->PostTask(
565 FROM_HERE
, base::Bind(&ClearQuotaData
, partition
, &run_loop
));
568 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin1
, kTemporary
,
570 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin2
, kTemporary
,
572 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin3
, kTemporary
,
574 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin1
, kPersistent
,
576 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin2
, kPersistent
,
578 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin3
, kPersistent
,
582 TEST_F(StoragePartitionImplTest
, RemoveQuotaManagedDataForeverSpecificOrigin
) {
583 PopulateTestQuotaManagedData(GetMockManager());
585 StoragePartitionImpl
* partition
= static_cast<StoragePartitionImpl
*>(
586 BrowserContext::GetDefaultStoragePartition(browser_context()));
587 partition
->OverrideQuotaManagerForTesting(
590 base::RunLoop run_loop
;
591 base::MessageLoop::current()->PostTask(
592 FROM_HERE
, base::Bind(&ClearQuotaDataForOrigin
,
593 partition
, kOrigin1
, base::Time(),
597 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin1
, kTemporary
,
599 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin2
, kTemporary
,
601 EXPECT_TRUE(GetMockManager()->OriginHasData(kOrigin3
, kTemporary
,
603 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin1
, kPersistent
,
605 EXPECT_TRUE(GetMockManager()->OriginHasData(kOrigin2
, kPersistent
,
607 EXPECT_TRUE(GetMockManager()->OriginHasData(kOrigin3
, kPersistent
,
611 TEST_F(StoragePartitionImplTest
, RemoveQuotaManagedDataForLastHour
) {
612 PopulateTestQuotaManagedData(GetMockManager());
614 StoragePartitionImpl
* partition
= static_cast<StoragePartitionImpl
*>(
615 BrowserContext::GetDefaultStoragePartition(browser_context()));
616 partition
->OverrideQuotaManagerForTesting(
619 base::RunLoop run_loop
;
620 base::MessageLoop::current()->PostTask(
621 FROM_HERE
, base::Bind(&ClearQuotaDataForOrigin
,
623 base::Time::Now() - base::TimeDelta::FromHours(1),
627 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin1
, kTemporary
,
629 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin2
, kTemporary
,
631 EXPECT_TRUE(GetMockManager()->OriginHasData(kOrigin3
, kTemporary
,
633 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin1
, kPersistent
,
635 EXPECT_TRUE(GetMockManager()->OriginHasData(kOrigin2
, kPersistent
,
637 EXPECT_TRUE(GetMockManager()->OriginHasData(kOrigin3
, kPersistent
,
641 TEST_F(StoragePartitionImplTest
, RemoveQuotaManagedDataForLastWeek
) {
642 PopulateTestQuotaManagedData(GetMockManager());
644 base::RunLoop run_loop
;
645 StoragePartitionImpl
* partition
= static_cast<StoragePartitionImpl
*>(
646 BrowserContext::GetDefaultStoragePartition(browser_context()));
647 partition
->OverrideQuotaManagerForTesting(
649 base::MessageLoop::current()->PostTask(
650 FROM_HERE
, base::Bind(&ClearQuotaDataForNonPersistent
,
652 base::Time::Now() - base::TimeDelta::FromDays(7),
656 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin1
, kTemporary
,
658 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin2
, kTemporary
,
660 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin3
, kTemporary
,
662 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin1
, kPersistent
,
664 EXPECT_TRUE(GetMockManager()->OriginHasData(kOrigin2
, kPersistent
,
666 EXPECT_TRUE(GetMockManager()->OriginHasData(kOrigin3
, kPersistent
,
670 TEST_F(StoragePartitionImplTest
, RemoveQuotaManagedUnprotectedOrigins
) {
672 scoped_refptr
<quota::MockSpecialStoragePolicy
> mock_policy
=
673 new quota::MockSpecialStoragePolicy
;
674 mock_policy
->AddProtected(kOrigin1
.GetOrigin());
676 PopulateTestQuotaManagedData(GetMockManager());
678 StoragePartitionImpl
* partition
= static_cast<StoragePartitionImpl
*>(
679 BrowserContext::GetDefaultStoragePartition(browser_context()));
680 partition
->OverrideQuotaManagerForTesting(
682 partition
->OverrideSpecialStoragePolicyForTesting(mock_policy
);
684 base::RunLoop run_loop
;
685 base::MessageLoop::current()->PostTask(
686 FROM_HERE
, base::Bind(&ClearQuotaDataWithOriginMatcher
,
688 base::Bind(&DoesOriginMatchForUnprotectedWeb
),
689 base::Time(), &run_loop
));
692 EXPECT_TRUE(GetMockManager()->OriginHasData(kOrigin1
, kTemporary
,
694 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin2
, kTemporary
,
696 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin3
, kTemporary
,
698 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin1
, kPersistent
,
700 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin2
, kPersistent
,
702 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin3
, kPersistent
,
706 TEST_F(StoragePartitionImplTest
, RemoveQuotaManagedProtectedSpecificOrigin
) {
708 scoped_refptr
<quota::MockSpecialStoragePolicy
> mock_policy
=
709 new quota::MockSpecialStoragePolicy
;
710 mock_policy
->AddProtected(kOrigin1
.GetOrigin());
712 PopulateTestQuotaManagedData(GetMockManager());
714 StoragePartitionImpl
* partition
= static_cast<StoragePartitionImpl
*>(
715 BrowserContext::GetDefaultStoragePartition(browser_context()));
716 partition
->OverrideQuotaManagerForTesting(
718 partition
->OverrideSpecialStoragePolicyForTesting(mock_policy
);
720 // Try to remove kOrigin1. Expect failure.
721 base::RunLoop run_loop
;
722 base::MessageLoop::current()->PostTask(
723 FROM_HERE
, base::Bind(&ClearQuotaDataWithOriginMatcher
,
725 base::Bind(&DoesOriginMatchForUnprotectedWeb
),
726 base::Time(), &run_loop
));
729 EXPECT_TRUE(GetMockManager()->OriginHasData(kOrigin1
, kTemporary
,
731 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin2
, kTemporary
,
733 EXPECT_TRUE(GetMockManager()->OriginHasData(kOrigin3
, kTemporary
,
735 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin1
, kPersistent
,
737 EXPECT_TRUE(GetMockManager()->OriginHasData(kOrigin2
, kPersistent
,
739 EXPECT_TRUE(GetMockManager()->OriginHasData(kOrigin3
, kPersistent
,
743 TEST_F(StoragePartitionImplTest
, RemoveQuotaManagedProtectedOrigins
) {
745 scoped_refptr
<quota::MockSpecialStoragePolicy
> mock_policy
=
746 new quota::MockSpecialStoragePolicy
;
747 mock_policy
->AddProtected(kOrigin1
.GetOrigin());
749 PopulateTestQuotaManagedData(GetMockManager());
751 // Try to remove kOrigin1. Expect success.
752 base::RunLoop run_loop
;
753 StoragePartitionImpl
* partition
= static_cast<StoragePartitionImpl
*>(
754 BrowserContext::GetDefaultStoragePartition(browser_context()));
755 partition
->OverrideQuotaManagerForTesting(
757 partition
->OverrideSpecialStoragePolicyForTesting(mock_policy
);
758 base::MessageLoop::current()->PostTask(
760 base::Bind(&ClearQuotaDataWithOriginMatcher
,
762 base::Bind(&DoesOriginMatchForBothProtectedAndUnprotectedWeb
),
763 base::Time(), &run_loop
));
766 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin1
, kTemporary
,
768 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin2
, kTemporary
,
770 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin3
, kTemporary
,
772 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin1
, kPersistent
,
774 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin2
, kPersistent
,
776 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin3
, kPersistent
,
780 TEST_F(StoragePartitionImplTest
, RemoveQuotaManagedIgnoreDevTools
) {
781 PopulateTestQuotaManagedNonBrowsingData(GetMockManager());
783 base::RunLoop run_loop
;
784 StoragePartitionImpl
* partition
= static_cast<StoragePartitionImpl
*>(
785 BrowserContext::GetDefaultStoragePartition(browser_context()));
786 partition
->OverrideQuotaManagerForTesting(
788 base::MessageLoop::current()->PostTask(
789 FROM_HERE
, base::Bind(&ClearQuotaDataWithOriginMatcher
,
791 base::Bind(&DoesOriginMatchUnprotected
),
792 base::Time(), &run_loop
));
795 // Check that devtools data isn't removed.
796 EXPECT_TRUE(GetMockManager()->OriginHasData(kOriginDevTools
, kTemporary
,
798 EXPECT_TRUE(GetMockManager()->OriginHasData(kOriginDevTools
, kPersistent
,
802 TEST_F(StoragePartitionImplTest
, RemoveCookieForever
) {
803 RemoveCookieTester
tester(browser_context());
806 ASSERT_TRUE(tester
.ContainsCookie());
808 StoragePartitionImpl
* partition
= static_cast<StoragePartitionImpl
*>(
809 BrowserContext::GetDefaultStoragePartition(browser_context()));
810 partition
->SetURLRequestContext(browser_context()->GetRequestContext());
812 base::RunLoop run_loop
;
813 base::MessageLoop::current()->PostTask(
814 FROM_HERE
, base::Bind(&ClearCookies
,
815 partition
, base::Time(), base::Time::Max(),
819 EXPECT_FALSE(tester
.ContainsCookie());
822 TEST_F(StoragePartitionImplTest
, RemoveCookieLastHour
) {
823 RemoveCookieTester
tester(browser_context());
826 ASSERT_TRUE(tester
.ContainsCookie());
828 StoragePartitionImpl
* partition
= static_cast<StoragePartitionImpl
*>(
829 BrowserContext::GetDefaultStoragePartition(browser_context()));
830 base::Time an_hour_ago
= base::Time::Now() - base::TimeDelta::FromHours(1);
831 partition
->SetURLRequestContext(browser_context()->GetRequestContext());
833 base::RunLoop run_loop
;
834 base::MessageLoop::current()->PostTask(
835 FROM_HERE
, base::Bind(&ClearCookies
,
836 partition
, an_hour_ago
, base::Time::Max(),
840 EXPECT_FALSE(tester
.ContainsCookie());
843 TEST_F(StoragePartitionImplTest
, RemoveUnprotectedLocalStorageForever
) {
845 scoped_refptr
<quota::MockSpecialStoragePolicy
> mock_policy
=
846 new quota::MockSpecialStoragePolicy
;
847 mock_policy
->AddProtected(kOrigin1
.GetOrigin());
849 RemoveLocalStorageTester
tester(browser_context());
851 tester
.AddDOMStorageTestData();
852 EXPECT_TRUE(tester
.DOMStorageExistsForOrigin(kOrigin1
));
853 EXPECT_TRUE(tester
.DOMStorageExistsForOrigin(kOrigin2
));
854 EXPECT_TRUE(tester
.DOMStorageExistsForOrigin(kOrigin3
));
856 StoragePartitionImpl
* partition
= static_cast<StoragePartitionImpl
*>(
857 BrowserContext::GetDefaultStoragePartition(browser_context()));
858 partition
->OverrideSpecialStoragePolicyForTesting(mock_policy
);
860 base::RunLoop run_loop
;
861 base::MessageLoop::current()->PostTask(
863 base::Bind(&ClearStuff
,
864 StoragePartitionImpl::REMOVE_DATA_MASK_LOCAL_STORAGE
,
865 partition
, base::Time(), base::Time::Max(),
866 base::Bind(&DoesOriginMatchForUnprotectedWeb
),
870 EXPECT_TRUE(tester
.DOMStorageExistsForOrigin(kOrigin1
));
871 EXPECT_FALSE(tester
.DOMStorageExistsForOrigin(kOrigin2
));
872 EXPECT_FALSE(tester
.DOMStorageExistsForOrigin(kOrigin3
));
875 TEST_F(StoragePartitionImplTest
, RemoveProtectedLocalStorageForever
) {
877 scoped_refptr
<quota::MockSpecialStoragePolicy
> mock_policy
=
878 new quota::MockSpecialStoragePolicy
;
879 mock_policy
->AddProtected(kOrigin1
.GetOrigin());
881 RemoveLocalStorageTester
tester(browser_context());
883 tester
.AddDOMStorageTestData();
884 EXPECT_TRUE(tester
.DOMStorageExistsForOrigin(kOrigin1
));
885 EXPECT_TRUE(tester
.DOMStorageExistsForOrigin(kOrigin2
));
886 EXPECT_TRUE(tester
.DOMStorageExistsForOrigin(kOrigin3
));
888 StoragePartitionImpl
* partition
= static_cast<StoragePartitionImpl
*>(
889 BrowserContext::GetDefaultStoragePartition(browser_context()));
890 partition
->OverrideSpecialStoragePolicyForTesting(mock_policy
);
892 base::RunLoop run_loop
;
893 base::MessageLoop::current()->PostTask(
895 base::Bind(&ClearStuff
,
896 StoragePartitionImpl::REMOVE_DATA_MASK_LOCAL_STORAGE
,
897 partition
, base::Time(), base::Time::Max(),
898 base::Bind(&DoesOriginMatchForBothProtectedAndUnprotectedWeb
),
902 // Even if kOrigin1 is protected, it will be deleted since we specify
903 // ClearData to delete protected data.
904 EXPECT_FALSE(tester
.DOMStorageExistsForOrigin(kOrigin1
));
905 EXPECT_FALSE(tester
.DOMStorageExistsForOrigin(kOrigin2
));
906 EXPECT_FALSE(tester
.DOMStorageExistsForOrigin(kOrigin3
));
909 TEST_F(StoragePartitionImplTest
, RemoveLocalStorageForLastWeek
) {
910 RemoveLocalStorageTester
tester(browser_context());
912 tester
.AddDOMStorageTestData();
913 EXPECT_TRUE(tester
.DOMStorageExistsForOrigin(kOrigin1
));
914 EXPECT_TRUE(tester
.DOMStorageExistsForOrigin(kOrigin2
));
915 EXPECT_TRUE(tester
.DOMStorageExistsForOrigin(kOrigin3
));
917 StoragePartitionImpl
* partition
= static_cast<StoragePartitionImpl
*>(
918 BrowserContext::GetDefaultStoragePartition(browser_context()));
919 base::Time a_week_ago
= base::Time::Now() - base::TimeDelta::FromDays(7);
921 base::RunLoop run_loop
;
922 base::MessageLoop::current()->PostTask(
924 base::Bind(&ClearStuff
,
925 StoragePartitionImpl::REMOVE_DATA_MASK_LOCAL_STORAGE
,
926 partition
, a_week_ago
, base::Time::Max(),
927 base::Bind(&DoesOriginMatchForBothProtectedAndUnprotectedWeb
),
931 // kOrigin1 and kOrigin2 do not have age more than a week.
932 EXPECT_FALSE(tester
.DOMStorageExistsForOrigin(kOrigin1
));
933 EXPECT_FALSE(tester
.DOMStorageExistsForOrigin(kOrigin2
));
934 EXPECT_TRUE(tester
.DOMStorageExistsForOrigin(kOrigin3
));
937 } // namespace content