[email protected] | 05f8087d | 2012-06-29 18:58:37 | [diff] [blame^] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 4 | |
| 5 | #include "net/disk_cache/disk_cache_test_base.h" |
| 6 | |
[email protected] | 79b3fec | 2011-11-15 22:33:47 | [diff] [blame] | 7 | #include "base/file_util.h" |
| 8 | #include "base/path_service.h" |
[email protected] | e1fcf14 | 2010-08-23 18:47:25 | [diff] [blame] | 9 | #include "net/base/io_buffer.h" |
[email protected] | fb2622f | 2010-07-13 18:00:56 | [diff] [blame] | 10 | #include "net/base/net_errors.h" |
[email protected] | bc4fb8e | 2010-03-18 23:58:17 | [diff] [blame] | 11 | #include "net/base/test_completion_callback.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 12 | #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] | 79b3fec | 2011-11-15 22:33:47 | [diff] [blame] | 16 | DiskCacheTest::DiskCacheTest() { |
| 17 | cache_path_ = GetCacheFilePath(); |
| 18 | if (!MessageLoop::current()) |
| 19 | message_loop_.reset(new MessageLoopForIO()); |
| 20 | } |
| 21 | |
| 22 | DiskCacheTest::~DiskCacheTest() { |
| 23 | } |
| 24 | |
| 25 | bool 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 | |
| 38 | bool DiskCacheTest::CleanupCacheDir() { |
| 39 | return DeleteCache(cache_path_); |
| 40 | } |
| 41 | |
[email protected] | 17b8914 | 2008-11-07 21:52:15 | [diff] [blame] | 42 | void DiskCacheTest::TearDown() { |
| 43 | MessageLoop::current()->RunAllPending(); |
| 44 | } |
| 45 | |
[email protected] | 7aefb15 | 2011-01-21 23:46:49 | [diff] [blame] | 46 | DiskCacheTestWithCache::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.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 61 | } |
| 62 | |
[email protected] | 7aefb15 | 2011-01-21 23:46:49 | [diff] [blame] | 63 | DiskCacheTestWithCache::~DiskCacheTestWithCache() {} |
| 64 | |
[email protected] | 4d5e036 | 2008-08-28 00:59:06 | [diff] [blame] | 65 | void DiskCacheTestWithCache::InitCache() { |
[email protected] | 220d7100 | 2009-04-17 00:50:04 | [diff] [blame] | 66 | if (mask_ || new_eviction_) |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 67 | implementation_ = true; |
| 68 | |
| 69 | if (memory_only_) |
| 70 | InitMemoryCache(); |
| 71 | else |
| 72 | InitDiskCache(); |
| 73 | |
| 74 | ASSERT_TRUE(NULL != cache_); |
[email protected] | a9da16d | 2008-07-30 21:41:54 | [diff] [blame] | 75 | if (first_cleanup_) |
| 76 | ASSERT_EQ(0, cache_->GetEntryCount()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 77 | } |
| 78 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 79 | // We are expected to leak memory when simulating crashes. |
[email protected] | 4d5e036 | 2008-08-28 00:59:06 | [diff] [blame] | 80 | void DiskCacheTestWithCache::SimulateCrash() { |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 81 | ASSERT_TRUE(implementation_ && !memory_only_); |
[email protected] | 2a65aceb8 | 2011-12-19 20:59:27 | [diff] [blame] | 82 | net::TestCompletionCallback cb; |
| 83 | int rv = cache_impl_->FlushQueueForTest(cb.callback()); |
[email protected] | fb2622f | 2010-07-13 18:00:56 | [diff] [blame] | 84 | ASSERT_EQ(net::OK, cb.GetResult(rv)); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 85 | cache_impl_->ClearRefCountForTest(); |
| 86 | |
| 87 | delete cache_impl_; |
[email protected] | 79b3fec | 2011-11-15 22:33:47 | [diff] [blame] | 88 | EXPECT_TRUE(CheckCacheIntegrity(cache_path_, new_eviction_, mask_)); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 89 | |
[email protected] | 79b3fec | 2011-11-15 22:33:47 | [diff] [blame] | 90 | InitDiskCacheImpl(); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 91 | } |
[email protected] | a9da16d | 2008-07-30 21:41:54 | [diff] [blame] | 92 | |
[email protected] | 4d5e036 | 2008-08-28 00:59:06 | [diff] [blame] | 93 | void DiskCacheTestWithCache::SetTestMode() { |
[email protected] | a9da16d | 2008-07-30 21:41:54 | [diff] [blame] | 94 | ASSERT_TRUE(implementation_ && !memory_only_); |
| 95 | cache_impl_->SetUnitTestMode(); |
| 96 | } |
[email protected] | bc4fb8e | 2010-03-18 23:58:17 | [diff] [blame] | 97 | |
[email protected] | 7aefb15 | 2011-01-21 23:46:49 | [diff] [blame] | 98 | void 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] | bc4fb8e | 2010-03-18 23:58:17 | [diff] [blame] | 107 | int DiskCacheTestWithCache::OpenEntry(const std::string& key, |
| 108 | disk_cache::Entry** entry) { |
[email protected] | 2a65aceb8 | 2011-12-19 20:59:27 | [diff] [blame] | 109 | net::TestCompletionCallback cb; |
| 110 | int rv = cache_->OpenEntry(key, entry, cb.callback()); |
[email protected] | bc4fb8e | 2010-03-18 23:58:17 | [diff] [blame] | 111 | return cb.GetResult(rv); |
| 112 | } |
| 113 | |
| 114 | int DiskCacheTestWithCache::CreateEntry(const std::string& key, |
| 115 | disk_cache::Entry** entry) { |
[email protected] | 2a65aceb8 | 2011-12-19 20:59:27 | [diff] [blame] | 116 | net::TestCompletionCallback cb; |
| 117 | int rv = cache_->CreateEntry(key, entry, cb.callback()); |
[email protected] | bc4fb8e | 2010-03-18 23:58:17 | [diff] [blame] | 118 | return cb.GetResult(rv); |
| 119 | } |
| 120 | |
| 121 | int DiskCacheTestWithCache::DoomEntry(const std::string& key) { |
[email protected] | 42c45963 | 2011-12-17 02:20:23 | [diff] [blame] | 122 | net::TestCompletionCallback cb; |
| 123 | int rv = cache_->DoomEntry(key, cb.callback()); |
[email protected] | bc4fb8e | 2010-03-18 23:58:17 | [diff] [blame] | 124 | return cb.GetResult(rv); |
| 125 | } |
| 126 | |
| 127 | int DiskCacheTestWithCache::DoomAllEntries() { |
[email protected] | 6ad7c091 | 2011-12-15 19:10:19 | [diff] [blame] | 128 | net::TestCompletionCallback cb; |
| 129 | int rv = cache_->DoomAllEntries(cb.callback()); |
[email protected] | bc4fb8e | 2010-03-18 23:58:17 | [diff] [blame] | 130 | return cb.GetResult(rv); |
| 131 | } |
| 132 | |
| 133 | int DiskCacheTestWithCache::DoomEntriesBetween(const base::Time initial_time, |
| 134 | const base::Time end_time) { |
[email protected] | 6ad7c091 | 2011-12-15 19:10:19 | [diff] [blame] | 135 | net::TestCompletionCallback cb; |
| 136 | int rv = cache_->DoomEntriesBetween(initial_time, end_time, cb.callback()); |
[email protected] | bc4fb8e | 2010-03-18 23:58:17 | [diff] [blame] | 137 | return cb.GetResult(rv); |
| 138 | } |
| 139 | |
| 140 | int DiskCacheTestWithCache::DoomEntriesSince(const base::Time initial_time) { |
[email protected] | 2a65aceb8 | 2011-12-19 20:59:27 | [diff] [blame] | 141 | net::TestCompletionCallback cb; |
| 142 | int rv = cache_->DoomEntriesSince(initial_time, cb.callback()); |
[email protected] | bc4fb8e | 2010-03-18 23:58:17 | [diff] [blame] | 143 | return cb.GetResult(rv); |
| 144 | } |
| 145 | |
| 146 | int DiskCacheTestWithCache::OpenNextEntry(void** iter, |
| 147 | disk_cache::Entry** next_entry) { |
[email protected] | 6ad7c091 | 2011-12-15 19:10:19 | [diff] [blame] | 148 | net::TestCompletionCallback cb; |
| 149 | int rv = cache_->OpenNextEntry(iter, next_entry, cb.callback()); |
[email protected] | bc4fb8e | 2010-03-18 23:58:17 | [diff] [blame] | 150 | return cb.GetResult(rv); |
| 151 | } |
[email protected] | fb2622f | 2010-07-13 18:00:56 | [diff] [blame] | 152 | |
| 153 | void DiskCacheTestWithCache::FlushQueueForTest() { |
| 154 | if (memory_only_ || !cache_impl_) |
| 155 | return; |
| 156 | |
[email protected] | 2a65aceb8 | 2011-12-19 20:59:27 | [diff] [blame] | 157 | net::TestCompletionCallback cb; |
| 158 | int rv = cache_impl_->FlushQueueForTest(cb.callback()); |
[email protected] | fb2622f | 2010-07-13 18:00:56 | [diff] [blame] | 159 | EXPECT_EQ(net::OK, cb.GetResult(rv)); |
| 160 | } |
[email protected] | e1fcf14 | 2010-08-23 18:47:25 | [diff] [blame] | 161 | |
[email protected] | f27bbe00 | 2011-12-22 11:29:34 | [diff] [blame] | 162 | void DiskCacheTestWithCache::RunTaskForTest(const base::Closure& closure) { |
[email protected] | 65188eb | 2010-09-16 20:59:29 | [diff] [blame] | 163 | if (memory_only_ || !cache_impl_) { |
[email protected] | f27bbe00 | 2011-12-22 11:29:34 | [diff] [blame] | 164 | closure.Run(); |
[email protected] | 65188eb | 2010-09-16 20:59:29 | [diff] [blame] | 165 | return; |
| 166 | } |
| 167 | |
[email protected] | 2a65aceb8 | 2011-12-19 20:59:27 | [diff] [blame] | 168 | net::TestCompletionCallback cb; |
[email protected] | f27bbe00 | 2011-12-22 11:29:34 | [diff] [blame] | 169 | int rv = cache_impl_->RunTaskForTest(closure, cb.callback()); |
[email protected] | 65188eb | 2010-09-16 20:59:29 | [diff] [blame] | 170 | EXPECT_EQ(net::OK, cb.GetResult(rv)); |
| 171 | } |
| 172 | |
[email protected] | e1fcf14 | 2010-08-23 18:47:25 | [diff] [blame] | 173 | int DiskCacheTestWithCache::ReadData(disk_cache::Entry* entry, int index, |
| 174 | int offset, net::IOBuffer* buf, int len) { |
[email protected] | 2a65aceb8 | 2011-12-19 20:59:27 | [diff] [blame] | 175 | net::TestCompletionCallback cb; |
| 176 | int rv = entry->ReadData(index, offset, buf, len, cb.callback()); |
[email protected] | e1fcf14 | 2010-08-23 18:47:25 | [diff] [blame] | 177 | return cb.GetResult(rv); |
| 178 | } |
| 179 | |
[email protected] | e1fcf14 | 2010-08-23 18:47:25 | [diff] [blame] | 180 | int DiskCacheTestWithCache::WriteData(disk_cache::Entry* entry, int index, |
| 181 | int offset, net::IOBuffer* buf, int len, |
| 182 | bool truncate) { |
[email protected] | 2a65aceb8 | 2011-12-19 20:59:27 | [diff] [blame] | 183 | net::TestCompletionCallback cb; |
| 184 | int rv = entry->WriteData(index, offset, buf, len, cb.callback(), truncate); |
[email protected] | e1fcf14 | 2010-08-23 18:47:25 | [diff] [blame] | 185 | return cb.GetResult(rv); |
| 186 | } |
| 187 | |
| 188 | int DiskCacheTestWithCache::ReadSparseData(disk_cache::Entry* entry, |
| 189 | int64 offset, net::IOBuffer* buf, |
| 190 | int len) { |
[email protected] | 2a65aceb8 | 2011-12-19 20:59:27 | [diff] [blame] | 191 | net::TestCompletionCallback cb; |
| 192 | int rv = entry->ReadSparseData(offset, buf, len, cb.callback()); |
[email protected] | e1fcf14 | 2010-08-23 18:47:25 | [diff] [blame] | 193 | return cb.GetResult(rv); |
| 194 | } |
| 195 | |
| 196 | int DiskCacheTestWithCache::WriteSparseData(disk_cache::Entry* entry, |
| 197 | int64 offset, |
| 198 | net::IOBuffer* buf, int len) { |
[email protected] | 2a65aceb8 | 2011-12-19 20:59:27 | [diff] [blame] | 199 | net::TestCompletionCallback cb; |
| 200 | int rv = entry->WriteSparseData(offset, buf, len, cb.callback()); |
[email protected] | e1fcf14 | 2010-08-23 18:47:25 | [diff] [blame] | 201 | return cb.GetResult(rv); |
| 202 | } |
[email protected] | 7aefb15 | 2011-01-21 23:46:49 | [diff] [blame] | 203 | |
[email protected] | ceb61da3 | 2011-01-25 23:52:02 | [diff] [blame] | 204 | void DiskCacheTestWithCache::TrimForTest(bool empty) { |
[email protected] | f27bbe00 | 2011-12-22 11:29:34 | [diff] [blame] | 205 | RunTaskForTest(base::Bind(&disk_cache::BackendImpl::TrimForTest, |
| 206 | base::Unretained(cache_impl_), |
| 207 | empty)); |
[email protected] | ceb61da3 | 2011-01-25 23:52:02 | [diff] [blame] | 208 | } |
| 209 | |
| 210 | void DiskCacheTestWithCache::TrimDeletedListForTest(bool empty) { |
[email protected] | f27bbe00 | 2011-12-22 11:29:34 | [diff] [blame] | 211 | RunTaskForTest(base::Bind(&disk_cache::BackendImpl::TrimDeletedListForTest, |
| 212 | base::Unretained(cache_impl_), |
| 213 | empty)); |
[email protected] | ceb61da3 | 2011-01-25 23:52:02 | [diff] [blame] | 214 | } |
| 215 | |
[email protected] | 05f8087d | 2012-06-29 18:58:37 | [diff] [blame^] | 216 | void 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] | 7aefb15 | 2011-01-21 23:46:49 | [diff] [blame] | 223 | void 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] | 79b3fec | 2011-11-15 22:33:47 | [diff] [blame] | 230 | EXPECT_TRUE(CheckCacheIntegrity(cache_path_, new_eviction_, mask_)); |
[email protected] | 7aefb15 | 2011-01-21 23:46:49 | [diff] [blame] | 231 | } |
| 232 | |
| 233 | PlatformTest::TearDown(); |
| 234 | } |
| 235 | |
| 236 | void DiskCacheTestWithCache::InitMemoryCache() { |
| 237 | if (!implementation_) { |
[email protected] | 9eb8cdf | 2011-03-17 18:53:02 | [diff] [blame] | 238 | cache_ = disk_cache::MemBackendImpl::CreateBackend(size_, NULL); |
[email protected] | 7aefb15 | 2011-01-21 23:46:49 | [diff] [blame] | 239 | return; |
| 240 | } |
| 241 | |
[email protected] | 9eb8cdf | 2011-03-17 18:53:02 | [diff] [blame] | 242 | mem_cache_ = new disk_cache::MemBackendImpl(NULL); |
[email protected] | 7aefb15 | 2011-01-21 23:46:49 | [diff] [blame] | 243 | 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 | |
| 252 | void DiskCacheTestWithCache::InitDiskCache() { |
[email protected] | 7aefb15 | 2011-01-21 23:46:49 | [diff] [blame] | 253 | if (first_cleanup_) |
[email protected] | 79b3fec | 2011-11-15 22:33:47 | [diff] [blame] | 254 | ASSERT_TRUE(CleanupCacheDir()); |
[email protected] | 7aefb15 | 2011-01-21 23:46:49 | [diff] [blame] | 255 | |
| 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] | 79b3fec | 2011-11-15 22:33:47 | [diff] [blame] | 263 | return InitDiskCacheImpl(); |
[email protected] | 7aefb15 | 2011-01-21 23:46:49 | [diff] [blame] | 264 | |
| 265 | scoped_refptr<base::MessageLoopProxy> thread = |
[email protected] | edd685f | 2011-08-15 20:33:46 | [diff] [blame] | 266 | use_current_thread_ ? base::MessageLoopProxy::current() : |
[email protected] | 7aefb15 | 2011-01-21 23:46:49 | [diff] [blame] | 267 | cache_thread_.message_loop_proxy(); |
| 268 | |
[email protected] | 2a65aceb8 | 2011-12-19 20:59:27 | [diff] [blame] | 269 | net::TestCompletionCallback cb; |
[email protected] | 7aefb15 | 2011-01-21 23:46:49 | [diff] [blame] | 270 | int rv = disk_cache::BackendImpl::CreateBackend( |
[email protected] | 79b3fec | 2011-11-15 22:33:47 | [diff] [blame] | 271 | cache_path_, force_creation_, size_, type_, |
[email protected] | 2a65aceb8 | 2011-12-19 20:59:27 | [diff] [blame] | 272 | disk_cache::kNoRandom, thread, NULL, &cache_, cb.callback()); |
[email protected] | 7aefb15 | 2011-01-21 23:46:49 | [diff] [blame] | 273 | ASSERT_EQ(net::OK, cb.GetResult(rv)); |
| 274 | } |
| 275 | |
[email protected] | 79b3fec | 2011-11-15 22:33:47 | [diff] [blame] | 276 | void DiskCacheTestWithCache::InitDiskCacheImpl() { |
[email protected] | 7aefb15 | 2011-01-21 23:46:49 | [diff] [blame] | 277 | scoped_refptr<base::MessageLoopProxy> thread = |
[email protected] | edd685f | 2011-08-15 20:33:46 | [diff] [blame] | 278 | use_current_thread_ ? base::MessageLoopProxy::current() : |
[email protected] | 7aefb15 | 2011-01-21 23:46:49 | [diff] [blame] | 279 | cache_thread_.message_loop_proxy(); |
| 280 | if (mask_) |
[email protected] | 79b3fec | 2011-11-15 22:33:47 | [diff] [blame] | 281 | cache_impl_ = new disk_cache::BackendImpl(cache_path_, mask_, thread, NULL); |
[email protected] | 7aefb15 | 2011-01-21 23:46:49 | [diff] [blame] | 282 | else |
[email protected] | 79b3fec | 2011-11-15 22:33:47 | [diff] [blame] | 283 | cache_impl_ = new disk_cache::BackendImpl(cache_path_, thread, NULL); |
[email protected] | 7aefb15 | 2011-01-21 23:46:49 | [diff] [blame] | 284 | |
| 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] | 42c45963 | 2011-12-17 02:20:23 | [diff] [blame] | 296 | net::TestCompletionCallback cb; |
| 297 | int rv = cache_impl_->Init(cb.callback()); |
[email protected] | 7aefb15 | 2011-01-21 23:46:49 | [diff] [blame] | 298 | ASSERT_EQ(net::OK, cb.GetResult(rv)); |
| 299 | } |