| 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 <stdint.h> |
| 14 |
| 13 #include "base/containers/hash_tables.h" | 15 #include "base/containers/hash_tables.h" |
| 14 #include "base/strings/string_split.h" | 16 #include "base/strings/string_split.h" |
| 15 #include "net/disk_cache/disk_cache.h" | 17 #include "net/disk_cache/disk_cache.h" |
| 16 #include "net/http/http_cache.h" | 18 #include "net/http/http_cache.h" |
| 17 #include "net/http/http_transaction_test_util.h" | 19 #include "net/http/http_transaction_test_util.h" |
| 18 | 20 |
| 19 namespace net { | 21 namespace net { |
| 20 | 22 |
| 21 //----------------------------------------------------------------------------- | 23 //----------------------------------------------------------------------------- |
| 22 // Mock disk cache (a very basic memory cache implementation). | 24 // Mock disk cache (a very basic memory cache implementation). |
| 23 | 25 |
| 24 class MockDiskEntry : public disk_cache::Entry, | 26 class MockDiskEntry : public disk_cache::Entry, |
| 25 public base::RefCounted<MockDiskEntry> { | 27 public base::RefCounted<MockDiskEntry> { |
| 26 public: | 28 public: |
| 27 explicit MockDiskEntry(const std::string& key); | 29 explicit MockDiskEntry(const std::string& key); |
| 28 | 30 |
| 29 bool is_doomed() const { return doomed_; } | 31 bool is_doomed() const { return doomed_; } |
| 30 | 32 |
| 31 void Doom() override; | 33 void Doom() override; |
| 32 void Close() override; | 34 void Close() override; |
| 33 std::string GetKey() const override; | 35 std::string GetKey() const override; |
| 34 base::Time GetLastUsed() const override; | 36 base::Time GetLastUsed() const override; |
| 35 base::Time GetLastModified() const override; | 37 base::Time GetLastModified() const override; |
| 36 int32 GetDataSize(int index) const override; | 38 int32_t GetDataSize(int index) const override; |
| 37 int ReadData(int index, | 39 int ReadData(int index, |
| 38 int offset, | 40 int offset, |
| 39 IOBuffer* buf, | 41 IOBuffer* buf, |
| 40 int buf_len, | 42 int buf_len, |
| 41 const CompletionCallback& callback) override; | 43 const CompletionCallback& callback) override; |
| 42 int WriteData(int index, | 44 int WriteData(int index, |
| 43 int offset, | 45 int offset, |
| 44 IOBuffer* buf, | 46 IOBuffer* buf, |
| 45 int buf_len, | 47 int buf_len, |
| 46 const CompletionCallback& callback, | 48 const CompletionCallback& callback, |
| 47 bool truncate) override; | 49 bool truncate) override; |
| 48 int ReadSparseData(int64 offset, | 50 int ReadSparseData(int64_t offset, |
| 49 IOBuffer* buf, | 51 IOBuffer* buf, |
| 50 int buf_len, | 52 int buf_len, |
| 51 const CompletionCallback& callback) override; | 53 const CompletionCallback& callback) override; |
| 52 int WriteSparseData(int64 offset, | 54 int WriteSparseData(int64_t offset, |
| 53 IOBuffer* buf, | 55 IOBuffer* buf, |
| 54 int buf_len, | 56 int buf_len, |
| 55 const CompletionCallback& callback) override; | 57 const CompletionCallback& callback) override; |
| 56 int GetAvailableRange(int64 offset, | 58 int GetAvailableRange(int64_t offset, |
| 57 int len, | 59 int len, |
| 58 int64* start, | 60 int64_t* start, |
| 59 const CompletionCallback& callback) override; | 61 const CompletionCallback& callback) override; |
| 60 bool CouldBeSparse() const override; | 62 bool CouldBeSparse() const override; |
| 61 void CancelSparseIO() override; | 63 void CancelSparseIO() override; |
| 62 int ReadyForSparseIO(const CompletionCallback& completion_callback) override; | 64 int ReadyForSparseIO(const CompletionCallback& completion_callback) override; |
| 63 | 65 |
| 64 // Fail most subsequent requests. | 66 // Fail most subsequent requests. |
| 65 void set_fail_requests() { fail_requests_ = true; } | 67 void set_fail_requests() { fail_requests_ = true; } |
| 66 | 68 |
| 67 void set_fail_sparse_requests() { fail_sparse_requests_ = true; } | 69 void set_fail_sparse_requests() { fail_sparse_requests_ = true; } |
| 68 | 70 |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 105 bool cancel_; | 107 bool cancel_; |
| 106 static bool ignore_callbacks_; | 108 static bool ignore_callbacks_; |
| 107 }; | 109 }; |
| 108 | 110 |
| 109 class MockDiskCache : public disk_cache::Backend { | 111 class MockDiskCache : public disk_cache::Backend { |
| 110 public: | 112 public: |
| 111 MockDiskCache(); | 113 MockDiskCache(); |
| 112 ~MockDiskCache() override; | 114 ~MockDiskCache() override; |
| 113 | 115 |
| 114 CacheType GetCacheType() const override; | 116 CacheType GetCacheType() const override; |
| 115 int32 GetEntryCount() const override; | 117 int32_t GetEntryCount() const override; |
| 116 int OpenEntry(const std::string& key, | 118 int OpenEntry(const std::string& key, |
| 117 disk_cache::Entry** entry, | 119 disk_cache::Entry** entry, |
| 118 const CompletionCallback& callback) override; | 120 const CompletionCallback& callback) override; |
| 119 int CreateEntry(const std::string& key, | 121 int CreateEntry(const std::string& key, |
| 120 disk_cache::Entry** entry, | 122 disk_cache::Entry** entry, |
| 121 const CompletionCallback& callback) override; | 123 const CompletionCallback& callback) override; |
| 122 int DoomEntry(const std::string& key, | 124 int DoomEntry(const std::string& key, |
| 123 const CompletionCallback& callback) override; | 125 const CompletionCallback& callback) override; |
| 124 int DoomAllEntries(const CompletionCallback& callback) override; | 126 int DoomAllEntries(const CompletionCallback& callback) override; |
| 125 int DoomEntriesBetween(base::Time initial_time, | 127 int DoomEntriesBetween(base::Time initial_time, |
| (...skipping 139 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 265 | 267 |
| 266 scoped_ptr<disk_cache::Backend>* backend_; | 268 scoped_ptr<disk_cache::Backend>* backend_; |
| 267 CompletionCallback callback_; | 269 CompletionCallback callback_; |
| 268 bool block_; | 270 bool block_; |
| 269 bool fail_; | 271 bool fail_; |
| 270 }; | 272 }; |
| 271 | 273 |
| 272 } // namespace net | 274 } // namespace net |
| 273 | 275 |
| 274 #endif // NET_HTTP_MOCK_HTTP_CACHE_H_ | 276 #endif // NET_HTTP_MOCK_HTTP_CACHE_H_ |
| OLD | NEW |