OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
100 << Asciify(data[i + 0]) | 100 << Asciify(data[i + 0]) |
101 << " '"; | 101 << " '"; |
102 break; | 102 break; |
103 } | 103 } |
104 } | 104 } |
105 } | 105 } |
106 | 106 |
107 void DumpMockRead(const MockRead& r) { | 107 void DumpMockRead(const MockRead& r) { |
108 if (logging::LOG_INFO < logging::GetMinLogLevel()) | 108 if (logging::LOG_INFO < logging::GetMinLogLevel()) |
109 return; | 109 return; |
110 DVLOG(1) << "Async: " << r.async | 110 DVLOG(1) << "Async: " << (r.mode == ASYNC) |
111 << "\nResult: " << r.result; | 111 << "\nResult: " << r.result; |
112 DumpData(r.data, r.data_len); | 112 DumpData(r.data, r.data_len); |
113 const char* stop = (r.sequence_number & MockRead::STOPLOOP) ? " (STOP)" : ""; | 113 const char* stop = (r.sequence_number & MockRead::STOPLOOP) ? " (STOP)" : ""; |
114 DVLOG(1) << "Stage: " << (r.sequence_number & ~MockRead::STOPLOOP) << stop | 114 DVLOG(1) << "Stage: " << (r.sequence_number & ~MockRead::STOPLOOP) << stop |
115 << "\nTime: " << r.time_stamp.ToInternalValue(); | 115 << "\nTime: " << r.time_stamp.ToInternalValue(); |
116 } | 116 } |
117 | 117 |
118 } // namespace | 118 } // namespace |
119 | 119 |
120 StaticSocketDataProvider::StaticSocketDataProvider() | 120 StaticSocketDataProvider::StaticSocketDataProvider() |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
162 | 162 |
163 MockRead StaticSocketDataProvider::GetNextRead() { | 163 MockRead StaticSocketDataProvider::GetNextRead() { |
164 DCHECK(!at_read_eof()); | 164 DCHECK(!at_read_eof()); |
165 reads_[read_index_].time_stamp = base::Time::Now(); | 165 reads_[read_index_].time_stamp = base::Time::Now(); |
166 return reads_[read_index_++]; | 166 return reads_[read_index_++]; |
167 } | 167 } |
168 | 168 |
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(SYNCHRONOUS, data.length()); |
173 } | 173 } |
174 | 174 LOG(INFO) << "about to write: " << data; |
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 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, ERR_UNEXPECTED); | 194 return MockWriteResult(SYNCHRONOUS, ERR_UNEXPECTED); |
195 if (result == 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->mode, 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 |
206 DynamicSocketDataProvider::DynamicSocketDataProvider() | 206 DynamicSocketDataProvider::DynamicSocketDataProvider() |
207 : short_read_limit_(0), | 207 : short_read_limit_(0), |
208 allow_unconsumed_reads_(false) { | 208 allow_unconsumed_reads_(false) { |
209 } | 209 } |
210 | 210 |
211 DynamicSocketDataProvider::~DynamicSocketDataProvider() {} | 211 DynamicSocketDataProvider::~DynamicSocketDataProvider() {} |
212 | 212 |
213 MockRead DynamicSocketDataProvider::GetNextRead() { | 213 MockRead DynamicSocketDataProvider::GetNextRead() { |
214 if (reads_.empty()) | 214 if (reads_.empty()) |
215 return MockRead(false, ERR_UNEXPECTED); | 215 return MockRead(SYNCHRONOUS, ERR_UNEXPECTED); |
216 MockRead result = reads_.front(); | 216 MockRead result = reads_.front(); |
217 if (short_read_limit_ == 0 || result.data_len <= short_read_limit_) { | 217 if (short_read_limit_ == 0 || result.data_len <= short_read_limit_) { |
218 reads_.pop_front(); | 218 reads_.pop_front(); |
219 } else { | 219 } else { |
220 result.data_len = short_read_limit_; | 220 result.data_len = short_read_limit_; |
221 reads_.front().data += result.data_len; | 221 reads_.front().data += result.data_len; |
222 reads_.front().data_len -= result.data_len; | 222 reads_.front().data_len -= result.data_len; |
223 } | 223 } |
224 return result; | 224 return result; |
225 } | 225 } |
226 | 226 |
227 void DynamicSocketDataProvider::Reset() { | 227 void DynamicSocketDataProvider::Reset() { |
228 reads_.clear(); | 228 reads_.clear(); |
229 } | 229 } |
230 | 230 |
231 void DynamicSocketDataProvider::SimulateRead(const char* data, | 231 void DynamicSocketDataProvider::SimulateRead(const char* data, |
232 const size_t length) { | 232 const size_t length) { |
233 if (!allow_unconsumed_reads_) { | 233 if (!allow_unconsumed_reads_) { |
234 EXPECT_TRUE(reads_.empty()) << "Unconsumed read: " << reads_.front().data; | 234 EXPECT_TRUE(reads_.empty()) << "Unconsumed read: " << reads_.front().data; |
235 } | 235 } |
236 reads_.push_back(MockRead(true, data, length)); | 236 reads_.push_back(MockRead(ASYNC, data, length)); |
237 } | 237 } |
238 | 238 |
239 SSLSocketDataProvider::SSLSocketDataProvider(bool async, int result) | 239 SSLSocketDataProvider::SSLSocketDataProvider(IoMode mode, int result) |
240 : connect(async ? ASYNC : SYNCHRONOUS, result), | 240 : connect(mode, result), |
241 next_proto_status(SSLClientSocket::kNextProtoUnsupported), | 241 next_proto_status(SSLClientSocket::kNextProtoUnsupported), |
242 was_npn_negotiated(false), | 242 was_npn_negotiated(false), |
243 protocol_negotiated(SSLClientSocket::kProtoUnknown), | 243 protocol_negotiated(SSLClientSocket::kProtoUnknown), |
244 client_cert_sent(false), | 244 client_cert_sent(false), |
245 cert_request_info(NULL), | 245 cert_request_info(NULL), |
246 origin_bound_cert_type(CLIENT_CERT_INVALID_TYPE) { | 246 origin_bound_cert_type(CLIENT_CERT_INVALID_TYPE) { |
247 } | 247 } |
248 | 248 |
249 SSLSocketDataProvider::~SSLSocketDataProvider() { | 249 SSLSocketDataProvider::~SSLSocketDataProvider() { |
250 } | 250 } |
(...skipping 29 matching lines...) Expand all Loading... |
280 DelayedSocketData::~DelayedSocketData() { | 280 DelayedSocketData::~DelayedSocketData() { |
281 } | 281 } |
282 | 282 |
283 void DelayedSocketData::ForceNextRead() { | 283 void DelayedSocketData::ForceNextRead() { |
284 DCHECK(read_in_progress_); | 284 DCHECK(read_in_progress_); |
285 write_delay_ = 0; | 285 write_delay_ = 0; |
286 CompleteRead(); | 286 CompleteRead(); |
287 } | 287 } |
288 | 288 |
289 MockRead DelayedSocketData::GetNextRead() { | 289 MockRead DelayedSocketData::GetNextRead() { |
290 MockRead out = MockRead(true, ERR_IO_PENDING); | 290 MockRead out = MockRead(ASYNC, ERR_IO_PENDING); |
291 if (write_delay_ <= 0) | 291 if (write_delay_ <= 0) |
292 out = StaticSocketDataProvider::GetNextRead(); | 292 out = StaticSocketDataProvider::GetNextRead(); |
293 read_in_progress_ = (out.result == ERR_IO_PENDING); | 293 read_in_progress_ = (out.result == ERR_IO_PENDING); |
294 return out; | 294 return out; |
295 } | 295 } |
296 | 296 |
297 MockWriteResult DelayedSocketData::OnWrite(const std::string& data) { | 297 MockWriteResult DelayedSocketData::OnWrite(const std::string& data) { |
298 MockWriteResult rv = StaticSocketDataProvider::OnWrite(data); | 298 MockWriteResult rv = StaticSocketDataProvider::OnWrite(data); |
299 // Now that our write has completed, we can allow reads to continue. | 299 // Now that our write has completed, we can allow reads to continue. |
300 if (!--write_delay_ && read_in_progress_) | 300 if (!--write_delay_ && read_in_progress_) |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
365 if ((next_read.sequence_number & ~MockRead::STOPLOOP) <= | 365 if ((next_read.sequence_number & ~MockRead::STOPLOOP) <= |
366 sequence_number_++) { | 366 sequence_number_++) { |
367 NET_TRACE(INFO, " *** ") << "Stage " << sequence_number_ - 1 | 367 NET_TRACE(INFO, " *** ") << "Stage " << sequence_number_ - 1 |
368 << ": Read " << read_index(); | 368 << ": Read " << read_index(); |
369 DumpMockRead(next_read); | 369 DumpMockRead(next_read); |
370 blocked_ = (next_read.result == ERR_IO_PENDING); | 370 blocked_ = (next_read.result == ERR_IO_PENDING); |
371 return StaticSocketDataProvider::GetNextRead(); | 371 return StaticSocketDataProvider::GetNextRead(); |
372 } | 372 } |
373 NET_TRACE(INFO, " *** ") << "Stage " << sequence_number_ - 1 | 373 NET_TRACE(INFO, " *** ") << "Stage " << sequence_number_ - 1 |
374 << ": I/O Pending"; | 374 << ": I/O Pending"; |
375 MockRead result = MockRead(true, ERR_IO_PENDING); | 375 MockRead result = MockRead(ASYNC, ERR_IO_PENDING); |
376 DumpMockRead(result); | 376 DumpMockRead(result); |
377 blocked_ = true; | 377 blocked_ = true; |
378 return result; | 378 return result; |
379 } | 379 } |
380 | 380 |
381 MockWriteResult OrderedSocketData::OnWrite(const std::string& data) { | 381 MockWriteResult OrderedSocketData::OnWrite(const std::string& data) { |
382 NET_TRACE(INFO, " *** ") << "Stage " << sequence_number_ | 382 NET_TRACE(INFO, " *** ") << "Stage " << sequence_number_ |
383 << ": Write " << write_index(); | 383 << ": Write " << write_index(); |
384 DumpMockRead(PeekWrite()); | 384 DumpMockRead(PeekWrite()); |
385 ++sequence_number_; | 385 ++sequence_number_; |
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
468 | 468 |
469 void DeterministicSocketData::StopAfter(int seq) { | 469 void DeterministicSocketData::StopAfter(int seq) { |
470 SetStop(sequence_number_ + seq); | 470 SetStop(sequence_number_ + seq); |
471 } | 471 } |
472 | 472 |
473 MockRead DeterministicSocketData::GetNextRead() { | 473 MockRead DeterministicSocketData::GetNextRead() { |
474 current_read_ = StaticSocketDataProvider::PeekRead(); | 474 current_read_ = StaticSocketDataProvider::PeekRead(); |
475 EXPECT_LE(sequence_number_, current_read_.sequence_number); | 475 EXPECT_LE(sequence_number_, current_read_.sequence_number); |
476 | 476 |
477 // Synchronous read while stopped is an error | 477 // Synchronous read while stopped is an error |
478 if (stopped() && !current_read_.async) { | 478 if (stopped() && current_read_.mode == SYNCHRONOUS) { |
479 LOG(ERROR) << "Unable to perform synchronous IO while stopped"; | 479 LOG(ERROR) << "Unable to perform synchronous IO while stopped"; |
480 return MockRead(false, ERR_UNEXPECTED); | 480 return MockRead(SYNCHRONOUS, ERR_UNEXPECTED); |
481 } | 481 } |
482 | 482 |
483 // Async read which will be called back in a future step. | 483 // Async read which will be called back in a future step. |
484 if (sequence_number_ < current_read_.sequence_number) { | 484 if (sequence_number_ < current_read_.sequence_number) { |
485 NET_TRACE(INFO, " *** ") << "Stage " << sequence_number_ | 485 NET_TRACE(INFO, " *** ") << "Stage " << sequence_number_ |
486 << ": I/O Pending"; | 486 << ": I/O Pending"; |
487 MockRead result = MockRead(false, ERR_IO_PENDING); | 487 MockRead result = MockRead(SYNCHRONOUS, ERR_IO_PENDING); |
488 if (!current_read_.async) { | 488 if (current_read_.mode == SYNCHRONOUS) { |
489 LOG(ERROR) << "Unable to perform synchronous read: " | 489 LOG(ERROR) << "Unable to perform synchronous read: " |
490 << current_read_.sequence_number | 490 << current_read_.sequence_number |
491 << " at stage: " << sequence_number_; | 491 << " at stage: " << sequence_number_; |
492 result = MockRead(false, ERR_UNEXPECTED); | 492 result = MockRead(SYNCHRONOUS, ERR_UNEXPECTED); |
493 } | 493 } |
494 if (print_debug_) | 494 if (print_debug_) |
495 DumpMockRead(result); | 495 DumpMockRead(result); |
496 return result; | 496 return result; |
497 } | 497 } |
498 | 498 |
499 NET_TRACE(INFO, " *** ") << "Stage " << sequence_number_ | 499 NET_TRACE(INFO, " *** ") << "Stage " << sequence_number_ |
500 << ": Read " << read_index(); | 500 << ": Read " << read_index(); |
501 if (print_debug_) | 501 if (print_debug_) |
502 DumpMockRead(current_read_); | 502 DumpMockRead(current_read_); |
503 | 503 |
504 // Increment the sequence number if IO is complete | 504 // Increment the sequence number if IO is complete |
505 if (!current_read_.async) | 505 if (current_read_.mode == SYNCHRONOUS) |
506 NextStep(); | 506 NextStep(); |
507 | 507 |
508 DCHECK_NE(ERR_IO_PENDING, current_read_.result); | 508 DCHECK_NE(ERR_IO_PENDING, current_read_.result); |
509 StaticSocketDataProvider::GetNextRead(); | 509 StaticSocketDataProvider::GetNextRead(); |
510 | 510 |
511 return current_read_; | 511 return current_read_; |
512 } | 512 } |
513 | 513 |
514 MockWriteResult DeterministicSocketData::OnWrite(const std::string& data) { | 514 MockWriteResult DeterministicSocketData::OnWrite(const std::string& data) { |
515 const MockWrite& next_write = StaticSocketDataProvider::PeekWrite(); | 515 const MockWrite& next_write = StaticSocketDataProvider::PeekWrite(); |
516 current_write_ = next_write; | 516 current_write_ = next_write; |
517 | 517 |
518 // Synchronous write while stopped is an error | 518 // Synchronous write while stopped is an error |
519 if (stopped() && !next_write.async) { | 519 if (stopped() && next_write.mode == SYNCHRONOUS) { |
520 LOG(ERROR) << "Unable to perform synchronous IO while stopped"; | 520 LOG(ERROR) << "Unable to perform synchronous IO while stopped"; |
521 return MockWriteResult(false, ERR_UNEXPECTED); | 521 return MockWriteResult(SYNCHRONOUS, ERR_UNEXPECTED); |
522 } | 522 } |
523 | 523 |
524 // Async write which will be called back in a future step. | 524 // Async write which will be called back in a future step. |
525 if (sequence_number_ < next_write.sequence_number) { | 525 if (sequence_number_ < next_write.sequence_number) { |
526 NET_TRACE(INFO, " *** ") << "Stage " << sequence_number_ | 526 NET_TRACE(INFO, " *** ") << "Stage " << sequence_number_ |
527 << ": I/O Pending"; | 527 << ": I/O Pending"; |
528 if (!next_write.async) { | 528 if (next_write.mode == SYNCHRONOUS) { |
529 LOG(ERROR) << "Unable to perform synchronous write: " | 529 LOG(ERROR) << "Unable to perform synchronous write: " |
530 << next_write.sequence_number << " at stage: " << sequence_number_; | 530 << next_write.sequence_number << " at stage: " << sequence_number_; |
531 return MockWriteResult(false, ERR_UNEXPECTED); | 531 return MockWriteResult(SYNCHRONOUS, ERR_UNEXPECTED); |
532 } | 532 } |
533 } else { | 533 } else { |
534 NET_TRACE(INFO, " *** ") << "Stage " << sequence_number_ | 534 NET_TRACE(INFO, " *** ") << "Stage " << sequence_number_ |
535 << ": Write " << write_index(); | 535 << ": Write " << write_index(); |
536 } | 536 } |
537 | 537 |
538 if (print_debug_) | 538 if (print_debug_) |
539 DumpMockRead(next_write); | 539 DumpMockRead(next_write); |
540 | 540 |
541 // Move to the next step if I/O is synchronous, since the operation will | 541 // Move to the next step if I/O is synchronous, since the operation will |
542 // complete when this method returns. | 542 // complete when this method returns. |
543 if (!next_write.async) | 543 if (next_write.mode == SYNCHRONOUS) |
544 NextStep(); | 544 NextStep(); |
545 | 545 |
546 // This is either a sync write for this step, or an async write. | 546 // This is either a sync write for this step, or an async write. |
547 return StaticSocketDataProvider::OnWrite(data); | 547 return StaticSocketDataProvider::OnWrite(data); |
548 } | 548 } |
549 | 549 |
550 void DeterministicSocketData::Reset() { | 550 void DeterministicSocketData::Reset() { |
551 NET_TRACE(INFO, " *** ") << "Stage " | 551 NET_TRACE(INFO, " *** ") << "Stage " |
552 << sequence_number_ << ": Reset()"; | 552 << sequence_number_ << ": Reset()"; |
553 sequence_number_ = 0; | 553 sequence_number_ = 0; |
(...skipping 170 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
724 } | 724 } |
725 | 725 |
726 MockTCPClientSocket::MockTCPClientSocket(const AddressList& addresses, | 726 MockTCPClientSocket::MockTCPClientSocket(const AddressList& addresses, |
727 net::NetLog* net_log, | 727 net::NetLog* net_log, |
728 SocketDataProvider* data) | 728 SocketDataProvider* data) |
729 : MockClientSocket(net_log), | 729 : MockClientSocket(net_log), |
730 addresses_(addresses), | 730 addresses_(addresses), |
731 data_(data), | 731 data_(data), |
732 read_offset_(0), | 732 read_offset_(0), |
733 num_bytes_read_(0), | 733 num_bytes_read_(0), |
734 read_data_(false, ERR_UNEXPECTED), | 734 read_data_(SYNCHRONOUS, ERR_UNEXPECTED), |
735 need_read_data_(true), | 735 need_read_data_(true), |
736 peer_closed_connection_(false), | 736 peer_closed_connection_(false), |
737 pending_buf_(NULL), | 737 pending_buf_(NULL), |
738 pending_buf_len_(0), | 738 pending_buf_len_(0), |
739 was_used_to_convey_data_(false) { | 739 was_used_to_convey_data_(false) { |
740 DCHECK(data_); | 740 DCHECK(data_); |
741 data_->Reset(); | 741 data_->Reset(); |
742 } | 742 } |
743 | 743 |
744 MockTCPClientSocket::~MockTCPClientSocket() {} | 744 MockTCPClientSocket::~MockTCPClientSocket() {} |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
783 DCHECK_GT(buf_len, 0); | 783 DCHECK_GT(buf_len, 0); |
784 | 784 |
785 if (!connected_) | 785 if (!connected_) |
786 return ERR_UNEXPECTED; | 786 return ERR_UNEXPECTED; |
787 | 787 |
788 std::string data(buf->data(), buf_len); | 788 std::string data(buf->data(), buf_len); |
789 MockWriteResult write_result = data_->OnWrite(data); | 789 MockWriteResult write_result = data_->OnWrite(data); |
790 | 790 |
791 was_used_to_convey_data_ = true; | 791 was_used_to_convey_data_ = true; |
792 | 792 |
793 if (write_result.async) { | 793 if (write_result.mode == ASYNC) { |
794 RunCallbackAsync(callback, write_result.result); | 794 RunCallbackAsync(callback, write_result.result); |
795 return ERR_IO_PENDING; | 795 return ERR_IO_PENDING; |
796 } | 796 } |
797 | 797 |
798 return write_result.result; | 798 return write_result.result; |
799 } | 799 } |
800 | 800 |
801 int MockTCPClientSocket::Connect(const CompletionCallback& callback) { | 801 int MockTCPClientSocket::Connect(const CompletionCallback& callback) { |
802 if (connected_) | 802 if (connected_) |
803 return OK; | 803 return OK; |
804 connected_ = true; | 804 connected_ = true; |
805 peer_closed_connection_ = false; | 805 peer_closed_connection_ = false; |
806 if (data_->connect_data().async) { | 806 if (data_->connect_data().mode == ASYNC) { |
807 RunCallbackAsync(callback, data_->connect_data().result); | 807 RunCallbackAsync(callback, data_->connect_data().result); |
808 return ERR_IO_PENDING; | 808 return ERR_IO_PENDING; |
809 } | 809 } |
810 return data_->connect_data().result; | 810 return data_->connect_data().result; |
811 } | 811 } |
812 | 812 |
813 void MockTCPClientSocket::Disconnect() { | 813 void MockTCPClientSocket::Disconnect() { |
814 MockClientSocket::Disconnect(); | 814 MockClientSocket::Disconnect(); |
815 pending_callback_.Reset(); | 815 pending_callback_.Reset(); |
816 } | 816 } |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
854 // You can't complete a read with another ERR_IO_PENDING status code. | 854 // You can't complete a read with another ERR_IO_PENDING status code. |
855 DCHECK_NE(ERR_IO_PENDING, data.result); | 855 DCHECK_NE(ERR_IO_PENDING, data.result); |
856 // Since we've been waiting for data, need_read_data_ should be true. | 856 // Since we've been waiting for data, need_read_data_ should be true. |
857 DCHECK(need_read_data_); | 857 DCHECK(need_read_data_); |
858 | 858 |
859 read_data_ = data; | 859 read_data_ = data; |
860 need_read_data_ = false; | 860 need_read_data_ = false; |
861 | 861 |
862 // The caller is simulating that this IO completes right now. Don't | 862 // The caller is simulating that this IO completes right now. Don't |
863 // let CompleteRead() schedule a callback. | 863 // let CompleteRead() schedule a callback. |
864 read_data_.async = false; | 864 read_data_.mode = SYNCHRONOUS; |
865 | 865 |
866 CompletionCallback callback = pending_callback_; | 866 CompletionCallback callback = pending_callback_; |
867 int rv = CompleteRead(); | 867 int rv = CompleteRead(); |
868 RunCallback(callback, rv); | 868 RunCallback(callback, rv); |
869 } | 869 } |
870 | 870 |
871 int MockTCPClientSocket::CompleteRead() { | 871 int MockTCPClientSocket::CompleteRead() { |
872 DCHECK(pending_buf_); | 872 DCHECK(pending_buf_); |
873 DCHECK(pending_buf_len_ > 0); | 873 DCHECK(pending_buf_len_ > 0); |
874 | 874 |
(...skipping 18 matching lines...) Expand all Loading... |
893 num_bytes_read_ += result; | 893 num_bytes_read_ += result; |
894 if (read_offset_ == read_data_.data_len) { | 894 if (read_offset_ == read_data_.data_len) { |
895 need_read_data_ = true; | 895 need_read_data_ = true; |
896 read_offset_ = 0; | 896 read_offset_ = 0; |
897 } | 897 } |
898 } else { | 898 } else { |
899 result = 0; // EOF | 899 result = 0; // EOF |
900 } | 900 } |
901 } | 901 } |
902 | 902 |
903 if (read_data_.async) { | 903 if (read_data_.mode == ASYNC) { |
904 DCHECK(!callback.is_null()); | 904 DCHECK(!callback.is_null()); |
905 RunCallbackAsync(callback, result); | 905 RunCallbackAsync(callback, result); |
906 return ERR_IO_PENDING; | 906 return ERR_IO_PENDING; |
907 } | 907 } |
908 return result; | 908 return result; |
909 } | 909 } |
910 | 910 |
911 DeterministicMockTCPClientSocket::DeterministicMockTCPClientSocket( | 911 DeterministicMockTCPClientSocket::DeterministicMockTCPClientSocket( |
912 net::NetLog* net_log, DeterministicSocketData* data) | 912 net::NetLog* net_log, DeterministicSocketData* data) |
913 : MockClientSocket(net_log), | 913 : MockClientSocket(net_log), |
(...skipping 17 matching lines...) Expand all Loading... |
931 int DeterministicMockTCPClientSocket::CompleteRead() { | 931 int DeterministicMockTCPClientSocket::CompleteRead() { |
932 DCHECK_GT(read_buf_len_, 0); | 932 DCHECK_GT(read_buf_len_, 0); |
933 DCHECK_LE(read_data_.data_len, read_buf_len_); | 933 DCHECK_LE(read_data_.data_len, read_buf_len_); |
934 DCHECK(read_buf_); | 934 DCHECK(read_buf_); |
935 | 935 |
936 was_used_to_convey_data_ = true; | 936 was_used_to_convey_data_ = true; |
937 | 937 |
938 if (read_data_.result == ERR_IO_PENDING) | 938 if (read_data_.result == ERR_IO_PENDING) |
939 read_data_ = data_->GetNextRead(); | 939 read_data_ = data_->GetNextRead(); |
940 DCHECK_NE(ERR_IO_PENDING, read_data_.result); | 940 DCHECK_NE(ERR_IO_PENDING, read_data_.result); |
941 // If read_data_.async is true, we do not need to wait, since this is already | 941 // If read_data_.mode is ASYNC, we do not need to wait, since this is already |
942 // the callback. Therefore we don't even bother to check it. | 942 // the callback. Therefore we don't even bother to check it. |
943 int result = read_data_.result; | 943 int result = read_data_.result; |
944 | 944 |
945 if (read_data_.data_len > 0) { | 945 if (read_data_.data_len > 0) { |
946 DCHECK(read_data_.data); | 946 DCHECK(read_data_.data); |
947 result = std::min(read_buf_len_, read_data_.data_len); | 947 result = std::min(read_buf_len_, read_data_.data_len); |
948 memcpy(read_buf_->data(), read_data_.data, result); | 948 memcpy(read_buf_->data(), read_data_.data, result); |
949 } | 949 } |
950 | 950 |
951 if (read_pending_) { | 951 if (read_pending_) { |
952 read_pending_ = false; | 952 read_pending_ = false; |
953 read_callback_.Run(result); | 953 read_callback_.Run(result); |
954 } | 954 } |
955 | 955 |
956 return result; | 956 return result; |
957 } | 957 } |
958 | 958 |
959 int DeterministicMockTCPClientSocket::Write( | 959 int DeterministicMockTCPClientSocket::Write( |
960 IOBuffer* buf, int buf_len, const CompletionCallback& callback) { | 960 IOBuffer* buf, int buf_len, const CompletionCallback& callback) { |
961 DCHECK(buf); | 961 DCHECK(buf); |
962 DCHECK_GT(buf_len, 0); | 962 DCHECK_GT(buf_len, 0); |
963 | 963 |
964 if (!connected_) | 964 if (!connected_) |
965 return ERR_UNEXPECTED; | 965 return ERR_UNEXPECTED; |
966 | 966 |
967 std::string data(buf->data(), buf_len); | 967 std::string data(buf->data(), buf_len); |
968 MockWriteResult write_result = data_->OnWrite(data); | 968 MockWriteResult write_result = data_->OnWrite(data); |
969 | 969 |
970 if (write_result.async) { | 970 if (write_result.mode == ASYNC) { |
971 write_callback_ = callback; | 971 write_callback_ = callback; |
972 write_result_ = write_result.result; | 972 write_result_ = write_result.result; |
973 DCHECK(!write_callback_.is_null()); | 973 DCHECK(!write_callback_.is_null()); |
974 write_pending_ = true; | 974 write_pending_ = true; |
975 return ERR_IO_PENDING; | 975 return ERR_IO_PENDING; |
976 } | 976 } |
977 | 977 |
978 was_used_to_convey_data_ = true; | 978 was_used_to_convey_data_ = true; |
979 write_pending_ = false; | 979 write_pending_ = false; |
980 return write_result.result; | 980 return write_result.result; |
981 } | 981 } |
982 | 982 |
983 int DeterministicMockTCPClientSocket::Read( | 983 int DeterministicMockTCPClientSocket::Read( |
984 IOBuffer* buf, int buf_len, const CompletionCallback& callback) { | 984 IOBuffer* buf, int buf_len, const CompletionCallback& callback) { |
985 if (!connected_) | 985 if (!connected_) |
986 return ERR_UNEXPECTED; | 986 return ERR_UNEXPECTED; |
987 | 987 |
988 read_data_ = data_->GetNextRead(); | 988 read_data_ = data_->GetNextRead(); |
989 // The buffer should always be big enough to contain all the MockRead data. To | 989 // The buffer should always be big enough to contain all the MockRead data. To |
990 // use small buffers, split the data into multiple MockReads. | 990 // use small buffers, split the data into multiple MockReads. |
991 DCHECK_LE(read_data_.data_len, buf_len); | 991 DCHECK_LE(read_data_.data_len, buf_len); |
992 | 992 |
993 read_buf_ = buf; | 993 read_buf_ = buf; |
994 read_buf_len_ = buf_len; | 994 read_buf_len_ = buf_len; |
995 read_callback_ = callback; | 995 read_callback_ = callback; |
996 | 996 |
997 if (read_data_.async || (read_data_.result == ERR_IO_PENDING)) { | 997 if (read_data_.mode == ASYNC || (read_data_.result == ERR_IO_PENDING)) { |
998 read_pending_ = true; | 998 read_pending_ = true; |
999 DCHECK(!read_callback_.is_null()); | 999 DCHECK(!read_callback_.is_null()); |
1000 return ERR_IO_PENDING; | 1000 return ERR_IO_PENDING; |
1001 } | 1001 } |
1002 | 1002 |
1003 was_used_to_convey_data_ = true; | 1003 was_used_to_convey_data_ = true; |
1004 return CompleteRead(); | 1004 return CompleteRead(); |
1005 } | 1005 } |
1006 | 1006 |
1007 // TODO(erikchen): Support connect sequencing. | 1007 // TODO(erikchen): Support connect sequencing. |
1008 int DeterministicMockTCPClientSocket::Connect( | 1008 int DeterministicMockTCPClientSocket::Connect( |
1009 const CompletionCallback& callback) { | 1009 const CompletionCallback& callback) { |
1010 if (connected_) | 1010 if (connected_) |
1011 return OK; | 1011 return OK; |
1012 connected_ = true; | 1012 connected_ = true; |
1013 if (data_->connect_data().async) { | 1013 if (data_->connect_data().mode == ASYNC) { |
1014 RunCallbackAsync(callback, data_->connect_data().result); | 1014 RunCallbackAsync(callback, data_->connect_data().result); |
1015 return ERR_IO_PENDING; | 1015 return ERR_IO_PENDING; |
1016 } | 1016 } |
1017 return data_->connect_data().result; | 1017 return data_->connect_data().result; |
1018 } | 1018 } |
1019 | 1019 |
1020 void DeterministicMockTCPClientSocket::Disconnect() { | 1020 void DeterministicMockTCPClientSocket::Disconnect() { |
1021 MockClientSocket::Disconnect(); | 1021 MockClientSocket::Disconnect(); |
1022 } | 1022 } |
1023 | 1023 |
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1087 const CompletionCallback& callback) { | 1087 const CompletionCallback& callback) { |
1088 return transport_->socket()->Write(buf, buf_len, callback); | 1088 return transport_->socket()->Write(buf, buf_len, callback); |
1089 } | 1089 } |
1090 | 1090 |
1091 int MockSSLClientSocket::Connect(const CompletionCallback& callback) { | 1091 int MockSSLClientSocket::Connect(const CompletionCallback& callback) { |
1092 int rv = transport_->socket()->Connect( | 1092 int rv = transport_->socket()->Connect( |
1093 base::Bind(&ConnectCallback, base::Unretained(this), callback)); | 1093 base::Bind(&ConnectCallback, base::Unretained(this), callback)); |
1094 if (rv == OK) { | 1094 if (rv == OK) { |
1095 if (data_->connect.result == OK) | 1095 if (data_->connect.result == OK) |
1096 connected_ = true; | 1096 connected_ = true; |
1097 if (data_->connect.async) { | 1097 if (data_->connect.mode == ASYNC) { |
1098 RunCallbackAsync(callback, data_->connect.result); | 1098 RunCallbackAsync(callback, data_->connect.result); |
1099 return ERR_IO_PENDING; | 1099 return ERR_IO_PENDING; |
1100 } | 1100 } |
1101 return data_->connect.result; | 1101 return data_->connect.result; |
1102 } | 1102 } |
1103 return rv; | 1103 return rv; |
1104 } | 1104 } |
1105 | 1105 |
1106 void MockSSLClientSocket::Disconnect() { | 1106 void MockSSLClientSocket::Disconnect() { |
1107 MockClientSocket::Disconnect(); | 1107 MockClientSocket::Disconnect(); |
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1196 | 1196 |
1197 void MockSSLClientSocket::OnReadComplete(const MockRead& data) { | 1197 void MockSSLClientSocket::OnReadComplete(const MockRead& data) { |
1198 NOTIMPLEMENTED(); | 1198 NOTIMPLEMENTED(); |
1199 } | 1199 } |
1200 | 1200 |
1201 MockUDPClientSocket::MockUDPClientSocket(SocketDataProvider* data, | 1201 MockUDPClientSocket::MockUDPClientSocket(SocketDataProvider* data, |
1202 net::NetLog* net_log) | 1202 net::NetLog* net_log) |
1203 : connected_(false), | 1203 : connected_(false), |
1204 data_(data), | 1204 data_(data), |
1205 read_offset_(0), | 1205 read_offset_(0), |
1206 read_data_(false, ERR_UNEXPECTED), | 1206 read_data_(SYNCHRONOUS, ERR_UNEXPECTED), |
1207 need_read_data_(true), | 1207 need_read_data_(true), |
1208 pending_buf_(NULL), | 1208 pending_buf_(NULL), |
1209 pending_buf_len_(0), | 1209 pending_buf_len_(0), |
1210 net_log_(net::NetLog::Source(), net_log), | 1210 net_log_(net::NetLog::Source(), net_log), |
1211 ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)) { | 1211 ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)) { |
1212 DCHECK(data_); | 1212 DCHECK(data_); |
1213 data_->Reset(); | 1213 data_->Reset(); |
1214 } | 1214 } |
1215 | 1215 |
1216 MockUDPClientSocket::~MockUDPClientSocket() {} | 1216 MockUDPClientSocket::~MockUDPClientSocket() {} |
(...skipping 30 matching lines...) Expand all Loading... |
1247 const CompletionCallback& callback) { | 1247 const CompletionCallback& callback) { |
1248 DCHECK(buf); | 1248 DCHECK(buf); |
1249 DCHECK_GT(buf_len, 0); | 1249 DCHECK_GT(buf_len, 0); |
1250 | 1250 |
1251 if (!connected_) | 1251 if (!connected_) |
1252 return ERR_UNEXPECTED; | 1252 return ERR_UNEXPECTED; |
1253 | 1253 |
1254 std::string data(buf->data(), buf_len); | 1254 std::string data(buf->data(), buf_len); |
1255 MockWriteResult write_result = data_->OnWrite(data); | 1255 MockWriteResult write_result = data_->OnWrite(data); |
1256 | 1256 |
1257 if (write_result.async) { | 1257 if (write_result.mode == ASYNC) { |
1258 RunCallbackAsync(callback, write_result.result); | 1258 RunCallbackAsync(callback, write_result.result); |
1259 return ERR_IO_PENDING; | 1259 return ERR_IO_PENDING; |
1260 } | 1260 } |
1261 return write_result.result; | 1261 return write_result.result; |
1262 } | 1262 } |
1263 | 1263 |
1264 bool MockUDPClientSocket::SetReceiveBufferSize(int32 size) { | 1264 bool MockUDPClientSocket::SetReceiveBufferSize(int32 size) { |
1265 return true; | 1265 return true; |
1266 } | 1266 } |
1267 | 1267 |
(...skipping 30 matching lines...) Expand all Loading... |
1298 // You can't complete a read with another ERR_IO_PENDING status code. | 1298 // You can't complete a read with another ERR_IO_PENDING status code. |
1299 DCHECK_NE(ERR_IO_PENDING, data.result); | 1299 DCHECK_NE(ERR_IO_PENDING, data.result); |
1300 // Since we've been waiting for data, need_read_data_ should be true. | 1300 // Since we've been waiting for data, need_read_data_ should be true. |
1301 DCHECK(need_read_data_); | 1301 DCHECK(need_read_data_); |
1302 | 1302 |
1303 read_data_ = data; | 1303 read_data_ = data; |
1304 need_read_data_ = false; | 1304 need_read_data_ = false; |
1305 | 1305 |
1306 // The caller is simulating that this IO completes right now. Don't | 1306 // The caller is simulating that this IO completes right now. Don't |
1307 // let CompleteRead() schedule a callback. | 1307 // let CompleteRead() schedule a callback. |
1308 read_data_.async = false; | 1308 read_data_.mode = SYNCHRONOUS; |
1309 | 1309 |
1310 net::CompletionCallback callback = pending_callback_; | 1310 net::CompletionCallback callback = pending_callback_; |
1311 int rv = CompleteRead(); | 1311 int rv = CompleteRead(); |
1312 RunCallback(callback, rv); | 1312 RunCallback(callback, rv); |
1313 } | 1313 } |
1314 | 1314 |
1315 int MockUDPClientSocket::CompleteRead() { | 1315 int MockUDPClientSocket::CompleteRead() { |
1316 DCHECK(pending_buf_); | 1316 DCHECK(pending_buf_); |
1317 DCHECK(pending_buf_len_ > 0); | 1317 DCHECK(pending_buf_len_ > 0); |
1318 | 1318 |
(...skipping 15 matching lines...) Expand all Loading... |
1334 read_offset_ += result; | 1334 read_offset_ += result; |
1335 if (read_offset_ == read_data_.data_len) { | 1335 if (read_offset_ == read_data_.data_len) { |
1336 need_read_data_ = true; | 1336 need_read_data_ = true; |
1337 read_offset_ = 0; | 1337 read_offset_ = 0; |
1338 } | 1338 } |
1339 } else { | 1339 } else { |
1340 result = 0; // EOF | 1340 result = 0; // EOF |
1341 } | 1341 } |
1342 } | 1342 } |
1343 | 1343 |
1344 if (read_data_.async) { | 1344 if (read_data_.mode == ASYNC) { |
1345 DCHECK(!callback.is_null()); | 1345 DCHECK(!callback.is_null()); |
1346 RunCallbackAsync(callback, result); | 1346 RunCallbackAsync(callback, result); |
1347 return ERR_IO_PENDING; | 1347 return ERR_IO_PENDING; |
1348 } | 1348 } |
1349 return result; | 1349 return result; |
1350 } | 1350 } |
1351 | 1351 |
1352 void MockUDPClientSocket::RunCallbackAsync(const CompletionCallback& callback, | 1352 void MockUDPClientSocket::RunCallbackAsync(const CompletionCallback& callback, |
1353 int result) { | 1353 int result) { |
1354 MessageLoop::current()->PostTask( | 1354 MessageLoop::current()->PostTask( |
(...skipping 264 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1619 | 1619 |
1620 const char kSOCKS5OkRequest[] = | 1620 const char kSOCKS5OkRequest[] = |
1621 { 0x05, 0x01, 0x00, 0x03, 0x04, 'h', 'o', 's', 't', 0x00, 0x50 }; | 1621 { 0x05, 0x01, 0x00, 0x03, 0x04, 'h', 'o', 's', 't', 0x00, 0x50 }; |
1622 const int kSOCKS5OkRequestLength = arraysize(kSOCKS5OkRequest); | 1622 const int kSOCKS5OkRequestLength = arraysize(kSOCKS5OkRequest); |
1623 | 1623 |
1624 const char kSOCKS5OkResponse[] = | 1624 const char kSOCKS5OkResponse[] = |
1625 { 0x05, 0x00, 0x00, 0x01, 127, 0, 0, 1, 0x00, 0x50 }; | 1625 { 0x05, 0x00, 0x00, 0x01, 127, 0, 0, 1, 0x00, 0x50 }; |
1626 const int kSOCKS5OkResponseLength = arraysize(kSOCKS5OkResponse); | 1626 const int kSOCKS5OkResponseLength = arraysize(kSOCKS5OkResponse); |
1627 | 1627 |
1628 } // namespace net | 1628 } // namespace net |
OLD | NEW |