[email protected] | bc4fb8e | 2010-03-18 23:58:17 | [diff] [blame^] | 1 | // Copyright (c) 2006-2010 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] | bc4fb8e | 2010-03-18 23:58:17 | [diff] [blame^] | 7 | #include "net/base/test_completion_callback.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 8 | #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] | 17b8914 | 2008-11-07 21:52:15 | [diff] [blame] | 12 | void DiskCacheTest::TearDown() { |
| 13 | MessageLoop::current()->RunAllPending(); |
| 14 | } |
| 15 | |
[email protected] | 4d5e036 | 2008-08-28 00:59:06 | [diff] [blame] | 16 | void DiskCacheTestWithCache::SetMaxSize(int size) { |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 17 | 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] | 4d5e036 | 2008-08-28 00:59:06 | [diff] [blame] | 25 | void DiskCacheTestWithCache::InitCache() { |
[email protected] | 220d7100 | 2009-04-17 00:50:04 | [diff] [blame] | 26 | if (mask_ || new_eviction_) |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 27 | implementation_ = true; |
| 28 | |
| 29 | if (memory_only_) |
| 30 | InitMemoryCache(); |
| 31 | else |
| 32 | InitDiskCache(); |
| 33 | |
| 34 | ASSERT_TRUE(NULL != cache_); |
[email protected] | a9da16d | 2008-07-30 21:41:54 | [diff] [blame] | 35 | if (first_cleanup_) |
| 36 | ASSERT_EQ(0, cache_->GetEntryCount()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 37 | } |
| 38 | |
[email protected] | 4d5e036 | 2008-08-28 00:59:06 | [diff] [blame] | 39 | void DiskCacheTestWithCache::InitMemoryCache() { |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 40 | 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] | 4d5e036 | 2008-08-28 00:59:06 | [diff] [blame] | 55 | void DiskCacheTestWithCache::InitDiskCache() { |
[email protected] | 5306d11 | 2009-10-15 17:38:04 | [diff] [blame] | 56 | FilePath path = GetCacheFilePath(); |
[email protected] | a9da16d | 2008-07-30 21:41:54 | [diff] [blame] | 57 | if (first_cleanup_) |
[email protected] | 5306d11 | 2009-10-15 17:38:04 | [diff] [blame] | 58 | ASSERT_TRUE(DeleteCache(path)); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 59 | |
[email protected] | 813149f8 | 2009-09-29 18:21:34 | [diff] [blame] | 60 | if (implementation_) |
[email protected] | 5306d11 | 2009-10-15 17:38:04 | [diff] [blame] | 61 | return InitDiskCacheImpl(path); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 62 | |
[email protected] | 813149f8 | 2009-09-29 18:21:34 | [diff] [blame] | 63 | cache_ = disk_cache::BackendImpl::CreateBackend(path, force_creation_, size_, |
| 64 | net::DISK_CACHE, |
| 65 | disk_cache::kNoRandom); |
[email protected] | 220d7100 | 2009-04-17 00:50:04 | [diff] [blame] | 66 | } |
| 67 | |
[email protected] | cfaa1f2 | 2009-10-12 17:14:59 | [diff] [blame] | 68 | void DiskCacheTestWithCache::InitDiskCacheImpl(const FilePath& path) { |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 69 | 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] | 220d7100 | 2009-04-17 00:50:04 | [diff] [blame] | 80 | if (new_eviction_) |
| 81 | cache_impl_->SetNewEviction(); |
| 82 | |
[email protected] | f1940b9 | 2009-08-01 00:58:01 | [diff] [blame] | 83 | cache_impl_->SetFlags(disk_cache::kNoRandom); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 84 | ASSERT_TRUE(cache_impl_->Init()); |
| 85 | } |
| 86 | |
[email protected] | 4d5e036 | 2008-08-28 00:59:06 | [diff] [blame] | 87 | void DiskCacheTestWithCache::TearDown() { |
[email protected] | 17b8914 | 2008-11-07 21:52:15 | [diff] [blame] | 88 | MessageLoop::current()->RunAllPending(); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 89 | delete cache_; |
| 90 | |
[email protected] | 220d7100 | 2009-04-17 00:50:04 | [diff] [blame] | 91 | if (!memory_only_ && integrity_) { |
[email protected] | 5306d11 | 2009-10-15 17:38:04 | [diff] [blame] | 92 | FilePath path = GetCacheFilePath(); |
[email protected] | 220d7100 | 2009-04-17 00:50:04 | [diff] [blame] | 93 | EXPECT_TRUE(CheckCacheIntegrity(path, new_eviction_)); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 94 | } |
[email protected] | 4d5e036 | 2008-08-28 00:59:06 | [diff] [blame] | 95 | |
| 96 | PlatformTest::TearDown(); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | // We are expected to leak memory when simulating crashes. |
[email protected] | 4d5e036 | 2008-08-28 00:59:06 | [diff] [blame] | 100 | void DiskCacheTestWithCache::SimulateCrash() { |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 101 | ASSERT_TRUE(implementation_ && !memory_only_); |
| 102 | cache_impl_->ClearRefCountForTest(); |
| 103 | |
| 104 | delete cache_impl_; |
[email protected] | 5306d11 | 2009-10-15 17:38:04 | [diff] [blame] | 105 | FilePath path = GetCacheFilePath(); |
[email protected] | 220d7100 | 2009-04-17 00:50:04 | [diff] [blame] | 106 | EXPECT_TRUE(CheckCacheIntegrity(path, new_eviction_)); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 107 | |
[email protected] | 5306d11 | 2009-10-15 17:38:04 | [diff] [blame] | 108 | InitDiskCacheImpl(path); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 109 | } |
[email protected] | a9da16d | 2008-07-30 21:41:54 | [diff] [blame] | 110 | |
[email protected] | 4d5e036 | 2008-08-28 00:59:06 | [diff] [blame] | 111 | void DiskCacheTestWithCache::SetTestMode() { |
[email protected] | a9da16d | 2008-07-30 21:41:54 | [diff] [blame] | 112 | ASSERT_TRUE(implementation_ && !memory_only_); |
| 113 | cache_impl_->SetUnitTestMode(); |
| 114 | } |
[email protected] | bc4fb8e | 2010-03-18 23:58:17 | [diff] [blame^] | 115 | |
| 116 | int 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 | |
| 123 | int 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 | |
| 130 | int DiskCacheTestWithCache::DoomEntry(const std::string& key) { |
| 131 | TestCompletionCallback cb; |
| 132 | int rv = cache_->DoomEntry(key, &cb); |
| 133 | return cb.GetResult(rv); |
| 134 | } |
| 135 | |
| 136 | int DiskCacheTestWithCache::DoomAllEntries() { |
| 137 | TestCompletionCallback cb; |
| 138 | int rv = cache_->DoomAllEntries(&cb); |
| 139 | return cb.GetResult(rv); |
| 140 | } |
| 141 | |
| 142 | int 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 | |
| 149 | int 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 | |
| 155 | int 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 | } |