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

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

Issue 2901083002: [CacheStorage] Pad and bin opaque resource sizes. (Closed)
Patch Set: Padding side data and writing padding alg. version to index. Created 3 years, 5 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 9
10 #include <memory> 10 #include <memory>
(...skipping 11 matching lines...) Expand all
22 #include "base/threading/thread_task_runner_handle.h" 22 #include "base/threading/thread_task_runner_handle.h"
23 #include "content/browser/blob_storage/chrome_blob_storage_context.h" 23 #include "content/browser/blob_storage/chrome_blob_storage_context.h"
24 #include "content/browser/cache_storage/cache_storage_cache_handle.h" 24 #include "content/browser/cache_storage/cache_storage_cache_handle.h"
25 #include "content/common/cache_storage/cache_storage_types.h" 25 #include "content/common/cache_storage/cache_storage_types.h"
26 #include "content/common/service_worker/service_worker_types.h" 26 #include "content/common/service_worker/service_worker_types.h"
27 #include "content/public/browser/browser_thread.h" 27 #include "content/public/browser/browser_thread.h"
28 #include "content/public/browser/storage_partition.h" 28 #include "content/public/browser/storage_partition.h"
29 #include "content/public/common/referrer.h" 29 #include "content/public/common/referrer.h"
30 #include "content/public/test/test_browser_context.h" 30 #include "content/public/test/test_browser_context.h"
31 #include "content/public/test/test_browser_thread_bundle.h" 31 #include "content/public/test/test_browser_thread_bundle.h"
32 #include "crypto/symmetric_key.h"
32 #include "net/base/test_completion_callback.h" 33 #include "net/base/test_completion_callback.h"
33 #include "net/url_request/url_request_context.h" 34 #include "net/url_request/url_request_context.h"
34 #include "net/url_request/url_request_context_getter.h" 35 #include "net/url_request/url_request_context_getter.h"
35 #include "net/url_request/url_request_job_factory_impl.h" 36 #include "net/url_request/url_request_job_factory_impl.h"
36 #include "storage/browser/blob/blob_data_builder.h" 37 #include "storage/browser/blob/blob_data_builder.h"
37 #include "storage/browser/blob/blob_data_handle.h" 38 #include "storage/browser/blob/blob_data_handle.h"
38 #include "storage/browser/blob/blob_data_snapshot.h" 39 #include "storage/browser/blob/blob_data_snapshot.h"
39 #include "storage/browser/blob/blob_storage_context.h" 40 #include "storage/browser/blob/blob_storage_context.h"
40 #include "storage/browser/blob/blob_url_request_job_factory.h" 41 #include "storage/browser/blob/blob_url_request_job_factory.h"
41 #include "storage/browser/quota/quota_manager_proxy.h" 42 #include "storage/browser/quota/quota_manager_proxy.h"
(...skipping 13 matching lines...) Expand all
55 std::unique_ptr<storage::BlobProtocolHandler> CreateMockBlobProtocolHandler( 56 std::unique_ptr<storage::BlobProtocolHandler> CreateMockBlobProtocolHandler(
56 storage::BlobStorageContext* blob_storage_context) { 57 storage::BlobStorageContext* blob_storage_context) {
57 return base::WrapUnique( 58 return base::WrapUnique(
58 new storage::BlobProtocolHandler(blob_storage_context, nullptr)); 59 new storage::BlobProtocolHandler(blob_storage_context, nullptr));
59 } 60 }
60 61
61 // A disk_cache::Backend wrapper that can delay operations. 62 // A disk_cache::Backend wrapper that can delay operations.
62 class DelayableBackend : public disk_cache::Backend { 63 class DelayableBackend : public disk_cache::Backend {
63 public: 64 public:
64 explicit DelayableBackend(std::unique_ptr<disk_cache::Backend> backend) 65 explicit DelayableBackend(std::unique_ptr<disk_cache::Backend> backend)
65 : backend_(std::move(backend)), delay_doom_(false) {} 66 : backend_(std::move(backend)), delay_open_entry_(false) {}
66 67
67 // disk_cache::Backend overrides 68 // disk_cache::Backend overrides
68 net::CacheType GetCacheType() const override { 69 net::CacheType GetCacheType() const override {
69 return backend_->GetCacheType(); 70 return backend_->GetCacheType();
70 } 71 }
71 int32_t GetEntryCount() const override { return backend_->GetEntryCount(); } 72 int32_t GetEntryCount() const override { return backend_->GetEntryCount(); }
72 int OpenEntry(const std::string& key, 73 int OpenEntry(const std::string& key,
73 disk_cache::Entry** entry, 74 disk_cache::Entry** entry,
74 const CompletionCallback& callback) override { 75 const CompletionCallback& callback) override {
76 if (delay_open_entry_ && open_entry_callback_.is_null()) {
77 open_entry_callback_ = base::BindOnce(
78 &DelayableBackend::OpenEntryDelayedImpl, base::Unretained(this), key,
79 base::Unretained(entry), callback);
80 return net::ERR_IO_PENDING;
81 }
75 return backend_->OpenEntry(key, entry, callback); 82 return backend_->OpenEntry(key, entry, callback);
76 } 83 }
77 84
78 int CreateEntry(const std::string& key, 85 int CreateEntry(const std::string& key,
79 disk_cache::Entry** entry, 86 disk_cache::Entry** entry,
80 const CompletionCallback& callback) override { 87 const CompletionCallback& callback) override {
81 return backend_->CreateEntry(key, entry, callback); 88 return backend_->CreateEntry(key, entry, callback);
82 } 89 }
83 int DoomEntry(const std::string& key, 90 int DoomEntry(const std::string& key,
84 const CompletionCallback& callback) override { 91 const CompletionCallback& callback) override {
85 if (delay_doom_) {
86 doom_entry_callback_ =
87 base::BindOnce(&DelayableBackend::DoomEntryDelayedImpl,
88 base::Unretained(this), key, callback);
89 return net::ERR_IO_PENDING;
90 }
91
92 return backend_->DoomEntry(key, callback); 92 return backend_->DoomEntry(key, callback);
93 } 93 }
94 int DoomAllEntries(const CompletionCallback& callback) override { 94 int DoomAllEntries(const CompletionCallback& callback) override {
95 return backend_->DoomAllEntries(callback); 95 return backend_->DoomAllEntries(callback);
96 } 96 }
97 int DoomEntriesBetween(base::Time initial_time, 97 int DoomEntriesBetween(base::Time initial_time,
98 base::Time end_time, 98 base::Time end_time,
99 const CompletionCallback& callback) override { 99 const CompletionCallback& callback) override {
100 return backend_->DoomEntriesBetween(initial_time, end_time, callback); 100 return backend_->DoomEntriesBetween(initial_time, end_time, callback);
101 } 101 }
(...skipping 15 matching lines...) Expand all
117 return backend_->OnExternalCacheHit(key); 117 return backend_->OnExternalCacheHit(key);
118 } 118 }
119 119
120 size_t DumpMemoryStats( 120 size_t DumpMemoryStats(
121 base::trace_event::ProcessMemoryDump* pmd, 121 base::trace_event::ProcessMemoryDump* pmd,
122 const std::string& parent_absolute_name) const override { 122 const std::string& parent_absolute_name) const override {
123 NOTREACHED(); 123 NOTREACHED();
124 return 0u; 124 return 0u;
125 } 125 }
126 126
127 // Call to continue a delayed doom. 127 // Call to continue a delayed call to OpenEntry.
128 void DoomEntryContinue() { 128 bool OpenEntryContinue() {
129 EXPECT_FALSE(doom_entry_callback_.is_null()); 129 if (open_entry_callback_.is_null())
130 std::move(doom_entry_callback_).Run(); 130 return false;
131 std::move(open_entry_callback_).Run();
132 return true;
131 } 133 }
132 134
133 void set_delay_doom(bool value) { delay_doom_ = value; } 135 void set_delay_open_entry(bool value) { delay_open_entry_ = value; }
134 136
135 private: 137 private:
136 void DoomEntryDelayedImpl(const std::string& key, 138 void OpenEntryDelayedImpl(const std::string& key,
139 disk_cache::Entry** entry,
137 const CompletionCallback& callback) { 140 const CompletionCallback& callback) {
138 int rv = backend_->DoomEntry(key, callback); 141 int rv = backend_->OpenEntry(key, entry, callback);
139 if (rv != net::ERR_IO_PENDING) 142 if (rv != net::ERR_IO_PENDING)
140 callback.Run(rv); 143 callback.Run(rv);
141 } 144 }
142 145
143 std::unique_ptr<disk_cache::Backend> backend_; 146 std::unique_ptr<disk_cache::Backend> backend_;
144 bool delay_doom_; 147 bool delay_open_entry_;
145 base::OnceClosure doom_entry_callback_; 148 base::OnceClosure open_entry_callback_;
146 }; 149 };
147 150
148 void CopyBody(const storage::BlobDataHandle& blob_handle, std::string* output) { 151 void CopyBody(const storage::BlobDataHandle& blob_handle, std::string* output) {
149 *output = std::string(); 152 *output = std::string();
150 std::unique_ptr<storage::BlobDataSnapshot> data = 153 std::unique_ptr<storage::BlobDataSnapshot> data =
151 blob_handle.CreateSnapshot(); 154 blob_handle.CreateSnapshot();
152 const auto& items = data->items(); 155 const auto& items = data->items();
153 for (const auto& item : items) { 156 for (const auto& item : items) {
154 switch (item->type()) { 157 switch (item->type()) {
155 case storage::DataElement::TYPE_BYTES: { 158 case storage::DataElement::TYPE_BYTES: {
(...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after
262 return expected_side_data == actual_body; 265 return expected_side_data == actual_body;
263 } 266 }
264 267
265 ServiceWorkerResponse SetCacheName(const ServiceWorkerResponse& original) { 268 ServiceWorkerResponse SetCacheName(const ServiceWorkerResponse& original) {
266 ServiceWorkerResponse result(original); 269 ServiceWorkerResponse result(original);
267 result.is_in_cache_storage = true; 270 result.is_in_cache_storage = true;
268 result.cache_storage_cache_name = kCacheName; 271 result.cache_storage_cache_name = kCacheName;
269 return result; 272 return result;
270 } 273 }
271 274
275 std::unique_ptr<crypto::SymmetricKey> CreateTestPaddingKey() {
276 return crypto::SymmetricKey::Import(crypto::SymmetricKey::HMAC_SHA1,
277 "abc123");
278 }
279
272 } // namespace 280 } // namespace
273 281
274 // A CacheStorageCache that can optionally delay during backend creation. 282 // A CacheStorageCache that can optionally delay during backend creation.
275 class TestCacheStorageCache : public CacheStorageCache { 283 class TestCacheStorageCache : public CacheStorageCache {
276 public: 284 public:
277 TestCacheStorageCache( 285 TestCacheStorageCache(
278 const GURL& origin, 286 const GURL& origin,
279 const std::string& cache_name, 287 const std::string& cache_name,
280 const base::FilePath& path, 288 const base::FilePath& path,
281 CacheStorage* cache_storage, 289 CacheStorage* cache_storage,
282 const scoped_refptr<net::URLRequestContextGetter>& request_context_getter, 290 const scoped_refptr<net::URLRequestContextGetter>& request_context_getter,
283 const scoped_refptr<storage::QuotaManagerProxy>& quota_manager_proxy, 291 const scoped_refptr<storage::QuotaManagerProxy>& quota_manager_proxy,
284 base::WeakPtr<storage::BlobStorageContext> blob_context) 292 base::WeakPtr<storage::BlobStorageContext> blob_context)
285 : CacheStorageCache(origin, 293 : CacheStorageCache(origin,
286 cache_name, 294 cache_name,
287 path, 295 path,
288 cache_storage, 296 cache_storage,
289 request_context_getter, 297 request_context_getter,
290 quota_manager_proxy, 298 quota_manager_proxy,
291 blob_context, 299 blob_context,
292 0 /* cache_size */), 300 0 /* cache_size */,
301 0 /* cache_padding */,
302 CreateTestPaddingKey()),
293 delay_backend_creation_(false) {} 303 delay_backend_creation_(false) {}
294 304
295 void CreateBackend(ErrorCallback callback) override { 305 void CreateBackend(ErrorCallback callback) override {
296 backend_creation_callback_ = std::move(callback); 306 backend_creation_callback_ = std::move(callback);
297 if (delay_backend_creation_) 307 if (delay_backend_creation_)
298 return; 308 return;
299 ContinueCreateBackend(); 309 ContinueCreateBackend();
300 } 310 }
301 311
302 void ContinueCreateBackend() { 312 void ContinueCreateBackend() {
(...skipping 1283 matching lines...) Expand 10 before | Expand all | Expand 10 after
1586 EXPECT_TRUE(Delete(no_body_request_)); 1596 EXPECT_TRUE(Delete(no_body_request_));
1587 EXPECT_EQ(0, Size()); 1597 EXPECT_EQ(0, Size());
1588 1598
1589 EXPECT_TRUE(Put(body_request_, body_response_)); 1599 EXPECT_TRUE(Put(body_request_, body_response_));
1590 EXPECT_LT(no_body_size, Size()); 1600 EXPECT_LT(no_body_size, Size());
1591 1601
1592 EXPECT_TRUE(Delete(body_request_)); 1602 EXPECT_TRUE(Delete(body_request_));
1593 EXPECT_EQ(0, Size()); 1603 EXPECT_EQ(0, Size());
1594 } 1604 }
1595 1605
1606 TEST_F(CacheStorageCacheTest, VerifyOpaqueSizePadding) {
1607 ServiceWorkerFetchRequest non_opaque_request(body_request_);
1608 non_opaque_request.url = GURL("http://example.com/no-pad.html");
1609 ServiceWorkerResponse non_opaque_response(body_response_);
1610 const int side_data_size = 0;
1611 EXPECT_EQ(0, CacheStorageCache::CalculateResponsePadding(
1612 non_opaque_response, CreateTestPaddingKey().get(),
1613 side_data_size));
1614 EXPECT_TRUE(Put(non_opaque_request, non_opaque_response));
1615 int64_t non_padded_cache_size = Size();
1616
1617 ServiceWorkerFetchRequest opaque_request(non_opaque_request);
1618 opaque_request.url = GURL("http://example.com/opaque.html");
1619 // Same URL length means same cache sizes (ignoring padding).
1620 EXPECT_EQ(opaque_request.url.spec().length(),
1621 non_opaque_request.url.spec().length());
1622 ServiceWorkerResponse opaque_response(non_opaque_response);
1623 opaque_response.response_type = blink::kWebServiceWorkerResponseTypeOpaque;
1624 base::Time response_time(base::Time::Now());
1625 opaque_response.response_time = response_time;
1626
1627 EXPECT_TRUE(Put(opaque_request, opaque_response));
1628 // Padding can be zero bytes, so if the opaque URL + padding key changes in
jkarlin 2017/07/21 21:18:09 Suggest: // This test is fragile. Right now it de
cmumford 2017/07/27 23:33:24 Done.
1629 // the future check for this.
1630 int64_t opaque_cache_size = Size();
1631 EXPECT_GT(opaque_cache_size, 2 * non_padded_cache_size);
1632
1633 // Now write side data and expect to see the padding change.
1634 const std::string expected_side_data1 = "OpaqueSideData";
1635 scoped_refptr<net::IOBuffer> buffer1(
1636 new net::StringIOBuffer(expected_side_data1));
1637 EXPECT_TRUE(WriteSideData(opaque_request.url, response_time, buffer1,
1638 expected_side_data1.length()));
1639 int64_t opaque_cache_size_with_side_data = Size();
1640 EXPECT_GT(opaque_cache_size_with_side_data, 2 * non_padded_cache_size);
1641 EXPECT_NE(opaque_cache_size_with_side_data, opaque_cache_size);
jkarlin 2017/07/21 21:18:09 How is this testing that the padding changed? It's
cmumford 2017/07/27 23:33:24 Agreed. I've changed a fair amount of this functio
1642
1643 // Now reset side data back to zero.
1644 const std::string expected_side_data2 = "";
1645 scoped_refptr<net::IOBuffer> buffer2(
1646 new net::StringIOBuffer(expected_side_data2));
1647 EXPECT_TRUE(WriteSideData(opaque_request.url, response_time, buffer2,
1648 expected_side_data2.length()));
1649 int64_t size_after_delete = Size();
1650 EXPECT_EQ(size_after_delete, opaque_cache_size);
jkarlin 2017/07/21 21:18:09 Also delete the opaque data and verify that the si
cmumford 2017/07/27 23:33:24 Done.
1651 }
1652
jkarlin 2017/07/21 21:18:09 We need some cache_storage_manager_unittest.cc tes
cmumford 2017/07/27 23:33:24 I added CacheStorageManagerTest.CacheSizePaddedAft
1596 TEST_P(CacheStorageCacheTestP, GetSizeThenClose) { 1653 TEST_P(CacheStorageCacheTestP, GetSizeThenClose) {
1597 EXPECT_TRUE(Put(body_request_, body_response_)); 1654 EXPECT_TRUE(Put(body_request_, body_response_));
1598 int64_t cache_size = Size(); 1655 int64_t cache_size = Size();
1599 EXPECT_EQ(cache_size, GetSizeThenClose()); 1656 EXPECT_EQ(cache_size, GetSizeThenClose());
1600 VerifyAllOpsFail(); 1657 VerifyAllOpsFail();
1601 } 1658 }
1602 1659
1603 TEST_P(CacheStorageCacheTestP, OpsFailOnClosedBackend) { 1660 TEST_P(CacheStorageCacheTestP, OpsFailOnClosedBackend) {
1604 // Create the backend and put something in it. 1661 // Create the backend and put something in it.
1605 EXPECT_TRUE(Put(body_request_, body_response_)); 1662 EXPECT_TRUE(Put(body_request_, body_response_));
1606 EXPECT_TRUE(Close()); 1663 EXPECT_TRUE(Close());
1607 VerifyAllOpsFail(); 1664 VerifyAllOpsFail();
1608 } 1665 }
1609 1666
1610 TEST_P(CacheStorageCacheTestP, VerifySerialScheduling) { 1667 TEST_P(CacheStorageCacheTestP, VerifySerialScheduling) {
1611 // Start two operations, the first one is delayed but the second isn't. The 1668 // Start two operations, the first one is delayed but the second isn't. The
1612 // second should wait for the first. 1669 // second should wait for the first.
1613 EXPECT_TRUE(Keys()); // Opens the backend. 1670 EXPECT_TRUE(Keys()); // Opens the backend.
1614 DelayableBackend* delayable_backend = cache_->UseDelayableBackend(); 1671 DelayableBackend* delayable_backend = cache_->UseDelayableBackend();
1615 delayable_backend->set_delay_doom(true); 1672 delayable_backend->set_delay_open_entry(true);
1616 1673
1617 int sequence_out = -1; 1674 int sequence_out = -1;
1618 1675
1619 CacheStorageBatchOperation operation1; 1676 CacheStorageBatchOperation operation1;
1620 operation1.operation_type = CACHE_STORAGE_CACHE_OPERATION_TYPE_PUT; 1677 operation1.operation_type = CACHE_STORAGE_CACHE_OPERATION_TYPE_PUT;
1621 operation1.request = body_request_; 1678 operation1.request = body_request_;
1622 operation1.response = body_response_; 1679 operation1.response = body_response_;
1623 1680
1624 std::unique_ptr<base::RunLoop> close_loop1(new base::RunLoop()); 1681 std::unique_ptr<base::RunLoop> close_loop1(new base::RunLoop());
1625 cache_->BatchOperation( 1682 cache_->BatchOperation(
1626 std::vector<CacheStorageBatchOperation>(1, operation1), 1683 std::vector<CacheStorageBatchOperation>(1, operation1),
1627 base::BindOnce(&CacheStorageCacheTest::SequenceCallback, 1684 base::BindOnce(&CacheStorageCacheTest::SequenceCallback,
1628 base::Unretained(this), 1, &sequence_out, 1685 base::Unretained(this), 1, &sequence_out,
1629 close_loop1.get())); 1686 close_loop1.get()));
1630 1687
1631 // Blocks on creating the cache entry. 1688 // Blocks on creating the cache entry.
1632 base::RunLoop().RunUntilIdle(); 1689 base::RunLoop().RunUntilIdle();
1633 1690
1634 CacheStorageBatchOperation operation2; 1691 CacheStorageBatchOperation operation2;
1635 operation2.operation_type = CACHE_STORAGE_CACHE_OPERATION_TYPE_PUT; 1692 operation2.operation_type = CACHE_STORAGE_CACHE_OPERATION_TYPE_PUT;
1636 operation2.request = body_request_; 1693 operation2.request = body_request_;
1637 operation2.response = body_response_; 1694 operation2.response = body_response_;
1638 1695
1639 delayable_backend->set_delay_doom(false); 1696 delayable_backend->set_delay_open_entry(false);
1640 std::unique_ptr<base::RunLoop> close_loop2(new base::RunLoop()); 1697 std::unique_ptr<base::RunLoop> close_loop2(new base::RunLoop());
1641 cache_->BatchOperation( 1698 cache_->BatchOperation(
1642 std::vector<CacheStorageBatchOperation>(1, operation2), 1699 std::vector<CacheStorageBatchOperation>(1, operation2),
1643 base::BindOnce(&CacheStorageCacheTest::SequenceCallback, 1700 base::BindOnce(&CacheStorageCacheTest::SequenceCallback,
1644 base::Unretained(this), 2, &sequence_out, 1701 base::Unretained(this), 2, &sequence_out,
1645 close_loop2.get())); 1702 close_loop2.get()));
1646 1703
1647 // The second put operation should wait for the first to complete. 1704 // The second put operation should wait for the first to complete.
1648 base::RunLoop().RunUntilIdle(); 1705 base::RunLoop().RunUntilIdle();
1649 EXPECT_FALSE(callback_response_); 1706 EXPECT_FALSE(callback_response_);
1650 1707
1651 delayable_backend->DoomEntryContinue(); 1708 EXPECT_TRUE(delayable_backend->OpenEntryContinue());
1652 close_loop1->Run(); 1709 close_loop1->Run();
1653 EXPECT_EQ(1, sequence_out); 1710 EXPECT_EQ(1, sequence_out);
1654 close_loop2->Run(); 1711 close_loop2->Run();
1655 EXPECT_EQ(2, sequence_out); 1712 EXPECT_EQ(2, sequence_out);
1656 } 1713 }
1657 1714
1658 INSTANTIATE_TEST_CASE_P(CacheStorageCacheTest, 1715 INSTANTIATE_TEST_CASE_P(CacheStorageCacheTest,
1659 CacheStorageCacheTestP, 1716 CacheStorageCacheTestP,
1660 ::testing::Values(false, true)); 1717 ::testing::Values(false, true));
1661 1718
1662 } // namespace content 1719 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698