Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / content / browser / cache_storage / cache_storage_manager_unittest.cc
blob2a5541fc2314e4cb342d1d9f369f77c74e44cd56
1 // Copyright 2014 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 "content/browser/cache_storage/cache_storage_manager.h"
7 #include "base/files/file_path.h"
8 #include "base/files/file_util.h"
9 #include "base/files/scoped_temp_dir.h"
10 #include "base/run_loop.h"
11 #include "base/stl_util.h"
12 #include "base/thread_task_runner_handle.h"
13 #include "content/browser/cache_storage/cache_storage_quota_client.h"
14 #include "content/browser/fileapi/chrome_blob_storage_context.h"
15 #include "content/browser/quota/mock_quota_manager_proxy.h"
16 #include "content/public/browser/browser_thread.h"
17 #include "content/public/browser/cache_storage_usage_info.h"
18 #include "content/public/test/test_browser_context.h"
19 #include "content/public/test/test_browser_thread_bundle.h"
20 #include "net/url_request/url_request_context_getter.h"
21 #include "storage/browser/blob/blob_storage_context.h"
22 #include "storage/browser/quota/quota_manager_proxy.h"
23 #include "testing/gtest/include/gtest/gtest.h"
25 namespace content {
27 class CacheStorageManagerTest : public testing::Test {
28 public:
29 CacheStorageManagerTest()
30 : browser_thread_bundle_(TestBrowserThreadBundle::IO_MAINLOOP),
31 callback_bool_(false),
32 callback_error_(CACHE_STORAGE_OK),
33 origin1_("http://example1.com"),
34 origin2_("http://example2.com") {}
36 void SetUp() override {
37 ChromeBlobStorageContext* blob_storage_context(
38 ChromeBlobStorageContext::GetFor(&browser_context_));
39 // Wait for ChromeBlobStorageContext to finish initializing.
40 base::RunLoop().RunUntilIdle();
42 quota_manager_proxy_ = new MockQuotaManagerProxy(
43 nullptr, base::ThreadTaskRunnerHandle::Get().get());
45 if (MemoryOnly()) {
46 cache_manager_ = CacheStorageManager::Create(
47 base::FilePath(), base::ThreadTaskRunnerHandle::Get(),
48 quota_manager_proxy_);
49 } else {
50 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
51 cache_manager_ = CacheStorageManager::Create(
52 temp_dir_.path(), base::ThreadTaskRunnerHandle::Get(),
53 quota_manager_proxy_);
56 cache_manager_->SetBlobParametersForCache(
57 browser_context_.GetRequestContext(),
58 blob_storage_context->context()->AsWeakPtr());
61 void TearDown() override {
62 quota_manager_proxy_->SimulateQuotaManagerDestroyed();
63 base::RunLoop().RunUntilIdle();
66 virtual bool MemoryOnly() { return false; }
68 void BoolAndErrorCallback(base::RunLoop* run_loop,
69 bool value,
70 CacheStorageError error) {
71 callback_bool_ = value;
72 callback_error_ = error;
73 run_loop->Quit();
76 void CacheAndErrorCallback(base::RunLoop* run_loop,
77 const scoped_refptr<CacheStorageCache>& cache,
78 CacheStorageError error) {
79 callback_cache_ = cache;
80 callback_error_ = error;
81 run_loop->Quit();
84 void StringsAndErrorCallback(base::RunLoop* run_loop,
85 const std::vector<std::string>& strings,
86 CacheStorageError error) {
87 callback_strings_ = strings;
88 callback_error_ = error;
89 run_loop->Quit();
92 void CachePutCallback(base::RunLoop* run_loop, CacheStorageError error) {
93 callback_error_ = error;
94 run_loop->Quit();
97 void CacheMatchCallback(
98 base::RunLoop* run_loop,
99 CacheStorageError error,
100 scoped_ptr<ServiceWorkerResponse> response,
101 scoped_ptr<storage::BlobDataHandle> blob_data_handle) {
102 callback_error_ = error;
103 callback_cache_response_ = response.Pass();
104 // Deliberately drop the data handle as only the url is being tested.
105 run_loop->Quit();
108 bool Open(const GURL& origin, const std::string& cache_name) {
109 base::RunLoop loop;
110 cache_manager_->OpenCache(
111 origin, cache_name,
112 base::Bind(&CacheStorageManagerTest::CacheAndErrorCallback,
113 base::Unretained(this), base::Unretained(&loop)));
114 loop.Run();
116 bool error = callback_error_ != CACHE_STORAGE_OK;
117 if (error)
118 EXPECT_TRUE(!callback_cache_.get());
119 else
120 EXPECT_TRUE(callback_cache_.get());
121 return !error;
124 bool Has(const GURL& origin, const std::string& cache_name) {
125 base::RunLoop loop;
126 cache_manager_->HasCache(
127 origin, cache_name,
128 base::Bind(&CacheStorageManagerTest::BoolAndErrorCallback,
129 base::Unretained(this), base::Unretained(&loop)));
130 loop.Run();
132 return callback_bool_;
135 bool Delete(const GURL& origin, const std::string& cache_name) {
136 base::RunLoop loop;
137 cache_manager_->DeleteCache(
138 origin, cache_name,
139 base::Bind(&CacheStorageManagerTest::BoolAndErrorCallback,
140 base::Unretained(this), base::Unretained(&loop)));
141 loop.Run();
143 return callback_bool_;
146 bool Keys(const GURL& origin) {
147 base::RunLoop loop;
148 cache_manager_->EnumerateCaches(
149 origin, base::Bind(&CacheStorageManagerTest::StringsAndErrorCallback,
150 base::Unretained(this), base::Unretained(&loop)));
151 loop.Run();
153 return callback_error_ == CACHE_STORAGE_OK;
156 bool StorageMatch(const GURL& origin,
157 const std::string& cache_name,
158 const GURL& url) {
159 scoped_ptr<ServiceWorkerFetchRequest> request(
160 new ServiceWorkerFetchRequest());
161 request->url = url;
162 base::RunLoop loop;
163 cache_manager_->MatchCache(
164 origin, cache_name, request.Pass(),
165 base::Bind(&CacheStorageManagerTest::CacheMatchCallback,
166 base::Unretained(this), base::Unretained(&loop)));
167 loop.Run();
169 return callback_error_ == CACHE_STORAGE_OK;
172 bool StorageMatchAll(const GURL& origin, const GURL& url) {
173 scoped_ptr<ServiceWorkerFetchRequest> request(
174 new ServiceWorkerFetchRequest());
175 request->url = url;
176 base::RunLoop loop;
177 cache_manager_->MatchAllCaches(
178 origin, request.Pass(),
179 base::Bind(&CacheStorageManagerTest::CacheMatchCallback,
180 base::Unretained(this), base::Unretained(&loop)));
181 loop.Run();
183 return callback_error_ == CACHE_STORAGE_OK;
186 bool CachePut(const scoped_refptr<CacheStorageCache>& cache,
187 const GURL& url) {
188 ServiceWorkerFetchRequest request;
189 ServiceWorkerResponse response;
190 request.url = url;
191 response.url = url;
193 CacheStorageBatchOperation operation;
194 operation.operation_type = CACHE_STORAGE_CACHE_OPERATION_TYPE_PUT;
195 operation.request = request;
196 operation.response = response;
198 base::RunLoop loop;
199 cache->BatchOperation(
200 std::vector<CacheStorageBatchOperation>(1, operation),
201 base::Bind(&CacheStorageManagerTest::CachePutCallback,
202 base::Unretained(this), base::Unretained(&loop)));
203 loop.Run();
205 return callback_error_ == CACHE_STORAGE_OK;
208 bool CacheMatch(const scoped_refptr<CacheStorageCache>& cache,
209 const GURL& url) {
210 scoped_ptr<ServiceWorkerFetchRequest> request(
211 new ServiceWorkerFetchRequest());
212 request->url = url;
213 base::RunLoop loop;
214 cache->Match(request.Pass(),
215 base::Bind(&CacheStorageManagerTest::CacheMatchCallback,
216 base::Unretained(this), base::Unretained(&loop)));
217 loop.Run();
219 return callback_error_ == CACHE_STORAGE_OK;
222 CacheStorage* CacheStorageForOrigin(const GURL& origin) {
223 return cache_manager_->FindOrCreateCacheStorage(origin);
226 int64 GetOriginUsage(const GURL& origin) {
227 base::RunLoop loop;
228 cache_manager_->GetOriginUsage(
229 origin, base::Bind(&CacheStorageManagerTest::UsageCallback,
230 base::Unretained(this), base::Unretained(&loop)));
231 loop.Run();
232 return callback_usage_;
235 void UsageCallback(base::RunLoop* run_loop, int64 usage) {
236 callback_usage_ = usage;
237 run_loop->Quit();
240 std::vector<CacheStorageUsageInfo> GetAllOriginsUsage() {
241 base::RunLoop loop;
242 cache_manager_->GetAllOriginsUsage(
243 base::Bind(&CacheStorageManagerTest::AllOriginsUsageCallback,
244 base::Unretained(this), base::Unretained(&loop)));
245 loop.Run();
246 return callback_all_origins_usage_;
249 void AllOriginsUsageCallback(
250 base::RunLoop* run_loop,
251 const std::vector<CacheStorageUsageInfo>& usage) {
252 callback_all_origins_usage_ = usage;
253 run_loop->Quit();
256 protected:
257 TestBrowserContext browser_context_;
258 TestBrowserThreadBundle browser_thread_bundle_;
260 base::ScopedTempDir temp_dir_;
261 scoped_refptr<MockQuotaManagerProxy> quota_manager_proxy_;
262 scoped_ptr<CacheStorageManager> cache_manager_;
264 scoped_refptr<CacheStorageCache> callback_cache_;
265 int callback_bool_;
266 CacheStorageError callback_error_;
267 scoped_ptr<ServiceWorkerResponse> callback_cache_response_;
268 std::vector<std::string> callback_strings_;
270 const GURL origin1_;
271 const GURL origin2_;
273 int64 callback_usage_;
274 std::vector<CacheStorageUsageInfo> callback_all_origins_usage_;
276 private:
277 DISALLOW_COPY_AND_ASSIGN(CacheStorageManagerTest);
280 class CacheStorageManagerMemoryOnlyTest : public CacheStorageManagerTest {
281 public:
282 bool MemoryOnly() override { return true; }
285 class CacheStorageManagerTestP : public CacheStorageManagerTest,
286 public testing::WithParamInterface<bool> {
287 public:
288 bool MemoryOnly() override { return !GetParam(); }
291 TEST_F(CacheStorageManagerTest, TestsRunOnIOThread) {
292 EXPECT_TRUE(BrowserThread::CurrentlyOn(BrowserThread::IO));
295 TEST_P(CacheStorageManagerTestP, OpenCache) {
296 EXPECT_TRUE(Open(origin1_, "foo"));
299 TEST_P(CacheStorageManagerTestP, OpenTwoCaches) {
300 EXPECT_TRUE(Open(origin1_, "foo"));
301 EXPECT_TRUE(Open(origin1_, "bar"));
304 TEST_P(CacheStorageManagerTestP, CachePointersDiffer) {
305 EXPECT_TRUE(Open(origin1_, "foo"));
306 scoped_refptr<CacheStorageCache> cache = callback_cache_;
307 EXPECT_TRUE(Open(origin1_, "bar"));
308 EXPECT_NE(callback_cache_.get(), cache.get());
311 TEST_P(CacheStorageManagerTestP, Open2CachesSameNameDiffOrigins) {
312 EXPECT_TRUE(Open(origin1_, "foo"));
313 scoped_refptr<CacheStorageCache> cache = callback_cache_;
314 EXPECT_TRUE(Open(origin2_, "foo"));
315 EXPECT_NE(cache.get(), callback_cache_.get());
318 TEST_P(CacheStorageManagerTestP, OpenExistingCache) {
319 EXPECT_TRUE(Open(origin1_, "foo"));
320 scoped_refptr<CacheStorageCache> cache = callback_cache_;
321 EXPECT_TRUE(Open(origin1_, "foo"));
322 EXPECT_EQ(callback_cache_.get(), cache.get());
325 TEST_P(CacheStorageManagerTestP, HasCache) {
326 EXPECT_TRUE(Open(origin1_, "foo"));
327 EXPECT_TRUE(Has(origin1_, "foo"));
328 EXPECT_TRUE(callback_bool_);
331 TEST_P(CacheStorageManagerTestP, HasNonExistent) {
332 EXPECT_FALSE(Has(origin1_, "foo"));
335 TEST_P(CacheStorageManagerTestP, DeleteCache) {
336 EXPECT_TRUE(Open(origin1_, "foo"));
337 EXPECT_TRUE(Delete(origin1_, "foo"));
338 EXPECT_FALSE(Has(origin1_, "foo"));
341 TEST_P(CacheStorageManagerTestP, DeleteTwice) {
342 EXPECT_TRUE(Open(origin1_, "foo"));
343 EXPECT_TRUE(Delete(origin1_, "foo"));
344 EXPECT_FALSE(Delete(origin1_, "foo"));
345 EXPECT_EQ(CACHE_STORAGE_ERROR_NOT_FOUND, callback_error_);
348 TEST_P(CacheStorageManagerTestP, EmptyKeys) {
349 EXPECT_TRUE(Keys(origin1_));
350 EXPECT_TRUE(callback_strings_.empty());
353 TEST_P(CacheStorageManagerTestP, SomeKeys) {
354 EXPECT_TRUE(Open(origin1_, "foo"));
355 EXPECT_TRUE(Open(origin1_, "bar"));
356 EXPECT_TRUE(Open(origin2_, "baz"));
357 EXPECT_TRUE(Keys(origin1_));
358 EXPECT_EQ(2u, callback_strings_.size());
359 std::vector<std::string> expected_keys;
360 expected_keys.push_back("foo");
361 expected_keys.push_back("bar");
362 EXPECT_EQ(expected_keys, callback_strings_);
363 EXPECT_TRUE(Keys(origin2_));
364 EXPECT_EQ(1u, callback_strings_.size());
365 EXPECT_STREQ("baz", callback_strings_[0].c_str());
368 TEST_P(CacheStorageManagerTestP, DeletedKeysGone) {
369 EXPECT_TRUE(Open(origin1_, "foo"));
370 EXPECT_TRUE(Open(origin1_, "bar"));
371 EXPECT_TRUE(Open(origin2_, "baz"));
372 EXPECT_TRUE(Delete(origin1_, "bar"));
373 EXPECT_TRUE(Keys(origin1_));
374 EXPECT_EQ(1u, callback_strings_.size());
375 EXPECT_STREQ("foo", callback_strings_[0].c_str());
378 TEST_P(CacheStorageManagerTestP, StorageMatchEntryExists) {
379 EXPECT_TRUE(Open(origin1_, "foo"));
380 EXPECT_TRUE(CachePut(callback_cache_, GURL("http://example.com/foo")));
381 EXPECT_TRUE(StorageMatch(origin1_, "foo", GURL("http://example.com/foo")));
384 TEST_P(CacheStorageManagerTestP, StorageMatchNoEntry) {
385 EXPECT_TRUE(Open(origin1_, "foo"));
386 EXPECT_TRUE(CachePut(callback_cache_, GURL("http://example.com/foo")));
387 EXPECT_FALSE(StorageMatch(origin1_, "foo", GURL("http://example.com/bar")));
388 EXPECT_EQ(CACHE_STORAGE_ERROR_NOT_FOUND, callback_error_);
391 TEST_P(CacheStorageManagerTestP, StorageMatchNoCache) {
392 EXPECT_TRUE(Open(origin1_, "foo"));
393 EXPECT_TRUE(CachePut(callback_cache_, GURL("http://example.com/foo")));
394 EXPECT_FALSE(StorageMatch(origin1_, "bar", GURL("http://example.com/foo")));
395 EXPECT_EQ(CACHE_STORAGE_ERROR_NOT_FOUND, callback_error_);
398 TEST_P(CacheStorageManagerTestP, StorageMatchAllEntryExists) {
399 EXPECT_TRUE(Open(origin1_, "foo"));
400 EXPECT_TRUE(CachePut(callback_cache_, GURL("http://example.com/foo")));
401 EXPECT_TRUE(StorageMatchAll(origin1_, GURL("http://example.com/foo")));
404 TEST_P(CacheStorageManagerTestP, StorageMatchAllNoEntry) {
405 EXPECT_TRUE(Open(origin1_, "foo"));
406 EXPECT_TRUE(CachePut(callback_cache_, GURL("http://example.com/foo")));
407 EXPECT_FALSE(StorageMatchAll(origin1_, GURL("http://example.com/bar")));
408 EXPECT_EQ(CACHE_STORAGE_ERROR_NOT_FOUND, callback_error_);
411 TEST_P(CacheStorageManagerTestP, StorageMatchAllNoCaches) {
412 EXPECT_FALSE(StorageMatchAll(origin1_, GURL("http://example.com/foo")));
413 EXPECT_EQ(CACHE_STORAGE_ERROR_NOT_FOUND, callback_error_);
416 TEST_P(CacheStorageManagerTestP, StorageMatchAllEntryExistsTwice) {
417 EXPECT_TRUE(Open(origin1_, "foo"));
418 EXPECT_TRUE(CachePut(callback_cache_, GURL("http://example.com/foo")));
419 EXPECT_TRUE(Open(origin1_, "bar"));
420 EXPECT_TRUE(CachePut(callback_cache_, GURL("http://example.com/foo")));
422 EXPECT_TRUE(StorageMatchAll(origin1_, GURL("http://example.com/foo")));
425 TEST_P(CacheStorageManagerTestP, StorageMatchInOneOfMany) {
426 EXPECT_TRUE(Open(origin1_, "foo"));
427 EXPECT_TRUE(Open(origin1_, "bar"));
428 EXPECT_TRUE(CachePut(callback_cache_, GURL("http://example.com/foo")));
429 EXPECT_TRUE(Open(origin1_, "baz"));
431 EXPECT_TRUE(StorageMatchAll(origin1_, GURL("http://example.com/foo")));
434 TEST_P(CacheStorageManagerTestP, Chinese) {
435 EXPECT_TRUE(Open(origin1_, "你好"));
436 scoped_refptr<CacheStorageCache> cache = callback_cache_;
437 EXPECT_TRUE(Open(origin1_, "你好"));
438 EXPECT_EQ(callback_cache_.get(), cache.get());
439 EXPECT_TRUE(Keys(origin1_));
440 EXPECT_EQ(1u, callback_strings_.size());
441 EXPECT_STREQ("你好", callback_strings_[0].c_str());
444 TEST_F(CacheStorageManagerTest, EmptyKey) {
445 EXPECT_TRUE(Open(origin1_, ""));
446 scoped_refptr<CacheStorageCache> cache = callback_cache_;
447 EXPECT_TRUE(Open(origin1_, ""));
448 EXPECT_EQ(cache.get(), callback_cache_.get());
449 EXPECT_TRUE(Keys(origin1_));
450 EXPECT_EQ(1u, callback_strings_.size());
451 EXPECT_STREQ("", callback_strings_[0].c_str());
452 EXPECT_TRUE(Has(origin1_, ""));
453 EXPECT_TRUE(Delete(origin1_, ""));
454 EXPECT_TRUE(Keys(origin1_));
455 EXPECT_EQ(0u, callback_strings_.size());
458 TEST_F(CacheStorageManagerTest, DataPersists) {
459 EXPECT_TRUE(Open(origin1_, "foo"));
460 EXPECT_TRUE(Open(origin1_, "bar"));
461 EXPECT_TRUE(Open(origin1_, "baz"));
462 EXPECT_TRUE(Open(origin2_, "raz"));
463 EXPECT_TRUE(Delete(origin1_, "bar"));
464 quota_manager_proxy_->SimulateQuotaManagerDestroyed();
465 cache_manager_ = CacheStorageManager::Create(cache_manager_.get());
466 EXPECT_TRUE(Keys(origin1_));
467 EXPECT_EQ(2u, callback_strings_.size());
468 std::vector<std::string> expected_keys;
469 expected_keys.push_back("foo");
470 expected_keys.push_back("baz");
471 EXPECT_EQ(expected_keys, callback_strings_);
474 TEST_F(CacheStorageManagerMemoryOnlyTest, DataLostWhenMemoryOnly) {
475 EXPECT_TRUE(Open(origin1_, "foo"));
476 EXPECT_TRUE(Open(origin2_, "baz"));
477 quota_manager_proxy_->SimulateQuotaManagerDestroyed();
478 cache_manager_ = CacheStorageManager::Create(cache_manager_.get());
479 EXPECT_TRUE(Keys(origin1_));
480 EXPECT_EQ(0u, callback_strings_.size());
483 TEST_F(CacheStorageManagerTest, BadCacheName) {
484 // Since the implementation writes cache names to disk, ensure that we don't
485 // escape the directory.
486 const std::string bad_name = "../../../../../../../../../../../../../../foo";
487 EXPECT_TRUE(Open(origin1_, bad_name));
488 EXPECT_TRUE(Keys(origin1_));
489 EXPECT_EQ(1u, callback_strings_.size());
490 EXPECT_STREQ(bad_name.c_str(), callback_strings_[0].c_str());
493 TEST_F(CacheStorageManagerTest, BadOriginName) {
494 // Since the implementation writes origin names to disk, ensure that we don't
495 // escape the directory.
496 GURL bad_origin("http://../../../../../../../../../../../../../../foo");
497 EXPECT_TRUE(Open(bad_origin, "foo"));
498 EXPECT_TRUE(Keys(bad_origin));
499 EXPECT_EQ(1u, callback_strings_.size());
500 EXPECT_STREQ("foo", callback_strings_[0].c_str());
503 // With a persistent cache if the client drops its reference to a
504 // CacheStorageCache
505 // it should be deleted.
506 TEST_F(CacheStorageManagerTest, DropReference) {
507 EXPECT_TRUE(Open(origin1_, "foo"));
508 base::WeakPtr<CacheStorageCache> cache = callback_cache_->AsWeakPtr();
509 callback_cache_ = NULL;
510 EXPECT_TRUE(!cache);
513 // With a memory cache the cache can't be freed from memory until the client
514 // calls delete.
515 TEST_F(CacheStorageManagerMemoryOnlyTest, MemoryLosesReferenceOnlyAfterDelete) {
516 EXPECT_TRUE(Open(origin1_, "foo"));
517 base::WeakPtr<CacheStorageCache> cache = callback_cache_->AsWeakPtr();
518 callback_cache_ = NULL;
519 EXPECT_TRUE(cache);
520 EXPECT_TRUE(Delete(origin1_, "foo"));
521 EXPECT_FALSE(cache);
524 TEST_P(CacheStorageManagerTestP, DeleteBeforeRelease) {
525 EXPECT_TRUE(Open(origin1_, "foo"));
526 EXPECT_TRUE(Delete(origin1_, "foo"));
527 EXPECT_TRUE(callback_cache_->AsWeakPtr());
530 TEST_P(CacheStorageManagerTestP, OpenRunsSerially) {
531 EXPECT_FALSE(Delete(origin1_, "tmp")); // Init storage.
532 CacheStorage* cache_storage = CacheStorageForOrigin(origin1_);
533 cache_storage->StartAsyncOperationForTesting();
535 base::RunLoop open_loop;
536 cache_manager_->OpenCache(
537 origin1_, "foo",
538 base::Bind(&CacheStorageManagerTest::CacheAndErrorCallback,
539 base::Unretained(this), base::Unretained(&open_loop)));
541 base::RunLoop().RunUntilIdle();
542 EXPECT_FALSE(callback_cache_);
544 cache_storage->CompleteAsyncOperationForTesting();
545 open_loop.Run();
546 EXPECT_TRUE(callback_cache_);
549 TEST_P(CacheStorageManagerTestP, GetOriginUsage) {
550 EXPECT_EQ(0, GetOriginUsage(origin1_));
551 EXPECT_TRUE(Open(origin1_, "foo"));
552 EXPECT_TRUE(CachePut(callback_cache_, GURL("http://example.com/foo")));
553 EXPECT_LT(0, GetOriginUsage(origin1_));
554 EXPECT_EQ(0, GetOriginUsage(origin2_));
557 TEST_P(CacheStorageManagerTestP, GetAllOriginsUsage) {
558 EXPECT_EQ(0ULL, GetAllOriginsUsage().size());
559 EXPECT_TRUE(Open(origin1_, "foo"));
560 EXPECT_TRUE(CachePut(callback_cache_, GURL("http://example.com/foo")));
561 std::vector<CacheStorageUsageInfo> usage = GetAllOriginsUsage();
562 EXPECT_EQ(1ULL, usage.size());
563 const CacheStorageUsageInfo& info = usage[0];
564 EXPECT_EQ(origin1_, info.origin);
565 EXPECT_LT(0, info.total_size_bytes);
566 if (MemoryOnly())
567 EXPECT_TRUE(info.last_modified.is_null());
568 else
569 EXPECT_FALSE(info.last_modified.is_null());
572 TEST_F(CacheStorageManagerMemoryOnlyTest, MemoryBackedSize) {
573 CacheStorage* cache_storage = CacheStorageForOrigin(origin1_);
574 EXPECT_EQ(0, cache_storage->MemoryBackedSize());
576 EXPECT_TRUE(Open(origin1_, "foo"));
577 scoped_refptr<CacheStorageCache> foo_cache = callback_cache_;
578 EXPECT_TRUE(Open(origin1_, "bar"));
579 scoped_refptr<CacheStorageCache> bar_cache = callback_cache_;
580 EXPECT_EQ(0, cache_storage->MemoryBackedSize());
582 EXPECT_TRUE(CachePut(foo_cache, GURL("http://example.com/foo")));
583 EXPECT_LT(0, cache_storage->MemoryBackedSize());
584 int64 foo_size = cache_storage->MemoryBackedSize();
586 EXPECT_TRUE(CachePut(bar_cache, GURL("http://example.com/foo")));
587 EXPECT_EQ(foo_size * 2, cache_storage->MemoryBackedSize());
590 TEST_F(CacheStorageManagerTest, MemoryBackedSizePersistent) {
591 CacheStorage* cache_storage = CacheStorageForOrigin(origin1_);
592 EXPECT_EQ(0, cache_storage->MemoryBackedSize());
593 EXPECT_TRUE(Open(origin1_, "foo"));
594 EXPECT_TRUE(CachePut(callback_cache_, GURL("http://example.com/foo")));
595 EXPECT_EQ(0, cache_storage->MemoryBackedSize());
598 class CacheStorageMigrationTest : public CacheStorageManagerTest {
599 protected:
600 CacheStorageMigrationTest() : cache1_("foo"), cache2_("bar") {}
602 void SetUp() override {
603 CacheStorageManagerTest::SetUp();
605 // Populate a cache, then move it to the "legacy" location
606 // so that tests can verify the results of migration.
607 legacy_path_ = CacheStorageManager::ConstructLegacyOriginPath(
608 cache_manager_->root_path(), origin1_);
609 new_path_ = CacheStorageManager::ConstructOriginPath(
610 cache_manager_->root_path(), origin1_);
612 ASSERT_FALSE(base::DirectoryExists(legacy_path_));
613 ASSERT_FALSE(base::DirectoryExists(new_path_));
614 ASSERT_TRUE(Open(origin1_, cache1_));
615 ASSERT_TRUE(Open(origin1_, cache2_));
616 callback_cache_ = nullptr;
617 ASSERT_FALSE(base::DirectoryExists(legacy_path_));
618 ASSERT_TRUE(base::DirectoryExists(new_path_));
620 quota_manager_proxy_->SimulateQuotaManagerDestroyed();
621 cache_manager_ = CacheStorageManager::Create(cache_manager_.get());
623 ASSERT_TRUE(base::Move(new_path_, legacy_path_));
624 ASSERT_TRUE(base::DirectoryExists(legacy_path_));
625 ASSERT_FALSE(base::DirectoryExists(new_path_));
628 base::FilePath legacy_path_;
629 base::FilePath new_path_;
631 const std::string cache1_;
632 const std::string cache2_;
634 DISALLOW_COPY_AND_ASSIGN(CacheStorageMigrationTest);
637 TEST_F(CacheStorageMigrationTest, OpenCache) {
638 EXPECT_TRUE(Open(origin1_, cache1_));
639 EXPECT_FALSE(base::DirectoryExists(legacy_path_));
640 EXPECT_TRUE(base::DirectoryExists(new_path_));
642 EXPECT_TRUE(Keys(origin1_));
643 std::vector<std::string> expected_keys;
644 expected_keys.push_back(cache1_);
645 expected_keys.push_back(cache2_);
646 EXPECT_EQ(expected_keys, callback_strings_);
649 TEST_F(CacheStorageMigrationTest, DeleteCache) {
650 EXPECT_TRUE(Delete(origin1_, cache1_));
651 EXPECT_FALSE(base::DirectoryExists(legacy_path_));
652 EXPECT_TRUE(base::DirectoryExists(new_path_));
654 EXPECT_TRUE(Keys(origin1_));
655 std::vector<std::string> expected_keys;
656 expected_keys.push_back(cache2_);
657 EXPECT_EQ(expected_keys, callback_strings_);
660 TEST_F(CacheStorageMigrationTest, GetOriginUsage) {
661 EXPECT_GT(GetOriginUsage(origin1_), 0);
662 EXPECT_FALSE(base::DirectoryExists(legacy_path_));
663 EXPECT_TRUE(base::DirectoryExists(new_path_));
666 TEST_F(CacheStorageMigrationTest, MoveFailure) {
667 // Revert the migration.
668 ASSERT_TRUE(base::Move(legacy_path_, new_path_));
669 ASSERT_FALSE(base::DirectoryExists(legacy_path_));
670 ASSERT_TRUE(base::DirectoryExists(new_path_));
672 // Make a dummy legacy directory.
673 ASSERT_TRUE(base::CreateDirectory(legacy_path_));
675 // Ensure that migration doesn't stomp existing new directory,
676 // but does clean up old directory.
677 EXPECT_TRUE(Open(origin1_, cache1_));
678 EXPECT_FALSE(base::DirectoryExists(legacy_path_));
679 EXPECT_TRUE(base::DirectoryExists(new_path_));
681 EXPECT_TRUE(Keys(origin1_));
682 std::vector<std::string> expected_keys;
683 expected_keys.push_back(cache1_);
684 expected_keys.push_back(cache2_);
685 EXPECT_EQ(expected_keys, callback_strings_);
688 class CacheStorageQuotaClientTest : public CacheStorageManagerTest {
689 protected:
690 CacheStorageQuotaClientTest() {}
692 void SetUp() override {
693 CacheStorageManagerTest::SetUp();
694 quota_client_.reset(
695 new CacheStorageQuotaClient(cache_manager_->AsWeakPtr()));
698 void QuotaUsageCallback(base::RunLoop* run_loop, int64 usage) {
699 callback_quota_usage_ = usage;
700 run_loop->Quit();
703 void OriginsCallback(base::RunLoop* run_loop, const std::set<GURL>& origins) {
704 callback_origins_ = origins;
705 run_loop->Quit();
708 void DeleteOriginCallback(base::RunLoop* run_loop,
709 storage::QuotaStatusCode status) {
710 callback_status_ = status;
711 run_loop->Quit();
714 int64 QuotaGetOriginUsage(const GURL& origin) {
715 base::RunLoop loop;
716 quota_client_->GetOriginUsage(
717 origin, storage::kStorageTypeTemporary,
718 base::Bind(&CacheStorageQuotaClientTest::QuotaUsageCallback,
719 base::Unretained(this), base::Unretained(&loop)));
720 loop.Run();
721 return callback_quota_usage_;
724 size_t QuotaGetOriginsForType() {
725 base::RunLoop loop;
726 quota_client_->GetOriginsForType(
727 storage::kStorageTypeTemporary,
728 base::Bind(&CacheStorageQuotaClientTest::OriginsCallback,
729 base::Unretained(this), base::Unretained(&loop)));
730 loop.Run();
731 return callback_origins_.size();
734 size_t QuotaGetOriginsForHost(const std::string& host) {
735 base::RunLoop loop;
736 quota_client_->GetOriginsForHost(
737 storage::kStorageTypeTemporary, host,
738 base::Bind(&CacheStorageQuotaClientTest::OriginsCallback,
739 base::Unretained(this), base::Unretained(&loop)));
740 loop.Run();
741 return callback_origins_.size();
744 bool QuotaDeleteOriginData(const GURL& origin) {
745 base::RunLoop loop;
746 quota_client_->DeleteOriginData(
747 origin, storage::kStorageTypeTemporary,
748 base::Bind(&CacheStorageQuotaClientTest::DeleteOriginCallback,
749 base::Unretained(this), base::Unretained(&loop)));
750 loop.Run();
751 return callback_status_ == storage::kQuotaStatusOk;
754 bool QuotaDoesSupport(storage::StorageType type) {
755 return quota_client_->DoesSupport(type);
758 scoped_ptr<CacheStorageQuotaClient> quota_client_;
760 storage::QuotaStatusCode callback_status_;
761 int64 callback_quota_usage_ = 0;
762 std::set<GURL> callback_origins_;
764 DISALLOW_COPY_AND_ASSIGN(CacheStorageQuotaClientTest);
767 class CacheStorageQuotaClientTestP : public CacheStorageQuotaClientTest,
768 public testing::WithParamInterface<bool> {
769 bool MemoryOnly() override { return !GetParam(); }
772 TEST_P(CacheStorageQuotaClientTestP, QuotaID) {
773 EXPECT_EQ(storage::QuotaClient::kServiceWorkerCache, quota_client_->id());
776 TEST_P(CacheStorageQuotaClientTestP, QuotaGetOriginUsage) {
777 EXPECT_EQ(0, QuotaGetOriginUsage(origin1_));
778 EXPECT_TRUE(Open(origin1_, "foo"));
779 EXPECT_TRUE(CachePut(callback_cache_, GURL("http://example.com/foo")));
780 EXPECT_LT(0, QuotaGetOriginUsage(origin1_));
783 TEST_P(CacheStorageQuotaClientTestP, QuotaGetOriginsForType) {
784 EXPECT_EQ(0u, QuotaGetOriginsForType());
785 EXPECT_TRUE(Open(origin1_, "foo"));
786 EXPECT_TRUE(Open(origin1_, "bar"));
787 EXPECT_TRUE(Open(origin2_, "foo"));
788 EXPECT_EQ(2u, QuotaGetOriginsForType());
791 TEST_P(CacheStorageQuotaClientTestP, QuotaGetOriginsForHost) {
792 EXPECT_EQ(0u, QuotaGetOriginsForHost("example.com"));
793 EXPECT_TRUE(Open(GURL("http://example.com:8080"), "foo"));
794 EXPECT_TRUE(Open(GURL("http://example.com:9000"), "foo"));
795 EXPECT_TRUE(Open(GURL("ftp://example.com"), "foo"));
796 EXPECT_TRUE(Open(GURL("http://example2.com"), "foo"));
797 EXPECT_EQ(3u, QuotaGetOriginsForHost("example.com"));
798 EXPECT_EQ(1u, QuotaGetOriginsForHost("example2.com"));
799 EXPECT_TRUE(callback_origins_.find(GURL("http://example2.com")) !=
800 callback_origins_.end());
801 EXPECT_EQ(0u, QuotaGetOriginsForHost("unknown.com"));
804 TEST_P(CacheStorageQuotaClientTestP, QuotaDeleteOriginData) {
805 EXPECT_TRUE(Open(origin1_, "foo"));
806 // Call put to test that initialized caches are properly deleted too.
807 EXPECT_TRUE(CachePut(callback_cache_, GURL("http://example.com/foo")));
808 EXPECT_TRUE(Open(origin1_, "bar"));
809 EXPECT_TRUE(Open(origin2_, "baz"));
811 EXPECT_TRUE(QuotaDeleteOriginData(origin1_));
813 EXPECT_FALSE(Has(origin1_, "foo"));
814 EXPECT_FALSE(Has(origin1_, "bar"));
815 EXPECT_TRUE(Has(origin2_, "baz"));
816 EXPECT_TRUE(Open(origin1_, "foo"));
819 TEST_P(CacheStorageQuotaClientTestP, QuotaDeleteEmptyOrigin) {
820 EXPECT_TRUE(QuotaDeleteOriginData(origin1_));
823 TEST_P(CacheStorageQuotaClientTestP, QuotaDoesSupport) {
824 EXPECT_TRUE(QuotaDoesSupport(storage::kStorageTypeTemporary));
825 EXPECT_FALSE(QuotaDoesSupport(storage::kStorageTypePersistent));
826 EXPECT_FALSE(QuotaDoesSupport(storage::kStorageTypeSyncable));
827 EXPECT_FALSE(QuotaDoesSupport(storage::kStorageTypeQuotaNotManaged));
828 EXPECT_FALSE(QuotaDoesSupport(storage::kStorageTypeUnknown));
831 INSTANTIATE_TEST_CASE_P(CacheStorageManagerTests,
832 CacheStorageManagerTestP,
833 ::testing::Values(false, true));
835 INSTANTIATE_TEST_CASE_P(CacheStorageQuotaClientTests,
836 CacheStorageQuotaClientTestP,
837 ::testing::Values(false, true));
839 } // namespace content