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/data_payload_decoder.h"
6 
7 #include <stddef.h>
8 
9 #include <string>
10 
11 #include "quiche/http2/decoder/http2_frame_decoder_listener.h"
12 #include "quiche/http2/http2_constants.h"
13 #include "quiche/http2/http2_structures.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_expect_bug.h"
22 #include "quiche/common/platform/api/quiche_logging.h"
23 #include "quiche/common/platform/api/quiche_test.h"
24 
25 namespace http2 {
26 namespace test {
27 
28 // Provides friend access to an instance of the payload decoder, and also
29 // provides info to aid in testing.
30 class DataPayloadDecoderPeer {
31  public:
FrameType()32   static constexpr Http2FrameType FrameType() { return Http2FrameType::DATA; }
33 
34   // Returns the mask of flags that affect the decoding of the payload (i.e.
35   // flags that that indicate the presence of certain fields or padding).
FlagsAffectingPayloadDecoding()36   static constexpr uint8_t FlagsAffectingPayloadDecoding() {
37     return Http2FrameFlag::PADDED;
38   }
39 };
40 
41 namespace {
42 
43 struct Listener : public FramePartsCollector {
OnDataStarthttp2::test::__anon7c22e9950111::Listener44   void OnDataStart(const Http2FrameHeader& header) override {
45     QUICHE_VLOG(1) << "OnDataStart: " << header;
46     StartFrame(header)->OnDataStart(header);
47   }
48 
OnDataPayloadhttp2::test::__anon7c22e9950111::Listener49   void OnDataPayload(const char* data, size_t len) override {
50     QUICHE_VLOG(1) << "OnDataPayload: len=" << len;
51     CurrentFrame()->OnDataPayload(data, len);
52   }
53 
OnDataEndhttp2::test::__anon7c22e9950111::Listener54   void OnDataEnd() override {
55     QUICHE_VLOG(1) << "OnDataEnd";
56     EndFrame()->OnDataEnd();
57   }
58 
OnPadLengthhttp2::test::__anon7c22e9950111::Listener59   void OnPadLength(size_t pad_length) override {
60     QUICHE_VLOG(1) << "OnPadLength: " << pad_length;
61     CurrentFrame()->OnPadLength(pad_length);
62   }
63 
OnPaddinghttp2::test::__anon7c22e9950111::Listener64   void OnPadding(const char* padding, size_t skipped_length) override {
65     QUICHE_VLOG(1) << "OnPadding: " << skipped_length;
66     CurrentFrame()->OnPadding(padding, skipped_length);
67   }
68 
OnPaddingTooLonghttp2::test::__anon7c22e9950111::Listener69   void OnPaddingTooLong(const Http2FrameHeader& header,
70                         size_t missing_length) override {
71     QUICHE_VLOG(1) << "OnPaddingTooLong: " << header
72                    << "    missing_length: " << missing_length;
73     EndFrame()->OnPaddingTooLong(header, missing_length);
74   }
75 };
76 
77 class DataPayloadDecoderTest
78     : public AbstractPaddablePayloadDecoderTest<
79           DataPayloadDecoder, DataPayloadDecoderPeer, Listener> {
80  protected:
CreateAndDecodeDataOfSize(size_t data_size)81   AssertionResult CreateAndDecodeDataOfSize(size_t data_size) {
82     Reset();
83     uint8_t flags = RandFlags();
84 
85     std::string data_payload = Random().RandString(data_size);
86     frame_builder_.Append(data_payload);
87     MaybeAppendTrailingPadding();
88 
89     Http2FrameHeader frame_header(frame_builder_.size(), Http2FrameType::DATA,
90                                   flags, RandStreamId());
91     set_frame_header(frame_header);
92     ScrubFlagsOfHeader(&frame_header);
93     FrameParts expected(frame_header, data_payload, total_pad_length_);
94     return DecodePayloadAndValidateSeveralWays(frame_builder_.buffer(),
95                                                expected);
96   }
97 };
98 
99 INSTANTIATE_TEST_SUITE_P(VariousPadLengths, DataPayloadDecoderTest,
100                          ::testing::Values(0, 1, 2, 3, 4, 254, 255, 256));
101 
TEST_P(DataPayloadDecoderTest,VariousDataPayloadSizes)102 TEST_P(DataPayloadDecoderTest, VariousDataPayloadSizes) {
103   for (size_t data_size : {0, 1, 2, 3, 255, 256, 1024}) {
104     EXPECT_TRUE(CreateAndDecodeDataOfSize(data_size));
105   }
106 }
107 
108 }  // namespace
109 }  // namespace test
110 }  // namespace http2
111