[email protected] | 05f8087d | 2012-06-29 18:58:37 | [diff] [blame] | 1 | // Copyright (c) 2012 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_ |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 7 | |
Avi Drissman | 13fc893 | 2015-12-20 04:40:46 | [diff] [blame] | 8 | #include <stdint.h> |
| 9 | |
danakj | d04b92d | 2016-04-16 01:16:49 | [diff] [blame] | 10 | #include <memory> |
| 11 | |
[email protected] | 5799981 | 2013-02-24 05:40:52 | [diff] [blame] | 12 | #include "base/files/file_path.h" |
[email protected] | ea1a3f6 | 2012-11-16 20:34:23 | [diff] [blame] | 13 | #include "base/files/scoped_temp_dir.h" |
Keishi Hattori | 0e45c02 | 2021-11-27 09:25:52 | [diff] [blame] | 14 | #include "base/memory/raw_ptr.h" |
[email protected] | 34b9963 | 2011-01-01 01:01:06 | [diff] [blame] | 15 | #include "base/threading/thread.h" |
[email protected] | 67b09ec | 2010-08-27 17:49:42 | [diff] [blame] | 16 | #include "net/base/cache_type.h" |
gavinp | b1aaa05 | 2014-09-24 14:54:35 | [diff] [blame] | 17 | #include "net/disk_cache/disk_cache.h" |
Gabriel Charette | c710874 | 2019-08-23 03:31:40 | [diff] [blame] | 18 | #include "net/test/test_with_task_environment.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 19 | #include "testing/gtest/include/gtest/gtest.h" |
[email protected] | 23887f04f | 2008-12-02 19:20:15 | [diff] [blame] | 20 | #include "testing/platform_test.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 21 | |
[email protected] | e1fcf14 | 2010-08-23 18:47:25 | [diff] [blame] | 22 | namespace net { |
| 23 | |
| 24 | class IOBuffer; |
| 25 | |
| 26 | } // namespace net |
| 27 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 28 | namespace disk_cache { |
| 29 | |
| 30 | class Backend; |
| 31 | class BackendImpl; |
[email protected] | bc4fb8e | 2010-03-18 23:58:17 | [diff] [blame] | 32 | class Entry; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 33 | class MemBackendImpl; |
[email protected] | 1ed9575 | 2013-04-23 00:12:36 | [diff] [blame] | 34 | class SimpleBackendImpl; |
Maks Orlovich | f378b3a | 2017-10-31 16:27:30 | [diff] [blame] | 35 | class SimpleFileTracker; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 36 | |
[email protected] | 4d5e036 | 2008-08-28 00:59:06 | [diff] [blame] | 37 | } // namespace disk_cache |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 38 | |
[email protected] | 17b8914 | 2008-11-07 21:52:15 | [diff] [blame] | 39 | // These tests can use the path service, which uses autoreleased objects on the |
| 40 | // Mac, so this needs to be a PlatformTest. Even tests that do not require a |
| 41 | // cache (and that do not need to be a DiskCacheTestWithCache) are susceptible |
| 42 | // to this problem; all such tests should use TEST_F(DiskCacheTest, ...). |
Gabriel Charette | 694c3c33 | 2019-08-19 14:53:05 | [diff] [blame] | 43 | class DiskCacheTest : public PlatformTest, public net::WithTaskEnvironment { |
[email protected] | 79b3fec | 2011-11-15 22:33:47 | [diff] [blame] | 44 | protected: |
| 45 | DiskCacheTest(); |
dcheng | 67be2b1f | 2014-10-27 21:47:29 | [diff] [blame] | 46 | ~DiskCacheTest() override; |
[email protected] | 79b3fec | 2011-11-15 22:33:47 | [diff] [blame] | 47 | |
| 48 | // Copies a set of cache files from the data folder to the test folder. |
| 49 | bool CopyTestCache(const std::string& name); |
| 50 | |
| 51 | // Deletes the contents of |cache_path_|. |
| 52 | bool CleanupCacheDir(); |
| 53 | |
dcheng | 67be2b1f | 2014-10-27 21:47:29 | [diff] [blame] | 54 | void TearDown() override; |
[email protected] | 79b3fec | 2011-11-15 22:33:47 | [diff] [blame] | 55 | |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 56 | base::FilePath cache_path_; |
[email protected] | 79b3fec | 2011-11-15 22:33:47 | [diff] [blame] | 57 | |
| 58 | private: |
[email protected] | ea1a3f6 | 2012-11-16 20:34:23 | [diff] [blame] | 59 | base::ScopedTempDir temp_dir_; |
[email protected] | 17b8914 | 2008-11-07 21:52:15 | [diff] [blame] | 60 | }; |
| 61 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 62 | // Provides basic support for cache related tests. |
[email protected] | 4d5e036 | 2008-08-28 00:59:06 | [diff] [blame] | 63 | class DiskCacheTestWithCache : public DiskCacheTest { |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 64 | protected: |
gavinp | b1aaa05 | 2014-09-24 14:54:35 | [diff] [blame] | 65 | class TestIterator { |
| 66 | public: |
danakj | d04b92d | 2016-04-16 01:16:49 | [diff] [blame] | 67 | explicit TestIterator( |
| 68 | std::unique_ptr<disk_cache::Backend::Iterator> iterator); |
gavinp | b1aaa05 | 2014-09-24 14:54:35 | [diff] [blame] | 69 | ~TestIterator(); |
| 70 | |
| 71 | int OpenNextEntry(disk_cache::Entry** next_entry); |
| 72 | |
| 73 | private: |
danakj | d04b92d | 2016-04-16 01:16:49 | [diff] [blame] | 74 | std::unique_ptr<disk_cache::Backend::Iterator> iterator_; |
gavinp | b1aaa05 | 2014-09-24 14:54:35 | [diff] [blame] | 75 | }; |
| 76 | |
[email protected] | 7aefb15 | 2011-01-21 23:46:49 | [diff] [blame] | 77 | DiskCacheTestWithCache(); |
Peter Boström | 407869b | 2021-10-07 04:42:48 | [diff] [blame] | 78 | |
| 79 | DiskCacheTestWithCache(const DiskCacheTestWithCache&) = delete; |
| 80 | DiskCacheTestWithCache& operator=(const DiskCacheTestWithCache&) = delete; |
| 81 | |
dcheng | 67be2b1f | 2014-10-27 21:47:29 | [diff] [blame] | 82 | ~DiskCacheTestWithCache() override; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 83 | |
Maks Orlovich | 463dc50 | 2017-07-25 14:52:49 | [diff] [blame] | 84 | void CreateBackend(uint32_t flags); |
[email protected] | 398ad13 | 2013-04-02 15:11:01 | [diff] [blame] | 85 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 86 | void InitCache(); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 87 | void SimulateCrash(); |
[email protected] | a9da16d | 2008-07-30 21:41:54 | [diff] [blame] | 88 | void SetTestMode(); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 89 | |
| 90 | void SetMemoryOnlyMode() { |
| 91 | memory_only_ = true; |
| 92 | } |
| 93 | |
[email protected] | 398ad13 | 2013-04-02 15:11:01 | [diff] [blame] | 94 | void SetSimpleCacheMode() { |
Maks Orlovich | 922db0c | 2018-02-21 15:28:46 | [diff] [blame] | 95 | DCHECK(!use_current_thread_); |
[email protected] | 398ad13 | 2013-04-02 15:11:01 | [diff] [blame] | 96 | simple_cache_mode_ = true; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 97 | } |
| 98 | |
Avi Drissman | 13fc893 | 2015-12-20 04:40:46 | [diff] [blame] | 99 | void SetMask(uint32_t mask) { mask_ = mask; } |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 100 | |
Ben Kelly | 5a16ed0 | 2018-09-07 21:28:09 | [diff] [blame] | 101 | void SetMaxSize(int64_t size, bool should_succeed = true); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 102 | |
Maks Orlovich | f386065 | 2017-12-13 18:03:16 | [diff] [blame] | 103 | // Returns value last given to SetMaxSize (or 0). |
| 104 | int MaxSize() const { return size_; } |
| 105 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 106 | // Deletes and re-creates the files on initialization errors. |
| 107 | void SetForceCreation() { |
| 108 | force_creation_ = true; |
| 109 | } |
| 110 | |
[email protected] | 220d7100 | 2009-04-17 00:50:04 | [diff] [blame] | 111 | void SetNewEviction() { |
| 112 | new_eviction_ = true; |
| 113 | } |
| 114 | |
[email protected] | 7e48c1b | 2013-07-12 12:15:43 | [diff] [blame] | 115 | void DisableSimpleCacheWaitForIndex() { |
| 116 | simple_cache_wait_for_index_ = false; |
| 117 | } |
| 118 | |
[email protected] | a9da16d | 2008-07-30 21:41:54 | [diff] [blame] | 119 | void DisableFirstCleanup() { |
| 120 | first_cleanup_ = false; |
| 121 | } |
| 122 | |
[email protected] | 220d7100 | 2009-04-17 00:50:04 | [diff] [blame] | 123 | void DisableIntegrityCheck() { |
| 124 | integrity_ = false; |
| 125 | } |
| 126 | |
Maks Orlovich | 922db0c | 2018-02-21 15:28:46 | [diff] [blame] | 127 | // This is only supported for blockfile cache. |
[email protected] | fb2622f | 2010-07-13 18:00:56 | [diff] [blame] | 128 | void UseCurrentThread() { |
Maks Orlovich | 922db0c | 2018-02-21 15:28:46 | [diff] [blame] | 129 | DCHECK(!simple_cache_mode_); |
[email protected] | fb2622f | 2010-07-13 18:00:56 | [diff] [blame] | 130 | use_current_thread_ = true; |
| 131 | } |
| 132 | |
[email protected] | 67b09ec | 2010-08-27 17:49:42 | [diff] [blame] | 133 | void SetCacheType(net::CacheType type) { |
| 134 | type_ = type; |
| 135 | } |
| 136 | |
[email protected] | bc4fb8e | 2010-03-18 23:58:17 | [diff] [blame] | 137 | // Utility methods to access the cache and wait for each operation to finish. |
Maks Orlovich | 8efea48 | 2019-08-20 17:14:53 | [diff] [blame] | 138 | // Also closer to legacy API. |
| 139 | // TODO(morlovich): Port all the tests to EntryResult. |
| 140 | disk_cache::EntryResult OpenOrCreateEntry(const std::string& key); |
| 141 | disk_cache::EntryResult OpenOrCreateEntryWithPriority( |
| 142 | const std::string& key, |
| 143 | net::RequestPriority request_priority); |
[email protected] | bc4fb8e | 2010-03-18 23:58:17 | [diff] [blame] | 144 | int OpenEntry(const std::string& key, disk_cache::Entry** entry); |
Josh Karlin | dd9a5d14 | 2018-06-06 00:35:48 | [diff] [blame] | 145 | int OpenEntryWithPriority(const std::string& key, |
| 146 | net::RequestPriority request_priority, |
| 147 | disk_cache::Entry** entry); |
[email protected] | bc4fb8e | 2010-03-18 23:58:17 | [diff] [blame] | 148 | int CreateEntry(const std::string& key, disk_cache::Entry** entry); |
Josh Karlin | dd9a5d14 | 2018-06-06 00:35:48 | [diff] [blame] | 149 | int CreateEntryWithPriority(const std::string& key, |
| 150 | net::RequestPriority request_priority, |
| 151 | disk_cache::Entry** entry); |
[email protected] | bc4fb8e | 2010-03-18 23:58:17 | [diff] [blame] | 152 | int DoomEntry(const std::string& key); |
| 153 | int DoomAllEntries(); |
| 154 | int DoomEntriesBetween(const base::Time initial_time, |
| 155 | const base::Time end_time); |
Ben Kelly | 5a16ed0 | 2018-09-07 21:28:09 | [diff] [blame] | 156 | int64_t CalculateSizeOfAllEntries(); |
| 157 | int64_t CalculateSizeOfEntriesBetween(const base::Time initial_time, |
| 158 | const base::Time end_time); |
[email protected] | bc4fb8e | 2010-03-18 23:58:17 | [diff] [blame] | 159 | int DoomEntriesSince(const base::Time initial_time); |
danakj | d04b92d | 2016-04-16 01:16:49 | [diff] [blame] | 160 | std::unique_ptr<TestIterator> CreateIterator(); |
[email protected] | fb2622f | 2010-07-13 18:00:56 | [diff] [blame] | 161 | void FlushQueueForTest(); |
Anna Malova | cb042b64 | 2020-03-03 15:50:00 | [diff] [blame] | 162 | void RunTaskForTest(base::OnceClosure closure); |
[email protected] | e1fcf14 | 2010-08-23 18:47:25 | [diff] [blame] | 163 | int ReadData(disk_cache::Entry* entry, int index, int offset, |
[email protected] | 67b09ec | 2010-08-27 17:49:42 | [diff] [blame] | 164 | net::IOBuffer* buf, int len); |
[email protected] | e1fcf14 | 2010-08-23 18:47:25 | [diff] [blame] | 165 | int WriteData(disk_cache::Entry* entry, int index, int offset, |
| 166 | net::IOBuffer* buf, int len, bool truncate); |
Avi Drissman | 13fc893 | 2015-12-20 04:40:46 | [diff] [blame] | 167 | int ReadSparseData(disk_cache::Entry* entry, |
| 168 | int64_t offset, |
| 169 | net::IOBuffer* buf, |
[email protected] | e1fcf14 | 2010-08-23 18:47:25 | [diff] [blame] | 170 | int len); |
Avi Drissman | 13fc893 | 2015-12-20 04:40:46 | [diff] [blame] | 171 | int WriteSparseData(disk_cache::Entry* entry, |
| 172 | int64_t offset, |
| 173 | net::IOBuffer* buf, |
| 174 | int len); |
Maks Orlovich | 04cd1ad | 2021-07-02 17:32:24 | [diff] [blame] | 175 | // TODO(morlovich): Port all the tests using this to RangeResult. |
Matthew Denton | 29cbe0ee | 2019-03-27 00:10:41 | [diff] [blame] | 176 | int GetAvailableRange(disk_cache::Entry* entry, |
| 177 | int64_t offset, |
| 178 | int len, |
| 179 | int64_t* start); |
[email protected] | bc4fb8e | 2010-03-18 23:58:17 | [diff] [blame] | 180 | |
[email protected] | 30f02c43 | 2011-05-10 22:27:06 | [diff] [blame] | 181 | // Asks the cache to trim an entry. If |empty| is true, the whole cache is |
[email protected] | ceb61da3 | 2011-01-25 23:52:02 | [diff] [blame] | 182 | // deleted. |
| 183 | void TrimForTest(bool empty); |
| 184 | |
[email protected] | 30f02c43 | 2011-05-10 22:27:06 | [diff] [blame] | 185 | // Asks the cache to trim an entry from the deleted list. If |empty| is |
| 186 | // true, the whole list is deleted. |
[email protected] | ceb61da3 | 2011-01-25 23:52:02 | [diff] [blame] | 187 | void TrimDeletedListForTest(bool empty); |
| 188 | |
[email protected] | 05f8087d | 2012-06-29 18:58:37 | [diff] [blame] | 189 | // Makes sure that some time passes before continuing the test. Time::Now() |
| 190 | // before and after this method will not be the same. |
| 191 | void AddDelay(); |
| 192 | |
Matthew Denton | 29cbe0ee | 2019-03-27 00:10:41 | [diff] [blame] | 193 | void OnExternalCacheHit(const std::string& key); |
| 194 | |
dcheng | 67be2b1f | 2014-10-27 21:47:29 | [diff] [blame] | 195 | void TearDown() override; |
[email protected] | 7aefb15 | 2011-01-21 23:46:49 | [diff] [blame] | 196 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 197 | // cache_ will always have a valid object, regardless of how the cache was |
| 198 | // initialized. The implementation pointers can be NULL. |
danakj | d04b92d | 2016-04-16 01:16:49 | [diff] [blame] | 199 | std::unique_ptr<disk_cache::Backend> cache_; |
Tsuyoshi Horo | 432981d5 | 2022-06-09 09:50:13 | [diff] [blame] | 200 | raw_ptr<disk_cache::BackendImpl> cache_impl_ = nullptr; |
Maks Orlovich | f378b3a | 2017-10-31 16:27:30 | [diff] [blame] | 201 | std::unique_ptr<disk_cache::SimpleFileTracker> simple_file_tracker_; |
Tsuyoshi Horo | 432981d5 | 2022-06-09 09:50:13 | [diff] [blame] | 202 | raw_ptr<disk_cache::SimpleBackendImpl> simple_cache_impl_ = nullptr; |
| 203 | raw_ptr<disk_cache::MemBackendImpl> mem_cache_ = nullptr; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 204 | |
Tsuyoshi Horo | e0235ed6 | 2022-06-09 01:42:30 | [diff] [blame] | 205 | uint32_t mask_ = 0; |
| 206 | int64_t size_ = 0; |
| 207 | net::CacheType type_ = net::DISK_CACHE; |
| 208 | bool memory_only_ = false; |
| 209 | bool simple_cache_mode_ = false; |
| 210 | bool simple_cache_wait_for_index_ = true; |
| 211 | bool force_creation_ = false; |
| 212 | bool new_eviction_ = false; |
| 213 | bool first_cleanup_ = true; |
| 214 | bool integrity_ = true; |
| 215 | bool use_current_thread_ = false; |
[email protected] | 220d7100 | 2009-04-17 00:50:04 | [diff] [blame] | 216 | // This is intentionally left uninitialized, to be used by any test. |
| 217 | bool success_; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 218 | |
| 219 | private: |
| 220 | void InitMemoryCache(); |
| 221 | void InitDiskCache(); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 222 | }; |
| 223 | |
[email protected] | 4d5e036 | 2008-08-28 00:59:06 | [diff] [blame] | 224 | #endif // NET_DISK_CACHE_DISK_CACHE_TEST_BASE_H_ |