disk_cache_test_base.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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_BASE_H_
  5. #define NET_DISK_CACHE_DISK_CACHE_TEST_BASE_H_
  6. #include <stdint.h>
  7. #include <memory>
  8. #include "base/files/file_path.h"
  9. #include "base/files/scoped_temp_dir.h"
  10. #include "base/memory/raw_ptr.h"
  11. #include "base/threading/thread.h"
  12. #include "net/base/cache_type.h"
  13. #include "net/disk_cache/disk_cache.h"
  14. #include "net/test/test_with_task_environment.h"
  15. #include "testing/gtest/include/gtest/gtest.h"
  16. #include "testing/platform_test.h"
  17. namespace net {
  18. class IOBuffer;
  19. } // namespace net
  20. namespace disk_cache {
  21. class Backend;
  22. class BackendImpl;
  23. class Entry;
  24. class MemBackendImpl;
  25. class SimpleBackendImpl;
  26. class SimpleFileTracker;
  27. } // namespace disk_cache
  28. // These tests can use the path service, which uses autoreleased objects on the
  29. // Mac, so this needs to be a PlatformTest. Even tests that do not require a
  30. // cache (and that do not need to be a DiskCacheTestWithCache) are susceptible
  31. // to this problem; all such tests should use TEST_F(DiskCacheTest, ...).
  32. class DiskCacheTest : public PlatformTest, public net::WithTaskEnvironment {
  33. protected:
  34. DiskCacheTest();
  35. ~DiskCacheTest() override;
  36. // Copies a set of cache files from the data folder to the test folder.
  37. bool CopyTestCache(const std::string& name);
  38. // Deletes the contents of |cache_path_|.
  39. bool CleanupCacheDir();
  40. void TearDown() override;
  41. base::FilePath cache_path_;
  42. private:
  43. base::ScopedTempDir temp_dir_;
  44. };
  45. // Provides basic support for cache related tests.
  46. class DiskCacheTestWithCache : public DiskCacheTest {
  47. protected:
  48. class TestIterator {
  49. public:
  50. explicit TestIterator(
  51. std::unique_ptr<disk_cache::Backend::Iterator> iterator);
  52. ~TestIterator();
  53. int OpenNextEntry(disk_cache::Entry** next_entry);
  54. private:
  55. std::unique_ptr<disk_cache::Backend::Iterator> iterator_;
  56. };
  57. DiskCacheTestWithCache();
  58. DiskCacheTestWithCache(const DiskCacheTestWithCache&) = delete;
  59. DiskCacheTestWithCache& operator=(const DiskCacheTestWithCache&) = delete;
  60. ~DiskCacheTestWithCache() override;
  61. void CreateBackend(uint32_t flags);
  62. void InitCache();
  63. void SimulateCrash();
  64. void SetTestMode();
  65. void SetMemoryOnlyMode() {
  66. memory_only_ = true;
  67. }
  68. void SetSimpleCacheMode() {
  69. DCHECK(!use_current_thread_);
  70. simple_cache_mode_ = true;
  71. }
  72. void SetMask(uint32_t mask) { mask_ = mask; }
  73. void SetMaxSize(int64_t size, bool should_succeed = true);
  74. // Returns value last given to SetMaxSize (or 0).
  75. int MaxSize() const { return size_; }
  76. // Deletes and re-creates the files on initialization errors.
  77. void SetForceCreation() {
  78. force_creation_ = true;
  79. }
  80. void SetNewEviction() {
  81. new_eviction_ = true;
  82. }
  83. void DisableSimpleCacheWaitForIndex() {
  84. simple_cache_wait_for_index_ = false;
  85. }
  86. void DisableFirstCleanup() {
  87. first_cleanup_ = false;
  88. }
  89. void DisableIntegrityCheck() {
  90. integrity_ = false;
  91. }
  92. // This is only supported for blockfile cache.
  93. void UseCurrentThread() {
  94. DCHECK(!simple_cache_mode_);
  95. use_current_thread_ = true;
  96. }
  97. void SetCacheType(net::CacheType type) {
  98. type_ = type;
  99. }
  100. // Utility methods to access the cache and wait for each operation to finish.
  101. // Also closer to legacy API.
  102. // TODO(morlovich): Port all the tests to EntryResult.
  103. disk_cache::EntryResult OpenOrCreateEntry(const std::string& key);
  104. disk_cache::EntryResult OpenOrCreateEntryWithPriority(
  105. const std::string& key,
  106. net::RequestPriority request_priority);
  107. int OpenEntry(const std::string& key, disk_cache::Entry** entry);
  108. int OpenEntryWithPriority(const std::string& key,
  109. net::RequestPriority request_priority,
  110. disk_cache::Entry** entry);
  111. int CreateEntry(const std::string& key, disk_cache::Entry** entry);
  112. int CreateEntryWithPriority(const std::string& key,
  113. net::RequestPriority request_priority,
  114. disk_cache::Entry** entry);
  115. int DoomEntry(const std::string& key);
  116. int DoomAllEntries();
  117. int DoomEntriesBetween(const base::Time initial_time,
  118. const base::Time end_time);
  119. int64_t CalculateSizeOfAllEntries();
  120. int64_t CalculateSizeOfEntriesBetween(const base::Time initial_time,
  121. const base::Time end_time);
  122. int DoomEntriesSince(const base::Time initial_time);
  123. std::unique_ptr<TestIterator> CreateIterator();
  124. void FlushQueueForTest();
  125. void RunTaskForTest(base::OnceClosure closure);
  126. int ReadData(disk_cache::Entry* entry, int index, int offset,
  127. net::IOBuffer* buf, int len);
  128. int WriteData(disk_cache::Entry* entry, int index, int offset,
  129. net::IOBuffer* buf, int len, bool truncate);
  130. int ReadSparseData(disk_cache::Entry* entry,
  131. int64_t offset,
  132. net::IOBuffer* buf,
  133. int len);
  134. int WriteSparseData(disk_cache::Entry* entry,
  135. int64_t offset,
  136. net::IOBuffer* buf,
  137. int len);
  138. // TODO(morlovich): Port all the tests using this to RangeResult.
  139. int GetAvailableRange(disk_cache::Entry* entry,
  140. int64_t offset,
  141. int len,
  142. int64_t* start);
  143. // Asks the cache to trim an entry. If |empty| is true, the whole cache is
  144. // deleted.
  145. void TrimForTest(bool empty);
  146. // Asks the cache to trim an entry from the deleted list. If |empty| is
  147. // true, the whole list is deleted.
  148. void TrimDeletedListForTest(bool empty);
  149. // Makes sure that some time passes before continuing the test. Time::Now()
  150. // before and after this method will not be the same.
  151. void AddDelay();
  152. void OnExternalCacheHit(const std::string& key);
  153. void TearDown() override;
  154. // cache_ will always have a valid object, regardless of how the cache was
  155. // initialized. The implementation pointers can be NULL.
  156. std::unique_ptr<disk_cache::Backend> cache_;
  157. raw_ptr<disk_cache::BackendImpl> cache_impl_ = nullptr;
  158. std::unique_ptr<disk_cache::SimpleFileTracker> simple_file_tracker_;
  159. raw_ptr<disk_cache::SimpleBackendImpl> simple_cache_impl_ = nullptr;
  160. raw_ptr<disk_cache::MemBackendImpl> mem_cache_ = nullptr;
  161. uint32_t mask_ = 0;
  162. int64_t size_ = 0;
  163. net::CacheType type_ = net::DISK_CACHE;
  164. bool memory_only_ = false;
  165. bool simple_cache_mode_ = false;
  166. bool simple_cache_wait_for_index_ = true;
  167. bool force_creation_ = false;
  168. bool new_eviction_ = false;
  169. bool first_cleanup_ = true;
  170. bool integrity_ = true;
  171. bool use_current_thread_ = false;
  172. // This is intentionally left uninitialized, to be used by any test.
  173. bool success_;
  174. private:
  175. void InitMemoryCache();
  176. void InitDiskCache();
  177. };
  178. #endif // NET_DISK_CACHE_DISK_CACHE_TEST_BASE_H_