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/goaway_payload_decoder.h" | |
6 | |
7 #include <stddef.h> | |
8 | |
9 #include <string> | |
10 | |
11 #include "base/bind.h" | |
12 #include "base/logging.h" | |
13 #include "net/http2/decoder/frame_parts.h" | |
14 #include "net/http2/decoder/frame_parts_collector.h" | |
15 #include "net/http2/decoder/http2_frame_decoder_listener.h" | |
16 #include "net/http2/decoder/payload_decoders/payload_decoder_base_test_util.h" | |
17 #include "net/http2/http2_constants.h" | |
18 #include "net/http2/http2_structures_test_util.h" | |
19 #include "net/http2/tools/http2_frame_builder.h" | |
20 #include "net/http2/tools/http2_random.h" | |
21 #include "net/http2/tools/random_decoder_test.h" | |
22 #include "testing/gtest/include/gtest/gtest.h" | |
23 | |
24 using std::string; | |
25 | |
26 namespace net { | |
27 namespace test { | |
28 | |
29 class GoAwayPayloadDecoderPeer { | |
30 public: | |
31 static constexpr Http2FrameType FrameType() { return Http2FrameType::GOAWAY; } | |
32 | |
33 // Returns the mask of flags that affect the decoding of the payload (i.e. | |
34 // flags that that indicate the presence of certain fields or padding). | |
35 static constexpr uint8_t FlagsAffectingPayloadDecoding() { return 0; } | |
36 | |
37 static void Randomize(GoAwayPayloadDecoder* p, RandomBase* rng) { | |
38 CorruptEnum(&p->payload_state_, rng); | |
39 test::Randomize(&p->goaway_fields_, rng); | |
40 VLOG(1) << "GoAwayPayloadDecoderPeer::Randomize goaway_fields: " | |
41 << p->goaway_fields_; | |
42 } | |
43 }; | |
44 | |
45 namespace { | |
46 | |
47 struct Listener : public FramePartsCollector { | |
48 void OnGoAwayStart(const Http2FrameHeader& header, | |
49 const Http2GoAwayFields& goaway) override { | |
50 VLOG(1) << "OnGoAwayStart header: " << header << "; goaway: " << goaway; | |
51 StartFrame(header)->OnGoAwayStart(header, goaway); | |
52 } | |
53 | |
54 void OnGoAwayOpaqueData(const char* data, size_t len) override { | |
55 VLOG(1) << "OnGoAwayOpaqueData: len=" << len; | |
56 CurrentFrame()->OnGoAwayOpaqueData(data, len); | |
57 } | |
58 | |
59 void OnGoAwayEnd() override { | |
60 VLOG(1) << "OnGoAwayEnd"; | |
61 EndFrame()->OnGoAwayEnd(); | |
62 } | |
63 | |
64 void OnFrameSizeError(const Http2FrameHeader& header) override { | |
65 VLOG(1) << "OnFrameSizeError: " << header; | |
66 FrameError(header)->OnFrameSizeError(header); | |
67 } | |
68 }; | |
69 | |
70 class GoAwayPayloadDecoderTest | |
71 : public AbstractPayloadDecoderTest<GoAwayPayloadDecoder, | |
72 GoAwayPayloadDecoderPeer, | |
73 Listener> { | |
74 public: | |
75 static bool ApproveSizeForTruncated(size_t size) { | |
76 return size != Http2GoAwayFields::EncodedSize(); | |
77 } | |
78 }; | |
79 | |
80 // Confirm we get an error if the payload is not long enough to hold | |
81 // Http2GoAwayFields. | |
82 TEST_F(GoAwayPayloadDecoderTest, Truncated) { | |
83 Http2FrameBuilder fb; | |
84 fb.Append(Http2GoAwayFields(123, Http2ErrorCode::ENHANCE_YOUR_CALM)); | |
85 EXPECT_TRUE(VerifyDetectsFrameSizeError( | |
86 0, fb.buffer(), | |
87 base::Bind(&GoAwayPayloadDecoderTest::ApproveSizeForTruncated))); | |
88 } | |
89 | |
90 class GoAwayOpaqueDataLengthTests | |
91 : public GoAwayPayloadDecoderTest, | |
92 public ::testing::WithParamInterface<uint32_t> { | |
93 protected: | |
94 GoAwayOpaqueDataLengthTests() : length_(GetParam()) { | |
95 VLOG(1) << "################ length_=" << length_ << " ################"; | |
96 } | |
97 | |
98 const uint32_t length_; | |
99 }; | |
100 | |
101 INSTANTIATE_TEST_CASE_P(VariousLengths, | |
102 GoAwayOpaqueDataLengthTests, | |
103 ::testing::Values(0, 1, 2, 3, 4, 5, 6)); | |
104 | |
105 TEST_P(GoAwayOpaqueDataLengthTests, ValidLength) { | |
106 Http2GoAwayFields goaway; | |
107 Randomize(&goaway, RandomPtr()); | |
108 string opaque_data = Random().RandString(length_); | |
109 Http2FrameBuilder fb; | |
110 fb.Append(goaway); | |
111 fb.Append(opaque_data); | |
112 Http2FrameHeader header(fb.size(), Http2FrameType::GOAWAY, RandFlags(), | |
113 RandStreamId()); | |
114 set_frame_header(header); | |
115 FrameParts expected(header, opaque_data); | |
116 expected.opt_goaway = goaway; | |
117 ASSERT_TRUE(DecodePayloadAndValidateSeveralWays(fb.buffer(), expected)); | |
118 } | |
119 | |
120 } // namespace | |
121 } // namespace test | |
122 } // namespace net | |
OLD | NEW |