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/goaway_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 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 | |
| 35 | namespace { |
| 36 | |
| 37 | struct Listener : public FramePartsCollector { |
| 38 | void OnGoAwayStart(const Http2FrameHeader& header, |
| 39 | const Http2GoAwayFields& goaway) override { |
QUICHE team | 61940b4 | 2019-03-07 23:32:27 -0500 | [diff] [blame] | 40 | HTTP2_VLOG(1) << "OnGoAwayStart header: " << header |
| 41 | << "; goaway: " << goaway; |
QUICHE team | fd50a40 | 2018-12-07 22:54:05 -0500 | [diff] [blame] | 42 | StartFrame(header)->OnGoAwayStart(header, goaway); |
| 43 | } |
| 44 | |
| 45 | void OnGoAwayOpaqueData(const char* data, size_t len) override { |
QUICHE team | 61940b4 | 2019-03-07 23:32:27 -0500 | [diff] [blame] | 46 | HTTP2_VLOG(1) << "OnGoAwayOpaqueData: len=" << len; |
QUICHE team | fd50a40 | 2018-12-07 22:54:05 -0500 | [diff] [blame] | 47 | CurrentFrame()->OnGoAwayOpaqueData(data, len); |
| 48 | } |
| 49 | |
| 50 | void OnGoAwayEnd() override { |
QUICHE team | 61940b4 | 2019-03-07 23:32:27 -0500 | [diff] [blame] | 51 | HTTP2_VLOG(1) << "OnGoAwayEnd"; |
QUICHE team | fd50a40 | 2018-12-07 22:54:05 -0500 | [diff] [blame] | 52 | EndFrame()->OnGoAwayEnd(); |
| 53 | } |
| 54 | |
| 55 | void OnFrameSizeError(const Http2FrameHeader& header) override { |
QUICHE team | 61940b4 | 2019-03-07 23:32:27 -0500 | [diff] [blame] | 56 | HTTP2_VLOG(1) << "OnFrameSizeError: " << header; |
QUICHE team | fd50a40 | 2018-12-07 22:54:05 -0500 | [diff] [blame] | 57 | FrameError(header)->OnFrameSizeError(header); |
| 58 | } |
| 59 | }; |
| 60 | |
| 61 | class 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. |
| 68 | TEST_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 | |
| 77 | class GoAwayOpaqueDataLengthTests |
| 78 | : public GoAwayPayloadDecoderTest, |
| 79 | public ::testing::WithParamInterface<uint32_t> { |
| 80 | protected: |
| 81 | GoAwayOpaqueDataLengthTests() : length_(GetParam()) { |
QUICHE team | 61940b4 | 2019-03-07 23:32:27 -0500 | [diff] [blame] | 82 | HTTP2_VLOG(1) << "################ length_=" << length_ |
| 83 | << " ################"; |
QUICHE team | fd50a40 | 2018-12-07 22:54:05 -0500 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | const uint32_t length_; |
| 87 | }; |
| 88 | |
QUICHE team | 61940b4 | 2019-03-07 23:32:27 -0500 | [diff] [blame] | 89 | INSTANTIATE_TEST_SUITE_P(VariousLengths, |
| 90 | GoAwayOpaqueDataLengthTests, |
QUICHE team | 900ba16 | 2019-01-30 21:07:06 -0500 | [diff] [blame] | 91 | ::testing::Values(0, 1, 2, 3, 4, 5, 6)); |
QUICHE team | fd50a40 | 2018-12-07 22:54:05 -0500 | [diff] [blame] | 92 | |
| 93 | TEST_P(GoAwayOpaqueDataLengthTests, ValidLength) { |
| 94 | Http2GoAwayFields goaway; |
| 95 | Randomize(&goaway, RandomPtr()); |
bnc | 4790400 | 2019-08-16 11:49:48 -0700 | [diff] [blame] | 96 | std::string opaque_data = Random().RandString(length_); |
QUICHE team | fd50a40 | 2018-12-07 22:54:05 -0500 | [diff] [blame] | 97 | 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 |