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_UTIL_H_ 6 #define NET_DISK_CACHE_DISK_CACHE_TEST_UTIL_H_ 7 8 #include <stddef.h> 9 #include <stdint.h> 10 11 #include <string> 12 13 #include "base/files/file_path.h" 14 #include "base/memory/raw_ptr.h" 15 #include "base/run_loop.h" 16 #include "base/timer/timer.h" 17 #include "build/build_config.h" 18 #include "net/base/test_completion_callback.h" 19 #include "net/disk_cache/disk_cache.h" 20 21 // Re-creates a given test file inside the cache test folder. 22 bool CreateCacheTestFile(const base::FilePath& name); 23 24 // Deletes all file son the cache. 25 bool DeleteCache(const base::FilePath& path); 26 27 // Fills buffer with random values (may contain nulls unless no_nulls is true). 28 void CacheTestFillBuffer(char* buffer, size_t len, bool no_nulls); 29 30 // Generates a random key of up to 200 bytes. 31 std::string GenerateKey(bool same_length); 32 33 // Returns true if the cache is not corrupt. Assumes blockfile cache. 34 // |max_size|, if non-zero, will be set as its size. 35 bool CheckCacheIntegrity(const base::FilePath& path, 36 bool new_eviction, 37 int max_size, 38 uint32_t mask); 39 40 // ----------------------------------------------------------------------- 41 42 // Like net::TestCompletionCallback, but for BackendResultCallback. 43 struct BackendResultIsPendingHelper { operatorBackendResultIsPendingHelper44 bool operator()(const disk_cache::BackendResult& result) const { 45 return result.net_error == net::ERR_IO_PENDING; 46 } 47 }; 48 using TestBackendResultCompletionCallbackBase = 49 net::internal::TestCompletionCallbackTemplate<disk_cache::BackendResult, 50 BackendResultIsPendingHelper>; 51 52 class TestBackendResultCompletionCallback 53 : public TestBackendResultCompletionCallbackBase { 54 public: 55 TestBackendResultCompletionCallback(); 56 57 TestBackendResultCompletionCallback( 58 const TestBackendResultCompletionCallback&) = delete; 59 TestBackendResultCompletionCallback& operator=( 60 const TestBackendResultCompletionCallback&) = delete; 61 62 ~TestBackendResultCompletionCallback() override; 63 64 disk_cache::BackendResultCallback callback(); 65 }; 66 67 // Like net::TestCompletionCallback, but for EntryResultCallback. 68 69 struct EntryResultIsPendingHelper { operatorEntryResultIsPendingHelper70 bool operator()(const disk_cache::EntryResult& result) const { 71 return result.net_error() == net::ERR_IO_PENDING; 72 } 73 }; 74 using TestEntryResultCompletionCallbackBase = 75 net::internal::TestCompletionCallbackTemplate<disk_cache::EntryResult, 76 EntryResultIsPendingHelper>; 77 78 class TestEntryResultCompletionCallback 79 : public TestEntryResultCompletionCallbackBase { 80 public: 81 TestEntryResultCompletionCallback(); 82 83 TestEntryResultCompletionCallback(const TestEntryResultCompletionCallback&) = 84 delete; 85 TestEntryResultCompletionCallback& operator=( 86 const TestEntryResultCompletionCallback&) = delete; 87 88 ~TestEntryResultCompletionCallback() override; 89 90 disk_cache::Backend::EntryResultCallback callback(); 91 }; 92 93 // Like net::TestCompletionCallback, but for RangeResultCallback. 94 struct RangeResultIsPendingHelper { operatorRangeResultIsPendingHelper95 bool operator()(const disk_cache::RangeResult& result) const { 96 return result.net_error == net::ERR_IO_PENDING; 97 } 98 }; 99 100 class TestRangeResultCompletionCallback 101 : public net::internal::TestCompletionCallbackTemplate< 102 disk_cache::RangeResult, 103 RangeResultIsPendingHelper> { 104 public: 105 TestRangeResultCompletionCallback(); 106 ~TestRangeResultCompletionCallback() override; 107 108 disk_cache::RangeResultCallback callback(); 109 110 private: 111 // Reference -> Value adapter --- disk_cache wants reference for callback, 112 // base class wants a value. 113 void HelpSetResult(const disk_cache::RangeResult& result); 114 }; 115 116 // ----------------------------------------------------------------------- 117 118 // Simple helper to deal with the message loop on a test. 119 class MessageLoopHelper { 120 public: 121 MessageLoopHelper(); 122 123 MessageLoopHelper(const MessageLoopHelper&) = delete; 124 MessageLoopHelper& operator=(const MessageLoopHelper&) = delete; 125 126 ~MessageLoopHelper(); 127 128 // Run the message loop and wait for num_callbacks before returning. Returns 129 // false if we are waiting to long. Each callback that will be waited on is 130 // required to call CallbackWasCalled() to indicate when it was called. 131 bool WaitUntilCacheIoFinished(int num_callbacks); 132 133 // True if a given callback was called more times than it expected. callback_reused_error()134 bool callback_reused_error() const { return callback_reused_error_; } set_callback_reused_error(bool error)135 void set_callback_reused_error(bool error) { 136 callback_reused_error_ = error; 137 } 138 callbacks_called()139 int callbacks_called() const { return callbacks_called_; } 140 // Report that a callback was called. Each callback that will be waited on 141 // via WaitUntilCacheIoFinished() is expected to call this method to 142 // indicate when it has been executed. CallbackWasCalled()143 void CallbackWasCalled() { ++callbacks_called_; } 144 145 private: 146 // Sets the number of callbacks that can be received so far. ExpectCallbacks(int num_callbacks)147 void ExpectCallbacks(int num_callbacks) { 148 num_callbacks_ = num_callbacks; 149 num_iterations_ = last_ = 0; 150 completed_ = false; 151 } 152 153 // Called periodically to test if WaitUntilCacheIoFinished should return. 154 void TimerExpired(); 155 156 std::unique_ptr<base::RunLoop> run_loop_; 157 int num_callbacks_ = 0; 158 int num_iterations_ = 0; 159 int last_ = 0; 160 bool completed_ = false; 161 162 // True if a callback was called/reused more than expected. 163 bool callback_reused_error_ = false; 164 int callbacks_called_ = 0; 165 }; 166 167 // ----------------------------------------------------------------------- 168 169 // Simple callback to process IO completions from the cache. It allows tests 170 // with multiple simultaneous IO operations. 171 class CallbackTest { 172 public: 173 // Creates a new CallbackTest object. When the callback is called, it will 174 // update |helper|. If |reuse| is false and a callback is called more than 175 // once, or if |reuse| is true and a callback is called more than twice, an 176 // error will be reported to |helper|. 177 CallbackTest(MessageLoopHelper* helper, bool reuse); 178 179 CallbackTest(const CallbackTest&) = delete; 180 CallbackTest& operator=(const CallbackTest&) = delete; 181 182 ~CallbackTest(); 183 184 void Run(int result); 185 void RunWithEntry(disk_cache::EntryResult result); 186 last_result()187 int last_result() const { return last_result_; } ReleaseLastEntryResult()188 disk_cache::EntryResult ReleaseLastEntryResult() { 189 return std::move(last_entry_result_); 190 } 191 192 private: 193 raw_ptr<MessageLoopHelper> helper_; 194 int reuse_; 195 int last_result_; 196 disk_cache::EntryResult last_entry_result_; 197 }; 198 199 #endif // NET_DISK_CACHE_DISK_CACHE_TEST_UTIL_H_ 200