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
3 // found in the LICENSE file.
5 #include "net/base/upload_data_stream.h"
10 #include "base/basictypes.h"
11 #include "base/bind.h"
12 #include "base/file_util.h"
13 #include "base/files/file_path.h"
14 #include "base/files/scoped_temp_dir.h"
15 #include "base/memory/scoped_ptr.h"
16 #include "base/message_loop/message_loop.h"
17 #include "base/time/time.h"
18 #include "net/base/io_buffer.h"
19 #include "net/base/net_errors.h"
20 #include "net/base/test_completion_callback.h"
21 #include "net/base/upload_bytes_element_reader.h"
22 #include "net/base/upload_file_element_reader.h"
23 #include "testing/gmock/include/gmock/gmock.h"
24 #include "testing/gtest/include/gtest/gtest.h"
25 #include "testing/platform_test.h"
27 using ::testing::DoAll
;
28 using ::testing::Invoke
;
29 using ::testing::Return
;
36 const char kTestData
[] = "0123456789";
37 const size_t kTestDataSize
= arraysize(kTestData
) - 1;
38 const size_t kTestBufferSize
= 1 << 14; // 16KB.
40 // Reads data from the upload data stream, and returns the data as string.
41 std::string
ReadFromUploadDataStream(UploadDataStream
* stream
) {
42 std::string data_read
;
43 scoped_refptr
<IOBuffer
> buf
= new IOBuffer(kTestBufferSize
);
44 while (!stream
->IsEOF()) {
45 TestCompletionCallback callback
;
47 stream
->Read(buf
.get(), kTestBufferSize
, callback
.callback());
48 const int bytes_read
=
49 result
!= ERR_IO_PENDING
? result
: callback
.WaitForResult();
50 data_read
.append(buf
->data(), bytes_read
);
55 // A mock class of UploadElementReader.
56 class MockUploadElementReader
: public UploadElementReader
{
58 MockUploadElementReader(int content_length
, bool is_in_memory
)
59 : content_length_(content_length
),
60 bytes_remaining_(content_length
),
61 is_in_memory_(is_in_memory
),
65 virtual ~MockUploadElementReader() {}
67 // UploadElementReader overrides.
68 MOCK_METHOD1(Init
, int(const CompletionCallback
& callback
));
69 virtual uint64
GetContentLength() const OVERRIDE
{ return content_length_
; }
70 virtual uint64
BytesRemaining() const OVERRIDE
{ return bytes_remaining_
; }
71 virtual bool IsInMemory() const OVERRIDE
{ return is_in_memory_
; }
72 MOCK_METHOD3(Read
, int(IOBuffer
* buf
,
74 const CompletionCallback
& callback
));
76 // Sets expectation to return the specified result from Init() asynchronously.
77 void SetAsyncInitExpectation(int result
) {
78 init_result_
= result
;
79 EXPECT_CALL(*this, Init(_
))
80 .WillOnce(DoAll(Invoke(this, &MockUploadElementReader::OnInit
),
81 Return(ERR_IO_PENDING
)));
84 // Sets expectation to return the specified result from Read().
85 void SetReadExpectation(int result
) {
86 read_result_
= result
;
87 EXPECT_CALL(*this, Read(_
, _
, _
))
88 .WillOnce(Invoke(this, &MockUploadElementReader::OnRead
));
92 void OnInit(const CompletionCallback
& callback
) {
93 base::MessageLoop::current()->PostTask(FROM_HERE
,
94 base::Bind(callback
, init_result_
));
97 int OnRead(IOBuffer
* buf
,
99 const CompletionCallback
& callback
) {
100 if (read_result_
> 0)
101 bytes_remaining_
= std::max(0, bytes_remaining_
- read_result_
);
105 base::MessageLoop::current()->PostTask(
106 FROM_HERE
, base::Bind(callback
, read_result_
));
107 return ERR_IO_PENDING
;
112 int bytes_remaining_
;
115 // Result value returned from Init().
118 // Result value returned from Read().
124 class UploadDataStreamTest
: public PlatformTest
{
126 virtual void SetUp() OVERRIDE
{
127 ASSERT_TRUE(temp_dir_
.CreateUniqueTempDir());
130 void FileChangedHelper(const base::FilePath
& file_path
,
131 const base::Time
& time
,
132 bool error_expected
);
134 base::ScopedTempDir temp_dir_
;
135 ScopedVector
<UploadElementReader
> element_readers_
;
138 TEST_F(UploadDataStreamTest
, EmptyUploadData
) {
139 UploadDataStream
stream(&element_readers_
, 0);
140 ASSERT_EQ(OK
, stream
.Init(CompletionCallback()));
141 EXPECT_TRUE(stream
.IsInMemory());
142 EXPECT_EQ(0U, stream
.size());
143 EXPECT_EQ(0U, stream
.position());
144 EXPECT_TRUE(stream
.IsEOF());
147 TEST_F(UploadDataStreamTest
, ConsumeAllBytes
) {
148 element_readers_
.push_back(new UploadBytesElementReader(
149 kTestData
, kTestDataSize
));
150 UploadDataStream
stream(&element_readers_
, 0);
151 ASSERT_EQ(OK
, stream
.Init(CompletionCallback()));
152 EXPECT_TRUE(stream
.IsInMemory());
153 EXPECT_EQ(kTestDataSize
, stream
.size());
154 EXPECT_EQ(0U, stream
.position());
155 EXPECT_FALSE(stream
.IsEOF());
156 scoped_refptr
<IOBuffer
> buf
= new IOBuffer(kTestBufferSize
);
157 while (!stream
.IsEOF()) {
159 stream
.Read(buf
.get(), kTestBufferSize
, CompletionCallback());
160 ASSERT_LE(0, bytes_read
); // Not an error.
162 EXPECT_EQ(kTestDataSize
, stream
.position());
163 ASSERT_TRUE(stream
.IsEOF());
166 TEST_F(UploadDataStreamTest
, File
) {
167 base::FilePath temp_file_path
;
168 ASSERT_TRUE(file_util::CreateTemporaryFileInDir(temp_dir_
.path(),
170 ASSERT_EQ(static_cast<int>(kTestDataSize
),
171 file_util::WriteFile(temp_file_path
, kTestData
, kTestDataSize
));
173 element_readers_
.push_back(
174 new UploadFileElementReader(base::MessageLoopProxy::current().get(),
180 TestCompletionCallback init_callback
;
181 UploadDataStream
stream(&element_readers_
, 0);
182 ASSERT_EQ(ERR_IO_PENDING
, stream
.Init(init_callback
.callback()));
183 ASSERT_EQ(OK
, init_callback
.WaitForResult());
184 EXPECT_FALSE(stream
.IsInMemory());
185 EXPECT_EQ(kTestDataSize
, stream
.size());
186 EXPECT_EQ(0U, stream
.position());
187 EXPECT_FALSE(stream
.IsEOF());
188 scoped_refptr
<IOBuffer
> buf
= new IOBuffer(kTestBufferSize
);
189 while (!stream
.IsEOF()) {
190 TestCompletionCallback read_callback
;
193 stream
.Read(buf
.get(), kTestBufferSize
, read_callback
.callback()));
194 ASSERT_LE(0, read_callback
.WaitForResult()); // Not an error.
196 EXPECT_EQ(kTestDataSize
, stream
.position());
197 ASSERT_TRUE(stream
.IsEOF());
200 TEST_F(UploadDataStreamTest
, FileSmallerThanLength
) {
201 base::FilePath temp_file_path
;
202 ASSERT_TRUE(file_util::CreateTemporaryFileInDir(temp_dir_
.path(),
204 ASSERT_EQ(static_cast<int>(kTestDataSize
),
205 file_util::WriteFile(temp_file_path
, kTestData
, kTestDataSize
));
206 const uint64 kFakeSize
= kTestDataSize
*2;
208 UploadFileElementReader::ScopedOverridingContentLengthForTests
209 overriding_content_length(kFakeSize
);
211 element_readers_
.push_back(
212 new UploadFileElementReader(base::MessageLoopProxy::current().get(),
218 TestCompletionCallback init_callback
;
219 UploadDataStream
stream(&element_readers_
, 0);
220 ASSERT_EQ(ERR_IO_PENDING
, stream
.Init(init_callback
.callback()));
221 ASSERT_EQ(OK
, init_callback
.WaitForResult());
222 EXPECT_FALSE(stream
.IsInMemory());
223 EXPECT_EQ(kFakeSize
, stream
.size());
224 EXPECT_EQ(0U, stream
.position());
225 EXPECT_FALSE(stream
.IsEOF());
226 uint64 read_counter
= 0;
227 scoped_refptr
<IOBuffer
> buf
= new IOBuffer(kTestBufferSize
);
228 while (!stream
.IsEOF()) {
229 TestCompletionCallback read_callback
;
232 stream
.Read(buf
.get(), kTestBufferSize
, read_callback
.callback()));
233 int bytes_read
= read_callback
.WaitForResult();
234 ASSERT_LE(0, bytes_read
); // Not an error.
235 read_counter
+= bytes_read
;
236 EXPECT_EQ(read_counter
, stream
.position());
238 // UpdateDataStream will pad out the file with 0 bytes so that the HTTP
239 // transaction doesn't hang. Therefore we expected the full size.
240 EXPECT_EQ(kFakeSize
, read_counter
);
241 EXPECT_EQ(read_counter
, stream
.position());
244 TEST_F(UploadDataStreamTest
, ReadErrorSync
) {
245 // This element cannot be read.
246 MockUploadElementReader
* reader
=
247 new MockUploadElementReader(kTestDataSize
, true);
248 EXPECT_CALL(*reader
, Init(_
)).WillOnce(Return(OK
));
249 reader
->SetReadExpectation(ERR_FAILED
);
250 element_readers_
.push_back(reader
);
252 // This element is ignored because of the error from the previous reader.
253 element_readers_
.push_back(new UploadBytesElementReader(
254 kTestData
, kTestDataSize
));
256 UploadDataStream
stream(&element_readers_
, 0);
259 ASSERT_EQ(OK
, stream
.Init(CompletionCallback()));
260 EXPECT_EQ(kTestDataSize
*2, stream
.size());
261 EXPECT_EQ(0U, stream
.position());
262 EXPECT_FALSE(stream
.IsEOF());
264 // Prepare a buffer filled with non-zero data.
265 scoped_refptr
<IOBuffer
> buf
= new IOBuffer(kTestBufferSize
);
266 std::fill_n(buf
->data(), kTestBufferSize
, -1);
268 // Read() results in success even when the reader returns error.
269 EXPECT_EQ(static_cast<int>(kTestDataSize
* 2),
270 stream
.Read(buf
.get(), kTestBufferSize
, CompletionCallback()));
271 EXPECT_EQ(kTestDataSize
* 2, stream
.position());
272 EXPECT_TRUE(stream
.IsEOF());
274 // The buffer is filled with zero.
275 EXPECT_EQ(static_cast<int>(kTestDataSize
*2),
276 std::count(buf
->data(), buf
->data() + kTestBufferSize
, 0));
279 TEST_F(UploadDataStreamTest
, ReadErrorAsync
) {
280 // This element cannot be read.
281 MockUploadElementReader
* reader
=
282 new MockUploadElementReader(kTestDataSize
, false);
283 reader
->SetAsyncInitExpectation(OK
);
284 reader
->SetReadExpectation(ERR_FAILED
);
285 element_readers_
.push_back(reader
);
287 // This element is ignored because of the error from the previous reader.
288 element_readers_
.push_back(new UploadBytesElementReader(
289 kTestData
, kTestDataSize
));
291 UploadDataStream
stream(&element_readers_
, 0);
294 TestCompletionCallback init_callback
;
295 ASSERT_EQ(ERR_IO_PENDING
, stream
.Init(init_callback
.callback()));
296 EXPECT_EQ(OK
, init_callback
.WaitForResult());
297 EXPECT_EQ(kTestDataSize
*2, stream
.size());
298 EXPECT_EQ(0U, stream
.position());
299 EXPECT_FALSE(stream
.IsEOF());
301 // Prepare a buffer filled with non-zero data.
302 scoped_refptr
<IOBuffer
> buf
= new IOBuffer(kTestBufferSize
);
303 std::fill_n(buf
->data(), kTestBufferSize
, -1);
305 // Read() results in success even when the reader returns error.
306 TestCompletionCallback read_callback
;
307 ASSERT_EQ(ERR_IO_PENDING
,
308 stream
.Read(buf
.get(), kTestBufferSize
, read_callback
.callback()));
309 EXPECT_EQ(static_cast<int>(kTestDataSize
* 2), read_callback
.WaitForResult());
310 EXPECT_EQ(kTestDataSize
*2, stream
.position());
311 EXPECT_TRUE(stream
.IsEOF());
313 // The buffer is filled with zero.
314 EXPECT_EQ(static_cast<int>(kTestDataSize
*2),
315 std::count(buf
->data(), buf
->data() + kTestBufferSize
, 0));
318 TEST_F(UploadDataStreamTest
, FileAndBytes
) {
319 base::FilePath temp_file_path
;
320 ASSERT_TRUE(file_util::CreateTemporaryFileInDir(temp_dir_
.path(),
322 ASSERT_EQ(static_cast<int>(kTestDataSize
),
323 file_util::WriteFile(temp_file_path
, kTestData
, kTestDataSize
));
325 const uint64 kFileRangeOffset
= 1;
326 const uint64 kFileRangeLength
= 4;
327 element_readers_
.push_back(
328 new UploadFileElementReader(base::MessageLoopProxy::current().get(),
334 element_readers_
.push_back(new UploadBytesElementReader(
335 kTestData
, kTestDataSize
));
337 const uint64 kStreamSize
= kTestDataSize
+ kFileRangeLength
;
338 TestCompletionCallback init_callback
;
339 UploadDataStream
stream(&element_readers_
, 0);
340 ASSERT_EQ(ERR_IO_PENDING
, stream
.Init(init_callback
.callback()));
341 ASSERT_EQ(OK
, init_callback
.WaitForResult());
342 EXPECT_FALSE(stream
.IsInMemory());
343 EXPECT_EQ(kStreamSize
, stream
.size());
344 EXPECT_EQ(0U, stream
.position());
345 EXPECT_FALSE(stream
.IsEOF());
346 scoped_refptr
<IOBuffer
> buf
= new IOBuffer(kTestBufferSize
);
347 while (!stream
.IsEOF()) {
348 TestCompletionCallback read_callback
;
350 stream
.Read(buf
.get(), kTestBufferSize
, read_callback
.callback());
351 const int bytes_read
=
352 result
!= ERR_IO_PENDING
? result
: read_callback
.WaitForResult();
353 ASSERT_LE(0, bytes_read
); // Not an error.
355 EXPECT_EQ(kStreamSize
, stream
.position());
356 ASSERT_TRUE(stream
.IsEOF());
359 TEST_F(UploadDataStreamTest
, Chunk
) {
360 const uint64 kStreamSize
= kTestDataSize
*2;
361 UploadDataStream
stream(UploadDataStream::CHUNKED
, 0);
362 stream
.AppendChunk(kTestData
, kTestDataSize
, false);
363 stream
.AppendChunk(kTestData
, kTestDataSize
, true);
365 ASSERT_EQ(OK
, stream
.Init(CompletionCallback()));
366 EXPECT_FALSE(stream
.IsInMemory());
367 EXPECT_EQ(0U, stream
.size()); // Content-Length is 0 for chunked data.
368 EXPECT_EQ(0U, stream
.position());
369 EXPECT_FALSE(stream
.IsEOF());
370 scoped_refptr
<IOBuffer
> buf
= new IOBuffer(kTestBufferSize
);
371 while (!stream
.IsEOF()) {
373 stream
.Read(buf
.get(), kTestBufferSize
, CompletionCallback());
374 ASSERT_LE(0, bytes_read
); // Not an error.
376 EXPECT_EQ(kStreamSize
, stream
.position());
377 ASSERT_TRUE(stream
.IsEOF());
380 // Init() with on-memory and not-on-memory readers.
381 TEST_F(UploadDataStreamTest
, InitAsync
) {
382 // Create UploadDataStream with mock readers.
383 MockUploadElementReader
* reader
= NULL
;
385 reader
= new MockUploadElementReader(kTestDataSize
, true);
386 EXPECT_CALL(*reader
, Init(_
)).WillOnce(Return(OK
));
387 element_readers_
.push_back(reader
);
389 reader
= new MockUploadElementReader(kTestDataSize
, true);
390 EXPECT_CALL(*reader
, Init(_
)).WillOnce(Return(OK
));
391 element_readers_
.push_back(reader
);
393 reader
= new MockUploadElementReader(kTestDataSize
, false);
394 reader
->SetAsyncInitExpectation(OK
);
395 element_readers_
.push_back(reader
);
397 reader
= new MockUploadElementReader(kTestDataSize
, false);
398 reader
->SetAsyncInitExpectation(OK
);
399 element_readers_
.push_back(reader
);
401 reader
= new MockUploadElementReader(kTestDataSize
, true);
402 EXPECT_CALL(*reader
, Init(_
)).WillOnce(Return(OK
));
403 element_readers_
.push_back(reader
);
405 UploadDataStream
stream(&element_readers_
, 0);
408 TestCompletionCallback callback
;
409 ASSERT_EQ(ERR_IO_PENDING
, stream
.Init(callback
.callback()));
410 EXPECT_EQ(OK
, callback
.WaitForResult());
413 // Init() of a reader fails asynchronously.
414 TEST_F(UploadDataStreamTest
, InitAsyncFailureAsync
) {
415 // Create UploadDataStream with a mock reader.
416 MockUploadElementReader
* reader
= NULL
;
418 reader
= new MockUploadElementReader(kTestDataSize
, false);
419 reader
->SetAsyncInitExpectation(ERR_FAILED
);
420 element_readers_
.push_back(reader
);
422 UploadDataStream
stream(&element_readers_
, 0);
425 TestCompletionCallback callback
;
426 ASSERT_EQ(ERR_IO_PENDING
, stream
.Init(callback
.callback()));
427 EXPECT_EQ(ERR_FAILED
, callback
.WaitForResult());
430 // Init() of a reader fails synchronously.
431 TEST_F(UploadDataStreamTest
, InitAsyncFailureSync
) {
432 // Create UploadDataStream with mock readers.
433 MockUploadElementReader
* reader
= NULL
;
435 reader
= new MockUploadElementReader(kTestDataSize
, false);
436 reader
->SetAsyncInitExpectation(OK
);
437 element_readers_
.push_back(reader
);
439 reader
= new MockUploadElementReader(kTestDataSize
, true);
440 EXPECT_CALL(*reader
, Init(_
)).WillOnce(Return(ERR_FAILED
));
441 element_readers_
.push_back(reader
);
443 UploadDataStream
stream(&element_readers_
, 0);
446 TestCompletionCallback callback
;
447 ASSERT_EQ(ERR_IO_PENDING
, stream
.Init(callback
.callback()));
448 EXPECT_EQ(ERR_FAILED
, callback
.WaitForResult());
451 // Read with a buffer whose size is same as the data.
452 TEST_F(UploadDataStreamTest
, ReadAsyncWithExactSizeBuffer
) {
453 element_readers_
.push_back(new UploadBytesElementReader(
454 kTestData
, kTestDataSize
));
455 UploadDataStream
stream(&element_readers_
, 0);
457 ASSERT_EQ(OK
, stream
.Init(CompletionCallback()));
458 EXPECT_TRUE(stream
.IsInMemory());
459 EXPECT_EQ(kTestDataSize
, stream
.size());
460 EXPECT_EQ(0U, stream
.position());
461 EXPECT_FALSE(stream
.IsEOF());
462 scoped_refptr
<IOBuffer
> buf
= new IOBuffer(kTestDataSize
);
463 int bytes_read
= stream
.Read(buf
.get(), kTestDataSize
, CompletionCallback());
464 ASSERT_EQ(static_cast<int>(kTestDataSize
), bytes_read
); // Not an error.
465 EXPECT_EQ(kTestDataSize
, stream
.position());
466 ASSERT_TRUE(stream
.IsEOF());
469 // Async Read() with on-memory and not-on-memory readers.
470 TEST_F(UploadDataStreamTest
, ReadAsync
) {
471 // Create UploadDataStream with mock readers.
472 MockUploadElementReader
* reader
= NULL
;
474 reader
= new MockUploadElementReader(kTestDataSize
, true);
475 EXPECT_CALL(*reader
, Init(_
)).WillOnce(Return(OK
));
476 reader
->SetReadExpectation(kTestDataSize
);
477 element_readers_
.push_back(reader
);
479 reader
= new MockUploadElementReader(kTestDataSize
, false);
480 reader
->SetAsyncInitExpectation(OK
);
481 reader
->SetReadExpectation(kTestDataSize
);
482 element_readers_
.push_back(reader
);
484 reader
= new MockUploadElementReader(kTestDataSize
, true);
485 EXPECT_CALL(*reader
, Init(_
)).WillOnce(Return(OK
));
486 reader
->SetReadExpectation(kTestDataSize
);
487 element_readers_
.push_back(reader
);
489 reader
= new MockUploadElementReader(kTestDataSize
, false);
490 reader
->SetAsyncInitExpectation(OK
);
491 reader
->SetReadExpectation(kTestDataSize
);
492 element_readers_
.push_back(reader
);
494 UploadDataStream
stream(&element_readers_
, 0);
497 TestCompletionCallback init_callback
;
498 EXPECT_EQ(ERR_IO_PENDING
, stream
.Init(init_callback
.callback()));
499 EXPECT_EQ(OK
, init_callback
.WaitForResult());
501 scoped_refptr
<IOBuffer
> buf
= new IOBuffer(kTestBufferSize
);
503 // Consume the first element.
504 TestCompletionCallback read_callback1
;
505 EXPECT_EQ(static_cast<int>(kTestDataSize
),
506 stream
.Read(buf
.get(), kTestDataSize
, read_callback1
.callback()));
507 base::MessageLoop::current()->RunUntilIdle();
508 EXPECT_FALSE(read_callback1
.have_result());
510 // Consume the second element.
511 TestCompletionCallback read_callback2
;
512 ASSERT_EQ(ERR_IO_PENDING
,
513 stream
.Read(buf
.get(), kTestDataSize
, read_callback2
.callback()));
514 EXPECT_EQ(static_cast<int>(kTestDataSize
), read_callback2
.WaitForResult());
516 // Consume the third and the fourth elements.
517 TestCompletionCallback read_callback3
;
520 stream
.Read(buf
.get(), kTestDataSize
* 2, read_callback3
.callback()));
521 EXPECT_EQ(static_cast<int>(kTestDataSize
* 2),
522 read_callback3
.WaitForResult());
525 void UploadDataStreamTest::FileChangedHelper(const base::FilePath
& file_path
,
526 const base::Time
& time
,
527 bool error_expected
) {
528 // Don't use element_readers_ here, as this function is called twice, and
529 // reusing element_readers_ is wrong.
530 ScopedVector
<UploadElementReader
> element_readers
;
531 element_readers
.push_back(new UploadFileElementReader(
532 base::MessageLoopProxy::current().get(), file_path
, 1, 2, time
));
534 TestCompletionCallback init_callback
;
535 UploadDataStream
stream(&element_readers
, 0);
536 ASSERT_EQ(ERR_IO_PENDING
, stream
.Init(init_callback
.callback()));
537 int error_code
= init_callback
.WaitForResult();
539 ASSERT_EQ(ERR_UPLOAD_FILE_CHANGED
, error_code
);
541 ASSERT_EQ(OK
, error_code
);
544 TEST_F(UploadDataStreamTest
, FileChanged
) {
545 base::FilePath temp_file_path
;
546 ASSERT_TRUE(file_util::CreateTemporaryFileInDir(temp_dir_
.path(),
548 ASSERT_EQ(static_cast<int>(kTestDataSize
),
549 file_util::WriteFile(temp_file_path
, kTestData
, kTestDataSize
));
551 base::PlatformFileInfo file_info
;
552 ASSERT_TRUE(file_util::GetFileInfo(temp_file_path
, &file_info
));
554 // Test file not changed.
555 FileChangedHelper(temp_file_path
, file_info
.last_modified
, false);
557 // Test file changed.
558 FileChangedHelper(temp_file_path
,
559 file_info
.last_modified
- base::TimeDelta::FromSeconds(1),
563 TEST_F(UploadDataStreamTest
, MultipleInit
) {
564 base::FilePath temp_file_path
;
565 ASSERT_TRUE(file_util::CreateTemporaryFileInDir(temp_dir_
.path(),
567 ASSERT_EQ(static_cast<int>(kTestDataSize
),
568 file_util::WriteFile(temp_file_path
, kTestData
, kTestDataSize
));
571 element_readers_
.push_back(new UploadBytesElementReader(
572 kTestData
, kTestDataSize
));
573 element_readers_
.push_back(
574 new UploadFileElementReader(base::MessageLoopProxy::current().get(),
579 UploadDataStream
stream(&element_readers_
, 0);
581 std::string
expected_data(kTestData
, kTestData
+ kTestDataSize
);
582 expected_data
+= expected_data
;
585 TestCompletionCallback init_callback1
;
586 ASSERT_EQ(ERR_IO_PENDING
, stream
.Init(init_callback1
.callback()));
587 ASSERT_EQ(OK
, init_callback1
.WaitForResult());
588 EXPECT_FALSE(stream
.IsEOF());
589 EXPECT_EQ(kTestDataSize
*2, stream
.size());
592 EXPECT_EQ(expected_data
, ReadFromUploadDataStream(&stream
));
593 EXPECT_TRUE(stream
.IsEOF());
595 // Call Init() again to reset.
596 TestCompletionCallback init_callback2
;
597 ASSERT_EQ(ERR_IO_PENDING
, stream
.Init(init_callback2
.callback()));
598 ASSERT_EQ(OK
, init_callback2
.WaitForResult());
599 EXPECT_FALSE(stream
.IsEOF());
600 EXPECT_EQ(kTestDataSize
*2, stream
.size());
603 EXPECT_EQ(expected_data
, ReadFromUploadDataStream(&stream
));
604 EXPECT_TRUE(stream
.IsEOF());
607 TEST_F(UploadDataStreamTest
, MultipleInitAsync
) {
608 base::FilePath temp_file_path
;
609 ASSERT_TRUE(file_util::CreateTemporaryFileInDir(temp_dir_
.path(),
611 ASSERT_EQ(static_cast<int>(kTestDataSize
),
612 file_util::WriteFile(temp_file_path
, kTestData
, kTestDataSize
));
613 TestCompletionCallback test_callback
;
616 element_readers_
.push_back(new UploadBytesElementReader(
617 kTestData
, kTestDataSize
));
618 element_readers_
.push_back(
619 new UploadFileElementReader(base::MessageLoopProxy::current().get(),
624 UploadDataStream
stream(&element_readers_
, 0);
626 std::string
expected_data(kTestData
, kTestData
+ kTestDataSize
);
627 expected_data
+= expected_data
;
630 ASSERT_EQ(ERR_IO_PENDING
, stream
.Init(test_callback
.callback()));
631 EXPECT_EQ(OK
, test_callback
.WaitForResult());
632 EXPECT_FALSE(stream
.IsEOF());
633 EXPECT_EQ(kTestDataSize
*2, stream
.size());
636 EXPECT_EQ(expected_data
, ReadFromUploadDataStream(&stream
));
637 EXPECT_TRUE(stream
.IsEOF());
639 // Call Init() again to reset.
640 ASSERT_EQ(ERR_IO_PENDING
, stream
.Init(test_callback
.callback()));
641 EXPECT_EQ(OK
, test_callback
.WaitForResult());
642 EXPECT_FALSE(stream
.IsEOF());
643 EXPECT_EQ(kTestDataSize
*2, stream
.size());
646 EXPECT_EQ(expected_data
, ReadFromUploadDataStream(&stream
));
647 EXPECT_TRUE(stream
.IsEOF());
650 TEST_F(UploadDataStreamTest
, InitToReset
) {
651 base::FilePath temp_file_path
;
652 ASSERT_TRUE(file_util::CreateTemporaryFileInDir(temp_dir_
.path(),
654 ASSERT_EQ(static_cast<int>(kTestDataSize
),
655 file_util::WriteFile(temp_file_path
, kTestData
, kTestDataSize
));
658 element_readers_
.push_back(new UploadBytesElementReader(
659 kTestData
, kTestDataSize
));
660 element_readers_
.push_back(
661 new UploadFileElementReader(base::MessageLoopProxy::current().get(),
666 UploadDataStream
stream(&element_readers_
, 0);
668 std::vector
<char> expected_data(kTestData
, kTestData
+ kTestDataSize
);
669 expected_data
.insert(expected_data
.end(), expected_data
.begin(),
670 expected_data
.begin() + kTestDataSize
);
673 TestCompletionCallback init_callback1
;
674 ASSERT_EQ(ERR_IO_PENDING
, stream
.Init(init_callback1
.callback()));
675 EXPECT_EQ(OK
, init_callback1
.WaitForResult());
676 EXPECT_FALSE(stream
.IsEOF());
677 EXPECT_EQ(kTestDataSize
*2, stream
.size());
680 TestCompletionCallback read_callback1
;
681 std::vector
<char> buf(kTestDataSize
+ kTestDataSize
/2);
682 scoped_refptr
<IOBuffer
> wrapped_buffer
= new WrappedIOBuffer(&buf
[0]);
685 stream
.Read(wrapped_buffer
.get(), buf
.size(), read_callback1
.callback()));
686 EXPECT_EQ(static_cast<int>(buf
.size()), read_callback1
.WaitForResult());
687 EXPECT_EQ(buf
.size(), stream
.position());
689 // Call Init to reset the state.
690 TestCompletionCallback init_callback2
;
691 ASSERT_EQ(ERR_IO_PENDING
, stream
.Init(init_callback2
.callback()));
692 EXPECT_EQ(OK
, init_callback2
.WaitForResult());
693 EXPECT_FALSE(stream
.IsEOF());
694 EXPECT_EQ(kTestDataSize
*2, stream
.size());
697 TestCompletionCallback read_callback2
;
698 std::vector
<char> buf2(kTestDataSize
*2);
699 scoped_refptr
<IOBuffer
> wrapped_buffer2
= new WrappedIOBuffer(&buf2
[0]);
700 EXPECT_EQ(ERR_IO_PENDING
,
702 wrapped_buffer2
.get(), buf2
.size(), read_callback2
.callback()));
703 EXPECT_EQ(static_cast<int>(buf2
.size()), read_callback2
.WaitForResult());
704 EXPECT_EQ(expected_data
, buf2
);
707 TEST_F(UploadDataStreamTest
, InitDuringAsyncInit
) {
708 base::FilePath temp_file_path
;
709 ASSERT_TRUE(file_util::CreateTemporaryFileInDir(temp_dir_
.path(),
711 ASSERT_EQ(static_cast<int>(kTestDataSize
),
712 file_util::WriteFile(temp_file_path
, kTestData
, kTestDataSize
));
715 element_readers_
.push_back(new UploadBytesElementReader(
716 kTestData
, kTestDataSize
));
717 element_readers_
.push_back(
718 new UploadFileElementReader(base::MessageLoopProxy::current().get(),
723 UploadDataStream
stream(&element_readers_
, 0);
725 std::vector
<char> expected_data(kTestData
, kTestData
+ kTestDataSize
);
726 expected_data
.insert(expected_data
.end(), expected_data
.begin(),
727 expected_data
.begin() + kTestDataSize
);
730 TestCompletionCallback init_callback1
;
731 EXPECT_EQ(ERR_IO_PENDING
, stream
.Init(init_callback1
.callback()));
733 // Call Init again to cancel the previous init.
734 TestCompletionCallback init_callback2
;
735 EXPECT_EQ(ERR_IO_PENDING
, stream
.Init(init_callback2
.callback()));
736 EXPECT_EQ(OK
, init_callback2
.WaitForResult());
737 EXPECT_FALSE(stream
.IsEOF());
738 EXPECT_EQ(kTestDataSize
*2, stream
.size());
741 TestCompletionCallback read_callback2
;
742 std::vector
<char> buf2(kTestDataSize
*2);
743 scoped_refptr
<IOBuffer
> wrapped_buffer2
= new WrappedIOBuffer(&buf2
[0]);
744 EXPECT_EQ(ERR_IO_PENDING
,
746 wrapped_buffer2
.get(), buf2
.size(), read_callback2
.callback()));
747 EXPECT_EQ(static_cast<int>(buf2
.size()), read_callback2
.WaitForResult());
748 EXPECT_EQ(expected_data
, buf2
);
749 EXPECT_TRUE(stream
.IsEOF());
751 // Make sure callbacks are not called for cancelled operations.
752 EXPECT_FALSE(init_callback1
.have_result());
755 TEST_F(UploadDataStreamTest
, InitDuringAsyncRead
) {
756 base::FilePath temp_file_path
;
757 ASSERT_TRUE(file_util::CreateTemporaryFileInDir(temp_dir_
.path(),
759 ASSERT_EQ(static_cast<int>(kTestDataSize
),
760 file_util::WriteFile(temp_file_path
, kTestData
, kTestDataSize
));
763 element_readers_
.push_back(new UploadBytesElementReader(
764 kTestData
, kTestDataSize
));
765 element_readers_
.push_back(
766 new UploadFileElementReader(base::MessageLoopProxy::current().get(),
771 UploadDataStream
stream(&element_readers_
, 0);
773 std::vector
<char> expected_data(kTestData
, kTestData
+ kTestDataSize
);
774 expected_data
.insert(expected_data
.end(), expected_data
.begin(),
775 expected_data
.begin() + kTestDataSize
);
778 TestCompletionCallback init_callback1
;
779 ASSERT_EQ(ERR_IO_PENDING
, stream
.Init(init_callback1
.callback()));
780 EXPECT_EQ(OK
, init_callback1
.WaitForResult());
781 EXPECT_FALSE(stream
.IsEOF());
782 EXPECT_EQ(kTestDataSize
*2, stream
.size());
785 TestCompletionCallback read_callback1
;
786 std::vector
<char> buf(kTestDataSize
*2);
787 scoped_refptr
<IOBuffer
> wrapped_buffer
= new WrappedIOBuffer(&buf
[0]);
790 stream
.Read(wrapped_buffer
.get(), buf
.size(), read_callback1
.callback()));
792 // Call Init to cancel the previous read.
793 TestCompletionCallback init_callback2
;
794 EXPECT_EQ(ERR_IO_PENDING
, stream
.Init(init_callback2
.callback()));
795 EXPECT_EQ(OK
, init_callback2
.WaitForResult());
796 EXPECT_FALSE(stream
.IsEOF());
797 EXPECT_EQ(kTestDataSize
*2, stream
.size());
800 TestCompletionCallback read_callback2
;
801 std::vector
<char> buf2(kTestDataSize
*2);
802 scoped_refptr
<IOBuffer
> wrapped_buffer2
= new WrappedIOBuffer(&buf2
[0]);
803 EXPECT_EQ(ERR_IO_PENDING
,
805 wrapped_buffer2
.get(), buf2
.size(), read_callback2
.callback()));
806 EXPECT_EQ(static_cast<int>(buf2
.size()), read_callback2
.WaitForResult());
807 EXPECT_EQ(expected_data
, buf2
);
808 EXPECT_TRUE(stream
.IsEOF());
810 // Make sure callbacks are not called for cancelled operations.
811 EXPECT_FALSE(read_callback1
.have_result());