disk_cache_test_util.cc 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. // Copyright (c) 2011 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. #include "net/disk_cache/disk_cache_test_util.h"
  5. #include "base/check_op.h"
  6. #include "base/files/file.h"
  7. #include "base/files/file_path.h"
  8. #include "base/run_loop.h"
  9. #include "base/threading/thread_task_runner_handle.h"
  10. #include "net/base/net_errors.h"
  11. #include "net/disk_cache/blockfile/backend_impl.h"
  12. #include "net/disk_cache/blockfile/file.h"
  13. #include "net/disk_cache/cache_util.h"
  14. using base::Time;
  15. std::string GenerateKey(bool same_length) {
  16. char key[200];
  17. CacheTestFillBuffer(key, sizeof(key), same_length);
  18. key[199] = '\0';
  19. return std::string(key);
  20. }
  21. void CacheTestFillBuffer(char* buffer, size_t len, bool no_nulls) {
  22. static bool called = false;
  23. if (!called) {
  24. called = true;
  25. int seed = static_cast<int>(Time::Now().ToInternalValue());
  26. srand(seed);
  27. }
  28. for (size_t i = 0; i < len; i++) {
  29. buffer[i] = static_cast<char>(rand());
  30. if (!buffer[i] && no_nulls)
  31. buffer[i] = 'g';
  32. }
  33. if (len && !buffer[0])
  34. buffer[0] = 'g';
  35. }
  36. bool CreateCacheTestFile(const base::FilePath& name) {
  37. int flags = base::File::FLAG_CREATE_ALWAYS | base::File::FLAG_READ |
  38. base::File::FLAG_WRITE;
  39. base::File file(name, flags);
  40. if (!file.IsValid())
  41. return false;
  42. file.SetLength(4 * 1024 * 1024);
  43. return true;
  44. }
  45. bool DeleteCache(const base::FilePath& path) {
  46. disk_cache::DeleteCache(path, false);
  47. return true;
  48. }
  49. bool CheckCacheIntegrity(const base::FilePath& path,
  50. bool new_eviction,
  51. int max_size,
  52. uint32_t mask) {
  53. auto cache = std::make_unique<disk_cache::BackendImpl>(
  54. path, mask, base::ThreadTaskRunnerHandle::Get(), net::DISK_CACHE,
  55. nullptr);
  56. if (max_size)
  57. cache->SetMaxSize(max_size);
  58. if (!cache.get())
  59. return false;
  60. if (new_eviction)
  61. cache->SetNewEviction();
  62. cache->SetFlags(disk_cache::kNoRandom);
  63. if (cache->SyncInit() != net::OK)
  64. return false;
  65. return cache->SelfCheck() >= 0;
  66. }
  67. // -----------------------------------------------------------------------
  68. TestBackendResultCompletionCallback::TestBackendResultCompletionCallback() =
  69. default;
  70. TestBackendResultCompletionCallback::~TestBackendResultCompletionCallback() =
  71. default;
  72. disk_cache::BackendResultCallback
  73. TestBackendResultCompletionCallback::callback() {
  74. return base::BindOnce(&TestBackendResultCompletionCallback::SetResult,
  75. base::Unretained(this));
  76. }
  77. TestEntryResultCompletionCallback::TestEntryResultCompletionCallback() =
  78. default;
  79. TestEntryResultCompletionCallback::~TestEntryResultCompletionCallback() =
  80. default;
  81. disk_cache::Backend::EntryResultCallback
  82. TestEntryResultCompletionCallback::callback() {
  83. return base::BindOnce(&TestEntryResultCompletionCallback::SetResult,
  84. base::Unretained(this));
  85. }
  86. TestRangeResultCompletionCallback::TestRangeResultCompletionCallback() =
  87. default;
  88. TestRangeResultCompletionCallback::~TestRangeResultCompletionCallback() =
  89. default;
  90. disk_cache::RangeResultCallback TestRangeResultCompletionCallback::callback() {
  91. return base::BindOnce(&TestRangeResultCompletionCallback::HelpSetResult,
  92. base::Unretained(this));
  93. }
  94. void TestRangeResultCompletionCallback::HelpSetResult(
  95. const disk_cache::RangeResult& result) {
  96. SetResult(result);
  97. }
  98. // -----------------------------------------------------------------------
  99. MessageLoopHelper::MessageLoopHelper() = default;
  100. MessageLoopHelper::~MessageLoopHelper() = default;
  101. bool MessageLoopHelper::WaitUntilCacheIoFinished(int num_callbacks) {
  102. if (num_callbacks == callbacks_called_)
  103. return true;
  104. ExpectCallbacks(num_callbacks);
  105. // Create a recurrent timer of 50 ms.
  106. base::RepeatingTimer timer;
  107. timer.Start(FROM_HERE, base::Milliseconds(50), this,
  108. &MessageLoopHelper::TimerExpired);
  109. run_loop_ = std::make_unique<base::RunLoop>();
  110. run_loop_->Run();
  111. run_loop_.reset();
  112. return completed_;
  113. }
  114. // Quits the message loop when all callbacks are called or we've been waiting
  115. // too long for them (2 secs without a callback).
  116. void MessageLoopHelper::TimerExpired() {
  117. CHECK_LE(callbacks_called_, num_callbacks_);
  118. if (callbacks_called_ == num_callbacks_) {
  119. completed_ = true;
  120. run_loop_->Quit();
  121. } else {
  122. // Not finished yet. See if we have to abort.
  123. if (last_ == callbacks_called_)
  124. num_iterations_++;
  125. else
  126. last_ = callbacks_called_;
  127. if (40 == num_iterations_)
  128. run_loop_->Quit();
  129. }
  130. }
  131. // -----------------------------------------------------------------------
  132. CallbackTest::CallbackTest(MessageLoopHelper* helper,
  133. bool reuse)
  134. : helper_(helper),
  135. reuse_(reuse ? 0 : 1) {
  136. }
  137. CallbackTest::~CallbackTest() = default;
  138. // On the actual callback, increase the number of tests received and check for
  139. // errors (an unexpected test received)
  140. void CallbackTest::Run(int result) {
  141. last_result_ = result;
  142. if (reuse_) {
  143. DCHECK_EQ(1, reuse_);
  144. if (2 == reuse_)
  145. helper_->set_callback_reused_error(true);
  146. reuse_++;
  147. }
  148. helper_->CallbackWasCalled();
  149. }
  150. void CallbackTest::RunWithEntry(disk_cache::EntryResult result) {
  151. last_entry_result_ = std::move(result);
  152. Run(last_entry_result_.net_error());
  153. }