blob: 66b3cfc32e8a5a32e376a89791a11b491f684446 [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"
initial.commit586acc5fe2008-07-26 22:42:5218#include "testing/gtest/include/gtest/gtest.h"
[email protected]23887f04f2008-12-02 19:20:1519#include "testing/platform_test.h"
initial.commit586acc5fe2008-07-26 22:42:5220
[email protected]e1fcf142010-08-23 18:47:2521namespace net {
22
23class IOBuffer;
24
25} // namespace net
26
initial.commit586acc5fe2008-07-26 22:42:5227namespace disk_cache {
28
29class Backend;
30class BackendImpl;
[email protected]bc4fb8e2010-03-18 23:58:1731class Entry;
initial.commit586acc5fe2008-07-26 22:42:5232class MemBackendImpl;
[email protected]1ed95752013-04-23 00:12:3633class SimpleBackendImpl;
Maks Orlovichf378b3a2017-10-31 16:27:3034class SimpleFileTracker;
initial.commit586acc5fe2008-07-26 22:42:5235
[email protected]4d5e0362008-08-28 00:59:0636} // namespace disk_cache
initial.commit586acc5fe2008-07-26 22:42:5237
[email protected]17b89142008-11-07 21:52:1538// 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, ...).
42class DiskCacheTest : public PlatformTest {
[email protected]79b3fec2011-11-15 22:33:4743 protected:
44 DiskCacheTest();
dcheng67be2b1f2014-10-27 21:47:2945 ~DiskCacheTest() override;
[email protected]79b3fec2011-11-15 22:33:4746
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
dcheng67be2b1f2014-10-27 21:47:2953 void TearDown() override;
[email protected]79b3fec2011-11-15 22:33:4754
[email protected]6cdfd7f2013-02-08 20:40:1555 base::FilePath cache_path_;
[email protected]79b3fec2011-11-15 22:33:4756
57 private:
[email protected]ea1a3f62012-11-16 20:34:2358 base::ScopedTempDir temp_dir_;
[email protected]17b89142008-11-07 21:52:1559};
60
initial.commit586acc5fe2008-07-26 22:42:5261// Provides basic support for cache related tests.
[email protected]4d5e0362008-08-28 00:59:0662class DiskCacheTestWithCache : public DiskCacheTest {
initial.commit586acc5fe2008-07-26 22:42:5263 protected:
gavinpb1aaa052014-09-24 14:54:3564 class TestIterator {
65 public:
danakjd04b92d2016-04-16 01:16:4966 explicit TestIterator(
67 std::unique_ptr<disk_cache::Backend::Iterator> iterator);
gavinpb1aaa052014-09-24 14:54:3568 ~TestIterator();
69
70 int OpenNextEntry(disk_cache::Entry** next_entry);
71
72 private:
danakjd04b92d2016-04-16 01:16:4973 std::unique_ptr<disk_cache::Backend::Iterator> iterator_;
gavinpb1aaa052014-09-24 14:54:3574 };
75
[email protected]7aefb152011-01-21 23:46:4976 DiskCacheTestWithCache();
dcheng67be2b1f2014-10-27 21:47:2977 ~DiskCacheTestWithCache() override;
initial.commit586acc5fe2008-07-26 22:42:5278
Maks Orlovich463dc502017-07-25 14:52:4979 void CreateBackend(uint32_t flags);
[email protected]398ad132013-04-02 15:11:0180
initial.commit586acc5fe2008-07-26 22:42:5281 void InitCache();
initial.commit586acc5fe2008-07-26 22:42:5282 void SimulateCrash();
[email protected]a9da16d2008-07-30 21:41:5483 void SetTestMode();
initial.commit586acc5fe2008-07-26 22:42:5284
85 void SetMemoryOnlyMode() {
86 memory_only_ = true;
87 }
88
[email protected]398ad132013-04-02 15:11:0189 void SetSimpleCacheMode() {
90 simple_cache_mode_ = true;
initial.commit586acc5fe2008-07-26 22:42:5291 }
92
Avi Drissman13fc8932015-12-20 04:40:4693 void SetMask(uint32_t mask) { mask_ = mask; }
initial.commit586acc5fe2008-07-26 22:42:5294
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]220d71002009-04-17 00:50:04102 void SetNewEviction() {
103 new_eviction_ = true;
104 }
105
[email protected]7e48c1b2013-07-12 12:15:43106 void DisableSimpleCacheWaitForIndex() {
107 simple_cache_wait_for_index_ = false;
108 }
109
[email protected]a9da16d2008-07-30 21:41:54110 void DisableFirstCleanup() {
111 first_cleanup_ = false;
112 }
113
[email protected]220d71002009-04-17 00:50:04114 void DisableIntegrityCheck() {
115 integrity_ = false;
116 }
117
[email protected]fb2622f2010-07-13 18:00:56118 void UseCurrentThread() {
119 use_current_thread_ = true;
120 }
121
[email protected]67b09ec2010-08-27 17:49:42122 void SetCacheType(net::CacheType type) {
123 type_ = type;
124 }
125
[email protected]bc4fb8e2010-03-18 23:58:17126 // 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);
msramekaee01ceb2015-10-07 14:23:33133 int CalculateSizeOfAllEntries();
dullwebera0030052017-01-16 11:56:04134 int CalculateSizeOfEntriesBetween(const base::Time initial_time,
135 const base::Time end_time);
[email protected]bc4fb8e2010-03-18 23:58:17136 int DoomEntriesSince(const base::Time initial_time);
danakjd04b92d2016-04-16 01:16:49137 std::unique_ptr<TestIterator> CreateIterator();
[email protected]fb2622f2010-07-13 18:00:56138 void FlushQueueForTest();
[email protected]f27bbe002011-12-22 11:29:34139 void RunTaskForTest(const base::Closure& closure);
[email protected]e1fcf142010-08-23 18:47:25140 int ReadData(disk_cache::Entry* entry, int index, int offset,
[email protected]67b09ec2010-08-27 17:49:42141 net::IOBuffer* buf, int len);
[email protected]e1fcf142010-08-23 18:47:25142 int WriteData(disk_cache::Entry* entry, int index, int offset,
143 net::IOBuffer* buf, int len, bool truncate);
Avi Drissman13fc8932015-12-20 04:40:46144 int ReadSparseData(disk_cache::Entry* entry,
145 int64_t offset,
146 net::IOBuffer* buf,
[email protected]e1fcf142010-08-23 18:47:25147 int len);
Avi Drissman13fc8932015-12-20 04:40:46148 int WriteSparseData(disk_cache::Entry* entry,
149 int64_t offset,
150 net::IOBuffer* buf,
151 int len);
[email protected]bc4fb8e2010-03-18 23:58:17152
[email protected]30f02c432011-05-10 22:27:06153 // Asks the cache to trim an entry. If |empty| is true, the whole cache is
[email protected]ceb61da32011-01-25 23:52:02154 // deleted.
155 void TrimForTest(bool empty);
156
[email protected]30f02c432011-05-10 22:27:06157 // Asks the cache to trim an entry from the deleted list. If |empty| is
158 // true, the whole list is deleted.
[email protected]ceb61da32011-01-25 23:52:02159 void TrimDeletedListForTest(bool empty);
160
[email protected]05f8087d2012-06-29 18:58:37161 // 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]7aefb152011-01-21 23:46:49165 // DiskCacheTest:
dcheng67be2b1f2014-10-27 21:47:29166 void TearDown() override;
[email protected]7aefb152011-01-21 23:46:49167
initial.commit586acc5fe2008-07-26 22:42:52168 // cache_ will always have a valid object, regardless of how the cache was
169 // initialized. The implementation pointers can be NULL.
danakjd04b92d2016-04-16 01:16:49170 std::unique_ptr<disk_cache::Backend> cache_;
initial.commit586acc5fe2008-07-26 22:42:52171 disk_cache::BackendImpl* cache_impl_;
Maks Orlovichf378b3a2017-10-31 16:27:30172 std::unique_ptr<disk_cache::SimpleFileTracker> simple_file_tracker_;
[email protected]1ed95752013-04-23 00:12:36173 disk_cache::SimpleBackendImpl* simple_cache_impl_;
initial.commit586acc5fe2008-07-26 22:42:52174 disk_cache::MemBackendImpl* mem_cache_;
175
Avi Drissman13fc8932015-12-20 04:40:46176 uint32_t mask_;
initial.commit586acc5fe2008-07-26 22:42:52177 int size_;
[email protected]67b09ec2010-08-27 17:49:42178 net::CacheType type_;
initial.commit586acc5fe2008-07-26 22:42:52179 bool memory_only_;
[email protected]398ad132013-04-02 15:11:01180 bool simple_cache_mode_;
[email protected]7e48c1b2013-07-12 12:15:43181 bool simple_cache_wait_for_index_;
initial.commit586acc5fe2008-07-26 22:42:52182 bool force_creation_;
[email protected]220d71002009-04-17 00:50:04183 bool new_eviction_;
[email protected]a9da16d2008-07-30 21:41:54184 bool first_cleanup_;
[email protected]220d71002009-04-17 00:50:04185 bool integrity_;
[email protected]fb2622f2010-07-13 18:00:56186 bool use_current_thread_;
[email protected]220d71002009-04-17 00:50:04187 // This is intentionally left uninitialized, to be used by any test.
188 bool success_;
initial.commit586acc5fe2008-07-26 22:42:52189
190 private:
191 void InitMemoryCache();
192 void InitDiskCache();
[email protected]ec44a9a2010-06-15 19:31:51193
[email protected]ec44a9a2010-06-15 19:31:51194 DISALLOW_COPY_AND_ASSIGN(DiskCacheTestWithCache);
initial.commit586acc5fe2008-07-26 22:42:52195};
196
[email protected]4d5e0362008-08-28 00:59:06197#endif // NET_DISK_CACHE_DISK_CACHE_TEST_BASE_H_