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/ping_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_frame_builder.h"
14 #include "quiche/http2/test_tools/http2_random.h"
15 #include "quiche/http2/test_tools/http2_structures_test_util.h"
16 #include "quiche/http2/test_tools/payload_decoder_base_test_util.h"
17 #include "quiche/http2/test_tools/random_decoder_test_base.h"
18 #include "quiche/common/platform/api/quiche_logging.h"
19 #include "quiche/common/platform/api/quiche_test.h"
20 
21 namespace http2 {
22 namespace test {
23 
24 class PingPayloadDecoderPeer {
25  public:
FrameType()26   static constexpr Http2FrameType FrameType() { return Http2FrameType::PING; }
27 
28   // Returns the mask of flags that affect the decoding of the payload (i.e.
29   // flags that that indicate the presence of certain fields or padding).
FlagsAffectingPayloadDecoding()30   static constexpr uint8_t FlagsAffectingPayloadDecoding() { return 0; }
31 };
32 
33 namespace {
34 
35 struct Listener : public FramePartsCollector {
OnPinghttp2::test::__anon24fd5b690111::Listener36   void OnPing(const Http2FrameHeader& header,
37               const Http2PingFields& ping) override {
38     QUICHE_VLOG(1) << "OnPing: " << header << "; " << ping;
39     StartAndEndFrame(header)->OnPing(header, ping);
40   }
41 
OnPingAckhttp2::test::__anon24fd5b690111::Listener42   void OnPingAck(const Http2FrameHeader& header,
43                  const Http2PingFields& ping) override {
44     QUICHE_VLOG(1) << "OnPingAck: " << header << "; " << ping;
45     StartAndEndFrame(header)->OnPingAck(header, ping);
46   }
47 
OnFrameSizeErrorhttp2::test::__anon24fd5b690111::Listener48   void OnFrameSizeError(const Http2FrameHeader& header) override {
49     QUICHE_VLOG(1) << "OnFrameSizeError: " << header;
50     FrameError(header)->OnFrameSizeError(header);
51   }
52 };
53 
54 class PingPayloadDecoderTest
55     : public AbstractPayloadDecoderTest<PingPayloadDecoder,
56                                         PingPayloadDecoderPeer, Listener> {
57  protected:
RandPingFields()58   Http2PingFields RandPingFields() {
59     Http2PingFields fields;
60     test::Randomize(&fields, RandomPtr());
61     return fields;
62   }
63 };
64 
65 // Confirm we get an error if the payload is not the correct size to hold
66 // exactly one Http2PingFields.
TEST_F(PingPayloadDecoderTest,WrongSize)67 TEST_F(PingPayloadDecoderTest, WrongSize) {
68   auto approve_size = [](size_t size) {
69     return size != Http2PingFields::EncodedSize();
70   };
71   Http2FrameBuilder fb;
72   fb.Append(RandPingFields());
73   fb.Append(RandPingFields());
74   fb.Append(RandPingFields());
75   EXPECT_TRUE(VerifyDetectsFrameSizeError(0, fb.buffer(), approve_size));
76 }
77 
TEST_F(PingPayloadDecoderTest,Ping)78 TEST_F(PingPayloadDecoderTest, Ping) {
79   for (int n = 0; n < 100; ++n) {
80     Http2PingFields fields = RandPingFields();
81     Http2FrameBuilder fb;
82     fb.Append(fields);
83     Http2FrameHeader header(fb.size(), Http2FrameType::PING,
84                             RandFlags() & ~Http2FrameFlag::ACK, RandStreamId());
85     set_frame_header(header);
86     FrameParts expected(header);
87     expected.SetOptPing(fields);
88     EXPECT_TRUE(DecodePayloadAndValidateSeveralWays(fb.buffer(), expected));
89   }
90 }
91 
TEST_F(PingPayloadDecoderTest,PingAck)92 TEST_F(PingPayloadDecoderTest, PingAck) {
93   for (int n = 0; n < 100; ++n) {
94     Http2PingFields fields;
95     Randomize(&fields, RandomPtr());
96     Http2FrameBuilder fb;
97     fb.Append(fields);
98     Http2FrameHeader header(fb.size(), Http2FrameType::PING,
99                             RandFlags() | Http2FrameFlag::ACK, RandStreamId());
100     set_frame_header(header);
101     FrameParts expected(header);
102     expected.SetOptPing(fields);
103     EXPECT_TRUE(DecodePayloadAndValidateSeveralWays(fb.buffer(), expected));
104   }
105 }
106 
107 }  // namespace
108 }  // namespace test
109 }  // namespace http2
110