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