blob: 2738674a3355a3ea553bc8d063323d8d37ddb880 [file] [log] [blame]
[email protected]bc4fb8e2010-03-18 23:58:171// Copyright (c) 2006-2010 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]bc4fb8e2010-03-18 23:58:177#include "net/base/test_completion_callback.h"
initial.commit586acc5fe2008-07-26 22:42:528#include "net/disk_cache/backend_impl.h"
9#include "net/disk_cache/disk_cache_test_util.h"
10#include "net/disk_cache/mem_backend_impl.h"
11
[email protected]17b89142008-11-07 21:52:1512void DiskCacheTest::TearDown() {
13 MessageLoop::current()->RunAllPending();
14}
15
[email protected]4d5e0362008-08-28 00:59:0616void DiskCacheTestWithCache::SetMaxSize(int size) {
initial.commit586acc5fe2008-07-26 22:42:5217 size_ = size;
18 if (cache_impl_)
19 EXPECT_TRUE(cache_impl_->SetMaxSize(size));
20
21 if (mem_cache_)
22 EXPECT_TRUE(mem_cache_->SetMaxSize(size));
23}
24
[email protected]4d5e0362008-08-28 00:59:0625void DiskCacheTestWithCache::InitCache() {
[email protected]220d71002009-04-17 00:50:0426 if (mask_ || new_eviction_)
initial.commit586acc5fe2008-07-26 22:42:5227 implementation_ = true;
28
29 if (memory_only_)
30 InitMemoryCache();
31 else
32 InitDiskCache();
33
34 ASSERT_TRUE(NULL != cache_);
[email protected]a9da16d2008-07-30 21:41:5435 if (first_cleanup_)
36 ASSERT_EQ(0, cache_->GetEntryCount());
initial.commit586acc5fe2008-07-26 22:42:5237}
38
[email protected]4d5e0362008-08-28 00:59:0639void DiskCacheTestWithCache::InitMemoryCache() {
initial.commit586acc5fe2008-07-26 22:42:5240 if (!implementation_) {
41 cache_ = disk_cache::CreateInMemoryCacheBackend(size_);
42 return;
43 }
44
45 mem_cache_ = new disk_cache::MemBackendImpl();
46 cache_ = mem_cache_;
47 ASSERT_TRUE(NULL != cache_);
48
49 if (size_)
50 EXPECT_TRUE(mem_cache_->SetMaxSize(size_));
51
52 ASSERT_TRUE(mem_cache_->Init());
53}
54
[email protected]4d5e0362008-08-28 00:59:0655void DiskCacheTestWithCache::InitDiskCache() {
[email protected]5306d112009-10-15 17:38:0456 FilePath path = GetCacheFilePath();
[email protected]a9da16d2008-07-30 21:41:5457 if (first_cleanup_)
[email protected]5306d112009-10-15 17:38:0458 ASSERT_TRUE(DeleteCache(path));
initial.commit586acc5fe2008-07-26 22:42:5259
[email protected]813149f82009-09-29 18:21:3460 if (implementation_)
[email protected]5306d112009-10-15 17:38:0461 return InitDiskCacheImpl(path);
initial.commit586acc5fe2008-07-26 22:42:5262
[email protected]813149f82009-09-29 18:21:3463 cache_ = disk_cache::BackendImpl::CreateBackend(path, force_creation_, size_,
64 net::DISK_CACHE,
65 disk_cache::kNoRandom);
[email protected]220d71002009-04-17 00:50:0466}
67
[email protected]cfaa1f22009-10-12 17:14:5968void DiskCacheTestWithCache::InitDiskCacheImpl(const FilePath& path) {
initial.commit586acc5fe2008-07-26 22:42:5269 if (mask_)
70 cache_impl_ = new disk_cache::BackendImpl(path, mask_);
71 else
72 cache_impl_ = new disk_cache::BackendImpl(path);
73
74 cache_ = cache_impl_;
75 ASSERT_TRUE(NULL != cache_);
76
77 if (size_)
78 EXPECT_TRUE(cache_impl_->SetMaxSize(size_));
79
[email protected]220d71002009-04-17 00:50:0480 if (new_eviction_)
81 cache_impl_->SetNewEviction();
82
[email protected]f1940b92009-08-01 00:58:0183 cache_impl_->SetFlags(disk_cache::kNoRandom);
initial.commit586acc5fe2008-07-26 22:42:5284 ASSERT_TRUE(cache_impl_->Init());
85}
86
[email protected]4d5e0362008-08-28 00:59:0687void DiskCacheTestWithCache::TearDown() {
[email protected]17b89142008-11-07 21:52:1588 MessageLoop::current()->RunAllPending();
initial.commit586acc5fe2008-07-26 22:42:5289 delete cache_;
90
[email protected]220d71002009-04-17 00:50:0491 if (!memory_only_ && integrity_) {
[email protected]5306d112009-10-15 17:38:0492 FilePath path = GetCacheFilePath();
[email protected]220d71002009-04-17 00:50:0493 EXPECT_TRUE(CheckCacheIntegrity(path, new_eviction_));
initial.commit586acc5fe2008-07-26 22:42:5294 }
[email protected]4d5e0362008-08-28 00:59:0695
96 PlatformTest::TearDown();
initial.commit586acc5fe2008-07-26 22:42:5297}
98
99// We are expected to leak memory when simulating crashes.
[email protected]4d5e0362008-08-28 00:59:06100void DiskCacheTestWithCache::SimulateCrash() {
initial.commit586acc5fe2008-07-26 22:42:52101 ASSERT_TRUE(implementation_ && !memory_only_);
102 cache_impl_->ClearRefCountForTest();
103
104 delete cache_impl_;
[email protected]5306d112009-10-15 17:38:04105 FilePath path = GetCacheFilePath();
[email protected]220d71002009-04-17 00:50:04106 EXPECT_TRUE(CheckCacheIntegrity(path, new_eviction_));
initial.commit586acc5fe2008-07-26 22:42:52107
[email protected]5306d112009-10-15 17:38:04108 InitDiskCacheImpl(path);
initial.commit586acc5fe2008-07-26 22:42:52109}
[email protected]a9da16d2008-07-30 21:41:54110
[email protected]4d5e0362008-08-28 00:59:06111void DiskCacheTestWithCache::SetTestMode() {
[email protected]a9da16d2008-07-30 21:41:54112 ASSERT_TRUE(implementation_ && !memory_only_);
113 cache_impl_->SetUnitTestMode();
114}
[email protected]bc4fb8e2010-03-18 23:58:17115
116int DiskCacheTestWithCache::OpenEntry(const std::string& key,
117 disk_cache::Entry** entry) {
118 TestCompletionCallback cb;
119 int rv = cache_->OpenEntry(key, entry, &cb);
120 return cb.GetResult(rv);
121}
122
123int DiskCacheTestWithCache::CreateEntry(const std::string& key,
124 disk_cache::Entry** entry) {
125 TestCompletionCallback cb;
126 int rv = cache_->CreateEntry(key, entry, &cb);
127 return cb.GetResult(rv);
128}
129
130int DiskCacheTestWithCache::DoomEntry(const std::string& key) {
131 TestCompletionCallback cb;
132 int rv = cache_->DoomEntry(key, &cb);
133 return cb.GetResult(rv);
134}
135
136int DiskCacheTestWithCache::DoomAllEntries() {
137 TestCompletionCallback cb;
138 int rv = cache_->DoomAllEntries(&cb);
139 return cb.GetResult(rv);
140}
141
142int DiskCacheTestWithCache::DoomEntriesBetween(const base::Time initial_time,
143 const base::Time end_time) {
144 TestCompletionCallback cb;
145 int rv = cache_->DoomEntriesBetween(initial_time, end_time, &cb);
146 return cb.GetResult(rv);
147}
148
149int DiskCacheTestWithCache::DoomEntriesSince(const base::Time initial_time) {
150 TestCompletionCallback cb;
151 int rv = cache_->DoomEntriesSince(initial_time, &cb);
152 return cb.GetResult(rv);
153}
154
155int DiskCacheTestWithCache::OpenNextEntry(void** iter,
156 disk_cache::Entry** next_entry) {
157 TestCompletionCallback cb;
158 int rv = cache_->OpenNextEntry(iter, next_entry, &cb);
159 return cb.GetResult(rv);
160}