blob: b2d96fb2d71f633b8b8cf1ab3f2c347c860280e3 [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/goaway_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 GoAwayPayloadDecoderPeer {
27 public:
28 static constexpr Http2FrameType FrameType() { return Http2FrameType::GOAWAY; }
29
30 // Returns the mask of flags that affect the decoding of the payload (i.e.
31 // flags that that indicate the presence of certain fields or padding).
32 static constexpr uint8_t FlagsAffectingPayloadDecoding() { return 0; }
33};
34
35namespace {
36
37struct Listener : public FramePartsCollector {
38 void OnGoAwayStart(const Http2FrameHeader& header,
39 const Http2GoAwayFields& goaway) override {
QUICHE team61940b42019-03-07 23:32:27 -050040 HTTP2_VLOG(1) << "OnGoAwayStart header: " << header
41 << "; goaway: " << goaway;
QUICHE teamfd50a402018-12-07 22:54:05 -050042 StartFrame(header)->OnGoAwayStart(header, goaway);
43 }
44
45 void OnGoAwayOpaqueData(const char* data, size_t len) override {
QUICHE team61940b42019-03-07 23:32:27 -050046 HTTP2_VLOG(1) << "OnGoAwayOpaqueData: len=" << len;
QUICHE teamfd50a402018-12-07 22:54:05 -050047 CurrentFrame()->OnGoAwayOpaqueData(data, len);
48 }
49
50 void OnGoAwayEnd() override {
QUICHE team61940b42019-03-07 23:32:27 -050051 HTTP2_VLOG(1) << "OnGoAwayEnd";
QUICHE teamfd50a402018-12-07 22:54:05 -050052 EndFrame()->OnGoAwayEnd();
53 }
54
55 void OnFrameSizeError(const Http2FrameHeader& header) override {
QUICHE team61940b42019-03-07 23:32:27 -050056 HTTP2_VLOG(1) << "OnFrameSizeError: " << header;
QUICHE teamfd50a402018-12-07 22:54:05 -050057 FrameError(header)->OnFrameSizeError(header);
58 }
59};
60
61class GoAwayPayloadDecoderTest
62 : public AbstractPayloadDecoderTest<GoAwayPayloadDecoder,
63 GoAwayPayloadDecoderPeer,
64 Listener> {};
65
66// Confirm we get an error if the payload is not long enough to hold
67// Http2GoAwayFields.
68TEST_F(GoAwayPayloadDecoderTest, Truncated) {
69 auto approve_size = [](size_t size) {
70 return size != Http2GoAwayFields::EncodedSize();
71 };
72 Http2FrameBuilder fb;
73 fb.Append(Http2GoAwayFields(123, Http2ErrorCode::ENHANCE_YOUR_CALM));
74 EXPECT_TRUE(VerifyDetectsFrameSizeError(0, fb.buffer(), approve_size));
75}
76
77class GoAwayOpaqueDataLengthTests
78 : public GoAwayPayloadDecoderTest,
79 public ::testing::WithParamInterface<uint32_t> {
80 protected:
81 GoAwayOpaqueDataLengthTests() : length_(GetParam()) {
QUICHE team61940b42019-03-07 23:32:27 -050082 HTTP2_VLOG(1) << "################ length_=" << length_
83 << " ################";
QUICHE teamfd50a402018-12-07 22:54:05 -050084 }
85
86 const uint32_t length_;
87};
88
QUICHE team61940b42019-03-07 23:32:27 -050089INSTANTIATE_TEST_SUITE_P(VariousLengths,
90 GoAwayOpaqueDataLengthTests,
QUICHE team900ba162019-01-30 21:07:06 -050091 ::testing::Values(0, 1, 2, 3, 4, 5, 6));
QUICHE teamfd50a402018-12-07 22:54:05 -050092
93TEST_P(GoAwayOpaqueDataLengthTests, ValidLength) {
94 Http2GoAwayFields goaway;
95 Randomize(&goaway, RandomPtr());
bnc47904002019-08-16 11:49:48 -070096 std::string opaque_data = Random().RandString(length_);
QUICHE teamfd50a402018-12-07 22:54:05 -050097 Http2FrameBuilder fb;
98 fb.Append(goaway);
99 fb.Append(opaque_data);
100 Http2FrameHeader header(fb.size(), Http2FrameType::GOAWAY, RandFlags(),
101 RandStreamId());
102 set_frame_header(header);
103 FrameParts expected(header, opaque_data);
104 expected.SetOptGoaway(goaway);
105 ASSERT_TRUE(DecodePayloadAndValidateSeveralWays(fb.buffer(), expected));
106}
107
108} // namespace
109} // namespace test
110} // namespace http2