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/location.h"
16 #include "base/memory/scoped_ptr.h"
17 #include "base/message_loop/message_loop.h"
18 #include "base/run_loop.h"
19 #include "base/single_thread_task_runner.h"
20 #include "base/thread_task_runner_handle.h"
21 #include "base/time/time.h"
22 #include "net/base/io_buffer.h"
23 #include "net/base/net_errors.h"
24 #include "net/base/test_completion_callback.h"
25 #include "net/base/upload_bytes_element_reader.h"
26 #include "net/base/upload_data_stream.h"
27 #include "net/base/upload_file_element_reader.h"
28 #include "testing/gmock/include/gmock/gmock.h"
29 #include "testing/gtest/include/gtest/gtest.h"
30 #include "testing/platform_test.h"
32 using ::testing::DoAll
;
33 using ::testing::Invoke
;
34 using ::testing::Return
;
41 const char kTestData
[] = "0123456789";
42 const size_t kTestDataSize
= arraysize(kTestData
) - 1;
43 const size_t kTestBufferSize
= 1 << 14; // 16KB.
45 // Reads data from the upload data stream, and returns the data as string.
46 std::string
ReadFromUploadDataStream(UploadDataStream
* stream
) {
47 std::string data_read
;
48 scoped_refptr
<IOBuffer
> buf
= new IOBuffer(kTestBufferSize
);
49 while (!stream
->IsEOF()) {
50 TestCompletionCallback callback
;
52 stream
->Read(buf
.get(), kTestBufferSize
, callback
.callback());
53 const int bytes_read
=
54 result
!= ERR_IO_PENDING
? result
: callback
.WaitForResult();
55 data_read
.append(buf
->data(), bytes_read
);
60 // A mock class of UploadElementReader.
61 class MockUploadElementReader
: public UploadElementReader
{
63 MockUploadElementReader(int content_length
, bool is_in_memory
)
64 : content_length_(content_length
),
65 bytes_remaining_(content_length
),
66 is_in_memory_(is_in_memory
),
70 ~MockUploadElementReader() override
{}
72 // UploadElementReader overrides.
73 MOCK_METHOD1(Init
, int(const CompletionCallback
& callback
));
74 uint64_t GetContentLength() const override
{ return content_length_
; }
75 uint64_t BytesRemaining() const override
{ return bytes_remaining_
; }
76 bool IsInMemory() const override
{ return is_in_memory_
; }
77 MOCK_METHOD3(Read
, int(IOBuffer
* buf
,
79 const CompletionCallback
& callback
));
81 // Sets expectation to return the specified result from Init() asynchronously.
82 void SetAsyncInitExpectation(int result
) {
83 init_result_
= result
;
84 EXPECT_CALL(*this, Init(_
))
85 .WillOnce(DoAll(Invoke(this, &MockUploadElementReader::OnInit
),
86 Return(ERR_IO_PENDING
)));
89 // Sets expectation to return the specified result from Read().
90 void SetReadExpectation(int result
) {
91 read_result_
= result
;
92 EXPECT_CALL(*this, Read(_
, _
, _
))
93 .WillOnce(Invoke(this, &MockUploadElementReader::OnRead
));
97 void OnInit(const CompletionCallback
& callback
) {
98 base::ThreadTaskRunnerHandle::Get()->PostTask(
99 FROM_HERE
, base::Bind(callback
, init_result_
));
102 int OnRead(IOBuffer
* buf
,
104 const CompletionCallback
& callback
) {
105 if (read_result_
> 0)
106 bytes_remaining_
= std::max(0, bytes_remaining_
- read_result_
);
110 base::ThreadTaskRunnerHandle::Get()->PostTask(
111 FROM_HERE
, base::Bind(callback
, read_result_
));
112 return ERR_IO_PENDING
;
117 int bytes_remaining_
;
120 // Result value returned from Init().
123 // Result value returned from Read().
129 class ElementsUploadDataStreamTest
: public PlatformTest
{
131 void SetUp() override
{
132 PlatformTest::SetUp();
133 ASSERT_TRUE(temp_dir_
.CreateUniqueTempDir());
135 ~ElementsUploadDataStreamTest() override
{
136 element_readers_
.clear();
137 base::RunLoop().RunUntilIdle();
140 void FileChangedHelper(const base::FilePath
& file_path
,
141 const base::Time
& time
,
142 bool error_expected
);
144 base::ScopedTempDir temp_dir_
;
145 ScopedVector
<UploadElementReader
> element_readers_
;
148 TEST_F(ElementsUploadDataStreamTest
, EmptyUploadData
) {
149 scoped_ptr
<UploadDataStream
> stream(
150 new ElementsUploadDataStream(element_readers_
.Pass(), 0));
151 ASSERT_EQ(OK
, stream
->Init(CompletionCallback()));
152 EXPECT_TRUE(stream
->IsInMemory());
153 EXPECT_EQ(0U, stream
->size());
154 EXPECT_EQ(0U, stream
->position());
155 EXPECT_TRUE(stream
->IsEOF());
158 TEST_F(ElementsUploadDataStreamTest
, ConsumeAllBytes
) {
159 element_readers_
.push_back(new UploadBytesElementReader(
160 kTestData
, kTestDataSize
));
161 scoped_ptr
<UploadDataStream
> stream(
162 new ElementsUploadDataStream(element_readers_
.Pass(), 0));
163 ASSERT_EQ(OK
, stream
->Init(CompletionCallback()));
164 EXPECT_TRUE(stream
->IsInMemory());
165 EXPECT_EQ(kTestDataSize
, stream
->size());
166 EXPECT_EQ(0U, stream
->position());
167 EXPECT_FALSE(stream
->IsEOF());
168 scoped_refptr
<IOBuffer
> buf
= new IOBuffer(kTestBufferSize
);
169 while (!stream
->IsEOF()) {
171 stream
->Read(buf
.get(), kTestBufferSize
, CompletionCallback());
172 ASSERT_LE(0, bytes_read
); // Not an error.
174 EXPECT_EQ(kTestDataSize
, stream
->position());
175 ASSERT_TRUE(stream
->IsEOF());
178 TEST_F(ElementsUploadDataStreamTest
, File
) {
179 base::FilePath temp_file_path
;
180 ASSERT_TRUE(base::CreateTemporaryFileInDir(temp_dir_
.path(),
182 ASSERT_EQ(static_cast<int>(kTestDataSize
),
183 base::WriteFile(temp_file_path
, kTestData
, kTestDataSize
));
185 element_readers_
.push_back(
186 new UploadFileElementReader(base::ThreadTaskRunnerHandle::Get().get(),
187 temp_file_path
, 0, kuint64max
, base::Time()));
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_t kFakeSize
= kTestDataSize
* 2;
218 UploadFileElementReader::ScopedOverridingContentLengthForTests
219 overriding_content_length(kFakeSize
);
221 element_readers_
.push_back(
222 new UploadFileElementReader(base::ThreadTaskRunnerHandle::Get().get(),
223 temp_file_path
, 0, kuint64max
, base::Time()));
225 TestCompletionCallback init_callback
;
226 scoped_ptr
<UploadDataStream
> stream(
227 new ElementsUploadDataStream(element_readers_
.Pass(), 0));
228 ASSERT_EQ(ERR_IO_PENDING
, stream
->Init(init_callback
.callback()));
229 ASSERT_EQ(OK
, init_callback
.WaitForResult());
230 EXPECT_FALSE(stream
->IsInMemory());
231 EXPECT_EQ(kFakeSize
, stream
->size());
232 EXPECT_EQ(0U, stream
->position());
233 EXPECT_FALSE(stream
->IsEOF());
234 uint64_t read_counter
= 0;
235 scoped_refptr
<IOBuffer
> buf
= new IOBuffer(kTestBufferSize
);
236 while (!stream
->IsEOF()) {
237 TestCompletionCallback read_callback
;
240 stream
->Read(buf
.get(), kTestBufferSize
, read_callback
.callback()));
241 int bytes_read
= read_callback
.WaitForResult();
242 ASSERT_LE(0, bytes_read
); // Not an error.
243 read_counter
+= bytes_read
;
244 EXPECT_EQ(read_counter
, stream
->position());
246 // UpdateDataStream will pad out the file with 0 bytes so that the HTTP
247 // transaction doesn't hang. Therefore we expected the full size.
248 EXPECT_EQ(kFakeSize
, read_counter
);
249 EXPECT_EQ(read_counter
, stream
->position());
252 TEST_F(ElementsUploadDataStreamTest
, ReadErrorSync
) {
253 // This element cannot be read.
254 MockUploadElementReader
* reader
=
255 new MockUploadElementReader(kTestDataSize
, true);
256 EXPECT_CALL(*reader
, Init(_
)).WillOnce(Return(OK
));
257 reader
->SetReadExpectation(ERR_FAILED
);
258 element_readers_
.push_back(reader
);
260 // This element is ignored because of the error from the previous reader.
261 element_readers_
.push_back(new UploadBytesElementReader(
262 kTestData
, kTestDataSize
));
264 scoped_ptr
<UploadDataStream
> stream(
265 new ElementsUploadDataStream(element_readers_
.Pass(), 0));
268 ASSERT_EQ(OK
, stream
->Init(CompletionCallback()));
269 EXPECT_EQ(kTestDataSize
*2, stream
->size());
270 EXPECT_EQ(0U, stream
->position());
271 EXPECT_FALSE(stream
->IsEOF());
273 // Prepare a buffer filled with non-zero data.
274 scoped_refptr
<IOBuffer
> buf
= new IOBuffer(kTestBufferSize
);
275 std::fill_n(buf
->data(), kTestBufferSize
, -1);
277 // Read() results in success even when the reader returns error.
278 EXPECT_EQ(static_cast<int>(kTestDataSize
* 2),
279 stream
->Read(buf
.get(), kTestBufferSize
, CompletionCallback()));
280 EXPECT_EQ(kTestDataSize
* 2, stream
->position());
281 EXPECT_TRUE(stream
->IsEOF());
283 // The buffer is filled with zero.
284 EXPECT_EQ(static_cast<int>(kTestDataSize
*2),
285 std::count(buf
->data(), buf
->data() + kTestBufferSize
, 0));
288 TEST_F(ElementsUploadDataStreamTest
, ReadErrorAsync
) {
289 // This element cannot be read.
290 MockUploadElementReader
* reader
=
291 new MockUploadElementReader(kTestDataSize
, false);
292 reader
->SetAsyncInitExpectation(OK
);
293 reader
->SetReadExpectation(ERR_FAILED
);
294 element_readers_
.push_back(reader
);
296 // This element is ignored because of the error from the previous reader.
297 element_readers_
.push_back(new UploadBytesElementReader(
298 kTestData
, kTestDataSize
));
300 scoped_ptr
<UploadDataStream
> stream(
301 new ElementsUploadDataStream(element_readers_
.Pass(), 0));
304 TestCompletionCallback init_callback
;
305 ASSERT_EQ(ERR_IO_PENDING
, stream
->Init(init_callback
.callback()));
306 EXPECT_EQ(OK
, init_callback
.WaitForResult());
307 EXPECT_EQ(kTestDataSize
*2, stream
->size());
308 EXPECT_EQ(0U, stream
->position());
309 EXPECT_FALSE(stream
->IsEOF());
311 // Prepare a buffer filled with non-zero data.
312 scoped_refptr
<IOBuffer
> buf
= new IOBuffer(kTestBufferSize
);
313 std::fill_n(buf
->data(), kTestBufferSize
, -1);
315 // Read() results in success even when the reader returns error.
316 TestCompletionCallback read_callback
;
317 ASSERT_EQ(ERR_IO_PENDING
,
318 stream
->Read(buf
.get(), kTestBufferSize
, read_callback
.callback()));
319 EXPECT_EQ(static_cast<int>(kTestDataSize
* 2), read_callback
.WaitForResult());
320 EXPECT_EQ(kTestDataSize
*2, stream
->position());
321 EXPECT_TRUE(stream
->IsEOF());
323 // The buffer is filled with zero.
324 EXPECT_EQ(static_cast<int>(kTestDataSize
*2),
325 std::count(buf
->data(), buf
->data() + kTestBufferSize
, 0));
328 TEST_F(ElementsUploadDataStreamTest
, FileAndBytes
) {
329 base::FilePath temp_file_path
;
330 ASSERT_TRUE(base::CreateTemporaryFileInDir(temp_dir_
.path(),
332 ASSERT_EQ(static_cast<int>(kTestDataSize
),
333 base::WriteFile(temp_file_path
, kTestData
, kTestDataSize
));
335 const uint64_t kFileRangeOffset
= 1;
336 const uint64_t kFileRangeLength
= 4;
337 element_readers_
.push_back(new UploadFileElementReader(
338 base::ThreadTaskRunnerHandle::Get().get(), temp_file_path
,
339 kFileRangeOffset
, kFileRangeLength
, base::Time()));
341 element_readers_
.push_back(new UploadBytesElementReader(
342 kTestData
, kTestDataSize
));
344 const uint64_t kStreamSize
= kTestDataSize
+ kFileRangeLength
;
345 TestCompletionCallback init_callback
;
346 scoped_ptr
<UploadDataStream
> stream(
347 new ElementsUploadDataStream(element_readers_
.Pass(), 0));
348 ASSERT_EQ(ERR_IO_PENDING
, stream
->Init(init_callback
.callback()));
349 ASSERT_EQ(OK
, init_callback
.WaitForResult());
350 EXPECT_FALSE(stream
->IsInMemory());
351 EXPECT_EQ(kStreamSize
, stream
->size());
352 EXPECT_EQ(0U, stream
->position());
353 EXPECT_FALSE(stream
->IsEOF());
354 scoped_refptr
<IOBuffer
> buf
= new IOBuffer(kTestBufferSize
);
355 while (!stream
->IsEOF()) {
356 TestCompletionCallback read_callback
;
358 stream
->Read(buf
.get(), kTestBufferSize
, read_callback
.callback());
359 const int bytes_read
=
360 result
!= ERR_IO_PENDING
? result
: read_callback
.WaitForResult();
361 ASSERT_LE(0, bytes_read
); // Not an error.
363 EXPECT_EQ(kStreamSize
, stream
->position());
364 ASSERT_TRUE(stream
->IsEOF());
367 // Init() with on-memory and not-on-memory readers.
368 TEST_F(ElementsUploadDataStreamTest
, InitAsync
) {
369 // Create UploadDataStream with mock readers.
370 MockUploadElementReader
* reader
= NULL
;
372 reader
= new MockUploadElementReader(kTestDataSize
, true);
373 EXPECT_CALL(*reader
, Init(_
)).WillOnce(Return(OK
));
374 element_readers_
.push_back(reader
);
376 reader
= new MockUploadElementReader(kTestDataSize
, true);
377 EXPECT_CALL(*reader
, Init(_
)).WillOnce(Return(OK
));
378 element_readers_
.push_back(reader
);
380 reader
= new MockUploadElementReader(kTestDataSize
, false);
381 reader
->SetAsyncInitExpectation(OK
);
382 element_readers_
.push_back(reader
);
384 reader
= new MockUploadElementReader(kTestDataSize
, false);
385 reader
->SetAsyncInitExpectation(OK
);
386 element_readers_
.push_back(reader
);
388 reader
= new MockUploadElementReader(kTestDataSize
, true);
389 EXPECT_CALL(*reader
, Init(_
)).WillOnce(Return(OK
));
390 element_readers_
.push_back(reader
);
392 scoped_ptr
<UploadDataStream
> stream(
393 new ElementsUploadDataStream(element_readers_
.Pass(), 0));
396 TestCompletionCallback callback
;
397 ASSERT_EQ(ERR_IO_PENDING
, stream
->Init(callback
.callback()));
398 EXPECT_EQ(OK
, callback
.WaitForResult());
401 // Init() of a reader fails asynchronously.
402 TEST_F(ElementsUploadDataStreamTest
, InitAsyncFailureAsync
) {
403 // Create UploadDataStream with a mock reader.
404 MockUploadElementReader
* reader
= NULL
;
406 reader
= new MockUploadElementReader(kTestDataSize
, false);
407 reader
->SetAsyncInitExpectation(ERR_FAILED
);
408 element_readers_
.push_back(reader
);
410 scoped_ptr
<UploadDataStream
> stream(
411 new ElementsUploadDataStream(element_readers_
.Pass(), 0));
414 TestCompletionCallback callback
;
415 ASSERT_EQ(ERR_IO_PENDING
, stream
->Init(callback
.callback()));
416 EXPECT_EQ(ERR_FAILED
, callback
.WaitForResult());
419 // Init() of a reader fails synchronously.
420 TEST_F(ElementsUploadDataStreamTest
, InitAsyncFailureSync
) {
421 // Create UploadDataStream with mock readers.
422 MockUploadElementReader
* reader
= NULL
;
424 reader
= new MockUploadElementReader(kTestDataSize
, false);
425 reader
->SetAsyncInitExpectation(OK
);
426 element_readers_
.push_back(reader
);
428 reader
= new MockUploadElementReader(kTestDataSize
, true);
429 EXPECT_CALL(*reader
, Init(_
)).WillOnce(Return(ERR_FAILED
));
430 element_readers_
.push_back(reader
);
432 scoped_ptr
<UploadDataStream
> stream(
433 new ElementsUploadDataStream(element_readers_
.Pass(), 0));
436 TestCompletionCallback callback
;
437 ASSERT_EQ(ERR_IO_PENDING
, stream
->Init(callback
.callback()));
438 EXPECT_EQ(ERR_FAILED
, callback
.WaitForResult());
441 // Read with a buffer whose size is same as the data.
442 TEST_F(ElementsUploadDataStreamTest
, ReadAsyncWithExactSizeBuffer
) {
443 element_readers_
.push_back(new UploadBytesElementReader(
444 kTestData
, kTestDataSize
));
445 scoped_ptr
<UploadDataStream
> stream(
446 new ElementsUploadDataStream(element_readers_
.Pass(), 0));
448 ASSERT_EQ(OK
, stream
->Init(CompletionCallback()));
449 EXPECT_TRUE(stream
->IsInMemory());
450 EXPECT_EQ(kTestDataSize
, stream
->size());
451 EXPECT_EQ(0U, stream
->position());
452 EXPECT_FALSE(stream
->IsEOF());
453 scoped_refptr
<IOBuffer
> buf
= new IOBuffer(kTestDataSize
);
454 int bytes_read
= stream
->Read(buf
.get(), kTestDataSize
, CompletionCallback());
455 ASSERT_EQ(static_cast<int>(kTestDataSize
), bytes_read
); // Not an error.
456 EXPECT_EQ(kTestDataSize
, stream
->position());
457 ASSERT_TRUE(stream
->IsEOF());
460 // Async Read() with on-memory and not-on-memory readers.
461 TEST_F(ElementsUploadDataStreamTest
, ReadAsync
) {
462 // Create UploadDataStream with mock readers.
463 MockUploadElementReader
* reader
= NULL
;
465 reader
= new MockUploadElementReader(kTestDataSize
, true);
466 EXPECT_CALL(*reader
, Init(_
)).WillOnce(Return(OK
));
467 reader
->SetReadExpectation(kTestDataSize
);
468 element_readers_
.push_back(reader
);
470 reader
= new MockUploadElementReader(kTestDataSize
, false);
471 reader
->SetAsyncInitExpectation(OK
);
472 reader
->SetReadExpectation(kTestDataSize
);
473 element_readers_
.push_back(reader
);
475 reader
= new MockUploadElementReader(kTestDataSize
, true);
476 EXPECT_CALL(*reader
, Init(_
)).WillOnce(Return(OK
));
477 reader
->SetReadExpectation(kTestDataSize
);
478 element_readers_
.push_back(reader
);
480 reader
= new MockUploadElementReader(kTestDataSize
, false);
481 reader
->SetAsyncInitExpectation(OK
);
482 reader
->SetReadExpectation(kTestDataSize
);
483 element_readers_
.push_back(reader
);
485 scoped_ptr
<UploadDataStream
> stream(
486 new ElementsUploadDataStream(element_readers_
.Pass(), 0));
489 TestCompletionCallback init_callback
;
490 EXPECT_EQ(ERR_IO_PENDING
, stream
->Init(init_callback
.callback()));
491 EXPECT_EQ(OK
, init_callback
.WaitForResult());
493 scoped_refptr
<IOBuffer
> buf
= new IOBuffer(kTestBufferSize
);
495 // Consume the first element.
496 TestCompletionCallback read_callback1
;
497 EXPECT_EQ(static_cast<int>(kTestDataSize
),
498 stream
->Read(buf
.get(), kTestDataSize
, read_callback1
.callback()));
499 base::MessageLoop::current()->RunUntilIdle();
500 EXPECT_FALSE(read_callback1
.have_result());
502 // Consume the second element.
503 TestCompletionCallback read_callback2
;
504 ASSERT_EQ(ERR_IO_PENDING
,
505 stream
->Read(buf
.get(), kTestDataSize
, read_callback2
.callback()));
506 EXPECT_EQ(static_cast<int>(kTestDataSize
), read_callback2
.WaitForResult());
508 // Consume the third and the fourth elements.
509 TestCompletionCallback read_callback3
;
512 stream
->Read(buf
.get(), kTestDataSize
* 2, read_callback3
.callback()));
513 EXPECT_EQ(static_cast<int>(kTestDataSize
* 2),
514 read_callback3
.WaitForResult());
517 void ElementsUploadDataStreamTest::FileChangedHelper(
518 const base::FilePath
& file_path
,
519 const base::Time
& time
,
520 bool error_expected
) {
521 // Don't use element_readers_ here, as this function is called twice, and
522 // reusing element_readers_ is wrong.
523 ScopedVector
<UploadElementReader
> element_readers
;
524 element_readers
.push_back(new UploadFileElementReader(
525 base::ThreadTaskRunnerHandle::Get().get(), file_path
, 1, 2, time
));
527 TestCompletionCallback init_callback
;
528 scoped_ptr
<UploadDataStream
> stream(
529 new ElementsUploadDataStream(element_readers
.Pass(), 0));
530 ASSERT_EQ(ERR_IO_PENDING
, stream
->Init(init_callback
.callback()));
531 int error_code
= init_callback
.WaitForResult();
533 ASSERT_EQ(ERR_UPLOAD_FILE_CHANGED
, error_code
);
535 ASSERT_EQ(OK
, error_code
);
538 TEST_F(ElementsUploadDataStreamTest
, FileChanged
) {
539 base::FilePath temp_file_path
;
540 ASSERT_TRUE(base::CreateTemporaryFileInDir(temp_dir_
.path(),
542 ASSERT_EQ(static_cast<int>(kTestDataSize
),
543 base::WriteFile(temp_file_path
, kTestData
, kTestDataSize
));
545 base::File::Info file_info
;
546 ASSERT_TRUE(base::GetFileInfo(temp_file_path
, &file_info
));
548 // Test file not changed.
549 FileChangedHelper(temp_file_path
, file_info
.last_modified
, false);
551 // Test file changed.
552 FileChangedHelper(temp_file_path
,
553 file_info
.last_modified
- base::TimeDelta::FromSeconds(1),
557 TEST_F(ElementsUploadDataStreamTest
, MultipleInit
) {
558 base::FilePath temp_file_path
;
559 ASSERT_TRUE(base::CreateTemporaryFileInDir(temp_dir_
.path(),
561 ASSERT_EQ(static_cast<int>(kTestDataSize
),
562 base::WriteFile(temp_file_path
, kTestData
, kTestDataSize
));
565 element_readers_
.push_back(new UploadBytesElementReader(
566 kTestData
, kTestDataSize
));
567 element_readers_
.push_back(
568 new UploadFileElementReader(base::ThreadTaskRunnerHandle::Get().get(),
569 temp_file_path
, 0, kuint64max
, base::Time()));
570 scoped_ptr
<UploadDataStream
> stream(
571 new ElementsUploadDataStream(element_readers_
.Pass(), 0));
573 std::string
expected_data(kTestData
, kTestData
+ kTestDataSize
);
574 expected_data
+= expected_data
;
577 TestCompletionCallback init_callback1
;
578 ASSERT_EQ(ERR_IO_PENDING
, stream
->Init(init_callback1
.callback()));
579 ASSERT_EQ(OK
, init_callback1
.WaitForResult());
580 EXPECT_FALSE(stream
->IsEOF());
581 EXPECT_EQ(kTestDataSize
*2, stream
->size());
584 EXPECT_EQ(expected_data
, ReadFromUploadDataStream(stream
.get()));
585 EXPECT_TRUE(stream
->IsEOF());
587 // Call Init() again to reset.
588 TestCompletionCallback init_callback2
;
589 ASSERT_EQ(ERR_IO_PENDING
, stream
->Init(init_callback2
.callback()));
590 ASSERT_EQ(OK
, init_callback2
.WaitForResult());
591 EXPECT_FALSE(stream
->IsEOF());
592 EXPECT_EQ(kTestDataSize
*2, stream
->size());
595 EXPECT_EQ(expected_data
, ReadFromUploadDataStream(stream
.get()));
596 EXPECT_TRUE(stream
->IsEOF());
599 TEST_F(ElementsUploadDataStreamTest
, MultipleInitAsync
) {
600 base::FilePath temp_file_path
;
601 ASSERT_TRUE(base::CreateTemporaryFileInDir(temp_dir_
.path(),
603 ASSERT_EQ(static_cast<int>(kTestDataSize
),
604 base::WriteFile(temp_file_path
, kTestData
, kTestDataSize
));
605 TestCompletionCallback test_callback
;
608 element_readers_
.push_back(new UploadBytesElementReader(
609 kTestData
, kTestDataSize
));
610 element_readers_
.push_back(
611 new UploadFileElementReader(base::ThreadTaskRunnerHandle::Get().get(),
612 temp_file_path
, 0, kuint64max
, base::Time()));
613 scoped_ptr
<UploadDataStream
> stream(
614 new ElementsUploadDataStream(element_readers_
.Pass(), 0));
616 std::string
expected_data(kTestData
, kTestData
+ kTestDataSize
);
617 expected_data
+= expected_data
;
620 ASSERT_EQ(ERR_IO_PENDING
, stream
->Init(test_callback
.callback()));
621 EXPECT_EQ(OK
, test_callback
.WaitForResult());
622 EXPECT_FALSE(stream
->IsEOF());
623 EXPECT_EQ(kTestDataSize
*2, stream
->size());
626 EXPECT_EQ(expected_data
, ReadFromUploadDataStream(stream
.get()));
627 EXPECT_TRUE(stream
->IsEOF());
629 // Call Init() again to reset.
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
.get()));
637 EXPECT_TRUE(stream
->IsEOF());
640 TEST_F(ElementsUploadDataStreamTest
, InitToReset
) {
641 base::FilePath temp_file_path
;
642 ASSERT_TRUE(base::CreateTemporaryFileInDir(temp_dir_
.path(),
644 ASSERT_EQ(static_cast<int>(kTestDataSize
),
645 base::WriteFile(temp_file_path
, kTestData
, kTestDataSize
));
648 element_readers_
.push_back(new UploadBytesElementReader(
649 kTestData
, kTestDataSize
));
650 element_readers_
.push_back(
651 new UploadFileElementReader(base::ThreadTaskRunnerHandle::Get().get(),
652 temp_file_path
, 0, kuint64max
, base::Time()));
653 scoped_ptr
<UploadDataStream
> stream(
654 new ElementsUploadDataStream(element_readers_
.Pass(), 0));
656 std::vector
<char> expected_data(kTestData
, kTestData
+ kTestDataSize
);
657 expected_data
.insert(expected_data
.end(), kTestData
,
658 kTestData
+ kTestDataSize
);
661 TestCompletionCallback init_callback1
;
662 ASSERT_EQ(ERR_IO_PENDING
, stream
->Init(init_callback1
.callback()));
663 EXPECT_EQ(OK
, init_callback1
.WaitForResult());
664 EXPECT_FALSE(stream
->IsEOF());
665 EXPECT_EQ(kTestDataSize
*2, stream
->size());
668 TestCompletionCallback read_callback1
;
669 std::vector
<char> buf(kTestDataSize
+ kTestDataSize
/2);
670 scoped_refptr
<IOBuffer
> wrapped_buffer
= new WrappedIOBuffer(&buf
[0]);
673 stream
->Read(wrapped_buffer
.get(), buf
.size(),
674 read_callback1
.callback()));
675 EXPECT_EQ(static_cast<int>(buf
.size()), read_callback1
.WaitForResult());
676 EXPECT_EQ(buf
.size(), stream
->position());
678 // Call Init to reset the state.
679 TestCompletionCallback init_callback2
;
680 ASSERT_EQ(ERR_IO_PENDING
, stream
->Init(init_callback2
.callback()));
681 EXPECT_EQ(OK
, init_callback2
.WaitForResult());
682 EXPECT_FALSE(stream
->IsEOF());
683 EXPECT_EQ(kTestDataSize
*2, stream
->size());
686 TestCompletionCallback read_callback2
;
687 std::vector
<char> buf2(kTestDataSize
*2);
688 scoped_refptr
<IOBuffer
> wrapped_buffer2
= new WrappedIOBuffer(&buf2
[0]);
689 EXPECT_EQ(ERR_IO_PENDING
,
691 wrapped_buffer2
.get(), buf2
.size(), read_callback2
.callback()));
692 EXPECT_EQ(static_cast<int>(buf2
.size()), read_callback2
.WaitForResult());
693 EXPECT_EQ(expected_data
, buf2
);
696 TEST_F(ElementsUploadDataStreamTest
, InitDuringAsyncInit
) {
697 base::FilePath temp_file_path
;
698 ASSERT_TRUE(base::CreateTemporaryFileInDir(temp_dir_
.path(),
700 ASSERT_EQ(static_cast<int>(kTestDataSize
),
701 base::WriteFile(temp_file_path
, kTestData
, kTestDataSize
));
704 element_readers_
.push_back(new UploadBytesElementReader(
705 kTestData
, kTestDataSize
));
706 element_readers_
.push_back(
707 new UploadFileElementReader(base::ThreadTaskRunnerHandle::Get().get(),
708 temp_file_path
, 0, kuint64max
, base::Time()));
709 scoped_ptr
<UploadDataStream
> stream(
710 new ElementsUploadDataStream(element_readers_
.Pass(), 0));
712 std::vector
<char> expected_data(kTestData
, kTestData
+ kTestDataSize
);
713 expected_data
.insert(expected_data
.end(), kTestData
,
714 kTestData
+ kTestDataSize
);
717 TestCompletionCallback init_callback1
;
718 EXPECT_EQ(ERR_IO_PENDING
, stream
->Init(init_callback1
.callback()));
720 // Call Init again to cancel the previous init.
721 TestCompletionCallback init_callback2
;
722 EXPECT_EQ(ERR_IO_PENDING
, stream
->Init(init_callback2
.callback()));
723 EXPECT_EQ(OK
, init_callback2
.WaitForResult());
724 EXPECT_FALSE(stream
->IsEOF());
725 EXPECT_EQ(kTestDataSize
*2, stream
->size());
728 TestCompletionCallback read_callback2
;
729 std::vector
<char> buf2(kTestDataSize
*2);
730 scoped_refptr
<IOBuffer
> wrapped_buffer2
= new WrappedIOBuffer(&buf2
[0]);
731 EXPECT_EQ(ERR_IO_PENDING
,
733 wrapped_buffer2
.get(), buf2
.size(), read_callback2
.callback()));
734 EXPECT_EQ(static_cast<int>(buf2
.size()), read_callback2
.WaitForResult());
735 EXPECT_EQ(expected_data
, buf2
);
736 EXPECT_TRUE(stream
->IsEOF());
738 // Make sure callbacks are not called for cancelled operations.
739 EXPECT_FALSE(init_callback1
.have_result());
742 TEST_F(ElementsUploadDataStreamTest
, InitDuringAsyncRead
) {
743 base::FilePath temp_file_path
;
744 ASSERT_TRUE(base::CreateTemporaryFileInDir(temp_dir_
.path(),
746 ASSERT_EQ(static_cast<int>(kTestDataSize
),
747 base::WriteFile(temp_file_path
, kTestData
, kTestDataSize
));
750 element_readers_
.push_back(new UploadBytesElementReader(
751 kTestData
, kTestDataSize
));
752 element_readers_
.push_back(
753 new UploadFileElementReader(base::ThreadTaskRunnerHandle::Get().get(),
754 temp_file_path
, 0, kuint64max
, base::Time()));
755 scoped_ptr
<UploadDataStream
> stream(
756 new ElementsUploadDataStream(element_readers_
.Pass(), 0));
758 std::vector
<char> expected_data(kTestData
, kTestData
+ kTestDataSize
);
759 expected_data
.insert(expected_data
.end(), kTestData
,
760 kTestData
+ kTestDataSize
);
763 TestCompletionCallback init_callback1
;
764 ASSERT_EQ(ERR_IO_PENDING
, stream
->Init(init_callback1
.callback()));
765 EXPECT_EQ(OK
, init_callback1
.WaitForResult());
766 EXPECT_FALSE(stream
->IsEOF());
767 EXPECT_EQ(kTestDataSize
*2, stream
->size());
770 TestCompletionCallback read_callback1
;
771 std::vector
<char> buf(kTestDataSize
*2);
772 scoped_refptr
<IOBuffer
> wrapped_buffer
= new WrappedIOBuffer(&buf
[0]);
775 stream
->Read(wrapped_buffer
.get(), buf
.size(),
776 read_callback1
.callback()));
778 // Call Init to cancel the previous read.
779 TestCompletionCallback init_callback2
;
780 EXPECT_EQ(ERR_IO_PENDING
, stream
->Init(init_callback2
.callback()));
781 EXPECT_EQ(OK
, init_callback2
.WaitForResult());
782 EXPECT_FALSE(stream
->IsEOF());
783 EXPECT_EQ(kTestDataSize
*2, stream
->size());
786 TestCompletionCallback read_callback2
;
787 std::vector
<char> buf2(kTestDataSize
*2);
788 scoped_refptr
<IOBuffer
> wrapped_buffer2
= new WrappedIOBuffer(&buf2
[0]);
789 EXPECT_EQ(ERR_IO_PENDING
,
791 wrapped_buffer2
.get(), buf2
.size(), read_callback2
.callback()));
792 EXPECT_EQ(static_cast<int>(buf2
.size()), read_callback2
.WaitForResult());
793 EXPECT_EQ(expected_data
, buf2
);
794 EXPECT_TRUE(stream
->IsEOF());
796 // Make sure callbacks are not called for cancelled operations.
797 EXPECT_FALSE(read_callback1
.have_result());