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

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: Scheduling index writes + lots of other fixes. Created 4 years 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 } // anonymous namespace
75
47 // Returns a BlobProtocolHandler that uses |blob_storage_context|. Caller owns 76 // Returns a BlobProtocolHandler that uses |blob_storage_context|. Caller owns
48 // the memory. 77 // the memory.
49 std::unique_ptr<storage::BlobProtocolHandler> CreateMockBlobProtocolHandler( 78 std::unique_ptr<storage::BlobProtocolHandler> CreateMockBlobProtocolHandler(
50 storage::BlobStorageContext* blob_storage_context) { 79 storage::BlobStorageContext* blob_storage_context) {
51 // The FileSystemContext and thread task runner are not actually used but a 80 // The FileSystemContext and thread task runner are not actually used but a
52 // task runner is needed to avoid a DCHECK in BlobURLRequestJob ctor. 81 // task runner is needed to avoid a DCHECK in BlobURLRequestJob ctor.
53 return base::WrapUnique(new storage::BlobProtocolHandler( 82 return base::WrapUnique(new storage::BlobProtocolHandler(
54 blob_storage_context, NULL, base::ThreadTaskRunnerHandle::Get().get())); 83 blob_storage_context, NULL, base::ThreadTaskRunnerHandle::Get().get()));
55 } 84 }
56 85
57 class CacheStorageManagerTest : public testing::Test { 86 class CacheStorageManagerTest : public testing::Test {
58 public: 87 public:
59 CacheStorageManagerTest() 88 CacheStorageManagerTest()
60 : browser_thread_bundle_(TestBrowserThreadBundle::IO_MAINLOOP), 89 : browser_thread_bundle_(TestBrowserThreadBundle::IO_MAINLOOP),
61 blob_storage_context_(nullptr), 90 blob_storage_context_(nullptr),
62 callback_bool_(false), 91 callback_bool_(false),
63 callback_error_(CACHE_STORAGE_OK), 92 callback_error_(CACHE_STORAGE_OK),
64 origin1_("http://example1.com"), 93 origin1_("http://example1.com"),
65 origin2_("http://example2.com") {} 94 origin2_("http://example2.com") {}
66 95
67 void SetUp() override { 96 void SetUp() override {
97 base::FilePath temp_dir_path;
98 const bool is_incognito = MemoryOnly();
99 if (!is_incognito)
100 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
101
102 CreateStorageManager();
103 }
104
105 void TearDown() override { DestroyStorageManager(); }
106
107 virtual bool MemoryOnly() { return false; }
108
109 void BoolAndErrorCallback(base::RunLoop* run_loop,
110 bool value,
111 CacheStorageError error) {
112 callback_bool_ = value;
113 callback_error_ = error;
114 run_loop->Quit();
115 }
116
117 void CacheAndErrorCallback(
118 base::RunLoop* run_loop,
119 std::unique_ptr<CacheStorageCacheHandle> cache_handle,
120 CacheStorageError error) {
121 callback_cache_handle_ = std::move(cache_handle);
122 callback_error_ = error;
123 run_loop->Quit();
124 }
125
126 void CacheMetadataCallback(base::RunLoop* run_loop,
127 const CacheStorageIndex& cache_index) {
128 callback_cache_index_ = cache_index;
129 run_loop->Quit();
130 }
131
132 const std::string& GetFirstIndexName() const {
133 return callback_cache_index_.ordered_cache_metadata().front().name;
134 }
135
136 std::vector<std::string> GetIndexNames() const {
137 std::vector<std::string> cache_names;
138 for (const auto& metadata : callback_cache_index_.ordered_cache_metadata())
139 cache_names.push_back(metadata.name);
140 return cache_names;
141 }
142
143 void CachePutCallback(base::RunLoop* run_loop, CacheStorageError error) {
144 callback_error_ = error;
145 run_loop->Quit();
146 }
147
148 void CacheMatchCallback(
149 base::RunLoop* run_loop,
150 CacheStorageError error,
151 std::unique_ptr<ServiceWorkerResponse> response,
152 std::unique_ptr<storage::BlobDataHandle> blob_data_handle) {
153 callback_error_ = error;
154 callback_cache_handle_response_ = std::move(response);
155 callback_data_handle_ = std::move(blob_data_handle);
156 run_loop->Quit();
157 }
158
159 void CreateStorageManager() {
68 ChromeBlobStorageContext* blob_storage_context( 160 ChromeBlobStorageContext* blob_storage_context(
69 ChromeBlobStorageContext::GetFor(&browser_context_)); 161 ChromeBlobStorageContext::GetFor(&browser_context_));
70 // Wait for ChromeBlobStorageContext to finish initializing. 162 // Wait for ChromeBlobStorageContext to finish initializing.
71 base::RunLoop().RunUntilIdle(); 163 base::RunLoop().RunUntilIdle();
72 164
73 blob_storage_context_ = blob_storage_context->context(); 165 blob_storage_context_ = blob_storage_context->context();
74 166
75 url_request_job_factory_.reset(new net::URLRequestJobFactoryImpl); 167 url_request_job_factory_.reset(new net::URLRequestJobFactoryImpl);
76 url_request_job_factory_->SetProtocolHandler( 168 url_request_job_factory_->SetProtocolHandler(
77 "blob", CreateMockBlobProtocolHandler(blob_storage_context->context())); 169 "blob", CreateMockBlobProtocolHandler(blob_storage_context->context()));
78 170
79 net::URLRequestContext* url_request_context = 171 net::URLRequestContext* url_request_context =
80 BrowserContext::GetDefaultStoragePartition(&browser_context_)-> 172 BrowserContext::GetDefaultStoragePartition(&browser_context_)
81 GetURLRequestContext()->GetURLRequestContext(); 173 ->GetURLRequestContext()
174 ->GetURLRequestContext();
82 175
83 url_request_context->set_job_factory(url_request_job_factory_.get()); 176 url_request_context->set_job_factory(url_request_job_factory_.get());
84 177
178 base::FilePath temp_dir_path;
85 const bool is_incognito = MemoryOnly(); 179 const bool is_incognito = MemoryOnly();
86 base::FilePath temp_dir_path; 180 if (!is_incognito)
87 if (!is_incognito) {
88 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
89 temp_dir_path = temp_dir_.GetPath(); 181 temp_dir_path = temp_dir_.GetPath();
90 }
91 182
92 quota_policy_ = new MockSpecialStoragePolicy; 183 quota_policy_ = new MockSpecialStoragePolicy;
93 mock_quota_manager_ = new MockQuotaManager( 184 mock_quota_manager_ = new MockQuotaManager(
94 is_incognito, temp_dir_path, base::ThreadTaskRunnerHandle::Get().get(), 185 is_incognito, temp_dir_path, base::ThreadTaskRunnerHandle::Get().get(),
95 base::ThreadTaskRunnerHandle::Get().get(), quota_policy_.get()); 186 base::ThreadTaskRunnerHandle::Get().get(), quota_policy_.get());
96 mock_quota_manager_->SetQuota( 187 mock_quota_manager_->SetQuota(
97 GURL(origin1_), storage::kStorageTypeTemporary, 1024 * 1024 * 100); 188 GURL(origin1_), storage::kStorageTypeTemporary, 1024 * 1024 * 100);
98 mock_quota_manager_->SetQuota( 189 mock_quota_manager_->SetQuota(
99 GURL(origin2_), storage::kStorageTypeTemporary, 1024 * 1024 * 100); 190 GURL(origin2_), storage::kStorageTypeTemporary, 1024 * 1024 * 100);
100 191
101 quota_manager_proxy_ = new MockQuotaManagerProxy( 192 quota_manager_proxy_ = new MockQuotaManagerProxy(
102 mock_quota_manager_.get(), base::ThreadTaskRunnerHandle::Get().get()); 193 mock_quota_manager_.get(), base::ThreadTaskRunnerHandle::Get().get());
103 194
104 cache_manager_ = CacheStorageManager::Create( 195 cache_manager_ = CacheStorageManager::Create(
105 temp_dir_path, base::ThreadTaskRunnerHandle::Get(), 196 temp_dir_path, base::ThreadTaskRunnerHandle::Get(),
106 quota_manager_proxy_); 197 quota_manager_proxy_);
107 198
108 cache_manager_->SetBlobParametersForCache( 199 cache_manager_->SetBlobParametersForCache(
109 BrowserContext::GetDefaultStoragePartition(&browser_context_)-> 200 BrowserContext::GetDefaultStoragePartition(&browser_context_)
110 GetURLRequestContext(), 201 ->GetURLRequestContext(),
111 blob_storage_context->context()->AsWeakPtr()); 202 blob_storage_context->context()->AsWeakPtr());
112 } 203 }
113 204
114 void TearDown() override { 205 bool FlushCacheStorageIndex(const GURL& origin) {
206 bool write_scheduled =
207 CacheStorageForOrigin(origin)->InitiateScheduledIndexWriteForTest();
208 // Write is done on I/O thread, so wait for completion.
jkarlin 2016/12/01 17:43:31 The write is done on the task runner passed to Cac
cmumford 2016/12/05 17:19:02 Done.
209 base::RunLoop().RunUntilIdle();
210 return write_scheduled;
211 }
212
213 void DestroyStorageManager() {
115 quota_manager_proxy_->SimulateQuotaManagerDestroyed(); 214 quota_manager_proxy_->SimulateQuotaManagerDestroyed();
116 base::RunLoop().RunUntilIdle(); 215 base::RunLoop().RunUntilIdle();
117 } 216 quota_manager_proxy_ = nullptr;
118 217
119 virtual bool MemoryOnly() { return false; } 218 url_request_job_factory_.reset();
219 blob_storage_context_ = nullptr;
120 220
121 void BoolAndErrorCallback(base::RunLoop* run_loop, 221 quota_policy_ = nullptr;
122 bool value, 222 mock_quota_manager_ = nullptr;
123 CacheStorageError error) { 223 quota_manager_proxy_ = nullptr;
124 callback_bool_ = value;
125 callback_error_ = error;
126 run_loop->Quit();
127 }
128 224
129 void CacheAndErrorCallback( 225 callback_cache_handle_ = nullptr;
130 base::RunLoop* run_loop, 226 callback_bool_ = false;
131 std::unique_ptr<CacheStorageCacheHandle> cache_handle, 227 callback_cache_handle_response_ = nullptr;
132 CacheStorageError error) { 228 callback_data_handle_ = nullptr;
133 callback_cache_handle_ = std::move(cache_handle); 229 callback_cache_index_ = CacheStorageIndex();
134 callback_error_ = error; 230 callback_all_origins_usage_.clear();
135 run_loop->Quit();
136 }
137 231
138 void StringsCallback(base::RunLoop* run_loop, 232 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 } 233 }
159 234
160 bool Open(const GURL& origin, const std::string& cache_name) { 235 bool Open(const GURL& origin, const std::string& cache_name) {
161 base::RunLoop loop; 236 base::RunLoop loop;
162 cache_manager_->OpenCache( 237 cache_manager_->OpenCache(
163 origin, cache_name, 238 origin, cache_name,
164 base::Bind(&CacheStorageManagerTest::CacheAndErrorCallback, 239 base::Bind(&CacheStorageManagerTest::CacheAndErrorCallback,
165 base::Unretained(this), base::Unretained(&loop))); 240 base::Unretained(this), base::Unretained(&loop)));
166 loop.Run(); 241 loop.Run();
167 242
(...skipping 23 matching lines...) Expand all
191 base::Bind(&CacheStorageManagerTest::BoolAndErrorCallback, 266 base::Bind(&CacheStorageManagerTest::BoolAndErrorCallback,
192 base::Unretained(this), base::Unretained(&loop))); 267 base::Unretained(this), base::Unretained(&loop)));
193 loop.Run(); 268 loop.Run();
194 269
195 return callback_bool_; 270 return callback_bool_;
196 } 271 }
197 272
198 size_t Keys(const GURL& origin) { 273 size_t Keys(const GURL& origin) {
199 base::RunLoop loop; 274 base::RunLoop loop;
200 cache_manager_->EnumerateCaches( 275 cache_manager_->EnumerateCaches(
201 origin, base::Bind(&CacheStorageManagerTest::StringsCallback, 276 origin, base::Bind(&CacheStorageManagerTest::CacheMetadataCallback,
202 base::Unretained(this), base::Unretained(&loop))); 277 base::Unretained(this), base::Unretained(&loop)));
203 loop.Run(); 278 loop.Run();
204 return callback_strings_.size(); 279 return callback_cache_index_.num_entries();
205 } 280 }
206 281
207 bool StorageMatch(const GURL& origin, 282 bool StorageMatch(const GURL& origin,
208 const std::string& cache_name, 283 const std::string& cache_name,
209 const GURL& url, 284 const GURL& url,
210 const CacheStorageCacheQueryParams& match_params = 285 const CacheStorageCacheQueryParams& match_params =
211 CacheStorageCacheQueryParams()) { 286 CacheStorageCacheQueryParams()) {
212 ServiceWorkerFetchRequest request; 287 ServiceWorkerFetchRequest request;
213 request.url = url; 288 request.url = url;
214 return StorageMatchWithRequest(origin, cache_name, request, match_params); 289 return StorageMatchWithRequest(origin, cache_name, request, match_params);
(...skipping 171 matching lines...) Expand 10 before | Expand all | Expand 10 after
386 scoped_refptr<MockSpecialStoragePolicy> quota_policy_; 461 scoped_refptr<MockSpecialStoragePolicy> quota_policy_;
387 scoped_refptr<MockQuotaManager> mock_quota_manager_; 462 scoped_refptr<MockQuotaManager> mock_quota_manager_;
388 scoped_refptr<MockQuotaManagerProxy> quota_manager_proxy_; 463 scoped_refptr<MockQuotaManagerProxy> quota_manager_proxy_;
389 std::unique_ptr<CacheStorageManager> cache_manager_; 464 std::unique_ptr<CacheStorageManager> cache_manager_;
390 465
391 std::unique_ptr<CacheStorageCacheHandle> callback_cache_handle_; 466 std::unique_ptr<CacheStorageCacheHandle> callback_cache_handle_;
392 int callback_bool_; 467 int callback_bool_;
393 CacheStorageError callback_error_; 468 CacheStorageError callback_error_;
394 std::unique_ptr<ServiceWorkerResponse> callback_cache_handle_response_; 469 std::unique_ptr<ServiceWorkerResponse> callback_cache_handle_response_;
395 std::unique_ptr<storage::BlobDataHandle> callback_data_handle_; 470 std::unique_ptr<storage::BlobDataHandle> callback_data_handle_;
396 std::vector<std::string> callback_strings_; 471 CacheStorageIndex callback_cache_index_;
397 472
398 const GURL origin1_; 473 const GURL origin1_;
399 const GURL origin2_; 474 const GURL origin2_;
400 475
401 int64_t callback_usage_; 476 int64_t callback_usage_;
402 std::vector<CacheStorageUsageInfo> callback_all_origins_usage_; 477 std::vector<CacheStorageUsageInfo> callback_all_origins_usage_;
403 478
404 private: 479 private:
405 DISALLOW_COPY_AND_ASSIGN(CacheStorageManagerTest); 480 DISALLOW_COPY_AND_ASSIGN(CacheStorageManagerTest);
406 }; 481 };
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after
499 } 574 }
500 575
501 TEST_P(CacheStorageManagerTestP, SomeKeys) { 576 TEST_P(CacheStorageManagerTestP, SomeKeys) {
502 EXPECT_TRUE(Open(origin1_, "foo")); 577 EXPECT_TRUE(Open(origin1_, "foo"));
503 EXPECT_TRUE(Open(origin1_, "bar")); 578 EXPECT_TRUE(Open(origin1_, "bar"));
504 EXPECT_TRUE(Open(origin2_, "baz")); 579 EXPECT_TRUE(Open(origin2_, "baz"));
505 EXPECT_EQ(2u, Keys(origin1_)); 580 EXPECT_EQ(2u, Keys(origin1_));
506 std::vector<std::string> expected_keys; 581 std::vector<std::string> expected_keys;
507 expected_keys.push_back("foo"); 582 expected_keys.push_back("foo");
508 expected_keys.push_back("bar"); 583 expected_keys.push_back("bar");
509 EXPECT_EQ(expected_keys, callback_strings_); 584 EXPECT_EQ(expected_keys, GetIndexNames());
510 EXPECT_EQ(1u, Keys(origin2_)); 585 EXPECT_EQ(1u, Keys(origin2_));
511 EXPECT_STREQ("baz", callback_strings_[0].c_str()); 586 EXPECT_STREQ("baz", GetFirstIndexName().c_str());
512 } 587 }
513 588
514 TEST_P(CacheStorageManagerTestP, DeletedKeysGone) { 589 TEST_P(CacheStorageManagerTestP, DeletedKeysGone) {
515 EXPECT_TRUE(Open(origin1_, "foo")); 590 EXPECT_TRUE(Open(origin1_, "foo"));
516 EXPECT_TRUE(Open(origin1_, "bar")); 591 EXPECT_TRUE(Open(origin1_, "bar"));
517 EXPECT_TRUE(Open(origin2_, "baz")); 592 EXPECT_TRUE(Open(origin2_, "baz"));
518 EXPECT_TRUE(Delete(origin1_, "bar")); 593 EXPECT_TRUE(Delete(origin1_, "bar"));
519 EXPECT_EQ(1u, Keys(origin1_)); 594 EXPECT_EQ(1u, Keys(origin1_));
520 EXPECT_STREQ("foo", callback_strings_[0].c_str()); 595 EXPECT_STREQ("foo", GetFirstIndexName().c_str());
521 } 596 }
522 597
523 TEST_P(CacheStorageManagerTestP, StorageMatchEntryExists) { 598 TEST_P(CacheStorageManagerTestP, StorageMatchEntryExists) {
524 EXPECT_TRUE(Open(origin1_, "foo")); 599 EXPECT_TRUE(Open(origin1_, "foo"));
525 EXPECT_TRUE(CachePut(callback_cache_handle_->value(), 600 EXPECT_TRUE(CachePut(callback_cache_handle_->value(),
526 GURL("http://example.com/foo"))); 601 GURL("http://example.com/foo")));
527 EXPECT_TRUE(StorageMatch(origin1_, "foo", GURL("http://example.com/foo"))); 602 EXPECT_TRUE(StorageMatch(origin1_, "foo", GURL("http://example.com/foo")));
528 } 603 }
529 604
530 TEST_P(CacheStorageManagerTestP, StorageMatchNoEntry) { 605 TEST_P(CacheStorageManagerTestP, StorageMatchNoEntry) {
(...skipping 125 matching lines...) Expand 10 before | Expand all | Expand 10 after
656 EXPECT_TRUE(StorageMatchAll(origin1_, GURL("http://example.com/foo"))); 731 EXPECT_TRUE(StorageMatchAll(origin1_, GURL("http://example.com/foo")));
657 } 732 }
658 733
659 TEST_P(CacheStorageManagerTestP, Chinese) { 734 TEST_P(CacheStorageManagerTestP, Chinese) {
660 EXPECT_TRUE(Open(origin1_, "你好")); 735 EXPECT_TRUE(Open(origin1_, "你好"));
661 std::unique_ptr<CacheStorageCacheHandle> cache_handle = 736 std::unique_ptr<CacheStorageCacheHandle> cache_handle =
662 std::move(callback_cache_handle_); 737 std::move(callback_cache_handle_);
663 EXPECT_TRUE(Open(origin1_, "你好")); 738 EXPECT_TRUE(Open(origin1_, "你好"));
664 EXPECT_EQ(callback_cache_handle_->value(), cache_handle->value()); 739 EXPECT_EQ(callback_cache_handle_->value(), cache_handle->value());
665 EXPECT_EQ(1u, Keys(origin1_)); 740 EXPECT_EQ(1u, Keys(origin1_));
666 EXPECT_STREQ("你好", callback_strings_[0].c_str()); 741 EXPECT_STREQ("你好", GetFirstIndexName().c_str());
667 } 742 }
668 743
669 TEST_F(CacheStorageManagerTest, EmptyKey) { 744 TEST_F(CacheStorageManagerTest, EmptyKey) {
670 EXPECT_TRUE(Open(origin1_, "")); 745 EXPECT_TRUE(Open(origin1_, ""));
671 std::unique_ptr<CacheStorageCacheHandle> cache_handle = 746 std::unique_ptr<CacheStorageCacheHandle> cache_handle =
672 std::move(callback_cache_handle_); 747 std::move(callback_cache_handle_);
673 EXPECT_TRUE(Open(origin1_, "")); 748 EXPECT_TRUE(Open(origin1_, ""));
674 EXPECT_EQ(cache_handle->value(), callback_cache_handle_->value()); 749 EXPECT_EQ(cache_handle->value(), callback_cache_handle_->value());
675 EXPECT_EQ(1u, Keys(origin1_)); 750 EXPECT_EQ(1u, Keys(origin1_));
676 EXPECT_STREQ("", callback_strings_[0].c_str()); 751 EXPECT_STREQ("", GetFirstIndexName().c_str());
677 EXPECT_TRUE(Has(origin1_, "")); 752 EXPECT_TRUE(Has(origin1_, ""));
678 EXPECT_TRUE(Delete(origin1_, "")); 753 EXPECT_TRUE(Delete(origin1_, ""));
679 EXPECT_EQ(0u, Keys(origin1_)); 754 EXPECT_EQ(0u, Keys(origin1_));
680 } 755 }
681 756
682 TEST_F(CacheStorageManagerTest, DataPersists) { 757 TEST_F(CacheStorageManagerTest, DataPersists) {
683 EXPECT_TRUE(Open(origin1_, "foo")); 758 EXPECT_TRUE(Open(origin1_, "foo"));
684 EXPECT_TRUE(Open(origin1_, "bar")); 759 EXPECT_TRUE(Open(origin1_, "bar"));
685 EXPECT_TRUE(Open(origin1_, "baz")); 760 EXPECT_TRUE(Open(origin1_, "baz"));
686 EXPECT_TRUE(Open(origin2_, "raz")); 761 EXPECT_TRUE(Open(origin2_, "raz"));
687 EXPECT_TRUE(Delete(origin1_, "bar")); 762 EXPECT_TRUE(Delete(origin1_, "bar"));
688 quota_manager_proxy_->SimulateQuotaManagerDestroyed(); 763 quota_manager_proxy_->SimulateQuotaManagerDestroyed();
689 cache_manager_ = CacheStorageManager::Create(cache_manager_.get()); 764 cache_manager_ = CacheStorageManager::Create(cache_manager_.get());
690 EXPECT_EQ(2u, Keys(origin1_)); 765 EXPECT_EQ(2u, Keys(origin1_));
691 std::vector<std::string> expected_keys; 766 std::vector<std::string> expected_keys;
692 expected_keys.push_back("foo"); 767 expected_keys.push_back("foo");
693 expected_keys.push_back("baz"); 768 expected_keys.push_back("baz");
694 EXPECT_EQ(expected_keys, callback_strings_); 769 EXPECT_EQ(expected_keys, GetIndexNames());
695 } 770 }
696 771
697 TEST_F(CacheStorageManagerMemoryOnlyTest, DataLostWhenMemoryOnly) { 772 TEST_F(CacheStorageManagerMemoryOnlyTest, DataLostWhenMemoryOnly) {
698 EXPECT_TRUE(Open(origin1_, "foo")); 773 EXPECT_TRUE(Open(origin1_, "foo"));
699 EXPECT_TRUE(Open(origin2_, "baz")); 774 EXPECT_TRUE(Open(origin2_, "baz"));
700 quota_manager_proxy_->SimulateQuotaManagerDestroyed(); 775 quota_manager_proxy_->SimulateQuotaManagerDestroyed();
701 cache_manager_ = CacheStorageManager::Create(cache_manager_.get()); 776 cache_manager_ = CacheStorageManager::Create(cache_manager_.get());
702 EXPECT_EQ(0u, Keys(origin1_)); 777 EXPECT_EQ(0u, Keys(origin1_));
703 } 778 }
704 779
705 TEST_F(CacheStorageManagerTest, BadCacheName) { 780 TEST_F(CacheStorageManagerTest, BadCacheName) {
706 // Since the implementation writes cache names to disk, ensure that we don't 781 // Since the implementation writes cache names to disk, ensure that we don't
707 // escape the directory. 782 // escape the directory.
708 const std::string bad_name = "../../../../../../../../../../../../../../foo"; 783 const std::string bad_name = "../../../../../../../../../../../../../../foo";
709 EXPECT_TRUE(Open(origin1_, bad_name)); 784 EXPECT_TRUE(Open(origin1_, bad_name));
710 EXPECT_EQ(1u, Keys(origin1_)); 785 EXPECT_EQ(1u, Keys(origin1_));
711 EXPECT_STREQ(bad_name.c_str(), callback_strings_[0].c_str()); 786 EXPECT_STREQ(bad_name.c_str(), GetFirstIndexName().c_str());
712 } 787 }
713 788
714 TEST_F(CacheStorageManagerTest, BadOriginName) { 789 TEST_F(CacheStorageManagerTest, BadOriginName) {
715 // Since the implementation writes origin names to disk, ensure that we don't 790 // Since the implementation writes origin names to disk, ensure that we don't
716 // escape the directory. 791 // escape the directory.
717 GURL bad_origin("http://../../../../../../../../../../../../../../foo"); 792 GURL bad_origin("http://../../../../../../../../../../../../../../foo");
718 EXPECT_TRUE(Open(bad_origin, "foo")); 793 EXPECT_TRUE(Open(bad_origin, "foo"));
719 EXPECT_EQ(1u, Keys(bad_origin)); 794 EXPECT_EQ(1u, Keys(bad_origin));
720 EXPECT_STREQ("foo", callback_strings_[0].c_str()); 795 EXPECT_STREQ("foo", GetFirstIndexName().c_str());
721 } 796 }
722 797
723 // With a persistent cache if the client drops its reference to a 798 // With a persistent cache if the client drops its reference to a
724 // CacheStorageCache it should be deleted. 799 // CacheStorageCache it should be deleted.
725 TEST_F(CacheStorageManagerTest, DropReference) { 800 TEST_F(CacheStorageManagerTest, DropReference) {
726 EXPECT_TRUE(Open(origin1_, "foo")); 801 EXPECT_TRUE(Open(origin1_, "foo"));
727 base::WeakPtr<CacheStorageCache> cache = 802 base::WeakPtr<CacheStorageCache> cache =
728 callback_cache_handle_->value()->AsWeakPtr(); 803 callback_cache_handle_->value()->AsWeakPtr();
729 // Run a cache operation to ensure that the cache has finished initializing so 804 // Run a cache operation to ensure that the cache has finished initializing so
730 // that when the handle is dropped it can close immediately. 805 // that when the handle is dropped it can close immediately.
(...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after
853 928
854 if (MemoryOnly()) { 929 if (MemoryOnly()) {
855 EXPECT_TRUE(usage[origin1_index].last_modified.is_null()); 930 EXPECT_TRUE(usage[origin1_index].last_modified.is_null());
856 EXPECT_TRUE(usage[origin2_index].last_modified.is_null()); 931 EXPECT_TRUE(usage[origin2_index].last_modified.is_null());
857 } else { 932 } else {
858 EXPECT_FALSE(usage[origin1_index].last_modified.is_null()); 933 EXPECT_FALSE(usage[origin1_index].last_modified.is_null());
859 EXPECT_FALSE(usage[origin2_index].last_modified.is_null()); 934 EXPECT_FALSE(usage[origin2_index].last_modified.is_null());
860 } 935 }
861 } 936 }
862 937
938 TEST_F(CacheStorageManagerTest, GetAllOriginsUsageWithOldIndex) {
939 // Write a single value (V1) to the cache.
940 const GURL kFooURL = origin1_.Resolve("foo");
941 const std::string kCacheName = "foo";
942 EXPECT_TRUE(Open(origin1_, kCacheName));
943 std::unique_ptr<CacheStorageCacheHandle> original_handle =
944 std::move(callback_cache_handle_);
945
946 EXPECT_TRUE(CachePut(original_handle->value(), kFooURL));
947 int64_t cache_size_v1 = Size(origin1_);
948 base::FilePath storage_dir = original_handle->value()->path().DirName();
949 original_handle.release();
950 EXPECT_GE(cache_size_v1, 0);
951
952 // Close the caches and cache manager.
953 EXPECT_TRUE(FlushCacheStorageIndex(origin1_));
954 DestroyStorageManager();
955
956 // Save a copy of the V1 index.
957 EXPECT_TRUE(IsIndexFileCurrent(storage_dir));
958 base::FilePath index_path = storage_dir.AppendASCII("index.txt");
959 EXPECT_TRUE(base::PathExists(index_path));
960 base::FilePath backup_index_path = storage_dir.AppendASCII("index.txt.bak");
961 EXPECT_TRUE(base::CopyFile(index_path, backup_index_path));
962
963 // Create a new CacheStorageManager that hasn't yet loaded the origin.
964 CreateStorageManager();
965 quota_manager_proxy_->SimulateQuotaManagerDestroyed();
966 cache_manager_ = CacheStorageManager::Create(cache_manager_.get());
967
968 // Create a second value (V2) in the cache.
969 EXPECT_TRUE(Open(origin1_, kCacheName));
970 original_handle = std::move(callback_cache_handle_);
971 const GURL kBarURL = origin1_.Resolve("bar");
972 EXPECT_TRUE(CachePut(original_handle->value(), kBarURL));
973 original_handle.release();
974
975 std::vector<CacheStorageUsageInfo> usage = GetAllOriginsUsage();
976 ASSERT_EQ(1ULL, usage.size());
977 int64_t usage_before_close = usage[0].total_size_bytes;
978 EXPECT_GT(usage_before_close, 0);
979
980 // Close the caches and cache manager.
981 DestroyStorageManager();
982
983 // Restore the index to the V1 state. Make the access/mod times of index file
984 // older than the other directories in the store to trigger size
985 // recalculation.
986 EXPECT_TRUE(base::CopyFile(backup_index_path, index_path));
987 base::Time t = base::Time::Now() - base::TimeDelta::FromHours(1);
988 EXPECT_TRUE(base::TouchFile(index_path, t, t));
989 EXPECT_FALSE(IsIndexFileCurrent(storage_dir));
990
991 CreateStorageManager();
992 usage = GetAllOriginsUsage();
993 ASSERT_EQ(1ULL, usage.size());
994
995 EXPECT_EQ(usage_before_close, usage[0].total_size_bytes);
996
997 EXPECT_FALSE(usage[0].last_modified.is_null());
998 }
999
1000 TEST_F(CacheStorageManagerTest, GetOriginSizeWithOldIndex) {
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 // Reopen the cache and write a second value (V2).
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 int64_t cache_size_v2 = Size(origin1_);
1037 EXPECT_GE(cache_size_v2, 0);
1038
1039 // Close the caches and cache manager.
1040 DestroyStorageManager();
1041
1042 // Restore the index to the V1 state.
1043 EXPECT_TRUE(base::CopyFile(backup_index_path, index_path));
1044
1045 // Make the access/mod times of index file older than the other files in the
1046 // cache to trigger size recalculation.
1047 base::Time t = base::Time::Now() - base::TimeDelta::FromHours(1);
1048 EXPECT_TRUE(base::TouchFile(index_path, t, t));
1049 EXPECT_FALSE(IsIndexFileCurrent(storage_dir));
1050
1051 // Reopen the cache and ensure the size is correct for the V2 value.
1052 CreateStorageManager();
1053 EXPECT_TRUE(Open(origin1_, kCacheName));
1054 EXPECT_EQ(cache_size_v2, Size(origin1_));
1055 }
1056
863 TEST_P(CacheStorageManagerTestP, GetSizeThenCloseAllCaches) { 1057 TEST_P(CacheStorageManagerTestP, GetSizeThenCloseAllCaches) {
864 EXPECT_TRUE(Open(origin1_, "foo")); 1058 EXPECT_TRUE(Open(origin1_, "foo"));
865 EXPECT_TRUE(CachePut(callback_cache_handle_->value(), 1059 EXPECT_TRUE(CachePut(callback_cache_handle_->value(),
866 GURL("http://example.com/foo"))); 1060 GURL("http://example.com/foo")));
867 EXPECT_TRUE(CachePut(callback_cache_handle_->value(), 1061 EXPECT_TRUE(CachePut(callback_cache_handle_->value(),
868 GURL("http://example.com/foo2"))); 1062 GURL("http://example.com/foo2")));
869 EXPECT_TRUE(Open(origin1_, "bar")); 1063 EXPECT_TRUE(Open(origin1_, "bar"));
870 EXPECT_TRUE(CachePut(callback_cache_handle_->value(), 1064 EXPECT_TRUE(CachePut(callback_cache_handle_->value(),
871 GURL("http://example.com/bar"))); 1065 GURL("http://example.com/bar")));
872 1066
(...skipping 364 matching lines...) Expand 10 before | Expand all | Expand 10 after
1237 1431
1238 INSTANTIATE_TEST_CASE_P(CacheStorageManagerTests, 1432 INSTANTIATE_TEST_CASE_P(CacheStorageManagerTests,
1239 CacheStorageManagerTestP, 1433 CacheStorageManagerTestP,
1240 ::testing::Values(false, true)); 1434 ::testing::Values(false, true));
1241 1435
1242 INSTANTIATE_TEST_CASE_P(CacheStorageQuotaClientTests, 1436 INSTANTIATE_TEST_CASE_P(CacheStorageQuotaClientTests,
1243 CacheStorageQuotaClientTestP, 1437 CacheStorageQuotaClientTestP,
1244 ::testing::Values(false, true)); 1438 ::testing::Values(false, true));
1245 1439
1246 } // namespace content 1440 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698