OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 #include "net/base/elements_upload_data_stream.h" | 5 #include "net/base/elements_upload_data_stream.h" |
6 | 6 |
7 #include <stdint.h> | 7 #include <stdint.h> |
8 | 8 |
9 #include <algorithm> | 9 #include <algorithm> |
10 #include <limits> | 10 #include <limits> |
(...skipping 214 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
225 std::numeric_limits<uint64_t>::max(), base::Time()))); | 225 std::numeric_limits<uint64_t>::max(), base::Time()))); |
226 | 226 |
227 TestCompletionCallback init_callback; | 227 TestCompletionCallback init_callback; |
228 std::unique_ptr<UploadDataStream> stream( | 228 std::unique_ptr<UploadDataStream> stream( |
229 new ElementsUploadDataStream(std::move(element_readers_), 0)); | 229 new ElementsUploadDataStream(std::move(element_readers_), 0)); |
230 ASSERT_EQ(ERR_IO_PENDING, stream->Init(init_callback.callback())); | 230 ASSERT_EQ(ERR_IO_PENDING, stream->Init(init_callback.callback())); |
231 ASSERT_EQ(OK, init_callback.WaitForResult()); | 231 ASSERT_EQ(OK, init_callback.WaitForResult()); |
232 EXPECT_FALSE(stream->IsInMemory()); | 232 EXPECT_FALSE(stream->IsInMemory()); |
233 EXPECT_EQ(kFakeSize, stream->size()); | 233 EXPECT_EQ(kFakeSize, stream->size()); |
234 EXPECT_EQ(0U, stream->position()); | 234 EXPECT_EQ(0U, stream->position()); |
| 235 |
| 236 scoped_refptr<IOBuffer> buf = new IOBuffer(kTestBufferSize); |
235 EXPECT_FALSE(stream->IsEOF()); | 237 EXPECT_FALSE(stream->IsEOF()); |
236 uint64_t read_counter = 0; | 238 |
237 scoped_refptr<IOBuffer> buf = new IOBuffer(kTestBufferSize); | 239 TestCompletionCallback read_callback; |
238 while (!stream->IsEOF()) { | 240 ASSERT_EQ(ERR_IO_PENDING, |
239 TestCompletionCallback read_callback; | 241 stream->Read(buf.get(), kTestBufferSize, read_callback.callback())); |
240 ASSERT_EQ( | 242 int bytes_read = read_callback.WaitForResult(); |
241 ERR_IO_PENDING, | 243 |
242 stream->Read(buf.get(), kTestBufferSize, read_callback.callback())); | 244 // UpdateDataStream will return error if there is something wrong. |
243 int bytes_read = read_callback.WaitForResult(); | 245 // Thus, |bytes_read| is actual error. |
244 ASSERT_LE(0, bytes_read); // Not an error. | 246 EXPECT_EQ(ERR_UPLOAD_FILE_CHANGED, bytes_read); |
245 read_counter += bytes_read; | 247 EXPECT_EQ(0U, stream->position()); |
246 EXPECT_EQ(read_counter, stream->position()); | 248 EXPECT_FALSE(stream->IsEOF()); |
247 } | |
248 // UpdateDataStream will pad out the file with 0 bytes so that the HTTP | |
249 // transaction doesn't hang. Therefore we expected the full size. | |
250 EXPECT_EQ(kFakeSize, read_counter); | |
251 EXPECT_EQ(read_counter, stream->position()); | |
252 } | 249 } |
253 | 250 |
254 TEST_F(ElementsUploadDataStreamTest, ReadErrorSync) { | 251 TEST_F(ElementsUploadDataStreamTest, ReadErrorSync) { |
255 // This element cannot be read. | 252 // This element cannot be read. |
256 std::unique_ptr<MockUploadElementReader> reader( | 253 std::unique_ptr<MockUploadElementReader> reader( |
257 new MockUploadElementReader(kTestDataSize, true)); | 254 new MockUploadElementReader(kTestDataSize, true)); |
258 EXPECT_CALL(*reader, Init(_)).WillOnce(Return(OK)); | 255 EXPECT_CALL(*reader, Init(_)).WillOnce(Return(OK)); |
259 reader->SetReadExpectation(ERR_FAILED); | 256 reader->SetReadExpectation(ERR_FAILED); |
260 element_readers_.push_back(std::move(reader)); | 257 element_readers_.push_back(std::move(reader)); |
261 | 258 |
262 // This element is ignored because of the error from the previous reader. | 259 // This element is ignored because of the error from the previous reader. |
263 element_readers_.push_back( | 260 element_readers_.push_back( |
264 base::WrapUnique(new UploadBytesElementReader(kTestData, kTestDataSize))); | 261 base::WrapUnique(new UploadBytesElementReader(kTestData, kTestDataSize))); |
265 | 262 |
266 std::unique_ptr<UploadDataStream> stream( | 263 std::unique_ptr<UploadDataStream> stream( |
267 new ElementsUploadDataStream(std::move(element_readers_), 0)); | 264 new ElementsUploadDataStream(std::move(element_readers_), 0)); |
268 | 265 |
269 // Run Init(). | 266 // Run Init(). |
270 ASSERT_EQ(OK, stream->Init(CompletionCallback())); | 267 ASSERT_EQ(OK, stream->Init(CompletionCallback())); |
271 EXPECT_EQ(kTestDataSize*2, stream->size()); | 268 EXPECT_EQ(kTestDataSize*2, stream->size()); |
272 EXPECT_EQ(0U, stream->position()); | 269 EXPECT_EQ(0U, stream->position()); |
273 EXPECT_FALSE(stream->IsEOF()); | 270 EXPECT_FALSE(stream->IsEOF()); |
274 | 271 |
275 // Prepare a buffer filled with non-zero data. | 272 // Prepare a buffer filled with non-zero data. |
276 scoped_refptr<IOBuffer> buf = new IOBuffer(kTestBufferSize); | 273 scoped_refptr<IOBuffer> buf = new IOBuffer(kTestBufferSize); |
277 std::fill_n(buf->data(), kTestBufferSize, -1); | 274 std::fill_n(buf->data(), kTestBufferSize, -1); |
278 | 275 |
279 // Read() results in success even when the reader returns error. | 276 // Read() results in success even when the reader returns error. |
280 EXPECT_EQ(static_cast<int>(kTestDataSize * 2), | 277 EXPECT_EQ(ERR_FAILED, |
281 stream->Read(buf.get(), kTestBufferSize, CompletionCallback())); | 278 stream->Read(buf.get(), kTestBufferSize, CompletionCallback())); |
282 EXPECT_EQ(kTestDataSize * 2, stream->position()); | 279 EXPECT_EQ(0U, stream->position()); |
283 EXPECT_TRUE(stream->IsEOF()); | 280 EXPECT_FALSE(stream->IsEOF()); |
284 | 281 |
285 // The buffer is filled with zero. | 282 // The buffer is filled with zero. |
286 EXPECT_EQ(static_cast<int>(kTestDataSize*2), | 283 EXPECT_EQ(0, std::count(buf->data(), buf->data() + kTestBufferSize, 0)); |
287 std::count(buf->data(), buf->data() + kTestBufferSize, 0)); | |
288 } | 284 } |
289 | 285 |
290 TEST_F(ElementsUploadDataStreamTest, ReadErrorAsync) { | 286 TEST_F(ElementsUploadDataStreamTest, ReadErrorAsync) { |
291 // This element cannot be read. | 287 // This element cannot be read. |
292 std::unique_ptr<MockUploadElementReader> reader( | 288 std::unique_ptr<MockUploadElementReader> reader( |
293 new MockUploadElementReader(kTestDataSize, false)); | 289 new MockUploadElementReader(kTestDataSize, false)); |
294 reader->SetAsyncInitExpectation(OK); | 290 reader->SetAsyncInitExpectation(OK); |
295 reader->SetReadExpectation(ERR_FAILED); | 291 reader->SetReadExpectation(ERR_FAILED); |
296 element_readers_.push_back(std::move(reader)); | 292 element_readers_.push_back(std::move(reader)); |
297 | 293 |
(...skipping 13 matching lines...) Expand all Loading... |
311 EXPECT_FALSE(stream->IsEOF()); | 307 EXPECT_FALSE(stream->IsEOF()); |
312 | 308 |
313 // Prepare a buffer filled with non-zero data. | 309 // Prepare a buffer filled with non-zero data. |
314 scoped_refptr<IOBuffer> buf = new IOBuffer(kTestBufferSize); | 310 scoped_refptr<IOBuffer> buf = new IOBuffer(kTestBufferSize); |
315 std::fill_n(buf->data(), kTestBufferSize, -1); | 311 std::fill_n(buf->data(), kTestBufferSize, -1); |
316 | 312 |
317 // Read() results in success even when the reader returns error. | 313 // Read() results in success even when the reader returns error. |
318 TestCompletionCallback read_callback; | 314 TestCompletionCallback read_callback; |
319 ASSERT_EQ(ERR_IO_PENDING, | 315 ASSERT_EQ(ERR_IO_PENDING, |
320 stream->Read(buf.get(), kTestBufferSize, read_callback.callback())); | 316 stream->Read(buf.get(), kTestBufferSize, read_callback.callback())); |
321 EXPECT_EQ(static_cast<int>(kTestDataSize * 2), read_callback.WaitForResult()); | 317 EXPECT_EQ(ERR_FAILED, read_callback.WaitForResult()); |
322 EXPECT_EQ(kTestDataSize*2, stream->position()); | 318 EXPECT_EQ(0U, stream->position()); |
323 EXPECT_TRUE(stream->IsEOF()); | 319 EXPECT_FALSE(stream->IsEOF()); |
324 | 320 |
325 // The buffer is filled with zero. | 321 // The buffer is empty |
326 EXPECT_EQ(static_cast<int>(kTestDataSize*2), | 322 EXPECT_EQ(0, std::count(buf->data(), buf->data() + kTestBufferSize, 0)); |
327 std::count(buf->data(), buf->data() + kTestBufferSize, 0)); | |
328 } | 323 } |
329 | 324 |
330 TEST_F(ElementsUploadDataStreamTest, FileAndBytes) { | 325 TEST_F(ElementsUploadDataStreamTest, FileAndBytes) { |
331 base::FilePath temp_file_path; | 326 base::FilePath temp_file_path; |
332 ASSERT_TRUE(base::CreateTemporaryFileInDir(temp_dir_.path(), | 327 ASSERT_TRUE(base::CreateTemporaryFileInDir(temp_dir_.path(), |
333 &temp_file_path)); | 328 &temp_file_path)); |
334 ASSERT_EQ(static_cast<int>(kTestDataSize), | 329 ASSERT_EQ(static_cast<int>(kTestDataSize), |
335 base::WriteFile(temp_file_path, kTestData, kTestDataSize)); | 330 base::WriteFile(temp_file_path, kTestData, kTestDataSize)); |
336 | 331 |
337 const uint64_t kFileRangeOffset = 1; | 332 const uint64_t kFileRangeOffset = 1; |
(...skipping 459 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
797 wrapped_buffer2.get(), buf2.size(), read_callback2.callback())); | 792 wrapped_buffer2.get(), buf2.size(), read_callback2.callback())); |
798 EXPECT_EQ(static_cast<int>(buf2.size()), read_callback2.WaitForResult()); | 793 EXPECT_EQ(static_cast<int>(buf2.size()), read_callback2.WaitForResult()); |
799 EXPECT_EQ(expected_data, buf2); | 794 EXPECT_EQ(expected_data, buf2); |
800 EXPECT_TRUE(stream->IsEOF()); | 795 EXPECT_TRUE(stream->IsEOF()); |
801 | 796 |
802 // Make sure callbacks are not called for cancelled operations. | 797 // Make sure callbacks are not called for cancelled operations. |
803 EXPECT_FALSE(read_callback1.have_result()); | 798 EXPECT_FALSE(read_callback1.have_result()); |
804 } | 799 } |
805 | 800 |
806 } // namespace net | 801 } // namespace net |
OLD | NEW |