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(const std::string
& prefix
,
20 const std::string
& suffix
) {
23 return SpdyPrefixedBufferReader(prefix_
.data(), prefix_
.length(),
24 suffix_
.data(), suffix_
.length());
26 std::string prefix_
, suffix_
;
29 TEST_F(SpdyPrefixedBufferReaderTest
, ReadRawFromPrefix
) {
30 SpdyPrefixedBufferReader reader
= Build("foobar", "");
31 EXPECT_EQ(6u, reader
.Available());
33 char buffer
[] = "123456";
34 EXPECT_FALSE(reader
.ReadN(10, buffer
)); // Not enough buffer.
35 EXPECT_TRUE(reader
.ReadN(6, buffer
));
36 EXPECT_THAT(buffer
, ElementsAreArray("foobar"));
37 EXPECT_EQ(0u, reader
.Available());
40 TEST_F(SpdyPrefixedBufferReaderTest
, ReadPieceFromPrefix
) {
41 SpdyPrefixedBufferReader reader
= Build("foobar", "");
42 EXPECT_EQ(6u, reader
.Available());
44 SpdyPinnableBufferPiece piece
;
45 EXPECT_FALSE(reader
.ReadN(10, &piece
)); // Not enough buffer.
46 EXPECT_TRUE(reader
.ReadN(6, &piece
));
47 EXPECT_FALSE(piece
.IsPinned());
48 EXPECT_EQ(StringPiece("foobar"), piece
);
49 EXPECT_EQ(0u, reader
.Available());
52 TEST_F(SpdyPrefixedBufferReaderTest
, ReadRawFromSuffix
) {
53 SpdyPrefixedBufferReader reader
= Build("", "foobar");
54 EXPECT_EQ(6u, reader
.Available());
56 char buffer
[] = "123456";
57 EXPECT_FALSE(reader
.ReadN(10, buffer
)); // Not enough buffer.
58 EXPECT_TRUE(reader
.ReadN(6, buffer
));
59 EXPECT_THAT(buffer
, ElementsAreArray("foobar"));
60 EXPECT_EQ(0u, reader
.Available());
63 TEST_F(SpdyPrefixedBufferReaderTest
, ReadPieceFromSuffix
) {
64 SpdyPrefixedBufferReader reader
= Build("", "foobar");
65 EXPECT_EQ(6u, reader
.Available());
67 SpdyPinnableBufferPiece piece
;
68 EXPECT_FALSE(reader
.ReadN(10, &piece
)); // Not enough buffer.
69 EXPECT_TRUE(reader
.ReadN(6, &piece
));
70 EXPECT_FALSE(piece
.IsPinned());
71 EXPECT_EQ(StringPiece("foobar"), piece
);
72 EXPECT_EQ(0u, reader
.Available());
75 TEST_F(SpdyPrefixedBufferReaderTest
, ReadRawSpanning
) {
76 SpdyPrefixedBufferReader reader
= Build("foob", "ar");
77 EXPECT_EQ(6u, reader
.Available());
79 char buffer
[] = "123456";
80 EXPECT_FALSE(reader
.ReadN(10, buffer
)); // Not enough buffer.
81 EXPECT_TRUE(reader
.ReadN(6, buffer
));
82 EXPECT_THAT(buffer
, ElementsAreArray("foobar"));
83 EXPECT_EQ(0u, reader
.Available());
86 TEST_F(SpdyPrefixedBufferReaderTest
, ReadPieceSpanning
) {
87 SpdyPrefixedBufferReader reader
= Build("foob", "ar");
88 EXPECT_EQ(6u, reader
.Available());
90 SpdyPinnableBufferPiece piece
;
91 EXPECT_FALSE(reader
.ReadN(10, &piece
)); // Not enough buffer.
92 EXPECT_TRUE(reader
.ReadN(6, &piece
));
93 EXPECT_TRUE(piece
.IsPinned());
94 EXPECT_EQ(StringPiece("foobar"), piece
);
95 EXPECT_EQ(0u, reader
.Available());
98 TEST_F(SpdyPrefixedBufferReaderTest
, ReadMixed
) {
99 SpdyPrefixedBufferReader reader
= Build("abcdef", "hijkl");
100 EXPECT_EQ(11u, reader
.Available());
102 char buffer
[] = "1234";
103 SpdyPinnableBufferPiece piece
;
105 EXPECT_TRUE(reader
.ReadN(3, buffer
));
106 EXPECT_THAT(buffer
, ElementsAreArray("abc4"));
107 EXPECT_EQ(8u, reader
.Available());
109 EXPECT_TRUE(reader
.ReadN(2, buffer
));
110 EXPECT_THAT(buffer
, ElementsAreArray("dec4"));
111 EXPECT_EQ(6u, reader
.Available());
113 EXPECT_TRUE(reader
.ReadN(3, &piece
));
114 EXPECT_EQ(StringPiece("fhi"), piece
);
115 EXPECT_TRUE(piece
.IsPinned());
116 EXPECT_EQ(3u, reader
.Available());
118 EXPECT_TRUE(reader
.ReadN(2, &piece
));
119 EXPECT_EQ(StringPiece("jk"), piece
);
120 EXPECT_FALSE(piece
.IsPinned());
121 EXPECT_EQ(1u, reader
.Available());
123 EXPECT_TRUE(reader
.ReadN(1, buffer
));
124 EXPECT_THAT(buffer
, ElementsAreArray("lec4"));
125 EXPECT_EQ(0u, reader
.Available());