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. 4 5 #include "net/base/upload_bytes_element_reader.h" 6 7 #include "base/basictypes.h" 8 #include "base/memory/scoped_ptr.h" 9 #include "net/base/io_buffer.h" 10 #include "net/base/net_errors.h" 11 #include "testing/gtest/include/gtest/gtest.h" 12 #include "testing/platform_test.h" 13 14 namespace net { 15 16 class UploadBytesElementReaderTest : public PlatformTest { 17 protected: 18 virtual void SetUp() OVERRIDE { 19 const char kData[] = "123abc"; 20 bytes_.assign(kData, kData + arraysize(kData)); 21 reader_.reset(new UploadBytesElementReader(&bytes_[0], bytes_.size())); 22 ASSERT_EQ(OK, reader_->Init(CompletionCallback())); 23 EXPECT_EQ(bytes_.size(), reader_->GetContentLength()); 24 EXPECT_EQ(bytes_.size(), reader_->BytesRemaining()); 25 EXPECT_TRUE(reader_->IsInMemory()); 26 } 27 28 std::vector<char> bytes_; 29 scoped_ptr<UploadElementReader> reader_; 30 }; 31 32 TEST_F(UploadBytesElementReaderTest, ReadPartially) { 33 const size_t kHalfSize = bytes_.size() / 2; 34 std::vector<char> buf(kHalfSize); 35 scoped_refptr<IOBuffer> wrapped_buffer = new WrappedIOBuffer(&buf[0]); 36 EXPECT_EQ( 37 static_cast<int>(buf.size()), 38 reader_->Read(wrapped_buffer.get(), buf.size(), CompletionCallback())); 39 EXPECT_EQ(bytes_.size() - buf.size(), reader_->BytesRemaining()); 40 bytes_.resize(kHalfSize); // Resize to compare. 41 EXPECT_EQ(bytes_, buf); 42 } 43 44 TEST_F(UploadBytesElementReaderTest, ReadAll) { 45 std::vector<char> buf(bytes_.size()); 46 scoped_refptr<IOBuffer> wrapped_buffer = new WrappedIOBuffer(&buf[0]); 47 EXPECT_EQ( 48 static_cast<int>(buf.size()), 49 reader_->Read(wrapped_buffer.get(), buf.size(), CompletionCallback())); 50 EXPECT_EQ(0U, reader_->BytesRemaining()); 51 EXPECT_EQ(bytes_, buf); 52 // Try to read again. 53 EXPECT_EQ( 54 0, reader_->Read(wrapped_buffer.get(), buf.size(), CompletionCallback())); 55 } 56 57 TEST_F(UploadBytesElementReaderTest, ReadTooMuch) { 58 const size_t kTooLargeSize = bytes_.size() * 2; 59 std::vector<char> buf(kTooLargeSize); 60 scoped_refptr<IOBuffer> wrapped_buffer = new WrappedIOBuffer(&buf[0]); 61 EXPECT_EQ( 62 static_cast<int>(bytes_.size()), 63 reader_->Read(wrapped_buffer.get(), buf.size(), CompletionCallback())); 64 EXPECT_EQ(0U, reader_->BytesRemaining()); 65 buf.resize(bytes_.size()); // Resize to compare. 66 EXPECT_EQ(bytes_, buf); 67 } 68 69 TEST_F(UploadBytesElementReaderTest, MultipleInit) { 70 std::vector<char> buf(bytes_.size()); 71 scoped_refptr<IOBuffer> wrapped_buffer = new WrappedIOBuffer(&buf[0]); 72 73 // Read all. 74 EXPECT_EQ( 75 static_cast<int>(buf.size()), 76 reader_->Read(wrapped_buffer.get(), buf.size(), CompletionCallback())); 77 EXPECT_EQ(0U, reader_->BytesRemaining()); 78 EXPECT_EQ(bytes_, buf); 79 80 // Call Init() again to reset the state. 81 ASSERT_EQ(OK, reader_->Init(CompletionCallback())); 82 EXPECT_EQ(bytes_.size(), reader_->GetContentLength()); 83 EXPECT_EQ(bytes_.size(), reader_->BytesRemaining()); 84 85 // Read again. 86 EXPECT_EQ( 87 static_cast<int>(buf.size()), 88 reader_->Read(wrapped_buffer.get(), buf.size(), CompletionCallback())); 89 EXPECT_EQ(0U, reader_->BytesRemaining()); 90 EXPECT_EQ(bytes_, buf); 91 } 92 93 } // namespace net 94