initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame^] | 1 | // Copyright 2008, Google Inc. |
| 2 | // All rights reserved. |
| 3 | // |
| 4 | // Redistribution and use in source and binary forms, with or without |
| 5 | // modification, are permitted provided that the following conditions are |
| 6 | // met: |
| 7 | // |
| 8 | // * Redistributions of source code must retain the above copyright |
| 9 | // notice, this list of conditions and the following disclaimer. |
| 10 | // * Redistributions in binary form must reproduce the above |
| 11 | // copyright notice, this list of conditions and the following disclaimer |
| 12 | // in the documentation and/or other materials provided with the |
| 13 | // distribution. |
| 14 | // * Neither the name of Google Inc. nor the names of its |
| 15 | // contributors may be used to endorse or promote products derived from |
| 16 | // this software without specific prior written permission. |
| 17 | // |
| 18 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 19 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 20 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 21 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 22 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 23 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 24 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 25 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 26 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 27 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 28 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 | |
| 30 | #include "net/disk_cache/disk_cache_test_base.h" |
| 31 | |
| 32 | #include "net/disk_cache/backend_impl.h" |
| 33 | #include "net/disk_cache/disk_cache_test_util.h" |
| 34 | #include "net/disk_cache/mem_backend_impl.h" |
| 35 | |
| 36 | void DiskCacheTestBase::SetMaxSize(int size) { |
| 37 | size_ = size; |
| 38 | if (cache_impl_) |
| 39 | EXPECT_TRUE(cache_impl_->SetMaxSize(size)); |
| 40 | |
| 41 | if (mem_cache_) |
| 42 | EXPECT_TRUE(mem_cache_->SetMaxSize(size)); |
| 43 | } |
| 44 | |
| 45 | void DiskCacheTestBase::InitCache() { |
| 46 | if (mask_) |
| 47 | implementation_ = true; |
| 48 | |
| 49 | if (memory_only_) |
| 50 | InitMemoryCache(); |
| 51 | else |
| 52 | InitDiskCache(); |
| 53 | |
| 54 | ASSERT_TRUE(NULL != cache_); |
| 55 | ASSERT_EQ(0, cache_->GetEntryCount()); |
| 56 | } |
| 57 | |
| 58 | void DiskCacheTestBase::InitMemoryCache() { |
| 59 | if (!implementation_) { |
| 60 | cache_ = disk_cache::CreateInMemoryCacheBackend(size_); |
| 61 | return; |
| 62 | } |
| 63 | |
| 64 | mem_cache_ = new disk_cache::MemBackendImpl(); |
| 65 | cache_ = mem_cache_; |
| 66 | ASSERT_TRUE(NULL != cache_); |
| 67 | |
| 68 | if (size_) |
| 69 | EXPECT_TRUE(mem_cache_->SetMaxSize(size_)); |
| 70 | |
| 71 | ASSERT_TRUE(mem_cache_->Init()); |
| 72 | } |
| 73 | |
| 74 | void DiskCacheTestBase::InitDiskCache() { |
| 75 | std::wstring path = GetCachePath(); |
| 76 | ASSERT_TRUE(DeleteCache(path.c_str())); |
| 77 | |
| 78 | if (!implementation_) { |
| 79 | cache_ = disk_cache::CreateCacheBackend(path, force_creation_, size_); |
| 80 | return; |
| 81 | } |
| 82 | |
| 83 | if (mask_) |
| 84 | cache_impl_ = new disk_cache::BackendImpl(path, mask_); |
| 85 | else |
| 86 | cache_impl_ = new disk_cache::BackendImpl(path); |
| 87 | |
| 88 | cache_ = cache_impl_; |
| 89 | ASSERT_TRUE(NULL != cache_); |
| 90 | |
| 91 | if (size_) |
| 92 | EXPECT_TRUE(cache_impl_->SetMaxSize(size_)); |
| 93 | |
| 94 | ASSERT_TRUE(cache_impl_->Init()); |
| 95 | } |
| 96 | |
| 97 | |
| 98 | void DiskCacheTestBase::TearDown() { |
| 99 | delete cache_; |
| 100 | |
| 101 | if (!memory_only_) { |
| 102 | std::wstring path = GetCachePath(); |
| 103 | EXPECT_TRUE(CheckCacheIntegrity(path)); |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | // We are expected to leak memory when simulating crashes. |
| 108 | void DiskCacheTestBase::SimulateCrash() { |
| 109 | ASSERT_TRUE(implementation_ && !memory_only_); |
| 110 | cache_impl_->ClearRefCountForTest(); |
| 111 | |
| 112 | delete cache_impl_; |
| 113 | std::wstring path = GetCachePath(); |
| 114 | EXPECT_TRUE(CheckCacheIntegrity(path)); |
| 115 | |
| 116 | if (mask_) |
| 117 | cache_impl_ = new disk_cache::BackendImpl(path, mask_); |
| 118 | else |
| 119 | cache_impl_ = new disk_cache::BackendImpl(path); |
| 120 | cache_ = cache_impl_; |
| 121 | ASSERT_TRUE(NULL != cache_); |
| 122 | |
| 123 | if (size_) |
| 124 | cache_impl_->SetMaxSize(size_); |
| 125 | ASSERT_TRUE(cache_impl_->Init()); |
| 126 | } |