disk_cache_test_util.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. // Copyright (c) 2012 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. #ifndef NET_DISK_CACHE_DISK_CACHE_TEST_UTIL_H_
  5. #define NET_DISK_CACHE_DISK_CACHE_TEST_UTIL_H_
  6. #include <stddef.h>
  7. #include <stdint.h>
  8. #include <string>
  9. #include "base/files/file_path.h"
  10. #include "base/memory/raw_ptr.h"
  11. #include "base/run_loop.h"
  12. #include "base/timer/timer.h"
  13. #include "build/build_config.h"
  14. #include "net/base/test_completion_callback.h"
  15. #include "net/disk_cache/disk_cache.h"
  16. // Re-creates a given test file inside the cache test folder.
  17. bool CreateCacheTestFile(const base::FilePath& name);
  18. // Deletes all file son the cache.
  19. bool DeleteCache(const base::FilePath& path);
  20. // Fills buffer with random values (may contain nulls unless no_nulls is true).
  21. void CacheTestFillBuffer(char* buffer, size_t len, bool no_nulls);
  22. // Generates a random key of up to 200 bytes.
  23. std::string GenerateKey(bool same_length);
  24. // Returns true if the cache is not corrupt. Assumes blockfile cache.
  25. // |max_size|, if non-zero, will be set as its size.
  26. bool CheckCacheIntegrity(const base::FilePath& path,
  27. bool new_eviction,
  28. int max_size,
  29. uint32_t mask);
  30. // -----------------------------------------------------------------------
  31. // Like net::TestCompletionCallback, but for BackendResultCallback.
  32. struct BackendResultIsPendingHelper {
  33. bool operator()(const disk_cache::BackendResult& result) const {
  34. return result.net_error == net::ERR_IO_PENDING;
  35. }
  36. };
  37. using TestBackendResultCompletionCallbackBase =
  38. net::internal::TestCompletionCallbackTemplate<disk_cache::BackendResult,
  39. BackendResultIsPendingHelper>;
  40. class TestBackendResultCompletionCallback
  41. : public TestBackendResultCompletionCallbackBase {
  42. public:
  43. TestBackendResultCompletionCallback();
  44. TestBackendResultCompletionCallback(
  45. const TestBackendResultCompletionCallback&) = delete;
  46. TestBackendResultCompletionCallback& operator=(
  47. const TestBackendResultCompletionCallback&) = delete;
  48. ~TestBackendResultCompletionCallback() override;
  49. disk_cache::BackendResultCallback callback();
  50. };
  51. // Like net::TestCompletionCallback, but for EntryResultCallback.
  52. struct EntryResultIsPendingHelper {
  53. bool operator()(const disk_cache::EntryResult& result) const {
  54. return result.net_error() == net::ERR_IO_PENDING;
  55. }
  56. };
  57. using TestEntryResultCompletionCallbackBase =
  58. net::internal::TestCompletionCallbackTemplate<disk_cache::EntryResult,
  59. EntryResultIsPendingHelper>;
  60. class TestEntryResultCompletionCallback
  61. : public TestEntryResultCompletionCallbackBase {
  62. public:
  63. TestEntryResultCompletionCallback();
  64. TestEntryResultCompletionCallback(const TestEntryResultCompletionCallback&) =
  65. delete;
  66. TestEntryResultCompletionCallback& operator=(
  67. const TestEntryResultCompletionCallback&) = delete;
  68. ~TestEntryResultCompletionCallback() override;
  69. disk_cache::Backend::EntryResultCallback callback();
  70. };
  71. // Like net::TestCompletionCallback, but for RangeResultCallback.
  72. struct RangeResultIsPendingHelper {
  73. bool operator()(const disk_cache::RangeResult& result) const {
  74. return result.net_error == net::ERR_IO_PENDING;
  75. }
  76. };
  77. class TestRangeResultCompletionCallback
  78. : public net::internal::TestCompletionCallbackTemplate<
  79. disk_cache::RangeResult,
  80. RangeResultIsPendingHelper> {
  81. public:
  82. TestRangeResultCompletionCallback();
  83. ~TestRangeResultCompletionCallback() override;
  84. disk_cache::RangeResultCallback callback();
  85. private:
  86. // Reference -> Value adapter --- disk_cache wants reference for callback,
  87. // base class wants a value.
  88. void HelpSetResult(const disk_cache::RangeResult& result);
  89. };
  90. // -----------------------------------------------------------------------
  91. // Simple helper to deal with the message loop on a test.
  92. class MessageLoopHelper {
  93. public:
  94. MessageLoopHelper();
  95. MessageLoopHelper(const MessageLoopHelper&) = delete;
  96. MessageLoopHelper& operator=(const MessageLoopHelper&) = delete;
  97. ~MessageLoopHelper();
  98. // Run the message loop and wait for num_callbacks before returning. Returns
  99. // false if we are waiting to long. Each callback that will be waited on is
  100. // required to call CallbackWasCalled() to indicate when it was called.
  101. bool WaitUntilCacheIoFinished(int num_callbacks);
  102. // True if a given callback was called more times than it expected.
  103. bool callback_reused_error() const { return callback_reused_error_; }
  104. void set_callback_reused_error(bool error) {
  105. callback_reused_error_ = error;
  106. }
  107. int callbacks_called() const { return callbacks_called_; }
  108. // Report that a callback was called. Each callback that will be waited on
  109. // via WaitUntilCacheIoFinished() is expected to call this method to
  110. // indicate when it has been executed.
  111. void CallbackWasCalled() { ++callbacks_called_; }
  112. private:
  113. // Sets the number of callbacks that can be received so far.
  114. void ExpectCallbacks(int num_callbacks) {
  115. num_callbacks_ = num_callbacks;
  116. num_iterations_ = last_ = 0;
  117. completed_ = false;
  118. }
  119. // Called periodically to test if WaitUntilCacheIoFinished should return.
  120. void TimerExpired();
  121. std::unique_ptr<base::RunLoop> run_loop_;
  122. int num_callbacks_ = 0;
  123. int num_iterations_ = 0;
  124. int last_ = 0;
  125. bool completed_ = false;
  126. // True if a callback was called/reused more than expected.
  127. bool callback_reused_error_ = false;
  128. int callbacks_called_ = 0;
  129. };
  130. // -----------------------------------------------------------------------
  131. // Simple callback to process IO completions from the cache. It allows tests
  132. // with multiple simultaneous IO operations.
  133. class CallbackTest {
  134. public:
  135. // Creates a new CallbackTest object. When the callback is called, it will
  136. // update |helper|. If |reuse| is false and a callback is called more than
  137. // once, or if |reuse| is true and a callback is called more than twice, an
  138. // error will be reported to |helper|.
  139. CallbackTest(MessageLoopHelper* helper, bool reuse);
  140. CallbackTest(const CallbackTest&) = delete;
  141. CallbackTest& operator=(const CallbackTest&) = delete;
  142. ~CallbackTest();
  143. void Run(int result);
  144. void RunWithEntry(disk_cache::EntryResult result);
  145. int last_result() const { return last_result_; }
  146. disk_cache::EntryResult ReleaseLastEntryResult() {
  147. return std::move(last_entry_result_);
  148. }
  149. private:
  150. raw_ptr<MessageLoopHelper> helper_;
  151. int reuse_;
  152. int last_result_;
  153. disk_cache::EntryResult last_entry_result_;
  154. };
  155. #endif // NET_DISK_CACHE_DISK_CACHE_TEST_UTIL_H_