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

Side by Side Diff: content/browser/cache_storage/cache_storage_manager_unittest.cc

Issue 2901083002: [CacheStorage] Pad and bin opaque resource sizes. (Closed)
Patch Set: Patch Set 8 changes. Created 3 years, 4 months 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 2014 The Chromium Authors. All rights reserved. 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 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 "content/browser/cache_storage/cache_storage_manager.h" 5 #include "content/browser/cache_storage/cache_storage_manager.h"
6 6
7 #include <stddef.h> 7 #include <stddef.h>
8 #include <stdint.h> 8 #include <stdint.h>
9 9
10 #include <list> 10 #include <list>
(...skipping 28 matching lines...) Expand all
39 #include "net/url_request/url_request_job_factory_impl.h" 39 #include "net/url_request/url_request_job_factory_impl.h"
40 #include "storage/browser/blob/blob_data_builder.h" 40 #include "storage/browser/blob/blob_data_builder.h"
41 #include "storage/browser/blob/blob_data_handle.h" 41 #include "storage/browser/blob/blob_data_handle.h"
42 #include "storage/browser/blob/blob_storage_context.h" 42 #include "storage/browser/blob/blob_storage_context.h"
43 #include "storage/browser/blob/blob_url_request_job_factory.h" 43 #include "storage/browser/blob/blob_url_request_job_factory.h"
44 #include "storage/browser/quota/quota_manager_proxy.h" 44 #include "storage/browser/quota/quota_manager_proxy.h"
45 #include "storage/browser/test/mock_quota_manager_proxy.h" 45 #include "storage/browser/test/mock_quota_manager_proxy.h"
46 #include "storage/browser/test/mock_special_storage_policy.h" 46 #include "storage/browser/test/mock_special_storage_policy.h"
47 #include "testing/gtest/include/gtest/gtest.h" 47 #include "testing/gtest/include/gtest/gtest.h"
48 48
49 using blink::mojom::FetchResponseType;
50
49 namespace content { 51 namespace content {
50 52
51 namespace { 53 namespace {
52 54
53 bool IsIndexFileCurrent(const base::FilePath& cache_dir) { 55 bool IsIndexFileCurrent(const base::FilePath& cache_dir) {
54 base::File::Info info; 56 base::File::Info info;
55 const base::FilePath index_path = 57 const base::FilePath index_path =
56 cache_dir.AppendASCII(CacheStorage::kIndexFileName); 58 cache_dir.AppendASCII(CacheStorage::kIndexFileName);
57 if (!GetFileInfo(index_path, &info)) 59 if (!GetFileInfo(index_path, &info))
58 return false; 60 return false;
(...skipping 281 matching lines...) Expand 10 before | Expand all | Expand 10 after
340 base::RunLoop loop; 342 base::RunLoop loop;
341 cache_manager_->MatchAllCaches( 343 cache_manager_->MatchAllCaches(
342 origin, std::move(unique_request), match_params, 344 origin, std::move(unique_request), match_params,
343 base::BindOnce(&CacheStorageManagerTest::CacheMatchCallback, 345 base::BindOnce(&CacheStorageManagerTest::CacheMatchCallback,
344 base::Unretained(this), base::Unretained(&loop))); 346 base::Unretained(this), base::Unretained(&loop)));
345 loop.Run(); 347 loop.Run();
346 348
347 return callback_error_ == CACHE_STORAGE_OK; 349 return callback_error_ == CACHE_STORAGE_OK;
348 } 350 }
349 351
350 bool CachePut(CacheStorageCache* cache, const GURL& url) { 352 bool CachePut(CacheStorageCache* cache,
353 const GURL& url,
354 FetchResponseType response_type = FetchResponseType::kDefault) {
351 ServiceWorkerFetchRequest request; 355 ServiceWorkerFetchRequest request;
352 request.url = url; 356 request.url = url;
353 357
354 return CachePutWithStatusCode(cache, request, 200); 358 return CachePutWithStatusCode(cache, request, 200, response_type);
355 } 359 }
356 360
357 bool CachePutWithRequestAndHeaders( 361 bool CachePutWithRequestAndHeaders(
358 CacheStorageCache* cache, 362 CacheStorageCache* cache,
359 const ServiceWorkerFetchRequest& request, 363 const ServiceWorkerFetchRequest& request,
360 const ServiceWorkerHeaderMap& response_headers) { 364 const ServiceWorkerHeaderMap& response_headers,
361 return CachePutWithStatusCode(cache, request, 200, response_headers); 365 FetchResponseType response_type = FetchResponseType::kDefault) {
366 return CachePutWithStatusCode(cache, request, 200, response_type,
367 response_headers);
362 } 368 }
363 369
364 bool CachePutWithStatusCode(CacheStorageCache* cache, 370 bool CachePutWithStatusCode(
365 const ServiceWorkerFetchRequest& request, 371 CacheStorageCache* cache,
366 int status_code, 372 const ServiceWorkerFetchRequest& request,
367 const ServiceWorkerHeaderMap& response_headers = 373 int status_code,
368 ServiceWorkerHeaderMap()) { 374 FetchResponseType response_type = FetchResponseType::kDefault,
375 const ServiceWorkerHeaderMap& response_headers =
376 ServiceWorkerHeaderMap()) {
369 std::unique_ptr<storage::BlobDataBuilder> blob_data( 377 std::unique_ptr<storage::BlobDataBuilder> blob_data(
370 new storage::BlobDataBuilder(base::GenerateGUID())); 378 new storage::BlobDataBuilder(base::GenerateGUID()));
371 blob_data->AppendData(request.url.spec()); 379 blob_data->AppendData(request.url.spec());
372 380
373 std::unique_ptr<storage::BlobDataHandle> blob_handle = 381 std::unique_ptr<storage::BlobDataHandle> blob_handle =
374 blob_storage_context_->AddFinishedBlob(blob_data.get()); 382 blob_storage_context_->AddFinishedBlob(blob_data.get());
375 std::unique_ptr<std::vector<GURL>> url_list = 383 std::unique_ptr<std::vector<GURL>> url_list =
376 base::MakeUnique<std::vector<GURL>>(); 384 base::MakeUnique<std::vector<GURL>>();
377 url_list->push_back(request.url); 385 url_list->push_back(request.url);
378 ServiceWorkerResponse response( 386 ServiceWorkerResponse response(
379 std::move(url_list), status_code, "OK", 387 std::move(url_list), status_code, "OK", response_type,
380 blink::mojom::FetchResponseType::kDefault,
381 base::MakeUnique<ServiceWorkerHeaderMap>(response_headers), 388 base::MakeUnique<ServiceWorkerHeaderMap>(response_headers),
382 blob_handle->uuid(), request.url.spec().size(), 389 blob_handle->uuid(), request.url.spec().size(),
383 blink::kWebServiceWorkerResponseErrorUnknown, base::Time(), 390 blink::kWebServiceWorkerResponseErrorUnknown, base::Time(),
384 false /* is_in_cache_storage */, 391 false /* is_in_cache_storage */,
385 std::string() /* cache_storage_cache_name */, 392 std::string() /* cache_storage_cache_name */,
386 base::MakeUnique< 393 base::MakeUnique<
387 ServiceWorkerHeaderList>() /* cors_exposed_header_names */); 394 ServiceWorkerHeaderList>() /* cors_exposed_header_names */);
388 395
389 CacheStorageBatchOperation operation; 396 CacheStorageBatchOperation operation;
390 operation.operation_type = CACHE_STORAGE_CACHE_OPERATION_TYPE_PUT; 397 operation.operation_type = CACHE_STORAGE_CACHE_OPERATION_TYPE_PUT;
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after
461 468
462 int64_t Size(const GURL& origin) { 469 int64_t Size(const GURL& origin) {
463 base::RunLoop loop; 470 base::RunLoop loop;
464 CacheStorage* cache_storage = CacheStorageForOrigin(origin); 471 CacheStorage* cache_storage = CacheStorageForOrigin(origin);
465 cache_storage->Size(base::BindOnce(&CacheStorageManagerTest::UsageCallback, 472 cache_storage->Size(base::BindOnce(&CacheStorageManagerTest::UsageCallback,
466 base::Unretained(this), &loop)); 473 base::Unretained(this), &loop));
467 loop.Run(); 474 loop.Run();
468 return callback_usage_; 475 return callback_usage_;
469 } 476 }
470 477
478 int64_t GetQuotaOriginUsage(const GURL& origin) {
479 int64_t usage(CacheStorage::kSizeUnknown);
480 base::RunLoop loop;
481 quota_manager_proxy_->GetUsageAndQuota(
482 base::ThreadTaskRunnerHandle::Get().get(), origin,
483 StorageType::kStorageTypeTemporary,
484 base::Bind(&CacheStorageManagerTest::DidGetQuotaOriginUsage,
485 base::Unretained(this), base::Unretained(&usage), &loop));
486 loop.Run();
487 return usage;
488 }
489
490 void DidGetQuotaOriginUsage(int64_t* out_usage,
491 base::RunLoop* run_loop,
492 QuotaStatusCode status_code,
493 int64_t usage,
494 int64_t quota) {
495 if (status_code == storage::kQuotaStatusOk)
496 *out_usage = usage;
497 run_loop->Quit();
498 }
499
471 protected: 500 protected:
472 // Temporary directory must be allocated first so as to be destroyed last. 501 // Temporary directory must be allocated first so as to be destroyed last.
473 base::ScopedTempDir temp_dir_; 502 base::ScopedTempDir temp_dir_;
474 503
475 TestBrowserThreadBundle browser_thread_bundle_; 504 TestBrowserThreadBundle browser_thread_bundle_;
476 TestBrowserContext browser_context_; 505 TestBrowserContext browser_context_;
477 std::unique_ptr<net::URLRequestJobFactoryImpl> url_request_job_factory_; 506 std::unique_ptr<net::URLRequestJobFactoryImpl> url_request_job_factory_;
478 storage::BlobStorageContext* blob_storage_context_; 507 storage::BlobStorageContext* blob_storage_context_;
479 508
480 scoped_refptr<MockSpecialStoragePolicy> quota_policy_; 509 scoped_refptr<MockSpecialStoragePolicy> quota_policy_;
(...skipping 429 matching lines...) Expand 10 before | Expand all | Expand 10 after
910 auto size_before_close = Size(origin1_); 939 auto size_before_close = Size(origin1_);
911 EXPECT_GT(size_before_close, 0); 940 EXPECT_GT(size_before_close, 0);
912 941
913 DestroyStorageManager(); 942 DestroyStorageManager();
914 CreateStorageManager(); 943 CreateStorageManager();
915 944
916 EXPECT_TRUE(Open(origin1_, kCacheName)); 945 EXPECT_TRUE(Open(origin1_, kCacheName));
917 EXPECT_EQ(size_before_close, Size(origin1_)); 946 EXPECT_EQ(size_before_close, Size(origin1_));
918 } 947 }
919 948
949 TEST_F(CacheStorageManagerTest, CacheSizePaddedAfterReopen) {
950 const GURL kFooURL = origin1_.Resolve("foo");
951 const std::string kCacheName = "foo";
952
953 int64_t put_delta = quota_manager_proxy_->last_notified_delta();
954 EXPECT_EQ(0, put_delta);
955 EXPECT_EQ(0, quota_manager_proxy_->notify_storage_modified_count());
956
957 EXPECT_TRUE(Open(origin1_, kCacheName));
958 std::unique_ptr<CacheStorageCacheHandle> original_handle =
959 std::move(callback_cache_handle_);
960
961 base::RunLoop().RunUntilIdle();
962 put_delta += quota_manager_proxy_->last_notified_delta();
963 EXPECT_EQ(0, put_delta);
964 EXPECT_EQ(0, quota_manager_proxy_->notify_storage_modified_count());
965
966 EXPECT_TRUE(
967 CachePut(original_handle->value(), kFooURL, FetchResponseType::kOpaque));
968 int64_t cache_size_before_close = Size(origin1_);
969 base::FilePath storage_dir = original_handle->value()->path().DirName();
970 original_handle = nullptr;
971 EXPECT_GT(cache_size_before_close, 0);
972
973 base::RunLoop().RunUntilIdle();
974 EXPECT_EQ(GetQuotaOriginUsage(origin1_), cache_size_before_close);
jkarlin 2017/07/31 14:03:25 swap argument order
cmumford 2017/08/10 16:37:11 Done.
975
976 base::RunLoop().RunUntilIdle();
977 put_delta = quota_manager_proxy_->last_notified_delta();
978 EXPECT_GT(put_delta, 0);
979 EXPECT_EQ(1, quota_manager_proxy_->notify_storage_modified_count());
980
981 EXPECT_EQ(GetQuotaOriginUsage(origin1_), put_delta);
982
983 // Close the caches and cache manager.
984 EXPECT_TRUE(FlushCacheStorageIndex(origin1_));
985 DestroyStorageManager();
986
987 // Create a new CacheStorageManager that hasn't yet loaded the origin.
988 CreateStorageManager();
989 quota_manager_proxy_->SimulateQuotaManagerDestroyed();
990 cache_manager_ = CacheStorageManager::Create(cache_manager_.get());
991 EXPECT_TRUE(Open(origin1_, kCacheName));
992
993 base::RunLoop().RunUntilIdle();
994 put_delta = quota_manager_proxy_->last_notified_delta();
995 EXPECT_EQ(0, put_delta);
996 EXPECT_EQ(0, quota_manager_proxy_->notify_storage_modified_count());
997
998 EXPECT_EQ(cache_size_before_close, Size(origin1_));
999 }
1000
1001 TEST_F(CacheStorageManagerTest, PersistedCacheKeyUsed) {
1002 const GURL kFooURL = origin1_.Resolve("foo");
1003 const std::string kCacheName = "foo";
1004
1005 EXPECT_TRUE(Open(origin1_, kCacheName));
1006 std::unique_ptr<CacheStorageCacheHandle> original_handle =
1007 std::move(callback_cache_handle_);
1008
1009 EXPECT_TRUE(
1010 CachePut(original_handle->value(), kFooURL, FetchResponseType::kOpaque));
1011
1012 int64_t cache_size_after_put = Size(origin1_);
1013 EXPECT_GT(cache_size_after_put, 0);
jkarlin 2017/07/31 14:03:25 swap argument order
cmumford 2017/08/10 16:37:11 Are you sure? EXPECT_GT(arg1, arg2) is expecting t
jkarlin 2017/08/11 15:04:37 Sorry. What I'm looking for is to have the expecte
1014
1015 // Close the caches and cache manager.
1016 EXPECT_TRUE(FlushCacheStorageIndex(origin1_));
1017 DestroyStorageManager();
1018
1019 // GenerateNewKeyForTest isn't thread safe so
1020 base::RunLoop().RunUntilIdle();
1021 CacheStorage::GenerateNewKeyForTesting();
1022
1023 // Create a new CacheStorageManager that hasn't yet loaded the origin.
1024 CreateStorageManager();
1025 quota_manager_proxy_->SimulateQuotaManagerDestroyed();
1026 cache_manager_ = CacheStorageManager::Create(cache_manager_.get());
1027
1028 // Reopening the origin/cache creates a new CacheStorage instance with a new
1029 // random key.
1030 EXPECT_TRUE(Open(origin1_, kCacheName));
1031
1032 // Size (before any change) should be the same as before it was closed.
1033 EXPECT_EQ(Size(origin1_), cache_size_after_put);
jkarlin 2017/07/31 14:03:25 swap argument order
cmumford 2017/08/10 16:37:11 Done.
1034
1035 // Delete the value. If the new padding key was used to deduct the padded size
1036 // then after deletion we would expect to see a non-zero cache size.
1037 EXPECT_TRUE(Delete(origin1_, "foo"));
1038 EXPECT_EQ(Size(origin1_), 0);
jkarlin 2017/07/31 14:03:25 swap argument order
1039
1040 // Now put the exact same resource back into the cache. This time we expect to
1041 // see a different size as the padding is calculated with a different key.
1042 std::unique_ptr<CacheStorageCacheHandle> new_handle =
1043 std::move(callback_cache_handle_);
1044 EXPECT_TRUE(
1045 CachePut(new_handle->value(), kFooURL, FetchResponseType::kOpaque));
1046
1047 EXPECT_NE(Size(origin1_), cache_size_after_put);
jkarlin 2017/07/31 14:03:25 swap argument order
cmumford 2017/08/10 16:37:11 Done, but I don't think that either of these are "
jkarlin 2017/08/11 15:04:37 cache_size_after_put is the thing that is known (v
1048 }
1049
920 // With a memory cache the cache can't be freed from memory until the client 1050 // With a memory cache the cache can't be freed from memory until the client
921 // calls delete. 1051 // calls delete.
922 TEST_F(CacheStorageManagerMemoryOnlyTest, MemoryLosesReferenceOnlyAfterDelete) { 1052 TEST_F(CacheStorageManagerMemoryOnlyTest, MemoryLosesReferenceOnlyAfterDelete) {
923 EXPECT_TRUE(Open(origin1_, "foo")); 1053 EXPECT_TRUE(Open(origin1_, "foo"));
924 base::WeakPtr<CacheStorageCache> cache = 1054 base::WeakPtr<CacheStorageCache> cache =
925 callback_cache_handle_->value()->AsWeakPtr(); 1055 callback_cache_handle_->value()->AsWeakPtr();
926 callback_cache_handle_ = nullptr; 1056 callback_cache_handle_ = nullptr;
927 EXPECT_TRUE(cache); 1057 EXPECT_TRUE(cache);
928 EXPECT_TRUE(Delete(origin1_, "foo")); 1058 EXPECT_TRUE(Delete(origin1_, "foo"));
929 base::RunLoop().RunUntilIdle(); 1059 base::RunLoop().RunUntilIdle();
(...skipping 572 matching lines...) Expand 10 before | Expand all | Expand 10 after
1502 1632
1503 INSTANTIATE_TEST_CASE_P(CacheStorageManagerTests, 1633 INSTANTIATE_TEST_CASE_P(CacheStorageManagerTests,
1504 CacheStorageManagerTestP, 1634 CacheStorageManagerTestP,
1505 ::testing::Values(false, true)); 1635 ::testing::Values(false, true));
1506 1636
1507 INSTANTIATE_TEST_CASE_P(CacheStorageQuotaClientTests, 1637 INSTANTIATE_TEST_CASE_P(CacheStorageQuotaClientTests,
1508 CacheStorageQuotaClientTestP, 1638 CacheStorageQuotaClientTestP,
1509 ::testing::Values(false, true)); 1639 ::testing::Values(false, true));
1510 1640
1511 } // namespace content 1641 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698