blob: 8f74a9f4dd5ef0033d8bf1e4ca242b737f0b0eea [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#ifndef QUICHE_HTTP2_TEST_TOOLS_FRAME_PARTS_COLLECTOR_H_
6#define QUICHE_HTTP2_TEST_TOOLS_FRAME_PARTS_COLLECTOR_H_
7
8// FramePartsCollector is a base class for Http2FrameDecoderListener
9// implementations that create one FrameParts instance for each decoded frame.
10
11#include <stddef.h>
12
13#include <memory>
14#include <vector>
15
QUICHE team5be974e2020-12-29 18:35:24 -050016#include "http2/decoder/http2_frame_decoder_listener.h"
17#include "http2/decoder/http2_frame_decoder_listener_test_util.h"
18#include "http2/http2_structures.h"
19#include "http2/test_tools/frame_parts.h"
QUICHE teamfd50a402018-12-07 22:54:05 -050020
21namespace http2 {
22namespace test {
23
24class FramePartsCollector : public FailingHttp2FrameDecoderListener {
25 public:
26 FramePartsCollector();
27 ~FramePartsCollector() override;
28
29 // Toss out the collected data.
30 void Reset();
31
32 // Returns true if has started recording the info for a frame and has not yet
33 // finished doing so.
34 bool IsInProgress() const { return current_frame_ != nullptr; }
35
36 // Returns the FrameParts instance into which we're currently recording
37 // callback info if IsInProgress, else nullptr.
38 const FrameParts* current_frame() const { return current_frame_.get(); }
39
40 // Returns the number of completely collected FrameParts instances.
41 size_t size() const { return collected_frames_.size(); }
42
43 // Returns the n'th frame, where 0 is the oldest of the collected frames,
44 // and n==size() is the frame currently being collected, if there is one.
45 // Returns nullptr if the requested index is not valid.
46 const FrameParts* frame(size_t n) const;
47
48 protected:
49 // In support of OnFrameHeader, set the header that we expect to be used in
50 // the next call.
51 // TODO(jamessynge): Remove ExpectFrameHeader et al. once done with supporting
52 // SpdyFramer's exact states.
53 void ExpectFrameHeader(const Http2FrameHeader& header);
54
55 // For use in implementing On*Start methods of Http2FrameDecoderListener,
56 // returns a FrameParts instance, which will be newly created if
57 // IsInProgress==false (which the caller should ensure), else will be the
58 // current_frame(); never returns nullptr.
59 // If called when IsInProgress==true, a test failure will be recorded.
60 Http2FrameDecoderListener* StartFrame(const Http2FrameHeader& header);
61
62 // For use in implementing On* callbacks, such as OnPingAck, that are the only
63 // call expected for the frame being decoded; not for On*Start methods.
64 // Returns a FrameParts instance, which will be newly created if
65 // IsInProgress==false (which the caller should ensure), else will be the
66 // current_frame(); never returns nullptr.
67 // If called when IsInProgress==true, a test failure will be recorded.
68 Http2FrameDecoderListener* StartAndEndFrame(const Http2FrameHeader& header);
69
70 // If IsInProgress==true, returns the FrameParts into which the current
71 // frame is being recorded; else records a test failure and returns
72 // failing_listener_, which will record a test failure when any of its
73 // On* methods is called.
74 Http2FrameDecoderListener* CurrentFrame();
75
76 // For use in implementing On*End methods, pushes the current frame onto
77 // the vector of completed frames, and returns a pointer to it for recording
78 // the info in the final call. If IsInProgress==false, records a test failure
79 // and returns failing_listener_, which will record a test failure when any
80 // of its On* methods is called.
81 Http2FrameDecoderListener* EndFrame();
82
83 // For use in implementing OnPaddingTooLong and OnFrameSizeError, is
84 // equivalent to EndFrame() if IsInProgress==true, else equivalent to
85 // StartAndEndFrame().
86 Http2FrameDecoderListener* FrameError(const Http2FrameHeader& header);
87
88 private:
89 // Returns the mutable FrameParts instance into which we're currently
90 // recording callback info if IsInProgress, else nullptr.
91 FrameParts* current_frame() { return current_frame_.get(); }
92
93 // If expected header is set, verify that it matches the header param.
94 // TODO(jamessynge): Remove TestExpectedHeader et al. once done
95 // with supporting SpdyFramer's exact states.
96 void TestExpectedHeader(const Http2FrameHeader& header);
97
98 std::unique_ptr<FrameParts> current_frame_;
99 std::vector<std::unique_ptr<FrameParts>> collected_frames_;
100 FailingHttp2FrameDecoderListener failing_listener_;
101
102 // TODO(jamessynge): Remove expected_header_ et al. once done with supporting
103 // SpdyFramer's exact states.
104 Http2FrameHeader expected_header_;
105 bool expected_header_set_ = false;
106};
107
108} // namespace test
109} // namespace http2
110
111#endif // QUICHE_HTTP2_TEST_TOOLS_FRAME_PARTS_COLLECTOR_H_