blob: a79fa0710fcd116fb623af452f421a56f8c7888d [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"
Gabriel Charetteb71eec892017-09-14 22:52:5615#include "base/run_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
Maks Orlovichf3860652017-12-13 18:03:1631// Returns true if the cache is not corrupt. Assumes blockfile cache.
32// |max_size|, if non-zero, will be set as its size.
Avi Drissman13fc8932015-12-20 04:40:4633bool CheckCacheIntegrity(const base::FilePath& path,
34 bool new_eviction,
Maks Orlovichf3860652017-12-13 18:03:1635 int max_size,
Avi Drissman13fc8932015-12-20 04:40:4636 uint32_t mask);
initial.commit586acc5fe2008-07-26 22:42:5237
38// -----------------------------------------------------------------------
39
initial.commit586acc5fe2008-07-26 22:42:5240// Simple helper to deal with the message loop on a test.
41class MessageLoopHelper {
42 public:
43 MessageLoopHelper();
[email protected]601858c02010-09-01 17:08:2044 ~MessageLoopHelper();
initial.commit586acc5fe2008-07-26 22:42:5245
46 // Run the message loop and wait for num_callbacks before returning. Returns
[email protected]24375762011-07-07 02:12:3847 // 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.commit586acc5fe2008-07-26 22:42:5249 bool WaitUntilCacheIoFinished(int num_callbacks);
50
[email protected]24375762011-07-07 02:12:3851 // 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.commit586acc5fe2008-07-26 22:42:5263 private:
[email protected]2d316662008-09-03 18:18:1464 // 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 Ulanovd160bd7e2017-09-12 22:04:4074 std::unique_ptr<base::RunLoop> run_loop_;
[email protected]2d316662008-09-03 18:18:1475 int num_callbacks_;
76 int num_iterations_;
77 int last_;
78 bool completed_;
79
[email protected]24375762011-07-07 02:12:3880 // True if a callback was called/reused more than expected.
81 bool callback_reused_error_;
82 int callbacks_called_;
83
[email protected]2d316662008-09-03 18:18:1484 DISALLOW_COPY_AND_ASSIGN(MessageLoopHelper);
initial.commit586acc5fe2008-07-26 22:42:5285};
86
[email protected]24375762011-07-07 02:12:3887// -----------------------------------------------------------------------
88
89// Simple callback to process IO completions from the cache. It allows tests
90// with multiple simultaneous IO operations.
[email protected]565ddfa2011-12-22 20:44:2291class CallbackTest {
[email protected]24375762011-07-07 02:12:3892 public:
93 // Creates a new CallbackTest object. When the callback is called, it will
[email protected]70740b72013-05-10 22:39:3994 // 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]24375762011-07-07 02:12:3897 CallbackTest(MessageLoopHelper* helper, bool reuse);
[email protected]565ddfa2011-12-22 20:44:2298 ~CallbackTest();
[email protected]24375762011-07-07 02:12:3899
[email protected]70740b72013-05-10 22:39:39100 void Run(int result);
101
102 int last_result() const { return last_result_; }
[email protected]24375762011-07-07 02:12:38103
104 private:
105 MessageLoopHelper* helper_;
106 int reuse_;
[email protected]70740b72013-05-10 22:39:39107 int last_result_;
[email protected]24375762011-07-07 02:12:38108 DISALLOW_COPY_AND_ASSIGN(CallbackTest);
109};
110
[email protected]2d316662008-09-03 18:18:14111#endif // NET_DISK_CACHE_DISK_CACHE_TEST_UTIL_H_