OLD | NEW |
| (Empty) |
1 // Copyright 2016 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. | |
4 | |
5 #include "net/spdy/hpack/hpack_decoder2.h" | |
6 | |
7 // Tests of HpackDecoder2. | |
8 | |
9 #include <string> | |
10 #include <tuple> | |
11 #include <utility> | |
12 #include <vector> | |
13 | |
14 #include "base/logging.h" | |
15 #include "base/strings/string_piece.h" | |
16 #include "net/http2/hpack/tools/hpack_block_builder.h" | |
17 #include "net/http2/tools/http2_random.h" | |
18 #include "net/spdy/hpack/hpack_encoder.h" | |
19 #include "net/spdy/hpack/hpack_entry.h" | |
20 #include "net/spdy/hpack/hpack_huffman_table.h" | |
21 #include "net/spdy/hpack/hpack_output_stream.h" | |
22 #include "net/spdy/spdy_test_utils.h" | |
23 #include "testing/gtest/include/gtest/gtest.h" | |
24 | |
25 using base::StringPiece; | |
26 using std::string; | |
27 | |
28 namespace net { | |
29 namespace test { | |
30 | |
31 class HpackDecoder2Peer { | |
32 public: | |
33 explicit HpackDecoder2Peer(HpackDecoder2* decoder) : decoder_(decoder) {} | |
34 | |
35 void HandleHeaderRepresentation(StringPiece name, StringPiece value) { | |
36 decoder_->HandleHeaderRepresentation(name, value); | |
37 } | |
38 HpackHeaderTable* header_table() { return &decoder_->header_table_; } | |
39 | |
40 private: | |
41 HpackDecoder2* decoder_; | |
42 }; | |
43 | |
44 namespace { | |
45 | |
46 using testing::ElementsAre; | |
47 using testing::Pair; | |
48 | |
49 // Is HandleControlFrameHeadersStart to be called, and with what value? | |
50 enum StartChoice { START_WITH_HANDLER, START_WITHOUT_HANDLER, NO_START }; | |
51 | |
52 class HpackDecoder2Test | |
53 : public ::testing::TestWithParam<std::tuple<StartChoice, bool>> { | |
54 protected: | |
55 HpackDecoder2Test() : decoder_(), decoder_peer_(&decoder_) {} | |
56 | |
57 void SetUp() override { | |
58 std::tie(start_choice_, randomly_split_input_buffer_) = GetParam(); | |
59 } | |
60 | |
61 void HandleControlFrameHeadersStart() { | |
62 switch (start_choice_) { | |
63 case START_WITH_HANDLER: | |
64 decoder_.HandleControlFrameHeadersStart(&handler_); | |
65 break; | |
66 case START_WITHOUT_HANDLER: | |
67 decoder_.HandleControlFrameHeadersStart(nullptr); | |
68 break; | |
69 case NO_START: | |
70 break; | |
71 } | |
72 } | |
73 | |
74 bool HandleControlFrameHeadersData(StringPiece str) { | |
75 return decoder_.HandleControlFrameHeadersData(str.data(), str.size()); | |
76 } | |
77 | |
78 bool HandleControlFrameHeadersComplete(size_t* size) { | |
79 return decoder_.HandleControlFrameHeadersComplete(size); | |
80 } | |
81 | |
82 bool DecodeHeaderBlock(StringPiece str) { | |
83 // Don't call this again if HandleControlFrameHeadersData failed previously. | |
84 EXPECT_FALSE(decode_has_failed_); | |
85 HandleControlFrameHeadersStart(); | |
86 if (randomly_split_input_buffer_) { | |
87 do { | |
88 // Decode some fragment of the remaining bytes. | |
89 size_t bytes = str.length(); | |
90 if (!str.empty()) { | |
91 bytes = (random_.Rand8() % str.length()) + 1; | |
92 } | |
93 EXPECT_LE(bytes, str.length()); | |
94 if (!HandleControlFrameHeadersData(str.substr(0, bytes))) { | |
95 decode_has_failed_ = true; | |
96 return false; | |
97 } | |
98 str.remove_prefix(bytes); | |
99 } while (!str.empty()); | |
100 } else if (!HandleControlFrameHeadersData(str)) { | |
101 decode_has_failed_ = true; | |
102 return false; | |
103 } | |
104 if (!HandleControlFrameHeadersComplete(nullptr)) { | |
105 decode_has_failed_ = true; | |
106 return false; | |
107 } | |
108 return true; | |
109 } | |
110 | |
111 const SpdyHeaderBlock& decoded_block() const { | |
112 if (start_choice_ == START_WITH_HANDLER) { | |
113 return handler_.decoded_block(); | |
114 } else { | |
115 return decoder_.decoded_block(); | |
116 } | |
117 } | |
118 | |
119 const SpdyHeaderBlock& DecodeBlockExpectingSuccess(StringPiece str) { | |
120 EXPECT_TRUE(DecodeHeaderBlock(str)); | |
121 return decoded_block(); | |
122 } | |
123 | |
124 void expectEntry(size_t index, | |
125 size_t size, | |
126 const string& name, | |
127 const string& value) { | |
128 const HpackEntry* entry = decoder_peer_.header_table()->GetByIndex(index); | |
129 EXPECT_EQ(name, entry->name()) << "index " << index; | |
130 EXPECT_EQ(value, entry->value()); | |
131 EXPECT_EQ(size, entry->Size()); | |
132 EXPECT_EQ(index, decoder_peer_.header_table()->IndexOf(entry)); | |
133 } | |
134 | |
135 SpdyHeaderBlock MakeHeaderBlock( | |
136 const std::vector<std::pair<string, string>>& headers) { | |
137 SpdyHeaderBlock result; | |
138 for (const auto& kv : headers) { | |
139 result.AppendValueOrAddHeader(kv.first, kv.second); | |
140 } | |
141 return result; | |
142 } | |
143 | |
144 Http2Random random_; | |
145 HpackDecoder2 decoder_; | |
146 test::HpackDecoder2Peer decoder_peer_; | |
147 TestHeadersHandler handler_; | |
148 StartChoice start_choice_; | |
149 bool randomly_split_input_buffer_; | |
150 bool decode_has_failed_ = false; | |
151 }; | |
152 | |
153 INSTANTIATE_TEST_CASE_P( | |
154 StartChoiceAndRandomlySplitChoice, | |
155 HpackDecoder2Test, | |
156 ::testing::Combine( | |
157 ::testing::Values(START_WITH_HANDLER, START_WITHOUT_HANDLER, NO_START), | |
158 ::testing::Bool())); | |
159 | |
160 TEST_P(HpackDecoder2Test, AddHeaderDataWithHandleControlFrameHeadersData) { | |
161 // The hpack decode buffer size is limited in size. This test verifies that | |
162 // adding encoded data under that limit is accepted, and data that exceeds the | |
163 // limit is rejected. | |
164 HandleControlFrameHeadersStart(); | |
165 const size_t kMaxBufferSizeBytes = 50; | |
166 const string a_value = string(49, 'x'); | |
167 decoder_.set_max_decode_buffer_size_bytes(kMaxBufferSizeBytes); | |
168 { | |
169 HpackBlockBuilder hbb; | |
170 hbb.AppendLiteralNameAndValue(HpackEntryType::kNeverIndexedLiteralHeader, | |
171 false, "a", false, a_value); | |
172 const auto& s = hbb.buffer(); | |
173 EXPECT_TRUE(decoder_.HandleControlFrameHeadersData(s.data(), s.size())); | |
174 } | |
175 { | |
176 HpackBlockBuilder hbb; | |
177 hbb.AppendLiteralNameAndValue(HpackEntryType::kNeverIndexedLiteralHeader, | |
178 false, "b", false, string(51, 'x')); | |
179 const auto& s = hbb.buffer(); | |
180 EXPECT_FALSE(decoder_.HandleControlFrameHeadersData(s.data(), s.size())); | |
181 } | |
182 | |
183 SpdyHeaderBlock expected_block = MakeHeaderBlock({{"a", a_value}}); | |
184 EXPECT_EQ(expected_block, decoded_block()); | |
185 } | |
186 | |
187 TEST_P(HpackDecoder2Test, NameTooLong) { | |
188 // Verify that a name longer than the allowed size generates an error. | |
189 const size_t kMaxBufferSizeBytes = 50; | |
190 const string name = string(2 * kMaxBufferSizeBytes, 'x'); | |
191 const string value = "abc"; | |
192 | |
193 decoder_.set_max_decode_buffer_size_bytes(kMaxBufferSizeBytes); | |
194 | |
195 HpackBlockBuilder hbb; | |
196 hbb.AppendLiteralNameAndValue(HpackEntryType::kNeverIndexedLiteralHeader, | |
197 false, name, false, value); | |
198 | |
199 const size_t fragment_size = (3 * kMaxBufferSizeBytes) / 2; | |
200 const string fragment = hbb.buffer().substr(0, fragment_size); | |
201 | |
202 HandleControlFrameHeadersStart(); | |
203 EXPECT_FALSE(HandleControlFrameHeadersData(fragment)); | |
204 } | |
205 | |
206 TEST_P(HpackDecoder2Test, HeaderTooLongToBuffer) { | |
207 // Verify that a header longer than the allowed size generates an error if | |
208 // it isn't all in one input buffer. | |
209 const string name = "some-key"; | |
210 const string value = "some-value"; | |
211 const size_t kMaxBufferSizeBytes = name.size() + value.size() - 2; | |
212 decoder_.set_max_decode_buffer_size_bytes(kMaxBufferSizeBytes); | |
213 | |
214 HpackBlockBuilder hbb; | |
215 hbb.AppendLiteralNameAndValue(HpackEntryType::kNeverIndexedLiteralHeader, | |
216 false, name, false, value); | |
217 const size_t fragment_size = hbb.size() - 1; | |
218 const string fragment = hbb.buffer().substr(0, fragment_size); | |
219 | |
220 HandleControlFrameHeadersStart(); | |
221 EXPECT_FALSE(HandleControlFrameHeadersData(fragment)); | |
222 } | |
223 | |
224 // Decode with incomplete data in buffer. | |
225 TEST_P(HpackDecoder2Test, DecodeWithIncompleteData) { | |
226 HandleControlFrameHeadersStart(); | |
227 | |
228 // No need to wait for more data. | |
229 EXPECT_TRUE(HandleControlFrameHeadersData("\x82\x85\x82")); | |
230 std::vector<std::pair<string, string>> expected_headers = { | |
231 {":method", "GET"}, {":path", "/index.html"}, {":method", "GET"}}; | |
232 | |
233 SpdyHeaderBlock expected_block1 = MakeHeaderBlock(expected_headers); | |
234 EXPECT_EQ(expected_block1, decoded_block()); | |
235 | |
236 // Full and partial headers, won't add partial to the headers. | |
237 EXPECT_TRUE( | |
238 HandleControlFrameHeadersData("\x40\x03goo" | |
239 "\x03gar\xbe\x40\x04spam")); | |
240 expected_headers.push_back({"goo", "gar"}); | |
241 expected_headers.push_back({"goo", "gar"}); | |
242 | |
243 SpdyHeaderBlock expected_block2 = MakeHeaderBlock(expected_headers); | |
244 EXPECT_EQ(expected_block2, decoded_block()); | |
245 | |
246 // Add the needed data. | |
247 EXPECT_TRUE(HandleControlFrameHeadersData("\x04gggs")); | |
248 | |
249 size_t size = 0; | |
250 EXPECT_TRUE(HandleControlFrameHeadersComplete(&size)); | |
251 EXPECT_EQ(24u, size); | |
252 | |
253 expected_headers.push_back({"spam", "gggs"}); | |
254 | |
255 SpdyHeaderBlock expected_block3 = MakeHeaderBlock(expected_headers); | |
256 EXPECT_EQ(expected_block3, decoded_block()); | |
257 } | |
258 | |
259 TEST_P(HpackDecoder2Test, HandleHeaderRepresentation) { | |
260 // Make sure the decoder is properly initialized. | |
261 HandleControlFrameHeadersStart(); | |
262 HandleControlFrameHeadersData(""); | |
263 | |
264 // All cookie crumbs are joined. | |
265 decoder_peer_.HandleHeaderRepresentation("cookie", " part 1"); | |
266 decoder_peer_.HandleHeaderRepresentation("cookie", "part 2 "); | |
267 decoder_peer_.HandleHeaderRepresentation("cookie", "part3"); | |
268 | |
269 // Already-delimited headers are passed through. | |
270 decoder_peer_.HandleHeaderRepresentation("passed-through", | |
271 string("foo\0baz", 7)); | |
272 | |
273 // Other headers are joined on \0. Case matters. | |
274 decoder_peer_.HandleHeaderRepresentation("joined", "not joined"); | |
275 decoder_peer_.HandleHeaderRepresentation("joineD", "value 1"); | |
276 decoder_peer_.HandleHeaderRepresentation("joineD", "value 2"); | |
277 | |
278 // Empty headers remain empty. | |
279 decoder_peer_.HandleHeaderRepresentation("empty", ""); | |
280 | |
281 // Joined empty headers work as expected. | |
282 decoder_peer_.HandleHeaderRepresentation("empty-joined", ""); | |
283 decoder_peer_.HandleHeaderRepresentation("empty-joined", "foo"); | |
284 decoder_peer_.HandleHeaderRepresentation("empty-joined", ""); | |
285 decoder_peer_.HandleHeaderRepresentation("empty-joined", ""); | |
286 | |
287 // Non-contiguous cookie crumb. | |
288 decoder_peer_.HandleHeaderRepresentation("cookie", " fin!"); | |
289 | |
290 // Finish and emit all headers. | |
291 decoder_.HandleControlFrameHeadersComplete(nullptr); | |
292 | |
293 // Resulting decoded headers are in the same order as the inputs. | |
294 EXPECT_THAT(decoded_block(), | |
295 ElementsAre(Pair("cookie", " part 1; part 2 ; part3; fin!"), | |
296 Pair("passed-through", StringPiece("foo\0baz", 7)), | |
297 Pair("joined", "not joined"), | |
298 Pair("joineD", StringPiece("value 1\0value 2", 15)), | |
299 Pair("empty", ""), | |
300 Pair("empty-joined", StringPiece("\0foo\0\0", 6)))); | |
301 } | |
302 | |
303 // Decoding indexed static table field should work. | |
304 TEST_P(HpackDecoder2Test, IndexedHeaderStatic) { | |
305 // Reference static table entries #2 and #5. | |
306 const SpdyHeaderBlock& header_set1 = DecodeBlockExpectingSuccess("\x82\x85"); | |
307 SpdyHeaderBlock expected_header_set1; | |
308 expected_header_set1[":method"] = "GET"; | |
309 expected_header_set1[":path"] = "/index.html"; | |
310 EXPECT_EQ(expected_header_set1, header_set1); | |
311 | |
312 // Reference static table entry #2. | |
313 const SpdyHeaderBlock& header_set2 = DecodeBlockExpectingSuccess("\x82"); | |
314 SpdyHeaderBlock expected_header_set2; | |
315 expected_header_set2[":method"] = "GET"; | |
316 EXPECT_EQ(expected_header_set2, header_set2); | |
317 } | |
318 | |
319 TEST_P(HpackDecoder2Test, IndexedHeaderDynamic) { | |
320 // First header block: add an entry to header table. | |
321 const SpdyHeaderBlock& header_set1 = DecodeBlockExpectingSuccess( | |
322 "\x40\x03" | |
323 "foo" | |
324 "\x03" | |
325 "bar"); | |
326 SpdyHeaderBlock expected_header_set1; | |
327 expected_header_set1["foo"] = "bar"; | |
328 EXPECT_EQ(expected_header_set1, header_set1); | |
329 | |
330 // Second header block: add another entry to header table. | |
331 const SpdyHeaderBlock& header_set2 = DecodeBlockExpectingSuccess( | |
332 "\xbe\x40\x04" | |
333 "spam" | |
334 "\x04" | |
335 "eggs"); | |
336 SpdyHeaderBlock expected_header_set2; | |
337 expected_header_set2["foo"] = "bar"; | |
338 expected_header_set2["spam"] = "eggs"; | |
339 EXPECT_EQ(expected_header_set2, header_set2); | |
340 | |
341 // Third header block: refer to most recently added entry. | |
342 const SpdyHeaderBlock& header_set3 = DecodeBlockExpectingSuccess("\xbe"); | |
343 SpdyHeaderBlock expected_header_set3; | |
344 expected_header_set3["spam"] = "eggs"; | |
345 EXPECT_EQ(expected_header_set3, header_set3); | |
346 } | |
347 | |
348 // Test a too-large indexed header. | |
349 TEST_P(HpackDecoder2Test, InvalidIndexedHeader) { | |
350 // High-bit set, and a prefix of one more than the number of static entries. | |
351 EXPECT_FALSE(DecodeHeaderBlock("\xbe")); | |
352 } | |
353 | |
354 TEST_P(HpackDecoder2Test, ContextUpdateMaximumSize) { | |
355 EXPECT_EQ(kDefaultHeaderTableSizeSetting, | |
356 decoder_peer_.header_table()->max_size()); | |
357 string input; | |
358 { | |
359 // Maximum-size update with size 126. Succeeds. | |
360 HpackOutputStream output_stream; | |
361 output_stream.AppendPrefix(kHeaderTableSizeUpdateOpcode); | |
362 output_stream.AppendUint32(126); | |
363 | |
364 output_stream.TakeString(&input); | |
365 EXPECT_TRUE(DecodeHeaderBlock(StringPiece(input))); | |
366 EXPECT_EQ(126u, decoder_peer_.header_table()->max_size()); | |
367 } | |
368 { | |
369 // Maximum-size update with kDefaultHeaderTableSizeSetting. Succeeds. | |
370 HpackOutputStream output_stream; | |
371 output_stream.AppendPrefix(kHeaderTableSizeUpdateOpcode); | |
372 output_stream.AppendUint32(kDefaultHeaderTableSizeSetting); | |
373 | |
374 output_stream.TakeString(&input); | |
375 EXPECT_TRUE(DecodeHeaderBlock(StringPiece(input))); | |
376 EXPECT_EQ(kDefaultHeaderTableSizeSetting, | |
377 decoder_peer_.header_table()->max_size()); | |
378 } | |
379 { | |
380 // Maximum-size update with kDefaultHeaderTableSizeSetting + 1. Fails. | |
381 HpackOutputStream output_stream; | |
382 output_stream.AppendPrefix(kHeaderTableSizeUpdateOpcode); | |
383 output_stream.AppendUint32(kDefaultHeaderTableSizeSetting + 1); | |
384 | |
385 output_stream.TakeString(&input); | |
386 EXPECT_FALSE(DecodeHeaderBlock(StringPiece(input))); | |
387 EXPECT_EQ(kDefaultHeaderTableSizeSetting, | |
388 decoder_peer_.header_table()->max_size()); | |
389 } | |
390 } | |
391 | |
392 // Two HeaderTableSizeUpdates may appear at the beginning of the block | |
393 TEST_P(HpackDecoder2Test, TwoTableSizeUpdates) { | |
394 string input; | |
395 { | |
396 // Should accept two table size updates, update to second one | |
397 HpackOutputStream output_stream; | |
398 output_stream.AppendPrefix(kHeaderTableSizeUpdateOpcode); | |
399 output_stream.AppendUint32(0); | |
400 output_stream.AppendPrefix(kHeaderTableSizeUpdateOpcode); | |
401 output_stream.AppendUint32(122); | |
402 | |
403 output_stream.TakeString(&input); | |
404 EXPECT_TRUE(DecodeHeaderBlock(StringPiece(input))); | |
405 EXPECT_EQ(122u, decoder_peer_.header_table()->max_size()); | |
406 } | |
407 } | |
408 | |
409 // Three HeaderTableSizeUpdates should result in an error | |
410 TEST_P(HpackDecoder2Test, ThreeTableSizeUpdatesError) { | |
411 string input; | |
412 { | |
413 // Should reject three table size updates, update to second one | |
414 HpackOutputStream output_stream; | |
415 output_stream.AppendPrefix(kHeaderTableSizeUpdateOpcode); | |
416 output_stream.AppendUint32(5); | |
417 output_stream.AppendPrefix(kHeaderTableSizeUpdateOpcode); | |
418 output_stream.AppendUint32(10); | |
419 output_stream.AppendPrefix(kHeaderTableSizeUpdateOpcode); | |
420 output_stream.AppendUint32(15); | |
421 | |
422 output_stream.TakeString(&input); | |
423 | |
424 EXPECT_FALSE(DecodeHeaderBlock(StringPiece(input))); | |
425 EXPECT_EQ(10u, decoder_peer_.header_table()->max_size()); | |
426 } | |
427 } | |
428 | |
429 // HeaderTableSizeUpdates may only appear at the beginning of the block | |
430 // Any other updates should result in an error | |
431 TEST_P(HpackDecoder2Test, TableSizeUpdateSecondError) { | |
432 string input; | |
433 { | |
434 // Should reject a table size update appearing after a different entry | |
435 // The table size should remain as the default | |
436 HpackOutputStream output_stream; | |
437 output_stream.AppendBytes("\x82\x85"); | |
438 output_stream.AppendPrefix(kHeaderTableSizeUpdateOpcode); | |
439 output_stream.AppendUint32(123); | |
440 | |
441 output_stream.TakeString(&input); | |
442 | |
443 EXPECT_FALSE(DecodeHeaderBlock(StringPiece(input))); | |
444 EXPECT_EQ(kDefaultHeaderTableSizeSetting, | |
445 decoder_peer_.header_table()->max_size()); | |
446 } | |
447 } | |
448 | |
449 // HeaderTableSizeUpdates may only appear at the beginning of the block | |
450 // Any other updates should result in an error | |
451 TEST_P(HpackDecoder2Test, TableSizeUpdateFirstThirdError) { | |
452 string input; | |
453 { | |
454 // Should reject the second table size update | |
455 // if a different entry appears after the first update | |
456 // The table size should update to the first but not the second | |
457 HpackOutputStream output_stream; | |
458 output_stream.AppendPrefix(kHeaderTableSizeUpdateOpcode); | |
459 output_stream.AppendUint32(60); | |
460 output_stream.AppendBytes("\x82\x85"); | |
461 output_stream.AppendPrefix(kHeaderTableSizeUpdateOpcode); | |
462 output_stream.AppendUint32(125); | |
463 | |
464 output_stream.TakeString(&input); | |
465 | |
466 EXPECT_FALSE(DecodeHeaderBlock(StringPiece(input))); | |
467 EXPECT_EQ(60u, decoder_peer_.header_table()->max_size()); | |
468 } | |
469 } | |
470 | |
471 // Decoding two valid encoded literal headers with no indexing should | |
472 // work. | |
473 TEST_P(HpackDecoder2Test, LiteralHeaderNoIndexing) { | |
474 // First header with indexed name, second header with string literal | |
475 // name. | |
476 const char input[] = "\x04\x0c/sample/path\x00\x06:path2\x0e/sample/path/2"; | |
477 const SpdyHeaderBlock& header_set = | |
478 DecodeBlockExpectingSuccess(StringPiece(input, arraysize(input) - 1)); | |
479 | |
480 SpdyHeaderBlock expected_header_set; | |
481 expected_header_set[":path"] = "/sample/path"; | |
482 expected_header_set[":path2"] = "/sample/path/2"; | |
483 EXPECT_EQ(expected_header_set, header_set); | |
484 } | |
485 | |
486 // Decoding two valid encoded literal headers with incremental | |
487 // indexing and string literal names should work. | |
488 TEST_P(HpackDecoder2Test, LiteralHeaderIncrementalIndexing) { | |
489 const char input[] = "\x44\x0c/sample/path\x40\x06:path2\x0e/sample/path/2"; | |
490 const SpdyHeaderBlock& header_set = | |
491 DecodeBlockExpectingSuccess(StringPiece(input, arraysize(input) - 1)); | |
492 | |
493 SpdyHeaderBlock expected_header_set; | |
494 expected_header_set[":path"] = "/sample/path"; | |
495 expected_header_set[":path2"] = "/sample/path/2"; | |
496 EXPECT_EQ(expected_header_set, header_set); | |
497 } | |
498 | |
499 TEST_P(HpackDecoder2Test, LiteralHeaderWithIndexingInvalidNameIndex) { | |
500 decoder_.ApplyHeaderTableSizeSetting(0); | |
501 | |
502 // Name is the last static index. Works. | |
503 EXPECT_TRUE(DecodeHeaderBlock(StringPiece("\x7d\x03ooo"))); | |
504 // Name is one beyond the last static index. Fails. | |
505 EXPECT_FALSE(DecodeHeaderBlock(StringPiece("\x7e\x03ooo"))); | |
506 } | |
507 | |
508 TEST_P(HpackDecoder2Test, LiteralHeaderNoIndexingInvalidNameIndex) { | |
509 // Name is the last static index. Works. | |
510 EXPECT_TRUE(DecodeHeaderBlock(StringPiece("\x0f\x2e\x03ooo"))); | |
511 // Name is one beyond the last static index. Fails. | |
512 EXPECT_FALSE(DecodeHeaderBlock(StringPiece("\x0f\x2f\x03ooo"))); | |
513 } | |
514 | |
515 TEST_P(HpackDecoder2Test, LiteralHeaderNeverIndexedInvalidNameIndex) { | |
516 // Name is the last static index. Works. | |
517 EXPECT_TRUE(DecodeHeaderBlock(StringPiece("\x1f\x2e\x03ooo"))); | |
518 // Name is one beyond the last static index. Fails. | |
519 EXPECT_FALSE(DecodeHeaderBlock(StringPiece("\x1f\x2f\x03ooo"))); | |
520 } | |
521 | |
522 TEST_P(HpackDecoder2Test, TruncatedIndex) { | |
523 // Indexed Header, varint for index requires multiple bytes, | |
524 // but only one provided. | |
525 EXPECT_FALSE(DecodeHeaderBlock(StringPiece("\xff", 1))); | |
526 } | |
527 | |
528 TEST_P(HpackDecoder2Test, TruncatedHuffmanLiteral) { | |
529 // Literal value, Huffman encoded, but with the last byte missing (i.e. | |
530 // drop the final ff shown below). | |
531 // | |
532 // 41 | == Literal indexed == | |
533 // | Indexed name (idx = 1) | |
534 // | :authority | |
535 // 8c | Literal value (len = 12) | |
536 // | Huffman encoded: | |
537 // f1e3 c2e5 f23a 6ba0 ab90 f4ff | .....:k..... | |
538 // | Decoded: | |
539 // | www.example.com | |
540 // | -> :authority: www.example.com | |
541 | |
542 string first = a2b_hex("418cf1e3c2e5f23a6ba0ab90f4ff"); | |
543 EXPECT_TRUE(DecodeHeaderBlock(first)); | |
544 first = a2b_hex("418cf1e3c2e5f23a6ba0ab90f4"); | |
545 EXPECT_FALSE(DecodeHeaderBlock(first)); | |
546 } | |
547 | |
548 TEST_P(HpackDecoder2Test, HuffmanEOSError) { | |
549 // Literal value, Huffman encoded, but with an additional ff byte at the end | |
550 // of the string, i.e. an EOS that is longer than permitted. | |
551 // | |
552 // 41 | == Literal indexed == | |
553 // | Indexed name (idx = 1) | |
554 // | :authority | |
555 // 8d | Literal value (len = 13) | |
556 // | Huffman encoded: | |
557 // f1e3 c2e5 f23a 6ba0 ab90 f4ff | .....:k..... | |
558 // | Decoded: | |
559 // | www.example.com | |
560 // | -> :authority: www.example.com | |
561 | |
562 string first = a2b_hex("418cf1e3c2e5f23a6ba0ab90f4ff"); | |
563 EXPECT_TRUE(DecodeHeaderBlock(first)); | |
564 first = a2b_hex("418df1e3c2e5f23a6ba0ab90f4ffff"); | |
565 EXPECT_FALSE(DecodeHeaderBlock(first)); | |
566 } | |
567 | |
568 // Round-tripping the header set from RFC 7541 C.3.1 should work. | |
569 // http://httpwg.org/specs/rfc7541.html#rfc.section.C.3.1 | |
570 TEST_P(HpackDecoder2Test, BasicC31) { | |
571 HpackEncoder encoder(ObtainHpackHuffmanTable()); | |
572 | |
573 SpdyHeaderBlock expected_header_set; | |
574 expected_header_set[":method"] = "GET"; | |
575 expected_header_set[":scheme"] = "http"; | |
576 expected_header_set[":path"] = "/"; | |
577 expected_header_set[":authority"] = "www.example.com"; | |
578 | |
579 string encoded_header_set; | |
580 EXPECT_TRUE( | |
581 encoder.EncodeHeaderSet(expected_header_set, &encoded_header_set)); | |
582 | |
583 EXPECT_TRUE(DecodeHeaderBlock(encoded_header_set)); | |
584 EXPECT_EQ(expected_header_set, decoded_block()); | |
585 } | |
586 | |
587 // RFC 7541, Section C.4: Request Examples with Huffman Coding | |
588 // http://httpwg.org/specs/rfc7541.html#rfc.section.C.4 | |
589 TEST_P(HpackDecoder2Test, SectionC4RequestHuffmanExamples) { | |
590 // TODO(jamessynge): Use net/http2/hpack/tools/hpack_example.h to parse the | |
591 // example directly, instead of having it as a comment. | |
592 // 82 | == Indexed - Add == | |
593 // | idx = 2 | |
594 // | -> :method: GET | |
595 // 86 | == Indexed - Add == | |
596 // | idx = 6 | |
597 // | -> :scheme: http | |
598 // 84 | == Indexed - Add == | |
599 // | idx = 4 | |
600 // | -> :path: / | |
601 // 41 | == Literal indexed == | |
602 // | Indexed name (idx = 1) | |
603 // | :authority | |
604 // 8c | Literal value (len = 12) | |
605 // | Huffman encoded: | |
606 // f1e3 c2e5 f23a 6ba0 ab90 f4ff | .....:k..... | |
607 // | Decoded: | |
608 // | www.example.com | |
609 // | -> :authority: www.example.com | |
610 string first = a2b_hex("828684418cf1e3c2e5f23a6ba0ab90f4ff"); | |
611 const SpdyHeaderBlock& first_header_set = DecodeBlockExpectingSuccess(first); | |
612 | |
613 EXPECT_THAT(first_header_set, | |
614 ElementsAre( | |
615 // clang-format off | |
616 Pair(":method", "GET"), | |
617 Pair(":scheme", "http"), | |
618 Pair(":path", "/"), | |
619 Pair(":authority", "www.example.com"))); | |
620 // clang-format on | |
621 | |
622 expectEntry(62, 57, ":authority", "www.example.com"); | |
623 EXPECT_EQ(57u, decoder_peer_.header_table()->size()); | |
624 | |
625 // 82 | == Indexed - Add == | |
626 // | idx = 2 | |
627 // | -> :method: GET | |
628 // 86 | == Indexed - Add == | |
629 // | idx = 6 | |
630 // | -> :scheme: http | |
631 // 84 | == Indexed - Add == | |
632 // | idx = 4 | |
633 // | -> :path: / | |
634 // be | == Indexed - Add == | |
635 // | idx = 62 | |
636 // | -> :authority: www.example.com | |
637 // 58 | == Literal indexed == | |
638 // | Indexed name (idx = 24) | |
639 // | cache-control | |
640 // 86 | Literal value (len = 8) | |
641 // | Huffman encoded: | |
642 // a8eb 1064 9cbf | ...d.. | |
643 // | Decoded: | |
644 // | no-cache | |
645 // | -> cache-control: no-cache | |
646 | |
647 string second = a2b_hex("828684be5886a8eb10649cbf"); | |
648 const SpdyHeaderBlock& second_header_set = | |
649 DecodeBlockExpectingSuccess(second); | |
650 | |
651 EXPECT_THAT(second_header_set, | |
652 ElementsAre( | |
653 // clang-format off | |
654 Pair(":method", "GET"), | |
655 Pair(":scheme", "http"), | |
656 Pair(":path", "/"), | |
657 Pair(":authority", "www.example.com"), | |
658 Pair("cache-control", "no-cache"))); | |
659 // clang-format on | |
660 | |
661 expectEntry(62, 53, "cache-control", "no-cache"); | |
662 expectEntry(63, 57, ":authority", "www.example.com"); | |
663 EXPECT_EQ(110u, decoder_peer_.header_table()->size()); | |
664 | |
665 // 82 | == Indexed - Add == | |
666 // | idx = 2 | |
667 // | -> :method: GET | |
668 // 87 | == Indexed - Add == | |
669 // | idx = 7 | |
670 // | -> :scheme: https | |
671 // 85 | == Indexed - Add == | |
672 // | idx = 5 | |
673 // | -> :path: /index.html | |
674 // bf | == Indexed - Add == | |
675 // | idx = 63 | |
676 // | -> :authority: www.example.com | |
677 // 40 | == Literal indexed == | |
678 // 88 | Literal name (len = 10) | |
679 // | Huffman encoded: | |
680 // 25a8 49e9 5ba9 7d7f | %.I.[.}. | |
681 // | Decoded: | |
682 // | custom-key | |
683 // 89 | Literal value (len = 12) | |
684 // | Huffman encoded: | |
685 // 25a8 49e9 5bb8 e8b4 bf | %.I.[.... | |
686 // | Decoded: | |
687 // | custom-value | |
688 // | -> custom-key: custom-value | |
689 string third = a2b_hex("828785bf408825a849e95ba97d7f8925a849e95bb8e8b4bf"); | |
690 const SpdyHeaderBlock& third_header_set = DecodeBlockExpectingSuccess(third); | |
691 | |
692 EXPECT_THAT( | |
693 third_header_set, | |
694 ElementsAre( | |
695 // clang-format off | |
696 Pair(":method", "GET"), | |
697 Pair(":scheme", "https"), | |
698 Pair(":path", "/index.html"), | |
699 Pair(":authority", "www.example.com"), | |
700 Pair("custom-key", "custom-value"))); | |
701 // clang-format on | |
702 | |
703 expectEntry(62, 54, "custom-key", "custom-value"); | |
704 expectEntry(63, 53, "cache-control", "no-cache"); | |
705 expectEntry(64, 57, ":authority", "www.example.com"); | |
706 EXPECT_EQ(164u, decoder_peer_.header_table()->size()); | |
707 } | |
708 | |
709 // RFC 7541, Section C.6: Response Examples with Huffman Coding | |
710 // http://httpwg.org/specs/rfc7541.html#rfc.section.C.6 | |
711 TEST_P(HpackDecoder2Test, SectionC6ResponseHuffmanExamples) { | |
712 decoder_.ApplyHeaderTableSizeSetting(256); | |
713 | |
714 // 48 | == Literal indexed == | |
715 // | Indexed name (idx = 8) | |
716 // | :status | |
717 // 82 | Literal value (len = 3) | |
718 // | Huffman encoded: | |
719 // 6402 | d. | |
720 // | Decoded: | |
721 // | 302 | |
722 // | -> :status: 302 | |
723 // 58 | == Literal indexed == | |
724 // | Indexed name (idx = 24) | |
725 // | cache-control | |
726 // 85 | Literal value (len = 7) | |
727 // | Huffman encoded: | |
728 // aec3 771a 4b | ..w.K | |
729 // | Decoded: | |
730 // | private | |
731 // | -> cache-control: private | |
732 // 61 | == Literal indexed == | |
733 // | Indexed name (idx = 33) | |
734 // | date | |
735 // 96 | Literal value (len = 29) | |
736 // | Huffman encoded: | |
737 // d07a be94 1054 d444 a820 0595 040b 8166 | .z...T.D. .....f | |
738 // e082 a62d 1bff | ...-.. | |
739 // | Decoded: | |
740 // | Mon, 21 Oct 2013 20:13:21 | |
741 // | GMT | |
742 // | -> date: Mon, 21 Oct 2013 | |
743 // | 20:13:21 GMT | |
744 // 6e | == Literal indexed == | |
745 // | Indexed name (idx = 46) | |
746 // | location | |
747 // 91 | Literal value (len = 23) | |
748 // | Huffman encoded: | |
749 // 9d29 ad17 1863 c78f 0b97 c8e9 ae82 ae43 | .)...c.........C | |
750 // d3 | . | |
751 // | Decoded: | |
752 // | https://www.example.com | |
753 // | -> location: https://www.e | |
754 // | xample.com | |
755 | |
756 string first = a2b_hex( | |
757 "488264025885aec3771a4b6196d07abe" | |
758 "941054d444a8200595040b8166e082a6" | |
759 "2d1bff6e919d29ad171863c78f0b97c8" | |
760 "e9ae82ae43d3"); | |
761 const SpdyHeaderBlock& first_header_set = DecodeBlockExpectingSuccess(first); | |
762 | |
763 EXPECT_THAT(first_header_set, | |
764 ElementsAre( | |
765 // clang-format off | |
766 Pair(":status", "302"), | |
767 Pair("cache-control", "private"), | |
768 Pair("date", "Mon, 21 Oct 2013 20:13:21 GMT"), | |
769 Pair("location", "https://www.example.com"))); | |
770 // clang-format on | |
771 | |
772 expectEntry(62, 63, "location", "https://www.example.com"); | |
773 expectEntry(63, 65, "date", "Mon, 21 Oct 2013 20:13:21 GMT"); | |
774 expectEntry(64, 52, "cache-control", "private"); | |
775 expectEntry(65, 42, ":status", "302"); | |
776 EXPECT_EQ(222u, decoder_peer_.header_table()->size()); | |
777 | |
778 // 48 | == Literal indexed == | |
779 // | Indexed name (idx = 8) | |
780 // | :status | |
781 // 83 | Literal value (len = 3) | |
782 // | Huffman encoded: | |
783 // 640e ff | d.. | |
784 // | Decoded: | |
785 // | 307 | |
786 // | - evict: :status: 302 | |
787 // | -> :status: 307 | |
788 // c1 | == Indexed - Add == | |
789 // | idx = 65 | |
790 // | -> cache-control: private | |
791 // c0 | == Indexed - Add == | |
792 // | idx = 64 | |
793 // | -> date: Mon, 21 Oct 2013 | |
794 // | 20:13:21 GMT | |
795 // bf | == Indexed - Add == | |
796 // | idx = 63 | |
797 // | -> location: | |
798 // | https://www.example.com | |
799 string second = a2b_hex("4883640effc1c0bf"); | |
800 const SpdyHeaderBlock& second_header_set = | |
801 DecodeBlockExpectingSuccess(second); | |
802 | |
803 EXPECT_THAT(second_header_set, | |
804 ElementsAre( | |
805 // clang-format off | |
806 Pair(":status", "307"), | |
807 Pair("cache-control", "private"), | |
808 Pair("date", "Mon, 21 Oct 2013 20:13:21 GMT"), | |
809 Pair("location", "https://www.example.com"))); | |
810 // clang-format on | |
811 | |
812 expectEntry(62, 42, ":status", "307"); | |
813 expectEntry(63, 63, "location", "https://www.example.com"); | |
814 expectEntry(64, 65, "date", "Mon, 21 Oct 2013 20:13:21 GMT"); | |
815 expectEntry(65, 52, "cache-control", "private"); | |
816 EXPECT_EQ(222u, decoder_peer_.header_table()->size()); | |
817 | |
818 // 88 | == Indexed - Add == | |
819 // | idx = 8 | |
820 // | -> :status: 200 | |
821 // c1 | == Indexed - Add == | |
822 // | idx = 65 | |
823 // | -> cache-control: private | |
824 // 61 | == Literal indexed == | |
825 // | Indexed name (idx = 33) | |
826 // | date | |
827 // 96 | Literal value (len = 22) | |
828 // | Huffman encoded: | |
829 // d07a be94 1054 d444 a820 0595 040b 8166 | .z...T.D. .....f | |
830 // e084 a62d 1bff | ...-.. | |
831 // | Decoded: | |
832 // | Mon, 21 Oct 2013 20:13:22 | |
833 // | GMT | |
834 // | - evict: cache-control: | |
835 // | private | |
836 // | -> date: Mon, 21 Oct 2013 | |
837 // | 20:13:22 GMT | |
838 // c0 | == Indexed - Add == | |
839 // | idx = 64 | |
840 // | -> location: | |
841 // | https://www.example.com | |
842 // 5a | == Literal indexed == | |
843 // | Indexed name (idx = 26) | |
844 // | content-encoding | |
845 // 83 | Literal value (len = 3) | |
846 // | Huffman encoded: | |
847 // 9bd9 ab | ... | |
848 // | Decoded: | |
849 // | gzip | |
850 // | - evict: date: Mon, 21 Oct | |
851 // | 2013 20:13:21 GMT | |
852 // | -> content-encoding: gzip | |
853 // 77 | == Literal indexed == | |
854 // | Indexed name (idx = 55) | |
855 // | set-cookie | |
856 // ad | Literal value (len = 45) | |
857 // | Huffman encoded: | |
858 // 94e7 821d d7f2 e6c7 b335 dfdf cd5b 3960 | .........5...[9` | |
859 // d5af 2708 7f36 72c1 ab27 0fb5 291f 9587 | ..'..6r..'..)... | |
860 // 3160 65c0 03ed 4ee5 b106 3d50 07 | 1`e...N...=P. | |
861 // | Decoded: | |
862 // | foo=ASDJKHQKBZXOQWEOPIUAXQ | |
863 // | WEOIU; max-age=3600; versi | |
864 // | on=1 | |
865 // | - evict: location: | |
866 // | https://www.example.com | |
867 // | - evict: :status: 307 | |
868 // | -> set-cookie: foo=ASDJKHQ | |
869 // | KBZXOQWEOPIUAXQWEOIU; | |
870 // | max-age=3600; version=1 | |
871 string third = a2b_hex( | |
872 "88c16196d07abe941054d444a8200595" | |
873 "040b8166e084a62d1bffc05a839bd9ab" | |
874 "77ad94e7821dd7f2e6c7b335dfdfcd5b" | |
875 "3960d5af27087f3672c1ab270fb5291f" | |
876 "9587316065c003ed4ee5b1063d5007"); | |
877 const SpdyHeaderBlock& third_header_set = DecodeBlockExpectingSuccess(third); | |
878 | |
879 EXPECT_THAT(third_header_set, | |
880 ElementsAre( | |
881 // clang-format off | |
882 Pair(":status", "200"), | |
883 Pair("cache-control", "private"), | |
884 Pair("date", "Mon, 21 Oct 2013 20:13:22 GMT"), | |
885 Pair("location", "https://www.example.com"), | |
886 Pair("content-encoding", "gzip"), | |
887 Pair("set-cookie", "foo=ASDJKHQKBZXOQWEOPIUAXQWEOIU;" | |
888 " max-age=3600; version=1"))); | |
889 // clang-format on | |
890 | |
891 expectEntry(62, 98, "set-cookie", | |
892 "foo=ASDJKHQKBZXOQWEOPIUAXQWEOIU;" | |
893 " max-age=3600; version=1"); | |
894 expectEntry(63, 52, "content-encoding", "gzip"); | |
895 expectEntry(64, 65, "date", "Mon, 21 Oct 2013 20:13:22 GMT"); | |
896 EXPECT_EQ(215u, decoder_peer_.header_table()->size()); | |
897 } | |
898 | |
899 // Regression test: Found that entries with dynamic indexed names and literal | |
900 // values caused "use after free" MSAN failures if the name was evicted as it | |
901 // was being re-used. | |
902 TEST_P(HpackDecoder2Test, ReuseNameOfEvictedEntry) { | |
903 // Each entry is measured as 32 bytes plus the sum of the lengths of the name | |
904 // and the value. Set the size big enough for at most one entry, and a fairly | |
905 // small one at that (31 ASCII characters). | |
906 decoder_.ApplyHeaderTableSizeSetting(63); | |
907 | |
908 HpackBlockBuilder hbb; | |
909 | |
910 const StringPiece name("some-name"); | |
911 const StringPiece value1("some-value"); | |
912 const StringPiece value2("another-value"); | |
913 const StringPiece value3("yet-another-value"); | |
914 | |
915 // Add an entry that will become the first in the dynamic table, entry 62. | |
916 hbb.AppendLiteralNameAndValue(HpackEntryType::kIndexedLiteralHeader, false, | |
917 name, false, value1); | |
918 | |
919 // Confirm that entry has been added by re-using it. | |
920 hbb.AppendIndexedHeader(62); | |
921 | |
922 // Add another entry referring to the name of the first. This will evict the | |
923 // first. | |
924 hbb.AppendNameIndexAndLiteralValue(HpackEntryType::kIndexedLiteralHeader, 62, | |
925 false, value2); | |
926 | |
927 // Confirm that entry has been added by re-using it. | |
928 hbb.AppendIndexedHeader(62); | |
929 | |
930 // Add another entry referring to the name of the second. This will evict the | |
931 // second. | |
932 hbb.AppendNameIndexAndLiteralValue(HpackEntryType::kIndexedLiteralHeader, 62, | |
933 false, value3); | |
934 | |
935 // Confirm that entry has been added by re-using it. | |
936 hbb.AppendIndexedHeader(62); | |
937 | |
938 EXPECT_TRUE(DecodeHeaderBlock(hbb.buffer())); | |
939 | |
940 SpdyHeaderBlock expected_header_set; | |
941 expected_header_set.AppendValueOrAddHeader(name, value1); | |
942 expected_header_set.AppendValueOrAddHeader(name, value1); | |
943 expected_header_set.AppendValueOrAddHeader(name, value2); | |
944 expected_header_set.AppendValueOrAddHeader(name, value2); | |
945 expected_header_set.AppendValueOrAddHeader(name, value3); | |
946 expected_header_set.AppendValueOrAddHeader(name, value3); | |
947 | |
948 // SpdyHeaderBlock stores these 6 strings as '\0' separated values. | |
949 // Make sure that is what happened. | |
950 string joined_values = expected_header_set[name].as_string(); | |
951 EXPECT_EQ(joined_values.size(), | |
952 2 * value1.size() + 2 * value2.size() + 2 * value3.size() + 5); | |
953 | |
954 EXPECT_EQ(expected_header_set, decoded_block()); | |
955 } | |
956 | |
957 } // namespace | |
958 } // namespace test | |
959 } // namespace net | |
OLD | NEW |