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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
|
// 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_headers_block_parser.h"
#include <string>
#include "base/memory/scoped_ptr.h"
#include "base/strings/string_number_conversions.h"
#include "base/sys_byteorder.h"
#include "net/test/gtest_util.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace net {
using base::IntToString;
using base::StringPiece;
using base::UintToString;
using std::string;
// A mock the handler class to check that we parse out the correct headers
// and call the callback methods when we should.
class MockSpdyHeadersHandler : public SpdyHeadersHandlerInterface {
public:
MOCK_METHOD1(OnHeaderBlock, void(uint32_t num_of_headers));
MOCK_METHOD1(OnHeaderBlockEnd, void(size_t bytes));
MOCK_METHOD2(OnHeader, void(StringPiece key, StringPiece value));
};
class SpdyHeadersBlockParserTest :
public ::testing::TestWithParam<SpdyMajorVersion> {
public:
virtual ~SpdyHeadersBlockParserTest() {}
protected:
void SetUp() override {
// Create a parser using the mock handler.
spdy_version_ = GetParam();
parser_.reset(new SpdyHeadersBlockParser(spdy_version_, &handler_));
length_field_size_ =
SpdyHeadersBlockParser::LengthFieldSizeForVersion(spdy_version_);
}
// Create a header block with a specified number of headers.
string CreateHeaders(uint32_t num_headers, bool insert_nulls) {
string headers;
// First, write the number of headers in the header block.
headers += EncodeLength(num_headers);
// Second, write the key-value pairs.
for (uint32_t i = 0; i < num_headers; i++) {
// Build the key.
string key;
if (insert_nulls) {
key = string(kBaseKey) + string("\0", 1) + UintToString(i);
} else {
key = string(kBaseKey) + UintToString(i);
}
// Encode the key as SPDY header.
headers += EncodeLength(key.length());
headers += key;
// Build the value.
string value;
if (insert_nulls) {
value = string(kBaseValue) + string("\0", 1) + UintToString(i);
} else {
value = string(kBaseValue) + UintToString(i);
}
// Encode the value as SPDY header.
headers += EncodeLength(value.length());
headers += value;
}
return headers;
}
string EncodeLength(uint32_t len) {
char buffer[4];
if (length_field_size_ == sizeof(uint32_t)) {
uint32_t net_order_len = htonl(len);
memcpy(buffer, &net_order_len, length_field_size_);
} else if (length_field_size_ == sizeof(uint16_t)) {
uint16_t net_order_len = htons(len);
memcpy(buffer, &net_order_len, length_field_size_);
} else {
CHECK(false) << "Invalid length field size";
}
return string(buffer, length_field_size_);
}
size_t length_field_size_;
SpdyMajorVersion spdy_version_;
MockSpdyHeadersHandler handler_;
scoped_ptr<SpdyHeadersBlockParser> parser_;
static const char *const kBaseKey;
static const char *const kBaseValue;
// Number of headers and header blocks used in the tests.
static const int kNumHeadersInBlock = 10;
static const int kNumHeaderBlocks = 10;
};
const char *const SpdyHeadersBlockParserTest::kBaseKey = "test_key";
const char *const SpdyHeadersBlockParserTest::kBaseValue = "test_value";
// All tests are run with 3 different SPDY versions: SPDY/2, SPDY/3, HTTP/2.
INSTANTIATE_TEST_CASE_P(SpdyHeadersBlockParserTests,
SpdyHeadersBlockParserTest,
::testing::Values(SPDY2, SPDY3, HTTP2));
TEST_P(SpdyHeadersBlockParserTest, BasicTest) {
// Sanity test, verify that we parse out correctly a block with
// a single key-value pair and that we notify when we start and finish
// handling a headers block.
EXPECT_EQ(spdy_version_, parser_->spdy_version());
string headers(CreateHeaders(1, false));
EXPECT_CALL(handler_, OnHeaderBlock(1)).Times(1);
std::string expect_key = kBaseKey + IntToString(0);
std::string expect_value = kBaseValue + IntToString(0);
EXPECT_CALL(handler_, OnHeader(StringPiece(expect_key),
StringPiece(expect_value))).Times(1);
EXPECT_CALL(handler_, OnHeaderBlockEnd(headers.length())).Times(1);
EXPECT_TRUE(parser_->
HandleControlFrameHeadersData(1, headers.c_str(), headers.length()));
EXPECT_EQ(SpdyHeadersBlockParser::NO_PARSER_ERROR, parser_->get_error());
}
TEST_P(SpdyHeadersBlockParserTest, NullsSupportedTest) {
// Sanity test, verify that we parse out correctly a block with
// a single key-value pair when the key and value contain null charecters.
string headers(CreateHeaders(1, true));
EXPECT_CALL(handler_, OnHeaderBlock(1)).Times(1);
std::string expect_key = kBaseKey + string("\0", 1) + IntToString(0);
std::string expect_value = kBaseValue + string("\0", 1) + IntToString(0);
EXPECT_CALL(handler_, OnHeader(StringPiece(expect_key),
StringPiece(expect_value))).Times(1);
EXPECT_CALL(handler_, OnHeaderBlockEnd(headers.length())).Times(1);
EXPECT_TRUE(parser_->
HandleControlFrameHeadersData(1, headers.c_str(), headers.length()));
EXPECT_EQ(SpdyHeadersBlockParser::NO_PARSER_ERROR, parser_->get_error());
}
TEST_P(SpdyHeadersBlockParserTest, MultipleBlocksAndHeadersWithPartialData) {
testing::InSequence s;
// CreateHeaders is deterministic; we can call it once for the whole test.
string headers(CreateHeaders(kNumHeadersInBlock, false));
// The mock doesn't retain storage of arguments, so keep them in scope.
std::vector<string> retained_arguments;
for (int i = 0; i < kNumHeadersInBlock; i++) {
retained_arguments.push_back(kBaseKey + IntToString(i));
retained_arguments.push_back(kBaseValue + IntToString(i));
}
// For each block we expect to parse out the headers in order.
for (int i = 0; i < kNumHeaderBlocks; i++) {
EXPECT_CALL(handler_, OnHeaderBlock(kNumHeadersInBlock)).Times(1);
for (int j = 0; j < kNumHeadersInBlock; j++) {
EXPECT_CALL(handler_, OnHeader(
StringPiece(retained_arguments[2 * j]),
StringPiece(retained_arguments[2 * j + 1]))).Times(1);
}
EXPECT_CALL(handler_, OnHeaderBlockEnd(headers.length())).Times(1);
}
// Parse the header blocks, feeding the parser one byte at a time.
for (int i = 1; i <= kNumHeaderBlocks; i++) {
for (string::iterator it = headers.begin(); it != headers.end(); ++it) {
if ((it + 1) == headers.end()) {
// Last byte completes the block.
EXPECT_TRUE(parser_->HandleControlFrameHeadersData(i, &(*it), 1));
EXPECT_EQ(SpdyHeadersBlockParser::NO_PARSER_ERROR,
parser_->get_error());
} else {
EXPECT_FALSE(parser_->HandleControlFrameHeadersData(i, &(*it), 1));
EXPECT_EQ(SpdyHeadersBlockParser::NEED_MORE_DATA, parser_->get_error());
}
}
}
}
TEST_P(SpdyHeadersBlockParserTest, HandlesEmptyCallsTest) {
EXPECT_CALL(handler_, OnHeaderBlock(1)).Times(1);
string headers(CreateHeaders(1, false));
string expect_key = kBaseKey + IntToString(0);
string expect_value = kBaseValue + IntToString(0);
EXPECT_CALL(handler_, OnHeader(StringPiece(expect_key),
StringPiece(expect_value))).Times(1);
EXPECT_CALL(handler_, OnHeaderBlockEnd(headers.length())).Times(1);
// Send a header in pieces with intermediate empty calls.
for (string::iterator it = headers.begin(); it != headers.end(); ++it) {
if ((it + 1) == headers.end()) {
// Last byte completes the block.
EXPECT_TRUE(parser_->HandleControlFrameHeadersData(1, &(*it), 1));
EXPECT_EQ(SpdyHeadersBlockParser::NO_PARSER_ERROR, parser_->get_error());
} else {
EXPECT_FALSE(parser_->HandleControlFrameHeadersData(1, &(*it), 1));
EXPECT_EQ(SpdyHeadersBlockParser::NEED_MORE_DATA, parser_->get_error());
EXPECT_FALSE(parser_->HandleControlFrameHeadersData(1, NULL, 0));
}
}
}
TEST_P(SpdyHeadersBlockParserTest, LargeBlocksDiscardedTest) {
// Header block with too many headers.
{
string headers = EncodeLength(
parser_->MaxNumberOfHeadersForVersion(spdy_version_) + 1);
EXPECT_FALSE(parser_->
HandleControlFrameHeadersData(1, headers.c_str(), headers.length()));
EXPECT_EQ(SpdyHeadersBlockParser::HEADER_BLOCK_TOO_LARGE,
parser_->get_error());
}
parser_.reset(new SpdyHeadersBlockParser(spdy_version_, &handler_));
// Header block with one header, which has a too-long key.
{
EXPECT_CALL(handler_, OnHeaderBlock(1)).Times(1);
string headers = EncodeLength(1) + EncodeLength(
SpdyHeadersBlockParser::kMaximumFieldLength + 1);
EXPECT_FALSE(parser_->
HandleControlFrameHeadersData(1, headers.c_str(), headers.length()));
EXPECT_EQ(SpdyHeadersBlockParser::HEADER_FIELD_TOO_LARGE,
parser_->get_error());
}
}
TEST_P(SpdyHeadersBlockParserTest, ExtraDataTest) {
string headers = CreateHeaders(1, false) + "foobar";
EXPECT_CALL(handler_, OnHeaderBlock(1)).Times(1);
EXPECT_CALL(handler_, OnHeaderBlockEnd(headers.length())).Times(1);
string expect_key = kBaseKey + IntToString(0);
string expect_value = kBaseValue + IntToString(0);
EXPECT_CALL(handler_, OnHeader(StringPiece(expect_key),
StringPiece(expect_value))).Times(1);
EXPECT_FALSE(parser_->HandleControlFrameHeadersData(1, headers.c_str(),
headers.length()));
EXPECT_EQ(SpdyHeadersBlockParser::TOO_MUCH_DATA, parser_->get_error());
}
TEST_P(SpdyHeadersBlockParserTest, WrongStreamIdTest) {
string headers(CreateHeaders(kNumHeadersInBlock, false));
EXPECT_FALSE(parser_->HandleControlFrameHeadersData(1, headers.data(), 1));
EXPECT_EQ(SpdyHeadersBlockParser::NEED_MORE_DATA, parser_->get_error());
bool result;
EXPECT_DFATAL(
result = parser_->HandleControlFrameHeadersData(2, headers.data() + 1, 1),
"Unexpected stream id: 2 \\(expected 1\\)");
EXPECT_FALSE(result);
EXPECT_EQ(SpdyHeadersBlockParser::UNEXPECTED_STREAM_ID, parser_->get_error());
}
TEST_P(SpdyHeadersBlockParserTest, InvalidStreamIdTest) {
string headers(CreateHeaders(kNumHeadersInBlock, false));
bool result;
EXPECT_DFATAL(
result = parser_->HandleControlFrameHeadersData(0, headers.data(), 1),
"Expected nonzero stream id, saw: 0");
EXPECT_FALSE(result);
EXPECT_EQ(SpdyHeadersBlockParser::UNEXPECTED_STREAM_ID, parser_->get_error());
}
} // namespace net
|