1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228
|
// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef NET_DISK_CACHE_DISK_CACHE_TEST_BASE_H_
#define NET_DISK_CACHE_DISK_CACHE_TEST_BASE_H_
#include <stdint.h>
#include <memory>
#include "base/files/file_path.h"
#include "base/files/scoped_temp_dir.h"
#include "base/memory/raw_ptr.h"
#include "base/threading/thread.h"
#include "net/base/cache_type.h"
#include "net/disk_cache/disk_cache.h"
#include "net/test/test_with_task_environment.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "testing/platform_test.h"
namespace net {
class IOBuffer;
} // namespace net
namespace disk_cache {
class Backend;
class BackendImpl;
class Entry;
class MemBackendImpl;
class SimpleBackendImpl;
class SimpleFileTracker;
} // namespace disk_cache
// These tests can use the path service, which uses autoreleased objects on the
// Mac, so this needs to be a PlatformTest. Even tests that do not require a
// cache (and that do not need to be a DiskCacheTestWithCache) are susceptible
// to this problem; all such tests should use TEST_F(DiskCacheTest, ...).
class DiskCacheTest : public PlatformTest, public net::WithTaskEnvironment {
protected:
DiskCacheTest();
~DiskCacheTest() override;
// Copies a set of cache files from the data folder to the test folder.
bool CopyTestCache(const std::string& name);
// Deletes the contents of |cache_path_|.
bool CleanupCacheDir();
void TearDown() override;
base::FilePath cache_path_;
private:
base::ScopedTempDir temp_dir_;
};
// Provides basic support for cache related tests.
class DiskCacheTestWithCache : public DiskCacheTest {
protected:
class TestIterator {
public:
explicit TestIterator(
std::unique_ptr<disk_cache::Backend::Iterator> iterator);
~TestIterator();
int OpenNextEntry(disk_cache::Entry** next_entry);
private:
std::unique_ptr<disk_cache::Backend::Iterator> iterator_;
};
DiskCacheTestWithCache();
DiskCacheTestWithCache(const DiskCacheTestWithCache&) = delete;
DiskCacheTestWithCache& operator=(const DiskCacheTestWithCache&) = delete;
~DiskCacheTestWithCache() override;
void CreateBackend(uint32_t flags);
void ResetCaches();
void InitCache();
void SimulateCrash();
void SetTestMode();
void SetMemoryOnlyMode() {
memory_only_ = true;
}
void SetSimpleCacheMode() {
DCHECK(!use_current_thread_);
simple_cache_mode_ = true;
}
void SetMask(uint32_t mask) { mask_ = mask; }
void SetMaxSize(int64_t size, bool should_succeed = true);
// Returns value last given to SetMaxSize (or 0).
int MaxSize() const { return size_; }
// Deletes and re-creates the files on initialization errors.
void SetForceCreation() {
force_creation_ = true;
}
void SetNewEviction() {
new_eviction_ = true;
}
void DisableSimpleCacheWaitForIndex() {
simple_cache_wait_for_index_ = false;
}
void DisableFirstCleanup() {
first_cleanup_ = false;
}
void DisableIntegrityCheck() {
integrity_ = false;
}
// This is only supported for blockfile cache.
void UseCurrentThread() {
DCHECK(!simple_cache_mode_);
use_current_thread_ = true;
}
void SetCacheType(net::CacheType type) {
type_ = type;
}
// Utility methods to access the cache and wait for each operation to finish.
// Also closer to legacy API.
// TODO(morlovich): Port all the tests to EntryResult.
disk_cache::EntryResult OpenOrCreateEntry(const std::string& key);
disk_cache::EntryResult OpenOrCreateEntryWithPriority(
const std::string& key,
net::RequestPriority request_priority);
int OpenEntry(const std::string& key, disk_cache::Entry** entry);
int OpenEntryWithPriority(const std::string& key,
net::RequestPriority request_priority,
disk_cache::Entry** entry);
int CreateEntry(const std::string& key, disk_cache::Entry** entry);
int CreateEntryWithPriority(const std::string& key,
net::RequestPriority request_priority,
disk_cache::Entry** entry);
int DoomEntry(const std::string& key);
int DoomAllEntries();
int DoomEntriesBetween(const base::Time initial_time,
const base::Time end_time);
int64_t CalculateSizeOfAllEntries();
int64_t CalculateSizeOfEntriesBetween(const base::Time initial_time,
const base::Time end_time);
int DoomEntriesSince(const base::Time initial_time);
std::unique_ptr<TestIterator> CreateIterator();
void FlushQueueForTest();
void RunTaskForTest(base::OnceClosure closure);
int ReadData(disk_cache::Entry* entry, int index, int offset,
net::IOBuffer* buf, int len);
int WriteData(disk_cache::Entry* entry, int index, int offset,
net::IOBuffer* buf, int len, bool truncate);
int ReadSparseData(disk_cache::Entry* entry,
int64_t offset,
net::IOBuffer* buf,
int len);
int WriteSparseData(disk_cache::Entry* entry,
int64_t offset,
net::IOBuffer* buf,
int len);
// TODO(morlovich): Port all the tests using this to RangeResult.
int GetAvailableRange(disk_cache::Entry* entry,
int64_t offset,
int len,
int64_t* start);
// Asks the cache to trim an entry. If |empty| is true, the whole cache is
// deleted.
void TrimForTest(bool empty);
// Asks the cache to trim an entry from the deleted list. If |empty| is
// true, the whole list is deleted.
void TrimDeletedListForTest(bool empty);
// Makes sure that some time passes before continuing the test. Time::Now()
// before and after this method will not be the same.
void AddDelay();
void OnExternalCacheHit(const std::string& key);
std::unique_ptr<disk_cache::Backend> TakeCache();
void TearDown() override;
// cache_ will always have a valid object, regardless of how the cache was
// initialized. The implementation pointers can be NULL.
std::unique_ptr<disk_cache::Backend> cache_;
raw_ptr<disk_cache::BackendImpl, DanglingUntriaged> cache_impl_ = nullptr;
std::unique_ptr<disk_cache::SimpleFileTracker> simple_file_tracker_;
raw_ptr<disk_cache::SimpleBackendImpl, DanglingUntriaged> simple_cache_impl_ =
nullptr;
raw_ptr<disk_cache::MemBackendImpl> mem_cache_ = nullptr;
uint32_t mask_ = 0;
int64_t size_ = 0;
net::CacheType type_ = net::DISK_CACHE;
bool memory_only_ = false;
bool simple_cache_mode_ = false;
bool simple_cache_wait_for_index_ = true;
bool force_creation_ = false;
bool new_eviction_ = false;
bool first_cleanup_ = true;
bool integrity_ = true;
bool use_current_thread_ = false;
// This is intentionally left uninitialized, to be used by any test.
bool success_;
private:
void InitMemoryCache();
void InitDiskCache();
};
#endif // NET_DISK_CACHE_DISK_CACHE_TEST_BASE_H_
|