OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/socket/socket_test_util.h" | 5 #include "net/socket/socket_test_util.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
10 #include "base/basictypes.h" | 10 #include "base/basictypes.h" |
(...skipping 158 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
169 MockWriteResult StaticSocketDataProvider::OnWrite(const std::string& data) { | 169 MockWriteResult StaticSocketDataProvider::OnWrite(const std::string& data) { |
170 if (!writes_) { | 170 if (!writes_) { |
171 // Not using mock writes; succeed synchronously. | 171 // Not using mock writes; succeed synchronously. |
172 return MockWriteResult(false, data.length()); | 172 return MockWriteResult(false, data.length()); |
173 } | 173 } |
174 | 174 |
175 DCHECK(!at_write_eof()); | 175 DCHECK(!at_write_eof()); |
176 | 176 |
177 // Check that what we are writing matches the expectation. | 177 // Check that what we are writing matches the expectation. |
178 // Then give the mocked return value. | 178 // Then give the mocked return value. |
179 net::MockWrite* w = &writes_[write_index_++]; | 179 MockWrite* w = &writes_[write_index_++]; |
180 w->time_stamp = base::Time::Now(); | 180 w->time_stamp = base::Time::Now(); |
181 int result = w->result; | 181 int result = w->result; |
182 if (w->data) { | 182 if (w->data) { |
183 // Note - we can simulate a partial write here. If the expected data | 183 // Note - we can simulate a partial write here. If the expected data |
184 // is a match, but shorter than the write actually written, that is legal. | 184 // is a match, but shorter than the write actually written, that is legal. |
185 // Example: | 185 // Example: |
186 // Application writes "foobarbaz" (9 bytes) | 186 // Application writes "foobarbaz" (9 bytes) |
187 // Expected write was "foo" (3 bytes) | 187 // Expected write was "foo" (3 bytes) |
188 // This is a success, and we return 3 to the application. | 188 // This is a success, and we return 3 to the application. |
189 std::string expected_data(w->data, w->data_len); | 189 std::string expected_data(w->data, w->data_len); |
190 EXPECT_GE(data.length(), expected_data.length()); | 190 EXPECT_GE(data.length(), expected_data.length()); |
191 std::string actual_data(data.substr(0, w->data_len)); | 191 std::string actual_data(data.substr(0, w->data_len)); |
192 EXPECT_EQ(expected_data, actual_data); | 192 EXPECT_EQ(expected_data, actual_data); |
193 if (expected_data != actual_data) | 193 if (expected_data != actual_data) |
194 return MockWriteResult(false, net::ERR_UNEXPECTED); | 194 return MockWriteResult(false, ERR_UNEXPECTED); |
195 if (result == net::OK) | 195 if (result == OK) |
196 result = w->data_len; | 196 result = w->data_len; |
197 } | 197 } |
198 return MockWriteResult(w->async, result); | 198 return MockWriteResult(w->async, result); |
199 } | 199 } |
200 | 200 |
201 void StaticSocketDataProvider::Reset() { | 201 void StaticSocketDataProvider::Reset() { |
202 read_index_ = 0; | 202 read_index_ = 0; |
203 write_index_ = 0; | 203 write_index_ = 0; |
204 } | 204 } |
205 | 205 |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
245 } | 245 } |
246 | 246 |
247 SSLSocketDataProvider::~SSLSocketDataProvider() { | 247 SSLSocketDataProvider::~SSLSocketDataProvider() { |
248 } | 248 } |
249 | 249 |
250 DelayedSocketData::DelayedSocketData( | 250 DelayedSocketData::DelayedSocketData( |
251 int write_delay, MockRead* reads, size_t reads_count, | 251 int write_delay, MockRead* reads, size_t reads_count, |
252 MockWrite* writes, size_t writes_count) | 252 MockWrite* writes, size_t writes_count) |
253 : StaticSocketDataProvider(reads, reads_count, writes, writes_count), | 253 : StaticSocketDataProvider(reads, reads_count, writes, writes_count), |
254 write_delay_(write_delay), | 254 write_delay_(write_delay), |
255 ALLOW_THIS_IN_INITIALIZER_LIST(factory_(this)) { | 255 ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)) { |
256 DCHECK_GE(write_delay_, 0); | 256 DCHECK_GE(write_delay_, 0); |
257 } | 257 } |
258 | 258 |
259 DelayedSocketData::DelayedSocketData( | 259 DelayedSocketData::DelayedSocketData( |
260 const MockConnect& connect, int write_delay, MockRead* reads, | 260 const MockConnect& connect, int write_delay, MockRead* reads, |
261 size_t reads_count, MockWrite* writes, size_t writes_count) | 261 size_t reads_count, MockWrite* writes, size_t writes_count) |
262 : StaticSocketDataProvider(reads, reads_count, writes, writes_count), | 262 : StaticSocketDataProvider(reads, reads_count, writes, writes_count), |
263 write_delay_(write_delay), | 263 write_delay_(write_delay), |
264 ALLOW_THIS_IN_INITIALIZER_LIST(factory_(this)) { | 264 ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)) { |
265 DCHECK_GE(write_delay_, 0); | 265 DCHECK_GE(write_delay_, 0); |
266 set_connect_data(connect); | 266 set_connect_data(connect); |
267 } | 267 } |
268 | 268 |
269 DelayedSocketData::~DelayedSocketData() { | 269 DelayedSocketData::~DelayedSocketData() { |
270 } | 270 } |
271 | 271 |
272 void DelayedSocketData::ForceNextRead() { | 272 void DelayedSocketData::ForceNextRead() { |
273 write_delay_ = 0; | 273 write_delay_ = 0; |
274 CompleteRead(); | 274 CompleteRead(); |
275 } | 275 } |
276 | 276 |
277 MockRead DelayedSocketData::GetNextRead() { | 277 MockRead DelayedSocketData::GetNextRead() { |
278 if (write_delay_ > 0) | 278 if (write_delay_ > 0) |
279 return MockRead(true, ERR_IO_PENDING); | 279 return MockRead(true, ERR_IO_PENDING); |
280 return StaticSocketDataProvider::GetNextRead(); | 280 return StaticSocketDataProvider::GetNextRead(); |
281 } | 281 } |
282 | 282 |
283 MockWriteResult DelayedSocketData::OnWrite(const std::string& data) { | 283 MockWriteResult DelayedSocketData::OnWrite(const std::string& data) { |
284 MockWriteResult rv = StaticSocketDataProvider::OnWrite(data); | 284 MockWriteResult rv = StaticSocketDataProvider::OnWrite(data); |
285 // Now that our write has completed, we can allow reads to continue. | 285 // Now that our write has completed, we can allow reads to continue. |
286 if (!--write_delay_) | 286 if (!--write_delay_) |
287 MessageLoop::current()->PostDelayedTask(FROM_HERE, | 287 MessageLoop::current()->PostDelayedTask( |
288 factory_.NewRunnableMethod(&DelayedSocketData::CompleteRead), 100); | 288 FROM_HERE, |
| 289 base::Bind(&DelayedSocketData::CompleteRead, |
| 290 weak_factory_.GetWeakPtr()), |
| 291 100); |
289 return rv; | 292 return rv; |
290 } | 293 } |
291 | 294 |
292 void DelayedSocketData::Reset() { | 295 void DelayedSocketData::Reset() { |
293 set_socket(NULL); | 296 set_socket(NULL); |
294 factory_.RevokeAll(); | 297 weak_factory_.InvalidateWeakPtrs(); |
295 StaticSocketDataProvider::Reset(); | 298 StaticSocketDataProvider::Reset(); |
296 } | 299 } |
297 | 300 |
298 void DelayedSocketData::CompleteRead() { | 301 void DelayedSocketData::CompleteRead() { |
299 if (socket()) | 302 if (socket()) |
300 socket()->OnReadComplete(GetNextRead()); | 303 socket()->OnReadComplete(GetNextRead()); |
301 } | 304 } |
302 | 305 |
303 OrderedSocketData::OrderedSocketData( | 306 OrderedSocketData::OrderedSocketData( |
304 MockRead* reads, size_t reads_count, MockWrite* writes, size_t writes_count) | 307 MockRead* reads, size_t reads_count, MockWrite* writes, size_t writes_count) |
305 : StaticSocketDataProvider(reads, reads_count, writes, writes_count), | 308 : StaticSocketDataProvider(reads, reads_count, writes, writes_count), |
306 sequence_number_(0), loop_stop_stage_(0), callback_(NULL), | 309 sequence_number_(0), loop_stop_stage_(0), |
307 blocked_(false), ALLOW_THIS_IN_INITIALIZER_LIST(factory_(this)) { | 310 blocked_(false), ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)) { |
308 } | 311 } |
309 | 312 |
310 OrderedSocketData::OrderedSocketData( | 313 OrderedSocketData::OrderedSocketData( |
311 const MockConnect& connect, | 314 const MockConnect& connect, |
312 MockRead* reads, size_t reads_count, | 315 MockRead* reads, size_t reads_count, |
313 MockWrite* writes, size_t writes_count) | 316 MockWrite* writes, size_t writes_count) |
314 : StaticSocketDataProvider(reads, reads_count, writes, writes_count), | 317 : StaticSocketDataProvider(reads, reads_count, writes, writes_count), |
315 sequence_number_(0), loop_stop_stage_(0), callback_(NULL), | 318 sequence_number_(0), loop_stop_stage_(0), |
316 blocked_(false), ALLOW_THIS_IN_INITIALIZER_LIST(factory_(this)) { | 319 blocked_(false), ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)) { |
317 set_connect_data(connect); | 320 set_connect_data(connect); |
318 } | 321 } |
319 | 322 |
320 void OrderedSocketData::EndLoop() { | 323 void OrderedSocketData::EndLoop() { |
321 // If we've already stopped the loop, don't do it again until we've advanced | 324 // If we've already stopped the loop, don't do it again until we've advanced |
322 // to the next sequence_number. | 325 // to the next sequence_number. |
323 NET_TRACE(INFO, " *** ") << "Stage " << sequence_number_ << ": EndLoop()"; | 326 NET_TRACE(INFO, " *** ") << "Stage " << sequence_number_ << ": EndLoop()"; |
324 if (loop_stop_stage_ > 0) { | 327 if (loop_stop_stage_ > 0) { |
325 const MockRead& next_read = StaticSocketDataProvider::PeekRead(); | 328 const MockRead& next_read = StaticSocketDataProvider::PeekRead(); |
326 if ((next_read.sequence_number & ~MockRead::STOPLOOP) > | 329 if ((next_read.sequence_number & ~MockRead::STOPLOOP) > |
327 loop_stop_stage_) { | 330 loop_stop_stage_) { |
328 NET_TRACE(INFO, " *** ") << "Stage " << sequence_number_ | 331 NET_TRACE(INFO, " *** ") << "Stage " << sequence_number_ |
329 << ": Clearing stop index"; | 332 << ": Clearing stop index"; |
330 loop_stop_stage_ = 0; | 333 loop_stop_stage_ = 0; |
331 } else { | 334 } else { |
332 return; | 335 return; |
333 } | 336 } |
334 } | 337 } |
335 // Record the sequence_number at which we stopped the loop. | 338 // Record the sequence_number at which we stopped the loop. |
336 NET_TRACE(INFO, " *** ") << "Stage " << sequence_number_ | 339 NET_TRACE(INFO, " *** ") << "Stage " << sequence_number_ |
337 << ": Posting Quit at read " << read_index(); | 340 << ": Posting Quit at read " << read_index(); |
338 loop_stop_stage_ = sequence_number_; | 341 loop_stop_stage_ = sequence_number_; |
339 if (callback_) | 342 if (!callback_.is_null()) |
340 callback_->RunWithParams(Tuple1<int>(ERR_IO_PENDING)); | 343 callback_.Run(ERR_IO_PENDING); |
341 } | 344 } |
342 | 345 |
343 MockRead OrderedSocketData::GetNextRead() { | 346 MockRead OrderedSocketData::GetNextRead() { |
344 factory_.RevokeAll(); | 347 weak_factory_.InvalidateWeakPtrs(); |
345 blocked_ = false; | 348 blocked_ = false; |
346 const MockRead& next_read = StaticSocketDataProvider::PeekRead(); | 349 const MockRead& next_read = StaticSocketDataProvider::PeekRead(); |
347 if (next_read.sequence_number & MockRead::STOPLOOP) | 350 if (next_read.sequence_number & MockRead::STOPLOOP) |
348 EndLoop(); | 351 EndLoop(); |
349 if ((next_read.sequence_number & ~MockRead::STOPLOOP) <= | 352 if ((next_read.sequence_number & ~MockRead::STOPLOOP) <= |
350 sequence_number_++) { | 353 sequence_number_++) { |
351 NET_TRACE(INFO, " *** ") << "Stage " << sequence_number_ - 1 | 354 NET_TRACE(INFO, " *** ") << "Stage " << sequence_number_ - 1 |
352 << ": Read " << read_index(); | 355 << ": Read " << read_index(); |
353 DumpMockRead(next_read); | 356 DumpMockRead(next_read); |
354 return StaticSocketDataProvider::GetNextRead(); | 357 return StaticSocketDataProvider::GetNextRead(); |
(...skipping 12 matching lines...) Expand all Loading... |
367 DumpMockRead(PeekWrite()); | 370 DumpMockRead(PeekWrite()); |
368 ++sequence_number_; | 371 ++sequence_number_; |
369 if (blocked_) { | 372 if (blocked_) { |
370 // TODO(willchan): This 100ms delay seems to work around some weirdness. We | 373 // TODO(willchan): This 100ms delay seems to work around some weirdness. We |
371 // should probably fix the weirdness. One example is in SpdyStream, | 374 // should probably fix the weirdness. One example is in SpdyStream, |
372 // DoSendRequest() will return ERR_IO_PENDING, and there's a race. If the | 375 // DoSendRequest() will return ERR_IO_PENDING, and there's a race. If the |
373 // SYN_REPLY causes OnResponseReceived() to get called before | 376 // SYN_REPLY causes OnResponseReceived() to get called before |
374 // SpdyStream::ReadResponseHeaders() is called, we hit a NOTREACHED(). | 377 // SpdyStream::ReadResponseHeaders() is called, we hit a NOTREACHED(). |
375 MessageLoop::current()->PostDelayedTask( | 378 MessageLoop::current()->PostDelayedTask( |
376 FROM_HERE, | 379 FROM_HERE, |
377 factory_.NewRunnableMethod(&OrderedSocketData::CompleteRead), 100); | 380 base::Bind(&OrderedSocketData::CompleteRead, |
| 381 weak_factory_.GetWeakPtr()), |
| 382 100); |
378 } | 383 } |
379 return StaticSocketDataProvider::OnWrite(data); | 384 return StaticSocketDataProvider::OnWrite(data); |
380 } | 385 } |
381 | 386 |
382 void OrderedSocketData::Reset() { | 387 void OrderedSocketData::Reset() { |
383 NET_TRACE(INFO, " *** ") << "Stage " | 388 NET_TRACE(INFO, " *** ") << "Stage " |
384 << sequence_number_ << ": Reset()"; | 389 << sequence_number_ << ": Reset()"; |
385 sequence_number_ = 0; | 390 sequence_number_ = 0; |
386 loop_stop_stage_ = 0; | 391 loop_stop_stage_ = 0; |
387 set_socket(NULL); | 392 set_socket(NULL); |
388 factory_.RevokeAll(); | 393 weak_factory_.InvalidateWeakPtrs(); |
389 StaticSocketDataProvider::Reset(); | 394 StaticSocketDataProvider::Reset(); |
390 } | 395 } |
391 | 396 |
392 void OrderedSocketData::CompleteRead() { | 397 void OrderedSocketData::CompleteRead() { |
393 if (socket()) { | 398 if (socket()) { |
394 NET_TRACE(INFO, " *** ") << "Stage " << sequence_number_; | 399 NET_TRACE(INFO, " *** ") << "Stage " << sequence_number_; |
395 socket()->OnReadComplete(GetNextRead()); | 400 socket()->OnReadComplete(GetNextRead()); |
396 } | 401 } |
397 } | 402 } |
398 | 403 |
(...skipping 187 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
586 | 591 |
587 MockSSLClientSocket* MockClientSocketFactory::GetMockSSLClientSocket( | 592 MockSSLClientSocket* MockClientSocketFactory::GetMockSSLClientSocket( |
588 size_t index) const { | 593 size_t index) const { |
589 DCHECK_LT(index, ssl_client_sockets_.size()); | 594 DCHECK_LT(index, ssl_client_sockets_.size()); |
590 return ssl_client_sockets_[index]; | 595 return ssl_client_sockets_[index]; |
591 } | 596 } |
592 | 597 |
593 DatagramClientSocket* MockClientSocketFactory::CreateDatagramClientSocket( | 598 DatagramClientSocket* MockClientSocketFactory::CreateDatagramClientSocket( |
594 DatagramSocket::BindType bind_type, | 599 DatagramSocket::BindType bind_type, |
595 const RandIntCallback& rand_int_cb, | 600 const RandIntCallback& rand_int_cb, |
596 NetLog* net_log, | 601 net::NetLog* net_log, |
597 const NetLog::Source& source) { | 602 const net::NetLog::Source& source) { |
598 SocketDataProvider* data_provider = mock_data_.GetNext(); | 603 SocketDataProvider* data_provider = mock_data_.GetNext(); |
599 MockUDPClientSocket* socket = new MockUDPClientSocket(data_provider, net_log); | 604 MockUDPClientSocket* socket = new MockUDPClientSocket(data_provider, net_log); |
600 data_provider->set_socket(socket); | 605 data_provider->set_socket(socket); |
601 udp_client_sockets_.push_back(socket); | 606 udp_client_sockets_.push_back(socket); |
602 return socket; | 607 return socket; |
603 } | 608 } |
604 | 609 |
605 StreamSocket* MockClientSocketFactory::CreateTransportClientSocket( | 610 StreamSocket* MockClientSocketFactory::CreateTransportClientSocket( |
606 const AddressList& addresses, | 611 const AddressList& addresses, |
607 net::NetLog* net_log, | 612 net::NetLog* net_log, |
608 const NetLog::Source& source) { | 613 const net::NetLog::Source& source) { |
609 SocketDataProvider* data_provider = mock_data_.GetNext(); | 614 SocketDataProvider* data_provider = mock_data_.GetNext(); |
610 MockTCPClientSocket* socket = | 615 MockTCPClientSocket* socket = |
611 new MockTCPClientSocket(addresses, net_log, data_provider); | 616 new MockTCPClientSocket(addresses, net_log, data_provider); |
612 data_provider->set_socket(socket); | 617 data_provider->set_socket(socket); |
613 tcp_client_sockets_.push_back(socket); | 618 tcp_client_sockets_.push_back(socket); |
614 return socket; | 619 return socket; |
615 } | 620 } |
616 | 621 |
617 SSLClientSocket* MockClientSocketFactory::CreateSSLClientSocket( | 622 SSLClientSocket* MockClientSocketFactory::CreateSSLClientSocket( |
618 ClientSocketHandle* transport_socket, | 623 ClientSocketHandle* transport_socket, |
619 const HostPortPair& host_and_port, | 624 const HostPortPair& host_and_port, |
620 const SSLConfig& ssl_config, | 625 const SSLConfig& ssl_config, |
621 SSLHostInfo* ssl_host_info, | 626 SSLHostInfo* ssl_host_info, |
622 const SSLClientSocketContext& context) { | 627 const SSLClientSocketContext& context) { |
623 MockSSLClientSocket* socket = | 628 MockSSLClientSocket* socket = |
624 new MockSSLClientSocket(transport_socket, host_and_port, ssl_config, | 629 new MockSSLClientSocket(transport_socket, host_and_port, ssl_config, |
625 ssl_host_info, mock_ssl_data_.GetNext()); | 630 ssl_host_info, mock_ssl_data_.GetNext()); |
626 ssl_client_sockets_.push_back(socket); | 631 ssl_client_sockets_.push_back(socket); |
627 return socket; | 632 return socket; |
628 } | 633 } |
629 | 634 |
630 void MockClientSocketFactory::ClearSSLSessionCache() { | 635 void MockClientSocketFactory::ClearSSLSessionCache() { |
631 } | 636 } |
632 | 637 |
633 MockClientSocket::MockClientSocket(net::NetLog* net_log) | 638 MockClientSocket::MockClientSocket(net::NetLog* net_log) |
634 : ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)), | 639 : ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)), |
635 connected_(false), | 640 connected_(false), |
636 net_log_(NetLog::Source(), net_log) { | 641 net_log_(net::NetLog::Source(), net_log) { |
637 } | 642 } |
638 | 643 |
639 bool MockClientSocket::SetReceiveBufferSize(int32 size) { | 644 bool MockClientSocket::SetReceiveBufferSize(int32 size) { |
640 return true; | 645 return true; |
641 } | 646 } |
642 | 647 |
643 bool MockClientSocket::SetSendBufferSize(int32 size) { | 648 bool MockClientSocket::SetSendBufferSize(int32 size) { |
644 return true; | 649 return true; |
645 } | 650 } |
646 | 651 |
(...skipping 22 matching lines...) Expand all Loading... |
669 bool rv = ParseIPLiteralToNumber("192.0.2.33", &ip); | 674 bool rv = ParseIPLiteralToNumber("192.0.2.33", &ip); |
670 CHECK(rv); | 675 CHECK(rv); |
671 *address = IPEndPoint(ip, 123); | 676 *address = IPEndPoint(ip, 123); |
672 return OK; | 677 return OK; |
673 } | 678 } |
674 | 679 |
675 const BoundNetLog& MockClientSocket::NetLog() const { | 680 const BoundNetLog& MockClientSocket::NetLog() const { |
676 return net_log_; | 681 return net_log_; |
677 } | 682 } |
678 | 683 |
679 void MockClientSocket::GetSSLInfo(net::SSLInfo* ssl_info) { | 684 void MockClientSocket::GetSSLInfo(SSLInfo* ssl_info) { |
680 NOTREACHED(); | 685 NOTREACHED(); |
681 } | 686 } |
682 | 687 |
683 void MockClientSocket::GetSSLCertRequestInfo( | 688 void MockClientSocket::GetSSLCertRequestInfo( |
684 net::SSLCertRequestInfo* cert_request_info) { | 689 SSLCertRequestInfo* cert_request_info) { |
685 } | 690 } |
686 | 691 |
687 int MockClientSocket::ExportKeyingMaterial(const base::StringPiece& label, | 692 int MockClientSocket::ExportKeyingMaterial(const base::StringPiece& label, |
688 const base::StringPiece& context, | 693 const base::StringPiece& context, |
689 unsigned char *out, | 694 unsigned char *out, |
690 unsigned int outlen) { | 695 unsigned int outlen) { |
691 NOTREACHED(); | 696 NOTREACHED(); |
692 return ERR_NOT_IMPLEMENTED; | 697 return ERR_NOT_IMPLEMENTED; |
693 } | 698 } |
694 | 699 |
695 SSLClientSocket::NextProtoStatus | 700 SSLClientSocket::NextProtoStatus |
696 MockClientSocket::GetNextProto(std::string* proto, std::string* server_protos) { | 701 MockClientSocket::GetNextProto(std::string* proto, std::string* server_protos) { |
697 proto->clear(); | 702 proto->clear(); |
698 server_protos->clear(); | 703 server_protos->clear(); |
699 return SSLClientSocket::kNextProtoUnsupported; | 704 return SSLClientSocket::kNextProtoUnsupported; |
700 } | 705 } |
701 | 706 |
702 MockClientSocket::~MockClientSocket() {} | 707 MockClientSocket::~MockClientSocket() {} |
703 | 708 |
704 void MockClientSocket::RunCallbackAsync(net::OldCompletionCallback* callback, | 709 void MockClientSocket::RunCallbackAsync(const CompletionCallback& callback, |
705 int result) { | |
706 MessageLoop::current()->PostTask(FROM_HERE, | |
707 base::Bind(&MockClientSocket::RunOldCallback, weak_factory_.GetWeakPtr(), | |
708 callback, result)); | |
709 } | |
710 void MockClientSocket::RunCallbackAsync(const net::CompletionCallback& callback, | |
711 int result) { | 710 int result) { |
712 MessageLoop::current()->PostTask(FROM_HERE, | 711 MessageLoop::current()->PostTask(FROM_HERE, |
713 base::Bind(&MockClientSocket::RunCallback, weak_factory_.GetWeakPtr(), | 712 base::Bind(&MockClientSocket::RunCallback, weak_factory_.GetWeakPtr(), |
714 callback, result)); | 713 callback, result)); |
715 } | 714 } |
716 | 715 |
717 void MockClientSocket::RunOldCallback(net::OldCompletionCallback* callback, | |
718 int result) { | |
719 if (callback) | |
720 callback->Run(result); | |
721 } | |
722 void MockClientSocket::RunCallback(const net::CompletionCallback& callback, | 716 void MockClientSocket::RunCallback(const net::CompletionCallback& callback, |
723 int result) { | 717 int result) { |
724 if (!callback.is_null()) | 718 if (!callback.is_null()) |
725 callback.Run(result); | 719 callback.Run(result); |
726 } | 720 } |
727 | 721 |
728 MockTCPClientSocket::MockTCPClientSocket(const net::AddressList& addresses, | 722 MockTCPClientSocket::MockTCPClientSocket(const AddressList& addresses, |
729 net::NetLog* net_log, | 723 net::NetLog* net_log, |
730 net::SocketDataProvider* data) | 724 SocketDataProvider* data) |
731 : MockClientSocket(net_log), | 725 : MockClientSocket(net_log), |
732 addresses_(addresses), | 726 addresses_(addresses), |
733 data_(data), | 727 data_(data), |
734 read_offset_(0), | 728 read_offset_(0), |
735 num_bytes_read_(0), | 729 num_bytes_read_(0), |
736 read_data_(false, net::ERR_UNEXPECTED), | 730 read_data_(false, ERR_UNEXPECTED), |
737 need_read_data_(true), | 731 need_read_data_(true), |
738 peer_closed_connection_(false), | 732 peer_closed_connection_(false), |
739 pending_buf_(NULL), | 733 pending_buf_(NULL), |
740 pending_buf_len_(0), | 734 pending_buf_len_(0), |
741 old_pending_callback_(NULL), | |
742 was_used_to_convey_data_(false) { | 735 was_used_to_convey_data_(false) { |
743 DCHECK(data_); | 736 DCHECK(data_); |
744 data_->Reset(); | 737 data_->Reset(); |
745 } | 738 } |
746 | 739 |
747 MockTCPClientSocket::~MockTCPClientSocket() {} | 740 MockTCPClientSocket::~MockTCPClientSocket() {} |
748 | 741 |
749 int MockTCPClientSocket::Read(net::IOBuffer* buf, int buf_len, | 742 int MockTCPClientSocket::Read(IOBuffer* buf, int buf_len, |
750 net::OldCompletionCallback* callback) { | 743 const CompletionCallback& callback) { |
751 if (!connected_) | 744 if (!connected_) |
752 return net::ERR_UNEXPECTED; | 745 return ERR_UNEXPECTED; |
753 | 746 |
754 // If the buffer is already in use, a read is already in progress! | 747 // If the buffer is already in use, a read is already in progress! |
755 DCHECK(pending_buf_ == NULL); | 748 DCHECK(pending_buf_ == NULL); |
756 | |
757 // Store our async IO data. | |
758 pending_buf_ = buf; | |
759 pending_buf_len_ = buf_len; | |
760 old_pending_callback_ = callback; | |
761 | |
762 if (need_read_data_) { | |
763 read_data_ = data_->GetNextRead(); | |
764 if (read_data_.result == ERR_TEST_PEER_CLOSE_AFTER_NEXT_MOCK_READ) { | |
765 // This MockRead is just a marker to instruct us to set | |
766 // peer_closed_connection_. Skip it and get the next one. | |
767 read_data_ = data_->GetNextRead(); | |
768 peer_closed_connection_ = true; | |
769 } | |
770 // ERR_IO_PENDING means that the SocketDataProvider is taking responsibility | |
771 // to complete the async IO manually later (via OnReadComplete). | |
772 if (read_data_.result == ERR_IO_PENDING) { | |
773 DCHECK(callback); // We need to be using async IO in this case. | |
774 return ERR_IO_PENDING; | |
775 } | |
776 need_read_data_ = false; | |
777 } | |
778 | |
779 return CompleteRead(); | |
780 } | |
781 int MockTCPClientSocket::Read(net::IOBuffer* buf, int buf_len, | |
782 const net::CompletionCallback& callback) { | |
783 if (!connected_) | |
784 return net::ERR_UNEXPECTED; | |
785 | |
786 // If the buffer is already in use, a read is already in progress! | |
787 DCHECK(pending_buf_ == NULL); | |
788 | 749 |
789 // Store our async IO data. | 750 // Store our async IO data. |
790 pending_buf_ = buf; | 751 pending_buf_ = buf; |
791 pending_buf_len_ = buf_len; | 752 pending_buf_len_ = buf_len; |
792 pending_callback_ = callback; | 753 pending_callback_ = callback; |
793 | 754 |
794 if (need_read_data_) { | 755 if (need_read_data_) { |
795 read_data_ = data_->GetNextRead(); | 756 read_data_ = data_->GetNextRead(); |
796 if (read_data_.result == ERR_TEST_PEER_CLOSE_AFTER_NEXT_MOCK_READ) { | 757 if (read_data_.result == ERR_TEST_PEER_CLOSE_AFTER_NEXT_MOCK_READ) { |
797 // This MockRead is just a marker to instruct us to set | 758 // This MockRead is just a marker to instruct us to set |
798 // peer_closed_connection_. Skip it and get the next one. | 759 // peer_closed_connection_. Skip it and get the next one. |
799 read_data_ = data_->GetNextRead(); | 760 read_data_ = data_->GetNextRead(); |
800 peer_closed_connection_ = true; | 761 peer_closed_connection_ = true; |
801 } | 762 } |
802 // ERR_IO_PENDING means that the SocketDataProvider is taking responsibility | 763 // ERR_IO_PENDING means that the SocketDataProvider is taking responsibility |
803 // to complete the async IO manually later (via OnReadComplete). | 764 // to complete the async IO manually later (via OnReadComplete). |
804 if (read_data_.result == ERR_IO_PENDING) { | 765 if (read_data_.result == ERR_IO_PENDING) { |
805 // We need to be using async IO in this case. | 766 // We need to be using async IO in this case. |
806 DCHECK(!callback.is_null()); | 767 DCHECK(!callback.is_null()); |
807 return ERR_IO_PENDING; | 768 return ERR_IO_PENDING; |
808 } | 769 } |
809 need_read_data_ = false; | 770 need_read_data_ = false; |
810 } | 771 } |
811 | 772 |
812 return CompleteRead(); | 773 return CompleteRead(); |
813 } | 774 } |
814 | 775 |
815 int MockTCPClientSocket::Write(net::IOBuffer* buf, int buf_len, | 776 int MockTCPClientSocket::Write(IOBuffer* buf, int buf_len, |
816 net::OldCompletionCallback* callback) { | 777 const CompletionCallback& callback) { |
817 DCHECK(buf); | 778 DCHECK(buf); |
818 DCHECK_GT(buf_len, 0); | 779 DCHECK_GT(buf_len, 0); |
819 | 780 |
820 if (!connected_) | 781 if (!connected_) |
821 return net::ERR_UNEXPECTED; | 782 return ERR_UNEXPECTED; |
822 | 783 |
823 std::string data(buf->data(), buf_len); | 784 std::string data(buf->data(), buf_len); |
824 net::MockWriteResult write_result = data_->OnWrite(data); | 785 MockWriteResult write_result = data_->OnWrite(data); |
825 | 786 |
826 was_used_to_convey_data_ = true; | 787 was_used_to_convey_data_ = true; |
827 | 788 |
828 if (write_result.async) { | 789 if (write_result.async) { |
829 RunCallbackAsync(callback, write_result.result); | 790 RunCallbackAsync(callback, write_result.result); |
830 return net::ERR_IO_PENDING; | 791 return ERR_IO_PENDING; |
831 } | 792 } |
832 | 793 |
833 return write_result.result; | 794 return write_result.result; |
834 } | 795 } |
835 | 796 |
836 int MockTCPClientSocket::Connect(net::OldCompletionCallback* callback) { | 797 int MockTCPClientSocket::Connect(const CompletionCallback& callback) { |
837 if (connected_) | 798 if (connected_) |
838 return net::OK; | 799 return OK; |
839 connected_ = true; | 800 connected_ = true; |
840 peer_closed_connection_ = false; | 801 peer_closed_connection_ = false; |
841 if (data_->connect_data().async) { | 802 if (data_->connect_data().async) { |
842 RunCallbackAsync(callback, data_->connect_data().result); | 803 RunCallbackAsync(callback, data_->connect_data().result); |
843 return net::ERR_IO_PENDING; | 804 return ERR_IO_PENDING; |
844 } | 805 } |
845 return data_->connect_data().result; | 806 return data_->connect_data().result; |
846 } | 807 } |
847 int MockTCPClientSocket::Connect(const net::CompletionCallback& callback) { | |
848 if (connected_) | |
849 return net::OK; | |
850 | |
851 connected_ = true; | |
852 peer_closed_connection_ = false; | |
853 if (data_->connect_data().async) { | |
854 RunCallbackAsync(callback, data_->connect_data().result); | |
855 return net::ERR_IO_PENDING; | |
856 } | |
857 | |
858 return data_->connect_data().result; | |
859 } | |
860 | 808 |
861 void MockTCPClientSocket::Disconnect() { | 809 void MockTCPClientSocket::Disconnect() { |
862 MockClientSocket::Disconnect(); | 810 MockClientSocket::Disconnect(); |
863 old_pending_callback_ = NULL; | |
864 pending_callback_.Reset(); | 811 pending_callback_.Reset(); |
865 } | 812 } |
866 | 813 |
867 bool MockTCPClientSocket::IsConnected() const { | 814 bool MockTCPClientSocket::IsConnected() const { |
868 return connected_ && !peer_closed_connection_; | 815 return connected_ && !peer_closed_connection_; |
869 } | 816 } |
870 | 817 |
871 bool MockTCPClientSocket::IsConnectedAndIdle() const { | 818 bool MockTCPClientSocket::IsConnectedAndIdle() const { |
872 return IsConnected(); | 819 return IsConnected(); |
873 } | 820 } |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
905 // Since we've been waiting for data, need_read_data_ should be true. | 852 // Since we've been waiting for data, need_read_data_ should be true. |
906 DCHECK(need_read_data_); | 853 DCHECK(need_read_data_); |
907 | 854 |
908 read_data_ = data; | 855 read_data_ = data; |
909 need_read_data_ = false; | 856 need_read_data_ = false; |
910 | 857 |
911 // The caller is simulating that this IO completes right now. Don't | 858 // The caller is simulating that this IO completes right now. Don't |
912 // let CompleteRead() schedule a callback. | 859 // let CompleteRead() schedule a callback. |
913 read_data_.async = false; | 860 read_data_.async = false; |
914 | 861 |
915 if (old_pending_callback_) { | 862 CompletionCallback callback = pending_callback_; |
916 net::OldCompletionCallback* callback = old_pending_callback_; | 863 int rv = CompleteRead(); |
917 int rv = CompleteRead(); | 864 RunCallback(callback, rv); |
918 RunOldCallback(callback, rv); | |
919 } else { | |
920 net::CompletionCallback callback = pending_callback_; | |
921 int rv = CompleteRead(); | |
922 RunCallback(callback, rv); | |
923 } | |
924 } | 865 } |
925 | 866 |
926 int MockTCPClientSocket::CompleteRead() { | 867 int MockTCPClientSocket::CompleteRead() { |
927 DCHECK(pending_buf_); | 868 DCHECK(pending_buf_); |
928 DCHECK(pending_buf_len_ > 0); | 869 DCHECK(pending_buf_len_ > 0); |
929 | 870 |
930 was_used_to_convey_data_ = true; | 871 was_used_to_convey_data_ = true; |
931 | 872 |
932 // Save the pending async IO data and reset our |pending_| state. | 873 // Save the pending async IO data and reset our |pending_| state. |
933 net::IOBuffer* buf = pending_buf_; | 874 IOBuffer* buf = pending_buf_; |
934 int buf_len = pending_buf_len_; | 875 int buf_len = pending_buf_len_; |
935 net::OldCompletionCallback* old_callback = old_pending_callback_; | 876 CompletionCallback callback = pending_callback_; |
936 net::CompletionCallback callback = pending_callback_; | |
937 pending_buf_ = NULL; | 877 pending_buf_ = NULL; |
938 pending_buf_len_ = 0; | 878 pending_buf_len_ = 0; |
939 old_pending_callback_ = NULL; | |
940 pending_callback_.Reset(); | 879 pending_callback_.Reset(); |
941 | 880 |
942 int result = read_data_.result; | 881 int result = read_data_.result; |
943 DCHECK(result != ERR_IO_PENDING); | 882 DCHECK(result != ERR_IO_PENDING); |
944 | 883 |
945 if (read_data_.data) { | 884 if (read_data_.data) { |
946 if (read_data_.data_len - read_offset_ > 0) { | 885 if (read_data_.data_len - read_offset_ > 0) { |
947 result = std::min(buf_len, read_data_.data_len - read_offset_); | 886 result = std::min(buf_len, read_data_.data_len - read_offset_); |
948 memcpy(buf->data(), read_data_.data + read_offset_, result); | 887 memcpy(buf->data(), read_data_.data + read_offset_, result); |
949 read_offset_ += result; | 888 read_offset_ += result; |
950 num_bytes_read_ += result; | 889 num_bytes_read_ += result; |
951 if (read_offset_ == read_data_.data_len) { | 890 if (read_offset_ == read_data_.data_len) { |
952 need_read_data_ = true; | 891 need_read_data_ = true; |
953 read_offset_ = 0; | 892 read_offset_ = 0; |
954 } | 893 } |
955 } else { | 894 } else { |
956 result = 0; // EOF | 895 result = 0; // EOF |
957 } | 896 } |
958 } | 897 } |
959 | 898 |
960 if (read_data_.async) { | 899 if (read_data_.async) { |
961 DCHECK(old_callback || !callback.is_null()); | 900 DCHECK(!callback.is_null()); |
962 if (old_callback) | 901 RunCallbackAsync(callback, result); |
963 RunCallbackAsync(old_callback, result); | 902 return ERR_IO_PENDING; |
964 else | |
965 RunCallbackAsync(callback, result); | |
966 return net::ERR_IO_PENDING; | |
967 } | 903 } |
968 return result; | 904 return result; |
969 } | 905 } |
970 | 906 |
971 DeterministicMockTCPClientSocket::DeterministicMockTCPClientSocket( | 907 DeterministicMockTCPClientSocket::DeterministicMockTCPClientSocket( |
972 net::NetLog* net_log, net::DeterministicSocketData* data) | 908 net::NetLog* net_log, DeterministicSocketData* data) |
973 : MockClientSocket(net_log), | 909 : MockClientSocket(net_log), |
974 write_pending_(false), | 910 write_pending_(false), |
975 write_callback_(NULL), | |
976 write_result_(0), | 911 write_result_(0), |
977 read_data_(), | 912 read_data_(), |
978 read_buf_(NULL), | 913 read_buf_(NULL), |
979 read_buf_len_(0), | 914 read_buf_len_(0), |
980 read_pending_(false), | 915 read_pending_(false), |
981 old_read_callback_(NULL), | |
982 data_(data), | 916 data_(data), |
983 was_used_to_convey_data_(false) {} | 917 was_used_to_convey_data_(false) {} |
984 | 918 |
985 DeterministicMockTCPClientSocket::~DeterministicMockTCPClientSocket() {} | 919 DeterministicMockTCPClientSocket::~DeterministicMockTCPClientSocket() {} |
986 | 920 |
987 void DeterministicMockTCPClientSocket::CompleteWrite() { | 921 void DeterministicMockTCPClientSocket::CompleteWrite() { |
988 was_used_to_convey_data_ = true; | 922 was_used_to_convey_data_ = true; |
989 write_pending_ = false; | 923 write_pending_ = false; |
990 write_callback_->Run(write_result_); | 924 write_callback_.Run(write_result_); |
991 } | 925 } |
992 | 926 |
993 int DeterministicMockTCPClientSocket::CompleteRead() { | 927 int DeterministicMockTCPClientSocket::CompleteRead() { |
994 DCHECK_GT(read_buf_len_, 0); | 928 DCHECK_GT(read_buf_len_, 0); |
995 DCHECK_LE(read_data_.data_len, read_buf_len_); | 929 DCHECK_LE(read_data_.data_len, read_buf_len_); |
996 DCHECK(read_buf_); | 930 DCHECK(read_buf_); |
997 | 931 |
998 was_used_to_convey_data_ = true; | 932 was_used_to_convey_data_ = true; |
999 | 933 |
1000 if (read_data_.result == ERR_IO_PENDING) | 934 if (read_data_.result == ERR_IO_PENDING) |
1001 read_data_ = data_->GetNextRead(); | 935 read_data_ = data_->GetNextRead(); |
1002 DCHECK_NE(ERR_IO_PENDING, read_data_.result); | 936 DCHECK_NE(ERR_IO_PENDING, read_data_.result); |
1003 // If read_data_.async is true, we do not need to wait, since this is already | 937 // If read_data_.async is true, we do not need to wait, since this is already |
1004 // the callback. Therefore we don't even bother to check it. | 938 // the callback. Therefore we don't even bother to check it. |
1005 int result = read_data_.result; | 939 int result = read_data_.result; |
1006 | 940 |
1007 if (read_data_.data_len > 0) { | 941 if (read_data_.data_len > 0) { |
1008 DCHECK(read_data_.data); | 942 DCHECK(read_data_.data); |
1009 result = std::min(read_buf_len_, read_data_.data_len); | 943 result = std::min(read_buf_len_, read_data_.data_len); |
1010 memcpy(read_buf_->data(), read_data_.data, result); | 944 memcpy(read_buf_->data(), read_data_.data, result); |
1011 } | 945 } |
1012 | 946 |
1013 if (read_pending_) { | 947 if (read_pending_) { |
1014 read_pending_ = false; | 948 read_pending_ = false; |
1015 if (old_read_callback_) | 949 read_callback_.Run(result); |
1016 old_read_callback_->Run(result); | |
1017 else | |
1018 read_callback_.Run(result); | |
1019 } | 950 } |
1020 | 951 |
1021 return result; | 952 return result; |
1022 } | 953 } |
1023 | 954 |
1024 int DeterministicMockTCPClientSocket::Write( | 955 int DeterministicMockTCPClientSocket::Write( |
1025 net::IOBuffer* buf, int buf_len, net::OldCompletionCallback* callback) { | 956 IOBuffer* buf, int buf_len, const CompletionCallback& callback) { |
1026 DCHECK(buf); | 957 DCHECK(buf); |
1027 DCHECK_GT(buf_len, 0); | 958 DCHECK_GT(buf_len, 0); |
1028 | 959 |
1029 if (!connected_) | 960 if (!connected_) |
1030 return net::ERR_UNEXPECTED; | 961 return ERR_UNEXPECTED; |
1031 | 962 |
1032 std::string data(buf->data(), buf_len); | 963 std::string data(buf->data(), buf_len); |
1033 net::MockWriteResult write_result = data_->OnWrite(data); | 964 MockWriteResult write_result = data_->OnWrite(data); |
1034 | 965 |
1035 if (write_result.async) { | 966 if (write_result.async) { |
1036 write_callback_ = callback; | 967 write_callback_ = callback; |
1037 write_result_ = write_result.result; | 968 write_result_ = write_result.result; |
1038 DCHECK(write_callback_ != NULL); | 969 DCHECK(!write_callback_.is_null()); |
1039 write_pending_ = true; | 970 write_pending_ = true; |
1040 return net::ERR_IO_PENDING; | 971 return ERR_IO_PENDING; |
1041 } | 972 } |
1042 | 973 |
1043 was_used_to_convey_data_ = true; | 974 was_used_to_convey_data_ = true; |
1044 write_pending_ = false; | 975 write_pending_ = false; |
1045 return write_result.result; | 976 return write_result.result; |
1046 } | 977 } |
1047 | 978 |
1048 int DeterministicMockTCPClientSocket::Read( | 979 int DeterministicMockTCPClientSocket::Read( |
1049 net::IOBuffer* buf, int buf_len, net::OldCompletionCallback* callback) { | 980 IOBuffer* buf, int buf_len, const CompletionCallback& callback) { |
1050 if (!connected_) | 981 if (!connected_) |
1051 return net::ERR_UNEXPECTED; | 982 return ERR_UNEXPECTED; |
1052 | 983 |
1053 read_data_ = data_->GetNextRead(); | 984 read_data_ = data_->GetNextRead(); |
1054 // The buffer should always be big enough to contain all the MockRead data. To | 985 // The buffer should always be big enough to contain all the MockRead data. To |
1055 // use small buffers, split the data into multiple MockReads. | |
1056 DCHECK_LE(read_data_.data_len, buf_len); | |
1057 | |
1058 read_buf_ = buf; | |
1059 read_buf_len_ = buf_len; | |
1060 old_read_callback_ = callback; | |
1061 | |
1062 if (read_data_.async || (read_data_.result == ERR_IO_PENDING)) { | |
1063 read_pending_ = true; | |
1064 DCHECK(old_read_callback_); | |
1065 return ERR_IO_PENDING; | |
1066 } | |
1067 | |
1068 was_used_to_convey_data_ = true; | |
1069 return CompleteRead(); | |
1070 } | |
1071 int DeterministicMockTCPClientSocket::Read( | |
1072 net::IOBuffer* buf, int buf_len, const net::CompletionCallback& callback) { | |
1073 if (!connected_) | |
1074 return net::ERR_UNEXPECTED; | |
1075 | |
1076 read_data_ = data_->GetNextRead(); | |
1077 // The buffer should always be big enough to contain all the MockRead data. To | |
1078 // use small buffers, split the data into multiple MockReads. | 986 // use small buffers, split the data into multiple MockReads. |
1079 DCHECK_LE(read_data_.data_len, buf_len); | 987 DCHECK_LE(read_data_.data_len, buf_len); |
1080 | 988 |
1081 read_buf_ = buf; | 989 read_buf_ = buf; |
1082 read_buf_len_ = buf_len; | 990 read_buf_len_ = buf_len; |
1083 read_callback_ = callback; | 991 read_callback_ = callback; |
1084 | 992 |
1085 if (read_data_.async || (read_data_.result == ERR_IO_PENDING)) { | 993 if (read_data_.async || (read_data_.result == ERR_IO_PENDING)) { |
1086 read_pending_ = true; | 994 read_pending_ = true; |
1087 DCHECK(!read_callback_.is_null()); | 995 DCHECK(!read_callback_.is_null()); |
1088 return ERR_IO_PENDING; | 996 return ERR_IO_PENDING; |
1089 } | 997 } |
1090 | 998 |
1091 was_used_to_convey_data_ = true; | 999 was_used_to_convey_data_ = true; |
1092 return CompleteRead(); | 1000 return CompleteRead(); |
1093 } | 1001 } |
1094 | 1002 |
1095 // TODO(erikchen): Support connect sequencing. | 1003 // TODO(erikchen): Support connect sequencing. |
1096 int DeterministicMockTCPClientSocket::Connect( | 1004 int DeterministicMockTCPClientSocket::Connect( |
1097 net::OldCompletionCallback* callback) { | 1005 const CompletionCallback& callback) { |
1098 if (connected_) | 1006 if (connected_) |
1099 return net::OK; | 1007 return OK; |
1100 connected_ = true; | 1008 connected_ = true; |
1101 if (data_->connect_data().async) { | 1009 if (data_->connect_data().async) { |
1102 RunCallbackAsync(callback, data_->connect_data().result); | 1010 RunCallbackAsync(callback, data_->connect_data().result); |
1103 return net::ERR_IO_PENDING; | 1011 return ERR_IO_PENDING; |
1104 } | 1012 } |
1105 return data_->connect_data().result; | 1013 return data_->connect_data().result; |
1106 } | 1014 } |
1107 int DeterministicMockTCPClientSocket::Connect( | |
1108 const net::CompletionCallback& callback) { | |
1109 if (connected_) | |
1110 return net::OK; | |
1111 | 1015 |
1112 connected_ = true; | |
1113 if (data_->connect_data().async) { | |
1114 RunCallbackAsync(callback, data_->connect_data().result); | |
1115 return net::ERR_IO_PENDING; | |
1116 } | |
1117 | |
1118 return data_->connect_data().result; | |
1119 } | |
1120 | |
1121 void DeterministicMockTCPClientSocket::Disconnect() { | 1016 void DeterministicMockTCPClientSocket::Disconnect() { |
1122 MockClientSocket::Disconnect(); | 1017 MockClientSocket::Disconnect(); |
1123 } | 1018 } |
1124 | 1019 |
1125 bool DeterministicMockTCPClientSocket::IsConnected() const { | 1020 bool DeterministicMockTCPClientSocket::IsConnected() const { |
1126 return connected_; | 1021 return connected_; |
1127 } | 1022 } |
1128 | 1023 |
1129 bool DeterministicMockTCPClientSocket::IsConnectedAndIdle() const { | 1024 bool DeterministicMockTCPClientSocket::IsConnectedAndIdle() const { |
1130 return IsConnected(); | 1025 return IsConnected(); |
(...skipping 10 matching lines...) Expand all Loading... |
1141 int64 DeterministicMockTCPClientSocket::NumBytesRead() const { | 1036 int64 DeterministicMockTCPClientSocket::NumBytesRead() const { |
1142 return -1; | 1037 return -1; |
1143 } | 1038 } |
1144 | 1039 |
1145 base::TimeDelta DeterministicMockTCPClientSocket::GetConnectTimeMicros() const { | 1040 base::TimeDelta DeterministicMockTCPClientSocket::GetConnectTimeMicros() const { |
1146 return base::TimeDelta::FromMicroseconds(-1); | 1041 return base::TimeDelta::FromMicroseconds(-1); |
1147 } | 1042 } |
1148 | 1043 |
1149 void DeterministicMockTCPClientSocket::OnReadComplete(const MockRead& data) {} | 1044 void DeterministicMockTCPClientSocket::OnReadComplete(const MockRead& data) {} |
1150 | 1045 |
1151 class MockSSLClientSocket::OldConnectCallback | 1046 // static |
1152 : public net::OldCompletionCallbackImpl< | 1047 void MockSSLClientSocket::ConnectCallback( |
1153 MockSSLClientSocket::OldConnectCallback> { | 1048 MockSSLClientSocket *ssl_client_socket, |
1154 public: | 1049 const CompletionCallback& callback, |
1155 OldConnectCallback(MockSSLClientSocket *ssl_client_socket, | 1050 int rv) { |
1156 net::OldCompletionCallback* user_callback, | 1051 if (rv == OK) |
1157 int rv) | 1052 ssl_client_socket->connected_ = true; |
1158 : ALLOW_THIS_IN_INITIALIZER_LIST( | 1053 callback.Run(rv); |
1159 net::OldCompletionCallbackImpl< | 1054 } |
1160 MockSSLClientSocket::OldConnectCallback>( | |
1161 this, &OldConnectCallback::Wrapper)), | |
1162 ssl_client_socket_(ssl_client_socket), | |
1163 user_callback_(user_callback), | |
1164 rv_(rv) { | |
1165 } | |
1166 | |
1167 private: | |
1168 void Wrapper(int rv) { | |
1169 if (rv_ == net::OK) | |
1170 ssl_client_socket_->connected_ = true; | |
1171 user_callback_->Run(rv_); | |
1172 delete this; | |
1173 } | |
1174 | |
1175 MockSSLClientSocket* ssl_client_socket_; | |
1176 net::OldCompletionCallback* user_callback_; | |
1177 int rv_; | |
1178 }; | |
1179 class MockSSLClientSocket::ConnectCallback { | |
1180 public: | |
1181 ConnectCallback(MockSSLClientSocket *ssl_client_socket, | |
1182 const CompletionCallback& user_callback, | |
1183 int rv) | |
1184 : ALLOW_THIS_IN_INITIALIZER_LIST(callback_( | |
1185 base::Bind(&ConnectCallback::Wrapper, base::Unretained(this)))), | |
1186 ssl_client_socket_(ssl_client_socket), | |
1187 user_callback_(user_callback), | |
1188 rv_(rv) { | |
1189 } | |
1190 | |
1191 const CompletionCallback& callback() const { return callback_; } | |
1192 | |
1193 private: | |
1194 void Wrapper(int rv) { | |
1195 if (rv_ == net::OK) | |
1196 ssl_client_socket_->connected_ = true; | |
1197 user_callback_.Run(rv_); | |
1198 } | |
1199 | |
1200 CompletionCallback callback_; | |
1201 MockSSLClientSocket* ssl_client_socket_; | |
1202 CompletionCallback user_callback_; | |
1203 int rv_; | |
1204 }; | |
1205 | 1055 |
1206 MockSSLClientSocket::MockSSLClientSocket( | 1056 MockSSLClientSocket::MockSSLClientSocket( |
1207 net::ClientSocketHandle* transport_socket, | 1057 ClientSocketHandle* transport_socket, |
1208 const HostPortPair& host_port_pair, | 1058 const HostPortPair& host_port_pair, |
1209 const net::SSLConfig& ssl_config, | 1059 const SSLConfig& ssl_config, |
1210 SSLHostInfo* ssl_host_info, | 1060 SSLHostInfo* ssl_host_info, |
1211 net::SSLSocketDataProvider* data) | 1061 SSLSocketDataProvider* data) |
1212 : MockClientSocket(transport_socket->socket()->NetLog().net_log()), | 1062 : MockClientSocket(transport_socket->socket()->NetLog().net_log()), |
1213 transport_(transport_socket), | 1063 transport_(transport_socket), |
1214 data_(data), | 1064 data_(data), |
1215 is_npn_state_set_(false), | 1065 is_npn_state_set_(false), |
1216 new_npn_value_(false) { | 1066 new_npn_value_(false) { |
1217 DCHECK(data_); | 1067 DCHECK(data_); |
1218 delete ssl_host_info; // we take ownership but don't use it. | 1068 delete ssl_host_info; // we take ownership but don't use it. |
1219 } | 1069 } |
1220 | 1070 |
1221 MockSSLClientSocket::~MockSSLClientSocket() { | 1071 MockSSLClientSocket::~MockSSLClientSocket() { |
1222 Disconnect(); | 1072 Disconnect(); |
1223 } | 1073 } |
1224 | 1074 |
1225 int MockSSLClientSocket::Read(net::IOBuffer* buf, int buf_len, | 1075 int MockSSLClientSocket::Read(IOBuffer* buf, int buf_len, |
1226 net::OldCompletionCallback* callback) { | 1076 const CompletionCallback& callback) { |
1227 return transport_->socket()->Read(buf, buf_len, callback); | |
1228 } | |
1229 int MockSSLClientSocket::Read(net::IOBuffer* buf, int buf_len, | |
1230 const net::CompletionCallback& callback) { | |
1231 return transport_->socket()->Read(buf, buf_len, callback); | 1077 return transport_->socket()->Read(buf, buf_len, callback); |
1232 } | 1078 } |
1233 | 1079 |
1234 int MockSSLClientSocket::Write(net::IOBuffer* buf, int buf_len, | 1080 int MockSSLClientSocket::Write(IOBuffer* buf, int buf_len, |
1235 net::OldCompletionCallback* callback) { | 1081 const CompletionCallback& callback) { |
1236 return transport_->socket()->Write(buf, buf_len, callback); | 1082 return transport_->socket()->Write(buf, buf_len, callback); |
1237 } | 1083 } |
1238 | 1084 |
1239 int MockSSLClientSocket::Connect(net::OldCompletionCallback* callback) { | 1085 int MockSSLClientSocket::Connect(const CompletionCallback& callback) { |
1240 OldConnectCallback* connect_callback = new OldConnectCallback( | 1086 int rv = transport_->socket()->Connect( |
1241 this, callback, data_->connect.result); | 1087 base::Bind(&ConnectCallback, base::Unretained(this), callback)); |
1242 int rv = transport_->socket()->Connect(connect_callback); | 1088 if (rv == OK) { |
1243 if (rv == net::OK) { | 1089 if (data_->connect.result == OK) |
1244 delete connect_callback; | |
1245 if (data_->connect.result == net::OK) | |
1246 connected_ = true; | 1090 connected_ = true; |
1247 if (data_->connect.async) { | 1091 if (data_->connect.async) { |
1248 RunCallbackAsync(callback, data_->connect.result); | 1092 RunCallbackAsync(callback, data_->connect.result); |
1249 return net::ERR_IO_PENDING; | 1093 return ERR_IO_PENDING; |
1250 } | 1094 } |
1251 return data_->connect.result; | 1095 return data_->connect.result; |
1252 } | 1096 } |
1253 return rv; | |
1254 } | |
1255 int MockSSLClientSocket::Connect(const net::CompletionCallback& callback) { | |
1256 ConnectCallback connect_callback(this, callback, data_->connect.result); | |
1257 int rv = transport_->socket()->Connect(connect_callback.callback()); | |
1258 if (rv == net::OK) { | |
1259 if (data_->connect.result == net::OK) | |
1260 connected_ = true; | |
1261 if (data_->connect.async) { | |
1262 RunCallbackAsync(callback, data_->connect.result); | |
1263 return net::ERR_IO_PENDING; | |
1264 } | |
1265 return data_->connect.result; | |
1266 } | |
1267 return rv; | 1097 return rv; |
1268 } | 1098 } |
1269 | 1099 |
1270 void MockSSLClientSocket::Disconnect() { | 1100 void MockSSLClientSocket::Disconnect() { |
1271 MockClientSocket::Disconnect(); | 1101 MockClientSocket::Disconnect(); |
1272 if (transport_->socket() != NULL) | 1102 if (transport_->socket() != NULL) |
1273 transport_->socket()->Disconnect(); | 1103 transport_->socket()->Disconnect(); |
1274 } | 1104 } |
1275 | 1105 |
1276 bool MockSSLClientSocket::IsConnected() const { | 1106 bool MockSSLClientSocket::IsConnected() const { |
1277 return transport_->socket()->IsConnected(); | 1107 return transport_->socket()->IsConnected(); |
1278 } | 1108 } |
1279 | 1109 |
1280 bool MockSSLClientSocket::WasEverUsed() const { | 1110 bool MockSSLClientSocket::WasEverUsed() const { |
1281 return transport_->socket()->WasEverUsed(); | 1111 return transport_->socket()->WasEverUsed(); |
1282 } | 1112 } |
1283 | 1113 |
1284 bool MockSSLClientSocket::UsingTCPFastOpen() const { | 1114 bool MockSSLClientSocket::UsingTCPFastOpen() const { |
1285 return transport_->socket()->UsingTCPFastOpen(); | 1115 return transport_->socket()->UsingTCPFastOpen(); |
1286 } | 1116 } |
1287 | 1117 |
1288 int64 MockSSLClientSocket::NumBytesRead() const { | 1118 int64 MockSSLClientSocket::NumBytesRead() const { |
1289 return -1; | 1119 return -1; |
1290 } | 1120 } |
1291 | 1121 |
1292 base::TimeDelta MockSSLClientSocket::GetConnectTimeMicros() const { | 1122 base::TimeDelta MockSSLClientSocket::GetConnectTimeMicros() const { |
1293 return base::TimeDelta::FromMicroseconds(-1); | 1123 return base::TimeDelta::FromMicroseconds(-1); |
1294 } | 1124 } |
1295 | 1125 |
1296 void MockSSLClientSocket::GetSSLInfo(net::SSLInfo* ssl_info) { | 1126 void MockSSLClientSocket::GetSSLInfo(SSLInfo* ssl_info) { |
1297 ssl_info->Reset(); | 1127 ssl_info->Reset(); |
1298 ssl_info->cert = data_->cert; | 1128 ssl_info->cert = data_->cert; |
1299 ssl_info->client_cert_sent = data_->client_cert_sent; | 1129 ssl_info->client_cert_sent = data_->client_cert_sent; |
1300 } | 1130 } |
1301 | 1131 |
1302 void MockSSLClientSocket::GetSSLCertRequestInfo( | 1132 void MockSSLClientSocket::GetSSLCertRequestInfo( |
1303 net::SSLCertRequestInfo* cert_request_info) { | 1133 SSLCertRequestInfo* cert_request_info) { |
1304 DCHECK(cert_request_info); | 1134 DCHECK(cert_request_info); |
1305 if (data_->cert_request_info) { | 1135 if (data_->cert_request_info) { |
1306 cert_request_info->host_and_port = | 1136 cert_request_info->host_and_port = |
1307 data_->cert_request_info->host_and_port; | 1137 data_->cert_request_info->host_and_port; |
1308 cert_request_info->client_certs = data_->cert_request_info->client_certs; | 1138 cert_request_info->client_certs = data_->cert_request_info->client_certs; |
1309 } else { | 1139 } else { |
1310 cert_request_info->Reset(); | 1140 cert_request_info->Reset(); |
1311 } | 1141 } |
1312 } | 1142 } |
1313 | 1143 |
(...skipping 17 matching lines...) Expand all Loading... |
1331 | 1161 |
1332 void MockSSLClientSocket::OnReadComplete(const MockRead& data) { | 1162 void MockSSLClientSocket::OnReadComplete(const MockRead& data) { |
1333 NOTIMPLEMENTED(); | 1163 NOTIMPLEMENTED(); |
1334 } | 1164 } |
1335 | 1165 |
1336 MockUDPClientSocket::MockUDPClientSocket(SocketDataProvider* data, | 1166 MockUDPClientSocket::MockUDPClientSocket(SocketDataProvider* data, |
1337 net::NetLog* net_log) | 1167 net::NetLog* net_log) |
1338 : connected_(false), | 1168 : connected_(false), |
1339 data_(data), | 1169 data_(data), |
1340 read_offset_(0), | 1170 read_offset_(0), |
1341 read_data_(false, net::ERR_UNEXPECTED), | 1171 read_data_(false, ERR_UNEXPECTED), |
1342 need_read_data_(true), | 1172 need_read_data_(true), |
1343 pending_buf_(NULL), | 1173 pending_buf_(NULL), |
1344 pending_buf_len_(0), | 1174 pending_buf_len_(0), |
1345 old_pending_callback_(NULL), | 1175 net_log_(net::NetLog::Source(), net_log), |
1346 net_log_(NetLog::Source(), net_log), | |
1347 ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)) { | 1176 ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)) { |
1348 DCHECK(data_); | 1177 DCHECK(data_); |
1349 data_->Reset(); | 1178 data_->Reset(); |
1350 } | 1179 } |
1351 | 1180 |
1352 MockUDPClientSocket::~MockUDPClientSocket() {} | 1181 MockUDPClientSocket::~MockUDPClientSocket() {} |
1353 | 1182 |
1354 int MockUDPClientSocket::Read(net::IOBuffer* buf, int buf_len, | 1183 int MockUDPClientSocket::Read(IOBuffer* buf, int buf_len, |
1355 net::OldCompletionCallback* callback) { | 1184 const CompletionCallback& callback) { |
1356 if (!connected_) | 1185 if (!connected_) |
1357 return net::ERR_UNEXPECTED; | 1186 return ERR_UNEXPECTED; |
1358 | 1187 |
1359 // If the buffer is already in use, a read is already in progress! | 1188 // If the buffer is already in use, a read is already in progress! |
1360 DCHECK(pending_buf_ == NULL); | 1189 DCHECK(pending_buf_ == NULL); |
1361 | |
1362 // Store our async IO data. | |
1363 pending_buf_ = buf; | |
1364 pending_buf_len_ = buf_len; | |
1365 old_pending_callback_ = callback; | |
1366 | |
1367 if (need_read_data_) { | |
1368 read_data_ = data_->GetNextRead(); | |
1369 // ERR_IO_PENDING means that the SocketDataProvider is taking responsibility | |
1370 // to complete the async IO manually later (via OnReadComplete). | |
1371 if (read_data_.result == ERR_IO_PENDING) { | |
1372 DCHECK(callback); // We need to be using async IO in this case. | |
1373 return ERR_IO_PENDING; | |
1374 } | |
1375 need_read_data_ = false; | |
1376 } | |
1377 | |
1378 return CompleteRead(); | |
1379 } | |
1380 int MockUDPClientSocket::Read(net::IOBuffer* buf, int buf_len, | |
1381 const net::CompletionCallback& callback) { | |
1382 if (!connected_) | |
1383 return net::ERR_UNEXPECTED; | |
1384 | |
1385 // If the buffer is already in use, a read is already in progress! | |
1386 DCHECK(pending_buf_ == NULL); | |
1387 | 1190 |
1388 // Store our async IO data. | 1191 // Store our async IO data. |
1389 pending_buf_ = buf; | 1192 pending_buf_ = buf; |
1390 pending_buf_len_ = buf_len; | 1193 pending_buf_len_ = buf_len; |
1391 pending_callback_ = callback; | 1194 pending_callback_ = callback; |
1392 | 1195 |
1393 if (need_read_data_) { | 1196 if (need_read_data_) { |
1394 read_data_ = data_->GetNextRead(); | 1197 read_data_ = data_->GetNextRead(); |
1395 // ERR_IO_PENDING means that the SocketDataProvider is taking responsibility | 1198 // ERR_IO_PENDING means that the SocketDataProvider is taking responsibility |
1396 // to complete the async IO manually later (via OnReadComplete). | 1199 // to complete the async IO manually later (via OnReadComplete). |
1397 if (read_data_.result == ERR_IO_PENDING) { | 1200 if (read_data_.result == ERR_IO_PENDING) { |
1398 // We need to be using async IO in this case. | 1201 // We need to be using async IO in this case. |
1399 DCHECK(!callback.is_null()); | 1202 DCHECK(!callback.is_null()); |
1400 return ERR_IO_PENDING; | 1203 return ERR_IO_PENDING; |
1401 } | 1204 } |
1402 need_read_data_ = false; | 1205 need_read_data_ = false; |
1403 } | 1206 } |
1404 | 1207 |
1405 return CompleteRead(); | 1208 return CompleteRead(); |
1406 } | 1209 } |
1407 | 1210 |
1408 int MockUDPClientSocket::Write(net::IOBuffer* buf, int buf_len, | 1211 int MockUDPClientSocket::Write(IOBuffer* buf, int buf_len, |
1409 net::OldCompletionCallback* callback) { | 1212 const CompletionCallback& callback) { |
1410 DCHECK(buf); | 1213 DCHECK(buf); |
1411 DCHECK_GT(buf_len, 0); | 1214 DCHECK_GT(buf_len, 0); |
1412 | 1215 |
1413 if (!connected_) | 1216 if (!connected_) |
1414 return ERR_UNEXPECTED; | 1217 return ERR_UNEXPECTED; |
1415 | 1218 |
1416 std::string data(buf->data(), buf_len); | 1219 std::string data(buf->data(), buf_len); |
1417 MockWriteResult write_result = data_->OnWrite(data); | 1220 MockWriteResult write_result = data_->OnWrite(data); |
1418 | 1221 |
1419 if (write_result.async) { | 1222 if (write_result.async) { |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1462 // Since we've been waiting for data, need_read_data_ should be true. | 1265 // Since we've been waiting for data, need_read_data_ should be true. |
1463 DCHECK(need_read_data_); | 1266 DCHECK(need_read_data_); |
1464 | 1267 |
1465 read_data_ = data; | 1268 read_data_ = data; |
1466 need_read_data_ = false; | 1269 need_read_data_ = false; |
1467 | 1270 |
1468 // The caller is simulating that this IO completes right now. Don't | 1271 // The caller is simulating that this IO completes right now. Don't |
1469 // let CompleteRead() schedule a callback. | 1272 // let CompleteRead() schedule a callback. |
1470 read_data_.async = false; | 1273 read_data_.async = false; |
1471 | 1274 |
1472 if (old_pending_callback_) { | 1275 net::CompletionCallback callback = pending_callback_; |
1473 net::OldCompletionCallback* callback = old_pending_callback_; | 1276 int rv = CompleteRead(); |
1474 int rv = CompleteRead(); | 1277 RunCallback(callback, rv); |
1475 RunOldCallback(callback, rv); | |
1476 } else { | |
1477 net::CompletionCallback callback = pending_callback_; | |
1478 int rv = CompleteRead(); | |
1479 RunCallback(callback, rv); | |
1480 } | |
1481 } | 1278 } |
1482 | 1279 |
1483 int MockUDPClientSocket::CompleteRead() { | 1280 int MockUDPClientSocket::CompleteRead() { |
1484 DCHECK(pending_buf_); | 1281 DCHECK(pending_buf_); |
1485 DCHECK(pending_buf_len_ > 0); | 1282 DCHECK(pending_buf_len_ > 0); |
1486 | 1283 |
1487 // Save the pending async IO data and reset our |pending_| state. | 1284 // Save the pending async IO data and reset our |pending_| state. |
1488 net::IOBuffer* buf = pending_buf_; | 1285 IOBuffer* buf = pending_buf_; |
1489 int buf_len = pending_buf_len_; | 1286 int buf_len = pending_buf_len_; |
1490 net::OldCompletionCallback* old_callback = old_pending_callback_; | 1287 CompletionCallback callback = pending_callback_; |
1491 net::CompletionCallback callback = pending_callback_; | |
1492 pending_buf_ = NULL; | 1288 pending_buf_ = NULL; |
1493 pending_buf_len_ = 0; | 1289 pending_buf_len_ = 0; |
1494 old_pending_callback_ = NULL; | |
1495 pending_callback_.Reset(); | |
1496 pending_callback_.Reset(); | 1290 pending_callback_.Reset(); |
1497 | 1291 |
1498 int result = read_data_.result; | 1292 int result = read_data_.result; |
1499 DCHECK(result != ERR_IO_PENDING); | 1293 DCHECK(result != ERR_IO_PENDING); |
1500 | 1294 |
1501 if (read_data_.data) { | 1295 if (read_data_.data) { |
1502 if (read_data_.data_len - read_offset_ > 0) { | 1296 if (read_data_.data_len - read_offset_ > 0) { |
1503 result = std::min(buf_len, read_data_.data_len - read_offset_); | 1297 result = std::min(buf_len, read_data_.data_len - read_offset_); |
1504 memcpy(buf->data(), read_data_.data + read_offset_, result); | 1298 memcpy(buf->data(), read_data_.data + read_offset_, result); |
1505 read_offset_ += result; | 1299 read_offset_ += result; |
1506 if (read_offset_ == read_data_.data_len) { | 1300 if (read_offset_ == read_data_.data_len) { |
1507 need_read_data_ = true; | 1301 need_read_data_ = true; |
1508 read_offset_ = 0; | 1302 read_offset_ = 0; |
1509 } | 1303 } |
1510 } else { | 1304 } else { |
1511 result = 0; // EOF | 1305 result = 0; // EOF |
1512 } | 1306 } |
1513 } | 1307 } |
1514 | 1308 |
1515 if (read_data_.async) { | 1309 if (read_data_.async) { |
1516 DCHECK(old_callback || !callback.is_null()); | 1310 DCHECK(!callback.is_null()); |
1517 if (old_callback) | 1311 RunCallbackAsync(callback, result); |
1518 RunCallbackAsync(old_callback, result); | 1312 return ERR_IO_PENDING; |
1519 else | |
1520 RunCallbackAsync(callback, result); | |
1521 return net::ERR_IO_PENDING; | |
1522 } | 1313 } |
1523 return result; | 1314 return result; |
1524 } | 1315 } |
1525 | 1316 |
1526 void MockUDPClientSocket::RunCallbackAsync(net::OldCompletionCallback* callback, | 1317 void MockUDPClientSocket::RunCallbackAsync(const CompletionCallback& callback, |
1527 int result) { | 1318 int result) { |
1528 MessageLoop::current()->PostTask(FROM_HERE, | 1319 MessageLoop::current()->PostTask( |
1529 base::Bind(&MockUDPClientSocket::RunOldCallback, | 1320 FROM_HERE, |
1530 weak_factory_.GetWeakPtr(), callback, result)); | |
1531 } | |
1532 void MockUDPClientSocket::RunCallbackAsync( | |
1533 const net::CompletionCallback& callback, int result) { | |
1534 MessageLoop::current()->PostTask(FROM_HERE, | |
1535 base::Bind(&MockUDPClientSocket::RunCallback, weak_factory_.GetWeakPtr(), | 1321 base::Bind(&MockUDPClientSocket::RunCallback, weak_factory_.GetWeakPtr(), |
1536 callback, result)); | 1322 callback, result)); |
1537 } | 1323 } |
1538 | 1324 |
1539 void MockUDPClientSocket::RunOldCallback(net::OldCompletionCallback* callback, | 1325 void MockUDPClientSocket::RunCallback(const CompletionCallback& callback, |
1540 int result) { | |
1541 if (callback) | |
1542 callback->Run(result); | |
1543 } | |
1544 void MockUDPClientSocket::RunCallback(const net::CompletionCallback& callback, | |
1545 int result) { | 1326 int result) { |
1546 if (!callback.is_null()) | 1327 if (!callback.is_null()) |
1547 callback.Run(result); | 1328 callback.Run(result); |
1548 } | 1329 } |
1549 | 1330 |
1550 TestSocketRequest::TestSocketRequest( | 1331 TestSocketRequest::TestSocketRequest( |
1551 std::vector<TestSocketRequest*>* request_order, | 1332 std::vector<TestSocketRequest*>* request_order, |
1552 size_t* completion_count) | 1333 size_t* completion_count) |
1553 : request_order_(request_order), | 1334 : request_order_(request_order), |
1554 completion_count_(completion_count) { | 1335 completion_count_(completion_count) { |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1607 void ClientSocketPoolTest::ReleaseAllConnections(KeepAlive keep_alive) { | 1388 void ClientSocketPoolTest::ReleaseAllConnections(KeepAlive keep_alive) { |
1608 bool released_one; | 1389 bool released_one; |
1609 do { | 1390 do { |
1610 released_one = ReleaseOneConnection(keep_alive); | 1391 released_one = ReleaseOneConnection(keep_alive); |
1611 } while (released_one); | 1392 } while (released_one); |
1612 } | 1393 } |
1613 | 1394 |
1614 MockTransportClientSocketPool::MockConnectJob::MockConnectJob( | 1395 MockTransportClientSocketPool::MockConnectJob::MockConnectJob( |
1615 StreamSocket* socket, | 1396 StreamSocket* socket, |
1616 ClientSocketHandle* handle, | 1397 ClientSocketHandle* handle, |
1617 OldCompletionCallback* callback) | 1398 const CompletionCallback& callback) |
1618 : socket_(socket), | 1399 : socket_(socket), |
1619 handle_(handle), | 1400 handle_(handle), |
1620 user_callback_(callback), | 1401 user_callback_(callback) { |
1621 ALLOW_THIS_IN_INITIALIZER_LIST( | |
1622 connect_callback_(this, &MockConnectJob::OnConnect)) { | |
1623 } | 1402 } |
1624 | 1403 |
1625 MockTransportClientSocketPool::MockConnectJob::~MockConnectJob() {} | 1404 MockTransportClientSocketPool::MockConnectJob::~MockConnectJob() {} |
1626 | 1405 |
1627 int MockTransportClientSocketPool::MockConnectJob::Connect() { | 1406 int MockTransportClientSocketPool::MockConnectJob::Connect() { |
1628 int rv = socket_->Connect(&connect_callback_); | 1407 int rv = socket_->Connect(base::Bind(&MockConnectJob::OnConnect, |
| 1408 base::Unretained(this))); |
1629 if (rv == OK) { | 1409 if (rv == OK) { |
1630 user_callback_ = NULL; | 1410 user_callback_.Reset(); |
1631 OnConnect(OK); | 1411 OnConnect(OK); |
1632 } | 1412 } |
1633 return rv; | 1413 return rv; |
1634 } | 1414 } |
1635 | 1415 |
1636 bool MockTransportClientSocketPool::MockConnectJob::CancelHandle( | 1416 bool MockTransportClientSocketPool::MockConnectJob::CancelHandle( |
1637 const ClientSocketHandle* handle) { | 1417 const ClientSocketHandle* handle) { |
1638 if (handle != handle_) | 1418 if (handle != handle_) |
1639 return false; | 1419 return false; |
1640 socket_.reset(); | 1420 socket_.reset(); |
1641 handle_ = NULL; | 1421 handle_ = NULL; |
1642 user_callback_ = NULL; | 1422 user_callback_.Reset(); |
1643 return true; | 1423 return true; |
1644 } | 1424 } |
1645 | 1425 |
1646 void MockTransportClientSocketPool::MockConnectJob::OnConnect(int rv) { | 1426 void MockTransportClientSocketPool::MockConnectJob::OnConnect(int rv) { |
1647 if (!socket_.get()) | 1427 if (!socket_.get()) |
1648 return; | 1428 return; |
1649 if (rv == OK) { | 1429 if (rv == OK) { |
1650 handle_->set_socket(socket_.release()); | 1430 handle_->set_socket(socket_.release()); |
1651 } else { | 1431 } else { |
1652 socket_.reset(); | 1432 socket_.reset(); |
1653 } | 1433 } |
1654 | 1434 |
1655 handle_ = NULL; | 1435 handle_ = NULL; |
1656 | 1436 |
1657 if (user_callback_) { | 1437 if (!user_callback_.is_null()) { |
1658 OldCompletionCallback* callback = user_callback_; | 1438 CompletionCallback callback = user_callback_; |
1659 user_callback_ = NULL; | 1439 user_callback_.Reset(); |
1660 callback->Run(rv); | 1440 callback.Run(rv); |
1661 } | 1441 } |
1662 } | 1442 } |
1663 | 1443 |
1664 MockTransportClientSocketPool::MockTransportClientSocketPool( | 1444 MockTransportClientSocketPool::MockTransportClientSocketPool( |
1665 int max_sockets, | 1445 int max_sockets, |
1666 int max_sockets_per_group, | 1446 int max_sockets_per_group, |
1667 ClientSocketPoolHistograms* histograms, | 1447 ClientSocketPoolHistograms* histograms, |
1668 ClientSocketFactory* socket_factory) | 1448 ClientSocketFactory* socket_factory) |
1669 : TransportClientSocketPool(max_sockets, max_sockets_per_group, histograms, | 1449 : TransportClientSocketPool(max_sockets, max_sockets_per_group, histograms, |
1670 NULL, NULL, NULL), | 1450 NULL, NULL, NULL), |
1671 client_socket_factory_(socket_factory), | 1451 client_socket_factory_(socket_factory), |
1672 release_count_(0), | 1452 release_count_(0), |
1673 cancel_count_(0) { | 1453 cancel_count_(0) { |
1674 } | 1454 } |
1675 | 1455 |
1676 MockTransportClientSocketPool::~MockTransportClientSocketPool() {} | 1456 MockTransportClientSocketPool::~MockTransportClientSocketPool() {} |
1677 | 1457 |
1678 int MockTransportClientSocketPool::RequestSocket(const std::string& group_name, | 1458 int MockTransportClientSocketPool::RequestSocket( |
1679 const void* socket_params, | 1459 const std::string& group_name, |
1680 RequestPriority priority, | 1460 const void* socket_params, |
1681 ClientSocketHandle* handle, | 1461 RequestPriority priority, |
1682 OldCompletionCallback* callback, | 1462 ClientSocketHandle* handle, |
1683 const BoundNetLog& net_log) { | 1463 OldCompletionCallback* callback, |
| 1464 const BoundNetLog& net_log) { |
1684 StreamSocket* socket = client_socket_factory_->CreateTransportClientSocket( | 1465 StreamSocket* socket = client_socket_factory_->CreateTransportClientSocket( |
1685 AddressList(), net_log.net_log(), net::NetLog::Source()); | 1466 AddressList(), net_log.net_log(), net::NetLog::Source()); |
1686 MockConnectJob* job = new MockConnectJob(socket, handle, callback); | 1467 CompletionCallback cb; |
| 1468 if (callback) { |
| 1469 cb = base::Bind(&OldCompletionCallback::Run<int>, |
| 1470 base::Unretained(callback)); |
| 1471 } |
| 1472 MockConnectJob* job = new MockConnectJob(socket, handle, cb); |
1687 job_list_.push_back(job); | 1473 job_list_.push_back(job); |
1688 handle->set_pool_id(1); | 1474 handle->set_pool_id(1); |
1689 return job->Connect(); | 1475 return job->Connect(); |
1690 } | 1476 } |
1691 | 1477 |
1692 void MockTransportClientSocketPool::CancelRequest(const std::string& group_name, | 1478 void MockTransportClientSocketPool::CancelRequest(const std::string& group_name, |
1693 ClientSocketHandle* handle) { | 1479 ClientSocketHandle* handle) { |
1694 std::vector<MockConnectJob*>::iterator i; | 1480 std::vector<MockConnectJob*>::iterator i; |
1695 for (i = job_list_.begin(); i != job_list_.end(); ++i) { | 1481 for (i = job_list_.begin(); i != job_list_.end(); ++i) { |
1696 if ((*i)->CancelHandle(handle)) { | 1482 if ((*i)->CancelHandle(handle)) { |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1729 MockSSLClientSocket* DeterministicMockClientSocketFactory:: | 1515 MockSSLClientSocket* DeterministicMockClientSocketFactory:: |
1730 GetMockSSLClientSocket(size_t index) const { | 1516 GetMockSSLClientSocket(size_t index) const { |
1731 DCHECK_LT(index, ssl_client_sockets_.size()); | 1517 DCHECK_LT(index, ssl_client_sockets_.size()); |
1732 return ssl_client_sockets_[index]; | 1518 return ssl_client_sockets_[index]; |
1733 } | 1519 } |
1734 | 1520 |
1735 DatagramClientSocket* | 1521 DatagramClientSocket* |
1736 DeterministicMockClientSocketFactory::CreateDatagramClientSocket( | 1522 DeterministicMockClientSocketFactory::CreateDatagramClientSocket( |
1737 DatagramSocket::BindType bind_type, | 1523 DatagramSocket::BindType bind_type, |
1738 const RandIntCallback& rand_int_cb, | 1524 const RandIntCallback& rand_int_cb, |
1739 NetLog* net_log, | 1525 net::NetLog* net_log, |
1740 const NetLog::Source& source) { | 1526 const NetLog::Source& source) { |
1741 NOTREACHED(); | 1527 NOTREACHED(); |
1742 return NULL; | 1528 return NULL; |
1743 } | 1529 } |
1744 | 1530 |
1745 StreamSocket* DeterministicMockClientSocketFactory::CreateTransportClientSocket( | 1531 StreamSocket* DeterministicMockClientSocketFactory::CreateTransportClientSocket( |
1746 const AddressList& addresses, | 1532 const AddressList& addresses, |
1747 net::NetLog* net_log, | 1533 net::NetLog* net_log, |
1748 const net::NetLog::Source& source) { | 1534 const net::NetLog::Source& source) { |
1749 DeterministicSocketData* data_provider = mock_data().GetNext(); | 1535 DeterministicSocketData* data_provider = mock_data().GetNext(); |
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1815 | 1601 |
1816 const char kSOCKS5OkRequest[] = | 1602 const char kSOCKS5OkRequest[] = |
1817 { 0x05, 0x01, 0x00, 0x03, 0x04, 'h', 'o', 's', 't', 0x00, 0x50 }; | 1603 { 0x05, 0x01, 0x00, 0x03, 0x04, 'h', 'o', 's', 't', 0x00, 0x50 }; |
1818 const int kSOCKS5OkRequestLength = arraysize(kSOCKS5OkRequest); | 1604 const int kSOCKS5OkRequestLength = arraysize(kSOCKS5OkRequest); |
1819 | 1605 |
1820 const char kSOCKS5OkResponse[] = | 1606 const char kSOCKS5OkResponse[] = |
1821 { 0x05, 0x00, 0x00, 0x01, 127, 0, 0, 1, 0x00, 0x50 }; | 1607 { 0x05, 0x00, 0x00, 0x01, 127, 0, 0, 1, 0x00, 0x50 }; |
1822 const int kSOCKS5OkResponseLength = arraysize(kSOCKS5OkResponse); | 1608 const int kSOCKS5OkResponseLength = arraysize(kSOCKS5OkResponse); |
1823 | 1609 |
1824 } // namespace net | 1610 } // namespace net |
OLD | NEW |