[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" |
Avi Drissman | 13fc893 | 2015-12-20 04:40:46 | [diff] [blame] | 14 | #include "base/macros.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" |
Bence Béky | 98447b1 | 2018-05-08 03:14:01 | [diff] [blame] | 18 | #include "net/test/test_with_scoped_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, ...). |
Bence Béky | 98447b1 | 2018-05-08 03:14:01 | [diff] [blame] | 43 | class DiskCacheTest : public PlatformTest, |
| 44 | public net::WithScopedTaskEnvironment { |
[email protected] | 79b3fec | 2011-11-15 22:33:47 | [diff] [blame] | 45 | protected: |
| 46 | DiskCacheTest(); |
dcheng | 67be2b1f | 2014-10-27 21:47:29 | [diff] [blame] | 47 | ~DiskCacheTest() override; |
[email protected] | 79b3fec | 2011-11-15 22:33:47 | [diff] [blame] | 48 | |
| 49 | // Copies a set of cache files from the data folder to the test folder. |
| 50 | bool CopyTestCache(const std::string& name); |
| 51 | |
| 52 | // Deletes the contents of |cache_path_|. |
| 53 | bool CleanupCacheDir(); |
| 54 | |
dcheng | 67be2b1f | 2014-10-27 21:47:29 | [diff] [blame] | 55 | void TearDown() override; |
[email protected] | 79b3fec | 2011-11-15 22:33:47 | [diff] [blame] | 56 | |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 57 | base::FilePath cache_path_; |
[email protected] | 79b3fec | 2011-11-15 22:33:47 | [diff] [blame] | 58 | |
| 59 | private: |
[email protected] | ea1a3f6 | 2012-11-16 20:34:23 | [diff] [blame] | 60 | base::ScopedTempDir temp_dir_; |
[email protected] | 17b8914 | 2008-11-07 21:52:15 | [diff] [blame] | 61 | }; |
| 62 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 63 | // Provides basic support for cache related tests. |
[email protected] | 4d5e036 | 2008-08-28 00:59:06 | [diff] [blame] | 64 | class DiskCacheTestWithCache : public DiskCacheTest { |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 65 | protected: |
gavinp | b1aaa05 | 2014-09-24 14:54:35 | [diff] [blame] | 66 | class TestIterator { |
| 67 | public: |
danakj | d04b92d | 2016-04-16 01:16:49 | [diff] [blame] | 68 | explicit TestIterator( |
| 69 | std::unique_ptr<disk_cache::Backend::Iterator> iterator); |
gavinp | b1aaa05 | 2014-09-24 14:54:35 | [diff] [blame] | 70 | ~TestIterator(); |
| 71 | |
| 72 | int OpenNextEntry(disk_cache::Entry** next_entry); |
| 73 | |
| 74 | private: |
danakj | d04b92d | 2016-04-16 01:16:49 | [diff] [blame] | 75 | std::unique_ptr<disk_cache::Backend::Iterator> iterator_; |
gavinp | b1aaa05 | 2014-09-24 14:54:35 | [diff] [blame] | 76 | }; |
| 77 | |
[email protected] | 7aefb15 | 2011-01-21 23:46:49 | [diff] [blame] | 78 | DiskCacheTestWithCache(); |
dcheng | 67be2b1f | 2014-10-27 21:47:29 | [diff] [blame] | 79 | ~DiskCacheTestWithCache() override; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 80 | |
Maks Orlovich | 463dc50 | 2017-07-25 14:52:49 | [diff] [blame] | 81 | void CreateBackend(uint32_t flags); |
[email protected] | 398ad13 | 2013-04-02 15:11:01 | [diff] [blame] | 82 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 83 | void InitCache(); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 84 | void SimulateCrash(); |
[email protected] | a9da16d | 2008-07-30 21:41:54 | [diff] [blame] | 85 | void SetTestMode(); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 86 | |
| 87 | void SetMemoryOnlyMode() { |
| 88 | memory_only_ = true; |
| 89 | } |
| 90 | |
[email protected] | 398ad13 | 2013-04-02 15:11:01 | [diff] [blame] | 91 | void SetSimpleCacheMode() { |
Maks Orlovich | 922db0c | 2018-02-21 15:28:46 | [diff] [blame] | 92 | DCHECK(!use_current_thread_); |
[email protected] | 398ad13 | 2013-04-02 15:11:01 | [diff] [blame] | 93 | simple_cache_mode_ = true; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 94 | } |
| 95 | |
Avi Drissman | 13fc893 | 2015-12-20 04:40:46 | [diff] [blame] | 96 | void SetMask(uint32_t mask) { mask_ = mask; } |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 97 | |
| 98 | void SetMaxSize(int size); |
| 99 | |
Maks Orlovich | f386065 | 2017-12-13 18:03:16 | [diff] [blame] | 100 | // Returns value last given to SetMaxSize (or 0). |
| 101 | int MaxSize() const { return size_; } |
| 102 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 103 | // Deletes and re-creates the files on initialization errors. |
| 104 | void SetForceCreation() { |
| 105 | force_creation_ = true; |
| 106 | } |
| 107 | |
[email protected] | 220d7100 | 2009-04-17 00:50:04 | [diff] [blame] | 108 | void SetNewEviction() { |
| 109 | new_eviction_ = true; |
| 110 | } |
| 111 | |
[email protected] | 7e48c1b | 2013-07-12 12:15:43 | [diff] [blame] | 112 | void DisableSimpleCacheWaitForIndex() { |
| 113 | simple_cache_wait_for_index_ = false; |
| 114 | } |
| 115 | |
[email protected] | a9da16d | 2008-07-30 21:41:54 | [diff] [blame] | 116 | void DisableFirstCleanup() { |
| 117 | first_cleanup_ = false; |
| 118 | } |
| 119 | |
[email protected] | 220d7100 | 2009-04-17 00:50:04 | [diff] [blame] | 120 | void DisableIntegrityCheck() { |
| 121 | integrity_ = false; |
| 122 | } |
| 123 | |
Maks Orlovich | 922db0c | 2018-02-21 15:28:46 | [diff] [blame] | 124 | // This is only supported for blockfile cache. |
[email protected] | fb2622f | 2010-07-13 18:00:56 | [diff] [blame] | 125 | void UseCurrentThread() { |
Maks Orlovich | 922db0c | 2018-02-21 15:28:46 | [diff] [blame] | 126 | DCHECK(!simple_cache_mode_); |
[email protected] | fb2622f | 2010-07-13 18:00:56 | [diff] [blame] | 127 | use_current_thread_ = true; |
| 128 | } |
| 129 | |
[email protected] | 67b09ec | 2010-08-27 17:49:42 | [diff] [blame] | 130 | void SetCacheType(net::CacheType type) { |
| 131 | type_ = type; |
| 132 | } |
| 133 | |
[email protected] | bc4fb8e | 2010-03-18 23:58:17 | [diff] [blame] | 134 | // Utility methods to access the cache and wait for each operation to finish. |
| 135 | int OpenEntry(const std::string& key, disk_cache::Entry** entry); |
Josh Karlin | dd9a5d14 | 2018-06-06 00:35:48 | [diff] [blame^] | 136 | int OpenEntryWithPriority(const std::string& key, |
| 137 | net::RequestPriority request_priority, |
| 138 | disk_cache::Entry** entry); |
[email protected] | bc4fb8e | 2010-03-18 23:58:17 | [diff] [blame] | 139 | int CreateEntry(const std::string& key, disk_cache::Entry** entry); |
Josh Karlin | dd9a5d14 | 2018-06-06 00:35:48 | [diff] [blame^] | 140 | int CreateEntryWithPriority(const std::string& key, |
| 141 | net::RequestPriority request_priority, |
| 142 | disk_cache::Entry** entry); |
[email protected] | bc4fb8e | 2010-03-18 23:58:17 | [diff] [blame] | 143 | int DoomEntry(const std::string& key); |
| 144 | int DoomAllEntries(); |
| 145 | int DoomEntriesBetween(const base::Time initial_time, |
| 146 | const base::Time end_time); |
msramek | aee01ceb | 2015-10-07 14:23:33 | [diff] [blame] | 147 | int CalculateSizeOfAllEntries(); |
dullweber | a003005 | 2017-01-16 11:56:04 | [diff] [blame] | 148 | int CalculateSizeOfEntriesBetween(const base::Time initial_time, |
| 149 | const base::Time end_time); |
[email protected] | bc4fb8e | 2010-03-18 23:58:17 | [diff] [blame] | 150 | int DoomEntriesSince(const base::Time initial_time); |
danakj | d04b92d | 2016-04-16 01:16:49 | [diff] [blame] | 151 | std::unique_ptr<TestIterator> CreateIterator(); |
[email protected] | fb2622f | 2010-07-13 18:00:56 | [diff] [blame] | 152 | void FlushQueueForTest(); |
[email protected] | f27bbe00 | 2011-12-22 11:29:34 | [diff] [blame] | 153 | void RunTaskForTest(const base::Closure& closure); |
[email protected] | e1fcf14 | 2010-08-23 18:47:25 | [diff] [blame] | 154 | int ReadData(disk_cache::Entry* entry, int index, int offset, |
[email protected] | 67b09ec | 2010-08-27 17:49:42 | [diff] [blame] | 155 | net::IOBuffer* buf, int len); |
[email protected] | e1fcf14 | 2010-08-23 18:47:25 | [diff] [blame] | 156 | int WriteData(disk_cache::Entry* entry, int index, int offset, |
| 157 | net::IOBuffer* buf, int len, bool truncate); |
Avi Drissman | 13fc893 | 2015-12-20 04:40:46 | [diff] [blame] | 158 | int ReadSparseData(disk_cache::Entry* entry, |
| 159 | int64_t offset, |
| 160 | net::IOBuffer* buf, |
[email protected] | e1fcf14 | 2010-08-23 18:47:25 | [diff] [blame] | 161 | int len); |
Avi Drissman | 13fc893 | 2015-12-20 04:40:46 | [diff] [blame] | 162 | int WriteSparseData(disk_cache::Entry* entry, |
| 163 | int64_t offset, |
| 164 | net::IOBuffer* buf, |
| 165 | int len); |
[email protected] | bc4fb8e | 2010-03-18 23:58:17 | [diff] [blame] | 166 | |
[email protected] | 30f02c43 | 2011-05-10 22:27:06 | [diff] [blame] | 167 | // 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] | 168 | // deleted. |
| 169 | void TrimForTest(bool empty); |
| 170 | |
[email protected] | 30f02c43 | 2011-05-10 22:27:06 | [diff] [blame] | 171 | // Asks the cache to trim an entry from the deleted list. If |empty| is |
| 172 | // true, the whole list is deleted. |
[email protected] | ceb61da3 | 2011-01-25 23:52:02 | [diff] [blame] | 173 | void TrimDeletedListForTest(bool empty); |
| 174 | |
[email protected] | 05f8087d | 2012-06-29 18:58:37 | [diff] [blame] | 175 | // Makes sure that some time passes before continuing the test. Time::Now() |
| 176 | // before and after this method will not be the same. |
| 177 | void AddDelay(); |
| 178 | |
dcheng | 67be2b1f | 2014-10-27 21:47:29 | [diff] [blame] | 179 | void TearDown() override; |
[email protected] | 7aefb15 | 2011-01-21 23:46:49 | [diff] [blame] | 180 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 181 | // cache_ will always have a valid object, regardless of how the cache was |
| 182 | // initialized. The implementation pointers can be NULL. |
danakj | d04b92d | 2016-04-16 01:16:49 | [diff] [blame] | 183 | std::unique_ptr<disk_cache::Backend> cache_; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 184 | disk_cache::BackendImpl* cache_impl_; |
Maks Orlovich | f378b3a | 2017-10-31 16:27:30 | [diff] [blame] | 185 | std::unique_ptr<disk_cache::SimpleFileTracker> simple_file_tracker_; |
[email protected] | 1ed9575 | 2013-04-23 00:12:36 | [diff] [blame] | 186 | disk_cache::SimpleBackendImpl* simple_cache_impl_; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 187 | disk_cache::MemBackendImpl* mem_cache_; |
| 188 | |
Avi Drissman | 13fc893 | 2015-12-20 04:40:46 | [diff] [blame] | 189 | uint32_t mask_; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 190 | int size_; |
[email protected] | 67b09ec | 2010-08-27 17:49:42 | [diff] [blame] | 191 | net::CacheType type_; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 192 | bool memory_only_; |
[email protected] | 398ad13 | 2013-04-02 15:11:01 | [diff] [blame] | 193 | bool simple_cache_mode_; |
[email protected] | 7e48c1b | 2013-07-12 12:15:43 | [diff] [blame] | 194 | bool simple_cache_wait_for_index_; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 195 | bool force_creation_; |
[email protected] | 220d7100 | 2009-04-17 00:50:04 | [diff] [blame] | 196 | bool new_eviction_; |
[email protected] | a9da16d | 2008-07-30 21:41:54 | [diff] [blame] | 197 | bool first_cleanup_; |
[email protected] | 220d7100 | 2009-04-17 00:50:04 | [diff] [blame] | 198 | bool integrity_; |
[email protected] | fb2622f | 2010-07-13 18:00:56 | [diff] [blame] | 199 | bool use_current_thread_; |
[email protected] | 220d7100 | 2009-04-17 00:50:04 | [diff] [blame] | 200 | // This is intentionally left uninitialized, to be used by any test. |
| 201 | bool success_; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 202 | |
| 203 | private: |
| 204 | void InitMemoryCache(); |
| 205 | void InitDiskCache(); |
[email protected] | ec44a9a | 2010-06-15 19:31:51 | [diff] [blame] | 206 | |
[email protected] | ec44a9a | 2010-06-15 19:31:51 | [diff] [blame] | 207 | DISALLOW_COPY_AND_ASSIGN(DiskCacheTestWithCache); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 208 | }; |
| 209 | |
[email protected] | 4d5e036 | 2008-08-28 00:59:06 | [diff] [blame] | 210 | #endif // NET_DISK_CACHE_DISK_CACHE_TEST_BASE_H_ |