blob: 345793baabf0b6b74a73d3caccf83a9bb478bb75 [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
thestigd8df0332014-09-04 06:33:297#include "base/files/file_util.h"
[email protected]79b3fec2011-11-15 22:33:478#include "base/path_service.h"
[email protected]94a9d1a2013-03-24 09:50:319#include "base/run_loop.h"
[email protected]81e549a2014-08-21 04:19:5510#include "base/single_thread_task_runner.h"
11#include "base/thread_task_runner_handle.h"
[email protected]8b2f8a52013-08-28 08:45:3512#include "base/threading/platform_thread.h"
[email protected]e1fcf142010-08-23 18:47:2513#include "net/base/io_buffer.h"
[email protected]fb2622f2010-07-13 18:00:5614#include "net/base/net_errors.h"
[email protected]bc4fb8e2010-03-18 23:58:1715#include "net/base/test_completion_callback.h"
[email protected]c2c5cfc2014-03-03 16:35:2816#include "net/disk_cache/blockfile/backend_impl.h"
[email protected]398ad132013-04-02 15:11:0117#include "net/disk_cache/cache_util.h"
18#include "net/disk_cache/disk_cache.h"
initial.commit586acc5fe2008-07-26 22:42:5219#include "net/disk_cache/disk_cache_test_util.h"
[email protected]c2c5cfc2014-03-03 16:35:2820#include "net/disk_cache/memory/mem_backend_impl.h"
[email protected]398ad132013-04-02 15:11:0121#include "net/disk_cache/simple/simple_backend_impl.h"
[email protected]7e48c1b2013-07-12 12:15:4322#include "net/disk_cache/simple/simple_index.h"
initial.commit586acc5fe2008-07-26 22:42:5223
[email protected]79b3fec2011-11-15 22:33:4724DiskCacheTest::DiskCacheTest() {
[email protected]784f9302012-08-17 00:13:4325 CHECK(temp_dir_.CreateUniqueTempDir());
26 cache_path_ = temp_dir_.path();
[email protected]2da659e2013-05-23 20:51:3427 if (!base::MessageLoop::current())
28 message_loop_.reset(new base::MessageLoopForIO());
[email protected]79b3fec2011-11-15 22:33:4729}
30
31DiskCacheTest::~DiskCacheTest() {
32}
33
34bool DiskCacheTest::CopyTestCache(const std::string& name) {
[email protected]6cdfd7f2013-02-08 20:40:1535 base::FilePath path;
[email protected]79b3fec2011-11-15 22:33:4736 PathService::Get(base::DIR_SOURCE_ROOT, &path);
37 path = path.AppendASCII("net");
38 path = path.AppendASCII("data");
39 path = path.AppendASCII("cache_tests");
40 path = path.AppendASCII(name);
41
42 if (!CleanupCacheDir())
43 return false;
[email protected]f0ff2ad2013-07-09 17:42:2644 return base::CopyDirectory(path, cache_path_, false);
[email protected]79b3fec2011-11-15 22:33:4745}
46
47bool DiskCacheTest::CleanupCacheDir() {
48 return DeleteCache(cache_path_);
49}
50
[email protected]17b89142008-11-07 21:52:1551void DiskCacheTest::TearDown() {
[email protected]94a9d1a2013-03-24 09:50:3152 base::RunLoop().RunUntilIdle();
[email protected]17b89142008-11-07 21:52:1553}
54
gavinpb1aaa052014-09-24 14:54:3555DiskCacheTestWithCache::TestIterator::TestIterator(
56 scoped_ptr<disk_cache::Backend::Iterator> iterator)
57 : iterator_(iterator.Pass()) {
58}
59
60DiskCacheTestWithCache::TestIterator::~TestIterator() {}
61
62int DiskCacheTestWithCache::TestIterator::OpenNextEntry(
63 disk_cache::Entry** next_entry) {
64 net::TestCompletionCallback cb;
65 int rv = iterator_->OpenNextEntry(next_entry, cb.callback());
66 return cb.GetResult(rv);
67}
68
[email protected]7aefb152011-01-21 23:46:4969DiskCacheTestWithCache::DiskCacheTestWithCache()
[email protected]8c3f5a32013-08-01 11:57:5370 : cache_impl_(NULL),
[email protected]1ed95752013-04-23 00:12:3671 simple_cache_impl_(NULL),
[email protected]7aefb152011-01-21 23:46:4972 mem_cache_(NULL),
73 mask_(0),
74 size_(0),
75 type_(net::DISK_CACHE),
76 memory_only_(false),
[email protected]398ad132013-04-02 15:11:0177 simple_cache_mode_(false),
[email protected]7e48c1b2013-07-12 12:15:4378 simple_cache_wait_for_index_(true),
[email protected]7aefb152011-01-21 23:46:4979 force_creation_(false),
80 new_eviction_(false),
81 first_cleanup_(true),
82 integrity_(true),
83 use_current_thread_(false),
84 cache_thread_("CacheThread") {
initial.commit586acc5fe2008-07-26 22:42:5285}
86
[email protected]7aefb152011-01-21 23:46:4987DiskCacheTestWithCache::~DiskCacheTestWithCache() {}
88
[email protected]4d5e0362008-08-28 00:59:0689void DiskCacheTestWithCache::InitCache() {
initial.commit586acc5fe2008-07-26 22:42:5290 if (memory_only_)
91 InitMemoryCache();
92 else
93 InitDiskCache();
94
95 ASSERT_TRUE(NULL != cache_);
[email protected]a2553842013-04-19 14:25:1896 if (first_cleanup_)
[email protected]a9da16d2008-07-30 21:41:5497 ASSERT_EQ(0, cache_->GetEntryCount());
initial.commit586acc5fe2008-07-26 22:42:5298}
99
initial.commit586acc5fe2008-07-26 22:42:52100// We are expected to leak memory when simulating crashes.
[email protected]4d5e0362008-08-28 00:59:06101void DiskCacheTestWithCache::SimulateCrash() {
[email protected]398ad132013-04-02 15:11:01102 ASSERT_TRUE(!memory_only_);
[email protected]2a65aceb82011-12-19 20:59:27103 net::TestCompletionCallback cb;
104 int rv = cache_impl_->FlushQueueForTest(cb.callback());
[email protected]fb2622f2010-07-13 18:00:56105 ASSERT_EQ(net::OK, cb.GetResult(rv));
initial.commit586acc5fe2008-07-26 22:42:52106 cache_impl_->ClearRefCountForTest();
107
[email protected]8c3f5a32013-08-01 11:57:53108 cache_.reset();
[email protected]79b3fec2011-11-15 22:33:47109 EXPECT_TRUE(CheckCacheIntegrity(cache_path_, new_eviction_, mask_));
initial.commit586acc5fe2008-07-26 22:42:52110
[email protected]398ad132013-04-02 15:11:01111 CreateBackend(disk_cache::kNoRandom, &cache_thread_);
initial.commit586acc5fe2008-07-26 22:42:52112}
[email protected]a9da16d2008-07-30 21:41:54113
[email protected]4d5e0362008-08-28 00:59:06114void DiskCacheTestWithCache::SetTestMode() {
[email protected]398ad132013-04-02 15:11:01115 ASSERT_TRUE(!memory_only_);
[email protected]a9da16d2008-07-30 21:41:54116 cache_impl_->SetUnitTestMode();
117}
[email protected]bc4fb8e2010-03-18 23:58:17118
[email protected]7aefb152011-01-21 23:46:49119void DiskCacheTestWithCache::SetMaxSize(int size) {
120 size_ = size;
[email protected]1ed95752013-04-23 00:12:36121 if (simple_cache_impl_)
122 EXPECT_TRUE(simple_cache_impl_->SetMaxSize(size));
123
[email protected]7aefb152011-01-21 23:46:49124 if (cache_impl_)
125 EXPECT_TRUE(cache_impl_->SetMaxSize(size));
126
127 if (mem_cache_)
128 EXPECT_TRUE(mem_cache_->SetMaxSize(size));
129}
130
[email protected]bc4fb8e2010-03-18 23:58:17131int DiskCacheTestWithCache::OpenEntry(const std::string& key,
132 disk_cache::Entry** entry) {
[email protected]2a65aceb82011-12-19 20:59:27133 net::TestCompletionCallback cb;
134 int rv = cache_->OpenEntry(key, entry, cb.callback());
[email protected]bc4fb8e2010-03-18 23:58:17135 return cb.GetResult(rv);
136}
137
138int DiskCacheTestWithCache::CreateEntry(const std::string& key,
139 disk_cache::Entry** entry) {
[email protected]2a65aceb82011-12-19 20:59:27140 net::TestCompletionCallback cb;
141 int rv = cache_->CreateEntry(key, entry, cb.callback());
[email protected]bc4fb8e2010-03-18 23:58:17142 return cb.GetResult(rv);
143}
144
145int DiskCacheTestWithCache::DoomEntry(const std::string& key) {
[email protected]42c459632011-12-17 02:20:23146 net::TestCompletionCallback cb;
147 int rv = cache_->DoomEntry(key, cb.callback());
[email protected]bc4fb8e2010-03-18 23:58:17148 return cb.GetResult(rv);
149}
150
151int DiskCacheTestWithCache::DoomAllEntries() {
[email protected]6ad7c0912011-12-15 19:10:19152 net::TestCompletionCallback cb;
153 int rv = cache_->DoomAllEntries(cb.callback());
[email protected]bc4fb8e2010-03-18 23:58:17154 return cb.GetResult(rv);
155}
156
157int DiskCacheTestWithCache::DoomEntriesBetween(const base::Time initial_time,
158 const base::Time end_time) {
[email protected]6ad7c0912011-12-15 19:10:19159 net::TestCompletionCallback cb;
160 int rv = cache_->DoomEntriesBetween(initial_time, end_time, cb.callback());
[email protected]bc4fb8e2010-03-18 23:58:17161 return cb.GetResult(rv);
162}
163
164int DiskCacheTestWithCache::DoomEntriesSince(const base::Time initial_time) {
[email protected]2a65aceb82011-12-19 20:59:27165 net::TestCompletionCallback cb;
166 int rv = cache_->DoomEntriesSince(initial_time, cb.callback());
[email protected]bc4fb8e2010-03-18 23:58:17167 return cb.GetResult(rv);
168}
169
msramekaee01ceb2015-10-07 14:23:33170int DiskCacheTestWithCache::CalculateSizeOfAllEntries() {
171 net::TestCompletionCallback cb;
172 int rv = cache_->CalculateSizeOfAllEntries(cb.callback());
173 return cb.GetResult(rv);
174}
175
gavinpb1aaa052014-09-24 14:54:35176scoped_ptr<DiskCacheTestWithCache::TestIterator>
177 DiskCacheTestWithCache::CreateIterator() {
178 return scoped_ptr<TestIterator>(new TestIterator(cache_->CreateIterator()));
[email protected]bc4fb8e2010-03-18 23:58:17179}
[email protected]fb2622f2010-07-13 18:00:56180
181void DiskCacheTestWithCache::FlushQueueForTest() {
182 if (memory_only_ || !cache_impl_)
183 return;
184
[email protected]2a65aceb82011-12-19 20:59:27185 net::TestCompletionCallback cb;
186 int rv = cache_impl_->FlushQueueForTest(cb.callback());
[email protected]fb2622f2010-07-13 18:00:56187 EXPECT_EQ(net::OK, cb.GetResult(rv));
188}
[email protected]e1fcf142010-08-23 18:47:25189
[email protected]f27bbe002011-12-22 11:29:34190void DiskCacheTestWithCache::RunTaskForTest(const base::Closure& closure) {
[email protected]65188eb2010-09-16 20:59:29191 if (memory_only_ || !cache_impl_) {
[email protected]f27bbe002011-12-22 11:29:34192 closure.Run();
[email protected]65188eb2010-09-16 20:59:29193 return;
194 }
195
[email protected]2a65aceb82011-12-19 20:59:27196 net::TestCompletionCallback cb;
[email protected]f27bbe002011-12-22 11:29:34197 int rv = cache_impl_->RunTaskForTest(closure, cb.callback());
[email protected]65188eb2010-09-16 20:59:29198 EXPECT_EQ(net::OK, cb.GetResult(rv));
199}
200
[email protected]e1fcf142010-08-23 18:47:25201int DiskCacheTestWithCache::ReadData(disk_cache::Entry* entry, int index,
202 int offset, net::IOBuffer* buf, int len) {
[email protected]2a65aceb82011-12-19 20:59:27203 net::TestCompletionCallback cb;
204 int rv = entry->ReadData(index, offset, buf, len, cb.callback());
[email protected]e1fcf142010-08-23 18:47:25205 return cb.GetResult(rv);
206}
207
[email protected]e1fcf142010-08-23 18:47:25208int DiskCacheTestWithCache::WriteData(disk_cache::Entry* entry, int index,
209 int offset, net::IOBuffer* buf, int len,
210 bool truncate) {
[email protected]2a65aceb82011-12-19 20:59:27211 net::TestCompletionCallback cb;
212 int rv = entry->WriteData(index, offset, buf, len, cb.callback(), truncate);
[email protected]e1fcf142010-08-23 18:47:25213 return cb.GetResult(rv);
214}
215
216int DiskCacheTestWithCache::ReadSparseData(disk_cache::Entry* entry,
217 int64 offset, net::IOBuffer* buf,
218 int len) {
[email protected]2a65aceb82011-12-19 20:59:27219 net::TestCompletionCallback cb;
220 int rv = entry->ReadSparseData(offset, buf, len, cb.callback());
[email protected]e1fcf142010-08-23 18:47:25221 return cb.GetResult(rv);
222}
223
224int DiskCacheTestWithCache::WriteSparseData(disk_cache::Entry* entry,
225 int64 offset,
226 net::IOBuffer* buf, int len) {
[email protected]2a65aceb82011-12-19 20:59:27227 net::TestCompletionCallback cb;
228 int rv = entry->WriteSparseData(offset, buf, len, cb.callback());
[email protected]e1fcf142010-08-23 18:47:25229 return cb.GetResult(rv);
230}
[email protected]7aefb152011-01-21 23:46:49231
[email protected]ceb61da32011-01-25 23:52:02232void DiskCacheTestWithCache::TrimForTest(bool empty) {
[email protected]f27bbe002011-12-22 11:29:34233 RunTaskForTest(base::Bind(&disk_cache::BackendImpl::TrimForTest,
234 base::Unretained(cache_impl_),
235 empty));
[email protected]ceb61da32011-01-25 23:52:02236}
237
238void DiskCacheTestWithCache::TrimDeletedListForTest(bool empty) {
[email protected]f27bbe002011-12-22 11:29:34239 RunTaskForTest(base::Bind(&disk_cache::BackendImpl::TrimDeletedListForTest,
240 base::Unretained(cache_impl_),
241 empty));
[email protected]ceb61da32011-01-25 23:52:02242}
243
[email protected]05f8087d2012-06-29 18:58:37244void DiskCacheTestWithCache::AddDelay() {
[email protected]8b2f8a52013-08-28 08:45:35245 if (simple_cache_mode_) {
246 // The simple cache uses second resolution for many timeouts, so it's safest
247 // to advance by at least whole seconds before falling back into the normal
248 // disk cache epsilon advance.
249 const base::Time initial_time = base::Time::Now();
250 do {
251 base::PlatformThread::YieldCurrentThread();
252 } while (base::Time::Now() -
253 initial_time < base::TimeDelta::FromSeconds(1));
254 }
255
[email protected]05f8087d2012-06-29 18:58:37256 base::Time initial = base::Time::Now();
257 while (base::Time::Now() <= initial) {
258 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(1));
259 };
260}
261
[email protected]7aefb152011-01-21 23:46:49262void DiskCacheTestWithCache::TearDown() {
[email protected]94a9d1a2013-03-24 09:50:31263 base::RunLoop().RunUntilIdle();
[email protected]71cfb0892013-08-30 18:29:14264 disk_cache::SimpleBackendImpl::FlushWorkerPoolForTesting();
265 base::RunLoop().RunUntilIdle();
[email protected]8c3f5a32013-08-01 11:57:53266 cache_.reset();
[email protected]7aefb152011-01-21 23:46:49267 if (cache_thread_.IsRunning())
268 cache_thread_.Stop();
269
[email protected]fccce902013-04-15 19:00:51270 if (!memory_only_ && !simple_cache_mode_ && integrity_) {
[email protected]79b3fec2011-11-15 22:33:47271 EXPECT_TRUE(CheckCacheIntegrity(cache_path_, new_eviction_, mask_));
[email protected]7aefb152011-01-21 23:46:49272 }
[email protected]de677dc2013-09-13 02:12:25273 base::RunLoop().RunUntilIdle();
274 disk_cache::SimpleBackendImpl::FlushWorkerPoolForTesting();
275 DiskCacheTest::TearDown();
[email protected]7aefb152011-01-21 23:46:49276}
277
278void DiskCacheTestWithCache::InitMemoryCache() {
[email protected]9eb8cdf2011-03-17 18:53:02279 mem_cache_ = new disk_cache::MemBackendImpl(NULL);
[email protected]8c3f5a32013-08-01 11:57:53280 cache_.reset(mem_cache_);
281 ASSERT_TRUE(cache_);
[email protected]7aefb152011-01-21 23:46:49282
283 if (size_)
284 EXPECT_TRUE(mem_cache_->SetMaxSize(size_));
285
286 ASSERT_TRUE(mem_cache_->Init());
287}
288
289void DiskCacheTestWithCache::InitDiskCache() {
[email protected]7aefb152011-01-21 23:46:49290 if (first_cleanup_)
[email protected]79b3fec2011-11-15 22:33:47291 ASSERT_TRUE(CleanupCacheDir());
[email protected]7aefb152011-01-21 23:46:49292
293 if (!cache_thread_.IsRunning()) {
[email protected]398ad132013-04-02 15:11:01294 ASSERT_TRUE(cache_thread_.StartWithOptions(
[email protected]2da659e2013-05-23 20:51:34295 base::Thread::Options(base::MessageLoop::TYPE_IO, 0)));
[email protected]7aefb152011-01-21 23:46:49296 }
297 ASSERT_TRUE(cache_thread_.message_loop() != NULL);
298
[email protected]398ad132013-04-02 15:11:01299 CreateBackend(disk_cache::kNoRandom, &cache_thread_);
300}
[email protected]7aefb152011-01-21 23:46:49301
[email protected]398ad132013-04-02 15:11:01302void DiskCacheTestWithCache::CreateBackend(uint32 flags, base::Thread* thread) {
[email protected]81e549a2014-08-21 04:19:55303 scoped_refptr<base::SingleThreadTaskRunner> runner;
[email protected]398ad132013-04-02 15:11:01304 if (use_current_thread_)
[email protected]81e549a2014-08-21 04:19:55305 runner = base::ThreadTaskRunnerHandle::Get();
[email protected]7aefb152011-01-21 23:46:49306 else
[email protected]81e549a2014-08-21 04:19:55307 runner = thread->task_runner();
[email protected]7aefb152011-01-21 23:46:49308
[email protected]398ad132013-04-02 15:11:01309 if (simple_cache_mode_) {
310 net::TestCompletionCallback cb;
[email protected]8c3f5a32013-08-01 11:57:53311 scoped_ptr<disk_cache::SimpleBackendImpl> simple_backend(
[email protected]cadac622013-06-11 16:46:36312 new disk_cache::SimpleBackendImpl(
[email protected]81e549a2014-08-21 04:19:55313 cache_path_, size_, type_, runner, NULL));
[email protected]00831c822013-04-10 15:37:12314 int rv = simple_backend->Init(cb.callback());
[email protected]398ad132013-04-02 15:11:01315 ASSERT_EQ(net::OK, cb.GetResult(rv));
[email protected]8c3f5a32013-08-01 11:57:53316 simple_cache_impl_ = simple_backend.get();
dchenge3d1ddc2014-10-15 19:30:51317 cache_ = simple_backend.Pass();
[email protected]7e48c1b2013-07-12 12:15:43318 if (simple_cache_wait_for_index_) {
319 net::TestCompletionCallback wait_for_index_cb;
[email protected]8c3f5a32013-08-01 11:57:53320 rv = simple_cache_impl_->index()->ExecuteWhenReady(
[email protected]7e48c1b2013-07-12 12:15:43321 wait_for_index_cb.callback());
322 ASSERT_EQ(net::OK, wait_for_index_cb.GetResult(rv));
323 }
[email protected]398ad132013-04-02 15:11:01324 return;
325 }
326
327 if (mask_)
328 cache_impl_ = new disk_cache::BackendImpl(cache_path_, mask_, runner, NULL);
329 else
330 cache_impl_ = new disk_cache::BackendImpl(cache_path_, runner, NULL);
[email protected]8c3f5a32013-08-01 11:57:53331 cache_.reset(cache_impl_);
332 ASSERT_TRUE(cache_);
[email protected]7aefb152011-01-21 23:46:49333 if (size_)
334 EXPECT_TRUE(cache_impl_->SetMaxSize(size_));
[email protected]7aefb152011-01-21 23:46:49335 if (new_eviction_)
336 cache_impl_->SetNewEviction();
[email protected]7aefb152011-01-21 23:46:49337 cache_impl_->SetType(type_);
[email protected]398ad132013-04-02 15:11:01338 cache_impl_->SetFlags(flags);
[email protected]42c459632011-12-17 02:20:23339 net::TestCompletionCallback cb;
340 int rv = cache_impl_->Init(cb.callback());
[email protected]7aefb152011-01-21 23:46:49341 ASSERT_EQ(net::OK, cb.GetResult(rv));
342}