blob: 10759329c0fe749bc957bcafa4f3aac5243657e0 [file] [log] [blame]
QUICHE teamfd50a402018-12-07 22:54:05 -05001// 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
bnc47904002019-08-16 11:49:48 -07009#include <string>
QUICHE teamfd50a402018-12-07 22:54:05 -050010#include <type_traits>
11
QUICHE teamfd50a402018-12-07 22:54:05 -050012#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 team61940b42019-03-07 23:32:27 -050017#include "net/third_party/quiche/src/http2/platform/api/http2_logging.h"
QUICHE teamfd50a402018-12-07 22:54:05 -050018#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
23namespace http2 {
24namespace test {
25namespace {
26Http2FrameType 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.
31class 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
40namespace {
41
42struct Listener : public FramePartsCollector {
43 void OnUnknownStart(const Http2FrameHeader& header) override {
QUICHE team61940b42019-03-07 23:32:27 -050044 HTTP2_VLOG(1) << "OnUnknownStart: " << header;
QUICHE teamfd50a402018-12-07 22:54:05 -050045 StartFrame(header)->OnUnknownStart(header);
46 }
47
48 void OnUnknownPayload(const char* data, size_t len) override {
QUICHE team61940b42019-03-07 23:32:27 -050049 HTTP2_VLOG(1) << "OnUnknownPayload: len=" << len;
QUICHE teamfd50a402018-12-07 22:54:05 -050050 CurrentFrame()->OnUnknownPayload(data, len);
51 }
52
53 void OnUnknownEnd() override {
QUICHE team61940b42019-03-07 23:32:27 -050054 HTTP2_VLOG(1) << "OnUnknownEnd";
QUICHE teamfd50a402018-12-07 22:54:05 -050055 EndFrame()->OnUnknownEnd();
56 }
57};
58
59constexpr bool SupportedFrameType = false;
60
61class UnknownPayloadDecoderTest
62 : public AbstractPayloadDecoderTest<UnknownPayloadDecoder,
63 UnknownPayloadDecoderPeer,
64 Listener,
65 SupportedFrameType>,
66 public ::testing::WithParamInterface<uint32_t> {
67 protected:
68 UnknownPayloadDecoderTest() : length_(GetParam()) {
QUICHE team61940b42019-03-07 23:32:27 -050069 HTTP2_VLOG(1) << "################ length_=" << length_
70 << " ################";
QUICHE teamfd50a402018-12-07 22:54:05 -050071
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 team61940b42019-03-07 23:32:27 -050081INSTANTIATE_TEST_SUITE_P(VariousLengths,
82 UnknownPayloadDecoderTest,
QUICHE team900ba162019-01-30 21:07:06 -050083 ::testing::Values(0, 1, 2, 3, 255, 256));
QUICHE teamfd50a402018-12-07 22:54:05 -050084
85TEST_P(UnknownPayloadDecoderTest, ValidLength) {
bnc47904002019-08-16 11:49:48 -070086 std::string unknown_payload = Random().RandString(length_);
QUICHE teamfd50a402018-12-07 22:54:05 -050087 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