[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" |
[email protected] | 5ee2098 | 2013-07-17 21:51:18 | [diff] [blame] | 15 | #include "base/message_loop/message_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 | |||||
31 | // Returns true if the cache is not corrupt. | ||||
Avi Drissman | 13fc893 | 2015-12-20 04:40:46 | [diff] [blame^] | 32 | bool CheckCacheIntegrity(const base::FilePath& path, |
33 | bool new_eviction, | ||||
34 | uint32_t mask); | ||||
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 35 | |
36 | // ----------------------------------------------------------------------- | ||||
37 | |||||
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 38 | // Simple helper to deal with the message loop on a test. |
39 | class MessageLoopHelper { | ||||
40 | public: | ||||
41 | MessageLoopHelper(); | ||||
[email protected] | 601858c0 | 2010-09-01 17:08:20 | [diff] [blame] | 42 | ~MessageLoopHelper(); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 43 | |
44 | // Run the message loop and wait for num_callbacks before returning. Returns | ||||
[email protected] | 2437576 | 2011-07-07 02:12:38 | [diff] [blame] | 45 | // false if we are waiting to long. Each callback that will be waited on is |
46 | // required to call CallbackWasCalled() to indicate when it was called. | ||||
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 47 | bool WaitUntilCacheIoFinished(int num_callbacks); |
48 | |||||
[email protected] | 2437576 | 2011-07-07 02:12:38 | [diff] [blame] | 49 | // True if a given callback was called more times than it expected. |
50 | bool callback_reused_error() const { return callback_reused_error_; } | ||||
51 | void set_callback_reused_error(bool error) { | ||||
52 | callback_reused_error_ = error; | ||||
53 | } | ||||
54 | |||||
55 | int callbacks_called() const { return callbacks_called_; } | ||||
56 | // Report that a callback was called. Each callback that will be waited on | ||||
57 | // via WaitUntilCacheIoFinished() is expected to call this method to | ||||
58 | // indicate when it has been executed. | ||||
59 | void CallbackWasCalled() { ++callbacks_called_; } | ||||
60 | |||||
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 61 | private: |
[email protected] | 2d31666 | 2008-09-03 18:18:14 | [diff] [blame] | 62 | // Sets the number of callbacks that can be received so far. |
63 | void ExpectCallbacks(int num_callbacks) { | ||||
64 | num_callbacks_ = num_callbacks; | ||||
65 | num_iterations_ = last_ = 0; | ||||
66 | completed_ = false; | ||||
67 | } | ||||
68 | |||||
69 | // Called periodically to test if WaitUntilCacheIoFinished should return. | ||||
70 | void TimerExpired(); | ||||
71 | |||||
danakj | 8c3eb80 | 2015-09-24 07:53:00 | [diff] [blame] | 72 | base::RepeatingTimer timer_; |
[email protected] | 2d31666 | 2008-09-03 18:18:14 | [diff] [blame] | 73 | int num_callbacks_; |
74 | int num_iterations_; | ||||
75 | int last_; | ||||
76 | bool completed_; | ||||
77 | |||||
[email protected] | 2437576 | 2011-07-07 02:12:38 | [diff] [blame] | 78 | // True if a callback was called/reused more than expected. |
79 | bool callback_reused_error_; | ||||
80 | int callbacks_called_; | ||||
81 | |||||
[email protected] | 2d31666 | 2008-09-03 18:18:14 | [diff] [blame] | 82 | DISALLOW_COPY_AND_ASSIGN(MessageLoopHelper); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 83 | }; |
84 | |||||
[email protected] | 2437576 | 2011-07-07 02:12:38 | [diff] [blame] | 85 | // ----------------------------------------------------------------------- |
86 | |||||
87 | // Simple callback to process IO completions from the cache. It allows tests | ||||
88 | // with multiple simultaneous IO operations. | ||||
[email protected] | 565ddfa | 2011-12-22 20:44:22 | [diff] [blame] | 89 | class CallbackTest { |
[email protected] | 2437576 | 2011-07-07 02:12:38 | [diff] [blame] | 90 | public: |
91 | // Creates a new CallbackTest object. When the callback is called, it will | ||||
[email protected] | 70740b7 | 2013-05-10 22:39:39 | [diff] [blame] | 92 | // update |helper|. If |reuse| is false and a callback is called more than |
93 | // once, or if |reuse| is true and a callback is called more than twice, an | ||||
94 | // error will be reported to |helper|. | ||||
[email protected] | 2437576 | 2011-07-07 02:12:38 | [diff] [blame] | 95 | CallbackTest(MessageLoopHelper* helper, bool reuse); |
[email protected] | 565ddfa | 2011-12-22 20:44:22 | [diff] [blame] | 96 | ~CallbackTest(); |
[email protected] | 2437576 | 2011-07-07 02:12:38 | [diff] [blame] | 97 | |
[email protected] | 70740b7 | 2013-05-10 22:39:39 | [diff] [blame] | 98 | void Run(int result); |
99 | |||||
100 | int last_result() const { return last_result_; } | ||||
[email protected] | 2437576 | 2011-07-07 02:12:38 | [diff] [blame] | 101 | |
102 | private: | ||||
103 | MessageLoopHelper* helper_; | ||||
104 | int reuse_; | ||||
[email protected] | 70740b7 | 2013-05-10 22:39:39 | [diff] [blame] | 105 | int last_result_; |
[email protected] | 2437576 | 2011-07-07 02:12:38 | [diff] [blame] | 106 | DISALLOW_COPY_AND_ASSIGN(CallbackTest); |
107 | }; | ||||
108 | |||||
[email protected] | 2d31666 | 2008-09-03 18:18:14 | [diff] [blame] | 109 | #endif // NET_DISK_CACHE_DISK_CACHE_TEST_UTIL_H_ |