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 "testing/gmock/include/gmock/gmock.h"
13 #include "testing/gtest/include/gtest/gtest.h"
17 using base::IntToString
;
18 using base::StringPiece
;
20 using testing::ElementsAre
;
21 using testing::ElementsAreArray
;
23 // A mock the handler class to check that we parse out the correct headers
24 // and call the callback methods when we should.
25 class MockSpdyHeadersHandler
: public SpdyHeadersHandlerInterface
{
27 MOCK_METHOD2(OnHeaderBlock
, void(SpdyStreamId stream_id
,
28 uint32_t num_of_headers
));
29 MOCK_METHOD2(OnHeaderBlockEnd
, void(SpdyStreamId stream_id
, size_t bytes
));
30 MOCK_METHOD3(OnHeader
, void(SpdyStreamId stream_id
,
35 class SpdyHeadersBlockParserTest
:
36 public ::testing::TestWithParam
<SpdyMajorVersion
> {
38 virtual ~SpdyHeadersBlockParserTest() {}
41 virtual void SetUp() {
42 // Create a parser using the mock handler.
43 spdy_version_
= GetParam();
44 parser_
.reset(new SpdyHeadersBlockParser(spdy_version_
, &handler_
));
46 SpdyHeadersBlockParser::LengthFieldSizeForVersion(spdy_version_
);
49 // Create a header block with a specified number of headers.
50 string
CreateHeaders(uint32_t num_headers
, bool insert_nulls
) {
53 // First, write the number of headers in the header block.
54 headers
+= EncodeLength(num_headers
);
56 // Second, write the key-value pairs.
57 for (uint32_t i
= 0; i
< num_headers
; i
++) {
61 key
= string(kBaseKey
) + string("\0", 1) + IntToString(i
);
63 key
= string(kBaseKey
) + IntToString(i
);
65 // Encode the key as SPDY header.
66 headers
+= EncodeLength(key
.length());
72 value
= string(kBaseValue
) + string("\0", 1) + IntToString(i
);
74 value
= string(kBaseValue
) + IntToString(i
);
76 // Encode the value as SPDY header.
77 headers
+= EncodeLength(value
.length());
83 string
EncodeLength(uint32_t len
) {
85 if (length_field_size_
== sizeof(uint32_t)) {
86 uint32_t net_order_len
= htonl(len
);
87 memcpy(buffer
, &net_order_len
, length_field_size_
);
88 } else if (length_field_size_
== sizeof(uint16_t)) {
89 uint16_t net_order_len
= htons(len
);
90 memcpy(buffer
, &net_order_len
, length_field_size_
);
92 CHECK(false) << "Invalid length field size";
94 return string(buffer
, length_field_size_
);
97 size_t length_field_size_
;
98 SpdyMajorVersion spdy_version_
;
100 MockSpdyHeadersHandler handler_
;
101 scoped_ptr
<SpdyHeadersBlockParser
> parser_
;
103 static const char *const kBaseKey
;
104 static const char *const kBaseValue
;
106 // Number of headers and header blocks used in the tests.
107 static const int kNumHeadersInBlock
= 10;
108 static const int kNumHeaderBlocks
= 10;
111 const char *const SpdyHeadersBlockParserTest::kBaseKey
= "test_key";
112 const char *const SpdyHeadersBlockParserTest::kBaseValue
= "test_value";
114 // All tests are run with 3 different SPDY versions: SPDY/2, SPDY/3, SPDY/4.
115 INSTANTIATE_TEST_CASE_P(SpdyHeadersBlockParserTests
,
116 SpdyHeadersBlockParserTest
,
117 ::testing::Values(SPDY2
, SPDY3
, SPDY4
));
119 TEST_P(SpdyHeadersBlockParserTest
, BasicTest
) {
120 // Sanity test, verify that we parse out correctly a block with
121 // a single key-value pair and that we notify when we start and finish
122 // handling a headers block.
123 string
headers(CreateHeaders(1, false));
125 EXPECT_CALL(handler_
, OnHeaderBlock(1, 1)).Times(1);
127 std::string expect_key
= kBaseKey
+ IntToString(0);
128 std::string expect_value
= kBaseValue
+ IntToString(0);
129 EXPECT_CALL(handler_
, OnHeader(1, StringPiece(expect_key
),
130 StringPiece(expect_value
))).Times(1);
131 EXPECT_CALL(handler_
, OnHeaderBlockEnd(1, headers
.length())).Times(1);
133 EXPECT_TRUE(parser_
->
134 HandleControlFrameHeadersData(1, headers
.c_str(), headers
.length()));
135 EXPECT_EQ(SpdyHeadersBlockParser::OK
, parser_
->get_error());
138 TEST_P(SpdyHeadersBlockParserTest
, NullsSupportedTest
) {
139 // Sanity test, verify that we parse out correctly a block with
140 // a single key-value pair when the key and value contain null charecters.
141 string
headers(CreateHeaders(1, true));
143 EXPECT_CALL(handler_
, OnHeaderBlock(1, 1)).Times(1);
145 std::string expect_key
= kBaseKey
+ string("\0", 1) + IntToString(0);
146 std::string expect_value
= kBaseValue
+ string("\0", 1) + IntToString(0);
147 EXPECT_CALL(handler_
, OnHeader(1, StringPiece(expect_key
),
148 StringPiece(expect_value
))).Times(1);
149 EXPECT_CALL(handler_
, OnHeaderBlockEnd(1, headers
.length())).Times(1);
151 EXPECT_TRUE(parser_
->
152 HandleControlFrameHeadersData(1, headers
.c_str(), headers
.length()));
153 EXPECT_EQ(SpdyHeadersBlockParser::OK
, parser_
->get_error());
156 TEST_P(SpdyHeadersBlockParserTest
, MultipleBlocksAndHeadersWithPartialData
) {
157 testing::InSequence s
;
159 // CreateHeaders is deterministic; we can call it once for the whole test.
160 string
headers(CreateHeaders(kNumHeadersInBlock
, false));
162 // The mock doesn't retain storage of arguments, so keep them in scope.
163 std::vector
<string
> retained_arguments
;
164 for (int i
= 0; i
< kNumHeadersInBlock
; i
++) {
165 retained_arguments
.push_back(kBaseKey
+ IntToString(i
));
166 retained_arguments
.push_back(kBaseValue
+ IntToString(i
));
168 // For each block we expect to parse out the headers in order.
169 for (int i
= 0; i
< kNumHeaderBlocks
; i
++) {
170 EXPECT_CALL(handler_
, OnHeaderBlock(i
, kNumHeadersInBlock
)).Times(1);
171 for (int j
= 0; j
< kNumHeadersInBlock
; j
++) {
172 EXPECT_CALL(handler_
, OnHeader(
174 StringPiece(retained_arguments
[2 * j
]),
175 StringPiece(retained_arguments
[2 * j
+ 1]))).Times(1);
177 EXPECT_CALL(handler_
, OnHeaderBlockEnd(i
, headers
.length())).Times(1);
179 // Parse the header blocks, feeding the parser one byte at a time.
180 for (int i
= 0; i
< kNumHeaderBlocks
; i
++) {
181 for (string::iterator it
= headers
.begin(); it
!= headers
.end(); ++it
) {
182 if ((it
+ 1) == headers
.end()) {
183 // Last byte completes the block.
184 EXPECT_TRUE(parser_
->HandleControlFrameHeadersData(i
, &(*it
), 1));
185 EXPECT_EQ(SpdyHeadersBlockParser::OK
, parser_
->get_error());
187 EXPECT_FALSE(parser_
->HandleControlFrameHeadersData(i
, &(*it
), 1));
188 EXPECT_EQ(SpdyHeadersBlockParser::NEED_MORE_DATA
, parser_
->get_error());
194 TEST_P(SpdyHeadersBlockParserTest
, HandlesEmptyCallsTest
) {
195 EXPECT_CALL(handler_
, OnHeaderBlock(1, 1)).Times(1);
197 string
headers(CreateHeaders(1, false));
199 string expect_key
= kBaseKey
+ IntToString(0);
200 string expect_value
= kBaseValue
+ IntToString(0);
201 EXPECT_CALL(handler_
, OnHeader(1, StringPiece(expect_key
),
202 StringPiece(expect_value
))).Times(1);
203 EXPECT_CALL(handler_
, OnHeaderBlockEnd(1, headers
.length())).Times(1);
205 // Send a header in pieces with intermediate empty calls.
206 for (string::iterator it
= headers
.begin(); it
!= headers
.end(); ++it
) {
207 if ((it
+ 1) == headers
.end()) {
208 // Last byte completes the block.
209 EXPECT_TRUE(parser_
->HandleControlFrameHeadersData(1, &(*it
), 1));
210 EXPECT_EQ(SpdyHeadersBlockParser::OK
, parser_
->get_error());
212 EXPECT_FALSE(parser_
->HandleControlFrameHeadersData(1, &(*it
), 1));
213 EXPECT_EQ(SpdyHeadersBlockParser::NEED_MORE_DATA
, parser_
->get_error());
214 EXPECT_FALSE(parser_
->HandleControlFrameHeadersData(1, NULL
, 0));
219 TEST_P(SpdyHeadersBlockParserTest
, LargeBlocksDiscardedTest
) {
220 // Header block with too many headers.
222 string headers
= EncodeLength(
223 parser_
->MaxNumberOfHeadersForVersion(spdy_version_
) + 1);
224 EXPECT_FALSE(parser_
->
225 HandleControlFrameHeadersData(1, headers
.c_str(), headers
.length()));
226 EXPECT_EQ(SpdyHeadersBlockParser::HEADER_BLOCK_TOO_LARGE
,
227 parser_
->get_error());
230 // Header block with one header, which has a too-long key.
232 EXPECT_CALL(handler_
, OnHeaderBlock(1, 1)).Times(1);
234 string headers
= EncodeLength(1) + EncodeLength(
235 SpdyHeadersBlockParser::kMaximumFieldLength
+ 1);
236 EXPECT_FALSE(parser_
->
237 HandleControlFrameHeadersData(1, headers
.c_str(), headers
.length()));
238 EXPECT_EQ(SpdyHeadersBlockParser::HEADER_FIELD_TOO_LARGE
,
239 parser_
->get_error());
243 TEST_P(SpdyHeadersBlockParserTest
, ExtraDataTest
) {
244 string headers
= CreateHeaders(1, false) + "foobar";
246 EXPECT_CALL(handler_
, OnHeaderBlock(1, 1)).Times(1);
247 EXPECT_CALL(handler_
, OnHeaderBlockEnd(1, headers
.length())).Times(1);
249 string expect_key
= kBaseKey
+ IntToString(0);
250 string expect_value
= kBaseValue
+ IntToString(0);
251 EXPECT_CALL(handler_
, OnHeader(1, StringPiece(expect_key
),
252 StringPiece(expect_value
))).Times(1);
254 EXPECT_FALSE(parser_
->
255 HandleControlFrameHeadersData(1, headers
.c_str(), headers
.length()));
256 EXPECT_EQ(SpdyHeadersBlockParser::TOO_MUCH_DATA
, parser_
->get_error());