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