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