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

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

Issue 1636613002: [CacheStorage] Check quota before put operations (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Increase max size per cache Created 4 years, 11 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_cache.h" 5 #include "content/browser/cache_storage/cache_storage_cache.h"
6 6
7 #include <stddef.h> 7 #include <stddef.h>
8 #include <stdint.h> 8 #include <stdint.h>
9 #include <utility> 9 #include <utility>
10 10
11 #include "base/files/file_path.h" 11 #include "base/files/file_path.h"
12 #include "base/files/scoped_temp_dir.h" 12 #include "base/files/scoped_temp_dir.h"
13 #include "base/macros.h" 13 #include "base/macros.h"
14 #include "base/memory/ref_counted.h" 14 #include "base/memory/ref_counted.h"
15 #include "base/memory/scoped_ptr.h" 15 #include "base/memory/scoped_ptr.h"
16 #include "base/run_loop.h" 16 #include "base/run_loop.h"
17 #include "base/strings/string_split.h" 17 #include "base/strings/string_split.h"
18 #include "base/thread_task_runner_handle.h" 18 #include "base/thread_task_runner_handle.h"
19 #include "content/browser/fileapi/chrome_blob_storage_context.h" 19 #include "content/browser/fileapi/chrome_blob_storage_context.h"
20 #include "content/browser/fileapi/mock_url_request_delegate.h" 20 #include "content/browser/fileapi/mock_url_request_delegate.h"
21 #include "content/browser/quota/mock_quota_manager_proxy.h" 21 #include "content/browser/quota/mock_quota_manager_proxy.h"
22 #include "content/common/cache_storage/cache_storage_types.h" 22 #include "content/common/cache_storage/cache_storage_types.h"
23 #include "content/common/service_worker/service_worker_types.h" 23 #include "content/common/service_worker/service_worker_types.h"
24 #include "content/public/browser/browser_thread.h" 24 #include "content/public/browser/browser_thread.h"
25 #include "content/public/common/referrer.h" 25 #include "content/public/common/referrer.h"
26 #include "content/public/test/mock_special_storage_policy.h"
26 #include "content/public/test/test_browser_context.h" 27 #include "content/public/test/test_browser_context.h"
27 #include "content/public/test/test_browser_thread_bundle.h" 28 #include "content/public/test/test_browser_thread_bundle.h"
28 #include "net/base/test_completion_callback.h" 29 #include "net/base/test_completion_callback.h"
29 #include "net/url_request/url_request_context.h" 30 #include "net/url_request/url_request_context.h"
30 #include "net/url_request/url_request_context_getter.h" 31 #include "net/url_request/url_request_context_getter.h"
31 #include "net/url_request/url_request_job_factory_impl.h" 32 #include "net/url_request/url_request_job_factory_impl.h"
32 #include "storage/browser/blob/blob_data_builder.h" 33 #include "storage/browser/blob/blob_data_builder.h"
33 #include "storage/browser/blob/blob_data_handle.h" 34 #include "storage/browser/blob/blob_data_handle.h"
34 #include "storage/browser/blob/blob_data_snapshot.h" 35 #include "storage/browser/blob/blob_data_snapshot.h"
35 #include "storage/browser/blob/blob_storage_context.h" 36 #include "storage/browser/blob/blob_storage_context.h"
36 #include "storage/browser/blob/blob_url_request_job_factory.h" 37 #include "storage/browser/blob/blob_url_request_job_factory.h"
37 #include "storage/browser/quota/quota_manager_proxy.h" 38 #include "storage/browser/quota/quota_manager_proxy.h"
38 #include "testing/gtest/include/gtest/gtest.h" 39 #include "testing/gtest/include/gtest/gtest.h"
39 40
40 namespace content { 41 namespace content {
41 42
42 namespace { 43 namespace {
43 const char kTestData[] = "Hello World"; 44 const char kTestData[] = "Hello World";
45 const char kOrigin[] = "http://example.com";
44 46
45 // Returns a BlobProtocolHandler that uses |blob_storage_context|. Caller owns 47 // Returns a BlobProtocolHandler that uses |blob_storage_context|. Caller owns
46 // the memory. 48 // the memory.
47 scoped_ptr<storage::BlobProtocolHandler> CreateMockBlobProtocolHandler( 49 scoped_ptr<storage::BlobProtocolHandler> CreateMockBlobProtocolHandler(
48 storage::BlobStorageContext* blob_storage_context) { 50 storage::BlobStorageContext* blob_storage_context) {
49 // The FileSystemContext and thread task runner are not actually used but a 51 // The FileSystemContext and thread task runner are not actually used but a
50 // task runner is needed to avoid a DCHECK in BlobURLRequestJob ctor. 52 // task runner is needed to avoid a DCHECK in BlobURLRequestJob ctor.
51 return make_scoped_ptr(new storage::BlobProtocolHandler( 53 return make_scoped_ptr(new storage::BlobProtocolHandler(
52 blob_storage_context, NULL, base::ThreadTaskRunnerHandle::Get().get())); 54 blob_storage_context, NULL, base::ThreadTaskRunnerHandle::Get().get()));
53 } 55 }
(...skipping 203 matching lines...) Expand 10 before | Expand all | Expand 10 after
257 callback_error_(CACHE_STORAGE_OK), 259 callback_error_(CACHE_STORAGE_OK),
258 callback_closed_(false) {} 260 callback_closed_(false) {}
259 261
260 void SetUp() override { 262 void SetUp() override {
261 ChromeBlobStorageContext* blob_storage_context = 263 ChromeBlobStorageContext* blob_storage_context =
262 ChromeBlobStorageContext::GetFor(&browser_context_); 264 ChromeBlobStorageContext::GetFor(&browser_context_);
263 // Wait for chrome_blob_storage_context to finish initializing. 265 // Wait for chrome_blob_storage_context to finish initializing.
264 base::RunLoop().RunUntilIdle(); 266 base::RunLoop().RunUntilIdle();
265 blob_storage_context_ = blob_storage_context->context(); 267 blob_storage_context_ = blob_storage_context->context();
266 268
269 if (!MemoryOnly())
270 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
271 base::FilePath path = MemoryOnly() ? base::FilePath() : temp_dir_.path();
272
273 quota_policy_ = new MockSpecialStoragePolicy;
274 mock_quota_manager_ = new MockQuotaManager(
275 MemoryOnly() /* is incognito */, path,
276 base::ThreadTaskRunnerHandle::Get().get(),
277 base::ThreadTaskRunnerHandle::Get().get(), quota_policy_.get());
278
267 quota_manager_proxy_ = new MockQuotaManagerProxy( 279 quota_manager_proxy_ = new MockQuotaManagerProxy(
268 nullptr, base::ThreadTaskRunnerHandle::Get().get()); 280 mock_quota_manager_.get(), base::ThreadTaskRunnerHandle::Get().get());
269 281
270 url_request_job_factory_.reset(new net::URLRequestJobFactoryImpl); 282 url_request_job_factory_.reset(new net::URLRequestJobFactoryImpl);
271 url_request_job_factory_->SetProtocolHandler( 283 url_request_job_factory_->SetProtocolHandler(
272 "blob", CreateMockBlobProtocolHandler(blob_storage_context->context())); 284 "blob", CreateMockBlobProtocolHandler(blob_storage_context->context()));
273 285
274 net::URLRequestContext* url_request_context = 286 net::URLRequestContext* url_request_context =
275 browser_context_.GetRequestContext()->GetURLRequestContext(); 287 browser_context_.GetRequestContext()->GetURLRequestContext();
276 288
277 url_request_context->set_job_factory(url_request_job_factory_.get()); 289 url_request_context->set_job_factory(url_request_job_factory_.get());
278 290
279 CreateRequests(blob_storage_context); 291 CreateRequests(blob_storage_context);
280 292
281 if (!MemoryOnly())
282 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
283 base::FilePath path = MemoryOnly() ? base::FilePath() : temp_dir_.path();
284
285 cache_ = make_scoped_refptr(new TestCacheStorageCache( 293 cache_ = make_scoped_refptr(new TestCacheStorageCache(
286 GURL("http://example.com"), path, browser_context_.GetRequestContext(), 294 GURL(kOrigin), path, browser_context_.GetRequestContext(),
287 quota_manager_proxy_, blob_storage_context->context()->AsWeakPtr())); 295 quota_manager_proxy_, blob_storage_context->context()->AsWeakPtr()));
288 } 296 }
289 297
290 void TearDown() override { 298 void TearDown() override {
291 quota_manager_proxy_->SimulateQuotaManagerDestroyed(); 299 quota_manager_proxy_->SimulateQuotaManagerDestroyed();
292 base::RunLoop().RunUntilIdle(); 300 base::RunLoop().RunUntilIdle();
293 } 301 }
294 302
295 void CreateRequests(ChromeBlobStorageContext* blob_storage_context) { 303 void CreateRequests(ChromeBlobStorageContext* blob_storage_context) {
296 ServiceWorkerHeaderMap headers; 304 ServiceWorkerHeaderMap headers;
(...skipping 210 matching lines...) Expand 10 before | Expand all | Expand 10 after
507 EXPECT_FALSE(Keys()); 515 EXPECT_FALSE(Keys());
508 } 516 }
509 517
510 virtual bool MemoryOnly() { return false; } 518 virtual bool MemoryOnly() { return false; }
511 519
512 protected: 520 protected:
513 base::ScopedTempDir temp_dir_; 521 base::ScopedTempDir temp_dir_;
514 TestBrowserContext browser_context_; 522 TestBrowserContext browser_context_;
515 TestBrowserThreadBundle browser_thread_bundle_; 523 TestBrowserThreadBundle browser_thread_bundle_;
516 scoped_ptr<net::URLRequestJobFactoryImpl> url_request_job_factory_; 524 scoped_ptr<net::URLRequestJobFactoryImpl> url_request_job_factory_;
525 scoped_refptr<MockSpecialStoragePolicy> quota_policy_;
526 scoped_refptr<MockQuotaManager> mock_quota_manager_;
517 scoped_refptr<MockQuotaManagerProxy> quota_manager_proxy_; 527 scoped_refptr<MockQuotaManagerProxy> quota_manager_proxy_;
518 storage::BlobStorageContext* blob_storage_context_; 528 storage::BlobStorageContext* blob_storage_context_;
519 529
520 scoped_refptr<TestCacheStorageCache> cache_; 530 scoped_refptr<TestCacheStorageCache> cache_;
521 531
522 ServiceWorkerFetchRequest body_request_; 532 ServiceWorkerFetchRequest body_request_;
523 ServiceWorkerResponse body_response_; 533 ServiceWorkerResponse body_response_;
524 ServiceWorkerFetchRequest no_body_request_; 534 ServiceWorkerFetchRequest no_body_request_;
525 ServiceWorkerResponse no_body_response_; 535 ServiceWorkerResponse no_body_response_;
526 scoped_ptr<storage::BlobDataHandle> blob_handle_; 536 scoped_ptr<storage::BlobDataHandle> blob_handle_;
(...skipping 402 matching lines...) Expand 10 before | Expand all | Expand 10 after
929 EXPECT_EQ(3, quota_manager_proxy_->notify_storage_modified_count()); 939 EXPECT_EQ(3, quota_manager_proxy_->notify_storage_modified_count());
930 sum_delta += quota_manager_proxy_->last_notified_delta(); 940 sum_delta += quota_manager_proxy_->last_notified_delta();
931 941
932 EXPECT_TRUE(Delete(no_body_request_)); 942 EXPECT_TRUE(Delete(no_body_request_));
933 EXPECT_EQ(4, quota_manager_proxy_->notify_storage_modified_count()); 943 EXPECT_EQ(4, quota_manager_proxy_->notify_storage_modified_count());
934 sum_delta += quota_manager_proxy_->last_notified_delta(); 944 sum_delta += quota_manager_proxy_->last_notified_delta();
935 945
936 EXPECT_EQ(0, sum_delta); 946 EXPECT_EQ(0, sum_delta);
937 } 947 }
938 948
949 TEST_P(CacheStorageCacheTestP, PutObeysQuotaLimits) {
950 mock_quota_manager_->SetQuota(GURL(kOrigin), storage::kStorageTypeTemporary,
951 0);
952 EXPECT_FALSE(Put(no_body_request_, no_body_response_));
953 EXPECT_EQ(CACHE_STORAGE_ERROR_QUOTA_EXCEEDED, callback_error_);
954 }
955
939 TEST_F(CacheStorageCacheMemoryOnlyTest, MemoryBackedSize) { 956 TEST_F(CacheStorageCacheMemoryOnlyTest, MemoryBackedSize) {
940 EXPECT_EQ(0, cache_->MemoryBackedSize()); 957 EXPECT_EQ(0, cache_->MemoryBackedSize());
941 EXPECT_TRUE(Put(no_body_request_, no_body_response_)); 958 EXPECT_TRUE(Put(no_body_request_, no_body_response_));
942 EXPECT_LT(0, cache_->MemoryBackedSize()); 959 EXPECT_LT(0, cache_->MemoryBackedSize());
943 int64_t no_body_size = cache_->MemoryBackedSize(); 960 int64_t no_body_size = cache_->MemoryBackedSize();
944 961
945 EXPECT_TRUE(Delete(no_body_request_)); 962 EXPECT_TRUE(Delete(no_body_request_));
946 EXPECT_EQ(0, cache_->MemoryBackedSize()); 963 EXPECT_EQ(0, cache_->MemoryBackedSize());
947 964
948 EXPECT_TRUE(Put(body_request_, body_response_)); 965 EXPECT_TRUE(Put(body_request_, body_response_));
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after
1016 EXPECT_EQ(1, sequence_out); 1033 EXPECT_EQ(1, sequence_out);
1017 close_loop2->Run(); 1034 close_loop2->Run();
1018 EXPECT_EQ(2, sequence_out); 1035 EXPECT_EQ(2, sequence_out);
1019 } 1036 }
1020 1037
1021 INSTANTIATE_TEST_CASE_P(CacheStorageCacheTest, 1038 INSTANTIATE_TEST_CASE_P(CacheStorageCacheTest,
1022 CacheStorageCacheTestP, 1039 CacheStorageCacheTestP,
1023 ::testing::Values(false, true)); 1040 ::testing::Values(false, true));
1024 1041
1025 } // namespace content 1042 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698