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