blob: 1dc348b55aee7468bb6c53be0f94471fb9d7a988 [file] [log] [blame]
[email protected]4c03b2e92012-01-03 19:36:571// Copyright (c) 2012 The Chromium Authors. All rights reserved.
license.botbf09a502008-08-24 00:55:552// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
initial.commit586acc5fe2008-07-26 22:42:524
[email protected]2d316662008-09-03 18:18:145#ifndef NET_DISK_CACHE_DISK_CACHE_TEST_UTIL_H_
6#define NET_DISK_CACHE_DISK_CACHE_TEST_UTIL_H_
initial.commit586acc5fe2008-07-26 22:42:527
Avi Drissman13fc8932015-12-20 04:40:468#include <stddef.h>
9#include <stdint.h>
10
initial.commit586acc5fe2008-07-26 22:42:5211#include <string>
12
[email protected]57999812013-02-24 05:40:5213#include "base/files/file_path.h"
Avi Drissman13fc8932015-12-20 04:40:4614#include "base/macros.h"
[email protected]5ee20982013-07-17 21:51:1815#include "base/message_loop/message_loop.h"
[email protected]9da992db2013-06-28 05:40:4716#include "base/timer/timer.h"
[email protected]5306d112009-10-15 17:38:0417#include "build/build_config.h"
initial.commit586acc5fe2008-07-26 22:42:5218
19// Re-creates a given test file inside the cache test folder.
[email protected]6cdfd7f2013-02-08 20:40:1520bool CreateCacheTestFile(const base::FilePath& name);
initial.commit586acc5fe2008-07-26 22:42:5221
22// Deletes all file son the cache.
[email protected]6cdfd7f2013-02-08 20:40:1523bool DeleteCache(const base::FilePath& path);
initial.commit586acc5fe2008-07-26 22:42:5224
initial.commit586acc5fe2008-07-26 22:42:5225// Fills buffer with random values (may contain nulls unless no_nulls is true).
26void CacheTestFillBuffer(char* buffer, size_t len, bool no_nulls);
27
initial.commit586acc5fe2008-07-26 22:42:5228// Generates a random key of up to 200 bytes.
29std::string GenerateKey(bool same_length);
30
31// Returns true if the cache is not corrupt.
Avi Drissman13fc8932015-12-20 04:40:4632bool CheckCacheIntegrity(const base::FilePath& path,
33 bool new_eviction,
34 uint32_t mask);
initial.commit586acc5fe2008-07-26 22:42:5235
36// -----------------------------------------------------------------------
37
initial.commit586acc5fe2008-07-26 22:42:5238// Simple helper to deal with the message loop on a test.
39class MessageLoopHelper {
40 public:
41 MessageLoopHelper();
[email protected]601858c02010-09-01 17:08:2042 ~MessageLoopHelper();
initial.commit586acc5fe2008-07-26 22:42:5243
44 // Run the message loop and wait for num_callbacks before returning. Returns
[email protected]24375762011-07-07 02:12:3845 // 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.commit586acc5fe2008-07-26 22:42:5247 bool WaitUntilCacheIoFinished(int num_callbacks);
48
[email protected]24375762011-07-07 02:12:3849 // 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.commit586acc5fe2008-07-26 22:42:5261 private:
[email protected]2d316662008-09-03 18:18:1462 // 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
danakj8c3eb802015-09-24 07:53:0072 base::RepeatingTimer timer_;
[email protected]2d316662008-09-03 18:18:1473 int num_callbacks_;
74 int num_iterations_;
75 int last_;
76 bool completed_;
77
[email protected]24375762011-07-07 02:12:3878 // True if a callback was called/reused more than expected.
79 bool callback_reused_error_;
80 int callbacks_called_;
81
[email protected]2d316662008-09-03 18:18:1482 DISALLOW_COPY_AND_ASSIGN(MessageLoopHelper);
initial.commit586acc5fe2008-07-26 22:42:5283};
84
[email protected]24375762011-07-07 02:12:3885// -----------------------------------------------------------------------
86
87// Simple callback to process IO completions from the cache. It allows tests
88// with multiple simultaneous IO operations.
[email protected]565ddfa2011-12-22 20:44:2289class CallbackTest {
[email protected]24375762011-07-07 02:12:3890 public:
91 // Creates a new CallbackTest object. When the callback is called, it will
[email protected]70740b72013-05-10 22:39:3992 // 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]24375762011-07-07 02:12:3895 CallbackTest(MessageLoopHelper* helper, bool reuse);
[email protected]565ddfa2011-12-22 20:44:2296 ~CallbackTest();
[email protected]24375762011-07-07 02:12:3897
[email protected]70740b72013-05-10 22:39:3998 void Run(int result);
99
100 int last_result() const { return last_result_; }
[email protected]24375762011-07-07 02:12:38101
102 private:
103 MessageLoopHelper* helper_;
104 int reuse_;
[email protected]70740b72013-05-10 22:39:39105 int last_result_;
[email protected]24375762011-07-07 02:12:38106 DISALLOW_COPY_AND_ASSIGN(CallbackTest);
107};
108
[email protected]2d316662008-09-03 18:18:14109#endif // NET_DISK_CACHE_DISK_CACHE_TEST_UTIL_H_