| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "net/disk_cache/disk_cache_test_base.h" | 5 #include "net/disk_cache/disk_cache_test_base.h" |
| 6 | 6 |
| 7 #include "base/file_util.h" |
| 8 #include "base/path_service.h" |
| 7 #include "net/base/io_buffer.h" | 9 #include "net/base/io_buffer.h" |
| 8 #include "net/base/net_errors.h" | 10 #include "net/base/net_errors.h" |
| 9 #include "net/base/test_completion_callback.h" | 11 #include "net/base/test_completion_callback.h" |
| 10 #include "net/disk_cache/backend_impl.h" | 12 #include "net/disk_cache/backend_impl.h" |
| 11 #include "net/disk_cache/disk_cache_test_util.h" | 13 #include "net/disk_cache/disk_cache_test_util.h" |
| 12 #include "net/disk_cache/mem_backend_impl.h" | 14 #include "net/disk_cache/mem_backend_impl.h" |
| 13 | 15 |
| 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 |
| 14 void DiskCacheTest::TearDown() { | 42 void DiskCacheTest::TearDown() { |
| 15 MessageLoop::current()->RunAllPending(); | 43 MessageLoop::current()->RunAllPending(); |
| 16 } | 44 } |
| 17 | 45 |
| 18 DiskCacheTestWithCache::DiskCacheTestWithCache() | 46 DiskCacheTestWithCache::DiskCacheTestWithCache() |
| 19 : cache_(NULL), | 47 : cache_(NULL), |
| 20 cache_impl_(NULL), | 48 cache_impl_(NULL), |
| 21 mem_cache_(NULL), | 49 mem_cache_(NULL), |
| 22 mask_(0), | 50 mask_(0), |
| 23 size_(0), | 51 size_(0), |
| (...skipping 26 matching lines...) Expand all Loading... |
| 50 | 78 |
| 51 // We are expected to leak memory when simulating crashes. | 79 // We are expected to leak memory when simulating crashes. |
| 52 void DiskCacheTestWithCache::SimulateCrash() { | 80 void DiskCacheTestWithCache::SimulateCrash() { |
| 53 ASSERT_TRUE(implementation_ && !memory_only_); | 81 ASSERT_TRUE(implementation_ && !memory_only_); |
| 54 TestOldCompletionCallback cb; | 82 TestOldCompletionCallback cb; |
| 55 int rv = cache_impl_->FlushQueueForTest(&cb); | 83 int rv = cache_impl_->FlushQueueForTest(&cb); |
| 56 ASSERT_EQ(net::OK, cb.GetResult(rv)); | 84 ASSERT_EQ(net::OK, cb.GetResult(rv)); |
| 57 cache_impl_->ClearRefCountForTest(); | 85 cache_impl_->ClearRefCountForTest(); |
| 58 | 86 |
| 59 delete cache_impl_; | 87 delete cache_impl_; |
| 60 FilePath path = GetCacheFilePath(); | 88 EXPECT_TRUE(CheckCacheIntegrity(cache_path_, new_eviction_, mask_)); |
| 61 EXPECT_TRUE(CheckCacheIntegrity(path, new_eviction_, mask_)); | |
| 62 | 89 |
| 63 InitDiskCacheImpl(path); | 90 InitDiskCacheImpl(); |
| 64 } | 91 } |
| 65 | 92 |
| 66 void DiskCacheTestWithCache::SetTestMode() { | 93 void DiskCacheTestWithCache::SetTestMode() { |
| 67 ASSERT_TRUE(implementation_ && !memory_only_); | 94 ASSERT_TRUE(implementation_ && !memory_only_); |
| 68 cache_impl_->SetUnitTestMode(); | 95 cache_impl_->SetUnitTestMode(); |
| 69 } | 96 } |
| 70 | 97 |
| 71 void DiskCacheTestWithCache::SetMaxSize(int size) { | 98 void DiskCacheTestWithCache::SetMaxSize(int size) { |
| 72 size_ = size; | 99 size_ = size; |
| 73 if (cache_impl_) | 100 if (cache_impl_) |
| (...skipping 131 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 205 RunTaskForTest(new TrimTask(cache_impl_, true, empty)); | 232 RunTaskForTest(new TrimTask(cache_impl_, true, empty)); |
| 206 } | 233 } |
| 207 | 234 |
| 208 void DiskCacheTestWithCache::TearDown() { | 235 void DiskCacheTestWithCache::TearDown() { |
| 209 MessageLoop::current()->RunAllPending(); | 236 MessageLoop::current()->RunAllPending(); |
| 210 delete cache_; | 237 delete cache_; |
| 211 if (cache_thread_.IsRunning()) | 238 if (cache_thread_.IsRunning()) |
| 212 cache_thread_.Stop(); | 239 cache_thread_.Stop(); |
| 213 | 240 |
| 214 if (!memory_only_ && integrity_) { | 241 if (!memory_only_ && integrity_) { |
| 215 FilePath path = GetCacheFilePath(); | 242 EXPECT_TRUE(CheckCacheIntegrity(cache_path_, new_eviction_, mask_)); |
| 216 EXPECT_TRUE(CheckCacheIntegrity(path, new_eviction_, mask_)); | |
| 217 } | 243 } |
| 218 | 244 |
| 219 PlatformTest::TearDown(); | 245 PlatformTest::TearDown(); |
| 220 } | 246 } |
| 221 | 247 |
| 222 void DiskCacheTestWithCache::InitMemoryCache() { | 248 void DiskCacheTestWithCache::InitMemoryCache() { |
| 223 if (!implementation_) { | 249 if (!implementation_) { |
| 224 cache_ = disk_cache::MemBackendImpl::CreateBackend(size_, NULL); | 250 cache_ = disk_cache::MemBackendImpl::CreateBackend(size_, NULL); |
| 225 return; | 251 return; |
| 226 } | 252 } |
| 227 | 253 |
| 228 mem_cache_ = new disk_cache::MemBackendImpl(NULL); | 254 mem_cache_ = new disk_cache::MemBackendImpl(NULL); |
| 229 cache_ = mem_cache_; | 255 cache_ = mem_cache_; |
| 230 ASSERT_TRUE(NULL != cache_); | 256 ASSERT_TRUE(NULL != cache_); |
| 231 | 257 |
| 232 if (size_) | 258 if (size_) |
| 233 EXPECT_TRUE(mem_cache_->SetMaxSize(size_)); | 259 EXPECT_TRUE(mem_cache_->SetMaxSize(size_)); |
| 234 | 260 |
| 235 ASSERT_TRUE(mem_cache_->Init()); | 261 ASSERT_TRUE(mem_cache_->Init()); |
| 236 } | 262 } |
| 237 | 263 |
| 238 void DiskCacheTestWithCache::InitDiskCache() { | 264 void DiskCacheTestWithCache::InitDiskCache() { |
| 239 FilePath path = GetCacheFilePath(); | |
| 240 if (first_cleanup_) | 265 if (first_cleanup_) |
| 241 ASSERT_TRUE(DeleteCache(path)); | 266 ASSERT_TRUE(CleanupCacheDir()); |
| 242 | 267 |
| 243 if (!cache_thread_.IsRunning()) { | 268 if (!cache_thread_.IsRunning()) { |
| 244 EXPECT_TRUE(cache_thread_.StartWithOptions( | 269 EXPECT_TRUE(cache_thread_.StartWithOptions( |
| 245 base::Thread::Options(MessageLoop::TYPE_IO, 0))); | 270 base::Thread::Options(MessageLoop::TYPE_IO, 0))); |
| 246 } | 271 } |
| 247 ASSERT_TRUE(cache_thread_.message_loop() != NULL); | 272 ASSERT_TRUE(cache_thread_.message_loop() != NULL); |
| 248 | 273 |
| 249 if (implementation_) | 274 if (implementation_) |
| 250 return InitDiskCacheImpl(path); | 275 return InitDiskCacheImpl(); |
| 251 | 276 |
| 252 scoped_refptr<base::MessageLoopProxy> thread = | 277 scoped_refptr<base::MessageLoopProxy> thread = |
| 253 use_current_thread_ ? base::MessageLoopProxy::current() : | 278 use_current_thread_ ? base::MessageLoopProxy::current() : |
| 254 cache_thread_.message_loop_proxy(); | 279 cache_thread_.message_loop_proxy(); |
| 255 | 280 |
| 256 TestOldCompletionCallback cb; | 281 TestOldCompletionCallback cb; |
| 257 int rv = disk_cache::BackendImpl::CreateBackend( | 282 int rv = disk_cache::BackendImpl::CreateBackend( |
| 258 path, force_creation_, size_, type_, | 283 cache_path_, force_creation_, size_, type_, |
| 259 disk_cache::kNoRandom, thread, NULL, &cache_, &cb); | 284 disk_cache::kNoRandom, thread, NULL, &cache_, &cb); |
| 260 ASSERT_EQ(net::OK, cb.GetResult(rv)); | 285 ASSERT_EQ(net::OK, cb.GetResult(rv)); |
| 261 } | 286 } |
| 262 | 287 |
| 263 void DiskCacheTestWithCache::InitDiskCacheImpl(const FilePath& path) { | 288 void DiskCacheTestWithCache::InitDiskCacheImpl() { |
| 264 scoped_refptr<base::MessageLoopProxy> thread = | 289 scoped_refptr<base::MessageLoopProxy> thread = |
| 265 use_current_thread_ ? base::MessageLoopProxy::current() : | 290 use_current_thread_ ? base::MessageLoopProxy::current() : |
| 266 cache_thread_.message_loop_proxy(); | 291 cache_thread_.message_loop_proxy(); |
| 267 if (mask_) | 292 if (mask_) |
| 268 cache_impl_ = new disk_cache::BackendImpl(path, mask_, thread, NULL); | 293 cache_impl_ = new disk_cache::BackendImpl(cache_path_, mask_, thread, NULL); |
| 269 else | 294 else |
| 270 cache_impl_ = new disk_cache::BackendImpl(path, thread, NULL); | 295 cache_impl_ = new disk_cache::BackendImpl(cache_path_, thread, NULL); |
| 271 | 296 |
| 272 cache_ = cache_impl_; | 297 cache_ = cache_impl_; |
| 273 ASSERT_TRUE(NULL != cache_); | 298 ASSERT_TRUE(NULL != cache_); |
| 274 | 299 |
| 275 if (size_) | 300 if (size_) |
| 276 EXPECT_TRUE(cache_impl_->SetMaxSize(size_)); | 301 EXPECT_TRUE(cache_impl_->SetMaxSize(size_)); |
| 277 | 302 |
| 278 if (new_eviction_) | 303 if (new_eviction_) |
| 279 cache_impl_->SetNewEviction(); | 304 cache_impl_->SetNewEviction(); |
| 280 | 305 |
| 281 cache_impl_->SetType(type_); | 306 cache_impl_->SetType(type_); |
| 282 cache_impl_->SetFlags(disk_cache::kNoRandom); | 307 cache_impl_->SetFlags(disk_cache::kNoRandom); |
| 283 TestOldCompletionCallback cb; | 308 TestOldCompletionCallback cb; |
| 284 int rv = cache_impl_->Init(&cb); | 309 int rv = cache_impl_->Init(&cb); |
| 285 ASSERT_EQ(net::OK, cb.GetResult(rv)); | 310 ASSERT_EQ(net::OK, cb.GetResult(rv)); |
| 286 } | 311 } |
| OLD | NEW |