blob: 219a213b73f1aee95529b2b97265228722f3ed1e [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
[email protected]79b3fec2011-11-15 22:33:477#include "base/file_util.h"
8#include "base/path_service.h"
[email protected]94a9d1a2013-03-24 09:50:319#include "base/run_loop.h"
[email protected]e1fcf142010-08-23 18:47:2510#include "net/base/io_buffer.h"
[email protected]fb2622f2010-07-13 18:00:5611#include "net/base/net_errors.h"
[email protected]bc4fb8e2010-03-18 23:58:1712#include "net/base/test_completion_callback.h"
initial.commit586acc5fe2008-07-26 22:42:5213#include "net/disk_cache/backend_impl.h"
[email protected]398ad132013-04-02 15:11:0114#include "net/disk_cache/cache_util.h"
15#include "net/disk_cache/disk_cache.h"
initial.commit586acc5fe2008-07-26 22:42:5216#include "net/disk_cache/disk_cache_test_util.h"
17#include "net/disk_cache/mem_backend_impl.h"
[email protected]398ad132013-04-02 15:11:0118#include "net/disk_cache/simple/simple_backend_impl.h"
[email protected]7e48c1b2013-07-12 12:15:4319#include "net/disk_cache/simple/simple_index.h"
initial.commit586acc5fe2008-07-26 22:42:5220
[email protected]79b3fec2011-11-15 22:33:4721DiskCacheTest::DiskCacheTest() {
[email protected]784f9302012-08-17 00:13:4322 CHECK(temp_dir_.CreateUniqueTempDir());
23 cache_path_ = temp_dir_.path();
[email protected]2da659e2013-05-23 20:51:3424 if (!base::MessageLoop::current())
25 message_loop_.reset(new base::MessageLoopForIO());
[email protected]79b3fec2011-11-15 22:33:4726}
27
28DiskCacheTest::~DiskCacheTest() {
29}
30
31bool DiskCacheTest::CopyTestCache(const std::string& name) {
[email protected]6cdfd7f2013-02-08 20:40:1532 base::FilePath path;
[email protected]79b3fec2011-11-15 22:33:4733 PathService::Get(base::DIR_SOURCE_ROOT, &path);
34 path = path.AppendASCII("net");
35 path = path.AppendASCII("data");
36 path = path.AppendASCII("cache_tests");
37 path = path.AppendASCII(name);
38
39 if (!CleanupCacheDir())
40 return false;
[email protected]f0ff2ad2013-07-09 17:42:2641 return base::CopyDirectory(path, cache_path_, false);
[email protected]79b3fec2011-11-15 22:33:4742}
43
44bool DiskCacheTest::CleanupCacheDir() {
45 return DeleteCache(cache_path_);
46}
47
[email protected]17b89142008-11-07 21:52:1548void DiskCacheTest::TearDown() {
[email protected]94a9d1a2013-03-24 09:50:3149 base::RunLoop().RunUntilIdle();
[email protected]17b89142008-11-07 21:52:1550}
51
[email protected]7aefb152011-01-21 23:46:4952DiskCacheTestWithCache::DiskCacheTestWithCache()
53 : cache_(NULL),
54 cache_impl_(NULL),
[email protected]1ed95752013-04-23 00:12:3655 simple_cache_impl_(NULL),
[email protected]7aefb152011-01-21 23:46:4956 mem_cache_(NULL),
57 mask_(0),
58 size_(0),
59 type_(net::DISK_CACHE),
60 memory_only_(false),
[email protected]398ad132013-04-02 15:11:0161 simple_cache_mode_(false),
[email protected]7e48c1b2013-07-12 12:15:4362 simple_cache_wait_for_index_(true),
[email protected]7aefb152011-01-21 23:46:4963 force_creation_(false),
64 new_eviction_(false),
65 first_cleanup_(true),
66 integrity_(true),
67 use_current_thread_(false),
68 cache_thread_("CacheThread") {
initial.commit586acc5fe2008-07-26 22:42:5269}
70
[email protected]7aefb152011-01-21 23:46:4971DiskCacheTestWithCache::~DiskCacheTestWithCache() {}
72
[email protected]4d5e0362008-08-28 00:59:0673void DiskCacheTestWithCache::InitCache() {
initial.commit586acc5fe2008-07-26 22:42:5274 if (memory_only_)
75 InitMemoryCache();
76 else
77 InitDiskCache();
78
79 ASSERT_TRUE(NULL != cache_);
[email protected]a2553842013-04-19 14:25:1880 if (first_cleanup_)
[email protected]a9da16d2008-07-30 21:41:5481 ASSERT_EQ(0, cache_->GetEntryCount());
initial.commit586acc5fe2008-07-26 22:42:5282}
83
initial.commit586acc5fe2008-07-26 22:42:5284// We are expected to leak memory when simulating crashes.
[email protected]4d5e0362008-08-28 00:59:0685void DiskCacheTestWithCache::SimulateCrash() {
[email protected]398ad132013-04-02 15:11:0186 ASSERT_TRUE(!memory_only_);
[email protected]2a65aceb82011-12-19 20:59:2787 net::TestCompletionCallback cb;
88 int rv = cache_impl_->FlushQueueForTest(cb.callback());
[email protected]fb2622f2010-07-13 18:00:5689 ASSERT_EQ(net::OK, cb.GetResult(rv));
initial.commit586acc5fe2008-07-26 22:42:5290 cache_impl_->ClearRefCountForTest();
91
92 delete cache_impl_;
[email protected]79b3fec2011-11-15 22:33:4793 EXPECT_TRUE(CheckCacheIntegrity(cache_path_, new_eviction_, mask_));
initial.commit586acc5fe2008-07-26 22:42:5294
[email protected]398ad132013-04-02 15:11:0195 CreateBackend(disk_cache::kNoRandom, &cache_thread_);
initial.commit586acc5fe2008-07-26 22:42:5296}
[email protected]a9da16d2008-07-30 21:41:5497
[email protected]4d5e0362008-08-28 00:59:0698void DiskCacheTestWithCache::SetTestMode() {
[email protected]398ad132013-04-02 15:11:0199 ASSERT_TRUE(!memory_only_);
[email protected]a9da16d2008-07-30 21:41:54100 cache_impl_->SetUnitTestMode();
101}
[email protected]bc4fb8e2010-03-18 23:58:17102
[email protected]7aefb152011-01-21 23:46:49103void DiskCacheTestWithCache::SetMaxSize(int size) {
104 size_ = size;
[email protected]1ed95752013-04-23 00:12:36105 if (simple_cache_impl_)
106 EXPECT_TRUE(simple_cache_impl_->SetMaxSize(size));
107
[email protected]7aefb152011-01-21 23:46:49108 if (cache_impl_)
109 EXPECT_TRUE(cache_impl_->SetMaxSize(size));
110
111 if (mem_cache_)
112 EXPECT_TRUE(mem_cache_->SetMaxSize(size));
113}
114
[email protected]bc4fb8e2010-03-18 23:58:17115int DiskCacheTestWithCache::OpenEntry(const std::string& key,
116 disk_cache::Entry** entry) {
[email protected]2a65aceb82011-12-19 20:59:27117 net::TestCompletionCallback cb;
118 int rv = cache_->OpenEntry(key, entry, cb.callback());
[email protected]bc4fb8e2010-03-18 23:58:17119 return cb.GetResult(rv);
120}
121
122int DiskCacheTestWithCache::CreateEntry(const std::string& key,
123 disk_cache::Entry** entry) {
[email protected]2a65aceb82011-12-19 20:59:27124 net::TestCompletionCallback cb;
125 int rv = cache_->CreateEntry(key, entry, cb.callback());
[email protected]bc4fb8e2010-03-18 23:58:17126 return cb.GetResult(rv);
127}
128
129int DiskCacheTestWithCache::DoomEntry(const std::string& key) {
[email protected]42c459632011-12-17 02:20:23130 net::TestCompletionCallback cb;
131 int rv = cache_->DoomEntry(key, cb.callback());
[email protected]bc4fb8e2010-03-18 23:58:17132 return cb.GetResult(rv);
133}
134
135int DiskCacheTestWithCache::DoomAllEntries() {
[email protected]6ad7c0912011-12-15 19:10:19136 net::TestCompletionCallback cb;
137 int rv = cache_->DoomAllEntries(cb.callback());
[email protected]bc4fb8e2010-03-18 23:58:17138 return cb.GetResult(rv);
139}
140
141int DiskCacheTestWithCache::DoomEntriesBetween(const base::Time initial_time,
142 const base::Time end_time) {
[email protected]6ad7c0912011-12-15 19:10:19143 net::TestCompletionCallback cb;
144 int rv = cache_->DoomEntriesBetween(initial_time, end_time, cb.callback());
[email protected]bc4fb8e2010-03-18 23:58:17145 return cb.GetResult(rv);
146}
147
148int DiskCacheTestWithCache::DoomEntriesSince(const base::Time initial_time) {
[email protected]2a65aceb82011-12-19 20:59:27149 net::TestCompletionCallback cb;
150 int rv = cache_->DoomEntriesSince(initial_time, cb.callback());
[email protected]bc4fb8e2010-03-18 23:58:17151 return cb.GetResult(rv);
152}
153
154int DiskCacheTestWithCache::OpenNextEntry(void** iter,
155 disk_cache::Entry** next_entry) {
[email protected]6ad7c0912011-12-15 19:10:19156 net::TestCompletionCallback cb;
157 int rv = cache_->OpenNextEntry(iter, next_entry, cb.callback());
[email protected]bc4fb8e2010-03-18 23:58:17158 return cb.GetResult(rv);
159}
[email protected]fb2622f2010-07-13 18:00:56160
161void DiskCacheTestWithCache::FlushQueueForTest() {
162 if (memory_only_ || !cache_impl_)
163 return;
164
[email protected]2a65aceb82011-12-19 20:59:27165 net::TestCompletionCallback cb;
166 int rv = cache_impl_->FlushQueueForTest(cb.callback());
[email protected]fb2622f2010-07-13 18:00:56167 EXPECT_EQ(net::OK, cb.GetResult(rv));
168}
[email protected]e1fcf142010-08-23 18:47:25169
[email protected]f27bbe002011-12-22 11:29:34170void DiskCacheTestWithCache::RunTaskForTest(const base::Closure& closure) {
[email protected]65188eb2010-09-16 20:59:29171 if (memory_only_ || !cache_impl_) {
[email protected]f27bbe002011-12-22 11:29:34172 closure.Run();
[email protected]65188eb2010-09-16 20:59:29173 return;
174 }
175
[email protected]2a65aceb82011-12-19 20:59:27176 net::TestCompletionCallback cb;
[email protected]f27bbe002011-12-22 11:29:34177 int rv = cache_impl_->RunTaskForTest(closure, cb.callback());
[email protected]65188eb2010-09-16 20:59:29178 EXPECT_EQ(net::OK, cb.GetResult(rv));
179}
180
[email protected]e1fcf142010-08-23 18:47:25181int DiskCacheTestWithCache::ReadData(disk_cache::Entry* entry, int index,
182 int offset, net::IOBuffer* buf, int len) {
[email protected]2a65aceb82011-12-19 20:59:27183 net::TestCompletionCallback cb;
184 int rv = entry->ReadData(index, offset, buf, len, cb.callback());
[email protected]e1fcf142010-08-23 18:47:25185 return cb.GetResult(rv);
186}
187
[email protected]e1fcf142010-08-23 18:47:25188int DiskCacheTestWithCache::WriteData(disk_cache::Entry* entry, int index,
189 int offset, net::IOBuffer* buf, int len,
190 bool truncate) {
[email protected]2a65aceb82011-12-19 20:59:27191 net::TestCompletionCallback cb;
192 int rv = entry->WriteData(index, offset, buf, len, cb.callback(), truncate);
[email protected]e1fcf142010-08-23 18:47:25193 return cb.GetResult(rv);
194}
195
196int DiskCacheTestWithCache::ReadSparseData(disk_cache::Entry* entry,
197 int64 offset, net::IOBuffer* buf,
198 int len) {
[email protected]2a65aceb82011-12-19 20:59:27199 net::TestCompletionCallback cb;
200 int rv = entry->ReadSparseData(offset, buf, len, cb.callback());
[email protected]e1fcf142010-08-23 18:47:25201 return cb.GetResult(rv);
202}
203
204int DiskCacheTestWithCache::WriteSparseData(disk_cache::Entry* entry,
205 int64 offset,
206 net::IOBuffer* buf, int len) {
[email protected]2a65aceb82011-12-19 20:59:27207 net::TestCompletionCallback cb;
208 int rv = entry->WriteSparseData(offset, buf, len, cb.callback());
[email protected]e1fcf142010-08-23 18:47:25209 return cb.GetResult(rv);
210}
[email protected]7aefb152011-01-21 23:46:49211
[email protected]ceb61da32011-01-25 23:52:02212void DiskCacheTestWithCache::TrimForTest(bool empty) {
[email protected]f27bbe002011-12-22 11:29:34213 RunTaskForTest(base::Bind(&disk_cache::BackendImpl::TrimForTest,
214 base::Unretained(cache_impl_),
215 empty));
[email protected]ceb61da32011-01-25 23:52:02216}
217
218void DiskCacheTestWithCache::TrimDeletedListForTest(bool empty) {
[email protected]f27bbe002011-12-22 11:29:34219 RunTaskForTest(base::Bind(&disk_cache::BackendImpl::TrimDeletedListForTest,
220 base::Unretained(cache_impl_),
221 empty));
[email protected]ceb61da32011-01-25 23:52:02222}
223
[email protected]05f8087d2012-06-29 18:58:37224void DiskCacheTestWithCache::AddDelay() {
225 base::Time initial = base::Time::Now();
226 while (base::Time::Now() <= initial) {
227 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(1));
228 };
229}
230
[email protected]7aefb152011-01-21 23:46:49231void DiskCacheTestWithCache::TearDown() {
[email protected]94a9d1a2013-03-24 09:50:31232 base::RunLoop().RunUntilIdle();
[email protected]7aefb152011-01-21 23:46:49233 delete cache_;
234 if (cache_thread_.IsRunning())
235 cache_thread_.Stop();
236
[email protected]fccce902013-04-15 19:00:51237 if (!memory_only_ && !simple_cache_mode_ && integrity_) {
[email protected]79b3fec2011-11-15 22:33:47238 EXPECT_TRUE(CheckCacheIntegrity(cache_path_, new_eviction_, mask_));
[email protected]7aefb152011-01-21 23:46:49239 }
240
241 PlatformTest::TearDown();
242}
243
244void DiskCacheTestWithCache::InitMemoryCache() {
[email protected]9eb8cdf2011-03-17 18:53:02245 mem_cache_ = new disk_cache::MemBackendImpl(NULL);
[email protected]7aefb152011-01-21 23:46:49246 cache_ = mem_cache_;
247 ASSERT_TRUE(NULL != cache_);
248
249 if (size_)
250 EXPECT_TRUE(mem_cache_->SetMaxSize(size_));
251
252 ASSERT_TRUE(mem_cache_->Init());
253}
254
255void DiskCacheTestWithCache::InitDiskCache() {
[email protected]7aefb152011-01-21 23:46:49256 if (first_cleanup_)
[email protected]79b3fec2011-11-15 22:33:47257 ASSERT_TRUE(CleanupCacheDir());
[email protected]7aefb152011-01-21 23:46:49258
259 if (!cache_thread_.IsRunning()) {
[email protected]398ad132013-04-02 15:11:01260 ASSERT_TRUE(cache_thread_.StartWithOptions(
[email protected]2da659e2013-05-23 20:51:34261 base::Thread::Options(base::MessageLoop::TYPE_IO, 0)));
[email protected]7aefb152011-01-21 23:46:49262 }
263 ASSERT_TRUE(cache_thread_.message_loop() != NULL);
264
[email protected]398ad132013-04-02 15:11:01265 CreateBackend(disk_cache::kNoRandom, &cache_thread_);
266}
[email protected]7aefb152011-01-21 23:46:49267
[email protected]398ad132013-04-02 15:11:01268void DiskCacheTestWithCache::CreateBackend(uint32 flags, base::Thread* thread) {
269 base::MessageLoopProxy* runner;
270 if (use_current_thread_)
[email protected]cadac622013-06-11 16:46:36271 runner = base::MessageLoopProxy::current().get();
[email protected]7aefb152011-01-21 23:46:49272 else
[email protected]cadac622013-06-11 16:46:36273 runner = thread->message_loop_proxy().get();
[email protected]7aefb152011-01-21 23:46:49274
[email protected]398ad132013-04-02 15:11:01275 if (simple_cache_mode_) {
276 net::TestCompletionCallback cb;
[email protected]00831c822013-04-10 15:37:12277 disk_cache::SimpleBackendImpl* simple_backend =
[email protected]cadac622013-06-11 16:46:36278 new disk_cache::SimpleBackendImpl(
279 cache_path_, size_, type_, make_scoped_refptr(runner).get(), NULL);
[email protected]00831c822013-04-10 15:37:12280 int rv = simple_backend->Init(cb.callback());
[email protected]398ad132013-04-02 15:11:01281 ASSERT_EQ(net::OK, cb.GetResult(rv));
[email protected]1ed95752013-04-23 00:12:36282 cache_ = simple_cache_impl_ = simple_backend;
[email protected]7e48c1b2013-07-12 12:15:43283 if (simple_cache_wait_for_index_) {
284 net::TestCompletionCallback wait_for_index_cb;
285 rv = simple_backend->index()->ExecuteWhenReady(
286 wait_for_index_cb.callback());
287 ASSERT_EQ(net::OK, wait_for_index_cb.GetResult(rv));
288 }
[email protected]398ad132013-04-02 15:11:01289 return;
290 }
291
292 if (mask_)
293 cache_impl_ = new disk_cache::BackendImpl(cache_path_, mask_, runner, NULL);
294 else
295 cache_impl_ = new disk_cache::BackendImpl(cache_path_, runner, NULL);
[email protected]7aefb152011-01-21 23:46:49296 cache_ = cache_impl_;
297 ASSERT_TRUE(NULL != cache_);
[email protected]7aefb152011-01-21 23:46:49298 if (size_)
299 EXPECT_TRUE(cache_impl_->SetMaxSize(size_));
[email protected]7aefb152011-01-21 23:46:49300 if (new_eviction_)
301 cache_impl_->SetNewEviction();
[email protected]7aefb152011-01-21 23:46:49302 cache_impl_->SetType(type_);
[email protected]398ad132013-04-02 15:11:01303 cache_impl_->SetFlags(flags);
[email protected]42c459632011-12-17 02:20:23304 net::TestCompletionCallback cb;
305 int rv = cache_impl_->Init(cb.callback());
[email protected]7aefb152011-01-21 23:46:49306 ASSERT_EQ(net::OK, cb.GetResult(rv));
[email protected]398ad132013-04-02 15:11:01307 cache_ = cache_impl_;
[email protected]7aefb152011-01-21 23:46:49308}