Make USB permissions work in the new permission message system
[chromium-blink-merge.git] / net / http / mock_http_cache.h
blob632bb3e63c16996290e5ed945c6d7f7909eea5c0
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 "base/strings/string_split.h"
15 #include "net/disk_cache/disk_cache.h"
16 #include "net/http/http_cache.h"
17 #include "net/http/http_transaction_test_util.h"
19 namespace net {
21 //-----------------------------------------------------------------------------
22 // Mock disk cache (a very basic memory cache implementation).
24 class MockDiskEntry : public disk_cache::Entry,
25 public base::RefCounted<MockDiskEntry> {
26 public:
27 explicit MockDiskEntry(const std::string& key);
29 bool is_doomed() const { return doomed_; }
31 void Doom() override;
32 void Close() override;
33 std::string GetKey() const override;
34 base::Time GetLastUsed() const override;
35 base::Time GetLastModified() const override;
36 int32 GetDataSize(int index) const override;
37 int ReadData(int index,
38 int offset,
39 IOBuffer* buf,
40 int buf_len,
41 const CompletionCallback& callback) override;
42 int WriteData(int index,
43 int offset,
44 IOBuffer* buf,
45 int buf_len,
46 const CompletionCallback& callback,
47 bool truncate) override;
48 int ReadSparseData(int64 offset,
49 IOBuffer* buf,
50 int buf_len,
51 const CompletionCallback& callback) override;
52 int WriteSparseData(int64 offset,
53 IOBuffer* buf,
54 int buf_len,
55 const CompletionCallback& callback) override;
56 int GetAvailableRange(int64 offset,
57 int len,
58 int64* start,
59 const CompletionCallback& callback) override;
60 bool CouldBeSparse() const override;
61 void CancelSparseIO() override;
62 int ReadyForSparseIO(const CompletionCallback& completion_callback) override;
64 // Fail most subsequent requests.
65 void set_fail_requests() { fail_requests_ = true; }
67 void set_fail_sparse_requests() { fail_sparse_requests_ = true; }
69 // If |value| is true, don't deliver any completion callbacks until called
70 // again with |value| set to false. Caution: remember to enable callbacks
71 // again or all subsequent tests will fail.
72 static void IgnoreCallbacks(bool value);
74 private:
75 friend class base::RefCounted<MockDiskEntry>;
76 struct CallbackInfo;
78 ~MockDiskEntry() override;
80 // Unlike the callbacks for MockHttpTransaction, we want this one to run even
81 // if the consumer called Close on the MockDiskEntry. We achieve that by
82 // leveraging the fact that this class is reference counted.
83 void CallbackLater(const CompletionCallback& callback, int result);
85 void RunCallback(const CompletionCallback& callback, int result);
87 // When |store| is true, stores the callback to be delivered later; otherwise
88 // delivers any callback previously stored.
89 static void StoreAndDeliverCallbacks(bool store,
90 MockDiskEntry* entry,
91 const CompletionCallback& callback,
92 int result);
94 static const int kNumCacheEntryDataIndices = 3;
96 std::string key_;
97 std::vector<char> data_[kNumCacheEntryDataIndices];
98 int test_mode_;
99 bool doomed_;
100 bool sparse_;
101 bool fail_requests_;
102 bool fail_sparse_requests_;
103 bool busy_;
104 bool delayed_;
105 bool cancel_;
106 static bool ignore_callbacks_;
109 class MockDiskCache : public disk_cache::Backend {
110 public:
111 MockDiskCache();
112 ~MockDiskCache() override;
114 CacheType GetCacheType() const override;
115 int32 GetEntryCount() const override;
116 int OpenEntry(const std::string& key,
117 disk_cache::Entry** entry,
118 const CompletionCallback& callback) override;
119 int CreateEntry(const std::string& key,
120 disk_cache::Entry** entry,
121 const CompletionCallback& callback) override;
122 int DoomEntry(const std::string& key,
123 const CompletionCallback& callback) override;
124 int DoomAllEntries(const CompletionCallback& callback) override;
125 int DoomEntriesBetween(base::Time initial_time,
126 base::Time end_time,
127 const CompletionCallback& callback) override;
128 int DoomEntriesSince(base::Time initial_time,
129 const CompletionCallback& callback) override;
130 scoped_ptr<Iterator> CreateIterator() override;
131 void GetStats(base::StringPairs* stats) override;
132 void OnExternalCacheHit(const std::string& key) override;
134 // Returns number of times a cache entry was successfully opened.
135 int open_count() const { return open_count_; }
137 // Returns number of times a cache entry was successfully created.
138 int create_count() const { return create_count_; }
140 // Fail any subsequent CreateEntry and OpenEntry.
141 void set_fail_requests() { fail_requests_ = true; }
143 // Return entries that fail some of their requests.
144 void set_soft_failures(bool value) { soft_failures_ = value; }
146 // Makes sure that CreateEntry is not called twice for a given key.
147 void set_double_create_check(bool value) { double_create_check_ = value; }
149 // Makes all requests for data ranges to fail as not implemented.
150 void set_fail_sparse_requests() { fail_sparse_requests_ = true; }
152 void ReleaseAll();
154 private:
155 typedef base::hash_map<std::string, MockDiskEntry*> EntryMap;
156 class NotImplementedIterator;
158 void CallbackLater(const CompletionCallback& callback, int result);
160 EntryMap entries_;
161 int open_count_;
162 int create_count_;
163 bool fail_requests_;
164 bool soft_failures_;
165 bool double_create_check_;
166 bool fail_sparse_requests_;
169 class MockBackendFactory : public HttpCache::BackendFactory {
170 public:
171 int CreateBackend(NetLog* net_log,
172 scoped_ptr<disk_cache::Backend>* backend,
173 const CompletionCallback& callback) override;
176 class MockHttpCache {
177 public:
178 MockHttpCache();
179 explicit MockHttpCache(HttpCache::BackendFactory* disk_cache_factory);
181 HttpCache* http_cache() { return &http_cache_; }
183 MockNetworkLayer* network_layer() {
184 return static_cast<MockNetworkLayer*>(http_cache_.network_layer());
186 disk_cache::Backend* backend();
187 MockDiskCache* disk_cache();
189 // Wrapper around http_cache()->CreateTransaction(DEFAULT_PRIORITY...)
190 int CreateTransaction(scoped_ptr<HttpTransaction>* trans);
192 // Wrapper to bypass the cache lock for new transactions.
193 void BypassCacheLock();
195 // Wrapper to fail request conditionalization for new transactions.
196 void FailConditionalizations();
198 // Helper function for reading response info from the disk cache.
199 static bool ReadResponseInfo(disk_cache::Entry* disk_entry,
200 HttpResponseInfo* response_info,
201 bool* response_truncated);
203 // Helper function for writing response info into the disk cache.
204 static bool WriteResponseInfo(disk_cache::Entry* disk_entry,
205 const HttpResponseInfo* response_info,
206 bool skip_transient_headers,
207 bool response_truncated);
209 // Helper function to synchronously open a backend entry.
210 bool OpenBackendEntry(const std::string& key, disk_cache::Entry** entry);
212 // Helper function to synchronously create a backend entry.
213 bool CreateBackendEntry(const std::string& key,
214 disk_cache::Entry** entry,
215 NetLog* net_log);
217 // Returns the test mode after considering the global override.
218 static int GetTestMode(int test_mode);
220 // Overrides the test mode for a given operation. Remember to reset it after
221 // the test! (by setting test_mode to zero).
222 static void SetTestMode(int test_mode);
224 private:
225 HttpCache http_cache_;
228 // This version of the disk cache doesn't invoke CreateEntry callbacks.
229 class MockDiskCacheNoCB : public MockDiskCache {
230 int CreateEntry(const std::string& key,
231 disk_cache::Entry** entry,
232 const CompletionCallback& callback) override;
235 class MockBackendNoCbFactory : public HttpCache::BackendFactory {
236 public:
237 int CreateBackend(NetLog* net_log,
238 scoped_ptr<disk_cache::Backend>* backend,
239 const CompletionCallback& callback) override;
242 // This backend factory allows us to control the backend instantiation.
243 class MockBlockingBackendFactory : public HttpCache::BackendFactory {
244 public:
245 MockBlockingBackendFactory();
246 ~MockBlockingBackendFactory() override;
248 int CreateBackend(NetLog* net_log,
249 scoped_ptr<disk_cache::Backend>* backend,
250 const CompletionCallback& callback) override;
252 // Completes the backend creation. Any blocked call will be notified via the
253 // provided callback.
254 void FinishCreation();
256 scoped_ptr<disk_cache::Backend>* backend() { return backend_; }
257 void set_fail(bool fail) { fail_ = fail; }
259 const CompletionCallback& callback() { return callback_; }
261 private:
262 int Result() { return fail_ ? ERR_FAILED : OK; }
264 scoped_ptr<disk_cache::Backend>* backend_;
265 CompletionCallback callback_;
266 bool block_;
267 bool fail_;
270 } // namespace net
272 #endif // NET_HTTP_MOCK_HTTP_CACHE_H_