13345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick// Copyright (c) 2010 The Chromium Authors. All rights reserved.
2c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// Use of this source code is governed by a BSD-style license that can be
3c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// found in the LICENSE file.
4c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
5c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott#ifndef NET_DISK_CACHE_DISK_CACHE_TEST_UTIL_H_
6c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott#define NET_DISK_CACHE_DISK_CACHE_TEST_UTIL_H_
73345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick#pragma once
8c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
9c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott#include <string>
10c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
11c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch#include "base/callback.h"
12c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott#include "base/file_path.h"
13c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott#include "base/message_loop.h"
14c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott#include "base/timer.h"
15c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott#include "build/build_config.h"
16c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
17c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// Re-creates a given test file inside the cache test folder.
18c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scottbool CreateCacheTestFile(const FilePath& name);
19c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
20c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// Deletes all file son the cache.
21c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scottbool DeleteCache(const FilePath& path);
22c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
23c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// Copies a set of cache files from the data folder to the test folder.
24c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdochbool CopyTestCache(const std::string& name);
25c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
26c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// Gets the path to the cache test folder.
27c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick ScottFilePath GetCacheFilePath();
28c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
29c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// Fills buffer with random values (may contain nulls unless no_nulls is true).
30c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scottvoid CacheTestFillBuffer(char* buffer, size_t len, bool no_nulls);
31c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
32c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// Generates a random key of up to 200 bytes.
33c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scottstd::string GenerateKey(bool same_length);
34c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
35c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// Returns true if the cache is not corrupt.
36c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scottbool CheckCacheIntegrity(const FilePath& path, bool new_eviction);
37c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
38c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// Helper class which ensures that the cache dir returned by GetCacheFilePath
39c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// exists and is clear in ctor and that the directory gets deleted in dtor.
40c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scottclass ScopedTestCache {
41c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott public:
42c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  ScopedTestCache();
433345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick  // Use a specific folder name.
443345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick  explicit ScopedTestCache(const std::string& name);
45c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  ~ScopedTestCache();
46c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
47c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  FilePath path() const { return path_; }
48c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
49c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott private:
50c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  const FilePath path_;  // Path to the cache test folder.
51c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
52c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  DISALLOW_COPY_AND_ASSIGN(ScopedTestCache);
53c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott};
54c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
55c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// -----------------------------------------------------------------------
56c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
57c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// Simple callback to process IO completions from the cache. It allows tests
58c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// with multiple simultaneous IO operations.
59c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scottclass CallbackTest : public CallbackRunner< Tuple1<int> >  {
60c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott public:
61731df977c0511bca2206b5f333555b1205ff1f43Iain Merrick  explicit CallbackTest(bool reuse);
62731df977c0511bca2206b5f333555b1205ff1f43Iain Merrick  virtual ~CallbackTest();
63c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
64c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  int result() const { return result_; }
6572a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen  virtual void RunWithParams(const Tuple1<int>& params);
66c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
67c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott private:
68c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  int result_;
69c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  int reuse_;
70c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  DISALLOW_COPY_AND_ASSIGN(CallbackTest);
71c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott};
72c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
73c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// -----------------------------------------------------------------------
74c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
75c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// Simple helper to deal with the message loop on a test.
76c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scottclass MessageLoopHelper {
77c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott public:
78c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  MessageLoopHelper();
793345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick  ~MessageLoopHelper();
80c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
81c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  // Run the message loop and wait for num_callbacks before returning. Returns
82c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  // false if we are waiting to long.
83c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  bool WaitUntilCacheIoFinished(int num_callbacks);
84c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
85c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott private:
86c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  // Sets the number of callbacks that can be received so far.
87c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  void ExpectCallbacks(int num_callbacks) {
88c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott    num_callbacks_ = num_callbacks;
89c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott    num_iterations_ = last_ = 0;
90c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott    completed_ = false;
91c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  }
92c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
93c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  // Called periodically to test if WaitUntilCacheIoFinished should return.
94c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  void TimerExpired();
95c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
96c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  base::RepeatingTimer<MessageLoopHelper> timer_;
97c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  int num_callbacks_;
98c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  int num_iterations_;
99c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  int last_;
100c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  bool completed_;
101c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
102c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  DISALLOW_COPY_AND_ASSIGN(MessageLoopHelper);
103c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott};
104c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
105c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott#endif  // NET_DISK_CACHE_DISK_CACHE_TEST_UTIL_H_
106