Blink roll 25b6bd3a7a131ffe68d809546ad1a20707915cdc:3a503f41ae42e5b79cfcd2ff10e65afde...
[chromium-blink-merge.git] / net / spdy / spdy_headers_block_parser_test.cc
blobdc2cbb5daecd20e38b178aa537017cb5a9135bb6
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"
7 #include <string>
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"
15 namespace net {
17 using base::IntToString;
18 using base::StringPiece;
19 using std::string;
21 // A mock the handler class to check that we parse out the correct headers
22 // and call the callback methods when we should.
23 class MockSpdyHeadersHandler : public SpdyHeadersHandlerInterface {
24 public:
25 MOCK_METHOD2(OnHeaderBlock, void(SpdyStreamId stream_id,
26 uint32_t num_of_headers));
27 MOCK_METHOD2(OnHeaderBlockEnd, void(SpdyStreamId stream_id, size_t bytes));
28 MOCK_METHOD3(OnHeader, void(SpdyStreamId stream_id,
29 StringPiece,
30 StringPiece));
33 class SpdyHeadersBlockParserTest :
34 public ::testing::TestWithParam<SpdyMajorVersion> {
35 public:
36 virtual ~SpdyHeadersBlockParserTest() {}
38 protected:
39 void SetUp() override {
40 // Create a parser using the mock handler.
41 spdy_version_ = GetParam();
42 parser_.reset(new SpdyHeadersBlockParser(spdy_version_, &handler_));
43 length_field_size_ =
44 SpdyHeadersBlockParser::LengthFieldSizeForVersion(spdy_version_);
47 // Create a header block with a specified number of headers.
48 string CreateHeaders(uint32_t num_headers, bool insert_nulls) {
49 string headers;
51 // First, write the number of headers in the header block.
52 headers += EncodeLength(num_headers);
54 // Second, write the key-value pairs.
55 for (uint32_t i = 0; i < num_headers; i++) {
56 // Build the key.
57 string key;
58 if (insert_nulls) {
59 key = string(kBaseKey) + string("\0", 1) + IntToString(i);
60 } else {
61 key = string(kBaseKey) + IntToString(i);
63 // Encode the key as SPDY header.
64 headers += EncodeLength(key.length());
65 headers += key;
67 // Build the value.
68 string value;
69 if (insert_nulls) {
70 value = string(kBaseValue) + string("\0", 1) + IntToString(i);
71 } else {
72 value = string(kBaseValue) + IntToString(i);
74 // Encode the value as SPDY header.
75 headers += EncodeLength(value.length());
76 headers += value;
78 return headers;
81 string EncodeLength(uint32_t len) {
82 char buffer[4];
83 if (length_field_size_ == sizeof(uint32_t)) {
84 uint32_t net_order_len = htonl(len);
85 memcpy(buffer, &net_order_len, length_field_size_);
86 } else if (length_field_size_ == sizeof(uint16_t)) {
87 uint16_t net_order_len = htons(len);
88 memcpy(buffer, &net_order_len, length_field_size_);
89 } else {
90 CHECK(false) << "Invalid length field size";
92 return string(buffer, length_field_size_);
95 size_t length_field_size_;
96 SpdyMajorVersion spdy_version_;
98 MockSpdyHeadersHandler handler_;
99 scoped_ptr<SpdyHeadersBlockParser> parser_;
101 static const char *const kBaseKey;
102 static const char *const kBaseValue;
104 // Number of headers and header blocks used in the tests.
105 static const int kNumHeadersInBlock = 10;
106 static const int kNumHeaderBlocks = 10;
109 const char *const SpdyHeadersBlockParserTest::kBaseKey = "test_key";
110 const char *const SpdyHeadersBlockParserTest::kBaseValue = "test_value";
112 // All tests are run with 3 different SPDY versions: SPDY/2, SPDY/3, SPDY/4.
113 INSTANTIATE_TEST_CASE_P(SpdyHeadersBlockParserTests,
114 SpdyHeadersBlockParserTest,
115 ::testing::Values(SPDY2, SPDY3, SPDY4));
117 TEST_P(SpdyHeadersBlockParserTest, BasicTest) {
118 // Sanity test, verify that we parse out correctly a block with
119 // a single key-value pair and that we notify when we start and finish
120 // handling a headers block.
121 string headers(CreateHeaders(1, false));
123 EXPECT_CALL(handler_, OnHeaderBlock(1, 1)).Times(1);
125 std::string expect_key = kBaseKey + IntToString(0);
126 std::string expect_value = kBaseValue + IntToString(0);
127 EXPECT_CALL(handler_, OnHeader(1, StringPiece(expect_key),
128 StringPiece(expect_value))).Times(1);
129 EXPECT_CALL(handler_, OnHeaderBlockEnd(1, headers.length())).Times(1);
131 EXPECT_TRUE(parser_->
132 HandleControlFrameHeadersData(1, headers.c_str(), headers.length()));
133 EXPECT_EQ(SpdyHeadersBlockParser::OK, parser_->get_error());
136 TEST_P(SpdyHeadersBlockParserTest, NullsSupportedTest) {
137 // Sanity test, verify that we parse out correctly a block with
138 // a single key-value pair when the key and value contain null charecters.
139 string headers(CreateHeaders(1, true));
141 EXPECT_CALL(handler_, OnHeaderBlock(1, 1)).Times(1);
143 std::string expect_key = kBaseKey + string("\0", 1) + IntToString(0);
144 std::string expect_value = kBaseValue + string("\0", 1) + IntToString(0);
145 EXPECT_CALL(handler_, OnHeader(1, StringPiece(expect_key),
146 StringPiece(expect_value))).Times(1);
147 EXPECT_CALL(handler_, OnHeaderBlockEnd(1, headers.length())).Times(1);
149 EXPECT_TRUE(parser_->
150 HandleControlFrameHeadersData(1, headers.c_str(), headers.length()));
151 EXPECT_EQ(SpdyHeadersBlockParser::OK, parser_->get_error());
154 TEST_P(SpdyHeadersBlockParserTest, MultipleBlocksAndHeadersWithPartialData) {
155 testing::InSequence s;
157 // CreateHeaders is deterministic; we can call it once for the whole test.
158 string headers(CreateHeaders(kNumHeadersInBlock, false));
160 // The mock doesn't retain storage of arguments, so keep them in scope.
161 std::vector<string> retained_arguments;
162 for (int i = 0; i < kNumHeadersInBlock; i++) {
163 retained_arguments.push_back(kBaseKey + IntToString(i));
164 retained_arguments.push_back(kBaseValue + IntToString(i));
166 // For each block we expect to parse out the headers in order.
167 for (int i = 0; i < kNumHeaderBlocks; i++) {
168 EXPECT_CALL(handler_, OnHeaderBlock(i, kNumHeadersInBlock)).Times(1);
169 for (int j = 0; j < kNumHeadersInBlock; j++) {
170 EXPECT_CALL(handler_, OnHeader(
172 StringPiece(retained_arguments[2 * j]),
173 StringPiece(retained_arguments[2 * j + 1]))).Times(1);
175 EXPECT_CALL(handler_, OnHeaderBlockEnd(i, headers.length())).Times(1);
177 // Parse the header blocks, feeding the parser one byte at a time.
178 for (int i = 0; i < kNumHeaderBlocks; i++) {
179 for (string::iterator it = headers.begin(); it != headers.end(); ++it) {
180 if ((it + 1) == headers.end()) {
181 // Last byte completes the block.
182 EXPECT_TRUE(parser_->HandleControlFrameHeadersData(i, &(*it), 1));
183 EXPECT_EQ(SpdyHeadersBlockParser::OK, parser_->get_error());
184 } else {
185 EXPECT_FALSE(parser_->HandleControlFrameHeadersData(i, &(*it), 1));
186 EXPECT_EQ(SpdyHeadersBlockParser::NEED_MORE_DATA, parser_->get_error());
192 TEST_P(SpdyHeadersBlockParserTest, HandlesEmptyCallsTest) {
193 EXPECT_CALL(handler_, OnHeaderBlock(1, 1)).Times(1);
195 string headers(CreateHeaders(1, false));
197 string expect_key = kBaseKey + IntToString(0);
198 string expect_value = kBaseValue + IntToString(0);
199 EXPECT_CALL(handler_, OnHeader(1, StringPiece(expect_key),
200 StringPiece(expect_value))).Times(1);
201 EXPECT_CALL(handler_, OnHeaderBlockEnd(1, headers.length())).Times(1);
203 // Send a header in pieces with intermediate empty calls.
204 for (string::iterator it = headers.begin(); it != headers.end(); ++it) {
205 if ((it + 1) == headers.end()) {
206 // Last byte completes the block.
207 EXPECT_TRUE(parser_->HandleControlFrameHeadersData(1, &(*it), 1));
208 EXPECT_EQ(SpdyHeadersBlockParser::OK, parser_->get_error());
209 } else {
210 EXPECT_FALSE(parser_->HandleControlFrameHeadersData(1, &(*it), 1));
211 EXPECT_EQ(SpdyHeadersBlockParser::NEED_MORE_DATA, parser_->get_error());
212 EXPECT_FALSE(parser_->HandleControlFrameHeadersData(1, NULL, 0));
217 TEST_P(SpdyHeadersBlockParserTest, LargeBlocksDiscardedTest) {
218 // Header block with too many headers.
220 string headers = EncodeLength(
221 parser_->MaxNumberOfHeadersForVersion(spdy_version_) + 1);
222 EXPECT_FALSE(parser_->
223 HandleControlFrameHeadersData(1, headers.c_str(), headers.length()));
224 EXPECT_EQ(SpdyHeadersBlockParser::HEADER_BLOCK_TOO_LARGE,
225 parser_->get_error());
227 parser_->Reset();
228 // Header block with one header, which has a too-long key.
230 EXPECT_CALL(handler_, OnHeaderBlock(1, 1)).Times(1);
232 string headers = EncodeLength(1) + EncodeLength(
233 SpdyHeadersBlockParser::kMaximumFieldLength + 1);
234 EXPECT_FALSE(parser_->
235 HandleControlFrameHeadersData(1, headers.c_str(), headers.length()));
236 EXPECT_EQ(SpdyHeadersBlockParser::HEADER_FIELD_TOO_LARGE,
237 parser_->get_error());
241 TEST_P(SpdyHeadersBlockParserTest, ExtraDataTest) {
242 string headers = CreateHeaders(1, false) + "foobar";
244 EXPECT_CALL(handler_, OnHeaderBlock(1, 1)).Times(1);
245 EXPECT_CALL(handler_, OnHeaderBlockEnd(1, headers.length())).Times(1);
247 string expect_key = kBaseKey + IntToString(0);
248 string expect_value = kBaseValue + IntToString(0);
249 EXPECT_CALL(handler_, OnHeader(1, StringPiece(expect_key),
250 StringPiece(expect_value))).Times(1);
252 EXPECT_FALSE(parser_->
253 HandleControlFrameHeadersData(1, headers.c_str(), headers.length()));
254 EXPECT_EQ(SpdyHeadersBlockParser::TOO_MUCH_DATA, parser_->get_error());
257 } // namespace net