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/rst_stream_payload_decoder.h"
6 
7 #include <stddef.h>
8 
9 #include "quiche/http2/decoder/http2_frame_decoder_listener.h"
10 #include "quiche/http2/http2_constants.h"
11 #include "quiche/http2/test_tools/frame_parts.h"
12 #include "quiche/http2/test_tools/frame_parts_collector.h"
13 #include "quiche/http2/test_tools/http2_constants_test_util.h"
14 #include "quiche/http2/test_tools/http2_frame_builder.h"
15 #include "quiche/http2/test_tools/http2_random.h"
16 #include "quiche/http2/test_tools/http2_structures_test_util.h"
17 #include "quiche/http2/test_tools/payload_decoder_base_test_util.h"
18 #include "quiche/http2/test_tools/random_decoder_test_base.h"
19 #include "quiche/common/platform/api/quiche_logging.h"
20 #include "quiche/common/platform/api/quiche_test.h"
21 
22 namespace http2 {
23 namespace test {
24 
25 class RstStreamPayloadDecoderPeer {
26  public:
FrameType()27   static constexpr Http2FrameType FrameType() {
28     return Http2FrameType::RST_STREAM;
29   }
30 
31   // Returns the mask of flags that affect the decoding of the payload (i.e.
32   // flags that that indicate the presence of certain fields or padding).
FlagsAffectingPayloadDecoding()33   static constexpr uint8_t FlagsAffectingPayloadDecoding() { return 0; }
34 };
35 
36 namespace {
37 
38 struct Listener : public FramePartsCollector {
OnRstStreamhttp2::test::__anon2a8ceb7f0111::Listener39   void OnRstStream(const Http2FrameHeader& header,
40                    Http2ErrorCode error_code) override {
41     QUICHE_VLOG(1) << "OnRstStream: " << header
42                    << "; error_code=" << error_code;
43     StartAndEndFrame(header)->OnRstStream(header, error_code);
44   }
45 
OnFrameSizeErrorhttp2::test::__anon2a8ceb7f0111::Listener46   void OnFrameSizeError(const Http2FrameHeader& header) override {
47     QUICHE_VLOG(1) << "OnFrameSizeError: " << header;
48     FrameError(header)->OnFrameSizeError(header);
49   }
50 };
51 
52 class RstStreamPayloadDecoderTest
53     : public AbstractPayloadDecoderTest<RstStreamPayloadDecoder,
54                                         RstStreamPayloadDecoderPeer, Listener> {
55  protected:
RandRstStreamFields()56   Http2RstStreamFields RandRstStreamFields() {
57     Http2RstStreamFields fields;
58     test::Randomize(&fields, RandomPtr());
59     return fields;
60   }
61 };
62 
63 // Confirm we get an error if the payload is not the correct size to hold
64 // exactly one Http2RstStreamFields.
TEST_F(RstStreamPayloadDecoderTest,WrongSize)65 TEST_F(RstStreamPayloadDecoderTest, WrongSize) {
66   auto approve_size = [](size_t size) {
67     return size != Http2RstStreamFields::EncodedSize();
68   };
69   Http2FrameBuilder fb;
70   fb.Append(RandRstStreamFields());
71   fb.Append(RandRstStreamFields());
72   fb.Append(RandRstStreamFields());
73   EXPECT_TRUE(VerifyDetectsFrameSizeError(0, fb.buffer(), approve_size));
74 }
75 
TEST_F(RstStreamPayloadDecoderTest,AllErrors)76 TEST_F(RstStreamPayloadDecoderTest, AllErrors) {
77   for (auto error_code : AllHttp2ErrorCodes()) {
78     Http2RstStreamFields fields{error_code};
79     Http2FrameBuilder fb;
80     fb.Append(fields);
81     Http2FrameHeader header(fb.size(), Http2FrameType::RST_STREAM, RandFlags(),
82                             RandStreamId());
83     set_frame_header(header);
84     FrameParts expected(header);
85     expected.SetOptRstStreamErrorCode(error_code);
86     EXPECT_TRUE(DecodePayloadAndValidateSeveralWays(fb.buffer(), expected));
87   }
88 }
89 
90 }  // namespace
91 }  // namespace test
92 }  // namespace http2
93