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/altsvc_payload_decoder.h"
6
7 #include <stddef.h>
8
9 #include <string>
10 #include <tuple>
11
12 #include "quiche/http2/decoder/http2_frame_decoder_listener.h"
13 #include "quiche/http2/http2_constants.h"
14 #include "quiche/http2/test_tools/frame_parts.h"
15 #include "quiche/http2/test_tools/frame_parts_collector.h"
16 #include "quiche/http2/test_tools/http2_frame_builder.h"
17 #include "quiche/http2/test_tools/http2_random.h"
18 #include "quiche/http2/test_tools/http2_structures_test_util.h"
19 #include "quiche/http2/test_tools/payload_decoder_base_test_util.h"
20 #include "quiche/http2/test_tools/random_decoder_test_base.h"
21 #include "quiche/common/platform/api/quiche_logging.h"
22 #include "quiche/common/platform/api/quiche_test.h"
23
24 namespace http2 {
25 namespace test {
26
27 // Provides friend access to an instance of the payload decoder, and also
28 // provides info to aid in testing.
29 class AltSvcPayloadDecoderPeer {
30 public:
FrameType()31 static constexpr Http2FrameType FrameType() { return Http2FrameType::ALTSVC; }
32
33 // Returns the mask of flags that affect the decoding of the payload (i.e.
34 // flags that that indicate the presence of certain fields or padding).
FlagsAffectingPayloadDecoding()35 static constexpr uint8_t FlagsAffectingPayloadDecoding() { return 0; }
36 };
37
38 namespace {
39
40 struct Listener : public FramePartsCollector {
OnAltSvcStarthttp2::test::__anon522b63680111::Listener41 void OnAltSvcStart(const Http2FrameHeader& header, size_t origin_length,
42 size_t value_length) override {
43 QUICHE_VLOG(1) << "OnAltSvcStart header: " << header
44 << "; origin_length=" << origin_length
45 << "; value_length=" << value_length;
46 StartFrame(header)->OnAltSvcStart(header, origin_length, value_length);
47 }
48
OnAltSvcOriginDatahttp2::test::__anon522b63680111::Listener49 void OnAltSvcOriginData(const char* data, size_t len) override {
50 QUICHE_VLOG(1) << "OnAltSvcOriginData: len=" << len;
51 CurrentFrame()->OnAltSvcOriginData(data, len);
52 }
53
OnAltSvcValueDatahttp2::test::__anon522b63680111::Listener54 void OnAltSvcValueData(const char* data, size_t len) override {
55 QUICHE_VLOG(1) << "OnAltSvcValueData: len=" << len;
56 CurrentFrame()->OnAltSvcValueData(data, len);
57 }
58
OnAltSvcEndhttp2::test::__anon522b63680111::Listener59 void OnAltSvcEnd() override {
60 QUICHE_VLOG(1) << "OnAltSvcEnd";
61 EndFrame()->OnAltSvcEnd();
62 }
63
OnFrameSizeErrorhttp2::test::__anon522b63680111::Listener64 void OnFrameSizeError(const Http2FrameHeader& header) override {
65 QUICHE_VLOG(1) << "OnFrameSizeError: " << header;
66 FrameError(header)->OnFrameSizeError(header);
67 }
68 };
69
70 class AltSvcPayloadDecoderTest
71 : public AbstractPayloadDecoderTest<AltSvcPayloadDecoder,
72 AltSvcPayloadDecoderPeer, Listener> {};
73
74 // Confirm we get an error if the payload is not long enough to hold
75 // Http2AltSvcFields and the indicated length of origin.
TEST_F(AltSvcPayloadDecoderTest,Truncated)76 TEST_F(AltSvcPayloadDecoderTest, Truncated) {
77 Http2FrameBuilder fb;
78 fb.Append(Http2AltSvcFields{0xffff}); // The longest possible origin length.
79 fb.Append("Too little origin!");
80 EXPECT_TRUE(
81 VerifyDetectsFrameSizeError(0, fb.buffer(), /*approve_size*/ nullptr));
82 }
83
84 class AltSvcPayloadLengthTests
85 : public AltSvcPayloadDecoderTest,
86 public ::testing::WithParamInterface<std::tuple<uint16_t, uint32_t>> {
87 protected:
AltSvcPayloadLengthTests()88 AltSvcPayloadLengthTests()
89 : origin_length_(std::get<0>(GetParam())),
90 value_length_(std::get<1>(GetParam())) {
91 QUICHE_VLOG(1) << "################ origin_length_=" << origin_length_
92 << " value_length_=" << value_length_
93 << " ################";
94 }
95
96 const uint16_t origin_length_;
97 const uint32_t value_length_;
98 };
99
100 INSTANTIATE_TEST_SUITE_P(VariousOriginAndValueLengths, AltSvcPayloadLengthTests,
101 ::testing::Combine(::testing::Values(0, 1, 3, 65535),
102 ::testing::Values(0, 1, 3, 65537)));
103
TEST_P(AltSvcPayloadLengthTests,ValidOriginAndValueLength)104 TEST_P(AltSvcPayloadLengthTests, ValidOriginAndValueLength) {
105 std::string origin = Random().RandString(origin_length_);
106 std::string value = Random().RandString(value_length_);
107 Http2FrameBuilder fb;
108 fb.Append(Http2AltSvcFields{origin_length_});
109 fb.Append(origin);
110 fb.Append(value);
111 Http2FrameHeader header(fb.size(), Http2FrameType::ALTSVC, RandFlags(),
112 RandStreamId());
113 set_frame_header(header);
114 FrameParts expected(header);
115 expected.SetAltSvcExpected(origin, value);
116 ASSERT_TRUE(DecodePayloadAndValidateSeveralWays(fb.buffer(), expected));
117 }
118
119 } // namespace
120 } // namespace test
121 } // namespace http2
122