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