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/data_payload_decoder.h" | |
6 | |
7 #include <stddef.h> | |
8 | |
9 #include <string> | |
10 | |
11 #include "base/logging.h" | |
12 #include "net/http2/decoder/frame_parts.h" | |
13 #include "net/http2/decoder/frame_parts_collector.h" | |
14 #include "net/http2/decoder/http2_frame_decoder_listener.h" | |
15 #include "net/http2/decoder/payload_decoders/payload_decoder_base_test_util.h" | |
16 #include "net/http2/http2_constants.h" | |
17 #include "net/http2/http2_structures.h" | |
18 #include "net/http2/http2_structures_test_util.h" | |
19 #include "net/http2/tools/failure.h" | |
20 #include "net/http2/tools/http2_frame_builder.h" | |
21 #include "net/http2/tools/http2_random.h" | |
22 #include "net/http2/tools/random_decoder_test.h" | |
23 #include "testing/gtest/include/gtest/gtest.h" | |
24 | |
25 using ::testing::AssertionResult; | |
26 using std::string; | |
27 | |
28 namespace net { | |
29 namespace test { | |
30 | |
31 // Provides friend access to an instance of the payload decoder, and also | |
32 // provides info to aid in testing. | |
33 class DataPayloadDecoderPeer { | |
34 public: | |
35 static constexpr Http2FrameType FrameType() { return Http2FrameType::DATA; } | |
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(DataPayloadDecoder* p, RandomBase* rng) { | |
44 VLOG(1) << "DataPayloadDecoderPeer::Randomize"; | |
45 CorruptEnum(&p->payload_state_, rng); | |
46 } | |
47 }; | |
48 | |
49 namespace { | |
50 | |
51 struct Listener : public FramePartsCollector { | |
52 void OnDataStart(const Http2FrameHeader& header) override { | |
53 VLOG(1) << "OnDataStart: " << header; | |
54 StartFrame(header)->OnDataStart(header); | |
55 } | |
56 | |
57 void OnDataPayload(const char* data, size_t len) override { | |
58 VLOG(1) << "OnDataPayload: len=" << len; | |
59 CurrentFrame()->OnDataPayload(data, len); | |
60 } | |
61 | |
62 void OnDataEnd() override { | |
63 VLOG(1) << "OnDataEnd"; | |
64 EndFrame()->OnDataEnd(); | |
65 } | |
66 | |
67 void OnPadLength(size_t pad_length) override { | |
68 VLOG(1) << "OnPadLength: " << pad_length; | |
69 CurrentFrame()->OnPadLength(pad_length); | |
70 } | |
71 | |
72 void OnPadding(const char* padding, size_t skipped_length) override { | |
73 VLOG(1) << "OnPadding: " << skipped_length; | |
74 CurrentFrame()->OnPadding(padding, skipped_length); | |
75 } | |
76 | |
77 void OnPaddingTooLong(const Http2FrameHeader& header, | |
78 size_t missing_length) override { | |
79 VLOG(1) << "OnPaddingTooLong: " << header | |
80 << " missing_length: " << missing_length; | |
81 EndFrame()->OnPaddingTooLong(header, missing_length); | |
82 } | |
83 }; | |
84 | |
85 class DataPayloadDecoderTest | |
86 : public AbstractPaddablePayloadDecoderTest<DataPayloadDecoder, | |
87 DataPayloadDecoderPeer, | |
88 Listener> { | |
89 protected: | |
90 AssertionResult CreateAndDecodeDataOfSize(size_t data_size) { | |
91 Reset(); | |
92 uint8_t flags = RandFlags(); | |
93 | |
94 string data_payload = Random().RandString(data_size); | |
95 frame_builder_.Append(data_payload); | |
96 MaybeAppendTrailingPadding(); | |
97 | |
98 Http2FrameHeader frame_header(frame_builder_.size(), Http2FrameType::DATA, | |
99 flags, RandStreamId()); | |
100 set_frame_header(frame_header); | |
101 ScrubFlagsOfHeader(&frame_header); | |
102 FrameParts expected(frame_header, data_payload, total_pad_length_); | |
103 VERIFY_AND_RETURN_SUCCESS( | |
104 DecodePayloadAndValidateSeveralWays(frame_builder_.buffer(), expected)); | |
105 } | |
106 }; | |
107 | |
108 INSTANTIATE_TEST_CASE_P(VariousPadLengths, | |
109 DataPayloadDecoderTest, | |
110 ::testing::Values(0, 1, 2, 3, 4, 254, 255, 256)); | |
111 | |
112 TEST_P(DataPayloadDecoderTest, VariousDataPayloadSizes) { | |
113 for (size_t data_size : {0, 1, 2, 3, 255, 256, 1024}) { | |
114 EXPECT_TRUE(CreateAndDecodeDataOfSize(data_size)); | |
115 } | |
116 } | |
117 | |
118 } // namespace | |
119 } // namespace test | |
120 } // namespace net | |
OLD | NEW |