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

Side by Side Diff: content/browser/storage_partition_impl_unittest.cc

Issue 37843003: BrowsingDataRemover, (re)use StoragePartition deletion code. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: rebase @tott. Created 7 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2013 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "base/file_util.h"
5 #include "base/files/scoped_temp_dir.h" 6 #include "base/files/scoped_temp_dir.h"
6 #include "base/message_loop/message_loop_proxy.h" 7 #include "base/message_loop/message_loop_proxy.h"
7 #include "base/run_loop.h" 8 #include "base/run_loop.h"
8 #include "base/threading/thread.h" 9 #include "base/threading/thread.h"
9 #include "content/browser/browser_thread_impl.h" 10 #include "content/browser/browser_thread_impl.h"
10 #include "content/browser/gpu/shader_disk_cache.h" 11 #include "content/browser/gpu/shader_disk_cache.h"
11 #include "content/browser/storage_partition_impl.h" 12 #include "content/browser/storage_partition_impl.h"
13 #include "content/public/browser/local_storage_usage_info.h"
12 #include "content/public/browser/storage_partition.h" 14 #include "content/public/browser/storage_partition.h"
15 #include "content/public/test/test_browser_context.h"
16 #include "content/public/test/test_browser_thread.h"
13 #include "content/public/test/test_browser_thread_bundle.h" 17 #include "content/public/test/test_browser_thread_bundle.h"
14 #include "net/base/test_completion_callback.h" 18 #include "net/base/test_completion_callback.h"
19 #include "net/cookies/cookie_monster.h"
20 #include "net/url_request/url_request_context.h"
21 #include "net/url_request/url_request_context_getter.h"
15 #include "testing/gtest/include/gtest/gtest.h" 22 #include "testing/gtest/include/gtest/gtest.h"
23 #include "webkit/browser/quota/mock_quota_manager.h"
24 #include "webkit/browser/quota/mock_special_storage_policy.h"
25 #include "webkit/browser/quota/quota_manager.h"
16 26
17 namespace content { 27 namespace content {
18 namespace { 28 namespace {
19 29
30 const int kDefaultClientId = 42;
31 const char kCacheKey[] = "key";
32 const char kCacheValue[] = "cached value";
33
34 const char kTestOrigin1[] = "http://host1:1/";
35 const char kTestOrigin2[] = "http://host2:1/";
36 const char kTestOrigin3[] = "http://host3:1/";
37 const char kTestOriginDevTools[] = "chrome-devtools://abcdefghijklmnopqrstuvw/";
38
39 const GURL kOrigin1(kTestOrigin1);
40 const GURL kOrigin2(kTestOrigin2);
41 const GURL kOrigin3(kTestOrigin3);
42 const GURL kOriginDevTools(kTestOriginDevTools);
43
44 const base::FilePath::CharType kDomStorageOrigin1[] =
45 FILE_PATH_LITERAL("http_host1_1.localstorage");
46
47 const base::FilePath::CharType kDomStorageOrigin2[] =
48 FILE_PATH_LITERAL("http_host2_1.localstorage");
49
50 const base::FilePath::CharType kDomStorageOrigin3[] =
51 FILE_PATH_LITERAL("http_host3_1.localstorage");
52
53 const quota::StorageType kTemporary = quota::kStorageTypeTemporary;
54 const quota::StorageType kPersistent = quota::kStorageTypePersistent;
55
56 const quota::QuotaClient::ID kClientFile = quota::QuotaClient::kFileSystem;
57
58 const uint32 kAllQuotaRemoveMask =
59 StoragePartition::REMOVE_DATA_MASK_INDEXEDDB |
60 StoragePartition::REMOVE_DATA_MASK_WEBSQL |
61 StoragePartition::REMOVE_DATA_MASK_FILE_SYSTEMS |
62 StoragePartition::REMOVE_DATA_MASK_APPCACHE;
63
20 class TestClosureCallback { 64 class TestClosureCallback {
21 public: 65 public:
22 TestClosureCallback() 66 TestClosureCallback()
23 : callback_(base::Bind( 67 : callback_(base::Bind(
24 &TestClosureCallback::StopWaiting, base::Unretained(this))) { 68 &TestClosureCallback::StopWaiting, base::Unretained(this))) {
25 } 69 }
26 70
27 void WaitForResult() { 71 void WaitForResult() {
28 wait_run_loop_.reset(new base::RunLoop()); 72 wait_run_loop_.reset(new base::RunLoop());
29 wait_run_loop_->Run(); 73 wait_run_loop_->Run();
30 } 74 }
31 75
32 const base::Closure& callback() { return callback_; } 76 const base::Closure& callback() { return callback_; }
33 77
34 private: 78 private:
35 void StopWaiting() { 79 void StopWaiting() {
36 wait_run_loop_->Quit(); 80 wait_run_loop_->Quit();
37 } 81 }
38 82
39 base::Closure callback_; 83 base::Closure callback_;
40 scoped_ptr<base::RunLoop> wait_run_loop_; 84 scoped_ptr<base::RunLoop> wait_run_loop_;
41 85
42 DISALLOW_COPY_AND_ASSIGN(TestClosureCallback); 86 DISALLOW_COPY_AND_ASSIGN(TestClosureCallback);
43 }; 87 };
44 88
45 const int kDefaultClientId = 42; 89 class AwaitCompletionHelper {
46 const char kCacheKey[] = "key"; 90 public:
47 const char kCacheValue[] = "cached value"; 91 AwaitCompletionHelper() : start_(false), already_quit_(false) {}
92 virtual ~AwaitCompletionHelper() {}
93
94 void BlockUntilNotified() {
95 if (!already_quit_) {
96 DCHECK(!start_);
97 start_ = true;
98 base::MessageLoop::current()->Run();
99 } else {
100 DCHECK(!start_);
101 already_quit_ = false;
102 }
103 }
104
105 void Notify() {
106 if (start_) {
107 DCHECK(!already_quit_);
108 base::MessageLoop::current()->Quit();
109 start_ = false;
110 } else {
111 DCHECK(!already_quit_);
112 already_quit_ = true;
113 }
114 }
115
116 // protected:
117 // // BrowsingDataRemover::Observer implementation.
118 // virtual void OnBrowsingDataRemoverDone() OVERRIDE {
119 // Notify();
120 // }
121 //
122 private:
123 // Helps prevent from running message_loop, if the callback invoked
124 // immediately.
125 bool start_;
126 bool already_quit_;
127
128 DISALLOW_COPY_AND_ASSIGN(AwaitCompletionHelper);
129 };
130
131 class RemoveCookieTester {
132 public:
133 explicit RemoveCookieTester(TestBrowserContext* context)
134 : get_cookie_success_(false), monster_(NULL) {
135 SetMonster(context->GetRequestContext()->GetURLRequestContext()->
136 cookie_store()->GetCookieMonster());
137 }
138
139 // Returns true, if the given cookie exists in the cookie store.
140 bool ContainsCookie() {
141 get_cookie_success_ = false;
142 monster_->GetCookiesWithOptionsAsync(
143 kOrigin1, net::CookieOptions(),
144 base::Bind(&RemoveCookieTester::GetCookieCallback,
145 base::Unretained(this)));
146 await_completion_.BlockUntilNotified();
147 return get_cookie_success_;
148 }
149
150 void AddCookie() {
151 monster_->SetCookieWithOptionsAsync(
152 kOrigin1, "A=1", net::CookieOptions(),
153 base::Bind(&RemoveCookieTester::SetCookieCallback,
154 base::Unretained(this)));
155 await_completion_.BlockUntilNotified();
156 }
157
158 protected:
159 void SetMonster(net::CookieStore* monster) {
160 monster_ = monster;
161 }
162
163 private:
164 void GetCookieCallback(const std::string& cookies) {
165 if (cookies == "A=1") {
166 get_cookie_success_ = true;
167 } else {
168 EXPECT_EQ("", cookies);
169 get_cookie_success_ = false;
170 }
171 await_completion_.Notify();
172 }
173
174 void SetCookieCallback(bool result) {
175 ASSERT_TRUE(result);
176 await_completion_.Notify();
177 }
178
179 bool get_cookie_success_;
180 AwaitCompletionHelper await_completion_;
181 net::CookieStore* monster_;
182
183 DISALLOW_COPY_AND_ASSIGN(RemoveCookieTester);
184 };
185
186 class RemoveLocalStorageTester {
187 public:
188 explicit RemoveLocalStorageTester(TestBrowserContext* profile)
189 : profile_(profile), dom_storage_context_(NULL) {
190 dom_storage_context_ =
191 content::BrowserContext::GetDefaultStoragePartition(profile)->
192 GetDOMStorageContext();
193 }
194
195 // Returns true, if the given origin URL exists.
196 bool DOMStorageExistsForOrigin(const GURL& origin) {
197 GetLocalStorageUsage();
198 await_completion_.BlockUntilNotified();
199 for (size_t i = 0; i < infos_.size(); ++i) {
200 if (origin == infos_[i].origin)
201 return true;
202 }
203 return false;
204 }
205
206 void AddDOMStorageTestData() {
207 // Note: This test depends on details of how the dom_storage library
208 // stores data in the host file system.
209 base::FilePath storage_path =
210 profile_->GetPath().AppendASCII("Local Storage");
211 file_util::CreateDirectory(storage_path);
212
213 // Write some files.
214 file_util::WriteFile(storage_path.Append(kDomStorageOrigin1), NULL, 0);
215 file_util::WriteFile(storage_path.Append(kDomStorageOrigin2), NULL, 0);
216 file_util::WriteFile(storage_path.Append(kDomStorageOrigin3), NULL, 0);
217
218 // Tweak their dates.
219 file_util::SetLastModifiedTime(storage_path.Append(kDomStorageOrigin1),
220 base::Time::Now());
221 file_util::SetLastModifiedTime(storage_path.Append(kDomStorageOrigin2),
222 base::Time::Now() - base::TimeDelta::FromDays(1));
223 file_util::SetLastModifiedTime(storage_path.Append(kDomStorageOrigin3),
224 base::Time::Now() - base::TimeDelta::FromDays(60));
225 }
226
227 private:
228 void GetLocalStorageUsage() {
229 dom_storage_context_->GetLocalStorageUsage(
230 base::Bind(&RemoveLocalStorageTester::OnGotLocalStorageUsage,
231 base::Unretained(this)));
232 }
233 void OnGotLocalStorageUsage(
234 const std::vector<content::LocalStorageUsageInfo>& infos) {
235 infos_ = infos;
236 await_completion_.Notify();
237 }
238
239 // We don't own these pointers.
240 TestBrowserContext* profile_;
241 content::DOMStorageContext* dom_storage_context_;
242
243 std::vector<content::LocalStorageUsageInfo> infos_;
244
245 AwaitCompletionHelper await_completion_;
246
247 DISALLOW_COPY_AND_ASSIGN(RemoveLocalStorageTester);
248 };
249
250 bool IsWebSafeSchemeForTest(const std::string& scheme) {
251 return scheme == "http";
252 }
253
254 bool DoesOriginMatchForUnprotectedWeb(
255 const GURL& origin,
256 quota::SpecialStoragePolicy* special_storage_policy) {
257 // if (IsWebSafeSchemeForTest(origin.scheme())) {
Nico 2013/11/07 20:01:30 Did you intentionally land this code commented out
lazyboy 2013/11/07 20:17:18 Mistake from me. Uncommenting them in other CL.
258 return !special_storage_policy->IsStorageProtected(origin.GetOrigin());
259
260 // }
261 // return false;
262 }
263
264 bool DoesOriginMatchForBothProtectedAndUnprotectedWeb(
265 const GURL& origin,
266 quota::SpecialStoragePolicy* special_storage_policy) {
267 return true;
268 }
269
270 bool DoesOriginMatchUnprotected(
271 const GURL& origin,
272 quota::SpecialStoragePolicy* special_storage_policy) {
273 return origin.GetOrigin().scheme() != kOriginDevTools.scheme();
274 }
275
276 void ClearQuotaData(content::StoragePartition* storage_partition,
277 const base::Closure& cb) {
278 storage_partition->ClearData(
279 kAllQuotaRemoveMask,
280 StoragePartition::QUOTA_MANAGED_STORAGE_MASK_ALL,
281 NULL, StoragePartition::OriginMatcherFunction(),
282 base::Time(), base::Time::Max(), cb);
283 }
284
285 void ClearQuotaDataWithOriginMatcher(
286 content::StoragePartition* storage_partition,
287 const GURL& remove_origin,
288 const StoragePartition::OriginMatcherFunction& origin_matcher,
289 const base::Time delete_begin,
290 const base::Closure& cb) {
291 storage_partition->ClearData(kAllQuotaRemoveMask,
292 StoragePartition::QUOTA_MANAGED_STORAGE_MASK_ALL,
293 &remove_origin, origin_matcher, delete_begin,
294 base::Time::Max(), cb);
295 }
296
297 void ClearQuotaDataForOrigin(
298 content::StoragePartition* storage_partition,
299 const GURL& remove_origin,
300 const base::Time delete_begin,
301 const base::Closure& cb) {
302 ClearQuotaDataWithOriginMatcher(
303 storage_partition, remove_origin,
304 StoragePartition::OriginMatcherFunction(), delete_begin, cb);
305 }
306
307 void ClearQuotaDataForNonPersistent(
308 content::StoragePartition* storage_partition,
309 const base::Time delete_begin,
310 const base::Closure& cb) {
311 uint32 quota_storage_remove_mask_no_persistent =
312 StoragePartition::QUOTA_MANAGED_STORAGE_MASK_ALL &
313 ~StoragePartition::QUOTA_MANAGED_STORAGE_MASK_PERSISTENT;
314 storage_partition->ClearData(
315 kAllQuotaRemoveMask, quota_storage_remove_mask_no_persistent,
316 NULL, StoragePartition::OriginMatcherFunction(),
317 delete_begin, base::Time::Max(), cb);
318 }
319
320 void ClearCookies(content::StoragePartition* storage_partition,
321 const base::Time delete_begin,
322 const base::Time delete_end,
323 const base::Closure& cb) {
324 storage_partition->ClearData(
325 StoragePartition::REMOVE_DATA_MASK_COOKIES,
326 StoragePartition::QUOTA_MANAGED_STORAGE_MASK_ALL,
327 NULL, StoragePartition::OriginMatcherFunction(),
328 delete_begin, delete_end, cb);
329 }
330
331 void ClearStuff(uint32 remove_mask,
332 content::StoragePartition* storage_partition,
333 const base::Time delete_begin,
334 const base::Time delete_end,
335 const StoragePartition::OriginMatcherFunction& origin_matcher,
336 const base::Closure& cb) {
337 storage_partition->ClearData(
338 remove_mask, StoragePartition::QUOTA_MANAGED_STORAGE_MASK_ALL,
339 NULL, origin_matcher, delete_begin, delete_end, cb);
340 }
48 341
49 } // namespace 342 } // namespace
50 343
344 class StoragePartitionImplTest : public testing::Test {
345 public:
346 StoragePartitionImplTest()
347 : browser_context_(new TestBrowserContext()),
348 thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP) {
349 }
350 virtual ~StoragePartitionImplTest() {}
351
352 quota::MockQuotaManager* GetMockManager() {
353 if (!quota_manager_.get()) {
354 quota_manager_ = new quota::MockQuotaManager(
355 browser_context_->IsOffTheRecord(),
356 browser_context_->GetPath(),
357 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO).get(),
358 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::DB).get(),
359 browser_context_->GetSpecialStoragePolicy());
360 }
361 return quota_manager_.get();
362 }
363
364 TestBrowserContext* GetBrowserContext() {
365 return browser_context_.get();
366 }
367
368 private:
369 scoped_ptr<TestBrowserContext> browser_context_;
370 scoped_refptr<quota::MockQuotaManager> quota_manager_;
371 content::TestBrowserThreadBundle thread_bundle_;
372
373 DISALLOW_COPY_AND_ASSIGN(StoragePartitionImplTest);
374 };
375
51 class StoragePartitionShaderClearTest : public testing::Test { 376 class StoragePartitionShaderClearTest : public testing::Test {
52 public: 377 public:
53 StoragePartitionShaderClearTest() 378 StoragePartitionShaderClearTest()
54 : thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP) { 379 : thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP) {
55 } 380 }
56 381
57 virtual ~StoragePartitionShaderClearTest() {} 382 virtual ~StoragePartitionShaderClearTest() {}
58 383
59 const base::FilePath& cache_path() { return temp_dir_.path(); } 384 const base::FilePath& cache_path() { return temp_dir_.path(); }
60 385
(...skipping 30 matching lines...) Expand all
91 416
92 base::ScopedTempDir temp_dir_; 417 base::ScopedTempDir temp_dir_;
93 content::TestBrowserThreadBundle thread_bundle_; 418 content::TestBrowserThreadBundle thread_bundle_;
94 419
95 scoped_refptr<ShaderDiskCache> cache_; 420 scoped_refptr<ShaderDiskCache> cache_;
96 }; 421 };
97 422
98 void ClearData(content::StoragePartitionImpl* sp, 423 void ClearData(content::StoragePartitionImpl* sp,
99 const base::Closure& cb) { 424 const base::Closure& cb) {
100 base::Time time; 425 base::Time time;
101 sp->ClearDataForRange( 426 sp->ClearData(
102 StoragePartition::REMOVE_DATA_MASK_SHADER_CACHE, 427 StoragePartition::REMOVE_DATA_MASK_SHADER_CACHE,
103 StoragePartition::QUOTA_MANAGED_STORAGE_MASK_ALL, 428 StoragePartition::QUOTA_MANAGED_STORAGE_MASK_ALL,
429 NULL, StoragePartition::OriginMatcherFunction(),
104 time, time, cb); 430 time, time, cb);
105 } 431 }
106 432
433 // Tests ---------------------------------------------------------------------
434
107 TEST_F(StoragePartitionShaderClearTest, ClearShaderCache) { 435 TEST_F(StoragePartitionShaderClearTest, ClearShaderCache) {
108 InitCache(); 436 InitCache();
109 EXPECT_EQ(1u, Size()); 437 EXPECT_EQ(1u, Size());
110 438
111 TestClosureCallback clear_cb; 439 TestClosureCallback clear_cb;
112 StoragePartitionImpl sp( 440 StoragePartitionImpl storage_partition(
113 cache_path(), NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL); 441 cache_path(), NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
114 base::MessageLoop::current()->PostTask( 442 base::MessageLoop::current()->PostTask(
115 FROM_HERE, base::Bind(&ClearData, &sp, clear_cb.callback())); 443 FROM_HERE, base::Bind(&ClearData, &storage_partition,
444 clear_cb.callback()));
116 clear_cb.WaitForResult(); 445 clear_cb.WaitForResult();
117 EXPECT_EQ(0u, Size()); 446 EXPECT_EQ(0u, Size());
118 } 447 }
119 448
449 TEST_F(StoragePartitionImplTest, QuotaClientMaskGeneration) {
450 EXPECT_EQ(quota::QuotaClient::kFileSystem,
451 StoragePartitionImpl::GenerateQuotaClientMask(
452 StoragePartition::REMOVE_DATA_MASK_FILE_SYSTEMS));
453 EXPECT_EQ(quota::QuotaClient::kDatabase,
454 StoragePartitionImpl::GenerateQuotaClientMask(
455 StoragePartition::REMOVE_DATA_MASK_WEBSQL));
456 EXPECT_EQ(quota::QuotaClient::kAppcache,
457 StoragePartitionImpl::GenerateQuotaClientMask(
458 StoragePartition::REMOVE_DATA_MASK_APPCACHE));
459 EXPECT_EQ(quota::QuotaClient::kIndexedDatabase,
460 StoragePartitionImpl::GenerateQuotaClientMask(
461 StoragePartition::REMOVE_DATA_MASK_INDEXEDDB));
462 EXPECT_EQ(quota::QuotaClient::kFileSystem |
463 quota::QuotaClient::kDatabase |
464 quota::QuotaClient::kAppcache |
465 quota::QuotaClient::kIndexedDatabase,
466 StoragePartitionImpl::GenerateQuotaClientMask(
467 StoragePartition::REMOVE_DATA_MASK_FILE_SYSTEMS |
468 StoragePartition::REMOVE_DATA_MASK_WEBSQL |
469 StoragePartition::REMOVE_DATA_MASK_APPCACHE |
470 StoragePartition::REMOVE_DATA_MASK_INDEXEDDB));
471 }
472
473 void PopulateTestQuotaManagedPersistentData(quota::MockQuotaManager* manager) {
474 manager->AddOrigin(kOrigin2, kPersistent, kClientFile, base::Time());
475 manager->AddOrigin(kOrigin3, kPersistent, kClientFile,
476 base::Time::Now() - base::TimeDelta::FromDays(1));
477
478 EXPECT_FALSE(manager->OriginHasData(kOrigin1, kPersistent, kClientFile));
479 EXPECT_TRUE(manager->OriginHasData(kOrigin2, kPersistent, kClientFile));
480 EXPECT_TRUE(manager->OriginHasData(kOrigin3, kPersistent, kClientFile));
481 }
482
483 void PopulateTestQuotaManagedTemporaryData(quota::MockQuotaManager* manager) {
484 manager->AddOrigin(kOrigin1, kTemporary, kClientFile, base::Time::Now());
485 manager->AddOrigin(kOrigin3, kTemporary, kClientFile,
486 base::Time::Now() - base::TimeDelta::FromDays(1));
487
488 EXPECT_TRUE(manager->OriginHasData(kOrigin1, kTemporary, kClientFile));
489 EXPECT_FALSE(manager->OriginHasData(kOrigin2, kTemporary, kClientFile));
490 EXPECT_TRUE(manager->OriginHasData(kOrigin3, kTemporary, kClientFile));
491 }
492
493 void PopulateTestQuotaManagedData(quota::MockQuotaManager* manager) {
494 // Set up kOrigin1 with a temporary quota, kOrigin2 with a persistent
495 // quota, and kOrigin3 with both. kOrigin1 is modified now, kOrigin2
496 // is modified at the beginning of time, and kOrigin3 is modified one day
497 // ago.
498 PopulateTestQuotaManagedPersistentData(manager);
499 PopulateTestQuotaManagedTemporaryData(manager);
500 }
501
502 void PopulateTestQuotaManagedNonBrowsingData(quota::MockQuotaManager* manager) {
503 manager->AddOrigin(kOriginDevTools, kTemporary, kClientFile, base::Time());
504 manager->AddOrigin(kOriginDevTools, kPersistent, kClientFile, base::Time());
505 }
506
507 TEST_F(StoragePartitionImplTest, RemoveQuotaManagedDataForeverBoth) {
508 PopulateTestQuotaManagedData(GetMockManager());
509
510 TestClosureCallback clear_cb;
511 StoragePartitionImpl sp(
512 base::FilePath(), NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
513 static_cast<StoragePartitionImpl*>(&sp)->OverrideQuotaManagerForTesting(
514 GetMockManager());
515 base::MessageLoop::current()->PostTask(
516 FROM_HERE, base::Bind(&ClearQuotaData, &sp, clear_cb.callback()));
517 clear_cb.WaitForResult();
518
519 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin1, kTemporary,
520 kClientFile));
521 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin2, kTemporary,
522 kClientFile));
523 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin3, kTemporary,
524 kClientFile));
525 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin1, kPersistent,
526 kClientFile));
527 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin2, kPersistent,
528 kClientFile));
529 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin3, kPersistent,
530 kClientFile));
531 }
532
533 TEST_F(StoragePartitionImplTest, RemoveQuotaManagedDataForeverOnlyTemporary) {
534 PopulateTestQuotaManagedTemporaryData(GetMockManager());
535
536 TestClosureCallback clear_cb;
537 StoragePartitionImpl sp(
538 base::FilePath(), NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
539 static_cast<StoragePartitionImpl*>(&sp)->OverrideQuotaManagerForTesting(
540 GetMockManager());
541 base::MessageLoop::current()->PostTask(
542 FROM_HERE, base::Bind(&ClearQuotaData, &sp, clear_cb.callback()));
543 clear_cb.WaitForResult();
544
545 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin1, kTemporary,
546 kClientFile));
547 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin2, kTemporary,
548 kClientFile));
549 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin3, kTemporary,
550 kClientFile));
551 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin1, kPersistent,
552 kClientFile));
553 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin2, kPersistent,
554 kClientFile));
555 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin3, kPersistent,
556 kClientFile));
557 }
558
559 TEST_F(StoragePartitionImplTest, RemoveQuotaManagedDataForeverOnlyPersistent) {
560 PopulateTestQuotaManagedPersistentData(GetMockManager());
561
562 TestClosureCallback clear_cb;
563 StoragePartitionImpl sp(
564 base::FilePath(), NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
565 static_cast<StoragePartitionImpl*>(&sp)->OverrideQuotaManagerForTesting(
566 GetMockManager());
567 base::MessageLoop::current()->PostTask(
568 FROM_HERE, base::Bind(&ClearQuotaData, &sp, clear_cb.callback()));
569 clear_cb.WaitForResult();
570
571 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin1, kTemporary,
572 kClientFile));
573 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin2, kTemporary,
574 kClientFile));
575 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin3, kTemporary,
576 kClientFile));
577 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin1, kPersistent,
578 kClientFile));
579 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin2, kPersistent,
580 kClientFile));
581 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin3, kPersistent,
582 kClientFile));
583 }
584
585 TEST_F(StoragePartitionImplTest, RemoveQuotaManagedDataForeverNeither) {
586 TestClosureCallback clear_cb;
587 StoragePartitionImpl sp(
588 base::FilePath(), NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
589 static_cast<StoragePartitionImpl*>(&sp)->OverrideQuotaManagerForTesting(
590 GetMockManager());
591 base::MessageLoop::current()->PostTask(
592 FROM_HERE, base::Bind(&ClearQuotaData, &sp, clear_cb.callback()));
593 clear_cb.WaitForResult();
594
595 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin1, kTemporary,
596 kClientFile));
597 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin2, kTemporary,
598 kClientFile));
599 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin3, kTemporary,
600 kClientFile));
601 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin1, kPersistent,
602 kClientFile));
603 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin2, kPersistent,
604 kClientFile));
605 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin3, kPersistent,
606 kClientFile));
607 }
608
609 TEST_F(StoragePartitionImplTest, RemoveQuotaManagedDataForeverSpecificOrigin) {
610 PopulateTestQuotaManagedData(GetMockManager());
611
612 TestClosureCallback clear_cb;
613 StoragePartitionImpl sp(
614 base::FilePath(), NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
615 static_cast<StoragePartitionImpl*>(&sp)->OverrideQuotaManagerForTesting(
616 GetMockManager());
617 base::MessageLoop::current()->PostTask(
618 FROM_HERE, base::Bind(&ClearQuotaDataForOrigin,
619 &sp, kOrigin1, base::Time(), clear_cb.callback()));
620 clear_cb.WaitForResult();
621
622 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin1, kTemporary,
623 kClientFile));
624 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin2, kTemporary,
625 kClientFile));
626 EXPECT_TRUE(GetMockManager()->OriginHasData(kOrigin3, kTemporary,
627 kClientFile));
628 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin1, kPersistent,
629 kClientFile));
630 EXPECT_TRUE(GetMockManager()->OriginHasData(kOrigin2, kPersistent,
631 kClientFile));
632 EXPECT_TRUE(GetMockManager()->OriginHasData(kOrigin3, kPersistent,
633 kClientFile));
634 }
635
636 TEST_F(StoragePartitionImplTest, RemoveQuotaManagedDataForLastHour) {
637 PopulateTestQuotaManagedData(GetMockManager());
638
639 TestClosureCallback clear_cb;
640 StoragePartitionImpl sp(
641 base::FilePath(), NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
642 static_cast<StoragePartitionImpl*>(&sp)->OverrideQuotaManagerForTesting(
643 GetMockManager());
644 base::MessageLoop::current()->PostTask(
645 FROM_HERE, base::Bind(&ClearQuotaDataForOrigin,
646 &sp, GURL(),
647 base::Time::Now() - base::TimeDelta::FromHours(1),
648 clear_cb.callback()));
649 clear_cb.WaitForResult();
650
651 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin1, kTemporary,
652 kClientFile));
653 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin2, kTemporary,
654 kClientFile));
655 EXPECT_TRUE(GetMockManager()->OriginHasData(kOrigin3, kTemporary,
656 kClientFile));
657 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin1, kPersistent,
658 kClientFile));
659 EXPECT_TRUE(GetMockManager()->OriginHasData(kOrigin2, kPersistent,
660 kClientFile));
661 EXPECT_TRUE(GetMockManager()->OriginHasData(kOrigin3, kPersistent,
662 kClientFile));
663 }
664
665 TEST_F(StoragePartitionImplTest, RemoveQuotaManagedDataForLastWeek) {
666 PopulateTestQuotaManagedData(GetMockManager());
667
668 TestClosureCallback clear_cb;
669 StoragePartitionImpl sp(
670 base::FilePath(), NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
671 static_cast<StoragePartitionImpl*>(&sp)->OverrideQuotaManagerForTesting(
672 GetMockManager());
673 base::MessageLoop::current()->PostTask(
674 FROM_HERE, base::Bind(&ClearQuotaDataForNonPersistent,
675 &sp,
676 base::Time::Now() - base::TimeDelta::FromDays(7),
677 clear_cb.callback()));
678 clear_cb.WaitForResult();
679
680 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin1, kTemporary,
681 kClientFile));
682 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin2, kTemporary,
683 kClientFile));
684 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin3, kTemporary,
685 kClientFile));
686 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin1, kPersistent,
687 kClientFile));
688 EXPECT_TRUE(GetMockManager()->OriginHasData(kOrigin2, kPersistent,
689 kClientFile));
690 EXPECT_TRUE(GetMockManager()->OriginHasData(kOrigin3, kPersistent,
691 kClientFile));
692 }
693
694 TEST_F(StoragePartitionImplTest, RemoveQuotaManagedUnprotectedOrigins) {
695 // Protect kOrigin1.
696 scoped_refptr<quota::MockSpecialStoragePolicy> mock_policy =
697 new quota::MockSpecialStoragePolicy;
698 mock_policy->AddProtected(kOrigin1.GetOrigin());
699
700 PopulateTestQuotaManagedData(GetMockManager());
701
702 TestClosureCallback clear_cb;
703 StoragePartitionImpl sp(
704 base::FilePath(), NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
705 static_cast<StoragePartitionImpl*>(&sp)->OverrideQuotaManagerForTesting(
706 GetMockManager());
707 static_cast<StoragePartitionImpl*>(
708 &sp)->OverrideSpecialStoragePolicyForTesting(mock_policy);
709 base::MessageLoop::current()->PostTask(
710 FROM_HERE, base::Bind(&ClearQuotaDataWithOriginMatcher,
711 &sp, GURL(),
712 base::Bind(&DoesOriginMatchForUnprotectedWeb),
713 base::Time(), clear_cb.callback()));
714 clear_cb.WaitForResult();
715
716 EXPECT_TRUE(GetMockManager()->OriginHasData(kOrigin1, kTemporary,
717 kClientFile));
718 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin2, kTemporary,
719 kClientFile));
720 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin3, kTemporary,
721 kClientFile));
722 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin1, kPersistent,
723 kClientFile));
724 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin2, kPersistent,
725 kClientFile));
726 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin3, kPersistent,
727 kClientFile));
728 }
729
730 TEST_F(StoragePartitionImplTest, RemoveQuotaManagedProtectedSpecificOrigin) {
731 // Protect kOrigin1.
732 scoped_refptr<quota::MockSpecialStoragePolicy> mock_policy =
733 new quota::MockSpecialStoragePolicy;
734 mock_policy->AddProtected(kOrigin1.GetOrigin());
735
736 PopulateTestQuotaManagedData(GetMockManager());
737
738 TestClosureCallback clear_cb;
739 StoragePartitionImpl sp(
740 base::FilePath(), NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
741 static_cast<StoragePartitionImpl*>(&sp)->OverrideQuotaManagerForTesting(
742 GetMockManager());
743 static_cast<StoragePartitionImpl*>(
744 &sp)->OverrideSpecialStoragePolicyForTesting(mock_policy);
745
746 // Try to remove kOrigin1. Expect failure.
747 base::MessageLoop::current()->PostTask(
748 FROM_HERE, base::Bind(&ClearQuotaDataWithOriginMatcher,
749 &sp, kOrigin1,
750 base::Bind(&DoesOriginMatchForUnprotectedWeb),
751 base::Time(), clear_cb.callback()));
752 clear_cb.WaitForResult();
753
754 EXPECT_TRUE(GetMockManager()->OriginHasData(kOrigin1, kTemporary,
755 kClientFile));
756 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin2, kTemporary,
757 kClientFile));
758 EXPECT_TRUE(GetMockManager()->OriginHasData(kOrigin3, kTemporary,
759 kClientFile));
760 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin1, kPersistent,
761 kClientFile));
762 EXPECT_TRUE(GetMockManager()->OriginHasData(kOrigin2, kPersistent,
763 kClientFile));
764 EXPECT_TRUE(GetMockManager()->OriginHasData(kOrigin3, kPersistent,
765 kClientFile));
766 }
767
768 TEST_F(StoragePartitionImplTest, RemoveQuotaManagedProtectedOrigins) {
769 // Protect kOrigin1.
770 scoped_refptr<quota::MockSpecialStoragePolicy> mock_policy =
771 new quota::MockSpecialStoragePolicy;
772 mock_policy->AddProtected(kOrigin1.GetOrigin());
773
774 PopulateTestQuotaManagedData(GetMockManager());
775
776 // Try to remove kOrigin1. Expect success.
777 TestClosureCallback clear_cb;
778 StoragePartitionImpl sp(
779 base::FilePath(), NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
780 static_cast<StoragePartitionImpl*>(&sp)->OverrideQuotaManagerForTesting(
781 GetMockManager());
782 static_cast<StoragePartitionImpl*>(
783 &sp)->OverrideSpecialStoragePolicyForTesting(mock_policy);
784 base::MessageLoop::current()->PostTask(
785 FROM_HERE,
786 base::Bind(&ClearQuotaDataWithOriginMatcher,
787 &sp, GURL(),
788 base::Bind(&DoesOriginMatchForBothProtectedAndUnprotectedWeb),
789 base::Time(), clear_cb.callback()));
790 clear_cb.WaitForResult();
791
792 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin1, kTemporary,
793 kClientFile));
794 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin2, kTemporary,
795 kClientFile));
796 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin3, kTemporary,
797 kClientFile));
798 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin1, kPersistent,
799 kClientFile));
800 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin2, kPersistent,
801 kClientFile));
802 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin3, kPersistent,
803 kClientFile));
804 }
805
806 TEST_F(StoragePartitionImplTest, RemoveQuotaManagedIgnoreDevTools) {
807 PopulateTestQuotaManagedNonBrowsingData(GetMockManager());
808
809 TestClosureCallback clear_cb;
810 StoragePartitionImpl sp(
811 base::FilePath(), NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
812 static_cast<StoragePartitionImpl*>(&sp)->OverrideQuotaManagerForTesting(
813 GetMockManager());
814 base::MessageLoop::current()->PostTask(
815 FROM_HERE, base::Bind(&ClearQuotaDataWithOriginMatcher,
816 &sp, GURL(),
817 base::Bind(&DoesOriginMatchUnprotected),
818 base::Time(), clear_cb.callback()));
819 clear_cb.WaitForResult();
820
821 // Check that devtools data isn't removed.
822 EXPECT_TRUE(GetMockManager()->OriginHasData(kOriginDevTools, kTemporary,
823 kClientFile));
824 EXPECT_TRUE(GetMockManager()->OriginHasData(kOriginDevTools, kPersistent,
825 kClientFile));
826 }
827
828 TEST_F(StoragePartitionImplTest, RemoveCookieForever) {
829 RemoveCookieTester tester(GetBrowserContext());
830
831 tester.AddCookie();
832 ASSERT_TRUE(tester.ContainsCookie());
833
834 TestClosureCallback clear_cb;
835 StoragePartitionImpl sp(
836 base::FilePath(), NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
837 sp.SetURLRequestContext(GetBrowserContext()->GetRequestContext());
838 base::MessageLoop::current()->PostTask(
839 FROM_HERE, base::Bind(&ClearCookies,
840 &sp, base::Time(), base::Time::Max(),
841 clear_cb.callback()));
842 clear_cb.WaitForResult();
843
844 EXPECT_FALSE(tester.ContainsCookie());
845 }
846
847 TEST_F(StoragePartitionImplTest, RemoveCookieLastHour) {
848 RemoveCookieTester tester(GetBrowserContext());
849
850 tester.AddCookie();
851 ASSERT_TRUE(tester.ContainsCookie());
852
853 TestClosureCallback clear_cb;
854 StoragePartitionImpl sp(
855 base::FilePath(), NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
856 base::Time an_hour_ago = base::Time::Now() - base::TimeDelta::FromHours(1);
857 sp.SetURLRequestContext(GetBrowserContext()->GetRequestContext());
858 base::MessageLoop::current()->PostTask(
859 FROM_HERE, base::Bind(&ClearCookies,
860 &sp, an_hour_ago, base::Time::Max(),
861 clear_cb.callback()));
862 clear_cb.WaitForResult();
863
864 EXPECT_FALSE(tester.ContainsCookie());
865 }
866
867 TEST_F(StoragePartitionImplTest, RemoveUnprotectedLocalStorageForever) {
868 // Protect kOrigin1.
869 scoped_refptr<quota::MockSpecialStoragePolicy> mock_policy =
870 new quota::MockSpecialStoragePolicy;
871 mock_policy->AddProtected(kOrigin1.GetOrigin());
872
873 RemoveLocalStorageTester tester(GetBrowserContext());
874
875 tester.AddDOMStorageTestData();
876 EXPECT_TRUE(tester.DOMStorageExistsForOrigin(kOrigin1));
877 EXPECT_TRUE(tester.DOMStorageExistsForOrigin(kOrigin2));
878 EXPECT_TRUE(tester.DOMStorageExistsForOrigin(kOrigin3));
879
880 TestClosureCallback clear_cb;
881 DOMStorageContextWrapper* dom_storage_context =
882 static_cast<DOMStorageContextWrapper*>(
883 content::BrowserContext::GetDefaultStoragePartition(
884 GetBrowserContext())->GetDOMStorageContext());
885 StoragePartitionImpl sp(base::FilePath(), NULL, NULL, NULL, NULL,
886 dom_storage_context, NULL, NULL, NULL, NULL);
887 static_cast<StoragePartitionImpl*>(
888 &sp)->OverrideSpecialStoragePolicyForTesting(mock_policy);
889 base::MessageLoop::current()->PostTask(
890 FROM_HERE,
891 base::Bind(&ClearStuff,
892 StoragePartitionImpl::REMOVE_DATA_MASK_LOCAL_STORAGE,
893 &sp, base::Time(), base::Time::Max(),
894 base::Bind(&DoesOriginMatchForUnprotectedWeb),
895 clear_cb.callback()));
896 clear_cb.WaitForResult();
897
898 EXPECT_TRUE(tester.DOMStorageExistsForOrigin(kOrigin1));
899 EXPECT_FALSE(tester.DOMStorageExistsForOrigin(kOrigin2));
900 EXPECT_FALSE(tester.DOMStorageExistsForOrigin(kOrigin3));
901 }
902
903 TEST_F(StoragePartitionImplTest, RemoveProtectedLocalStorageForever) {
904 // Protect kOrigin1.
905 scoped_refptr<quota::MockSpecialStoragePolicy> mock_policy =
906 new quota::MockSpecialStoragePolicy;
907 mock_policy->AddProtected(kOrigin1.GetOrigin());
908
909 RemoveLocalStorageTester tester(GetBrowserContext());
910
911 tester.AddDOMStorageTestData();
912 EXPECT_TRUE(tester.DOMStorageExistsForOrigin(kOrigin1));
913 EXPECT_TRUE(tester.DOMStorageExistsForOrigin(kOrigin2));
914 EXPECT_TRUE(tester.DOMStorageExistsForOrigin(kOrigin3));
915
916 TestClosureCallback clear_cb;
917 DOMStorageContextWrapper* dom_storage_context =
918 static_cast<DOMStorageContextWrapper*>(
919 content::BrowserContext::GetDefaultStoragePartition(
920 GetBrowserContext())->GetDOMStorageContext());
921 StoragePartitionImpl sp(base::FilePath(), NULL, NULL, NULL, NULL,
922 dom_storage_context, NULL, NULL, NULL, NULL);
923 static_cast<StoragePartitionImpl*>(
924 &sp)->OverrideSpecialStoragePolicyForTesting(mock_policy);
925 base::MessageLoop::current()->PostTask(
926 FROM_HERE,
927 base::Bind(&ClearStuff,
928 StoragePartitionImpl::REMOVE_DATA_MASK_LOCAL_STORAGE,
929 &sp, base::Time(), base::Time::Max(),
930 base::Bind(&DoesOriginMatchForBothProtectedAndUnprotectedWeb),
931 clear_cb.callback()));
932 clear_cb.WaitForResult();
933
934 // Even if kOrigin1 is protected, it will be deleted since we specify
935 // ClearData to delete protected data.
936 EXPECT_FALSE(tester.DOMStorageExistsForOrigin(kOrigin1));
937 EXPECT_FALSE(tester.DOMStorageExistsForOrigin(kOrigin2));
938 EXPECT_FALSE(tester.DOMStorageExistsForOrigin(kOrigin3));
939 }
940
941 TEST_F(StoragePartitionImplTest, RemoveLocalStorageForLastWeek) {
942 RemoveLocalStorageTester tester(GetBrowserContext());
943
944 tester.AddDOMStorageTestData();
945 EXPECT_TRUE(tester.DOMStorageExistsForOrigin(kOrigin1));
946 EXPECT_TRUE(tester.DOMStorageExistsForOrigin(kOrigin2));
947 EXPECT_TRUE(tester.DOMStorageExistsForOrigin(kOrigin3));
948
949 TestClosureCallback clear_cb;
950 DOMStorageContextWrapper* dom_storage_context =
951 static_cast<DOMStorageContextWrapper*>(
952 content::BrowserContext::GetDefaultStoragePartition(
953 GetBrowserContext())->GetDOMStorageContext());
954 StoragePartitionImpl sp(base::FilePath(), NULL, NULL, NULL, NULL,
955 dom_storage_context, NULL, NULL, NULL, NULL);
956 base::Time a_week_ago = base::Time::Now() - base::TimeDelta::FromDays(7);
957 base::MessageLoop::current()->PostTask(
958 FROM_HERE,
959 base::Bind(&ClearStuff,
960 StoragePartitionImpl::REMOVE_DATA_MASK_LOCAL_STORAGE,
961 &sp, a_week_ago, base::Time::Max(),
962 base::Bind(&DoesOriginMatchForBothProtectedAndUnprotectedWeb),
963 clear_cb.callback()));
964 clear_cb.WaitForResult();
965
966 // kOrigin1 and kOrigin2 do not have age more than a week.
967 EXPECT_FALSE(tester.DOMStorageExistsForOrigin(kOrigin1));
968 EXPECT_FALSE(tester.DOMStorageExistsForOrigin(kOrigin2));
969 EXPECT_TRUE(tester.DOMStorageExistsForOrigin(kOrigin3));
970 }
971
120 } // namespace content 972 } // namespace content
OLDNEW
« no previous file with comments | « content/browser/storage_partition_impl_map.cc ('k') | content/public/browser/storage_partition.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698