1 // Copyright (c) 2006-2010 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "net/disk_cache/disk_cache_test_base.h"
7 #include "net/base/io_buffer.h"
8 #include "net/base/net_errors.h"
9 #include "net/base/test_completion_callback.h"
10 #include "net/disk_cache/backend_impl.h"
11 #include "net/disk_cache/disk_cache_test_util.h"
12 #include "net/disk_cache/mem_backend_impl.h"
14 void DiskCacheTest::TearDown() {
15 MessageLoop::current()->RunAllPending();
18 void DiskCacheTestWithCache::SetMaxSize(int size
) {
21 EXPECT_TRUE(cache_impl_
->SetMaxSize(size
));
24 EXPECT_TRUE(mem_cache_
->SetMaxSize(size
));
27 void DiskCacheTestWithCache::InitCache() {
28 if (mask_
|| new_eviction_
)
29 implementation_
= true;
36 ASSERT_TRUE(NULL
!= cache_
);
38 ASSERT_EQ(0, cache_
->GetEntryCount());
41 void DiskCacheTestWithCache::InitMemoryCache() {
42 if (!implementation_
) {
43 cache_
= disk_cache::MemBackendImpl::CreateBackend(size_
);
47 mem_cache_
= new disk_cache::MemBackendImpl();
49 ASSERT_TRUE(NULL
!= cache_
);
52 EXPECT_TRUE(mem_cache_
->SetMaxSize(size_
));
54 ASSERT_TRUE(mem_cache_
->Init());
57 void DiskCacheTestWithCache::InitDiskCache() {
58 FilePath path
= GetCacheFilePath();
60 ASSERT_TRUE(DeleteCache(path
));
62 if (!cache_thread_
.IsRunning()) {
63 EXPECT_TRUE(cache_thread_
.StartWithOptions(
64 base::Thread::Options(MessageLoop::TYPE_IO
, 0)));
66 ASSERT_TRUE(cache_thread_
.message_loop() != NULL
);
69 return InitDiskCacheImpl(path
);
71 scoped_refptr
<base::MessageLoopProxy
> thread
=
72 use_current_thread_
? base::MessageLoopProxy::CreateForCurrentThread() :
73 cache_thread_
.message_loop_proxy();
75 TestCompletionCallback cb
;
76 int rv
= disk_cache::BackendImpl::CreateBackend(
77 path
, force_creation_
, size_
, type_
,
78 disk_cache::kNoRandom
, thread
, &cache_
, &cb
);
79 ASSERT_EQ(net::OK
, cb
.GetResult(rv
));
82 void DiskCacheTestWithCache::InitDiskCacheImpl(const FilePath
& path
) {
83 scoped_refptr
<base::MessageLoopProxy
> thread
=
84 use_current_thread_
? base::MessageLoopProxy::CreateForCurrentThread() :
85 cache_thread_
.message_loop_proxy();
87 cache_impl_
= new disk_cache::BackendImpl(path
, mask_
, thread
);
89 cache_impl_
= new disk_cache::BackendImpl(path
, thread
);
92 ASSERT_TRUE(NULL
!= cache_
);
95 EXPECT_TRUE(cache_impl_
->SetMaxSize(size_
));
98 cache_impl_
->SetNewEviction();
100 cache_impl_
->SetType(type_
);
101 cache_impl_
->SetFlags(disk_cache::kNoRandom
);
102 TestCompletionCallback cb
;
103 int rv
= cache_impl_
->Init(&cb
);
104 ASSERT_EQ(net::OK
, cb
.GetResult(rv
));
107 void DiskCacheTestWithCache::TearDown() {
108 MessageLoop::current()->RunAllPending();
110 if (cache_thread_
.IsRunning())
111 cache_thread_
.Stop();
113 if (!memory_only_
&& integrity_
) {
114 FilePath path
= GetCacheFilePath();
115 EXPECT_TRUE(CheckCacheIntegrity(path
, new_eviction_
));
118 PlatformTest::TearDown();
121 // We are expected to leak memory when simulating crashes.
122 void DiskCacheTestWithCache::SimulateCrash() {
123 ASSERT_TRUE(implementation_
&& !memory_only_
);
124 TestCompletionCallback cb
;
125 int rv
= cache_impl_
->FlushQueueForTest(&cb
);
126 ASSERT_EQ(net::OK
, cb
.GetResult(rv
));
127 cache_impl_
->ClearRefCountForTest();
130 FilePath path
= GetCacheFilePath();
131 EXPECT_TRUE(CheckCacheIntegrity(path
, new_eviction_
));
133 InitDiskCacheImpl(path
);
136 void DiskCacheTestWithCache::SetTestMode() {
137 ASSERT_TRUE(implementation_
&& !memory_only_
);
138 cache_impl_
->SetUnitTestMode();
141 int DiskCacheTestWithCache::OpenEntry(const std::string
& key
,
142 disk_cache::Entry
** entry
) {
143 TestCompletionCallback cb
;
144 int rv
= cache_
->OpenEntry(key
, entry
, &cb
);
145 return cb
.GetResult(rv
);
148 int DiskCacheTestWithCache::CreateEntry(const std::string
& key
,
149 disk_cache::Entry
** entry
) {
150 TestCompletionCallback cb
;
151 int rv
= cache_
->CreateEntry(key
, entry
, &cb
);
152 return cb
.GetResult(rv
);
155 int DiskCacheTestWithCache::DoomEntry(const std::string
& key
) {
156 TestCompletionCallback cb
;
157 int rv
= cache_
->DoomEntry(key
, &cb
);
158 return cb
.GetResult(rv
);
161 int DiskCacheTestWithCache::DoomAllEntries() {
162 TestCompletionCallback cb
;
163 int rv
= cache_
->DoomAllEntries(&cb
);
164 return cb
.GetResult(rv
);
167 int DiskCacheTestWithCache::DoomEntriesBetween(const base::Time initial_time
,
168 const base::Time end_time
) {
169 TestCompletionCallback cb
;
170 int rv
= cache_
->DoomEntriesBetween(initial_time
, end_time
, &cb
);
171 return cb
.GetResult(rv
);
174 int DiskCacheTestWithCache::DoomEntriesSince(const base::Time initial_time
) {
175 TestCompletionCallback cb
;
176 int rv
= cache_
->DoomEntriesSince(initial_time
, &cb
);
177 return cb
.GetResult(rv
);
180 int DiskCacheTestWithCache::OpenNextEntry(void** iter
,
181 disk_cache::Entry
** next_entry
) {
182 TestCompletionCallback cb
;
183 int rv
= cache_
->OpenNextEntry(iter
, next_entry
, &cb
);
184 return cb
.GetResult(rv
);
187 void DiskCacheTestWithCache::FlushQueueForTest() {
188 if (memory_only_
|| !cache_impl_
)
191 TestCompletionCallback cb
;
192 int rv
= cache_impl_
->FlushQueueForTest(&cb
);
193 EXPECT_EQ(net::OK
, cb
.GetResult(rv
));
196 void DiskCacheTestWithCache::RunTaskForTest(Task
* task
) {
197 if (memory_only_
|| !cache_impl_
) {
203 TestCompletionCallback cb
;
204 int rv
= cache_impl_
->RunTaskForTest(task
, &cb
);
205 EXPECT_EQ(net::OK
, cb
.GetResult(rv
));
208 int DiskCacheTestWithCache::ReadData(disk_cache::Entry
* entry
, int index
,
209 int offset
, net::IOBuffer
* buf
, int len
) {
210 TestCompletionCallback cb
;
211 int rv
= entry
->ReadData(index
, offset
, buf
, len
, &cb
);
212 return cb
.GetResult(rv
);
216 int DiskCacheTestWithCache::WriteData(disk_cache::Entry
* entry
, int index
,
217 int offset
, net::IOBuffer
* buf
, int len
,
219 TestCompletionCallback cb
;
220 int rv
= entry
->WriteData(index
, offset
, buf
, len
, &cb
, truncate
);
221 return cb
.GetResult(rv
);
224 int DiskCacheTestWithCache::ReadSparseData(disk_cache::Entry
* entry
,
225 int64 offset
, net::IOBuffer
* buf
,
227 TestCompletionCallback cb
;
228 int rv
= entry
->ReadSparseData(offset
, buf
, len
, &cb
);
229 return cb
.GetResult(rv
);
232 int DiskCacheTestWithCache::WriteSparseData(disk_cache::Entry
* entry
,
234 net::IOBuffer
* buf
, int len
) {
235 TestCompletionCallback cb
;
236 int rv
= entry
->WriteSparseData(offset
, buf
, len
, &cb
);
237 return cb
.GetResult(rv
);