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