blob: 1aad55ee5ce04cb7076600b5efb466af93aa7c9b [file] [log] [blame]
[email protected]d98961652012-09-11 20:27:211// 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"
[email protected]5b768142012-10-17 10:15:179#include "net/base/io_buffer.h"
[email protected]d98961652012-09-11 20:27:2110#include "net/base/net_errors.h"
11#include "testing/gtest/include/gtest/gtest.h"
12#include "testing/platform_test.h"
13
14namespace net {
15
16class 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()));
[email protected]4db27d82012-12-20 11:50:2422 ASSERT_EQ(OK, reader_->Init(CompletionCallback()));
[email protected]d98961652012-09-11 20:27:2123 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
32TEST_F(UploadBytesElementReaderTest, ReadPartially) {
33 const size_t kHalfSize = bytes_.size() / 2;
34 std::vector<char> buf(kHalfSize);
[email protected]5b768142012-10-17 10:15:1735 scoped_refptr<IOBuffer> wrapped_buffer = new WrappedIOBuffer(&buf[0]);
[email protected]90499482013-06-01 00:39:5036 EXPECT_EQ(
37 static_cast<int>(buf.size()),
38 reader_->Read(wrapped_buffer.get(), buf.size(), CompletionCallback()));
[email protected]d98961652012-09-11 20:27:2139 EXPECT_EQ(bytes_.size() - buf.size(), reader_->BytesRemaining());
40 bytes_.resize(kHalfSize); // Resize to compare.
41 EXPECT_EQ(bytes_, buf);
42}
43
44TEST_F(UploadBytesElementReaderTest, ReadAll) {
45 std::vector<char> buf(bytes_.size());
[email protected]5b768142012-10-17 10:15:1746 scoped_refptr<IOBuffer> wrapped_buffer = new WrappedIOBuffer(&buf[0]);
[email protected]90499482013-06-01 00:39:5047 EXPECT_EQ(
48 static_cast<int>(buf.size()),
49 reader_->Read(wrapped_buffer.get(), buf.size(), CompletionCallback()));
[email protected]d98961652012-09-11 20:27:2150 EXPECT_EQ(0U, reader_->BytesRemaining());
51 EXPECT_EQ(bytes_, buf);
52 // Try to read again.
[email protected]90499482013-06-01 00:39:5053 EXPECT_EQ(
54 0, reader_->Read(wrapped_buffer.get(), buf.size(), CompletionCallback()));
[email protected]d98961652012-09-11 20:27:2155}
56
57TEST_F(UploadBytesElementReaderTest, ReadTooMuch) {
58 const size_t kTooLargeSize = bytes_.size() * 2;
59 std::vector<char> buf(kTooLargeSize);
[email protected]5b768142012-10-17 10:15:1760 scoped_refptr<IOBuffer> wrapped_buffer = new WrappedIOBuffer(&buf[0]);
[email protected]90499482013-06-01 00:39:5061 EXPECT_EQ(
62 static_cast<int>(bytes_.size()),
63 reader_->Read(wrapped_buffer.get(), buf.size(), CompletionCallback()));
[email protected]d98961652012-09-11 20:27:2164 EXPECT_EQ(0U, reader_->BytesRemaining());
65 buf.resize(bytes_.size()); // Resize to compare.
66 EXPECT_EQ(bytes_, buf);
67}
68
[email protected]e5d477342012-11-02 15:18:4669TEST_F(UploadBytesElementReaderTest, MultipleInit) {
70 std::vector<char> buf(bytes_.size());
71 scoped_refptr<IOBuffer> wrapped_buffer = new WrappedIOBuffer(&buf[0]);
72
73 // Read all.
[email protected]90499482013-06-01 00:39:5074 EXPECT_EQ(
75 static_cast<int>(buf.size()),
76 reader_->Read(wrapped_buffer.get(), buf.size(), CompletionCallback()));
[email protected]e5d477342012-11-02 15:18:4677 EXPECT_EQ(0U, reader_->BytesRemaining());
78 EXPECT_EQ(bytes_, buf);
79
80 // Call Init() again to reset the state.
[email protected]4db27d82012-12-20 11:50:2481 ASSERT_EQ(OK, reader_->Init(CompletionCallback()));
[email protected]e5d477342012-11-02 15:18:4682 EXPECT_EQ(bytes_.size(), reader_->GetContentLength());
83 EXPECT_EQ(bytes_.size(), reader_->BytesRemaining());
84
85 // Read again.
[email protected]90499482013-06-01 00:39:5086 EXPECT_EQ(
87 static_cast<int>(buf.size()),
88 reader_->Read(wrapped_buffer.get(), buf.size(), CompletionCallback()));
[email protected]e5d477342012-11-02 15:18:4689 EXPECT_EQ(0U, reader_->BytesRemaining());
90 EXPECT_EQ(bytes_, buf);
91}
92
[email protected]d98961652012-09-11 20:27:2193} // namespace net