[email protected] | 4c03b2e9 | 2012-01-03 19:36:57 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 4 | |
[email protected] | 2d31666 | 2008-09-03 18:18:14 | [diff] [blame] | 5 | #ifndef NET_DISK_CACHE_DISK_CACHE_TEST_UTIL_H_ |
| 6 | #define NET_DISK_CACHE_DISK_CACHE_TEST_UTIL_H_ |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 7 | |
Avi Drissman | 13fc893 | 2015-12-20 04:40:46 | [diff] [blame] | 8 | #include <stddef.h> |
| 9 | #include <stdint.h> |
| 10 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 11 | #include <string> |
| 12 | |
[email protected] | 5799981 | 2013-02-24 05:40:52 | [diff] [blame] | 13 | #include "base/files/file_path.h" |
Avi Drissman | 13fc893 | 2015-12-20 04:40:46 | [diff] [blame] | 14 | #include "base/macros.h" |
Gabriel Charette | b71eec89 | 2017-09-14 22:52:56 | [diff] [blame] | 15 | #include "base/run_loop.h" |
[email protected] | 9da992db | 2013-06-28 05:40:47 | [diff] [blame] | 16 | #include "base/timer/timer.h" |
[email protected] | 5306d11 | 2009-10-15 17:38:04 | [diff] [blame] | 17 | #include "build/build_config.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 18 | |
| 19 | // Re-creates a given test file inside the cache test folder. |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 20 | bool CreateCacheTestFile(const base::FilePath& name); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 21 | |
| 22 | // Deletes all file son the cache. |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 23 | bool DeleteCache(const base::FilePath& path); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 24 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 25 | // Fills buffer with random values (may contain nulls unless no_nulls is true). |
| 26 | void CacheTestFillBuffer(char* buffer, size_t len, bool no_nulls); |
| 27 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 28 | // Generates a random key of up to 200 bytes. |
| 29 | std::string GenerateKey(bool same_length); |
| 30 | |
Maks Orlovich | f386065 | 2017-12-13 18:03:16 | [diff] [blame] | 31 | // Returns true if the cache is not corrupt. Assumes blockfile cache. |
| 32 | // |max_size|, if non-zero, will be set as its size. |
Avi Drissman | 13fc893 | 2015-12-20 04:40:46 | [diff] [blame] | 33 | bool CheckCacheIntegrity(const base::FilePath& path, |
| 34 | bool new_eviction, |
Maks Orlovich | f386065 | 2017-12-13 18:03:16 | [diff] [blame] | 35 | int max_size, |
Avi Drissman | 13fc893 | 2015-12-20 04:40:46 | [diff] [blame] | 36 | uint32_t mask); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 37 | |
| 38 | // ----------------------------------------------------------------------- |
| 39 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 40 | // Simple helper to deal with the message loop on a test. |
| 41 | class MessageLoopHelper { |
| 42 | public: |
| 43 | MessageLoopHelper(); |
[email protected] | 601858c0 | 2010-09-01 17:08:20 | [diff] [blame] | 44 | ~MessageLoopHelper(); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 45 | |
| 46 | // Run the message loop and wait for num_callbacks before returning. Returns |
[email protected] | 2437576 | 2011-07-07 02:12:38 | [diff] [blame] | 47 | // false if we are waiting to long. Each callback that will be waited on is |
| 48 | // required to call CallbackWasCalled() to indicate when it was called. |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 49 | bool WaitUntilCacheIoFinished(int num_callbacks); |
| 50 | |
[email protected] | 2437576 | 2011-07-07 02:12:38 | [diff] [blame] | 51 | // True if a given callback was called more times than it expected. |
| 52 | bool callback_reused_error() const { return callback_reused_error_; } |
| 53 | void set_callback_reused_error(bool error) { |
| 54 | callback_reused_error_ = error; |
| 55 | } |
| 56 | |
| 57 | int callbacks_called() const { return callbacks_called_; } |
| 58 | // Report that a callback was called. Each callback that will be waited on |
| 59 | // via WaitUntilCacheIoFinished() is expected to call this method to |
| 60 | // indicate when it has been executed. |
| 61 | void CallbackWasCalled() { ++callbacks_called_; } |
| 62 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 63 | private: |
[email protected] | 2d31666 | 2008-09-03 18:18:14 | [diff] [blame] | 64 | // Sets the number of callbacks that can be received so far. |
| 65 | void ExpectCallbacks(int num_callbacks) { |
| 66 | num_callbacks_ = num_callbacks; |
| 67 | num_iterations_ = last_ = 0; |
| 68 | completed_ = false; |
| 69 | } |
| 70 | |
| 71 | // Called periodically to test if WaitUntilCacheIoFinished should return. |
| 72 | void TimerExpired(); |
| 73 | |
Sergey Ulanov | d160bd7e | 2017-09-12 22:04:40 | [diff] [blame] | 74 | std::unique_ptr<base::RunLoop> run_loop_; |
[email protected] | 2d31666 | 2008-09-03 18:18:14 | [diff] [blame] | 75 | int num_callbacks_; |
| 76 | int num_iterations_; |
| 77 | int last_; |
| 78 | bool completed_; |
| 79 | |
[email protected] | 2437576 | 2011-07-07 02:12:38 | [diff] [blame] | 80 | // True if a callback was called/reused more than expected. |
| 81 | bool callback_reused_error_; |
| 82 | int callbacks_called_; |
| 83 | |
[email protected] | 2d31666 | 2008-09-03 18:18:14 | [diff] [blame] | 84 | DISALLOW_COPY_AND_ASSIGN(MessageLoopHelper); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 85 | }; |
| 86 | |
[email protected] | 2437576 | 2011-07-07 02:12:38 | [diff] [blame] | 87 | // ----------------------------------------------------------------------- |
| 88 | |
| 89 | // Simple callback to process IO completions from the cache. It allows tests |
| 90 | // with multiple simultaneous IO operations. |
[email protected] | 565ddfa | 2011-12-22 20:44:22 | [diff] [blame] | 91 | class CallbackTest { |
[email protected] | 2437576 | 2011-07-07 02:12:38 | [diff] [blame] | 92 | public: |
| 93 | // Creates a new CallbackTest object. When the callback is called, it will |
[email protected] | 70740b7 | 2013-05-10 22:39:39 | [diff] [blame] | 94 | // update |helper|. If |reuse| is false and a callback is called more than |
| 95 | // once, or if |reuse| is true and a callback is called more than twice, an |
| 96 | // error will be reported to |helper|. |
[email protected] | 2437576 | 2011-07-07 02:12:38 | [diff] [blame] | 97 | CallbackTest(MessageLoopHelper* helper, bool reuse); |
[email protected] | 565ddfa | 2011-12-22 20:44:22 | [diff] [blame] | 98 | ~CallbackTest(); |
[email protected] | 2437576 | 2011-07-07 02:12:38 | [diff] [blame] | 99 | |
[email protected] | 70740b7 | 2013-05-10 22:39:39 | [diff] [blame] | 100 | void Run(int result); |
| 101 | |
| 102 | int last_result() const { return last_result_; } |
[email protected] | 2437576 | 2011-07-07 02:12:38 | [diff] [blame] | 103 | |
| 104 | private: |
| 105 | MessageLoopHelper* helper_; |
| 106 | int reuse_; |
[email protected] | 70740b7 | 2013-05-10 22:39:39 | [diff] [blame] | 107 | int last_result_; |
[email protected] | 2437576 | 2011-07-07 02:12:38 | [diff] [blame] | 108 | DISALLOW_COPY_AND_ASSIGN(CallbackTest); |
| 109 | }; |
| 110 | |
[email protected] | 2d31666 | 2008-09-03 18:18:14 | [diff] [blame] | 111 | #endif // NET_DISK_CACHE_DISK_CACHE_TEST_UTIL_H_ |