1 // Copyright 2012 The Chromium Authors 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 NET_SPDY_SPDY_STREAM_TEST_UTIL_H_ 6 #define NET_SPDY_SPDY_STREAM_TEST_UTIL_H_ 7 8 #include <memory> 9 #include <string> 10 #include <string_view> 11 12 #include "base/compiler_specific.h" 13 #include "base/memory/scoped_refptr.h" 14 #include "net/base/io_buffer.h" 15 #include "net/base/load_timing_info.h" 16 #include "net/base/test_completion_callback.h" 17 #include "net/log/net_log_source.h" 18 #include "net/spdy/spdy_read_queue.h" 19 #include "net/spdy/spdy_stream.h" 20 #include "net/third_party/quiche/src/quiche/common/http/http_header_block.h" 21 22 namespace net::test { 23 24 // Delegate that calls Close() on |stream_| on OnClose. Used by tests 25 // to make sure that such an action is harmless. 26 class ClosingDelegate : public SpdyStream::Delegate { 27 public: 28 explicit ClosingDelegate(const base::WeakPtr<SpdyStream>& stream); 29 ~ClosingDelegate() override; 30 31 // SpdyStream::Delegate implementation. 32 void OnEarlyHintsReceived(const quiche::HttpHeaderBlock& headers) override; 33 void OnHeadersSent() override; 34 void OnHeadersReceived( 35 const quiche::HttpHeaderBlock& response_headers) override; 36 void OnDataReceived(std::unique_ptr<SpdyBuffer> buffer) override; 37 void OnDataSent() override; 38 void OnTrailers(const quiche::HttpHeaderBlock& trailers) override; 39 void OnClose(int status) override; 40 bool CanGreaseFrameType() const override; 41 NetLogSource source_dependency() const override; 42 43 // Returns whether or not the stream is closed. StreamIsClosed()44 bool StreamIsClosed() const { return !stream_.get(); } 45 46 private: 47 base::WeakPtr<SpdyStream> stream_; 48 }; 49 50 // Base class with shared functionality for test delegate 51 // implementations below. 52 class StreamDelegateBase : public SpdyStream::Delegate { 53 public: 54 explicit StreamDelegateBase(const base::WeakPtr<SpdyStream>& stream); 55 ~StreamDelegateBase() override; 56 57 void OnHeadersSent() override; 58 void OnEarlyHintsReceived(const quiche::HttpHeaderBlock& headers) override; 59 void OnHeadersReceived( 60 const quiche::HttpHeaderBlock& response_headers) override; 61 void OnDataReceived(std::unique_ptr<SpdyBuffer> buffer) override; 62 void OnDataSent() override; 63 void OnTrailers(const quiche::HttpHeaderBlock& trailers) override; 64 void OnClose(int status) override; 65 bool CanGreaseFrameType() const override; 66 NetLogSource source_dependency() const override; 67 68 // Waits for the stream to be closed and returns the status passed 69 // to OnClose(). 70 int WaitForClose(); 71 72 // Drains all data from the underlying read queue and returns it as 73 // a string. 74 std::string TakeReceivedData(); 75 76 // Returns whether or not the stream is closed. StreamIsClosed()77 bool StreamIsClosed() const { return !stream_.get(); } 78 79 // Returns the stream's ID. If called when the stream is closed, 80 // returns the stream's ID when it was open. stream_id()81 spdy::SpdyStreamId stream_id() const { return stream_id_; } 82 83 // Returns 103 Early Hints response headers. early_hints()84 const std::vector<quiche::HttpHeaderBlock>& early_hints() const { 85 return early_hints_; 86 } 87 88 std::string GetResponseHeaderValue(const std::string& name) const; send_headers_completed()89 bool send_headers_completed() const { return send_headers_completed_; } 90 91 // Returns the load timing info on the stream. This must be called after the 92 // stream is closed in order to get the up-to-date information. 93 const LoadTimingInfo& GetLoadTimingInfo(); 94 95 protected: stream()96 const base::WeakPtr<SpdyStream>& stream() { return stream_; } 97 98 private: 99 base::WeakPtr<SpdyStream> stream_; 100 spdy::SpdyStreamId stream_id_ = 0; 101 TestCompletionCallback callback_; 102 bool send_headers_completed_ = false; 103 std::vector<quiche::HttpHeaderBlock> early_hints_; 104 quiche::HttpHeaderBlock response_headers_; 105 SpdyReadQueue received_data_queue_; 106 LoadTimingInfo load_timing_info_; 107 }; 108 109 // Test delegate that does nothing. Used to capture data about the 110 // stream, e.g. its id when it was open. 111 class StreamDelegateDoNothing : public StreamDelegateBase { 112 public: 113 explicit StreamDelegateDoNothing(const base::WeakPtr<SpdyStream>& stream); 114 ~StreamDelegateDoNothing() override; 115 }; 116 117 // Test delegate that consumes data as it arrives. 118 class StreamDelegateConsumeData : public StreamDelegateBase { 119 public: 120 explicit StreamDelegateConsumeData(const base::WeakPtr<SpdyStream>& stream); 121 ~StreamDelegateConsumeData() override; 122 123 void OnDataReceived(std::unique_ptr<SpdyBuffer> buffer) override; 124 }; 125 126 // Test delegate that sends data immediately in OnHeadersReceived(). 127 class StreamDelegateSendImmediate : public StreamDelegateBase { 128 public: 129 // |data| can be NULL. 130 StreamDelegateSendImmediate(const base::WeakPtr<SpdyStream>& stream, 131 std::string_view data); 132 ~StreamDelegateSendImmediate() override; 133 134 void OnHeadersReceived( 135 const quiche::HttpHeaderBlock& response_headers) override; 136 137 private: 138 std::string_view data_; 139 }; 140 141 // Test delegate that sends body data. 142 class StreamDelegateWithBody : public StreamDelegateBase { 143 public: 144 StreamDelegateWithBody(const base::WeakPtr<SpdyStream>& stream, 145 std::string_view data); 146 ~StreamDelegateWithBody() override; 147 148 void OnHeadersSent() override; 149 150 private: 151 scoped_refptr<StringIOBuffer> buf_; 152 }; 153 154 // Test delegate that closes stream in OnHeadersReceived(). 155 class StreamDelegateCloseOnHeaders : public StreamDelegateBase { 156 public: 157 explicit StreamDelegateCloseOnHeaders( 158 const base::WeakPtr<SpdyStream>& stream); 159 ~StreamDelegateCloseOnHeaders() override; 160 161 void OnHeadersReceived( 162 const quiche::HttpHeaderBlock& response_headers) override; 163 }; 164 165 // Test delegate that sets a flag when EOF is detected. 166 class StreamDelegateDetectEOF : public StreamDelegateBase { 167 public: 168 explicit StreamDelegateDetectEOF(const base::WeakPtr<SpdyStream>& stream); 169 ~StreamDelegateDetectEOF() override; 170 171 void OnDataReceived(std::unique_ptr<SpdyBuffer> buffer) override; 172 eof_detected()173 bool eof_detected() const { return eof_detected_; } 174 175 private: 176 bool eof_detected_ = false; 177 }; 178 179 } // namespace net::test 180 181 #endif // NET_SPDY_SPDY_STREAM_TEST_UTIL_H_ 182