blob: 608b729c0cd259f142460db9bcfe7196fb908a63 [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() {
[email protected]784f9302012-08-17 00:13:4317 CHECK(temp_dir_.CreateUniqueTempDir());
18 cache_path_ = temp_dir_.path();
[email protected]79b3fec2011-11-15 22:33:4719 if (!MessageLoop::current())
20 message_loop_.reset(new MessageLoopForIO());
21}
22
23DiskCacheTest::~DiskCacheTest() {
24}
25
26bool DiskCacheTest::CopyTestCache(const std::string& name) {
27 FilePath path;
28 PathService::Get(base::DIR_SOURCE_ROOT, &path);
29 path = path.AppendASCII("net");
30 path = path.AppendASCII("data");
31 path = path.AppendASCII("cache_tests");
32 path = path.AppendASCII(name);
33
34 if (!CleanupCacheDir())
35 return false;
36 return file_util::CopyDirectory(path, cache_path_, false);
37}
38
39bool DiskCacheTest::CleanupCacheDir() {
40 return DeleteCache(cache_path_);
41}
42
[email protected]17b89142008-11-07 21:52:1543void DiskCacheTest::TearDown() {
44 MessageLoop::current()->RunAllPending();
45}
46
[email protected]7aefb152011-01-21 23:46:4947DiskCacheTestWithCache::DiskCacheTestWithCache()
48 : cache_(NULL),
49 cache_impl_(NULL),
50 mem_cache_(NULL),
51 mask_(0),
52 size_(0),
53 type_(net::DISK_CACHE),
54 memory_only_(false),
55 implementation_(false),
56 force_creation_(false),
57 new_eviction_(false),
58 first_cleanup_(true),
59 integrity_(true),
60 use_current_thread_(false),
61 cache_thread_("CacheThread") {
initial.commit586acc5fe2008-07-26 22:42:5262}
63
[email protected]7aefb152011-01-21 23:46:4964DiskCacheTestWithCache::~DiskCacheTestWithCache() {}
65
[email protected]4d5e0362008-08-28 00:59:0666void DiskCacheTestWithCache::InitCache() {
[email protected]220d71002009-04-17 00:50:0467 if (mask_ || new_eviction_)
initial.commit586acc5fe2008-07-26 22:42:5268 implementation_ = true;
69
70 if (memory_only_)
71 InitMemoryCache();
72 else
73 InitDiskCache();
74
75 ASSERT_TRUE(NULL != cache_);
[email protected]a9da16d2008-07-30 21:41:5476 if (first_cleanup_)
77 ASSERT_EQ(0, cache_->GetEntryCount());
initial.commit586acc5fe2008-07-26 22:42:5278}
79
initial.commit586acc5fe2008-07-26 22:42:5280// We are expected to leak memory when simulating crashes.
[email protected]4d5e0362008-08-28 00:59:0681void DiskCacheTestWithCache::SimulateCrash() {
initial.commit586acc5fe2008-07-26 22:42:5282 ASSERT_TRUE(implementation_ && !memory_only_);
[email protected]2a65aceb82011-12-19 20:59:2783 net::TestCompletionCallback cb;
84 int rv = cache_impl_->FlushQueueForTest(cb.callback());
[email protected]fb2622f2010-07-13 18:00:5685 ASSERT_EQ(net::OK, cb.GetResult(rv));
initial.commit586acc5fe2008-07-26 22:42:5286 cache_impl_->ClearRefCountForTest();
87
88 delete cache_impl_;
[email protected]79b3fec2011-11-15 22:33:4789 EXPECT_TRUE(CheckCacheIntegrity(cache_path_, new_eviction_, mask_));
initial.commit586acc5fe2008-07-26 22:42:5290
[email protected]79b3fec2011-11-15 22:33:4791 InitDiskCacheImpl();
initial.commit586acc5fe2008-07-26 22:42:5292}
[email protected]a9da16d2008-07-30 21:41:5493
[email protected]4d5e0362008-08-28 00:59:0694void DiskCacheTestWithCache::SetTestMode() {
[email protected]a9da16d2008-07-30 21:41:5495 ASSERT_TRUE(implementation_ && !memory_only_);
96 cache_impl_->SetUnitTestMode();
97}
[email protected]bc4fb8e2010-03-18 23:58:1798
[email protected]7aefb152011-01-21 23:46:4999void DiskCacheTestWithCache::SetMaxSize(int size) {
100 size_ = size;
101 if (cache_impl_)
102 EXPECT_TRUE(cache_impl_->SetMaxSize(size));
103
104 if (mem_cache_)
105 EXPECT_TRUE(mem_cache_->SetMaxSize(size));
106}
107
[email protected]bc4fb8e2010-03-18 23:58:17108int DiskCacheTestWithCache::OpenEntry(const std::string& key,
109 disk_cache::Entry** entry) {
[email protected]2a65aceb82011-12-19 20:59:27110 net::TestCompletionCallback cb;
111 int rv = cache_->OpenEntry(key, entry, cb.callback());
[email protected]bc4fb8e2010-03-18 23:58:17112 return cb.GetResult(rv);
113}
114
115int DiskCacheTestWithCache::CreateEntry(const std::string& key,
116 disk_cache::Entry** entry) {
[email protected]2a65aceb82011-12-19 20:59:27117 net::TestCompletionCallback cb;
118 int rv = cache_->CreateEntry(key, entry, cb.callback());
[email protected]bc4fb8e2010-03-18 23:58:17119 return cb.GetResult(rv);
120}
121
122int DiskCacheTestWithCache::DoomEntry(const std::string& key) {
[email protected]42c459632011-12-17 02:20:23123 net::TestCompletionCallback cb;
124 int rv = cache_->DoomEntry(key, cb.callback());
[email protected]bc4fb8e2010-03-18 23:58:17125 return cb.GetResult(rv);
126}
127
128int DiskCacheTestWithCache::DoomAllEntries() {
[email protected]6ad7c0912011-12-15 19:10:19129 net::TestCompletionCallback cb;
130 int rv = cache_->DoomAllEntries(cb.callback());
[email protected]bc4fb8e2010-03-18 23:58:17131 return cb.GetResult(rv);
132}
133
134int DiskCacheTestWithCache::DoomEntriesBetween(const base::Time initial_time,
135 const base::Time end_time) {
[email protected]6ad7c0912011-12-15 19:10:19136 net::TestCompletionCallback cb;
137 int rv = cache_->DoomEntriesBetween(initial_time, end_time, cb.callback());
[email protected]bc4fb8e2010-03-18 23:58:17138 return cb.GetResult(rv);
139}
140
141int DiskCacheTestWithCache::DoomEntriesSince(const base::Time initial_time) {
[email protected]2a65aceb82011-12-19 20:59:27142 net::TestCompletionCallback cb;
143 int rv = cache_->DoomEntriesSince(initial_time, cb.callback());
[email protected]bc4fb8e2010-03-18 23:58:17144 return cb.GetResult(rv);
145}
146
147int DiskCacheTestWithCache::OpenNextEntry(void** iter,
148 disk_cache::Entry** next_entry) {
[email protected]6ad7c0912011-12-15 19:10:19149 net::TestCompletionCallback cb;
150 int rv = cache_->OpenNextEntry(iter, next_entry, cb.callback());
[email protected]bc4fb8e2010-03-18 23:58:17151 return cb.GetResult(rv);
152}
[email protected]fb2622f2010-07-13 18:00:56153
154void DiskCacheTestWithCache::FlushQueueForTest() {
155 if (memory_only_ || !cache_impl_)
156 return;
157
[email protected]2a65aceb82011-12-19 20:59:27158 net::TestCompletionCallback cb;
159 int rv = cache_impl_->FlushQueueForTest(cb.callback());
[email protected]fb2622f2010-07-13 18:00:56160 EXPECT_EQ(net::OK, cb.GetResult(rv));
161}
[email protected]e1fcf142010-08-23 18:47:25162
[email protected]f27bbe002011-12-22 11:29:34163void DiskCacheTestWithCache::RunTaskForTest(const base::Closure& closure) {
[email protected]65188eb2010-09-16 20:59:29164 if (memory_only_ || !cache_impl_) {
[email protected]f27bbe002011-12-22 11:29:34165 closure.Run();
[email protected]65188eb2010-09-16 20:59:29166 return;
167 }
168
[email protected]2a65aceb82011-12-19 20:59:27169 net::TestCompletionCallback cb;
[email protected]f27bbe002011-12-22 11:29:34170 int rv = cache_impl_->RunTaskForTest(closure, cb.callback());
[email protected]65188eb2010-09-16 20:59:29171 EXPECT_EQ(net::OK, cb.GetResult(rv));
172}
173
[email protected]e1fcf142010-08-23 18:47:25174int DiskCacheTestWithCache::ReadData(disk_cache::Entry* entry, int index,
175 int offset, net::IOBuffer* buf, int len) {
[email protected]2a65aceb82011-12-19 20:59:27176 net::TestCompletionCallback cb;
177 int rv = entry->ReadData(index, offset, buf, len, cb.callback());
[email protected]e1fcf142010-08-23 18:47:25178 return cb.GetResult(rv);
179}
180
[email protected]e1fcf142010-08-23 18:47:25181int DiskCacheTestWithCache::WriteData(disk_cache::Entry* entry, int index,
182 int offset, net::IOBuffer* buf, int len,
183 bool truncate) {
[email protected]2a65aceb82011-12-19 20:59:27184 net::TestCompletionCallback cb;
185 int rv = entry->WriteData(index, offset, buf, len, cb.callback(), truncate);
[email protected]e1fcf142010-08-23 18:47:25186 return cb.GetResult(rv);
187}
188
189int DiskCacheTestWithCache::ReadSparseData(disk_cache::Entry* entry,
190 int64 offset, net::IOBuffer* buf,
191 int len) {
[email protected]2a65aceb82011-12-19 20:59:27192 net::TestCompletionCallback cb;
193 int rv = entry->ReadSparseData(offset, buf, len, cb.callback());
[email protected]e1fcf142010-08-23 18:47:25194 return cb.GetResult(rv);
195}
196
197int DiskCacheTestWithCache::WriteSparseData(disk_cache::Entry* entry,
198 int64 offset,
199 net::IOBuffer* buf, int len) {
[email protected]2a65aceb82011-12-19 20:59:27200 net::TestCompletionCallback cb;
201 int rv = entry->WriteSparseData(offset, buf, len, cb.callback());
[email protected]e1fcf142010-08-23 18:47:25202 return cb.GetResult(rv);
203}
[email protected]7aefb152011-01-21 23:46:49204
[email protected]ceb61da32011-01-25 23:52:02205void DiskCacheTestWithCache::TrimForTest(bool empty) {
[email protected]f27bbe002011-12-22 11:29:34206 RunTaskForTest(base::Bind(&disk_cache::BackendImpl::TrimForTest,
207 base::Unretained(cache_impl_),
208 empty));
[email protected]ceb61da32011-01-25 23:52:02209}
210
211void DiskCacheTestWithCache::TrimDeletedListForTest(bool empty) {
[email protected]f27bbe002011-12-22 11:29:34212 RunTaskForTest(base::Bind(&disk_cache::BackendImpl::TrimDeletedListForTest,
213 base::Unretained(cache_impl_),
214 empty));
[email protected]ceb61da32011-01-25 23:52:02215}
216
[email protected]05f8087d2012-06-29 18:58:37217void DiskCacheTestWithCache::AddDelay() {
218 base::Time initial = base::Time::Now();
219 while (base::Time::Now() <= initial) {
220 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(1));
221 };
222}
223
[email protected]7aefb152011-01-21 23:46:49224void DiskCacheTestWithCache::TearDown() {
225 MessageLoop::current()->RunAllPending();
226 delete cache_;
227 if (cache_thread_.IsRunning())
228 cache_thread_.Stop();
229
230 if (!memory_only_ && integrity_) {
[email protected]79b3fec2011-11-15 22:33:47231 EXPECT_TRUE(CheckCacheIntegrity(cache_path_, new_eviction_, mask_));
[email protected]7aefb152011-01-21 23:46:49232 }
233
234 PlatformTest::TearDown();
235}
236
237void DiskCacheTestWithCache::InitMemoryCache() {
238 if (!implementation_) {
[email protected]9eb8cdf2011-03-17 18:53:02239 cache_ = disk_cache::MemBackendImpl::CreateBackend(size_, NULL);
[email protected]7aefb152011-01-21 23:46:49240 return;
241 }
242
[email protected]9eb8cdf2011-03-17 18:53:02243 mem_cache_ = new disk_cache::MemBackendImpl(NULL);
[email protected]7aefb152011-01-21 23:46:49244 cache_ = mem_cache_;
245 ASSERT_TRUE(NULL != cache_);
246
247 if (size_)
248 EXPECT_TRUE(mem_cache_->SetMaxSize(size_));
249
250 ASSERT_TRUE(mem_cache_->Init());
251}
252
253void DiskCacheTestWithCache::InitDiskCache() {
[email protected]7aefb152011-01-21 23:46:49254 if (first_cleanup_)
[email protected]79b3fec2011-11-15 22:33:47255 ASSERT_TRUE(CleanupCacheDir());
[email protected]7aefb152011-01-21 23:46:49256
257 if (!cache_thread_.IsRunning()) {
258 EXPECT_TRUE(cache_thread_.StartWithOptions(
259 base::Thread::Options(MessageLoop::TYPE_IO, 0)));
260 }
261 ASSERT_TRUE(cache_thread_.message_loop() != NULL);
262
263 if (implementation_)
[email protected]79b3fec2011-11-15 22:33:47264 return InitDiskCacheImpl();
[email protected]7aefb152011-01-21 23:46:49265
266 scoped_refptr<base::MessageLoopProxy> thread =
[email protected]edd685f2011-08-15 20:33:46267 use_current_thread_ ? base::MessageLoopProxy::current() :
[email protected]7aefb152011-01-21 23:46:49268 cache_thread_.message_loop_proxy();
269
[email protected]2a65aceb82011-12-19 20:59:27270 net::TestCompletionCallback cb;
[email protected]7aefb152011-01-21 23:46:49271 int rv = disk_cache::BackendImpl::CreateBackend(
[email protected]79b3fec2011-11-15 22:33:47272 cache_path_, force_creation_, size_, type_,
[email protected]2a65aceb82011-12-19 20:59:27273 disk_cache::kNoRandom, thread, NULL, &cache_, cb.callback());
[email protected]7aefb152011-01-21 23:46:49274 ASSERT_EQ(net::OK, cb.GetResult(rv));
275}
276
[email protected]79b3fec2011-11-15 22:33:47277void DiskCacheTestWithCache::InitDiskCacheImpl() {
[email protected]7aefb152011-01-21 23:46:49278 scoped_refptr<base::MessageLoopProxy> thread =
[email protected]edd685f2011-08-15 20:33:46279 use_current_thread_ ? base::MessageLoopProxy::current() :
[email protected]7aefb152011-01-21 23:46:49280 cache_thread_.message_loop_proxy();
281 if (mask_)
[email protected]79b3fec2011-11-15 22:33:47282 cache_impl_ = new disk_cache::BackendImpl(cache_path_, mask_, thread, NULL);
[email protected]7aefb152011-01-21 23:46:49283 else
[email protected]79b3fec2011-11-15 22:33:47284 cache_impl_ = new disk_cache::BackendImpl(cache_path_, thread, NULL);
[email protected]7aefb152011-01-21 23:46:49285
286 cache_ = cache_impl_;
287 ASSERT_TRUE(NULL != cache_);
288
289 if (size_)
290 EXPECT_TRUE(cache_impl_->SetMaxSize(size_));
291
292 if (new_eviction_)
293 cache_impl_->SetNewEviction();
294
295 cache_impl_->SetType(type_);
296 cache_impl_->SetFlags(disk_cache::kNoRandom);
[email protected]42c459632011-12-17 02:20:23297 net::TestCompletionCallback cb;
298 int rv = cache_impl_->Init(cb.callback());
[email protected]7aefb152011-01-21 23:46:49299 ASSERT_EQ(net::OK, cb.GetResult(rv));
300}