blob: b24c9d691f7ddd8564a2c8b65748273bd51d5b1d [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, ...).
Bence Béky98447b12018-05-08 03:14:0143class DiskCacheTest : public PlatformTest,
44 public net::WithScopedTaskEnvironment {
[email protected]79b3fec2011-11-15 22:33:4745 protected:
46 DiskCacheTest();
dcheng67be2b1f2014-10-27 21:47:2947 ~DiskCacheTest() override;
[email protected]79b3fec2011-11-15 22:33:4748
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
dcheng67be2b1f2014-10-27 21:47:2955 void TearDown() override;
[email protected]79b3fec2011-11-15 22:33:4756
[email protected]6cdfd7f2013-02-08 20:40:1557 base::FilePath cache_path_;
[email protected]79b3fec2011-11-15 22:33:4758
59 private:
[email protected]ea1a3f62012-11-16 20:34:2360 base::ScopedTempDir temp_dir_;
[email protected]17b89142008-11-07 21:52:1561};
62
initial.commit586acc5fe2008-07-26 22:42:5263// Provides basic support for cache related tests.
[email protected]4d5e0362008-08-28 00:59:0664class DiskCacheTestWithCache : public DiskCacheTest {
initial.commit586acc5fe2008-07-26 22:42:5265 protected:
gavinpb1aaa052014-09-24 14:54:3566 class TestIterator {
67 public:
danakjd04b92d2016-04-16 01:16:4968 explicit TestIterator(
69 std::unique_ptr<disk_cache::Backend::Iterator> iterator);
gavinpb1aaa052014-09-24 14:54:3570 ~TestIterator();
71
72 int OpenNextEntry(disk_cache::Entry** next_entry);
73
74 private:
danakjd04b92d2016-04-16 01:16:4975 std::unique_ptr<disk_cache::Backend::Iterator> iterator_;
gavinpb1aaa052014-09-24 14:54:3576 };
77
[email protected]7aefb152011-01-21 23:46:4978 DiskCacheTestWithCache();
dcheng67be2b1f2014-10-27 21:47:2979 ~DiskCacheTestWithCache() override;
initial.commit586acc5fe2008-07-26 22:42:5280
Maks Orlovich463dc502017-07-25 14:52:4981 void CreateBackend(uint32_t flags);
[email protected]398ad132013-04-02 15:11:0182
initial.commit586acc5fe2008-07-26 22:42:5283 void InitCache();
initial.commit586acc5fe2008-07-26 22:42:5284 void SimulateCrash();
[email protected]a9da16d2008-07-30 21:41:5485 void SetTestMode();
initial.commit586acc5fe2008-07-26 22:42:5286
87 void SetMemoryOnlyMode() {
88 memory_only_ = true;
89 }
90
[email protected]398ad132013-04-02 15:11:0191 void SetSimpleCacheMode() {
Maks Orlovich922db0c2018-02-21 15:28:4692 DCHECK(!use_current_thread_);
[email protected]398ad132013-04-02 15:11:0193 simple_cache_mode_ = true;
initial.commit586acc5fe2008-07-26 22:42:5294 }
95
Avi Drissman13fc8932015-12-20 04:40:4696 void SetMask(uint32_t mask) { mask_ = mask; }
initial.commit586acc5fe2008-07-26 22:42:5297
98 void SetMaxSize(int size);
99
Maks Orlovichf3860652017-12-13 18:03:16100 // Returns value last given to SetMaxSize (or 0).
101 int MaxSize() const { return size_; }
102
initial.commit586acc5fe2008-07-26 22:42:52103 // Deletes and re-creates the files on initialization errors.
104 void SetForceCreation() {
105 force_creation_ = true;
106 }
107
[email protected]220d71002009-04-17 00:50:04108 void SetNewEviction() {
109 new_eviction_ = true;
110 }
111
[email protected]7e48c1b2013-07-12 12:15:43112 void DisableSimpleCacheWaitForIndex() {
113 simple_cache_wait_for_index_ = false;
114 }
115
[email protected]a9da16d2008-07-30 21:41:54116 void DisableFirstCleanup() {
117 first_cleanup_ = false;
118 }
119
[email protected]220d71002009-04-17 00:50:04120 void DisableIntegrityCheck() {
121 integrity_ = false;
122 }
123
Maks Orlovich922db0c2018-02-21 15:28:46124 // This is only supported for blockfile cache.
[email protected]fb2622f2010-07-13 18:00:56125 void UseCurrentThread() {
Maks Orlovich922db0c2018-02-21 15:28:46126 DCHECK(!simple_cache_mode_);
[email protected]fb2622f2010-07-13 18:00:56127 use_current_thread_ = true;
128 }
129
[email protected]67b09ec2010-08-27 17:49:42130 void SetCacheType(net::CacheType type) {
131 type_ = type;
132 }
133
[email protected]bc4fb8e2010-03-18 23:58:17134 // 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 Karlindd9a5d142018-06-06 00:35:48136 int OpenEntryWithPriority(const std::string& key,
137 net::RequestPriority request_priority,
138 disk_cache::Entry** entry);
[email protected]bc4fb8e2010-03-18 23:58:17139 int CreateEntry(const std::string& key, disk_cache::Entry** entry);
Josh Karlindd9a5d142018-06-06 00:35:48140 int CreateEntryWithPriority(const std::string& key,
141 net::RequestPriority request_priority,
142 disk_cache::Entry** entry);
[email protected]bc4fb8e2010-03-18 23:58:17143 int DoomEntry(const std::string& key);
144 int DoomAllEntries();
145 int DoomEntriesBetween(const base::Time initial_time,
146 const base::Time end_time);
msramekaee01ceb2015-10-07 14:23:33147 int CalculateSizeOfAllEntries();
dullwebera0030052017-01-16 11:56:04148 int CalculateSizeOfEntriesBetween(const base::Time initial_time,
149 const base::Time end_time);
[email protected]bc4fb8e2010-03-18 23:58:17150 int DoomEntriesSince(const base::Time initial_time);
danakjd04b92d2016-04-16 01:16:49151 std::unique_ptr<TestIterator> CreateIterator();
[email protected]fb2622f2010-07-13 18:00:56152 void FlushQueueForTest();
[email protected]f27bbe002011-12-22 11:29:34153 void RunTaskForTest(const base::Closure& closure);
[email protected]e1fcf142010-08-23 18:47:25154 int ReadData(disk_cache::Entry* entry, int index, int offset,
[email protected]67b09ec2010-08-27 17:49:42155 net::IOBuffer* buf, int len);
[email protected]e1fcf142010-08-23 18:47:25156 int WriteData(disk_cache::Entry* entry, int index, int offset,
157 net::IOBuffer* buf, int len, bool truncate);
Avi Drissman13fc8932015-12-20 04:40:46158 int ReadSparseData(disk_cache::Entry* entry,
159 int64_t offset,
160 net::IOBuffer* buf,
[email protected]e1fcf142010-08-23 18:47:25161 int len);
Avi Drissman13fc8932015-12-20 04:40:46162 int WriteSparseData(disk_cache::Entry* entry,
163 int64_t offset,
164 net::IOBuffer* buf,
165 int len);
[email protected]bc4fb8e2010-03-18 23:58:17166
[email protected]30f02c432011-05-10 22:27:06167 // Asks the cache to trim an entry. If |empty| is true, the whole cache is
[email protected]ceb61da32011-01-25 23:52:02168 // deleted.
169 void TrimForTest(bool empty);
170
[email protected]30f02c432011-05-10 22:27:06171 // Asks the cache to trim an entry from the deleted list. If |empty| is
172 // true, the whole list is deleted.
[email protected]ceb61da32011-01-25 23:52:02173 void TrimDeletedListForTest(bool empty);
174
[email protected]05f8087d2012-06-29 18:58:37175 // 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
dcheng67be2b1f2014-10-27 21:47:29179 void TearDown() override;
[email protected]7aefb152011-01-21 23:46:49180
initial.commit586acc5fe2008-07-26 22:42:52181 // cache_ will always have a valid object, regardless of how the cache was
182 // initialized. The implementation pointers can be NULL.
danakjd04b92d2016-04-16 01:16:49183 std::unique_ptr<disk_cache::Backend> cache_;
initial.commit586acc5fe2008-07-26 22:42:52184 disk_cache::BackendImpl* cache_impl_;
Maks Orlovichf378b3a2017-10-31 16:27:30185 std::unique_ptr<disk_cache::SimpleFileTracker> simple_file_tracker_;
[email protected]1ed95752013-04-23 00:12:36186 disk_cache::SimpleBackendImpl* simple_cache_impl_;
initial.commit586acc5fe2008-07-26 22:42:52187 disk_cache::MemBackendImpl* mem_cache_;
188
Avi Drissman13fc8932015-12-20 04:40:46189 uint32_t mask_;
initial.commit586acc5fe2008-07-26 22:42:52190 int size_;
[email protected]67b09ec2010-08-27 17:49:42191 net::CacheType type_;
initial.commit586acc5fe2008-07-26 22:42:52192 bool memory_only_;
[email protected]398ad132013-04-02 15:11:01193 bool simple_cache_mode_;
[email protected]7e48c1b2013-07-12 12:15:43194 bool simple_cache_wait_for_index_;
initial.commit586acc5fe2008-07-26 22:42:52195 bool force_creation_;
[email protected]220d71002009-04-17 00:50:04196 bool new_eviction_;
[email protected]a9da16d2008-07-30 21:41:54197 bool first_cleanup_;
[email protected]220d71002009-04-17 00:50:04198 bool integrity_;
[email protected]fb2622f2010-07-13 18:00:56199 bool use_current_thread_;
[email protected]220d71002009-04-17 00:50:04200 // This is intentionally left uninitialized, to be used by any test.
201 bool success_;
initial.commit586acc5fe2008-07-26 22:42:52202
203 private:
204 void InitMemoryCache();
205 void InitDiskCache();
[email protected]ec44a9a2010-06-15 19:31:51206
[email protected]ec44a9a2010-06-15 19:31:51207 DISALLOW_COPY_AND_ASSIGN(DiskCacheTestWithCache);
initial.commit586acc5fe2008-07-26 22:42:52208};
209
[email protected]4d5e0362008-08-28 00:59:06210#endif // NET_DISK_CACHE_DISK_CACHE_TEST_BASE_H_