| 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_unittest.h" | 16 #include "net/http/http_transaction_unittest.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 MockDiskCache; |
| 22 |
| 21 class MockDiskEntry : public disk_cache::Entry, | 23 class MockDiskEntry : public disk_cache::Entry, |
| 22 public base::RefCounted<MockDiskEntry> { | 24 public base::RefCounted<MockDiskEntry> { |
| 23 public: | 25 public: |
| 24 MockDiskEntry(); | 26 MockDiskEntry(MockDiskCache* backend, const std::string& key); |
| 25 explicit MockDiskEntry(const std::string& key); | |
| 26 | 27 |
| 27 bool is_doomed() const { return doomed_; } | 28 bool is_doomed() const { return doomed_; } |
| 28 | 29 |
| 29 virtual void Doom() OVERRIDE; | 30 virtual void Doom() OVERRIDE; |
| 30 virtual void Close() OVERRIDE; | 31 virtual void Close() OVERRIDE; |
| 31 virtual std::string GetKey() const OVERRIDE; | 32 virtual std::string GetKey() const OVERRIDE; |
| 32 virtual base::Time GetLastUsed() const OVERRIDE; | 33 virtual base::Time GetLastUsed() const OVERRIDE; |
| 33 virtual base::Time GetLastModified() const OVERRIDE; | 34 virtual base::Time GetLastModified() const OVERRIDE; |
| 34 virtual int32 GetDataSize(int index) const OVERRIDE; | 35 virtual int32 GetDataSize(int index) const OVERRIDE; |
| 35 virtual int ReadData(int index, int offset, net::IOBuffer* buf, int buf_len, | 36 virtual int ReadData(int index, int offset, net::IOBuffer* buf, int buf_len, |
| (...skipping 10 matching lines...) Expand all Loading... |
| 46 int64 offset, int len, int64* start, | 47 int64 offset, int len, int64* start, |
| 47 const net::CompletionCallback& callback) OVERRIDE; | 48 const net::CompletionCallback& callback) OVERRIDE; |
| 48 virtual bool CouldBeSparse() const OVERRIDE; | 49 virtual bool CouldBeSparse() const OVERRIDE; |
| 49 virtual void CancelSparseIO() OVERRIDE; | 50 virtual void CancelSparseIO() OVERRIDE; |
| 50 virtual int ReadyForSparseIO( | 51 virtual int ReadyForSparseIO( |
| 51 const net::CompletionCallback& completion_callback) OVERRIDE; | 52 const net::CompletionCallback& completion_callback) OVERRIDE; |
| 52 | 53 |
| 53 // Fail most subsequent requests. | 54 // Fail most subsequent requests. |
| 54 void set_fail_requests() { fail_requests_ = true; } | 55 void set_fail_requests() { fail_requests_ = true; } |
| 55 | 56 |
| 57 void set_fail_sparse_requests() { fail_sparse_requests_ = true; } |
| 58 |
| 56 // If |value| is true, don't deliver any completion callbacks until called | 59 // If |value| is true, don't deliver any completion callbacks until called |
| 57 // again with |value| set to false. Caution: remember to enable callbacks | 60 // again with |value| set to false. Caution: remember to enable callbacks |
| 58 // again or all subsequent tests will fail. | 61 // again or all subsequent tests will fail. |
| 59 static void IgnoreCallbacks(bool value); | 62 static void IgnoreCallbacks(bool value); |
| 60 | 63 |
| 61 private: | 64 private: |
| 62 friend class base::RefCounted<MockDiskEntry>; | 65 friend class base::RefCounted<MockDiskEntry>; |
| 63 struct CallbackInfo; | 66 struct CallbackInfo; |
| 64 | 67 |
| 65 virtual ~MockDiskEntry(); | 68 virtual ~MockDiskEntry(); |
| (...skipping 12 matching lines...) Expand all Loading... |
| 78 int result); | 81 int result); |
| 79 | 82 |
| 80 static const int kNumCacheEntryDataIndices = 3; | 83 static const int kNumCacheEntryDataIndices = 3; |
| 81 | 84 |
| 82 std::string key_; | 85 std::string key_; |
| 83 std::vector<char> data_[kNumCacheEntryDataIndices]; | 86 std::vector<char> data_[kNumCacheEntryDataIndices]; |
| 84 int test_mode_; | 87 int test_mode_; |
| 85 bool doomed_; | 88 bool doomed_; |
| 86 bool sparse_; | 89 bool sparse_; |
| 87 bool fail_requests_; | 90 bool fail_requests_; |
| 91 bool fail_sparse_requests_; |
| 88 bool busy_; | 92 bool busy_; |
| 89 bool delayed_; | 93 bool delayed_; |
| 90 static bool cancel_; | 94 static bool cancel_; |
| 91 static bool ignore_callbacks_; | 95 static bool ignore_callbacks_; |
| 96 MockDiskCache* backend_; |
| 92 }; | 97 }; |
| 93 | 98 |
| 94 class MockDiskCache : public disk_cache::Backend { | 99 class MockDiskCache : public disk_cache::Backend { |
| 95 public: | 100 public: |
| 96 MockDiskCache(); | 101 MockDiskCache(); |
| 97 virtual ~MockDiskCache(); | 102 virtual ~MockDiskCache(); |
| 98 | 103 |
| 99 virtual net::CacheType GetCacheType() const OVERRIDE; | 104 virtual net::CacheType GetCacheType() const OVERRIDE; |
| 100 virtual int32 GetEntryCount() const OVERRIDE; | 105 virtual int32 GetEntryCount() const OVERRIDE; |
| 101 virtual int OpenEntry(const std::string& key, disk_cache::Entry** entry, | 106 virtual int OpenEntry(const std::string& key, disk_cache::Entry** entry, |
| (...skipping 16 matching lines...) Expand all Loading... |
| 118 virtual void GetStats( | 123 virtual void GetStats( |
| 119 std::vector<std::pair<std::string, std::string> >* stats) OVERRIDE; | 124 std::vector<std::pair<std::string, std::string> >* stats) OVERRIDE; |
| 120 virtual void OnExternalCacheHit(const std::string& key) OVERRIDE; | 125 virtual void OnExternalCacheHit(const std::string& key) OVERRIDE; |
| 121 | 126 |
| 122 // Returns number of times a cache entry was successfully opened. | 127 // Returns number of times a cache entry was successfully opened. |
| 123 int open_count() const { return open_count_; } | 128 int open_count() const { return open_count_; } |
| 124 | 129 |
| 125 // Returns number of times a cache entry was successfully created. | 130 // Returns number of times a cache entry was successfully created. |
| 126 int create_count() const { return create_count_; } | 131 int create_count() const { return create_count_; } |
| 127 | 132 |
| 133 // Returns the number of operations attempted that are specific to range |
| 134 // requests. |
| 135 int sparse_count() const { return sparse_count_; } |
| 136 |
| 128 // Fail any subsequent CreateEntry and OpenEntry. | 137 // Fail any subsequent CreateEntry and OpenEntry. |
| 129 void set_fail_requests() { fail_requests_ = true; } | 138 void set_fail_requests() { fail_requests_ = true; } |
| 130 | 139 |
| 131 // Return entries that fail some of their requests. | 140 // Return entries that fail some of their requests. |
| 132 void set_soft_failures(bool value) { soft_failures_ = value; } | 141 void set_soft_failures(bool value) { soft_failures_ = value; } |
| 133 | 142 |
| 134 // Makes sure that CreateEntry is not called twice for a given key. | 143 // Makes sure that CreateEntry is not called twice for a given key. |
| 135 void set_double_create_check(bool value) { double_create_check_ = value; } | 144 void set_double_create_check(bool value) { double_create_check_ = value; } |
| 136 | 145 |
| 146 // Makes all requests for data ranges to fail as not implemented. |
| 147 void set_fail_sparse_requests() { fail_sparse_requests_ = true; } |
| 148 |
| 149 void NotifySparseIO() { sparse_count_++; } |
| 150 |
| 137 void ReleaseAll(); | 151 void ReleaseAll(); |
| 138 | 152 |
| 139 private: | 153 private: |
| 140 typedef base::hash_map<std::string, MockDiskEntry*> EntryMap; | 154 typedef base::hash_map<std::string, MockDiskEntry*> EntryMap; |
| 141 | 155 |
| 142 void CallbackLater(const net::CompletionCallback& callback, int result); | 156 void CallbackLater(const net::CompletionCallback& callback, int result); |
| 143 | 157 |
| 144 EntryMap entries_; | 158 EntryMap entries_; |
| 145 int open_count_; | 159 int open_count_; |
| 146 int create_count_; | 160 int create_count_; |
| 161 int sparse_count_; |
| 147 bool fail_requests_; | 162 bool fail_requests_; |
| 148 bool soft_failures_; | 163 bool soft_failures_; |
| 149 bool double_create_check_; | 164 bool double_create_check_; |
| 165 bool fail_sparse_requests_; |
| 150 }; | 166 }; |
| 151 | 167 |
| 152 class MockBackendFactory : public net::HttpCache::BackendFactory { | 168 class MockBackendFactory : public net::HttpCache::BackendFactory { |
| 153 public: | 169 public: |
| 154 virtual int CreateBackend(net::NetLog* net_log, | 170 virtual int CreateBackend(net::NetLog* net_log, |
| 155 scoped_ptr<disk_cache::Backend>* backend, | 171 scoped_ptr<disk_cache::Backend>* backend, |
| 156 const net::CompletionCallback& callback) OVERRIDE; | 172 const net::CompletionCallback& callback) OVERRIDE; |
| 157 }; | 173 }; |
| 158 | 174 |
| 159 class MockHttpCache { | 175 class MockHttpCache { |
| (...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 232 private: | 248 private: |
| 233 int Result() { return fail_ ? net::ERR_FAILED : net::OK; } | 249 int Result() { return fail_ ? net::ERR_FAILED : net::OK; } |
| 234 | 250 |
| 235 scoped_ptr<disk_cache::Backend>* backend_; | 251 scoped_ptr<disk_cache::Backend>* backend_; |
| 236 net::CompletionCallback callback_; | 252 net::CompletionCallback callback_; |
| 237 bool block_; | 253 bool block_; |
| 238 bool fail_; | 254 bool fail_; |
| 239 }; | 255 }; |
| 240 | 256 |
| 241 #endif // NET_HTTP_MOCK_HTTP_CACHE_H_ | 257 #endif // NET_HTTP_MOCK_HTTP_CACHE_H_ |
| OLD | NEW |