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" 119ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch#include "base/message_loop/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) 195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Deletes all file son the cache. 202a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)bool DeleteCache(const base::FilePath& path); 215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Fills buffer with random values (may contain nulls unless no_nulls is true). 235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void CacheTestFillBuffer(char* buffer, size_t len, bool no_nulls); 245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Generates a random key of up to 200 bytes. 265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)std::string GenerateKey(bool same_length); 275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Returns true if the cache is not corrupt. 292a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)bool CheckCacheIntegrity(const base::FilePath& path, bool new_eviction, 302a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) uint32 mask); 315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// ----------------------------------------------------------------------- 335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Simple helper to deal with the message loop on a test. 355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class MessageLoopHelper { 365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) public: 375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) MessageLoopHelper(); 385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) ~MessageLoopHelper(); 395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Run the message loop and wait for num_callbacks before returning. Returns 415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // false if we are waiting to long. Each callback that will be waited on is 425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // required to call CallbackWasCalled() to indicate when it was called. 435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) bool WaitUntilCacheIoFinished(int num_callbacks); 445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // True if a given callback was called more times than it expected. 465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) bool callback_reused_error() const { return callback_reused_error_; } 475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) void set_callback_reused_error(bool error) { 485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) callback_reused_error_ = error; 495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) } 505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) int callbacks_called() const { return callbacks_called_; } 525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Report that a callback was called. Each callback that will be waited on 535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // via WaitUntilCacheIoFinished() is expected to call this method to 545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // indicate when it has been executed. 555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) void CallbackWasCalled() { ++callbacks_called_; } 565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) private: 585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Sets the number of callbacks that can be received so far. 595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) void ExpectCallbacks(int num_callbacks) { 605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) num_callbacks_ = num_callbacks; 615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) num_iterations_ = last_ = 0; 625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) completed_ = false; 635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) } 645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Called periodically to test if WaitUntilCacheIoFinished should return. 665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) void TimerExpired(); 675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) base::RepeatingTimer<MessageLoopHelper> timer_; 695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) int num_callbacks_; 705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) int num_iterations_; 715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) int last_; 725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) bool completed_; 735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // True if a callback was called/reused more than expected. 755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) bool callback_reused_error_; 765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) int callbacks_called_; 775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) DISALLOW_COPY_AND_ASSIGN(MessageLoopHelper); 795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}; 805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// ----------------------------------------------------------------------- 825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Simple callback to process IO completions from the cache. It allows tests 845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// with multiple simultaneous IO operations. 855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class CallbackTest { 865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) public: 875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Creates a new CallbackTest object. When the callback is called, it will 88a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles) // update |helper|. If |reuse| is false and a callback is called more than 89a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles) // once, or if |reuse| is true and a callback is called more than twice, an 90a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles) // error will be reported to |helper|. 915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) CallbackTest(MessageLoopHelper* helper, bool reuse); 925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) ~CallbackTest(); 935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 94a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles) void Run(int result); 95a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles) 96a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles) int last_result() const { return last_result_; } 975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) private: 995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) MessageLoopHelper* helper_; 1005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) int reuse_; 101a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles) int last_result_; 1025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) DISALLOW_COPY_AND_ASSIGN(CallbackTest); 1035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}; 1045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif // NET_DISK_CACHE_DISK_CACHE_TEST_UTIL_H_ 106