[email protected] | 05f8087d | 2012-06-29 18:58:37 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 4 | |
| 5 | #include "net/disk_cache/disk_cache_test_base.h" |
| 6 | |
Peter Boström | 8a754069 | 2021-04-05 20:48:20 | [diff] [blame] | 7 | #include <memory> |
dcheng | c7eeda42 | 2015-12-26 03:56:48 | [diff] [blame] | 8 | #include <utility> |
| 9 | |
Sebastien Marchand | 6d0558fd | 2019-01-25 16:49:37 | [diff] [blame] | 10 | #include "base/bind.h" |
thestig | d8df033 | 2014-09-04 06:33:29 | [diff] [blame] | 11 | #include "base/files/file_util.h" |
[email protected] | 79b3fec | 2011-11-15 22:33:47 | [diff] [blame] | 12 | #include "base/path_service.h" |
[email protected] | 94a9d1a | 2013-03-24 09:50:31 | [diff] [blame] | 13 | #include "base/run_loop.h" |
Patrick Monette | 643cdf6 | 2021-10-15 19:13:42 | [diff] [blame] | 14 | #include "base/task/single_thread_task_runner.h" |
[email protected] | 8b2f8a5 | 2013-08-28 08:45:35 | [diff] [blame] | 15 | #include "base/threading/platform_thread.h" |
gab | f767595f | 2016-05-11 18:50:35 | [diff] [blame] | 16 | #include "base/threading/thread_task_runner_handle.h" |
[email protected] | e1fcf14 | 2010-08-23 18:47:25 | [diff] [blame] | 17 | #include "net/base/io_buffer.h" |
[email protected] | fb2622f | 2010-07-13 18:00:56 | [diff] [blame] | 18 | #include "net/base/net_errors.h" |
Josh Karlin | dd9a5d14 | 2018-06-06 00:35:48 | [diff] [blame] | 19 | #include "net/base/request_priority.h" |
[email protected] | bc4fb8e | 2010-03-18 23:58:17 | [diff] [blame] | 20 | #include "net/base/test_completion_callback.h" |
Maks Orlovich | 036fd1f | 2017-08-07 17:51:11 | [diff] [blame] | 21 | #include "net/disk_cache/backend_cleanup_tracker.h" |
[email protected] | c2c5cfc | 2014-03-03 16:35:28 | [diff] [blame] | 22 | #include "net/disk_cache/blockfile/backend_impl.h" |
[email protected] | 398ad13 | 2013-04-02 15:11:01 | [diff] [blame] | 23 | #include "net/disk_cache/cache_util.h" |
| 24 | #include "net/disk_cache/disk_cache.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 25 | #include "net/disk_cache/disk_cache_test_util.h" |
[email protected] | c2c5cfc | 2014-03-03 16:35:28 | [diff] [blame] | 26 | #include "net/disk_cache/memory/mem_backend_impl.h" |
[email protected] | 398ad13 | 2013-04-02 15:11:01 | [diff] [blame] | 27 | #include "net/disk_cache/simple/simple_backend_impl.h" |
Maks Orlovich | f378b3a | 2017-10-31 16:27:30 | [diff] [blame] | 28 | #include "net/disk_cache/simple/simple_file_tracker.h" |
[email protected] | 7e48c1b | 2013-07-12 12:15:43 | [diff] [blame] | 29 | #include "net/disk_cache/simple/simple_index.h" |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 30 | #include "net/test/gtest_util.h" |
| 31 | #include "testing/gmock/include/gmock/gmock.h" |
| 32 | #include "testing/gtest/include/gtest/gtest.h" |
| 33 | |
| 34 | using net::test::IsOk; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 35 | |
[email protected] | 79b3fec | 2011-11-15 22:33:47 | [diff] [blame] | 36 | DiskCacheTest::DiskCacheTest() { |
[email protected] | 784f930 | 2012-08-17 00:13:43 | [diff] [blame] | 37 | CHECK(temp_dir_.CreateUniqueTempDir()); |
Maks Orlovich | 8acfd6d | 2019-09-04 00:31:13 | [diff] [blame] | 38 | // Put the cache into a subdir of |temp_dir_|, to permit tests to safely |
| 39 | // remove the cache directory without risking collisions with other tests. |
| 40 | cache_path_ = temp_dir_.GetPath().AppendASCII("cache"); |
| 41 | CHECK(base::CreateDirectory(cache_path_)); |
[email protected] | 79b3fec | 2011-11-15 22:33:47 | [diff] [blame] | 42 | } |
| 43 | |
Chris Watkins | 68b1503 | 2017-12-01 03:07:13 | [diff] [blame] | 44 | DiskCacheTest::~DiskCacheTest() = default; |
[email protected] | 79b3fec | 2011-11-15 22:33:47 | [diff] [blame] | 45 | |
| 46 | bool DiskCacheTest::CopyTestCache(const std::string& name) { |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 47 | base::FilePath path; |
Avi Drissman | 5c80d83 | 2018-05-01 17:01:19 | [diff] [blame] | 48 | base::PathService::Get(base::DIR_SOURCE_ROOT, &path); |
[email protected] | 79b3fec | 2011-11-15 22:33:47 | [diff] [blame] | 49 | path = path.AppendASCII("net"); |
| 50 | path = path.AppendASCII("data"); |
| 51 | path = path.AppendASCII("cache_tests"); |
| 52 | path = path.AppendASCII(name); |
| 53 | |
| 54 | if (!CleanupCacheDir()) |
| 55 | return false; |
[email protected] | f0ff2ad | 2013-07-09 17:42:26 | [diff] [blame] | 56 | return base::CopyDirectory(path, cache_path_, false); |
[email protected] | 79b3fec | 2011-11-15 22:33:47 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | bool DiskCacheTest::CleanupCacheDir() { |
| 60 | return DeleteCache(cache_path_); |
| 61 | } |
| 62 | |
[email protected] | 17b8914 | 2008-11-07 21:52:15 | [diff] [blame] | 63 | void DiskCacheTest::TearDown() { |
Sergey Ulanov | ed1f494 | 2022-06-01 00:08:31 | [diff] [blame] | 64 | RunUntilIdle(); |
[email protected] | 17b8914 | 2008-11-07 21:52:15 | [diff] [blame] | 65 | } |
| 66 | |
gavinp | b1aaa05 | 2014-09-24 14:54:35 | [diff] [blame] | 67 | DiskCacheTestWithCache::TestIterator::TestIterator( |
danakj | d04b92d | 2016-04-16 01:16:49 | [diff] [blame] | 68 | std::unique_ptr<disk_cache::Backend::Iterator> iterator) |
dcheng | c7eeda42 | 2015-12-26 03:56:48 | [diff] [blame] | 69 | : iterator_(std::move(iterator)) {} |
gavinp | b1aaa05 | 2014-09-24 14:54:35 | [diff] [blame] | 70 | |
Chris Watkins | 68b1503 | 2017-12-01 03:07:13 | [diff] [blame] | 71 | DiskCacheTestWithCache::TestIterator::~TestIterator() = default; |
gavinp | b1aaa05 | 2014-09-24 14:54:35 | [diff] [blame] | 72 | |
| 73 | int DiskCacheTestWithCache::TestIterator::OpenNextEntry( |
| 74 | disk_cache::Entry** next_entry) { |
Maks Orlovich | 8efea48 | 2019-08-20 17:14:53 | [diff] [blame] | 75 | TestEntryResultCompletionCallback cb; |
| 76 | disk_cache::EntryResult result = |
| 77 | cb.GetResult(iterator_->OpenNextEntry(cb.callback())); |
| 78 | int rv = result.net_error(); |
| 79 | *next_entry = result.ReleaseEntry(); |
| 80 | return rv; |
gavinp | b1aaa05 | 2014-09-24 14:54:35 | [diff] [blame] | 81 | } |
| 82 | |
Tsuyoshi Horo | 432981d5 | 2022-06-09 09:50:13 | [diff] [blame] | 83 | DiskCacheTestWithCache::DiskCacheTestWithCache() = default; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 84 | |
Chris Watkins | 68b1503 | 2017-12-01 03:07:13 | [diff] [blame] | 85 | DiskCacheTestWithCache::~DiskCacheTestWithCache() = default; |
[email protected] | 7aefb15 | 2011-01-21 23:46:49 | [diff] [blame] | 86 | |
[email protected] | 4d5e036 | 2008-08-28 00:59:06 | [diff] [blame] | 87 | void DiskCacheTestWithCache::InitCache() { |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 88 | if (memory_only_) |
| 89 | InitMemoryCache(); |
| 90 | else |
| 91 | InitDiskCache(); |
| 92 | |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 93 | ASSERT_TRUE(nullptr != cache_); |
[email protected] | a255384 | 2013-04-19 14:25:18 | [diff] [blame] | 94 | if (first_cleanup_) |
[email protected] | a9da16d | 2008-07-30 21:41:54 | [diff] [blame] | 95 | ASSERT_EQ(0, cache_->GetEntryCount()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 96 | } |
| 97 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 98 | // We are expected to leak memory when simulating crashes. |
[email protected] | 4d5e036 | 2008-08-28 00:59:06 | [diff] [blame] | 99 | void DiskCacheTestWithCache::SimulateCrash() { |
[email protected] | 398ad13 | 2013-04-02 15:11:01 | [diff] [blame] | 100 | ASSERT_TRUE(!memory_only_); |
[email protected] | 2a65aceb8 | 2011-12-19 20:59:27 | [diff] [blame] | 101 | net::TestCompletionCallback cb; |
| 102 | int rv = cache_impl_->FlushQueueForTest(cb.callback()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 103 | ASSERT_THAT(cb.GetResult(rv), IsOk()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 104 | cache_impl_->ClearRefCountForTest(); |
| 105 | |
[email protected] | 8c3f5a3 | 2013-08-01 11:57:53 | [diff] [blame] | 106 | cache_.reset(); |
Maks Orlovich | f386065 | 2017-12-13 18:03:16 | [diff] [blame] | 107 | EXPECT_TRUE(CheckCacheIntegrity(cache_path_, new_eviction_, size_, mask_)); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 108 | |
Maks Orlovich | 463dc50 | 2017-07-25 14:52:49 | [diff] [blame] | 109 | CreateBackend(disk_cache::kNoRandom); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 110 | } |
[email protected] | a9da16d | 2008-07-30 21:41:54 | [diff] [blame] | 111 | |
[email protected] | 4d5e036 | 2008-08-28 00:59:06 | [diff] [blame] | 112 | void DiskCacheTestWithCache::SetTestMode() { |
[email protected] | 398ad13 | 2013-04-02 15:11:01 | [diff] [blame] | 113 | ASSERT_TRUE(!memory_only_); |
[email protected] | a9da16d | 2008-07-30 21:41:54 | [diff] [blame] | 114 | cache_impl_->SetUnitTestMode(); |
| 115 | } |
[email protected] | bc4fb8e | 2010-03-18 23:58:17 | [diff] [blame] | 116 | |
Ben Kelly | 5a16ed0 | 2018-09-07 21:28:09 | [diff] [blame] | 117 | void DiskCacheTestWithCache::SetMaxSize(int64_t size, bool should_succeed) { |
[email protected] | 7aefb15 | 2011-01-21 23:46:49 | [diff] [blame] | 118 | size_ = size; |
[email protected] | 1ed9575 | 2013-04-23 00:12:36 | [diff] [blame] | 119 | if (simple_cache_impl_) |
Ben Kelly | 5a16ed0 | 2018-09-07 21:28:09 | [diff] [blame] | 120 | EXPECT_EQ(should_succeed, simple_cache_impl_->SetMaxSize(size)); |
[email protected] | 1ed9575 | 2013-04-23 00:12:36 | [diff] [blame] | 121 | |
[email protected] | 7aefb15 | 2011-01-21 23:46:49 | [diff] [blame] | 122 | if (cache_impl_) |
Ben Kelly | 5a16ed0 | 2018-09-07 21:28:09 | [diff] [blame] | 123 | EXPECT_EQ(should_succeed, cache_impl_->SetMaxSize(size)); |
[email protected] | 7aefb15 | 2011-01-21 23:46:49 | [diff] [blame] | 124 | |
| 125 | if (mem_cache_) |
Ben Kelly | 5a16ed0 | 2018-09-07 21:28:09 | [diff] [blame] | 126 | EXPECT_EQ(should_succeed, mem_cache_->SetMaxSize(size)); |
[email protected] | 7aefb15 | 2011-01-21 23:46:49 | [diff] [blame] | 127 | } |
| 128 | |
Maks Orlovich | 8efea48 | 2019-08-20 17:14:53 | [diff] [blame] | 129 | disk_cache::EntryResult DiskCacheTestWithCache::OpenOrCreateEntry( |
| 130 | const std::string& key) { |
| 131 | return OpenOrCreateEntryWithPriority(key, net::HIGHEST); |
Steven Bingler | 6afedf8 | 2019-01-08 19:34:49 | [diff] [blame] | 132 | } |
| 133 | |
Maks Orlovich | 8efea48 | 2019-08-20 17:14:53 | [diff] [blame] | 134 | disk_cache::EntryResult DiskCacheTestWithCache::OpenOrCreateEntryWithPriority( |
Steven Bingler | 6afedf8 | 2019-01-08 19:34:49 | [diff] [blame] | 135 | const std::string& key, |
Maks Orlovich | 8efea48 | 2019-08-20 17:14:53 | [diff] [blame] | 136 | net::RequestPriority request_priority) { |
| 137 | TestEntryResultCompletionCallback cb; |
| 138 | disk_cache::EntryResult result = |
| 139 | cache_->OpenOrCreateEntry(key, request_priority, cb.callback()); |
| 140 | return cb.GetResult(std::move(result)); |
Steven Bingler | 6afedf8 | 2019-01-08 19:34:49 | [diff] [blame] | 141 | } |
| 142 | |
[email protected] | bc4fb8e | 2010-03-18 23:58:17 | [diff] [blame] | 143 | int DiskCacheTestWithCache::OpenEntry(const std::string& key, |
| 144 | disk_cache::Entry** entry) { |
Josh Karlin | dd9a5d14 | 2018-06-06 00:35:48 | [diff] [blame] | 145 | return OpenEntryWithPriority(key, net::HIGHEST, entry); |
| 146 | } |
| 147 | |
| 148 | int DiskCacheTestWithCache::OpenEntryWithPriority( |
| 149 | const std::string& key, |
| 150 | net::RequestPriority request_priority, |
| 151 | disk_cache::Entry** entry) { |
Maks Orlovich | 8efea48 | 2019-08-20 17:14:53 | [diff] [blame] | 152 | TestEntryResultCompletionCallback cb; |
| 153 | disk_cache::EntryResult result = |
| 154 | cb.GetResult(cache_->OpenEntry(key, request_priority, cb.callback())); |
| 155 | int rv = result.net_error(); |
| 156 | *entry = result.ReleaseEntry(); |
| 157 | return rv; |
[email protected] | bc4fb8e | 2010-03-18 23:58:17 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | int DiskCacheTestWithCache::CreateEntry(const std::string& key, |
| 161 | disk_cache::Entry** entry) { |
Josh Karlin | dd9a5d14 | 2018-06-06 00:35:48 | [diff] [blame] | 162 | return CreateEntryWithPriority(key, net::HIGHEST, entry); |
| 163 | } |
| 164 | |
| 165 | int DiskCacheTestWithCache::CreateEntryWithPriority( |
| 166 | const std::string& key, |
| 167 | net::RequestPriority request_priority, |
| 168 | disk_cache::Entry** entry) { |
Maks Orlovich | 8efea48 | 2019-08-20 17:14:53 | [diff] [blame] | 169 | TestEntryResultCompletionCallback cb; |
| 170 | disk_cache::EntryResult result = |
| 171 | cb.GetResult(cache_->CreateEntry(key, request_priority, cb.callback())); |
| 172 | int rv = result.net_error(); |
| 173 | *entry = result.ReleaseEntry(); |
| 174 | return rv; |
[email protected] | bc4fb8e | 2010-03-18 23:58:17 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | int DiskCacheTestWithCache::DoomEntry(const std::string& key) { |
[email protected] | 42c45963 | 2011-12-17 02:20:23 | [diff] [blame] | 178 | net::TestCompletionCallback cb; |
Josh Karlin | dd9a5d14 | 2018-06-06 00:35:48 | [diff] [blame] | 179 | int rv = cache_->DoomEntry(key, net::HIGHEST, cb.callback()); |
[email protected] | bc4fb8e | 2010-03-18 23:58:17 | [diff] [blame] | 180 | return cb.GetResult(rv); |
| 181 | } |
| 182 | |
| 183 | int DiskCacheTestWithCache::DoomAllEntries() { |
[email protected] | 6ad7c091 | 2011-12-15 19:10:19 | [diff] [blame] | 184 | net::TestCompletionCallback cb; |
| 185 | int rv = cache_->DoomAllEntries(cb.callback()); |
[email protected] | bc4fb8e | 2010-03-18 23:58:17 | [diff] [blame] | 186 | return cb.GetResult(rv); |
| 187 | } |
| 188 | |
| 189 | int DiskCacheTestWithCache::DoomEntriesBetween(const base::Time initial_time, |
| 190 | const base::Time end_time) { |
[email protected] | 6ad7c091 | 2011-12-15 19:10:19 | [diff] [blame] | 191 | net::TestCompletionCallback cb; |
| 192 | int rv = cache_->DoomEntriesBetween(initial_time, end_time, cb.callback()); |
[email protected] | bc4fb8e | 2010-03-18 23:58:17 | [diff] [blame] | 193 | return cb.GetResult(rv); |
| 194 | } |
| 195 | |
| 196 | int DiskCacheTestWithCache::DoomEntriesSince(const base::Time initial_time) { |
[email protected] | 2a65aceb8 | 2011-12-19 20:59:27 | [diff] [blame] | 197 | net::TestCompletionCallback cb; |
| 198 | int rv = cache_->DoomEntriesSince(initial_time, cb.callback()); |
[email protected] | bc4fb8e | 2010-03-18 23:58:17 | [diff] [blame] | 199 | return cb.GetResult(rv); |
| 200 | } |
| 201 | |
Ben Kelly | 5a16ed0 | 2018-09-07 21:28:09 | [diff] [blame] | 202 | int64_t DiskCacheTestWithCache::CalculateSizeOfAllEntries() { |
| 203 | net::TestInt64CompletionCallback cb; |
| 204 | int64_t rv = cache_->CalculateSizeOfAllEntries(cb.callback()); |
msramek | aee01ceb | 2015-10-07 14:23:33 | [diff] [blame] | 205 | return cb.GetResult(rv); |
| 206 | } |
| 207 | |
Ben Kelly | 5a16ed0 | 2018-09-07 21:28:09 | [diff] [blame] | 208 | int64_t DiskCacheTestWithCache::CalculateSizeOfEntriesBetween( |
dullweber | a003005 | 2017-01-16 11:56:04 | [diff] [blame] | 209 | const base::Time initial_time, |
| 210 | const base::Time end_time) { |
Ben Kelly | 5a16ed0 | 2018-09-07 21:28:09 | [diff] [blame] | 211 | net::TestInt64CompletionCallback cb; |
| 212 | int64_t rv = cache_->CalculateSizeOfEntriesBetween(initial_time, end_time, |
| 213 | cb.callback()); |
dullweber | a003005 | 2017-01-16 11:56:04 | [diff] [blame] | 214 | return cb.GetResult(rv); |
| 215 | } |
| 216 | |
danakj | d04b92d | 2016-04-16 01:16:49 | [diff] [blame] | 217 | std::unique_ptr<DiskCacheTestWithCache::TestIterator> |
| 218 | DiskCacheTestWithCache::CreateIterator() { |
Peter Boström | 8a754069 | 2021-04-05 20:48:20 | [diff] [blame] | 219 | return std::make_unique<TestIterator>(cache_->CreateIterator()); |
[email protected] | bc4fb8e | 2010-03-18 23:58:17 | [diff] [blame] | 220 | } |
[email protected] | fb2622f | 2010-07-13 18:00:56 | [diff] [blame] | 221 | |
| 222 | void DiskCacheTestWithCache::FlushQueueForTest() { |
Josh Karlin | 393a723 | 2020-01-21 19:08:14 | [diff] [blame] | 223 | if (memory_only_) |
[email protected] | fb2622f | 2010-07-13 18:00:56 | [diff] [blame] | 224 | return; |
| 225 | |
Josh Karlin | 393a723 | 2020-01-21 19:08:14 | [diff] [blame] | 226 | if (simple_cache_impl_) { |
Yutaka Hirano | 2d9ce05 | 2022-04-22 02:56:24 | [diff] [blame] | 227 | disk_cache::FlushCacheThreadForTesting(); |
Josh Karlin | 393a723 | 2020-01-21 19:08:14 | [diff] [blame] | 228 | return; |
| 229 | } |
| 230 | |
| 231 | DCHECK(cache_impl_); |
[email protected] | 2a65aceb8 | 2011-12-19 20:59:27 | [diff] [blame] | 232 | net::TestCompletionCallback cb; |
| 233 | int rv = cache_impl_->FlushQueueForTest(cb.callback()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 234 | EXPECT_THAT(cb.GetResult(rv), IsOk()); |
[email protected] | fb2622f | 2010-07-13 18:00:56 | [diff] [blame] | 235 | } |
[email protected] | e1fcf14 | 2010-08-23 18:47:25 | [diff] [blame] | 236 | |
Anna Malova | cb042b64 | 2020-03-03 15:50:00 | [diff] [blame] | 237 | void DiskCacheTestWithCache::RunTaskForTest(base::OnceClosure closure) { |
[email protected] | 65188eb | 2010-09-16 20:59:29 | [diff] [blame] | 238 | if (memory_only_ || !cache_impl_) { |
Anna Malova | cb042b64 | 2020-03-03 15:50:00 | [diff] [blame] | 239 | std::move(closure).Run(); |
[email protected] | 65188eb | 2010-09-16 20:59:29 | [diff] [blame] | 240 | return; |
| 241 | } |
| 242 | |
[email protected] | 2a65aceb8 | 2011-12-19 20:59:27 | [diff] [blame] | 243 | net::TestCompletionCallback cb; |
Anna Malova | cb042b64 | 2020-03-03 15:50:00 | [diff] [blame] | 244 | int rv = cache_impl_->RunTaskForTest(std::move(closure), cb.callback()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 245 | EXPECT_THAT(cb.GetResult(rv), IsOk()); |
[email protected] | 65188eb | 2010-09-16 20:59:29 | [diff] [blame] | 246 | } |
| 247 | |
Matthew Denton | 29cbe0ee | 2019-03-27 00:10:41 | [diff] [blame] | 248 | int DiskCacheTestWithCache::ReadData(disk_cache::Entry* entry, |
| 249 | int index, |
| 250 | int offset, |
| 251 | net::IOBuffer* buf, |
| 252 | int len) { |
[email protected] | 2a65aceb8 | 2011-12-19 20:59:27 | [diff] [blame] | 253 | net::TestCompletionCallback cb; |
| 254 | int rv = entry->ReadData(index, offset, buf, len, cb.callback()); |
[email protected] | e1fcf14 | 2010-08-23 18:47:25 | [diff] [blame] | 255 | return cb.GetResult(rv); |
| 256 | } |
| 257 | |
Matthew Denton | 29cbe0ee | 2019-03-27 00:10:41 | [diff] [blame] | 258 | int DiskCacheTestWithCache::WriteData(disk_cache::Entry* entry, |
| 259 | int index, |
| 260 | int offset, |
| 261 | net::IOBuffer* buf, |
| 262 | int len, |
[email protected] | e1fcf14 | 2010-08-23 18:47:25 | [diff] [blame] | 263 | bool truncate) { |
[email protected] | 2a65aceb8 | 2011-12-19 20:59:27 | [diff] [blame] | 264 | net::TestCompletionCallback cb; |
| 265 | int rv = entry->WriteData(index, offset, buf, len, cb.callback(), truncate); |
[email protected] | e1fcf14 | 2010-08-23 18:47:25 | [diff] [blame] | 266 | return cb.GetResult(rv); |
| 267 | } |
| 268 | |
| 269 | int DiskCacheTestWithCache::ReadSparseData(disk_cache::Entry* entry, |
Avi Drissman | 13fc893 | 2015-12-20 04:40:46 | [diff] [blame] | 270 | int64_t offset, |
| 271 | net::IOBuffer* buf, |
[email protected] | e1fcf14 | 2010-08-23 18:47:25 | [diff] [blame] | 272 | int len) { |
[email protected] | 2a65aceb8 | 2011-12-19 20:59:27 | [diff] [blame] | 273 | net::TestCompletionCallback cb; |
| 274 | int rv = entry->ReadSparseData(offset, buf, len, cb.callback()); |
[email protected] | e1fcf14 | 2010-08-23 18:47:25 | [diff] [blame] | 275 | return cb.GetResult(rv); |
| 276 | } |
| 277 | |
| 278 | int DiskCacheTestWithCache::WriteSparseData(disk_cache::Entry* entry, |
Avi Drissman | 13fc893 | 2015-12-20 04:40:46 | [diff] [blame] | 279 | int64_t offset, |
| 280 | net::IOBuffer* buf, |
| 281 | int len) { |
[email protected] | 2a65aceb8 | 2011-12-19 20:59:27 | [diff] [blame] | 282 | net::TestCompletionCallback cb; |
| 283 | int rv = entry->WriteSparseData(offset, buf, len, cb.callback()); |
[email protected] | e1fcf14 | 2010-08-23 18:47:25 | [diff] [blame] | 284 | return cb.GetResult(rv); |
| 285 | } |
[email protected] | 7aefb15 | 2011-01-21 23:46:49 | [diff] [blame] | 286 | |
Matthew Denton | 29cbe0ee | 2019-03-27 00:10:41 | [diff] [blame] | 287 | int DiskCacheTestWithCache::GetAvailableRange(disk_cache::Entry* entry, |
| 288 | int64_t offset, |
| 289 | int len, |
| 290 | int64_t* start) { |
Maks Orlovich | 04cd1ad | 2021-07-02 17:32:24 | [diff] [blame] | 291 | TestRangeResultCompletionCallback cb; |
| 292 | disk_cache::RangeResult result = |
| 293 | cb.GetResult(entry->GetAvailableRange(offset, len, cb.callback())); |
| 294 | |
| 295 | if (result.net_error == net::OK) { |
| 296 | *start = result.start; |
| 297 | return result.available_len; |
| 298 | } |
| 299 | return result.net_error; |
Matthew Denton | 29cbe0ee | 2019-03-27 00:10:41 | [diff] [blame] | 300 | } |
| 301 | |
[email protected] | ceb61da3 | 2011-01-25 23:52:02 | [diff] [blame] | 302 | void DiskCacheTestWithCache::TrimForTest(bool empty) { |
Matthew Denton | 29cbe0ee | 2019-03-27 00:10:41 | [diff] [blame] | 303 | if (memory_only_ || !cache_impl_) |
| 304 | return; |
| 305 | |
Anna Malova | cb042b64 | 2020-03-03 15:50:00 | [diff] [blame] | 306 | RunTaskForTest(base::BindOnce(&disk_cache::BackendImpl::TrimForTest, |
| 307 | base::Unretained(cache_impl_), empty)); |
[email protected] | ceb61da3 | 2011-01-25 23:52:02 | [diff] [blame] | 308 | } |
| 309 | |
| 310 | void DiskCacheTestWithCache::TrimDeletedListForTest(bool empty) { |
Matthew Denton | 29cbe0ee | 2019-03-27 00:10:41 | [diff] [blame] | 311 | if (memory_only_ || !cache_impl_) |
| 312 | return; |
| 313 | |
Anna Malova | cb042b64 | 2020-03-03 15:50:00 | [diff] [blame] | 314 | RunTaskForTest( |
| 315 | base::BindOnce(&disk_cache::BackendImpl::TrimDeletedListForTest, |
| 316 | base::Unretained(cache_impl_), empty)); |
[email protected] | ceb61da3 | 2011-01-25 23:52:02 | [diff] [blame] | 317 | } |
| 318 | |
[email protected] | 05f8087d | 2012-06-29 18:58:37 | [diff] [blame] | 319 | void DiskCacheTestWithCache::AddDelay() { |
[email protected] | 8b2f8a5 | 2013-08-28 08:45:35 | [diff] [blame] | 320 | if (simple_cache_mode_) { |
| 321 | // The simple cache uses second resolution for many timeouts, so it's safest |
| 322 | // to advance by at least whole seconds before falling back into the normal |
| 323 | // disk cache epsilon advance. |
| 324 | const base::Time initial_time = base::Time::Now(); |
| 325 | do { |
| 326 | base::PlatformThread::YieldCurrentThread(); |
Peter Kasting | e5a38ed | 2021-10-02 03:06:35 | [diff] [blame] | 327 | } while (base::Time::Now() - initial_time < base::Seconds(1)); |
[email protected] | 8b2f8a5 | 2013-08-28 08:45:35 | [diff] [blame] | 328 | } |
| 329 | |
[email protected] | 05f8087d | 2012-06-29 18:58:37 | [diff] [blame] | 330 | base::Time initial = base::Time::Now(); |
| 331 | while (base::Time::Now() <= initial) { |
Peter Kasting | e5a38ed | 2021-10-02 03:06:35 | [diff] [blame] | 332 | base::PlatformThread::Sleep(base::Milliseconds(1)); |
[email protected] | 05f8087d | 2012-06-29 18:58:37 | [diff] [blame] | 333 | }; |
| 334 | } |
| 335 | |
Matthew Denton | 29cbe0ee | 2019-03-27 00:10:41 | [diff] [blame] | 336 | void DiskCacheTestWithCache::OnExternalCacheHit(const std::string& key) { |
| 337 | cache_->OnExternalCacheHit(key); |
| 338 | } |
| 339 | |
[email protected] | 7aefb15 | 2011-01-21 23:46:49 | [diff] [blame] | 340 | void DiskCacheTestWithCache::TearDown() { |
Bence Béky | 98447b1 | 2018-05-08 03:14:01 | [diff] [blame] | 341 | RunUntilIdle(); |
[email protected] | 8c3f5a3 | 2013-08-01 11:57:53 | [diff] [blame] | 342 | cache_.reset(); |
[email protected] | 7aefb15 | 2011-01-21 23:46:49 | [diff] [blame] | 343 | |
[email protected] | fccce90 | 2013-04-15 19:00:51 | [diff] [blame] | 344 | if (!memory_only_ && !simple_cache_mode_ && integrity_) { |
Maks Orlovich | f386065 | 2017-12-13 18:03:16 | [diff] [blame] | 345 | EXPECT_TRUE(CheckCacheIntegrity(cache_path_, new_eviction_, size_, mask_)); |
[email protected] | 7aefb15 | 2011-01-21 23:46:49 | [diff] [blame] | 346 | } |
Bence Béky | 98447b1 | 2018-05-08 03:14:01 | [diff] [blame] | 347 | RunUntilIdle(); |
Maks Orlovich | f378b3a | 2017-10-31 16:27:30 | [diff] [blame] | 348 | if (simple_cache_mode_ && simple_file_tracker_) |
| 349 | EXPECT_TRUE(simple_file_tracker_->IsEmptyForTesting()); |
| 350 | |
[email protected] | de677dc | 2013-09-13 02:12:25 | [diff] [blame] | 351 | DiskCacheTest::TearDown(); |
[email protected] | 7aefb15 | 2011-01-21 23:46:49 | [diff] [blame] | 352 | } |
| 353 | |
| 354 | void DiskCacheTestWithCache::InitMemoryCache() { |
Tsuyoshi Horo | f8861cb | 2022-07-05 23:50:20 | [diff] [blame] | 355 | auto cache = std::make_unique<disk_cache::MemBackendImpl>(nullptr); |
| 356 | mem_cache_ = cache.get(); |
| 357 | cache_ = std::move(cache); |
[email protected] | 8c3f5a3 | 2013-08-01 11:57:53 | [diff] [blame] | 358 | ASSERT_TRUE(cache_); |
[email protected] | 7aefb15 | 2011-01-21 23:46:49 | [diff] [blame] | 359 | |
| 360 | if (size_) |
| 361 | EXPECT_TRUE(mem_cache_->SetMaxSize(size_)); |
| 362 | |
| 363 | ASSERT_TRUE(mem_cache_->Init()); |
| 364 | } |
| 365 | |
| 366 | void DiskCacheTestWithCache::InitDiskCache() { |
[email protected] | 7aefb15 | 2011-01-21 23:46:49 | [diff] [blame] | 367 | if (first_cleanup_) |
[email protected] | 79b3fec | 2011-11-15 22:33:47 | [diff] [blame] | 368 | ASSERT_TRUE(CleanupCacheDir()); |
[email protected] | 7aefb15 | 2011-01-21 23:46:49 | [diff] [blame] | 369 | |
Maks Orlovich | 463dc50 | 2017-07-25 14:52:49 | [diff] [blame] | 370 | CreateBackend(disk_cache::kNoRandom); |
[email protected] | 398ad13 | 2013-04-02 15:11:01 | [diff] [blame] | 371 | } |
[email protected] | 7aefb15 | 2011-01-21 23:46:49 | [diff] [blame] | 372 | |
Maks Orlovich | 463dc50 | 2017-07-25 14:52:49 | [diff] [blame] | 373 | void DiskCacheTestWithCache::CreateBackend(uint32_t flags) { |
[email protected] | 81e549a | 2014-08-21 04:19:55 | [diff] [blame] | 374 | scoped_refptr<base::SingleThreadTaskRunner> runner; |
[email protected] | 398ad13 | 2013-04-02 15:11:01 | [diff] [blame] | 375 | if (use_current_thread_) |
[email protected] | 81e549a | 2014-08-21 04:19:55 | [diff] [blame] | 376 | runner = base::ThreadTaskRunnerHandle::Get(); |
[email protected] | 7aefb15 | 2011-01-21 23:46:49 | [diff] [blame] | 377 | else |
Maks Orlovich | 463dc50 | 2017-07-25 14:52:49 | [diff] [blame] | 378 | runner = nullptr; // let the backend sort it out. |
[email protected] | 7aefb15 | 2011-01-21 23:46:49 | [diff] [blame] | 379 | |
[email protected] | 398ad13 | 2013-04-02 15:11:01 | [diff] [blame] | 380 | if (simple_cache_mode_) { |
Maks Orlovich | 922db0c | 2018-02-21 15:28:46 | [diff] [blame] | 381 | DCHECK(!use_current_thread_) |
| 382 | << "Using current thread unsupported by SimpleCache"; |
[email protected] | 398ad13 | 2013-04-02 15:11:01 | [diff] [blame] | 383 | net::TestCompletionCallback cb; |
Maks Orlovich | db1f092 | 2018-01-31 03:41:17 | [diff] [blame] | 384 | // We limit ourselves to 64 fds since OS X by default gives us 256. |
| 385 | // (Chrome raises the number on startup, but the test fixture doesn't). |
Maks Orlovich | f378b3a | 2017-10-31 16:27:30 | [diff] [blame] | 386 | if (!simple_file_tracker_) |
Maks Orlovich | db1f092 | 2018-01-31 03:41:17 | [diff] [blame] | 387 | simple_file_tracker_ = |
| 388 | std::make_unique<disk_cache::SimpleFileTracker>(64); |
Maks Orlovich | f378b3a | 2017-10-31 16:27:30 | [diff] [blame] | 389 | std::unique_ptr<disk_cache::SimpleBackendImpl> simple_backend = |
| 390 | std::make_unique<disk_cache::SimpleBackendImpl>( |
Yutaka Hirano | 23a59034 | 2022-03-10 13:31:42 | [diff] [blame] | 391 | /*file_operations=*/nullptr, cache_path_, |
| 392 | /* cleanup_tracker = */ nullptr, simple_file_tracker_.get(), size_, |
| 393 | type_, /*net_log = */ nullptr); |
Maks Orlovich | 2690c35 | 2022-06-24 15:48:44 | [diff] [blame] | 394 | simple_backend->Init(cb.callback()); |
| 395 | ASSERT_THAT(cb.WaitForResult(), IsOk()); |
[email protected] | 8c3f5a3 | 2013-08-01 11:57:53 | [diff] [blame] | 396 | simple_cache_impl_ = simple_backend.get(); |
dcheng | c7eeda42 | 2015-12-26 03:56:48 | [diff] [blame] | 397 | cache_ = std::move(simple_backend); |
[email protected] | 7e48c1b | 2013-07-12 12:15:43 | [diff] [blame] | 398 | if (simple_cache_wait_for_index_) { |
| 399 | net::TestCompletionCallback wait_for_index_cb; |
Maks Orlovich | 928035b | 2019-07-29 20:19:45 | [diff] [blame] | 400 | simple_cache_impl_->index()->ExecuteWhenReady( |
[email protected] | 7e48c1b | 2013-07-12 12:15:43 | [diff] [blame] | 401 | wait_for_index_cb.callback()); |
Maks Orlovich | 2690c35 | 2022-06-24 15:48:44 | [diff] [blame] | 402 | int rv = wait_for_index_cb.WaitForResult(); |
Maks Orlovich | 928035b | 2019-07-29 20:19:45 | [diff] [blame] | 403 | ASSERT_THAT(rv, IsOk()); |
[email protected] | 7e48c1b | 2013-07-12 12:15:43 | [diff] [blame] | 404 | } |
[email protected] | 398ad13 | 2013-04-02 15:11:01 | [diff] [blame] | 405 | return; |
| 406 | } |
| 407 | |
Tsuyoshi Horo | f8861cb | 2022-07-05 23:50:20 | [diff] [blame] | 408 | std::unique_ptr<disk_cache::BackendImpl> cache; |
| 409 | if (mask_) { |
| 410 | cache = std::make_unique<disk_cache::BackendImpl>(cache_path_, mask_, |
| 411 | runner, type_, |
| 412 | /* net_log = */ nullptr); |
| 413 | } else { |
| 414 | cache = std::make_unique<disk_cache::BackendImpl>( |
Steven Bingler | 252ffae | 2019-03-19 17:43:16 | [diff] [blame] | 415 | cache_path_, /* cleanup_tracker = */ nullptr, runner, type_, |
| 416 | /* net_log = */ nullptr); |
Tsuyoshi Horo | f8861cb | 2022-07-05 23:50:20 | [diff] [blame] | 417 | } |
| 418 | cache_impl_ = cache.get(); |
| 419 | cache_ = std::move(cache); |
[email protected] | 8c3f5a3 | 2013-08-01 11:57:53 | [diff] [blame] | 420 | ASSERT_TRUE(cache_); |
[email protected] | 7aefb15 | 2011-01-21 23:46:49 | [diff] [blame] | 421 | if (size_) |
| 422 | EXPECT_TRUE(cache_impl_->SetMaxSize(size_)); |
[email protected] | 7aefb15 | 2011-01-21 23:46:49 | [diff] [blame] | 423 | if (new_eviction_) |
| 424 | cache_impl_->SetNewEviction(); |
[email protected] | 398ad13 | 2013-04-02 15:11:01 | [diff] [blame] | 425 | cache_impl_->SetFlags(flags); |
[email protected] | 42c45963 | 2011-12-17 02:20:23 | [diff] [blame] | 426 | net::TestCompletionCallback cb; |
Maks Orlovich | 2690c35 | 2022-06-24 15:48:44 | [diff] [blame] | 427 | cache_impl_->Init(cb.callback()); |
| 428 | ASSERT_THAT(cb.WaitForResult(), IsOk()); |
[email protected] | 7aefb15 | 2011-01-21 23:46:49 | [diff] [blame] | 429 | } |