blob: 050401fd9d972ab8acb0f10d362234deabd2bc77 [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"
Avi Drissman13fc8932015-12-20 04:40:4614#include "base/macros.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"
Bence Béky98447b12018-05-08 03:14:0118#include "net/test/test_with_scoped_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();
dcheng67be2b1f2014-10-27 21:47:2978 ~DiskCacheTestWithCache() override;
initial.commit586acc5fe2008-07-26 22:42:5279
Maks Orlovich463dc502017-07-25 14:52:4980 void CreateBackend(uint32_t flags);
[email protected]398ad132013-04-02 15:11:0181
initial.commit586acc5fe2008-07-26 22:42:5282 void InitCache();
initial.commit586acc5fe2008-07-26 22:42:5283 void SimulateCrash();
[email protected]a9da16d2008-07-30 21:41:5484 void SetTestMode();
initial.commit586acc5fe2008-07-26 22:42:5285
86 void SetMemoryOnlyMode() {
87 memory_only_ = true;
88 }
89
[email protected]398ad132013-04-02 15:11:0190 void SetSimpleCacheMode() {
Maks Orlovich922db0c2018-02-21 15:28:4691 DCHECK(!use_current_thread_);
[email protected]398ad132013-04-02 15:11:0192 simple_cache_mode_ = true;
initial.commit586acc5fe2008-07-26 22:42:5293 }
94
Avi Drissman13fc8932015-12-20 04:40:4695 void SetMask(uint32_t mask) { mask_ = mask; }
initial.commit586acc5fe2008-07-26 22:42:5296
Ben Kelly5a16ed02018-09-07 21:28:0997 void SetMaxSize(int64_t size, bool should_succeed = true);
initial.commit586acc5fe2008-07-26 22:42:5298
Maks Orlovichf3860652017-12-13 18:03:1699 // Returns value last given to SetMaxSize (or 0).
100 int MaxSize() const { return size_; }
101
initial.commit586acc5fe2008-07-26 22:42:52102 // Deletes and re-creates the files on initialization errors.
103 void SetForceCreation() {
104 force_creation_ = true;
105 }
106
[email protected]220d71002009-04-17 00:50:04107 void SetNewEviction() {
108 new_eviction_ = true;
109 }
110
[email protected]7e48c1b2013-07-12 12:15:43111 void DisableSimpleCacheWaitForIndex() {
112 simple_cache_wait_for_index_ = false;
113 }
114
[email protected]a9da16d2008-07-30 21:41:54115 void DisableFirstCleanup() {
116 first_cleanup_ = false;
117 }
118
[email protected]220d71002009-04-17 00:50:04119 void DisableIntegrityCheck() {
120 integrity_ = false;
121 }
122
Maks Orlovich922db0c2018-02-21 15:28:46123 // This is only supported for blockfile cache.
[email protected]fb2622f2010-07-13 18:00:56124 void UseCurrentThread() {
Maks Orlovich922db0c2018-02-21 15:28:46125 DCHECK(!simple_cache_mode_);
[email protected]fb2622f2010-07-13 18:00:56126 use_current_thread_ = true;
127 }
128
[email protected]67b09ec2010-08-27 17:49:42129 void SetCacheType(net::CacheType type) {
130 type_ = type;
131 }
132
[email protected]bc4fb8e2010-03-18 23:58:17133 // Utility methods to access the cache and wait for each operation to finish.
Steven Bingler6a1d77d2019-01-16 01:57:59134 int OpenOrCreateEntry(const std::string& key,
135 disk_cache::EntryWithOpened* entry_struct);
Steven Bingler6afedf82019-01-08 19:34:49136 int OpenOrCreateEntryWithPriority(const std::string& key,
137 net::RequestPriority request_priority,
Steven Bingler6a1d77d2019-01-16 01:57:59138 disk_cache::EntryWithOpened* entry_struct);
[email protected]bc4fb8e2010-03-18 23:58:17139 int OpenEntry(const std::string& key, disk_cache::Entry** entry);
Josh Karlindd9a5d142018-06-06 00:35:48140 int OpenEntryWithPriority(const std::string& key,
141 net::RequestPriority request_priority,
142 disk_cache::Entry** entry);
[email protected]bc4fb8e2010-03-18 23:58:17143 int CreateEntry(const std::string& key, disk_cache::Entry** entry);
Josh Karlindd9a5d142018-06-06 00:35:48144 int CreateEntryWithPriority(const std::string& key,
145 net::RequestPriority request_priority,
146 disk_cache::Entry** entry);
[email protected]bc4fb8e2010-03-18 23:58:17147 int DoomEntry(const std::string& key);
148 int DoomAllEntries();
149 int DoomEntriesBetween(const base::Time initial_time,
150 const base::Time end_time);
Ben Kelly5a16ed02018-09-07 21:28:09151 int64_t CalculateSizeOfAllEntries();
152 int64_t CalculateSizeOfEntriesBetween(const base::Time initial_time,
153 const base::Time end_time);
[email protected]bc4fb8e2010-03-18 23:58:17154 int DoomEntriesSince(const base::Time initial_time);
danakjd04b92d2016-04-16 01:16:49155 std::unique_ptr<TestIterator> CreateIterator();
[email protected]fb2622f2010-07-13 18:00:56156 void FlushQueueForTest();
[email protected]f27bbe002011-12-22 11:29:34157 void RunTaskForTest(const base::Closure& closure);
[email protected]e1fcf142010-08-23 18:47:25158 int ReadData(disk_cache::Entry* entry, int index, int offset,
[email protected]67b09ec2010-08-27 17:49:42159 net::IOBuffer* buf, int len);
[email protected]e1fcf142010-08-23 18:47:25160 int WriteData(disk_cache::Entry* entry, int index, int offset,
161 net::IOBuffer* buf, int len, bool truncate);
Avi Drissman13fc8932015-12-20 04:40:46162 int ReadSparseData(disk_cache::Entry* entry,
163 int64_t offset,
164 net::IOBuffer* buf,
[email protected]e1fcf142010-08-23 18:47:25165 int len);
Avi Drissman13fc8932015-12-20 04:40:46166 int WriteSparseData(disk_cache::Entry* entry,
167 int64_t offset,
168 net::IOBuffer* buf,
169 int len);
Matthew Denton29cbe0ee2019-03-27 00:10:41170 int GetAvailableRange(disk_cache::Entry* entry,
171 int64_t offset,
172 int len,
173 int64_t* start);
[email protected]bc4fb8e2010-03-18 23:58:17174
[email protected]30f02c432011-05-10 22:27:06175 // Asks the cache to trim an entry. If |empty| is true, the whole cache is
[email protected]ceb61da32011-01-25 23:52:02176 // deleted.
177 void TrimForTest(bool empty);
178
[email protected]30f02c432011-05-10 22:27:06179 // Asks the cache to trim an entry from the deleted list. If |empty| is
180 // true, the whole list is deleted.
[email protected]ceb61da32011-01-25 23:52:02181 void TrimDeletedListForTest(bool empty);
182
[email protected]05f8087d2012-06-29 18:58:37183 // Makes sure that some time passes before continuing the test. Time::Now()
184 // before and after this method will not be the same.
185 void AddDelay();
186
Matthew Denton29cbe0ee2019-03-27 00:10:41187 void OnExternalCacheHit(const std::string& key);
188
dcheng67be2b1f2014-10-27 21:47:29189 void TearDown() override;
[email protected]7aefb152011-01-21 23:46:49190
initial.commit586acc5fe2008-07-26 22:42:52191 // cache_ will always have a valid object, regardless of how the cache was
192 // initialized. The implementation pointers can be NULL.
danakjd04b92d2016-04-16 01:16:49193 std::unique_ptr<disk_cache::Backend> cache_;
initial.commit586acc5fe2008-07-26 22:42:52194 disk_cache::BackendImpl* cache_impl_;
Maks Orlovichf378b3a2017-10-31 16:27:30195 std::unique_ptr<disk_cache::SimpleFileTracker> simple_file_tracker_;
[email protected]1ed95752013-04-23 00:12:36196 disk_cache::SimpleBackendImpl* simple_cache_impl_;
initial.commit586acc5fe2008-07-26 22:42:52197 disk_cache::MemBackendImpl* mem_cache_;
198
Avi Drissman13fc8932015-12-20 04:40:46199 uint32_t mask_;
Ben Kelly5a16ed02018-09-07 21:28:09200 int64_t size_;
[email protected]67b09ec2010-08-27 17:49:42201 net::CacheType type_;
initial.commit586acc5fe2008-07-26 22:42:52202 bool memory_only_;
[email protected]398ad132013-04-02 15:11:01203 bool simple_cache_mode_;
[email protected]7e48c1b2013-07-12 12:15:43204 bool simple_cache_wait_for_index_;
initial.commit586acc5fe2008-07-26 22:42:52205 bool force_creation_;
[email protected]220d71002009-04-17 00:50:04206 bool new_eviction_;
[email protected]a9da16d2008-07-30 21:41:54207 bool first_cleanup_;
[email protected]220d71002009-04-17 00:50:04208 bool integrity_;
[email protected]fb2622f2010-07-13 18:00:56209 bool use_current_thread_;
[email protected]220d71002009-04-17 00:50:04210 // This is intentionally left uninitialized, to be used by any test.
211 bool success_;
initial.commit586acc5fe2008-07-26 22:42:52212
213 private:
214 void InitMemoryCache();
215 void InitDiskCache();
[email protected]ec44a9a2010-06-15 19:31:51216
[email protected]ec44a9a2010-06-15 19:31:51217 DISALLOW_COPY_AND_ASSIGN(DiskCacheTestWithCache);
initial.commit586acc5fe2008-07-26 22:42:52218};
219
[email protected]4d5e0362008-08-28 00:59:06220#endif // NET_DISK_CACHE_DISK_CACHE_TEST_BASE_H_