blob: fbd770527cb06df47deb265afb0bdb93cb51921b [file] [log] [blame]
[email protected]ca690b02013-04-17 10:38:431// Copyright (c) 2013 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/spdy/spdy_buffer.h"
6
7#include <cstddef>
8#include <string>
9
10#include "base/basictypes.h"
11#include "base/memory/ref_counted.h"
12#include "base/memory/scoped_ptr.h"
13#include "net/base/io_buffer.h"
14#include "net/spdy/spdy_protocol.h"
15#include "testing/gtest/include/gtest/gtest.h"
16
17namespace net {
18
19namespace {
20
21const char kData[] = "hello!\0hi.";
22const size_t kDataSize = arraysize(kData);
23
24class SpdyBufferTest : public ::testing::Test {};
25
26// Make a string from the data remaining in |buffer|.
27std::string BufferToString(const SpdyBuffer& buffer) {
28 return std::string(buffer.GetRemainingData(), buffer.GetRemainingSize());
29}
30
31// Construct a SpdyBuffer from a SpdyFrame and make sure its data
32// points to the frame's underlying data.
33TEST_F(SpdyBufferTest, FrameConstructor) {
34 SpdyBuffer buffer(
35 scoped_ptr<SpdyFrame>(
36 new SpdyFrame(const_cast<char*>(kData), kDataSize,
37 false /* owns_buffer */)));
38
39 EXPECT_EQ(kData, buffer.GetRemainingData());
40 EXPECT_EQ(kDataSize, buffer.GetRemainingSize());
41}
42
43// Construct a SpdyBuffer from a const char*/size_t pair and make sure
44// it makes a copy of the data.
45TEST_F(SpdyBufferTest, DataConstructor) {
46 std::string data(kData, kDataSize);
47 SpdyBuffer buffer(data.data(), data.size());
48 // This mutation shouldn't affect |buffer|'s data.
49 data[0] = 'H';
50
51 EXPECT_NE(kData, buffer.GetRemainingData());
52 EXPECT_EQ(kDataSize, buffer.GetRemainingSize());
53 EXPECT_EQ(std::string(kData, kDataSize), BufferToString(buffer));
54}
55
56// Construct a SpdyBuffer and call Consume() on it, which should
57// update the remaining data pointer and size appropriately.
58TEST_F(SpdyBufferTest, Consume) {
59 SpdyBuffer buffer(kData, kDataSize);
60
61 EXPECT_EQ(std::string(kData, kDataSize), BufferToString(buffer));
62
63 buffer.Consume(5);
64 EXPECT_EQ(std::string(kData + 5, kDataSize - 5), BufferToString(buffer));
65
66 buffer.Consume(kDataSize - 5);
67 EXPECT_EQ(0u, buffer.GetRemainingSize());
68}
69
70// Make sure the IOBuffer returned by GetIOBufferForRemainingData()
71// points to the buffer's remaining data and isn't updated by
72// Consume().
73TEST_F(SpdyBufferTest, GetIOBufferForRemainingData) {
74 SpdyBuffer buffer(kData, kDataSize);
75
76 buffer.Consume(5);
77 scoped_refptr<IOBuffer> io_buffer = buffer.GetIOBufferForRemainingData();
78 size_t io_buffer_size = buffer.GetRemainingSize();
79 const std::string expectedData(kData + 5, kDataSize - 5);
80 EXPECT_EQ(expectedData, std::string(io_buffer->data(), io_buffer_size));
81
82 buffer.Consume(kDataSize - 5);
83 EXPECT_EQ(expectedData, std::string(io_buffer->data(), io_buffer_size));
84}
85
86} // namespace
87
88} // namespace net