| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 #ifndef NET_DISK_CACHE_DISK_CACHE_TEST_BASE_H_ | 5 #ifndef NET_DISK_CACHE_DISK_CACHE_TEST_BASE_H_ |
| 6 #define NET_DISK_CACHE_DISK_CACHE_TEST_BASE_H_ | 6 #define NET_DISK_CACHE_DISK_CACHE_TEST_BASE_H_ |
| 7 | 7 |
| 8 #include "base/basictypes.h" | 8 #include "base/basictypes.h" |
| 9 #include "base/files/file_path.h" | 9 #include "base/files/file_path.h" |
| 10 #include "base/files/scoped_temp_dir.h" | 10 #include "base/files/scoped_temp_dir.h" |
| 11 #include "base/memory/scoped_ptr.h" | 11 #include "base/memory/scoped_ptr.h" |
| 12 #include "base/threading/thread.h" | 12 #include "base/threading/thread.h" |
| 13 #include "base/time.h" |
| 13 #include "net/base/cache_type.h" | 14 #include "net/base/cache_type.h" |
| 14 #include "testing/gtest/include/gtest/gtest.h" | 15 #include "testing/gtest/include/gtest/gtest.h" |
| 15 #include "testing/platform_test.h" | 16 #include "testing/platform_test.h" |
| 16 | 17 |
| 17 namespace net { | 18 namespace net { |
| 18 | 19 |
| 19 class IOBuffer; | 20 class IOBuffer; |
| 20 | 21 |
| 21 } // namespace net | 22 } // namespace net |
| 22 | 23 |
| 23 namespace disk_cache { | 24 namespace disk_cache { |
| 24 | 25 |
| 25 class Backend; | 26 class Backend; |
| 26 class BackendImpl; | 27 class BackendImpl; |
| 28 class BackendImplV3; |
| 27 class Entry; | 29 class Entry; |
| 28 class MemBackendImpl; | 30 class MemBackendImpl; |
| 29 class SimpleBackendImpl; | 31 class SimpleBackendImpl; |
| 30 | 32 |
| 31 } // namespace disk_cache | 33 } // namespace disk_cache |
| 32 | 34 |
| 33 // These tests can use the path service, which uses autoreleased objects on the | 35 // These tests can use the path service, which uses autoreleased objects on the |
| 34 // Mac, so this needs to be a PlatformTest. Even tests that do not require a | 36 // Mac, so this needs to be a PlatformTest. Even tests that do not require a |
| 35 // cache (and that do not need to be a DiskCacheTestWithCache) are susceptible | 37 // cache (and that do not need to be a DiskCacheTestWithCache) are susceptible |
| 36 // to this problem; all such tests should use TEST_F(DiskCacheTest, ...). | 38 // to this problem; all such tests should use TEST_F(DiskCacheTest, ...). |
| 37 class DiskCacheTest : public PlatformTest { | 39 class DiskCacheTest : public PlatformTest { |
| 38 protected: | 40 protected: |
| 39 DiskCacheTest(); | 41 DiskCacheTest(); |
| 40 virtual ~DiskCacheTest(); | 42 virtual ~DiskCacheTest(); |
| 41 | 43 |
| 42 // Copies a set of cache files from the data folder to the test folder. | 44 // Copies a set of cache files from the data folder to the test folder. |
| 43 bool CopyTestCache(const std::string& name); | 45 bool CopyTestCache(const std::string& name); |
| 44 | 46 |
| 45 // Deletes the contents of |cache_path_|. | 47 // Deletes the contents of |cache_path_|. |
| 46 bool CleanupCacheDir(); | 48 bool CleanupCacheDir(); |
| 47 | 49 |
| 50 // Forces the use of a random cache directory. |
| 51 bool ForceRandomCacheDir(); |
| 52 |
| 48 virtual void TearDown() OVERRIDE; | 53 virtual void TearDown() OVERRIDE; |
| 49 | 54 |
| 50 base::FilePath cache_path_; | 55 base::FilePath cache_path_; |
| 51 | 56 |
| 52 private: | 57 private: |
| 53 base::ScopedTempDir temp_dir_; | 58 base::ScopedTempDir temp_dir_; |
| 54 scoped_ptr<MessageLoop> message_loop_; | 59 scoped_ptr<MessageLoop> message_loop_; |
| 55 }; | 60 }; |
| 56 | 61 |
| 57 // Provides basic support for cache related tests. | 62 // Provides basic support for cache related tests. |
| 58 class DiskCacheTestWithCache : public DiskCacheTest { | 63 class DiskCacheTestWithCache : public DiskCacheTest { |
| 59 protected: | 64 protected: |
| 60 DiskCacheTestWithCache(); | 65 DiskCacheTestWithCache(); |
| 61 virtual ~DiskCacheTestWithCache(); | 66 virtual ~DiskCacheTestWithCache(); |
| 62 | 67 |
| 63 void CreateBackend(uint32 flags, base::Thread* thread); | 68 void CreateBackend(base::Thread* thread); |
| 64 | 69 |
| 65 void InitCache(); | 70 void InitCache(); |
| 66 void SimulateCrash(); | 71 void SimulateCrash(); |
| 67 void SetTestMode(); | 72 void SetTestMode(); |
| 68 | 73 |
| 74 // Same as SetTestMode except that sets the flag on contruction. |
| 75 void PresetTestMode() { |
| 76 EXPECT_FALSE(cache_); |
| 77 unit_test_mode_ = true; |
| 78 } |
| 79 |
| 69 void SetMemoryOnlyMode() { | 80 void SetMemoryOnlyMode() { |
| 81 EXPECT_FALSE(cache_); |
| 70 memory_only_ = true; | 82 memory_only_ = true; |
| 71 } | 83 } |
| 72 | 84 |
| 73 void SetSimpleCacheMode() { | 85 void SetSimpleCacheMode() { |
| 86 EXPECT_FALSE(cache_); |
| 74 simple_cache_mode_ = true; | 87 simple_cache_mode_ = true; |
| 75 } | 88 } |
| 76 | 89 |
| 77 void SetMask(uint32 mask) { | 90 void SetMask(uint32 mask) { |
| 91 EXPECT_FALSE(cache_); |
| 78 mask_ = mask; | 92 mask_ = mask; |
| 79 } | 93 } |
| 80 | 94 |
| 81 void SetMaxSize(int size); | 95 void SetMaxSize(int size); |
| 82 | 96 |
| 83 // Deletes and re-creates the files on initialization errors. | 97 // Deletes and re-creates the files on initialization errors. |
| 84 void SetForceCreation() { | 98 void SetForceCreation() { |
| 99 EXPECT_FALSE(cache_); |
| 85 force_creation_ = true; | 100 force_creation_ = true; |
| 86 } | 101 } |
| 87 | 102 |
| 88 void SetNewEviction() { | 103 void SetNewEviction() { |
| 104 EXPECT_FALSE(cache_); |
| 89 new_eviction_ = true; | 105 new_eviction_ = true; |
| 90 } | 106 } |
| 91 | 107 |
| 108 void UseVersion3() { |
| 109 EXPECT_FALSE(cache_); |
| 110 v3_ = true; |
| 111 } |
| 112 |
| 92 void DisableFirstCleanup() { | 113 void DisableFirstCleanup() { |
| 114 EXPECT_FALSE(cache_); |
| 93 first_cleanup_ = false; | 115 first_cleanup_ = false; |
| 94 } | 116 } |
| 95 | 117 |
| 96 void DisableIntegrityCheck() { | 118 void DisableIntegrityCheck() { |
| 97 integrity_ = false; | 119 integrity_ = false; |
| 98 } | 120 } |
| 99 | 121 |
| 100 void UseCurrentThread() { | 122 void UseCurrentThread() { |
| 123 EXPECT_FALSE(cache_); |
| 101 use_current_thread_ = true; | 124 use_current_thread_ = true; |
| 102 } | 125 } |
| 103 | 126 |
| 127 void AvoidTestFlag() { |
| 128 EXPECT_FALSE(cache_); |
| 129 EXPECT_FALSE(v3_); |
| 130 avoid_test_flag_ = true; |
| 131 } |
| 132 |
| 104 void SetCacheType(net::CacheType type) { | 133 void SetCacheType(net::CacheType type) { |
| 134 EXPECT_FALSE(cache_); |
| 105 type_ = type; | 135 type_ = type; |
| 106 } | 136 } |
| 107 | 137 |
| 108 // Utility methods to access the cache and wait for each operation to finish. | 138 // Utility methods to access the cache and wait for each operation to finish. |
| 109 int OpenEntry(const std::string& key, disk_cache::Entry** entry); | 139 int OpenEntry(const std::string& key, disk_cache::Entry** entry); |
| 110 int CreateEntry(const std::string& key, disk_cache::Entry** entry); | 140 int CreateEntry(const std::string& key, disk_cache::Entry** entry); |
| 111 int DoomEntry(const std::string& key); | 141 int DoomEntry(const std::string& key); |
| 112 int DoomAllEntries(); | 142 int DoomAllEntries(); |
| 113 int DoomEntriesBetween(const base::Time initial_time, | 143 int DoomEntriesBetween(const base::Time initial_time, |
| 114 const base::Time end_time); | 144 const base::Time end_time); |
| 115 int DoomEntriesSince(const base::Time initial_time); | 145 int DoomEntriesSince(const base::Time initial_time); |
| 116 int OpenNextEntry(void** iter, disk_cache::Entry** next_entry); | 146 int OpenNextEntry(void** iter, disk_cache::Entry** next_entry); |
| 117 void FlushQueueForTest(); | 147 void FlushQueueForTest(); |
| 148 void CleanupForTest(); |
| 118 void RunTaskForTest(const base::Closure& closure); | 149 void RunTaskForTest(const base::Closure& closure); |
| 150 |
| 119 int ReadData(disk_cache::Entry* entry, int index, int offset, | 151 int ReadData(disk_cache::Entry* entry, int index, int offset, |
| 120 net::IOBuffer* buf, int len); | 152 net::IOBuffer* buf, int len); |
| 121 int WriteData(disk_cache::Entry* entry, int index, int offset, | 153 int WriteData(disk_cache::Entry* entry, int index, int offset, |
| 122 net::IOBuffer* buf, int len, bool truncate); | 154 net::IOBuffer* buf, int len, bool truncate); |
| 123 int ReadSparseData(disk_cache::Entry* entry, int64 offset, net::IOBuffer* buf, | 155 int ReadSparseData(disk_cache::Entry* entry, int64 offset, net::IOBuffer* buf, |
| 124 int len); | 156 int len); |
| 125 int WriteSparseData(disk_cache::Entry* entry, int64 offset, | 157 int WriteSparseData(disk_cache::Entry* entry, int64 offset, |
| 126 net::IOBuffer* buf, int len); | 158 net::IOBuffer* buf, int len); |
| 159 int GetAvailableRange(disk_cache::Entry* entry, int64 offset, int len, |
| 160 int64* start); |
| 161 |
| 162 bool IsAllocAllowed(int current_size, int new_size); |
| 163 void BufferDeleted(int size); |
| 164 int GetTotalBuffersSize(); |
| 165 void SetNoBuffering(); |
| 166 void WaitForEntryToClose(const std::string& key); |
| 127 | 167 |
| 128 // Asks the cache to trim an entry. If |empty| is true, the whole cache is | 168 // Asks the cache to trim an entry. If |empty| is true, the whole cache is |
| 129 // deleted. | 169 // deleted. |
| 130 void TrimForTest(bool empty); | 170 void TrimForTest(bool empty); |
| 131 | 171 |
| 132 // Asks the cache to trim an entry from the deleted list. If |empty| is | 172 // Asks the cache to trim an entry from the deleted list. If |empty| is |
| 133 // true, the whole list is deleted. | 173 // true, the whole list is deleted. |
| 134 void TrimDeletedListForTest(bool empty); | 174 void TrimDeletedListForTest(bool empty); |
| 135 | 175 |
| 176 // Returns the current time as seen by the backend. |
| 177 base::Time GetCurrentTime(); |
| 178 |
| 179 // Simulates that some |seconds| have passed before proceeding with a test. |
| 180 void AddDelayForTest(int seconds); |
| 181 |
| 136 // Makes sure that some time passes before continuing the test. Time::Now() | 182 // Makes sure that some time passes before continuing the test. Time::Now() |
| 137 // before and after this method will not be the same. | 183 // before and after this method will not be the same. |
| 138 void AddDelay(); | 184 void AddDelay(); |
| 139 | 185 |
| 140 // DiskCacheTest: | 186 // DiskCacheTest: |
| 141 virtual void TearDown() OVERRIDE; | 187 virtual void TearDown() OVERRIDE; |
| 142 | 188 |
| 143 // cache_ will always have a valid object, regardless of how the cache was | 189 // cache_ will always have a valid object, regardless of how the cache was |
| 144 // initialized. The implementation pointers can be NULL. | 190 // initialized. The implementation pointers can be NULL. |
| 145 disk_cache::Backend* cache_; | 191 disk_cache::Backend* cache_; |
| 146 disk_cache::BackendImpl* cache_impl_; | 192 disk_cache::BackendImpl* cache_impl_; |
| 147 disk_cache::SimpleBackendImpl* simple_cache_impl_; | 193 disk_cache::SimpleBackendImpl* simple_cache_impl_; |
| 148 disk_cache::MemBackendImpl* mem_cache_; | 194 disk_cache::MemBackendImpl* mem_cache_; |
| 195 disk_cache::BackendImplV3* cache_impl_v3_; |
| 149 | 196 |
| 150 uint32 mask_; | 197 uint32 mask_; |
| 151 int size_; | 198 int size_; |
| 152 net::CacheType type_; | 199 net::CacheType type_; |
| 153 bool memory_only_; | 200 bool memory_only_; |
| 154 bool simple_cache_mode_; | 201 bool simple_cache_mode_; |
| 155 bool force_creation_; | 202 bool force_creation_; |
| 156 bool new_eviction_; | 203 bool new_eviction_; |
| 204 bool v3_; |
| 157 bool first_cleanup_; | 205 bool first_cleanup_; |
| 158 bool integrity_; | 206 bool integrity_; |
| 159 bool use_current_thread_; | 207 bool use_current_thread_; |
| 208 bool avoid_test_flag_; |
| 209 bool unit_test_mode_; |
| 160 // This is intentionally left uninitialized, to be used by any test. | 210 // This is intentionally left uninitialized, to be used by any test. |
| 161 bool success_; | 211 bool success_; |
| 162 | 212 |
| 163 private: | 213 private: |
| 164 void InitMemoryCache(); | 214 void InitMemoryCache(); |
| 165 void InitDiskCache(); | 215 void InitDiskCache(); |
| 166 | 216 |
| 167 base::Thread cache_thread_; | 217 base::Thread cache_thread_; |
| 168 DISALLOW_COPY_AND_ASSIGN(DiskCacheTestWithCache); | 218 DISALLOW_COPY_AND_ASSIGN(DiskCacheTestWithCache); |
| 169 }; | 219 }; |
| 170 | 220 |
| 171 #endif // NET_DISK_CACHE_DISK_CACHE_TEST_BASE_H_ | 221 #endif // NET_DISK_CACHE_DISK_CACHE_TEST_BASE_H_ |
| OLD | NEW |