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
3 // found in the LICENSE file.
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
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.
10 #ifndef NET_HTTP_MOCK_HTTP_CACHE_H_
11 #define NET_HTTP_MOCK_HTTP_CACHE_H_
13 #include "base/containers/hash_tables.h"
14 #include "net/disk_cache/disk_cache.h"
15 #include "net/http/http_cache.h"
16 #include "net/http/http_transaction_test_util.h"
18 //-----------------------------------------------------------------------------
19 // Mock disk cache (a very basic memory cache implementation).
21 class MockDiskEntry
: public disk_cache::Entry
,
22 public base::RefCounted
<MockDiskEntry
> {
24 explicit MockDiskEntry(const std::string
& key
);
26 bool is_doomed() const { return doomed_
; }
29 void Close() override
;
30 std::string
GetKey() const override
;
31 base::Time
GetLastUsed() const override
;
32 base::Time
GetLastModified() const override
;
33 int32
GetDataSize(int index
) const override
;
34 int ReadData(int index
,
38 const net::CompletionCallback
& callback
) override
;
39 int WriteData(int index
,
43 const net::CompletionCallback
& callback
,
44 bool truncate
) override
;
45 int ReadSparseData(int64 offset
,
48 const net::CompletionCallback
& callback
) override
;
49 int WriteSparseData(int64 offset
,
52 const net::CompletionCallback
& callback
) override
;
53 int GetAvailableRange(int64 offset
,
56 const net::CompletionCallback
& callback
) override
;
57 bool CouldBeSparse() const override
;
58 void CancelSparseIO() override
;
60 const net::CompletionCallback
& completion_callback
) override
;
62 // Fail most subsequent requests.
63 void set_fail_requests() { fail_requests_
= true; }
65 void set_fail_sparse_requests() { fail_sparse_requests_
= true; }
67 // If |value| is true, don't deliver any completion callbacks until called
68 // again with |value| set to false. Caution: remember to enable callbacks
69 // again or all subsequent tests will fail.
70 static void IgnoreCallbacks(bool value
);
73 friend class base::RefCounted
<MockDiskEntry
>;
76 ~MockDiskEntry() override
;
78 // Unlike the callbacks for MockHttpTransaction, we want this one to run even
79 // if the consumer called Close on the MockDiskEntry. We achieve that by
80 // leveraging the fact that this class is reference counted.
81 void CallbackLater(const net::CompletionCallback
& callback
, int result
);
83 void RunCallback(const net::CompletionCallback
& callback
, int result
);
85 // When |store| is true, stores the callback to be delivered later; otherwise
86 // delivers any callback previously stored.
87 static void StoreAndDeliverCallbacks(bool store
, MockDiskEntry
* entry
,
88 const net::CompletionCallback
& callback
,
91 static const int kNumCacheEntryDataIndices
= 3;
94 std::vector
<char> data_
[kNumCacheEntryDataIndices
];
99 bool fail_sparse_requests_
;
103 static bool ignore_callbacks_
;
106 class MockDiskCache
: public disk_cache::Backend
{
109 ~MockDiskCache() override
;
111 net::CacheType
GetCacheType() const override
;
112 int32
GetEntryCount() const override
;
113 int OpenEntry(const std::string
& key
,
114 disk_cache::Entry
** entry
,
115 const net::CompletionCallback
& callback
) override
;
116 int CreateEntry(const std::string
& key
,
117 disk_cache::Entry
** entry
,
118 const net::CompletionCallback
& callback
) override
;
119 int DoomEntry(const std::string
& key
,
120 const net::CompletionCallback
& callback
) override
;
121 int DoomAllEntries(const net::CompletionCallback
& callback
) override
;
122 int DoomEntriesBetween(base::Time initial_time
,
124 const net::CompletionCallback
& callback
) override
;
125 int DoomEntriesSince(base::Time initial_time
,
126 const net::CompletionCallback
& callback
) override
;
127 scoped_ptr
<Iterator
> CreateIterator() override
;
129 std::vector
<std::pair
<std::string
, std::string
>>* stats
) override
;
130 void OnExternalCacheHit(const std::string
& key
) override
;
132 // Returns number of times a cache entry was successfully opened.
133 int open_count() const { return open_count_
; }
135 // Returns number of times a cache entry was successfully created.
136 int create_count() const { return create_count_
; }
138 // Fail any subsequent CreateEntry and OpenEntry.
139 void set_fail_requests() { fail_requests_
= true; }
141 // Return entries that fail some of their requests.
142 void set_soft_failures(bool value
) { soft_failures_
= value
; }
144 // Makes sure that CreateEntry is not called twice for a given key.
145 void set_double_create_check(bool value
) { double_create_check_
= value
; }
147 // Makes all requests for data ranges to fail as not implemented.
148 void set_fail_sparse_requests() { fail_sparse_requests_
= true; }
153 typedef base::hash_map
<std::string
, MockDiskEntry
*> EntryMap
;
154 class NotImplementedIterator
;
156 void CallbackLater(const net::CompletionCallback
& callback
, int result
);
163 bool double_create_check_
;
164 bool fail_sparse_requests_
;
167 class MockBackendFactory
: public net::HttpCache::BackendFactory
{
169 int CreateBackend(net::NetLog
* net_log
,
170 scoped_ptr
<disk_cache::Backend
>* backend
,
171 const net::CompletionCallback
& callback
) override
;
174 class MockHttpCache
{
177 explicit MockHttpCache(net::HttpCache::BackendFactory
* disk_cache_factory
);
179 net::HttpCache
* http_cache() { return &http_cache_
; }
181 MockNetworkLayer
* network_layer() {
182 return static_cast<MockNetworkLayer
*>(http_cache_
.network_layer());
184 disk_cache::Backend
* backend();
185 MockDiskCache
* disk_cache();
187 // Wrapper around http_cache()->CreateTransaction(net::DEFAULT_PRIORITY...)
188 int CreateTransaction(scoped_ptr
<net::HttpTransaction
>* trans
);
190 // Wrapper to bypass the cache lock for new transactions.
191 void BypassCacheLock();
193 // Wrapper to fail request conditionalization for new transactions.
194 void FailConditionalizations();
196 // Helper function for reading response info from the disk cache.
197 static bool ReadResponseInfo(disk_cache::Entry
* disk_entry
,
198 net::HttpResponseInfo
* response_info
,
199 bool* response_truncated
);
201 // Helper function for writing response info into the disk cache.
202 static bool WriteResponseInfo(disk_cache::Entry
* disk_entry
,
203 const net::HttpResponseInfo
* response_info
,
204 bool skip_transient_headers
,
205 bool response_truncated
);
207 // Helper function to synchronously open a backend entry.
208 bool OpenBackendEntry(const std::string
& key
, disk_cache::Entry
** entry
);
210 // Helper function to synchronously create a backend entry.
211 bool CreateBackendEntry(const std::string
& key
, disk_cache::Entry
** entry
,
212 net::NetLog
* net_log
);
214 // Returns the test mode after considering the global override.
215 static int GetTestMode(int test_mode
);
217 // Overrides the test mode for a given operation. Remember to reset it after
218 // the test! (by setting test_mode to zero).
219 static void SetTestMode(int test_mode
);
222 net::HttpCache http_cache_
;
225 // This version of the disk cache doesn't invoke CreateEntry callbacks.
226 class MockDiskCacheNoCB
: public MockDiskCache
{
227 int CreateEntry(const std::string
& key
,
228 disk_cache::Entry
** entry
,
229 const net::CompletionCallback
& callback
) override
;
232 class MockBackendNoCbFactory
: public net::HttpCache::BackendFactory
{
234 int CreateBackend(net::NetLog
* net_log
,
235 scoped_ptr
<disk_cache::Backend
>* backend
,
236 const net::CompletionCallback
& callback
) override
;
239 // This backend factory allows us to control the backend instantiation.
240 class MockBlockingBackendFactory
: public net::HttpCache::BackendFactory
{
242 MockBlockingBackendFactory();
243 ~MockBlockingBackendFactory() override
;
245 int CreateBackend(net::NetLog
* net_log
,
246 scoped_ptr
<disk_cache::Backend
>* backend
,
247 const net::CompletionCallback
& callback
) override
;
249 // Completes the backend creation. Any blocked call will be notified via the
250 // provided callback.
251 void FinishCreation();
253 scoped_ptr
<disk_cache::Backend
>* backend() { return backend_
; }
254 void set_fail(bool fail
) { fail_
= fail
; }
256 const net::CompletionCallback
& callback() { return callback_
; }
259 int Result() { return fail_
? net::ERR_FAILED
: net::OK
; }
261 scoped_ptr
<disk_cache::Backend
>* backend_
;
262 net::CompletionCallback callback_
;
267 #endif // NET_HTTP_MOCK_HTTP_CACHE_H_