| 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 void Doom() override; |
| 29 virtual void Close() override; | 29 void Close() override; |
| 30 virtual std::string GetKey() const override; | 30 std::string GetKey() const override; |
| 31 virtual base::Time GetLastUsed() const override; | 31 base::Time GetLastUsed() const override; |
| 32 virtual base::Time GetLastModified() const override; | 32 base::Time GetLastModified() const override; |
| 33 virtual int32 GetDataSize(int index) const override; | 33 int32 GetDataSize(int index) const override; |
| 34 virtual int ReadData(int index, int offset, net::IOBuffer* buf, int buf_len, | 34 int ReadData(int index, |
| 35 const net::CompletionCallback& callback) override; | 35 int offset, |
| 36 virtual int WriteData(int index, int offset, net::IOBuffer* buf, int buf_len, | 36 net::IOBuffer* buf, |
| 37 const net::CompletionCallback& callback, | 37 int buf_len, |
| 38 bool truncate) override; | 38 const net::CompletionCallback& callback) override; |
| 39 virtual int ReadSparseData(int64 offset, net::IOBuffer* buf, int buf_len, | 39 int WriteData(int index, |
| 40 const net::CompletionCallback& callback) override; | 40 int offset, |
| 41 virtual int WriteSparseData( | 41 net::IOBuffer* buf, |
| 42 int64 offset, net::IOBuffer* buf, int buf_len, | 42 int buf_len, |
| 43 const net::CompletionCallback& callback) override; | 43 const net::CompletionCallback& callback, |
| 44 virtual int GetAvailableRange( | 44 bool truncate) override; |
| 45 int64 offset, int len, int64* start, | 45 int ReadSparseData(int64 offset, |
| 46 const net::CompletionCallback& callback) override; | 46 net::IOBuffer* buf, |
| 47 virtual bool CouldBeSparse() const override; | 47 int buf_len, |
| 48 virtual void CancelSparseIO() override; | 48 const net::CompletionCallback& callback) override; |
| 49 virtual int ReadyForSparseIO( | 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( |
| 50 const net::CompletionCallback& completion_callback) override; | 60 const net::CompletionCallback& completion_callback) override; |
| 51 | 61 |
| 52 // Fail most subsequent requests. | 62 // Fail most subsequent requests. |
| 53 void set_fail_requests() { fail_requests_ = true; } | 63 void set_fail_requests() { fail_requests_ = true; } |
| 54 | 64 |
| 55 void set_fail_sparse_requests() { fail_sparse_requests_ = true; } | 65 void set_fail_sparse_requests() { fail_sparse_requests_ = true; } |
| 56 | 66 |
| 57 // If |value| is true, don't deliver any completion callbacks until called | 67 // If |value| is true, don't deliver any completion callbacks until called |
| 58 // again with |value| set to false. Caution: remember to enable callbacks | 68 // again with |value| set to false. Caution: remember to enable callbacks |
| 59 // again or all subsequent tests will fail. | 69 // again or all subsequent tests will fail. |
| 60 static void IgnoreCallbacks(bool value); | 70 static void IgnoreCallbacks(bool value); |
| 61 | 71 |
| 62 private: | 72 private: |
| 63 friend class base::RefCounted<MockDiskEntry>; | 73 friend class base::RefCounted<MockDiskEntry>; |
| 64 struct CallbackInfo; | 74 struct CallbackInfo; |
| 65 | 75 |
| 66 virtual ~MockDiskEntry(); | 76 ~MockDiskEntry() override; |
| 67 | 77 |
| 68 // Unlike the callbacks for MockHttpTransaction, we want this one to run even | 78 // Unlike the callbacks for MockHttpTransaction, we want this one to run even |
| 69 // if the consumer called Close on the MockDiskEntry. We achieve that by | 79 // if the consumer called Close on the MockDiskEntry. We achieve that by |
| 70 // leveraging the fact that this class is reference counted. | 80 // leveraging the fact that this class is reference counted. |
| 71 void CallbackLater(const net::CompletionCallback& callback, int result); | 81 void CallbackLater(const net::CompletionCallback& callback, int result); |
| 72 | 82 |
| 73 void RunCallback(const net::CompletionCallback& callback, int result); | 83 void RunCallback(const net::CompletionCallback& callback, int result); |
| 74 | 84 |
| 75 // When |store| is true, stores the callback to be delivered later; otherwise | 85 // When |store| is true, stores the callback to be delivered later; otherwise |
| 76 // delivers any callback previously stored. | 86 // delivers any callback previously stored. |
| (...skipping 12 matching lines...) Expand all Loading... |
| 89 bool fail_sparse_requests_; | 99 bool fail_sparse_requests_; |
| 90 bool busy_; | 100 bool busy_; |
| 91 bool delayed_; | 101 bool delayed_; |
| 92 static bool cancel_; | 102 static bool cancel_; |
| 93 static bool ignore_callbacks_; | 103 static bool ignore_callbacks_; |
| 94 }; | 104 }; |
| 95 | 105 |
| 96 class MockDiskCache : public disk_cache::Backend { | 106 class MockDiskCache : public disk_cache::Backend { |
| 97 public: | 107 public: |
| 98 MockDiskCache(); | 108 MockDiskCache(); |
| 99 virtual ~MockDiskCache(); | 109 ~MockDiskCache() override; |
| 100 | 110 |
| 101 virtual net::CacheType GetCacheType() const override; | 111 net::CacheType GetCacheType() const override; |
| 102 virtual int32 GetEntryCount() const override; | 112 int32 GetEntryCount() const override; |
| 103 virtual int OpenEntry(const std::string& key, disk_cache::Entry** entry, | 113 int OpenEntry(const std::string& key, |
| 104 const net::CompletionCallback& callback) override; | 114 disk_cache::Entry** entry, |
| 105 virtual int CreateEntry(const std::string& key, disk_cache::Entry** entry, | 115 const net::CompletionCallback& callback) override; |
| 106 const net::CompletionCallback& callback) override; | 116 int CreateEntry(const std::string& key, |
| 107 virtual int DoomEntry(const std::string& key, | 117 disk_cache::Entry** entry, |
| 108 const net::CompletionCallback& callback) override; | 118 const net::CompletionCallback& callback) override; |
| 109 virtual int DoomAllEntries(const net::CompletionCallback& callback) override; | 119 int DoomEntry(const std::string& key, |
| 110 virtual int DoomEntriesBetween( | 120 const net::CompletionCallback& callback) override; |
| 111 base::Time initial_time, | 121 int DoomAllEntries(const net::CompletionCallback& callback) override; |
| 112 base::Time end_time, | 122 int DoomEntriesBetween(base::Time initial_time, |
| 113 const net::CompletionCallback& callback) override; | 123 base::Time end_time, |
| 114 virtual int DoomEntriesSince( | 124 const net::CompletionCallback& callback) override; |
| 115 base::Time initial_time, | 125 int DoomEntriesSince(base::Time initial_time, |
| 116 const net::CompletionCallback& callback) override; | 126 const net::CompletionCallback& callback) override; |
| 117 virtual scoped_ptr<Iterator> CreateIterator() override; | 127 scoped_ptr<Iterator> CreateIterator() override; |
| 118 virtual void GetStats( | 128 void GetStats( |
| 119 std::vector<std::pair<std::string, std::string> >* stats) override; | 129 std::vector<std::pair<std::string, std::string>>* stats) override; |
| 120 virtual void OnExternalCacheHit(const std::string& key) override; | 130 void OnExternalCacheHit(const std::string& key) override; |
| 121 | 131 |
| 122 // Returns number of times a cache entry was successfully opened. | 132 // Returns number of times a cache entry was successfully opened. |
| 123 int open_count() const { return open_count_; } | 133 int open_count() const { return open_count_; } |
| 124 | 134 |
| 125 // Returns number of times a cache entry was successfully created. | 135 // Returns number of times a cache entry was successfully created. |
| 126 int create_count() const { return create_count_; } | 136 int create_count() const { return create_count_; } |
| 127 | 137 |
| 128 // Fail any subsequent CreateEntry and OpenEntry. | 138 // Fail any subsequent CreateEntry and OpenEntry. |
| 129 void set_fail_requests() { fail_requests_ = true; } | 139 void set_fail_requests() { fail_requests_ = true; } |
| 130 | 140 |
| (...skipping 18 matching lines...) Expand all Loading... |
| 149 int open_count_; | 159 int open_count_; |
| 150 int create_count_; | 160 int create_count_; |
| 151 bool fail_requests_; | 161 bool fail_requests_; |
| 152 bool soft_failures_; | 162 bool soft_failures_; |
| 153 bool double_create_check_; | 163 bool double_create_check_; |
| 154 bool fail_sparse_requests_; | 164 bool fail_sparse_requests_; |
| 155 }; | 165 }; |
| 156 | 166 |
| 157 class MockBackendFactory : public net::HttpCache::BackendFactory { | 167 class MockBackendFactory : public net::HttpCache::BackendFactory { |
| 158 public: | 168 public: |
| 159 virtual int CreateBackend(net::NetLog* net_log, | 169 int CreateBackend(net::NetLog* net_log, |
| 160 scoped_ptr<disk_cache::Backend>* backend, | 170 scoped_ptr<disk_cache::Backend>* backend, |
| 161 const net::CompletionCallback& callback) override; | 171 const net::CompletionCallback& callback) override; |
| 162 }; | 172 }; |
| 163 | 173 |
| 164 class MockHttpCache { | 174 class MockHttpCache { |
| 165 public: | 175 public: |
| 166 MockHttpCache(); | 176 MockHttpCache(); |
| 167 explicit MockHttpCache(net::HttpCache::BackendFactory* disk_cache_factory); | 177 explicit MockHttpCache(net::HttpCache::BackendFactory* disk_cache_factory); |
| 168 | 178 |
| 169 net::HttpCache* http_cache() { return &http_cache_; } | 179 net::HttpCache* http_cache() { return &http_cache_; } |
| 170 | 180 |
| 171 MockNetworkLayer* network_layer() { | 181 MockNetworkLayer* network_layer() { |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 203 // Overrides the test mode for a given operation. Remember to reset it after | 213 // Overrides the test mode for a given operation. Remember to reset it after |
| 204 // the test! (by setting test_mode to zero). | 214 // the test! (by setting test_mode to zero). |
| 205 static void SetTestMode(int test_mode); | 215 static void SetTestMode(int test_mode); |
| 206 | 216 |
| 207 private: | 217 private: |
| 208 net::HttpCache http_cache_; | 218 net::HttpCache http_cache_; |
| 209 }; | 219 }; |
| 210 | 220 |
| 211 // This version of the disk cache doesn't invoke CreateEntry callbacks. | 221 // This version of the disk cache doesn't invoke CreateEntry callbacks. |
| 212 class MockDiskCacheNoCB : public MockDiskCache { | 222 class MockDiskCacheNoCB : public MockDiskCache { |
| 213 virtual int CreateEntry(const std::string& key, disk_cache::Entry** entry, | 223 int CreateEntry(const std::string& key, |
| 214 const net::CompletionCallback& callback) override; | 224 disk_cache::Entry** entry, |
| 225 const net::CompletionCallback& callback) override; |
| 215 }; | 226 }; |
| 216 | 227 |
| 217 class MockBackendNoCbFactory : public net::HttpCache::BackendFactory { | 228 class MockBackendNoCbFactory : public net::HttpCache::BackendFactory { |
| 218 public: | 229 public: |
| 219 virtual int CreateBackend(net::NetLog* net_log, | 230 int CreateBackend(net::NetLog* net_log, |
| 220 scoped_ptr<disk_cache::Backend>* backend, | 231 scoped_ptr<disk_cache::Backend>* backend, |
| 221 const net::CompletionCallback& callback) override; | 232 const net::CompletionCallback& callback) override; |
| 222 }; | 233 }; |
| 223 | 234 |
| 224 // This backend factory allows us to control the backend instantiation. | 235 // This backend factory allows us to control the backend instantiation. |
| 225 class MockBlockingBackendFactory : public net::HttpCache::BackendFactory { | 236 class MockBlockingBackendFactory : public net::HttpCache::BackendFactory { |
| 226 public: | 237 public: |
| 227 MockBlockingBackendFactory(); | 238 MockBlockingBackendFactory(); |
| 228 virtual ~MockBlockingBackendFactory(); | 239 ~MockBlockingBackendFactory() override; |
| 229 | 240 |
| 230 virtual int CreateBackend(net::NetLog* net_log, | 241 int CreateBackend(net::NetLog* net_log, |
| 231 scoped_ptr<disk_cache::Backend>* backend, | 242 scoped_ptr<disk_cache::Backend>* backend, |
| 232 const net::CompletionCallback& callback) override; | 243 const net::CompletionCallback& callback) override; |
| 233 | 244 |
| 234 // Completes the backend creation. Any blocked call will be notified via the | 245 // Completes the backend creation. Any blocked call will be notified via the |
| 235 // provided callback. | 246 // provided callback. |
| 236 void FinishCreation(); | 247 void FinishCreation(); |
| 237 | 248 |
| 238 scoped_ptr<disk_cache::Backend>* backend() { return backend_; } | 249 scoped_ptr<disk_cache::Backend>* backend() { return backend_; } |
| 239 void set_fail(bool fail) { fail_ = fail; } | 250 void set_fail(bool fail) { fail_ = fail; } |
| 240 | 251 |
| 241 const net::CompletionCallback& callback() { return callback_; } | 252 const net::CompletionCallback& callback() { return callback_; } |
| 242 | 253 |
| 243 private: | 254 private: |
| 244 int Result() { return fail_ ? net::ERR_FAILED : net::OK; } | 255 int Result() { return fail_ ? net::ERR_FAILED : net::OK; } |
| 245 | 256 |
| 246 scoped_ptr<disk_cache::Backend>* backend_; | 257 scoped_ptr<disk_cache::Backend>* backend_; |
| 247 net::CompletionCallback callback_; | 258 net::CompletionCallback callback_; |
| 248 bool block_; | 259 bool block_; |
| 249 bool fail_; | 260 bool fail_; |
| 250 }; | 261 }; |
| 251 | 262 |
| 252 #endif // NET_HTTP_MOCK_HTTP_CACHE_H_ | 263 #endif // NET_HTTP_MOCK_HTTP_CACHE_H_ |
| OLD | NEW |