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/push_promise_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 // Provides friend access to an instance of the payload decoder, and also |
| 30 // provides info to aid in testing. |
| 31 class PushPromisePayloadDecoderPeer { |
| 32 public: |
| 33 static constexpr Http2FrameType FrameType() { |
| 34 return Http2FrameType::PUSH_PROMISE; |
| 35 } |
| 36 |
| 37 // Returns the mask of flags that affect the decoding of the payload (i.e. |
| 38 // flags that that indicate the presence of certain fields or padding). |
| 39 static constexpr uint8_t FlagsAffectingPayloadDecoding() { |
| 40 return Http2FrameFlag::FLAG_PADDED; |
| 41 } |
| 42 |
| 43 static void Randomize(PushPromisePayloadDecoder* p, RandomBase* rng) { |
| 44 VLOG(1) << "PushPromisePayloadDecoderPeer::Randomize"; |
| 45 CorruptEnum(&p->payload_state_, rng); |
| 46 test::Randomize(&p->push_promise_fields_, rng); |
| 47 } |
| 48 }; |
| 49 |
| 50 namespace { |
| 51 |
| 52 // Listener listens for only those methods expected by the payload decoder |
| 53 // under test, and forwards them onto the FrameParts instance for the current |
| 54 // frame. |
| 55 struct Listener : public FramePartsCollector { |
| 56 void OnPushPromiseStart(const Http2FrameHeader& header, |
| 57 const Http2PushPromiseFields& promise, |
| 58 size_t total_padding_length) override { |
| 59 VLOG(1) << "OnPushPromiseStart header: " << header |
| 60 << " promise: " << promise |
| 61 << " total_padding_length: " << total_padding_length; |
| 62 EXPECT_EQ(Http2FrameType::PUSH_PROMISE, header.type); |
| 63 StartFrame(header)->OnPushPromiseStart(header, promise, |
| 64 total_padding_length); |
| 65 } |
| 66 |
| 67 void OnHpackFragment(const char* data, size_t len) override { |
| 68 VLOG(1) << "OnHpackFragment: len=" << len; |
| 69 CurrentFrame()->OnHpackFragment(data, len); |
| 70 } |
| 71 |
| 72 void OnPushPromiseEnd() override { |
| 73 VLOG(1) << "OnPushPromiseEnd"; |
| 74 EndFrame()->OnPushPromiseEnd(); |
| 75 } |
| 76 |
| 77 void OnPadding(const char* padding, size_t skipped_length) override { |
| 78 VLOG(1) << "OnPadding: " << skipped_length; |
| 79 CurrentFrame()->OnPadding(padding, skipped_length); |
| 80 } |
| 81 |
| 82 void OnPaddingTooLong(const Http2FrameHeader& header, |
| 83 size_t missing_length) override { |
| 84 VLOG(1) << "OnPaddingTooLong: " << header |
| 85 << "; missing_length: " << missing_length; |
| 86 FrameError(header)->OnPaddingTooLong(header, missing_length); |
| 87 } |
| 88 |
| 89 void OnFrameSizeError(const Http2FrameHeader& header) override { |
| 90 VLOG(1) << "OnFrameSizeError: " << header; |
| 91 FrameError(header)->OnFrameSizeError(header); |
| 92 } |
| 93 }; |
| 94 |
| 95 class PushPromisePayloadDecoderTest |
| 96 : public AbstractPaddablePayloadDecoderTest<PushPromisePayloadDecoder, |
| 97 PushPromisePayloadDecoderPeer, |
| 98 Listener> { |
| 99 public: |
| 100 static bool ApproveSizeForTruncated(size_t size) { |
| 101 return size != Http2PushPromiseFields::EncodedSize(); |
| 102 } |
| 103 }; |
| 104 |
| 105 INSTANTIATE_TEST_CASE_P(VariousPadLengths, |
| 106 PushPromisePayloadDecoderTest, |
| 107 ::testing::Values(0, 1, 2, 3, 4, 254, 255, 256)); |
| 108 |
| 109 // Payload contains the required Http2PushPromiseFields, followed by some |
| 110 // (fake) HPACK payload. |
| 111 TEST_P(PushPromisePayloadDecoderTest, VariousHpackPayloadSizes) { |
| 112 for (size_t hpack_size : {0, 1, 2, 3, 255, 256, 1024}) { |
| 113 LOG(INFO) << "########### hpack_size = " << hpack_size << " ###########"; |
| 114 Reset(); |
| 115 string hpack_payload = Random().RandString(hpack_size); |
| 116 Http2PushPromiseFields push_promise{RandStreamId()}; |
| 117 frame_builder_.Append(push_promise); |
| 118 frame_builder_.Append(hpack_payload); |
| 119 MaybeAppendTrailingPadding(); |
| 120 Http2FrameHeader frame_header(frame_builder_.size(), |
| 121 Http2FrameType::PUSH_PROMISE, RandFlags(), |
| 122 RandStreamId()); |
| 123 set_frame_header(frame_header); |
| 124 FrameParts expected(frame_header, hpack_payload, total_pad_length_); |
| 125 expected.opt_push_promise = push_promise; |
| 126 EXPECT_TRUE( |
| 127 DecodePayloadAndValidateSeveralWays(frame_builder_.buffer(), expected)); |
| 128 } |
| 129 } |
| 130 |
| 131 // Confirm we get an error if the payload is not long enough for the required |
| 132 // portion of the payload, regardless of the amount of (valid) padding. |
| 133 TEST_P(PushPromisePayloadDecoderTest, Truncated) { |
| 134 Http2PushPromiseFields push_promise{RandStreamId()}; |
| 135 Http2FrameBuilder fb; |
| 136 fb.Append(push_promise); |
| 137 EXPECT_TRUE(VerifyDetectsMultipleFrameSizeErrors( |
| 138 0, fb.buffer(), |
| 139 base::Bind(&PushPromisePayloadDecoderTest::ApproveSizeForTruncated), |
| 140 total_pad_length_)); |
| 141 } |
| 142 |
| 143 // Confirm we get an error if the PADDED flag is set but the payload is not |
| 144 // long enough to hold even the Pad Length amount of padding. |
| 145 TEST_P(PushPromisePayloadDecoderTest, PaddingTooLong) { |
| 146 EXPECT_TRUE(VerifyDetectsPaddingTooLong()); |
| 147 } |
| 148 |
| 149 } // namespace |
| 150 } // namespace test |
| 151 } // namespace net |
OLD | NEW |