blob: d3a212573253c0cb57fdae0040f3ea73c0dd3fac [file] [log] [blame]
[email protected]05f8087d2012-06-29 18:58:371// Copyright (c) 2012 The Chromium Authors. All rights reserved.
license.botbf09a502008-08-24 00:55:552// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
initial.commit586acc5fe2008-07-26 22:42:524
5#include "net/disk_cache/disk_cache_test_base.h"
6
dchengc7eeda422015-12-26 03:56:487#include <utility>
8
thestigd8df0332014-09-04 06:33:299#include "base/files/file_util.h"
[email protected]79b3fec2011-11-15 22:33:4710#include "base/path_service.h"
[email protected]94a9d1a2013-03-24 09:50:3111#include "base/run_loop.h"
[email protected]81e549a2014-08-21 04:19:5512#include "base/single_thread_task_runner.h"
13#include "base/thread_task_runner_handle.h"
[email protected]8b2f8a52013-08-28 08:45:3514#include "base/threading/platform_thread.h"
[email protected]e1fcf142010-08-23 18:47:2515#include "net/base/io_buffer.h"
[email protected]fb2622f2010-07-13 18:00:5616#include "net/base/net_errors.h"
[email protected]bc4fb8e2010-03-18 23:58:1717#include "net/base/test_completion_callback.h"
[email protected]c2c5cfc2014-03-03 16:35:2818#include "net/disk_cache/blockfile/backend_impl.h"
[email protected]398ad132013-04-02 15:11:0119#include "net/disk_cache/cache_util.h"
20#include "net/disk_cache/disk_cache.h"
initial.commit586acc5fe2008-07-26 22:42:5221#include "net/disk_cache/disk_cache_test_util.h"
[email protected]c2c5cfc2014-03-03 16:35:2822#include "net/disk_cache/memory/mem_backend_impl.h"
[email protected]398ad132013-04-02 15:11:0123#include "net/disk_cache/simple/simple_backend_impl.h"
[email protected]7e48c1b2013-07-12 12:15:4324#include "net/disk_cache/simple/simple_index.h"
initial.commit586acc5fe2008-07-26 22:42:5225
[email protected]79b3fec2011-11-15 22:33:4726DiskCacheTest::DiskCacheTest() {
[email protected]784f9302012-08-17 00:13:4327 CHECK(temp_dir_.CreateUniqueTempDir());
28 cache_path_ = temp_dir_.path();
[email protected]2da659e2013-05-23 20:51:3429 if (!base::MessageLoop::current())
30 message_loop_.reset(new base::MessageLoopForIO());
[email protected]79b3fec2011-11-15 22:33:4731}
32
33DiskCacheTest::~DiskCacheTest() {
34}
35
36bool DiskCacheTest::CopyTestCache(const std::string& name) {
[email protected]6cdfd7f2013-02-08 20:40:1537 base::FilePath path;
[email protected]79b3fec2011-11-15 22:33:4738 PathService::Get(base::DIR_SOURCE_ROOT, &path);
39 path = path.AppendASCII("net");
40 path = path.AppendASCII("data");
41 path = path.AppendASCII("cache_tests");
42 path = path.AppendASCII(name);
43
44 if (!CleanupCacheDir())
45 return false;
[email protected]f0ff2ad2013-07-09 17:42:2646 return base::CopyDirectory(path, cache_path_, false);
[email protected]79b3fec2011-11-15 22:33:4747}
48
49bool DiskCacheTest::CleanupCacheDir() {
50 return DeleteCache(cache_path_);
51}
52
[email protected]17b89142008-11-07 21:52:1553void DiskCacheTest::TearDown() {
[email protected]94a9d1a2013-03-24 09:50:3154 base::RunLoop().RunUntilIdle();
[email protected]17b89142008-11-07 21:52:1555}
56
gavinpb1aaa052014-09-24 14:54:3557DiskCacheTestWithCache::TestIterator::TestIterator(
58 scoped_ptr<disk_cache::Backend::Iterator> iterator)
dchengc7eeda422015-12-26 03:56:4859 : iterator_(std::move(iterator)) {}
gavinpb1aaa052014-09-24 14:54:3560
61DiskCacheTestWithCache::TestIterator::~TestIterator() {}
62
63int DiskCacheTestWithCache::TestIterator::OpenNextEntry(
64 disk_cache::Entry** next_entry) {
65 net::TestCompletionCallback cb;
66 int rv = iterator_->OpenNextEntry(next_entry, cb.callback());
67 return cb.GetResult(rv);
68}
69
[email protected]7aefb152011-01-21 23:46:4970DiskCacheTestWithCache::DiskCacheTestWithCache()
[email protected]8c3f5a32013-08-01 11:57:5371 : cache_impl_(NULL),
[email protected]1ed95752013-04-23 00:12:3672 simple_cache_impl_(NULL),
[email protected]7aefb152011-01-21 23:46:4973 mem_cache_(NULL),
74 mask_(0),
75 size_(0),
76 type_(net::DISK_CACHE),
77 memory_only_(false),
[email protected]398ad132013-04-02 15:11:0178 simple_cache_mode_(false),
[email protected]7e48c1b2013-07-12 12:15:4379 simple_cache_wait_for_index_(true),
[email protected]7aefb152011-01-21 23:46:4980 force_creation_(false),
81 new_eviction_(false),
82 first_cleanup_(true),
83 integrity_(true),
84 use_current_thread_(false),
85 cache_thread_("CacheThread") {
initial.commit586acc5fe2008-07-26 22:42:5286}
87
[email protected]7aefb152011-01-21 23:46:4988DiskCacheTestWithCache::~DiskCacheTestWithCache() {}
89
[email protected]4d5e0362008-08-28 00:59:0690void DiskCacheTestWithCache::InitCache() {
initial.commit586acc5fe2008-07-26 22:42:5291 if (memory_only_)
92 InitMemoryCache();
93 else
94 InitDiskCache();
95
96 ASSERT_TRUE(NULL != cache_);
[email protected]a2553842013-04-19 14:25:1897 if (first_cleanup_)
[email protected]a9da16d2008-07-30 21:41:5498 ASSERT_EQ(0, cache_->GetEntryCount());
initial.commit586acc5fe2008-07-26 22:42:5299}
100
initial.commit586acc5fe2008-07-26 22:42:52101// We are expected to leak memory when simulating crashes.
[email protected]4d5e0362008-08-28 00:59:06102void DiskCacheTestWithCache::SimulateCrash() {
[email protected]398ad132013-04-02 15:11:01103 ASSERT_TRUE(!memory_only_);
[email protected]2a65aceb82011-12-19 20:59:27104 net::TestCompletionCallback cb;
105 int rv = cache_impl_->FlushQueueForTest(cb.callback());
[email protected]fb2622f2010-07-13 18:00:56106 ASSERT_EQ(net::OK, cb.GetResult(rv));
initial.commit586acc5fe2008-07-26 22:42:52107 cache_impl_->ClearRefCountForTest();
108
[email protected]8c3f5a32013-08-01 11:57:53109 cache_.reset();
[email protected]79b3fec2011-11-15 22:33:47110 EXPECT_TRUE(CheckCacheIntegrity(cache_path_, new_eviction_, mask_));
initial.commit586acc5fe2008-07-26 22:42:52111
[email protected]398ad132013-04-02 15:11:01112 CreateBackend(disk_cache::kNoRandom, &cache_thread_);
initial.commit586acc5fe2008-07-26 22:42:52113}
[email protected]a9da16d2008-07-30 21:41:54114
[email protected]4d5e0362008-08-28 00:59:06115void DiskCacheTestWithCache::SetTestMode() {
[email protected]398ad132013-04-02 15:11:01116 ASSERT_TRUE(!memory_only_);
[email protected]a9da16d2008-07-30 21:41:54117 cache_impl_->SetUnitTestMode();
118}
[email protected]bc4fb8e2010-03-18 23:58:17119
[email protected]7aefb152011-01-21 23:46:49120void DiskCacheTestWithCache::SetMaxSize(int size) {
121 size_ = size;
[email protected]1ed95752013-04-23 00:12:36122 if (simple_cache_impl_)
123 EXPECT_TRUE(simple_cache_impl_->SetMaxSize(size));
124
[email protected]7aefb152011-01-21 23:46:49125 if (cache_impl_)
126 EXPECT_TRUE(cache_impl_->SetMaxSize(size));
127
128 if (mem_cache_)
129 EXPECT_TRUE(mem_cache_->SetMaxSize(size));
130}
131
[email protected]bc4fb8e2010-03-18 23:58:17132int DiskCacheTestWithCache::OpenEntry(const std::string& key,
133 disk_cache::Entry** entry) {
[email protected]2a65aceb82011-12-19 20:59:27134 net::TestCompletionCallback cb;
135 int rv = cache_->OpenEntry(key, entry, cb.callback());
[email protected]bc4fb8e2010-03-18 23:58:17136 return cb.GetResult(rv);
137}
138
139int DiskCacheTestWithCache::CreateEntry(const std::string& key,
140 disk_cache::Entry** entry) {
[email protected]2a65aceb82011-12-19 20:59:27141 net::TestCompletionCallback cb;
142 int rv = cache_->CreateEntry(key, entry, cb.callback());
[email protected]bc4fb8e2010-03-18 23:58:17143 return cb.GetResult(rv);
144}
145
146int DiskCacheTestWithCache::DoomEntry(const std::string& key) {
[email protected]42c459632011-12-17 02:20:23147 net::TestCompletionCallback cb;
148 int rv = cache_->DoomEntry(key, cb.callback());
[email protected]bc4fb8e2010-03-18 23:58:17149 return cb.GetResult(rv);
150}
151
152int DiskCacheTestWithCache::DoomAllEntries() {
[email protected]6ad7c0912011-12-15 19:10:19153 net::TestCompletionCallback cb;
154 int rv = cache_->DoomAllEntries(cb.callback());
[email protected]bc4fb8e2010-03-18 23:58:17155 return cb.GetResult(rv);
156}
157
158int DiskCacheTestWithCache::DoomEntriesBetween(const base::Time initial_time,
159 const base::Time end_time) {
[email protected]6ad7c0912011-12-15 19:10:19160 net::TestCompletionCallback cb;
161 int rv = cache_->DoomEntriesBetween(initial_time, end_time, cb.callback());
[email protected]bc4fb8e2010-03-18 23:58:17162 return cb.GetResult(rv);
163}
164
165int DiskCacheTestWithCache::DoomEntriesSince(const base::Time initial_time) {
[email protected]2a65aceb82011-12-19 20:59:27166 net::TestCompletionCallback cb;
167 int rv = cache_->DoomEntriesSince(initial_time, cb.callback());
[email protected]bc4fb8e2010-03-18 23:58:17168 return cb.GetResult(rv);
169}
170
msramekaee01ceb2015-10-07 14:23:33171int DiskCacheTestWithCache::CalculateSizeOfAllEntries() {
172 net::TestCompletionCallback cb;
173 int rv = cache_->CalculateSizeOfAllEntries(cb.callback());
174 return cb.GetResult(rv);
175}
176
gavinpb1aaa052014-09-24 14:54:35177scoped_ptr<DiskCacheTestWithCache::TestIterator>
178 DiskCacheTestWithCache::CreateIterator() {
179 return scoped_ptr<TestIterator>(new TestIterator(cache_->CreateIterator()));
[email protected]bc4fb8e2010-03-18 23:58:17180}
[email protected]fb2622f2010-07-13 18:00:56181
182void DiskCacheTestWithCache::FlushQueueForTest() {
183 if (memory_only_ || !cache_impl_)
184 return;
185
[email protected]2a65aceb82011-12-19 20:59:27186 net::TestCompletionCallback cb;
187 int rv = cache_impl_->FlushQueueForTest(cb.callback());
[email protected]fb2622f2010-07-13 18:00:56188 EXPECT_EQ(net::OK, cb.GetResult(rv));
189}
[email protected]e1fcf142010-08-23 18:47:25190
[email protected]f27bbe002011-12-22 11:29:34191void DiskCacheTestWithCache::RunTaskForTest(const base::Closure& closure) {
[email protected]65188eb2010-09-16 20:59:29192 if (memory_only_ || !cache_impl_) {
[email protected]f27bbe002011-12-22 11:29:34193 closure.Run();
[email protected]65188eb2010-09-16 20:59:29194 return;
195 }
196
[email protected]2a65aceb82011-12-19 20:59:27197 net::TestCompletionCallback cb;
[email protected]f27bbe002011-12-22 11:29:34198 int rv = cache_impl_->RunTaskForTest(closure, cb.callback());
[email protected]65188eb2010-09-16 20:59:29199 EXPECT_EQ(net::OK, cb.GetResult(rv));
200}
201
[email protected]e1fcf142010-08-23 18:47:25202int DiskCacheTestWithCache::ReadData(disk_cache::Entry* entry, int index,
203 int offset, net::IOBuffer* buf, int len) {
[email protected]2a65aceb82011-12-19 20:59:27204 net::TestCompletionCallback cb;
205 int rv = entry->ReadData(index, offset, buf, len, cb.callback());
[email protected]e1fcf142010-08-23 18:47:25206 return cb.GetResult(rv);
207}
208
[email protected]e1fcf142010-08-23 18:47:25209int DiskCacheTestWithCache::WriteData(disk_cache::Entry* entry, int index,
210 int offset, net::IOBuffer* buf, int len,
211 bool truncate) {
[email protected]2a65aceb82011-12-19 20:59:27212 net::TestCompletionCallback cb;
213 int rv = entry->WriteData(index, offset, buf, len, cb.callback(), truncate);
[email protected]e1fcf142010-08-23 18:47:25214 return cb.GetResult(rv);
215}
216
217int DiskCacheTestWithCache::ReadSparseData(disk_cache::Entry* entry,
Avi Drissman13fc8932015-12-20 04:40:46218 int64_t offset,
219 net::IOBuffer* buf,
[email protected]e1fcf142010-08-23 18:47:25220 int len) {
[email protected]2a65aceb82011-12-19 20:59:27221 net::TestCompletionCallback cb;
222 int rv = entry->ReadSparseData(offset, buf, len, cb.callback());
[email protected]e1fcf142010-08-23 18:47:25223 return cb.GetResult(rv);
224}
225
226int DiskCacheTestWithCache::WriteSparseData(disk_cache::Entry* entry,
Avi Drissman13fc8932015-12-20 04:40:46227 int64_t offset,
228 net::IOBuffer* buf,
229 int len) {
[email protected]2a65aceb82011-12-19 20:59:27230 net::TestCompletionCallback cb;
231 int rv = entry->WriteSparseData(offset, buf, len, cb.callback());
[email protected]e1fcf142010-08-23 18:47:25232 return cb.GetResult(rv);
233}
[email protected]7aefb152011-01-21 23:46:49234
[email protected]ceb61da32011-01-25 23:52:02235void DiskCacheTestWithCache::TrimForTest(bool empty) {
[email protected]f27bbe002011-12-22 11:29:34236 RunTaskForTest(base::Bind(&disk_cache::BackendImpl::TrimForTest,
237 base::Unretained(cache_impl_),
238 empty));
[email protected]ceb61da32011-01-25 23:52:02239}
240
241void DiskCacheTestWithCache::TrimDeletedListForTest(bool empty) {
[email protected]f27bbe002011-12-22 11:29:34242 RunTaskForTest(base::Bind(&disk_cache::BackendImpl::TrimDeletedListForTest,
243 base::Unretained(cache_impl_),
244 empty));
[email protected]ceb61da32011-01-25 23:52:02245}
246
[email protected]05f8087d2012-06-29 18:58:37247void DiskCacheTestWithCache::AddDelay() {
[email protected]8b2f8a52013-08-28 08:45:35248 if (simple_cache_mode_) {
249 // The simple cache uses second resolution for many timeouts, so it's safest
250 // to advance by at least whole seconds before falling back into the normal
251 // disk cache epsilon advance.
252 const base::Time initial_time = base::Time::Now();
253 do {
254 base::PlatformThread::YieldCurrentThread();
255 } while (base::Time::Now() -
256 initial_time < base::TimeDelta::FromSeconds(1));
257 }
258
[email protected]05f8087d2012-06-29 18:58:37259 base::Time initial = base::Time::Now();
260 while (base::Time::Now() <= initial) {
261 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(1));
262 };
263}
264
[email protected]7aefb152011-01-21 23:46:49265void DiskCacheTestWithCache::TearDown() {
[email protected]94a9d1a2013-03-24 09:50:31266 base::RunLoop().RunUntilIdle();
[email protected]71cfb0892013-08-30 18:29:14267 disk_cache::SimpleBackendImpl::FlushWorkerPoolForTesting();
268 base::RunLoop().RunUntilIdle();
[email protected]8c3f5a32013-08-01 11:57:53269 cache_.reset();
[email protected]7aefb152011-01-21 23:46:49270 if (cache_thread_.IsRunning())
271 cache_thread_.Stop();
272
[email protected]fccce902013-04-15 19:00:51273 if (!memory_only_ && !simple_cache_mode_ && integrity_) {
[email protected]79b3fec2011-11-15 22:33:47274 EXPECT_TRUE(CheckCacheIntegrity(cache_path_, new_eviction_, mask_));
[email protected]7aefb152011-01-21 23:46:49275 }
[email protected]de677dc2013-09-13 02:12:25276 base::RunLoop().RunUntilIdle();
277 disk_cache::SimpleBackendImpl::FlushWorkerPoolForTesting();
278 DiskCacheTest::TearDown();
[email protected]7aefb152011-01-21 23:46:49279}
280
281void DiskCacheTestWithCache::InitMemoryCache() {
[email protected]9eb8cdf2011-03-17 18:53:02282 mem_cache_ = new disk_cache::MemBackendImpl(NULL);
[email protected]8c3f5a32013-08-01 11:57:53283 cache_.reset(mem_cache_);
284 ASSERT_TRUE(cache_);
[email protected]7aefb152011-01-21 23:46:49285
286 if (size_)
287 EXPECT_TRUE(mem_cache_->SetMaxSize(size_));
288
289 ASSERT_TRUE(mem_cache_->Init());
290}
291
292void DiskCacheTestWithCache::InitDiskCache() {
[email protected]7aefb152011-01-21 23:46:49293 if (first_cleanup_)
[email protected]79b3fec2011-11-15 22:33:47294 ASSERT_TRUE(CleanupCacheDir());
[email protected]7aefb152011-01-21 23:46:49295
296 if (!cache_thread_.IsRunning()) {
[email protected]398ad132013-04-02 15:11:01297 ASSERT_TRUE(cache_thread_.StartWithOptions(
[email protected]2da659e2013-05-23 20:51:34298 base::Thread::Options(base::MessageLoop::TYPE_IO, 0)));
[email protected]7aefb152011-01-21 23:46:49299 }
300 ASSERT_TRUE(cache_thread_.message_loop() != NULL);
301
[email protected]398ad132013-04-02 15:11:01302 CreateBackend(disk_cache::kNoRandom, &cache_thread_);
303}
[email protected]7aefb152011-01-21 23:46:49304
Avi Drissman13fc8932015-12-20 04:40:46305void DiskCacheTestWithCache::CreateBackend(uint32_t flags,
306 base::Thread* thread) {
[email protected]81e549a2014-08-21 04:19:55307 scoped_refptr<base::SingleThreadTaskRunner> runner;
[email protected]398ad132013-04-02 15:11:01308 if (use_current_thread_)
[email protected]81e549a2014-08-21 04:19:55309 runner = base::ThreadTaskRunnerHandle::Get();
[email protected]7aefb152011-01-21 23:46:49310 else
[email protected]81e549a2014-08-21 04:19:55311 runner = thread->task_runner();
[email protected]7aefb152011-01-21 23:46:49312
[email protected]398ad132013-04-02 15:11:01313 if (simple_cache_mode_) {
314 net::TestCompletionCallback cb;
[email protected]8c3f5a32013-08-01 11:57:53315 scoped_ptr<disk_cache::SimpleBackendImpl> simple_backend(
[email protected]cadac622013-06-11 16:46:36316 new disk_cache::SimpleBackendImpl(
[email protected]81e549a2014-08-21 04:19:55317 cache_path_, size_, type_, runner, NULL));
[email protected]00831c822013-04-10 15:37:12318 int rv = simple_backend->Init(cb.callback());
[email protected]398ad132013-04-02 15:11:01319 ASSERT_EQ(net::OK, cb.GetResult(rv));
[email protected]8c3f5a32013-08-01 11:57:53320 simple_cache_impl_ = simple_backend.get();
dchengc7eeda422015-12-26 03:56:48321 cache_ = std::move(simple_backend);
[email protected]7e48c1b2013-07-12 12:15:43322 if (simple_cache_wait_for_index_) {
323 net::TestCompletionCallback wait_for_index_cb;
[email protected]8c3f5a32013-08-01 11:57:53324 rv = simple_cache_impl_->index()->ExecuteWhenReady(
[email protected]7e48c1b2013-07-12 12:15:43325 wait_for_index_cb.callback());
326 ASSERT_EQ(net::OK, wait_for_index_cb.GetResult(rv));
327 }
[email protected]398ad132013-04-02 15:11:01328 return;
329 }
330
331 if (mask_)
332 cache_impl_ = new disk_cache::BackendImpl(cache_path_, mask_, runner, NULL);
333 else
334 cache_impl_ = new disk_cache::BackendImpl(cache_path_, runner, NULL);
[email protected]8c3f5a32013-08-01 11:57:53335 cache_.reset(cache_impl_);
336 ASSERT_TRUE(cache_);
[email protected]7aefb152011-01-21 23:46:49337 if (size_)
338 EXPECT_TRUE(cache_impl_->SetMaxSize(size_));
[email protected]7aefb152011-01-21 23:46:49339 if (new_eviction_)
340 cache_impl_->SetNewEviction();
[email protected]7aefb152011-01-21 23:46:49341 cache_impl_->SetType(type_);
[email protected]398ad132013-04-02 15:11:01342 cache_impl_->SetFlags(flags);
[email protected]42c459632011-12-17 02:20:23343 net::TestCompletionCallback cb;
344 int rv = cache_impl_->Init(cb.callback());
[email protected]7aefb152011-01-21 23:46:49345 ASSERT_EQ(net::OK, cb.GetResult(rv));
346}