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/ping_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_structures_test_util.h" |
| 17 #include "net/http2/tools/http2_frame_builder.h" |
| 18 #include "net/http2/tools/http2_random.h" |
| 19 #include "net/http2/tools/random_decoder_test.h" |
| 20 #include "testing/gtest/include/gtest/gtest.h" |
| 21 |
| 22 namespace net { |
| 23 namespace test { |
| 24 |
| 25 class PingPayloadDecoderPeer { |
| 26 public: |
| 27 static constexpr Http2FrameType FrameType() { return Http2FrameType::PING; } |
| 28 |
| 29 // Returns the mask of flags that affect the decoding of the payload (i.e. |
| 30 // flags that that indicate the presence of certain fields or padding). |
| 31 static constexpr uint8_t FlagsAffectingPayloadDecoding() { return 0; } |
| 32 |
| 33 static void Randomize(PingPayloadDecoder* p, RandomBase* rng) { |
| 34 VLOG(1) << "PingPayloadDecoderPeer::Randomize"; |
| 35 test::Randomize(&p->ping_fields_, rng); |
| 36 } |
| 37 }; |
| 38 |
| 39 namespace { |
| 40 |
| 41 struct Listener : public FramePartsCollector { |
| 42 void OnPing(const Http2FrameHeader& header, |
| 43 const Http2PingFields& ping) override { |
| 44 VLOG(1) << "OnPing: " << header << "; " << ping; |
| 45 StartAndEndFrame(header)->OnPing(header, ping); |
| 46 } |
| 47 |
| 48 void OnPingAck(const Http2FrameHeader& header, |
| 49 const Http2PingFields& ping) override { |
| 50 VLOG(1) << "OnPingAck: " << header << "; " << ping; |
| 51 StartAndEndFrame(header)->OnPingAck(header, ping); |
| 52 } |
| 53 |
| 54 void OnFrameSizeError(const Http2FrameHeader& header) override { |
| 55 VLOG(1) << "OnFrameSizeError: " << header; |
| 56 FrameError(header)->OnFrameSizeError(header); |
| 57 } |
| 58 }; |
| 59 |
| 60 class PingPayloadDecoderTest |
| 61 : public AbstractPayloadDecoderTest<PingPayloadDecoder, |
| 62 PingPayloadDecoderPeer, |
| 63 Listener> { |
| 64 public: |
| 65 static bool ApproveSizeForWrongSize(size_t size) { |
| 66 return size != Http2PingFields::EncodedSize(); |
| 67 } |
| 68 |
| 69 protected: |
| 70 Http2PingFields RandPingFields() { |
| 71 Http2PingFields fields; |
| 72 test::Randomize(&fields, RandomPtr()); |
| 73 return fields; |
| 74 } |
| 75 }; |
| 76 |
| 77 // Confirm we get an error if the payload is not the correct size to hold |
| 78 // exactly one Http2PingFields. |
| 79 TEST_F(PingPayloadDecoderTest, WrongSize) { |
| 80 Http2FrameBuilder fb; |
| 81 fb.Append(RandPingFields()); |
| 82 fb.Append(RandPingFields()); |
| 83 fb.Append(RandPingFields()); |
| 84 EXPECT_TRUE(VerifyDetectsFrameSizeError( |
| 85 0, fb.buffer(), |
| 86 base::Bind(&PingPayloadDecoderTest::ApproveSizeForWrongSize))); |
| 87 } |
| 88 |
| 89 TEST_F(PingPayloadDecoderTest, Ping) { |
| 90 for (int n = 0; n < 100; ++n) { |
| 91 Http2PingFields fields = RandPingFields(); |
| 92 Http2FrameBuilder fb; |
| 93 fb.Append(fields); |
| 94 Http2FrameHeader header(fb.size(), Http2FrameType::PING, |
| 95 RandFlags() & ~Http2FrameFlag::FLAG_ACK, |
| 96 RandStreamId()); |
| 97 set_frame_header(header); |
| 98 FrameParts expected(header); |
| 99 expected.opt_ping = fields; |
| 100 EXPECT_TRUE(DecodePayloadAndValidateSeveralWays(fb.buffer(), expected)); |
| 101 } |
| 102 } |
| 103 |
| 104 TEST_F(PingPayloadDecoderTest, PingAck) { |
| 105 for (int n = 0; n < 100; ++n) { |
| 106 Http2PingFields fields; |
| 107 Randomize(&fields, RandomPtr()); |
| 108 Http2FrameBuilder fb; |
| 109 fb.Append(fields); |
| 110 Http2FrameHeader header(fb.size(), Http2FrameType::PING, |
| 111 RandFlags() | Http2FrameFlag::FLAG_ACK, |
| 112 RandStreamId()); |
| 113 set_frame_header(header); |
| 114 FrameParts expected(header); |
| 115 expected.opt_ping = fields; |
| 116 EXPECT_TRUE(DecodePayloadAndValidateSeveralWays(fb.buffer(), expected)); |
| 117 } |
| 118 } |
| 119 |
| 120 } // namespace |
| 121 } // namespace test |
| 122 } // namespace net |
OLD | NEW |