Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / net / http / mock_http_cache.h
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.
4
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.
9
10 #ifndef NET_HTTP_MOCK_HTTP_CACHE_H_
11 #define NET_HTTP_MOCK_HTTP_CACHE_H_
12
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"
17
18 //-----------------------------------------------------------------------------
19 // Mock disk cache (a very basic memory cache implementation).
20
21 class MockDiskEntry : public disk_cache::Entry,
22                       public base::RefCounted<MockDiskEntry> {
23  public:
24   explicit MockDiskEntry(const std::string& key);
25
26   bool is_doomed() const { return doomed_; }
27
28   void Doom() override;
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,
35                int offset,
36                net::IOBuffer* buf,
37                int buf_len,
38                const net::CompletionCallback& callback) override;
39   int WriteData(int index,
40                 int offset,
41                 net::IOBuffer* buf,
42                 int buf_len,
43                 const net::CompletionCallback& callback,
44                 bool truncate) override;
45   int ReadSparseData(int64 offset,
46                      net::IOBuffer* buf,
47                      int buf_len,
48                      const net::CompletionCallback& callback) override;
49   int WriteSparseData(int64 offset,
50                       net::IOBuffer* buf,
51                       int buf_len,
52                       const net::CompletionCallback& callback) override;
53   int GetAvailableRange(int64 offset,
54                         int len,
55                         int64* start,
56                         const net::CompletionCallback& callback) override;
57   bool CouldBeSparse() const override;
58   void CancelSparseIO() override;
59   int ReadyForSparseIO(
60       const net::CompletionCallback& completion_callback) override;
61
62   // Fail most subsequent requests.
63   void set_fail_requests() { fail_requests_ = true; }
64
65   void set_fail_sparse_requests() { fail_sparse_requests_ = true; }
66
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);
71
72  private:
73   friend class base::RefCounted<MockDiskEntry>;
74   struct CallbackInfo;
75
76   ~MockDiskEntry() override;
77
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);
82
83   void RunCallback(const net::CompletionCallback& callback, int result);
84
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,
89                                        int result);
90
91   static const int kNumCacheEntryDataIndices = 3;
92
93   std::string key_;
94   std::vector<char> data_[kNumCacheEntryDataIndices];
95   int test_mode_;
96   bool doomed_;
97   bool sparse_;
98   bool fail_requests_;
99   bool fail_sparse_requests_;
100   bool busy_;
101   bool delayed_;
102   static bool cancel_;
103   static bool ignore_callbacks_;
104 };
105
106 class MockDiskCache : public disk_cache::Backend {
107  public:
108   MockDiskCache();
109   ~MockDiskCache() override;
110
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,
123                          base::Time end_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;
128   void GetStats(
129       std::vector<std::pair<std::string, std::string>>* stats) override;
130   void OnExternalCacheHit(const std::string& key) override;
131
132   // Returns number of times a cache entry was successfully opened.
133   int open_count() const { return open_count_; }
134
135   // Returns number of times a cache entry was successfully created.
136   int create_count() const { return create_count_; }
137
138   // Fail any subsequent CreateEntry and OpenEntry.
139   void set_fail_requests() { fail_requests_ = true; }
140
141   // Return entries that fail some of their requests.
142   void set_soft_failures(bool value) { soft_failures_ = value; }
143
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; }
146
147   // Makes all requests for data ranges to fail as not implemented.
148   void set_fail_sparse_requests() { fail_sparse_requests_ = true; }
149
150   void ReleaseAll();
151
152  private:
153   typedef base::hash_map<std::string, MockDiskEntry*> EntryMap;
154   class NotImplementedIterator;
155
156   void CallbackLater(const net::CompletionCallback& callback, int result);
157
158   EntryMap entries_;
159   int open_count_;
160   int create_count_;
161   bool fail_requests_;
162   bool soft_failures_;
163   bool double_create_check_;
164   bool fail_sparse_requests_;
165 };
166
167 class MockBackendFactory : public net::HttpCache::BackendFactory {
168  public:
169   int CreateBackend(net::NetLog* net_log,
170                     scoped_ptr<disk_cache::Backend>* backend,
171                     const net::CompletionCallback& callback) override;
172 };
173
174 class MockHttpCache {
175  public:
176   MockHttpCache();
177   explicit MockHttpCache(net::HttpCache::BackendFactory* disk_cache_factory);
178
179   net::HttpCache* http_cache() { return &http_cache_; }
180
181   MockNetworkLayer* network_layer() {
182     return static_cast<MockNetworkLayer*>(http_cache_.network_layer());
183   }
184   MockDiskCache* disk_cache();
185
186   // Wrapper around http_cache()->CreateTransaction(net::DEFAULT_PRIORITY...)
187   int CreateTransaction(scoped_ptr<net::HttpTransaction>* trans);
188
189   // Wrapper to bypass the cache lock for new transactions.
190   void BypassCacheLock();
191
192   // Helper function for reading response info from the disk cache.
193   static bool ReadResponseInfo(disk_cache::Entry* disk_entry,
194                                net::HttpResponseInfo* response_info,
195                                bool* response_truncated);
196
197   // Helper function for writing response info into the disk cache.
198   static bool WriteResponseInfo(disk_cache::Entry* disk_entry,
199                                 const net::HttpResponseInfo* response_info,
200                                 bool skip_transient_headers,
201                                 bool response_truncated);
202
203   // Helper function to synchronously open a backend entry.
204   bool OpenBackendEntry(const std::string& key, disk_cache::Entry** entry);
205
206   // Helper function to synchronously create a backend entry.
207   bool CreateBackendEntry(const std::string& key, disk_cache::Entry** entry,
208                           net::NetLog* net_log);
209
210   // Returns the test mode after considering the global override.
211   static int GetTestMode(int test_mode);
212
213   // Overrides the test mode for a given operation. Remember to reset it after
214   // the test! (by setting test_mode to zero).
215   static void SetTestMode(int test_mode);
216
217  private:
218   net::HttpCache http_cache_;
219 };
220
221 // This version of the disk cache doesn't invoke CreateEntry callbacks.
222 class MockDiskCacheNoCB : public MockDiskCache {
223   int CreateEntry(const std::string& key,
224                   disk_cache::Entry** entry,
225                   const net::CompletionCallback& callback) override;
226 };
227
228 class MockBackendNoCbFactory : public net::HttpCache::BackendFactory {
229  public:
230   int CreateBackend(net::NetLog* net_log,
231                     scoped_ptr<disk_cache::Backend>* backend,
232                     const net::CompletionCallback& callback) override;
233 };
234
235 // This backend factory allows us to control the backend instantiation.
236 class MockBlockingBackendFactory : public net::HttpCache::BackendFactory {
237  public:
238   MockBlockingBackendFactory();
239   ~MockBlockingBackendFactory() override;
240
241   int CreateBackend(net::NetLog* net_log,
242                     scoped_ptr<disk_cache::Backend>* backend,
243                     const net::CompletionCallback& callback) override;
244
245   // Completes the backend creation. Any blocked call will be notified via the
246   // provided callback.
247   void FinishCreation();
248
249   scoped_ptr<disk_cache::Backend>* backend() { return backend_; }
250   void set_fail(bool fail) { fail_ = fail; }
251
252   const net::CompletionCallback& callback() { return callback_; }
253
254  private:
255   int Result() { return fail_ ? net::ERR_FAILED : net::OK; }
256
257   scoped_ptr<disk_cache::Backend>* backend_;
258   net::CompletionCallback callback_;
259   bool block_;
260   bool fail_;
261 };
262
263 #endif  // NET_HTTP_MOCK_HTTP_CACHE_H_