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/message_loop/message_loop_proxy.h"
11 #include "base/run_loop.h"
12 #include "base/stl_util.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/test/test_browser_context.h"
18 #include "content/public/test/test_browser_thread_bundle.h"
19 #include "net/url_request/url_request_context_getter.h"
20 #include "storage/browser/blob/blob_storage_context.h"
21 #include "storage/browser/quota/quota_manager_proxy.h"
22 #include "testing/gtest/include/gtest/gtest.h"
26 class CacheStorageManagerTest
: public testing::Test
{
28 CacheStorageManagerTest()
29 : browser_thread_bundle_(TestBrowserThreadBundle::IO_MAINLOOP
),
30 callback_bool_(false),
31 callback_error_(CacheStorage::CACHE_STORAGE_ERROR_NO_ERROR
),
32 callback_cache_error_(CacheStorageCache::ERROR_TYPE_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::MessageLoopProxy::current().get());
45 net::URLRequestContext
* url_request_context
=
46 browser_context_
.GetRequestContext()->GetURLRequestContext();
48 cache_manager_
= CacheStorageManager::Create(
49 base::FilePath(), base::MessageLoopProxy::current(),
50 quota_manager_proxy_
);
52 ASSERT_TRUE(temp_dir_
.CreateUniqueTempDir());
53 cache_manager_
= CacheStorageManager::Create(
54 temp_dir_
.path(), base::MessageLoopProxy::current(),
55 quota_manager_proxy_
);
58 cache_manager_
->SetBlobParametersForCache(
59 url_request_context
, blob_storage_context
->context()->AsWeakPtr());
62 void TearDown() override
{
63 quota_manager_proxy_
->SimulateQuotaManagerDestroyed();
64 base::RunLoop().RunUntilIdle();
67 virtual bool MemoryOnly() { return false; }
69 void BoolAndErrorCallback(base::RunLoop
* run_loop
,
71 CacheStorage::CacheStorageError error
) {
72 callback_bool_
= value
;
73 callback_error_
= error
;
77 void CacheAndErrorCallback(base::RunLoop
* run_loop
,
78 const scoped_refptr
<CacheStorageCache
>& cache
,
79 CacheStorage::CacheStorageError error
) {
80 callback_cache_
= cache
;
81 callback_error_
= error
;
85 void StringsAndErrorCallback(base::RunLoop
* run_loop
,
86 const std::vector
<std::string
>& strings
,
87 CacheStorage::CacheStorageError error
) {
88 callback_strings_
= strings
;
89 callback_error_
= error
;
93 void CachePutCallback(base::RunLoop
* run_loop
,
94 CacheStorageCache::ErrorType error
,
95 scoped_ptr
<ServiceWorkerResponse
> response
,
96 scoped_ptr
<storage::BlobDataHandle
> blob_data_handle
) {
97 callback_cache_error_
= error
;
101 void CacheMatchCallback(
102 base::RunLoop
* run_loop
,
103 CacheStorageCache::ErrorType error
,
104 scoped_ptr
<ServiceWorkerResponse
> response
,
105 scoped_ptr
<storage::BlobDataHandle
> blob_data_handle
) {
106 callback_cache_error_
= error
;
107 callback_cache_response_
= response
.Pass();
108 // Deliberately drop the data handle as only the url is being tested.
112 bool Open(const GURL
& origin
, const std::string
& cache_name
) {
113 scoped_ptr
<base::RunLoop
> loop(new base::RunLoop());
114 cache_manager_
->OpenCache(
116 base::Bind(&CacheStorageManagerTest::CacheAndErrorCallback
,
117 base::Unretained(this), base::Unretained(loop
.get())));
120 bool error
= callback_error_
!= CacheStorage::CACHE_STORAGE_ERROR_NO_ERROR
;
122 EXPECT_TRUE(!callback_cache_
.get());
124 EXPECT_TRUE(callback_cache_
.get());
128 bool Has(const GURL
& origin
, const std::string
& cache_name
) {
129 scoped_ptr
<base::RunLoop
> loop(new base::RunLoop());
130 cache_manager_
->HasCache(
132 base::Bind(&CacheStorageManagerTest::BoolAndErrorCallback
,
133 base::Unretained(this), base::Unretained(loop
.get())));
136 return callback_bool_
;
139 bool Delete(const GURL
& origin
, const std::string
& cache_name
) {
140 scoped_ptr
<base::RunLoop
> loop(new base::RunLoop());
141 cache_manager_
->DeleteCache(
143 base::Bind(&CacheStorageManagerTest::BoolAndErrorCallback
,
144 base::Unretained(this), base::Unretained(loop
.get())));
147 return callback_bool_
;
150 bool Keys(const GURL
& origin
) {
151 scoped_ptr
<base::RunLoop
> loop(new base::RunLoop());
152 cache_manager_
->EnumerateCaches(
154 base::Bind(&CacheStorageManagerTest::StringsAndErrorCallback
,
155 base::Unretained(this), base::Unretained(loop
.get())));
158 bool error
= callback_error_
!= CacheStorage::CACHE_STORAGE_ERROR_NO_ERROR
;
162 bool StorageMatch(const GURL
& origin
,
163 const std::string
& cache_name
,
165 scoped_ptr
<ServiceWorkerFetchRequest
> request(
166 new ServiceWorkerFetchRequest());
168 scoped_ptr
<base::RunLoop
> loop(new base::RunLoop());
169 cache_manager_
->MatchCache(
170 origin
, cache_name
, request
.Pass(),
171 base::Bind(&CacheStorageManagerTest::CacheMatchCallback
,
172 base::Unretained(this), base::Unretained(loop
.get())));
175 bool error
= callback_cache_error_
!= CacheStorageCache::ERROR_TYPE_OK
;
179 bool StorageMatchAll(const GURL
& origin
, const GURL
& url
) {
180 scoped_ptr
<ServiceWorkerFetchRequest
> request(
181 new ServiceWorkerFetchRequest());
183 scoped_ptr
<base::RunLoop
> loop(new base::RunLoop());
184 cache_manager_
->MatchAllCaches(
185 origin
, request
.Pass(),
186 base::Bind(&CacheStorageManagerTest::CacheMatchCallback
,
187 base::Unretained(this), base::Unretained(loop
.get())));
190 bool error
= callback_cache_error_
!= CacheStorageCache::ERROR_TYPE_OK
;
194 bool CachePut(const scoped_refptr
<CacheStorageCache
>& cache
,
196 scoped_ptr
<ServiceWorkerFetchRequest
> request(
197 new ServiceWorkerFetchRequest());
198 scoped_ptr
<ServiceWorkerResponse
> response(new ServiceWorkerResponse());
201 scoped_ptr
<base::RunLoop
> loop(new base::RunLoop());
203 request
.Pass(), response
.Pass(),
204 base::Bind(&CacheStorageManagerTest::CachePutCallback
,
205 base::Unretained(this), base::Unretained(loop
.get())));
208 bool error
= callback_cache_error_
!= CacheStorageCache::ERROR_TYPE_OK
;
212 bool CacheMatch(const scoped_refptr
<CacheStorageCache
>& cache
,
214 scoped_ptr
<ServiceWorkerFetchRequest
> request(
215 new ServiceWorkerFetchRequest());
217 scoped_ptr
<base::RunLoop
> loop(new base::RunLoop());
220 base::Bind(&CacheStorageManagerTest::CacheMatchCallback
,
221 base::Unretained(this), base::Unretained(loop
.get())));
224 bool error
= callback_cache_error_
!= CacheStorageCache::ERROR_TYPE_OK
;
228 CacheStorage
* CacheStorageForOrigin(const GURL
& origin
) {
229 return cache_manager_
->FindOrCreateCacheStorage(origin
);
233 TestBrowserContext browser_context_
;
234 TestBrowserThreadBundle browser_thread_bundle_
;
236 base::ScopedTempDir temp_dir_
;
237 scoped_refptr
<MockQuotaManagerProxy
> quota_manager_proxy_
;
238 scoped_ptr
<CacheStorageManager
> cache_manager_
;
240 scoped_refptr
<CacheStorageCache
> callback_cache_
;
242 CacheStorage::CacheStorageError callback_error_
;
243 CacheStorageCache::ErrorType callback_cache_error_
;
244 scoped_ptr
<ServiceWorkerResponse
> callback_cache_response_
;
245 std::vector
<std::string
> callback_strings_
;
251 DISALLOW_COPY_AND_ASSIGN(CacheStorageManagerTest
);
254 class CacheStorageManagerMemoryOnlyTest
: public CacheStorageManagerTest
{
255 bool MemoryOnly() override
{ return true; }
258 class CacheStorageManagerTestP
: public CacheStorageManagerTest
,
259 public testing::WithParamInterface
<bool> {
260 bool MemoryOnly() override
{ return !GetParam(); }
263 TEST_F(CacheStorageManagerTest
, TestsRunOnIOThread
) {
264 EXPECT_TRUE(BrowserThread::CurrentlyOn(BrowserThread::IO
));
267 TEST_P(CacheStorageManagerTestP
, OpenCache
) {
268 EXPECT_TRUE(Open(origin1_
, "foo"));
271 TEST_P(CacheStorageManagerTestP
, OpenTwoCaches
) {
272 EXPECT_TRUE(Open(origin1_
, "foo"));
273 EXPECT_TRUE(Open(origin1_
, "bar"));
276 TEST_P(CacheStorageManagerTestP
, CachePointersDiffer
) {
277 EXPECT_TRUE(Open(origin1_
, "foo"));
278 scoped_refptr
<CacheStorageCache
> cache
= callback_cache_
;
279 EXPECT_TRUE(Open(origin1_
, "bar"));
280 EXPECT_NE(callback_cache_
.get(), cache
.get());
283 TEST_P(CacheStorageManagerTestP
, Open2CachesSameNameDiffOrigins
) {
284 EXPECT_TRUE(Open(origin1_
, "foo"));
285 scoped_refptr
<CacheStorageCache
> cache
= callback_cache_
;
286 EXPECT_TRUE(Open(origin2_
, "foo"));
287 EXPECT_NE(cache
.get(), callback_cache_
.get());
290 TEST_P(CacheStorageManagerTestP
, OpenExistingCache
) {
291 EXPECT_TRUE(Open(origin1_
, "foo"));
292 scoped_refptr
<CacheStorageCache
> cache
= callback_cache_
;
293 EXPECT_TRUE(Open(origin1_
, "foo"));
294 EXPECT_EQ(callback_cache_
.get(), cache
.get());
297 TEST_P(CacheStorageManagerTestP
, HasCache
) {
298 EXPECT_TRUE(Open(origin1_
, "foo"));
299 EXPECT_TRUE(Has(origin1_
, "foo"));
300 EXPECT_TRUE(callback_bool_
);
303 TEST_P(CacheStorageManagerTestP
, HasNonExistent
) {
304 EXPECT_FALSE(Has(origin1_
, "foo"));
307 TEST_P(CacheStorageManagerTestP
, DeleteCache
) {
308 EXPECT_TRUE(Open(origin1_
, "foo"));
309 EXPECT_TRUE(Delete(origin1_
, "foo"));
310 EXPECT_FALSE(Has(origin1_
, "foo"));
313 TEST_P(CacheStorageManagerTestP
, DeleteTwice
) {
314 EXPECT_TRUE(Open(origin1_
, "foo"));
315 EXPECT_TRUE(Delete(origin1_
, "foo"));
316 EXPECT_FALSE(Delete(origin1_
, "foo"));
317 EXPECT_EQ(CacheStorage::CACHE_STORAGE_ERROR_NOT_FOUND
, callback_error_
);
320 TEST_P(CacheStorageManagerTestP
, EmptyKeys
) {
321 EXPECT_TRUE(Keys(origin1_
));
322 EXPECT_TRUE(callback_strings_
.empty());
325 TEST_P(CacheStorageManagerTestP
, SomeKeys
) {
326 EXPECT_TRUE(Open(origin1_
, "foo"));
327 EXPECT_TRUE(Open(origin1_
, "bar"));
328 EXPECT_TRUE(Open(origin2_
, "baz"));
329 EXPECT_TRUE(Keys(origin1_
));
330 EXPECT_EQ(2u, callback_strings_
.size());
331 std::vector
<std::string
> expected_keys
;
332 expected_keys
.push_back("foo");
333 expected_keys
.push_back("bar");
334 EXPECT_EQ(expected_keys
, callback_strings_
);
335 EXPECT_TRUE(Keys(origin2_
));
336 EXPECT_EQ(1u, callback_strings_
.size());
337 EXPECT_STREQ("baz", callback_strings_
[0].c_str());
340 TEST_P(CacheStorageManagerTestP
, DeletedKeysGone
) {
341 EXPECT_TRUE(Open(origin1_
, "foo"));
342 EXPECT_TRUE(Open(origin1_
, "bar"));
343 EXPECT_TRUE(Open(origin2_
, "baz"));
344 EXPECT_TRUE(Delete(origin1_
, "bar"));
345 EXPECT_TRUE(Keys(origin1_
));
346 EXPECT_EQ(1u, callback_strings_
.size());
347 EXPECT_STREQ("foo", callback_strings_
[0].c_str());
350 TEST_P(CacheStorageManagerTestP
, StorageMatchEntryExists
) {
351 EXPECT_TRUE(Open(origin1_
, "foo"));
352 EXPECT_TRUE(CachePut(callback_cache_
, GURL("http://example.com/foo")));
353 EXPECT_TRUE(StorageMatch(origin1_
, "foo", GURL("http://example.com/foo")));
356 TEST_P(CacheStorageManagerTestP
, StorageMatchNoEntry
) {
357 EXPECT_TRUE(Open(origin1_
, "foo"));
358 EXPECT_TRUE(CachePut(callback_cache_
, GURL("http://example.com/foo")));
359 EXPECT_FALSE(StorageMatch(origin1_
, "foo", GURL("http://example.com/bar")));
360 EXPECT_EQ(CacheStorageCache::ERROR_TYPE_NOT_FOUND
, callback_cache_error_
);
363 TEST_P(CacheStorageManagerTestP
, StorageMatchNoCache
) {
364 EXPECT_TRUE(Open(origin1_
, "foo"));
365 EXPECT_TRUE(CachePut(callback_cache_
, GURL("http://example.com/foo")));
366 EXPECT_FALSE(StorageMatch(origin1_
, "bar", GURL("http://example.com/foo")));
367 EXPECT_EQ(CacheStorageCache::ERROR_TYPE_NOT_FOUND
, callback_cache_error_
);
370 TEST_P(CacheStorageManagerTestP
, StorageMatchAllEntryExists
) {
371 EXPECT_TRUE(Open(origin1_
, "foo"));
372 EXPECT_TRUE(CachePut(callback_cache_
, GURL("http://example.com/foo")));
373 EXPECT_TRUE(StorageMatchAll(origin1_
, GURL("http://example.com/foo")));
376 TEST_P(CacheStorageManagerTestP
, StorageMatchAllNoEntry
) {
377 EXPECT_TRUE(Open(origin1_
, "foo"));
378 EXPECT_TRUE(CachePut(callback_cache_
, GURL("http://example.com/foo")));
379 EXPECT_FALSE(StorageMatchAll(origin1_
, GURL("http://example.com/bar")));
380 EXPECT_EQ(CacheStorageCache::ERROR_TYPE_NOT_FOUND
, callback_cache_error_
);
383 TEST_P(CacheStorageManagerTestP
, StorageMatchAllNoCaches
) {
384 EXPECT_FALSE(StorageMatchAll(origin1_
, GURL("http://example.com/foo")));
385 EXPECT_EQ(CacheStorageCache::ERROR_TYPE_NOT_FOUND
, callback_cache_error_
);
388 TEST_P(CacheStorageManagerTestP
, StorageMatchAllEntryExistsTwice
) {
389 EXPECT_TRUE(Open(origin1_
, "foo"));
390 EXPECT_TRUE(CachePut(callback_cache_
, GURL("http://example.com/foo")));
391 EXPECT_TRUE(Open(origin1_
, "bar"));
392 EXPECT_TRUE(CachePut(callback_cache_
, GURL("http://example.com/foo")));
394 EXPECT_TRUE(StorageMatchAll(origin1_
, GURL("http://example.com/foo")));
397 TEST_P(CacheStorageManagerTestP
, StorageMatchInOneOfMany
) {
398 EXPECT_TRUE(Open(origin1_
, "foo"));
399 EXPECT_TRUE(Open(origin1_
, "bar"));
400 EXPECT_TRUE(CachePut(callback_cache_
, GURL("http://example.com/foo")));
401 EXPECT_TRUE(Open(origin1_
, "baz"));
403 EXPECT_TRUE(StorageMatchAll(origin1_
, GURL("http://example.com/foo")));
406 TEST_P(CacheStorageManagerTestP
, Chinese
) {
407 EXPECT_TRUE(Open(origin1_
, "ä½ å¥½"));
408 scoped_refptr
<CacheStorageCache
> cache
= callback_cache_
;
409 EXPECT_TRUE(Open(origin1_
, "ä½ å¥½"));
410 EXPECT_EQ(callback_cache_
.get(), cache
.get());
411 EXPECT_TRUE(Keys(origin1_
));
412 EXPECT_EQ(1u, callback_strings_
.size());
413 EXPECT_STREQ("ä½ å¥½", callback_strings_
[0].c_str());
416 TEST_F(CacheStorageManagerTest
, EmptyKey
) {
417 EXPECT_TRUE(Open(origin1_
, ""));
418 scoped_refptr
<CacheStorageCache
> cache
= callback_cache_
;
419 EXPECT_TRUE(Open(origin1_
, ""));
420 EXPECT_EQ(cache
.get(), callback_cache_
.get());
421 EXPECT_TRUE(Keys(origin1_
));
422 EXPECT_EQ(1u, callback_strings_
.size());
423 EXPECT_STREQ("", callback_strings_
[0].c_str());
424 EXPECT_TRUE(Has(origin1_
, ""));
425 EXPECT_TRUE(Delete(origin1_
, ""));
426 EXPECT_TRUE(Keys(origin1_
));
427 EXPECT_EQ(0u, callback_strings_
.size());
430 TEST_F(CacheStorageManagerTest
, DataPersists
) {
431 EXPECT_TRUE(Open(origin1_
, "foo"));
432 EXPECT_TRUE(Open(origin1_
, "bar"));
433 EXPECT_TRUE(Open(origin1_
, "baz"));
434 EXPECT_TRUE(Open(origin2_
, "raz"));
435 EXPECT_TRUE(Delete(origin1_
, "bar"));
436 quota_manager_proxy_
->SimulateQuotaManagerDestroyed();
437 cache_manager_
= CacheStorageManager::Create(cache_manager_
.get());
438 EXPECT_TRUE(Keys(origin1_
));
439 EXPECT_EQ(2u, callback_strings_
.size());
440 std::vector
<std::string
> expected_keys
;
441 expected_keys
.push_back("foo");
442 expected_keys
.push_back("baz");
443 EXPECT_EQ(expected_keys
, callback_strings_
);
446 TEST_F(CacheStorageManagerMemoryOnlyTest
, DataLostWhenMemoryOnly
) {
447 EXPECT_TRUE(Open(origin1_
, "foo"));
448 EXPECT_TRUE(Open(origin2_
, "baz"));
449 quota_manager_proxy_
->SimulateQuotaManagerDestroyed();
450 cache_manager_
= CacheStorageManager::Create(cache_manager_
.get());
451 EXPECT_TRUE(Keys(origin1_
));
452 EXPECT_EQ(0u, callback_strings_
.size());
455 TEST_F(CacheStorageManagerTest
, BadCacheName
) {
456 // Since the implementation writes cache names to disk, ensure that we don't
457 // escape the directory.
458 const std::string bad_name
= "../../../../../../../../../../../../../../foo";
459 EXPECT_TRUE(Open(origin1_
, bad_name
));
460 EXPECT_TRUE(Keys(origin1_
));
461 EXPECT_EQ(1u, callback_strings_
.size());
462 EXPECT_STREQ(bad_name
.c_str(), callback_strings_
[0].c_str());
465 TEST_F(CacheStorageManagerTest
, BadOriginName
) {
466 // Since the implementation writes origin names to disk, ensure that we don't
467 // escape the directory.
468 GURL
bad_origin("http://../../../../../../../../../../../../../../foo");
469 EXPECT_TRUE(Open(bad_origin
, "foo"));
470 EXPECT_TRUE(Keys(bad_origin
));
471 EXPECT_EQ(1u, callback_strings_
.size());
472 EXPECT_STREQ("foo", callback_strings_
[0].c_str());
475 // With a persistent cache if the client drops its reference to a
477 // it should be deleted.
478 TEST_F(CacheStorageManagerTest
, DropReference
) {
479 EXPECT_TRUE(Open(origin1_
, "foo"));
480 base::WeakPtr
<CacheStorageCache
> cache
= callback_cache_
->AsWeakPtr();
481 callback_cache_
= NULL
;
485 // With a memory cache the cache can't be freed from memory until the client
487 TEST_F(CacheStorageManagerMemoryOnlyTest
, MemoryLosesReferenceOnlyAfterDelete
) {
488 EXPECT_TRUE(Open(origin1_
, "foo"));
489 base::WeakPtr
<CacheStorageCache
> cache
= callback_cache_
->AsWeakPtr();
490 callback_cache_
= NULL
;
492 EXPECT_TRUE(Delete(origin1_
, "foo"));
496 TEST_P(CacheStorageManagerTestP
, DeleteBeforeRelease
) {
497 EXPECT_TRUE(Open(origin1_
, "foo"));
498 EXPECT_TRUE(Delete(origin1_
, "foo"));
499 EXPECT_TRUE(callback_cache_
->AsWeakPtr());
502 TEST_P(CacheStorageManagerTestP
, OpenRunsSerially
) {
503 EXPECT_FALSE(Delete(origin1_
, "tmp")); // Init storage.
504 CacheStorage
* cache_storage
= CacheStorageForOrigin(origin1_
);
505 cache_storage
->StartAsyncOperationForTesting();
507 scoped_ptr
<base::RunLoop
> open_loop(new base::RunLoop());
508 cache_manager_
->OpenCache(
510 base::Bind(&CacheStorageManagerTest::CacheAndErrorCallback
,
511 base::Unretained(this), base::Unretained(open_loop
.get())));
513 base::RunLoop().RunUntilIdle();
514 EXPECT_FALSE(callback_cache_
);
516 cache_storage
->CompleteAsyncOperationForTesting();
518 EXPECT_TRUE(callback_cache_
);
521 TEST_F(CacheStorageManagerMemoryOnlyTest
, MemoryBackedSize
) {
522 CacheStorage
* cache_storage
= CacheStorageForOrigin(origin1_
);
523 EXPECT_EQ(0, cache_storage
->MemoryBackedSize());
525 EXPECT_TRUE(Open(origin1_
, "foo"));
526 scoped_refptr
<CacheStorageCache
> foo_cache
= callback_cache_
;
527 EXPECT_TRUE(Open(origin1_
, "bar"));
528 scoped_refptr
<CacheStorageCache
> bar_cache
= callback_cache_
;
529 EXPECT_EQ(0, cache_storage
->MemoryBackedSize());
531 EXPECT_TRUE(CachePut(foo_cache
, GURL("http://example.com/foo")));
532 EXPECT_LT(0, cache_storage
->MemoryBackedSize());
533 int64 foo_size
= cache_storage
->MemoryBackedSize();
535 EXPECT_TRUE(CachePut(bar_cache
, GURL("http://example.com/foo")));
536 EXPECT_EQ(foo_size
* 2, cache_storage
->MemoryBackedSize());
539 TEST_F(CacheStorageManagerTest
, MemoryBackedSizePersistent
) {
540 CacheStorage
* cache_storage
= CacheStorageForOrigin(origin1_
);
541 EXPECT_EQ(0, cache_storage
->MemoryBackedSize());
542 EXPECT_TRUE(Open(origin1_
, "foo"));
543 EXPECT_TRUE(CachePut(callback_cache_
, GURL("http://example.com/foo")));
544 EXPECT_EQ(0, cache_storage
->MemoryBackedSize());
547 class CacheStorageMigrationTest
: public CacheStorageManagerTest
{
549 CacheStorageMigrationTest() : cache1_("foo"), cache2_("bar") {}
551 void SetUp() override
{
552 CacheStorageManagerTest::SetUp();
554 // Populate a cache, then move it to the "legacy" location
555 // so that tests can verify the results of migration.
556 legacy_path_
= CacheStorageManager::ConstructLegacyOriginPath(
557 cache_manager_
->root_path(), origin1_
);
558 new_path_
= CacheStorageManager::ConstructOriginPath(
559 cache_manager_
->root_path(), origin1_
);
561 ASSERT_FALSE(base::DirectoryExists(legacy_path_
));
562 ASSERT_FALSE(base::DirectoryExists(new_path_
));
563 ASSERT_TRUE(Open(origin1_
, cache1_
));
564 ASSERT_TRUE(Open(origin1_
, cache2_
));
565 callback_cache_
= nullptr;
566 ASSERT_FALSE(base::DirectoryExists(legacy_path_
));
567 ASSERT_TRUE(base::DirectoryExists(new_path_
));
569 quota_manager_proxy_
->SimulateQuotaManagerDestroyed();
570 cache_manager_
= CacheStorageManager::Create(cache_manager_
.get());
572 ASSERT_TRUE(base::Move(new_path_
, legacy_path_
));
573 ASSERT_TRUE(base::DirectoryExists(legacy_path_
));
574 ASSERT_FALSE(base::DirectoryExists(new_path_
));
577 int64
GetOriginUsage(const GURL
& origin
) {
578 scoped_ptr
<base::RunLoop
> loop(new base::RunLoop());
579 cache_manager_
->GetOriginUsage(
581 base::Bind(&CacheStorageMigrationTest::UsageCallback
,
582 base::Unretained(this), base::Unretained(loop
.get())));
584 return callback_usage_
;
587 void UsageCallback(base::RunLoop
* run_loop
, int64 usage
) {
588 callback_usage_
= usage
;
592 base::FilePath legacy_path_
;
593 base::FilePath new_path_
;
595 const std::string cache1_
;
596 const std::string cache2_
;
598 int64 callback_usage_
;
600 DISALLOW_COPY_AND_ASSIGN(CacheStorageMigrationTest
);
603 TEST_F(CacheStorageMigrationTest
, OpenCache
) {
604 EXPECT_TRUE(Open(origin1_
, cache1_
));
605 EXPECT_FALSE(base::DirectoryExists(legacy_path_
));
606 EXPECT_TRUE(base::DirectoryExists(new_path_
));
608 EXPECT_TRUE(Keys(origin1_
));
609 std::vector
<std::string
> expected_keys
;
610 expected_keys
.push_back(cache1_
);
611 expected_keys
.push_back(cache2_
);
612 EXPECT_EQ(expected_keys
, callback_strings_
);
615 TEST_F(CacheStorageMigrationTest
, DeleteCache
) {
616 EXPECT_TRUE(Delete(origin1_
, cache1_
));
617 EXPECT_FALSE(base::DirectoryExists(legacy_path_
));
618 EXPECT_TRUE(base::DirectoryExists(new_path_
));
620 EXPECT_TRUE(Keys(origin1_
));
621 std::vector
<std::string
> expected_keys
;
622 expected_keys
.push_back(cache2_
);
623 EXPECT_EQ(expected_keys
, callback_strings_
);
626 TEST_F(CacheStorageMigrationTest
, GetOriginUsage
) {
627 EXPECT_GT(GetOriginUsage(origin1_
), 0);
628 EXPECT_FALSE(base::DirectoryExists(legacy_path_
));
629 EXPECT_TRUE(base::DirectoryExists(new_path_
));
632 TEST_F(CacheStorageMigrationTest
, MoveFailure
) {
633 // Revert the migration.
634 ASSERT_TRUE(base::Move(legacy_path_
, new_path_
));
635 ASSERT_FALSE(base::DirectoryExists(legacy_path_
));
636 ASSERT_TRUE(base::DirectoryExists(new_path_
));
638 // Make a dummy legacy directory.
639 ASSERT_TRUE(base::CreateDirectory(legacy_path_
));
641 // Ensure that migration doesn't stomp existing new directory,
642 // but does clean up old directory.
643 EXPECT_TRUE(Open(origin1_
, cache1_
));
644 EXPECT_FALSE(base::DirectoryExists(legacy_path_
));
645 EXPECT_TRUE(base::DirectoryExists(new_path_
));
647 EXPECT_TRUE(Keys(origin1_
));
648 std::vector
<std::string
> expected_keys
;
649 expected_keys
.push_back(cache1_
);
650 expected_keys
.push_back(cache2_
);
651 EXPECT_EQ(expected_keys
, callback_strings_
);
654 class CacheStorageQuotaClientTest
: public CacheStorageManagerTest
{
656 CacheStorageQuotaClientTest() {}
658 void SetUp() override
{
659 CacheStorageManagerTest::SetUp();
661 new CacheStorageQuotaClient(cache_manager_
->AsWeakPtr()));
664 void UsageCallback(base::RunLoop
* run_loop
, int64 usage
) {
665 callback_usage_
= usage
;
669 void OriginsCallback(base::RunLoop
* run_loop
, const std::set
<GURL
>& origins
) {
670 callback_origins_
= origins
;
674 void DeleteOriginCallback(base::RunLoop
* run_loop
,
675 storage::QuotaStatusCode status
) {
676 callback_status_
= status
;
680 int64
QuotaGetOriginUsage(const GURL
& origin
) {
681 scoped_ptr
<base::RunLoop
> loop(new base::RunLoop());
682 quota_client_
->GetOriginUsage(
683 origin
, storage::kStorageTypeTemporary
,
684 base::Bind(&CacheStorageQuotaClientTest::UsageCallback
,
685 base::Unretained(this), base::Unretained(loop
.get())));
687 return callback_usage_
;
690 size_t QuotaGetOriginsForType() {
691 scoped_ptr
<base::RunLoop
> loop(new base::RunLoop());
692 quota_client_
->GetOriginsForType(
693 storage::kStorageTypeTemporary
,
694 base::Bind(&CacheStorageQuotaClientTest::OriginsCallback
,
695 base::Unretained(this), base::Unretained(loop
.get())));
697 return callback_origins_
.size();
700 size_t QuotaGetOriginsForHost(const std::string
& host
) {
701 scoped_ptr
<base::RunLoop
> loop(new base::RunLoop());
702 quota_client_
->GetOriginsForHost(
703 storage::kStorageTypeTemporary
, host
,
704 base::Bind(&CacheStorageQuotaClientTest::OriginsCallback
,
705 base::Unretained(this), base::Unretained(loop
.get())));
707 return callback_origins_
.size();
710 bool QuotaDeleteOriginData(const GURL
& origin
) {
711 scoped_ptr
<base::RunLoop
> loop(new base::RunLoop());
712 quota_client_
->DeleteOriginData(
713 origin
, storage::kStorageTypeTemporary
,
714 base::Bind(&CacheStorageQuotaClientTest::DeleteOriginCallback
,
715 base::Unretained(this), base::Unretained(loop
.get())));
717 return callback_status_
== storage::kQuotaStatusOk
;
720 bool QuotaDoesSupport(storage::StorageType type
) {
721 return quota_client_
->DoesSupport(type
);
724 scoped_ptr
<CacheStorageQuotaClient
> quota_client_
;
726 storage::QuotaStatusCode callback_status_
;
727 int64 callback_usage_
;
728 std::set
<GURL
> callback_origins_
;
730 DISALLOW_COPY_AND_ASSIGN(CacheStorageQuotaClientTest
);
733 class CacheStorageQuotaClientTestP
: public CacheStorageQuotaClientTest
,
734 public testing::WithParamInterface
<bool> {
735 bool MemoryOnly() override
{ return !GetParam(); }
738 TEST_P(CacheStorageQuotaClientTestP
, QuotaID
) {
739 EXPECT_EQ(storage::QuotaClient::kServiceWorkerCache
, quota_client_
->id());
742 TEST_P(CacheStorageQuotaClientTestP
, QuotaGetOriginUsage
) {
743 EXPECT_EQ(0, QuotaGetOriginUsage(origin1_
));
744 EXPECT_TRUE(Open(origin1_
, "foo"));
745 EXPECT_TRUE(CachePut(callback_cache_
, GURL("http://example.com/foo")));
746 EXPECT_LT(0, QuotaGetOriginUsage(origin1_
));
749 TEST_P(CacheStorageQuotaClientTestP
, QuotaGetOriginsForType
) {
750 EXPECT_EQ(0u, QuotaGetOriginsForType());
751 EXPECT_TRUE(Open(origin1_
, "foo"));
752 EXPECT_TRUE(Open(origin1_
, "bar"));
753 EXPECT_TRUE(Open(origin2_
, "foo"));
754 EXPECT_EQ(2u, QuotaGetOriginsForType());
757 TEST_P(CacheStorageQuotaClientTestP
, QuotaGetOriginsForHost
) {
758 EXPECT_EQ(0u, QuotaGetOriginsForHost("example.com"));
759 EXPECT_TRUE(Open(GURL("http://example.com:8080"), "foo"));
760 EXPECT_TRUE(Open(GURL("http://example.com:9000"), "foo"));
761 EXPECT_TRUE(Open(GURL("ftp://example.com"), "foo"));
762 EXPECT_TRUE(Open(GURL("http://example2.com"), "foo"));
763 EXPECT_EQ(3u, QuotaGetOriginsForHost("example.com"));
764 EXPECT_EQ(1u, QuotaGetOriginsForHost("example2.com"));
765 EXPECT_TRUE(callback_origins_
.find(GURL("http://example2.com")) !=
766 callback_origins_
.end());
767 EXPECT_EQ(0u, QuotaGetOriginsForHost("unknown.com"));
770 TEST_P(CacheStorageQuotaClientTestP
, QuotaDeleteOriginData
) {
771 EXPECT_TRUE(Open(origin1_
, "foo"));
772 // Call put to test that initialized caches are properly deleted too.
773 EXPECT_TRUE(CachePut(callback_cache_
, GURL("http://example.com/foo")));
774 EXPECT_TRUE(Open(origin1_
, "bar"));
775 EXPECT_TRUE(Open(origin2_
, "baz"));
777 EXPECT_TRUE(QuotaDeleteOriginData(origin1_
));
779 EXPECT_FALSE(Has(origin1_
, "foo"));
780 EXPECT_FALSE(Has(origin1_
, "bar"));
781 EXPECT_TRUE(Has(origin2_
, "baz"));
782 EXPECT_TRUE(Open(origin1_
, "foo"));
785 TEST_P(CacheStorageQuotaClientTestP
, QuotaDeleteEmptyOrigin
) {
786 EXPECT_TRUE(QuotaDeleteOriginData(origin1_
));
789 TEST_P(CacheStorageQuotaClientTestP
, QuotaDoesSupport
) {
790 EXPECT_TRUE(QuotaDoesSupport(storage::kStorageTypeTemporary
));
791 EXPECT_FALSE(QuotaDoesSupport(storage::kStorageTypePersistent
));
792 EXPECT_FALSE(QuotaDoesSupport(storage::kStorageTypeSyncable
));
793 EXPECT_FALSE(QuotaDoesSupport(storage::kStorageTypeQuotaNotManaged
));
794 EXPECT_FALSE(QuotaDoesSupport(storage::kStorageTypeUnknown
));
797 INSTANTIATE_TEST_CASE_P(CacheStorageManagerTests
,
798 CacheStorageManagerTestP
,
799 ::testing::Values(false, true));
801 INSTANTIATE_TEST_CASE_P(CacheStorageQuotaClientTests
,
802 CacheStorageQuotaClientTestP
,
803 ::testing::Values(false, true));
805 } // namespace content