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