OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "net/http/mock_http_cache.h" | 5 #include "net/http/mock_http_cache.h" |
6 | 6 |
7 #include <limits> | 7 #include <limits> |
8 #include <utility> | 8 #include <utility> |
9 | 9 |
10 #include "base/bind.h" | 10 #include "base/bind.h" |
11 #include "base/location.h" | 11 #include "base/location.h" |
12 #include "base/memory/ptr_util.h" | 12 #include "base/memory/ptr_util.h" |
13 #include "base/single_thread_task_runner.h" | 13 #include "base/single_thread_task_runner.h" |
14 #include "base/threading/thread_task_runner_handle.h" | 14 #include "base/threading/thread_task_runner_handle.h" |
15 #include "net/base/completion_callback.h" | 15 #include "net/base/completion_callback.h" |
16 #include "net/base/net_errors.h" | 16 #include "net/base/net_errors.h" |
17 #include "testing/gtest/include/gtest/gtest.h" | 17 #include "testing/gtest/include/gtest/gtest.h" |
18 | 18 |
19 namespace net { | 19 namespace net { |
20 | 20 |
21 namespace { | 21 namespace { |
22 | 22 |
23 #if DCHECK_IS_ON() | |
24 // During testing, we are going to limit the size of a cache entry to this many | |
25 // bytes using DCHECKs in order to prevent a test from causing unbounded memory | |
26 // growth. In practice cache entry shouldn't come anywhere near this limit for | |
27 // tests that use the mock cache. If they do, that's likely a problem with the | |
28 // test. If a test requires using massive cache entries, they should use a real | |
29 // cache backend instead. | |
30 const int kMaxMockCacheEntrySize = 100000000; | |
gavinp
2016/06/28 14:24:43
Nit: I have trouble counting zeroes this quickly.
asanka
2016/06/28 16:55:38
Done. :-)
| |
31 #endif | |
32 | |
23 // We can override the test mode for a given operation by setting this global | 33 // We can override the test mode for a given operation by setting this global |
24 // variable. | 34 // variable. |
25 int g_test_mode = 0; | 35 int g_test_mode = 0; |
26 | 36 |
27 int GetTestModeForEntry(const std::string& key) { | 37 int GetTestModeForEntry(const std::string& key) { |
28 // 'key' is prefixed with an identifier if it corresponds to a cached POST. | 38 // 'key' is prefixed with an identifier if it corresponds to a cached POST. |
29 // Skip past that to locate the actual URL. | 39 // Skip past that to locate the actual URL. |
30 // | 40 // |
31 // TODO(darin): It breaks the abstraction a bit that we assume 'key' is an | 41 // 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 | 42 // URL corresponding to a registered MockTransaction. It would be good to |
(...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
132 DCHECK(truncate); | 142 DCHECK(truncate); |
133 | 143 |
134 if (fail_requests_) { | 144 if (fail_requests_) { |
135 CallbackLater(callback, ERR_CACHE_READ_FAILURE); | 145 CallbackLater(callback, ERR_CACHE_READ_FAILURE); |
136 return ERR_IO_PENDING; | 146 return ERR_IO_PENDING; |
137 } | 147 } |
138 | 148 |
139 if (offset < 0 || offset > static_cast<int>(data_[index].size())) | 149 if (offset < 0 || offset > static_cast<int>(data_[index].size())) |
140 return ERR_FAILED; | 150 return ERR_FAILED; |
141 | 151 |
152 DCHECK(offset + buf_len < kMaxMockCacheEntrySize); | |
gavinp
2016/06/28 14:24:43
DCHECK_LT?
asanka
2016/06/28 16:55:38
Done here and on L223.
| |
142 data_[index].resize(offset + buf_len); | 153 data_[index].resize(offset + buf_len); |
143 if (buf_len) | 154 if (buf_len) |
144 memcpy(&data_[index][offset], buf->data(), buf_len); | 155 memcpy(&data_[index][offset], buf->data(), buf_len); |
145 | 156 |
146 if (MockHttpCache::GetTestMode(test_mode_) & TEST_MODE_SYNC_CACHE_WRITE) | 157 if (MockHttpCache::GetTestMode(test_mode_) & TEST_MODE_SYNC_CACHE_WRITE) |
147 return buf_len; | 158 return buf_len; |
148 | 159 |
149 CallbackLater(callback, buf_len); | 160 CallbackLater(callback, buf_len); |
150 return ERR_IO_PENDING; | 161 return ERR_IO_PENDING; |
151 } | 162 } |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
201 return ERR_FAILED; | 212 return ERR_FAILED; |
202 if (!buf_len) | 213 if (!buf_len) |
203 return 0; | 214 return 0; |
204 | 215 |
205 if (fail_requests_) | 216 if (fail_requests_) |
206 return ERR_CACHE_READ_FAILURE; | 217 return ERR_CACHE_READ_FAILURE; |
207 | 218 |
208 DCHECK(offset < std::numeric_limits<int32_t>::max()); | 219 DCHECK(offset < std::numeric_limits<int32_t>::max()); |
209 int real_offset = static_cast<int>(offset); | 220 int real_offset = static_cast<int>(offset); |
210 | 221 |
211 if (static_cast<int>(data_[1].size()) < real_offset + buf_len) | 222 if (static_cast<int>(data_[1].size()) < real_offset + buf_len) { |
223 DCHECK(real_offset + buf_len < kMaxMockCacheEntrySize); | |
212 data_[1].resize(real_offset + buf_len); | 224 data_[1].resize(real_offset + buf_len); |
225 } | |
213 | 226 |
214 memcpy(&data_[1][real_offset], buf->data(), buf_len); | 227 memcpy(&data_[1][real_offset], buf->data(), buf_len); |
215 if (MockHttpCache::GetTestMode(test_mode_) & TEST_MODE_SYNC_CACHE_WRITE) | 228 if (MockHttpCache::GetTestMode(test_mode_) & TEST_MODE_SYNC_CACHE_WRITE) |
216 return buf_len; | 229 return buf_len; |
217 | 230 |
218 CallbackLater(callback, buf_len); | 231 CallbackLater(callback, buf_len); |
219 return ERR_IO_PENDING; | 232 return ERR_IO_PENDING; |
220 } | 233 } |
221 | 234 |
222 int MockDiskEntry::GetAvailableRange(int64_t offset, | 235 int MockDiskEntry::GetAvailableRange(int64_t offset, |
(...skipping 449 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
672 if (!callback_.is_null()) { | 685 if (!callback_.is_null()) { |
673 if (!fail_) | 686 if (!fail_) |
674 backend_->reset(new MockDiskCache()); | 687 backend_->reset(new MockDiskCache()); |
675 CompletionCallback cb = callback_; | 688 CompletionCallback cb = callback_; |
676 callback_.Reset(); | 689 callback_.Reset(); |
677 cb.Run(Result()); // This object can be deleted here. | 690 cb.Run(Result()); // This object can be deleted here. |
678 } | 691 } |
679 } | 692 } |
680 | 693 |
681 } // namespace net | 694 } // namespace net |
OLD | NEW |