| 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..deafa60c6eabedbad6f08a7b7345c74edaa48e99 100644
|
| --- a/content/browser/cache_storage/cache_storage_cache_unittest.cc
|
| +++ b/content/browser/cache_storage/cache_storage_cache_unittest.cc
|
| @@ -12,6 +12,7 @@
|
| #include <utility>
|
|
|
| #include "base/files/file_path.h"
|
| +#include "base/files/file_util.h"
|
| #include "base/files/scoped_temp_dir.h"
|
| #include "base/logging.h"
|
| #include "base/macros.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) {
|
| @@ -689,6 +693,8 @@ class CacheStorageCacheTest : public testing::Test {
|
|
|
| virtual bool MemoryOnly() { return false; }
|
|
|
| + virtual bool PadResources() const { return false; }
|
| +
|
| void SetMaxQuerySizeBytes(size_t max_bytes) {
|
| cache_->max_query_size_bytes_ = max_bytes;
|
| }
|
| @@ -723,9 +729,21 @@ class CacheStorageCacheTest : public testing::Test {
|
| int64_t callback_size_ = 0;
|
| };
|
|
|
| +enum class CacheLocation { IN_MEMORY, ON_DISK };
|
| +
|
| +enum class PadType { ADD_PADDING, NO_PADDING };
|
| +
|
| class CacheStorageCacheTestP : public CacheStorageCacheTest,
|
| - public testing::WithParamInterface<bool> {
|
| - bool MemoryOnly() override { return !GetParam(); }
|
| + public testing::WithParamInterface<
|
| + testing::tuple<CacheLocation, PadType>> {
|
| + public:
|
| + bool MemoryOnly() override {
|
| + return testing::get<0>(GetParam()) == CacheLocation::IN_MEMORY;
|
| + }
|
| +
|
| + bool PadResources() const override {
|
| + return testing::get<1>(GetParam()) == PadType::ADD_PADDING;
|
| + }
|
| };
|
|
|
| TEST_P(CacheStorageCacheTestP, PutNoBody) {
|
| @@ -1594,6 +1612,34 @@ TEST_P(CacheStorageCacheTestP, Size) {
|
| EXPECT_EQ(0, Size());
|
| }
|
|
|
| +TEST_P(CacheStorageCacheTestP, VerifyOpaqueSizePadding) {
|
| + // Test InMemory/OnDisk configs
|
| + if (PadResources())
|
| + return;
|
| +
|
| + 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(CacheStorageCache::CalculateResponsePadding(non_opaque_response),
|
| + 0);
|
| + 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;
|
| +
|
| + const int64_t padding_size =
|
| + CacheStorageCache::CalculateResponsePadding(opaque_response);
|
| + EXPECT_GT(padding_size, 0);
|
| + EXPECT_TRUE(Put(opaque_request, opaque_response));
|
| + EXPECT_EQ(2 * non_padded_cache_size + padding_size, Size());
|
| +}
|
| +
|
| TEST_P(CacheStorageCacheTestP, GetSizeThenClose) {
|
| EXPECT_TRUE(Put(body_request_, body_response_));
|
| int64_t cache_size = Size();
|
| @@ -1613,7 +1659,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 +1682,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,15 +1693,18 @@ 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();
|
| EXPECT_EQ(2, sequence_out);
|
| }
|
|
|
| -INSTANTIATE_TEST_CASE_P(CacheStorageCacheTest,
|
| - CacheStorageCacheTestP,
|
| - ::testing::Values(false, true));
|
| +INSTANTIATE_TEST_CASE_P(
|
| + CacheStorageCacheTest,
|
| + CacheStorageCacheTestP,
|
| + ::testing::Combine(
|
| + ::testing::Values(CacheLocation::IN_MEMORY, CacheLocation::ON_DISK),
|
| + ::testing::Values(PadType::ADD_PADDING, PadType::NO_PADDING)));
|
|
|
| } // namespace content
|
|
|