OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include "net/base/upload_disk_cache_entry_element_reader.h" | |
6 | |
7 #include <stdint.h> | |
8 | |
9 #include <algorithm> | |
10 #include <string> | |
11 #include <vector> | |
12 | |
13 #include "base/basictypes.h" | |
14 #include "base/bind.h" | |
15 #include "base/callback.h" | |
16 #include "base/macros.h" | |
17 #include "base/memory/ref_counted.h" | |
18 #include "base/memory/scoped_ptr.h" | |
19 #include "base/time/time.h" | |
20 #include "net/base/io_buffer.h" | |
21 #include "net/base/net_errors.h" | |
22 #include "net/base/test_completion_callback.h" | |
23 #include "net/disk_cache/disk_cache.h" | |
24 #include "testing/gtest/include/gtest/gtest.h" | |
25 #include "testing/platform_test.h" | |
26 | |
27 namespace net { | |
28 namespace { | |
29 | |
30 const int kTestDiskCacheStreamIndex = 0; | |
31 | |
32 const char kDataKey[] = "a key"; | |
33 | |
34 const char kData[] = "this is data in a disk cache entry"; | |
35 const size_t kDataSize = arraysize(kData) - 1; | |
36 | |
37 // A disk_cache::Entry that arbitrarily delays the completion of a read | |
38 // operation to allow testing some races without flake. This is particularly | |
39 // relevant in this unit test, which uses the always-synchronous MEMORY_CACHE. | |
40 class DelayedReadEntry : public disk_cache::Entry { | |
41 public: | |
42 explicit DelayedReadEntry(disk_cache::ScopedEntryPtr entry) | |
43 : entry_(entry.Pass()) {} | |
44 ~DelayedReadEntry() override { EXPECT_FALSE(HasPendingReadCallbacks()); } | |
45 | |
46 bool HasPendingReadCallbacks() { return !pending_read_callbacks_.empty(); } | |
47 | |
48 void RunPendingReadCallbacks() { | |
49 std::vector<base::Callback<void(void)>> callbacks; | |
50 pending_read_callbacks_.swap(callbacks); | |
51 for (const auto& callback : callbacks) | |
52 callback.Run(); | |
53 } | |
54 | |
55 // From disk_cache::Entry: | |
56 void Doom() override { entry_->Doom(); } | |
57 void Close() override { delete this; } // Note this is required by the API. | |
58 std::string GetKey() const override { return entry_->GetKey(); } | |
59 base::Time GetLastUsed() const override { return entry_->GetLastUsed(); } | |
60 base::Time GetLastModified() const override { | |
61 return entry_->GetLastModified(); | |
62 } | |
63 int32 GetDataSize(int index) const override { | |
64 return entry_->GetDataSize(index); | |
65 } | |
mmenke
2015/06/17 19:11:16
optioanl: Suggest most of these be separated by b
gavinp
2015/06/18 18:50:55
Done.
| |
66 int ReadData(int index, | |
67 int offset, | |
68 IOBuffer* buf, | |
69 int buf_len, | |
70 const CompletionCallback& original_callback) override { | |
71 TestCompletionCallback callback; | |
72 int rv = entry_->ReadData(index, offset, buf, buf_len, callback.callback()); | |
73 DCHECK_NE(rv, ERR_IO_PENDING) | |
mmenke
2015/06/17 19:11:16
nit: include base/logging.h
gavinp
2015/06/18 18:50:55
Done.
| |
74 << "Test expects to use a MEMORY_CACHE instance, which is synchronous."; | |
75 pending_read_callbacks_.push_back(base::Bind(original_callback, rv)); | |
76 return ERR_IO_PENDING; | |
77 } | |
78 int WriteData(int index, | |
79 int offset, | |
80 IOBuffer* buf, | |
81 int buf_len, | |
82 const CompletionCallback& callback, | |
83 bool truncate) override { | |
84 return entry_->WriteData(index, offset, buf, buf_len, callback, truncate); | |
85 } | |
86 int ReadSparseData(int64 offset, | |
87 IOBuffer* buf, | |
88 int buf_len, | |
89 const CompletionCallback& callback) override { | |
90 return entry_->ReadSparseData(offset, buf, buf_len, callback); | |
91 } | |
92 int WriteSparseData(int64 offset, | |
93 IOBuffer* buf, | |
94 int buf_len, | |
95 const CompletionCallback& callback) override { | |
96 return entry_->WriteSparseData(offset, buf, buf_len, callback); | |
97 } | |
98 int GetAvailableRange(int64 offset, | |
99 int len, | |
100 int64* start, | |
101 const CompletionCallback& callback) override { | |
102 return entry_->GetAvailableRange(offset, len, start, callback); | |
103 } | |
104 bool CouldBeSparse() const override { return entry_->CouldBeSparse(); } | |
105 void CancelSparseIO() override { entry_->CancelSparseIO(); } | |
106 int ReadyForSparseIO(const CompletionCallback& callback) override { | |
107 return ReadyForSparseIO(callback); | |
108 } | |
109 | |
110 private: | |
111 disk_cache::ScopedEntryPtr entry_; | |
112 std::vector<base::Callback<void(void)>> pending_read_callbacks_; | |
113 }; | |
114 | |
115 class UploadDiskCacheEntryElementReaderTest : public PlatformTest { | |
116 public: | |
117 UploadDiskCacheEntryElementReaderTest() {} | |
118 | |
119 ~UploadDiskCacheEntryElementReaderTest() override {} | |
120 | |
121 void SetUp() override { | |
122 TestCompletionCallback callback; | |
123 int rv = disk_cache::CreateCacheBackend(MEMORY_CACHE, CACHE_BACKEND_DEFAULT, | |
124 base::FilePath(), 0, false, NULL, | |
125 NULL, &cache_, callback.callback()); | |
126 ASSERT_EQ(OK, callback.GetResult(rv)); | |
127 | |
128 disk_cache::Entry* tmp_entry = nullptr; | |
129 rv = cache_->CreateEntry(kDataKey, &tmp_entry, callback.callback()); | |
130 ASSERT_EQ(OK, callback.GetResult(rv)); | |
131 entry_.reset(tmp_entry); | |
132 | |
133 scoped_refptr<IOBuffer> io_buffer = new WrappedIOBuffer(kData); | |
134 rv = entry_->WriteData(kTestDiskCacheStreamIndex, 0, io_buffer.get(), | |
135 kDataSize, callback.callback(), false); | |
136 EXPECT_EQ(static_cast<int>(kDataSize), callback.GetResult(rv)); | |
137 } | |
138 | |
139 void set_entry(disk_cache::ScopedEntryPtr entry) { entry_.swap(entry); } | |
140 disk_cache::Entry* entry() { return entry_.get(); } | |
141 disk_cache::ScopedEntryPtr release_entry() { return entry_.Pass(); } | |
142 | |
143 private: | |
144 scoped_ptr<disk_cache::Backend> cache_; | |
145 disk_cache::ScopedEntryPtr entry_; | |
146 }; | |
147 | |
148 TEST_F(UploadDiskCacheEntryElementReaderTest, ReadAll) { | |
149 UploadDiskCacheEntryElementReader reader(entry(), kTestDiskCacheStreamIndex, | |
150 0, kDataSize); | |
151 EXPECT_EQ(static_cast<uint64_t>(kDataSize), reader.BytesRemaining()); | |
152 | |
153 char read_buffer[kDataSize]; | |
154 std::fill(read_buffer, read_buffer + arraysize(read_buffer), '\0'); | |
155 | |
156 scoped_refptr<IOBuffer> io_buffer = new WrappedIOBuffer(read_buffer); | |
157 TestCompletionCallback callback; | |
158 int rv = reader.Read(io_buffer.get(), kDataSize, callback.callback()); | |
159 EXPECT_EQ(static_cast<int>(kDataSize), callback.GetResult(rv)); | |
160 EXPECT_EQ(0U, reader.BytesRemaining()) | |
161 << "Expected a single read of |kDataSize| to retrieve entire entry."; | |
162 EXPECT_EQ(std::string(kData, kDataSize), std::string(read_buffer, kDataSize)); | |
163 } | |
164 | |
165 TEST_F(UploadDiskCacheEntryElementReaderTest, ReadPartially) { | |
166 UploadDiskCacheEntryElementReader reader(entry(), kTestDiskCacheStreamIndex, | |
167 0, kDataSize); | |
168 EXPECT_EQ(static_cast<uint64_t>(kDataSize), reader.BytesRemaining()); | |
169 | |
170 char read_buffer1[kDataSize]; | |
171 std::fill(read_buffer1, read_buffer1 + arraysize(read_buffer1), '\0'); | |
172 | |
173 const size_t kIOBuffer1Size = kDataSize / 3; | |
174 scoped_refptr<IOBuffer> io_buffer1 = new WrappedIOBuffer(read_buffer1); | |
175 | |
176 char read_buffer2[kDataSize]; | |
177 const size_t kIOBuffer2Size = kDataSize - kIOBuffer1Size; | |
178 scoped_refptr<IOBuffer> io_buffer2 = new WrappedIOBuffer(read_buffer2); | |
179 | |
180 TestCompletionCallback callback; | |
181 int rv = reader.Read(io_buffer1.get(), kIOBuffer1Size, callback.callback()); | |
182 EXPECT_EQ(static_cast<int>(kIOBuffer1Size), callback.GetResult(rv)); | |
183 EXPECT_EQ(static_cast<uint64_t>(kIOBuffer2Size), reader.BytesRemaining()); | |
184 | |
185 rv = reader.Read(io_buffer2.get(), kIOBuffer2Size, callback.callback()); | |
186 EXPECT_EQ(static_cast<int>(kIOBuffer2Size), callback.GetResult(rv)); | |
187 EXPECT_EQ(0U, reader.BytesRemaining()); | |
mmenke
2015/06/17 19:11:16
Should we have some check that we don't write beyo
gavinp
2015/06/18 18:50:55
Aha, I now understand that earlier comment.
Done,
| |
188 | |
189 EXPECT_EQ(std::string(kData, kDataSize), | |
190 std::string(read_buffer1, kIOBuffer1Size) + | |
191 std::string(read_buffer2, kIOBuffer2Size)); | |
192 } | |
193 | |
194 TEST_F(UploadDiskCacheEntryElementReaderTest, ReadTooMuch) { | |
195 UploadDiskCacheEntryElementReader reader(entry(), kTestDiskCacheStreamIndex, | |
196 0, kDataSize); | |
197 EXPECT_EQ(static_cast<uint64_t>(kDataSize), reader.BytesRemaining()); | |
198 | |
199 const size_t kTooLargeSize = kDataSize + kDataSize / 2; | |
200 | |
201 char read_buffer[kTooLargeSize]; | |
202 std::fill(read_buffer, read_buffer + arraysize(read_buffer), '\0'); | |
203 | |
204 scoped_refptr<IOBuffer> io_buffer = new WrappedIOBuffer(read_buffer); | |
205 TestCompletionCallback callback; | |
206 int rv = reader.Read(io_buffer.get(), kTooLargeSize, callback.callback()); | |
207 EXPECT_EQ(static_cast<int>(kDataSize), callback.GetResult(rv)); | |
208 EXPECT_EQ(0U, reader.BytesRemaining()); | |
209 EXPECT_EQ(std::string(kData, kDataSize), std::string(read_buffer, kDataSize)); | |
210 } | |
211 | |
212 TEST_F(UploadDiskCacheEntryElementReaderTest, ReadAsync) { | |
213 DelayedReadEntry* delayed_read_entry = new DelayedReadEntry(release_entry()); | |
214 set_entry(disk_cache::ScopedEntryPtr(delayed_read_entry)); | |
215 | |
216 UploadDiskCacheEntryElementReader reader(entry(), kTestDiskCacheStreamIndex, | |
217 0, kDataSize); | |
218 | |
219 char read_buffer[kDataSize]; | |
220 std::fill(read_buffer, read_buffer + arraysize(read_buffer), '\0'); | |
221 | |
222 scoped_refptr<IOBuffer> io_buffer = new WrappedIOBuffer(read_buffer); | |
223 TestCompletionCallback callback; | |
224 int rv = reader.Read(io_buffer.get(), kDataSize, callback.callback()); | |
225 EXPECT_EQ(ERR_IO_PENDING, rv); | |
226 EXPECT_TRUE(delayed_read_entry->HasPendingReadCallbacks()); | |
227 EXPECT_EQ(static_cast<uint64_t>(kDataSize), reader.BytesRemaining()); | |
228 | |
229 delayed_read_entry->RunPendingReadCallbacks(); | |
230 EXPECT_EQ(static_cast<int>(kDataSize), callback.GetResult(rv)); | |
231 EXPECT_EQ(0U, reader.BytesRemaining()) | |
232 << "Expected a single read of |kDataSize| to retrieve entire entry."; | |
233 EXPECT_EQ(std::string(kData, kDataSize), std::string(read_buffer, kDataSize)); | |
234 } | |
235 | |
236 TEST_F(UploadDiskCacheEntryElementReaderTest, MultipleInit) { | |
237 UploadDiskCacheEntryElementReader reader(entry(), kTestDiskCacheStreamIndex, | |
238 0, kDataSize); | |
239 char read_buffer[kDataSize]; | |
240 std::fill(read_buffer, read_buffer + arraysize(read_buffer), '\0'); | |
241 | |
242 scoped_refptr<IOBuffer> io_buffer = new WrappedIOBuffer(read_buffer); | |
243 TestCompletionCallback callback; | |
244 int rv = reader.Read(io_buffer.get(), kDataSize, callback.callback()); | |
245 EXPECT_EQ(static_cast<int>(kDataSize), callback.GetResult(rv)); | |
246 EXPECT_EQ(std::string(kData, kDataSize), std::string(read_buffer, kDataSize)); | |
247 | |
248 rv = reader.Init(callback.callback()); | |
249 EXPECT_EQ(OK, callback.GetResult(rv)); | |
250 EXPECT_EQ(static_cast<uint64_t>(kDataSize), reader.BytesRemaining()); | |
251 rv = reader.Read(io_buffer.get(), kDataSize, callback.callback()); | |
252 EXPECT_EQ(static_cast<int>(kDataSize), callback.GetResult(rv)); | |
253 EXPECT_EQ(std::string(kData, kDataSize), std::string(read_buffer, kDataSize)); | |
254 } | |
255 | |
256 TEST_F(UploadDiskCacheEntryElementReaderTest, InitDuringAsyncOperation) { | |
257 DelayedReadEntry* delayed_read_entry = new DelayedReadEntry(release_entry()); | |
258 set_entry(disk_cache::ScopedEntryPtr(delayed_read_entry)); | |
259 | |
260 UploadDiskCacheEntryElementReader reader(entry(), kTestDiskCacheStreamIndex, | |
261 0, kDataSize); | |
262 char read_buffer[kDataSize]; | |
263 std::fill(read_buffer, read_buffer + arraysize(read_buffer), '\0'); | |
264 | |
265 scoped_refptr<IOBuffer> io_buffer = new WrappedIOBuffer(read_buffer); | |
266 TestCompletionCallback read_callback; | |
267 int rv = reader.Read(io_buffer.get(), kDataSize, read_callback.callback()); | |
268 EXPECT_EQ(ERR_IO_PENDING, rv); | |
269 EXPECT_TRUE(delayed_read_entry->HasPendingReadCallbacks()); | |
270 EXPECT_EQ(static_cast<uint64_t>(kDataSize), reader.BytesRemaining()); | |
271 | |
272 TestCompletionCallback init_callback; | |
273 rv = reader.Init(init_callback.callback()); | |
274 EXPECT_EQ(OK, init_callback.GetResult(rv)); | |
275 | |
276 delayed_read_entry->RunPendingReadCallbacks(); | |
277 EXPECT_FALSE(delayed_read_entry->HasPendingReadCallbacks()); | |
278 EXPECT_EQ(static_cast<uint64_t>(kDataSize), reader.BytesRemaining()); | |
279 | |
280 char read_buffer2[kDataSize]; | |
281 std::fill(read_buffer2, read_buffer2 + arraysize(read_buffer2), '\0'); | |
282 scoped_refptr<IOBuffer> io_buffer2 = new WrappedIOBuffer(read_buffer2); | |
283 TestCompletionCallback read_callback2; | |
284 rv = reader.Read(io_buffer2.get(), kDataSize, read_callback2.callback()); | |
285 EXPECT_EQ(ERR_IO_PENDING, rv); | |
286 EXPECT_TRUE(delayed_read_entry->HasPendingReadCallbacks()); | |
287 EXPECT_EQ(static_cast<uint64_t>(kDataSize), reader.BytesRemaining()); | |
288 | |
289 delayed_read_entry->RunPendingReadCallbacks(); | |
290 EXPECT_FALSE(delayed_read_entry->HasPendingReadCallbacks()); | |
291 read_callback2.WaitForResult(); // Succeeds if this does not deadlock. | |
292 EXPECT_EQ(std::string(kData, kDataSize), | |
293 std::string(read_buffer2, kDataSize)); | |
294 } | |
295 | |
296 TEST_F(UploadDiskCacheEntryElementReaderTest, Range) { | |
mmenke
2015/06/17 19:11:16
Should we also test a 0-byte range? Or could DCHE
gavinp
2015/06/18 18:50:55
I used the DCHECK option.
| |
297 const size_t kOffset = kDataSize / 4; | |
298 const size_t kLength = kDataSize / 3; | |
299 | |
300 UploadDiskCacheEntryElementReader reader(entry(), kTestDiskCacheStreamIndex, | |
301 kOffset, kLength); | |
302 EXPECT_EQ(static_cast<uint64_t>(kLength), reader.BytesRemaining()); | |
303 | |
304 char read_buffer[kLength]; | |
305 std::fill(read_buffer, read_buffer + arraysize(read_buffer), '\0'); | |
306 | |
307 scoped_refptr<IOBuffer> io_buffer = new WrappedIOBuffer(read_buffer); | |
308 TestCompletionCallback callback; | |
309 int rv = reader.Read(io_buffer.get(), kLength, callback.callback()); | |
310 EXPECT_EQ(static_cast<int>(kLength), callback.GetResult(rv)); | |
311 EXPECT_EQ(0U, reader.BytesRemaining()); | |
312 EXPECT_EQ(std::string(kData + kOffset, kLength), | |
313 std::string(read_buffer, kLength)); | |
314 } | |
315 | |
316 } // namespace | |
317 } // namespace net | |
OLD | NEW |