blob: cb0e32b228796460885feb9a8ff281803308b6ef [file] [log] [blame]
QUICHE teamfd50a402018-12-07 22:54:05 -05001// 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 teamfd50a402018-12-07 22:54:05 -05009#include "testing/gtest/include/gtest/gtest.h"
10#include "net/third_party/quiche/src/http2/http2_structures_test_util.h"
QUICHE team61940b42019-03-07 23:32:27 -050011#include "net/third_party/quiche/src/http2/platform/api/http2_logging.h"
QUICHE teamfd50a402018-12-07 22:54:05 -050012#include "net/third_party/quiche/src/http2/platform/api/http2_ptr_util.h"
13
14namespace http2 {
15namespace test {
16
17FramePartsCollector::FramePartsCollector() = default;
18FramePartsCollector::~FramePartsCollector() = default;
19
20void FramePartsCollector::Reset() {
21 current_frame_.reset();
22 collected_frames_.clear();
23 expected_header_set_ = false;
24}
25
26const 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
34void 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
45void FramePartsCollector::TestExpectedHeader(const Http2FrameHeader& header) {
46 if (expected_header_set_) {
47 EXPECT_EQ(header, expected_header_);
48 expected_header_set_ = false;
49 }
50}
51
52Http2FrameDecoderListener* 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
62Http2FrameDecoderListener* 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
74Http2FrameDecoderListener* FramePartsCollector::CurrentFrame() {
75 EXPECT_TRUE(IsInProgress());
76 if (current_frame_ == nullptr) {
77 return &failing_listener_;
78 }
79 return current_frame();
80}
81
82Http2FrameDecoderListener* 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
92Http2FrameDecoderListener* 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