QUICHE team | fd50a40 | 2018-12-07 22:54:05 -0500 | [diff] [blame] | 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/third_party/quiche/src/http2/decoder/payload_decoders/headers_payload_decoder.h" |
| 6 | |
| 7 | #include <stddef.h> |
| 8 | |
bnc | 4790400 | 2019-08-16 11:49:48 -0700 | [diff] [blame] | 9 | #include <string> |
| 10 | |
QUICHE team | fd50a40 | 2018-12-07 22:54:05 -0500 | [diff] [blame] | 11 | #include "testing/gtest/include/gtest/gtest.h" |
| 12 | #include "net/third_party/quiche/src/http2/decoder/http2_frame_decoder_listener.h" |
| 13 | #include "net/third_party/quiche/src/http2/decoder/payload_decoders/payload_decoder_base_test_util.h" |
| 14 | #include "net/third_party/quiche/src/http2/http2_constants.h" |
| 15 | #include "net/third_party/quiche/src/http2/http2_structures_test_util.h" |
QUICHE team | 61940b4 | 2019-03-07 23:32:27 -0500 | [diff] [blame] | 16 | #include "net/third_party/quiche/src/http2/platform/api/http2_logging.h" |
QUICHE team | fd50a40 | 2018-12-07 22:54:05 -0500 | [diff] [blame] | 17 | #include "net/third_party/quiche/src/http2/test_tools/frame_parts.h" |
| 18 | #include "net/third_party/quiche/src/http2/test_tools/frame_parts_collector.h" |
| 19 | #include "net/third_party/quiche/src/http2/test_tools/http2_random.h" |
| 20 | #include "net/third_party/quiche/src/http2/tools/http2_frame_builder.h" |
| 21 | #include "net/third_party/quiche/src/http2/tools/random_decoder_test.h" |
| 22 | |
| 23 | namespace http2 { |
| 24 | namespace test { |
| 25 | |
| 26 | class HeadersPayloadDecoderPeer { |
| 27 | public: |
| 28 | static constexpr Http2FrameType FrameType() { |
| 29 | return Http2FrameType::HEADERS; |
| 30 | } |
| 31 | |
| 32 | // Returns the mask of flags that affect the decoding of the payload (i.e. |
| 33 | // flags that that indicate the presence of certain fields or padding). |
| 34 | static constexpr uint8_t FlagsAffectingPayloadDecoding() { |
| 35 | return Http2FrameFlag::PADDED | Http2FrameFlag::PRIORITY; |
| 36 | } |
| 37 | }; |
| 38 | |
| 39 | namespace { |
| 40 | |
| 41 | // Listener handles all On* methods that are expected to be called. If any other |
| 42 | // On* methods of Http2FrameDecoderListener is called then the test fails; this |
| 43 | // is achieved by way of FailingHttp2FrameDecoderListener, the base class of |
| 44 | // FramePartsCollector. |
| 45 | // These On* methods make use of StartFrame, EndFrame, etc. of the base class |
| 46 | // to create and access to FrameParts instance(s) that will record the details. |
| 47 | // After decoding, the test validation code can access the FramePart instance(s) |
| 48 | // via the public methods of FramePartsCollector. |
| 49 | struct Listener : public FramePartsCollector { |
| 50 | void OnHeadersStart(const Http2FrameHeader& header) override { |
QUICHE team | 61940b4 | 2019-03-07 23:32:27 -0500 | [diff] [blame] | 51 | HTTP2_VLOG(1) << "OnHeadersStart: " << header; |
QUICHE team | fd50a40 | 2018-12-07 22:54:05 -0500 | [diff] [blame] | 52 | StartFrame(header)->OnHeadersStart(header); |
| 53 | } |
| 54 | |
| 55 | void OnHeadersPriority(const Http2PriorityFields& priority) override { |
QUICHE team | 61940b4 | 2019-03-07 23:32:27 -0500 | [diff] [blame] | 56 | HTTP2_VLOG(1) << "OnHeadersPriority: " << priority; |
QUICHE team | fd50a40 | 2018-12-07 22:54:05 -0500 | [diff] [blame] | 57 | CurrentFrame()->OnHeadersPriority(priority); |
| 58 | } |
| 59 | |
| 60 | void OnHpackFragment(const char* data, size_t len) override { |
QUICHE team | 61940b4 | 2019-03-07 23:32:27 -0500 | [diff] [blame] | 61 | HTTP2_VLOG(1) << "OnHpackFragment: len=" << len; |
QUICHE team | fd50a40 | 2018-12-07 22:54:05 -0500 | [diff] [blame] | 62 | CurrentFrame()->OnHpackFragment(data, len); |
| 63 | } |
| 64 | |
| 65 | void OnHeadersEnd() override { |
QUICHE team | 61940b4 | 2019-03-07 23:32:27 -0500 | [diff] [blame] | 66 | HTTP2_VLOG(1) << "OnHeadersEnd"; |
QUICHE team | fd50a40 | 2018-12-07 22:54:05 -0500 | [diff] [blame] | 67 | EndFrame()->OnHeadersEnd(); |
| 68 | } |
| 69 | |
| 70 | void OnPadLength(size_t pad_length) override { |
QUICHE team | 61940b4 | 2019-03-07 23:32:27 -0500 | [diff] [blame] | 71 | HTTP2_VLOG(1) << "OnPadLength: " << pad_length; |
QUICHE team | fd50a40 | 2018-12-07 22:54:05 -0500 | [diff] [blame] | 72 | CurrentFrame()->OnPadLength(pad_length); |
| 73 | } |
| 74 | |
| 75 | void OnPadding(const char* padding, size_t skipped_length) override { |
QUICHE team | 61940b4 | 2019-03-07 23:32:27 -0500 | [diff] [blame] | 76 | HTTP2_VLOG(1) << "OnPadding: " << skipped_length; |
QUICHE team | fd50a40 | 2018-12-07 22:54:05 -0500 | [diff] [blame] | 77 | CurrentFrame()->OnPadding(padding, skipped_length); |
| 78 | } |
| 79 | |
| 80 | void OnPaddingTooLong(const Http2FrameHeader& header, |
| 81 | size_t missing_length) override { |
QUICHE team | 61940b4 | 2019-03-07 23:32:27 -0500 | [diff] [blame] | 82 | HTTP2_VLOG(1) << "OnPaddingTooLong: " << header |
| 83 | << "; missing_length: " << missing_length; |
QUICHE team | fd50a40 | 2018-12-07 22:54:05 -0500 | [diff] [blame] | 84 | FrameError(header)->OnPaddingTooLong(header, missing_length); |
| 85 | } |
| 86 | |
| 87 | void OnFrameSizeError(const Http2FrameHeader& header) override { |
QUICHE team | 61940b4 | 2019-03-07 23:32:27 -0500 | [diff] [blame] | 88 | HTTP2_VLOG(1) << "OnFrameSizeError: " << header; |
QUICHE team | fd50a40 | 2018-12-07 22:54:05 -0500 | [diff] [blame] | 89 | FrameError(header)->OnFrameSizeError(header); |
| 90 | } |
| 91 | }; |
| 92 | |
| 93 | class HeadersPayloadDecoderTest |
| 94 | : public AbstractPaddablePayloadDecoderTest<HeadersPayloadDecoder, |
| 95 | HeadersPayloadDecoderPeer, |
| 96 | Listener> {}; |
| 97 | |
QUICHE team | 61940b4 | 2019-03-07 23:32:27 -0500 | [diff] [blame] | 98 | INSTANTIATE_TEST_SUITE_P(VariousPadLengths, |
| 99 | HeadersPayloadDecoderTest, |
QUICHE team | 900ba16 | 2019-01-30 21:07:06 -0500 | [diff] [blame] | 100 | ::testing::Values(0, 1, 2, 3, 4, 254, 255, 256)); |
QUICHE team | fd50a40 | 2018-12-07 22:54:05 -0500 | [diff] [blame] | 101 | |
| 102 | // Decode various sizes of (fake) HPACK payload, both with and without the |
| 103 | // PRIORITY flag set. |
| 104 | TEST_P(HeadersPayloadDecoderTest, VariousHpackPayloadSizes) { |
| 105 | for (size_t hpack_size : {0, 1, 2, 3, 255, 256, 1024}) { |
QUICHE team | 61940b4 | 2019-03-07 23:32:27 -0500 | [diff] [blame] | 106 | HTTP2_LOG(INFO) << "########### hpack_size = " << hpack_size |
| 107 | << " ###########"; |
QUICHE team | fd50a40 | 2018-12-07 22:54:05 -0500 | [diff] [blame] | 108 | Http2PriorityFields priority(RandStreamId(), 1 + Random().Rand8(), |
| 109 | Random().OneIn(2)); |
| 110 | |
| 111 | for (bool has_priority : {false, true}) { |
| 112 | Reset(); |
| 113 | ASSERT_EQ(IsPadded() ? 1u : 0u, frame_builder_.size()); |
| 114 | uint8_t flags = RandFlags(); |
| 115 | if (has_priority) { |
| 116 | flags |= Http2FrameFlag::PRIORITY; |
| 117 | frame_builder_.Append(priority); |
| 118 | } |
| 119 | |
bnc | 4790400 | 2019-08-16 11:49:48 -0700 | [diff] [blame] | 120 | std::string hpack_payload = Random().RandString(hpack_size); |
QUICHE team | fd50a40 | 2018-12-07 22:54:05 -0500 | [diff] [blame] | 121 | frame_builder_.Append(hpack_payload); |
| 122 | |
| 123 | MaybeAppendTrailingPadding(); |
| 124 | Http2FrameHeader frame_header(frame_builder_.size(), |
| 125 | Http2FrameType::HEADERS, flags, |
| 126 | RandStreamId()); |
| 127 | set_frame_header(frame_header); |
| 128 | ScrubFlagsOfHeader(&frame_header); |
| 129 | FrameParts expected(frame_header, hpack_payload, total_pad_length_); |
| 130 | if (has_priority) { |
| 131 | expected.SetOptPriority(priority); |
| 132 | } |
| 133 | EXPECT_TRUE(DecodePayloadAndValidateSeveralWays(frame_builder_.buffer(), |
| 134 | expected)); |
| 135 | } |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | // Confirm we get an error if the PRIORITY flag is set but the payload is |
| 140 | // not long enough, regardless of the amount of (valid) padding. |
| 141 | TEST_P(HeadersPayloadDecoderTest, Truncated) { |
| 142 | auto approve_size = [](size_t size) { |
| 143 | return size != Http2PriorityFields::EncodedSize(); |
| 144 | }; |
| 145 | Http2FrameBuilder fb; |
| 146 | fb.Append(Http2PriorityFields(RandStreamId(), 1 + Random().Rand8(), |
| 147 | Random().OneIn(2))); |
| 148 | EXPECT_TRUE(VerifyDetectsMultipleFrameSizeErrors( |
| 149 | Http2FrameFlag::PRIORITY, fb.buffer(), approve_size, total_pad_length_)); |
| 150 | } |
| 151 | |
| 152 | // Confirm we get an error if the PADDED flag is set but the payload is not |
| 153 | // long enough to hold even the Pad Length amount of padding. |
| 154 | TEST_P(HeadersPayloadDecoderTest, PaddingTooLong) { |
| 155 | EXPECT_TRUE(VerifyDetectsPaddingTooLong()); |
| 156 | } |
| 157 | |
| 158 | } // namespace |
| 159 | } // namespace test |
| 160 | } // namespace http2 |