blob: 47125a30cc53f4bdf2969ae35e1c5b607c2706c4 [file] [log] [blame]
[email protected]93fe75162012-02-09 21:51:311// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]f40156002011-11-22 21:19:082// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "net/http/mock_http_cache.h"
6
avid0181f32015-12-10 19:41:477#include <limits>
dchengc7eeda422015-12-26 03:56:488#include <utility>
avid0181f32015-12-10 19:41:479
[email protected]f40156002011-11-22 21:19:0810#include "base/bind.h"
skyostil4891b25b2015-06-11 11:43:4511#include "base/location.h"
danakj1fd259a02016-04-16 03:17:0912#include "base/memory/ptr_util.h"
skyostil4891b25b2015-06-11 11:43:4513#include "base/single_thread_task_runner.h"
gabf767595f2016-05-11 18:50:3514#include "base/threading/thread_task_runner_handle.h"
[email protected]42c459632011-12-17 02:20:2315#include "net/base/completion_callback.h"
[email protected]f40156002011-11-22 21:19:0816#include "net/base/net_errors.h"
17#include "testing/gtest/include/gtest/gtest.h"
18
ttuttle859dc7a2015-04-23 19:42:2919namespace net {
20
[email protected]f40156002011-11-22 21:19:0821namespace {
22
asankab5fb4b42016-06-29 11:06:0823// During testing, we are going to limit the size of a cache entry to this many
24// bytes using DCHECKs in order to prevent a test from causing unbounded memory
25// growth. In practice cache entry shouldn't come anywhere near this limit for
26// tests that use the mock cache. If they do, that's likely a problem with the
27// test. If a test requires using massive cache entries, they should use a real
28// cache backend instead.
29const int kMaxMockCacheEntrySize = 100 * 1000 * 1000;
30
[email protected]42c459632011-12-17 02:20:2331// We can override the test mode for a given operation by setting this global
32// variable.
33int g_test_mode = 0;
34
[email protected]f40156002011-11-22 21:19:0835int GetTestModeForEntry(const std::string& key) {
36 // 'key' is prefixed with an identifier if it corresponds to a cached POST.
37 // Skip past that to locate the actual URL.
38 //
39 // TODO(darin): It breaks the abstraction a bit that we assume 'key' is an
40 // URL corresponding to a registered MockTransaction. It would be good to
41 // have another way to access the test_mode.
42 GURL url;
43 if (isdigit(key[0])) {
44 size_t slash = key.find('/');
45 DCHECK(slash != std::string::npos);
46 url = GURL(key.substr(slash + 1));
47 } else {
48 url = GURL(key);
49 }
50 const MockTransaction* t = FindMockTransaction(url);
51 DCHECK(t);
52 return t->test_mode;
53}
54
ttuttle859dc7a2015-04-23 19:42:2955void CallbackForwader(const CompletionCallback& callback, int result) {
[email protected]42c459632011-12-17 02:20:2356 callback.Run(result);
57}
[email protected]f40156002011-11-22 21:19:0858
59} // namespace
60
61//-----------------------------------------------------------------------------
62
63struct MockDiskEntry::CallbackInfo {
64 scoped_refptr<MockDiskEntry> entry;
ttuttle859dc7a2015-04-23 19:42:2965 CompletionCallback callback;
[email protected]f40156002011-11-22 21:19:0866 int result;
67};
68
[email protected]f40156002011-11-22 21:19:0869MockDiskEntry::MockDiskEntry(const std::string& key)
rvargas9ba24452015-07-18 03:13:2570 : key_(key),
71 doomed_(false),
72 sparse_(false),
73 fail_requests_(false),
74 fail_sparse_requests_(false),
75 busy_(false),
76 delayed_(false),
77 cancel_(false) {
[email protected]f40156002011-11-22 21:19:0878 test_mode_ = GetTestModeForEntry(key);
79}
80
81void MockDiskEntry::Doom() {
82 doomed_ = true;
83}
84
85void MockDiskEntry::Close() {
86 Release();
87}
88
89std::string MockDiskEntry::GetKey() const {
[email protected]f40156002011-11-22 21:19:0890 return key_;
91}
92
93base::Time MockDiskEntry::GetLastUsed() const {
jkarlin91ae3602016-05-25 14:05:3894 return base::Time::Now();
[email protected]f40156002011-11-22 21:19:0895}
96
97base::Time MockDiskEntry::GetLastModified() const {
jkarlin91ae3602016-05-25 14:05:3898 return base::Time::Now();
[email protected]f40156002011-11-22 21:19:0899}
100
avid0181f32015-12-10 19:41:47101int32_t MockDiskEntry::GetDataSize(int index) const {
[email protected]f40156002011-11-22 21:19:08102 DCHECK(index >= 0 && index < kNumCacheEntryDataIndices);
avid0181f32015-12-10 19:41:47103 return static_cast<int32_t>(data_[index].size());
[email protected]f40156002011-11-22 21:19:08104}
105
ttuttle859dc7a2015-04-23 19:42:29106int MockDiskEntry::ReadData(int index,
107 int offset,
108 IOBuffer* buf,
109 int buf_len,
110 const CompletionCallback& callback) {
[email protected]f40156002011-11-22 21:19:08111 DCHECK(index >= 0 && index < kNumCacheEntryDataIndices);
[email protected]2a65aceb82011-12-19 20:59:27112 DCHECK(!callback.is_null());
[email protected]f40156002011-11-22 21:19:08113
114 if (fail_requests_)
ttuttle859dc7a2015-04-23 19:42:29115 return ERR_CACHE_READ_FAILURE;
[email protected]f40156002011-11-22 21:19:08116
117 if (offset < 0 || offset > static_cast<int>(data_[index].size()))
ttuttle859dc7a2015-04-23 19:42:29118 return ERR_FAILED;
[email protected]f40156002011-11-22 21:19:08119 if (static_cast<size_t>(offset) == data_[index].size())
120 return 0;
121
122 int num = std::min(buf_len, static_cast<int>(data_[index].size()) - offset);
123 memcpy(buf->data(), &data_[index][offset], num);
124
125 if (MockHttpCache::GetTestMode(test_mode_) & TEST_MODE_SYNC_CACHE_READ)
126 return num;
127
128 CallbackLater(callback, num);
ttuttle859dc7a2015-04-23 19:42:29129 return ERR_IO_PENDING;
[email protected]f40156002011-11-22 21:19:08130}
131
ttuttle859dc7a2015-04-23 19:42:29132int MockDiskEntry::WriteData(int index,
133 int offset,
134 IOBuffer* buf,
135 int buf_len,
136 const CompletionCallback& callback,
137 bool truncate) {
[email protected]f40156002011-11-22 21:19:08138 DCHECK(index >= 0 && index < kNumCacheEntryDataIndices);
[email protected]2a65aceb82011-12-19 20:59:27139 DCHECK(!callback.is_null());
[email protected]f40156002011-11-22 21:19:08140 DCHECK(truncate);
141
142 if (fail_requests_) {
ttuttle859dc7a2015-04-23 19:42:29143 CallbackLater(callback, ERR_CACHE_READ_FAILURE);
144 return ERR_IO_PENDING;
[email protected]f40156002011-11-22 21:19:08145 }
146
147 if (offset < 0 || offset > static_cast<int>(data_[index].size()))
ttuttle859dc7a2015-04-23 19:42:29148 return ERR_FAILED;
[email protected]f40156002011-11-22 21:19:08149
asankab5fb4b42016-06-29 11:06:08150 DCHECK_LT(offset + buf_len, kMaxMockCacheEntrySize);
[email protected]f40156002011-11-22 21:19:08151 data_[index].resize(offset + buf_len);
152 if (buf_len)
153 memcpy(&data_[index][offset], buf->data(), buf_len);
154
155 if (MockHttpCache::GetTestMode(test_mode_) & TEST_MODE_SYNC_CACHE_WRITE)
156 return buf_len;
157
158 CallbackLater(callback, buf_len);
ttuttle859dc7a2015-04-23 19:42:29159 return ERR_IO_PENDING;
[email protected]f40156002011-11-22 21:19:08160}
161
avid0181f32015-12-10 19:41:47162int MockDiskEntry::ReadSparseData(int64_t offset,
ttuttle859dc7a2015-04-23 19:42:29163 IOBuffer* buf,
164 int buf_len,
165 const CompletionCallback& callback) {
[email protected]2a65aceb82011-12-19 20:59:27166 DCHECK(!callback.is_null());
[email protected]954bbe42013-08-30 12:38:39167 if (fail_sparse_requests_)
ttuttle859dc7a2015-04-23 19:42:29168 return ERR_NOT_IMPLEMENTED;
rvargas9ba24452015-07-18 03:13:25169 if (!sparse_ || busy_ || cancel_)
ttuttle859dc7a2015-04-23 19:42:29170 return ERR_CACHE_OPERATION_NOT_SUPPORTED;
[email protected]f40156002011-11-22 21:19:08171 if (offset < 0)
ttuttle859dc7a2015-04-23 19:42:29172 return ERR_FAILED;
[email protected]f40156002011-11-22 21:19:08173
174 if (fail_requests_)
ttuttle859dc7a2015-04-23 19:42:29175 return ERR_CACHE_READ_FAILURE;
[email protected]f40156002011-11-22 21:19:08176
avid0181f32015-12-10 19:41:47177 DCHECK(offset < std::numeric_limits<int32_t>::max());
[email protected]f40156002011-11-22 21:19:08178 int real_offset = static_cast<int>(offset);
179 if (!buf_len)
180 return 0;
181
182 int num = std::min(static_cast<int>(data_[1].size()) - real_offset,
183 buf_len);
184 memcpy(buf->data(), &data_[1][real_offset], num);
185
186 if (MockHttpCache::GetTestMode(test_mode_) & TEST_MODE_SYNC_CACHE_READ)
187 return num;
188
189 CallbackLater(callback, num);
190 busy_ = true;
191 delayed_ = false;
ttuttle859dc7a2015-04-23 19:42:29192 return ERR_IO_PENDING;
[email protected]f40156002011-11-22 21:19:08193}
194
avid0181f32015-12-10 19:41:47195int MockDiskEntry::WriteSparseData(int64_t offset,
ttuttle859dc7a2015-04-23 19:42:29196 IOBuffer* buf,
[email protected]f40156002011-11-22 21:19:08197 int buf_len,
ttuttle859dc7a2015-04-23 19:42:29198 const CompletionCallback& callback) {
[email protected]2a65aceb82011-12-19 20:59:27199 DCHECK(!callback.is_null());
[email protected]954bbe42013-08-30 12:38:39200 if (fail_sparse_requests_)
ttuttle859dc7a2015-04-23 19:42:29201 return ERR_NOT_IMPLEMENTED;
rvargas9ba24452015-07-18 03:13:25202 if (busy_ || cancel_)
ttuttle859dc7a2015-04-23 19:42:29203 return ERR_CACHE_OPERATION_NOT_SUPPORTED;
[email protected]f40156002011-11-22 21:19:08204 if (!sparse_) {
205 if (data_[1].size())
ttuttle859dc7a2015-04-23 19:42:29206 return ERR_CACHE_OPERATION_NOT_SUPPORTED;
[email protected]f40156002011-11-22 21:19:08207 sparse_ = true;
208 }
209 if (offset < 0)
ttuttle859dc7a2015-04-23 19:42:29210 return ERR_FAILED;
[email protected]f40156002011-11-22 21:19:08211 if (!buf_len)
212 return 0;
213
214 if (fail_requests_)
ttuttle859dc7a2015-04-23 19:42:29215 return ERR_CACHE_READ_FAILURE;
[email protected]f40156002011-11-22 21:19:08216
avid0181f32015-12-10 19:41:47217 DCHECK(offset < std::numeric_limits<int32_t>::max());
[email protected]f40156002011-11-22 21:19:08218 int real_offset = static_cast<int>(offset);
219
asankab5fb4b42016-06-29 11:06:08220 if (static_cast<int>(data_[1].size()) < real_offset + buf_len) {
221 DCHECK_LT(real_offset + buf_len, kMaxMockCacheEntrySize);
[email protected]f40156002011-11-22 21:19:08222 data_[1].resize(real_offset + buf_len);
asankab5fb4b42016-06-29 11:06:08223 }
[email protected]f40156002011-11-22 21:19:08224
225 memcpy(&data_[1][real_offset], buf->data(), buf_len);
226 if (MockHttpCache::GetTestMode(test_mode_) & TEST_MODE_SYNC_CACHE_WRITE)
227 return buf_len;
228
229 CallbackLater(callback, buf_len);
ttuttle859dc7a2015-04-23 19:42:29230 return ERR_IO_PENDING;
[email protected]f40156002011-11-22 21:19:08231}
232
avid0181f32015-12-10 19:41:47233int MockDiskEntry::GetAvailableRange(int64_t offset,
ttuttle859dc7a2015-04-23 19:42:29234 int len,
avid0181f32015-12-10 19:41:47235 int64_t* start,
ttuttle859dc7a2015-04-23 19:42:29236 const CompletionCallback& callback) {
[email protected]2a65aceb82011-12-19 20:59:27237 DCHECK(!callback.is_null());
rvargas9ba24452015-07-18 03:13:25238 if (!sparse_ || busy_ || cancel_)
ttuttle859dc7a2015-04-23 19:42:29239 return ERR_CACHE_OPERATION_NOT_SUPPORTED;
[email protected]f40156002011-11-22 21:19:08240 if (offset < 0)
ttuttle859dc7a2015-04-23 19:42:29241 return ERR_FAILED;
[email protected]f40156002011-11-22 21:19:08242
243 if (fail_requests_)
ttuttle859dc7a2015-04-23 19:42:29244 return ERR_CACHE_READ_FAILURE;
[email protected]f40156002011-11-22 21:19:08245
246 *start = offset;
avid0181f32015-12-10 19:41:47247 DCHECK(offset < std::numeric_limits<int32_t>::max());
[email protected]f40156002011-11-22 21:19:08248 int real_offset = static_cast<int>(offset);
249 if (static_cast<int>(data_[1].size()) < real_offset)
250 return 0;
251
252 int num = std::min(static_cast<int>(data_[1].size()) - real_offset, len);
253 int count = 0;
254 for (; num > 0; num--, real_offset++) {
255 if (!count) {
256 if (data_[1][real_offset]) {
257 count++;
258 *start = real_offset;
259 }
260 } else {
261 if (!data_[1][real_offset])
262 break;
263 count++;
264 }
265 }
266 if (MockHttpCache::GetTestMode(test_mode_) & TEST_MODE_SYNC_CACHE_WRITE)
267 return count;
268
269 CallbackLater(callback, count);
ttuttle859dc7a2015-04-23 19:42:29270 return ERR_IO_PENDING;
[email protected]f40156002011-11-22 21:19:08271}
272
273bool MockDiskEntry::CouldBeSparse() const {
[email protected]954bbe42013-08-30 12:38:39274 if (fail_sparse_requests_)
275 return false;
[email protected]f40156002011-11-22 21:19:08276 return sparse_;
277}
278
279void MockDiskEntry::CancelSparseIO() {
280 cancel_ = true;
281}
282
ttuttle859dc7a2015-04-23 19:42:29283int MockDiskEntry::ReadyForSparseIO(const CompletionCallback& callback) {
[email protected]954bbe42013-08-30 12:38:39284 if (fail_sparse_requests_)
ttuttle859dc7a2015-04-23 19:42:29285 return ERR_NOT_IMPLEMENTED;
[email protected]f40156002011-11-22 21:19:08286 if (!cancel_)
ttuttle859dc7a2015-04-23 19:42:29287 return OK;
[email protected]f40156002011-11-22 21:19:08288
289 cancel_ = false;
[email protected]2a65aceb82011-12-19 20:59:27290 DCHECK(!callback.is_null());
[email protected]f40156002011-11-22 21:19:08291 if (MockHttpCache::GetTestMode(test_mode_) & TEST_MODE_SYNC_CACHE_READ)
ttuttle859dc7a2015-04-23 19:42:29292 return OK;
[email protected]f40156002011-11-22 21:19:08293
[email protected]2a65aceb82011-12-19 20:59:27294 // The pending operation is already in the message loop (and hopefully
[email protected]f40156002011-11-22 21:19:08295 // already in the second pass). Just notify the caller that it finished.
296 CallbackLater(callback, 0);
ttuttle859dc7a2015-04-23 19:42:29297 return ERR_IO_PENDING;
[email protected]f40156002011-11-22 21:19:08298}
299
300// If |value| is true, don't deliver any completion callbacks until called
301// again with |value| set to false. Caution: remember to enable callbacks
302// again or all subsequent tests will fail.
303// Static.
304void MockDiskEntry::IgnoreCallbacks(bool value) {
305 if (ignore_callbacks_ == value)
306 return;
307 ignore_callbacks_ = value;
308 if (!value)
ttuttle859dc7a2015-04-23 19:42:29309 StoreAndDeliverCallbacks(false, NULL, CompletionCallback(), 0);
[email protected]f40156002011-11-22 21:19:08310}
311
312MockDiskEntry::~MockDiskEntry() {
313}
314
315// Unlike the callbacks for MockHttpTransaction, we want this one to run even
316// if the consumer called Close on the MockDiskEntry. We achieve that by
317// leveraging the fact that this class is reference counted.
ttuttle859dc7a2015-04-23 19:42:29318void MockDiskEntry::CallbackLater(const CompletionCallback& callback,
[email protected]f40156002011-11-22 21:19:08319 int result) {
[email protected]2c5c9d52011-12-07 14:15:36320 if (ignore_callbacks_)
321 return StoreAndDeliverCallbacks(true, this, callback, result);
skyostil4891b25b2015-06-11 11:43:45322 base::ThreadTaskRunnerHandle::Get()->PostTask(
[email protected]2da659e2013-05-23 20:51:34323 FROM_HERE,
324 base::Bind(&MockDiskEntry::RunCallback, this, callback, result));
[email protected]f40156002011-11-22 21:19:08325}
326
ttuttle859dc7a2015-04-23 19:42:29327void MockDiskEntry::RunCallback(const CompletionCallback& callback,
328 int result) {
[email protected]f40156002011-11-22 21:19:08329 if (busy_) {
330 // This is kind of hacky, but controlling the behavior of just this entry
331 // from a test is sort of complicated. What we really want to do is
332 // delay the delivery of a sparse IO operation a little more so that the
333 // request start operation (async) will finish without seeing the end of
334 // this operation (already posted to the message loop)... and without
335 // just delaying for n mS (which may cause trouble with slow bots). So
336 // we re-post this operation (all async sparse IO operations will take two
[email protected]2a65aceb82011-12-19 20:59:27337 // trips through the message loop instead of one).
[email protected]f40156002011-11-22 21:19:08338 if (!delayed_) {
339 delayed_ = true;
340 return CallbackLater(callback, result);
341 }
342 }
343 busy_ = false;
[email protected]2a65aceb82011-12-19 20:59:27344 callback.Run(result);
[email protected]f40156002011-11-22 21:19:08345}
346
347// When |store| is true, stores the callback to be delivered later; otherwise
348// delivers any callback previously stored.
349// Static.
ttuttle859dc7a2015-04-23 19:42:29350void MockDiskEntry::StoreAndDeliverCallbacks(bool store,
351 MockDiskEntry* entry,
352 const CompletionCallback& callback,
353 int result) {
[email protected]f40156002011-11-22 21:19:08354 static std::vector<CallbackInfo> callback_list;
355 if (store) {
[email protected]2c5c9d52011-12-07 14:15:36356 CallbackInfo c = {entry, callback, result};
[email protected]f40156002011-11-22 21:19:08357 callback_list.push_back(c);
358 } else {
[email protected]2c5c9d52011-12-07 14:15:36359 for (size_t i = 0; i < callback_list.size(); i++) {
[email protected]f40156002011-11-22 21:19:08360 CallbackInfo& c = callback_list[i];
[email protected]2c5c9d52011-12-07 14:15:36361 c.entry->CallbackLater(c.callback, c.result);
[email protected]f40156002011-11-22 21:19:08362 }
363 callback_list.clear();
364 }
365}
366
367// Statics.
[email protected]f40156002011-11-22 21:19:08368bool MockDiskEntry::ignore_callbacks_ = false;
369
370//-----------------------------------------------------------------------------
371
[email protected]f40156002011-11-22 21:19:08372MockDiskCache::MockDiskCache()
373 : open_count_(0), create_count_(0), fail_requests_(false),
[email protected]954bbe42013-08-30 12:38:39374 soft_failures_(false), double_create_check_(true),
375 fail_sparse_requests_(false) {
[email protected]f40156002011-11-22 21:19:08376}
377
378MockDiskCache::~MockDiskCache() {
379 ReleaseAll();
380}
381
ttuttle859dc7a2015-04-23 19:42:29382CacheType MockDiskCache::GetCacheType() const {
383 return DISK_CACHE;
[email protected]6e143962012-08-09 21:34:33384}
385
avid0181f32015-12-10 19:41:47386int32_t MockDiskCache::GetEntryCount() const {
387 return static_cast<int32_t>(entries_.size());
[email protected]f40156002011-11-22 21:19:08388}
389
ttuttle859dc7a2015-04-23 19:42:29390int MockDiskCache::OpenEntry(const std::string& key,
391 disk_cache::Entry** entry,
392 const CompletionCallback& callback) {
[email protected]2a65aceb82011-12-19 20:59:27393 DCHECK(!callback.is_null());
[email protected]f40156002011-11-22 21:19:08394 if (fail_requests_)
ttuttle859dc7a2015-04-23 19:42:29395 return ERR_CACHE_OPEN_FAILURE;
[email protected]f40156002011-11-22 21:19:08396
397 EntryMap::iterator it = entries_.find(key);
398 if (it == entries_.end())
ttuttle859dc7a2015-04-23 19:42:29399 return ERR_CACHE_OPEN_FAILURE;
[email protected]f40156002011-11-22 21:19:08400
401 if (it->second->is_doomed()) {
402 it->second->Release();
403 entries_.erase(it);
ttuttle859dc7a2015-04-23 19:42:29404 return ERR_CACHE_OPEN_FAILURE;
[email protected]f40156002011-11-22 21:19:08405 }
406
407 open_count_++;
408
409 it->second->AddRef();
410 *entry = it->second;
411
412 if (soft_failures_)
413 it->second->set_fail_requests();
414
415 if (GetTestModeForEntry(key) & TEST_MODE_SYNC_CACHE_START)
ttuttle859dc7a2015-04-23 19:42:29416 return OK;
[email protected]f40156002011-11-22 21:19:08417
ttuttle859dc7a2015-04-23 19:42:29418 CallbackLater(callback, OK);
419 return ERR_IO_PENDING;
[email protected]f40156002011-11-22 21:19:08420}
421
422int MockDiskCache::CreateEntry(const std::string& key,
423 disk_cache::Entry** entry,
ttuttle859dc7a2015-04-23 19:42:29424 const CompletionCallback& callback) {
[email protected]2a65aceb82011-12-19 20:59:27425 DCHECK(!callback.is_null());
[email protected]f40156002011-11-22 21:19:08426 if (fail_requests_)
ttuttle859dc7a2015-04-23 19:42:29427 return ERR_CACHE_CREATE_FAILURE;
[email protected]f40156002011-11-22 21:19:08428
429 EntryMap::iterator it = entries_.find(key);
430 if (it != entries_.end()) {
[email protected]6a019de2011-11-30 19:57:29431 if (!it->second->is_doomed()) {
432 if (double_create_check_)
433 NOTREACHED();
434 else
ttuttle859dc7a2015-04-23 19:42:29435 return ERR_CACHE_CREATE_FAILURE;
[email protected]6a019de2011-11-30 19:57:29436 }
[email protected]f40156002011-11-22 21:19:08437 it->second->Release();
438 entries_.erase(it);
439 }
440
441 create_count_++;
442
443 MockDiskEntry* new_entry = new MockDiskEntry(key);
444
445 new_entry->AddRef();
446 entries_[key] = new_entry;
447
448 new_entry->AddRef();
449 *entry = new_entry;
450
451 if (soft_failures_)
452 new_entry->set_fail_requests();
453
[email protected]954bbe42013-08-30 12:38:39454 if (fail_sparse_requests_)
455 new_entry->set_fail_sparse_requests();
456
[email protected]f40156002011-11-22 21:19:08457 if (GetTestModeForEntry(key) & TEST_MODE_SYNC_CACHE_START)
ttuttle859dc7a2015-04-23 19:42:29458 return OK;
[email protected]f40156002011-11-22 21:19:08459
ttuttle859dc7a2015-04-23 19:42:29460 CallbackLater(callback, OK);
461 return ERR_IO_PENDING;
[email protected]f40156002011-11-22 21:19:08462}
463
464int MockDiskCache::DoomEntry(const std::string& key,
ttuttle859dc7a2015-04-23 19:42:29465 const CompletionCallback& callback) {
[email protected]42c459632011-12-17 02:20:23466 DCHECK(!callback.is_null());
[email protected]f40156002011-11-22 21:19:08467 EntryMap::iterator it = entries_.find(key);
468 if (it != entries_.end()) {
469 it->second->Release();
470 entries_.erase(it);
471 }
472
473 if (GetTestModeForEntry(key) & TEST_MODE_SYNC_CACHE_START)
ttuttle859dc7a2015-04-23 19:42:29474 return OK;
[email protected]f40156002011-11-22 21:19:08475
ttuttle859dc7a2015-04-23 19:42:29476 CallbackLater(callback, OK);
477 return ERR_IO_PENDING;
[email protected]f40156002011-11-22 21:19:08478}
479
ttuttle859dc7a2015-04-23 19:42:29480int MockDiskCache::DoomAllEntries(const CompletionCallback& callback) {
481 return ERR_NOT_IMPLEMENTED;
[email protected]dbe1ffe2011-12-12 19:18:56482}
483
[email protected]f40156002011-11-22 21:19:08484int MockDiskCache::DoomEntriesBetween(const base::Time initial_time,
485 const base::Time end_time,
ttuttle859dc7a2015-04-23 19:42:29486 const CompletionCallback& callback) {
487 return ERR_NOT_IMPLEMENTED;
[email protected]dbe1ffe2011-12-12 19:18:56488}
489
[email protected]f40156002011-11-22 21:19:08490int MockDiskCache::DoomEntriesSince(const base::Time initial_time,
ttuttle859dc7a2015-04-23 19:42:29491 const CompletionCallback& callback) {
492 return ERR_NOT_IMPLEMENTED;
[email protected]f40156002011-11-22 21:19:08493}
494
msramekaee01ceb2015-10-07 14:23:33495int MockDiskCache::CalculateSizeOfAllEntries(
496 const CompletionCallback& callback) {
497 return ERR_NOT_IMPLEMENTED;
498}
499
gavinpb1aaa052014-09-24 14:54:35500class MockDiskCache::NotImplementedIterator : public Iterator {
501 public:
dchengb03027d2014-10-21 12:00:20502 int OpenNextEntry(disk_cache::Entry** next_entry,
ttuttle859dc7a2015-04-23 19:42:29503 const CompletionCallback& callback) override {
504 return ERR_NOT_IMPLEMENTED;
gavinpb1aaa052014-09-24 14:54:35505 }
506};
[email protected]275dbf42011-12-12 20:41:01507
danakj1fd259a02016-04-16 03:17:09508std::unique_ptr<disk_cache::Backend::Iterator> MockDiskCache::CreateIterator() {
509 return std::unique_ptr<Iterator>(new NotImplementedIterator());
[email protected]f40156002011-11-22 21:19:08510}
511
payal.pandey90b81372015-04-30 06:53:04512void MockDiskCache::GetStats(base::StringPairs* stats) {
[email protected]f40156002011-11-22 21:19:08513}
514
515void MockDiskCache::OnExternalCacheHit(const std::string& key) {
516}
517
jkarlin1f2ef1772017-03-28 19:04:38518size_t MockDiskCache::DumpMemoryStats(
519 base::trace_event::ProcessMemoryDump* pmd,
520 const std::string& parent_absolute_name) const {
521 return 0u;
xunjielia0166f42017-02-23 17:44:57522}
523
[email protected]f40156002011-11-22 21:19:08524void MockDiskCache::ReleaseAll() {
525 EntryMap::iterator it = entries_.begin();
526 for (; it != entries_.end(); ++it)
527 it->second->Release();
528 entries_.clear();
529}
530
ttuttle859dc7a2015-04-23 19:42:29531void MockDiskCache::CallbackLater(const CompletionCallback& callback,
[email protected]f40156002011-11-22 21:19:08532 int result) {
skyostil4891b25b2015-06-11 11:43:45533 base::ThreadTaskRunnerHandle::Get()->PostTask(
[email protected]42c459632011-12-17 02:20:23534 FROM_HERE, base::Bind(&CallbackForwader, callback, result));
[email protected]f40156002011-11-22 21:19:08535}
536
537//-----------------------------------------------------------------------------
538
danakj1fd259a02016-04-16 03:17:09539int MockBackendFactory::CreateBackend(
540 NetLog* net_log,
541 std::unique_ptr<disk_cache::Backend>* backend,
542 const CompletionCallback& callback) {
[email protected]8c3f5a32013-08-01 11:57:53543 backend->reset(new MockDiskCache());
ttuttle859dc7a2015-04-23 19:42:29544 return OK;
[email protected]f40156002011-11-22 21:19:08545}
546
547//-----------------------------------------------------------------------------
548
zhongyifcd139742017-02-02 02:26:42549MockHttpCache::MockHttpCache() : MockHttpCache(false) {}
[email protected]f40156002011-11-22 21:19:08550
mmenkebc31a2c2015-10-29 13:44:45551MockHttpCache::MockHttpCache(
danakj1fd259a02016-04-16 03:17:09552 std::unique_ptr<HttpCache::BackendFactory> disk_cache_factory)
zhongyifcd139742017-02-02 02:26:42553 : MockHttpCache(std::move(disk_cache_factory), false) {}
554
zhongyi4928bd52017-02-08 02:16:27555MockHttpCache::MockHttpCache(bool is_main_cache)
556 : MockHttpCache(base::MakeUnique<MockBackendFactory>(), is_main_cache) {}
zhongyifcd139742017-02-02 02:26:42557
558MockHttpCache::MockHttpCache(
559 std::unique_ptr<HttpCache::BackendFactory> disk_cache_factory,
zhongyi4928bd52017-02-08 02:16:27560 bool is_main_cache)
ricea2deef682016-09-09 08:04:07561 : http_cache_(base::MakeUnique<MockNetworkLayer>(),
dchengc7eeda422015-12-26 03:56:48562 std::move(disk_cache_factory),
zhongyi4928bd52017-02-08 02:16:27563 is_main_cache) {}
[email protected]f40156002011-11-22 21:19:08564
pcc64237962015-02-25 03:32:53565disk_cache::Backend* MockHttpCache::backend() {
ttuttle859dc7a2015-04-23 19:42:29566 TestCompletionCallback cb;
[email protected]f40156002011-11-22 21:19:08567 disk_cache::Backend* backend;
[email protected]2a65aceb82011-12-19 20:59:27568 int rv = http_cache_.GetBackend(&backend, cb.callback());
[email protected]f40156002011-11-22 21:19:08569 rv = cb.GetResult(rv);
ttuttle859dc7a2015-04-23 19:42:29570 return (rv == OK) ? backend : NULL;
pcc64237962015-02-25 03:32:53571}
572
573MockDiskCache* MockHttpCache::disk_cache() {
574 return static_cast<MockDiskCache*>(backend());
[email protected]f40156002011-11-22 21:19:08575}
576
danakj1fd259a02016-04-16 03:17:09577int MockHttpCache::CreateTransaction(std::unique_ptr<HttpTransaction>* trans) {
ttuttle859dc7a2015-04-23 19:42:29578 return http_cache_.CreateTransaction(DEFAULT_PRIORITY, trans);
[email protected]027bd85a2013-12-27 22:39:10579}
580
shivanisha3701cde02017-02-09 17:43:07581void MockHttpCache::SimulateCacheLockTimeout() {
582 http_cache_.SimulateCacheLockTimeout();
[email protected]8aacaf382014-06-24 05:33:41583}
584
rvargas43dc8fd2015-01-07 23:03:25585void MockHttpCache::FailConditionalizations() {
586 http_cache_.FailConditionalizationForTest();
587}
588
[email protected]f40156002011-11-22 21:19:08589bool MockHttpCache::ReadResponseInfo(disk_cache::Entry* disk_entry,
ttuttle859dc7a2015-04-23 19:42:29590 HttpResponseInfo* response_info,
[email protected]f40156002011-11-22 21:19:08591 bool* response_truncated) {
592 int size = disk_entry->GetDataSize(0);
593
ttuttle859dc7a2015-04-23 19:42:29594 TestCompletionCallback cb;
595 scoped_refptr<IOBuffer> buffer(new IOBuffer(size));
[email protected]90499482013-06-01 00:39:50596 int rv = disk_entry->ReadData(0, 0, buffer.get(), size, cb.callback());
[email protected]f40156002011-11-22 21:19:08597 rv = cb.GetResult(rv);
598 EXPECT_EQ(size, rv);
599
ttuttle859dc7a2015-04-23 19:42:29600 return HttpCache::ParseResponseInfo(buffer->data(), size, response_info,
601 response_truncated);
[email protected]f40156002011-11-22 21:19:08602}
603
ttuttle859dc7a2015-04-23 19:42:29604bool MockHttpCache::WriteResponseInfo(disk_cache::Entry* disk_entry,
605 const HttpResponseInfo* response_info,
606 bool skip_transient_headers,
607 bool response_truncated) {
brettwbd4d7112015-06-03 04:29:25608 base::Pickle pickle;
[email protected]f40156002011-11-22 21:19:08609 response_info->Persist(
610 &pickle, skip_transient_headers, response_truncated);
611
ttuttle859dc7a2015-04-23 19:42:29612 TestCompletionCallback cb;
613 scoped_refptr<WrappedIOBuffer> data(
614 new WrappedIOBuffer(reinterpret_cast<const char*>(pickle.data())));
[email protected]f40156002011-11-22 21:19:08615 int len = static_cast<int>(pickle.size());
616
[email protected]90499482013-06-01 00:39:50617 int rv = disk_entry->WriteData(0, 0, data.get(), len, cb.callback(), true);
[email protected]f40156002011-11-22 21:19:08618 rv = cb.GetResult(rv);
619 return (rv == len);
620}
621
622bool MockHttpCache::OpenBackendEntry(const std::string& key,
623 disk_cache::Entry** entry) {
ttuttle859dc7a2015-04-23 19:42:29624 TestCompletionCallback cb;
pcc64237962015-02-25 03:32:53625 int rv = backend()->OpenEntry(key, entry, cb.callback());
ttuttle859dc7a2015-04-23 19:42:29626 return (cb.GetResult(rv) == OK);
[email protected]f40156002011-11-22 21:19:08627}
628
629bool MockHttpCache::CreateBackendEntry(const std::string& key,
630 disk_cache::Entry** entry,
ttuttle859dc7a2015-04-23 19:42:29631 NetLog* net_log) {
632 TestCompletionCallback cb;
pcc64237962015-02-25 03:32:53633 int rv = backend()->CreateEntry(key, entry, cb.callback());
ttuttle859dc7a2015-04-23 19:42:29634 return (cb.GetResult(rv) == OK);
[email protected]f40156002011-11-22 21:19:08635}
636
637// Static.
638int MockHttpCache::GetTestMode(int test_mode) {
639 if (!g_test_mode)
640 return test_mode;
641
642 return g_test_mode;
643}
644
645// Static.
646void MockHttpCache::SetTestMode(int test_mode) {
647 g_test_mode = test_mode;
648}
649
650//-----------------------------------------------------------------------------
651
652int MockDiskCacheNoCB::CreateEntry(const std::string& key,
653 disk_cache::Entry** entry,
ttuttle859dc7a2015-04-23 19:42:29654 const CompletionCallback& callback) {
655 return ERR_IO_PENDING;
[email protected]f40156002011-11-22 21:19:08656}
657
658//-----------------------------------------------------------------------------
659
[email protected]2a65aceb82011-12-19 20:59:27660int MockBackendNoCbFactory::CreateBackend(
ttuttle859dc7a2015-04-23 19:42:29661 NetLog* net_log,
danakj1fd259a02016-04-16 03:17:09662 std::unique_ptr<disk_cache::Backend>* backend,
ttuttle859dc7a2015-04-23 19:42:29663 const CompletionCallback& callback) {
[email protected]8c3f5a32013-08-01 11:57:53664 backend->reset(new MockDiskCacheNoCB());
ttuttle859dc7a2015-04-23 19:42:29665 return OK;
[email protected]f40156002011-11-22 21:19:08666}
667
668//-----------------------------------------------------------------------------
669
670MockBlockingBackendFactory::MockBlockingBackendFactory()
[email protected]2a65aceb82011-12-19 20:59:27671 : backend_(NULL),
672 block_(true),
673 fail_(false) {
674}
675
676MockBlockingBackendFactory::~MockBlockingBackendFactory() {
[email protected]f40156002011-11-22 21:19:08677}
678
679int MockBlockingBackendFactory::CreateBackend(
ttuttle859dc7a2015-04-23 19:42:29680 NetLog* net_log,
danakj1fd259a02016-04-16 03:17:09681 std::unique_ptr<disk_cache::Backend>* backend,
ttuttle859dc7a2015-04-23 19:42:29682 const CompletionCallback& callback) {
[email protected]f40156002011-11-22 21:19:08683 if (!block_) {
684 if (!fail_)
[email protected]8c3f5a32013-08-01 11:57:53685 backend->reset(new MockDiskCache());
[email protected]f40156002011-11-22 21:19:08686 return Result();
687 }
688
689 backend_ = backend;
690 callback_ = callback;
ttuttle859dc7a2015-04-23 19:42:29691 return ERR_IO_PENDING;
[email protected]f40156002011-11-22 21:19:08692}
693
694void MockBlockingBackendFactory::FinishCreation() {
695 block_ = false;
[email protected]2a65aceb82011-12-19 20:59:27696 if (!callback_.is_null()) {
[email protected]f40156002011-11-22 21:19:08697 if (!fail_)
[email protected]8c3f5a32013-08-01 11:57:53698 backend_->reset(new MockDiskCache());
ttuttle859dc7a2015-04-23 19:42:29699 CompletionCallback cb = callback_;
[email protected]2a65aceb82011-12-19 20:59:27700 callback_.Reset();
701 cb.Run(Result()); // This object can be deleted here.
[email protected]f40156002011-11-22 21:19:08702 }
703}
ttuttle859dc7a2015-04-23 19:42:29704
705} // namespace net