Revert "Fix broken channel icon in chrome://help on CrOS" and try again
[chromium-blink-merge.git] / net / spdy / spdy_headers_block_parser_test.cc
blobb835a623b7f24595159e05e9f05788947c9beae5
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 "net/test/gtest_util.h"
13 #include "testing/gmock/include/gmock/gmock.h"
14 #include "testing/gtest/include/gtest/gtest.h"
16 namespace net {
18 using base::IntToString;
19 using base::StringPiece;
20 using base::UintToString;
21 using std::string;
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 {
26 public:
27 MOCK_METHOD1(OnHeaderBlock, void(uint32_t num_of_headers));
28 MOCK_METHOD1(OnHeaderBlockEnd, void(size_t bytes));
29 MOCK_METHOD2(OnHeader, void(StringPiece key, StringPiece value));
32 class SpdyHeadersBlockParserTest :
33 public ::testing::TestWithParam<SpdyMajorVersion> {
34 public:
35 virtual ~SpdyHeadersBlockParserTest() {}
37 protected:
38 void SetUp() override {
39 // Create a parser using the mock handler.
40 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) + UintToString(i);
60 } else {
61 key = string(kBaseKey) + UintToString(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) + UintToString(i);
71 } else {
72 value = string(kBaseValue) + UintToString(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, HTTP/2.
113 INSTANTIATE_TEST_CASE_P(SpdyHeadersBlockParserTests,
114 SpdyHeadersBlockParserTest,
115 ::testing::Values(SPDY2, SPDY3, HTTP2));
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 EXPECT_EQ(spdy_version_, parser_->spdy_version());
123 string headers(CreateHeaders(1, false));
125 EXPECT_CALL(handler_, OnHeaderBlock(1)).Times(1);
127 std::string expect_key = kBaseKey + IntToString(0);
128 std::string expect_value = kBaseValue + IntToString(0);
129 EXPECT_CALL(handler_, OnHeader(StringPiece(expect_key),
130 StringPiece(expect_value))).Times(1);
131 EXPECT_CALL(handler_, OnHeaderBlockEnd(headers.length())).Times(1);
133 EXPECT_TRUE(parser_->
134 HandleControlFrameHeadersData(1, headers.c_str(), headers.length()));
135 EXPECT_EQ(SpdyHeadersBlockParser::NO_PARSER_ERROR, 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)).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(StringPiece(expect_key),
148 StringPiece(expect_value))).Times(1);
149 EXPECT_CALL(handler_, OnHeaderBlockEnd(headers.length())).Times(1);
151 EXPECT_TRUE(parser_->
152 HandleControlFrameHeadersData(1, headers.c_str(), headers.length()));
153 EXPECT_EQ(SpdyHeadersBlockParser::NO_PARSER_ERROR, 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(kNumHeadersInBlock)).Times(1);
171 for (int j = 0; j < kNumHeadersInBlock; j++) {
172 EXPECT_CALL(handler_, OnHeader(
173 StringPiece(retained_arguments[2 * j]),
174 StringPiece(retained_arguments[2 * j + 1]))).Times(1);
176 EXPECT_CALL(handler_, OnHeaderBlockEnd(headers.length())).Times(1);
178 // Parse the header blocks, feeding the parser one byte at a time.
179 for (int i = 1; i <= kNumHeaderBlocks; i++) {
180 for (string::iterator it = headers.begin(); it != headers.end(); ++it) {
181 if ((it + 1) == headers.end()) {
182 // Last byte completes the block.
183 EXPECT_TRUE(parser_->HandleControlFrameHeadersData(i, &(*it), 1));
184 EXPECT_EQ(SpdyHeadersBlockParser::NO_PARSER_ERROR,
185 parser_->get_error());
186 } else {
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)).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(StringPiece(expect_key),
202 StringPiece(expect_value))).Times(1);
203 EXPECT_CALL(handler_, OnHeaderBlockEnd(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::NO_PARSER_ERROR, parser_->get_error());
211 } else {
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());
229 parser_.reset(new SpdyHeadersBlockParser(spdy_version_, &handler_));
230 // Header block with one header, which has a too-long key.
232 EXPECT_CALL(handler_, OnHeaderBlock(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)).Times(1);
247 EXPECT_CALL(handler_, OnHeaderBlockEnd(headers.length())).Times(1);
249 string expect_key = kBaseKey + IntToString(0);
250 string expect_value = kBaseValue + IntToString(0);
251 EXPECT_CALL(handler_, OnHeader(StringPiece(expect_key),
252 StringPiece(expect_value))).Times(1);
254 EXPECT_FALSE(parser_->HandleControlFrameHeadersData(1, headers.c_str(),
255 headers.length()));
256 EXPECT_EQ(SpdyHeadersBlockParser::TOO_MUCH_DATA, parser_->get_error());
259 TEST_P(SpdyHeadersBlockParserTest, WrongStreamIdTest) {
260 string headers(CreateHeaders(kNumHeadersInBlock, false));
261 EXPECT_FALSE(parser_->HandleControlFrameHeadersData(1, headers.data(), 1));
262 EXPECT_EQ(SpdyHeadersBlockParser::NEED_MORE_DATA, parser_->get_error());
263 bool result;
264 EXPECT_DFATAL(
265 result = parser_->HandleControlFrameHeadersData(2, headers.data() + 1, 1),
266 "Unexpected stream id: 2 \\(expected 1\\)");
267 EXPECT_FALSE(result);
268 EXPECT_EQ(SpdyHeadersBlockParser::UNEXPECTED_STREAM_ID, parser_->get_error());
271 TEST_P(SpdyHeadersBlockParserTest, InvalidStreamIdTest) {
272 string headers(CreateHeaders(kNumHeadersInBlock, false));
273 bool result;
274 EXPECT_DFATAL(
275 result = parser_->HandleControlFrameHeadersData(0, headers.data(), 1),
276 "Expected nonzero stream id, saw: 0");
277 EXPECT_FALSE(result);
278 EXPECT_EQ(SpdyHeadersBlockParser::UNEXPECTED_STREAM_ID, parser_->get_error());
281 } // namespace net