blob: 02dd6f491c0c3c8f915ad884370812db4e386652 [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"
Gabriel Charetteb71eec892017-09-14 22:52:5616#include "base/run_loop.h"
[email protected]9da992db2013-06-28 05:40:4717#include "base/timer/timer.h"
[email protected]5306d112009-10-15 17:38:0418#include "build/build_config.h"
initial.commit586acc5fe2008-07-26 22:42:5219
20// Re-creates a given test file inside the cache test folder.
[email protected]6cdfd7f2013-02-08 20:40:1521bool CreateCacheTestFile(const base::FilePath& name);
initial.commit586acc5fe2008-07-26 22:42:5222
23// Deletes all file son the cache.
[email protected]6cdfd7f2013-02-08 20:40:1524bool DeleteCache(const base::FilePath& path);
initial.commit586acc5fe2008-07-26 22:42:5225
initial.commit586acc5fe2008-07-26 22:42:5226// Fills buffer with random values (may contain nulls unless no_nulls is true).
27void CacheTestFillBuffer(char* buffer, size_t len, bool no_nulls);
28
initial.commit586acc5fe2008-07-26 22:42:5229// Generates a random key of up to 200 bytes.
30std::string GenerateKey(bool same_length);
31
Maks Orlovichf3860652017-12-13 18:03:1632// Returns true if the cache is not corrupt. Assumes blockfile cache.
33// |max_size|, if non-zero, will be set as its size.
Avi Drissman13fc8932015-12-20 04:40:4634bool CheckCacheIntegrity(const base::FilePath& path,
35 bool new_eviction,
Maks Orlovichf3860652017-12-13 18:03:1636 int max_size,
Avi Drissman13fc8932015-12-20 04:40:4637 uint32_t mask);
initial.commit586acc5fe2008-07-26 22:42:5238
39// -----------------------------------------------------------------------
40
initial.commit586acc5fe2008-07-26 22:42:5241// Simple helper to deal with the message loop on a test.
42class MessageLoopHelper {
43 public:
44 MessageLoopHelper();
[email protected]601858c02010-09-01 17:08:2045 ~MessageLoopHelper();
initial.commit586acc5fe2008-07-26 22:42:5246
47 // Run the message loop and wait for num_callbacks before returning. Returns
[email protected]24375762011-07-07 02:12:3848 // false if we are waiting to long. Each callback that will be waited on is
49 // required to call CallbackWasCalled() to indicate when it was called.
initial.commit586acc5fe2008-07-26 22:42:5250 bool WaitUntilCacheIoFinished(int num_callbacks);
51
[email protected]24375762011-07-07 02:12:3852 // True if a given callback was called more times than it expected.
53 bool callback_reused_error() const { return callback_reused_error_; }
54 void set_callback_reused_error(bool error) {
55 callback_reused_error_ = error;
56 }
57
58 int callbacks_called() const { return callbacks_called_; }
59 // Report that a callback was called. Each callback that will be waited on
60 // via WaitUntilCacheIoFinished() is expected to call this method to
61 // indicate when it has been executed.
62 void CallbackWasCalled() { ++callbacks_called_; }
63
initial.commit586acc5fe2008-07-26 22:42:5264 private:
[email protected]2d316662008-09-03 18:18:1465 // Sets the number of callbacks that can be received so far.
66 void ExpectCallbacks(int num_callbacks) {
67 num_callbacks_ = num_callbacks;
68 num_iterations_ = last_ = 0;
69 completed_ = false;
70 }
71
72 // Called periodically to test if WaitUntilCacheIoFinished should return.
73 void TimerExpired();
74
Sergey Ulanovd160bd7e2017-09-12 22:04:4075 std::unique_ptr<base::RunLoop> run_loop_;
[email protected]2d316662008-09-03 18:18:1476 int num_callbacks_;
77 int num_iterations_;
78 int last_;
79 bool completed_;
80
[email protected]24375762011-07-07 02:12:3881 // True if a callback was called/reused more than expected.
82 bool callback_reused_error_;
83 int callbacks_called_;
84
[email protected]2d316662008-09-03 18:18:1485 DISALLOW_COPY_AND_ASSIGN(MessageLoopHelper);
initial.commit586acc5fe2008-07-26 22:42:5286};
87
[email protected]24375762011-07-07 02:12:3888// -----------------------------------------------------------------------
89
90// Simple callback to process IO completions from the cache. It allows tests
91// with multiple simultaneous IO operations.
[email protected]565ddfa2011-12-22 20:44:2292class CallbackTest {
[email protected]24375762011-07-07 02:12:3893 public:
94 // Creates a new CallbackTest object. When the callback is called, it will
[email protected]70740b72013-05-10 22:39:3995 // update |helper|. If |reuse| is false and a callback is called more than
96 // once, or if |reuse| is true and a callback is called more than twice, an
97 // error will be reported to |helper|.
[email protected]24375762011-07-07 02:12:3898 CallbackTest(MessageLoopHelper* helper, bool reuse);
[email protected]565ddfa2011-12-22 20:44:2299 ~CallbackTest();
[email protected]24375762011-07-07 02:12:38100
[email protected]70740b72013-05-10 22:39:39101 void Run(int result);
102
103 int last_result() const { return last_result_; }
[email protected]24375762011-07-07 02:12:38104
105 private:
106 MessageLoopHelper* helper_;
107 int reuse_;
[email protected]70740b72013-05-10 22:39:39108 int last_result_;
[email protected]24375762011-07-07 02:12:38109 DISALLOW_COPY_AND_ASSIGN(CallbackTest);
110};
111
[email protected]2d316662008-09-03 18:18:14112#endif // NET_DISK_CACHE_DISK_CACHE_TEST_UTIL_H_