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