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/data_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.h" |
| 16 | #include "net/third_party/quiche/src/http2/http2_structures_test_util.h" |
QUICHE team | 61940b4 | 2019-03-07 23:32:27 -0500 | [diff] [blame] | 17 | #include "net/third_party/quiche/src/http2/platform/api/http2_logging.h" |
QUICHE team | fd50a40 | 2018-12-07 22:54:05 -0500 | [diff] [blame] | 18 | #include "net/third_party/quiche/src/http2/platform/api/http2_test_helpers.h" |
| 19 | #include "net/third_party/quiche/src/http2/test_tools/frame_parts.h" |
| 20 | #include "net/third_party/quiche/src/http2/test_tools/frame_parts_collector.h" |
| 21 | #include "net/third_party/quiche/src/http2/test_tools/http2_random.h" |
| 22 | #include "net/third_party/quiche/src/http2/tools/http2_frame_builder.h" |
| 23 | #include "net/third_party/quiche/src/http2/tools/random_decoder_test.h" |
| 24 | |
| 25 | using ::testing::AssertionResult; |
| 26 | |
| 27 | namespace http2 { |
| 28 | namespace test { |
| 29 | |
| 30 | // Provides friend access to an instance of the payload decoder, and also |
| 31 | // provides info to aid in testing. |
| 32 | class DataPayloadDecoderPeer { |
| 33 | public: |
| 34 | static constexpr Http2FrameType FrameType() { return Http2FrameType::DATA; } |
| 35 | |
| 36 | // Returns the mask of flags that affect the decoding of the payload (i.e. |
| 37 | // flags that that indicate the presence of certain fields or padding). |
| 38 | static constexpr uint8_t FlagsAffectingPayloadDecoding() { |
| 39 | return Http2FrameFlag::PADDED; |
| 40 | } |
| 41 | }; |
| 42 | |
| 43 | namespace { |
| 44 | |
| 45 | struct Listener : public FramePartsCollector { |
| 46 | void OnDataStart(const Http2FrameHeader& header) override { |
QUICHE team | 61940b4 | 2019-03-07 23:32:27 -0500 | [diff] [blame] | 47 | HTTP2_VLOG(1) << "OnDataStart: " << header; |
QUICHE team | fd50a40 | 2018-12-07 22:54:05 -0500 | [diff] [blame] | 48 | StartFrame(header)->OnDataStart(header); |
| 49 | } |
| 50 | |
| 51 | void OnDataPayload(const char* data, size_t len) override { |
QUICHE team | 61940b4 | 2019-03-07 23:32:27 -0500 | [diff] [blame] | 52 | HTTP2_VLOG(1) << "OnDataPayload: len=" << len; |
QUICHE team | fd50a40 | 2018-12-07 22:54:05 -0500 | [diff] [blame] | 53 | CurrentFrame()->OnDataPayload(data, len); |
| 54 | } |
| 55 | |
| 56 | void OnDataEnd() override { |
QUICHE team | 61940b4 | 2019-03-07 23:32:27 -0500 | [diff] [blame] | 57 | HTTP2_VLOG(1) << "OnDataEnd"; |
QUICHE team | fd50a40 | 2018-12-07 22:54:05 -0500 | [diff] [blame] | 58 | EndFrame()->OnDataEnd(); |
| 59 | } |
| 60 | |
| 61 | void OnPadLength(size_t pad_length) override { |
QUICHE team | 61940b4 | 2019-03-07 23:32:27 -0500 | [diff] [blame] | 62 | HTTP2_VLOG(1) << "OnPadLength: " << pad_length; |
QUICHE team | fd50a40 | 2018-12-07 22:54:05 -0500 | [diff] [blame] | 63 | CurrentFrame()->OnPadLength(pad_length); |
| 64 | } |
| 65 | |
| 66 | void OnPadding(const char* padding, size_t skipped_length) override { |
QUICHE team | 61940b4 | 2019-03-07 23:32:27 -0500 | [diff] [blame] | 67 | HTTP2_VLOG(1) << "OnPadding: " << skipped_length; |
QUICHE team | fd50a40 | 2018-12-07 22:54:05 -0500 | [diff] [blame] | 68 | CurrentFrame()->OnPadding(padding, skipped_length); |
| 69 | } |
| 70 | |
| 71 | void OnPaddingTooLong(const Http2FrameHeader& header, |
| 72 | size_t missing_length) override { |
QUICHE team | 61940b4 | 2019-03-07 23:32:27 -0500 | [diff] [blame] | 73 | HTTP2_VLOG(1) << "OnPaddingTooLong: " << header |
| 74 | << " missing_length: " << missing_length; |
QUICHE team | fd50a40 | 2018-12-07 22:54:05 -0500 | [diff] [blame] | 75 | EndFrame()->OnPaddingTooLong(header, missing_length); |
| 76 | } |
| 77 | }; |
| 78 | |
| 79 | class DataPayloadDecoderTest |
| 80 | : public AbstractPaddablePayloadDecoderTest<DataPayloadDecoder, |
| 81 | DataPayloadDecoderPeer, |
| 82 | Listener> { |
| 83 | protected: |
| 84 | AssertionResult CreateAndDecodeDataOfSize(size_t data_size) { |
| 85 | Reset(); |
| 86 | uint8_t flags = RandFlags(); |
| 87 | |
bnc | 4790400 | 2019-08-16 11:49:48 -0700 | [diff] [blame] | 88 | std::string data_payload = Random().RandString(data_size); |
QUICHE team | fd50a40 | 2018-12-07 22:54:05 -0500 | [diff] [blame] | 89 | frame_builder_.Append(data_payload); |
| 90 | MaybeAppendTrailingPadding(); |
| 91 | |
| 92 | Http2FrameHeader frame_header(frame_builder_.size(), Http2FrameType::DATA, |
| 93 | flags, RandStreamId()); |
| 94 | set_frame_header(frame_header); |
| 95 | ScrubFlagsOfHeader(&frame_header); |
| 96 | FrameParts expected(frame_header, data_payload, total_pad_length_); |
| 97 | VERIFY_AND_RETURN_SUCCESS( |
| 98 | DecodePayloadAndValidateSeveralWays(frame_builder_.buffer(), expected)); |
| 99 | } |
| 100 | }; |
| 101 | |
QUICHE team | 61940b4 | 2019-03-07 23:32:27 -0500 | [diff] [blame] | 102 | INSTANTIATE_TEST_SUITE_P(VariousPadLengths, |
| 103 | DataPayloadDecoderTest, |
QUICHE team | 900ba16 | 2019-01-30 21:07:06 -0500 | [diff] [blame] | 104 | ::testing::Values(0, 1, 2, 3, 4, 254, 255, 256)); |
QUICHE team | fd50a40 | 2018-12-07 22:54:05 -0500 | [diff] [blame] | 105 | |
| 106 | TEST_P(DataPayloadDecoderTest, VariousDataPayloadSizes) { |
| 107 | for (size_t data_size : {0, 1, 2, 3, 255, 256, 1024}) { |
| 108 | EXPECT_TRUE(CreateAndDecodeDataOfSize(data_size)); |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | } // namespace |
| 113 | } // namespace test |
| 114 | } // namespace http2 |