[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 | |
[email protected] | 4d5e036 | 2008-08-28 00:59:06 | [diff] [blame] | 5 | #ifndef NET_DISK_CACHE_DISK_CACHE_TEST_BASE_H_ |
| 6 | #define NET_DISK_CACHE_DISK_CACHE_TEST_BASE_H_ |
[email protected] | 32b76ef | 2010-07-26 23:08:24 | [diff] [blame] | 7 | #pragma once |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 8 | |
| 9 | #include "base/basictypes.h" |
[email protected] | 34b9963 | 2011-01-01 01:01:06 | [diff] [blame^] | 10 | #include "base/threading/thread.h" |
[email protected] | 67b09ec | 2010-08-27 17:49:42 | [diff] [blame] | 11 | #include "net/base/cache_type.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 12 | #include "testing/gtest/include/gtest/gtest.h" |
[email protected] | 23887f04f | 2008-12-02 19:20:15 | [diff] [blame] | 13 | #include "testing/platform_test.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 14 | |
[email protected] | cfaa1f2 | 2009-10-12 17:14:59 | [diff] [blame] | 15 | class FilePath; |
| 16 | |
[email protected] | e1fcf14 | 2010-08-23 18:47:25 | [diff] [blame] | 17 | namespace net { |
| 18 | |
| 19 | class IOBuffer; |
| 20 | |
| 21 | } // namespace net |
| 22 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 23 | namespace disk_cache { |
| 24 | |
| 25 | class Backend; |
| 26 | class BackendImpl; |
[email protected] | bc4fb8e | 2010-03-18 23:58:17 | [diff] [blame] | 27 | class Entry; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 28 | class MemBackendImpl; |
| 29 | |
[email protected] | 4d5e036 | 2008-08-28 00:59:06 | [diff] [blame] | 30 | } // namespace disk_cache |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 31 | |
[email protected] | 17b8914 | 2008-11-07 21:52:15 | [diff] [blame] | 32 | // These tests can use the path service, which uses autoreleased objects on the |
| 33 | // Mac, so this needs to be a PlatformTest. Even tests that do not require a |
| 34 | // cache (and that do not need to be a DiskCacheTestWithCache) are susceptible |
| 35 | // to this problem; all such tests should use TEST_F(DiskCacheTest, ...). |
| 36 | class DiskCacheTest : public PlatformTest { |
| 37 | virtual void TearDown(); |
| 38 | }; |
| 39 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 40 | // Provides basic support for cache related tests. |
[email protected] | 4d5e036 | 2008-08-28 00:59:06 | [diff] [blame] | 41 | class DiskCacheTestWithCache : public DiskCacheTest { |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 42 | protected: |
[email protected] | 4d5e036 | 2008-08-28 00:59:06 | [diff] [blame] | 43 | DiskCacheTestWithCache() |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 44 | : cache_(NULL), cache_impl_(NULL), mem_cache_(NULL), mask_(0), size_(0), |
[email protected] | 67b09ec | 2010-08-27 17:49:42 | [diff] [blame] | 45 | type_(net::DISK_CACHE), memory_only_(false), implementation_(false), |
| 46 | force_creation_(false), new_eviction_(false), first_cleanup_(true), |
| 47 | integrity_(true), use_current_thread_(false), |
| 48 | cache_thread_("CacheThread") {} |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 49 | |
| 50 | void InitCache(); |
| 51 | virtual void TearDown(); |
| 52 | void SimulateCrash(); |
[email protected] | a9da16d | 2008-07-30 21:41:54 | [diff] [blame] | 53 | void SetTestMode(); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 54 | |
| 55 | void SetMemoryOnlyMode() { |
| 56 | memory_only_ = true; |
| 57 | } |
| 58 | |
| 59 | // Use the implementation directly instead of the factory provided object. |
| 60 | void SetDirectMode() { |
| 61 | implementation_ = true; |
| 62 | } |
| 63 | |
| 64 | void SetMask(uint32 mask) { |
| 65 | mask_ = mask; |
| 66 | } |
| 67 | |
| 68 | void SetMaxSize(int size); |
| 69 | |
| 70 | // Deletes and re-creates the files on initialization errors. |
| 71 | void SetForceCreation() { |
| 72 | force_creation_ = true; |
| 73 | } |
| 74 | |
[email protected] | 220d7100 | 2009-04-17 00:50:04 | [diff] [blame] | 75 | void SetNewEviction() { |
| 76 | new_eviction_ = true; |
| 77 | } |
| 78 | |
[email protected] | a9da16d | 2008-07-30 21:41:54 | [diff] [blame] | 79 | void DisableFirstCleanup() { |
| 80 | first_cleanup_ = false; |
| 81 | } |
| 82 | |
[email protected] | 220d7100 | 2009-04-17 00:50:04 | [diff] [blame] | 83 | void DisableIntegrityCheck() { |
| 84 | integrity_ = false; |
| 85 | } |
| 86 | |
[email protected] | fb2622f | 2010-07-13 18:00:56 | [diff] [blame] | 87 | void UseCurrentThread() { |
| 88 | use_current_thread_ = true; |
| 89 | } |
| 90 | |
[email protected] | 67b09ec | 2010-08-27 17:49:42 | [diff] [blame] | 91 | void SetCacheType(net::CacheType type) { |
| 92 | type_ = type; |
| 93 | } |
| 94 | |
[email protected] | bc4fb8e | 2010-03-18 23:58:17 | [diff] [blame] | 95 | // Utility methods to access the cache and wait for each operation to finish. |
| 96 | int OpenEntry(const std::string& key, disk_cache::Entry** entry); |
| 97 | int CreateEntry(const std::string& key, disk_cache::Entry** entry); |
| 98 | int DoomEntry(const std::string& key); |
| 99 | int DoomAllEntries(); |
| 100 | int DoomEntriesBetween(const base::Time initial_time, |
| 101 | const base::Time end_time); |
| 102 | int DoomEntriesSince(const base::Time initial_time); |
| 103 | int OpenNextEntry(void** iter, disk_cache::Entry** next_entry); |
[email protected] | fb2622f | 2010-07-13 18:00:56 | [diff] [blame] | 104 | void FlushQueueForTest(); |
[email protected] | 65188eb | 2010-09-16 20:59:29 | [diff] [blame] | 105 | void RunTaskForTest(Task* task); |
[email protected] | e1fcf14 | 2010-08-23 18:47:25 | [diff] [blame] | 106 | int ReadData(disk_cache::Entry* entry, int index, int offset, |
[email protected] | 67b09ec | 2010-08-27 17:49:42 | [diff] [blame] | 107 | net::IOBuffer* buf, int len); |
[email protected] | e1fcf14 | 2010-08-23 18:47:25 | [diff] [blame] | 108 | int WriteData(disk_cache::Entry* entry, int index, int offset, |
| 109 | net::IOBuffer* buf, int len, bool truncate); |
| 110 | int ReadSparseData(disk_cache::Entry* entry, int64 offset, net::IOBuffer* buf, |
| 111 | int len); |
| 112 | int WriteSparseData(disk_cache::Entry* entry, int64 offset, |
| 113 | net::IOBuffer* buf, int len); |
[email protected] | bc4fb8e | 2010-03-18 23:58:17 | [diff] [blame] | 114 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 115 | // cache_ will always have a valid object, regardless of how the cache was |
| 116 | // initialized. The implementation pointers can be NULL. |
| 117 | disk_cache::Backend* cache_; |
| 118 | disk_cache::BackendImpl* cache_impl_; |
| 119 | disk_cache::MemBackendImpl* mem_cache_; |
| 120 | |
| 121 | uint32 mask_; |
| 122 | int size_; |
[email protected] | 67b09ec | 2010-08-27 17:49:42 | [diff] [blame] | 123 | net::CacheType type_; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 124 | bool memory_only_; |
| 125 | bool implementation_; |
| 126 | bool force_creation_; |
[email protected] | 220d7100 | 2009-04-17 00:50:04 | [diff] [blame] | 127 | bool new_eviction_; |
[email protected] | a9da16d | 2008-07-30 21:41:54 | [diff] [blame] | 128 | bool first_cleanup_; |
[email protected] | 220d7100 | 2009-04-17 00:50:04 | [diff] [blame] | 129 | bool integrity_; |
[email protected] | fb2622f | 2010-07-13 18:00:56 | [diff] [blame] | 130 | bool use_current_thread_; |
[email protected] | 220d7100 | 2009-04-17 00:50:04 | [diff] [blame] | 131 | // This is intentionally left uninitialized, to be used by any test. |
| 132 | bool success_; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 133 | |
| 134 | private: |
| 135 | void InitMemoryCache(); |
| 136 | void InitDiskCache(); |
[email protected] | cfaa1f2 | 2009-10-12 17:14:59 | [diff] [blame] | 137 | void InitDiskCacheImpl(const FilePath& path); |
[email protected] | ec44a9a | 2010-06-15 19:31:51 | [diff] [blame] | 138 | |
| 139 | base::Thread cache_thread_; |
| 140 | DISALLOW_COPY_AND_ASSIGN(DiskCacheTestWithCache); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 141 | }; |
| 142 | |
[email protected] | 4d5e036 | 2008-08-28 00:59:06 | [diff] [blame] | 143 | #endif // NET_DISK_CACHE_DISK_CACHE_TEST_BASE_H_ |