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