| OLD | NEW |
| 1 // Copyright (c) 2006-2010 The Chromium Authors. All rights reserved. | 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 | 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 "net/base/io_buffer.h" | 7 #include "net/base/io_buffer.h" |
| 8 #include "net/base/net_errors.h" | 8 #include "net/base/net_errors.h" |
| 9 #include "net/base/test_completion_callback.h" | 9 #include "net/base/test_completion_callback.h" |
| 10 #include "net/disk_cache/backend_impl.h" | 10 #include "net/disk_cache/backend_impl.h" |
| 11 #include "net/disk_cache/disk_cache_test_util.h" | 11 #include "net/disk_cache/disk_cache_test_util.h" |
| 12 #include "net/disk_cache/mem_backend_impl.h" | 12 #include "net/disk_cache/mem_backend_impl.h" |
| 13 | 13 |
| 14 void DiskCacheTest::TearDown() { | 14 void DiskCacheTest::TearDown() { |
| 15 MessageLoop::current()->RunAllPending(); | 15 MessageLoop::current()->RunAllPending(); |
| 16 } | 16 } |
| 17 | 17 |
| 18 void DiskCacheTestWithCache::SetMaxSize(int size) { | 18 DiskCacheTestWithCache::DiskCacheTestWithCache() |
| 19 size_ = size; | 19 : cache_(NULL), |
| 20 if (cache_impl_) | 20 cache_impl_(NULL), |
| 21 EXPECT_TRUE(cache_impl_->SetMaxSize(size)); | 21 mem_cache_(NULL), |
| 22 mask_(0), |
| 23 size_(0), |
| 24 type_(net::DISK_CACHE), |
| 25 memory_only_(false), |
| 26 implementation_(false), |
| 27 force_creation_(false), |
| 28 new_eviction_(false), |
| 29 first_cleanup_(true), |
| 30 integrity_(true), |
| 31 use_current_thread_(false), |
| 32 cache_thread_("CacheThread") { |
| 33 } |
| 22 | 34 |
| 23 if (mem_cache_) | 35 DiskCacheTestWithCache::~DiskCacheTestWithCache() {} |
| 24 EXPECT_TRUE(mem_cache_->SetMaxSize(size)); | |
| 25 } | |
| 26 | 36 |
| 27 void DiskCacheTestWithCache::InitCache() { | 37 void DiskCacheTestWithCache::InitCache() { |
| 28 if (mask_ || new_eviction_) | 38 if (mask_ || new_eviction_) |
| 29 implementation_ = true; | 39 implementation_ = true; |
| 30 | 40 |
| 31 if (memory_only_) | 41 if (memory_only_) |
| 32 InitMemoryCache(); | 42 InitMemoryCache(); |
| 33 else | 43 else |
| 34 InitDiskCache(); | 44 InitDiskCache(); |
| 35 | 45 |
| 36 ASSERT_TRUE(NULL != cache_); | 46 ASSERT_TRUE(NULL != cache_); |
| 37 if (first_cleanup_) | 47 if (first_cleanup_) |
| 38 ASSERT_EQ(0, cache_->GetEntryCount()); | 48 ASSERT_EQ(0, cache_->GetEntryCount()); |
| 39 } | 49 } |
| 40 | 50 |
| 41 void DiskCacheTestWithCache::InitMemoryCache() { | |
| 42 if (!implementation_) { | |
| 43 cache_ = disk_cache::MemBackendImpl::CreateBackend(size_); | |
| 44 return; | |
| 45 } | |
| 46 | |
| 47 mem_cache_ = new disk_cache::MemBackendImpl(); | |
| 48 cache_ = mem_cache_; | |
| 49 ASSERT_TRUE(NULL != cache_); | |
| 50 | |
| 51 if (size_) | |
| 52 EXPECT_TRUE(mem_cache_->SetMaxSize(size_)); | |
| 53 | |
| 54 ASSERT_TRUE(mem_cache_->Init()); | |
| 55 } | |
| 56 | |
| 57 void DiskCacheTestWithCache::InitDiskCache() { | |
| 58 FilePath path = GetCacheFilePath(); | |
| 59 if (first_cleanup_) | |
| 60 ASSERT_TRUE(DeleteCache(path)); | |
| 61 | |
| 62 if (!cache_thread_.IsRunning()) { | |
| 63 EXPECT_TRUE(cache_thread_.StartWithOptions( | |
| 64 base::Thread::Options(MessageLoop::TYPE_IO, 0))); | |
| 65 } | |
| 66 ASSERT_TRUE(cache_thread_.message_loop() != NULL); | |
| 67 | |
| 68 if (implementation_) | |
| 69 return InitDiskCacheImpl(path); | |
| 70 | |
| 71 scoped_refptr<base::MessageLoopProxy> thread = | |
| 72 use_current_thread_ ? base::MessageLoopProxy::CreateForCurrentThread() : | |
| 73 cache_thread_.message_loop_proxy(); | |
| 74 | |
| 75 TestCompletionCallback cb; | |
| 76 int rv = disk_cache::BackendImpl::CreateBackend( | |
| 77 path, force_creation_, size_, type_, | |
| 78 disk_cache::kNoRandom, thread, NULL, &cache_, &cb); | |
| 79 ASSERT_EQ(net::OK, cb.GetResult(rv)); | |
| 80 } | |
| 81 | |
| 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(); | |
| 86 if (mask_) | |
| 87 cache_impl_ = new disk_cache::BackendImpl(path, mask_, thread, NULL); | |
| 88 else | |
| 89 cache_impl_ = new disk_cache::BackendImpl(path, thread, NULL); | |
| 90 | |
| 91 cache_ = cache_impl_; | |
| 92 ASSERT_TRUE(NULL != cache_); | |
| 93 | |
| 94 if (size_) | |
| 95 EXPECT_TRUE(cache_impl_->SetMaxSize(size_)); | |
| 96 | |
| 97 if (new_eviction_) | |
| 98 cache_impl_->SetNewEviction(); | |
| 99 | |
| 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)); | |
| 105 } | |
| 106 | |
| 107 void DiskCacheTestWithCache::TearDown() { | |
| 108 MessageLoop::current()->RunAllPending(); | |
| 109 delete cache_; | |
| 110 if (cache_thread_.IsRunning()) | |
| 111 cache_thread_.Stop(); | |
| 112 | |
| 113 if (!memory_only_ && integrity_) { | |
| 114 FilePath path = GetCacheFilePath(); | |
| 115 EXPECT_TRUE(CheckCacheIntegrity(path, new_eviction_)); | |
| 116 } | |
| 117 | |
| 118 PlatformTest::TearDown(); | |
| 119 } | |
| 120 | |
| 121 // We are expected to leak memory when simulating crashes. | 51 // We are expected to leak memory when simulating crashes. |
| 122 void DiskCacheTestWithCache::SimulateCrash() { | 52 void DiskCacheTestWithCache::SimulateCrash() { |
| 123 ASSERT_TRUE(implementation_ && !memory_only_); | 53 ASSERT_TRUE(implementation_ && !memory_only_); |
| 124 TestCompletionCallback cb; | 54 TestCompletionCallback cb; |
| 125 int rv = cache_impl_->FlushQueueForTest(&cb); | 55 int rv = cache_impl_->FlushQueueForTest(&cb); |
| 126 ASSERT_EQ(net::OK, cb.GetResult(rv)); | 56 ASSERT_EQ(net::OK, cb.GetResult(rv)); |
| 127 cache_impl_->ClearRefCountForTest(); | 57 cache_impl_->ClearRefCountForTest(); |
| 128 | 58 |
| 129 delete cache_impl_; | 59 delete cache_impl_; |
| 130 FilePath path = GetCacheFilePath(); | 60 FilePath path = GetCacheFilePath(); |
| 131 EXPECT_TRUE(CheckCacheIntegrity(path, new_eviction_)); | 61 EXPECT_TRUE(CheckCacheIntegrity(path, new_eviction_)); |
| 132 | 62 |
| 133 InitDiskCacheImpl(path); | 63 InitDiskCacheImpl(path); |
| 134 } | 64 } |
| 135 | 65 |
| 136 void DiskCacheTestWithCache::SetTestMode() { | 66 void DiskCacheTestWithCache::SetTestMode() { |
| 137 ASSERT_TRUE(implementation_ && !memory_only_); | 67 ASSERT_TRUE(implementation_ && !memory_only_); |
| 138 cache_impl_->SetUnitTestMode(); | 68 cache_impl_->SetUnitTestMode(); |
| 139 } | 69 } |
| 140 | 70 |
| 71 void DiskCacheTestWithCache::SetMaxSize(int size) { |
| 72 size_ = size; |
| 73 if (cache_impl_) |
| 74 EXPECT_TRUE(cache_impl_->SetMaxSize(size)); |
| 75 |
| 76 if (mem_cache_) |
| 77 EXPECT_TRUE(mem_cache_->SetMaxSize(size)); |
| 78 } |
| 79 |
| 141 int DiskCacheTestWithCache::OpenEntry(const std::string& key, | 80 int DiskCacheTestWithCache::OpenEntry(const std::string& key, |
| 142 disk_cache::Entry** entry) { | 81 disk_cache::Entry** entry) { |
| 143 TestCompletionCallback cb; | 82 TestCompletionCallback cb; |
| 144 int rv = cache_->OpenEntry(key, entry, &cb); | 83 int rv = cache_->OpenEntry(key, entry, &cb); |
| 145 return cb.GetResult(rv); | 84 return cb.GetResult(rv); |
| 146 } | 85 } |
| 147 | 86 |
| 148 int DiskCacheTestWithCache::CreateEntry(const std::string& key, | 87 int DiskCacheTestWithCache::CreateEntry(const std::string& key, |
| 149 disk_cache::Entry** entry) { | 88 disk_cache::Entry** entry) { |
| 150 TestCompletionCallback cb; | 89 TestCompletionCallback cb; |
| (...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 229 return cb.GetResult(rv); | 168 return cb.GetResult(rv); |
| 230 } | 169 } |
| 231 | 170 |
| 232 int DiskCacheTestWithCache::WriteSparseData(disk_cache::Entry* entry, | 171 int DiskCacheTestWithCache::WriteSparseData(disk_cache::Entry* entry, |
| 233 int64 offset, | 172 int64 offset, |
| 234 net::IOBuffer* buf, int len) { | 173 net::IOBuffer* buf, int len) { |
| 235 TestCompletionCallback cb; | 174 TestCompletionCallback cb; |
| 236 int rv = entry->WriteSparseData(offset, buf, len, &cb); | 175 int rv = entry->WriteSparseData(offset, buf, len, &cb); |
| 237 return cb.GetResult(rv); | 176 return cb.GetResult(rv); |
| 238 } | 177 } |
| 178 |
| 179 void DiskCacheTestWithCache::TearDown() { |
| 180 MessageLoop::current()->RunAllPending(); |
| 181 delete cache_; |
| 182 if (cache_thread_.IsRunning()) |
| 183 cache_thread_.Stop(); |
| 184 |
| 185 if (!memory_only_ && integrity_) { |
| 186 FilePath path = GetCacheFilePath(); |
| 187 EXPECT_TRUE(CheckCacheIntegrity(path, new_eviction_)); |
| 188 } |
| 189 |
| 190 PlatformTest::TearDown(); |
| 191 } |
| 192 |
| 193 void DiskCacheTestWithCache::InitMemoryCache() { |
| 194 if (!implementation_) { |
| 195 cache_ = disk_cache::MemBackendImpl::CreateBackend(size_); |
| 196 return; |
| 197 } |
| 198 |
| 199 mem_cache_ = new disk_cache::MemBackendImpl(); |
| 200 cache_ = mem_cache_; |
| 201 ASSERT_TRUE(NULL != cache_); |
| 202 |
| 203 if (size_) |
| 204 EXPECT_TRUE(mem_cache_->SetMaxSize(size_)); |
| 205 |
| 206 ASSERT_TRUE(mem_cache_->Init()); |
| 207 } |
| 208 |
| 209 void DiskCacheTestWithCache::InitDiskCache() { |
| 210 FilePath path = GetCacheFilePath(); |
| 211 if (first_cleanup_) |
| 212 ASSERT_TRUE(DeleteCache(path)); |
| 213 |
| 214 if (!cache_thread_.IsRunning()) { |
| 215 EXPECT_TRUE(cache_thread_.StartWithOptions( |
| 216 base::Thread::Options(MessageLoop::TYPE_IO, 0))); |
| 217 } |
| 218 ASSERT_TRUE(cache_thread_.message_loop() != NULL); |
| 219 |
| 220 if (implementation_) |
| 221 return InitDiskCacheImpl(path); |
| 222 |
| 223 scoped_refptr<base::MessageLoopProxy> thread = |
| 224 use_current_thread_ ? base::MessageLoopProxy::CreateForCurrentThread() : |
| 225 cache_thread_.message_loop_proxy(); |
| 226 |
| 227 TestCompletionCallback cb; |
| 228 int rv = disk_cache::BackendImpl::CreateBackend( |
| 229 path, force_creation_, size_, type_, |
| 230 disk_cache::kNoRandom, thread, NULL, &cache_, &cb); |
| 231 ASSERT_EQ(net::OK, cb.GetResult(rv)); |
| 232 } |
| 233 |
| 234 void DiskCacheTestWithCache::InitDiskCacheImpl(const FilePath& path) { |
| 235 scoped_refptr<base::MessageLoopProxy> thread = |
| 236 use_current_thread_ ? base::MessageLoopProxy::CreateForCurrentThread() : |
| 237 cache_thread_.message_loop_proxy(); |
| 238 if (mask_) |
| 239 cache_impl_ = new disk_cache::BackendImpl(path, mask_, thread, NULL); |
| 240 else |
| 241 cache_impl_ = new disk_cache::BackendImpl(path, thread, NULL); |
| 242 |
| 243 cache_ = cache_impl_; |
| 244 ASSERT_TRUE(NULL != cache_); |
| 245 |
| 246 if (size_) |
| 247 EXPECT_TRUE(cache_impl_->SetMaxSize(size_)); |
| 248 |
| 249 if (new_eviction_) |
| 250 cache_impl_->SetNewEviction(); |
| 251 |
| 252 cache_impl_->SetType(type_); |
| 253 cache_impl_->SetFlags(disk_cache::kNoRandom); |
| 254 TestCompletionCallback cb; |
| 255 int rv = cache_impl_->Init(&cb); |
| 256 ASSERT_EQ(net::OK, cb.GetResult(rv)); |
| 257 } |
| OLD | NEW |