xunjieli | 707f895 | 2016-06-06 15:22:06 | [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 | |
Bence Béky | 94658bf | 2018-05-11 19:22:58 | [diff] [blame] | 5 | #include "net/spdy/bidirectional_stream_spdy_impl.h" |
xunjieli | 707f895 | 2016-06-06 15:22:06 | [diff] [blame] | 6 | |
Bence Béky | 4e83f49 | 2018-05-13 23:14:25 | [diff] [blame] | 7 | #include <string> |
| 8 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 9 | #include "base/containers/span.h" |
xunjieli | 707f895 | 2016-06-06 15:22:06 | [diff] [blame] | 10 | #include "base/run_loop.h" |
Avi Drissman | 4365a478 | 2018-12-28 19:26:24 | [diff] [blame] | 11 | #include "base/stl_util.h" |
xunjieli | 707f895 | 2016-06-06 15:22:06 | [diff] [blame] | 12 | #include "base/strings/string_number_conversions.h" |
Bence Béky | d74f438 | 2018-02-20 18:26:19 | [diff] [blame] | 13 | #include "base/strings/string_piece.h" |
xunjieli | 707f895 | 2016-06-06 15:22:06 | [diff] [blame] | 14 | #include "base/time/time.h" |
| 15 | #include "base/timer/mock_timer.h" |
Sebastien Marchand | efda77e53 | 2019-01-25 22:53:52 | [diff] [blame] | 16 | #include "base/timer/timer.h" |
xunjieli | 6d0b944d | 2016-09-21 01:53:57 | [diff] [blame] | 17 | #include "net/base/load_timing_info.h" |
| 18 | #include "net/base/load_timing_info_test_util.h" |
xunjieli | 707f895 | 2016-06-06 15:22:06 | [diff] [blame] | 19 | #include "net/base/net_errors.h" |
xunjieli | 707f895 | 2016-06-06 15:22:06 | [diff] [blame] | 20 | #include "net/http/http_request_info.h" |
| 21 | #include "net/http/http_response_headers.h" |
| 22 | #include "net/http/http_response_info.h" |
xunjieli | 707f895 | 2016-06-06 15:22:06 | [diff] [blame] | 23 | #include "net/log/test_net_log.h" |
Paul Jensen | a457017a | 2018-01-19 23:52:04 | [diff] [blame] | 24 | #include "net/socket/socket_tag.h" |
xunjieli | 707f895 | 2016-06-06 15:22:06 | [diff] [blame] | 25 | #include "net/socket/socket_test_util.h" |
Bence Béky | 94658bf | 2018-05-11 19:22:58 | [diff] [blame] | 26 | #include "net/spdy/spdy_session.h" |
| 27 | #include "net/spdy/spdy_test_util_common.h" |
xunjieli | 707f895 | 2016-06-06 15:22:06 | [diff] [blame] | 28 | #include "net/test/cert_test_util.h" |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 29 | #include "net/test/gtest_util.h" |
rsleevi | a69c79a | 2016-06-22 03:28:43 | [diff] [blame] | 30 | #include "net/test/test_data_directory.h" |
Bence Béky | 98447b1 | 2018-05-08 03:14:01 | [diff] [blame] | 31 | #include "net/test/test_with_scoped_task_environment.h" |
Ramin Halavati | 3c96c6d | 2018-03-11 13:29:44 | [diff] [blame] | 32 | #include "net/traffic_annotation/network_traffic_annotation_test_helper.h" |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 33 | #include "testing/gmock/include/gmock/gmock.h" |
xunjieli | 707f895 | 2016-06-06 15:22:06 | [diff] [blame] | 34 | #include "testing/gtest/include/gtest/gtest.h" |
| 35 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 36 | using net::test::IsError; |
| 37 | using net::test::IsOk; |
| 38 | |
xunjieli | 707f895 | 2016-06-06 15:22:06 | [diff] [blame] | 39 | namespace net { |
| 40 | |
| 41 | namespace { |
| 42 | |
| 43 | const char kBodyData[] = "Body data"; |
Avi Drissman | 4365a478 | 2018-12-28 19:26:24 | [diff] [blame] | 44 | const size_t kBodyDataSize = base::size(kBodyData); |
xunjieli | 707f895 | 2016-06-06 15:22:06 | [diff] [blame] | 45 | // Size of the buffer to be allocated for each read. |
| 46 | const size_t kReadBufferSize = 4096; |
| 47 | |
xunjieli | 6d0b944d | 2016-09-21 01:53:57 | [diff] [blame] | 48 | // Tests the load timing of a stream that's connected and is not the first |
| 49 | // request sent on a connection. |
| 50 | void TestLoadTimingReused(const LoadTimingInfo& load_timing_info) { |
| 51 | EXPECT_TRUE(load_timing_info.socket_reused); |
mikecirone | f22f981 | 2016-10-04 03:40:19 | [diff] [blame] | 52 | EXPECT_NE(NetLogSource::kInvalidId, load_timing_info.socket_log_id); |
xunjieli | 6d0b944d | 2016-09-21 01:53:57 | [diff] [blame] | 53 | |
| 54 | ExpectConnectTimingHasNoTimes(load_timing_info.connect_timing); |
| 55 | ExpectLoadTimingHasOnlyConnectionTimes(load_timing_info); |
| 56 | } |
| 57 | |
| 58 | // Tests the load timing of a stream that's connected and using a fresh |
| 59 | // connection. |
| 60 | void TestLoadTimingNotReused(const LoadTimingInfo& load_timing_info) { |
| 61 | EXPECT_FALSE(load_timing_info.socket_reused); |
mikecirone | f22f981 | 2016-10-04 03:40:19 | [diff] [blame] | 62 | EXPECT_NE(NetLogSource::kInvalidId, load_timing_info.socket_log_id); |
xunjieli | 6d0b944d | 2016-09-21 01:53:57 | [diff] [blame] | 63 | |
| 64 | ExpectConnectTimingHasTimes( |
| 65 | load_timing_info.connect_timing, |
| 66 | CONNECT_TIMING_HAS_SSL_TIMES | CONNECT_TIMING_HAS_DNS_TIMES); |
| 67 | ExpectLoadTimingHasOnlyConnectionTimes(load_timing_info); |
| 68 | } |
| 69 | |
xunjieli | 707f895 | 2016-06-06 15:22:06 | [diff] [blame] | 70 | class TestDelegateBase : public BidirectionalStreamImpl::Delegate { |
| 71 | public: |
| 72 | TestDelegateBase(base::WeakPtr<SpdySession> session, |
| 73 | IOBuffer* read_buf, |
| 74 | int read_buf_len) |
Jeremy Roman | 0579ed6 | 2017-08-29 15:56:19 | [diff] [blame] | 75 | : stream_(std::make_unique<BidirectionalStreamSpdyImpl>(session, |
bnc | 3f6a855 | 2017-05-17 13:40:34 | [diff] [blame] | 76 | NetLogSource())), |
xunjieli | 707f895 | 2016-06-06 15:22:06 | [diff] [blame] | 77 | read_buf_(read_buf), |
| 78 | read_buf_len_(read_buf_len), |
| 79 | loop_(nullptr), |
| 80 | error_(OK), |
| 81 | bytes_read_(0), |
| 82 | on_data_read_count_(0), |
| 83 | on_data_sent_count_(0), |
| 84 | do_not_start_read_(false), |
| 85 | run_until_completion_(false), |
| 86 | not_expect_callback_(false), |
| 87 | on_failed_called_(false) {} |
| 88 | |
Chris Watkins | 61914cb | 2017-12-01 19:59:00 | [diff] [blame] | 89 | ~TestDelegateBase() override = default; |
xunjieli | 707f895 | 2016-06-06 15:22:06 | [diff] [blame] | 90 | |
| 91 | void OnStreamReady(bool request_headers_sent) override { |
| 92 | CHECK(!on_failed_called_); |
| 93 | } |
| 94 | |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 95 | void OnHeadersReceived( |
| 96 | const spdy::SpdyHeaderBlock& response_headers) override { |
xunjieli | 707f895 | 2016-06-06 15:22:06 | [diff] [blame] | 97 | CHECK(!on_failed_called_); |
| 98 | CHECK(!not_expect_callback_); |
bnc | 94893a7 | 2016-06-30 13:45:25 | [diff] [blame] | 99 | response_headers_ = response_headers.Clone(); |
xunjieli | 707f895 | 2016-06-06 15:22:06 | [diff] [blame] | 100 | if (!do_not_start_read_) |
| 101 | StartOrContinueReading(); |
| 102 | } |
| 103 | |
| 104 | void OnDataRead(int bytes_read) override { |
| 105 | CHECK(!on_failed_called_); |
| 106 | CHECK(!not_expect_callback_); |
| 107 | on_data_read_count_++; |
| 108 | CHECK_GE(bytes_read, OK); |
| 109 | bytes_read_ += bytes_read; |
| 110 | data_received_.append(read_buf_->data(), bytes_read); |
| 111 | if (!do_not_start_read_) |
| 112 | StartOrContinueReading(); |
| 113 | } |
| 114 | |
| 115 | void OnDataSent() override { |
| 116 | CHECK(!on_failed_called_); |
| 117 | CHECK(!not_expect_callback_); |
| 118 | on_data_sent_count_++; |
| 119 | } |
| 120 | |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 121 | void OnTrailersReceived(const spdy::SpdyHeaderBlock& trailers) override { |
xunjieli | 707f895 | 2016-06-06 15:22:06 | [diff] [blame] | 122 | CHECK(!on_failed_called_); |
bnc | 94893a7 | 2016-06-30 13:45:25 | [diff] [blame] | 123 | trailers_ = trailers.Clone(); |
xunjieli | 707f895 | 2016-06-06 15:22:06 | [diff] [blame] | 124 | if (run_until_completion_) |
| 125 | loop_->Quit(); |
| 126 | } |
| 127 | |
| 128 | void OnFailed(int error) override { |
| 129 | CHECK(!on_failed_called_); |
| 130 | CHECK(!not_expect_callback_); |
| 131 | CHECK_NE(OK, error); |
| 132 | error_ = error; |
| 133 | on_failed_called_ = true; |
| 134 | if (run_until_completion_) |
| 135 | loop_->Quit(); |
| 136 | } |
| 137 | |
| 138 | void Start(const BidirectionalStreamRequestInfo* request, |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 139 | const NetLogWithSource& net_log) { |
xunjieli | 707f895 | 2016-06-06 15:22:06 | [diff] [blame] | 140 | stream_->Start(request, net_log, |
| 141 | /*send_request_headers_automatically=*/false, this, |
tzik | 08d8d6e | 2018-07-09 04:11:47 | [diff] [blame] | 142 | std::make_unique<base::OneShotTimer>(), |
Ramin Halavati | 3c96c6d | 2018-03-11 13:29:44 | [diff] [blame] | 143 | TRAFFIC_ANNOTATION_FOR_TESTS); |
xunjieli | 707f895 | 2016-06-06 15:22:06 | [diff] [blame] | 144 | not_expect_callback_ = false; |
| 145 | } |
| 146 | |
| 147 | void SendData(IOBuffer* data, int length, bool end_of_stream) { |
rch | ad39988f | 2017-06-02 05:34:32 | [diff] [blame] | 148 | SendvData({data}, {length}, end_of_stream); |
xunjieli | 707f895 | 2016-06-06 15:22:06 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | void SendvData(const std::vector<scoped_refptr<IOBuffer>>& data, |
| 152 | const std::vector<int>& length, |
| 153 | bool end_of_stream) { |
| 154 | not_expect_callback_ = true; |
| 155 | stream_->SendvData(data, length, end_of_stream); |
| 156 | not_expect_callback_ = false; |
| 157 | } |
| 158 | |
| 159 | // Sets whether the delegate should wait until the completion of the stream. |
| 160 | void SetRunUntilCompletion(bool run_until_completion) { |
| 161 | run_until_completion_ = run_until_completion; |
Jeremy Roman | 0579ed6 | 2017-08-29 15:56:19 | [diff] [blame] | 162 | loop_ = std::make_unique<base::RunLoop>(); |
xunjieli | 707f895 | 2016-06-06 15:22:06 | [diff] [blame] | 163 | } |
| 164 | |
xunjieli | 6d0b944d | 2016-09-21 01:53:57 | [diff] [blame] | 165 | // Wait until the stream reaches completion. |
| 166 | void WaitUntilCompletion() { loop_->Run(); } |
| 167 | |
xunjieli | 707f895 | 2016-06-06 15:22:06 | [diff] [blame] | 168 | // Starts or continues read data from |stream_| until there is no more |
| 169 | // byte can be read synchronously. |
| 170 | void StartOrContinueReading() { |
| 171 | int rv = ReadData(); |
| 172 | while (rv > 0) { |
| 173 | rv = ReadData(); |
| 174 | } |
| 175 | if (run_until_completion_ && rv == 0) |
| 176 | loop_->Quit(); |
| 177 | } |
| 178 | |
| 179 | // Calls ReadData on the |stream_| and updates internal states. |
| 180 | int ReadData() { |
| 181 | int rv = stream_->ReadData(read_buf_.get(), read_buf_len_); |
| 182 | if (rv > 0) { |
| 183 | data_received_.append(read_buf_->data(), rv); |
| 184 | bytes_read_ += rv; |
| 185 | } |
| 186 | return rv; |
| 187 | } |
| 188 | |
| 189 | NextProto GetProtocol() const { return stream_->GetProtocol(); } |
| 190 | |
| 191 | int64_t GetTotalReceivedBytes() const { |
xunjieli | 9ff75c56 | 2016-08-10 20:26:16 | [diff] [blame] | 192 | return stream_->GetTotalReceivedBytes(); |
xunjieli | 707f895 | 2016-06-06 15:22:06 | [diff] [blame] | 193 | } |
| 194 | |
xunjieli | 9ff75c56 | 2016-08-10 20:26:16 | [diff] [blame] | 195 | int64_t GetTotalSentBytes() const { |
xunjieli | 9ff75c56 | 2016-08-10 20:26:16 | [diff] [blame] | 196 | return stream_->GetTotalSentBytes(); |
xunjieli | 6d0b944d | 2016-09-21 01:53:57 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | bool GetLoadTimingInfo(LoadTimingInfo* load_timing_info) const { |
| 200 | return stream_->GetLoadTimingInfo(load_timing_info); |
xunjieli | 9ff75c56 | 2016-08-10 20:26:16 | [diff] [blame] | 201 | } |
xunjieli | 707f895 | 2016-06-06 15:22:06 | [diff] [blame] | 202 | |
| 203 | // Const getters for internal states. |
Bence Béky | 4e83f49 | 2018-05-13 23:14:25 | [diff] [blame] | 204 | const std::string& data_received() const { return data_received_; } |
xunjieli | 707f895 | 2016-06-06 15:22:06 | [diff] [blame] | 205 | int bytes_read() const { return bytes_read_; } |
| 206 | int error() const { return error_; } |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 207 | const spdy::SpdyHeaderBlock& response_headers() const { |
| 208 | return response_headers_; |
| 209 | } |
| 210 | const spdy::SpdyHeaderBlock& trailers() const { return trailers_; } |
xunjieli | 707f895 | 2016-06-06 15:22:06 | [diff] [blame] | 211 | int on_data_read_count() const { return on_data_read_count_; } |
| 212 | int on_data_sent_count() const { return on_data_sent_count_; } |
| 213 | bool on_failed_called() const { return on_failed_called_; } |
| 214 | |
| 215 | // Sets whether the delegate should automatically start reading. |
| 216 | void set_do_not_start_read(bool do_not_start_read) { |
| 217 | do_not_start_read_ = do_not_start_read; |
| 218 | } |
| 219 | |
xunjieli | 707f895 | 2016-06-06 15:22:06 | [diff] [blame] | 220 | private: |
| 221 | std::unique_ptr<BidirectionalStreamSpdyImpl> stream_; |
| 222 | scoped_refptr<IOBuffer> read_buf_; |
| 223 | int read_buf_len_; |
Bence Béky | 4e83f49 | 2018-05-13 23:14:25 | [diff] [blame] | 224 | std::string data_received_; |
xunjieli | 707f895 | 2016-06-06 15:22:06 | [diff] [blame] | 225 | std::unique_ptr<base::RunLoop> loop_; |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 226 | spdy::SpdyHeaderBlock response_headers_; |
| 227 | spdy::SpdyHeaderBlock trailers_; |
xunjieli | 707f895 | 2016-06-06 15:22:06 | [diff] [blame] | 228 | int error_; |
| 229 | int bytes_read_; |
| 230 | int on_data_read_count_; |
| 231 | int on_data_sent_count_; |
| 232 | bool do_not_start_read_; |
| 233 | bool run_until_completion_; |
| 234 | bool not_expect_callback_; |
| 235 | bool on_failed_called_; |
| 236 | |
| 237 | DISALLOW_COPY_AND_ASSIGN(TestDelegateBase); |
| 238 | }; |
| 239 | |
| 240 | } // namespace |
| 241 | |
Bence Béky | 98447b1 | 2018-05-08 03:14:01 | [diff] [blame] | 242 | class BidirectionalStreamSpdyImplTest : public testing::TestWithParam<bool>, |
| 243 | public WithScopedTaskEnvironment { |
xunjieli | 707f895 | 2016-06-06 15:22:06 | [diff] [blame] | 244 | public: |
| 245 | BidirectionalStreamSpdyImplTest() |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 246 | : default_url_(kDefaultUrl), |
bnc | 3d9035b3 | 2016-06-30 18:18:48 | [diff] [blame] | 247 | host_port_pair_(HostPortPair::FromURL(default_url_)), |
Paul Jensen | a457017a | 2018-01-19 23:52:04 | [diff] [blame] | 248 | key_(host_port_pair_, |
| 249 | ProxyServer::Direct(), |
| 250 | PRIVACY_MODE_DISABLED, |
Matt Menke | 2436b2f | 2018-12-11 18:07:11 | [diff] [blame] | 251 | SpdySessionKey::IsProxySession::kFalse, |
Paul Jensen | a457017a | 2018-01-19 23:52:04 | [diff] [blame] | 252 | SocketTag()), |
xunjieli | 707f895 | 2016-06-06 15:22:06 | [diff] [blame] | 253 | ssl_data_(SSLSocketDataProvider(ASYNC, OK)) { |
bnc | 3cf2a59 | 2016-08-11 14:48:36 | [diff] [blame] | 254 | ssl_data_.next_proto = kProtoHTTP2; |
Ryan Sleevi | 4f83209 | 2017-11-21 23:25:49 | [diff] [blame] | 255 | ssl_data_.ssl_info.cert = |
| 256 | ImportCertFromFile(GetTestCertsDirectory(), "ok_cert.pem"); |
xunjieli | 707f895 | 2016-06-06 15:22:06 | [diff] [blame] | 257 | } |
| 258 | |
| 259 | protected: |
| 260 | void TearDown() override { |
| 261 | if (sequenced_data_) { |
| 262 | EXPECT_TRUE(sequenced_data_->AllReadDataConsumed()); |
| 263 | EXPECT_TRUE(sequenced_data_->AllWriteDataConsumed()); |
| 264 | } |
| 265 | } |
| 266 | |
| 267 | // Initializes the session using SequencedSocketData. |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 268 | void InitSession(base::span<const MockRead> reads, |
| 269 | base::span<const MockWrite> writes) { |
Ryan Sleevi | 4f83209 | 2017-11-21 23:25:49 | [diff] [blame] | 270 | ASSERT_TRUE(ssl_data_.ssl_info.cert.get()); |
xunjieli | 707f895 | 2016-06-06 15:22:06 | [diff] [blame] | 271 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl_data_); |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 272 | sequenced_data_ = std::make_unique<SequencedSocketData>(reads, writes); |
xunjieli | 707f895 | 2016-06-06 15:22:06 | [diff] [blame] | 273 | session_deps_.socket_factory->AddSocketDataProvider(sequenced_data_.get()); |
| 274 | session_deps_.net_log = net_log_.bound().net_log(); |
| 275 | http_session_ = SpdySessionDependencies::SpdyCreateSession(&session_deps_); |
Bence Béky | 0ef1556e | 2017-06-30 19:52:52 | [diff] [blame] | 276 | session_ = CreateSpdySession(http_session_.get(), key_, net_log_.bound()); |
xunjieli | 707f895 | 2016-06-06 15:22:06 | [diff] [blame] | 277 | } |
| 278 | |
| 279 | BoundTestNetLog net_log_; |
| 280 | SpdyTestUtil spdy_util_; |
| 281 | SpdySessionDependencies session_deps_; |
bnc | 3d9035b3 | 2016-06-30 18:18:48 | [diff] [blame] | 282 | const GURL default_url_; |
| 283 | const HostPortPair host_port_pair_; |
| 284 | const SpdySessionKey key_; |
xunjieli | 707f895 | 2016-06-06 15:22:06 | [diff] [blame] | 285 | std::unique_ptr<SequencedSocketData> sequenced_data_; |
| 286 | std::unique_ptr<HttpNetworkSession> http_session_; |
| 287 | base::WeakPtr<SpdySession> session_; |
| 288 | |
| 289 | private: |
| 290 | SSLSocketDataProvider ssl_data_; |
| 291 | }; |
| 292 | |
xunjieli | 6d0b944d | 2016-09-21 01:53:57 | [diff] [blame] | 293 | TEST_F(BidirectionalStreamSpdyImplTest, SimplePostRequest) { |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 294 | spdy::SpdySerializedFrame req(spdy_util_.ConstructSpdyPost( |
xunjieli | 6d0b944d | 2016-09-21 01:53:57 | [diff] [blame] | 295 | kDefaultUrl, 1, kBodyDataSize, LOW, nullptr, 0)); |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 296 | spdy::SpdySerializedFrame data_frame(spdy_util_.ConstructSpdyDataFrame( |
Bence Béky | d74f438 | 2018-02-20 18:26:19 | [diff] [blame] | 297 | 1, base::StringPiece(kBodyData, kBodyDataSize), /*fin=*/true)); |
xunjieli | 6d0b944d | 2016-09-21 01:53:57 | [diff] [blame] | 298 | MockWrite writes[] = { |
| 299 | CreateMockWrite(req, 0), CreateMockWrite(data_frame, 3), |
| 300 | }; |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 301 | spdy::SpdySerializedFrame resp(spdy_util_.ConstructSpdyPostReply(nullptr, 0)); |
| 302 | spdy::SpdySerializedFrame response_body_frame( |
xunjieli | 6d0b944d | 2016-09-21 01:53:57 | [diff] [blame] | 303 | spdy_util_.ConstructSpdyDataFrame(1, /*fin=*/true)); |
| 304 | MockRead reads[] = { |
| 305 | CreateMockRead(resp, 1), |
| 306 | MockRead(ASYNC, ERR_IO_PENDING, 2), // Force a pause. |
| 307 | CreateMockRead(response_body_frame, 4), MockRead(ASYNC, 0, 5), |
| 308 | }; |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 309 | InitSession(reads, writes); |
xunjieli | 6d0b944d | 2016-09-21 01:53:57 | [diff] [blame] | 310 | |
| 311 | BidirectionalStreamRequestInfo request_info; |
| 312 | request_info.method = "POST"; |
| 313 | request_info.url = default_url_; |
| 314 | request_info.extra_headers.SetHeader(net::HttpRequestHeaders::kContentLength, |
Brett Wilson | 5accd24 | 2017-11-30 22:07:32 | [diff] [blame] | 315 | base::NumberToString(kBodyDataSize)); |
xunjieli | 6d0b944d | 2016-09-21 01:53:57 | [diff] [blame] | 316 | |
Victor Costan | 9c7302b | 2018-08-27 16:39:44 | [diff] [blame] | 317 | scoped_refptr<IOBuffer> read_buffer = |
| 318 | base::MakeRefCounted<IOBuffer>(kReadBufferSize); |
Jeremy Roman | 0579ed6 | 2017-08-29 15:56:19 | [diff] [blame] | 319 | auto delegate = std::make_unique<TestDelegateBase>( |
bnc | 3f6a855 | 2017-05-17 13:40:34 | [diff] [blame] | 320 | session_, read_buffer.get(), kReadBufferSize); |
xunjieli | 6d0b944d | 2016-09-21 01:53:57 | [diff] [blame] | 321 | delegate->SetRunUntilCompletion(true); |
| 322 | delegate->Start(&request_info, net_log_.bound()); |
| 323 | sequenced_data_->RunUntilPaused(); |
| 324 | |
Victor Costan | 9c7302b | 2018-08-27 16:39:44 | [diff] [blame] | 325 | scoped_refptr<StringIOBuffer> write_buffer = |
| 326 | base::MakeRefCounted<StringIOBuffer>( |
| 327 | std::string(kBodyData, kBodyDataSize)); |
xunjieli | 6d0b944d | 2016-09-21 01:53:57 | [diff] [blame] | 328 | delegate->SendData(write_buffer.get(), write_buffer->size(), true); |
| 329 | sequenced_data_->Resume(); |
| 330 | base::RunLoop().RunUntilIdle(); |
| 331 | delegate->WaitUntilCompletion(); |
| 332 | LoadTimingInfo load_timing_info; |
| 333 | EXPECT_TRUE(delegate->GetLoadTimingInfo(&load_timing_info)); |
| 334 | TestLoadTimingNotReused(load_timing_info); |
| 335 | |
| 336 | EXPECT_EQ(1, delegate->on_data_read_count()); |
| 337 | EXPECT_EQ(1, delegate->on_data_sent_count()); |
| 338 | EXPECT_EQ(kProtoHTTP2, delegate->GetProtocol()); |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 339 | EXPECT_EQ(CountWriteBytes(writes), delegate->GetTotalSentBytes()); |
| 340 | EXPECT_EQ(CountReadBytes(reads), delegate->GetTotalReceivedBytes()); |
xunjieli | 6d0b944d | 2016-09-21 01:53:57 | [diff] [blame] | 341 | } |
| 342 | |
| 343 | TEST_F(BidirectionalStreamSpdyImplTest, LoadTimingTwoRequests) { |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 344 | spdy::SpdySerializedFrame req( |
Bence Béky | 27ad0a1 | 2018-02-08 00:35:48 | [diff] [blame] | 345 | spdy_util_.ConstructSpdyGet(nullptr, 0, /*stream_id=*/1, LOW)); |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 346 | spdy::SpdySerializedFrame req2( |
Bence Béky | 27ad0a1 | 2018-02-08 00:35:48 | [diff] [blame] | 347 | spdy_util_.ConstructSpdyGet(nullptr, 0, /*stream_id=*/3, LOW)); |
xunjieli | 6d0b944d | 2016-09-21 01:53:57 | [diff] [blame] | 348 | MockWrite writes[] = { |
| 349 | CreateMockWrite(req, 0), CreateMockWrite(req2, 2), |
| 350 | }; |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 351 | spdy::SpdySerializedFrame resp( |
xunjieli | 6d0b944d | 2016-09-21 01:53:57 | [diff] [blame] | 352 | spdy_util_.ConstructSpdyGetReply(nullptr, 0, /*stream_id=*/1)); |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 353 | spdy::SpdySerializedFrame resp2( |
xunjieli | 6d0b944d | 2016-09-21 01:53:57 | [diff] [blame] | 354 | spdy_util_.ConstructSpdyGetReply(nullptr, 0, /*stream_id=*/3)); |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 355 | spdy::SpdySerializedFrame resp_body( |
xunjieli | 6d0b944d | 2016-09-21 01:53:57 | [diff] [blame] | 356 | spdy_util_.ConstructSpdyDataFrame(/*stream_id=*/1, /*fin=*/true)); |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 357 | spdy::SpdySerializedFrame resp_body2( |
xunjieli | 6d0b944d | 2016-09-21 01:53:57 | [diff] [blame] | 358 | spdy_util_.ConstructSpdyDataFrame(/*stream_id=*/3, /*fin=*/true)); |
| 359 | MockRead reads[] = {CreateMockRead(resp, 1), CreateMockRead(resp_body, 3), |
| 360 | CreateMockRead(resp2, 4), CreateMockRead(resp_body2, 5), |
| 361 | MockRead(ASYNC, 0, 6)}; |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 362 | InitSession(reads, writes); |
xunjieli | 6d0b944d | 2016-09-21 01:53:57 | [diff] [blame] | 363 | |
| 364 | BidirectionalStreamRequestInfo request_info; |
| 365 | request_info.method = "GET"; |
| 366 | request_info.url = default_url_; |
| 367 | request_info.end_stream_on_headers = true; |
| 368 | |
Victor Costan | 9c7302b | 2018-08-27 16:39:44 | [diff] [blame] | 369 | scoped_refptr<IOBuffer> read_buffer = |
| 370 | base::MakeRefCounted<IOBuffer>(kReadBufferSize); |
| 371 | scoped_refptr<IOBuffer> read_buffer2 = |
| 372 | base::MakeRefCounted<IOBuffer>(kReadBufferSize); |
Jeremy Roman | 0579ed6 | 2017-08-29 15:56:19 | [diff] [blame] | 373 | auto delegate = std::make_unique<TestDelegateBase>( |
bnc | 3f6a855 | 2017-05-17 13:40:34 | [diff] [blame] | 374 | session_, read_buffer.get(), kReadBufferSize); |
Jeremy Roman | 0579ed6 | 2017-08-29 15:56:19 | [diff] [blame] | 375 | auto delegate2 = std::make_unique<TestDelegateBase>( |
bnc | 3f6a855 | 2017-05-17 13:40:34 | [diff] [blame] | 376 | session_, read_buffer2.get(), kReadBufferSize); |
xunjieli | 6d0b944d | 2016-09-21 01:53:57 | [diff] [blame] | 377 | delegate->SetRunUntilCompletion(true); |
| 378 | delegate2->SetRunUntilCompletion(true); |
| 379 | delegate->Start(&request_info, net_log_.bound()); |
| 380 | delegate2->Start(&request_info, net_log_.bound()); |
| 381 | |
| 382 | base::RunLoop().RunUntilIdle(); |
| 383 | delegate->WaitUntilCompletion(); |
| 384 | delegate2->WaitUntilCompletion(); |
| 385 | LoadTimingInfo load_timing_info; |
| 386 | EXPECT_TRUE(delegate->GetLoadTimingInfo(&load_timing_info)); |
| 387 | TestLoadTimingNotReused(load_timing_info); |
| 388 | LoadTimingInfo load_timing_info2; |
| 389 | EXPECT_TRUE(delegate2->GetLoadTimingInfo(&load_timing_info2)); |
| 390 | TestLoadTimingReused(load_timing_info2); |
| 391 | } |
| 392 | |
xunjieli | 707f895 | 2016-06-06 15:22:06 | [diff] [blame] | 393 | TEST_F(BidirectionalStreamSpdyImplTest, SendDataAfterStreamFailed) { |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 394 | spdy::SpdySerializedFrame req(spdy_util_.ConstructSpdyPost( |
bnc | 3d9035b3 | 2016-06-30 18:18:48 | [diff] [blame] | 395 | kDefaultUrl, 1, kBodyDataSize * 3, LOW, nullptr, 0)); |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 396 | spdy::SpdySerializedFrame rst( |
| 397 | spdy_util_.ConstructSpdyRstStream(1, spdy::ERROR_CODE_PROTOCOL_ERROR)); |
xunjieli | 707f895 | 2016-06-06 15:22:06 | [diff] [blame] | 398 | |
| 399 | MockWrite writes[] = { |
bnc | df80d44fd | 2016-07-15 20:27:41 | [diff] [blame] | 400 | CreateMockWrite(req, 0), CreateMockWrite(rst, 2), |
xunjieli | 707f895 | 2016-06-06 15:22:06 | [diff] [blame] | 401 | }; |
| 402 | |
| 403 | const char* const kExtraHeaders[] = {"X-UpperCase", "yes"}; |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 404 | spdy::SpdySerializedFrame resp( |
bnc | 4233140 | 2016-07-25 13:36:15 | [diff] [blame] | 405 | spdy_util_.ConstructSpdyGetReply(kExtraHeaders, 1, 1)); |
xunjieli | 707f895 | 2016-06-06 15:22:06 | [diff] [blame] | 406 | |
| 407 | MockRead reads[] = { |
bnc | df80d44fd | 2016-07-15 20:27:41 | [diff] [blame] | 408 | CreateMockRead(resp, 1), MockRead(ASYNC, 0, 3), |
xunjieli | 707f895 | 2016-06-06 15:22:06 | [diff] [blame] | 409 | }; |
| 410 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 411 | InitSession(reads, writes); |
xunjieli | 707f895 | 2016-06-06 15:22:06 | [diff] [blame] | 412 | |
| 413 | BidirectionalStreamRequestInfo request_info; |
| 414 | request_info.method = "POST"; |
bnc | 3d9035b3 | 2016-06-30 18:18:48 | [diff] [blame] | 415 | request_info.url = default_url_; |
xunjieli | 707f895 | 2016-06-06 15:22:06 | [diff] [blame] | 416 | request_info.extra_headers.SetHeader(net::HttpRequestHeaders::kContentLength, |
Brett Wilson | 5accd24 | 2017-11-30 22:07:32 | [diff] [blame] | 417 | base::NumberToString(kBodyDataSize * 3)); |
xunjieli | 707f895 | 2016-06-06 15:22:06 | [diff] [blame] | 418 | |
Victor Costan | 9c7302b | 2018-08-27 16:39:44 | [diff] [blame] | 419 | scoped_refptr<IOBuffer> read_buffer = |
| 420 | base::MakeRefCounted<IOBuffer>(kReadBufferSize); |
Jeremy Roman | 0579ed6 | 2017-08-29 15:56:19 | [diff] [blame] | 421 | auto delegate = std::make_unique<TestDelegateBase>( |
bnc | 3f6a855 | 2017-05-17 13:40:34 | [diff] [blame] | 422 | session_, read_buffer.get(), kReadBufferSize); |
xunjieli | 707f895 | 2016-06-06 15:22:06 | [diff] [blame] | 423 | delegate->SetRunUntilCompletion(true); |
| 424 | delegate->Start(&request_info, net_log_.bound()); |
| 425 | base::RunLoop().RunUntilIdle(); |
| 426 | |
| 427 | EXPECT_TRUE(delegate->on_failed_called()); |
| 428 | |
| 429 | // Try to send data after OnFailed(), should not get called back. |
Victor Costan | 9c7302b | 2018-08-27 16:39:44 | [diff] [blame] | 430 | scoped_refptr<StringIOBuffer> buf = |
| 431 | base::MakeRefCounted<StringIOBuffer>("dummy"); |
xunjieli | 707f895 | 2016-06-06 15:22:06 | [diff] [blame] | 432 | delegate->SendData(buf.get(), buf->size(), false); |
| 433 | base::RunLoop().RunUntilIdle(); |
| 434 | |
Bence Béky | d0d6950 | 2019-06-25 19:47:18 | [diff] [blame^] | 435 | EXPECT_THAT(delegate->error(), IsError(ERR_HTTP2_PROTOCOL_ERROR)); |
xunjieli | 707f895 | 2016-06-06 15:22:06 | [diff] [blame] | 436 | EXPECT_EQ(0, delegate->on_data_read_count()); |
| 437 | EXPECT_EQ(0, delegate->on_data_sent_count()); |
| 438 | EXPECT_EQ(kProtoHTTP2, delegate->GetProtocol()); |
| 439 | // BidirectionalStreamSpdyStreamJob does not count the bytes sent for |rst| |
| 440 | // because it is sent after SpdyStream::Delegate::OnClose is called. |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 441 | EXPECT_EQ(CountWriteBytes(base::make_span(writes, 1)), |
| 442 | delegate->GetTotalSentBytes()); |
Minjeong Lee | bbd22577 | 2017-11-28 14:37:25 | [diff] [blame] | 443 | EXPECT_EQ(0, delegate->GetTotalReceivedBytes()); |
xunjieli | 707f895 | 2016-06-06 15:22:06 | [diff] [blame] | 444 | } |
| 445 | |
Victor Costan | 2309ea0 | 2019-02-13 21:35:47 | [diff] [blame] | 446 | INSTANTIATE_TEST_SUITE_P(BidirectionalStreamSpdyImplTests, |
| 447 | BidirectionalStreamSpdyImplTest, |
| 448 | ::testing::Bool()); |
xunjieli | 4f8b6bb6 | 2016-10-31 23:16:00 | [diff] [blame] | 449 | |
| 450 | // Tests that when received RST_STREAM with NO_ERROR, BidirectionalStream does |
| 451 | // not crash when processing pending writes. See crbug.com/650438. |
| 452 | TEST_P(BidirectionalStreamSpdyImplTest, RstWithNoErrorBeforeSendIsComplete) { |
| 453 | bool is_test_sendv = GetParam(); |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 454 | spdy::SpdySerializedFrame req(spdy_util_.ConstructSpdyPost( |
xunjieli | 4f8b6bb6 | 2016-10-31 23:16:00 | [diff] [blame] | 455 | kDefaultUrl, 1, kBodyDataSize * 3, LOW, nullptr, 0)); |
| 456 | MockWrite writes[] = {CreateMockWrite(req, 0)}; |
| 457 | |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 458 | spdy::SpdySerializedFrame resp(spdy_util_.ConstructSpdyPostReply(nullptr, 0)); |
| 459 | spdy::SpdySerializedFrame rst( |
| 460 | spdy_util_.ConstructSpdyRstStream(1, spdy::ERROR_CODE_NO_ERROR)); |
xunjieli | 4f8b6bb6 | 2016-10-31 23:16:00 | [diff] [blame] | 461 | MockRead reads[] = {CreateMockRead(resp, 1), |
| 462 | MockRead(ASYNC, ERR_IO_PENDING, 2), // Force a pause. |
| 463 | CreateMockRead(rst, 3), MockRead(ASYNC, 0, 4)}; |
| 464 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 465 | InitSession(reads, writes); |
xunjieli | 4f8b6bb6 | 2016-10-31 23:16:00 | [diff] [blame] | 466 | |
| 467 | BidirectionalStreamRequestInfo request_info; |
| 468 | request_info.method = "POST"; |
| 469 | request_info.url = default_url_; |
| 470 | request_info.extra_headers.SetHeader(net::HttpRequestHeaders::kContentLength, |
Brett Wilson | 5accd24 | 2017-11-30 22:07:32 | [diff] [blame] | 471 | base::NumberToString(kBodyDataSize * 3)); |
xunjieli | 4f8b6bb6 | 2016-10-31 23:16:00 | [diff] [blame] | 472 | |
Victor Costan | 9c7302b | 2018-08-27 16:39:44 | [diff] [blame] | 473 | scoped_refptr<IOBuffer> read_buffer = |
| 474 | base::MakeRefCounted<IOBuffer>(kReadBufferSize); |
Jeremy Roman | 0579ed6 | 2017-08-29 15:56:19 | [diff] [blame] | 475 | auto delegate = std::make_unique<TestDelegateBase>( |
bnc | 3f6a855 | 2017-05-17 13:40:34 | [diff] [blame] | 476 | session_, read_buffer.get(), kReadBufferSize); |
xunjieli | 4f8b6bb6 | 2016-10-31 23:16:00 | [diff] [blame] | 477 | delegate->SetRunUntilCompletion(true); |
| 478 | delegate->Start(&request_info, net_log_.bound()); |
| 479 | sequenced_data_->RunUntilPaused(); |
| 480 | // Make a write pending before receiving RST_STREAM. |
Victor Costan | 9c7302b | 2018-08-27 16:39:44 | [diff] [blame] | 481 | scoped_refptr<StringIOBuffer> write_buffer = |
| 482 | base::MakeRefCounted<StringIOBuffer>( |
| 483 | std::string(kBodyData, kBodyDataSize)); |
xunjieli | 4f8b6bb6 | 2016-10-31 23:16:00 | [diff] [blame] | 484 | delegate->SendData(write_buffer.get(), write_buffer->size(), false); |
| 485 | sequenced_data_->Resume(); |
| 486 | base::RunLoop().RunUntilIdle(); |
| 487 | |
| 488 | // Make sure OnClose() without an error completes any pending write(). |
| 489 | EXPECT_EQ(1, delegate->on_data_sent_count()); |
| 490 | EXPECT_FALSE(delegate->on_failed_called()); |
| 491 | |
| 492 | if (is_test_sendv) { |
| 493 | std::vector<scoped_refptr<IOBuffer>> three_buffers = { |
| 494 | write_buffer.get(), write_buffer.get(), write_buffer.get()}; |
| 495 | std::vector<int> three_lengths = { |
| 496 | write_buffer->size(), write_buffer->size(), write_buffer->size()}; |
| 497 | delegate->SendvData(three_buffers, three_lengths, /*end_of_stream=*/true); |
| 498 | base::RunLoop().RunUntilIdle(); |
| 499 | } else { |
| 500 | for (size_t j = 0; j < 3; j++) { |
| 501 | delegate->SendData(write_buffer.get(), write_buffer->size(), |
| 502 | /*end_of_stream=*/j == 2); |
| 503 | base::RunLoop().RunUntilIdle(); |
| 504 | } |
| 505 | } |
| 506 | delegate->WaitUntilCompletion(); |
| 507 | LoadTimingInfo load_timing_info; |
| 508 | EXPECT_TRUE(delegate->GetLoadTimingInfo(&load_timing_info)); |
| 509 | TestLoadTimingNotReused(load_timing_info); |
| 510 | |
| 511 | EXPECT_THAT(delegate->error(), IsError(OK)); |
| 512 | EXPECT_EQ(1, delegate->on_data_read_count()); |
| 513 | EXPECT_EQ(is_test_sendv ? 2 : 4, delegate->on_data_sent_count()); |
| 514 | EXPECT_EQ(kProtoHTTP2, delegate->GetProtocol()); |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 515 | EXPECT_EQ(CountWriteBytes(base::make_span(writes, 1)), |
| 516 | delegate->GetTotalSentBytes()); |
xunjieli | 4f8b6bb6 | 2016-10-31 23:16:00 | [diff] [blame] | 517 | // Should not count RST stream. |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 518 | EXPECT_EQ(CountReadBytes(base::make_span(reads).first(base::size(reads) - 2)), |
xunjieli | 4f8b6bb6 | 2016-10-31 23:16:00 | [diff] [blame] | 519 | delegate->GetTotalReceivedBytes()); |
| 520 | |
| 521 | // Now call SendData again should produce an error because end of stream |
| 522 | // flag has been written. |
| 523 | if (is_test_sendv) { |
| 524 | std::vector<scoped_refptr<IOBuffer>> buffer = {write_buffer.get()}; |
| 525 | std::vector<int> buffer_size = {write_buffer->size()}; |
| 526 | delegate->SendvData(buffer, buffer_size, true); |
| 527 | } else { |
| 528 | delegate->SendData(write_buffer.get(), write_buffer->size(), true); |
| 529 | } |
| 530 | base::RunLoop().RunUntilIdle(); |
| 531 | EXPECT_THAT(delegate->error(), IsError(ERR_UNEXPECTED)); |
| 532 | EXPECT_TRUE(delegate->on_failed_called()); |
| 533 | EXPECT_EQ(is_test_sendv ? 2 : 4, delegate->on_data_sent_count()); |
| 534 | } |
| 535 | |
xunjieli | 707f895 | 2016-06-06 15:22:06 | [diff] [blame] | 536 | } // namespace net |