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.
5 #include "net/spdy/spdy_prefixed_buffer_reader.h"
7 #include "testing/gmock/include/gmock/gmock.h"
8 #include "testing/gtest/include/gtest/gtest.h"
14 using base::StringPiece
;
15 using testing::ElementsAreArray
;
17 class SpdyPrefixedBufferReaderTest
: public ::testing::Test
{
19 SpdyPrefixedBufferReader
Build(std::string prefix
, std::string suffix
) {
22 return SpdyPrefixedBufferReader(prefix_
.data(), prefix_
.length(),
23 suffix_
.data(), suffix_
.length());
25 std::string prefix_
, suffix_
;
28 TEST_F(SpdyPrefixedBufferReaderTest
, ReadRawFromPrefix
) {
29 SpdyPrefixedBufferReader reader
= Build("foobar", "");
30 EXPECT_EQ(6u, reader
.Available());
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());
39 TEST_F(SpdyPrefixedBufferReaderTest
, ReadPieceFromPrefix
) {
40 SpdyPrefixedBufferReader reader
= Build("foobar", "");
41 EXPECT_EQ(6u, reader
.Available());
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());
51 TEST_F(SpdyPrefixedBufferReaderTest
, ReadRawFromSuffix
) {
52 SpdyPrefixedBufferReader reader
= Build("", "foobar");
53 EXPECT_EQ(6u, reader
.Available());
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());
62 TEST_F(SpdyPrefixedBufferReaderTest
, ReadPieceFromSuffix
) {
63 SpdyPrefixedBufferReader reader
= Build("", "foobar");
64 EXPECT_EQ(6u, reader
.Available());
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());
74 TEST_F(SpdyPrefixedBufferReaderTest
, ReadRawSpanning
) {
75 SpdyPrefixedBufferReader reader
= Build("foob", "ar");
76 EXPECT_EQ(6u, reader
.Available());
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());
85 TEST_F(SpdyPrefixedBufferReaderTest
, ReadPieceSpanning
) {
86 SpdyPrefixedBufferReader reader
= Build("foob", "ar");
87 EXPECT_EQ(6u, reader
.Available());
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());
97 TEST_F(SpdyPrefixedBufferReaderTest
, ReadMixed
) {
98 SpdyPrefixedBufferReader reader
= Build("abcdef", "hijkl");
99 EXPECT_EQ(11u, reader
.Available());
101 char buffer
[] = "1234";
102 SpdyPinnableBufferPiece piece
;
104 EXPECT_TRUE(reader
.ReadN(3, buffer
));
105 EXPECT_THAT(buffer
, ElementsAreArray("abc4"));
106 EXPECT_EQ(8u, reader
.Available());
108 EXPECT_TRUE(reader
.ReadN(2, buffer
));
109 EXPECT_THAT(buffer
, ElementsAreArray("dec4"));
110 EXPECT_EQ(6u, reader
.Available());
112 EXPECT_TRUE(reader
.ReadN(3, &piece
));
113 EXPECT_EQ(StringPiece("fhi"), piece
);
114 EXPECT_TRUE(piece
.IsPinned());
115 EXPECT_EQ(3u, reader
.Available());
117 EXPECT_TRUE(reader
.ReadN(2, &piece
));
118 EXPECT_EQ(StringPiece("jk"), piece
);
119 EXPECT_FALSE(piece
.IsPinned());
120 EXPECT_EQ(1u, reader
.Available());
122 EXPECT_TRUE(reader
.ReadN(1, buffer
));
123 EXPECT_THAT(buffer
, ElementsAreArray("lec4"));
124 EXPECT_EQ(0u, reader
.Available());