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/unknown_payload_decoder.h"
6
7 #include <stddef.h>
8
9 #include <string>
10 #include <type_traits>
11
12 #include "quiche/http2/decoder/http2_frame_decoder_listener.h"
13 #include "quiche/http2/http2_constants.h"
14 #include "quiche/http2/http2_structures.h"
15 #include "quiche/http2/test_tools/frame_parts.h"
16 #include "quiche/http2/test_tools/frame_parts_collector.h"
17 #include "quiche/http2/test_tools/http2_random.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 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:
FrameType()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).
FlagsAffectingPayloadDecoding()37 static constexpr uint8_t FlagsAffectingPayloadDecoding() { return 0; }
38 };
39
40 namespace {
41
42 struct Listener : public FramePartsCollector {
OnUnknownStarthttp2::test::__anon8ccb5a6b0211::Listener43 void OnUnknownStart(const Http2FrameHeader& header) override {
44 QUICHE_VLOG(1) << "OnUnknownStart: " << header;
45 StartFrame(header)->OnUnknownStart(header);
46 }
47
OnUnknownPayloadhttp2::test::__anon8ccb5a6b0211::Listener48 void OnUnknownPayload(const char* data, size_t len) override {
49 QUICHE_VLOG(1) << "OnUnknownPayload: len=" << len;
50 CurrentFrame()->OnUnknownPayload(data, len);
51 }
52
OnUnknownEndhttp2::test::__anon8ccb5a6b0211::Listener53 void OnUnknownEnd() override {
54 QUICHE_VLOG(1) << "OnUnknownEnd";
55 EndFrame()->OnUnknownEnd();
56 }
57 };
58
59 constexpr bool SupportedFrameType = false;
60
61 class UnknownPayloadDecoderTest
62 : public AbstractPayloadDecoderTest<UnknownPayloadDecoder,
63 UnknownPayloadDecoderPeer, Listener,
64 SupportedFrameType>,
65 public ::testing::WithParamInterface<uint32_t> {
66 protected:
UnknownPayloadDecoderTest()67 UnknownPayloadDecoderTest() : length_(GetParam()) {
68 QUICHE_VLOG(1) << "################ length_=" << length_
69 << " ################";
70
71 // Each test case will choose a random frame type that isn't supported.
72 do {
73 g_unknown_frame_type = static_cast<Http2FrameType>(Random().Rand8());
74 } while (IsSupportedHttp2FrameType(g_unknown_frame_type));
75 }
76
77 const uint32_t length_;
78 };
79
80 INSTANTIATE_TEST_SUITE_P(VariousLengths, UnknownPayloadDecoderTest,
81 ::testing::Values(0, 1, 2, 3, 255, 256));
82
TEST_P(UnknownPayloadDecoderTest,ValidLength)83 TEST_P(UnknownPayloadDecoderTest, ValidLength) {
84 std::string unknown_payload = Random().RandString(length_);
85 Http2FrameHeader frame_header(length_, g_unknown_frame_type, Random().Rand8(),
86 RandStreamId());
87 set_frame_header(frame_header);
88 FrameParts expected(frame_header, unknown_payload);
89 EXPECT_TRUE(DecodePayloadAndValidateSeveralWays(unknown_payload, expected));
90 // TODO(jamessynge): Check here (and in other such tests) that the fast
91 // and slow decode counts are both non-zero. Perhaps also add some kind of
92 // test for the listener having been called. That could simply be a test
93 // that there is a single collected FrameParts instance, and that it matches
94 // expected.
95 }
96
97 } // namespace
98 } // namespace test
99 } // namespace http2
100