disk_cache_test_base.cc 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  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. #include "net/disk_cache/disk_cache_test_base.h"
  5. #include <memory>
  6. #include <utility>
  7. #include "base/bind.h"
  8. #include "base/files/file_util.h"
  9. #include "base/path_service.h"
  10. #include "base/run_loop.h"
  11. #include "base/task/single_thread_task_runner.h"
  12. #include "base/threading/platform_thread.h"
  13. #include "base/threading/thread_task_runner_handle.h"
  14. #include "net/base/io_buffer.h"
  15. #include "net/base/net_errors.h"
  16. #include "net/base/request_priority.h"
  17. #include "net/base/test_completion_callback.h"
  18. #include "net/disk_cache/backend_cleanup_tracker.h"
  19. #include "net/disk_cache/blockfile/backend_impl.h"
  20. #include "net/disk_cache/cache_util.h"
  21. #include "net/disk_cache/disk_cache.h"
  22. #include "net/disk_cache/disk_cache_test_util.h"
  23. #include "net/disk_cache/memory/mem_backend_impl.h"
  24. #include "net/disk_cache/simple/simple_backend_impl.h"
  25. #include "net/disk_cache/simple/simple_file_tracker.h"
  26. #include "net/disk_cache/simple/simple_index.h"
  27. #include "net/test/gtest_util.h"
  28. #include "testing/gmock/include/gmock/gmock.h"
  29. #include "testing/gtest/include/gtest/gtest.h"
  30. using net::test::IsOk;
  31. DiskCacheTest::DiskCacheTest() {
  32. CHECK(temp_dir_.CreateUniqueTempDir());
  33. // Put the cache into a subdir of |temp_dir_|, to permit tests to safely
  34. // remove the cache directory without risking collisions with other tests.
  35. cache_path_ = temp_dir_.GetPath().AppendASCII("cache");
  36. CHECK(base::CreateDirectory(cache_path_));
  37. }
  38. DiskCacheTest::~DiskCacheTest() = default;
  39. bool DiskCacheTest::CopyTestCache(const std::string& name) {
  40. base::FilePath path;
  41. base::PathService::Get(base::DIR_SOURCE_ROOT, &path);
  42. path = path.AppendASCII("net");
  43. path = path.AppendASCII("data");
  44. path = path.AppendASCII("cache_tests");
  45. path = path.AppendASCII(name);
  46. if (!CleanupCacheDir())
  47. return false;
  48. return base::CopyDirectory(path, cache_path_, false);
  49. }
  50. bool DiskCacheTest::CleanupCacheDir() {
  51. return DeleteCache(cache_path_);
  52. }
  53. void DiskCacheTest::TearDown() {
  54. RunUntilIdle();
  55. }
  56. DiskCacheTestWithCache::TestIterator::TestIterator(
  57. std::unique_ptr<disk_cache::Backend::Iterator> iterator)
  58. : iterator_(std::move(iterator)) {}
  59. DiskCacheTestWithCache::TestIterator::~TestIterator() = default;
  60. int DiskCacheTestWithCache::TestIterator::OpenNextEntry(
  61. disk_cache::Entry** next_entry) {
  62. TestEntryResultCompletionCallback cb;
  63. disk_cache::EntryResult result =
  64. cb.GetResult(iterator_->OpenNextEntry(cb.callback()));
  65. int rv = result.net_error();
  66. *next_entry = result.ReleaseEntry();
  67. return rv;
  68. }
  69. DiskCacheTestWithCache::DiskCacheTestWithCache() = default;
  70. DiskCacheTestWithCache::~DiskCacheTestWithCache() = default;
  71. void DiskCacheTestWithCache::InitCache() {
  72. if (memory_only_)
  73. InitMemoryCache();
  74. else
  75. InitDiskCache();
  76. ASSERT_TRUE(nullptr != cache_);
  77. if (first_cleanup_)
  78. ASSERT_EQ(0, cache_->GetEntryCount());
  79. }
  80. // We are expected to leak memory when simulating crashes.
  81. void DiskCacheTestWithCache::SimulateCrash() {
  82. ASSERT_TRUE(!memory_only_);
  83. net::TestCompletionCallback cb;
  84. int rv = cache_impl_->FlushQueueForTest(cb.callback());
  85. ASSERT_THAT(cb.GetResult(rv), IsOk());
  86. cache_impl_->ClearRefCountForTest();
  87. cache_.reset();
  88. EXPECT_TRUE(CheckCacheIntegrity(cache_path_, new_eviction_, size_, mask_));
  89. CreateBackend(disk_cache::kNoRandom);
  90. }
  91. void DiskCacheTestWithCache::SetTestMode() {
  92. ASSERT_TRUE(!memory_only_);
  93. cache_impl_->SetUnitTestMode();
  94. }
  95. void DiskCacheTestWithCache::SetMaxSize(int64_t size, bool should_succeed) {
  96. size_ = size;
  97. if (simple_cache_impl_)
  98. EXPECT_EQ(should_succeed, simple_cache_impl_->SetMaxSize(size));
  99. if (cache_impl_)
  100. EXPECT_EQ(should_succeed, cache_impl_->SetMaxSize(size));
  101. if (mem_cache_)
  102. EXPECT_EQ(should_succeed, mem_cache_->SetMaxSize(size));
  103. }
  104. disk_cache::EntryResult DiskCacheTestWithCache::OpenOrCreateEntry(
  105. const std::string& key) {
  106. return OpenOrCreateEntryWithPriority(key, net::HIGHEST);
  107. }
  108. disk_cache::EntryResult DiskCacheTestWithCache::OpenOrCreateEntryWithPriority(
  109. const std::string& key,
  110. net::RequestPriority request_priority) {
  111. TestEntryResultCompletionCallback cb;
  112. disk_cache::EntryResult result =
  113. cache_->OpenOrCreateEntry(key, request_priority, cb.callback());
  114. return cb.GetResult(std::move(result));
  115. }
  116. int DiskCacheTestWithCache::OpenEntry(const std::string& key,
  117. disk_cache::Entry** entry) {
  118. return OpenEntryWithPriority(key, net::HIGHEST, entry);
  119. }
  120. int DiskCacheTestWithCache::OpenEntryWithPriority(
  121. const std::string& key,
  122. net::RequestPriority request_priority,
  123. disk_cache::Entry** entry) {
  124. TestEntryResultCompletionCallback cb;
  125. disk_cache::EntryResult result =
  126. cb.GetResult(cache_->OpenEntry(key, request_priority, cb.callback()));
  127. int rv = result.net_error();
  128. *entry = result.ReleaseEntry();
  129. return rv;
  130. }
  131. int DiskCacheTestWithCache::CreateEntry(const std::string& key,
  132. disk_cache::Entry** entry) {
  133. return CreateEntryWithPriority(key, net::HIGHEST, entry);
  134. }
  135. int DiskCacheTestWithCache::CreateEntryWithPriority(
  136. const std::string& key,
  137. net::RequestPriority request_priority,
  138. disk_cache::Entry** entry) {
  139. TestEntryResultCompletionCallback cb;
  140. disk_cache::EntryResult result =
  141. cb.GetResult(cache_->CreateEntry(key, request_priority, cb.callback()));
  142. int rv = result.net_error();
  143. *entry = result.ReleaseEntry();
  144. return rv;
  145. }
  146. int DiskCacheTestWithCache::DoomEntry(const std::string& key) {
  147. net::TestCompletionCallback cb;
  148. int rv = cache_->DoomEntry(key, net::HIGHEST, cb.callback());
  149. return cb.GetResult(rv);
  150. }
  151. int DiskCacheTestWithCache::DoomAllEntries() {
  152. net::TestCompletionCallback cb;
  153. int rv = cache_->DoomAllEntries(cb.callback());
  154. return cb.GetResult(rv);
  155. }
  156. int DiskCacheTestWithCache::DoomEntriesBetween(const base::Time initial_time,
  157. const base::Time end_time) {
  158. net::TestCompletionCallback cb;
  159. int rv = cache_->DoomEntriesBetween(initial_time, end_time, cb.callback());
  160. return cb.GetResult(rv);
  161. }
  162. int DiskCacheTestWithCache::DoomEntriesSince(const base::Time initial_time) {
  163. net::TestCompletionCallback cb;
  164. int rv = cache_->DoomEntriesSince(initial_time, cb.callback());
  165. return cb.GetResult(rv);
  166. }
  167. int64_t DiskCacheTestWithCache::CalculateSizeOfAllEntries() {
  168. net::TestInt64CompletionCallback cb;
  169. int64_t rv = cache_->CalculateSizeOfAllEntries(cb.callback());
  170. return cb.GetResult(rv);
  171. }
  172. int64_t DiskCacheTestWithCache::CalculateSizeOfEntriesBetween(
  173. const base::Time initial_time,
  174. const base::Time end_time) {
  175. net::TestInt64CompletionCallback cb;
  176. int64_t rv = cache_->CalculateSizeOfEntriesBetween(initial_time, end_time,
  177. cb.callback());
  178. return cb.GetResult(rv);
  179. }
  180. std::unique_ptr<DiskCacheTestWithCache::TestIterator>
  181. DiskCacheTestWithCache::CreateIterator() {
  182. return std::make_unique<TestIterator>(cache_->CreateIterator());
  183. }
  184. void DiskCacheTestWithCache::FlushQueueForTest() {
  185. if (memory_only_)
  186. return;
  187. if (simple_cache_impl_) {
  188. disk_cache::FlushCacheThreadForTesting();
  189. return;
  190. }
  191. DCHECK(cache_impl_);
  192. net::TestCompletionCallback cb;
  193. int rv = cache_impl_->FlushQueueForTest(cb.callback());
  194. EXPECT_THAT(cb.GetResult(rv), IsOk());
  195. }
  196. void DiskCacheTestWithCache::RunTaskForTest(base::OnceClosure closure) {
  197. if (memory_only_ || !cache_impl_) {
  198. std::move(closure).Run();
  199. return;
  200. }
  201. net::TestCompletionCallback cb;
  202. int rv = cache_impl_->RunTaskForTest(std::move(closure), cb.callback());
  203. EXPECT_THAT(cb.GetResult(rv), IsOk());
  204. }
  205. int DiskCacheTestWithCache::ReadData(disk_cache::Entry* entry,
  206. int index,
  207. int offset,
  208. net::IOBuffer* buf,
  209. int len) {
  210. net::TestCompletionCallback cb;
  211. int rv = entry->ReadData(index, offset, buf, len, cb.callback());
  212. return cb.GetResult(rv);
  213. }
  214. int DiskCacheTestWithCache::WriteData(disk_cache::Entry* entry,
  215. int index,
  216. int offset,
  217. net::IOBuffer* buf,
  218. int len,
  219. bool truncate) {
  220. net::TestCompletionCallback cb;
  221. int rv = entry->WriteData(index, offset, buf, len, cb.callback(), truncate);
  222. return cb.GetResult(rv);
  223. }
  224. int DiskCacheTestWithCache::ReadSparseData(disk_cache::Entry* entry,
  225. int64_t offset,
  226. net::IOBuffer* buf,
  227. int len) {
  228. net::TestCompletionCallback cb;
  229. int rv = entry->ReadSparseData(offset, buf, len, cb.callback());
  230. return cb.GetResult(rv);
  231. }
  232. int DiskCacheTestWithCache::WriteSparseData(disk_cache::Entry* entry,
  233. int64_t offset,
  234. net::IOBuffer* buf,
  235. int len) {
  236. net::TestCompletionCallback cb;
  237. int rv = entry->WriteSparseData(offset, buf, len, cb.callback());
  238. return cb.GetResult(rv);
  239. }
  240. int DiskCacheTestWithCache::GetAvailableRange(disk_cache::Entry* entry,
  241. int64_t offset,
  242. int len,
  243. int64_t* start) {
  244. TestRangeResultCompletionCallback cb;
  245. disk_cache::RangeResult result =
  246. cb.GetResult(entry->GetAvailableRange(offset, len, cb.callback()));
  247. if (result.net_error == net::OK) {
  248. *start = result.start;
  249. return result.available_len;
  250. }
  251. return result.net_error;
  252. }
  253. void DiskCacheTestWithCache::TrimForTest(bool empty) {
  254. if (memory_only_ || !cache_impl_)
  255. return;
  256. RunTaskForTest(base::BindOnce(&disk_cache::BackendImpl::TrimForTest,
  257. base::Unretained(cache_impl_), empty));
  258. }
  259. void DiskCacheTestWithCache::TrimDeletedListForTest(bool empty) {
  260. if (memory_only_ || !cache_impl_)
  261. return;
  262. RunTaskForTest(
  263. base::BindOnce(&disk_cache::BackendImpl::TrimDeletedListForTest,
  264. base::Unretained(cache_impl_), empty));
  265. }
  266. void DiskCacheTestWithCache::AddDelay() {
  267. if (simple_cache_mode_) {
  268. // The simple cache uses second resolution for many timeouts, so it's safest
  269. // to advance by at least whole seconds before falling back into the normal
  270. // disk cache epsilon advance.
  271. const base::Time initial_time = base::Time::Now();
  272. do {
  273. base::PlatformThread::YieldCurrentThread();
  274. } while (base::Time::Now() - initial_time < base::Seconds(1));
  275. }
  276. base::Time initial = base::Time::Now();
  277. while (base::Time::Now() <= initial) {
  278. base::PlatformThread::Sleep(base::Milliseconds(1));
  279. };
  280. }
  281. void DiskCacheTestWithCache::OnExternalCacheHit(const std::string& key) {
  282. cache_->OnExternalCacheHit(key);
  283. }
  284. void DiskCacheTestWithCache::TearDown() {
  285. RunUntilIdle();
  286. cache_.reset();
  287. if (!memory_only_ && !simple_cache_mode_ && integrity_) {
  288. EXPECT_TRUE(CheckCacheIntegrity(cache_path_, new_eviction_, size_, mask_));
  289. }
  290. RunUntilIdle();
  291. if (simple_cache_mode_ && simple_file_tracker_)
  292. EXPECT_TRUE(simple_file_tracker_->IsEmptyForTesting());
  293. DiskCacheTest::TearDown();
  294. }
  295. void DiskCacheTestWithCache::InitMemoryCache() {
  296. auto cache = std::make_unique<disk_cache::MemBackendImpl>(nullptr);
  297. mem_cache_ = cache.get();
  298. cache_ = std::move(cache);
  299. ASSERT_TRUE(cache_);
  300. if (size_)
  301. EXPECT_TRUE(mem_cache_->SetMaxSize(size_));
  302. ASSERT_TRUE(mem_cache_->Init());
  303. }
  304. void DiskCacheTestWithCache::InitDiskCache() {
  305. if (first_cleanup_)
  306. ASSERT_TRUE(CleanupCacheDir());
  307. CreateBackend(disk_cache::kNoRandom);
  308. }
  309. void DiskCacheTestWithCache::CreateBackend(uint32_t flags) {
  310. scoped_refptr<base::SingleThreadTaskRunner> runner;
  311. if (use_current_thread_)
  312. runner = base::ThreadTaskRunnerHandle::Get();
  313. else
  314. runner = nullptr; // let the backend sort it out.
  315. if (simple_cache_mode_) {
  316. DCHECK(!use_current_thread_)
  317. << "Using current thread unsupported by SimpleCache";
  318. net::TestCompletionCallback cb;
  319. // We limit ourselves to 64 fds since OS X by default gives us 256.
  320. // (Chrome raises the number on startup, but the test fixture doesn't).
  321. if (!simple_file_tracker_)
  322. simple_file_tracker_ =
  323. std::make_unique<disk_cache::SimpleFileTracker>(64);
  324. std::unique_ptr<disk_cache::SimpleBackendImpl> simple_backend =
  325. std::make_unique<disk_cache::SimpleBackendImpl>(
  326. /*file_operations=*/nullptr, cache_path_,
  327. /* cleanup_tracker = */ nullptr, simple_file_tracker_.get(), size_,
  328. type_, /*net_log = */ nullptr);
  329. simple_backend->Init(cb.callback());
  330. ASSERT_THAT(cb.WaitForResult(), IsOk());
  331. simple_cache_impl_ = simple_backend.get();
  332. cache_ = std::move(simple_backend);
  333. if (simple_cache_wait_for_index_) {
  334. net::TestCompletionCallback wait_for_index_cb;
  335. simple_cache_impl_->index()->ExecuteWhenReady(
  336. wait_for_index_cb.callback());
  337. int rv = wait_for_index_cb.WaitForResult();
  338. ASSERT_THAT(rv, IsOk());
  339. }
  340. return;
  341. }
  342. std::unique_ptr<disk_cache::BackendImpl> cache;
  343. if (mask_) {
  344. cache = std::make_unique<disk_cache::BackendImpl>(cache_path_, mask_,
  345. runner, type_,
  346. /* net_log = */ nullptr);
  347. } else {
  348. cache = std::make_unique<disk_cache::BackendImpl>(
  349. cache_path_, /* cleanup_tracker = */ nullptr, runner, type_,
  350. /* net_log = */ nullptr);
  351. }
  352. cache_impl_ = cache.get();
  353. cache_ = std::move(cache);
  354. ASSERT_TRUE(cache_);
  355. if (size_)
  356. EXPECT_TRUE(cache_impl_->SetMaxSize(size_));
  357. if (new_eviction_)
  358. cache_impl_->SetNewEviction();
  359. cache_impl_->SetFlags(flags);
  360. net::TestCompletionCallback cb;
  361. cache_impl_->Init(cb.callback());
  362. ASSERT_THAT(cb.WaitForResult(), IsOk());
  363. }