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