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/altsvc_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 AltSvcPayloadDecoderPeer { |
| 32 public: |
| 33 static constexpr Http2FrameType FrameType() { return Http2FrameType::ALTSVC; } |
| 34 |
| 35 // Returns the mask of flags that affect the decoding of the payload (i.e. |
| 36 // flags that that indicate the presence of certain fields or padding). |
| 37 static constexpr uint8_t FlagsAffectingPayloadDecoding() { return 0; } |
| 38 |
| 39 static void Randomize(AltSvcPayloadDecoder* p, RandomBase* rng) { |
| 40 CorruptEnum(&p->payload_state_, rng); |
| 41 test::Randomize(&p->altsvc_fields_, rng); |
| 42 VLOG(1) << "AltSvcPayloadDecoderPeer::Randomize altsvc_fields_=" |
| 43 << p->altsvc_fields_; |
| 44 } |
| 45 }; |
| 46 |
| 47 namespace { |
| 48 |
| 49 struct Listener : public FramePartsCollector { |
| 50 void OnAltSvcStart(const Http2FrameHeader& header, |
| 51 size_t origin_length, |
| 52 size_t value_length) override { |
| 53 VLOG(1) << "OnAltSvcStart header: " << header |
| 54 << "; origin_length=" << origin_length |
| 55 << "; value_length=" << value_length; |
| 56 StartFrame(header)->OnAltSvcStart(header, origin_length, value_length); |
| 57 } |
| 58 |
| 59 void OnAltSvcOriginData(const char* data, size_t len) override { |
| 60 VLOG(1) << "OnAltSvcOriginData: len=" << len; |
| 61 CurrentFrame()->OnAltSvcOriginData(data, len); |
| 62 } |
| 63 |
| 64 void OnAltSvcValueData(const char* data, size_t len) override { |
| 65 VLOG(1) << "OnAltSvcValueData: len=" << len; |
| 66 CurrentFrame()->OnAltSvcValueData(data, len); |
| 67 } |
| 68 |
| 69 void OnAltSvcEnd() override { |
| 70 VLOG(1) << "OnAltSvcEnd"; |
| 71 EndFrame()->OnAltSvcEnd(); |
| 72 } |
| 73 |
| 74 void OnFrameSizeError(const Http2FrameHeader& header) override { |
| 75 VLOG(1) << "OnFrameSizeError: " << header; |
| 76 FrameError(header)->OnFrameSizeError(header); |
| 77 } |
| 78 }; |
| 79 |
| 80 class AltSvcPayloadDecoderTest |
| 81 : public AbstractPayloadDecoderTest<AltSvcPayloadDecoder, |
| 82 AltSvcPayloadDecoderPeer, |
| 83 Listener> {}; |
| 84 |
| 85 // Confirm we get an error if the payload is not long enough to hold |
| 86 // Http2AltSvcFields and the indicated length of origin. |
| 87 TEST_F(AltSvcPayloadDecoderTest, Truncated) { |
| 88 Http2FrameBuilder fb; |
| 89 fb.Append(Http2AltSvcFields{0xffff}); // The longest possible origin length. |
| 90 fb.Append("Too little origin!"); |
| 91 EXPECT_TRUE(VerifyDetectsFrameSizeError( |
| 92 0, fb.buffer(), |
| 93 base::Bind(&AbstractPayloadDecoderTest::SucceedingApproveSize))); |
| 94 } |
| 95 |
| 96 class AltSvcPayloadLengthTests : public AltSvcPayloadDecoderTest, |
| 97 public ::testing::WithParamInterface< |
| 98 ::testing::tuple<uint16_t, uint32_t>> { |
| 99 protected: |
| 100 AltSvcPayloadLengthTests() |
| 101 : origin_length_(::testing::get<0>(GetParam())), |
| 102 value_length_(::testing::get<1>(GetParam())) { |
| 103 VLOG(1) << "################ origin_length_=" << origin_length_ |
| 104 << " value_length_=" << value_length_ << " ################"; |
| 105 } |
| 106 |
| 107 const uint16_t origin_length_; |
| 108 const uint32_t value_length_; |
| 109 }; |
| 110 |
| 111 INSTANTIATE_TEST_CASE_P(VariousOriginAndValueLengths, |
| 112 AltSvcPayloadLengthTests, |
| 113 ::testing::Combine(::testing::Values(0, 1, 3, 65535), |
| 114 ::testing::Values(0, 1, 3, 65537))); |
| 115 |
| 116 TEST_P(AltSvcPayloadLengthTests, ValidOriginAndValueLength) { |
| 117 string origin = Random().RandString(origin_length_); |
| 118 string value = Random().RandString(value_length_); |
| 119 Http2FrameBuilder fb; |
| 120 fb.Append(Http2AltSvcFields{origin_length_}); |
| 121 fb.Append(origin); |
| 122 fb.Append(value); |
| 123 Http2FrameHeader header(fb.size(), Http2FrameType::ALTSVC, RandFlags(), |
| 124 RandStreamId()); |
| 125 set_frame_header(header); |
| 126 FrameParts expected(header); |
| 127 expected.SetAltSvcExpected(origin, value); |
| 128 ASSERT_TRUE(DecodePayloadAndValidateSeveralWays(fb.buffer(), expected)); |
| 129 } |
| 130 |
| 131 } // namespace |
| 132 } // namespace test |
| 133 } // namespace net |
OLD | NEW |