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_headers_block_parser.h"
9 #include "base/memory/scoped_ptr.h"
10 #include "base/strings/string_number_conversions.h"
11 #include "base/sys_byteorder.h"
12 #include "net/test/gtest_util.h"
13 #include "testing/gmock/include/gmock/gmock.h"
14 #include "testing/gtest/include/gtest/gtest.h"
18 using base::IntToString
;
19 using base::StringPiece
;
22 // A mock the handler class to check that we parse out the correct headers
23 // and call the callback methods when we should.
24 class MockSpdyHeadersHandler
: public SpdyHeadersHandlerInterface
{
26 MOCK_METHOD1(OnHeaderBlock
, void(uint32_t num_of_headers
));
27 MOCK_METHOD1(OnHeaderBlockEnd
, void(size_t bytes
));
28 MOCK_METHOD2(OnHeader
, void(StringPiece key
, StringPiece value
));
31 class SpdyHeadersBlockParserTest
:
32 public ::testing::TestWithParam
<SpdyMajorVersion
> {
34 virtual ~SpdyHeadersBlockParserTest() {}
37 void SetUp() override
{
38 // Create a parser using the mock handler.
39 spdy_version_
= GetParam();
41 parser_
.reset(new SpdyHeadersBlockParser(spdy_version_
, &handler_
));
43 SpdyHeadersBlockParser::LengthFieldSizeForVersion(spdy_version_
);
46 // Create a header block with a specified number of headers.
47 string
CreateHeaders(uint32_t num_headers
, bool insert_nulls
) {
50 // First, write the number of headers in the header block.
51 headers
+= EncodeLength(num_headers
);
53 // Second, write the key-value pairs.
54 for (uint32_t i
= 0; i
< num_headers
; i
++) {
58 key
= string(kBaseKey
) + string("\0", 1) + IntToString(i
);
60 key
= string(kBaseKey
) + IntToString(i
);
62 // Encode the key as SPDY header.
63 headers
+= EncodeLength(key
.length());
69 value
= string(kBaseValue
) + string("\0", 1) + IntToString(i
);
71 value
= string(kBaseValue
) + IntToString(i
);
73 // Encode the value as SPDY header.
74 headers
+= EncodeLength(value
.length());
80 string
EncodeLength(uint32_t len
) {
82 if (length_field_size_
== sizeof(uint32_t)) {
83 uint32_t net_order_len
= htonl(len
);
84 memcpy(buffer
, &net_order_len
, length_field_size_
);
85 } else if (length_field_size_
== sizeof(uint16_t)) {
86 uint16_t net_order_len
= htons(len
);
87 memcpy(buffer
, &net_order_len
, length_field_size_
);
89 CHECK(false) << "Invalid length field size";
91 return string(buffer
, length_field_size_
);
94 size_t length_field_size_
;
95 SpdyMajorVersion spdy_version_
;
97 MockSpdyHeadersHandler handler_
;
98 scoped_ptr
<SpdyHeadersBlockParser
> parser_
;
100 static const char *const kBaseKey
;
101 static const char *const kBaseValue
;
103 // Number of headers and header blocks used in the tests.
104 static const int kNumHeadersInBlock
= 10;
105 static const int kNumHeaderBlocks
= 10;
108 const char *const SpdyHeadersBlockParserTest::kBaseKey
= "test_key";
109 const char *const SpdyHeadersBlockParserTest::kBaseValue
= "test_value";
111 // All tests are run with 3 different SPDY versions: SPDY/2, SPDY/3, SPDY/4.
112 INSTANTIATE_TEST_CASE_P(SpdyHeadersBlockParserTests
,
113 SpdyHeadersBlockParserTest
,
114 ::testing::Values(SPDY2
, SPDY3
, SPDY4
));
116 TEST_P(SpdyHeadersBlockParserTest
, BasicTest
) {
117 // Sanity test, verify that we parse out correctly a block with
118 // a single key-value pair and that we notify when we start and finish
119 // handling a headers block.
120 string
headers(CreateHeaders(1, false));
122 EXPECT_CALL(handler_
, OnHeaderBlock(1)).Times(1);
124 std::string expect_key
= kBaseKey
+ IntToString(0);
125 std::string expect_value
= kBaseValue
+ IntToString(0);
126 EXPECT_CALL(handler_
, OnHeader(StringPiece(expect_key
),
127 StringPiece(expect_value
))).Times(1);
128 EXPECT_CALL(handler_
, OnHeaderBlockEnd(headers
.length())).Times(1);
130 EXPECT_TRUE(parser_
->
131 HandleControlFrameHeadersData(1, headers
.c_str(), headers
.length()));
132 EXPECT_EQ(SpdyHeadersBlockParser::NO_PARSER_ERROR
, parser_
->get_error());
135 TEST_P(SpdyHeadersBlockParserTest
, NullsSupportedTest
) {
136 // Sanity test, verify that we parse out correctly a block with
137 // a single key-value pair when the key and value contain null charecters.
138 string
headers(CreateHeaders(1, true));
140 EXPECT_CALL(handler_
, OnHeaderBlock(1)).Times(1);
142 std::string expect_key
= kBaseKey
+ string("\0", 1) + IntToString(0);
143 std::string expect_value
= kBaseValue
+ string("\0", 1) + IntToString(0);
144 EXPECT_CALL(handler_
, OnHeader(StringPiece(expect_key
),
145 StringPiece(expect_value
))).Times(1);
146 EXPECT_CALL(handler_
, OnHeaderBlockEnd(headers
.length())).Times(1);
148 EXPECT_TRUE(parser_
->
149 HandleControlFrameHeadersData(1, headers
.c_str(), headers
.length()));
150 EXPECT_EQ(SpdyHeadersBlockParser::NO_PARSER_ERROR
, parser_
->get_error());
153 TEST_P(SpdyHeadersBlockParserTest
, MultipleBlocksAndHeadersWithPartialData
) {
154 testing::InSequence s
;
156 // CreateHeaders is deterministic; we can call it once for the whole test.
157 string
headers(CreateHeaders(kNumHeadersInBlock
, false));
159 // The mock doesn't retain storage of arguments, so keep them in scope.
160 std::vector
<string
> retained_arguments
;
161 for (int i
= 0; i
< kNumHeadersInBlock
; i
++) {
162 retained_arguments
.push_back(kBaseKey
+ IntToString(i
));
163 retained_arguments
.push_back(kBaseValue
+ IntToString(i
));
165 // For each block we expect to parse out the headers in order.
166 for (int i
= 0; i
< kNumHeaderBlocks
; i
++) {
167 EXPECT_CALL(handler_
, OnHeaderBlock(kNumHeadersInBlock
)).Times(1);
168 for (int j
= 0; j
< kNumHeadersInBlock
; j
++) {
169 EXPECT_CALL(handler_
, OnHeader(
170 StringPiece(retained_arguments
[2 * j
]),
171 StringPiece(retained_arguments
[2 * j
+ 1]))).Times(1);
173 EXPECT_CALL(handler_
, OnHeaderBlockEnd(headers
.length())).Times(1);
175 // Parse the header blocks, feeding the parser one byte at a time.
176 for (int i
= 1; i
<= kNumHeaderBlocks
; i
++) {
177 for (string::iterator it
= headers
.begin(); it
!= headers
.end(); ++it
) {
178 if ((it
+ 1) == headers
.end()) {
179 // Last byte completes the block.
180 EXPECT_TRUE(parser_
->HandleControlFrameHeadersData(i
, &(*it
), 1));
181 EXPECT_EQ(SpdyHeadersBlockParser::NO_PARSER_ERROR
,
182 parser_
->get_error());
184 EXPECT_FALSE(parser_
->HandleControlFrameHeadersData(i
, &(*it
), 1));
185 EXPECT_EQ(SpdyHeadersBlockParser::NEED_MORE_DATA
, parser_
->get_error());
191 TEST_P(SpdyHeadersBlockParserTest
, HandlesEmptyCallsTest
) {
192 EXPECT_CALL(handler_
, OnHeaderBlock(1)).Times(1);
194 string
headers(CreateHeaders(1, false));
196 string expect_key
= kBaseKey
+ IntToString(0);
197 string expect_value
= kBaseValue
+ IntToString(0);
198 EXPECT_CALL(handler_
, OnHeader(StringPiece(expect_key
),
199 StringPiece(expect_value
))).Times(1);
200 EXPECT_CALL(handler_
, OnHeaderBlockEnd(headers
.length())).Times(1);
202 // Send a header in pieces with intermediate empty calls.
203 for (string::iterator it
= headers
.begin(); it
!= headers
.end(); ++it
) {
204 if ((it
+ 1) == headers
.end()) {
205 // Last byte completes the block.
206 EXPECT_TRUE(parser_
->HandleControlFrameHeadersData(1, &(*it
), 1));
207 EXPECT_EQ(SpdyHeadersBlockParser::NO_PARSER_ERROR
, parser_
->get_error());
209 EXPECT_FALSE(parser_
->HandleControlFrameHeadersData(1, &(*it
), 1));
210 EXPECT_EQ(SpdyHeadersBlockParser::NEED_MORE_DATA
, parser_
->get_error());
211 EXPECT_FALSE(parser_
->HandleControlFrameHeadersData(1, NULL
, 0));
216 TEST_P(SpdyHeadersBlockParserTest
, LargeBlocksDiscardedTest
) {
217 // Header block with too many headers.
219 string headers
= EncodeLength(
220 parser_
->MaxNumberOfHeadersForVersion(spdy_version_
) + 1);
221 EXPECT_FALSE(parser_
->
222 HandleControlFrameHeadersData(1, headers
.c_str(), headers
.length()));
223 EXPECT_EQ(SpdyHeadersBlockParser::HEADER_BLOCK_TOO_LARGE
,
224 parser_
->get_error());
226 parser_
.reset(new SpdyHeadersBlockParser(spdy_version_
, &handler_
));
227 // Header block with one header, which has a too-long key.
229 EXPECT_CALL(handler_
, OnHeaderBlock(1)).Times(1);
231 string headers
= EncodeLength(1) + EncodeLength(
232 SpdyHeadersBlockParser::kMaximumFieldLength
+ 1);
233 EXPECT_FALSE(parser_
->
234 HandleControlFrameHeadersData(1, headers
.c_str(), headers
.length()));
235 EXPECT_EQ(SpdyHeadersBlockParser::HEADER_FIELD_TOO_LARGE
,
236 parser_
->get_error());
240 TEST_P(SpdyHeadersBlockParserTest
, ExtraDataTest
) {
241 string headers
= CreateHeaders(1, false) + "foobar";
243 EXPECT_CALL(handler_
, OnHeaderBlock(1)).Times(1);
244 EXPECT_CALL(handler_
, OnHeaderBlockEnd(headers
.length())).Times(1);
246 string expect_key
= kBaseKey
+ IntToString(0);
247 string expect_value
= kBaseValue
+ IntToString(0);
248 EXPECT_CALL(handler_
, OnHeader(StringPiece(expect_key
),
249 StringPiece(expect_value
))).Times(1);
251 EXPECT_FALSE(parser_
->HandleControlFrameHeadersData(1, headers
.c_str(),
253 EXPECT_EQ(SpdyHeadersBlockParser::TOO_MUCH_DATA
, parser_
->get_error());
256 TEST_P(SpdyHeadersBlockParserTest
, WrongStreamIdTest
) {
257 string
headers(CreateHeaders(kNumHeadersInBlock
, false));
258 EXPECT_FALSE(parser_
->HandleControlFrameHeadersData(1, headers
.data(), 1));
259 EXPECT_EQ(SpdyHeadersBlockParser::NEED_MORE_DATA
, parser_
->get_error());
262 result
= parser_
->HandleControlFrameHeadersData(2, headers
.data() + 1, 1),
263 "Unexpected stream id: 2 \\(expected 1\\)");
264 EXPECT_FALSE(result
);
265 EXPECT_EQ(SpdyHeadersBlockParser::UNEXPECTED_STREAM_ID
, parser_
->get_error());
268 TEST_P(SpdyHeadersBlockParserTest
, InvalidStreamIdTest
) {
269 string
headers(CreateHeaders(kNumHeadersInBlock
, false));
272 result
= parser_
->HandleControlFrameHeadersData(0, headers
.data(), 1),
273 "Expected nonzero stream id, saw: 0");
274 EXPECT_FALSE(result
);
275 EXPECT_EQ(SpdyHeadersBlockParser::UNEXPECTED_STREAM_ID
, parser_
->get_error());