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

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: Consolidated observer methods, renames, cleanup, etc. Created 4 years, 1 month 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"
27 #include "content/browser/cache_storage/cache_storage_quota_client.h" 30 #include "content/browser/cache_storage/cache_storage_quota_client.h"
28 #include "content/browser/quota/mock_quota_manager_proxy.h" 31 #include "content/browser/quota/mock_quota_manager_proxy.h"
29 #include "content/public/browser/browser_thread.h" 32 #include "content/public/browser/browser_thread.h"
30 #include "content/public/browser/cache_storage_usage_info.h" 33 #include "content/public/browser/cache_storage_usage_info.h"
31 #include "content/public/browser/storage_partition.h" 34 #include "content/public/browser/storage_partition.h"
32 #include "content/public/test/mock_special_storage_policy.h" 35 #include "content/public/test/mock_special_storage_policy.h"
33 #include "content/public/test/test_browser_context.h" 36 #include "content/public/test/test_browser_context.h"
34 #include "content/public/test/test_browser_thread_bundle.h" 37 #include "content/public/test/test_browser_thread_bundle.h"
35 #include "net/url_request/url_request_context.h" 38 #include "net/url_request/url_request_context.h"
36 #include "net/url_request/url_request_context_getter.h" 39 #include "net/url_request/url_request_context_getter.h"
37 #include "net/url_request/url_request_job_factory_impl.h" 40 #include "net/url_request/url_request_job_factory_impl.h"
38 #include "storage/browser/blob/blob_data_builder.h" 41 #include "storage/browser/blob/blob_data_builder.h"
39 #include "storage/browser/blob/blob_data_handle.h" 42 #include "storage/browser/blob/blob_data_handle.h"
40 #include "storage/browser/blob/blob_storage_context.h" 43 #include "storage/browser/blob/blob_storage_context.h"
41 #include "storage/browser/blob/blob_url_request_job_factory.h" 44 #include "storage/browser/blob/blob_url_request_job_factory.h"
42 #include "storage/browser/quota/quota_manager_proxy.h" 45 #include "storage/browser/quota/quota_manager_proxy.h"
43 #include "testing/gtest/include/gtest/gtest.h" 46 #include "testing/gtest/include/gtest/gtest.h"
44 47
45 namespace content { 48 namespace content {
46 49
50 namespace {
51
52 bool IsIndexFileCurrent(const base::FilePath& cache_dir) {
53 base::File::Info info;
54 const base::FilePath index_path =
55 cache_dir.AppendASCII(CacheStorage::kIndexFileName);
56 if (!GetFileInfo(index_path, &info))
57 return false;
58 base::Time index_last_modified = info.last_modified;
59
60 base::FileEnumerator enumerator(cache_dir, false,
61 base::FileEnumerator::DIRECTORIES);
62 for (base::FilePath file_path = enumerator.Next(); !file_path.empty();
63 file_path = enumerator.Next()) {
64 if (!GetFileInfo(file_path, &info))
65 return false;
66 if (index_last_modified < info.last_modified)
67 return false;
68 }
69
70 return true;
71 }
72
73 } // anonymous namespace
74
75 std::vector<std::string> GetCacheMetadataNames(
76 const std::list<CacheStorage::CacheMetadata>& cache_metadata) {
77 std::vector<std::string> cache_names;
78 for (const auto& metadata : cache_metadata)
79 cache_names.push_back(metadata.name);
80 return cache_names;
81 }
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 const bool is_incognito = MemoryOnly();
106 if (!is_incognito)
107 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
108
109 CreateStorageManager();
110 }
111
112 void TearDown() override { DestroyStorageManager(); }
113
114 virtual bool MemoryOnly() { return false; }
115
116 void BoolAndErrorCallback(base::RunLoop* run_loop,
117 bool value,
118 CacheStorageError error) {
119 callback_bool_ = value;
120 callback_error_ = error;
121 run_loop->Quit();
122 }
123
124 void CacheAndErrorCallback(
125 base::RunLoop* run_loop,
126 std::unique_ptr<CacheStorageCacheHandle> cache_handle,
127 CacheStorageError error) {
128 callback_cache_handle_ = std::move(cache_handle);
129 callback_error_ = error;
130 run_loop->Quit();
131 }
132
133 void CacheMetadataCallback(
134 base::RunLoop* run_loop,
135 const std::list<CacheStorage::CacheMetadata>& cache_metadata) {
136 cache_metadata_ = cache_metadata;
137 run_loop->Quit();
138 }
139
140 void CachePutCallback(base::RunLoop* run_loop, CacheStorageError error) {
141 callback_error_ = error;
142 run_loop->Quit();
143 }
144
145 void CacheMatchCallback(
146 base::RunLoop* run_loop,
147 CacheStorageError error,
148 std::unique_ptr<ServiceWorkerResponse> response,
149 std::unique_ptr<storage::BlobDataHandle> blob_data_handle) {
150 callback_error_ = error;
151 callback_cache_handle_response_ = std::move(response);
152 callback_data_handle_ = std::move(blob_data_handle);
153 run_loop->Quit();
154 }
155
156 void CreateStorageManager() {
68 ChromeBlobStorageContext* blob_storage_context( 157 ChromeBlobStorageContext* blob_storage_context(
69 ChromeBlobStorageContext::GetFor(&browser_context_)); 158 ChromeBlobStorageContext::GetFor(&browser_context_));
70 // Wait for ChromeBlobStorageContext to finish initializing. 159 // Wait for ChromeBlobStorageContext to finish initializing.
71 base::RunLoop().RunUntilIdle(); 160 base::RunLoop().RunUntilIdle();
72 161
73 blob_storage_context_ = blob_storage_context->context(); 162 blob_storage_context_ = blob_storage_context->context();
74 163
75 url_request_job_factory_.reset(new net::URLRequestJobFactoryImpl); 164 url_request_job_factory_.reset(new net::URLRequestJobFactoryImpl);
76 url_request_job_factory_->SetProtocolHandler( 165 url_request_job_factory_->SetProtocolHandler(
77 "blob", CreateMockBlobProtocolHandler(blob_storage_context->context())); 166 "blob", CreateMockBlobProtocolHandler(blob_storage_context->context()));
78 167
79 net::URLRequestContext* url_request_context = 168 net::URLRequestContext* url_request_context =
80 BrowserContext::GetDefaultStoragePartition(&browser_context_)-> 169 BrowserContext::GetDefaultStoragePartition(&browser_context_)
81 GetURLRequestContext()->GetURLRequestContext(); 170 ->GetURLRequestContext()
171 ->GetURLRequestContext();
82 172
83 url_request_context->set_job_factory(url_request_job_factory_.get()); 173 url_request_context->set_job_factory(url_request_job_factory_.get());
84 174
175 base::FilePath temp_dir_path;
85 const bool is_incognito = MemoryOnly(); 176 const bool is_incognito = MemoryOnly();
86 base::FilePath temp_dir_path; 177 if (!is_incognito)
87 if (!is_incognito) {
88 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
89 temp_dir_path = temp_dir_.GetPath(); 178 temp_dir_path = temp_dir_.GetPath();
90 }
91 179
92 quota_policy_ = new MockSpecialStoragePolicy; 180 quota_policy_ = new MockSpecialStoragePolicy;
93 mock_quota_manager_ = new MockQuotaManager( 181 mock_quota_manager_ = new MockQuotaManager(
94 is_incognito, temp_dir_path, base::ThreadTaskRunnerHandle::Get().get(), 182 is_incognito, temp_dir_path, base::ThreadTaskRunnerHandle::Get().get(),
95 base::ThreadTaskRunnerHandle::Get().get(), quota_policy_.get()); 183 base::ThreadTaskRunnerHandle::Get().get(), quota_policy_.get());
96 mock_quota_manager_->SetQuota( 184 mock_quota_manager_->SetQuota(
97 GURL(origin1_), storage::kStorageTypeTemporary, 1024 * 1024 * 100); 185 GURL(origin1_), storage::kStorageTypeTemporary, 1024 * 1024 * 100);
98 mock_quota_manager_->SetQuota( 186 mock_quota_manager_->SetQuota(
99 GURL(origin2_), storage::kStorageTypeTemporary, 1024 * 1024 * 100); 187 GURL(origin2_), storage::kStorageTypeTemporary, 1024 * 1024 * 100);
100 188
101 quota_manager_proxy_ = new MockQuotaManagerProxy( 189 quota_manager_proxy_ = new MockQuotaManagerProxy(
102 mock_quota_manager_.get(), base::ThreadTaskRunnerHandle::Get().get()); 190 mock_quota_manager_.get(), base::ThreadTaskRunnerHandle::Get().get());
103 191
104 cache_manager_ = CacheStorageManager::Create( 192 cache_manager_ = CacheStorageManager::Create(
105 temp_dir_path, base::ThreadTaskRunnerHandle::Get(), 193 temp_dir_path, base::ThreadTaskRunnerHandle::Get(),
106 quota_manager_proxy_); 194 quota_manager_proxy_);
107 195
108 cache_manager_->SetBlobParametersForCache( 196 cache_manager_->SetBlobParametersForCache(
109 BrowserContext::GetDefaultStoragePartition(&browser_context_)-> 197 BrowserContext::GetDefaultStoragePartition(&browser_context_)
110 GetURLRequestContext(), 198 ->GetURLRequestContext(),
111 blob_storage_context->context()->AsWeakPtr()); 199 blob_storage_context->context()->AsWeakPtr());
112 } 200 }
113 201
114 void TearDown() override { 202 void DestroyStorageManager() {
115 quota_manager_proxy_->SimulateQuotaManagerDestroyed(); 203 quota_manager_proxy_->SimulateQuotaManagerDestroyed();
116 base::RunLoop().RunUntilIdle(); 204 base::RunLoop().RunUntilIdle();
117 } 205 quota_manager_proxy_ = nullptr;
118 206
119 virtual bool MemoryOnly() { return false; } 207 url_request_job_factory_.reset();
208 blob_storage_context_ = nullptr;
120 209
121 void BoolAndErrorCallback(base::RunLoop* run_loop, 210 quota_policy_ = nullptr;
122 bool value, 211 mock_quota_manager_ = nullptr;
123 CacheStorageError error) { 212 quota_manager_proxy_ = nullptr;
124 callback_bool_ = value;
125 callback_error_ = error;
126 run_loop->Quit();
127 }
128 213
129 void CacheAndErrorCallback( 214 callback_cache_handle_ = nullptr;
130 base::RunLoop* run_loop, 215 callback_bool_ = false;
131 std::unique_ptr<CacheStorageCacheHandle> cache_handle, 216 callback_cache_handle_response_ = nullptr;
132 CacheStorageError error) { 217 callback_data_handle_ = nullptr;
133 callback_cache_handle_ = std::move(cache_handle); 218 cache_metadata_.clear();
134 callback_error_ = error; 219 callback_all_origins_usage_.clear();
135 run_loop->Quit();
136 }
137 220
138 void StringsCallback(base::RunLoop* run_loop, 221 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 } 222 }
159 223
160 bool Open(const GURL& origin, const std::string& cache_name) { 224 bool Open(const GURL& origin, const std::string& cache_name) {
161 base::RunLoop loop; 225 base::RunLoop loop;
162 cache_manager_->OpenCache( 226 cache_manager_->OpenCache(
163 origin, cache_name, 227 origin, cache_name,
164 base::Bind(&CacheStorageManagerTest::CacheAndErrorCallback, 228 base::Bind(&CacheStorageManagerTest::CacheAndErrorCallback,
165 base::Unretained(this), base::Unretained(&loop))); 229 base::Unretained(this), base::Unretained(&loop)));
166 loop.Run(); 230 loop.Run();
167 231
(...skipping 23 matching lines...) Expand all
191 base::Bind(&CacheStorageManagerTest::BoolAndErrorCallback, 255 base::Bind(&CacheStorageManagerTest::BoolAndErrorCallback,
192 base::Unretained(this), base::Unretained(&loop))); 256 base::Unretained(this), base::Unretained(&loop)));
193 loop.Run(); 257 loop.Run();
194 258
195 return callback_bool_; 259 return callback_bool_;
196 } 260 }
197 261
198 size_t Keys(const GURL& origin) { 262 size_t Keys(const GURL& origin) {
199 base::RunLoop loop; 263 base::RunLoop loop;
200 cache_manager_->EnumerateCaches( 264 cache_manager_->EnumerateCaches(
201 origin, base::Bind(&CacheStorageManagerTest::StringsCallback, 265 origin, base::Bind(&CacheStorageManagerTest::CacheMetadataCallback,
202 base::Unretained(this), base::Unretained(&loop))); 266 base::Unretained(this), base::Unretained(&loop)));
203 loop.Run(); 267 loop.Run();
204 return callback_strings_.size(); 268 return cache_metadata_.size();
205 } 269 }
206 270
207 bool StorageMatch(const GURL& origin, 271 bool StorageMatch(const GURL& origin,
208 const std::string& cache_name, 272 const std::string& cache_name,
209 const GURL& url, 273 const GURL& url,
210 const CacheStorageCacheQueryParams& match_params = 274 const CacheStorageCacheQueryParams& match_params =
211 CacheStorageCacheQueryParams()) { 275 CacheStorageCacheQueryParams()) {
212 ServiceWorkerFetchRequest request; 276 ServiceWorkerFetchRequest request;
213 request.url = url; 277 request.url = url;
214 return StorageMatchWithRequest(origin, cache_name, request, match_params); 278 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_; 450 scoped_refptr<MockSpecialStoragePolicy> quota_policy_;
387 scoped_refptr<MockQuotaManager> mock_quota_manager_; 451 scoped_refptr<MockQuotaManager> mock_quota_manager_;
388 scoped_refptr<MockQuotaManagerProxy> quota_manager_proxy_; 452 scoped_refptr<MockQuotaManagerProxy> quota_manager_proxy_;
389 std::unique_ptr<CacheStorageManager> cache_manager_; 453 std::unique_ptr<CacheStorageManager> cache_manager_;
390 454
391 std::unique_ptr<CacheStorageCacheHandle> callback_cache_handle_; 455 std::unique_ptr<CacheStorageCacheHandle> callback_cache_handle_;
392 int callback_bool_; 456 int callback_bool_;
393 CacheStorageError callback_error_; 457 CacheStorageError callback_error_;
394 std::unique_ptr<ServiceWorkerResponse> callback_cache_handle_response_; 458 std::unique_ptr<ServiceWorkerResponse> callback_cache_handle_response_;
395 std::unique_ptr<storage::BlobDataHandle> callback_data_handle_; 459 std::unique_ptr<storage::BlobDataHandle> callback_data_handle_;
396 std::vector<std::string> callback_strings_; 460 std::list<CacheStorage::CacheMetadata> cache_metadata_;
397 461
398 const GURL origin1_; 462 const GURL origin1_;
399 const GURL origin2_; 463 const GURL origin2_;
400 464
401 int64_t callback_usage_; 465 int64_t callback_usage_;
402 std::vector<CacheStorageUsageInfo> callback_all_origins_usage_; 466 std::vector<CacheStorageUsageInfo> callback_all_origins_usage_;
403 467
404 private: 468 private:
405 DISALLOW_COPY_AND_ASSIGN(CacheStorageManagerTest); 469 DISALLOW_COPY_AND_ASSIGN(CacheStorageManagerTest);
406 }; 470 };
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after
499 } 563 }
500 564
501 TEST_P(CacheStorageManagerTestP, SomeKeys) { 565 TEST_P(CacheStorageManagerTestP, SomeKeys) {
502 EXPECT_TRUE(Open(origin1_, "foo")); 566 EXPECT_TRUE(Open(origin1_, "foo"));
503 EXPECT_TRUE(Open(origin1_, "bar")); 567 EXPECT_TRUE(Open(origin1_, "bar"));
504 EXPECT_TRUE(Open(origin2_, "baz")); 568 EXPECT_TRUE(Open(origin2_, "baz"));
505 EXPECT_EQ(2u, Keys(origin1_)); 569 EXPECT_EQ(2u, Keys(origin1_));
506 std::vector<std::string> expected_keys; 570 std::vector<std::string> expected_keys;
507 expected_keys.push_back("foo"); 571 expected_keys.push_back("foo");
508 expected_keys.push_back("bar"); 572 expected_keys.push_back("bar");
509 EXPECT_EQ(expected_keys, callback_strings_); 573 EXPECT_EQ(expected_keys, GetCacheMetadataNames(cache_metadata_));
510 EXPECT_EQ(1u, Keys(origin2_)); 574 EXPECT_EQ(1u, Keys(origin2_));
511 EXPECT_STREQ("baz", callback_strings_[0].c_str()); 575 EXPECT_STREQ("baz", cache_metadata_.front().name.c_str());
512 } 576 }
513 577
514 TEST_P(CacheStorageManagerTestP, DeletedKeysGone) { 578 TEST_P(CacheStorageManagerTestP, DeletedKeysGone) {
515 EXPECT_TRUE(Open(origin1_, "foo")); 579 EXPECT_TRUE(Open(origin1_, "foo"));
516 EXPECT_TRUE(Open(origin1_, "bar")); 580 EXPECT_TRUE(Open(origin1_, "bar"));
517 EXPECT_TRUE(Open(origin2_, "baz")); 581 EXPECT_TRUE(Open(origin2_, "baz"));
518 EXPECT_TRUE(Delete(origin1_, "bar")); 582 EXPECT_TRUE(Delete(origin1_, "bar"));
519 EXPECT_EQ(1u, Keys(origin1_)); 583 EXPECT_EQ(1u, Keys(origin1_));
520 EXPECT_STREQ("foo", callback_strings_[0].c_str()); 584 EXPECT_STREQ("foo", cache_metadata_.front().name.c_str());
521 } 585 }
522 586
523 TEST_P(CacheStorageManagerTestP, StorageMatchEntryExists) { 587 TEST_P(CacheStorageManagerTestP, StorageMatchEntryExists) {
524 EXPECT_TRUE(Open(origin1_, "foo")); 588 EXPECT_TRUE(Open(origin1_, "foo"));
525 EXPECT_TRUE(CachePut(callback_cache_handle_->value(), 589 EXPECT_TRUE(CachePut(callback_cache_handle_->value(),
526 GURL("http://example.com/foo"))); 590 GURL("http://example.com/foo")));
527 EXPECT_TRUE(StorageMatch(origin1_, "foo", GURL("http://example.com/foo"))); 591 EXPECT_TRUE(StorageMatch(origin1_, "foo", GURL("http://example.com/foo")));
528 } 592 }
529 593
530 TEST_P(CacheStorageManagerTestP, StorageMatchNoEntry) { 594 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"))); 720 EXPECT_TRUE(StorageMatchAll(origin1_, GURL("http://example.com/foo")));
657 } 721 }
658 722
659 TEST_P(CacheStorageManagerTestP, Chinese) { 723 TEST_P(CacheStorageManagerTestP, Chinese) {
660 EXPECT_TRUE(Open(origin1_, "你好")); 724 EXPECT_TRUE(Open(origin1_, "你好"));
661 std::unique_ptr<CacheStorageCacheHandle> cache_handle = 725 std::unique_ptr<CacheStorageCacheHandle> cache_handle =
662 std::move(callback_cache_handle_); 726 std::move(callback_cache_handle_);
663 EXPECT_TRUE(Open(origin1_, "你好")); 727 EXPECT_TRUE(Open(origin1_, "你好"));
664 EXPECT_EQ(callback_cache_handle_->value(), cache_handle->value()); 728 EXPECT_EQ(callback_cache_handle_->value(), cache_handle->value());
665 EXPECT_EQ(1u, Keys(origin1_)); 729 EXPECT_EQ(1u, Keys(origin1_));
666 EXPECT_STREQ("你好", callback_strings_[0].c_str()); 730 EXPECT_STREQ("你好", cache_metadata_.front().name.c_str());
667 } 731 }
668 732
669 TEST_F(CacheStorageManagerTest, EmptyKey) { 733 TEST_F(CacheStorageManagerTest, EmptyKey) {
670 EXPECT_TRUE(Open(origin1_, "")); 734 EXPECT_TRUE(Open(origin1_, ""));
671 std::unique_ptr<CacheStorageCacheHandle> cache_handle = 735 std::unique_ptr<CacheStorageCacheHandle> cache_handle =
672 std::move(callback_cache_handle_); 736 std::move(callback_cache_handle_);
673 EXPECT_TRUE(Open(origin1_, "")); 737 EXPECT_TRUE(Open(origin1_, ""));
674 EXPECT_EQ(cache_handle->value(), callback_cache_handle_->value()); 738 EXPECT_EQ(cache_handle->value(), callback_cache_handle_->value());
675 EXPECT_EQ(1u, Keys(origin1_)); 739 EXPECT_EQ(1u, Keys(origin1_));
676 EXPECT_STREQ("", callback_strings_[0].c_str()); 740 EXPECT_STREQ("", cache_metadata_.front().name.c_str());
677 EXPECT_TRUE(Has(origin1_, "")); 741 EXPECT_TRUE(Has(origin1_, ""));
678 EXPECT_TRUE(Delete(origin1_, "")); 742 EXPECT_TRUE(Delete(origin1_, ""));
679 EXPECT_EQ(0u, Keys(origin1_)); 743 EXPECT_EQ(0u, Keys(origin1_));
680 } 744 }
681 745
682 TEST_F(CacheStorageManagerTest, DataPersists) { 746 TEST_F(CacheStorageManagerTest, DataPersists) {
683 EXPECT_TRUE(Open(origin1_, "foo")); 747 EXPECT_TRUE(Open(origin1_, "foo"));
684 EXPECT_TRUE(Open(origin1_, "bar")); 748 EXPECT_TRUE(Open(origin1_, "bar"));
685 EXPECT_TRUE(Open(origin1_, "baz")); 749 EXPECT_TRUE(Open(origin1_, "baz"));
686 EXPECT_TRUE(Open(origin2_, "raz")); 750 EXPECT_TRUE(Open(origin2_, "raz"));
687 EXPECT_TRUE(Delete(origin1_, "bar")); 751 EXPECT_TRUE(Delete(origin1_, "bar"));
688 quota_manager_proxy_->SimulateQuotaManagerDestroyed(); 752 quota_manager_proxy_->SimulateQuotaManagerDestroyed();
689 cache_manager_ = CacheStorageManager::Create(cache_manager_.get()); 753 cache_manager_ = CacheStorageManager::Create(cache_manager_.get());
690 EXPECT_EQ(2u, Keys(origin1_)); 754 EXPECT_EQ(2u, Keys(origin1_));
691 std::vector<std::string> expected_keys; 755 std::vector<std::string> expected_keys;
692 expected_keys.push_back("foo"); 756 expected_keys.push_back("foo");
693 expected_keys.push_back("baz"); 757 expected_keys.push_back("baz");
694 EXPECT_EQ(expected_keys, callback_strings_); 758 EXPECT_EQ(expected_keys, GetCacheMetadataNames(cache_metadata_));
695 } 759 }
696 760
697 TEST_F(CacheStorageManagerMemoryOnlyTest, DataLostWhenMemoryOnly) { 761 TEST_F(CacheStorageManagerMemoryOnlyTest, DataLostWhenMemoryOnly) {
698 EXPECT_TRUE(Open(origin1_, "foo")); 762 EXPECT_TRUE(Open(origin1_, "foo"));
699 EXPECT_TRUE(Open(origin2_, "baz")); 763 EXPECT_TRUE(Open(origin2_, "baz"));
700 quota_manager_proxy_->SimulateQuotaManagerDestroyed(); 764 quota_manager_proxy_->SimulateQuotaManagerDestroyed();
701 cache_manager_ = CacheStorageManager::Create(cache_manager_.get()); 765 cache_manager_ = CacheStorageManager::Create(cache_manager_.get());
702 EXPECT_EQ(0u, Keys(origin1_)); 766 EXPECT_EQ(0u, Keys(origin1_));
703 } 767 }
704 768
705 TEST_F(CacheStorageManagerTest, BadCacheName) { 769 TEST_F(CacheStorageManagerTest, BadCacheName) {
706 // Since the implementation writes cache names to disk, ensure that we don't 770 // Since the implementation writes cache names to disk, ensure that we don't
707 // escape the directory. 771 // escape the directory.
708 const std::string bad_name = "../../../../../../../../../../../../../../foo"; 772 const std::string bad_name = "../../../../../../../../../../../../../../foo";
709 EXPECT_TRUE(Open(origin1_, bad_name)); 773 EXPECT_TRUE(Open(origin1_, bad_name));
710 EXPECT_EQ(1u, Keys(origin1_)); 774 EXPECT_EQ(1u, Keys(origin1_));
711 EXPECT_STREQ(bad_name.c_str(), callback_strings_[0].c_str()); 775 EXPECT_STREQ(bad_name.c_str(), cache_metadata_.front().name.c_str());
712 } 776 }
713 777
714 TEST_F(CacheStorageManagerTest, BadOriginName) { 778 TEST_F(CacheStorageManagerTest, BadOriginName) {
715 // Since the implementation writes origin names to disk, ensure that we don't 779 // Since the implementation writes origin names to disk, ensure that we don't
716 // escape the directory. 780 // escape the directory.
717 GURL bad_origin("http://../../../../../../../../../../../../../../foo"); 781 GURL bad_origin("http://../../../../../../../../../../../../../../foo");
718 EXPECT_TRUE(Open(bad_origin, "foo")); 782 EXPECT_TRUE(Open(bad_origin, "foo"));
719 EXPECT_EQ(1u, Keys(bad_origin)); 783 EXPECT_EQ(1u, Keys(bad_origin));
720 EXPECT_STREQ("foo", callback_strings_[0].c_str()); 784 EXPECT_STREQ("foo", cache_metadata_.front().name.c_str());
721 } 785 }
722 786
723 // With a persistent cache if the client drops its reference to a 787 // With a persistent cache if the client drops its reference to a
724 // CacheStorageCache it should be deleted. 788 // CacheStorageCache it should be deleted.
725 TEST_F(CacheStorageManagerTest, DropReference) { 789 TEST_F(CacheStorageManagerTest, DropReference) {
726 EXPECT_TRUE(Open(origin1_, "foo")); 790 EXPECT_TRUE(Open(origin1_, "foo"));
727 base::WeakPtr<CacheStorageCache> cache = 791 base::WeakPtr<CacheStorageCache> cache =
728 callback_cache_handle_->value()->AsWeakPtr(); 792 callback_cache_handle_->value()->AsWeakPtr();
729 // Run a cache operation to ensure that the cache has finished initializing so 793 // Run a cache operation to ensure that the cache has finished initializing so
730 // that when the handle is dropped it can close immediately. 794 // that when the handle is dropped it can close immediately.
(...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after
853 917
854 if (MemoryOnly()) { 918 if (MemoryOnly()) {
855 EXPECT_TRUE(usage[origin1_index].last_modified.is_null()); 919 EXPECT_TRUE(usage[origin1_index].last_modified.is_null());
856 EXPECT_TRUE(usage[origin2_index].last_modified.is_null()); 920 EXPECT_TRUE(usage[origin2_index].last_modified.is_null());
857 } else { 921 } else {
858 EXPECT_FALSE(usage[origin1_index].last_modified.is_null()); 922 EXPECT_FALSE(usage[origin1_index].last_modified.is_null());
859 EXPECT_FALSE(usage[origin2_index].last_modified.is_null()); 923 EXPECT_FALSE(usage[origin2_index].last_modified.is_null());
860 } 924 }
861 } 925 }
862 926
927 TEST_P(CacheStorageManagerTestP, GetAllOriginsUsageWithOldIndex) {
928 // in-memory caches aren't persisted, so skip those.
929 if (MemoryOnly())
930 return;
931
932 // Write a single value (V1) to the cache.
933 const GURL kFooURL = origin1_.Resolve("foo");
934 const std::string kCacheName = "foo";
935 EXPECT_TRUE(Open(origin1_, kCacheName));
936 std::unique_ptr<CacheStorageCacheHandle> original_handle =
937 std::move(callback_cache_handle_);
938
939 EXPECT_TRUE(CachePut(original_handle->value(), kFooURL));
940 int64_t cache_size_v1 = Size(origin1_);
941 base::FilePath storage_dir = original_handle->value()->path().DirName();
942 original_handle.release();
943 EXPECT_GE(cache_size_v1, 0);
944
945 // Close the caches and cache manager.
946 DestroyStorageManager();
947
948 // Save a copy of the V1 index.
949 EXPECT_TRUE(IsIndexFileCurrent(storage_dir));
950 base::FilePath index_path = storage_dir.AppendASCII("index.txt");
951 EXPECT_TRUE(base::PathExists(index_path));
952 base::FilePath backup_index_path = storage_dir.AppendASCII("index.txt.bak");
953 EXPECT_TRUE(base::CopyFile(index_path, backup_index_path));
954
955 // Create a new CacheStorageManager that hasn't yet loaded the origin.
956 CreateStorageManager();
957 quota_manager_proxy_->SimulateQuotaManagerDestroyed();
958 cache_manager_ = CacheStorageManager::Create(cache_manager_.get());
959
960 // Create a second value (V2) in the cache.
961 EXPECT_TRUE(Open(origin1_, kCacheName));
962 original_handle = std::move(callback_cache_handle_);
963 const GURL kBarURL = origin1_.Resolve("bar");
964 EXPECT_TRUE(CachePut(original_handle->value(), kBarURL));
965 original_handle.release();
966
967 std::vector<CacheStorageUsageInfo> usage = GetAllOriginsUsage();
968 ASSERT_EQ(1ULL, usage.size());
969 int64_t usage_before_close = usage[0].total_size_bytes;
970 EXPECT_GT(usage_before_close, 0);
971
972 // Close the caches and cache manager.
973 DestroyStorageManager();
974
975 // Restore the index to the V1 state. Make the access/mod times of index file
976 // "older" than the other directories in the store to trigger size
977 // recalculation.
978 EXPECT_TRUE(IsIndexFileCurrent(storage_dir));
979 EXPECT_TRUE(base::CopyFile(backup_index_path, index_path));
980 base::Time t = base::Time::Now() - base::TimeDelta::FromHours(1);
981 EXPECT_TRUE(base::TouchFile(index_path, t, t));
982 EXPECT_FALSE(IsIndexFileCurrent(storage_dir));
983
984 CreateStorageManager();
985 usage = GetAllOriginsUsage();
986 ASSERT_EQ(1ULL, usage.size());
987
988 EXPECT_EQ(usage_before_close, usage[0].total_size_bytes);
989
990 EXPECT_FALSE(usage[0].last_modified.is_null());
991 }
992
993 TEST_P(CacheStorageManagerTestP, GetOriginSizeWithOldIndex) {
994 // in-memory caches aren't persisted, so skip those.
995 if (MemoryOnly())
996 return;
997
998 // Write a single value (V1) to the cache.
999 const GURL kFooURL = origin1_.Resolve("foo");
1000 const std::string kCacheName = "foo";
1001 EXPECT_TRUE(Open(origin1_, kCacheName));
1002 std::unique_ptr<CacheStorageCacheHandle> original_handle =
1003 std::move(callback_cache_handle_);
1004
1005 EXPECT_TRUE(CachePut(original_handle->value(), kFooURL));
1006 int64_t cache_size_v1 = Size(origin1_);
1007 base::FilePath storage_dir = original_handle->value()->path().DirName();
1008 original_handle.release();
1009 EXPECT_GE(cache_size_v1, 0);
1010
1011 // Close the caches and cache manager.
1012 DestroyStorageManager();
1013
1014 // Save a copy of the V1 index.
1015 EXPECT_TRUE(IsIndexFileCurrent(storage_dir));
1016 base::FilePath index_path = storage_dir.AppendASCII("index.txt");
1017 EXPECT_TRUE(base::PathExists(index_path));
1018 base::FilePath backup_index_path = storage_dir.AppendASCII("index.txt.bak");
1019 EXPECT_TRUE(base::CopyFile(index_path, backup_index_path));
1020
1021 // Create a new CacheStorageManager that hasn't yet loaded the origin.
1022 CreateStorageManager();
1023 quota_manager_proxy_->SimulateQuotaManagerDestroyed();
1024 cache_manager_ = CacheStorageManager::Create(cache_manager_.get());
1025
1026 // Reopen the cache and write a second value (V2).
1027 EXPECT_TRUE(Open(origin1_, kCacheName));
1028 original_handle = std::move(callback_cache_handle_);
1029 const GURL kBarURL = origin1_.Resolve("bar");
1030 EXPECT_TRUE(CachePut(original_handle->value(), kBarURL));
1031 original_handle.release();
1032 int64_t cache_size_v2 = GetSizeThenCloseAllCaches(origin1_);
1033 EXPECT_GE(cache_size_v2, 0);
1034
1035 // Close the caches and cache manager.
1036 DestroyStorageManager();
1037
1038 // Restore the index to the V1 state.
1039 EXPECT_TRUE(IsIndexFileCurrent(storage_dir));
1040 EXPECT_TRUE(base::CopyFile(backup_index_path, index_path));
1041
1042 // Make the access/mod times of index file "older" than the other files in the
1043 // cache to trigger size recalculation.
1044 base::Time t = base::Time::Now() - base::TimeDelta::FromHours(1);
1045 EXPECT_TRUE(base::TouchFile(index_path, t, t));
1046 EXPECT_FALSE(IsIndexFileCurrent(storage_dir));
1047
1048 // Reopen the cache and ensure the size is correct for the V2 value.
1049 CreateStorageManager();
1050 EXPECT_TRUE(Open(origin1_, kCacheName));
1051 EXPECT_EQ(cache_size_v2, Size(origin1_));
1052 }
1053
863 TEST_P(CacheStorageManagerTestP, GetSizeThenCloseAllCaches) { 1054 TEST_P(CacheStorageManagerTestP, GetSizeThenCloseAllCaches) {
864 EXPECT_TRUE(Open(origin1_, "foo")); 1055 EXPECT_TRUE(Open(origin1_, "foo"));
865 EXPECT_TRUE(CachePut(callback_cache_handle_->value(), 1056 EXPECT_TRUE(CachePut(callback_cache_handle_->value(),
866 GURL("http://example.com/foo"))); 1057 GURL("http://example.com/foo")));
867 EXPECT_TRUE(CachePut(callback_cache_handle_->value(), 1058 EXPECT_TRUE(CachePut(callback_cache_handle_->value(),
868 GURL("http://example.com/foo2"))); 1059 GURL("http://example.com/foo2")));
869 EXPECT_TRUE(Open(origin1_, "bar")); 1060 EXPECT_TRUE(Open(origin1_, "bar"));
870 EXPECT_TRUE(CachePut(callback_cache_handle_->value(), 1061 EXPECT_TRUE(CachePut(callback_cache_handle_->value(),
871 GURL("http://example.com/bar"))); 1062 GURL("http://example.com/bar")));
872 1063
(...skipping 364 matching lines...) Expand 10 before | Expand all | Expand 10 after
1237 1428
1238 INSTANTIATE_TEST_CASE_P(CacheStorageManagerTests, 1429 INSTANTIATE_TEST_CASE_P(CacheStorageManagerTests,
1239 CacheStorageManagerTestP, 1430 CacheStorageManagerTestP,
1240 ::testing::Values(false, true)); 1431 ::testing::Values(false, true));
1241 1432
1242 INSTANTIATE_TEST_CASE_P(CacheStorageQuotaClientTests, 1433 INSTANTIATE_TEST_CASE_P(CacheStorageQuotaClientTests,
1243 CacheStorageQuotaClientTestP, 1434 CacheStorageQuotaClientTestP,
1244 ::testing::Values(false, true)); 1435 ::testing::Values(false, true));
1245 1436
1246 } // namespace content 1437 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698