| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "net/base/elements_upload_data_stream.h" | 5 #include "net/base/elements_upload_data_stream.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <vector> | 8 #include <vector> |
| 9 | 9 |
| 10 #include "base/basictypes.h" | 10 #include "base/basictypes.h" |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 61 : content_length_(content_length), | 61 : content_length_(content_length), |
| 62 bytes_remaining_(content_length), | 62 bytes_remaining_(content_length), |
| 63 is_in_memory_(is_in_memory), | 63 is_in_memory_(is_in_memory), |
| 64 init_result_(OK), | 64 init_result_(OK), |
| 65 read_result_(OK) {} | 65 read_result_(OK) {} |
| 66 | 66 |
| 67 ~MockUploadElementReader() override {} | 67 ~MockUploadElementReader() override {} |
| 68 | 68 |
| 69 // UploadElementReader overrides. | 69 // UploadElementReader overrides. |
| 70 MOCK_METHOD1(Init, int(const CompletionCallback& callback)); | 70 MOCK_METHOD1(Init, int(const CompletionCallback& callback)); |
| 71 uint64 GetContentLength() const override { return content_length_; } | 71 uint64_t GetContentLength() const override { return content_length_; } |
| 72 uint64 BytesRemaining() const override { return bytes_remaining_; } | 72 uint64_t BytesRemaining() const override { return bytes_remaining_; } |
| 73 bool IsInMemory() const override { return is_in_memory_; } | 73 bool IsInMemory() const override { return is_in_memory_; } |
| 74 MOCK_METHOD3(Read, int(IOBuffer* buf, | 74 MOCK_METHOD3(Read, int(IOBuffer* buf, |
| 75 int buf_length, | 75 int buf_length, |
| 76 const CompletionCallback& callback)); | 76 const CompletionCallback& callback)); |
| 77 | 77 |
| 78 // Sets expectation to return the specified result from Init() asynchronously. | 78 // Sets expectation to return the specified result from Init() asynchronously. |
| 79 void SetAsyncInitExpectation(int result) { | 79 void SetAsyncInitExpectation(int result) { |
| 80 init_result_ = result; | 80 init_result_ = result; |
| 81 EXPECT_CALL(*this, Init(_)) | 81 EXPECT_CALL(*this, Init(_)) |
| 82 .WillOnce(DoAll(Invoke(this, &MockUploadElementReader::OnInit), | 82 .WillOnce(DoAll(Invoke(this, &MockUploadElementReader::OnInit), |
| (...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 206 EXPECT_EQ(kTestDataSize, stream->position()); | 206 EXPECT_EQ(kTestDataSize, stream->position()); |
| 207 ASSERT_TRUE(stream->IsEOF()); | 207 ASSERT_TRUE(stream->IsEOF()); |
| 208 } | 208 } |
| 209 | 209 |
| 210 TEST_F(ElementsUploadDataStreamTest, FileSmallerThanLength) { | 210 TEST_F(ElementsUploadDataStreamTest, FileSmallerThanLength) { |
| 211 base::FilePath temp_file_path; | 211 base::FilePath temp_file_path; |
| 212 ASSERT_TRUE(base::CreateTemporaryFileInDir(temp_dir_.path(), | 212 ASSERT_TRUE(base::CreateTemporaryFileInDir(temp_dir_.path(), |
| 213 &temp_file_path)); | 213 &temp_file_path)); |
| 214 ASSERT_EQ(static_cast<int>(kTestDataSize), | 214 ASSERT_EQ(static_cast<int>(kTestDataSize), |
| 215 base::WriteFile(temp_file_path, kTestData, kTestDataSize)); | 215 base::WriteFile(temp_file_path, kTestData, kTestDataSize)); |
| 216 const uint64 kFakeSize = kTestDataSize*2; | 216 const uint64_t kFakeSize = kTestDataSize * 2; |
| 217 | 217 |
| 218 UploadFileElementReader::ScopedOverridingContentLengthForTests | 218 UploadFileElementReader::ScopedOverridingContentLengthForTests |
| 219 overriding_content_length(kFakeSize); | 219 overriding_content_length(kFakeSize); |
| 220 | 220 |
| 221 element_readers_.push_back( | 221 element_readers_.push_back( |
| 222 new UploadFileElementReader(base::MessageLoopProxy::current().get(), | 222 new UploadFileElementReader(base::MessageLoopProxy::current().get(), |
| 223 temp_file_path, | 223 temp_file_path, |
| 224 0, | 224 0, |
| 225 kuint64max, | 225 kuint64max, |
| 226 base::Time())); | 226 base::Time())); |
| 227 | 227 |
| 228 TestCompletionCallback init_callback; | 228 TestCompletionCallback init_callback; |
| 229 scoped_ptr<UploadDataStream> stream( | 229 scoped_ptr<UploadDataStream> stream( |
| 230 new ElementsUploadDataStream(element_readers_.Pass(), 0)); | 230 new ElementsUploadDataStream(element_readers_.Pass(), 0)); |
| 231 ASSERT_EQ(ERR_IO_PENDING, stream->Init(init_callback.callback())); | 231 ASSERT_EQ(ERR_IO_PENDING, stream->Init(init_callback.callback())); |
| 232 ASSERT_EQ(OK, init_callback.WaitForResult()); | 232 ASSERT_EQ(OK, init_callback.WaitForResult()); |
| 233 EXPECT_FALSE(stream->IsInMemory()); | 233 EXPECT_FALSE(stream->IsInMemory()); |
| 234 EXPECT_EQ(kFakeSize, stream->size()); | 234 EXPECT_EQ(kFakeSize, stream->size()); |
| 235 EXPECT_EQ(0U, stream->position()); | 235 EXPECT_EQ(0U, stream->position()); |
| 236 EXPECT_FALSE(stream->IsEOF()); | 236 EXPECT_FALSE(stream->IsEOF()); |
| 237 uint64 read_counter = 0; | 237 uint64_t read_counter = 0; |
| 238 scoped_refptr<IOBuffer> buf = new IOBuffer(kTestBufferSize); | 238 scoped_refptr<IOBuffer> buf = new IOBuffer(kTestBufferSize); |
| 239 while (!stream->IsEOF()) { | 239 while (!stream->IsEOF()) { |
| 240 TestCompletionCallback read_callback; | 240 TestCompletionCallback read_callback; |
| 241 ASSERT_EQ( | 241 ASSERT_EQ( |
| 242 ERR_IO_PENDING, | 242 ERR_IO_PENDING, |
| 243 stream->Read(buf.get(), kTestBufferSize, read_callback.callback())); | 243 stream->Read(buf.get(), kTestBufferSize, read_callback.callback())); |
| 244 int bytes_read = read_callback.WaitForResult(); | 244 int bytes_read = read_callback.WaitForResult(); |
| 245 ASSERT_LE(0, bytes_read); // Not an error. | 245 ASSERT_LE(0, bytes_read); // Not an error. |
| 246 read_counter += bytes_read; | 246 read_counter += bytes_read; |
| 247 EXPECT_EQ(read_counter, stream->position()); | 247 EXPECT_EQ(read_counter, stream->position()); |
| (...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 328 std::count(buf->data(), buf->data() + kTestBufferSize, 0)); | 328 std::count(buf->data(), buf->data() + kTestBufferSize, 0)); |
| 329 } | 329 } |
| 330 | 330 |
| 331 TEST_F(ElementsUploadDataStreamTest, FileAndBytes) { | 331 TEST_F(ElementsUploadDataStreamTest, FileAndBytes) { |
| 332 base::FilePath temp_file_path; | 332 base::FilePath temp_file_path; |
| 333 ASSERT_TRUE(base::CreateTemporaryFileInDir(temp_dir_.path(), | 333 ASSERT_TRUE(base::CreateTemporaryFileInDir(temp_dir_.path(), |
| 334 &temp_file_path)); | 334 &temp_file_path)); |
| 335 ASSERT_EQ(static_cast<int>(kTestDataSize), | 335 ASSERT_EQ(static_cast<int>(kTestDataSize), |
| 336 base::WriteFile(temp_file_path, kTestData, kTestDataSize)); | 336 base::WriteFile(temp_file_path, kTestData, kTestDataSize)); |
| 337 | 337 |
| 338 const uint64 kFileRangeOffset = 1; | 338 const uint64_t kFileRangeOffset = 1; |
| 339 const uint64 kFileRangeLength = 4; | 339 const uint64_t kFileRangeLength = 4; |
| 340 element_readers_.push_back( | 340 element_readers_.push_back( |
| 341 new UploadFileElementReader(base::MessageLoopProxy::current().get(), | 341 new UploadFileElementReader(base::MessageLoopProxy::current().get(), |
| 342 temp_file_path, | 342 temp_file_path, |
| 343 kFileRangeOffset, | 343 kFileRangeOffset, |
| 344 kFileRangeLength, | 344 kFileRangeLength, |
| 345 base::Time())); | 345 base::Time())); |
| 346 | 346 |
| 347 element_readers_.push_back(new UploadBytesElementReader( | 347 element_readers_.push_back(new UploadBytesElementReader( |
| 348 kTestData, kTestDataSize)); | 348 kTestData, kTestDataSize)); |
| 349 | 349 |
| 350 const uint64 kStreamSize = kTestDataSize + kFileRangeLength; | 350 const uint64_t kStreamSize = kTestDataSize + kFileRangeLength; |
| 351 TestCompletionCallback init_callback; | 351 TestCompletionCallback init_callback; |
| 352 scoped_ptr<UploadDataStream> stream( | 352 scoped_ptr<UploadDataStream> stream( |
| 353 new ElementsUploadDataStream(element_readers_.Pass(), 0)); | 353 new ElementsUploadDataStream(element_readers_.Pass(), 0)); |
| 354 ASSERT_EQ(ERR_IO_PENDING, stream->Init(init_callback.callback())); | 354 ASSERT_EQ(ERR_IO_PENDING, stream->Init(init_callback.callback())); |
| 355 ASSERT_EQ(OK, init_callback.WaitForResult()); | 355 ASSERT_EQ(OK, init_callback.WaitForResult()); |
| 356 EXPECT_FALSE(stream->IsInMemory()); | 356 EXPECT_FALSE(stream->IsInMemory()); |
| 357 EXPECT_EQ(kStreamSize, stream->size()); | 357 EXPECT_EQ(kStreamSize, stream->size()); |
| 358 EXPECT_EQ(0U, stream->position()); | 358 EXPECT_EQ(0U, stream->position()); |
| 359 EXPECT_FALSE(stream->IsEOF()); | 359 EXPECT_FALSE(stream->IsEOF()); |
| 360 scoped_refptr<IOBuffer> buf = new IOBuffer(kTestBufferSize); | 360 scoped_refptr<IOBuffer> buf = new IOBuffer(kTestBufferSize); |
| (...skipping 451 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 812 wrapped_buffer2.get(), buf2.size(), read_callback2.callback())); | 812 wrapped_buffer2.get(), buf2.size(), read_callback2.callback())); |
| 813 EXPECT_EQ(static_cast<int>(buf2.size()), read_callback2.WaitForResult()); | 813 EXPECT_EQ(static_cast<int>(buf2.size()), read_callback2.WaitForResult()); |
| 814 EXPECT_EQ(expected_data, buf2); | 814 EXPECT_EQ(expected_data, buf2); |
| 815 EXPECT_TRUE(stream->IsEOF()); | 815 EXPECT_TRUE(stream->IsEOF()); |
| 816 | 816 |
| 817 // Make sure callbacks are not called for cancelled operations. | 817 // Make sure callbacks are not called for cancelled operations. |
| 818 EXPECT_FALSE(read_callback1.have_result()); | 818 EXPECT_FALSE(read_callback1.have_result()); |
| 819 } | 819 } |
| 820 | 820 |
| 821 } // namespace net | 821 } // namespace net |
| OLD | NEW |