Port Android relocation packer to chromium build
[chromium-blink-merge.git] / net / spdy / spdy_headers_block_parser_test.cc
blobbd4a01bfade708967e04ee09f9702456bee2a9c9
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 std::string;
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 {
25 public:
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> {
33 public:
34 virtual ~SpdyHeadersBlockParserTest() {}
36 protected:
37 void SetUp() override {
38 // Create a parser using the mock handler.
39 spdy_version_ = GetParam();
41 parser_.reset(new SpdyHeadersBlockParser(spdy_version_, &handler_));
42 length_field_size_ =
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) {
48 string headers;
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++) {
55 // Build the key.
56 string key;
57 if (insert_nulls) {
58 key = string(kBaseKey) + string("\0", 1) + IntToString(i);
59 } else {
60 key = string(kBaseKey) + IntToString(i);
62 // Encode the key as SPDY header.
63 headers += EncodeLength(key.length());
64 headers += key;
66 // Build the value.
67 string value;
68 if (insert_nulls) {
69 value = string(kBaseValue) + string("\0", 1) + IntToString(i);
70 } else {
71 value = string(kBaseValue) + IntToString(i);
73 // Encode the value as SPDY header.
74 headers += EncodeLength(value.length());
75 headers += value;
77 return headers;
80 string EncodeLength(uint32_t len) {
81 char buffer[4];
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_);
88 } else {
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 EXPECT_EQ(spdy_version_, parser_->spdy_version());
122 string headers(CreateHeaders(1, false));
124 EXPECT_CALL(handler_, OnHeaderBlock(1)).Times(1);
126 std::string expect_key = kBaseKey + IntToString(0);
127 std::string expect_value = kBaseValue + IntToString(0);
128 EXPECT_CALL(handler_, OnHeader(StringPiece(expect_key),
129 StringPiece(expect_value))).Times(1);
130 EXPECT_CALL(handler_, OnHeaderBlockEnd(headers.length())).Times(1);
132 EXPECT_TRUE(parser_->
133 HandleControlFrameHeadersData(1, headers.c_str(), headers.length()));
134 EXPECT_EQ(SpdyHeadersBlockParser::NO_PARSER_ERROR, parser_->get_error());
137 TEST_P(SpdyHeadersBlockParserTest, NullsSupportedTest) {
138 // Sanity test, verify that we parse out correctly a block with
139 // a single key-value pair when the key and value contain null charecters.
140 string headers(CreateHeaders(1, true));
142 EXPECT_CALL(handler_, OnHeaderBlock(1)).Times(1);
144 std::string expect_key = kBaseKey + string("\0", 1) + IntToString(0);
145 std::string expect_value = kBaseValue + string("\0", 1) + IntToString(0);
146 EXPECT_CALL(handler_, OnHeader(StringPiece(expect_key),
147 StringPiece(expect_value))).Times(1);
148 EXPECT_CALL(handler_, OnHeaderBlockEnd(headers.length())).Times(1);
150 EXPECT_TRUE(parser_->
151 HandleControlFrameHeadersData(1, headers.c_str(), headers.length()));
152 EXPECT_EQ(SpdyHeadersBlockParser::NO_PARSER_ERROR, parser_->get_error());
155 TEST_P(SpdyHeadersBlockParserTest, MultipleBlocksAndHeadersWithPartialData) {
156 testing::InSequence s;
158 // CreateHeaders is deterministic; we can call it once for the whole test.
159 string headers(CreateHeaders(kNumHeadersInBlock, false));
161 // The mock doesn't retain storage of arguments, so keep them in scope.
162 std::vector<string> retained_arguments;
163 for (int i = 0; i < kNumHeadersInBlock; i++) {
164 retained_arguments.push_back(kBaseKey + IntToString(i));
165 retained_arguments.push_back(kBaseValue + IntToString(i));
167 // For each block we expect to parse out the headers in order.
168 for (int i = 0; i < kNumHeaderBlocks; i++) {
169 EXPECT_CALL(handler_, OnHeaderBlock(kNumHeadersInBlock)).Times(1);
170 for (int j = 0; j < kNumHeadersInBlock; j++) {
171 EXPECT_CALL(handler_, OnHeader(
172 StringPiece(retained_arguments[2 * j]),
173 StringPiece(retained_arguments[2 * j + 1]))).Times(1);
175 EXPECT_CALL(handler_, OnHeaderBlockEnd(headers.length())).Times(1);
177 // Parse the header blocks, feeding the parser one byte at a time.
178 for (int i = 1; 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::NO_PARSER_ERROR,
184 parser_->get_error());
185 } else {
186 EXPECT_FALSE(parser_->HandleControlFrameHeadersData(i, &(*it), 1));
187 EXPECT_EQ(SpdyHeadersBlockParser::NEED_MORE_DATA, parser_->get_error());
193 TEST_P(SpdyHeadersBlockParserTest, HandlesEmptyCallsTest) {
194 EXPECT_CALL(handler_, OnHeaderBlock(1)).Times(1);
196 string headers(CreateHeaders(1, false));
198 string expect_key = kBaseKey + IntToString(0);
199 string expect_value = kBaseValue + IntToString(0);
200 EXPECT_CALL(handler_, OnHeader(StringPiece(expect_key),
201 StringPiece(expect_value))).Times(1);
202 EXPECT_CALL(handler_, OnHeaderBlockEnd(headers.length())).Times(1);
204 // Send a header in pieces with intermediate empty calls.
205 for (string::iterator it = headers.begin(); it != headers.end(); ++it) {
206 if ((it + 1) == headers.end()) {
207 // Last byte completes the block.
208 EXPECT_TRUE(parser_->HandleControlFrameHeadersData(1, &(*it), 1));
209 EXPECT_EQ(SpdyHeadersBlockParser::NO_PARSER_ERROR, parser_->get_error());
210 } else {
211 EXPECT_FALSE(parser_->HandleControlFrameHeadersData(1, &(*it), 1));
212 EXPECT_EQ(SpdyHeadersBlockParser::NEED_MORE_DATA, parser_->get_error());
213 EXPECT_FALSE(parser_->HandleControlFrameHeadersData(1, NULL, 0));
218 TEST_P(SpdyHeadersBlockParserTest, LargeBlocksDiscardedTest) {
219 // Header block with too many headers.
221 string headers = EncodeLength(
222 parser_->MaxNumberOfHeadersForVersion(spdy_version_) + 1);
223 EXPECT_FALSE(parser_->
224 HandleControlFrameHeadersData(1, headers.c_str(), headers.length()));
225 EXPECT_EQ(SpdyHeadersBlockParser::HEADER_BLOCK_TOO_LARGE,
226 parser_->get_error());
228 parser_.reset(new SpdyHeadersBlockParser(spdy_version_, &handler_));
229 // Header block with one header, which has a too-long key.
231 EXPECT_CALL(handler_, OnHeaderBlock(1)).Times(1);
233 string headers = EncodeLength(1) + EncodeLength(
234 SpdyHeadersBlockParser::kMaximumFieldLength + 1);
235 EXPECT_FALSE(parser_->
236 HandleControlFrameHeadersData(1, headers.c_str(), headers.length()));
237 EXPECT_EQ(SpdyHeadersBlockParser::HEADER_FIELD_TOO_LARGE,
238 parser_->get_error());
242 TEST_P(SpdyHeadersBlockParserTest, ExtraDataTest) {
243 string headers = CreateHeaders(1, false) + "foobar";
245 EXPECT_CALL(handler_, OnHeaderBlock(1)).Times(1);
246 EXPECT_CALL(handler_, OnHeaderBlockEnd(headers.length())).Times(1);
248 string expect_key = kBaseKey + IntToString(0);
249 string expect_value = kBaseValue + IntToString(0);
250 EXPECT_CALL(handler_, OnHeader(StringPiece(expect_key),
251 StringPiece(expect_value))).Times(1);
253 EXPECT_FALSE(parser_->HandleControlFrameHeadersData(1, headers.c_str(),
254 headers.length()));
255 EXPECT_EQ(SpdyHeadersBlockParser::TOO_MUCH_DATA, parser_->get_error());
258 TEST_P(SpdyHeadersBlockParserTest, WrongStreamIdTest) {
259 string headers(CreateHeaders(kNumHeadersInBlock, false));
260 EXPECT_FALSE(parser_->HandleControlFrameHeadersData(1, headers.data(), 1));
261 EXPECT_EQ(SpdyHeadersBlockParser::NEED_MORE_DATA, parser_->get_error());
262 bool result;
263 EXPECT_DFATAL(
264 result = parser_->HandleControlFrameHeadersData(2, headers.data() + 1, 1),
265 "Unexpected stream id: 2 \\(expected 1\\)");
266 EXPECT_FALSE(result);
267 EXPECT_EQ(SpdyHeadersBlockParser::UNEXPECTED_STREAM_ID, parser_->get_error());
270 TEST_P(SpdyHeadersBlockParserTest, InvalidStreamIdTest) {
271 string headers(CreateHeaders(kNumHeadersInBlock, false));
272 bool result;
273 EXPECT_DFATAL(
274 result = parser_->HandleControlFrameHeadersData(0, headers.data(), 1),
275 "Expected nonzero stream id, saw: 0");
276 EXPECT_FALSE(result);
277 EXPECT_EQ(SpdyHeadersBlockParser::UNEXPECTED_STREAM_ID, parser_->get_error());
280 } // namespace net