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 17 matching lines...) Expand all Loading... |
28 bool is_doomed() const { return doomed_; } | 28 bool is_doomed() const { return doomed_; } |
29 | 29 |
30 virtual void Doom() OVERRIDE; | 30 virtual void Doom() OVERRIDE; |
31 virtual void Close() OVERRIDE; | 31 virtual void Close() OVERRIDE; |
32 virtual std::string GetKey() const OVERRIDE; | 32 virtual std::string GetKey() const OVERRIDE; |
33 virtual base::Time GetLastUsed() const OVERRIDE; | 33 virtual base::Time GetLastUsed() const OVERRIDE; |
34 virtual base::Time GetLastModified() const OVERRIDE; | 34 virtual base::Time GetLastModified() const OVERRIDE; |
35 virtual int32 GetDataSize(int index) const OVERRIDE; | 35 virtual int32 GetDataSize(int index) const OVERRIDE; |
36 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, |
37 net::OldCompletionCallback* callback) OVERRIDE; | 37 net::OldCompletionCallback* callback) OVERRIDE; |
38 virtual int ReadData(int index, int offset, net::IOBuffer* buf, int buf_len, | |
39 const net::CompletionCallback& callback) OVERRIDE; | |
40 virtual int WriteData(int index, int offset, net::IOBuffer* buf, int buf_len, | 38 virtual int WriteData(int index, int offset, net::IOBuffer* buf, int buf_len, |
41 net::OldCompletionCallback* callback, | 39 net::OldCompletionCallback* callback, |
42 bool truncate) OVERRIDE; | 40 bool truncate) OVERRIDE; |
43 virtual int WriteData(int index, int offset, net::IOBuffer* buf, int buf_len, | |
44 const net::CompletionCallback& callback, | |
45 bool truncate) OVERRIDE; | |
46 virtual int ReadSparseData(int64 offset, net::IOBuffer* buf, int buf_len, | 41 virtual int ReadSparseData(int64 offset, net::IOBuffer* buf, int buf_len, |
47 net::OldCompletionCallback* callback) OVERRIDE; | 42 net::OldCompletionCallback* callback) OVERRIDE; |
48 virtual int WriteSparseData(int64 offset, net::IOBuffer* buf, int buf_len, | 43 virtual int WriteSparseData(int64 offset, net::IOBuffer* buf, int buf_len, |
49 net::OldCompletionCallback* callback) OVERRIDE; | 44 net::OldCompletionCallback* callback) OVERRIDE; |
50 virtual int GetAvailableRange(int64 offset, int len, int64* start, | 45 virtual int GetAvailableRange(int64 offset, int len, int64* start, |
51 net::OldCompletionCallback* callback) OVERRIDE; | 46 net::OldCompletionCallback* callback) OVERRIDE; |
52 virtual bool CouldBeSparse() const OVERRIDE; | 47 virtual bool CouldBeSparse() const OVERRIDE; |
53 virtual void CancelSparseIO() OVERRIDE; | 48 virtual void CancelSparseIO() OVERRIDE; |
54 virtual int ReadyForSparseIO( | 49 virtual int ReadyForSparseIO( |
55 net::OldCompletionCallback* completion_callback) OVERRIDE; | 50 net::OldCompletionCallback* completion_callback) OVERRIDE; |
56 | 51 |
57 // Fail most subsequent requests. | 52 // Fail most subsequent requests. |
58 void set_fail_requests() { fail_requests_ = true; } | 53 void set_fail_requests() { fail_requests_ = true; } |
59 | 54 |
60 // If |value| is true, don't deliver any completion callbacks until called | 55 // If |value| is true, don't deliver any completion callbacks until called |
61 // again with |value| set to false. Caution: remember to enable callbacks | 56 // again with |value| set to false. Caution: remember to enable callbacks |
62 // again or all subsequent tests will fail. | 57 // again or all subsequent tests will fail. |
63 static void IgnoreCallbacks(bool value); | 58 static void IgnoreCallbacks(bool value); |
64 | 59 |
65 private: | 60 private: |
66 friend class base::RefCounted<MockDiskEntry>; | 61 friend class base::RefCounted<MockDiskEntry>; |
67 struct CallbackInfo; | 62 struct CallbackInfo; |
68 | 63 |
69 virtual ~MockDiskEntry(); | 64 virtual ~MockDiskEntry(); |
70 | 65 |
71 // Unlike the callbacks for MockHttpTransaction, we want this one to run even | 66 // Unlike the callbacks for MockHttpTransaction, we want this one to run even |
72 // if the consumer called Close on the MockDiskEntry. We achieve that by | 67 // if the consumer called Close on the MockDiskEntry. We achieve that by |
73 // leveraging the fact that this class is reference counted. | 68 // leveraging the fact that this class is reference counted. |
74 void CallbackLater(net::OldCompletionCallback* callback, int result); | 69 void CallbackLater(net::OldCompletionCallback* callback, int result); |
75 void CallbackLater(const net::CompletionCallback& callback, int result); | |
76 | 70 |
77 void RunOldCallback(net::OldCompletionCallback* callback, int result); | 71 void RunCallback(net::OldCompletionCallback* callback, int result); |
78 void RunCallback(const net::CompletionCallback& callback, int result); | |
79 | 72 |
80 // When |store| is true, stores the callback to be delivered later; otherwise | 73 // When |store| is true, stores the callback to be delivered later; otherwise |
81 // delivers any callback previously stored. | 74 // delivers any callback previously stored. |
82 static void StoreAndDeliverCallbacks(bool store, MockDiskEntry* entry, | 75 static void StoreAndDeliverCallbacks(bool store, MockDiskEntry* entry, |
83 net::OldCompletionCallback* old_callback, | 76 net::OldCompletionCallback* callback, |
84 const net::CompletionCallback& callback, | |
85 int result); | 77 int result); |
86 | 78 |
87 static const int kNumCacheEntryDataIndices = 3; | 79 static const int kNumCacheEntryDataIndices = 3; |
88 | 80 |
89 std::string key_; | 81 std::string key_; |
90 std::vector<char> data_[kNumCacheEntryDataIndices]; | 82 std::vector<char> data_[kNumCacheEntryDataIndices]; |
91 int test_mode_; | 83 int test_mode_; |
92 bool doomed_; | 84 bool doomed_; |
93 bool sparse_; | 85 bool sparse_; |
94 bool fail_requests_; | 86 bool fail_requests_; |
95 bool busy_; | 87 bool busy_; |
96 bool delayed_; | 88 bool delayed_; |
97 static bool cancel_; | 89 static bool cancel_; |
98 static bool ignore_callbacks_; | 90 static bool ignore_callbacks_; |
99 }; | 91 }; |
100 | 92 |
101 class MockDiskCache : public disk_cache::Backend { | 93 class MockDiskCache : public disk_cache::Backend { |
102 public: | 94 public: |
103 MockDiskCache(); | 95 MockDiskCache(); |
104 virtual ~MockDiskCache(); | 96 virtual ~MockDiskCache(); |
105 | 97 |
106 virtual int32 GetEntryCount() const OVERRIDE; | 98 virtual int32 GetEntryCount() const OVERRIDE; |
107 virtual int OpenEntry(const std::string& key, disk_cache::Entry** entry, | 99 virtual int OpenEntry(const std::string& key, disk_cache::Entry** entry, |
108 net::OldCompletionCallback* callback) OVERRIDE; | 100 net::OldCompletionCallback* callback) OVERRIDE; |
109 virtual int OpenEntry(const std::string& key, disk_cache::Entry** entry, | |
110 const net::CompletionCallback& callback) OVERRIDE; | |
111 virtual int CreateEntry(const std::string& key, disk_cache::Entry** entry, | 101 virtual int CreateEntry(const std::string& key, disk_cache::Entry** entry, |
112 net::OldCompletionCallback* callback) OVERRIDE; | 102 net::OldCompletionCallback* callback) OVERRIDE; |
113 virtual int CreateEntry(const std::string& key, disk_cache::Entry** entry, | |
114 const net::CompletionCallback& callback) OVERRIDE; | |
115 virtual int DoomEntry(const std::string& key, | 103 virtual int DoomEntry(const std::string& key, |
116 net::OldCompletionCallback* callback) OVERRIDE; | 104 net::OldCompletionCallback* callback) OVERRIDE; |
117 virtual int DoomAllEntries(net::OldCompletionCallback* callback) OVERRIDE; | 105 virtual int DoomAllEntries(net::OldCompletionCallback* callback) OVERRIDE; |
118 virtual int DoomAllEntries(const net::CompletionCallback& callback) OVERRIDE; | |
119 virtual int DoomEntriesBetween(const base::Time initial_time, | 106 virtual int DoomEntriesBetween(const base::Time initial_time, |
120 const base::Time end_time, | 107 const base::Time end_time, |
121 net::OldCompletionCallback* callback) OVERRIDE; | 108 net::OldCompletionCallback* callback) OVERRIDE; |
122 virtual int DoomEntriesBetween( | |
123 const base::Time initial_time, | |
124 const base::Time end_time, | |
125 const net::CompletionCallback& callback) OVERRIDE; | |
126 virtual int DoomEntriesSince(const base::Time initial_time, | 109 virtual int DoomEntriesSince(const base::Time initial_time, |
127 net::OldCompletionCallback* callback) OVERRIDE; | 110 net::OldCompletionCallback* callback) OVERRIDE; |
128 virtual int OpenNextEntry(void** iter, disk_cache::Entry** next_entry, | 111 virtual int OpenNextEntry(void** iter, disk_cache::Entry** next_entry, |
129 net::OldCompletionCallback* callback) OVERRIDE; | 112 net::OldCompletionCallback* callback) OVERRIDE; |
130 virtual int OpenNextEntry(void** iter, disk_cache::Entry** next_entry, | |
131 const net::CompletionCallback& callback) OVERRIDE; | |
132 virtual void EndEnumeration(void** iter) OVERRIDE; | 113 virtual void EndEnumeration(void** iter) OVERRIDE; |
133 virtual void GetStats( | 114 virtual void GetStats( |
134 std::vector<std::pair<std::string, std::string> >* stats) OVERRIDE; | 115 std::vector<std::pair<std::string, std::string> >* stats) OVERRIDE; |
135 virtual void OnExternalCacheHit(const std::string& key) OVERRIDE; | 116 virtual void OnExternalCacheHit(const std::string& key) OVERRIDE; |
136 | 117 |
137 // Returns number of times a cache entry was successfully opened. | 118 // Returns number of times a cache entry was successfully opened. |
138 int open_count() const { return open_count_; } | 119 int open_count() const { return open_count_; } |
139 | 120 |
140 // Returns number of times a cache entry was successfully created. | 121 // Returns number of times a cache entry was successfully created. |
141 int create_count() const { return create_count_; } | 122 int create_count() const { return create_count_; } |
142 | 123 |
143 // Fail any subsequent CreateEntry and OpenEntry. | 124 // Fail any subsequent CreateEntry and OpenEntry. |
144 void set_fail_requests() { fail_requests_ = true; } | 125 void set_fail_requests() { fail_requests_ = true; } |
145 | 126 |
146 // Return entries that fail some of their requests. | 127 // Return entries that fail some of their requests. |
147 void set_soft_failures(bool value) { soft_failures_ = value; } | 128 void set_soft_failures(bool value) { soft_failures_ = value; } |
148 | 129 |
149 // Makes sure that CreateEntry is not called twice for a given key. | 130 // Makes sure that CreateEntry is not called twice for a given key. |
150 void set_double_create_check(bool value) { double_create_check_ = value; } | 131 void set_double_create_check(bool value) { double_create_check_ = value; } |
151 | 132 |
152 void ReleaseAll(); | 133 void ReleaseAll(); |
153 | 134 |
154 private: | 135 private: |
155 typedef base::hash_map<std::string, MockDiskEntry*> EntryMap; | 136 typedef base::hash_map<std::string, MockDiskEntry*> EntryMap; |
| 137 class CallbackRunner; |
156 | 138 |
157 void CallbackLater(net::OldCompletionCallback* callback, int result); | 139 void CallbackLater(net::OldCompletionCallback* callback, int result); |
158 void CallbackLater(const net::CompletionCallback& callback, int result); | |
159 | 140 |
160 EntryMap entries_; | 141 EntryMap entries_; |
161 int open_count_; | 142 int open_count_; |
162 int create_count_; | 143 int create_count_; |
163 bool fail_requests_; | 144 bool fail_requests_; |
164 bool soft_failures_; | 145 bool soft_failures_; |
165 bool double_create_check_; | 146 bool double_create_check_; |
166 }; | 147 }; |
167 | 148 |
168 class MockBackendFactory : public net::HttpCache::BackendFactory { | 149 class MockBackendFactory : public net::HttpCache::BackendFactory { |
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
247 private: | 228 private: |
248 int Result() { return fail_ ? net::ERR_FAILED : net::OK; } | 229 int Result() { return fail_ ? net::ERR_FAILED : net::OK; } |
249 | 230 |
250 disk_cache::Backend** backend_; | 231 disk_cache::Backend** backend_; |
251 net::OldCompletionCallback* callback_; | 232 net::OldCompletionCallback* callback_; |
252 bool block_; | 233 bool block_; |
253 bool fail_; | 234 bool fail_; |
254 }; | 235 }; |
255 | 236 |
256 #endif // NET_HTTP_MOCK_HTTP_CACHE_H_ | 237 #endif // NET_HTTP_MOCK_HTTP_CACHE_H_ |
OLD | NEW |