1// Copyright 2014 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_prefixed_buffer_reader.h"
6
7#include "testing/gmock/include/gmock/gmock.h"
8#include "testing/gtest/include/gtest/gtest.h"
9
10namespace net {
11
12namespace test {
13
14using base::StringPiece;
15using testing::ElementsAreArray;
16
17class SpdyPrefixedBufferReaderTest : public ::testing::Test {
18 protected:
19  SpdyPrefixedBufferReader Build(std::string prefix, std::string suffix) {
20    prefix_ = prefix;
21    suffix_ = suffix;
22    return SpdyPrefixedBufferReader(prefix_.data(), prefix_.length(),
23                                    suffix_.data(), suffix_.length());
24  }
25  std::string prefix_, suffix_;
26};
27
28TEST_F(SpdyPrefixedBufferReaderTest, ReadRawFromPrefix) {
29  SpdyPrefixedBufferReader reader = Build("foobar", "");
30  EXPECT_EQ(6u, reader.Available());
31
32  char buffer[] = "123456";
33  EXPECT_FALSE(reader.ReadN(10, buffer));  // Not enough buffer.
34  EXPECT_TRUE(reader.ReadN(6, buffer));
35  EXPECT_THAT(buffer, ElementsAreArray("foobar"));
36  EXPECT_EQ(0u, reader.Available());
37}
38
39TEST_F(SpdyPrefixedBufferReaderTest, ReadPieceFromPrefix) {
40  SpdyPrefixedBufferReader reader = Build("foobar", "");
41  EXPECT_EQ(6u, reader.Available());
42
43  SpdyPinnableBufferPiece piece;
44  EXPECT_FALSE(reader.ReadN(10, &piece));  // Not enough buffer.
45  EXPECT_TRUE(reader.ReadN(6, &piece));
46  EXPECT_FALSE(piece.IsPinned());
47  EXPECT_EQ(StringPiece("foobar"), piece);
48  EXPECT_EQ(0u, reader.Available());
49}
50
51TEST_F(SpdyPrefixedBufferReaderTest, ReadRawFromSuffix) {
52  SpdyPrefixedBufferReader reader = Build("", "foobar");
53  EXPECT_EQ(6u, reader.Available());
54
55  char buffer[] = "123456";
56  EXPECT_FALSE(reader.ReadN(10, buffer));  // Not enough buffer.
57  EXPECT_TRUE(reader.ReadN(6, buffer));
58  EXPECT_THAT(buffer, ElementsAreArray("foobar"));
59  EXPECT_EQ(0u, reader.Available());
60}
61
62TEST_F(SpdyPrefixedBufferReaderTest, ReadPieceFromSuffix) {
63  SpdyPrefixedBufferReader reader = Build("", "foobar");
64  EXPECT_EQ(6u, reader.Available());
65
66  SpdyPinnableBufferPiece piece;
67  EXPECT_FALSE(reader.ReadN(10, &piece));  // Not enough buffer.
68  EXPECT_TRUE(reader.ReadN(6, &piece));
69  EXPECT_FALSE(piece.IsPinned());
70  EXPECT_EQ(StringPiece("foobar"), piece);
71  EXPECT_EQ(0u, reader.Available());
72}
73
74TEST_F(SpdyPrefixedBufferReaderTest, ReadRawSpanning) {
75  SpdyPrefixedBufferReader reader = Build("foob", "ar");
76  EXPECT_EQ(6u, reader.Available());
77
78  char buffer[] = "123456";
79  EXPECT_FALSE(reader.ReadN(10, buffer));  // Not enough buffer.
80  EXPECT_TRUE(reader.ReadN(6, buffer));
81  EXPECT_THAT(buffer, ElementsAreArray("foobar"));
82  EXPECT_EQ(0u, reader.Available());
83}
84
85TEST_F(SpdyPrefixedBufferReaderTest, ReadPieceSpanning) {
86  SpdyPrefixedBufferReader reader = Build("foob", "ar");
87  EXPECT_EQ(6u, reader.Available());
88
89  SpdyPinnableBufferPiece piece;
90  EXPECT_FALSE(reader.ReadN(10, &piece));  // Not enough buffer.
91  EXPECT_TRUE(reader.ReadN(6, &piece));
92  EXPECT_TRUE(piece.IsPinned());
93  EXPECT_EQ(StringPiece("foobar"), piece);
94  EXPECT_EQ(0u, reader.Available());
95}
96
97TEST_F(SpdyPrefixedBufferReaderTest, ReadMixed) {
98  SpdyPrefixedBufferReader reader = Build("abcdef", "hijkl");
99  EXPECT_EQ(11u, reader.Available());
100
101  char buffer[] = "1234";
102  SpdyPinnableBufferPiece piece;
103
104  EXPECT_TRUE(reader.ReadN(3, buffer));
105  EXPECT_THAT(buffer, ElementsAreArray("abc4"));
106  EXPECT_EQ(8u, reader.Available());
107
108  EXPECT_TRUE(reader.ReadN(2, buffer));
109  EXPECT_THAT(buffer, ElementsAreArray("dec4"));
110  EXPECT_EQ(6u, reader.Available());
111
112  EXPECT_TRUE(reader.ReadN(3, &piece));
113  EXPECT_EQ(StringPiece("fhi"), piece);
114  EXPECT_TRUE(piece.IsPinned());
115  EXPECT_EQ(3u, reader.Available());
116
117  EXPECT_TRUE(reader.ReadN(2, &piece));
118  EXPECT_EQ(StringPiece("jk"), piece);
119  EXPECT_FALSE(piece.IsPinned());
120  EXPECT_EQ(1u, reader.Available());
121
122  EXPECT_TRUE(reader.ReadN(1, buffer));
123  EXPECT_THAT(buffer, ElementsAreArray("lec4"));
124  EXPECT_EQ(0u, reader.Available());
125}
126
127}  // namespace test
128
129}  // namespace net
130