Chromium Code Reviews| 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..f097e849a414eff6a4d7289bd2cef4222f1a8af5 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) { |
| @@ -290,7 +294,9 @@ class TestCacheStorageCache : public CacheStorageCache { |
| request_context_getter, |
| quota_manager_proxy, |
| blob_context, |
| - 0 /* cache_size */), |
| + 0 /* cache_size */, |
| + 0 /* cache_padding */, |
| + "123abc" /* padding_key */), |
| delay_backend_creation_(false) {} |
| void CreateBackend(const ErrorCallback& callback) override { |
| @@ -689,6 +695,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 +731,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 { |
|
jkarlin
2017/06/08 18:58:48
It's unclear to me what PadResources is being used
cmumford
2017/06/12 18:09:31
It was, but unnecessarily, so I removed it.
|
| + return testing::get<1>(GetParam()) == PadType::ADD_PADDING; |
| + } |
| }; |
| TEST_P(CacheStorageCacheTestP, PutNoBody) { |
| @@ -1594,6 +1614,37 @@ TEST_P(CacheStorageCacheTestP, Size) { |
| EXPECT_EQ(0, Size()); |
| } |
| +TEST_P(CacheStorageCacheTestP, VerifyOpaqueSizePadding) { |
| + // Test InMemory/OnDisk configs |
| + if (PadResources()) |
| + return; |
| + |
| + const std::string padding_key = "123abc"; |
| + |
| + 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, |
|
jkarlin
2017/06/08 18:58:48
EXPECT_EQ(0, ...)
cmumford
2017/06/12 18:09:31
Done.
|
| + padding_key), |
| + 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, padding_key); |
|
jkarlin
2017/06/08 18:58:48
I'd rather that we not call this private function
cmumford
2017/06/12 18:09:31
Will do. Technically padding can be zero, so the t
|
| + 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 +1664,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 +1687,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 +1698,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 |