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/priority_payload_decoder.h" |
| 6 |
| 7 #include <stddef.h> |
| 8 |
| 9 #include "base/bind.h" |
| 10 #include "base/logging.h" |
| 11 #include "net/http2/decoder/frame_parts.h" |
| 12 #include "net/http2/decoder/frame_parts_collector.h" |
| 13 #include "net/http2/decoder/http2_frame_decoder_listener.h" |
| 14 #include "net/http2/decoder/payload_decoders/payload_decoder_base_test_util.h" |
| 15 #include "net/http2/http2_constants.h" |
| 16 #include "net/http2/http2_structures_test_util.h" |
| 17 #include "net/http2/tools/http2_frame_builder.h" |
| 18 #include "net/http2/tools/http2_random.h" |
| 19 #include "net/http2/tools/random_decoder_test.h" |
| 20 #include "testing/gtest/include/gtest/gtest.h" |
| 21 |
| 22 namespace net { |
| 23 namespace test { |
| 24 |
| 25 class PriorityPayloadDecoderPeer { |
| 26 public: |
| 27 static constexpr Http2FrameType FrameType() { |
| 28 return Http2FrameType::PRIORITY; |
| 29 } |
| 30 |
| 31 // Returns the mask of flags that affect the decoding of the payload (i.e. |
| 32 // flags that that indicate the presence of certain fields or padding). |
| 33 static constexpr uint8_t FlagsAffectingPayloadDecoding() { return 0; } |
| 34 |
| 35 static void Randomize(PriorityPayloadDecoder* p, RandomBase* rng) { |
| 36 VLOG(1) << "PriorityPayloadDecoderPeer::Randomize"; |
| 37 test::Randomize(&p->priority_fields_, rng); |
| 38 } |
| 39 }; |
| 40 |
| 41 namespace { |
| 42 |
| 43 struct Listener : public FramePartsCollector { |
| 44 void OnPriorityFrame(const Http2FrameHeader& header, |
| 45 const Http2PriorityFields& priority_fields) override { |
| 46 VLOG(1) << "OnPriority: " << header << "; " << priority_fields; |
| 47 StartAndEndFrame(header)->OnPriorityFrame(header, priority_fields); |
| 48 } |
| 49 |
| 50 void OnFrameSizeError(const Http2FrameHeader& header) override { |
| 51 VLOG(1) << "OnFrameSizeError: " << header; |
| 52 FrameError(header)->OnFrameSizeError(header); |
| 53 } |
| 54 }; |
| 55 |
| 56 class PriorityPayloadDecoderTest |
| 57 : public AbstractPayloadDecoderTest<PriorityPayloadDecoder, |
| 58 PriorityPayloadDecoderPeer, |
| 59 Listener> { |
| 60 public: |
| 61 static bool ApproveSizeForWrongSize(size_t size) { |
| 62 return size != Http2PriorityFields::EncodedSize(); |
| 63 } |
| 64 |
| 65 protected: |
| 66 Http2PriorityFields RandPriorityFields() { |
| 67 Http2PriorityFields fields; |
| 68 test::Randomize(&fields, RandomPtr()); |
| 69 return fields; |
| 70 } |
| 71 }; |
| 72 |
| 73 // Confirm we get an error if the payload is not the correct size to hold |
| 74 // exactly one Http2PriorityFields. |
| 75 TEST_F(PriorityPayloadDecoderTest, WrongSize) { |
| 76 Http2FrameBuilder fb; |
| 77 fb.Append(RandPriorityFields()); |
| 78 fb.Append(RandPriorityFields()); |
| 79 EXPECT_TRUE(VerifyDetectsFrameSizeError( |
| 80 0, fb.buffer(), |
| 81 base::Bind(&PriorityPayloadDecoderTest::ApproveSizeForWrongSize))); |
| 82 } |
| 83 |
| 84 TEST_F(PriorityPayloadDecoderTest, VariousPayloads) { |
| 85 for (int n = 0; n < 100; ++n) { |
| 86 Http2PriorityFields fields = RandPriorityFields(); |
| 87 Http2FrameBuilder fb; |
| 88 fb.Append(fields); |
| 89 Http2FrameHeader header(fb.size(), Http2FrameType::PRIORITY, RandFlags(), |
| 90 RandStreamId()); |
| 91 set_frame_header(header); |
| 92 FrameParts expected(header); |
| 93 expected.opt_priority = fields; |
| 94 EXPECT_TRUE(DecodePayloadAndValidateSeveralWays(fb.buffer(), expected)); |
| 95 } |
| 96 } |
| 97 |
| 98 } // namespace |
| 99 } // namespace test |
| 100 } // namespace net |
OLD | NEW |