| 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/quic/bidirectional_stream_quic_impl.h" | 5 #include "net/quic/bidirectional_stream_quic_impl.h" |
| 6 | 6 |
| 7 #include <stdint.h> | 7 #include <stdint.h> |
| 8 | 8 |
| 9 #include <memory> | 9 #include <memory> |
| 10 #include <vector> | 10 #include <vector> |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 68 int read_buf_len, | 68 int read_buf_len, |
| 69 std::unique_ptr<base::Timer> timer) | 69 std::unique_ptr<base::Timer> timer) |
| 70 : read_buf_(read_buf), | 70 : read_buf_(read_buf), |
| 71 read_buf_len_(read_buf_len), | 71 read_buf_len_(read_buf_len), |
| 72 timer_(std::move(timer)), | 72 timer_(std::move(timer)), |
| 73 loop_(nullptr), | 73 loop_(nullptr), |
| 74 error_(OK), | 74 error_(OK), |
| 75 on_data_read_count_(0), | 75 on_data_read_count_(0), |
| 76 on_data_sent_count_(0), | 76 on_data_sent_count_(0), |
| 77 not_expect_callback_(false), | 77 not_expect_callback_(false), |
| 78 on_failed_called_(false), |
| 78 send_request_headers_automatically_(true) { | 79 send_request_headers_automatically_(true) { |
| 79 loop_.reset(new base::RunLoop); | 80 loop_.reset(new base::RunLoop); |
| 80 } | 81 } |
| 81 | 82 |
| 82 ~TestDelegateBase() override {} | 83 ~TestDelegateBase() override {} |
| 83 | 84 |
| 84 void OnStreamReady(bool request_headers_sent) override { | 85 void OnStreamReady(bool request_headers_sent) override { |
| 86 CHECK(!on_failed_called_); |
| 85 EXPECT_EQ(send_request_headers_automatically_, request_headers_sent); | 87 EXPECT_EQ(send_request_headers_automatically_, request_headers_sent); |
| 86 CHECK(!not_expect_callback_); | 88 CHECK(!not_expect_callback_); |
| 87 loop_->Quit(); | 89 loop_->Quit(); |
| 88 } | 90 } |
| 89 | 91 |
| 90 void OnHeadersReceived(const SpdyHeaderBlock& response_headers) override { | 92 void OnHeadersReceived(const SpdyHeaderBlock& response_headers) override { |
| 93 CHECK(!on_failed_called_); |
| 91 CHECK(!not_expect_callback_); | 94 CHECK(!not_expect_callback_); |
| 92 | 95 |
| 93 response_headers_ = response_headers; | 96 response_headers_ = response_headers; |
| 94 loop_->Quit(); | 97 loop_->Quit(); |
| 95 } | 98 } |
| 96 | 99 |
| 97 void OnDataRead(int bytes_read) override { | 100 void OnDataRead(int bytes_read) override { |
| 101 CHECK(!on_failed_called_); |
| 98 CHECK(!not_expect_callback_); | 102 CHECK(!not_expect_callback_); |
| 99 CHECK(!callback_.is_null()); | 103 CHECK(!callback_.is_null()); |
| 100 | 104 |
| 101 ++on_data_read_count_; | 105 ++on_data_read_count_; |
| 102 CHECK_GE(bytes_read, OK); | 106 CHECK_GE(bytes_read, OK); |
| 103 data_received_.append(read_buf_->data(), bytes_read); | 107 data_received_.append(read_buf_->data(), bytes_read); |
| 104 base::ResetAndReturn(&callback_).Run(bytes_read); | 108 base::ResetAndReturn(&callback_).Run(bytes_read); |
| 105 } | 109 } |
| 106 | 110 |
| 107 void OnDataSent() override { | 111 void OnDataSent() override { |
| 112 CHECK(!on_failed_called_); |
| 108 CHECK(!not_expect_callback_); | 113 CHECK(!not_expect_callback_); |
| 109 | 114 |
| 110 ++on_data_sent_count_; | 115 ++on_data_sent_count_; |
| 111 loop_->Quit(); | 116 loop_->Quit(); |
| 112 } | 117 } |
| 113 | 118 |
| 114 void OnTrailersReceived(const SpdyHeaderBlock& trailers) override { | 119 void OnTrailersReceived(const SpdyHeaderBlock& trailers) override { |
| 120 CHECK(!on_failed_called_); |
| 115 CHECK(!not_expect_callback_); | 121 CHECK(!not_expect_callback_); |
| 116 | 122 |
| 117 trailers_ = trailers; | 123 trailers_ = trailers; |
| 118 loop_->Quit(); | 124 loop_->Quit(); |
| 119 } | 125 } |
| 120 | 126 |
| 121 void OnFailed(int error) override { | 127 void OnFailed(int error) override { |
| 128 CHECK(!on_failed_called_); |
| 122 CHECK(!not_expect_callback_); | 129 CHECK(!not_expect_callback_); |
| 123 CHECK_EQ(OK, error_); | 130 CHECK_EQ(OK, error_); |
| 124 CHECK_NE(OK, error); | 131 CHECK_NE(OK, error); |
| 125 | 132 |
| 133 on_failed_called_ = true; |
| 126 error_ = error; | 134 error_ = error; |
| 127 loop_->Quit(); | 135 loop_->Quit(); |
| 128 } | 136 } |
| 129 | 137 |
| 130 void Start(const BidirectionalStreamRequestInfo* request_info, | 138 void Start(const BidirectionalStreamRequestInfo* request_info, |
| 131 const BoundNetLog& net_log, | 139 const BoundNetLog& net_log, |
| 132 const base::WeakPtr<QuicChromiumClientSession> session) { | 140 const base::WeakPtr<QuicChromiumClientSession> session) { |
| 133 stream_.reset(new BidirectionalStreamQuicImpl(session)); | 141 stream_.reset(new BidirectionalStreamQuicImpl(session)); |
| 134 stream_->Start(request_info, net_log, send_request_headers_automatically_, | 142 stream_->Start(request_info, net_log, send_request_headers_automatically_, |
| 135 this, nullptr); | 143 this, nullptr); |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 186 send_request_headers_automatically_ = false; | 194 send_request_headers_automatically_ = false; |
| 187 } | 195 } |
| 188 | 196 |
| 189 // Const getters for internal states. | 197 // Const getters for internal states. |
| 190 const std::string& data_received() const { return data_received_; } | 198 const std::string& data_received() const { return data_received_; } |
| 191 int error() const { return error_; } | 199 int error() const { return error_; } |
| 192 const SpdyHeaderBlock& response_headers() const { return response_headers_; } | 200 const SpdyHeaderBlock& response_headers() const { return response_headers_; } |
| 193 const SpdyHeaderBlock& trailers() const { return trailers_; } | 201 const SpdyHeaderBlock& trailers() const { return trailers_; } |
| 194 int on_data_read_count() const { return on_data_read_count_; } | 202 int on_data_read_count() const { return on_data_read_count_; } |
| 195 int on_data_sent_count() const { return on_data_sent_count_; } | 203 int on_data_sent_count() const { return on_data_sent_count_; } |
| 204 bool on_failed_called() const { return on_failed_called_; } |
| 196 | 205 |
| 197 protected: | 206 protected: |
| 198 // Quits |loop_|. | 207 // Quits |loop_|. |
| 199 void QuitLoop() { loop_->Quit(); } | 208 void QuitLoop() { loop_->Quit(); } |
| 200 | 209 |
| 201 // Deletes |stream_|. | 210 // Deletes |stream_|. |
| 202 void DeleteStream() { stream_.reset(); } | 211 void DeleteStream() { stream_.reset(); } |
| 203 | 212 |
| 204 private: | 213 private: |
| 205 std::unique_ptr<BidirectionalStreamQuicImpl> stream_; | 214 std::unique_ptr<BidirectionalStreamQuicImpl> stream_; |
| 206 scoped_refptr<IOBuffer> read_buf_; | 215 scoped_refptr<IOBuffer> read_buf_; |
| 207 int read_buf_len_; | 216 int read_buf_len_; |
| 208 std::unique_ptr<base::Timer> timer_; | 217 std::unique_ptr<base::Timer> timer_; |
| 209 std::string data_received_; | 218 std::string data_received_; |
| 210 std::unique_ptr<base::RunLoop> loop_; | 219 std::unique_ptr<base::RunLoop> loop_; |
| 211 SpdyHeaderBlock response_headers_; | 220 SpdyHeaderBlock response_headers_; |
| 212 SpdyHeaderBlock trailers_; | 221 SpdyHeaderBlock trailers_; |
| 213 int error_; | 222 int error_; |
| 214 int on_data_read_count_; | 223 int on_data_read_count_; |
| 215 int on_data_sent_count_; | 224 int on_data_sent_count_; |
| 216 // This is to ensure that delegate callback is not invoked synchronously when | 225 // This is to ensure that delegate callback is not invoked synchronously when |
| 217 // calling into |stream_|. | 226 // calling into |stream_|. |
| 218 bool not_expect_callback_; | 227 bool not_expect_callback_; |
| 228 bool on_failed_called_; |
| 219 CompletionCallback callback_; | 229 CompletionCallback callback_; |
| 220 bool send_request_headers_automatically_; | 230 bool send_request_headers_automatically_; |
| 221 | 231 |
| 222 DISALLOW_COPY_AND_ASSIGN(TestDelegateBase); | 232 DISALLOW_COPY_AND_ASSIGN(TestDelegateBase); |
| 223 }; | 233 }; |
| 224 | 234 |
| 225 // A delegate that deletes the stream in a particular callback. | 235 // A delegate that deletes the stream in a particular callback. |
| 226 class DeleteStreamDelegate : public TestDelegateBase { | 236 class DeleteStreamDelegate : public TestDelegateBase { |
| 227 public: | 237 public: |
| 228 // Specifies in which callback the stream can be deleted. | 238 // Specifies in which callback the stream can be deleted. |
| (...skipping 1045 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1274 | 1284 |
| 1275 // Send a DATA frame. | 1285 // Send a DATA frame. |
| 1276 scoped_refptr<StringIOBuffer> buf(new StringIOBuffer(kUploadData)); | 1286 scoped_refptr<StringIOBuffer> buf(new StringIOBuffer(kUploadData)); |
| 1277 | 1287 |
| 1278 delegate->SendData(buf, buf->size(), false); | 1288 delegate->SendData(buf, buf->size(), false); |
| 1279 delegate->WaitUntilNextCallback(); // OnDataSent | 1289 delegate->WaitUntilNextCallback(); // OnDataSent |
| 1280 | 1290 |
| 1281 delegate->CancelStream(); | 1291 delegate->CancelStream(); |
| 1282 base::MessageLoop::current()->RunUntilIdle(); | 1292 base::MessageLoop::current()->RunUntilIdle(); |
| 1283 | 1293 |
| 1294 // Try to send data after Cancel(), should not get called back. |
| 1295 delegate->SendData(buf, buf->size(), false); |
| 1296 base::MessageLoop::current()->RunUntilIdle(); |
| 1297 EXPECT_FALSE(delegate->on_failed_called()); |
| 1298 |
| 1284 EXPECT_EQ(0, delegate->on_data_read_count()); | 1299 EXPECT_EQ(0, delegate->on_data_read_count()); |
| 1285 EXPECT_EQ(1, delegate->on_data_sent_count()); | 1300 EXPECT_EQ(1, delegate->on_data_sent_count()); |
| 1286 EXPECT_EQ(kProtoQUIC1SPDY3, delegate->GetProtocol()); | 1301 EXPECT_EQ(kProtoQUIC1SPDY3, delegate->GetProtocol()); |
| 1287 EXPECT_EQ(static_cast<int64_t>(spdy_request_headers_frame_length + | 1302 EXPECT_EQ(static_cast<int64_t>(spdy_request_headers_frame_length + |
| 1288 strlen(kUploadData)), | 1303 strlen(kUploadData)), |
| 1289 delegate->GetTotalSentBytes()); | 1304 delegate->GetTotalSentBytes()); |
| 1290 EXPECT_EQ(static_cast<int64_t>(spdy_response_headers_frame_length), | 1305 EXPECT_EQ(static_cast<int64_t>(spdy_response_headers_frame_length), |
| 1291 delegate->GetTotalReceivedBytes()); | 1306 delegate->GetTotalReceivedBytes()); |
| 1292 } | 1307 } |
| 1293 | 1308 |
| 1294 TEST_P(BidirectionalStreamQuicImplTest, SessionClosedBeforeReadData) { | 1309 TEST_P(BidirectionalStreamQuicImplTest, SessionClosedBeforeReadData) { |
| 1295 SetRequest("GET", "/", DEFAULT_PRIORITY); | 1310 SetRequest("POST", "/", DEFAULT_PRIORITY); |
| 1296 size_t spdy_request_headers_frame_length; | 1311 size_t spdy_request_headers_frame_length; |
| 1297 AddWrite(ConstructRequestHeadersPacket(1, kFin, DEFAULT_PRIORITY, | 1312 AddWrite(ConstructRequestHeadersPacket(1, !kFin, DEFAULT_PRIORITY, |
| 1298 &spdy_request_headers_frame_length)); | 1313 &spdy_request_headers_frame_length)); |
| 1299 Initialize(); | 1314 Initialize(); |
| 1300 | 1315 |
| 1301 BidirectionalStreamRequestInfo request; | 1316 BidirectionalStreamRequestInfo request; |
| 1302 request.method = "GET"; | 1317 request.method = "POST"; |
| 1303 request.url = GURL("http://www.google.com/"); | 1318 request.url = GURL("http://www.google.com/"); |
| 1304 request.end_stream_on_headers = true; | 1319 request.end_stream_on_headers = false; |
| 1305 request.priority = DEFAULT_PRIORITY; | 1320 request.priority = DEFAULT_PRIORITY; |
| 1306 | 1321 |
| 1307 scoped_refptr<IOBuffer> read_buffer(new IOBuffer(kReadBufferSize)); | 1322 scoped_refptr<IOBuffer> read_buffer(new IOBuffer(kReadBufferSize)); |
| 1308 std::unique_ptr<TestDelegateBase> delegate( | 1323 std::unique_ptr<TestDelegateBase> delegate( |
| 1309 new TestDelegateBase(read_buffer.get(), kReadBufferSize)); | 1324 new TestDelegateBase(read_buffer.get(), kReadBufferSize)); |
| 1310 delegate->Start(&request, net_log().bound(), session()->GetWeakPtr()); | 1325 delegate->Start(&request, net_log().bound(), session()->GetWeakPtr()); |
| 1311 delegate->WaitUntilNextCallback(); // OnStreamReady | 1326 delegate->WaitUntilNextCallback(); // OnStreamReady |
| 1312 | 1327 |
| 1313 // Server acks the request. | 1328 // Server acks the request. |
| 1314 ProcessPacket(ConstructServerAckPacket(1, 0, 0)); | 1329 ProcessPacket(ConstructServerAckPacket(1, 0, 0)); |
| 1315 | 1330 |
| 1316 // Server sends the response headers. | 1331 // Server sends the response headers. |
| 1317 SpdyHeaderBlock response_headers = ConstructResponseHeaders("200"); | 1332 SpdyHeaderBlock response_headers = ConstructResponseHeaders("200"); |
| 1318 | 1333 |
| 1319 size_t spdy_response_headers_frame_length; | 1334 size_t spdy_response_headers_frame_length; |
| 1320 QuicStreamOffset offset = 0; | 1335 QuicStreamOffset offset = 0; |
| 1321 ProcessPacket(ConstructResponseHeadersPacket( | 1336 ProcessPacket(ConstructResponseHeadersPacket( |
| 1322 2, !kFin, response_headers, &spdy_response_headers_frame_length, | 1337 2, !kFin, response_headers, &spdy_response_headers_frame_length, |
| 1323 &offset)); | 1338 &offset)); |
| 1324 | 1339 |
| 1325 delegate->WaitUntilNextCallback(); // OnHeadersReceived | 1340 delegate->WaitUntilNextCallback(); // OnHeadersReceived |
| 1326 TestCompletionCallback cb; | 1341 TestCompletionCallback cb; |
| 1327 int rv = delegate->ReadData(cb.callback()); | 1342 int rv = delegate->ReadData(cb.callback()); |
| 1328 EXPECT_EQ(ERR_IO_PENDING, rv); | 1343 EXPECT_EQ(ERR_IO_PENDING, rv); |
| 1329 session()->connection()->CloseConnection( | 1344 session()->connection()->CloseConnection( |
| 1330 QUIC_NO_ERROR, "test", ConnectionCloseBehavior::SILENT_CLOSE); | 1345 QUIC_NO_ERROR, "test", ConnectionCloseBehavior::SILENT_CLOSE); |
| 1331 delegate->WaitUntilNextCallback(); // OnFailed | 1346 delegate->WaitUntilNextCallback(); // OnFailed |
| 1347 EXPECT_TRUE(delegate->on_failed_called()); |
| 1332 | 1348 |
| 1349 // Try to send data after OnFailed(), should not get called back. |
| 1350 scoped_refptr<StringIOBuffer> buf(new StringIOBuffer(kUploadData)); |
| 1351 delegate->SendData(buf, buf->size(), false); |
| 1333 base::MessageLoop::current()->RunUntilIdle(); | 1352 base::MessageLoop::current()->RunUntilIdle(); |
| 1334 | 1353 |
| 1335 EXPECT_EQ(ERR_UNEXPECTED, delegate->ReadData(cb.callback())); | 1354 EXPECT_EQ(ERR_UNEXPECTED, delegate->ReadData(cb.callback())); |
| 1336 EXPECT_EQ(ERR_UNEXPECTED, delegate->error()); | 1355 EXPECT_EQ(ERR_UNEXPECTED, delegate->error()); |
| 1337 EXPECT_EQ(0, delegate->on_data_read_count()); | 1356 EXPECT_EQ(0, delegate->on_data_read_count()); |
| 1338 EXPECT_EQ(0, delegate->on_data_sent_count()); | 1357 EXPECT_EQ(0, delegate->on_data_sent_count()); |
| 1339 EXPECT_EQ(kProtoQUIC1SPDY3, delegate->GetProtocol()); | 1358 EXPECT_EQ(kProtoQUIC1SPDY3, delegate->GetProtocol()); |
| 1340 EXPECT_EQ(static_cast<int64_t>(spdy_request_headers_frame_length), | 1359 EXPECT_EQ(static_cast<int64_t>(spdy_request_headers_frame_length), |
| 1341 delegate->GetTotalSentBytes()); | 1360 delegate->GetTotalSentBytes()); |
| 1342 EXPECT_EQ(static_cast<int64_t>(spdy_response_headers_frame_length), | 1361 EXPECT_EQ(static_cast<int64_t>(spdy_response_headers_frame_length), |
| (...skipping 207 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1550 | 1569 |
| 1551 base::MessageLoop::current()->RunUntilIdle(); | 1570 base::MessageLoop::current()->RunUntilIdle(); |
| 1552 | 1571 |
| 1553 EXPECT_EQ(1, delegate->on_data_read_count()); | 1572 EXPECT_EQ(1, delegate->on_data_read_count()); |
| 1554 EXPECT_EQ(0, delegate->on_data_sent_count()); | 1573 EXPECT_EQ(0, delegate->on_data_sent_count()); |
| 1555 } | 1574 } |
| 1556 | 1575 |
| 1557 } // namespace test | 1576 } // namespace test |
| 1558 | 1577 |
| 1559 } // namespace net | 1578 } // namespace net |
| OLD | NEW |