This source file includes following definitions.
- ReadFromUploadDataStream
- read_result_
- GetContentLength
- BytesRemaining
- IsInMemory
- SetAsyncInitExpectation
- SetReadExpectation
- OnInit
- OnRead
- SetUp
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- FileChangedHelper
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
#include "net/base/upload_data_stream.h"
#include <algorithm>
#include <vector>
#include "base/basictypes.h"
#include "base/bind.h"
#include "base/file_util.h"
#include "base/files/file_path.h"
#include "base/files/scoped_temp_dir.h"
#include "base/memory/scoped_ptr.h"
#include "base/message_loop/message_loop.h"
#include "base/run_loop.h"
#include "base/time/time.h"
#include "net/base/io_buffer.h"
#include "net/base/net_errors.h"
#include "net/base/test_completion_callback.h"
#include "net/base/upload_bytes_element_reader.h"
#include "net/base/upload_file_element_reader.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "testing/platform_test.h"
using ::testing::DoAll;
using ::testing::Invoke;
using ::testing::Return;
using ::testing::_;
namespace net {
namespace {
const char kTestData[] = "0123456789";
const size_t kTestDataSize = arraysize(kTestData) - 1;
const size_t kTestBufferSize = 1 << 14;
std::string ReadFromUploadDataStream(UploadDataStream* stream) {
std::string data_read;
scoped_refptr<IOBuffer> buf = new IOBuffer(kTestBufferSize);
while (!stream->IsEOF()) {
TestCompletionCallback callback;
const int result =
stream->Read(buf.get(), kTestBufferSize, callback.callback());
const int bytes_read =
result != ERR_IO_PENDING ? result : callback.WaitForResult();
data_read.append(buf->data(), bytes_read);
}
return data_read;
}
class MockUploadElementReader : public UploadElementReader {
public:
MockUploadElementReader(int content_length, bool is_in_memory)
: content_length_(content_length),
bytes_remaining_(content_length),
is_in_memory_(is_in_memory),
init_result_(OK),
read_result_(OK) {}
virtual ~MockUploadElementReader() {}
MOCK_METHOD1(Init, int(const CompletionCallback& callback));
virtual uint64 GetContentLength() const OVERRIDE { return content_length_; }
virtual uint64 BytesRemaining() const OVERRIDE { return bytes_remaining_; }
virtual bool IsInMemory() const OVERRIDE { return is_in_memory_; }
MOCK_METHOD3(Read, int(IOBuffer* buf,
int buf_length,
const CompletionCallback& callback));
void SetAsyncInitExpectation(int result) {
init_result_ = result;
EXPECT_CALL(*this, Init(_))
.WillOnce(DoAll(Invoke(this, &MockUploadElementReader::OnInit),
Return(ERR_IO_PENDING)));
}
void SetReadExpectation(int result) {
read_result_ = result;
EXPECT_CALL(*this, Read(_, _, _))
.WillOnce(Invoke(this, &MockUploadElementReader::OnRead));
}
private:
void OnInit(const CompletionCallback& callback) {
base::MessageLoop::current()->PostTask(FROM_HERE,
base::Bind(callback, init_result_));
}
int OnRead(IOBuffer* buf,
int buf_length,
const CompletionCallback& callback) {
if (read_result_ > 0)
bytes_remaining_ = std::max(0, bytes_remaining_ - read_result_);
if (IsInMemory()) {
return read_result_;
} else {
base::MessageLoop::current()->PostTask(
FROM_HERE, base::Bind(callback, read_result_));
return ERR_IO_PENDING;
}
}
int content_length_;
int bytes_remaining_;
bool is_in_memory_;
int init_result_;
int read_result_;
};
}
class UploadDataStreamTest : public PlatformTest {
public:
virtual void SetUp() {
PlatformTest::SetUp();
ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
}
virtual ~UploadDataStreamTest() {
element_readers_.clear();
base::RunLoop().RunUntilIdle();
}
void FileChangedHelper(const base::FilePath& file_path,
const base::Time& time,
bool error_expected);
base::ScopedTempDir temp_dir_;
ScopedVector<UploadElementReader> element_readers_;
};
TEST_F(UploadDataStreamTest, EmptyUploadData) {
UploadDataStream stream(element_readers_.Pass(), 0);
ASSERT_EQ(OK, stream.Init(CompletionCallback()));
EXPECT_TRUE(stream.IsInMemory());
EXPECT_EQ(0U, stream.size());
EXPECT_EQ(0U, stream.position());
EXPECT_TRUE(stream.IsEOF());
}
TEST_F(UploadDataStreamTest, ConsumeAllBytes) {
element_readers_.push_back(new UploadBytesElementReader(
kTestData, kTestDataSize));
UploadDataStream stream(element_readers_.Pass(), 0);
ASSERT_EQ(OK, stream.Init(CompletionCallback()));
EXPECT_TRUE(stream.IsInMemory());
EXPECT_EQ(kTestDataSize, stream.size());
EXPECT_EQ(0U, stream.position());
EXPECT_FALSE(stream.IsEOF());
scoped_refptr<IOBuffer> buf = new IOBuffer(kTestBufferSize);
while (!stream.IsEOF()) {
int bytes_read =
stream.Read(buf.get(), kTestBufferSize, CompletionCallback());
ASSERT_LE(0, bytes_read);
}
EXPECT_EQ(kTestDataSize, stream.position());
ASSERT_TRUE(stream.IsEOF());
}
TEST_F(UploadDataStreamTest, File) {
base::FilePath temp_file_path;
ASSERT_TRUE(base::CreateTemporaryFileInDir(temp_dir_.path(),
&temp_file_path));
ASSERT_EQ(static_cast<int>(kTestDataSize),
base::WriteFile(temp_file_path, kTestData, kTestDataSize));
element_readers_.push_back(
new UploadFileElementReader(base::MessageLoopProxy::current().get(),
temp_file_path,
0,
kuint64max,
base::Time()));
TestCompletionCallback init_callback;
UploadDataStream stream(element_readers_.Pass(), 0);
ASSERT_EQ(ERR_IO_PENDING, stream.Init(init_callback.callback()));
ASSERT_EQ(OK, init_callback.WaitForResult());
EXPECT_FALSE(stream.IsInMemory());
EXPECT_EQ(kTestDataSize, stream.size());
EXPECT_EQ(0U, stream.position());
EXPECT_FALSE(stream.IsEOF());
scoped_refptr<IOBuffer> buf = new IOBuffer(kTestBufferSize);
while (!stream.IsEOF()) {
TestCompletionCallback read_callback;
ASSERT_EQ(
ERR_IO_PENDING,
stream.Read(buf.get(), kTestBufferSize, read_callback.callback()));
ASSERT_LE(0, read_callback.WaitForResult());
}
EXPECT_EQ(kTestDataSize, stream.position());
ASSERT_TRUE(stream.IsEOF());
}
TEST_F(UploadDataStreamTest, FileSmallerThanLength) {
base::FilePath temp_file_path;
ASSERT_TRUE(base::CreateTemporaryFileInDir(temp_dir_.path(),
&temp_file_path));
ASSERT_EQ(static_cast<int>(kTestDataSize),
base::WriteFile(temp_file_path, kTestData, kTestDataSize));
const uint64 kFakeSize = kTestDataSize*2;
UploadFileElementReader::ScopedOverridingContentLengthForTests
overriding_content_length(kFakeSize);
element_readers_.push_back(
new UploadFileElementReader(base::MessageLoopProxy::current().get(),
temp_file_path,
0,
kuint64max,
base::Time()));
TestCompletionCallback init_callback;
UploadDataStream stream(element_readers_.Pass(), 0);
ASSERT_EQ(ERR_IO_PENDING, stream.Init(init_callback.callback()));
ASSERT_EQ(OK, init_callback.WaitForResult());
EXPECT_FALSE(stream.IsInMemory());
EXPECT_EQ(kFakeSize, stream.size());
EXPECT_EQ(0U, stream.position());
EXPECT_FALSE(stream.IsEOF());
uint64 read_counter = 0;
scoped_refptr<IOBuffer> buf = new IOBuffer(kTestBufferSize);
while (!stream.IsEOF()) {
TestCompletionCallback read_callback;
ASSERT_EQ(
ERR_IO_PENDING,
stream.Read(buf.get(), kTestBufferSize, read_callback.callback()));
int bytes_read = read_callback.WaitForResult();
ASSERT_LE(0, bytes_read);
read_counter += bytes_read;
EXPECT_EQ(read_counter, stream.position());
}
EXPECT_EQ(kFakeSize, read_counter);
EXPECT_EQ(read_counter, stream.position());
}
TEST_F(UploadDataStreamTest, ReadErrorSync) {
MockUploadElementReader* reader =
new MockUploadElementReader(kTestDataSize, true);
EXPECT_CALL(*reader, Init(_)).WillOnce(Return(OK));
reader->SetReadExpectation(ERR_FAILED);
element_readers_.push_back(reader);
element_readers_.push_back(new UploadBytesElementReader(
kTestData, kTestDataSize));
UploadDataStream stream(element_readers_.Pass(), 0);
ASSERT_EQ(OK, stream.Init(CompletionCallback()));
EXPECT_EQ(kTestDataSize*2, stream.size());
EXPECT_EQ(0U, stream.position());
EXPECT_FALSE(stream.IsEOF());
scoped_refptr<IOBuffer> buf = new IOBuffer(kTestBufferSize);
std::fill_n(buf->data(), kTestBufferSize, -1);
EXPECT_EQ(static_cast<int>(kTestDataSize * 2),
stream.Read(buf.get(), kTestBufferSize, CompletionCallback()));
EXPECT_EQ(kTestDataSize * 2, stream.position());
EXPECT_TRUE(stream.IsEOF());
EXPECT_EQ(static_cast<int>(kTestDataSize*2),
std::count(buf->data(), buf->data() + kTestBufferSize, 0));
}
TEST_F(UploadDataStreamTest, ReadErrorAsync) {
MockUploadElementReader* reader =
new MockUploadElementReader(kTestDataSize, false);
reader->SetAsyncInitExpectation(OK);
reader->SetReadExpectation(ERR_FAILED);
element_readers_.push_back(reader);
element_readers_.push_back(new UploadBytesElementReader(
kTestData, kTestDataSize));
UploadDataStream stream(element_readers_.Pass(), 0);
TestCompletionCallback init_callback;
ASSERT_EQ(ERR_IO_PENDING, stream.Init(init_callback.callback()));
EXPECT_EQ(OK, init_callback.WaitForResult());
EXPECT_EQ(kTestDataSize*2, stream.size());
EXPECT_EQ(0U, stream.position());
EXPECT_FALSE(stream.IsEOF());
scoped_refptr<IOBuffer> buf = new IOBuffer(kTestBufferSize);
std::fill_n(buf->data(), kTestBufferSize, -1);
TestCompletionCallback read_callback;
ASSERT_EQ(ERR_IO_PENDING,
stream.Read(buf.get(), kTestBufferSize, read_callback.callback()));
EXPECT_EQ(static_cast<int>(kTestDataSize * 2), read_callback.WaitForResult());
EXPECT_EQ(kTestDataSize*2, stream.position());
EXPECT_TRUE(stream.IsEOF());
EXPECT_EQ(static_cast<int>(kTestDataSize*2),
std::count(buf->data(), buf->data() + kTestBufferSize, 0));
}
TEST_F(UploadDataStreamTest, FileAndBytes) {
base::FilePath temp_file_path;
ASSERT_TRUE(base::CreateTemporaryFileInDir(temp_dir_.path(),
&temp_file_path));
ASSERT_EQ(static_cast<int>(kTestDataSize),
base::WriteFile(temp_file_path, kTestData, kTestDataSize));
const uint64 kFileRangeOffset = 1;
const uint64 kFileRangeLength = 4;
element_readers_.push_back(
new UploadFileElementReader(base::MessageLoopProxy::current().get(),
temp_file_path,
kFileRangeOffset,
kFileRangeLength,
base::Time()));
element_readers_.push_back(new UploadBytesElementReader(
kTestData, kTestDataSize));
const uint64 kStreamSize = kTestDataSize + kFileRangeLength;
TestCompletionCallback init_callback;
UploadDataStream stream(element_readers_.Pass(), 0);
ASSERT_EQ(ERR_IO_PENDING, stream.Init(init_callback.callback()));
ASSERT_EQ(OK, init_callback.WaitForResult());
EXPECT_FALSE(stream.IsInMemory());
EXPECT_EQ(kStreamSize, stream.size());
EXPECT_EQ(0U, stream.position());
EXPECT_FALSE(stream.IsEOF());
scoped_refptr<IOBuffer> buf = new IOBuffer(kTestBufferSize);
while (!stream.IsEOF()) {
TestCompletionCallback read_callback;
const int result =
stream.Read(buf.get(), kTestBufferSize, read_callback.callback());
const int bytes_read =
result != ERR_IO_PENDING ? result : read_callback.WaitForResult();
ASSERT_LE(0, bytes_read);
}
EXPECT_EQ(kStreamSize, stream.position());
ASSERT_TRUE(stream.IsEOF());
}
TEST_F(UploadDataStreamTest, Chunk) {
const uint64 kStreamSize = kTestDataSize*2;
UploadDataStream stream(UploadDataStream::CHUNKED, 0);
stream.AppendChunk(kTestData, kTestDataSize, false);
stream.AppendChunk(kTestData, kTestDataSize, true);
ASSERT_EQ(OK, stream.Init(CompletionCallback()));
EXPECT_FALSE(stream.IsInMemory());
EXPECT_EQ(0U, stream.size());
EXPECT_EQ(0U, stream.position());
EXPECT_FALSE(stream.IsEOF());
scoped_refptr<IOBuffer> buf = new IOBuffer(kTestBufferSize);
while (!stream.IsEOF()) {
int bytes_read =
stream.Read(buf.get(), kTestBufferSize, CompletionCallback());
ASSERT_LE(0, bytes_read);
}
EXPECT_EQ(kStreamSize, stream.position());
ASSERT_TRUE(stream.IsEOF());
}
TEST_F(UploadDataStreamTest, InitAsync) {
MockUploadElementReader* reader = NULL;
reader = new MockUploadElementReader(kTestDataSize, true);
EXPECT_CALL(*reader, Init(_)).WillOnce(Return(OK));
element_readers_.push_back(reader);
reader = new MockUploadElementReader(kTestDataSize, true);
EXPECT_CALL(*reader, Init(_)).WillOnce(Return(OK));
element_readers_.push_back(reader);
reader = new MockUploadElementReader(kTestDataSize, false);
reader->SetAsyncInitExpectation(OK);
element_readers_.push_back(reader);
reader = new MockUploadElementReader(kTestDataSize, false);
reader->SetAsyncInitExpectation(OK);
element_readers_.push_back(reader);
reader = new MockUploadElementReader(kTestDataSize, true);
EXPECT_CALL(*reader, Init(_)).WillOnce(Return(OK));
element_readers_.push_back(reader);
UploadDataStream stream(element_readers_.Pass(), 0);
TestCompletionCallback callback;
ASSERT_EQ(ERR_IO_PENDING, stream.Init(callback.callback()));
EXPECT_EQ(OK, callback.WaitForResult());
}
TEST_F(UploadDataStreamTest, InitAsyncFailureAsync) {
MockUploadElementReader* reader = NULL;
reader = new MockUploadElementReader(kTestDataSize, false);
reader->SetAsyncInitExpectation(ERR_FAILED);
element_readers_.push_back(reader);
UploadDataStream stream(element_readers_.Pass(), 0);
TestCompletionCallback callback;
ASSERT_EQ(ERR_IO_PENDING, stream.Init(callback.callback()));
EXPECT_EQ(ERR_FAILED, callback.WaitForResult());
}
TEST_F(UploadDataStreamTest, InitAsyncFailureSync) {
MockUploadElementReader* reader = NULL;
reader = new MockUploadElementReader(kTestDataSize, false);
reader->SetAsyncInitExpectation(OK);
element_readers_.push_back(reader);
reader = new MockUploadElementReader(kTestDataSize, true);
EXPECT_CALL(*reader, Init(_)).WillOnce(Return(ERR_FAILED));
element_readers_.push_back(reader);
UploadDataStream stream(element_readers_.Pass(), 0);
TestCompletionCallback callback;
ASSERT_EQ(ERR_IO_PENDING, stream.Init(callback.callback()));
EXPECT_EQ(ERR_FAILED, callback.WaitForResult());
}
TEST_F(UploadDataStreamTest, ReadAsyncWithExactSizeBuffer) {
element_readers_.push_back(new UploadBytesElementReader(
kTestData, kTestDataSize));
UploadDataStream stream(element_readers_.Pass(), 0);
ASSERT_EQ(OK, stream.Init(CompletionCallback()));
EXPECT_TRUE(stream.IsInMemory());
EXPECT_EQ(kTestDataSize, stream.size());
EXPECT_EQ(0U, stream.position());
EXPECT_FALSE(stream.IsEOF());
scoped_refptr<IOBuffer> buf = new IOBuffer(kTestDataSize);
int bytes_read = stream.Read(buf.get(), kTestDataSize, CompletionCallback());
ASSERT_EQ(static_cast<int>(kTestDataSize), bytes_read);
EXPECT_EQ(kTestDataSize, stream.position());
ASSERT_TRUE(stream.IsEOF());
}
TEST_F(UploadDataStreamTest, ReadAsync) {
MockUploadElementReader* reader = NULL;
reader = new MockUploadElementReader(kTestDataSize, true);
EXPECT_CALL(*reader, Init(_)).WillOnce(Return(OK));
reader->SetReadExpectation(kTestDataSize);
element_readers_.push_back(reader);
reader = new MockUploadElementReader(kTestDataSize, false);
reader->SetAsyncInitExpectation(OK);
reader->SetReadExpectation(kTestDataSize);
element_readers_.push_back(reader);
reader = new MockUploadElementReader(kTestDataSize, true);
EXPECT_CALL(*reader, Init(_)).WillOnce(Return(OK));
reader->SetReadExpectation(kTestDataSize);
element_readers_.push_back(reader);
reader = new MockUploadElementReader(kTestDataSize, false);
reader->SetAsyncInitExpectation(OK);
reader->SetReadExpectation(kTestDataSize);
element_readers_.push_back(reader);
UploadDataStream stream(element_readers_.Pass(), 0);
TestCompletionCallback init_callback;
EXPECT_EQ(ERR_IO_PENDING, stream.Init(init_callback.callback()));
EXPECT_EQ(OK, init_callback.WaitForResult());
scoped_refptr<IOBuffer> buf = new IOBuffer(kTestBufferSize);
TestCompletionCallback read_callback1;
EXPECT_EQ(static_cast<int>(kTestDataSize),
stream.Read(buf.get(), kTestDataSize, read_callback1.callback()));
base::MessageLoop::current()->RunUntilIdle();
EXPECT_FALSE(read_callback1.have_result());
TestCompletionCallback read_callback2;
ASSERT_EQ(ERR_IO_PENDING,
stream.Read(buf.get(), kTestDataSize, read_callback2.callback()));
EXPECT_EQ(static_cast<int>(kTestDataSize), read_callback2.WaitForResult());
TestCompletionCallback read_callback3;
ASSERT_EQ(
ERR_IO_PENDING,
stream.Read(buf.get(), kTestDataSize * 2, read_callback3.callback()));
EXPECT_EQ(static_cast<int>(kTestDataSize * 2),
read_callback3.WaitForResult());
}
void UploadDataStreamTest::FileChangedHelper(const base::FilePath& file_path,
const base::Time& time,
bool error_expected) {
ScopedVector<UploadElementReader> element_readers;
element_readers.push_back(new UploadFileElementReader(
base::MessageLoopProxy::current().get(), file_path, 1, 2, time));
TestCompletionCallback init_callback;
UploadDataStream stream(element_readers.Pass(), 0);
ASSERT_EQ(ERR_IO_PENDING, stream.Init(init_callback.callback()));
int error_code = init_callback.WaitForResult();
if (error_expected)
ASSERT_EQ(ERR_UPLOAD_FILE_CHANGED, error_code);
else
ASSERT_EQ(OK, error_code);
}
TEST_F(UploadDataStreamTest, FileChanged) {
base::FilePath temp_file_path;
ASSERT_TRUE(base::CreateTemporaryFileInDir(temp_dir_.path(),
&temp_file_path));
ASSERT_EQ(static_cast<int>(kTestDataSize),
base::WriteFile(temp_file_path, kTestData, kTestDataSize));
base::File::Info file_info;
ASSERT_TRUE(base::GetFileInfo(temp_file_path, &file_info));
FileChangedHelper(temp_file_path, file_info.last_modified, false);
FileChangedHelper(temp_file_path,
file_info.last_modified - base::TimeDelta::FromSeconds(1),
true);
}
TEST_F(UploadDataStreamTest, MultipleInit) {
base::FilePath temp_file_path;
ASSERT_TRUE(base::CreateTemporaryFileInDir(temp_dir_.path(),
&temp_file_path));
ASSERT_EQ(static_cast<int>(kTestDataSize),
base::WriteFile(temp_file_path, kTestData, kTestDataSize));
element_readers_.push_back(new UploadBytesElementReader(
kTestData, kTestDataSize));
element_readers_.push_back(
new UploadFileElementReader(base::MessageLoopProxy::current().get(),
temp_file_path,
0,
kuint64max,
base::Time()));
UploadDataStream stream(element_readers_.Pass(), 0);
std::string expected_data(kTestData, kTestData + kTestDataSize);
expected_data += expected_data;
TestCompletionCallback init_callback1;
ASSERT_EQ(ERR_IO_PENDING, stream.Init(init_callback1.callback()));
ASSERT_EQ(OK, init_callback1.WaitForResult());
EXPECT_FALSE(stream.IsEOF());
EXPECT_EQ(kTestDataSize*2, stream.size());
EXPECT_EQ(expected_data, ReadFromUploadDataStream(&stream));
EXPECT_TRUE(stream.IsEOF());
TestCompletionCallback init_callback2;
ASSERT_EQ(ERR_IO_PENDING, stream.Init(init_callback2.callback()));
ASSERT_EQ(OK, init_callback2.WaitForResult());
EXPECT_FALSE(stream.IsEOF());
EXPECT_EQ(kTestDataSize*2, stream.size());
EXPECT_EQ(expected_data, ReadFromUploadDataStream(&stream));
EXPECT_TRUE(stream.IsEOF());
}
TEST_F(UploadDataStreamTest, MultipleInitAsync) {
base::FilePath temp_file_path;
ASSERT_TRUE(base::CreateTemporaryFileInDir(temp_dir_.path(),
&temp_file_path));
ASSERT_EQ(static_cast<int>(kTestDataSize),
base::WriteFile(temp_file_path, kTestData, kTestDataSize));
TestCompletionCallback test_callback;
element_readers_.push_back(new UploadBytesElementReader(
kTestData, kTestDataSize));
element_readers_.push_back(
new UploadFileElementReader(base::MessageLoopProxy::current().get(),
temp_file_path,
0,
kuint64max,
base::Time()));
UploadDataStream stream(element_readers_.Pass(), 0);
std::string expected_data(kTestData, kTestData + kTestDataSize);
expected_data += expected_data;
ASSERT_EQ(ERR_IO_PENDING, stream.Init(test_callback.callback()));
EXPECT_EQ(OK, test_callback.WaitForResult());
EXPECT_FALSE(stream.IsEOF());
EXPECT_EQ(kTestDataSize*2, stream.size());
EXPECT_EQ(expected_data, ReadFromUploadDataStream(&stream));
EXPECT_TRUE(stream.IsEOF());
ASSERT_EQ(ERR_IO_PENDING, stream.Init(test_callback.callback()));
EXPECT_EQ(OK, test_callback.WaitForResult());
EXPECT_FALSE(stream.IsEOF());
EXPECT_EQ(kTestDataSize*2, stream.size());
EXPECT_EQ(expected_data, ReadFromUploadDataStream(&stream));
EXPECT_TRUE(stream.IsEOF());
}
TEST_F(UploadDataStreamTest, InitToReset) {
base::FilePath temp_file_path;
ASSERT_TRUE(base::CreateTemporaryFileInDir(temp_dir_.path(),
&temp_file_path));
ASSERT_EQ(static_cast<int>(kTestDataSize),
base::WriteFile(temp_file_path, kTestData, kTestDataSize));
element_readers_.push_back(new UploadBytesElementReader(
kTestData, kTestDataSize));
element_readers_.push_back(
new UploadFileElementReader(base::MessageLoopProxy::current().get(),
temp_file_path,
0,
kuint64max,
base::Time()));
UploadDataStream stream(element_readers_.Pass(), 0);
std::vector<char> expected_data(kTestData, kTestData + kTestDataSize);
expected_data.insert(expected_data.end(), expected_data.begin(),
expected_data.begin() + kTestDataSize);
TestCompletionCallback init_callback1;
ASSERT_EQ(ERR_IO_PENDING, stream.Init(init_callback1.callback()));
EXPECT_EQ(OK, init_callback1.WaitForResult());
EXPECT_FALSE(stream.IsEOF());
EXPECT_EQ(kTestDataSize*2, stream.size());
TestCompletionCallback read_callback1;
std::vector<char> buf(kTestDataSize + kTestDataSize/2);
scoped_refptr<IOBuffer> wrapped_buffer = new WrappedIOBuffer(&buf[0]);
EXPECT_EQ(
ERR_IO_PENDING,
stream.Read(wrapped_buffer.get(), buf.size(), read_callback1.callback()));
EXPECT_EQ(static_cast<int>(buf.size()), read_callback1.WaitForResult());
EXPECT_EQ(buf.size(), stream.position());
TestCompletionCallback init_callback2;
ASSERT_EQ(ERR_IO_PENDING, stream.Init(init_callback2.callback()));
EXPECT_EQ(OK, init_callback2.WaitForResult());
EXPECT_FALSE(stream.IsEOF());
EXPECT_EQ(kTestDataSize*2, stream.size());
TestCompletionCallback read_callback2;
std::vector<char> buf2(kTestDataSize*2);
scoped_refptr<IOBuffer> wrapped_buffer2 = new WrappedIOBuffer(&buf2[0]);
EXPECT_EQ(ERR_IO_PENDING,
stream.Read(
wrapped_buffer2.get(), buf2.size(), read_callback2.callback()));
EXPECT_EQ(static_cast<int>(buf2.size()), read_callback2.WaitForResult());
EXPECT_EQ(expected_data, buf2);
}
TEST_F(UploadDataStreamTest, InitDuringAsyncInit) {
base::FilePath temp_file_path;
ASSERT_TRUE(base::CreateTemporaryFileInDir(temp_dir_.path(),
&temp_file_path));
ASSERT_EQ(static_cast<int>(kTestDataSize),
base::WriteFile(temp_file_path, kTestData, kTestDataSize));
element_readers_.push_back(new UploadBytesElementReader(
kTestData, kTestDataSize));
element_readers_.push_back(
new UploadFileElementReader(base::MessageLoopProxy::current().get(),
temp_file_path,
0,
kuint64max,
base::Time()));
UploadDataStream stream(element_readers_.Pass(), 0);
std::vector<char> expected_data(kTestData, kTestData + kTestDataSize);
expected_data.insert(expected_data.end(), expected_data.begin(),
expected_data.begin() + kTestDataSize);
TestCompletionCallback init_callback1;
EXPECT_EQ(ERR_IO_PENDING, stream.Init(init_callback1.callback()));
TestCompletionCallback init_callback2;
EXPECT_EQ(ERR_IO_PENDING, stream.Init(init_callback2.callback()));
EXPECT_EQ(OK, init_callback2.WaitForResult());
EXPECT_FALSE(stream.IsEOF());
EXPECT_EQ(kTestDataSize*2, stream.size());
TestCompletionCallback read_callback2;
std::vector<char> buf2(kTestDataSize*2);
scoped_refptr<IOBuffer> wrapped_buffer2 = new WrappedIOBuffer(&buf2[0]);
EXPECT_EQ(ERR_IO_PENDING,
stream.Read(
wrapped_buffer2.get(), buf2.size(), read_callback2.callback()));
EXPECT_EQ(static_cast<int>(buf2.size()), read_callback2.WaitForResult());
EXPECT_EQ(expected_data, buf2);
EXPECT_TRUE(stream.IsEOF());
EXPECT_FALSE(init_callback1.have_result());
}
TEST_F(UploadDataStreamTest, InitDuringAsyncRead) {
base::FilePath temp_file_path;
ASSERT_TRUE(base::CreateTemporaryFileInDir(temp_dir_.path(),
&temp_file_path));
ASSERT_EQ(static_cast<int>(kTestDataSize),
base::WriteFile(temp_file_path, kTestData, kTestDataSize));
element_readers_.push_back(new UploadBytesElementReader(
kTestData, kTestDataSize));
element_readers_.push_back(
new UploadFileElementReader(base::MessageLoopProxy::current().get(),
temp_file_path,
0,
kuint64max,
base::Time()));
UploadDataStream stream(element_readers_.Pass(), 0);
std::vector<char> expected_data(kTestData, kTestData + kTestDataSize);
expected_data.insert(expected_data.end(), expected_data.begin(),
expected_data.begin() + kTestDataSize);
TestCompletionCallback init_callback1;
ASSERT_EQ(ERR_IO_PENDING, stream.Init(init_callback1.callback()));
EXPECT_EQ(OK, init_callback1.WaitForResult());
EXPECT_FALSE(stream.IsEOF());
EXPECT_EQ(kTestDataSize*2, stream.size());
TestCompletionCallback read_callback1;
std::vector<char> buf(kTestDataSize*2);
scoped_refptr<IOBuffer> wrapped_buffer = new WrappedIOBuffer(&buf[0]);
EXPECT_EQ(
ERR_IO_PENDING,
stream.Read(wrapped_buffer.get(), buf.size(), read_callback1.callback()));
TestCompletionCallback init_callback2;
EXPECT_EQ(ERR_IO_PENDING, stream.Init(init_callback2.callback()));
EXPECT_EQ(OK, init_callback2.WaitForResult());
EXPECT_FALSE(stream.IsEOF());
EXPECT_EQ(kTestDataSize*2, stream.size());
TestCompletionCallback read_callback2;
std::vector<char> buf2(kTestDataSize*2);
scoped_refptr<IOBuffer> wrapped_buffer2 = new WrappedIOBuffer(&buf2[0]);
EXPECT_EQ(ERR_IO_PENDING,
stream.Read(
wrapped_buffer2.get(), buf2.size(), read_callback2.callback()));
EXPECT_EQ(static_cast<int>(buf2.size()), read_callback2.WaitForResult());
EXPECT_EQ(expected_data, buf2);
EXPECT_TRUE(stream.IsEOF());
EXPECT_FALSE(read_callback1.have_result());
}
}