blob: 3f8cce4bc0a2cde211978c9055017ec976ed5067 [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
[email protected]42c459632011-12-17 02:20:2323// We can override the test mode for a given operation by setting this global
24// variable.
25int g_test_mode = 0;
26
[email protected]f40156002011-11-22 21:19:0827int GetTestModeForEntry(const std::string& key) {
28 // 'key' is prefixed with an identifier if it corresponds to a cached POST.
29 // Skip past that to locate the actual URL.
30 //
31 // TODO(darin): It breaks the abstraction a bit that we assume 'key' is an
32 // URL corresponding to a registered MockTransaction. It would be good to
33 // have another way to access the test_mode.
34 GURL url;
35 if (isdigit(key[0])) {
36 size_t slash = key.find('/');
37 DCHECK(slash != std::string::npos);
38 url = GURL(key.substr(slash + 1));
39 } else {
40 url = GURL(key);
41 }
42 const MockTransaction* t = FindMockTransaction(url);
43 DCHECK(t);
44 return t->test_mode;
45}
46
ttuttle859dc7a2015-04-23 19:42:2947void CallbackForwader(const CompletionCallback& callback, int result) {
[email protected]42c459632011-12-17 02:20:2348 callback.Run(result);
49}
[email protected]f40156002011-11-22 21:19:0850
51} // namespace
52
53//-----------------------------------------------------------------------------
54
55struct MockDiskEntry::CallbackInfo {
56 scoped_refptr<MockDiskEntry> entry;
ttuttle859dc7a2015-04-23 19:42:2957 CompletionCallback callback;
[email protected]f40156002011-11-22 21:19:0858 int result;
59};
60
[email protected]f40156002011-11-22 21:19:0861MockDiskEntry::MockDiskEntry(const std::string& key)
rvargas9ba24452015-07-18 03:13:2562 : key_(key),
63 doomed_(false),
64 sparse_(false),
65 fail_requests_(false),
66 fail_sparse_requests_(false),
67 busy_(false),
68 delayed_(false),
69 cancel_(false) {
[email protected]f40156002011-11-22 21:19:0870 test_mode_ = GetTestModeForEntry(key);
71}
72
73void MockDiskEntry::Doom() {
74 doomed_ = true;
75}
76
77void MockDiskEntry::Close() {
78 Release();
79}
80
81std::string MockDiskEntry::GetKey() const {
[email protected]f40156002011-11-22 21:19:0882 return key_;
83}
84
85base::Time MockDiskEntry::GetLastUsed() const {
86 return base::Time::FromInternalValue(0);
87}
88
89base::Time MockDiskEntry::GetLastModified() const {
90 return base::Time::FromInternalValue(0);
91}
92
avid0181f32015-12-10 19:41:4793int32_t MockDiskEntry::GetDataSize(int index) const {
[email protected]f40156002011-11-22 21:19:0894 DCHECK(index >= 0 && index < kNumCacheEntryDataIndices);
avid0181f32015-12-10 19:41:4795 return static_cast<int32_t>(data_[index].size());
[email protected]f40156002011-11-22 21:19:0896}
97
ttuttle859dc7a2015-04-23 19:42:2998int MockDiskEntry::ReadData(int index,
99 int offset,
100 IOBuffer* buf,
101 int buf_len,
102 const CompletionCallback& callback) {
[email protected]f40156002011-11-22 21:19:08103 DCHECK(index >= 0 && index < kNumCacheEntryDataIndices);
[email protected]2a65aceb82011-12-19 20:59:27104 DCHECK(!callback.is_null());
[email protected]f40156002011-11-22 21:19:08105
106 if (fail_requests_)
ttuttle859dc7a2015-04-23 19:42:29107 return ERR_CACHE_READ_FAILURE;
[email protected]f40156002011-11-22 21:19:08108
109 if (offset < 0 || offset > static_cast<int>(data_[index].size()))
ttuttle859dc7a2015-04-23 19:42:29110 return ERR_FAILED;
[email protected]f40156002011-11-22 21:19:08111 if (static_cast<size_t>(offset) == data_[index].size())
112 return 0;
113
114 int num = std::min(buf_len, static_cast<int>(data_[index].size()) - offset);
115 memcpy(buf->data(), &data_[index][offset], num);
116
117 if (MockHttpCache::GetTestMode(test_mode_) & TEST_MODE_SYNC_CACHE_READ)
118 return num;
119
120 CallbackLater(callback, num);
ttuttle859dc7a2015-04-23 19:42:29121 return ERR_IO_PENDING;
[email protected]f40156002011-11-22 21:19:08122}
123
ttuttle859dc7a2015-04-23 19:42:29124int MockDiskEntry::WriteData(int index,
125 int offset,
126 IOBuffer* buf,
127 int buf_len,
128 const CompletionCallback& callback,
129 bool truncate) {
[email protected]f40156002011-11-22 21:19:08130 DCHECK(index >= 0 && index < kNumCacheEntryDataIndices);
[email protected]2a65aceb82011-12-19 20:59:27131 DCHECK(!callback.is_null());
[email protected]f40156002011-11-22 21:19:08132 DCHECK(truncate);
133
134 if (fail_requests_) {
ttuttle859dc7a2015-04-23 19:42:29135 CallbackLater(callback, ERR_CACHE_READ_FAILURE);
136 return ERR_IO_PENDING;
[email protected]f40156002011-11-22 21:19:08137 }
138
139 if (offset < 0 || offset > static_cast<int>(data_[index].size()))
ttuttle859dc7a2015-04-23 19:42:29140 return ERR_FAILED;
[email protected]f40156002011-11-22 21:19:08141
142 data_[index].resize(offset + buf_len);
143 if (buf_len)
144 memcpy(&data_[index][offset], buf->data(), buf_len);
145
146 if (MockHttpCache::GetTestMode(test_mode_) & TEST_MODE_SYNC_CACHE_WRITE)
147 return buf_len;
148
149 CallbackLater(callback, buf_len);
ttuttle859dc7a2015-04-23 19:42:29150 return ERR_IO_PENDING;
[email protected]f40156002011-11-22 21:19:08151}
152
avid0181f32015-12-10 19:41:47153int MockDiskEntry::ReadSparseData(int64_t offset,
ttuttle859dc7a2015-04-23 19:42:29154 IOBuffer* buf,
155 int buf_len,
156 const CompletionCallback& callback) {
[email protected]2a65aceb82011-12-19 20:59:27157 DCHECK(!callback.is_null());
[email protected]954bbe42013-08-30 12:38:39158 if (fail_sparse_requests_)
ttuttle859dc7a2015-04-23 19:42:29159 return ERR_NOT_IMPLEMENTED;
rvargas9ba24452015-07-18 03:13:25160 if (!sparse_ || busy_ || cancel_)
ttuttle859dc7a2015-04-23 19:42:29161 return ERR_CACHE_OPERATION_NOT_SUPPORTED;
[email protected]f40156002011-11-22 21:19:08162 if (offset < 0)
ttuttle859dc7a2015-04-23 19:42:29163 return ERR_FAILED;
[email protected]f40156002011-11-22 21:19:08164
165 if (fail_requests_)
ttuttle859dc7a2015-04-23 19:42:29166 return ERR_CACHE_READ_FAILURE;
[email protected]f40156002011-11-22 21:19:08167
avid0181f32015-12-10 19:41:47168 DCHECK(offset < std::numeric_limits<int32_t>::max());
[email protected]f40156002011-11-22 21:19:08169 int real_offset = static_cast<int>(offset);
170 if (!buf_len)
171 return 0;
172
173 int num = std::min(static_cast<int>(data_[1].size()) - real_offset,
174 buf_len);
175 memcpy(buf->data(), &data_[1][real_offset], num);
176
177 if (MockHttpCache::GetTestMode(test_mode_) & TEST_MODE_SYNC_CACHE_READ)
178 return num;
179
180 CallbackLater(callback, num);
181 busy_ = true;
182 delayed_ = false;
ttuttle859dc7a2015-04-23 19:42:29183 return ERR_IO_PENDING;
[email protected]f40156002011-11-22 21:19:08184}
185
avid0181f32015-12-10 19:41:47186int MockDiskEntry::WriteSparseData(int64_t offset,
ttuttle859dc7a2015-04-23 19:42:29187 IOBuffer* buf,
[email protected]f40156002011-11-22 21:19:08188 int buf_len,
ttuttle859dc7a2015-04-23 19:42:29189 const CompletionCallback& callback) {
[email protected]2a65aceb82011-12-19 20:59:27190 DCHECK(!callback.is_null());
[email protected]954bbe42013-08-30 12:38:39191 if (fail_sparse_requests_)
ttuttle859dc7a2015-04-23 19:42:29192 return ERR_NOT_IMPLEMENTED;
rvargas9ba24452015-07-18 03:13:25193 if (busy_ || cancel_)
ttuttle859dc7a2015-04-23 19:42:29194 return ERR_CACHE_OPERATION_NOT_SUPPORTED;
[email protected]f40156002011-11-22 21:19:08195 if (!sparse_) {
196 if (data_[1].size())
ttuttle859dc7a2015-04-23 19:42:29197 return ERR_CACHE_OPERATION_NOT_SUPPORTED;
[email protected]f40156002011-11-22 21:19:08198 sparse_ = true;
199 }
200 if (offset < 0)
ttuttle859dc7a2015-04-23 19:42:29201 return ERR_FAILED;
[email protected]f40156002011-11-22 21:19:08202 if (!buf_len)
203 return 0;
204
205 if (fail_requests_)
ttuttle859dc7a2015-04-23 19:42:29206 return ERR_CACHE_READ_FAILURE;
[email protected]f40156002011-11-22 21:19:08207
avid0181f32015-12-10 19:41:47208 DCHECK(offset < std::numeric_limits<int32_t>::max());
[email protected]f40156002011-11-22 21:19:08209 int real_offset = static_cast<int>(offset);
210
211 if (static_cast<int>(data_[1].size()) < real_offset + buf_len)
212 data_[1].resize(real_offset + buf_len);
213
214 memcpy(&data_[1][real_offset], buf->data(), buf_len);
215 if (MockHttpCache::GetTestMode(test_mode_) & TEST_MODE_SYNC_CACHE_WRITE)
216 return buf_len;
217
218 CallbackLater(callback, buf_len);
ttuttle859dc7a2015-04-23 19:42:29219 return ERR_IO_PENDING;
[email protected]f40156002011-11-22 21:19:08220}
221
avid0181f32015-12-10 19:41:47222int MockDiskEntry::GetAvailableRange(int64_t offset,
ttuttle859dc7a2015-04-23 19:42:29223 int len,
avid0181f32015-12-10 19:41:47224 int64_t* start,
ttuttle859dc7a2015-04-23 19:42:29225 const CompletionCallback& callback) {
[email protected]2a65aceb82011-12-19 20:59:27226 DCHECK(!callback.is_null());
rvargas9ba24452015-07-18 03:13:25227 if (!sparse_ || busy_ || cancel_)
ttuttle859dc7a2015-04-23 19:42:29228 return ERR_CACHE_OPERATION_NOT_SUPPORTED;
[email protected]f40156002011-11-22 21:19:08229 if (offset < 0)
ttuttle859dc7a2015-04-23 19:42:29230 return ERR_FAILED;
[email protected]f40156002011-11-22 21:19:08231
232 if (fail_requests_)
ttuttle859dc7a2015-04-23 19:42:29233 return ERR_CACHE_READ_FAILURE;
[email protected]f40156002011-11-22 21:19:08234
235 *start = offset;
avid0181f32015-12-10 19:41:47236 DCHECK(offset < std::numeric_limits<int32_t>::max());
[email protected]f40156002011-11-22 21:19:08237 int real_offset = static_cast<int>(offset);
238 if (static_cast<int>(data_[1].size()) < real_offset)
239 return 0;
240
241 int num = std::min(static_cast<int>(data_[1].size()) - real_offset, len);
242 int count = 0;
243 for (; num > 0; num--, real_offset++) {
244 if (!count) {
245 if (data_[1][real_offset]) {
246 count++;
247 *start = real_offset;
248 }
249 } else {
250 if (!data_[1][real_offset])
251 break;
252 count++;
253 }
254 }
255 if (MockHttpCache::GetTestMode(test_mode_) & TEST_MODE_SYNC_CACHE_WRITE)
256 return count;
257
258 CallbackLater(callback, count);
ttuttle859dc7a2015-04-23 19:42:29259 return ERR_IO_PENDING;
[email protected]f40156002011-11-22 21:19:08260}
261
262bool MockDiskEntry::CouldBeSparse() const {
[email protected]954bbe42013-08-30 12:38:39263 if (fail_sparse_requests_)
264 return false;
[email protected]f40156002011-11-22 21:19:08265 return sparse_;
266}
267
268void MockDiskEntry::CancelSparseIO() {
269 cancel_ = true;
270}
271
ttuttle859dc7a2015-04-23 19:42:29272int MockDiskEntry::ReadyForSparseIO(const CompletionCallback& callback) {
[email protected]954bbe42013-08-30 12:38:39273 if (fail_sparse_requests_)
ttuttle859dc7a2015-04-23 19:42:29274 return ERR_NOT_IMPLEMENTED;
[email protected]f40156002011-11-22 21:19:08275 if (!cancel_)
ttuttle859dc7a2015-04-23 19:42:29276 return OK;
[email protected]f40156002011-11-22 21:19:08277
278 cancel_ = false;
[email protected]2a65aceb82011-12-19 20:59:27279 DCHECK(!callback.is_null());
[email protected]f40156002011-11-22 21:19:08280 if (MockHttpCache::GetTestMode(test_mode_) & TEST_MODE_SYNC_CACHE_READ)
ttuttle859dc7a2015-04-23 19:42:29281 return OK;
[email protected]f40156002011-11-22 21:19:08282
[email protected]2a65aceb82011-12-19 20:59:27283 // The pending operation is already in the message loop (and hopefully
[email protected]f40156002011-11-22 21:19:08284 // already in the second pass). Just notify the caller that it finished.
285 CallbackLater(callback, 0);
ttuttle859dc7a2015-04-23 19:42:29286 return ERR_IO_PENDING;
[email protected]f40156002011-11-22 21:19:08287}
288
289// If |value| is true, don't deliver any completion callbacks until called
290// again with |value| set to false. Caution: remember to enable callbacks
291// again or all subsequent tests will fail.
292// Static.
293void MockDiskEntry::IgnoreCallbacks(bool value) {
294 if (ignore_callbacks_ == value)
295 return;
296 ignore_callbacks_ = value;
297 if (!value)
ttuttle859dc7a2015-04-23 19:42:29298 StoreAndDeliverCallbacks(false, NULL, CompletionCallback(), 0);
[email protected]f40156002011-11-22 21:19:08299}
300
301MockDiskEntry::~MockDiskEntry() {
302}
303
304// Unlike the callbacks for MockHttpTransaction, we want this one to run even
305// if the consumer called Close on the MockDiskEntry. We achieve that by
306// leveraging the fact that this class is reference counted.
ttuttle859dc7a2015-04-23 19:42:29307void MockDiskEntry::CallbackLater(const CompletionCallback& callback,
[email protected]f40156002011-11-22 21:19:08308 int result) {
[email protected]2c5c9d52011-12-07 14:15:36309 if (ignore_callbacks_)
310 return StoreAndDeliverCallbacks(true, this, callback, result);
skyostil4891b25b2015-06-11 11:43:45311 base::ThreadTaskRunnerHandle::Get()->PostTask(
[email protected]2da659e2013-05-23 20:51:34312 FROM_HERE,
313 base::Bind(&MockDiskEntry::RunCallback, this, callback, result));
[email protected]f40156002011-11-22 21:19:08314}
315
ttuttle859dc7a2015-04-23 19:42:29316void MockDiskEntry::RunCallback(const CompletionCallback& callback,
317 int result) {
[email protected]f40156002011-11-22 21:19:08318 if (busy_) {
319 // This is kind of hacky, but controlling the behavior of just this entry
320 // from a test is sort of complicated. What we really want to do is
321 // delay the delivery of a sparse IO operation a little more so that the
322 // request start operation (async) will finish without seeing the end of
323 // this operation (already posted to the message loop)... and without
324 // just delaying for n mS (which may cause trouble with slow bots). So
325 // we re-post this operation (all async sparse IO operations will take two
[email protected]2a65aceb82011-12-19 20:59:27326 // trips through the message loop instead of one).
[email protected]f40156002011-11-22 21:19:08327 if (!delayed_) {
328 delayed_ = true;
329 return CallbackLater(callback, result);
330 }
331 }
332 busy_ = false;
[email protected]2a65aceb82011-12-19 20:59:27333 callback.Run(result);
[email protected]f40156002011-11-22 21:19:08334}
335
336// When |store| is true, stores the callback to be delivered later; otherwise
337// delivers any callback previously stored.
338// Static.
ttuttle859dc7a2015-04-23 19:42:29339void MockDiskEntry::StoreAndDeliverCallbacks(bool store,
340 MockDiskEntry* entry,
341 const CompletionCallback& callback,
342 int result) {
[email protected]f40156002011-11-22 21:19:08343 static std::vector<CallbackInfo> callback_list;
344 if (store) {
[email protected]2c5c9d52011-12-07 14:15:36345 CallbackInfo c = {entry, callback, result};
[email protected]f40156002011-11-22 21:19:08346 callback_list.push_back(c);
347 } else {
[email protected]2c5c9d52011-12-07 14:15:36348 for (size_t i = 0; i < callback_list.size(); i++) {
[email protected]f40156002011-11-22 21:19:08349 CallbackInfo& c = callback_list[i];
[email protected]2c5c9d52011-12-07 14:15:36350 c.entry->CallbackLater(c.callback, c.result);
[email protected]f40156002011-11-22 21:19:08351 }
352 callback_list.clear();
353 }
354}
355
356// Statics.
[email protected]f40156002011-11-22 21:19:08357bool MockDiskEntry::ignore_callbacks_ = false;
358
359//-----------------------------------------------------------------------------
360
[email protected]f40156002011-11-22 21:19:08361MockDiskCache::MockDiskCache()
362 : open_count_(0), create_count_(0), fail_requests_(false),
[email protected]954bbe42013-08-30 12:38:39363 soft_failures_(false), double_create_check_(true),
364 fail_sparse_requests_(false) {
[email protected]f40156002011-11-22 21:19:08365}
366
367MockDiskCache::~MockDiskCache() {
368 ReleaseAll();
369}
370
ttuttle859dc7a2015-04-23 19:42:29371CacheType MockDiskCache::GetCacheType() const {
372 return DISK_CACHE;
[email protected]6e143962012-08-09 21:34:33373}
374
avid0181f32015-12-10 19:41:47375int32_t MockDiskCache::GetEntryCount() const {
376 return static_cast<int32_t>(entries_.size());
[email protected]f40156002011-11-22 21:19:08377}
378
ttuttle859dc7a2015-04-23 19:42:29379int MockDiskCache::OpenEntry(const std::string& key,
380 disk_cache::Entry** entry,
381 const CompletionCallback& callback) {
[email protected]2a65aceb82011-12-19 20:59:27382 DCHECK(!callback.is_null());
[email protected]f40156002011-11-22 21:19:08383 if (fail_requests_)
ttuttle859dc7a2015-04-23 19:42:29384 return ERR_CACHE_OPEN_FAILURE;
[email protected]f40156002011-11-22 21:19:08385
386 EntryMap::iterator it = entries_.find(key);
387 if (it == entries_.end())
ttuttle859dc7a2015-04-23 19:42:29388 return ERR_CACHE_OPEN_FAILURE;
[email protected]f40156002011-11-22 21:19:08389
390 if (it->second->is_doomed()) {
391 it->second->Release();
392 entries_.erase(it);
ttuttle859dc7a2015-04-23 19:42:29393 return ERR_CACHE_OPEN_FAILURE;
[email protected]f40156002011-11-22 21:19:08394 }
395
396 open_count_++;
397
398 it->second->AddRef();
399 *entry = it->second;
400
401 if (soft_failures_)
402 it->second->set_fail_requests();
403
404 if (GetTestModeForEntry(key) & TEST_MODE_SYNC_CACHE_START)
ttuttle859dc7a2015-04-23 19:42:29405 return OK;
[email protected]f40156002011-11-22 21:19:08406
ttuttle859dc7a2015-04-23 19:42:29407 CallbackLater(callback, OK);
408 return ERR_IO_PENDING;
[email protected]f40156002011-11-22 21:19:08409}
410
411int MockDiskCache::CreateEntry(const std::string& key,
412 disk_cache::Entry** entry,
ttuttle859dc7a2015-04-23 19:42:29413 const CompletionCallback& callback) {
[email protected]2a65aceb82011-12-19 20:59:27414 DCHECK(!callback.is_null());
[email protected]f40156002011-11-22 21:19:08415 if (fail_requests_)
ttuttle859dc7a2015-04-23 19:42:29416 return ERR_CACHE_CREATE_FAILURE;
[email protected]f40156002011-11-22 21:19:08417
418 EntryMap::iterator it = entries_.find(key);
419 if (it != entries_.end()) {
[email protected]6a019de2011-11-30 19:57:29420 if (!it->second->is_doomed()) {
421 if (double_create_check_)
422 NOTREACHED();
423 else
ttuttle859dc7a2015-04-23 19:42:29424 return ERR_CACHE_CREATE_FAILURE;
[email protected]6a019de2011-11-30 19:57:29425 }
[email protected]f40156002011-11-22 21:19:08426 it->second->Release();
427 entries_.erase(it);
428 }
429
430 create_count_++;
431
432 MockDiskEntry* new_entry = new MockDiskEntry(key);
433
434 new_entry->AddRef();
435 entries_[key] = new_entry;
436
437 new_entry->AddRef();
438 *entry = new_entry;
439
440 if (soft_failures_)
441 new_entry->set_fail_requests();
442
[email protected]954bbe42013-08-30 12:38:39443 if (fail_sparse_requests_)
444 new_entry->set_fail_sparse_requests();
445
[email protected]f40156002011-11-22 21:19:08446 if (GetTestModeForEntry(key) & TEST_MODE_SYNC_CACHE_START)
ttuttle859dc7a2015-04-23 19:42:29447 return OK;
[email protected]f40156002011-11-22 21:19:08448
ttuttle859dc7a2015-04-23 19:42:29449 CallbackLater(callback, OK);
450 return ERR_IO_PENDING;
[email protected]f40156002011-11-22 21:19:08451}
452
453int MockDiskCache::DoomEntry(const std::string& key,
ttuttle859dc7a2015-04-23 19:42:29454 const CompletionCallback& callback) {
[email protected]42c459632011-12-17 02:20:23455 DCHECK(!callback.is_null());
[email protected]f40156002011-11-22 21:19:08456 EntryMap::iterator it = entries_.find(key);
457 if (it != entries_.end()) {
458 it->second->Release();
459 entries_.erase(it);
460 }
461
462 if (GetTestModeForEntry(key) & TEST_MODE_SYNC_CACHE_START)
ttuttle859dc7a2015-04-23 19:42:29463 return OK;
[email protected]f40156002011-11-22 21:19:08464
ttuttle859dc7a2015-04-23 19:42:29465 CallbackLater(callback, OK);
466 return ERR_IO_PENDING;
[email protected]f40156002011-11-22 21:19:08467}
468
ttuttle859dc7a2015-04-23 19:42:29469int MockDiskCache::DoomAllEntries(const CompletionCallback& callback) {
470 return ERR_NOT_IMPLEMENTED;
[email protected]dbe1ffe2011-12-12 19:18:56471}
472
[email protected]f40156002011-11-22 21:19:08473int MockDiskCache::DoomEntriesBetween(const base::Time initial_time,
474 const base::Time end_time,
ttuttle859dc7a2015-04-23 19:42:29475 const CompletionCallback& callback) {
476 return ERR_NOT_IMPLEMENTED;
[email protected]dbe1ffe2011-12-12 19:18:56477}
478
[email protected]f40156002011-11-22 21:19:08479int MockDiskCache::DoomEntriesSince(const base::Time initial_time,
ttuttle859dc7a2015-04-23 19:42:29480 const CompletionCallback& callback) {
481 return ERR_NOT_IMPLEMENTED;
[email protected]f40156002011-11-22 21:19:08482}
483
msramekaee01ceb2015-10-07 14:23:33484int MockDiskCache::CalculateSizeOfAllEntries(
485 const CompletionCallback& callback) {
486 return ERR_NOT_IMPLEMENTED;
487}
488
gavinpb1aaa052014-09-24 14:54:35489class MockDiskCache::NotImplementedIterator : public Iterator {
490 public:
dchengb03027d2014-10-21 12:00:20491 int OpenNextEntry(disk_cache::Entry** next_entry,
ttuttle859dc7a2015-04-23 19:42:29492 const CompletionCallback& callback) override {
493 return ERR_NOT_IMPLEMENTED;
gavinpb1aaa052014-09-24 14:54:35494 }
495};
[email protected]275dbf42011-12-12 20:41:01496
danakj1fd259a02016-04-16 03:17:09497std::unique_ptr<disk_cache::Backend::Iterator> MockDiskCache::CreateIterator() {
498 return std::unique_ptr<Iterator>(new NotImplementedIterator());
[email protected]f40156002011-11-22 21:19:08499}
500
payal.pandey90b81372015-04-30 06:53:04501void MockDiskCache::GetStats(base::StringPairs* stats) {
[email protected]f40156002011-11-22 21:19:08502}
503
504void MockDiskCache::OnExternalCacheHit(const std::string& key) {
505}
506
507void MockDiskCache::ReleaseAll() {
508 EntryMap::iterator it = entries_.begin();
509 for (; it != entries_.end(); ++it)
510 it->second->Release();
511 entries_.clear();
512}
513
ttuttle859dc7a2015-04-23 19:42:29514void MockDiskCache::CallbackLater(const CompletionCallback& callback,
[email protected]f40156002011-11-22 21:19:08515 int result) {
skyostil4891b25b2015-06-11 11:43:45516 base::ThreadTaskRunnerHandle::Get()->PostTask(
[email protected]42c459632011-12-17 02:20:23517 FROM_HERE, base::Bind(&CallbackForwader, callback, result));
[email protected]f40156002011-11-22 21:19:08518}
519
520//-----------------------------------------------------------------------------
521
danakj1fd259a02016-04-16 03:17:09522int MockBackendFactory::CreateBackend(
523 NetLog* net_log,
524 std::unique_ptr<disk_cache::Backend>* backend,
525 const CompletionCallback& callback) {
[email protected]8c3f5a32013-08-01 11:57:53526 backend->reset(new MockDiskCache());
ttuttle859dc7a2015-04-23 19:42:29527 return OK;
[email protected]f40156002011-11-22 21:19:08528}
529
530//-----------------------------------------------------------------------------
531
mmenkebc31a2c2015-10-29 13:44:45532MockHttpCache::MockHttpCache()
danakj1fd259a02016-04-16 03:17:09533 : MockHttpCache(base::WrapUnique(new MockBackendFactory())) {}
[email protected]f40156002011-11-22 21:19:08534
mmenkebc31a2c2015-10-29 13:44:45535MockHttpCache::MockHttpCache(
danakj1fd259a02016-04-16 03:17:09536 std::unique_ptr<HttpCache::BackendFactory> disk_cache_factory)
537 : http_cache_(base::WrapUnique(new MockNetworkLayer()),
dchengc7eeda422015-12-26 03:56:48538 std::move(disk_cache_factory),
mmenkebc31a2c2015-10-29 13:44:45539 true) {}
[email protected]f40156002011-11-22 21:19:08540
pcc64237962015-02-25 03:32:53541disk_cache::Backend* MockHttpCache::backend() {
ttuttle859dc7a2015-04-23 19:42:29542 TestCompletionCallback cb;
[email protected]f40156002011-11-22 21:19:08543 disk_cache::Backend* backend;
[email protected]2a65aceb82011-12-19 20:59:27544 int rv = http_cache_.GetBackend(&backend, cb.callback());
[email protected]f40156002011-11-22 21:19:08545 rv = cb.GetResult(rv);
ttuttle859dc7a2015-04-23 19:42:29546 return (rv == OK) ? backend : NULL;
pcc64237962015-02-25 03:32:53547}
548
549MockDiskCache* MockHttpCache::disk_cache() {
550 return static_cast<MockDiskCache*>(backend());
[email protected]f40156002011-11-22 21:19:08551}
552
danakj1fd259a02016-04-16 03:17:09553int MockHttpCache::CreateTransaction(std::unique_ptr<HttpTransaction>* trans) {
ttuttle859dc7a2015-04-23 19:42:29554 return http_cache_.CreateTransaction(DEFAULT_PRIORITY, trans);
[email protected]027bd85a2013-12-27 22:39:10555}
556
[email protected]8aacaf382014-06-24 05:33:41557void MockHttpCache::BypassCacheLock() {
558 http_cache_.BypassLockForTest();
559}
560
rvargas43dc8fd2015-01-07 23:03:25561void MockHttpCache::FailConditionalizations() {
562 http_cache_.FailConditionalizationForTest();
563}
564
[email protected]f40156002011-11-22 21:19:08565bool MockHttpCache::ReadResponseInfo(disk_cache::Entry* disk_entry,
ttuttle859dc7a2015-04-23 19:42:29566 HttpResponseInfo* response_info,
[email protected]f40156002011-11-22 21:19:08567 bool* response_truncated) {
568 int size = disk_entry->GetDataSize(0);
569
ttuttle859dc7a2015-04-23 19:42:29570 TestCompletionCallback cb;
571 scoped_refptr<IOBuffer> buffer(new IOBuffer(size));
[email protected]90499482013-06-01 00:39:50572 int rv = disk_entry->ReadData(0, 0, buffer.get(), size, cb.callback());
[email protected]f40156002011-11-22 21:19:08573 rv = cb.GetResult(rv);
574 EXPECT_EQ(size, rv);
575
ttuttle859dc7a2015-04-23 19:42:29576 return HttpCache::ParseResponseInfo(buffer->data(), size, response_info,
577 response_truncated);
[email protected]f40156002011-11-22 21:19:08578}
579
ttuttle859dc7a2015-04-23 19:42:29580bool MockHttpCache::WriteResponseInfo(disk_cache::Entry* disk_entry,
581 const HttpResponseInfo* response_info,
582 bool skip_transient_headers,
583 bool response_truncated) {
brettwbd4d7112015-06-03 04:29:25584 base::Pickle pickle;
[email protected]f40156002011-11-22 21:19:08585 response_info->Persist(
586 &pickle, skip_transient_headers, response_truncated);
587
ttuttle859dc7a2015-04-23 19:42:29588 TestCompletionCallback cb;
589 scoped_refptr<WrappedIOBuffer> data(
590 new WrappedIOBuffer(reinterpret_cast<const char*>(pickle.data())));
[email protected]f40156002011-11-22 21:19:08591 int len = static_cast<int>(pickle.size());
592
[email protected]90499482013-06-01 00:39:50593 int rv = disk_entry->WriteData(0, 0, data.get(), len, cb.callback(), true);
[email protected]f40156002011-11-22 21:19:08594 rv = cb.GetResult(rv);
595 return (rv == len);
596}
597
598bool MockHttpCache::OpenBackendEntry(const std::string& key,
599 disk_cache::Entry** entry) {
ttuttle859dc7a2015-04-23 19:42:29600 TestCompletionCallback cb;
pcc64237962015-02-25 03:32:53601 int rv = backend()->OpenEntry(key, entry, cb.callback());
ttuttle859dc7a2015-04-23 19:42:29602 return (cb.GetResult(rv) == OK);
[email protected]f40156002011-11-22 21:19:08603}
604
605bool MockHttpCache::CreateBackendEntry(const std::string& key,
606 disk_cache::Entry** entry,
ttuttle859dc7a2015-04-23 19:42:29607 NetLog* net_log) {
608 TestCompletionCallback cb;
pcc64237962015-02-25 03:32:53609 int rv = backend()->CreateEntry(key, entry, cb.callback());
ttuttle859dc7a2015-04-23 19:42:29610 return (cb.GetResult(rv) == OK);
[email protected]f40156002011-11-22 21:19:08611}
612
613// Static.
614int MockHttpCache::GetTestMode(int test_mode) {
615 if (!g_test_mode)
616 return test_mode;
617
618 return g_test_mode;
619}
620
621// Static.
622void MockHttpCache::SetTestMode(int test_mode) {
623 g_test_mode = test_mode;
624}
625
626//-----------------------------------------------------------------------------
627
628int MockDiskCacheNoCB::CreateEntry(const std::string& key,
629 disk_cache::Entry** entry,
ttuttle859dc7a2015-04-23 19:42:29630 const CompletionCallback& callback) {
631 return ERR_IO_PENDING;
[email protected]f40156002011-11-22 21:19:08632}
633
634//-----------------------------------------------------------------------------
635
[email protected]2a65aceb82011-12-19 20:59:27636int MockBackendNoCbFactory::CreateBackend(
ttuttle859dc7a2015-04-23 19:42:29637 NetLog* net_log,
danakj1fd259a02016-04-16 03:17:09638 std::unique_ptr<disk_cache::Backend>* backend,
ttuttle859dc7a2015-04-23 19:42:29639 const CompletionCallback& callback) {
[email protected]8c3f5a32013-08-01 11:57:53640 backend->reset(new MockDiskCacheNoCB());
ttuttle859dc7a2015-04-23 19:42:29641 return OK;
[email protected]f40156002011-11-22 21:19:08642}
643
644//-----------------------------------------------------------------------------
645
646MockBlockingBackendFactory::MockBlockingBackendFactory()
[email protected]2a65aceb82011-12-19 20:59:27647 : backend_(NULL),
648 block_(true),
649 fail_(false) {
650}
651
652MockBlockingBackendFactory::~MockBlockingBackendFactory() {
[email protected]f40156002011-11-22 21:19:08653}
654
655int MockBlockingBackendFactory::CreateBackend(
ttuttle859dc7a2015-04-23 19:42:29656 NetLog* net_log,
danakj1fd259a02016-04-16 03:17:09657 std::unique_ptr<disk_cache::Backend>* backend,
ttuttle859dc7a2015-04-23 19:42:29658 const CompletionCallback& callback) {
[email protected]f40156002011-11-22 21:19:08659 if (!block_) {
660 if (!fail_)
[email protected]8c3f5a32013-08-01 11:57:53661 backend->reset(new MockDiskCache());
[email protected]f40156002011-11-22 21:19:08662 return Result();
663 }
664
665 backend_ = backend;
666 callback_ = callback;
ttuttle859dc7a2015-04-23 19:42:29667 return ERR_IO_PENDING;
[email protected]f40156002011-11-22 21:19:08668}
669
670void MockBlockingBackendFactory::FinishCreation() {
671 block_ = false;
[email protected]2a65aceb82011-12-19 20:59:27672 if (!callback_.is_null()) {
[email protected]f40156002011-11-22 21:19:08673 if (!fail_)
[email protected]8c3f5a32013-08-01 11:57:53674 backend_->reset(new MockDiskCache());
ttuttle859dc7a2015-04-23 19:42:29675 CompletionCallback cb = callback_;
[email protected]2a65aceb82011-12-19 20:59:27676 callback_.Reset();
677 cb.Run(Result()); // This object can be deleted here.
[email protected]f40156002011-11-22 21:19:08678 }
679}
ttuttle859dc7a2015-04-23 19:42:29680
681} // namespace net