blob: 4e0f2e307bbde709c7a6b548637f89877e78433d [file] [log] [blame]
QUICHE teamfd50a402018-12-07 22:54:05 -05001// 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
bnc47904002019-08-16 11:49:48 -07009#include <string>
10
QUICHE teamfd50a402018-12-07 22:54:05 -050011#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 team61940b42019-03-07 23:32:27 -050016#include "net/third_party/quiche/src/http2/platform/api/http2_logging.h"
QUICHE teamfd50a402018-12-07 22:54:05 -050017#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
23namespace http2 {
24namespace test {
25
26class 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
39namespace {
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.
49struct Listener : public FramePartsCollector {
50 void OnHeadersStart(const Http2FrameHeader& header) override {
QUICHE team61940b42019-03-07 23:32:27 -050051 HTTP2_VLOG(1) << "OnHeadersStart: " << header;
QUICHE teamfd50a402018-12-07 22:54:05 -050052 StartFrame(header)->OnHeadersStart(header);
53 }
54
55 void OnHeadersPriority(const Http2PriorityFields& priority) override {
QUICHE team61940b42019-03-07 23:32:27 -050056 HTTP2_VLOG(1) << "OnHeadersPriority: " << priority;
QUICHE teamfd50a402018-12-07 22:54:05 -050057 CurrentFrame()->OnHeadersPriority(priority);
58 }
59
60 void OnHpackFragment(const char* data, size_t len) override {
QUICHE team61940b42019-03-07 23:32:27 -050061 HTTP2_VLOG(1) << "OnHpackFragment: len=" << len;
QUICHE teamfd50a402018-12-07 22:54:05 -050062 CurrentFrame()->OnHpackFragment(data, len);
63 }
64
65 void OnHeadersEnd() override {
QUICHE team61940b42019-03-07 23:32:27 -050066 HTTP2_VLOG(1) << "OnHeadersEnd";
QUICHE teamfd50a402018-12-07 22:54:05 -050067 EndFrame()->OnHeadersEnd();
68 }
69
70 void OnPadLength(size_t pad_length) override {
QUICHE team61940b42019-03-07 23:32:27 -050071 HTTP2_VLOG(1) << "OnPadLength: " << pad_length;
QUICHE teamfd50a402018-12-07 22:54:05 -050072 CurrentFrame()->OnPadLength(pad_length);
73 }
74
75 void OnPadding(const char* padding, size_t skipped_length) override {
QUICHE team61940b42019-03-07 23:32:27 -050076 HTTP2_VLOG(1) << "OnPadding: " << skipped_length;
QUICHE teamfd50a402018-12-07 22:54:05 -050077 CurrentFrame()->OnPadding(padding, skipped_length);
78 }
79
80 void OnPaddingTooLong(const Http2FrameHeader& header,
81 size_t missing_length) override {
QUICHE team61940b42019-03-07 23:32:27 -050082 HTTP2_VLOG(1) << "OnPaddingTooLong: " << header
83 << "; missing_length: " << missing_length;
QUICHE teamfd50a402018-12-07 22:54:05 -050084 FrameError(header)->OnPaddingTooLong(header, missing_length);
85 }
86
87 void OnFrameSizeError(const Http2FrameHeader& header) override {
QUICHE team61940b42019-03-07 23:32:27 -050088 HTTP2_VLOG(1) << "OnFrameSizeError: " << header;
QUICHE teamfd50a402018-12-07 22:54:05 -050089 FrameError(header)->OnFrameSizeError(header);
90 }
91};
92
93class HeadersPayloadDecoderTest
94 : public AbstractPaddablePayloadDecoderTest<HeadersPayloadDecoder,
95 HeadersPayloadDecoderPeer,
96 Listener> {};
97
QUICHE team61940b42019-03-07 23:32:27 -050098INSTANTIATE_TEST_SUITE_P(VariousPadLengths,
99 HeadersPayloadDecoderTest,
QUICHE team900ba162019-01-30 21:07:06 -0500100 ::testing::Values(0, 1, 2, 3, 4, 254, 255, 256));
QUICHE teamfd50a402018-12-07 22:54:05 -0500101
102// Decode various sizes of (fake) HPACK payload, both with and without the
103// PRIORITY flag set.
104TEST_P(HeadersPayloadDecoderTest, VariousHpackPayloadSizes) {
105 for (size_t hpack_size : {0, 1, 2, 3, 255, 256, 1024}) {
QUICHE team61940b42019-03-07 23:32:27 -0500106 HTTP2_LOG(INFO) << "########### hpack_size = " << hpack_size
107 << " ###########";
QUICHE teamfd50a402018-12-07 22:54:05 -0500108 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
bnc47904002019-08-16 11:49:48 -0700120 std::string hpack_payload = Random().RandString(hpack_size);
QUICHE teamfd50a402018-12-07 22:54:05 -0500121 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.
141TEST_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.
154TEST_P(HeadersPayloadDecoderTest, PaddingTooLong) {
155 EXPECT_TRUE(VerifyDetectsPaddingTooLong());
156}
157
158} // namespace
159} // namespace test
160} // namespace http2