QUICHE team | fd50a40 | 2018-12-07 22:54:05 -0500 | [diff] [blame] | 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 | |
QUICHE team | 5be974e | 2020-12-29 18:35:24 -0500 | [diff] [blame] | 5 | #include "http2/test_tools/frame_parts_collector.h" |
QUICHE team | fd50a40 | 2018-12-07 22:54:05 -0500 | [diff] [blame] | 6 | |
| 7 | #include <utility> |
| 8 | |
QUICHE team | 5be974e | 2020-12-29 18:35:24 -0500 | [diff] [blame] | 9 | #include "http2/http2_structures_test_util.h" |
| 10 | #include "http2/platform/api/http2_logging.h" |
| 11 | #include "common/platform/api/quiche_test.h" |
QUICHE team | fd50a40 | 2018-12-07 22:54:05 -0500 | [diff] [blame] | 12 | |
| 13 | namespace http2 { |
| 14 | namespace test { |
| 15 | |
| 16 | FramePartsCollector::FramePartsCollector() = default; |
| 17 | FramePartsCollector::~FramePartsCollector() = default; |
| 18 | |
| 19 | void FramePartsCollector::Reset() { |
| 20 | current_frame_.reset(); |
| 21 | collected_frames_.clear(); |
| 22 | expected_header_set_ = false; |
| 23 | } |
| 24 | |
| 25 | const FrameParts* FramePartsCollector::frame(size_t n) const { |
| 26 | if (n < size()) { |
| 27 | return collected_frames_.at(n).get(); |
| 28 | } |
vasilvv | afcc317 | 2021-02-02 12:01:07 -0800 | [diff] [blame] | 29 | QUICHE_CHECK(n == size()); |
QUICHE team | fd50a40 | 2018-12-07 22:54:05 -0500 | [diff] [blame] | 30 | return current_frame(); |
| 31 | } |
| 32 | |
| 33 | void FramePartsCollector::ExpectFrameHeader(const Http2FrameHeader& header) { |
| 34 | EXPECT_FALSE(IsInProgress()); |
| 35 | EXPECT_FALSE(expected_header_set_) |
| 36 | << "expected_header_: " << expected_header_; |
| 37 | expected_header_ = header; |
| 38 | expected_header_set_ = true; |
| 39 | // OnFrameHeader is called before the flags are scrubbed, but the other |
| 40 | // methods are called after, so scrub the invalid flags from expected_header_. |
| 41 | ScrubFlagsOfHeader(&expected_header_); |
| 42 | } |
| 43 | |
| 44 | void FramePartsCollector::TestExpectedHeader(const Http2FrameHeader& header) { |
| 45 | if (expected_header_set_) { |
| 46 | EXPECT_EQ(header, expected_header_); |
| 47 | expected_header_set_ = false; |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | Http2FrameDecoderListener* FramePartsCollector::StartFrame( |
| 52 | const Http2FrameHeader& header) { |
| 53 | TestExpectedHeader(header); |
| 54 | EXPECT_FALSE(IsInProgress()); |
| 55 | if (current_frame_ == nullptr) { |
bnc | 463f235 | 2019-10-10 04:49:34 -0700 | [diff] [blame] | 56 | current_frame_ = std::make_unique<FrameParts>(header); |
QUICHE team | fd50a40 | 2018-12-07 22:54:05 -0500 | [diff] [blame] | 57 | } |
| 58 | return current_frame(); |
| 59 | } |
| 60 | |
| 61 | Http2FrameDecoderListener* FramePartsCollector::StartAndEndFrame( |
| 62 | const Http2FrameHeader& header) { |
| 63 | TestExpectedHeader(header); |
| 64 | EXPECT_FALSE(IsInProgress()); |
| 65 | if (current_frame_ == nullptr) { |
bnc | 463f235 | 2019-10-10 04:49:34 -0700 | [diff] [blame] | 66 | current_frame_ = std::make_unique<FrameParts>(header); |
QUICHE team | fd50a40 | 2018-12-07 22:54:05 -0500 | [diff] [blame] | 67 | } |
| 68 | Http2FrameDecoderListener* result = current_frame(); |
| 69 | collected_frames_.push_back(std::move(current_frame_)); |
| 70 | return result; |
| 71 | } |
| 72 | |
| 73 | Http2FrameDecoderListener* FramePartsCollector::CurrentFrame() { |
| 74 | EXPECT_TRUE(IsInProgress()); |
| 75 | if (current_frame_ == nullptr) { |
| 76 | return &failing_listener_; |
| 77 | } |
| 78 | return current_frame(); |
| 79 | } |
| 80 | |
| 81 | Http2FrameDecoderListener* FramePartsCollector::EndFrame() { |
| 82 | EXPECT_TRUE(IsInProgress()); |
| 83 | if (current_frame_ == nullptr) { |
| 84 | return &failing_listener_; |
| 85 | } |
| 86 | Http2FrameDecoderListener* result = current_frame(); |
| 87 | collected_frames_.push_back(std::move(current_frame_)); |
| 88 | return result; |
| 89 | } |
| 90 | |
| 91 | Http2FrameDecoderListener* FramePartsCollector::FrameError( |
| 92 | const Http2FrameHeader& header) { |
| 93 | TestExpectedHeader(header); |
| 94 | if (current_frame_ == nullptr) { |
| 95 | // The decoder may detect an error before making any calls to the listener |
| 96 | // regarding the frame, in which case current_frame_==nullptr and we need |
| 97 | // to create a FrameParts instance. |
bnc | 463f235 | 2019-10-10 04:49:34 -0700 | [diff] [blame] | 98 | current_frame_ = std::make_unique<FrameParts>(header); |
QUICHE team | fd50a40 | 2018-12-07 22:54:05 -0500 | [diff] [blame] | 99 | } else { |
| 100 | // Similarly, the decoder may have made calls to the listener regarding the |
| 101 | // frame before detecting the error; for example, the DATA payload decoder |
| 102 | // calls OnDataStart before it can detect padding errors, hence before it |
| 103 | // can call OnPaddingTooLong. |
| 104 | EXPECT_EQ(header, current_frame_->GetFrameHeader()); |
| 105 | } |
| 106 | Http2FrameDecoderListener* result = current_frame(); |
| 107 | collected_frames_.push_back(std::move(current_frame_)); |
| 108 | return result; |
| 109 | } |
| 110 | |
| 111 | } // namespace test |
| 112 | } // namespace http2 |