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/window_update_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 WindowUpdatePayloadDecoderPeer {
25  public:
FrameType()26   static constexpr Http2FrameType FrameType() {
27     return Http2FrameType::WINDOW_UPDATE;
28   }
29 
30   // Returns the mask of flags that affect the decoding of the payload (i.e.
31   // flags that that indicate the presence of certain fields or padding).
FlagsAffectingPayloadDecoding()32   static constexpr uint8_t FlagsAffectingPayloadDecoding() { return 0; }
33 };
34 
35 namespace {
36 
37 struct Listener : public FramePartsCollector {
OnWindowUpdatehttp2::test::__anon92d7d5150111::Listener38   void OnWindowUpdate(const Http2FrameHeader& header,
39                       uint32_t window_size_increment) override {
40     QUICHE_VLOG(1) << "OnWindowUpdate: " << header
41                    << "; window_size_increment=" << window_size_increment;
42     EXPECT_EQ(Http2FrameType::WINDOW_UPDATE, header.type);
43     StartAndEndFrame(header)->OnWindowUpdate(header, window_size_increment);
44   }
45 
OnFrameSizeErrorhttp2::test::__anon92d7d5150111::Listener46   void OnFrameSizeError(const Http2FrameHeader& header) override {
47     QUICHE_VLOG(1) << "OnFrameSizeError: " << header;
48     FrameError(header)->OnFrameSizeError(header);
49   }
50 };
51 
52 class WindowUpdatePayloadDecoderTest
53     : public AbstractPayloadDecoderTest<WindowUpdatePayloadDecoder,
54                                         WindowUpdatePayloadDecoderPeer,
55                                         Listener> {
56  protected:
RandWindowUpdateFields()57   Http2WindowUpdateFields RandWindowUpdateFields() {
58     Http2WindowUpdateFields fields;
59     test::Randomize(&fields, RandomPtr());
60     QUICHE_VLOG(3) << "RandWindowUpdateFields: " << fields;
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 Http2WindowUpdateFields.
TEST_F(WindowUpdatePayloadDecoderTest,WrongSize)67 TEST_F(WindowUpdatePayloadDecoderTest, WrongSize) {
68   auto approve_size = [](size_t size) {
69     return size != Http2WindowUpdateFields::EncodedSize();
70   };
71   Http2FrameBuilder fb;
72   fb.Append(RandWindowUpdateFields());
73   fb.Append(RandWindowUpdateFields());
74   fb.Append(RandWindowUpdateFields());
75   EXPECT_TRUE(VerifyDetectsFrameSizeError(0, fb.buffer(), approve_size));
76 }
77 
TEST_F(WindowUpdatePayloadDecoderTest,VariousPayloads)78 TEST_F(WindowUpdatePayloadDecoderTest, VariousPayloads) {
79   for (int n = 0; n < 100; ++n) {
80     uint32_t stream_id = n == 0 ? 0 : RandStreamId();
81     Http2WindowUpdateFields fields = RandWindowUpdateFields();
82     Http2FrameBuilder fb;
83     fb.Append(fields);
84     Http2FrameHeader header(fb.size(), Http2FrameType::WINDOW_UPDATE,
85                             RandFlags(), stream_id);
86     set_frame_header(header);
87     FrameParts expected(header);
88     expected.SetOptWindowUpdateIncrement(fields.window_size_increment);
89     EXPECT_TRUE(DecodePayloadAndValidateSeveralWays(fb.buffer(), expected));
90   }
91 }
92 
93 }  // namespace
94 }  // namespace test
95 }  // namespace http2
96