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/goaway_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 GoAwayPayloadDecoderPeer {
27  public:
FrameType()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).
FlagsAffectingPayloadDecoding()32   static constexpr uint8_t FlagsAffectingPayloadDecoding() { return 0; }
33 };
34 
35 namespace {
36 
37 struct Listener : public FramePartsCollector {
OnGoAwayStarthttp2::test::__anonf6a624e30111::Listener38   void OnGoAwayStart(const Http2FrameHeader& header,
39                      const Http2GoAwayFields& goaway) override {
40     QUICHE_VLOG(1) << "OnGoAwayStart header: " << header
41                    << "; goaway: " << goaway;
42     StartFrame(header)->OnGoAwayStart(header, goaway);
43   }
44 
OnGoAwayOpaqueDatahttp2::test::__anonf6a624e30111::Listener45   void OnGoAwayOpaqueData(const char* data, size_t len) override {
46     QUICHE_VLOG(1) << "OnGoAwayOpaqueData: len=" << len;
47     CurrentFrame()->OnGoAwayOpaqueData(data, len);
48   }
49 
OnGoAwayEndhttp2::test::__anonf6a624e30111::Listener50   void OnGoAwayEnd() override {
51     QUICHE_VLOG(1) << "OnGoAwayEnd";
52     EndFrame()->OnGoAwayEnd();
53   }
54 
OnFrameSizeErrorhttp2::test::__anonf6a624e30111::Listener55   void OnFrameSizeError(const Http2FrameHeader& header) override {
56     QUICHE_VLOG(1) << "OnFrameSizeError: " << header;
57     FrameError(header)->OnFrameSizeError(header);
58   }
59 };
60 
61 class GoAwayPayloadDecoderTest
62     : public AbstractPayloadDecoderTest<GoAwayPayloadDecoder,
63                                         GoAwayPayloadDecoderPeer, Listener> {};
64 
65 // Confirm we get an error if the payload is not long enough to hold
66 // Http2GoAwayFields.
TEST_F(GoAwayPayloadDecoderTest,Truncated)67 TEST_F(GoAwayPayloadDecoderTest, Truncated) {
68   auto approve_size = [](size_t size) {
69     return size != Http2GoAwayFields::EncodedSize();
70   };
71   Http2FrameBuilder fb;
72   fb.Append(Http2GoAwayFields(123, Http2ErrorCode::ENHANCE_YOUR_CALM));
73   EXPECT_TRUE(VerifyDetectsFrameSizeError(0, fb.buffer(), approve_size));
74 }
75 
76 class GoAwayOpaqueDataLengthTests
77     : public GoAwayPayloadDecoderTest,
78       public ::testing::WithParamInterface<uint32_t> {
79  protected:
GoAwayOpaqueDataLengthTests()80   GoAwayOpaqueDataLengthTests() : length_(GetParam()) {
81     QUICHE_VLOG(1) << "################  length_=" << length_
82                    << "  ################";
83   }
84 
85   const uint32_t length_;
86 };
87 
88 INSTANTIATE_TEST_SUITE_P(VariousLengths, GoAwayOpaqueDataLengthTests,
89                          ::testing::Values(0, 1, 2, 3, 4, 5, 6));
90 
TEST_P(GoAwayOpaqueDataLengthTests,ValidLength)91 TEST_P(GoAwayOpaqueDataLengthTests, ValidLength) {
92   Http2GoAwayFields goaway;
93   Randomize(&goaway, RandomPtr());
94   std::string opaque_data = Random().RandString(length_);
95   Http2FrameBuilder fb;
96   fb.Append(goaway);
97   fb.Append(opaque_data);
98   Http2FrameHeader header(fb.size(), Http2FrameType::GOAWAY, RandFlags(),
99                           RandStreamId());
100   set_frame_header(header);
101   FrameParts expected(header, opaque_data);
102   expected.SetOptGoaway(goaway);
103   ASSERT_TRUE(DecodePayloadAndValidateSeveralWays(fb.buffer(), expected));
104 }
105 
106 }  // namespace
107 }  // namespace test
108 }  // namespace http2
109