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