| Index: content/browser/cache_storage/cache_storage_cache_unittest.cc
|
| diff --git a/content/browser/cache_storage/cache_storage_cache_unittest.cc b/content/browser/cache_storage/cache_storage_cache_unittest.cc
|
| index 3f0227b43bcb896b1ce3036d6b6f7e68871f1850..65fff3138b6a4195f27094ccdd30add41468a40b 100644
|
| --- a/content/browser/cache_storage/cache_storage_cache_unittest.cc
|
| +++ b/content/browser/cache_storage/cache_storage_cache_unittest.cc
|
| @@ -29,6 +29,7 @@
|
| #include "content/public/common/referrer.h"
|
| #include "content/public/test/test_browser_context.h"
|
| #include "content/public/test/test_browser_thread_bundle.h"
|
| +#include "crypto/symmetric_key.h"
|
| #include "net/base/test_completion_callback.h"
|
| #include "net/url_request/url_request_context.h"
|
| #include "net/url_request/url_request_context_getter.h"
|
| @@ -64,7 +65,7 @@ std::unique_ptr<storage::BlobProtocolHandler> CreateMockBlobProtocolHandler(
|
| class DelayableBackend : public disk_cache::Backend {
|
| public:
|
| explicit DelayableBackend(std::unique_ptr<disk_cache::Backend> backend)
|
| - : backend_(std::move(backend)), delay_doom_(false) {}
|
| + : backend_(std::move(backend)), delay_open_entry_(false) {}
|
|
|
| // disk_cache::Backend overrides
|
| net::CacheType GetCacheType() const override {
|
| @@ -74,6 +75,12 @@ class DelayableBackend : public disk_cache::Backend {
|
| int OpenEntry(const std::string& key,
|
| disk_cache::Entry** entry,
|
| const CompletionCallback& callback) override {
|
| + if (delay_open_entry_ && open_entry_callback_.is_null()) {
|
| + open_entry_callback_ = base::Bind(&DelayableBackend::OpenEntryDelayedImpl,
|
| + base::Unretained(this), key,
|
| + base::Unretained(entry), callback);
|
| + return net::ERR_IO_PENDING;
|
| + }
|
| return backend_->OpenEntry(key, entry, callback);
|
| }
|
|
|
| @@ -84,12 +91,6 @@ class DelayableBackend : public disk_cache::Backend {
|
| }
|
| int DoomEntry(const std::string& key,
|
| const CompletionCallback& callback) override {
|
| - if (delay_doom_) {
|
| - doom_entry_callback_ = base::Bind(&DelayableBackend::DoomEntryDelayedImpl,
|
| - base::Unretained(this), key, callback);
|
| - return net::ERR_IO_PENDING;
|
| - }
|
| -
|
| return backend_->DoomEntry(key, callback);
|
| }
|
| int DoomAllEntries(const CompletionCallback& callback) override {
|
| @@ -125,25 +126,28 @@ class DelayableBackend : public disk_cache::Backend {
|
| return 0u;
|
| }
|
|
|
| - // Call to continue a delayed doom.
|
| - void DoomEntryContinue() {
|
| - EXPECT_FALSE(doom_entry_callback_.is_null());
|
| - doom_entry_callback_.Run();
|
| + // Call to continue a delayed call to OpenEntry.
|
| + bool OpenEntryContinue() {
|
| + if (open_entry_callback_.is_null())
|
| + return false;
|
| + open_entry_callback_.Run();
|
| + return true;
|
| }
|
|
|
| - void set_delay_doom(bool value) { delay_doom_ = value; }
|
| + void set_delay_open_entry(bool value) { delay_open_entry_ = value; }
|
|
|
| private:
|
| - void DoomEntryDelayedImpl(const std::string& key,
|
| + void OpenEntryDelayedImpl(const std::string& key,
|
| + disk_cache::Entry** entry,
|
| const CompletionCallback& callback) {
|
| - int rv = backend_->DoomEntry(key, callback);
|
| + int rv = backend_->OpenEntry(key, entry, callback);
|
| if (rv != net::ERR_IO_PENDING)
|
| callback.Run(rv);
|
| }
|
|
|
| std::unique_ptr<disk_cache::Backend> backend_;
|
| - bool delay_doom_;
|
| - base::Closure doom_entry_callback_;
|
| + bool delay_open_entry_;
|
| + base::Closure open_entry_callback_;
|
| };
|
|
|
| void CopyBody(const storage::BlobDataHandle& blob_handle, std::string* output) {
|
| @@ -270,6 +274,11 @@ ServiceWorkerResponse SetCacheName(const ServiceWorkerResponse& original) {
|
| return result;
|
| }
|
|
|
| +std::unique_ptr<crypto::SymmetricKey> CreateTestPaddingKey() {
|
| + return crypto::SymmetricKey::Import(crypto::SymmetricKey::HMAC_SHA1,
|
| + "abc123");
|
| +}
|
| +
|
| } // namespace
|
|
|
| // A CacheStorageCache that can optionally delay during backend creation.
|
| @@ -290,7 +299,9 @@ class TestCacheStorageCache : public CacheStorageCache {
|
| request_context_getter,
|
| quota_manager_proxy,
|
| blob_context,
|
| - 0 /* cache_size */),
|
| + 0 /* cache_size */,
|
| + 0 /* cache_padding */,
|
| + CreateTestPaddingKey()),
|
| delay_backend_creation_(false) {}
|
|
|
| void CreateBackend(const ErrorCallback& callback) override {
|
| @@ -1594,6 +1605,29 @@ TEST_P(CacheStorageCacheTestP, Size) {
|
| EXPECT_EQ(0, Size());
|
| }
|
|
|
| +TEST_F(CacheStorageCacheTest, VerifyOpaqueSizePadding) {
|
| + ServiceWorkerFetchRequest non_opaque_request(body_request_);
|
| + non_opaque_request.url = GURL("http://example.com/no-pad.html");
|
| + ServiceWorkerResponse non_opaque_response(body_response_);
|
| + EXPECT_EQ(0, CacheStorageCache::CalculateResponsePadding(
|
| + non_opaque_response, CreateTestPaddingKey().get()));
|
| + EXPECT_TRUE(Put(non_opaque_request, non_opaque_response));
|
| + int64_t non_padded_cache_size = Size();
|
| +
|
| + ServiceWorkerFetchRequest opaque_request(non_opaque_request);
|
| + opaque_request.url = GURL("http://example.com/opaque.html");
|
| + // Same URL length means same cache sizes (ignoring padding).
|
| + EXPECT_EQ(opaque_request.url.spec().length(),
|
| + non_opaque_request.url.spec().length());
|
| + ServiceWorkerResponse opaque_response(non_opaque_response);
|
| + opaque_response.response_type = blink::kWebServiceWorkerResponseTypeOpaque;
|
| +
|
| + EXPECT_TRUE(Put(opaque_request, opaque_response));
|
| + // Padding can be zero bytes, so if the opaque URL + padding key changes in
|
| + // the future check for this.
|
| + EXPECT_GT(Size(), 2 * non_padded_cache_size);
|
| +}
|
| +
|
| TEST_P(CacheStorageCacheTestP, GetSizeThenClose) {
|
| EXPECT_TRUE(Put(body_request_, body_response_));
|
| int64_t cache_size = Size();
|
| @@ -1613,7 +1647,7 @@ TEST_P(CacheStorageCacheTestP, VerifySerialScheduling) {
|
| // second should wait for the first.
|
| EXPECT_TRUE(Keys()); // Opens the backend.
|
| DelayableBackend* delayable_backend = cache_->UseDelayableBackend();
|
| - delayable_backend->set_delay_doom(true);
|
| + delayable_backend->set_delay_open_entry(true);
|
|
|
| int sequence_out = -1;
|
|
|
| @@ -1636,7 +1670,7 @@ TEST_P(CacheStorageCacheTestP, VerifySerialScheduling) {
|
| operation2.request = body_request_;
|
| operation2.response = body_response_;
|
|
|
| - delayable_backend->set_delay_doom(false);
|
| + delayable_backend->set_delay_open_entry(false);
|
| std::unique_ptr<base::RunLoop> close_loop2(new base::RunLoop());
|
| cache_->BatchOperation(
|
| std::vector<CacheStorageBatchOperation>(1, operation2),
|
| @@ -1647,7 +1681,7 @@ TEST_P(CacheStorageCacheTestP, VerifySerialScheduling) {
|
| base::RunLoop().RunUntilIdle();
|
| EXPECT_FALSE(callback_response_);
|
|
|
| - delayable_backend->DoomEntryContinue();
|
| + EXPECT_TRUE(delayable_backend->OpenEntryContinue());
|
| close_loop1->Run();
|
| EXPECT_EQ(1, sequence_out);
|
| close_loop2->Run();
|
|
|