| 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_ |
| (...skipping 13 matching lines...) Expand all Loading... |
| 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, |
| 35 int offset, |
| 36 net::IOBuffer* buf, |
| 37 int buf_len, |
| 35 const net::CompletionCallback& callback) OVERRIDE; | 38 const net::CompletionCallback& callback) OVERRIDE; |
| 36 virtual int WriteData(int index, int offset, net::IOBuffer* buf, int buf_len, | 39 virtual int WriteData(int index, |
| 40 int offset, |
| 41 net::IOBuffer* buf, |
| 42 int buf_len, |
| 37 const net::CompletionCallback& callback, | 43 const net::CompletionCallback& callback, |
| 38 bool truncate) OVERRIDE; | 44 bool truncate) OVERRIDE; |
| 39 virtual int ReadSparseData(int64 offset, net::IOBuffer* buf, int buf_len, | 45 virtual int ReadSparseData(int64 offset, |
| 46 net::IOBuffer* buf, |
| 47 int buf_len, |
| 40 const net::CompletionCallback& callback) OVERRIDE; | 48 const net::CompletionCallback& callback) OVERRIDE; |
| 41 virtual int WriteSparseData( | 49 virtual int WriteSparseData(int64 offset, |
| 42 int64 offset, net::IOBuffer* buf, int buf_len, | 50 net::IOBuffer* buf, |
| 43 const net::CompletionCallback& callback) OVERRIDE; | 51 int buf_len, |
| 52 const net::CompletionCallback& callback) OVERRIDE; |
| 44 virtual int GetAvailableRange( | 53 virtual int GetAvailableRange( |
| 45 int64 offset, int len, int64* start, | 54 int64 offset, |
| 55 int len, |
| 56 int64* start, |
| 46 const net::CompletionCallback& callback) OVERRIDE; | 57 const net::CompletionCallback& callback) OVERRIDE; |
| 47 virtual bool CouldBeSparse() const OVERRIDE; | 58 virtual bool CouldBeSparse() const OVERRIDE; |
| 48 virtual void CancelSparseIO() OVERRIDE; | 59 virtual void CancelSparseIO() OVERRIDE; |
| 49 virtual int ReadyForSparseIO( | 60 virtual int ReadyForSparseIO( |
| 50 const net::CompletionCallback& completion_callback) OVERRIDE; | 61 const net::CompletionCallback& completion_callback) OVERRIDE; |
| 51 | 62 |
| 52 // Fail most subsequent requests. | 63 // Fail most subsequent requests. |
| 53 void set_fail_requests() { fail_requests_ = true; } | 64 void set_fail_requests() { fail_requests_ = true; } |
| 54 | 65 |
| 55 void set_fail_sparse_requests() { fail_sparse_requests_ = true; } | 66 void set_fail_sparse_requests() { fail_sparse_requests_ = true; } |
| (...skipping 11 matching lines...) Expand all Loading... |
| 67 | 78 |
| 68 // Unlike the callbacks for MockHttpTransaction, we want this one to run even | 79 // 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 | 80 // if the consumer called Close on the MockDiskEntry. We achieve that by |
| 70 // leveraging the fact that this class is reference counted. | 81 // leveraging the fact that this class is reference counted. |
| 71 void CallbackLater(const net::CompletionCallback& callback, int result); | 82 void CallbackLater(const net::CompletionCallback& callback, int result); |
| 72 | 83 |
| 73 void RunCallback(const net::CompletionCallback& callback, int result); | 84 void RunCallback(const net::CompletionCallback& callback, int result); |
| 74 | 85 |
| 75 // When |store| is true, stores the callback to be delivered later; otherwise | 86 // When |store| is true, stores the callback to be delivered later; otherwise |
| 76 // delivers any callback previously stored. | 87 // delivers any callback previously stored. |
| 77 static void StoreAndDeliverCallbacks(bool store, MockDiskEntry* entry, | 88 static void StoreAndDeliverCallbacks(bool store, |
| 89 MockDiskEntry* entry, |
| 78 const net::CompletionCallback& callback, | 90 const net::CompletionCallback& callback, |
| 79 int result); | 91 int result); |
| 80 | 92 |
| 81 static const int kNumCacheEntryDataIndices = 3; | 93 static const int kNumCacheEntryDataIndices = 3; |
| 82 | 94 |
| 83 std::string key_; | 95 std::string key_; |
| 84 std::vector<char> data_[kNumCacheEntryDataIndices]; | 96 std::vector<char> data_[kNumCacheEntryDataIndices]; |
| 85 int test_mode_; | 97 int test_mode_; |
| 86 bool doomed_; | 98 bool doomed_; |
| 87 bool sparse_; | 99 bool sparse_; |
| 88 bool fail_requests_; | 100 bool fail_requests_; |
| 89 bool fail_sparse_requests_; | 101 bool fail_sparse_requests_; |
| 90 bool busy_; | 102 bool busy_; |
| 91 bool delayed_; | 103 bool delayed_; |
| 92 static bool cancel_; | 104 static bool cancel_; |
| 93 static bool ignore_callbacks_; | 105 static bool ignore_callbacks_; |
| 94 }; | 106 }; |
| 95 | 107 |
| 96 class MockDiskCache : public disk_cache::Backend { | 108 class MockDiskCache : public disk_cache::Backend { |
| 97 public: | 109 public: |
| 98 MockDiskCache(); | 110 MockDiskCache(); |
| 99 virtual ~MockDiskCache(); | 111 virtual ~MockDiskCache(); |
| 100 | 112 |
| 101 virtual net::CacheType GetCacheType() const OVERRIDE; | 113 virtual net::CacheType GetCacheType() const OVERRIDE; |
| 102 virtual int32 GetEntryCount() const OVERRIDE; | 114 virtual int32 GetEntryCount() const OVERRIDE; |
| 103 virtual int OpenEntry(const std::string& key, disk_cache::Entry** entry, | 115 virtual int OpenEntry(const std::string& key, |
| 116 disk_cache::Entry** entry, |
| 104 const net::CompletionCallback& callback) OVERRIDE; | 117 const net::CompletionCallback& callback) OVERRIDE; |
| 105 virtual int CreateEntry(const std::string& key, disk_cache::Entry** entry, | 118 virtual int CreateEntry(const std::string& key, |
| 119 disk_cache::Entry** entry, |
| 106 const net::CompletionCallback& callback) OVERRIDE; | 120 const net::CompletionCallback& callback) OVERRIDE; |
| 107 virtual int DoomEntry(const std::string& key, | 121 virtual int DoomEntry(const std::string& key, |
| 108 const net::CompletionCallback& callback) OVERRIDE; | 122 const net::CompletionCallback& callback) OVERRIDE; |
| 109 virtual int DoomAllEntries(const net::CompletionCallback& callback) OVERRIDE; | 123 virtual int DoomAllEntries(const net::CompletionCallback& callback) OVERRIDE; |
| 110 virtual int DoomEntriesBetween( | 124 virtual int DoomEntriesBetween( |
| 111 base::Time initial_time, | 125 base::Time initial_time, |
| 112 base::Time end_time, | 126 base::Time end_time, |
| 113 const net::CompletionCallback& callback) OVERRIDE; | 127 const net::CompletionCallback& callback) OVERRIDE; |
| 114 virtual int DoomEntriesSince( | 128 virtual int DoomEntriesSince( |
| 115 base::Time initial_time, | 129 base::Time initial_time, |
| 116 const net::CompletionCallback& callback) OVERRIDE; | 130 const net::CompletionCallback& callback) OVERRIDE; |
| 117 virtual int OpenNextEntry(void** iter, disk_cache::Entry** next_entry, | 131 virtual int OpenNextEntry(void** iter, |
| 132 disk_cache::Entry** next_entry, |
| 118 const net::CompletionCallback& callback) OVERRIDE; | 133 const net::CompletionCallback& callback) OVERRIDE; |
| 119 virtual void EndEnumeration(void** iter) OVERRIDE; | 134 virtual void EndEnumeration(void** iter) OVERRIDE; |
| 120 virtual void GetStats( | 135 virtual void GetStats( |
| 121 std::vector<std::pair<std::string, std::string> >* stats) OVERRIDE; | 136 std::vector<std::pair<std::string, std::string> >* stats) OVERRIDE; |
| 122 virtual void OnExternalCacheHit(const std::string& key) OVERRIDE; | 137 virtual void OnExternalCacheHit(const std::string& key) OVERRIDE; |
| 123 | 138 |
| 124 // Returns number of times a cache entry was successfully opened. | 139 // Returns number of times a cache entry was successfully opened. |
| 125 int open_count() const { return open_count_; } | 140 int open_count() const { return open_count_; } |
| 126 | 141 |
| 127 // Returns number of times a cache entry was successfully created. | 142 // Returns number of times a cache entry was successfully created. |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 185 // Helper function for writing response info into the disk cache. | 200 // Helper function for writing response info into the disk cache. |
| 186 static bool WriteResponseInfo(disk_cache::Entry* disk_entry, | 201 static bool WriteResponseInfo(disk_cache::Entry* disk_entry, |
| 187 const net::HttpResponseInfo* response_info, | 202 const net::HttpResponseInfo* response_info, |
| 188 bool skip_transient_headers, | 203 bool skip_transient_headers, |
| 189 bool response_truncated); | 204 bool response_truncated); |
| 190 | 205 |
| 191 // Helper function to synchronously open a backend entry. | 206 // Helper function to synchronously open a backend entry. |
| 192 bool OpenBackendEntry(const std::string& key, disk_cache::Entry** entry); | 207 bool OpenBackendEntry(const std::string& key, disk_cache::Entry** entry); |
| 193 | 208 |
| 194 // Helper function to synchronously create a backend entry. | 209 // Helper function to synchronously create a backend entry. |
| 195 bool CreateBackendEntry(const std::string& key, disk_cache::Entry** entry, | 210 bool CreateBackendEntry(const std::string& key, |
| 211 disk_cache::Entry** entry, |
| 196 net::NetLog* net_log); | 212 net::NetLog* net_log); |
| 197 | 213 |
| 198 // Returns the test mode after considering the global override. | 214 // Returns the test mode after considering the global override. |
| 199 static int GetTestMode(int test_mode); | 215 static int GetTestMode(int test_mode); |
| 200 | 216 |
| 201 // Overrides the test mode for a given operation. Remember to reset it after | 217 // Overrides the test mode for a given operation. Remember to reset it after |
| 202 // the test! (by setting test_mode to zero). | 218 // the test! (by setting test_mode to zero). |
| 203 static void SetTestMode(int test_mode); | 219 static void SetTestMode(int test_mode); |
| 204 | 220 |
| 205 private: | 221 private: |
| 206 net::HttpCache http_cache_; | 222 net::HttpCache http_cache_; |
| 207 }; | 223 }; |
| 208 | 224 |
| 209 // This version of the disk cache doesn't invoke CreateEntry callbacks. | 225 // This version of the disk cache doesn't invoke CreateEntry callbacks. |
| 210 class MockDiskCacheNoCB : public MockDiskCache { | 226 class MockDiskCacheNoCB : public MockDiskCache { |
| 211 virtual int CreateEntry(const std::string& key, disk_cache::Entry** entry, | 227 virtual int CreateEntry(const std::string& key, |
| 228 disk_cache::Entry** entry, |
| 212 const net::CompletionCallback& callback) OVERRIDE; | 229 const net::CompletionCallback& callback) OVERRIDE; |
| 213 }; | 230 }; |
| 214 | 231 |
| 215 class MockBackendNoCbFactory : public net::HttpCache::BackendFactory { | 232 class MockBackendNoCbFactory : public net::HttpCache::BackendFactory { |
| 216 public: | 233 public: |
| 217 virtual int CreateBackend(net::NetLog* net_log, | 234 virtual int CreateBackend(net::NetLog* net_log, |
| 218 scoped_ptr<disk_cache::Backend>* backend, | 235 scoped_ptr<disk_cache::Backend>* backend, |
| 219 const net::CompletionCallback& callback) OVERRIDE; | 236 const net::CompletionCallback& callback) OVERRIDE; |
| 220 }; | 237 }; |
| 221 | 238 |
| (...skipping 19 matching lines...) Expand all Loading... |
| 241 private: | 258 private: |
| 242 int Result() { return fail_ ? net::ERR_FAILED : net::OK; } | 259 int Result() { return fail_ ? net::ERR_FAILED : net::OK; } |
| 243 | 260 |
| 244 scoped_ptr<disk_cache::Backend>* backend_; | 261 scoped_ptr<disk_cache::Backend>* backend_; |
| 245 net::CompletionCallback callback_; | 262 net::CompletionCallback callback_; |
| 246 bool block_; | 263 bool block_; |
| 247 bool fail_; | 264 bool fail_; |
| 248 }; | 265 }; |
| 249 | 266 |
| 250 #endif // NET_HTTP_MOCK_HTTP_CACHE_H_ | 267 #endif // NET_HTTP_MOCK_HTTP_CACHE_H_ |
| OLD | NEW |