blob: f1dc017c9daf548d2b0ff12f2b67c12d39844370 [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/data_payload_decoder.h"
6
7#include <stddef.h>
8
bnc47904002019-08-16 11:49:48 -07009#include <string>
10
QUICHE teamfd50a402018-12-07 22:54:05 -050011#include "testing/gtest/include/gtest/gtest.h"
12#include "net/third_party/quiche/src/http2/decoder/http2_frame_decoder_listener.h"
13#include "net/third_party/quiche/src/http2/decoder/payload_decoders/payload_decoder_base_test_util.h"
14#include "net/third_party/quiche/src/http2/http2_constants.h"
15#include "net/third_party/quiche/src/http2/http2_structures.h"
16#include "net/third_party/quiche/src/http2/http2_structures_test_util.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/platform/api/http2_test_helpers.h"
19#include "net/third_party/quiche/src/http2/test_tools/frame_parts.h"
20#include "net/third_party/quiche/src/http2/test_tools/frame_parts_collector.h"
21#include "net/third_party/quiche/src/http2/test_tools/http2_random.h"
22#include "net/third_party/quiche/src/http2/tools/http2_frame_builder.h"
23#include "net/third_party/quiche/src/http2/tools/random_decoder_test.h"
24
25using ::testing::AssertionResult;
26
27namespace http2 {
28namespace test {
29
30// Provides friend access to an instance of the payload decoder, and also
31// provides info to aid in testing.
32class DataPayloadDecoderPeer {
33 public:
34 static constexpr Http2FrameType FrameType() { return Http2FrameType::DATA; }
35
36 // Returns the mask of flags that affect the decoding of the payload (i.e.
37 // flags that that indicate the presence of certain fields or padding).
38 static constexpr uint8_t FlagsAffectingPayloadDecoding() {
39 return Http2FrameFlag::PADDED;
40 }
41};
42
43namespace {
44
45struct Listener : public FramePartsCollector {
46 void OnDataStart(const Http2FrameHeader& header) override {
QUICHE team61940b42019-03-07 23:32:27 -050047 HTTP2_VLOG(1) << "OnDataStart: " << header;
QUICHE teamfd50a402018-12-07 22:54:05 -050048 StartFrame(header)->OnDataStart(header);
49 }
50
51 void OnDataPayload(const char* data, size_t len) override {
QUICHE team61940b42019-03-07 23:32:27 -050052 HTTP2_VLOG(1) << "OnDataPayload: len=" << len;
QUICHE teamfd50a402018-12-07 22:54:05 -050053 CurrentFrame()->OnDataPayload(data, len);
54 }
55
56 void OnDataEnd() override {
QUICHE team61940b42019-03-07 23:32:27 -050057 HTTP2_VLOG(1) << "OnDataEnd";
QUICHE teamfd50a402018-12-07 22:54:05 -050058 EndFrame()->OnDataEnd();
59 }
60
61 void OnPadLength(size_t pad_length) override {
QUICHE team61940b42019-03-07 23:32:27 -050062 HTTP2_VLOG(1) << "OnPadLength: " << pad_length;
QUICHE teamfd50a402018-12-07 22:54:05 -050063 CurrentFrame()->OnPadLength(pad_length);
64 }
65
66 void OnPadding(const char* padding, size_t skipped_length) override {
QUICHE team61940b42019-03-07 23:32:27 -050067 HTTP2_VLOG(1) << "OnPadding: " << skipped_length;
QUICHE teamfd50a402018-12-07 22:54:05 -050068 CurrentFrame()->OnPadding(padding, skipped_length);
69 }
70
71 void OnPaddingTooLong(const Http2FrameHeader& header,
72 size_t missing_length) override {
QUICHE team61940b42019-03-07 23:32:27 -050073 HTTP2_VLOG(1) << "OnPaddingTooLong: " << header
74 << " missing_length: " << missing_length;
QUICHE teamfd50a402018-12-07 22:54:05 -050075 EndFrame()->OnPaddingTooLong(header, missing_length);
76 }
77};
78
79class DataPayloadDecoderTest
80 : public AbstractPaddablePayloadDecoderTest<DataPayloadDecoder,
81 DataPayloadDecoderPeer,
82 Listener> {
83 protected:
84 AssertionResult CreateAndDecodeDataOfSize(size_t data_size) {
85 Reset();
86 uint8_t flags = RandFlags();
87
bnc47904002019-08-16 11:49:48 -070088 std::string data_payload = Random().RandString(data_size);
QUICHE teamfd50a402018-12-07 22:54:05 -050089 frame_builder_.Append(data_payload);
90 MaybeAppendTrailingPadding();
91
92 Http2FrameHeader frame_header(frame_builder_.size(), Http2FrameType::DATA,
93 flags, RandStreamId());
94 set_frame_header(frame_header);
95 ScrubFlagsOfHeader(&frame_header);
96 FrameParts expected(frame_header, data_payload, total_pad_length_);
97 VERIFY_AND_RETURN_SUCCESS(
98 DecodePayloadAndValidateSeveralWays(frame_builder_.buffer(), expected));
99 }
100};
101
QUICHE team61940b42019-03-07 23:32:27 -0500102INSTANTIATE_TEST_SUITE_P(VariousPadLengths,
103 DataPayloadDecoderTest,
QUICHE team900ba162019-01-30 21:07:06 -0500104 ::testing::Values(0, 1, 2, 3, 4, 254, 255, 256));
QUICHE teamfd50a402018-12-07 22:54:05 -0500105
106TEST_P(DataPayloadDecoderTest, VariousDataPayloadSizes) {
107 for (size_t data_size : {0, 1, 2, 3, 255, 256, 1024}) {
108 EXPECT_TRUE(CreateAndDecodeDataOfSize(data_size));
109 }
110}
111
112} // namespace
113} // namespace test
114} // namespace http2