blob: 3659a1a7ea9d2cb09fda4e2bd64d7504e1c70e20 [file] [log] [blame]
[email protected]05f8087d2012-06-29 18:58:371// Copyright (c) 2012 The Chromium Authors. All rights reserved.
license.botbf09a502008-08-24 00:55:552// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
initial.commit586acc5fe2008-07-26 22:42:524
[email protected]4d5e0362008-08-28 00:59:065#ifndef NET_DISK_CACHE_DISK_CACHE_TEST_BASE_H_
6#define NET_DISK_CACHE_DISK_CACHE_TEST_BASE_H_
initial.commit586acc5fe2008-07-26 22:42:527
Avi Drissman13fc8932015-12-20 04:40:468#include <stdint.h>
9
danakjd04b92d2016-04-16 01:16:4910#include <memory>
11
[email protected]57999812013-02-24 05:40:5212#include "base/files/file_path.h"
[email protected]ea1a3f62012-11-16 20:34:2313#include "base/files/scoped_temp_dir.h"
Keishi Hattori0e45c022021-11-27 09:25:5214#include "base/memory/raw_ptr.h"
[email protected]34b99632011-01-01 01:01:0615#include "base/threading/thread.h"
[email protected]67b09ec2010-08-27 17:49:4216#include "net/base/cache_type.h"
gavinpb1aaa052014-09-24 14:54:3517#include "net/disk_cache/disk_cache.h"
Gabriel Charettec7108742019-08-23 03:31:4018#include "net/test/test_with_task_environment.h"
initial.commit586acc5fe2008-07-26 22:42:5219#include "testing/gtest/include/gtest/gtest.h"
[email protected]23887f04f2008-12-02 19:20:1520#include "testing/platform_test.h"
initial.commit586acc5fe2008-07-26 22:42:5221
[email protected]e1fcf142010-08-23 18:47:2522namespace net {
23
24class IOBuffer;
25
26} // namespace net
27
initial.commit586acc5fe2008-07-26 22:42:5228namespace disk_cache {
29
30class Backend;
31class BackendImpl;
[email protected]bc4fb8e2010-03-18 23:58:1732class Entry;
initial.commit586acc5fe2008-07-26 22:42:5233class MemBackendImpl;
[email protected]1ed95752013-04-23 00:12:3634class SimpleBackendImpl;
Maks Orlovichf378b3a2017-10-31 16:27:3035class SimpleFileTracker;
initial.commit586acc5fe2008-07-26 22:42:5236
[email protected]4d5e0362008-08-28 00:59:0637} // namespace disk_cache
initial.commit586acc5fe2008-07-26 22:42:5238
[email protected]17b89142008-11-07 21:52:1539// 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 Charette694c3c332019-08-19 14:53:0543class DiskCacheTest : public PlatformTest, public net::WithTaskEnvironment {
[email protected]79b3fec2011-11-15 22:33:4744 protected:
45 DiskCacheTest();
dcheng67be2b1f2014-10-27 21:47:2946 ~DiskCacheTest() override;
[email protected]79b3fec2011-11-15 22:33:4747
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
dcheng67be2b1f2014-10-27 21:47:2954 void TearDown() override;
[email protected]79b3fec2011-11-15 22:33:4755
[email protected]6cdfd7f2013-02-08 20:40:1556 base::FilePath cache_path_;
[email protected]79b3fec2011-11-15 22:33:4757
58 private:
[email protected]ea1a3f62012-11-16 20:34:2359 base::ScopedTempDir temp_dir_;
[email protected]17b89142008-11-07 21:52:1560};
61
initial.commit586acc5fe2008-07-26 22:42:5262// Provides basic support for cache related tests.
[email protected]4d5e0362008-08-28 00:59:0663class DiskCacheTestWithCache : public DiskCacheTest {
initial.commit586acc5fe2008-07-26 22:42:5264 protected:
gavinpb1aaa052014-09-24 14:54:3565 class TestIterator {
66 public:
danakjd04b92d2016-04-16 01:16:4967 explicit TestIterator(
68 std::unique_ptr<disk_cache::Backend::Iterator> iterator);
gavinpb1aaa052014-09-24 14:54:3569 ~TestIterator();
70
71 int OpenNextEntry(disk_cache::Entry** next_entry);
72
73 private:
danakjd04b92d2016-04-16 01:16:4974 std::unique_ptr<disk_cache::Backend::Iterator> iterator_;
gavinpb1aaa052014-09-24 14:54:3575 };
76
[email protected]7aefb152011-01-21 23:46:4977 DiskCacheTestWithCache();
Peter Boström407869b2021-10-07 04:42:4878
79 DiskCacheTestWithCache(const DiskCacheTestWithCache&) = delete;
80 DiskCacheTestWithCache& operator=(const DiskCacheTestWithCache&) = delete;
81
dcheng67be2b1f2014-10-27 21:47:2982 ~DiskCacheTestWithCache() override;
initial.commit586acc5fe2008-07-26 22:42:5283
Maks Orlovich463dc502017-07-25 14:52:4984 void CreateBackend(uint32_t flags);
[email protected]398ad132013-04-02 15:11:0185
initial.commit586acc5fe2008-07-26 22:42:5286 void InitCache();
initial.commit586acc5fe2008-07-26 22:42:5287 void SimulateCrash();
[email protected]a9da16d2008-07-30 21:41:5488 void SetTestMode();
initial.commit586acc5fe2008-07-26 22:42:5289
90 void SetMemoryOnlyMode() {
91 memory_only_ = true;
92 }
93
[email protected]398ad132013-04-02 15:11:0194 void SetSimpleCacheMode() {
Maks Orlovich922db0c2018-02-21 15:28:4695 DCHECK(!use_current_thread_);
[email protected]398ad132013-04-02 15:11:0196 simple_cache_mode_ = true;
initial.commit586acc5fe2008-07-26 22:42:5297 }
98
Avi Drissman13fc8932015-12-20 04:40:4699 void SetMask(uint32_t mask) { mask_ = mask; }
initial.commit586acc5fe2008-07-26 22:42:52100
Ben Kelly5a16ed02018-09-07 21:28:09101 void SetMaxSize(int64_t size, bool should_succeed = true);
initial.commit586acc5fe2008-07-26 22:42:52102
Maks Orlovichf3860652017-12-13 18:03:16103 // Returns value last given to SetMaxSize (or 0).
104 int MaxSize() const { return size_; }
105
initial.commit586acc5fe2008-07-26 22:42:52106 // Deletes and re-creates the files on initialization errors.
107 void SetForceCreation() {
108 force_creation_ = true;
109 }
110
[email protected]220d71002009-04-17 00:50:04111 void SetNewEviction() {
112 new_eviction_ = true;
113 }
114
[email protected]7e48c1b2013-07-12 12:15:43115 void DisableSimpleCacheWaitForIndex() {
116 simple_cache_wait_for_index_ = false;
117 }
118
[email protected]a9da16d2008-07-30 21:41:54119 void DisableFirstCleanup() {
120 first_cleanup_ = false;
121 }
122
[email protected]220d71002009-04-17 00:50:04123 void DisableIntegrityCheck() {
124 integrity_ = false;
125 }
126
Maks Orlovich922db0c2018-02-21 15:28:46127 // This is only supported for blockfile cache.
[email protected]fb2622f2010-07-13 18:00:56128 void UseCurrentThread() {
Maks Orlovich922db0c2018-02-21 15:28:46129 DCHECK(!simple_cache_mode_);
[email protected]fb2622f2010-07-13 18:00:56130 use_current_thread_ = true;
131 }
132
[email protected]67b09ec2010-08-27 17:49:42133 void SetCacheType(net::CacheType type) {
134 type_ = type;
135 }
136
[email protected]bc4fb8e2010-03-18 23:58:17137 // Utility methods to access the cache and wait for each operation to finish.
Maks Orlovich8efea482019-08-20 17:14:53138 // 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]bc4fb8e2010-03-18 23:58:17144 int OpenEntry(const std::string& key, disk_cache::Entry** entry);
Josh Karlindd9a5d142018-06-06 00:35:48145 int OpenEntryWithPriority(const std::string& key,
146 net::RequestPriority request_priority,
147 disk_cache::Entry** entry);
[email protected]bc4fb8e2010-03-18 23:58:17148 int CreateEntry(const std::string& key, disk_cache::Entry** entry);
Josh Karlindd9a5d142018-06-06 00:35:48149 int CreateEntryWithPriority(const std::string& key,
150 net::RequestPriority request_priority,
151 disk_cache::Entry** entry);
[email protected]bc4fb8e2010-03-18 23:58:17152 int DoomEntry(const std::string& key);
153 int DoomAllEntries();
154 int DoomEntriesBetween(const base::Time initial_time,
155 const base::Time end_time);
Ben Kelly5a16ed02018-09-07 21:28:09156 int64_t CalculateSizeOfAllEntries();
157 int64_t CalculateSizeOfEntriesBetween(const base::Time initial_time,
158 const base::Time end_time);
[email protected]bc4fb8e2010-03-18 23:58:17159 int DoomEntriesSince(const base::Time initial_time);
danakjd04b92d2016-04-16 01:16:49160 std::unique_ptr<TestIterator> CreateIterator();
[email protected]fb2622f2010-07-13 18:00:56161 void FlushQueueForTest();
Anna Malovacb042b642020-03-03 15:50:00162 void RunTaskForTest(base::OnceClosure closure);
[email protected]e1fcf142010-08-23 18:47:25163 int ReadData(disk_cache::Entry* entry, int index, int offset,
[email protected]67b09ec2010-08-27 17:49:42164 net::IOBuffer* buf, int len);
[email protected]e1fcf142010-08-23 18:47:25165 int WriteData(disk_cache::Entry* entry, int index, int offset,
166 net::IOBuffer* buf, int len, bool truncate);
Avi Drissman13fc8932015-12-20 04:40:46167 int ReadSparseData(disk_cache::Entry* entry,
168 int64_t offset,
169 net::IOBuffer* buf,
[email protected]e1fcf142010-08-23 18:47:25170 int len);
Avi Drissman13fc8932015-12-20 04:40:46171 int WriteSparseData(disk_cache::Entry* entry,
172 int64_t offset,
173 net::IOBuffer* buf,
174 int len);
Maks Orlovich04cd1ad2021-07-02 17:32:24175 // TODO(morlovich): Port all the tests using this to RangeResult.
Matthew Denton29cbe0ee2019-03-27 00:10:41176 int GetAvailableRange(disk_cache::Entry* entry,
177 int64_t offset,
178 int len,
179 int64_t* start);
[email protected]bc4fb8e2010-03-18 23:58:17180
[email protected]30f02c432011-05-10 22:27:06181 // Asks the cache to trim an entry. If |empty| is true, the whole cache is
[email protected]ceb61da32011-01-25 23:52:02182 // deleted.
183 void TrimForTest(bool empty);
184
[email protected]30f02c432011-05-10 22:27:06185 // Asks the cache to trim an entry from the deleted list. If |empty| is
186 // true, the whole list is deleted.
[email protected]ceb61da32011-01-25 23:52:02187 void TrimDeletedListForTest(bool empty);
188
[email protected]05f8087d2012-06-29 18:58:37189 // 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 Denton29cbe0ee2019-03-27 00:10:41193 void OnExternalCacheHit(const std::string& key);
194
dcheng67be2b1f2014-10-27 21:47:29195 void TearDown() override;
[email protected]7aefb152011-01-21 23:46:49196
initial.commit586acc5fe2008-07-26 22:42:52197 // cache_ will always have a valid object, regardless of how the cache was
198 // initialized. The implementation pointers can be NULL.
danakjd04b92d2016-04-16 01:16:49199 std::unique_ptr<disk_cache::Backend> cache_;
Tsuyoshi Horo432981d52022-06-09 09:50:13200 raw_ptr<disk_cache::BackendImpl> cache_impl_ = nullptr;
Maks Orlovichf378b3a2017-10-31 16:27:30201 std::unique_ptr<disk_cache::SimpleFileTracker> simple_file_tracker_;
Tsuyoshi Horo432981d52022-06-09 09:50:13202 raw_ptr<disk_cache::SimpleBackendImpl> simple_cache_impl_ = nullptr;
203 raw_ptr<disk_cache::MemBackendImpl> mem_cache_ = nullptr;
initial.commit586acc5fe2008-07-26 22:42:52204
Tsuyoshi Horoe0235ed62022-06-09 01:42:30205 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]220d71002009-04-17 00:50:04216 // This is intentionally left uninitialized, to be used by any test.
217 bool success_;
initial.commit586acc5fe2008-07-26 22:42:52218
219 private:
220 void InitMemoryCache();
221 void InitDiskCache();
initial.commit586acc5fe2008-07-26 22:42:52222};
223
[email protected]4d5e0362008-08-28 00:59:06224#endif // NET_DISK_CACHE_DISK_CACHE_TEST_BASE_H_