OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 // This is a mock of the http cache and related testing classes. To be fair, it | 5 // This is a mock of the http cache and related testing classes. To be fair, it |
6 // is not really a mock http cache given that it uses the real implementation of | 6 // is not really a mock http cache given that it uses the real implementation of |
7 // the http cache, but it has fake implementations of all required components, | 7 // the http cache, but it has fake implementations of all required components, |
8 // so it is useful for unit tests at the http layer. | 8 // so it is useful for unit tests at the http layer. |
9 | 9 |
10 #ifndef NET_HTTP_MOCK_HTTP_CACHE_H_ | 10 #ifndef NET_HTTP_MOCK_HTTP_CACHE_H_ |
11 #define NET_HTTP_MOCK_HTTP_CACHE_H_ | 11 #define NET_HTTP_MOCK_HTTP_CACHE_H_ |
12 | 12 |
13 #include "base/containers/hash_tables.h" | 13 #include "base/containers/hash_tables.h" |
14 #include "net/disk_cache/disk_cache.h" | 14 #include "net/disk_cache/disk_cache.h" |
15 #include "net/http/http_cache.h" | 15 #include "net/http/http_cache.h" |
16 #include "net/http/http_transaction_test_util.h" | 16 #include "net/http/http_transaction_test_util.h" |
17 | 17 |
18 //----------------------------------------------------------------------------- | 18 //----------------------------------------------------------------------------- |
19 // Mock disk cache (a very basic memory cache implementation). | 19 // Mock disk cache (a very basic memory cache implementation). |
20 | 20 |
21 class MockDiskEntry : public disk_cache::Entry, | 21 class MockDiskEntry : public disk_cache::Entry, |
22 public base::RefCounted<MockDiskEntry> { | 22 public base::RefCounted<MockDiskEntry> { |
23 public: | 23 public: |
24 explicit MockDiskEntry(const std::string& key); | 24 explicit MockDiskEntry(const std::string& key); |
25 | 25 |
26 bool is_doomed() const { return doomed_; } | 26 bool is_doomed() const { return doomed_; } |
27 | 27 |
28 virtual void Doom() OVERRIDE; | 28 virtual void Doom() override; |
29 virtual void Close() OVERRIDE; | 29 virtual void Close() override; |
30 virtual std::string GetKey() const OVERRIDE; | 30 virtual std::string GetKey() const override; |
31 virtual base::Time GetLastUsed() const OVERRIDE; | 31 virtual base::Time GetLastUsed() const override; |
32 virtual base::Time GetLastModified() const OVERRIDE; | 32 virtual base::Time GetLastModified() const override; |
33 virtual int32 GetDataSize(int index) const OVERRIDE; | 33 virtual int32 GetDataSize(int index) const override; |
34 virtual int ReadData(int index, int offset, net::IOBuffer* buf, int buf_len, | 34 virtual int ReadData(int index, int offset, net::IOBuffer* buf, int buf_len, |
35 const net::CompletionCallback& callback) OVERRIDE; | 35 const net::CompletionCallback& callback) override; |
36 virtual int WriteData(int index, int offset, net::IOBuffer* buf, int buf_len, | 36 virtual int WriteData(int index, int offset, net::IOBuffer* buf, int buf_len, |
37 const net::CompletionCallback& callback, | 37 const net::CompletionCallback& callback, |
38 bool truncate) OVERRIDE; | 38 bool truncate) override; |
39 virtual int ReadSparseData(int64 offset, net::IOBuffer* buf, int buf_len, | 39 virtual int ReadSparseData(int64 offset, net::IOBuffer* buf, int buf_len, |
40 const net::CompletionCallback& callback) OVERRIDE; | 40 const net::CompletionCallback& callback) override; |
41 virtual int WriteSparseData( | 41 virtual int WriteSparseData( |
42 int64 offset, net::IOBuffer* buf, int buf_len, | 42 int64 offset, net::IOBuffer* buf, int buf_len, |
43 const net::CompletionCallback& callback) OVERRIDE; | 43 const net::CompletionCallback& callback) override; |
44 virtual int GetAvailableRange( | 44 virtual int GetAvailableRange( |
45 int64 offset, int len, int64* start, | 45 int64 offset, int len, int64* start, |
46 const net::CompletionCallback& callback) OVERRIDE; | 46 const net::CompletionCallback& callback) override; |
47 virtual bool CouldBeSparse() const OVERRIDE; | 47 virtual bool CouldBeSparse() const override; |
48 virtual void CancelSparseIO() OVERRIDE; | 48 virtual void CancelSparseIO() override; |
49 virtual int ReadyForSparseIO( | 49 virtual int ReadyForSparseIO( |
50 const net::CompletionCallback& completion_callback) OVERRIDE; | 50 const net::CompletionCallback& completion_callback) override; |
51 | 51 |
52 // Fail most subsequent requests. | 52 // Fail most subsequent requests. |
53 void set_fail_requests() { fail_requests_ = true; } | 53 void set_fail_requests() { fail_requests_ = true; } |
54 | 54 |
55 void set_fail_sparse_requests() { fail_sparse_requests_ = true; } | 55 void set_fail_sparse_requests() { fail_sparse_requests_ = true; } |
56 | 56 |
57 // If |value| is true, don't deliver any completion callbacks until called | 57 // If |value| is true, don't deliver any completion callbacks until called |
58 // again with |value| set to false. Caution: remember to enable callbacks | 58 // again with |value| set to false. Caution: remember to enable callbacks |
59 // again or all subsequent tests will fail. | 59 // again or all subsequent tests will fail. |
60 static void IgnoreCallbacks(bool value); | 60 static void IgnoreCallbacks(bool value); |
(...skipping 30 matching lines...) Expand all Loading... |
91 bool delayed_; | 91 bool delayed_; |
92 static bool cancel_; | 92 static bool cancel_; |
93 static bool ignore_callbacks_; | 93 static bool ignore_callbacks_; |
94 }; | 94 }; |
95 | 95 |
96 class MockDiskCache : public disk_cache::Backend { | 96 class MockDiskCache : public disk_cache::Backend { |
97 public: | 97 public: |
98 MockDiskCache(); | 98 MockDiskCache(); |
99 virtual ~MockDiskCache(); | 99 virtual ~MockDiskCache(); |
100 | 100 |
101 virtual net::CacheType GetCacheType() const OVERRIDE; | 101 virtual net::CacheType GetCacheType() const override; |
102 virtual int32 GetEntryCount() const OVERRIDE; | 102 virtual int32 GetEntryCount() const override; |
103 virtual int OpenEntry(const std::string& key, disk_cache::Entry** entry, | 103 virtual int OpenEntry(const std::string& key, disk_cache::Entry** entry, |
104 const net::CompletionCallback& callback) OVERRIDE; | 104 const net::CompletionCallback& callback) override; |
105 virtual int CreateEntry(const std::string& key, disk_cache::Entry** entry, | 105 virtual int CreateEntry(const std::string& key, disk_cache::Entry** entry, |
106 const net::CompletionCallback& callback) OVERRIDE; | 106 const net::CompletionCallback& callback) override; |
107 virtual int DoomEntry(const std::string& key, | 107 virtual int DoomEntry(const std::string& key, |
108 const net::CompletionCallback& callback) OVERRIDE; | 108 const net::CompletionCallback& callback) override; |
109 virtual int DoomAllEntries(const net::CompletionCallback& callback) OVERRIDE; | 109 virtual int DoomAllEntries(const net::CompletionCallback& callback) override; |
110 virtual int DoomEntriesBetween( | 110 virtual int DoomEntriesBetween( |
111 base::Time initial_time, | 111 base::Time initial_time, |
112 base::Time end_time, | 112 base::Time end_time, |
113 const net::CompletionCallback& callback) OVERRIDE; | 113 const net::CompletionCallback& callback) override; |
114 virtual int DoomEntriesSince( | 114 virtual int DoomEntriesSince( |
115 base::Time initial_time, | 115 base::Time initial_time, |
116 const net::CompletionCallback& callback) OVERRIDE; | 116 const net::CompletionCallback& callback) override; |
117 virtual scoped_ptr<Iterator> CreateIterator() OVERRIDE; | 117 virtual scoped_ptr<Iterator> CreateIterator() override; |
118 virtual void GetStats( | 118 virtual void GetStats( |
119 std::vector<std::pair<std::string, std::string> >* stats) OVERRIDE; | 119 std::vector<std::pair<std::string, std::string> >* stats) override; |
120 virtual void OnExternalCacheHit(const std::string& key) OVERRIDE; | 120 virtual void OnExternalCacheHit(const std::string& key) override; |
121 | 121 |
122 // Returns number of times a cache entry was successfully opened. | 122 // Returns number of times a cache entry was successfully opened. |
123 int open_count() const { return open_count_; } | 123 int open_count() const { return open_count_; } |
124 | 124 |
125 // Returns number of times a cache entry was successfully created. | 125 // Returns number of times a cache entry was successfully created. |
126 int create_count() const { return create_count_; } | 126 int create_count() const { return create_count_; } |
127 | 127 |
128 // Fail any subsequent CreateEntry and OpenEntry. | 128 // Fail any subsequent CreateEntry and OpenEntry. |
129 void set_fail_requests() { fail_requests_ = true; } | 129 void set_fail_requests() { fail_requests_ = true; } |
130 | 130 |
(...skipping 20 matching lines...) Expand all Loading... |
151 bool fail_requests_; | 151 bool fail_requests_; |
152 bool soft_failures_; | 152 bool soft_failures_; |
153 bool double_create_check_; | 153 bool double_create_check_; |
154 bool fail_sparse_requests_; | 154 bool fail_sparse_requests_; |
155 }; | 155 }; |
156 | 156 |
157 class MockBackendFactory : public net::HttpCache::BackendFactory { | 157 class MockBackendFactory : public net::HttpCache::BackendFactory { |
158 public: | 158 public: |
159 virtual int CreateBackend(net::NetLog* net_log, | 159 virtual int CreateBackend(net::NetLog* net_log, |
160 scoped_ptr<disk_cache::Backend>* backend, | 160 scoped_ptr<disk_cache::Backend>* backend, |
161 const net::CompletionCallback& callback) OVERRIDE; | 161 const net::CompletionCallback& callback) override; |
162 }; | 162 }; |
163 | 163 |
164 class MockHttpCache { | 164 class MockHttpCache { |
165 public: | 165 public: |
166 MockHttpCache(); | 166 MockHttpCache(); |
167 explicit MockHttpCache(net::HttpCache::BackendFactory* disk_cache_factory); | 167 explicit MockHttpCache(net::HttpCache::BackendFactory* disk_cache_factory); |
168 | 168 |
169 net::HttpCache* http_cache() { return &http_cache_; } | 169 net::HttpCache* http_cache() { return &http_cache_; } |
170 | 170 |
171 MockNetworkLayer* network_layer() { | 171 MockNetworkLayer* network_layer() { |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
204 // the test! (by setting test_mode to zero). | 204 // the test! (by setting test_mode to zero). |
205 static void SetTestMode(int test_mode); | 205 static void SetTestMode(int test_mode); |
206 | 206 |
207 private: | 207 private: |
208 net::HttpCache http_cache_; | 208 net::HttpCache http_cache_; |
209 }; | 209 }; |
210 | 210 |
211 // This version of the disk cache doesn't invoke CreateEntry callbacks. | 211 // This version of the disk cache doesn't invoke CreateEntry callbacks. |
212 class MockDiskCacheNoCB : public MockDiskCache { | 212 class MockDiskCacheNoCB : public MockDiskCache { |
213 virtual int CreateEntry(const std::string& key, disk_cache::Entry** entry, | 213 virtual int CreateEntry(const std::string& key, disk_cache::Entry** entry, |
214 const net::CompletionCallback& callback) OVERRIDE; | 214 const net::CompletionCallback& callback) override; |
215 }; | 215 }; |
216 | 216 |
217 class MockBackendNoCbFactory : public net::HttpCache::BackendFactory { | 217 class MockBackendNoCbFactory : public net::HttpCache::BackendFactory { |
218 public: | 218 public: |
219 virtual int CreateBackend(net::NetLog* net_log, | 219 virtual int CreateBackend(net::NetLog* net_log, |
220 scoped_ptr<disk_cache::Backend>* backend, | 220 scoped_ptr<disk_cache::Backend>* backend, |
221 const net::CompletionCallback& callback) OVERRIDE; | 221 const net::CompletionCallback& callback) override; |
222 }; | 222 }; |
223 | 223 |
224 // This backend factory allows us to control the backend instantiation. | 224 // This backend factory allows us to control the backend instantiation. |
225 class MockBlockingBackendFactory : public net::HttpCache::BackendFactory { | 225 class MockBlockingBackendFactory : public net::HttpCache::BackendFactory { |
226 public: | 226 public: |
227 MockBlockingBackendFactory(); | 227 MockBlockingBackendFactory(); |
228 virtual ~MockBlockingBackendFactory(); | 228 virtual ~MockBlockingBackendFactory(); |
229 | 229 |
230 virtual int CreateBackend(net::NetLog* net_log, | 230 virtual int CreateBackend(net::NetLog* net_log, |
231 scoped_ptr<disk_cache::Backend>* backend, | 231 scoped_ptr<disk_cache::Backend>* backend, |
232 const net::CompletionCallback& callback) OVERRIDE; | 232 const net::CompletionCallback& callback) override; |
233 | 233 |
234 // Completes the backend creation. Any blocked call will be notified via the | 234 // Completes the backend creation. Any blocked call will be notified via the |
235 // provided callback. | 235 // provided callback. |
236 void FinishCreation(); | 236 void FinishCreation(); |
237 | 237 |
238 scoped_ptr<disk_cache::Backend>* backend() { return backend_; } | 238 scoped_ptr<disk_cache::Backend>* backend() { return backend_; } |
239 void set_fail(bool fail) { fail_ = fail; } | 239 void set_fail(bool fail) { fail_ = fail; } |
240 | 240 |
241 const net::CompletionCallback& callback() { return callback_; } | 241 const net::CompletionCallback& callback() { return callback_; } |
242 | 242 |
243 private: | 243 private: |
244 int Result() { return fail_ ? net::ERR_FAILED : net::OK; } | 244 int Result() { return fail_ ? net::ERR_FAILED : net::OK; } |
245 | 245 |
246 scoped_ptr<disk_cache::Backend>* backend_; | 246 scoped_ptr<disk_cache::Backend>* backend_; |
247 net::CompletionCallback callback_; | 247 net::CompletionCallback callback_; |
248 bool block_; | 248 bool block_; |
249 bool fail_; | 249 bool fail_; |
250 }; | 250 }; |
251 | 251 |
252 #endif // NET_HTTP_MOCK_HTTP_CACHE_H_ | 252 #endif // NET_HTTP_MOCK_HTTP_CACHE_H_ |
OLD | NEW |