blob: 5ed2f90a6b0d53e5241f3264456caeceb19f17f3 [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]e1fcf142010-08-23 18:47:259#include "net/base/io_buffer.h"
[email protected]fb2622f2010-07-13 18:00:5610#include "net/base/net_errors.h"
[email protected]bc4fb8e2010-03-18 23:58:1711#include "net/base/test_completion_callback.h"
initial.commit586acc5fe2008-07-26 22:42:5212#include "net/disk_cache/backend_impl.h"
13#include "net/disk_cache/disk_cache_test_util.h"
14#include "net/disk_cache/mem_backend_impl.h"
15
[email protected]79b3fec2011-11-15 22:33:4716DiskCacheTest::DiskCacheTest() {
17 cache_path_ = GetCacheFilePath();
18 if (!MessageLoop::current())
19 message_loop_.reset(new MessageLoopForIO());
20}
21
22DiskCacheTest::~DiskCacheTest() {
23}
24
25bool DiskCacheTest::CopyTestCache(const std::string& name) {
26 FilePath path;
27 PathService::Get(base::DIR_SOURCE_ROOT, &path);
28 path = path.AppendASCII("net");
29 path = path.AppendASCII("data");
30 path = path.AppendASCII("cache_tests");
31 path = path.AppendASCII(name);
32
33 if (!CleanupCacheDir())
34 return false;
35 return file_util::CopyDirectory(path, cache_path_, false);
36}
37
38bool DiskCacheTest::CleanupCacheDir() {
39 return DeleteCache(cache_path_);
40}
41
[email protected]17b89142008-11-07 21:52:1542void DiskCacheTest::TearDown() {
43 MessageLoop::current()->RunAllPending();
44}
45
[email protected]7aefb152011-01-21 23:46:4946DiskCacheTestWithCache::DiskCacheTestWithCache()
47 : cache_(NULL),
48 cache_impl_(NULL),
49 mem_cache_(NULL),
50 mask_(0),
51 size_(0),
52 type_(net::DISK_CACHE),
53 memory_only_(false),
54 implementation_(false),
55 force_creation_(false),
56 new_eviction_(false),
57 first_cleanup_(true),
58 integrity_(true),
59 use_current_thread_(false),
60 cache_thread_("CacheThread") {
initial.commit586acc5fe2008-07-26 22:42:5261}
62
[email protected]7aefb152011-01-21 23:46:4963DiskCacheTestWithCache::~DiskCacheTestWithCache() {}
64
[email protected]4d5e0362008-08-28 00:59:0665void DiskCacheTestWithCache::InitCache() {
[email protected]220d71002009-04-17 00:50:0466 if (mask_ || new_eviction_)
initial.commit586acc5fe2008-07-26 22:42:5267 implementation_ = true;
68
69 if (memory_only_)
70 InitMemoryCache();
71 else
72 InitDiskCache();
73
74 ASSERT_TRUE(NULL != cache_);
[email protected]a9da16d2008-07-30 21:41:5475 if (first_cleanup_)
76 ASSERT_EQ(0, cache_->GetEntryCount());
initial.commit586acc5fe2008-07-26 22:42:5277}
78
initial.commit586acc5fe2008-07-26 22:42:5279// We are expected to leak memory when simulating crashes.
[email protected]4d5e0362008-08-28 00:59:0680void DiskCacheTestWithCache::SimulateCrash() {
initial.commit586acc5fe2008-07-26 22:42:5281 ASSERT_TRUE(implementation_ && !memory_only_);
[email protected]2a65aceb82011-12-19 20:59:2782 net::TestCompletionCallback cb;
83 int rv = cache_impl_->FlushQueueForTest(cb.callback());
[email protected]fb2622f2010-07-13 18:00:5684 ASSERT_EQ(net::OK, cb.GetResult(rv));
initial.commit586acc5fe2008-07-26 22:42:5285 cache_impl_->ClearRefCountForTest();
86
87 delete cache_impl_;
[email protected]79b3fec2011-11-15 22:33:4788 EXPECT_TRUE(CheckCacheIntegrity(cache_path_, new_eviction_, mask_));
initial.commit586acc5fe2008-07-26 22:42:5289
[email protected]79b3fec2011-11-15 22:33:4790 InitDiskCacheImpl();
initial.commit586acc5fe2008-07-26 22:42:5291}
[email protected]a9da16d2008-07-30 21:41:5492
[email protected]4d5e0362008-08-28 00:59:0693void DiskCacheTestWithCache::SetTestMode() {
[email protected]a9da16d2008-07-30 21:41:5494 ASSERT_TRUE(implementation_ && !memory_only_);
95 cache_impl_->SetUnitTestMode();
96}
[email protected]bc4fb8e2010-03-18 23:58:1797
[email protected]7aefb152011-01-21 23:46:4998void DiskCacheTestWithCache::SetMaxSize(int size) {
99 size_ = size;
100 if (cache_impl_)
101 EXPECT_TRUE(cache_impl_->SetMaxSize(size));
102
103 if (mem_cache_)
104 EXPECT_TRUE(mem_cache_->SetMaxSize(size));
105}
106
[email protected]bc4fb8e2010-03-18 23:58:17107int DiskCacheTestWithCache::OpenEntry(const std::string& key,
108 disk_cache::Entry** entry) {
[email protected]2a65aceb82011-12-19 20:59:27109 net::TestCompletionCallback cb;
110 int rv = cache_->OpenEntry(key, entry, cb.callback());
[email protected]bc4fb8e2010-03-18 23:58:17111 return cb.GetResult(rv);
112}
113
114int DiskCacheTestWithCache::CreateEntry(const std::string& key,
115 disk_cache::Entry** entry) {
[email protected]2a65aceb82011-12-19 20:59:27116 net::TestCompletionCallback cb;
117 int rv = cache_->CreateEntry(key, entry, cb.callback());
[email protected]bc4fb8e2010-03-18 23:58:17118 return cb.GetResult(rv);
119}
120
121int DiskCacheTestWithCache::DoomEntry(const std::string& key) {
[email protected]42c459632011-12-17 02:20:23122 net::TestCompletionCallback cb;
123 int rv = cache_->DoomEntry(key, cb.callback());
[email protected]bc4fb8e2010-03-18 23:58:17124 return cb.GetResult(rv);
125}
126
127int DiskCacheTestWithCache::DoomAllEntries() {
[email protected]6ad7c0912011-12-15 19:10:19128 net::TestCompletionCallback cb;
129 int rv = cache_->DoomAllEntries(cb.callback());
[email protected]bc4fb8e2010-03-18 23:58:17130 return cb.GetResult(rv);
131}
132
133int DiskCacheTestWithCache::DoomEntriesBetween(const base::Time initial_time,
134 const base::Time end_time) {
[email protected]6ad7c0912011-12-15 19:10:19135 net::TestCompletionCallback cb;
136 int rv = cache_->DoomEntriesBetween(initial_time, end_time, cb.callback());
[email protected]bc4fb8e2010-03-18 23:58:17137 return cb.GetResult(rv);
138}
139
140int DiskCacheTestWithCache::DoomEntriesSince(const base::Time initial_time) {
[email protected]2a65aceb82011-12-19 20:59:27141 net::TestCompletionCallback cb;
142 int rv = cache_->DoomEntriesSince(initial_time, cb.callback());
[email protected]bc4fb8e2010-03-18 23:58:17143 return cb.GetResult(rv);
144}
145
146int DiskCacheTestWithCache::OpenNextEntry(void** iter,
147 disk_cache::Entry** next_entry) {
[email protected]6ad7c0912011-12-15 19:10:19148 net::TestCompletionCallback cb;
149 int rv = cache_->OpenNextEntry(iter, next_entry, cb.callback());
[email protected]bc4fb8e2010-03-18 23:58:17150 return cb.GetResult(rv);
151}
[email protected]fb2622f2010-07-13 18:00:56152
153void DiskCacheTestWithCache::FlushQueueForTest() {
154 if (memory_only_ || !cache_impl_)
155 return;
156
[email protected]2a65aceb82011-12-19 20:59:27157 net::TestCompletionCallback cb;
158 int rv = cache_impl_->FlushQueueForTest(cb.callback());
[email protected]fb2622f2010-07-13 18:00:56159 EXPECT_EQ(net::OK, cb.GetResult(rv));
160}
[email protected]e1fcf142010-08-23 18:47:25161
[email protected]f27bbe002011-12-22 11:29:34162void DiskCacheTestWithCache::RunTaskForTest(const base::Closure& closure) {
[email protected]65188eb2010-09-16 20:59:29163 if (memory_only_ || !cache_impl_) {
[email protected]f27bbe002011-12-22 11:29:34164 closure.Run();
[email protected]65188eb2010-09-16 20:59:29165 return;
166 }
167
[email protected]2a65aceb82011-12-19 20:59:27168 net::TestCompletionCallback cb;
[email protected]f27bbe002011-12-22 11:29:34169 int rv = cache_impl_->RunTaskForTest(closure, cb.callback());
[email protected]65188eb2010-09-16 20:59:29170 EXPECT_EQ(net::OK, cb.GetResult(rv));
171}
172
[email protected]e1fcf142010-08-23 18:47:25173int DiskCacheTestWithCache::ReadData(disk_cache::Entry* entry, int index,
174 int offset, net::IOBuffer* buf, int len) {
[email protected]2a65aceb82011-12-19 20:59:27175 net::TestCompletionCallback cb;
176 int rv = entry->ReadData(index, offset, buf, len, cb.callback());
[email protected]e1fcf142010-08-23 18:47:25177 return cb.GetResult(rv);
178}
179
[email protected]e1fcf142010-08-23 18:47:25180int DiskCacheTestWithCache::WriteData(disk_cache::Entry* entry, int index,
181 int offset, net::IOBuffer* buf, int len,
182 bool truncate) {
[email protected]2a65aceb82011-12-19 20:59:27183 net::TestCompletionCallback cb;
184 int rv = entry->WriteData(index, offset, buf, len, cb.callback(), truncate);
[email protected]e1fcf142010-08-23 18:47:25185 return cb.GetResult(rv);
186}
187
188int DiskCacheTestWithCache::ReadSparseData(disk_cache::Entry* entry,
189 int64 offset, net::IOBuffer* buf,
190 int len) {
[email protected]2a65aceb82011-12-19 20:59:27191 net::TestCompletionCallback cb;
192 int rv = entry->ReadSparseData(offset, buf, len, cb.callback());
[email protected]e1fcf142010-08-23 18:47:25193 return cb.GetResult(rv);
194}
195
196int DiskCacheTestWithCache::WriteSparseData(disk_cache::Entry* entry,
197 int64 offset,
198 net::IOBuffer* buf, int len) {
[email protected]2a65aceb82011-12-19 20:59:27199 net::TestCompletionCallback cb;
200 int rv = entry->WriteSparseData(offset, buf, len, cb.callback());
[email protected]e1fcf142010-08-23 18:47:25201 return cb.GetResult(rv);
202}
[email protected]7aefb152011-01-21 23:46:49203
[email protected]ceb61da32011-01-25 23:52:02204void DiskCacheTestWithCache::TrimForTest(bool empty) {
[email protected]f27bbe002011-12-22 11:29:34205 RunTaskForTest(base::Bind(&disk_cache::BackendImpl::TrimForTest,
206 base::Unretained(cache_impl_),
207 empty));
[email protected]ceb61da32011-01-25 23:52:02208}
209
210void DiskCacheTestWithCache::TrimDeletedListForTest(bool empty) {
[email protected]f27bbe002011-12-22 11:29:34211 RunTaskForTest(base::Bind(&disk_cache::BackendImpl::TrimDeletedListForTest,
212 base::Unretained(cache_impl_),
213 empty));
[email protected]ceb61da32011-01-25 23:52:02214}
215
[email protected]05f8087d2012-06-29 18:58:37216void DiskCacheTestWithCache::AddDelay() {
217 base::Time initial = base::Time::Now();
218 while (base::Time::Now() <= initial) {
219 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(1));
220 };
221}
222
[email protected]7aefb152011-01-21 23:46:49223void DiskCacheTestWithCache::TearDown() {
224 MessageLoop::current()->RunAllPending();
225 delete cache_;
226 if (cache_thread_.IsRunning())
227 cache_thread_.Stop();
228
229 if (!memory_only_ && integrity_) {
[email protected]79b3fec2011-11-15 22:33:47230 EXPECT_TRUE(CheckCacheIntegrity(cache_path_, new_eviction_, mask_));
[email protected]7aefb152011-01-21 23:46:49231 }
232
233 PlatformTest::TearDown();
234}
235
236void DiskCacheTestWithCache::InitMemoryCache() {
237 if (!implementation_) {
[email protected]9eb8cdf2011-03-17 18:53:02238 cache_ = disk_cache::MemBackendImpl::CreateBackend(size_, NULL);
[email protected]7aefb152011-01-21 23:46:49239 return;
240 }
241
[email protected]9eb8cdf2011-03-17 18:53:02242 mem_cache_ = new disk_cache::MemBackendImpl(NULL);
[email protected]7aefb152011-01-21 23:46:49243 cache_ = mem_cache_;
244 ASSERT_TRUE(NULL != cache_);
245
246 if (size_)
247 EXPECT_TRUE(mem_cache_->SetMaxSize(size_));
248
249 ASSERT_TRUE(mem_cache_->Init());
250}
251
252void DiskCacheTestWithCache::InitDiskCache() {
[email protected]7aefb152011-01-21 23:46:49253 if (first_cleanup_)
[email protected]79b3fec2011-11-15 22:33:47254 ASSERT_TRUE(CleanupCacheDir());
[email protected]7aefb152011-01-21 23:46:49255
256 if (!cache_thread_.IsRunning()) {
257 EXPECT_TRUE(cache_thread_.StartWithOptions(
258 base::Thread::Options(MessageLoop::TYPE_IO, 0)));
259 }
260 ASSERT_TRUE(cache_thread_.message_loop() != NULL);
261
262 if (implementation_)
[email protected]79b3fec2011-11-15 22:33:47263 return InitDiskCacheImpl();
[email protected]7aefb152011-01-21 23:46:49264
265 scoped_refptr<base::MessageLoopProxy> thread =
[email protected]edd685f2011-08-15 20:33:46266 use_current_thread_ ? base::MessageLoopProxy::current() :
[email protected]7aefb152011-01-21 23:46:49267 cache_thread_.message_loop_proxy();
268
[email protected]2a65aceb82011-12-19 20:59:27269 net::TestCompletionCallback cb;
[email protected]7aefb152011-01-21 23:46:49270 int rv = disk_cache::BackendImpl::CreateBackend(
[email protected]79b3fec2011-11-15 22:33:47271 cache_path_, force_creation_, size_, type_,
[email protected]2a65aceb82011-12-19 20:59:27272 disk_cache::kNoRandom, thread, NULL, &cache_, cb.callback());
[email protected]7aefb152011-01-21 23:46:49273 ASSERT_EQ(net::OK, cb.GetResult(rv));
274}
275
[email protected]79b3fec2011-11-15 22:33:47276void DiskCacheTestWithCache::InitDiskCacheImpl() {
[email protected]7aefb152011-01-21 23:46:49277 scoped_refptr<base::MessageLoopProxy> thread =
[email protected]edd685f2011-08-15 20:33:46278 use_current_thread_ ? base::MessageLoopProxy::current() :
[email protected]7aefb152011-01-21 23:46:49279 cache_thread_.message_loop_proxy();
280 if (mask_)
[email protected]79b3fec2011-11-15 22:33:47281 cache_impl_ = new disk_cache::BackendImpl(cache_path_, mask_, thread, NULL);
[email protected]7aefb152011-01-21 23:46:49282 else
[email protected]79b3fec2011-11-15 22:33:47283 cache_impl_ = new disk_cache::BackendImpl(cache_path_, thread, NULL);
[email protected]7aefb152011-01-21 23:46:49284
285 cache_ = cache_impl_;
286 ASSERT_TRUE(NULL != cache_);
287
288 if (size_)
289 EXPECT_TRUE(cache_impl_->SetMaxSize(size_));
290
291 if (new_eviction_)
292 cache_impl_->SetNewEviction();
293
294 cache_impl_->SetType(type_);
295 cache_impl_->SetFlags(disk_cache::kNoRandom);
[email protected]42c459632011-12-17 02:20:23296 net::TestCompletionCallback cb;
297 int rv = cache_impl_->Init(cb.callback());
[email protected]7aefb152011-01-21 23:46:49298 ASSERT_EQ(net::OK, cb.GetResult(rv));
299}