| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "net/spdy/bidirectional_stream_spdy_impl.h" | 5 #include "net/spdy/bidirectional_stream_spdy_impl.h" |
| 6 | 6 |
| 7 #include <memory> | 7 #include <memory> |
| 8 #include <string> | 8 #include <string> |
| 9 | 9 |
| 10 #include "base/macros.h" | 10 #include "base/macros.h" |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 43 | 43 |
| 44 class TestDelegateBase : public BidirectionalStreamImpl::Delegate { | 44 class TestDelegateBase : public BidirectionalStreamImpl::Delegate { |
| 45 public: | 45 public: |
| 46 TestDelegateBase(base::WeakPtr<SpdySession> session, | 46 TestDelegateBase(base::WeakPtr<SpdySession> session, |
| 47 IOBuffer* read_buf, | 47 IOBuffer* read_buf, |
| 48 int read_buf_len) | 48 int read_buf_len) |
| 49 : stream_(new BidirectionalStreamSpdyImpl(session)), | 49 : stream_(new BidirectionalStreamSpdyImpl(session)), |
| 50 read_buf_(read_buf), | 50 read_buf_(read_buf), |
| 51 read_buf_len_(read_buf_len), | 51 read_buf_len_(read_buf_len), |
| 52 loop_(nullptr), | 52 loop_(nullptr), |
| 53 received_bytes_(0), |
| 54 sent_bytes_(0), |
| 53 error_(OK), | 55 error_(OK), |
| 54 bytes_read_(0), | 56 bytes_read_(0), |
| 55 on_data_read_count_(0), | 57 on_data_read_count_(0), |
| 56 on_data_sent_count_(0), | 58 on_data_sent_count_(0), |
| 57 do_not_start_read_(false), | 59 do_not_start_read_(false), |
| 58 run_until_completion_(false), | 60 run_until_completion_(false), |
| 59 not_expect_callback_(false), | 61 not_expect_callback_(false), |
| 60 on_failed_called_(false) {} | 62 on_failed_called_(false) {} |
| 61 | 63 |
| 62 ~TestDelegateBase() override {} | 64 ~TestDelegateBase() override {} |
| (...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 152 if (rv > 0) { | 154 if (rv > 0) { |
| 153 data_received_.append(read_buf_->data(), rv); | 155 data_received_.append(read_buf_->data(), rv); |
| 154 bytes_read_ += rv; | 156 bytes_read_ += rv; |
| 155 } | 157 } |
| 156 return rv; | 158 return rv; |
| 157 } | 159 } |
| 158 | 160 |
| 159 NextProto GetProtocol() const { return stream_->GetProtocol(); } | 161 NextProto GetProtocol() const { return stream_->GetProtocol(); } |
| 160 | 162 |
| 161 int64_t GetTotalReceivedBytes() const { | 163 int64_t GetTotalReceivedBytes() const { |
| 162 return stream_->GetTotalReceivedBytes(); | 164 if (stream_) |
| 165 return stream_->GetTotalReceivedBytes(); |
| 166 return received_bytes_; |
| 163 } | 167 } |
| 164 | 168 |
| 165 int64_t GetTotalSentBytes() const { return stream_->GetTotalSentBytes(); } | 169 int64_t GetTotalSentBytes() const { |
| 170 if (stream_) |
| 171 return stream_->GetTotalSentBytes(); |
| 172 return sent_bytes_; |
| 173 } |
| 166 | 174 |
| 167 // Const getters for internal states. | 175 // Const getters for internal states. |
| 168 const std::string& data_received() const { return data_received_; } | 176 const std::string& data_received() const { return data_received_; } |
| 169 int bytes_read() const { return bytes_read_; } | 177 int bytes_read() const { return bytes_read_; } |
| 170 int error() const { return error_; } | 178 int error() const { return error_; } |
| 171 const SpdyHeaderBlock& response_headers() const { return response_headers_; } | 179 const SpdyHeaderBlock& response_headers() const { return response_headers_; } |
| 172 const SpdyHeaderBlock& trailers() const { return trailers_; } | 180 const SpdyHeaderBlock& trailers() const { return trailers_; } |
| 173 int on_data_read_count() const { return on_data_read_count_; } | 181 int on_data_read_count() const { return on_data_read_count_; } |
| 174 int on_data_sent_count() const { return on_data_sent_count_; } | 182 int on_data_sent_count() const { return on_data_sent_count_; } |
| 175 bool on_failed_called() const { return on_failed_called_; } | 183 bool on_failed_called() const { return on_failed_called_; } |
| 176 | 184 |
| 177 // Sets whether the delegate should automatically start reading. | 185 // Sets whether the delegate should automatically start reading. |
| 178 void set_do_not_start_read(bool do_not_start_read) { | 186 void set_do_not_start_read(bool do_not_start_read) { |
| 179 do_not_start_read_ = do_not_start_read; | 187 do_not_start_read_ = do_not_start_read; |
| 180 } | 188 } |
| 181 | 189 |
| 182 // Cancels |stream_|. | 190 void DeleteStream() { stream_.reset(); } |
| 183 void CancelStream() { stream_->Cancel(); } | |
| 184 | 191 |
| 185 private: | 192 private: |
| 186 std::unique_ptr<BidirectionalStreamSpdyImpl> stream_; | 193 std::unique_ptr<BidirectionalStreamSpdyImpl> stream_; |
| 187 scoped_refptr<IOBuffer> read_buf_; | 194 scoped_refptr<IOBuffer> read_buf_; |
| 188 int read_buf_len_; | 195 int read_buf_len_; |
| 189 std::string data_received_; | 196 std::string data_received_; |
| 190 std::unique_ptr<base::RunLoop> loop_; | 197 std::unique_ptr<base::RunLoop> loop_; |
| 191 SpdyHeaderBlock response_headers_; | 198 SpdyHeaderBlock response_headers_; |
| 192 SpdyHeaderBlock trailers_; | 199 SpdyHeaderBlock trailers_; |
| 200 int64_t received_bytes_; |
| 201 int64_t sent_bytes_; |
| 193 int error_; | 202 int error_; |
| 194 int bytes_read_; | 203 int bytes_read_; |
| 195 int on_data_read_count_; | 204 int on_data_read_count_; |
| 196 int on_data_sent_count_; | 205 int on_data_sent_count_; |
| 197 bool do_not_start_read_; | 206 bool do_not_start_read_; |
| 198 bool run_until_completion_; | 207 bool run_until_completion_; |
| 199 bool not_expect_callback_; | 208 bool not_expect_callback_; |
| 200 bool on_failed_called_; | 209 bool on_failed_called_; |
| 201 | 210 |
| 202 DISALLOW_COPY_AND_ASSIGN(TestDelegateBase); | 211 DISALLOW_COPY_AND_ASSIGN(TestDelegateBase); |
| (...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 297 EXPECT_EQ(0, delegate->on_data_read_count()); | 306 EXPECT_EQ(0, delegate->on_data_read_count()); |
| 298 EXPECT_EQ(0, delegate->on_data_sent_count()); | 307 EXPECT_EQ(0, delegate->on_data_sent_count()); |
| 299 EXPECT_EQ(kProtoHTTP2, delegate->GetProtocol()); | 308 EXPECT_EQ(kProtoHTTP2, delegate->GetProtocol()); |
| 300 // BidirectionalStreamSpdyStreamJob does not count the bytes sent for |rst| | 309 // BidirectionalStreamSpdyStreamJob does not count the bytes sent for |rst| |
| 301 // because it is sent after SpdyStream::Delegate::OnClose is called. | 310 // because it is sent after SpdyStream::Delegate::OnClose is called. |
| 302 EXPECT_EQ(CountWriteBytes(writes, 1), delegate->GetTotalSentBytes()); | 311 EXPECT_EQ(CountWriteBytes(writes, 1), delegate->GetTotalSentBytes()); |
| 303 EXPECT_EQ(CountReadBytes(reads, arraysize(reads)), | 312 EXPECT_EQ(CountReadBytes(reads, arraysize(reads)), |
| 304 delegate->GetTotalReceivedBytes()); | 313 delegate->GetTotalReceivedBytes()); |
| 305 } | 314 } |
| 306 | 315 |
| 307 TEST_F(BidirectionalStreamSpdyImplTest, SendDataAfterCancelStream) { | |
| 308 SpdySerializedFrame req(spdy_util_.ConstructSpdyPost( | |
| 309 kDefaultUrl, 1, kBodyDataSize * 3, LOWEST, nullptr, 0)); | |
| 310 SpdySerializedFrame data_frame( | |
| 311 spdy_util_.ConstructSpdyDataFrame(1, kBodyData, kBodyDataSize, | |
| 312 /*fin=*/false)); | |
| 313 SpdySerializedFrame rst( | |
| 314 spdy_util_.ConstructSpdyRstStream(1, RST_STREAM_CANCEL)); | |
| 315 | |
| 316 MockWrite writes[] = { | |
| 317 CreateMockWrite(req, 0), CreateMockWrite(data_frame, 3), | |
| 318 CreateMockWrite(rst, 5), | |
| 319 }; | |
| 320 | |
| 321 SpdySerializedFrame resp(spdy_util_.ConstructSpdyGetReply(nullptr, 0, 1)); | |
| 322 SpdySerializedFrame response_body_frame( | |
| 323 spdy_util_.ConstructSpdyDataFrame(1, false)); | |
| 324 | |
| 325 MockRead reads[] = { | |
| 326 CreateMockRead(resp, 1), | |
| 327 MockRead(ASYNC, ERR_IO_PENDING, 2), // Force a pause. | |
| 328 MockRead(ASYNC, ERR_IO_PENDING, 4), // Force a pause. | |
| 329 MockRead(ASYNC, 0, 6), | |
| 330 }; | |
| 331 | |
| 332 InitSession(reads, arraysize(reads), writes, arraysize(writes)); | |
| 333 | |
| 334 BidirectionalStreamRequestInfo request_info; | |
| 335 request_info.method = "POST"; | |
| 336 request_info.url = default_url_; | |
| 337 request_info.priority = LOWEST; | |
| 338 request_info.extra_headers.SetHeader(net::HttpRequestHeaders::kContentLength, | |
| 339 base::SizeTToString(kBodyDataSize * 3)); | |
| 340 | |
| 341 scoped_refptr<IOBuffer> read_buffer(new IOBuffer(kReadBufferSize)); | |
| 342 std::unique_ptr<TestDelegateBase> delegate( | |
| 343 new TestDelegateBase(session_, read_buffer.get(), kReadBufferSize)); | |
| 344 delegate->set_do_not_start_read(true); | |
| 345 delegate->Start(&request_info, net_log_.bound()); | |
| 346 // Send the request and receive response headers. | |
| 347 sequenced_data_->RunUntilPaused(); | |
| 348 EXPECT_EQ(kProtoHTTP2, delegate->GetProtocol()); | |
| 349 | |
| 350 // Send a DATA frame. | |
| 351 scoped_refptr<StringIOBuffer> buf( | |
| 352 new StringIOBuffer(std::string(kBodyData, kBodyDataSize))); | |
| 353 delegate->SendData(buf.get(), buf->size(), false); | |
| 354 sequenced_data_->Resume(); | |
| 355 base::RunLoop().RunUntilIdle(); | |
| 356 // Cancel the stream. | |
| 357 delegate->CancelStream(); | |
| 358 sequenced_data_->Resume(); | |
| 359 base::RunLoop().RunUntilIdle(); | |
| 360 | |
| 361 // Try to send data after Cancel(), should not get called back. | |
| 362 delegate->SendData(buf.get(), buf->size(), false); | |
| 363 base::RunLoop().RunUntilIdle(); | |
| 364 EXPECT_FALSE(delegate->on_failed_called()); | |
| 365 | |
| 366 EXPECT_EQ("200", delegate->response_headers().find(":status")->second); | |
| 367 EXPECT_EQ(0, delegate->on_data_read_count()); | |
| 368 EXPECT_EQ(kProtoHTTP2, delegate->GetProtocol()); | |
| 369 EXPECT_EQ(0, delegate->GetTotalSentBytes()); | |
| 370 EXPECT_EQ(0, delegate->GetTotalReceivedBytes()); | |
| 371 } | |
| 372 | |
| 373 } // namespace net | 316 } // namespace net |
| OLD | NEW |