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_(CACHE_STORAGE_OK
),
32 origin1_("http://example1.com"),
33 origin2_("http://example2.com") {}
35 void SetUp() override
{
36 ChromeBlobStorageContext
* blob_storage_context(
37 ChromeBlobStorageContext::GetFor(&browser_context_
));
38 // Wait for ChromeBlobStorageContext to finish initializing.
39 base::RunLoop().RunUntilIdle();
41 quota_manager_proxy_
= new MockQuotaManagerProxy(
42 nullptr, base::MessageLoopProxy::current().get());
44 net::URLRequestContext
* url_request_context
=
45 browser_context_
.GetRequestContext()->GetURLRequestContext();
47 cache_manager_
= CacheStorageManager::Create(
48 base::FilePath(), base::MessageLoopProxy::current(),
49 quota_manager_proxy_
);
51 ASSERT_TRUE(temp_dir_
.CreateUniqueTempDir());
52 cache_manager_
= CacheStorageManager::Create(
53 temp_dir_
.path(), base::MessageLoopProxy::current(),
54 quota_manager_proxy_
);
57 cache_manager_
->SetBlobParametersForCache(
58 url_request_context
, 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
,
70 CacheStorageError error
) {
71 callback_bool_
= value
;
72 callback_error_
= error
;
76 void CacheAndErrorCallback(base::RunLoop
* run_loop
,
77 const scoped_refptr
<CacheStorageCache
>& cache
,
78 CacheStorageError error
) {
79 callback_cache_
= cache
;
80 callback_error_
= error
;
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
;
92 void CachePutCallback(base::RunLoop
* run_loop
, CacheStorageError error
) {
93 callback_error_
= error
;
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.
108 bool Open(const GURL
& origin
, const std::string
& cache_name
) {
109 scoped_ptr
<base::RunLoop
> loop(new base::RunLoop());
110 cache_manager_
->OpenCache(
112 base::Bind(&CacheStorageManagerTest::CacheAndErrorCallback
,
113 base::Unretained(this), base::Unretained(loop
.get())));
116 bool error
= callback_error_
!= CACHE_STORAGE_OK
;
118 EXPECT_TRUE(!callback_cache_
.get());
120 EXPECT_TRUE(callback_cache_
.get());
124 bool Has(const GURL
& origin
, const std::string
& cache_name
) {
125 scoped_ptr
<base::RunLoop
> loop(new base::RunLoop());
126 cache_manager_
->HasCache(
128 base::Bind(&CacheStorageManagerTest::BoolAndErrorCallback
,
129 base::Unretained(this), base::Unretained(loop
.get())));
132 return callback_bool_
;
135 bool Delete(const GURL
& origin
, const std::string
& cache_name
) {
136 scoped_ptr
<base::RunLoop
> loop(new base::RunLoop());
137 cache_manager_
->DeleteCache(
139 base::Bind(&CacheStorageManagerTest::BoolAndErrorCallback
,
140 base::Unretained(this), base::Unretained(loop
.get())));
143 return callback_bool_
;
146 bool Keys(const GURL
& origin
) {
147 scoped_ptr
<base::RunLoop
> loop(new base::RunLoop());
148 cache_manager_
->EnumerateCaches(
150 base::Bind(&CacheStorageManagerTest::StringsAndErrorCallback
,
151 base::Unretained(this), base::Unretained(loop
.get())));
154 return callback_error_
== CACHE_STORAGE_OK
;
157 bool StorageMatch(const GURL
& origin
,
158 const std::string
& cache_name
,
160 scoped_ptr
<ServiceWorkerFetchRequest
> request(
161 new ServiceWorkerFetchRequest());
163 scoped_ptr
<base::RunLoop
> loop(new base::RunLoop());
164 cache_manager_
->MatchCache(
165 origin
, cache_name
, request
.Pass(),
166 base::Bind(&CacheStorageManagerTest::CacheMatchCallback
,
167 base::Unretained(this), base::Unretained(loop
.get())));
170 return callback_error_
== CACHE_STORAGE_OK
;
173 bool StorageMatchAll(const GURL
& origin
, const GURL
& url
) {
174 scoped_ptr
<ServiceWorkerFetchRequest
> request(
175 new ServiceWorkerFetchRequest());
177 scoped_ptr
<base::RunLoop
> loop(new base::RunLoop());
178 cache_manager_
->MatchAllCaches(
179 origin
, request
.Pass(),
180 base::Bind(&CacheStorageManagerTest::CacheMatchCallback
,
181 base::Unretained(this), base::Unretained(loop
.get())));
184 return callback_error_
== CACHE_STORAGE_OK
;
187 bool CachePut(const scoped_refptr
<CacheStorageCache
>& cache
,
189 ServiceWorkerFetchRequest request
;
190 ServiceWorkerResponse response
;
194 CacheStorageBatchOperation operation
;
195 operation
.operation_type
= CACHE_STORAGE_CACHE_OPERATION_TYPE_PUT
;
196 operation
.request
= request
;
197 operation
.response
= response
;
199 scoped_ptr
<base::RunLoop
> loop(new base::RunLoop());
200 cache
->BatchOperation(
201 std::vector
<CacheStorageBatchOperation
>(1, operation
),
202 base::Bind(&CacheStorageManagerTest::CachePutCallback
,
203 base::Unretained(this), base::Unretained(loop
.get())));
206 return callback_error_
== CACHE_STORAGE_OK
;
209 bool CacheMatch(const scoped_refptr
<CacheStorageCache
>& cache
,
211 scoped_ptr
<ServiceWorkerFetchRequest
> request(
212 new ServiceWorkerFetchRequest());
214 scoped_ptr
<base::RunLoop
> loop(new base::RunLoop());
217 base::Bind(&CacheStorageManagerTest::CacheMatchCallback
,
218 base::Unretained(this), base::Unretained(loop
.get())));
221 return callback_error_
== CACHE_STORAGE_OK
;
224 CacheStorage
* CacheStorageForOrigin(const GURL
& origin
) {
225 return cache_manager_
->FindOrCreateCacheStorage(origin
);
229 TestBrowserContext browser_context_
;
230 TestBrowserThreadBundle browser_thread_bundle_
;
232 base::ScopedTempDir temp_dir_
;
233 scoped_refptr
<MockQuotaManagerProxy
> quota_manager_proxy_
;
234 scoped_ptr
<CacheStorageManager
> cache_manager_
;
236 scoped_refptr
<CacheStorageCache
> callback_cache_
;
238 CacheStorageError callback_error_
;
239 scoped_ptr
<ServiceWorkerResponse
> callback_cache_response_
;
240 std::vector
<std::string
> callback_strings_
;
246 DISALLOW_COPY_AND_ASSIGN(CacheStorageManagerTest
);
249 class CacheStorageManagerMemoryOnlyTest
: public CacheStorageManagerTest
{
250 bool MemoryOnly() override
{ return true; }
253 class CacheStorageManagerTestP
: public CacheStorageManagerTest
,
254 public testing::WithParamInterface
<bool> {
255 bool MemoryOnly() override
{ return !GetParam(); }
258 TEST_F(CacheStorageManagerTest
, TestsRunOnIOThread
) {
259 EXPECT_TRUE(BrowserThread::CurrentlyOn(BrowserThread::IO
));
262 TEST_P(CacheStorageManagerTestP
, OpenCache
) {
263 EXPECT_TRUE(Open(origin1_
, "foo"));
266 TEST_P(CacheStorageManagerTestP
, OpenTwoCaches
) {
267 EXPECT_TRUE(Open(origin1_
, "foo"));
268 EXPECT_TRUE(Open(origin1_
, "bar"));
271 TEST_P(CacheStorageManagerTestP
, CachePointersDiffer
) {
272 EXPECT_TRUE(Open(origin1_
, "foo"));
273 scoped_refptr
<CacheStorageCache
> cache
= callback_cache_
;
274 EXPECT_TRUE(Open(origin1_
, "bar"));
275 EXPECT_NE(callback_cache_
.get(), cache
.get());
278 TEST_P(CacheStorageManagerTestP
, Open2CachesSameNameDiffOrigins
) {
279 EXPECT_TRUE(Open(origin1_
, "foo"));
280 scoped_refptr
<CacheStorageCache
> cache
= callback_cache_
;
281 EXPECT_TRUE(Open(origin2_
, "foo"));
282 EXPECT_NE(cache
.get(), callback_cache_
.get());
285 TEST_P(CacheStorageManagerTestP
, OpenExistingCache
) {
286 EXPECT_TRUE(Open(origin1_
, "foo"));
287 scoped_refptr
<CacheStorageCache
> cache
= callback_cache_
;
288 EXPECT_TRUE(Open(origin1_
, "foo"));
289 EXPECT_EQ(callback_cache_
.get(), cache
.get());
292 TEST_P(CacheStorageManagerTestP
, HasCache
) {
293 EXPECT_TRUE(Open(origin1_
, "foo"));
294 EXPECT_TRUE(Has(origin1_
, "foo"));
295 EXPECT_TRUE(callback_bool_
);
298 TEST_P(CacheStorageManagerTestP
, HasNonExistent
) {
299 EXPECT_FALSE(Has(origin1_
, "foo"));
302 TEST_P(CacheStorageManagerTestP
, DeleteCache
) {
303 EXPECT_TRUE(Open(origin1_
, "foo"));
304 EXPECT_TRUE(Delete(origin1_
, "foo"));
305 EXPECT_FALSE(Has(origin1_
, "foo"));
308 TEST_P(CacheStorageManagerTestP
, DeleteTwice
) {
309 EXPECT_TRUE(Open(origin1_
, "foo"));
310 EXPECT_TRUE(Delete(origin1_
, "foo"));
311 EXPECT_FALSE(Delete(origin1_
, "foo"));
312 EXPECT_EQ(CACHE_STORAGE_ERROR_NOT_FOUND
, callback_error_
);
315 TEST_P(CacheStorageManagerTestP
, EmptyKeys
) {
316 EXPECT_TRUE(Keys(origin1_
));
317 EXPECT_TRUE(callback_strings_
.empty());
320 TEST_P(CacheStorageManagerTestP
, SomeKeys
) {
321 EXPECT_TRUE(Open(origin1_
, "foo"));
322 EXPECT_TRUE(Open(origin1_
, "bar"));
323 EXPECT_TRUE(Open(origin2_
, "baz"));
324 EXPECT_TRUE(Keys(origin1_
));
325 EXPECT_EQ(2u, callback_strings_
.size());
326 std::vector
<std::string
> expected_keys
;
327 expected_keys
.push_back("foo");
328 expected_keys
.push_back("bar");
329 EXPECT_EQ(expected_keys
, callback_strings_
);
330 EXPECT_TRUE(Keys(origin2_
));
331 EXPECT_EQ(1u, callback_strings_
.size());
332 EXPECT_STREQ("baz", callback_strings_
[0].c_str());
335 TEST_P(CacheStorageManagerTestP
, DeletedKeysGone
) {
336 EXPECT_TRUE(Open(origin1_
, "foo"));
337 EXPECT_TRUE(Open(origin1_
, "bar"));
338 EXPECT_TRUE(Open(origin2_
, "baz"));
339 EXPECT_TRUE(Delete(origin1_
, "bar"));
340 EXPECT_TRUE(Keys(origin1_
));
341 EXPECT_EQ(1u, callback_strings_
.size());
342 EXPECT_STREQ("foo", callback_strings_
[0].c_str());
345 TEST_P(CacheStorageManagerTestP
, StorageMatchEntryExists
) {
346 EXPECT_TRUE(Open(origin1_
, "foo"));
347 EXPECT_TRUE(CachePut(callback_cache_
, GURL("http://example.com/foo")));
348 EXPECT_TRUE(StorageMatch(origin1_
, "foo", GURL("http://example.com/foo")));
351 TEST_P(CacheStorageManagerTestP
, StorageMatchNoEntry
) {
352 EXPECT_TRUE(Open(origin1_
, "foo"));
353 EXPECT_TRUE(CachePut(callback_cache_
, GURL("http://example.com/foo")));
354 EXPECT_FALSE(StorageMatch(origin1_
, "foo", GURL("http://example.com/bar")));
355 EXPECT_EQ(CACHE_STORAGE_ERROR_NOT_FOUND
, callback_error_
);
358 TEST_P(CacheStorageManagerTestP
, StorageMatchNoCache
) {
359 EXPECT_TRUE(Open(origin1_
, "foo"));
360 EXPECT_TRUE(CachePut(callback_cache_
, GURL("http://example.com/foo")));
361 EXPECT_FALSE(StorageMatch(origin1_
, "bar", GURL("http://example.com/foo")));
362 EXPECT_EQ(CACHE_STORAGE_ERROR_NOT_FOUND
, callback_error_
);
365 TEST_P(CacheStorageManagerTestP
, StorageMatchAllEntryExists
) {
366 EXPECT_TRUE(Open(origin1_
, "foo"));
367 EXPECT_TRUE(CachePut(callback_cache_
, GURL("http://example.com/foo")));
368 EXPECT_TRUE(StorageMatchAll(origin1_
, GURL("http://example.com/foo")));
371 TEST_P(CacheStorageManagerTestP
, StorageMatchAllNoEntry
) {
372 EXPECT_TRUE(Open(origin1_
, "foo"));
373 EXPECT_TRUE(CachePut(callback_cache_
, GURL("http://example.com/foo")));
374 EXPECT_FALSE(StorageMatchAll(origin1_
, GURL("http://example.com/bar")));
375 EXPECT_EQ(CACHE_STORAGE_ERROR_NOT_FOUND
, callback_error_
);
378 TEST_P(CacheStorageManagerTestP
, StorageMatchAllNoCaches
) {
379 EXPECT_FALSE(StorageMatchAll(origin1_
, GURL("http://example.com/foo")));
380 EXPECT_EQ(CACHE_STORAGE_ERROR_NOT_FOUND
, callback_error_
);
383 TEST_P(CacheStorageManagerTestP
, StorageMatchAllEntryExistsTwice
) {
384 EXPECT_TRUE(Open(origin1_
, "foo"));
385 EXPECT_TRUE(CachePut(callback_cache_
, GURL("http://example.com/foo")));
386 EXPECT_TRUE(Open(origin1_
, "bar"));
387 EXPECT_TRUE(CachePut(callback_cache_
, GURL("http://example.com/foo")));
389 EXPECT_TRUE(StorageMatchAll(origin1_
, GURL("http://example.com/foo")));
392 TEST_P(CacheStorageManagerTestP
, StorageMatchInOneOfMany
) {
393 EXPECT_TRUE(Open(origin1_
, "foo"));
394 EXPECT_TRUE(Open(origin1_
, "bar"));
395 EXPECT_TRUE(CachePut(callback_cache_
, GURL("http://example.com/foo")));
396 EXPECT_TRUE(Open(origin1_
, "baz"));
398 EXPECT_TRUE(StorageMatchAll(origin1_
, GURL("http://example.com/foo")));
401 TEST_P(CacheStorageManagerTestP
, Chinese
) {
402 EXPECT_TRUE(Open(origin1_
, "ä½ å¥½"));
403 scoped_refptr
<CacheStorageCache
> cache
= callback_cache_
;
404 EXPECT_TRUE(Open(origin1_
, "ä½ å¥½"));
405 EXPECT_EQ(callback_cache_
.get(), cache
.get());
406 EXPECT_TRUE(Keys(origin1_
));
407 EXPECT_EQ(1u, callback_strings_
.size());
408 EXPECT_STREQ("ä½ å¥½", callback_strings_
[0].c_str());
411 TEST_F(CacheStorageManagerTest
, EmptyKey
) {
412 EXPECT_TRUE(Open(origin1_
, ""));
413 scoped_refptr
<CacheStorageCache
> cache
= callback_cache_
;
414 EXPECT_TRUE(Open(origin1_
, ""));
415 EXPECT_EQ(cache
.get(), callback_cache_
.get());
416 EXPECT_TRUE(Keys(origin1_
));
417 EXPECT_EQ(1u, callback_strings_
.size());
418 EXPECT_STREQ("", callback_strings_
[0].c_str());
419 EXPECT_TRUE(Has(origin1_
, ""));
420 EXPECT_TRUE(Delete(origin1_
, ""));
421 EXPECT_TRUE(Keys(origin1_
));
422 EXPECT_EQ(0u, callback_strings_
.size());
425 TEST_F(CacheStorageManagerTest
, DataPersists
) {
426 EXPECT_TRUE(Open(origin1_
, "foo"));
427 EXPECT_TRUE(Open(origin1_
, "bar"));
428 EXPECT_TRUE(Open(origin1_
, "baz"));
429 EXPECT_TRUE(Open(origin2_
, "raz"));
430 EXPECT_TRUE(Delete(origin1_
, "bar"));
431 quota_manager_proxy_
->SimulateQuotaManagerDestroyed();
432 cache_manager_
= CacheStorageManager::Create(cache_manager_
.get());
433 EXPECT_TRUE(Keys(origin1_
));
434 EXPECT_EQ(2u, callback_strings_
.size());
435 std::vector
<std::string
> expected_keys
;
436 expected_keys
.push_back("foo");
437 expected_keys
.push_back("baz");
438 EXPECT_EQ(expected_keys
, callback_strings_
);
441 TEST_F(CacheStorageManagerMemoryOnlyTest
, DataLostWhenMemoryOnly
) {
442 EXPECT_TRUE(Open(origin1_
, "foo"));
443 EXPECT_TRUE(Open(origin2_
, "baz"));
444 quota_manager_proxy_
->SimulateQuotaManagerDestroyed();
445 cache_manager_
= CacheStorageManager::Create(cache_manager_
.get());
446 EXPECT_TRUE(Keys(origin1_
));
447 EXPECT_EQ(0u, callback_strings_
.size());
450 TEST_F(CacheStorageManagerTest
, BadCacheName
) {
451 // Since the implementation writes cache names to disk, ensure that we don't
452 // escape the directory.
453 const std::string bad_name
= "../../../../../../../../../../../../../../foo";
454 EXPECT_TRUE(Open(origin1_
, bad_name
));
455 EXPECT_TRUE(Keys(origin1_
));
456 EXPECT_EQ(1u, callback_strings_
.size());
457 EXPECT_STREQ(bad_name
.c_str(), callback_strings_
[0].c_str());
460 TEST_F(CacheStorageManagerTest
, BadOriginName
) {
461 // Since the implementation writes origin names to disk, ensure that we don't
462 // escape the directory.
463 GURL
bad_origin("http://../../../../../../../../../../../../../../foo");
464 EXPECT_TRUE(Open(bad_origin
, "foo"));
465 EXPECT_TRUE(Keys(bad_origin
));
466 EXPECT_EQ(1u, callback_strings_
.size());
467 EXPECT_STREQ("foo", callback_strings_
[0].c_str());
470 // With a persistent cache if the client drops its reference to a
472 // it should be deleted.
473 TEST_F(CacheStorageManagerTest
, DropReference
) {
474 EXPECT_TRUE(Open(origin1_
, "foo"));
475 base::WeakPtr
<CacheStorageCache
> cache
= callback_cache_
->AsWeakPtr();
476 callback_cache_
= NULL
;
480 // With a memory cache the cache can't be freed from memory until the client
482 TEST_F(CacheStorageManagerMemoryOnlyTest
, MemoryLosesReferenceOnlyAfterDelete
) {
483 EXPECT_TRUE(Open(origin1_
, "foo"));
484 base::WeakPtr
<CacheStorageCache
> cache
= callback_cache_
->AsWeakPtr();
485 callback_cache_
= NULL
;
487 EXPECT_TRUE(Delete(origin1_
, "foo"));
491 TEST_P(CacheStorageManagerTestP
, DeleteBeforeRelease
) {
492 EXPECT_TRUE(Open(origin1_
, "foo"));
493 EXPECT_TRUE(Delete(origin1_
, "foo"));
494 EXPECT_TRUE(callback_cache_
->AsWeakPtr());
497 TEST_P(CacheStorageManagerTestP
, OpenRunsSerially
) {
498 EXPECT_FALSE(Delete(origin1_
, "tmp")); // Init storage.
499 CacheStorage
* cache_storage
= CacheStorageForOrigin(origin1_
);
500 cache_storage
->StartAsyncOperationForTesting();
502 scoped_ptr
<base::RunLoop
> open_loop(new base::RunLoop());
503 cache_manager_
->OpenCache(
505 base::Bind(&CacheStorageManagerTest::CacheAndErrorCallback
,
506 base::Unretained(this), base::Unretained(open_loop
.get())));
508 base::RunLoop().RunUntilIdle();
509 EXPECT_FALSE(callback_cache_
);
511 cache_storage
->CompleteAsyncOperationForTesting();
513 EXPECT_TRUE(callback_cache_
);
516 TEST_F(CacheStorageManagerMemoryOnlyTest
, MemoryBackedSize
) {
517 CacheStorage
* cache_storage
= CacheStorageForOrigin(origin1_
);
518 EXPECT_EQ(0, cache_storage
->MemoryBackedSize());
520 EXPECT_TRUE(Open(origin1_
, "foo"));
521 scoped_refptr
<CacheStorageCache
> foo_cache
= callback_cache_
;
522 EXPECT_TRUE(Open(origin1_
, "bar"));
523 scoped_refptr
<CacheStorageCache
> bar_cache
= callback_cache_
;
524 EXPECT_EQ(0, cache_storage
->MemoryBackedSize());
526 EXPECT_TRUE(CachePut(foo_cache
, GURL("http://example.com/foo")));
527 EXPECT_LT(0, cache_storage
->MemoryBackedSize());
528 int64 foo_size
= cache_storage
->MemoryBackedSize();
530 EXPECT_TRUE(CachePut(bar_cache
, GURL("http://example.com/foo")));
531 EXPECT_EQ(foo_size
* 2, cache_storage
->MemoryBackedSize());
534 TEST_F(CacheStorageManagerTest
, MemoryBackedSizePersistent
) {
535 CacheStorage
* cache_storage
= CacheStorageForOrigin(origin1_
);
536 EXPECT_EQ(0, cache_storage
->MemoryBackedSize());
537 EXPECT_TRUE(Open(origin1_
, "foo"));
538 EXPECT_TRUE(CachePut(callback_cache_
, GURL("http://example.com/foo")));
539 EXPECT_EQ(0, cache_storage
->MemoryBackedSize());
542 class CacheStorageMigrationTest
: public CacheStorageManagerTest
{
544 CacheStorageMigrationTest() : cache1_("foo"), cache2_("bar") {}
546 void SetUp() override
{
547 CacheStorageManagerTest::SetUp();
549 // Populate a cache, then move it to the "legacy" location
550 // so that tests can verify the results of migration.
551 legacy_path_
= CacheStorageManager::ConstructLegacyOriginPath(
552 cache_manager_
->root_path(), origin1_
);
553 new_path_
= CacheStorageManager::ConstructOriginPath(
554 cache_manager_
->root_path(), origin1_
);
556 ASSERT_FALSE(base::DirectoryExists(legacy_path_
));
557 ASSERT_FALSE(base::DirectoryExists(new_path_
));
558 ASSERT_TRUE(Open(origin1_
, cache1_
));
559 ASSERT_TRUE(Open(origin1_
, cache2_
));
560 callback_cache_
= nullptr;
561 ASSERT_FALSE(base::DirectoryExists(legacy_path_
));
562 ASSERT_TRUE(base::DirectoryExists(new_path_
));
564 quota_manager_proxy_
->SimulateQuotaManagerDestroyed();
565 cache_manager_
= CacheStorageManager::Create(cache_manager_
.get());
567 ASSERT_TRUE(base::Move(new_path_
, legacy_path_
));
568 ASSERT_TRUE(base::DirectoryExists(legacy_path_
));
569 ASSERT_FALSE(base::DirectoryExists(new_path_
));
572 int64
GetOriginUsage(const GURL
& origin
) {
573 scoped_ptr
<base::RunLoop
> loop(new base::RunLoop());
574 cache_manager_
->GetOriginUsage(
576 base::Bind(&CacheStorageMigrationTest::UsageCallback
,
577 base::Unretained(this), base::Unretained(loop
.get())));
579 return callback_usage_
;
582 void UsageCallback(base::RunLoop
* run_loop
, int64 usage
) {
583 callback_usage_
= usage
;
587 base::FilePath legacy_path_
;
588 base::FilePath new_path_
;
590 const std::string cache1_
;
591 const std::string cache2_
;
593 int64 callback_usage_
;
595 DISALLOW_COPY_AND_ASSIGN(CacheStorageMigrationTest
);
598 TEST_F(CacheStorageMigrationTest
, OpenCache
) {
599 EXPECT_TRUE(Open(origin1_
, cache1_
));
600 EXPECT_FALSE(base::DirectoryExists(legacy_path_
));
601 EXPECT_TRUE(base::DirectoryExists(new_path_
));
603 EXPECT_TRUE(Keys(origin1_
));
604 std::vector
<std::string
> expected_keys
;
605 expected_keys
.push_back(cache1_
);
606 expected_keys
.push_back(cache2_
);
607 EXPECT_EQ(expected_keys
, callback_strings_
);
610 TEST_F(CacheStorageMigrationTest
, DeleteCache
) {
611 EXPECT_TRUE(Delete(origin1_
, cache1_
));
612 EXPECT_FALSE(base::DirectoryExists(legacy_path_
));
613 EXPECT_TRUE(base::DirectoryExists(new_path_
));
615 EXPECT_TRUE(Keys(origin1_
));
616 std::vector
<std::string
> expected_keys
;
617 expected_keys
.push_back(cache2_
);
618 EXPECT_EQ(expected_keys
, callback_strings_
);
621 TEST_F(CacheStorageMigrationTest
, GetOriginUsage
) {
622 EXPECT_GT(GetOriginUsage(origin1_
), 0);
623 EXPECT_FALSE(base::DirectoryExists(legacy_path_
));
624 EXPECT_TRUE(base::DirectoryExists(new_path_
));
627 TEST_F(CacheStorageMigrationTest
, MoveFailure
) {
628 // Revert the migration.
629 ASSERT_TRUE(base::Move(legacy_path_
, new_path_
));
630 ASSERT_FALSE(base::DirectoryExists(legacy_path_
));
631 ASSERT_TRUE(base::DirectoryExists(new_path_
));
633 // Make a dummy legacy directory.
634 ASSERT_TRUE(base::CreateDirectory(legacy_path_
));
636 // Ensure that migration doesn't stomp existing new directory,
637 // but does clean up old directory.
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 class CacheStorageQuotaClientTest
: public CacheStorageManagerTest
{
651 CacheStorageQuotaClientTest() {}
653 void SetUp() override
{
654 CacheStorageManagerTest::SetUp();
656 new CacheStorageQuotaClient(cache_manager_
->AsWeakPtr()));
659 void UsageCallback(base::RunLoop
* run_loop
, int64 usage
) {
660 callback_usage_
= usage
;
664 void OriginsCallback(base::RunLoop
* run_loop
, const std::set
<GURL
>& origins
) {
665 callback_origins_
= origins
;
669 void DeleteOriginCallback(base::RunLoop
* run_loop
,
670 storage::QuotaStatusCode status
) {
671 callback_status_
= status
;
675 int64
QuotaGetOriginUsage(const GURL
& origin
) {
676 scoped_ptr
<base::RunLoop
> loop(new base::RunLoop());
677 quota_client_
->GetOriginUsage(
678 origin
, storage::kStorageTypeTemporary
,
679 base::Bind(&CacheStorageQuotaClientTest::UsageCallback
,
680 base::Unretained(this), base::Unretained(loop
.get())));
682 return callback_usage_
;
685 size_t QuotaGetOriginsForType() {
686 scoped_ptr
<base::RunLoop
> loop(new base::RunLoop());
687 quota_client_
->GetOriginsForType(
688 storage::kStorageTypeTemporary
,
689 base::Bind(&CacheStorageQuotaClientTest::OriginsCallback
,
690 base::Unretained(this), base::Unretained(loop
.get())));
692 return callback_origins_
.size();
695 size_t QuotaGetOriginsForHost(const std::string
& host
) {
696 scoped_ptr
<base::RunLoop
> loop(new base::RunLoop());
697 quota_client_
->GetOriginsForHost(
698 storage::kStorageTypeTemporary
, host
,
699 base::Bind(&CacheStorageQuotaClientTest::OriginsCallback
,
700 base::Unretained(this), base::Unretained(loop
.get())));
702 return callback_origins_
.size();
705 bool QuotaDeleteOriginData(const GURL
& origin
) {
706 scoped_ptr
<base::RunLoop
> loop(new base::RunLoop());
707 quota_client_
->DeleteOriginData(
708 origin
, storage::kStorageTypeTemporary
,
709 base::Bind(&CacheStorageQuotaClientTest::DeleteOriginCallback
,
710 base::Unretained(this), base::Unretained(loop
.get())));
712 return callback_status_
== storage::kQuotaStatusOk
;
715 bool QuotaDoesSupport(storage::StorageType type
) {
716 return quota_client_
->DoesSupport(type
);
719 scoped_ptr
<CacheStorageQuotaClient
> quota_client_
;
721 storage::QuotaStatusCode callback_status_
;
722 int64 callback_usage_
;
723 std::set
<GURL
> callback_origins_
;
725 DISALLOW_COPY_AND_ASSIGN(CacheStorageQuotaClientTest
);
728 class CacheStorageQuotaClientTestP
: public CacheStorageQuotaClientTest
,
729 public testing::WithParamInterface
<bool> {
730 bool MemoryOnly() override
{ return !GetParam(); }
733 TEST_P(CacheStorageQuotaClientTestP
, QuotaID
) {
734 EXPECT_EQ(storage::QuotaClient::kServiceWorkerCache
, quota_client_
->id());
737 TEST_P(CacheStorageQuotaClientTestP
, QuotaGetOriginUsage
) {
738 EXPECT_EQ(0, QuotaGetOriginUsage(origin1_
));
739 EXPECT_TRUE(Open(origin1_
, "foo"));
740 EXPECT_TRUE(CachePut(callback_cache_
, GURL("http://example.com/foo")));
741 EXPECT_LT(0, QuotaGetOriginUsage(origin1_
));
744 TEST_P(CacheStorageQuotaClientTestP
, QuotaGetOriginsForType
) {
745 EXPECT_EQ(0u, QuotaGetOriginsForType());
746 EXPECT_TRUE(Open(origin1_
, "foo"));
747 EXPECT_TRUE(Open(origin1_
, "bar"));
748 EXPECT_TRUE(Open(origin2_
, "foo"));
749 EXPECT_EQ(2u, QuotaGetOriginsForType());
752 TEST_P(CacheStorageQuotaClientTestP
, QuotaGetOriginsForHost
) {
753 EXPECT_EQ(0u, QuotaGetOriginsForHost("example.com"));
754 EXPECT_TRUE(Open(GURL("http://example.com:8080"), "foo"));
755 EXPECT_TRUE(Open(GURL("http://example.com:9000"), "foo"));
756 EXPECT_TRUE(Open(GURL("ftp://example.com"), "foo"));
757 EXPECT_TRUE(Open(GURL("http://example2.com"), "foo"));
758 EXPECT_EQ(3u, QuotaGetOriginsForHost("example.com"));
759 EXPECT_EQ(1u, QuotaGetOriginsForHost("example2.com"));
760 EXPECT_TRUE(callback_origins_
.find(GURL("http://example2.com")) !=
761 callback_origins_
.end());
762 EXPECT_EQ(0u, QuotaGetOriginsForHost("unknown.com"));
765 TEST_P(CacheStorageQuotaClientTestP
, QuotaDeleteOriginData
) {
766 EXPECT_TRUE(Open(origin1_
, "foo"));
767 // Call put to test that initialized caches are properly deleted too.
768 EXPECT_TRUE(CachePut(callback_cache_
, GURL("http://example.com/foo")));
769 EXPECT_TRUE(Open(origin1_
, "bar"));
770 EXPECT_TRUE(Open(origin2_
, "baz"));
772 EXPECT_TRUE(QuotaDeleteOriginData(origin1_
));
774 EXPECT_FALSE(Has(origin1_
, "foo"));
775 EXPECT_FALSE(Has(origin1_
, "bar"));
776 EXPECT_TRUE(Has(origin2_
, "baz"));
777 EXPECT_TRUE(Open(origin1_
, "foo"));
780 TEST_P(CacheStorageQuotaClientTestP
, QuotaDeleteEmptyOrigin
) {
781 EXPECT_TRUE(QuotaDeleteOriginData(origin1_
));
784 TEST_P(CacheStorageQuotaClientTestP
, QuotaDoesSupport
) {
785 EXPECT_TRUE(QuotaDoesSupport(storage::kStorageTypeTemporary
));
786 EXPECT_FALSE(QuotaDoesSupport(storage::kStorageTypePersistent
));
787 EXPECT_FALSE(QuotaDoesSupport(storage::kStorageTypeSyncable
));
788 EXPECT_FALSE(QuotaDoesSupport(storage::kStorageTypeQuotaNotManaged
));
789 EXPECT_FALSE(QuotaDoesSupport(storage::kStorageTypeUnknown
));
792 INSTANTIATE_TEST_CASE_P(CacheStorageManagerTests
,
793 CacheStorageManagerTestP
,
794 ::testing::Values(false, true));
796 INSTANTIATE_TEST_CASE_P(CacheStorageQuotaClientTests
,
797 CacheStorageQuotaClientTestP
,
798 ::testing::Values(false, true));
800 } // namespace content