1 // Copyright 2012 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "net/disk_cache/disk_cache_test_base.h"
6
7 #include <memory>
8 #include <utility>
9
10 #include "base/files/file_util.h"
11 #include "base/functional/bind.h"
12 #include "base/path_service.h"
13 #include "base/run_loop.h"
14 #include "base/task/single_thread_task_runner.h"
15 #include "base/threading/platform_thread.h"
16 #include "net/base/io_buffer.h"
17 #include "net/base/net_errors.h"
18 #include "net/base/request_priority.h"
19 #include "net/base/test_completion_callback.h"
20 #include "net/disk_cache/backend_cleanup_tracker.h"
21 #include "net/disk_cache/blockfile/backend_impl.h"
22 #include "net/disk_cache/cache_util.h"
23 #include "net/disk_cache/disk_cache.h"
24 #include "net/disk_cache/disk_cache_test_util.h"
25 #include "net/disk_cache/memory/mem_backend_impl.h"
26 #include "net/disk_cache/simple/simple_backend_impl.h"
27 #include "net/disk_cache/simple/simple_file_tracker.h"
28 #include "net/disk_cache/simple/simple_index.h"
29 #include "net/test/gtest_util.h"
30 #include "testing/gmock/include/gmock/gmock.h"
31 #include "testing/gtest/include/gtest/gtest.h"
32
33 using net::test::IsOk;
34
DiskCacheTest()35 DiskCacheTest::DiskCacheTest() {
36 CHECK(temp_dir_.CreateUniqueTempDir());
37 // Put the cache into a subdir of |temp_dir_|, to permit tests to safely
38 // remove the cache directory without risking collisions with other tests.
39 cache_path_ = temp_dir_.GetPath().AppendASCII("cache");
40 CHECK(base::CreateDirectory(cache_path_));
41 }
42
43 DiskCacheTest::~DiskCacheTest() = default;
44
CopyTestCache(const std::string & name)45 bool DiskCacheTest::CopyTestCache(const std::string& name) {
46 base::FilePath path;
47 base::PathService::Get(base::DIR_SRC_TEST_DATA_ROOT, &path);
48 path = path.AppendASCII("net");
49 path = path.AppendASCII("data");
50 path = path.AppendASCII("cache_tests");
51 path = path.AppendASCII(name);
52
53 if (!CleanupCacheDir())
54 return false;
55 return base::CopyDirectory(path, cache_path_, false);
56 }
57
CleanupCacheDir()58 bool DiskCacheTest::CleanupCacheDir() {
59 return DeleteCache(cache_path_);
60 }
61
TearDown()62 void DiskCacheTest::TearDown() {
63 RunUntilIdle();
64 }
65
TestIterator(std::unique_ptr<disk_cache::Backend::Iterator> iterator)66 DiskCacheTestWithCache::TestIterator::TestIterator(
67 std::unique_ptr<disk_cache::Backend::Iterator> iterator)
68 : iterator_(std::move(iterator)) {}
69
70 DiskCacheTestWithCache::TestIterator::~TestIterator() = default;
71
OpenNextEntry(disk_cache::Entry ** next_entry)72 int DiskCacheTestWithCache::TestIterator::OpenNextEntry(
73 disk_cache::Entry** next_entry) {
74 TestEntryResultCompletionCallback cb;
75 disk_cache::EntryResult result =
76 cb.GetResult(iterator_->OpenNextEntry(cb.callback()));
77 int rv = result.net_error();
78 *next_entry = result.ReleaseEntry();
79 return rv;
80 }
81
82 DiskCacheTestWithCache::DiskCacheTestWithCache() = default;
83
84 DiskCacheTestWithCache::~DiskCacheTestWithCache() = default;
85
InitCache()86 void DiskCacheTestWithCache::InitCache() {
87 if (memory_only_)
88 InitMemoryCache();
89 else
90 InitDiskCache();
91
92 ASSERT_TRUE(nullptr != cache_);
93 if (first_cleanup_)
94 ASSERT_EQ(0, cache_->GetEntryCount());
95 }
96
97 // We are expected to leak memory when simulating crashes.
SimulateCrash()98 void DiskCacheTestWithCache::SimulateCrash() {
99 ASSERT_TRUE(!memory_only_);
100 net::TestCompletionCallback cb;
101 int rv = cache_impl_->FlushQueueForTest(cb.callback());
102 ASSERT_THAT(cb.GetResult(rv), IsOk());
103 cache_impl_->ClearRefCountForTest();
104
105 ResetCaches();
106 EXPECT_TRUE(CheckCacheIntegrity(cache_path_, new_eviction_, size_, mask_));
107
108 CreateBackend(disk_cache::kNoRandom);
109 }
110
SetTestMode()111 void DiskCacheTestWithCache::SetTestMode() {
112 ASSERT_TRUE(!memory_only_);
113 cache_impl_->SetUnitTestMode();
114 }
115
SetMaxSize(int64_t size)116 void DiskCacheTestWithCache::SetMaxSize(int64_t size) {
117 size_ = size;
118 // Cache size should not generally be changed dynamically; it takes
119 // backend-specific knowledge to make it even semi-reasonable to do.
120 DCHECK(!cache_);
121 }
122
OpenOrCreateEntry(const std::string & key)123 disk_cache::EntryResult DiskCacheTestWithCache::OpenOrCreateEntry(
124 const std::string& key) {
125 return OpenOrCreateEntryWithPriority(key, net::HIGHEST);
126 }
127
OpenOrCreateEntryWithPriority(const std::string & key,net::RequestPriority request_priority)128 disk_cache::EntryResult DiskCacheTestWithCache::OpenOrCreateEntryWithPriority(
129 const std::string& key,
130 net::RequestPriority request_priority) {
131 TestEntryResultCompletionCallback cb;
132 disk_cache::EntryResult result =
133 cache_->OpenOrCreateEntry(key, request_priority, cb.callback());
134 return cb.GetResult(std::move(result));
135 }
136
OpenEntry(const std::string & key,disk_cache::Entry ** entry)137 int DiskCacheTestWithCache::OpenEntry(const std::string& key,
138 disk_cache::Entry** entry) {
139 return OpenEntryWithPriority(key, net::HIGHEST, entry);
140 }
141
OpenEntryWithPriority(const std::string & key,net::RequestPriority request_priority,disk_cache::Entry ** entry)142 int DiskCacheTestWithCache::OpenEntryWithPriority(
143 const std::string& key,
144 net::RequestPriority request_priority,
145 disk_cache::Entry** entry) {
146 TestEntryResultCompletionCallback cb;
147 disk_cache::EntryResult result =
148 cb.GetResult(cache_->OpenEntry(key, request_priority, cb.callback()));
149 int rv = result.net_error();
150 *entry = result.ReleaseEntry();
151 return rv;
152 }
153
CreateEntry(const std::string & key,disk_cache::Entry ** entry)154 int DiskCacheTestWithCache::CreateEntry(const std::string& key,
155 disk_cache::Entry** entry) {
156 return CreateEntryWithPriority(key, net::HIGHEST, entry);
157 }
158
CreateEntryWithPriority(const std::string & key,net::RequestPriority request_priority,disk_cache::Entry ** entry)159 int DiskCacheTestWithCache::CreateEntryWithPriority(
160 const std::string& key,
161 net::RequestPriority request_priority,
162 disk_cache::Entry** entry) {
163 TestEntryResultCompletionCallback cb;
164 disk_cache::EntryResult result =
165 cb.GetResult(cache_->CreateEntry(key, request_priority, cb.callback()));
166 int rv = result.net_error();
167 *entry = result.ReleaseEntry();
168 return rv;
169 }
170
DoomEntry(const std::string & key)171 int DiskCacheTestWithCache::DoomEntry(const std::string& key) {
172 net::TestCompletionCallback cb;
173 int rv = cache_->DoomEntry(key, net::HIGHEST, cb.callback());
174 return cb.GetResult(rv);
175 }
176
DoomAllEntries()177 int DiskCacheTestWithCache::DoomAllEntries() {
178 net::TestCompletionCallback cb;
179 int rv = cache_->DoomAllEntries(cb.callback());
180 return cb.GetResult(rv);
181 }
182
DoomEntriesBetween(const base::Time initial_time,const base::Time end_time)183 int DiskCacheTestWithCache::DoomEntriesBetween(const base::Time initial_time,
184 const base::Time end_time) {
185 net::TestCompletionCallback cb;
186 int rv = cache_->DoomEntriesBetween(initial_time, end_time, cb.callback());
187 return cb.GetResult(rv);
188 }
189
DoomEntriesSince(const base::Time initial_time)190 int DiskCacheTestWithCache::DoomEntriesSince(const base::Time initial_time) {
191 net::TestCompletionCallback cb;
192 int rv = cache_->DoomEntriesSince(initial_time, cb.callback());
193 return cb.GetResult(rv);
194 }
195
CalculateSizeOfAllEntries()196 int64_t DiskCacheTestWithCache::CalculateSizeOfAllEntries() {
197 net::TestInt64CompletionCallback cb;
198 int64_t rv = cache_->CalculateSizeOfAllEntries(cb.callback());
199 return cb.GetResult(rv);
200 }
201
CalculateSizeOfEntriesBetween(const base::Time initial_time,const base::Time end_time)202 int64_t DiskCacheTestWithCache::CalculateSizeOfEntriesBetween(
203 const base::Time initial_time,
204 const base::Time end_time) {
205 net::TestInt64CompletionCallback cb;
206 int64_t rv = cache_->CalculateSizeOfEntriesBetween(initial_time, end_time,
207 cb.callback());
208 return cb.GetResult(rv);
209 }
210
211 std::unique_ptr<DiskCacheTestWithCache::TestIterator>
CreateIterator()212 DiskCacheTestWithCache::CreateIterator() {
213 return std::make_unique<TestIterator>(cache_->CreateIterator());
214 }
215
FlushQueueForTest()216 void DiskCacheTestWithCache::FlushQueueForTest() {
217 if (memory_only_)
218 return;
219
220 if (simple_cache_impl_) {
221 disk_cache::FlushCacheThreadForTesting();
222 return;
223 }
224
225 DCHECK(cache_impl_);
226 net::TestCompletionCallback cb;
227 int rv = cache_impl_->FlushQueueForTest(cb.callback());
228 EXPECT_THAT(cb.GetResult(rv), IsOk());
229 }
230
RunTaskForTest(base::OnceClosure closure)231 void DiskCacheTestWithCache::RunTaskForTest(base::OnceClosure closure) {
232 if (memory_only_ || !cache_impl_) {
233 std::move(closure).Run();
234 return;
235 }
236
237 net::TestCompletionCallback cb;
238 int rv = cache_impl_->RunTaskForTest(std::move(closure), cb.callback());
239 EXPECT_THAT(cb.GetResult(rv), IsOk());
240 }
241
ReadData(disk_cache::Entry * entry,int index,int offset,net::IOBuffer * buf,int len)242 int DiskCacheTestWithCache::ReadData(disk_cache::Entry* entry,
243 int index,
244 int offset,
245 net::IOBuffer* buf,
246 int len) {
247 net::TestCompletionCallback cb;
248 int rv = entry->ReadData(index, offset, buf, len, cb.callback());
249 return cb.GetResult(rv);
250 }
251
WriteData(disk_cache::Entry * entry,int index,int offset,net::IOBuffer * buf,int len,bool truncate)252 int DiskCacheTestWithCache::WriteData(disk_cache::Entry* entry,
253 int index,
254 int offset,
255 net::IOBuffer* buf,
256 int len,
257 bool truncate) {
258 net::TestCompletionCallback cb;
259 int rv = entry->WriteData(index, offset, buf, len, cb.callback(), truncate);
260 return cb.GetResult(rv);
261 }
262
ReadSparseData(disk_cache::Entry * entry,int64_t offset,net::IOBuffer * buf,int len)263 int DiskCacheTestWithCache::ReadSparseData(disk_cache::Entry* entry,
264 int64_t offset,
265 net::IOBuffer* buf,
266 int len) {
267 net::TestCompletionCallback cb;
268 int rv = entry->ReadSparseData(offset, buf, len, cb.callback());
269 return cb.GetResult(rv);
270 }
271
WriteSparseData(disk_cache::Entry * entry,int64_t offset,net::IOBuffer * buf,int len)272 int DiskCacheTestWithCache::WriteSparseData(disk_cache::Entry* entry,
273 int64_t offset,
274 net::IOBuffer* buf,
275 int len) {
276 net::TestCompletionCallback cb;
277 int rv = entry->WriteSparseData(offset, buf, len, cb.callback());
278 return cb.GetResult(rv);
279 }
280
GetAvailableRange(disk_cache::Entry * entry,int64_t offset,int len,int64_t * start)281 int DiskCacheTestWithCache::GetAvailableRange(disk_cache::Entry* entry,
282 int64_t offset,
283 int len,
284 int64_t* start) {
285 TestRangeResultCompletionCallback cb;
286 disk_cache::RangeResult result =
287 cb.GetResult(entry->GetAvailableRange(offset, len, cb.callback()));
288
289 if (result.net_error == net::OK) {
290 *start = result.start;
291 return result.available_len;
292 }
293 return result.net_error;
294 }
295
TrimForTest(bool empty)296 void DiskCacheTestWithCache::TrimForTest(bool empty) {
297 if (memory_only_ || !cache_impl_)
298 return;
299
300 RunTaskForTest(base::BindOnce(&disk_cache::BackendImpl::TrimForTest,
301 base::Unretained(cache_impl_), empty));
302 }
303
TrimDeletedListForTest(bool empty)304 void DiskCacheTestWithCache::TrimDeletedListForTest(bool empty) {
305 if (memory_only_ || !cache_impl_)
306 return;
307
308 RunTaskForTest(
309 base::BindOnce(&disk_cache::BackendImpl::TrimDeletedListForTest,
310 base::Unretained(cache_impl_), empty));
311 }
312
AddDelay()313 void DiskCacheTestWithCache::AddDelay() {
314 if (simple_cache_mode_) {
315 // The simple cache uses second resolution for many timeouts, so it's safest
316 // to advance by at least whole seconds before falling back into the normal
317 // disk cache epsilon advance.
318 const base::Time initial_time = base::Time::Now();
319 do {
320 base::PlatformThread::YieldCurrentThread();
321 } while (base::Time::Now() - initial_time < base::Seconds(1));
322 }
323
324 base::Time initial = base::Time::Now();
325 while (base::Time::Now() <= initial) {
326 base::PlatformThread::Sleep(base::Milliseconds(1));
327 };
328 }
329
OnExternalCacheHit(const std::string & key)330 void DiskCacheTestWithCache::OnExternalCacheHit(const std::string& key) {
331 cache_->OnExternalCacheHit(key);
332 }
333
TakeCache()334 std::unique_ptr<disk_cache::Backend> DiskCacheTestWithCache::TakeCache() {
335 mem_cache_ = nullptr;
336 simple_cache_impl_ = nullptr;
337 cache_impl_ = nullptr;
338 return std::move(cache_);
339 }
340
TearDown()341 void DiskCacheTestWithCache::TearDown() {
342 RunUntilIdle();
343 ResetCaches();
344 if (!memory_only_ && !simple_cache_mode_ && integrity_) {
345 EXPECT_TRUE(CheckCacheIntegrity(cache_path_, new_eviction_, size_, mask_));
346 }
347 RunUntilIdle();
348 if (simple_cache_mode_ && simple_file_tracker_) {
349 EXPECT_TRUE(simple_file_tracker_->IsEmptyForTesting());
350 }
351 DiskCacheTest::TearDown();
352 }
353
ResetCaches()354 void DiskCacheTestWithCache::ResetCaches() {
355 // Deletion occurs by `cache` going out of scope.
356 std::unique_ptr<disk_cache::Backend> cache = TakeCache();
357 }
358
InitMemoryCache()359 void DiskCacheTestWithCache::InitMemoryCache() {
360 auto cache =
361 disk_cache::MemBackendImpl::CreateBackend(size_, /*net_log=*/nullptr);
362 mem_cache_ = cache.get();
363 cache_ = std::move(cache);
364 ASSERT_TRUE(cache_);
365 }
366
InitDiskCache()367 void DiskCacheTestWithCache::InitDiskCache() {
368 if (first_cleanup_)
369 ASSERT_TRUE(CleanupCacheDir());
370
371 CreateBackend(disk_cache::kNoRandom);
372 }
373
CreateBackend(uint32_t flags)374 void DiskCacheTestWithCache::CreateBackend(uint32_t flags) {
375 scoped_refptr<base::SingleThreadTaskRunner> runner;
376 if (use_current_thread_)
377 runner = base::SingleThreadTaskRunner::GetCurrentDefault();
378 else
379 runner = nullptr; // let the backend sort it out.
380
381 if (simple_cache_mode_) {
382 DCHECK(!use_current_thread_)
383 << "Using current thread unsupported by SimpleCache";
384 net::TestCompletionCallback cb;
385 // We limit ourselves to 64 fds since OS X by default gives us 256.
386 // (Chrome raises the number on startup, but the test fixture doesn't).
387 if (!simple_file_tracker_)
388 simple_file_tracker_ =
389 std::make_unique<disk_cache::SimpleFileTracker>(64);
390 std::unique_ptr<disk_cache::SimpleBackendImpl> simple_backend =
391 std::make_unique<disk_cache::SimpleBackendImpl>(
392 /*file_operations=*/nullptr, cache_path_,
393 /* cleanup_tracker = */ nullptr, simple_file_tracker_.get(), size_,
394 type_, /*net_log = */ nullptr);
395 simple_backend->Init(cb.callback());
396 ASSERT_THAT(cb.WaitForResult(), IsOk());
397 simple_cache_impl_ = simple_backend.get();
398 cache_ = std::move(simple_backend);
399 if (simple_cache_wait_for_index_) {
400 net::TestCompletionCallback wait_for_index_cb;
401 simple_cache_impl_->index()->ExecuteWhenReady(
402 wait_for_index_cb.callback());
403 int rv = wait_for_index_cb.WaitForResult();
404 ASSERT_THAT(rv, IsOk());
405 }
406 return;
407 }
408
409 std::unique_ptr<disk_cache::BackendImpl> cache;
410 if (mask_) {
411 cache = std::make_unique<disk_cache::BackendImpl>(
412 cache_path_, mask_,
413 /* cleanup_tracker = */ nullptr, runner, type_,
414 /* net_log = */ nullptr);
415 } else {
416 cache = std::make_unique<disk_cache::BackendImpl>(
417 cache_path_, /* cleanup_tracker = */ nullptr, runner, type_,
418 /* net_log = */ nullptr);
419 }
420 cache_impl_ = cache.get();
421 cache_ = std::move(cache);
422 ASSERT_TRUE(cache_);
423 if (size_)
424 EXPECT_TRUE(cache_impl_->SetMaxSize(size_));
425 if (new_eviction_)
426 cache_impl_->SetNewEviction();
427 cache_impl_->SetFlags(flags);
428 net::TestCompletionCallback cb;
429 cache_impl_->Init(cb.callback());
430 ASSERT_THAT(cb.WaitForResult(), IsOk());
431 }
432