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/unknown_payload_decoder.h" |
| 6 | |
| 7 | #include <stddef.h> |
| 8 | |
bnc | 4790400 | 2019-08-16 11:49:48 -0700 | [diff] [blame] | 9 | #include <string> |
QUICHE team | fd50a40 | 2018-12-07 22:54:05 -0500 | [diff] [blame] | 10 | #include <type_traits> |
| 11 | |
QUICHE team | fd50a40 | 2018-12-07 22:54:05 -0500 | [diff] [blame] | 12 | #include "testing/gtest/include/gtest/gtest.h" |
| 13 | #include "net/third_party/quiche/src/http2/decoder/http2_frame_decoder_listener.h" |
| 14 | #include "net/third_party/quiche/src/http2/decoder/payload_decoders/payload_decoder_base_test_util.h" |
| 15 | #include "net/third_party/quiche/src/http2/http2_constants.h" |
| 16 | #include "net/third_party/quiche/src/http2/http2_structures.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/test_tools/frame_parts.h" |
| 19 | #include "net/third_party/quiche/src/http2/test_tools/frame_parts_collector.h" |
| 20 | #include "net/third_party/quiche/src/http2/test_tools/http2_random.h" |
| 21 | #include "net/third_party/quiche/src/http2/tools/random_decoder_test.h" |
| 22 | |
| 23 | namespace http2 { |
| 24 | namespace test { |
| 25 | namespace { |
| 26 | Http2FrameType g_unknown_frame_type; |
| 27 | } // namespace |
| 28 | |
| 29 | // Provides friend access to an instance of the payload decoder, and also |
| 30 | // provides info to aid in testing. |
| 31 | class UnknownPayloadDecoderPeer { |
| 32 | public: |
| 33 | static Http2FrameType FrameType() { return g_unknown_frame_type; } |
| 34 | |
| 35 | // Returns the mask of flags that affect the decoding of the payload (i.e. |
| 36 | // flags that that indicate the presence of certain fields or padding). |
| 37 | static constexpr uint8_t FlagsAffectingPayloadDecoding() { return 0; } |
| 38 | }; |
| 39 | |
| 40 | namespace { |
| 41 | |
| 42 | struct Listener : public FramePartsCollector { |
| 43 | void OnUnknownStart(const Http2FrameHeader& header) override { |
QUICHE team | 61940b4 | 2019-03-07 23:32:27 -0500 | [diff] [blame] | 44 | HTTP2_VLOG(1) << "OnUnknownStart: " << header; |
QUICHE team | fd50a40 | 2018-12-07 22:54:05 -0500 | [diff] [blame] | 45 | StartFrame(header)->OnUnknownStart(header); |
| 46 | } |
| 47 | |
| 48 | void OnUnknownPayload(const char* data, size_t len) override { |
QUICHE team | 61940b4 | 2019-03-07 23:32:27 -0500 | [diff] [blame] | 49 | HTTP2_VLOG(1) << "OnUnknownPayload: len=" << len; |
QUICHE team | fd50a40 | 2018-12-07 22:54:05 -0500 | [diff] [blame] | 50 | CurrentFrame()->OnUnknownPayload(data, len); |
| 51 | } |
| 52 | |
| 53 | void OnUnknownEnd() override { |
QUICHE team | 61940b4 | 2019-03-07 23:32:27 -0500 | [diff] [blame] | 54 | HTTP2_VLOG(1) << "OnUnknownEnd"; |
QUICHE team | fd50a40 | 2018-12-07 22:54:05 -0500 | [diff] [blame] | 55 | EndFrame()->OnUnknownEnd(); |
| 56 | } |
| 57 | }; |
| 58 | |
| 59 | constexpr bool SupportedFrameType = false; |
| 60 | |
| 61 | class UnknownPayloadDecoderTest |
| 62 | : public AbstractPayloadDecoderTest<UnknownPayloadDecoder, |
| 63 | UnknownPayloadDecoderPeer, |
| 64 | Listener, |
| 65 | SupportedFrameType>, |
| 66 | public ::testing::WithParamInterface<uint32_t> { |
| 67 | protected: |
| 68 | UnknownPayloadDecoderTest() : length_(GetParam()) { |
QUICHE team | 61940b4 | 2019-03-07 23:32:27 -0500 | [diff] [blame] | 69 | HTTP2_VLOG(1) << "################ length_=" << length_ |
| 70 | << " ################"; |
QUICHE team | fd50a40 | 2018-12-07 22:54:05 -0500 | [diff] [blame] | 71 | |
| 72 | // Each test case will choose a random frame type that isn't supported. |
| 73 | do { |
| 74 | g_unknown_frame_type = static_cast<Http2FrameType>(Random().Rand8()); |
| 75 | } while (IsSupportedHttp2FrameType(g_unknown_frame_type)); |
| 76 | } |
| 77 | |
| 78 | const uint32_t length_; |
| 79 | }; |
| 80 | |
QUICHE team | 61940b4 | 2019-03-07 23:32:27 -0500 | [diff] [blame] | 81 | INSTANTIATE_TEST_SUITE_P(VariousLengths, |
| 82 | UnknownPayloadDecoderTest, |
QUICHE team | 900ba16 | 2019-01-30 21:07:06 -0500 | [diff] [blame] | 83 | ::testing::Values(0, 1, 2, 3, 255, 256)); |
QUICHE team | fd50a40 | 2018-12-07 22:54:05 -0500 | [diff] [blame] | 84 | |
| 85 | TEST_P(UnknownPayloadDecoderTest, ValidLength) { |
bnc | 4790400 | 2019-08-16 11:49:48 -0700 | [diff] [blame] | 86 | std::string unknown_payload = Random().RandString(length_); |
QUICHE team | fd50a40 | 2018-12-07 22:54:05 -0500 | [diff] [blame] | 87 | Http2FrameHeader frame_header(length_, g_unknown_frame_type, Random().Rand8(), |
| 88 | RandStreamId()); |
| 89 | set_frame_header(frame_header); |
| 90 | FrameParts expected(frame_header, unknown_payload); |
| 91 | EXPECT_TRUE(DecodePayloadAndValidateSeveralWays(unknown_payload, expected)); |
| 92 | // TODO(jamessynge): Check here (and in other such tests) that the fast |
| 93 | // and slow decode counts are both non-zero. Perhaps also add some kind of |
| 94 | // test for the listener having been called. That could simply be a test |
| 95 | // that there is a single collected FrameParts instance, and that it matches |
| 96 | // expected. |
| 97 | } |
| 98 | |
| 99 | } // namespace |
| 100 | } // namespace test |
| 101 | } // namespace http2 |