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 // 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. |
| 29 const int kMaxMockCacheEntrySize = 100 * 1000 * 1000; |
| 30 |
23 // We can override the test mode for a given operation by setting this global | 31 // We can override the test mode for a given operation by setting this global |
24 // variable. | 32 // variable. |
25 int g_test_mode = 0; | 33 int g_test_mode = 0; |
26 | 34 |
27 int GetTestModeForEntry(const std::string& key) { | 35 int GetTestModeForEntry(const std::string& key) { |
28 // 'key' is prefixed with an identifier if it corresponds to a cached POST. | 36 // 'key' is prefixed with an identifier if it corresponds to a cached POST. |
29 // Skip past that to locate the actual URL. | 37 // Skip past that to locate the actual URL. |
30 // | 38 // |
31 // TODO(darin): It breaks the abstraction a bit that we assume 'key' is an | 39 // 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 | 40 // 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); | 140 DCHECK(truncate); |
133 | 141 |
134 if (fail_requests_) { | 142 if (fail_requests_) { |
135 CallbackLater(callback, ERR_CACHE_READ_FAILURE); | 143 CallbackLater(callback, ERR_CACHE_READ_FAILURE); |
136 return ERR_IO_PENDING; | 144 return ERR_IO_PENDING; |
137 } | 145 } |
138 | 146 |
139 if (offset < 0 || offset > static_cast<int>(data_[index].size())) | 147 if (offset < 0 || offset > static_cast<int>(data_[index].size())) |
140 return ERR_FAILED; | 148 return ERR_FAILED; |
141 | 149 |
| 150 DCHECK_LT(offset + buf_len, kMaxMockCacheEntrySize); |
142 data_[index].resize(offset + buf_len); | 151 data_[index].resize(offset + buf_len); |
143 if (buf_len) | 152 if (buf_len) |
144 memcpy(&data_[index][offset], buf->data(), buf_len); | 153 memcpy(&data_[index][offset], buf->data(), buf_len); |
145 | 154 |
146 if (MockHttpCache::GetTestMode(test_mode_) & TEST_MODE_SYNC_CACHE_WRITE) | 155 if (MockHttpCache::GetTestMode(test_mode_) & TEST_MODE_SYNC_CACHE_WRITE) |
147 return buf_len; | 156 return buf_len; |
148 | 157 |
149 CallbackLater(callback, buf_len); | 158 CallbackLater(callback, buf_len); |
150 return ERR_IO_PENDING; | 159 return ERR_IO_PENDING; |
151 } | 160 } |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
201 return ERR_FAILED; | 210 return ERR_FAILED; |
202 if (!buf_len) | 211 if (!buf_len) |
203 return 0; | 212 return 0; |
204 | 213 |
205 if (fail_requests_) | 214 if (fail_requests_) |
206 return ERR_CACHE_READ_FAILURE; | 215 return ERR_CACHE_READ_FAILURE; |
207 | 216 |
208 DCHECK(offset < std::numeric_limits<int32_t>::max()); | 217 DCHECK(offset < std::numeric_limits<int32_t>::max()); |
209 int real_offset = static_cast<int>(offset); | 218 int real_offset = static_cast<int>(offset); |
210 | 219 |
211 if (static_cast<int>(data_[1].size()) < real_offset + buf_len) | 220 if (static_cast<int>(data_[1].size()) < real_offset + buf_len) { |
| 221 DCHECK_LT(real_offset + buf_len, kMaxMockCacheEntrySize); |
212 data_[1].resize(real_offset + buf_len); | 222 data_[1].resize(real_offset + buf_len); |
| 223 } |
213 | 224 |
214 memcpy(&data_[1][real_offset], buf->data(), buf_len); | 225 memcpy(&data_[1][real_offset], buf->data(), buf_len); |
215 if (MockHttpCache::GetTestMode(test_mode_) & TEST_MODE_SYNC_CACHE_WRITE) | 226 if (MockHttpCache::GetTestMode(test_mode_) & TEST_MODE_SYNC_CACHE_WRITE) |
216 return buf_len; | 227 return buf_len; |
217 | 228 |
218 CallbackLater(callback, buf_len); | 229 CallbackLater(callback, buf_len); |
219 return ERR_IO_PENDING; | 230 return ERR_IO_PENDING; |
220 } | 231 } |
221 | 232 |
222 int MockDiskEntry::GetAvailableRange(int64_t offset, | 233 int MockDiskEntry::GetAvailableRange(int64_t offset, |
(...skipping 449 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
672 if (!callback_.is_null()) { | 683 if (!callback_.is_null()) { |
673 if (!fail_) | 684 if (!fail_) |
674 backend_->reset(new MockDiskCache()); | 685 backend_->reset(new MockDiskCache()); |
675 CompletionCallback cb = callback_; | 686 CompletionCallback cb = callback_; |
676 callback_.Reset(); | 687 callback_.Reset(); |
677 cb.Run(Result()); // This object can be deleted here. | 688 cb.Run(Result()); // This object can be deleted here. |
678 } | 689 } |
679 } | 690 } |
680 | 691 |
681 } // namespace net | 692 } // namespace net |
OLD | NEW |