prune resources in MemoryCache
[chromium-blink-merge.git] / net / spdy / spdy_headers_block_parser_test.cc
blobe2fdb311a1bbc268f6df018cbbca80486e52bb2a
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();
43 parser_.reset(new SpdyHeadersBlockParser(spdy_version_, &handler_));
44 length_field_size_ =
45 SpdyHeadersBlockParser::LengthFieldSizeForVersion(spdy_version_);
48 // Create a header block with a specified number of headers.
49 string CreateHeaders(uint32_t num_headers, bool insert_nulls) {
50 string headers;
52 // First, write the number of headers in the header block.
53 headers += EncodeLength(num_headers);
55 // Second, write the key-value pairs.
56 for (uint32_t i = 0; i < num_headers; i++) {
57 // Build the key.
58 string key;
59 if (insert_nulls) {
60 key = string(kBaseKey) + string("\0", 1) + IntToString(i);
61 } else {
62 key = string(kBaseKey) + IntToString(i);
64 // Encode the key as SPDY header.
65 headers += EncodeLength(key.length());
66 headers += key;
68 // Build the value.
69 string value;
70 if (insert_nulls) {
71 value = string(kBaseValue) + string("\0", 1) + IntToString(i);
72 } else {
73 value = string(kBaseValue) + IntToString(i);
75 // Encode the value as SPDY header.
76 headers += EncodeLength(value.length());
77 headers += value;
79 return headers;
82 string EncodeLength(uint32_t len) {
83 char buffer[4];
84 if (length_field_size_ == sizeof(uint32_t)) {
85 uint32_t net_order_len = htonl(len);
86 memcpy(buffer, &net_order_len, length_field_size_);
87 } else if (length_field_size_ == sizeof(uint16_t)) {
88 uint16_t net_order_len = htons(len);
89 memcpy(buffer, &net_order_len, length_field_size_);
90 } else {
91 CHECK(false) << "Invalid length field size";
93 return string(buffer, length_field_size_);
96 size_t length_field_size_;
97 SpdyMajorVersion spdy_version_;
99 MockSpdyHeadersHandler handler_;
100 scoped_ptr<SpdyHeadersBlockParser> parser_;
102 static const char *const kBaseKey;
103 static const char *const kBaseValue;
105 // Number of headers and header blocks used in the tests.
106 static const int kNumHeadersInBlock = 10;
107 static const int kNumHeaderBlocks = 10;
110 const char *const SpdyHeadersBlockParserTest::kBaseKey = "test_key";
111 const char *const SpdyHeadersBlockParserTest::kBaseValue = "test_value";
113 // All tests are run with 3 different SPDY versions: SPDY/2, SPDY/3, SPDY/4.
114 INSTANTIATE_TEST_CASE_P(SpdyHeadersBlockParserTests,
115 SpdyHeadersBlockParserTest,
116 ::testing::Values(SPDY2, SPDY3, SPDY4));
118 TEST_P(SpdyHeadersBlockParserTest, BasicTest) {
119 // Sanity test, verify that we parse out correctly a block with
120 // a single key-value pair and that we notify when we start and finish
121 // handling a headers block.
122 string headers(CreateHeaders(1, false));
124 EXPECT_CALL(handler_, OnHeaderBlock(1, 1)).Times(1);
126 std::string expect_key = kBaseKey + IntToString(0);
127 std::string expect_value = kBaseValue + IntToString(0);
128 EXPECT_CALL(handler_, OnHeader(1, StringPiece(expect_key),
129 StringPiece(expect_value))).Times(1);
130 EXPECT_CALL(handler_, OnHeaderBlockEnd(1, headers.length())).Times(1);
132 EXPECT_TRUE(parser_->
133 HandleControlFrameHeadersData(1, headers.c_str(), headers.length()));
134 EXPECT_EQ(SpdyHeadersBlockParser::OK, 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, 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(1, StringPiece(expect_key),
147 StringPiece(expect_value))).Times(1);
148 EXPECT_CALL(handler_, OnHeaderBlockEnd(1, headers.length())).Times(1);
150 EXPECT_TRUE(parser_->
151 HandleControlFrameHeadersData(1, headers.c_str(), headers.length()));
152 EXPECT_EQ(SpdyHeadersBlockParser::OK, 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(i, kNumHeadersInBlock)).Times(1);
170 for (int j = 0; j < kNumHeadersInBlock; j++) {
171 EXPECT_CALL(handler_, OnHeader(
173 StringPiece(retained_arguments[2 * j]),
174 StringPiece(retained_arguments[2 * j + 1]))).Times(1);
176 EXPECT_CALL(handler_, OnHeaderBlockEnd(i, headers.length())).Times(1);
178 // Parse the header blocks, feeding the parser one byte at a time.
179 for (int i = 0; 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::OK, 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, 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(1, StringPiece(expect_key),
201 StringPiece(expect_value))).Times(1);
202 EXPECT_CALL(handler_, OnHeaderBlockEnd(1, 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::OK, 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, 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, 1)).Times(1);
246 EXPECT_CALL(handler_, OnHeaderBlockEnd(1, headers.length())).Times(1);
248 string expect_key = kBaseKey + IntToString(0);
249 string expect_value = kBaseValue + IntToString(0);
250 EXPECT_CALL(handler_, OnHeader(1, StringPiece(expect_key),
251 StringPiece(expect_value))).Times(1);
253 EXPECT_FALSE(parser_->
254 HandleControlFrameHeadersData(1, headers.c_str(), headers.length()));
255 EXPECT_EQ(SpdyHeadersBlockParser::TOO_MUCH_DATA, parser_->get_error());
258 } // namespace net