Work on Windows GN component build.
[chromium-blink-merge.git] / net / base / upload_bytes_element_reader_unittest.cc
blob1d2f52ffb2c32be3183f81248b75d9d7e5399bd6
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/upload_bytes_element_reader.h"
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"
14 namespace net {
16 class UploadBytesElementReaderTest : public PlatformTest {
17 protected:
18 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());
28 std::vector<char> bytes_;
29 scoped_ptr<UploadElementReader> reader_;
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);
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()));
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);
69 TEST_F(UploadBytesElementReaderTest, MultipleInit) {
70 std::vector<char> buf(bytes_.size());
71 scoped_refptr<IOBuffer> wrapped_buffer = new WrappedIOBuffer(&buf[0]);
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);
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());
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);
93 } // namespace net