xref: /aosp_15_r20/external/cronet/net/disk_cache/disk_cache_test_base.cc (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
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,bool should_succeed)116 void DiskCacheTestWithCache::SetMaxSize(int64_t size, bool should_succeed) {
117   size_ = size;
118   if (simple_cache_impl_)
119     EXPECT_EQ(should_succeed, simple_cache_impl_->SetMaxSize(size));
120 
121   if (cache_impl_)
122     EXPECT_EQ(should_succeed, cache_impl_->SetMaxSize(size));
123 
124   if (mem_cache_)
125     EXPECT_EQ(should_succeed, mem_cache_->SetMaxSize(size));
126 }
127 
OpenOrCreateEntry(const std::string & key)128 disk_cache::EntryResult DiskCacheTestWithCache::OpenOrCreateEntry(
129     const std::string& key) {
130   return OpenOrCreateEntryWithPriority(key, net::HIGHEST);
131 }
132 
OpenOrCreateEntryWithPriority(const std::string & key,net::RequestPriority request_priority)133 disk_cache::EntryResult DiskCacheTestWithCache::OpenOrCreateEntryWithPriority(
134     const std::string& key,
135     net::RequestPriority request_priority) {
136   TestEntryResultCompletionCallback cb;
137   disk_cache::EntryResult result =
138       cache_->OpenOrCreateEntry(key, request_priority, cb.callback());
139   return cb.GetResult(std::move(result));
140 }
141 
OpenEntry(const std::string & key,disk_cache::Entry ** entry)142 int DiskCacheTestWithCache::OpenEntry(const std::string& key,
143                                       disk_cache::Entry** entry) {
144   return OpenEntryWithPriority(key, net::HIGHEST, entry);
145 }
146 
OpenEntryWithPriority(const std::string & key,net::RequestPriority request_priority,disk_cache::Entry ** entry)147 int DiskCacheTestWithCache::OpenEntryWithPriority(
148     const std::string& key,
149     net::RequestPriority request_priority,
150     disk_cache::Entry** entry) {
151   TestEntryResultCompletionCallback cb;
152   disk_cache::EntryResult result =
153       cb.GetResult(cache_->OpenEntry(key, request_priority, cb.callback()));
154   int rv = result.net_error();
155   *entry = result.ReleaseEntry();
156   return rv;
157 }
158 
CreateEntry(const std::string & key,disk_cache::Entry ** entry)159 int DiskCacheTestWithCache::CreateEntry(const std::string& key,
160                                         disk_cache::Entry** entry) {
161   return CreateEntryWithPriority(key, net::HIGHEST, entry);
162 }
163 
CreateEntryWithPriority(const std::string & key,net::RequestPriority request_priority,disk_cache::Entry ** entry)164 int DiskCacheTestWithCache::CreateEntryWithPriority(
165     const std::string& key,
166     net::RequestPriority request_priority,
167     disk_cache::Entry** entry) {
168   TestEntryResultCompletionCallback cb;
169   disk_cache::EntryResult result =
170       cb.GetResult(cache_->CreateEntry(key, request_priority, cb.callback()));
171   int rv = result.net_error();
172   *entry = result.ReleaseEntry();
173   return rv;
174 }
175 
DoomEntry(const std::string & key)176 int DiskCacheTestWithCache::DoomEntry(const std::string& key) {
177   net::TestCompletionCallback cb;
178   int rv = cache_->DoomEntry(key, net::HIGHEST, cb.callback());
179   return cb.GetResult(rv);
180 }
181 
DoomAllEntries()182 int DiskCacheTestWithCache::DoomAllEntries() {
183   net::TestCompletionCallback cb;
184   int rv = cache_->DoomAllEntries(cb.callback());
185   return cb.GetResult(rv);
186 }
187 
DoomEntriesBetween(const base::Time initial_time,const base::Time end_time)188 int DiskCacheTestWithCache::DoomEntriesBetween(const base::Time initial_time,
189                                                const base::Time end_time) {
190   net::TestCompletionCallback cb;
191   int rv = cache_->DoomEntriesBetween(initial_time, end_time, cb.callback());
192   return cb.GetResult(rv);
193 }
194 
DoomEntriesSince(const base::Time initial_time)195 int DiskCacheTestWithCache::DoomEntriesSince(const base::Time initial_time) {
196   net::TestCompletionCallback cb;
197   int rv = cache_->DoomEntriesSince(initial_time, cb.callback());
198   return cb.GetResult(rv);
199 }
200 
CalculateSizeOfAllEntries()201 int64_t DiskCacheTestWithCache::CalculateSizeOfAllEntries() {
202   net::TestInt64CompletionCallback cb;
203   int64_t rv = cache_->CalculateSizeOfAllEntries(cb.callback());
204   return cb.GetResult(rv);
205 }
206 
CalculateSizeOfEntriesBetween(const base::Time initial_time,const base::Time end_time)207 int64_t DiskCacheTestWithCache::CalculateSizeOfEntriesBetween(
208     const base::Time initial_time,
209     const base::Time end_time) {
210   net::TestInt64CompletionCallback cb;
211   int64_t rv = cache_->CalculateSizeOfEntriesBetween(initial_time, end_time,
212                                                      cb.callback());
213   return cb.GetResult(rv);
214 }
215 
216 std::unique_ptr<DiskCacheTestWithCache::TestIterator>
CreateIterator()217 DiskCacheTestWithCache::CreateIterator() {
218   return std::make_unique<TestIterator>(cache_->CreateIterator());
219 }
220 
FlushQueueForTest()221 void DiskCacheTestWithCache::FlushQueueForTest() {
222   if (memory_only_)
223     return;
224 
225   if (simple_cache_impl_) {
226     disk_cache::FlushCacheThreadForTesting();
227     return;
228   }
229 
230   DCHECK(cache_impl_);
231   net::TestCompletionCallback cb;
232   int rv = cache_impl_->FlushQueueForTest(cb.callback());
233   EXPECT_THAT(cb.GetResult(rv), IsOk());
234 }
235 
RunTaskForTest(base::OnceClosure closure)236 void DiskCacheTestWithCache::RunTaskForTest(base::OnceClosure closure) {
237   if (memory_only_ || !cache_impl_) {
238     std::move(closure).Run();
239     return;
240   }
241 
242   net::TestCompletionCallback cb;
243   int rv = cache_impl_->RunTaskForTest(std::move(closure), cb.callback());
244   EXPECT_THAT(cb.GetResult(rv), IsOk());
245 }
246 
ReadData(disk_cache::Entry * entry,int index,int offset,net::IOBuffer * buf,int len)247 int DiskCacheTestWithCache::ReadData(disk_cache::Entry* entry,
248                                      int index,
249                                      int offset,
250                                      net::IOBuffer* buf,
251                                      int len) {
252   net::TestCompletionCallback cb;
253   int rv = entry->ReadData(index, offset, buf, len, cb.callback());
254   return cb.GetResult(rv);
255 }
256 
WriteData(disk_cache::Entry * entry,int index,int offset,net::IOBuffer * buf,int len,bool truncate)257 int DiskCacheTestWithCache::WriteData(disk_cache::Entry* entry,
258                                       int index,
259                                       int offset,
260                                       net::IOBuffer* buf,
261                                       int len,
262                                       bool truncate) {
263   net::TestCompletionCallback cb;
264   int rv = entry->WriteData(index, offset, buf, len, cb.callback(), truncate);
265   return cb.GetResult(rv);
266 }
267 
ReadSparseData(disk_cache::Entry * entry,int64_t offset,net::IOBuffer * buf,int len)268 int DiskCacheTestWithCache::ReadSparseData(disk_cache::Entry* entry,
269                                            int64_t offset,
270                                            net::IOBuffer* buf,
271                                            int len) {
272   net::TestCompletionCallback cb;
273   int rv = entry->ReadSparseData(offset, buf, len, cb.callback());
274   return cb.GetResult(rv);
275 }
276 
WriteSparseData(disk_cache::Entry * entry,int64_t offset,net::IOBuffer * buf,int len)277 int DiskCacheTestWithCache::WriteSparseData(disk_cache::Entry* entry,
278                                             int64_t offset,
279                                             net::IOBuffer* buf,
280                                             int len) {
281   net::TestCompletionCallback cb;
282   int rv = entry->WriteSparseData(offset, buf, len, cb.callback());
283   return cb.GetResult(rv);
284 }
285 
GetAvailableRange(disk_cache::Entry * entry,int64_t offset,int len,int64_t * start)286 int DiskCacheTestWithCache::GetAvailableRange(disk_cache::Entry* entry,
287                                               int64_t offset,
288                                               int len,
289                                               int64_t* start) {
290   TestRangeResultCompletionCallback cb;
291   disk_cache::RangeResult result =
292       cb.GetResult(entry->GetAvailableRange(offset, len, cb.callback()));
293 
294   if (result.net_error == net::OK) {
295     *start = result.start;
296     return result.available_len;
297   }
298   return result.net_error;
299 }
300 
TrimForTest(bool empty)301 void DiskCacheTestWithCache::TrimForTest(bool empty) {
302   if (memory_only_ || !cache_impl_)
303     return;
304 
305   RunTaskForTest(base::BindOnce(&disk_cache::BackendImpl::TrimForTest,
306                                 base::Unretained(cache_impl_), empty));
307 }
308 
TrimDeletedListForTest(bool empty)309 void DiskCacheTestWithCache::TrimDeletedListForTest(bool empty) {
310   if (memory_only_ || !cache_impl_)
311     return;
312 
313   RunTaskForTest(
314       base::BindOnce(&disk_cache::BackendImpl::TrimDeletedListForTest,
315                      base::Unretained(cache_impl_), empty));
316 }
317 
AddDelay()318 void DiskCacheTestWithCache::AddDelay() {
319   if (simple_cache_mode_) {
320     // The simple cache uses second resolution for many timeouts, so it's safest
321     // to advance by at least whole seconds before falling back into the normal
322     // disk cache epsilon advance.
323     const base::Time initial_time = base::Time::Now();
324     do {
325       base::PlatformThread::YieldCurrentThread();
326     } while (base::Time::Now() - initial_time < base::Seconds(1));
327   }
328 
329   base::Time initial = base::Time::Now();
330   while (base::Time::Now() <= initial) {
331     base::PlatformThread::Sleep(base::Milliseconds(1));
332   };
333 }
334 
OnExternalCacheHit(const std::string & key)335 void DiskCacheTestWithCache::OnExternalCacheHit(const std::string& key) {
336   cache_->OnExternalCacheHit(key);
337 }
338 
TakeCache()339 std::unique_ptr<disk_cache::Backend> DiskCacheTestWithCache::TakeCache() {
340   mem_cache_ = nullptr;
341   simple_cache_impl_ = nullptr;
342   cache_impl_ = nullptr;
343   return std::move(cache_);
344 }
345 
TearDown()346 void DiskCacheTestWithCache::TearDown() {
347   RunUntilIdle();
348   ResetCaches();
349   if (!memory_only_ && !simple_cache_mode_ && integrity_) {
350     EXPECT_TRUE(CheckCacheIntegrity(cache_path_, new_eviction_, size_, mask_));
351   }
352   RunUntilIdle();
353   if (simple_cache_mode_ && simple_file_tracker_) {
354     EXPECT_TRUE(simple_file_tracker_->IsEmptyForTesting());
355   }
356   DiskCacheTest::TearDown();
357 }
358 
ResetCaches()359 void DiskCacheTestWithCache::ResetCaches() {
360   // Deletion occurs by `cache` going out of scope.
361   std::unique_ptr<disk_cache::Backend> cache = TakeCache();
362 }
363 
InitMemoryCache()364 void DiskCacheTestWithCache::InitMemoryCache() {
365   auto cache = std::make_unique<disk_cache::MemBackendImpl>(nullptr);
366   mem_cache_ = cache.get();
367   cache_ = std::move(cache);
368   ASSERT_TRUE(cache_);
369 
370   if (size_)
371     EXPECT_TRUE(mem_cache_->SetMaxSize(size_));
372 
373   ASSERT_TRUE(mem_cache_->Init());
374 }
375 
InitDiskCache()376 void DiskCacheTestWithCache::InitDiskCache() {
377   if (first_cleanup_)
378     ASSERT_TRUE(CleanupCacheDir());
379 
380   CreateBackend(disk_cache::kNoRandom);
381 }
382 
CreateBackend(uint32_t flags)383 void DiskCacheTestWithCache::CreateBackend(uint32_t flags) {
384   scoped_refptr<base::SingleThreadTaskRunner> runner;
385   if (use_current_thread_)
386     runner = base::SingleThreadTaskRunner::GetCurrentDefault();
387   else
388     runner = nullptr;  // let the backend sort it out.
389 
390   if (simple_cache_mode_) {
391     DCHECK(!use_current_thread_)
392         << "Using current thread unsupported by SimpleCache";
393     net::TestCompletionCallback cb;
394     // We limit ourselves to 64 fds since OS X by default gives us 256.
395     // (Chrome raises the number on startup, but the test fixture doesn't).
396     if (!simple_file_tracker_)
397       simple_file_tracker_ =
398           std::make_unique<disk_cache::SimpleFileTracker>(64);
399     std::unique_ptr<disk_cache::SimpleBackendImpl> simple_backend =
400         std::make_unique<disk_cache::SimpleBackendImpl>(
401             /*file_operations=*/nullptr, cache_path_,
402             /* cleanup_tracker = */ nullptr, simple_file_tracker_.get(), size_,
403             type_, /*net_log = */ nullptr);
404     simple_backend->Init(cb.callback());
405     ASSERT_THAT(cb.WaitForResult(), IsOk());
406     simple_cache_impl_ = simple_backend.get();
407     cache_ = std::move(simple_backend);
408     if (simple_cache_wait_for_index_) {
409       net::TestCompletionCallback wait_for_index_cb;
410       simple_cache_impl_->index()->ExecuteWhenReady(
411           wait_for_index_cb.callback());
412       int rv = wait_for_index_cb.WaitForResult();
413       ASSERT_THAT(rv, IsOk());
414     }
415     return;
416   }
417 
418   std::unique_ptr<disk_cache::BackendImpl> cache;
419   if (mask_) {
420     cache = std::make_unique<disk_cache::BackendImpl>(cache_path_, mask_,
421                                                       runner, type_,
422                                                       /* net_log = */ nullptr);
423   } else {
424     cache = std::make_unique<disk_cache::BackendImpl>(
425         cache_path_, /* cleanup_tracker = */ nullptr, runner, type_,
426         /* net_log = */ nullptr);
427   }
428   cache_impl_ = cache.get();
429   cache_ = std::move(cache);
430   ASSERT_TRUE(cache_);
431   if (size_)
432     EXPECT_TRUE(cache_impl_->SetMaxSize(size_));
433   if (new_eviction_)
434     cache_impl_->SetNewEviction();
435   cache_impl_->SetFlags(flags);
436   net::TestCompletionCallback cb;
437   cache_impl_->Init(cb.callback());
438   ASSERT_THAT(cb.WaitForResult(), IsOk());
439 }
440