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 "quiche/http2/decoder/payload_decoders/headers_payload_decoder.h"
6
7 #include <stddef.h>
8
9 #include <string>
10
11 #include "quiche/http2/decoder/http2_frame_decoder_listener.h"
12 #include "quiche/http2/http2_constants.h"
13 #include "quiche/http2/test_tools/frame_parts.h"
14 #include "quiche/http2/test_tools/frame_parts_collector.h"
15 #include "quiche/http2/test_tools/http2_frame_builder.h"
16 #include "quiche/http2/test_tools/http2_random.h"
17 #include "quiche/http2/test_tools/http2_structures_test_util.h"
18 #include "quiche/http2/test_tools/payload_decoder_base_test_util.h"
19 #include "quiche/http2/test_tools/random_decoder_test_base.h"
20 #include "quiche/common/platform/api/quiche_logging.h"
21 #include "quiche/common/platform/api/quiche_test.h"
22
23 namespace http2 {
24 namespace test {
25
26 class HeadersPayloadDecoderPeer {
27 public:
FrameType()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).
FlagsAffectingPayloadDecoding()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 {
OnHeadersStarthttp2::test::__anonf74a9ef70111::Listener50 void OnHeadersStart(const Http2FrameHeader& header) override {
51 QUICHE_VLOG(1) << "OnHeadersStart: " << header;
52 StartFrame(header)->OnHeadersStart(header);
53 }
54
OnHeadersPriorityhttp2::test::__anonf74a9ef70111::Listener55 void OnHeadersPriority(const Http2PriorityFields& priority) override {
56 QUICHE_VLOG(1) << "OnHeadersPriority: " << priority;
57 CurrentFrame()->OnHeadersPriority(priority);
58 }
59
OnHpackFragmenthttp2::test::__anonf74a9ef70111::Listener60 void OnHpackFragment(const char* data, size_t len) override {
61 QUICHE_VLOG(1) << "OnHpackFragment: len=" << len;
62 CurrentFrame()->OnHpackFragment(data, len);
63 }
64
OnHeadersEndhttp2::test::__anonf74a9ef70111::Listener65 void OnHeadersEnd() override {
66 QUICHE_VLOG(1) << "OnHeadersEnd";
67 EndFrame()->OnHeadersEnd();
68 }
69
OnPadLengthhttp2::test::__anonf74a9ef70111::Listener70 void OnPadLength(size_t pad_length) override {
71 QUICHE_VLOG(1) << "OnPadLength: " << pad_length;
72 CurrentFrame()->OnPadLength(pad_length);
73 }
74
OnPaddinghttp2::test::__anonf74a9ef70111::Listener75 void OnPadding(const char* padding, size_t skipped_length) override {
76 QUICHE_VLOG(1) << "OnPadding: " << skipped_length;
77 CurrentFrame()->OnPadding(padding, skipped_length);
78 }
79
OnPaddingTooLonghttp2::test::__anonf74a9ef70111::Listener80 void OnPaddingTooLong(const Http2FrameHeader& header,
81 size_t missing_length) override {
82 QUICHE_VLOG(1) << "OnPaddingTooLong: " << header
83 << "; missing_length: " << missing_length;
84 FrameError(header)->OnPaddingTooLong(header, missing_length);
85 }
86
OnFrameSizeErrorhttp2::test::__anonf74a9ef70111::Listener87 void OnFrameSizeError(const Http2FrameHeader& header) override {
88 QUICHE_VLOG(1) << "OnFrameSizeError: " << header;
89 FrameError(header)->OnFrameSizeError(header);
90 }
91 };
92
93 class HeadersPayloadDecoderTest
94 : public AbstractPaddablePayloadDecoderTest<
95 HeadersPayloadDecoder, HeadersPayloadDecoderPeer, Listener> {};
96
97 INSTANTIATE_TEST_SUITE_P(VariousPadLengths, HeadersPayloadDecoderTest,
98 ::testing::Values(0, 1, 2, 3, 4, 254, 255, 256));
99
100 // Decode various sizes of (fake) HPACK payload, both with and without the
101 // PRIORITY flag set.
TEST_P(HeadersPayloadDecoderTest,VariousHpackPayloadSizes)102 TEST_P(HeadersPayloadDecoderTest, VariousHpackPayloadSizes) {
103 for (size_t hpack_size : {0, 1, 2, 3, 255, 256, 1024}) {
104 QUICHE_LOG(INFO) << "########### hpack_size = " << hpack_size
105 << " ###########";
106 Http2PriorityFields priority(RandStreamId(), 1 + Random().Rand8(),
107 Random().OneIn(2));
108
109 for (bool has_priority : {false, true}) {
110 Reset();
111 ASSERT_EQ(IsPadded() ? 1u : 0u, frame_builder_.size());
112 uint8_t flags = RandFlags();
113 if (has_priority) {
114 flags |= Http2FrameFlag::PRIORITY;
115 frame_builder_.Append(priority);
116 }
117
118 std::string hpack_payload = Random().RandString(hpack_size);
119 frame_builder_.Append(hpack_payload);
120
121 MaybeAppendTrailingPadding();
122 Http2FrameHeader frame_header(frame_builder_.size(),
123 Http2FrameType::HEADERS, flags,
124 RandStreamId());
125 set_frame_header(frame_header);
126 ScrubFlagsOfHeader(&frame_header);
127 FrameParts expected(frame_header, hpack_payload, total_pad_length_);
128 if (has_priority) {
129 expected.SetOptPriority(priority);
130 }
131 EXPECT_TRUE(DecodePayloadAndValidateSeveralWays(frame_builder_.buffer(),
132 expected));
133 }
134 }
135 }
136
137 // Confirm we get an error if the PRIORITY flag is set but the payload is
138 // not long enough, regardless of the amount of (valid) padding.
TEST_P(HeadersPayloadDecoderTest,Truncated)139 TEST_P(HeadersPayloadDecoderTest, Truncated) {
140 auto approve_size = [](size_t size) {
141 return size != Http2PriorityFields::EncodedSize();
142 };
143 Http2FrameBuilder fb;
144 fb.Append(Http2PriorityFields(RandStreamId(), 1 + Random().Rand8(),
145 Random().OneIn(2)));
146 EXPECT_TRUE(VerifyDetectsMultipleFrameSizeErrors(
147 Http2FrameFlag::PRIORITY, fb.buffer(), approve_size, total_pad_length_));
148 }
149
150 // Confirm we get an error if the PADDED flag is set but the payload is not
151 // long enough to hold even the Pad Length amount of padding.
TEST_P(HeadersPayloadDecoderTest,PaddingTooLong)152 TEST_P(HeadersPayloadDecoderTest, PaddingTooLong) {
153 EXPECT_TRUE(VerifyDetectsPaddingTooLong());
154 }
155
156 } // namespace
157 } // namespace test
158 } // namespace http2
159