disk_cache_test_util.h revision eb525c5499e34cc9c4b825d6d9e75bb07cc06ace
15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Copyright (c) 2012 The Chromium Authors. All rights reserved.
25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// found in the LICENSE file.
45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
55821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#ifndef NET_DISK_CACHE_DISK_CACHE_TEST_UTIL_H_
65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define NET_DISK_CACHE_DISK_CACHE_TEST_UTIL_H_
75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <string>
95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
102a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "base/files/file_path.h"
115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/message_loop.h"
12eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch#include "base/timer/timer.h"
135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/tuple.h"
145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "build/build_config.h"
155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Re-creates a given test file inside the cache test folder.
172a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)bool CreateCacheTestFile(const base::FilePath& name);
185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
197d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)// Re-creates a given test file inside the cache test folder of the given size.
207d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)bool CreateCacheTestFileWithSize(const base::FilePath& name, size_t size);
217d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Deletes all file son the cache.
232a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)bool DeleteCache(const base::FilePath& path);
245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Fills buffer with random values (may contain nulls unless no_nulls is true).
265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void CacheTestFillBuffer(char* buffer, size_t len, bool no_nulls);
275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Generates a random key of up to 200 bytes.
295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)std::string GenerateKey(bool same_length);
305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Returns true if the cache is not corrupt.
322a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)bool CheckCacheIntegrity(const base::FilePath& path, bool new_eviction,
332a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                         uint32 mask);
345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// -----------------------------------------------------------------------
365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Simple helper to deal with the message loop on a test.
385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class MessageLoopHelper {
395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) public:
405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  MessageLoopHelper();
415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ~MessageLoopHelper();
425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Run the message loop and wait for num_callbacks before returning. Returns
445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // false if we are waiting to long. Each callback that will be waited on is
455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // required to call CallbackWasCalled() to indicate when it was called.
465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool WaitUntilCacheIoFinished(int num_callbacks);
475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // True if a given callback was called more times than it expected.
495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool callback_reused_error() const { return callback_reused_error_; }
505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void set_callback_reused_error(bool error) {
515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    callback_reused_error_ = error;
525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  int callbacks_called() const { return callbacks_called_; }
555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Report that a callback was called. Each callback that will be waited on
565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // via WaitUntilCacheIoFinished() is expected to call this method to
575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // indicate when it has been executed.
585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void CallbackWasCalled() { ++callbacks_called_; }
595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) private:
615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Sets the number of callbacks that can be received so far.
625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void ExpectCallbacks(int num_callbacks) {
635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    num_callbacks_ = num_callbacks;
645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    num_iterations_ = last_ = 0;
655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    completed_ = false;
665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Called periodically to test if WaitUntilCacheIoFinished should return.
695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void TimerExpired();
705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  base::RepeatingTimer<MessageLoopHelper> timer_;
725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  int num_callbacks_;
735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  int num_iterations_;
745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  int last_;
755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool completed_;
765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // True if a callback was called/reused more than expected.
785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool callback_reused_error_;
795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  int callbacks_called_;
805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DISALLOW_COPY_AND_ASSIGN(MessageLoopHelper);
825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)};
835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// -----------------------------------------------------------------------
855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Simple callback to process IO completions from the cache. It allows tests
875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// with multiple simultaneous IO operations.
885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class CallbackTest {
895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) public:
905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Creates a new CallbackTest object. When the callback is called, it will
91a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  // update |helper|. If |reuse| is false and a callback is called more than
92a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  // once, or if |reuse| is true and a callback is called more than twice, an
93a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  // error will be reported to |helper|.
945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  CallbackTest(MessageLoopHelper* helper, bool reuse);
955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ~CallbackTest();
965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
97a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  void Run(int result);
98a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)
99a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  int last_result() const { return last_result_; }
1005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) private:
1025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  MessageLoopHelper* helper_;
1035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  int reuse_;
104a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  int last_result_;
1055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DISALLOW_COPY_AND_ASSIGN(CallbackTest);
1065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)};
1075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif  // NET_DISK_CACHE_DISK_CACHE_TEST_UTIL_H_
109