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/elements_upload_data_stream.h"
10 #include "base/basictypes.h"
11 #include "base/bind.h"
12 #include "base/files/file_path.h"
13 #include "base/files/file_util.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/run_loop.h"
18 #include "base/time/time.h"
19 #include "net/base/io_buffer.h"
20 #include "net/base/net_errors.h"
21 #include "net/base/test_completion_callback.h"
22 #include "net/base/upload_bytes_element_reader.h"
23 #include "net/base/upload_data_stream.h"
24 #include "net/base/upload_file_element_reader.h"
25 #include "testing/gmock/include/gmock/gmock.h"
26 #include "testing/gtest/include/gtest/gtest.h"
27 #include "testing/platform_test.h"
29 using ::testing::DoAll
;
30 using ::testing::Invoke
;
31 using ::testing::Return
;
38 const char kTestData
[] = "0123456789";
39 const size_t kTestDataSize
= arraysize(kTestData
) - 1;
40 const size_t kTestBufferSize
= 1 << 14; // 16KB.
42 // Reads data from the upload data stream, and returns the data as string.
43 std::string
ReadFromUploadDataStream(UploadDataStream
* stream
) {
44 std::string data_read
;
45 scoped_refptr
<IOBuffer
> buf
= new IOBuffer(kTestBufferSize
);
46 while (!stream
->IsEOF()) {
47 TestCompletionCallback callback
;
49 stream
->Read(buf
.get(), kTestBufferSize
, callback
.callback());
50 const int bytes_read
=
51 result
!= ERR_IO_PENDING
? result
: callback
.WaitForResult();
52 data_read
.append(buf
->data(), bytes_read
);
57 // A mock class of UploadElementReader.
58 class MockUploadElementReader
: public UploadElementReader
{
60 MockUploadElementReader(int content_length
, bool is_in_memory
)
61 : content_length_(content_length
),
62 bytes_remaining_(content_length
),
63 is_in_memory_(is_in_memory
),
67 virtual ~MockUploadElementReader() {}
69 // UploadElementReader overrides.
70 MOCK_METHOD1(Init
, int(const CompletionCallback
& callback
));
71 virtual uint64
GetContentLength() const override
{ return content_length_
; }
72 virtual uint64
BytesRemaining() const override
{ return bytes_remaining_
; }
73 virtual bool IsInMemory() const override
{ return is_in_memory_
; }
74 MOCK_METHOD3(Read
, int(IOBuffer
* buf
,
76 const CompletionCallback
& callback
));
78 // Sets expectation to return the specified result from Init() asynchronously.
79 void SetAsyncInitExpectation(int result
) {
80 init_result_
= result
;
81 EXPECT_CALL(*this, Init(_
))
82 .WillOnce(DoAll(Invoke(this, &MockUploadElementReader::OnInit
),
83 Return(ERR_IO_PENDING
)));
86 // Sets expectation to return the specified result from Read().
87 void SetReadExpectation(int result
) {
88 read_result_
= result
;
89 EXPECT_CALL(*this, Read(_
, _
, _
))
90 .WillOnce(Invoke(this, &MockUploadElementReader::OnRead
));
94 void OnInit(const CompletionCallback
& callback
) {
95 base::MessageLoop::current()->PostTask(FROM_HERE
,
96 base::Bind(callback
, init_result_
));
99 int OnRead(IOBuffer
* buf
,
101 const CompletionCallback
& callback
) {
102 if (read_result_
> 0)
103 bytes_remaining_
= std::max(0, bytes_remaining_
- read_result_
);
107 base::MessageLoop::current()->PostTask(
108 FROM_HERE
, base::Bind(callback
, read_result_
));
109 return ERR_IO_PENDING
;
114 int bytes_remaining_
;
117 // Result value returned from Init().
120 // Result value returned from Read().
126 class ElementsUploadDataStreamTest
: public PlatformTest
{
128 void SetUp() override
{
129 PlatformTest::SetUp();
130 ASSERT_TRUE(temp_dir_
.CreateUniqueTempDir());
132 ~ElementsUploadDataStreamTest() override
{
133 element_readers_
.clear();
134 base::RunLoop().RunUntilIdle();
137 void FileChangedHelper(const base::FilePath
& file_path
,
138 const base::Time
& time
,
139 bool error_expected
);
141 base::ScopedTempDir temp_dir_
;
142 ScopedVector
<UploadElementReader
> element_readers_
;
145 TEST_F(ElementsUploadDataStreamTest
, EmptyUploadData
) {
146 scoped_ptr
<UploadDataStream
> stream(
147 new ElementsUploadDataStream(element_readers_
.Pass(), 0));
148 ASSERT_EQ(OK
, stream
->Init(CompletionCallback()));
149 EXPECT_TRUE(stream
->IsInMemory());
150 EXPECT_EQ(0U, stream
->size());
151 EXPECT_EQ(0U, stream
->position());
152 EXPECT_TRUE(stream
->IsEOF());
155 TEST_F(ElementsUploadDataStreamTest
, ConsumeAllBytes
) {
156 element_readers_
.push_back(new UploadBytesElementReader(
157 kTestData
, kTestDataSize
));
158 scoped_ptr
<UploadDataStream
> stream(
159 new ElementsUploadDataStream(element_readers_
.Pass(), 0));
160 ASSERT_EQ(OK
, stream
->Init(CompletionCallback()));
161 EXPECT_TRUE(stream
->IsInMemory());
162 EXPECT_EQ(kTestDataSize
, stream
->size());
163 EXPECT_EQ(0U, stream
->position());
164 EXPECT_FALSE(stream
->IsEOF());
165 scoped_refptr
<IOBuffer
> buf
= new IOBuffer(kTestBufferSize
);
166 while (!stream
->IsEOF()) {
168 stream
->Read(buf
.get(), kTestBufferSize
, CompletionCallback());
169 ASSERT_LE(0, bytes_read
); // Not an error.
171 EXPECT_EQ(kTestDataSize
, stream
->position());
172 ASSERT_TRUE(stream
->IsEOF());
175 TEST_F(ElementsUploadDataStreamTest
, File
) {
176 base::FilePath temp_file_path
;
177 ASSERT_TRUE(base::CreateTemporaryFileInDir(temp_dir_
.path(),
179 ASSERT_EQ(static_cast<int>(kTestDataSize
),
180 base::WriteFile(temp_file_path
, kTestData
, kTestDataSize
));
182 element_readers_
.push_back(
183 new UploadFileElementReader(base::MessageLoopProxy::current().get(),
189 TestCompletionCallback init_callback
;
190 scoped_ptr
<UploadDataStream
> stream(
191 new ElementsUploadDataStream(element_readers_
.Pass(), 0));
192 ASSERT_EQ(ERR_IO_PENDING
, stream
->Init(init_callback
.callback()));
193 ASSERT_EQ(OK
, init_callback
.WaitForResult());
194 EXPECT_FALSE(stream
->IsInMemory());
195 EXPECT_EQ(kTestDataSize
, stream
->size());
196 EXPECT_EQ(0U, stream
->position());
197 EXPECT_FALSE(stream
->IsEOF());
198 scoped_refptr
<IOBuffer
> buf
= new IOBuffer(kTestBufferSize
);
199 while (!stream
->IsEOF()) {
200 TestCompletionCallback read_callback
;
203 stream
->Read(buf
.get(), kTestBufferSize
, read_callback
.callback()));
204 ASSERT_LE(0, read_callback
.WaitForResult()); // Not an error.
206 EXPECT_EQ(kTestDataSize
, stream
->position());
207 ASSERT_TRUE(stream
->IsEOF());
210 TEST_F(ElementsUploadDataStreamTest
, FileSmallerThanLength
) {
211 base::FilePath temp_file_path
;
212 ASSERT_TRUE(base::CreateTemporaryFileInDir(temp_dir_
.path(),
214 ASSERT_EQ(static_cast<int>(kTestDataSize
),
215 base::WriteFile(temp_file_path
, kTestData
, kTestDataSize
));
216 const uint64 kFakeSize
= kTestDataSize
*2;
218 UploadFileElementReader::ScopedOverridingContentLengthForTests
219 overriding_content_length(kFakeSize
);
221 element_readers_
.push_back(
222 new UploadFileElementReader(base::MessageLoopProxy::current().get(),
228 TestCompletionCallback init_callback
;
229 scoped_ptr
<UploadDataStream
> stream(
230 new ElementsUploadDataStream(element_readers_
.Pass(), 0));
231 ASSERT_EQ(ERR_IO_PENDING
, stream
->Init(init_callback
.callback()));
232 ASSERT_EQ(OK
, init_callback
.WaitForResult());
233 EXPECT_FALSE(stream
->IsInMemory());
234 EXPECT_EQ(kFakeSize
, stream
->size());
235 EXPECT_EQ(0U, stream
->position());
236 EXPECT_FALSE(stream
->IsEOF());
237 uint64 read_counter
= 0;
238 scoped_refptr
<IOBuffer
> buf
= new IOBuffer(kTestBufferSize
);
239 while (!stream
->IsEOF()) {
240 TestCompletionCallback read_callback
;
243 stream
->Read(buf
.get(), kTestBufferSize
, read_callback
.callback()));
244 int bytes_read
= read_callback
.WaitForResult();
245 ASSERT_LE(0, bytes_read
); // Not an error.
246 read_counter
+= bytes_read
;
247 EXPECT_EQ(read_counter
, stream
->position());
249 // UpdateDataStream will pad out the file with 0 bytes so that the HTTP
250 // transaction doesn't hang. Therefore we expected the full size.
251 EXPECT_EQ(kFakeSize
, read_counter
);
252 EXPECT_EQ(read_counter
, stream
->position());
255 TEST_F(ElementsUploadDataStreamTest
, ReadErrorSync
) {
256 // This element cannot be read.
257 MockUploadElementReader
* reader
=
258 new MockUploadElementReader(kTestDataSize
, true);
259 EXPECT_CALL(*reader
, Init(_
)).WillOnce(Return(OK
));
260 reader
->SetReadExpectation(ERR_FAILED
);
261 element_readers_
.push_back(reader
);
263 // This element is ignored because of the error from the previous reader.
264 element_readers_
.push_back(new UploadBytesElementReader(
265 kTestData
, kTestDataSize
));
267 scoped_ptr
<UploadDataStream
> stream(
268 new ElementsUploadDataStream(element_readers_
.Pass(), 0));
271 ASSERT_EQ(OK
, stream
->Init(CompletionCallback()));
272 EXPECT_EQ(kTestDataSize
*2, stream
->size());
273 EXPECT_EQ(0U, stream
->position());
274 EXPECT_FALSE(stream
->IsEOF());
276 // Prepare a buffer filled with non-zero data.
277 scoped_refptr
<IOBuffer
> buf
= new IOBuffer(kTestBufferSize
);
278 std::fill_n(buf
->data(), kTestBufferSize
, -1);
280 // Read() results in success even when the reader returns error.
281 EXPECT_EQ(static_cast<int>(kTestDataSize
* 2),
282 stream
->Read(buf
.get(), kTestBufferSize
, CompletionCallback()));
283 EXPECT_EQ(kTestDataSize
* 2, stream
->position());
284 EXPECT_TRUE(stream
->IsEOF());
286 // The buffer is filled with zero.
287 EXPECT_EQ(static_cast<int>(kTestDataSize
*2),
288 std::count(buf
->data(), buf
->data() + kTestBufferSize
, 0));
291 TEST_F(ElementsUploadDataStreamTest
, ReadErrorAsync
) {
292 // This element cannot be read.
293 MockUploadElementReader
* reader
=
294 new MockUploadElementReader(kTestDataSize
, false);
295 reader
->SetAsyncInitExpectation(OK
);
296 reader
->SetReadExpectation(ERR_FAILED
);
297 element_readers_
.push_back(reader
);
299 // This element is ignored because of the error from the previous reader.
300 element_readers_
.push_back(new UploadBytesElementReader(
301 kTestData
, kTestDataSize
));
303 scoped_ptr
<UploadDataStream
> stream(
304 new ElementsUploadDataStream(element_readers_
.Pass(), 0));
307 TestCompletionCallback init_callback
;
308 ASSERT_EQ(ERR_IO_PENDING
, stream
->Init(init_callback
.callback()));
309 EXPECT_EQ(OK
, init_callback
.WaitForResult());
310 EXPECT_EQ(kTestDataSize
*2, stream
->size());
311 EXPECT_EQ(0U, stream
->position());
312 EXPECT_FALSE(stream
->IsEOF());
314 // Prepare a buffer filled with non-zero data.
315 scoped_refptr
<IOBuffer
> buf
= new IOBuffer(kTestBufferSize
);
316 std::fill_n(buf
->data(), kTestBufferSize
, -1);
318 // Read() results in success even when the reader returns error.
319 TestCompletionCallback read_callback
;
320 ASSERT_EQ(ERR_IO_PENDING
,
321 stream
->Read(buf
.get(), kTestBufferSize
, read_callback
.callback()));
322 EXPECT_EQ(static_cast<int>(kTestDataSize
* 2), read_callback
.WaitForResult());
323 EXPECT_EQ(kTestDataSize
*2, stream
->position());
324 EXPECT_TRUE(stream
->IsEOF());
326 // The buffer is filled with zero.
327 EXPECT_EQ(static_cast<int>(kTestDataSize
*2),
328 std::count(buf
->data(), buf
->data() + kTestBufferSize
, 0));
331 TEST_F(ElementsUploadDataStreamTest
, FileAndBytes
) {
332 base::FilePath temp_file_path
;
333 ASSERT_TRUE(base::CreateTemporaryFileInDir(temp_dir_
.path(),
335 ASSERT_EQ(static_cast<int>(kTestDataSize
),
336 base::WriteFile(temp_file_path
, kTestData
, kTestDataSize
));
338 const uint64 kFileRangeOffset
= 1;
339 const uint64 kFileRangeLength
= 4;
340 element_readers_
.push_back(
341 new UploadFileElementReader(base::MessageLoopProxy::current().get(),
347 element_readers_
.push_back(new UploadBytesElementReader(
348 kTestData
, kTestDataSize
));
350 const uint64 kStreamSize
= kTestDataSize
+ kFileRangeLength
;
351 TestCompletionCallback init_callback
;
352 scoped_ptr
<UploadDataStream
> stream(
353 new ElementsUploadDataStream(element_readers_
.Pass(), 0));
354 ASSERT_EQ(ERR_IO_PENDING
, stream
->Init(init_callback
.callback()));
355 ASSERT_EQ(OK
, init_callback
.WaitForResult());
356 EXPECT_FALSE(stream
->IsInMemory());
357 EXPECT_EQ(kStreamSize
, stream
->size());
358 EXPECT_EQ(0U, stream
->position());
359 EXPECT_FALSE(stream
->IsEOF());
360 scoped_refptr
<IOBuffer
> buf
= new IOBuffer(kTestBufferSize
);
361 while (!stream
->IsEOF()) {
362 TestCompletionCallback read_callback
;
364 stream
->Read(buf
.get(), kTestBufferSize
, read_callback
.callback());
365 const int bytes_read
=
366 result
!= ERR_IO_PENDING
? result
: read_callback
.WaitForResult();
367 ASSERT_LE(0, bytes_read
); // Not an error.
369 EXPECT_EQ(kStreamSize
, stream
->position());
370 ASSERT_TRUE(stream
->IsEOF());
373 // Init() with on-memory and not-on-memory readers.
374 TEST_F(ElementsUploadDataStreamTest
, InitAsync
) {
375 // Create UploadDataStream with mock readers.
376 MockUploadElementReader
* reader
= NULL
;
378 reader
= new MockUploadElementReader(kTestDataSize
, true);
379 EXPECT_CALL(*reader
, Init(_
)).WillOnce(Return(OK
));
380 element_readers_
.push_back(reader
);
382 reader
= new MockUploadElementReader(kTestDataSize
, true);
383 EXPECT_CALL(*reader
, Init(_
)).WillOnce(Return(OK
));
384 element_readers_
.push_back(reader
);
386 reader
= new MockUploadElementReader(kTestDataSize
, false);
387 reader
->SetAsyncInitExpectation(OK
);
388 element_readers_
.push_back(reader
);
390 reader
= new MockUploadElementReader(kTestDataSize
, false);
391 reader
->SetAsyncInitExpectation(OK
);
392 element_readers_
.push_back(reader
);
394 reader
= new MockUploadElementReader(kTestDataSize
, true);
395 EXPECT_CALL(*reader
, Init(_
)).WillOnce(Return(OK
));
396 element_readers_
.push_back(reader
);
398 scoped_ptr
<UploadDataStream
> stream(
399 new ElementsUploadDataStream(element_readers_
.Pass(), 0));
402 TestCompletionCallback callback
;
403 ASSERT_EQ(ERR_IO_PENDING
, stream
->Init(callback
.callback()));
404 EXPECT_EQ(OK
, callback
.WaitForResult());
407 // Init() of a reader fails asynchronously.
408 TEST_F(ElementsUploadDataStreamTest
, InitAsyncFailureAsync
) {
409 // Create UploadDataStream with a mock reader.
410 MockUploadElementReader
* reader
= NULL
;
412 reader
= new MockUploadElementReader(kTestDataSize
, false);
413 reader
->SetAsyncInitExpectation(ERR_FAILED
);
414 element_readers_
.push_back(reader
);
416 scoped_ptr
<UploadDataStream
> stream(
417 new ElementsUploadDataStream(element_readers_
.Pass(), 0));
420 TestCompletionCallback callback
;
421 ASSERT_EQ(ERR_IO_PENDING
, stream
->Init(callback
.callback()));
422 EXPECT_EQ(ERR_FAILED
, callback
.WaitForResult());
425 // Init() of a reader fails synchronously.
426 TEST_F(ElementsUploadDataStreamTest
, InitAsyncFailureSync
) {
427 // Create UploadDataStream with mock readers.
428 MockUploadElementReader
* reader
= NULL
;
430 reader
= new MockUploadElementReader(kTestDataSize
, false);
431 reader
->SetAsyncInitExpectation(OK
);
432 element_readers_
.push_back(reader
);
434 reader
= new MockUploadElementReader(kTestDataSize
, true);
435 EXPECT_CALL(*reader
, Init(_
)).WillOnce(Return(ERR_FAILED
));
436 element_readers_
.push_back(reader
);
438 scoped_ptr
<UploadDataStream
> stream(
439 new ElementsUploadDataStream(element_readers_
.Pass(), 0));
442 TestCompletionCallback callback
;
443 ASSERT_EQ(ERR_IO_PENDING
, stream
->Init(callback
.callback()));
444 EXPECT_EQ(ERR_FAILED
, callback
.WaitForResult());
447 // Read with a buffer whose size is same as the data.
448 TEST_F(ElementsUploadDataStreamTest
, ReadAsyncWithExactSizeBuffer
) {
449 element_readers_
.push_back(new UploadBytesElementReader(
450 kTestData
, kTestDataSize
));
451 scoped_ptr
<UploadDataStream
> stream(
452 new ElementsUploadDataStream(element_readers_
.Pass(), 0));
454 ASSERT_EQ(OK
, stream
->Init(CompletionCallback()));
455 EXPECT_TRUE(stream
->IsInMemory());
456 EXPECT_EQ(kTestDataSize
, stream
->size());
457 EXPECT_EQ(0U, stream
->position());
458 EXPECT_FALSE(stream
->IsEOF());
459 scoped_refptr
<IOBuffer
> buf
= new IOBuffer(kTestDataSize
);
460 int bytes_read
= stream
->Read(buf
.get(), kTestDataSize
, CompletionCallback());
461 ASSERT_EQ(static_cast<int>(kTestDataSize
), bytes_read
); // Not an error.
462 EXPECT_EQ(kTestDataSize
, stream
->position());
463 ASSERT_TRUE(stream
->IsEOF());
466 // Async Read() with on-memory and not-on-memory readers.
467 TEST_F(ElementsUploadDataStreamTest
, ReadAsync
) {
468 // Create UploadDataStream with mock readers.
469 MockUploadElementReader
* reader
= NULL
;
471 reader
= new MockUploadElementReader(kTestDataSize
, true);
472 EXPECT_CALL(*reader
, Init(_
)).WillOnce(Return(OK
));
473 reader
->SetReadExpectation(kTestDataSize
);
474 element_readers_
.push_back(reader
);
476 reader
= new MockUploadElementReader(kTestDataSize
, false);
477 reader
->SetAsyncInitExpectation(OK
);
478 reader
->SetReadExpectation(kTestDataSize
);
479 element_readers_
.push_back(reader
);
481 reader
= new MockUploadElementReader(kTestDataSize
, true);
482 EXPECT_CALL(*reader
, Init(_
)).WillOnce(Return(OK
));
483 reader
->SetReadExpectation(kTestDataSize
);
484 element_readers_
.push_back(reader
);
486 reader
= new MockUploadElementReader(kTestDataSize
, false);
487 reader
->SetAsyncInitExpectation(OK
);
488 reader
->SetReadExpectation(kTestDataSize
);
489 element_readers_
.push_back(reader
);
491 scoped_ptr
<UploadDataStream
> stream(
492 new ElementsUploadDataStream(element_readers_
.Pass(), 0));
495 TestCompletionCallback init_callback
;
496 EXPECT_EQ(ERR_IO_PENDING
, stream
->Init(init_callback
.callback()));
497 EXPECT_EQ(OK
, init_callback
.WaitForResult());
499 scoped_refptr
<IOBuffer
> buf
= new IOBuffer(kTestBufferSize
);
501 // Consume the first element.
502 TestCompletionCallback read_callback1
;
503 EXPECT_EQ(static_cast<int>(kTestDataSize
),
504 stream
->Read(buf
.get(), kTestDataSize
, read_callback1
.callback()));
505 base::MessageLoop::current()->RunUntilIdle();
506 EXPECT_FALSE(read_callback1
.have_result());
508 // Consume the second element.
509 TestCompletionCallback read_callback2
;
510 ASSERT_EQ(ERR_IO_PENDING
,
511 stream
->Read(buf
.get(), kTestDataSize
, read_callback2
.callback()));
512 EXPECT_EQ(static_cast<int>(kTestDataSize
), read_callback2
.WaitForResult());
514 // Consume the third and the fourth elements.
515 TestCompletionCallback read_callback3
;
518 stream
->Read(buf
.get(), kTestDataSize
* 2, read_callback3
.callback()));
519 EXPECT_EQ(static_cast<int>(kTestDataSize
* 2),
520 read_callback3
.WaitForResult());
523 void ElementsUploadDataStreamTest::FileChangedHelper(
524 const base::FilePath
& file_path
,
525 const base::Time
& time
,
526 bool error_expected
) {
527 // Don't use element_readers_ here, as this function is called twice, and
528 // reusing element_readers_ is wrong.
529 ScopedVector
<UploadElementReader
> element_readers
;
530 element_readers
.push_back(new UploadFileElementReader(
531 base::MessageLoopProxy::current().get(), file_path
, 1, 2, time
));
533 TestCompletionCallback init_callback
;
534 scoped_ptr
<UploadDataStream
> stream(
535 new ElementsUploadDataStream(element_readers
.Pass(), 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(ElementsUploadDataStreamTest
, FileChanged
) {
545 base::FilePath temp_file_path
;
546 ASSERT_TRUE(base::CreateTemporaryFileInDir(temp_dir_
.path(),
548 ASSERT_EQ(static_cast<int>(kTestDataSize
),
549 base::WriteFile(temp_file_path
, kTestData
, kTestDataSize
));
551 base::File::Info file_info
;
552 ASSERT_TRUE(base::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(ElementsUploadDataStreamTest
, MultipleInit
) {
564 base::FilePath temp_file_path
;
565 ASSERT_TRUE(base::CreateTemporaryFileInDir(temp_dir_
.path(),
567 ASSERT_EQ(static_cast<int>(kTestDataSize
),
568 base::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 scoped_ptr
<UploadDataStream
> stream(
580 new ElementsUploadDataStream(element_readers_
.Pass(), 0));
582 std::string
expected_data(kTestData
, kTestData
+ kTestDataSize
);
583 expected_data
+= expected_data
;
586 TestCompletionCallback init_callback1
;
587 ASSERT_EQ(ERR_IO_PENDING
, stream
->Init(init_callback1
.callback()));
588 ASSERT_EQ(OK
, init_callback1
.WaitForResult());
589 EXPECT_FALSE(stream
->IsEOF());
590 EXPECT_EQ(kTestDataSize
*2, stream
->size());
593 EXPECT_EQ(expected_data
, ReadFromUploadDataStream(stream
.get()));
594 EXPECT_TRUE(stream
->IsEOF());
596 // Call Init() again to reset.
597 TestCompletionCallback init_callback2
;
598 ASSERT_EQ(ERR_IO_PENDING
, stream
->Init(init_callback2
.callback()));
599 ASSERT_EQ(OK
, init_callback2
.WaitForResult());
600 EXPECT_FALSE(stream
->IsEOF());
601 EXPECT_EQ(kTestDataSize
*2, stream
->size());
604 EXPECT_EQ(expected_data
, ReadFromUploadDataStream(stream
.get()));
605 EXPECT_TRUE(stream
->IsEOF());
608 TEST_F(ElementsUploadDataStreamTest
, MultipleInitAsync
) {
609 base::FilePath temp_file_path
;
610 ASSERT_TRUE(base::CreateTemporaryFileInDir(temp_dir_
.path(),
612 ASSERT_EQ(static_cast<int>(kTestDataSize
),
613 base::WriteFile(temp_file_path
, kTestData
, kTestDataSize
));
614 TestCompletionCallback test_callback
;
617 element_readers_
.push_back(new UploadBytesElementReader(
618 kTestData
, kTestDataSize
));
619 element_readers_
.push_back(
620 new UploadFileElementReader(base::MessageLoopProxy::current().get(),
625 scoped_ptr
<UploadDataStream
> stream(
626 new ElementsUploadDataStream(element_readers_
.Pass(), 0));
628 std::string
expected_data(kTestData
, kTestData
+ kTestDataSize
);
629 expected_data
+= expected_data
;
632 ASSERT_EQ(ERR_IO_PENDING
, stream
->Init(test_callback
.callback()));
633 EXPECT_EQ(OK
, test_callback
.WaitForResult());
634 EXPECT_FALSE(stream
->IsEOF());
635 EXPECT_EQ(kTestDataSize
*2, stream
->size());
638 EXPECT_EQ(expected_data
, ReadFromUploadDataStream(stream
.get()));
639 EXPECT_TRUE(stream
->IsEOF());
641 // Call Init() again to reset.
642 ASSERT_EQ(ERR_IO_PENDING
, stream
->Init(test_callback
.callback()));
643 EXPECT_EQ(OK
, test_callback
.WaitForResult());
644 EXPECT_FALSE(stream
->IsEOF());
645 EXPECT_EQ(kTestDataSize
*2, stream
->size());
648 EXPECT_EQ(expected_data
, ReadFromUploadDataStream(stream
.get()));
649 EXPECT_TRUE(stream
->IsEOF());
652 TEST_F(ElementsUploadDataStreamTest
, InitToReset
) {
653 base::FilePath temp_file_path
;
654 ASSERT_TRUE(base::CreateTemporaryFileInDir(temp_dir_
.path(),
656 ASSERT_EQ(static_cast<int>(kTestDataSize
),
657 base::WriteFile(temp_file_path
, kTestData
, kTestDataSize
));
660 element_readers_
.push_back(new UploadBytesElementReader(
661 kTestData
, kTestDataSize
));
662 element_readers_
.push_back(
663 new UploadFileElementReader(base::MessageLoopProxy::current().get(),
668 scoped_ptr
<UploadDataStream
> stream(
669 new ElementsUploadDataStream(element_readers_
.Pass(), 0));
671 std::vector
<char> expected_data(kTestData
, kTestData
+ kTestDataSize
);
672 expected_data
.insert(expected_data
.end(), kTestData
,
673 kTestData
+ kTestDataSize
);
676 TestCompletionCallback init_callback1
;
677 ASSERT_EQ(ERR_IO_PENDING
, stream
->Init(init_callback1
.callback()));
678 EXPECT_EQ(OK
, init_callback1
.WaitForResult());
679 EXPECT_FALSE(stream
->IsEOF());
680 EXPECT_EQ(kTestDataSize
*2, stream
->size());
683 TestCompletionCallback read_callback1
;
684 std::vector
<char> buf(kTestDataSize
+ kTestDataSize
/2);
685 scoped_refptr
<IOBuffer
> wrapped_buffer
= new WrappedIOBuffer(&buf
[0]);
688 stream
->Read(wrapped_buffer
.get(), buf
.size(),
689 read_callback1
.callback()));
690 EXPECT_EQ(static_cast<int>(buf
.size()), read_callback1
.WaitForResult());
691 EXPECT_EQ(buf
.size(), stream
->position());
693 // Call Init to reset the state.
694 TestCompletionCallback init_callback2
;
695 ASSERT_EQ(ERR_IO_PENDING
, stream
->Init(init_callback2
.callback()));
696 EXPECT_EQ(OK
, init_callback2
.WaitForResult());
697 EXPECT_FALSE(stream
->IsEOF());
698 EXPECT_EQ(kTestDataSize
*2, stream
->size());
701 TestCompletionCallback read_callback2
;
702 std::vector
<char> buf2(kTestDataSize
*2);
703 scoped_refptr
<IOBuffer
> wrapped_buffer2
= new WrappedIOBuffer(&buf2
[0]);
704 EXPECT_EQ(ERR_IO_PENDING
,
706 wrapped_buffer2
.get(), buf2
.size(), read_callback2
.callback()));
707 EXPECT_EQ(static_cast<int>(buf2
.size()), read_callback2
.WaitForResult());
708 EXPECT_EQ(expected_data
, buf2
);
711 TEST_F(ElementsUploadDataStreamTest
, InitDuringAsyncInit
) {
712 base::FilePath temp_file_path
;
713 ASSERT_TRUE(base::CreateTemporaryFileInDir(temp_dir_
.path(),
715 ASSERT_EQ(static_cast<int>(kTestDataSize
),
716 base::WriteFile(temp_file_path
, kTestData
, kTestDataSize
));
719 element_readers_
.push_back(new UploadBytesElementReader(
720 kTestData
, kTestDataSize
));
721 element_readers_
.push_back(
722 new UploadFileElementReader(base::MessageLoopProxy::current().get(),
727 scoped_ptr
<UploadDataStream
> stream(
728 new ElementsUploadDataStream(element_readers_
.Pass(), 0));
730 std::vector
<char> expected_data(kTestData
, kTestData
+ kTestDataSize
);
731 expected_data
.insert(expected_data
.end(), kTestData
,
732 kTestData
+ kTestDataSize
);
735 TestCompletionCallback init_callback1
;
736 EXPECT_EQ(ERR_IO_PENDING
, stream
->Init(init_callback1
.callback()));
738 // Call Init again to cancel the previous init.
739 TestCompletionCallback init_callback2
;
740 EXPECT_EQ(ERR_IO_PENDING
, stream
->Init(init_callback2
.callback()));
741 EXPECT_EQ(OK
, init_callback2
.WaitForResult());
742 EXPECT_FALSE(stream
->IsEOF());
743 EXPECT_EQ(kTestDataSize
*2, stream
->size());
746 TestCompletionCallback read_callback2
;
747 std::vector
<char> buf2(kTestDataSize
*2);
748 scoped_refptr
<IOBuffer
> wrapped_buffer2
= new WrappedIOBuffer(&buf2
[0]);
749 EXPECT_EQ(ERR_IO_PENDING
,
751 wrapped_buffer2
.get(), buf2
.size(), read_callback2
.callback()));
752 EXPECT_EQ(static_cast<int>(buf2
.size()), read_callback2
.WaitForResult());
753 EXPECT_EQ(expected_data
, buf2
);
754 EXPECT_TRUE(stream
->IsEOF());
756 // Make sure callbacks are not called for cancelled operations.
757 EXPECT_FALSE(init_callback1
.have_result());
760 TEST_F(ElementsUploadDataStreamTest
, InitDuringAsyncRead
) {
761 base::FilePath temp_file_path
;
762 ASSERT_TRUE(base::CreateTemporaryFileInDir(temp_dir_
.path(),
764 ASSERT_EQ(static_cast<int>(kTestDataSize
),
765 base::WriteFile(temp_file_path
, kTestData
, kTestDataSize
));
768 element_readers_
.push_back(new UploadBytesElementReader(
769 kTestData
, kTestDataSize
));
770 element_readers_
.push_back(
771 new UploadFileElementReader(base::MessageLoopProxy::current().get(),
776 scoped_ptr
<UploadDataStream
> stream(
777 new ElementsUploadDataStream(element_readers_
.Pass(), 0));
779 std::vector
<char> expected_data(kTestData
, kTestData
+ kTestDataSize
);
780 expected_data
.insert(expected_data
.end(), kTestData
,
781 kTestData
+ kTestDataSize
);
784 TestCompletionCallback init_callback1
;
785 ASSERT_EQ(ERR_IO_PENDING
, stream
->Init(init_callback1
.callback()));
786 EXPECT_EQ(OK
, init_callback1
.WaitForResult());
787 EXPECT_FALSE(stream
->IsEOF());
788 EXPECT_EQ(kTestDataSize
*2, stream
->size());
791 TestCompletionCallback read_callback1
;
792 std::vector
<char> buf(kTestDataSize
*2);
793 scoped_refptr
<IOBuffer
> wrapped_buffer
= new WrappedIOBuffer(&buf
[0]);
796 stream
->Read(wrapped_buffer
.get(), buf
.size(),
797 read_callback1
.callback()));
799 // Call Init to cancel the previous read.
800 TestCompletionCallback init_callback2
;
801 EXPECT_EQ(ERR_IO_PENDING
, stream
->Init(init_callback2
.callback()));
802 EXPECT_EQ(OK
, init_callback2
.WaitForResult());
803 EXPECT_FALSE(stream
->IsEOF());
804 EXPECT_EQ(kTestDataSize
*2, stream
->size());
807 TestCompletionCallback read_callback2
;
808 std::vector
<char> buf2(kTestDataSize
*2);
809 scoped_refptr
<IOBuffer
> wrapped_buffer2
= new WrappedIOBuffer(&buf2
[0]);
810 EXPECT_EQ(ERR_IO_PENDING
,
812 wrapped_buffer2
.get(), buf2
.size(), read_callback2
.callback()));
813 EXPECT_EQ(static_cast<int>(buf2
.size()), read_callback2
.WaitForResult());
814 EXPECT_EQ(expected_data
, buf2
);
815 EXPECT_TRUE(stream
->IsEOF());
817 // Make sure callbacks are not called for cancelled operations.
818 EXPECT_FALSE(read_callback1
.have_result());