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

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

Issue 2416713002: Write out CacheStorageCache size to index file. (Closed)
Patch Set: Posting index write to the IO thread. Created 3 years, 12 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_manager.h" 5 #include "content/browser/cache_storage/cache_storage_manager.h"
6 6
7 #include <stddef.h> 7 #include <stddef.h>
8 #include <stdint.h> 8 #include <stdint.h>
9 9
10 #include <list>
10 #include <set> 11 #include <set>
11 #include <utility> 12 #include <utility>
12 13
14 #include "base/files/file_enumerator.h"
13 #include "base/files/file_path.h" 15 #include "base/files/file_path.h"
14 #include "base/files/file_util.h" 16 #include "base/files/file_util.h"
15 #include "base/files/scoped_temp_dir.h" 17 #include "base/files/scoped_temp_dir.h"
16 #include "base/guid.h" 18 #include "base/guid.h"
17 #include "base/macros.h" 19 #include "base/macros.h"
18 #include "base/memory/ptr_util.h" 20 #include "base/memory/ptr_util.h"
19 #include "base/run_loop.h" 21 #include "base/run_loop.h"
20 #include "base/sha1.h" 22 #include "base/sha1.h"
21 #include "base/stl_util.h" 23 #include "base/stl_util.h"
22 #include "base/strings/string_number_conversions.h" 24 #include "base/strings/string_number_conversions.h"
23 #include "base/threading/thread_task_runner_handle.h" 25 #include "base/threading/thread_task_runner_handle.h"
24 #include "content/browser/blob_storage/chrome_blob_storage_context.h" 26 #include "content/browser/blob_storage/chrome_blob_storage_context.h"
27 #include "content/browser/cache_storage/cache_storage.h"
25 #include "content/browser/cache_storage/cache_storage.pb.h" 28 #include "content/browser/cache_storage/cache_storage.pb.h"
26 #include "content/browser/cache_storage/cache_storage_cache_handle.h" 29 #include "content/browser/cache_storage/cache_storage_cache_handle.h"
30 #include "content/browser/cache_storage/cache_storage_index.h"
27 #include "content/browser/cache_storage/cache_storage_quota_client.h" 31 #include "content/browser/cache_storage/cache_storage_quota_client.h"
28 #include "content/browser/quota/mock_quota_manager_proxy.h" 32 #include "content/browser/quota/mock_quota_manager_proxy.h"
29 #include "content/public/browser/browser_thread.h" 33 #include "content/public/browser/browser_thread.h"
30 #include "content/public/browser/cache_storage_usage_info.h" 34 #include "content/public/browser/cache_storage_usage_info.h"
31 #include "content/public/browser/storage_partition.h" 35 #include "content/public/browser/storage_partition.h"
32 #include "content/public/test/mock_special_storage_policy.h" 36 #include "content/public/test/mock_special_storage_policy.h"
33 #include "content/public/test/test_browser_context.h" 37 #include "content/public/test/test_browser_context.h"
34 #include "content/public/test/test_browser_thread_bundle.h" 38 #include "content/public/test/test_browser_thread_bundle.h"
35 #include "net/url_request/url_request_context.h" 39 #include "net/url_request/url_request_context.h"
36 #include "net/url_request/url_request_context_getter.h" 40 #include "net/url_request/url_request_context_getter.h"
37 #include "net/url_request/url_request_job_factory_impl.h" 41 #include "net/url_request/url_request_job_factory_impl.h"
38 #include "storage/browser/blob/blob_data_builder.h" 42 #include "storage/browser/blob/blob_data_builder.h"
39 #include "storage/browser/blob/blob_data_handle.h" 43 #include "storage/browser/blob/blob_data_handle.h"
40 #include "storage/browser/blob/blob_storage_context.h" 44 #include "storage/browser/blob/blob_storage_context.h"
41 #include "storage/browser/blob/blob_url_request_job_factory.h" 45 #include "storage/browser/blob/blob_url_request_job_factory.h"
42 #include "storage/browser/quota/quota_manager_proxy.h" 46 #include "storage/browser/quota/quota_manager_proxy.h"
43 #include "testing/gtest/include/gtest/gtest.h" 47 #include "testing/gtest/include/gtest/gtest.h"
44 48
45 namespace content { 49 namespace content {
46 50
51 namespace {
52
53 bool IsIndexFileCurrent(const base::FilePath& cache_dir) {
54 base::File::Info info;
55 const base::FilePath index_path =
56 cache_dir.AppendASCII(CacheStorage::kIndexFileName);
57 if (!GetFileInfo(index_path, &info))
58 return false;
59 base::Time index_last_modified = info.last_modified;
60
61 base::FileEnumerator enumerator(cache_dir, false,
62 base::FileEnumerator::DIRECTORIES);
63 for (base::FilePath file_path = enumerator.Next(); !file_path.empty();
64 file_path = enumerator.Next()) {
65 if (!GetFileInfo(file_path, &info))
66 return false;
67 if (index_last_modified < info.last_modified)
68 return false;
69 }
70
71 return true;
72 }
73
74 void CopyCacheStorageIndex(CacheStorageIndex* dest,
75 const CacheStorageIndex& src) {
76 DCHECK_EQ(0U, dest->num_entries());
77 for (const auto& cache_metadata : src.ordered_cache_metadata())
78 dest->Insert(cache_metadata);
79 }
80
81 } // anonymous namespace
82
47 // Returns a BlobProtocolHandler that uses |blob_storage_context|. Caller owns 83 // Returns a BlobProtocolHandler that uses |blob_storage_context|. Caller owns
48 // the memory. 84 // the memory.
49 std::unique_ptr<storage::BlobProtocolHandler> CreateMockBlobProtocolHandler( 85 std::unique_ptr<storage::BlobProtocolHandler> CreateMockBlobProtocolHandler(
50 storage::BlobStorageContext* blob_storage_context) { 86 storage::BlobStorageContext* blob_storage_context) {
51 // The FileSystemContext and thread task runner are not actually used but a 87 // The FileSystemContext and thread task runner are not actually used but a
52 // task runner is needed to avoid a DCHECK in BlobURLRequestJob ctor. 88 // task runner is needed to avoid a DCHECK in BlobURLRequestJob ctor.
53 return base::WrapUnique(new storage::BlobProtocolHandler( 89 return base::WrapUnique(new storage::BlobProtocolHandler(
54 blob_storage_context, NULL, base::ThreadTaskRunnerHandle::Get().get())); 90 blob_storage_context, NULL, base::ThreadTaskRunnerHandle::Get().get()));
55 } 91 }
56 92
57 class CacheStorageManagerTest : public testing::Test { 93 class CacheStorageManagerTest : public testing::Test {
58 public: 94 public:
59 CacheStorageManagerTest() 95 CacheStorageManagerTest()
60 : browser_thread_bundle_(TestBrowserThreadBundle::IO_MAINLOOP), 96 : browser_thread_bundle_(TestBrowserThreadBundle::IO_MAINLOOP),
61 blob_storage_context_(nullptr), 97 blob_storage_context_(nullptr),
62 callback_bool_(false), 98 callback_bool_(false),
63 callback_error_(CACHE_STORAGE_OK), 99 callback_error_(CACHE_STORAGE_OK),
64 origin1_("http://example1.com"), 100 origin1_("http://example1.com"),
65 origin2_("http://example2.com") {} 101 origin2_("http://example2.com") {}
66 102
67 void SetUp() override { 103 void SetUp() override {
104 base::FilePath temp_dir_path;
105 if (!MemoryOnly())
106 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
107
108 CreateStorageManager();
109 }
110
111 void TearDown() override { DestroyStorageManager(); }
112
113 virtual bool MemoryOnly() { return false; }
114
115 void BoolAndErrorCallback(base::RunLoop* run_loop,
116 bool value,
117 CacheStorageError error) {
118 callback_bool_ = value;
119 callback_error_ = error;
120 run_loop->Quit();
121 }
122
123 void CacheAndErrorCallback(
124 base::RunLoop* run_loop,
125 std::unique_ptr<CacheStorageCacheHandle> cache_handle,
126 CacheStorageError error) {
127 callback_cache_handle_ = std::move(cache_handle);
128 callback_error_ = error;
129 run_loop->Quit();
130 }
131
132 void CacheMetadataCallback(base::RunLoop* run_loop,
133 const CacheStorageIndex& cache_index) {
134 callback_cache_index_ = CacheStorageIndex();
135 CopyCacheStorageIndex(&callback_cache_index_, cache_index);
136 run_loop->Quit();
137 }
138
139 const std::string& GetFirstIndexName() const {
140 return callback_cache_index_.ordered_cache_metadata().front().name;
141 }
142
143 std::vector<std::string> GetIndexNames() const {
144 std::vector<std::string> cache_names;
145 for (const auto& metadata : callback_cache_index_.ordered_cache_metadata())
146 cache_names.push_back(metadata.name);
147 return cache_names;
148 }
149
150 void CachePutCallback(base::RunLoop* run_loop, CacheStorageError error) {
151 callback_error_ = error;
152 run_loop->Quit();
153 }
154
155 void CacheMatchCallback(
156 base::RunLoop* run_loop,
157 CacheStorageError error,
158 std::unique_ptr<ServiceWorkerResponse> response,
159 std::unique_ptr<storage::BlobDataHandle> blob_data_handle) {
160 callback_error_ = error;
161 callback_cache_handle_response_ = std::move(response);
162 callback_data_handle_ = std::move(blob_data_handle);
163 run_loop->Quit();
164 }
165
166 void CreateStorageManager() {
68 ChromeBlobStorageContext* blob_storage_context( 167 ChromeBlobStorageContext* blob_storage_context(
69 ChromeBlobStorageContext::GetFor(&browser_context_)); 168 ChromeBlobStorageContext::GetFor(&browser_context_));
70 // Wait for ChromeBlobStorageContext to finish initializing. 169 // Wait for ChromeBlobStorageContext to finish initializing.
71 base::RunLoop().RunUntilIdle(); 170 base::RunLoop().RunUntilIdle();
72 171
73 blob_storage_context_ = blob_storage_context->context(); 172 blob_storage_context_ = blob_storage_context->context();
74 173
75 url_request_job_factory_.reset(new net::URLRequestJobFactoryImpl); 174 url_request_job_factory_.reset(new net::URLRequestJobFactoryImpl);
76 url_request_job_factory_->SetProtocolHandler( 175 url_request_job_factory_->SetProtocolHandler(
77 "blob", CreateMockBlobProtocolHandler(blob_storage_context->context())); 176 "blob", CreateMockBlobProtocolHandler(blob_storage_context->context()));
78 177
79 net::URLRequestContext* url_request_context = 178 net::URLRequestContext* url_request_context =
80 BrowserContext::GetDefaultStoragePartition(&browser_context_)-> 179 BrowserContext::GetDefaultStoragePartition(&browser_context_)->
81 GetURLRequestContext()->GetURLRequestContext(); 180 GetURLRequestContext()->GetURLRequestContext();
82 181
83 url_request_context->set_job_factory(url_request_job_factory_.get()); 182 url_request_context->set_job_factory(url_request_job_factory_.get());
84 183
85 const bool is_incognito = MemoryOnly();
86 base::FilePath temp_dir_path; 184 base::FilePath temp_dir_path;
87 if (!is_incognito) { 185 if (!MemoryOnly())
88 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
89 temp_dir_path = temp_dir_.GetPath(); 186 temp_dir_path = temp_dir_.GetPath();
90 }
91 187
92 quota_policy_ = new MockSpecialStoragePolicy; 188 quota_policy_ = new MockSpecialStoragePolicy;
93 mock_quota_manager_ = new MockQuotaManager( 189 mock_quota_manager_ = new MockQuotaManager(
94 is_incognito, temp_dir_path, base::ThreadTaskRunnerHandle::Get().get(), 190 MemoryOnly(), temp_dir_path, base::ThreadTaskRunnerHandle::Get().get(),
95 base::ThreadTaskRunnerHandle::Get().get(), quota_policy_.get()); 191 base::ThreadTaskRunnerHandle::Get().get(), quota_policy_.get());
96 mock_quota_manager_->SetQuota( 192 mock_quota_manager_->SetQuota(
97 GURL(origin1_), storage::kStorageTypeTemporary, 1024 * 1024 * 100); 193 GURL(origin1_), storage::kStorageTypeTemporary, 1024 * 1024 * 100);
98 mock_quota_manager_->SetQuota( 194 mock_quota_manager_->SetQuota(
99 GURL(origin2_), storage::kStorageTypeTemporary, 1024 * 1024 * 100); 195 GURL(origin2_), storage::kStorageTypeTemporary, 1024 * 1024 * 100);
100 196
101 quota_manager_proxy_ = new MockQuotaManagerProxy( 197 quota_manager_proxy_ = new MockQuotaManagerProxy(
102 mock_quota_manager_.get(), base::ThreadTaskRunnerHandle::Get().get()); 198 mock_quota_manager_.get(), base::ThreadTaskRunnerHandle::Get().get());
103 199
104 cache_manager_ = CacheStorageManager::Create( 200 cache_manager_ = CacheStorageManager::Create(
105 temp_dir_path, base::ThreadTaskRunnerHandle::Get(), 201 temp_dir_path, base::ThreadTaskRunnerHandle::Get(),
106 quota_manager_proxy_); 202 quota_manager_proxy_);
107 203
108 cache_manager_->SetBlobParametersForCache( 204 cache_manager_->SetBlobParametersForCache(
109 BrowserContext::GetDefaultStoragePartition(&browser_context_)-> 205 BrowserContext::GetDefaultStoragePartition(&browser_context_)
110 GetURLRequestContext(), 206 ->GetURLRequestContext(),
111 blob_storage_context->context()->AsWeakPtr()); 207 blob_storage_context->context()->AsWeakPtr());
112 } 208 }
113 209
114 void TearDown() override { 210 bool FlushCacheStorageIndex(const GURL& origin) {
211 bool write_scheduled =
212 CacheStorageForOrigin(origin)->InitiateScheduledIndexWriteForTest();
213 // Wait for write to complete.
214 base::RunLoop().RunUntilIdle();
215 return write_scheduled;
216 }
217
218 void DestroyStorageManager() {
115 quota_manager_proxy_->SimulateQuotaManagerDestroyed(); 219 quota_manager_proxy_->SimulateQuotaManagerDestroyed();
116 base::RunLoop().RunUntilIdle(); 220 base::RunLoop().RunUntilIdle();
117 } 221 quota_manager_proxy_ = nullptr;
118 222
119 virtual bool MemoryOnly() { return false; } 223 url_request_job_factory_.reset();
224 blob_storage_context_ = nullptr;
120 225
121 void BoolAndErrorCallback(base::RunLoop* run_loop, 226 quota_policy_ = nullptr;
122 bool value, 227 mock_quota_manager_ = nullptr;
123 CacheStorageError error) { 228 quota_manager_proxy_ = nullptr;
124 callback_bool_ = value;
125 callback_error_ = error;
126 run_loop->Quit();
127 }
128 229
129 void CacheAndErrorCallback( 230 callback_cache_handle_ = nullptr;
130 base::RunLoop* run_loop, 231 callback_bool_ = false;
131 std::unique_ptr<CacheStorageCacheHandle> cache_handle, 232 callback_cache_handle_response_ = nullptr;
132 CacheStorageError error) { 233 callback_data_handle_ = nullptr;
133 callback_cache_handle_ = std::move(cache_handle); 234 callback_cache_index_ = CacheStorageIndex();
134 callback_error_ = error; 235 callback_all_origins_usage_.clear();
135 run_loop->Quit();
136 }
137 236
138 void StringsCallback(base::RunLoop* run_loop, 237 cache_manager_ = nullptr;
139 const std::vector<std::string>& strings) {
140 callback_strings_ = strings;
141 run_loop->Quit();
142 }
143
144 void CachePutCallback(base::RunLoop* run_loop, CacheStorageError error) {
145 callback_error_ = error;
146 run_loop->Quit();
147 }
148
149 void CacheMatchCallback(
150 base::RunLoop* run_loop,
151 CacheStorageError error,
152 std::unique_ptr<ServiceWorkerResponse> response,
153 std::unique_ptr<storage::BlobDataHandle> blob_data_handle) {
154 callback_error_ = error;
155 callback_cache_handle_response_ = std::move(response);
156 callback_data_handle_ = std::move(blob_data_handle);
157 run_loop->Quit();
158 } 238 }
159 239
160 bool Open(const GURL& origin, const std::string& cache_name) { 240 bool Open(const GURL& origin, const std::string& cache_name) {
161 base::RunLoop loop; 241 base::RunLoop loop;
162 cache_manager_->OpenCache( 242 cache_manager_->OpenCache(
163 origin, cache_name, 243 origin, cache_name,
164 base::Bind(&CacheStorageManagerTest::CacheAndErrorCallback, 244 base::Bind(&CacheStorageManagerTest::CacheAndErrorCallback,
165 base::Unretained(this), base::Unretained(&loop))); 245 base::Unretained(this), base::Unretained(&loop)));
166 loop.Run(); 246 loop.Run();
167 247
(...skipping 23 matching lines...) Expand all
191 base::Bind(&CacheStorageManagerTest::BoolAndErrorCallback, 271 base::Bind(&CacheStorageManagerTest::BoolAndErrorCallback,
192 base::Unretained(this), base::Unretained(&loop))); 272 base::Unretained(this), base::Unretained(&loop)));
193 loop.Run(); 273 loop.Run();
194 274
195 return callback_bool_; 275 return callback_bool_;
196 } 276 }
197 277
198 size_t Keys(const GURL& origin) { 278 size_t Keys(const GURL& origin) {
199 base::RunLoop loop; 279 base::RunLoop loop;
200 cache_manager_->EnumerateCaches( 280 cache_manager_->EnumerateCaches(
201 origin, base::Bind(&CacheStorageManagerTest::StringsCallback, 281 origin, base::Bind(&CacheStorageManagerTest::CacheMetadataCallback,
202 base::Unretained(this), base::Unretained(&loop))); 282 base::Unretained(this), base::Unretained(&loop)));
203 loop.Run(); 283 loop.Run();
204 return callback_strings_.size(); 284 return callback_cache_index_.num_entries();
205 } 285 }
206 286
207 bool StorageMatch(const GURL& origin, 287 bool StorageMatch(const GURL& origin,
208 const std::string& cache_name, 288 const std::string& cache_name,
209 const GURL& url, 289 const GURL& url,
210 const CacheStorageCacheQueryParams& match_params = 290 const CacheStorageCacheQueryParams& match_params =
211 CacheStorageCacheQueryParams()) { 291 CacheStorageCacheQueryParams()) {
212 ServiceWorkerFetchRequest request; 292 ServiceWorkerFetchRequest request;
213 request.url = url; 293 request.url = url;
214 return StorageMatchWithRequest(origin, cache_name, request, match_params); 294 return StorageMatchWithRequest(origin, cache_name, request, match_params);
(...skipping 176 matching lines...) Expand 10 before | Expand all | Expand 10 after
391 scoped_refptr<MockSpecialStoragePolicy> quota_policy_; 471 scoped_refptr<MockSpecialStoragePolicy> quota_policy_;
392 scoped_refptr<MockQuotaManager> mock_quota_manager_; 472 scoped_refptr<MockQuotaManager> mock_quota_manager_;
393 scoped_refptr<MockQuotaManagerProxy> quota_manager_proxy_; 473 scoped_refptr<MockQuotaManagerProxy> quota_manager_proxy_;
394 std::unique_ptr<CacheStorageManager> cache_manager_; 474 std::unique_ptr<CacheStorageManager> cache_manager_;
395 475
396 std::unique_ptr<CacheStorageCacheHandle> callback_cache_handle_; 476 std::unique_ptr<CacheStorageCacheHandle> callback_cache_handle_;
397 int callback_bool_; 477 int callback_bool_;
398 CacheStorageError callback_error_; 478 CacheStorageError callback_error_;
399 std::unique_ptr<ServiceWorkerResponse> callback_cache_handle_response_; 479 std::unique_ptr<ServiceWorkerResponse> callback_cache_handle_response_;
400 std::unique_ptr<storage::BlobDataHandle> callback_data_handle_; 480 std::unique_ptr<storage::BlobDataHandle> callback_data_handle_;
401 std::vector<std::string> callback_strings_; 481 CacheStorageIndex callback_cache_index_;
402 482
403 const GURL origin1_; 483 const GURL origin1_;
404 const GURL origin2_; 484 const GURL origin2_;
405 485
406 int64_t callback_usage_; 486 int64_t callback_usage_;
407 std::vector<CacheStorageUsageInfo> callback_all_origins_usage_; 487 std::vector<CacheStorageUsageInfo> callback_all_origins_usage_;
408 488
409 private: 489 private:
410 DISALLOW_COPY_AND_ASSIGN(CacheStorageManagerTest); 490 DISALLOW_COPY_AND_ASSIGN(CacheStorageManagerTest);
411 }; 491 };
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after
504 } 584 }
505 585
506 TEST_P(CacheStorageManagerTestP, SomeKeys) { 586 TEST_P(CacheStorageManagerTestP, SomeKeys) {
507 EXPECT_TRUE(Open(origin1_, "foo")); 587 EXPECT_TRUE(Open(origin1_, "foo"));
508 EXPECT_TRUE(Open(origin1_, "bar")); 588 EXPECT_TRUE(Open(origin1_, "bar"));
509 EXPECT_TRUE(Open(origin2_, "baz")); 589 EXPECT_TRUE(Open(origin2_, "baz"));
510 EXPECT_EQ(2u, Keys(origin1_)); 590 EXPECT_EQ(2u, Keys(origin1_));
511 std::vector<std::string> expected_keys; 591 std::vector<std::string> expected_keys;
512 expected_keys.push_back("foo"); 592 expected_keys.push_back("foo");
513 expected_keys.push_back("bar"); 593 expected_keys.push_back("bar");
514 EXPECT_EQ(expected_keys, callback_strings_); 594 EXPECT_EQ(expected_keys, GetIndexNames());
515 EXPECT_EQ(1u, Keys(origin2_)); 595 EXPECT_EQ(1u, Keys(origin2_));
516 EXPECT_STREQ("baz", callback_strings_[0].c_str()); 596 EXPECT_STREQ("baz", GetFirstIndexName().c_str());
517 } 597 }
518 598
519 TEST_P(CacheStorageManagerTestP, DeletedKeysGone) { 599 TEST_P(CacheStorageManagerTestP, DeletedKeysGone) {
520 EXPECT_TRUE(Open(origin1_, "foo")); 600 EXPECT_TRUE(Open(origin1_, "foo"));
521 EXPECT_TRUE(Open(origin1_, "bar")); 601 EXPECT_TRUE(Open(origin1_, "bar"));
522 EXPECT_TRUE(Open(origin2_, "baz")); 602 EXPECT_TRUE(Open(origin2_, "baz"));
523 EXPECT_TRUE(Delete(origin1_, "bar")); 603 EXPECT_TRUE(Delete(origin1_, "bar"));
524 EXPECT_EQ(1u, Keys(origin1_)); 604 EXPECT_EQ(1u, Keys(origin1_));
525 EXPECT_STREQ("foo", callback_strings_[0].c_str()); 605 EXPECT_STREQ("foo", GetFirstIndexName().c_str());
526 } 606 }
527 607
528 TEST_P(CacheStorageManagerTestP, StorageMatchEntryExists) { 608 TEST_P(CacheStorageManagerTestP, StorageMatchEntryExists) {
529 EXPECT_TRUE(Open(origin1_, "foo")); 609 EXPECT_TRUE(Open(origin1_, "foo"));
530 EXPECT_TRUE(CachePut(callback_cache_handle_->value(), 610 EXPECT_TRUE(CachePut(callback_cache_handle_->value(),
531 GURL("http://example.com/foo"))); 611 GURL("http://example.com/foo")));
532 EXPECT_TRUE(StorageMatch(origin1_, "foo", GURL("http://example.com/foo"))); 612 EXPECT_TRUE(StorageMatch(origin1_, "foo", GURL("http://example.com/foo")));
533 } 613 }
534 614
535 TEST_P(CacheStorageManagerTestP, StorageMatchNoEntry) { 615 TEST_P(CacheStorageManagerTestP, StorageMatchNoEntry) {
(...skipping 125 matching lines...) Expand 10 before | Expand all | Expand 10 after
661 EXPECT_TRUE(StorageMatchAll(origin1_, GURL("http://example.com/foo"))); 741 EXPECT_TRUE(StorageMatchAll(origin1_, GURL("http://example.com/foo")));
662 } 742 }
663 743
664 TEST_P(CacheStorageManagerTestP, Chinese) { 744 TEST_P(CacheStorageManagerTestP, Chinese) {
665 EXPECT_TRUE(Open(origin1_, "你好")); 745 EXPECT_TRUE(Open(origin1_, "你好"));
666 std::unique_ptr<CacheStorageCacheHandle> cache_handle = 746 std::unique_ptr<CacheStorageCacheHandle> cache_handle =
667 std::move(callback_cache_handle_); 747 std::move(callback_cache_handle_);
668 EXPECT_TRUE(Open(origin1_, "你好")); 748 EXPECT_TRUE(Open(origin1_, "你好"));
669 EXPECT_EQ(callback_cache_handle_->value(), cache_handle->value()); 749 EXPECT_EQ(callback_cache_handle_->value(), cache_handle->value());
670 EXPECT_EQ(1u, Keys(origin1_)); 750 EXPECT_EQ(1u, Keys(origin1_));
671 EXPECT_STREQ("你好", callback_strings_[0].c_str()); 751 EXPECT_STREQ("你好", GetFirstIndexName().c_str());
672 } 752 }
673 753
674 TEST_F(CacheStorageManagerTest, EmptyKey) { 754 TEST_F(CacheStorageManagerTest, EmptyKey) {
675 EXPECT_TRUE(Open(origin1_, "")); 755 EXPECT_TRUE(Open(origin1_, ""));
676 std::unique_ptr<CacheStorageCacheHandle> cache_handle = 756 std::unique_ptr<CacheStorageCacheHandle> cache_handle =
677 std::move(callback_cache_handle_); 757 std::move(callback_cache_handle_);
678 EXPECT_TRUE(Open(origin1_, "")); 758 EXPECT_TRUE(Open(origin1_, ""));
679 EXPECT_EQ(cache_handle->value(), callback_cache_handle_->value()); 759 EXPECT_EQ(cache_handle->value(), callback_cache_handle_->value());
680 EXPECT_EQ(1u, Keys(origin1_)); 760 EXPECT_EQ(1u, Keys(origin1_));
681 EXPECT_STREQ("", callback_strings_[0].c_str()); 761 EXPECT_STREQ("", GetFirstIndexName().c_str());
682 EXPECT_TRUE(Has(origin1_, "")); 762 EXPECT_TRUE(Has(origin1_, ""));
683 EXPECT_TRUE(Delete(origin1_, "")); 763 EXPECT_TRUE(Delete(origin1_, ""));
684 EXPECT_EQ(0u, Keys(origin1_)); 764 EXPECT_EQ(0u, Keys(origin1_));
685 } 765 }
686 766
687 TEST_F(CacheStorageManagerTest, DataPersists) { 767 TEST_F(CacheStorageManagerTest, DataPersists) {
688 EXPECT_TRUE(Open(origin1_, "foo")); 768 EXPECT_TRUE(Open(origin1_, "foo"));
689 EXPECT_TRUE(Open(origin1_, "bar")); 769 EXPECT_TRUE(Open(origin1_, "bar"));
690 EXPECT_TRUE(Open(origin1_, "baz")); 770 EXPECT_TRUE(Open(origin1_, "baz"));
691 EXPECT_TRUE(Open(origin2_, "raz")); 771 EXPECT_TRUE(Open(origin2_, "raz"));
692 EXPECT_TRUE(Delete(origin1_, "bar")); 772 EXPECT_TRUE(Delete(origin1_, "bar"));
693 quota_manager_proxy_->SimulateQuotaManagerDestroyed(); 773 quota_manager_proxy_->SimulateQuotaManagerDestroyed();
694 cache_manager_ = CacheStorageManager::Create(cache_manager_.get()); 774 cache_manager_ = CacheStorageManager::Create(cache_manager_.get());
695 EXPECT_EQ(2u, Keys(origin1_)); 775 EXPECT_EQ(2u, Keys(origin1_));
696 std::vector<std::string> expected_keys; 776 std::vector<std::string> expected_keys;
697 expected_keys.push_back("foo"); 777 expected_keys.push_back("foo");
698 expected_keys.push_back("baz"); 778 expected_keys.push_back("baz");
699 EXPECT_EQ(expected_keys, callback_strings_); 779 EXPECT_EQ(expected_keys, GetIndexNames());
700 } 780 }
701 781
702 TEST_F(CacheStorageManagerMemoryOnlyTest, DataLostWhenMemoryOnly) { 782 TEST_F(CacheStorageManagerMemoryOnlyTest, DataLostWhenMemoryOnly) {
703 EXPECT_TRUE(Open(origin1_, "foo")); 783 EXPECT_TRUE(Open(origin1_, "foo"));
704 EXPECT_TRUE(Open(origin2_, "baz")); 784 EXPECT_TRUE(Open(origin2_, "baz"));
705 quota_manager_proxy_->SimulateQuotaManagerDestroyed(); 785 quota_manager_proxy_->SimulateQuotaManagerDestroyed();
706 cache_manager_ = CacheStorageManager::Create(cache_manager_.get()); 786 cache_manager_ = CacheStorageManager::Create(cache_manager_.get());
707 EXPECT_EQ(0u, Keys(origin1_)); 787 EXPECT_EQ(0u, Keys(origin1_));
708 } 788 }
709 789
710 TEST_F(CacheStorageManagerTest, BadCacheName) { 790 TEST_F(CacheStorageManagerTest, BadCacheName) {
711 // Since the implementation writes cache names to disk, ensure that we don't 791 // Since the implementation writes cache names to disk, ensure that we don't
712 // escape the directory. 792 // escape the directory.
713 const std::string bad_name = "../../../../../../../../../../../../../../foo"; 793 const std::string bad_name = "../../../../../../../../../../../../../../foo";
714 EXPECT_TRUE(Open(origin1_, bad_name)); 794 EXPECT_TRUE(Open(origin1_, bad_name));
715 EXPECT_EQ(1u, Keys(origin1_)); 795 EXPECT_EQ(1u, Keys(origin1_));
716 EXPECT_STREQ(bad_name.c_str(), callback_strings_[0].c_str()); 796 EXPECT_STREQ(bad_name.c_str(), GetFirstIndexName().c_str());
717 } 797 }
718 798
719 TEST_F(CacheStorageManagerTest, BadOriginName) { 799 TEST_F(CacheStorageManagerTest, BadOriginName) {
720 // Since the implementation writes origin names to disk, ensure that we don't 800 // Since the implementation writes origin names to disk, ensure that we don't
721 // escape the directory. 801 // escape the directory.
722 GURL bad_origin("http://../../../../../../../../../../../../../../foo"); 802 GURL bad_origin("http://../../../../../../../../../../../../../../foo");
723 EXPECT_TRUE(Open(bad_origin, "foo")); 803 EXPECT_TRUE(Open(bad_origin, "foo"));
724 EXPECT_EQ(1u, Keys(bad_origin)); 804 EXPECT_EQ(1u, Keys(bad_origin));
725 EXPECT_STREQ("foo", callback_strings_[0].c_str()); 805 EXPECT_STREQ("foo", GetFirstIndexName().c_str());
726 } 806 }
727 807
728 // With a persistent cache if the client drops its reference to a 808 // With a persistent cache if the client drops its reference to a
729 // CacheStorageCache it should be deleted. 809 // CacheStorageCache it should be deleted.
730 TEST_F(CacheStorageManagerTest, DropReference) { 810 TEST_F(CacheStorageManagerTest, DropReference) {
731 EXPECT_TRUE(Open(origin1_, "foo")); 811 EXPECT_TRUE(Open(origin1_, "foo"));
732 base::WeakPtr<CacheStorageCache> cache = 812 base::WeakPtr<CacheStorageCache> cache =
733 callback_cache_handle_->value()->AsWeakPtr(); 813 callback_cache_handle_->value()->AsWeakPtr();
734 // Run a cache operation to ensure that the cache has finished initializing so 814 // Run a cache operation to ensure that the cache has finished initializing so
735 // that when the handle is dropped it can close immediately. 815 // that when the handle is dropped it can close immediately.
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
769 849
770 EXPECT_FALSE(CacheMatch(new_handle->value(), kFooURL)); 850 EXPECT_FALSE(CacheMatch(new_handle->value(), kFooURL));
771 EXPECT_FALSE(CacheMatch(new_handle->value(), kBarURL)); 851 EXPECT_FALSE(CacheMatch(new_handle->value(), kBarURL));
772 EXPECT_TRUE(CacheMatch(new_handle->value(), kBazURL)); 852 EXPECT_TRUE(CacheMatch(new_handle->value(), kBazURL));
773 853
774 EXPECT_TRUE(CacheMatch(original_handle->value(), kFooURL)); 854 EXPECT_TRUE(CacheMatch(original_handle->value(), kFooURL));
775 EXPECT_TRUE(CacheMatch(original_handle->value(), kBarURL)); 855 EXPECT_TRUE(CacheMatch(original_handle->value(), kBarURL));
776 EXPECT_FALSE(CacheMatch(original_handle->value(), kBazURL)); 856 EXPECT_FALSE(CacheMatch(original_handle->value(), kBazURL));
777 } 857 }
778 858
859 // Deleted caches can still be modified, but all changes will eventually be
860 // thrown away when all references are released.
861 TEST_F(CacheStorageManagerTest, DeletedCacheIgnoredInIndex) {
862 const GURL kFooURL("http://example.com/foo");
863 const GURL kBarURL("http://example.com/bar");
864 const GURL kBazURL("http://example.com/baz");
865 const std::string kCacheName = "foo";
866
867 EXPECT_TRUE(Open(origin1_, kCacheName));
868 auto original_handle = std::move(callback_cache_handle_);
869 EXPECT_TRUE(CachePut(original_handle->value(), kFooURL));
870 EXPECT_TRUE(Delete(origin1_, kCacheName));
871
872 // Now a second cache using the same name, but with different data.
873 EXPECT_TRUE(Open(origin1_, kCacheName));
874 auto new_handle = std::move(callback_cache_handle_);
875 EXPECT_TRUE(CachePut(new_handle->value(), kFooURL));
876 EXPECT_TRUE(CachePut(new_handle->value(), kBarURL));
877 EXPECT_TRUE(CachePut(new_handle->value(), kBazURL));
878 auto new_cache_size = Size(origin1_);
879
880 // Now modify the first cache.
881 EXPECT_TRUE(CachePut(original_handle->value(), kBarURL));
882
883 // Now deref both caches, and recreate the storage manager.
884 original_handle = nullptr;
885 new_handle = nullptr;
886 EXPECT_TRUE(FlushCacheStorageIndex(origin1_));
887 DestroyStorageManager();
888 CreateStorageManager();
889
890 EXPECT_TRUE(Open(origin1_, kCacheName));
891 EXPECT_EQ(new_cache_size, Size(origin1_));
892 }
893
894 TEST_F(CacheStorageManagerTest, CacheSizeCorrectAfterReopen) {
895 const GURL kFooURL("http://example.com/foo");
896 const std::string kCacheName = "foo";
897
898 EXPECT_TRUE(Open(origin1_, kCacheName));
899 auto original_handle = std::move(callback_cache_handle_);
900 EXPECT_TRUE(CachePut(original_handle->value(), kFooURL));
901 auto size_before_close = Size(origin1_);
902 EXPECT_GT(size_before_close, 0);
903
904 DestroyStorageManager();
905 CreateStorageManager();
906
907 EXPECT_TRUE(Open(origin1_, kCacheName));
908 EXPECT_EQ(size_before_close, Size(origin1_));
909 }
910
779 // With a memory cache the cache can't be freed from memory until the client 911 // With a memory cache the cache can't be freed from memory until the client
780 // calls delete. 912 // calls delete.
781 TEST_F(CacheStorageManagerMemoryOnlyTest, MemoryLosesReferenceOnlyAfterDelete) { 913 TEST_F(CacheStorageManagerMemoryOnlyTest, MemoryLosesReferenceOnlyAfterDelete) {
782 EXPECT_TRUE(Open(origin1_, "foo")); 914 EXPECT_TRUE(Open(origin1_, "foo"));
783 base::WeakPtr<CacheStorageCache> cache = 915 base::WeakPtr<CacheStorageCache> cache =
784 callback_cache_handle_->value()->AsWeakPtr(); 916 callback_cache_handle_->value()->AsWeakPtr();
785 callback_cache_handle_ = nullptr; 917 callback_cache_handle_ = nullptr;
786 EXPECT_TRUE(cache); 918 EXPECT_TRUE(cache);
787 EXPECT_TRUE(Delete(origin1_, "foo")); 919 EXPECT_TRUE(Delete(origin1_, "foo"));
788 base::RunLoop().RunUntilIdle(); 920 base::RunLoop().RunUntilIdle();
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after
858 990
859 if (MemoryOnly()) { 991 if (MemoryOnly()) {
860 EXPECT_TRUE(usage[origin1_index].last_modified.is_null()); 992 EXPECT_TRUE(usage[origin1_index].last_modified.is_null());
861 EXPECT_TRUE(usage[origin2_index].last_modified.is_null()); 993 EXPECT_TRUE(usage[origin2_index].last_modified.is_null());
862 } else { 994 } else {
863 EXPECT_FALSE(usage[origin1_index].last_modified.is_null()); 995 EXPECT_FALSE(usage[origin1_index].last_modified.is_null());
864 EXPECT_FALSE(usage[origin2_index].last_modified.is_null()); 996 EXPECT_FALSE(usage[origin2_index].last_modified.is_null());
865 } 997 }
866 } 998 }
867 999
1000 TEST_F(CacheStorageManagerTest, GetAllOriginsUsageWithOldIndex) {
1001 // Write a single value (V1) to the cache.
1002 const GURL kFooURL = origin1_.Resolve("foo");
1003 const std::string kCacheName = "foo";
1004 EXPECT_TRUE(Open(origin1_, kCacheName));
1005 std::unique_ptr<CacheStorageCacheHandle> original_handle =
1006 std::move(callback_cache_handle_);
1007
1008 EXPECT_TRUE(CachePut(original_handle->value(), kFooURL));
1009 int64_t cache_size_v1 = Size(origin1_);
1010 base::FilePath storage_dir = original_handle->value()->path().DirName();
1011 original_handle.release();
1012 EXPECT_GE(cache_size_v1, 0);
1013
1014 // Close the caches and cache manager.
1015 EXPECT_TRUE(FlushCacheStorageIndex(origin1_));
1016 DestroyStorageManager();
1017
1018 // Save a copy of the V1 index.
1019 EXPECT_TRUE(IsIndexFileCurrent(storage_dir));
1020 base::FilePath index_path = storage_dir.AppendASCII("index.txt");
1021 EXPECT_TRUE(base::PathExists(index_path));
1022 base::FilePath backup_index_path = storage_dir.AppendASCII("index.txt.bak");
1023 EXPECT_TRUE(base::CopyFile(index_path, backup_index_path));
1024
1025 // Create a new CacheStorageManager that hasn't yet loaded the origin.
1026 CreateStorageManager();
1027 quota_manager_proxy_->SimulateQuotaManagerDestroyed();
1028 cache_manager_ = CacheStorageManager::Create(cache_manager_.get());
1029
1030 // Create a second value (V2) in the cache.
1031 EXPECT_TRUE(Open(origin1_, kCacheName));
1032 original_handle = std::move(callback_cache_handle_);
1033 const GURL kBarURL = origin1_.Resolve("bar");
1034 EXPECT_TRUE(CachePut(original_handle->value(), kBarURL));
1035 original_handle.release();
1036
1037 std::vector<CacheStorageUsageInfo> usage = GetAllOriginsUsage();
1038 ASSERT_EQ(1ULL, usage.size());
1039 int64_t usage_before_close = usage[0].total_size_bytes;
1040 EXPECT_GT(usage_before_close, 0);
1041
1042 // Close the caches and cache manager.
1043 DestroyStorageManager();
1044
1045 // Restore the index to the V1 state. Make the access/mod times of index file
1046 // older than the other directories in the store to trigger size
1047 // recalculation.
1048 EXPECT_TRUE(base::CopyFile(backup_index_path, index_path));
1049 base::Time t = base::Time::Now() - base::TimeDelta::FromHours(1);
1050 EXPECT_TRUE(base::TouchFile(index_path, t, t));
1051 EXPECT_FALSE(IsIndexFileCurrent(storage_dir));
1052
1053 CreateStorageManager();
1054 usage = GetAllOriginsUsage();
1055 ASSERT_EQ(1ULL, usage.size());
1056
1057 EXPECT_EQ(usage_before_close, usage[0].total_size_bytes);
1058
1059 EXPECT_FALSE(usage[0].last_modified.is_null());
1060 }
1061
1062 TEST_F(CacheStorageManagerTest, GetOriginSizeWithOldIndex) {
1063 // Write a single value (V1) to the cache.
1064 const GURL kFooURL = origin1_.Resolve("foo");
1065 const std::string kCacheName = "foo";
1066 EXPECT_TRUE(Open(origin1_, kCacheName));
1067 std::unique_ptr<CacheStorageCacheHandle> original_handle =
1068 std::move(callback_cache_handle_);
1069
1070 EXPECT_TRUE(CachePut(original_handle->value(), kFooURL));
1071 int64_t cache_size_v1 = Size(origin1_);
1072 base::FilePath storage_dir = original_handle->value()->path().DirName();
1073 original_handle.release();
1074 EXPECT_GE(cache_size_v1, 0);
1075
1076 // Close the caches and cache manager.
1077 EXPECT_TRUE(FlushCacheStorageIndex(origin1_));
1078 DestroyStorageManager();
1079
1080 // Save a copy of the V1 index.
1081 EXPECT_TRUE(IsIndexFileCurrent(storage_dir));
1082 base::FilePath index_path = storage_dir.AppendASCII("index.txt");
1083 EXPECT_TRUE(base::PathExists(index_path));
1084 base::FilePath backup_index_path = storage_dir.AppendASCII("index.txt.bak");
1085 EXPECT_TRUE(base::CopyFile(index_path, backup_index_path));
1086
1087 // Create a new CacheStorageManager that hasn't yet loaded the origin.
1088 CreateStorageManager();
1089 quota_manager_proxy_->SimulateQuotaManagerDestroyed();
1090 cache_manager_ = CacheStorageManager::Create(cache_manager_.get());
1091
1092 // Reopen the cache and write a second value (V2).
1093 EXPECT_TRUE(Open(origin1_, kCacheName));
1094 original_handle = std::move(callback_cache_handle_);
1095 const GURL kBarURL = origin1_.Resolve("bar");
1096 EXPECT_TRUE(CachePut(original_handle->value(), kBarURL));
1097 original_handle.release();
1098 int64_t cache_size_v2 = Size(origin1_);
1099 EXPECT_GE(cache_size_v2, 0);
1100
1101 // Close the caches and cache manager.
1102 DestroyStorageManager();
1103
1104 // Restore the index to the V1 state.
1105 EXPECT_TRUE(base::CopyFile(backup_index_path, index_path));
1106
1107 // Make the access/mod times of index file older than the other files in the
1108 // cache to trigger size recalculation.
1109 base::Time t = base::Time::Now() - base::TimeDelta::FromHours(1);
1110 EXPECT_TRUE(base::TouchFile(index_path, t, t));
1111 EXPECT_FALSE(IsIndexFileCurrent(storage_dir));
1112
1113 // Reopen the cache and ensure the size is correct for the V2 value.
1114 CreateStorageManager();
1115 EXPECT_TRUE(Open(origin1_, kCacheName));
1116 EXPECT_EQ(cache_size_v2, Size(origin1_));
1117 }
1118
868 TEST_P(CacheStorageManagerTestP, GetSizeThenCloseAllCaches) { 1119 TEST_P(CacheStorageManagerTestP, GetSizeThenCloseAllCaches) {
869 EXPECT_TRUE(Open(origin1_, "foo")); 1120 EXPECT_TRUE(Open(origin1_, "foo"));
870 EXPECT_TRUE(CachePut(callback_cache_handle_->value(), 1121 EXPECT_TRUE(CachePut(callback_cache_handle_->value(),
871 GURL("http://example.com/foo"))); 1122 GURL("http://example.com/foo")));
872 EXPECT_TRUE(CachePut(callback_cache_handle_->value(), 1123 EXPECT_TRUE(CachePut(callback_cache_handle_->value(),
873 GURL("http://example.com/foo2"))); 1124 GURL("http://example.com/foo2")));
874 EXPECT_TRUE(Open(origin1_, "bar")); 1125 EXPECT_TRUE(Open(origin1_, "bar"));
875 EXPECT_TRUE(CachePut(callback_cache_handle_->value(), 1126 EXPECT_TRUE(CachePut(callback_cache_handle_->value(),
876 GURL("http://example.com/bar"))); 1127 GURL("http://example.com/bar")));
877 1128
(...skipping 364 matching lines...) Expand 10 before | Expand all | Expand 10 after
1242 1493
1243 INSTANTIATE_TEST_CASE_P(CacheStorageManagerTests, 1494 INSTANTIATE_TEST_CASE_P(CacheStorageManagerTests,
1244 CacheStorageManagerTestP, 1495 CacheStorageManagerTestP,
1245 ::testing::Values(false, true)); 1496 ::testing::Values(false, true));
1246 1497
1247 INSTANTIATE_TEST_CASE_P(CacheStorageQuotaClientTests, 1498 INSTANTIATE_TEST_CASE_P(CacheStorageQuotaClientTests,
1248 CacheStorageQuotaClientTestP, 1499 CacheStorageQuotaClientTestP,
1249 ::testing::Values(false, true)); 1500 ::testing::Values(false, true));
1250 1501
1251 } // namespace content 1502 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698