Make USB permissions work in the new permission message system
[chromium-blink-merge.git] / content / browser / storage_partition_impl_unittest.cc
blob79e33beb2500a0331329c421d40df6c0edd86f7a
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"
28 namespace content {
29 namespace {
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 {
66 public:
67 AwaitCompletionHelper() : start_(false), already_quit_(false) {}
68 virtual ~AwaitCompletionHelper() {}
70 void BlockUntilNotified() {
71 if (!already_quit_) {
72 DCHECK(!start_);
73 start_ = true;
74 base::MessageLoop::current()->Run();
75 } else {
76 DCHECK(!start_);
77 already_quit_ = false;
81 void Notify() {
82 if (start_) {
83 DCHECK(!already_quit_);
84 base::MessageLoop::current()->Quit();
85 start_ = false;
86 } else {
87 DCHECK(!already_quit_);
88 already_quit_ = true;
92 private:
93 // Helps prevent from running message_loop, if the callback invoked
94 // immediately.
95 bool start_;
96 bool already_quit_;
98 DISALLOW_COPY_AND_ASSIGN(AwaitCompletionHelper);
101 class RemoveCookieTester {
102 public:
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_;
120 void AddCookie() {
121 monster_->SetCookieWithOptionsAsync(
122 kOrigin1, "A=1", net::CookieOptions(),
123 base::Bind(&RemoveCookieTester::SetCookieCallback,
124 base::Unretained(this)));
125 await_completion_.BlockUntilNotified();
128 protected:
129 void SetMonster(net::CookieStore* monster) {
130 monster_ = monster;
133 private:
134 void GetCookieCallback(const std::string& cookies) {
135 if (cookies == "A=1") {
136 get_cookie_success_ = true;
137 } else {
138 EXPECT_EQ("", cookies);
139 get_cookie_success_ = false;
141 await_completion_.Notify();
144 void SetCookieCallback(bool result) {
145 ASSERT_TRUE(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 {
157 public:
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)
171 return true;
173 return false;
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);
183 // Write some files.
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);
201 private:
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) {
209 infos_ = 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(
229 const GURL& origin,
230 storage::SpecialStoragePolicy* special_storage_policy) {
231 if (IsWebSafeSchemeForTest(origin.scheme()))
232 return !special_storage_policy->IsStorageProtected(origin.GetOrigin());
234 return false;
237 bool DoesOriginMatchForBothProtectedAndUnprotectedWeb(
238 const GURL& origin,
239 storage::SpecialStoragePolicy* special_storage_policy) {
240 return true;
243 bool DoesOriginMatchUnprotected(
244 const GURL& origin,
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,
277 loop_to_quit);
280 void ClearQuotaDataForNonPersistent(
281 content::StoragePartition* partition,
282 const base::Time delete_begin,
283 base::RunLoop* loop_to_quit) {
284 partition->ClearData(
285 kAllQuotaRemoveMask,
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) {
316 base::Time time;
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());
324 } // namespace
326 class StoragePartitionImplTest : public testing::Test {
327 public:
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();
349 private:
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 {
358 public:
359 StoragePartitionShaderClearTest()
360 : thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP),
361 browser_context_(new TestBrowserContext()) {
362 ShaderCacheFactory::GetInstance()->SetCacheInfo(
363 kDefaultClientId,
364 BrowserContext::GetDefaultStoragePartition(
365 browser_context())->GetPath());
366 cache_ = ShaderCacheFactory::GetInstance()->Get(kDefaultClientId);
369 ~StoragePartitionShaderClearTest() override {
370 cache_ = NULL;
371 ShaderCacheFactory::GetInstance()->RemoveCacheInfo(kDefaultClientId);
374 void InitCache() {
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();
394 private:
395 content::TestBrowserThreadBundle thread_bundle_;
396 scoped_ptr<TestBrowserContext> browser_context_;
398 scoped_refptr<ShaderDiskCache> cache_;
401 // Tests ---------------------------------------------------------------------
403 TEST_F(StoragePartitionShaderClearTest, ClearShaderCache) {
404 InitCache();
405 EXPECT_EQ(1u, Size());
407 base::RunLoop run_loop;
408 base::ThreadTaskRunnerHandle::Get()->PostTask(
409 FROM_HERE,
410 base::Bind(&ClearData,
411 BrowserContext::GetDefaultStoragePartition(browser_context()),
412 &run_loop));
413 run_loop.Run();
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
461 // ago.
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(
477 GetMockManager());
479 base::RunLoop run_loop;
480 base::ThreadTaskRunnerHandle::Get()->PostTask(
481 FROM_HERE, base::Bind(&ClearQuotaData, partition, &run_loop));
482 run_loop.Run();
484 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin1, kTemporary,
485 kClientFile));
486 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin2, kTemporary,
487 kClientFile));
488 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin3, kTemporary,
489 kClientFile));
490 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin1, kPersistent,
491 kClientFile));
492 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin2, kPersistent,
493 kClientFile));
494 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin3, kPersistent,
495 kClientFile));
498 TEST_F(StoragePartitionImplTest, RemoveQuotaManagedDataForeverOnlyTemporary) {
499 PopulateTestQuotaManagedTemporaryData(GetMockManager());
501 StoragePartitionImpl* partition = static_cast<StoragePartitionImpl*>(
502 BrowserContext::GetDefaultStoragePartition(browser_context()));
503 partition->OverrideQuotaManagerForTesting(
504 GetMockManager());
506 base::RunLoop run_loop;
507 base::ThreadTaskRunnerHandle::Get()->PostTask(
508 FROM_HERE, base::Bind(&ClearQuotaData, partition, &run_loop));
509 run_loop.Run();
511 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin1, kTemporary,
512 kClientFile));
513 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin2, kTemporary,
514 kClientFile));
515 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin3, kTemporary,
516 kClientFile));
517 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin1, kPersistent,
518 kClientFile));
519 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin2, kPersistent,
520 kClientFile));
521 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin3, kPersistent,
522 kClientFile));
525 TEST_F(StoragePartitionImplTest, RemoveQuotaManagedDataForeverOnlyPersistent) {
526 PopulateTestQuotaManagedPersistentData(GetMockManager());
528 StoragePartitionImpl* partition = static_cast<StoragePartitionImpl*>(
529 BrowserContext::GetDefaultStoragePartition(browser_context()));
530 partition->OverrideQuotaManagerForTesting(
531 GetMockManager());
533 base::RunLoop run_loop;
534 base::ThreadTaskRunnerHandle::Get()->PostTask(
535 FROM_HERE, base::Bind(&ClearQuotaData, partition, &run_loop));
536 run_loop.Run();
538 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin1, kTemporary,
539 kClientFile));
540 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin2, kTemporary,
541 kClientFile));
542 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin3, kTemporary,
543 kClientFile));
544 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin1, kPersistent,
545 kClientFile));
546 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin2, kPersistent,
547 kClientFile));
548 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin3, kPersistent,
549 kClientFile));
552 TEST_F(StoragePartitionImplTest, RemoveQuotaManagedDataForeverNeither) {
553 StoragePartitionImpl* partition = static_cast<StoragePartitionImpl*>(
554 BrowserContext::GetDefaultStoragePartition(browser_context()));
555 partition->OverrideQuotaManagerForTesting(
556 GetMockManager());
558 base::RunLoop run_loop;
559 base::ThreadTaskRunnerHandle::Get()->PostTask(
560 FROM_HERE, base::Bind(&ClearQuotaData, partition, &run_loop));
561 run_loop.Run();
563 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin1, kTemporary,
564 kClientFile));
565 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin2, kTemporary,
566 kClientFile));
567 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin3, kTemporary,
568 kClientFile));
569 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin1, kPersistent,
570 kClientFile));
571 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin2, kPersistent,
572 kClientFile));
573 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin3, kPersistent,
574 kClientFile));
577 TEST_F(StoragePartitionImplTest, RemoveQuotaManagedDataForeverSpecificOrigin) {
578 PopulateTestQuotaManagedData(GetMockManager());
580 StoragePartitionImpl* partition = static_cast<StoragePartitionImpl*>(
581 BrowserContext::GetDefaultStoragePartition(browser_context()));
582 partition->OverrideQuotaManagerForTesting(
583 GetMockManager());
585 base::RunLoop run_loop;
586 base::ThreadTaskRunnerHandle::Get()->PostTask(
587 FROM_HERE, base::Bind(&ClearQuotaDataForOrigin, partition, kOrigin1,
588 base::Time(), &run_loop));
589 run_loop.Run();
591 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin1, kTemporary,
592 kClientFile));
593 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin2, kTemporary,
594 kClientFile));
595 EXPECT_TRUE(GetMockManager()->OriginHasData(kOrigin3, kTemporary,
596 kClientFile));
597 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin1, kPersistent,
598 kClientFile));
599 EXPECT_TRUE(GetMockManager()->OriginHasData(kOrigin2, kPersistent,
600 kClientFile));
601 EXPECT_TRUE(GetMockManager()->OriginHasData(kOrigin3, kPersistent,
602 kClientFile));
605 TEST_F(StoragePartitionImplTest, RemoveQuotaManagedDataForLastHour) {
606 PopulateTestQuotaManagedData(GetMockManager());
608 StoragePartitionImpl* partition = static_cast<StoragePartitionImpl*>(
609 BrowserContext::GetDefaultStoragePartition(browser_context()));
610 partition->OverrideQuotaManagerForTesting(
611 GetMockManager());
613 base::RunLoop run_loop;
614 base::ThreadTaskRunnerHandle::Get()->PostTask(
615 FROM_HERE,
616 base::Bind(&ClearQuotaDataForOrigin, partition, GURL(),
617 base::Time::Now() - base::TimeDelta::FromHours(1), &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::ThreadTaskRunnerHandle::Get()->PostTask(
643 FROM_HERE,
644 base::Bind(&ClearQuotaDataForNonPersistent, partition,
645 base::Time::Now() - base::TimeDelta::FromDays(7), &run_loop));
646 run_loop.Run();
648 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin1, kTemporary,
649 kClientFile));
650 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin2, kTemporary,
651 kClientFile));
652 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin3, kTemporary,
653 kClientFile));
654 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin1, kPersistent,
655 kClientFile));
656 EXPECT_TRUE(GetMockManager()->OriginHasData(kOrigin2, kPersistent,
657 kClientFile));
658 EXPECT_TRUE(GetMockManager()->OriginHasData(kOrigin3, kPersistent,
659 kClientFile));
662 TEST_F(StoragePartitionImplTest, RemoveQuotaManagedUnprotectedOrigins) {
663 // Protect kOrigin1.
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(
673 GetMockManager());
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));
681 run_loop.Run();
683 EXPECT_TRUE(GetMockManager()->OriginHasData(kOrigin1, kTemporary,
684 kClientFile));
685 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin2, kTemporary,
686 kClientFile));
687 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin3, kTemporary,
688 kClientFile));
689 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin1, kPersistent,
690 kClientFile));
691 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin2, kPersistent,
692 kClientFile));
693 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin3, kPersistent,
694 kClientFile));
697 TEST_F(StoragePartitionImplTest, RemoveQuotaManagedProtectedSpecificOrigin) {
698 // Protect kOrigin1.
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(
708 GetMockManager());
709 partition->OverrideSpecialStoragePolicyForTesting(mock_policy.get());
711 // Try to remove kOrigin1. Expect failure.
712 base::RunLoop run_loop;
713 base::ThreadTaskRunnerHandle::Get()->PostTask(
714 FROM_HERE,
715 base::Bind(&ClearQuotaDataWithOriginMatcher, partition, kOrigin1,
716 base::Bind(&DoesOriginMatchForUnprotectedWeb), base::Time(),
717 &run_loop));
718 run_loop.Run();
720 EXPECT_TRUE(GetMockManager()->OriginHasData(kOrigin1, kTemporary,
721 kClientFile));
722 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin2, kTemporary,
723 kClientFile));
724 EXPECT_TRUE(GetMockManager()->OriginHasData(kOrigin3, kTemporary,
725 kClientFile));
726 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin1, kPersistent,
727 kClientFile));
728 EXPECT_TRUE(GetMockManager()->OriginHasData(kOrigin2, kPersistent,
729 kClientFile));
730 EXPECT_TRUE(GetMockManager()->OriginHasData(kOrigin3, kPersistent,
731 kClientFile));
734 TEST_F(StoragePartitionImplTest, RemoveQuotaManagedProtectedOrigins) {
735 // Protect kOrigin1.
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(
747 GetMockManager());
748 partition->OverrideSpecialStoragePolicyForTesting(mock_policy.get());
749 base::ThreadTaskRunnerHandle::Get()->PostTask(
750 FROM_HERE,
751 base::Bind(&ClearQuotaDataWithOriginMatcher, partition, GURL(),
752 base::Bind(&DoesOriginMatchForBothProtectedAndUnprotectedWeb),
753 base::Time(), &run_loop));
754 run_loop.Run();
756 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin1, kTemporary,
757 kClientFile));
758 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin2, kTemporary,
759 kClientFile));
760 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin3, kTemporary,
761 kClientFile));
762 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin1, kPersistent,
763 kClientFile));
764 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin2, kPersistent,
765 kClientFile));
766 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin3, kPersistent,
767 kClientFile));
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(
777 GetMockManager());
778 base::ThreadTaskRunnerHandle::Get()->PostTask(
779 FROM_HERE, base::Bind(&ClearQuotaDataWithOriginMatcher, partition, GURL(),
780 base::Bind(&DoesOriginMatchUnprotected),
781 base::Time(), &run_loop));
782 run_loop.Run();
784 // Check that devtools data isn't removed.
785 EXPECT_TRUE(GetMockManager()->OriginHasData(kOriginDevTools, kTemporary,
786 kClientFile));
787 EXPECT_TRUE(GetMockManager()->OriginHasData(kOriginDevTools, kPersistent,
788 kClientFile));
791 TEST_F(StoragePartitionImplTest, RemoveCookieForever) {
792 RemoveCookieTester tester(browser_context());
794 tester.AddCookie();
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));
805 run_loop.Run();
807 EXPECT_FALSE(tester.ContainsCookie());
810 TEST_F(StoragePartitionImplTest, RemoveCookieLastHour) {
811 RemoveCookieTester tester(browser_context());
813 tester.AddCookie();
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));
825 run_loop.Run();
827 EXPECT_FALSE(tester.ContainsCookie());
830 TEST_F(StoragePartitionImplTest, RemoveUnprotectedLocalStorageForever) {
831 // Protect kOrigin1.
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(
849 FROM_HERE,
850 base::Bind(&ClearStuff,
851 StoragePartitionImpl::REMOVE_DATA_MASK_LOCAL_STORAGE,
852 partition, base::Time(), base::Time::Max(),
853 base::Bind(&DoesOriginMatchForUnprotectedWeb), &run_loop));
854 run_loop.Run();
856 EXPECT_TRUE(tester.DOMStorageExistsForOrigin(kOrigin1));
857 EXPECT_FALSE(tester.DOMStorageExistsForOrigin(kOrigin2));
858 EXPECT_FALSE(tester.DOMStorageExistsForOrigin(kOrigin3));
861 TEST_F(StoragePartitionImplTest, RemoveProtectedLocalStorageForever) {
862 // Protect kOrigin1.
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(
880 FROM_HERE,
881 base::Bind(&ClearStuff,
882 StoragePartitionImpl::REMOVE_DATA_MASK_LOCAL_STORAGE,
883 partition, base::Time(), base::Time::Max(),
884 base::Bind(&DoesOriginMatchForBothProtectedAndUnprotectedWeb),
885 &run_loop));
886 run_loop.Run();
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(
909 FROM_HERE,
910 base::Bind(&ClearStuff,
911 StoragePartitionImpl::REMOVE_DATA_MASK_LOCAL_STORAGE,
912 partition, a_week_ago, base::Time::Max(),
913 base::Bind(&DoesOriginMatchForBothProtectedAndUnprotectedWeb),
914 &run_loop));
915 run_loop.Run();
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