Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(100)

Side by Side Diff: net/socket/socket_test_util.cc

Issue 9425016: Change MockRead and MockWrite (et. al.) to take an IoMode enum, instead (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: sync Created 8 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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
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
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 22 matching lines...) Expand all
273 DelayedSocketData::~DelayedSocketData() { 273 DelayedSocketData::~DelayedSocketData() {
274 } 274 }
275 275
276 void DelayedSocketData::ForceNextRead() { 276 void DelayedSocketData::ForceNextRead() {
277 DCHECK(read_in_progress_); 277 DCHECK(read_in_progress_);
278 write_delay_ = 0; 278 write_delay_ = 0;
279 CompleteRead(); 279 CompleteRead();
280 } 280 }
281 281
282 MockRead DelayedSocketData::GetNextRead() { 282 MockRead DelayedSocketData::GetNextRead() {
283 MockRead out = MockRead(true, ERR_IO_PENDING); 283 MockRead out = MockRead(ASYNC, ERR_IO_PENDING);
284 if (write_delay_ <= 0) 284 if (write_delay_ <= 0)
285 out = StaticSocketDataProvider::GetNextRead(); 285 out = StaticSocketDataProvider::GetNextRead();
286 read_in_progress_ = (out.result == ERR_IO_PENDING); 286 read_in_progress_ = (out.result == ERR_IO_PENDING);
287 return out; 287 return out;
288 } 288 }
289 289
290 MockWriteResult DelayedSocketData::OnWrite(const std::string& data) { 290 MockWriteResult DelayedSocketData::OnWrite(const std::string& data) {
291 MockWriteResult rv = StaticSocketDataProvider::OnWrite(data); 291 MockWriteResult rv = StaticSocketDataProvider::OnWrite(data);
292 // Now that our write has completed, we can allow reads to continue. 292 // Now that our write has completed, we can allow reads to continue.
293 if (!--write_delay_ && read_in_progress_) 293 if (!--write_delay_ && read_in_progress_)
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after
358 if ((next_read.sequence_number & ~MockRead::STOPLOOP) <= 358 if ((next_read.sequence_number & ~MockRead::STOPLOOP) <=
359 sequence_number_++) { 359 sequence_number_++) {
360 NET_TRACE(INFO, " *** ") << "Stage " << sequence_number_ - 1 360 NET_TRACE(INFO, " *** ") << "Stage " << sequence_number_ - 1
361 << ": Read " << read_index(); 361 << ": Read " << read_index();
362 DumpMockRead(next_read); 362 DumpMockRead(next_read);
363 blocked_ = (next_read.result == ERR_IO_PENDING); 363 blocked_ = (next_read.result == ERR_IO_PENDING);
364 return StaticSocketDataProvider::GetNextRead(); 364 return StaticSocketDataProvider::GetNextRead();
365 } 365 }
366 NET_TRACE(INFO, " *** ") << "Stage " << sequence_number_ - 1 366 NET_TRACE(INFO, " *** ") << "Stage " << sequence_number_ - 1
367 << ": I/O Pending"; 367 << ": I/O Pending";
368 MockRead result = MockRead(true, ERR_IO_PENDING); 368 MockRead result = MockRead(ASYNC, ERR_IO_PENDING);
369 DumpMockRead(result); 369 DumpMockRead(result);
370 blocked_ = true; 370 blocked_ = true;
371 return result; 371 return result;
372 } 372 }
373 373
374 MockWriteResult OrderedSocketData::OnWrite(const std::string& data) { 374 MockWriteResult OrderedSocketData::OnWrite(const std::string& data) {
375 NET_TRACE(INFO, " *** ") << "Stage " << sequence_number_ 375 NET_TRACE(INFO, " *** ") << "Stage " << sequence_number_
376 << ": Write " << write_index(); 376 << ": Write " << write_index();
377 DumpMockRead(PeekWrite()); 377 DumpMockRead(PeekWrite());
378 ++sequence_number_; 378 ++sequence_number_;
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after
461 461
462 void DeterministicSocketData::StopAfter(int seq) { 462 void DeterministicSocketData::StopAfter(int seq) {
463 SetStop(sequence_number_ + seq); 463 SetStop(sequence_number_ + seq);
464 } 464 }
465 465
466 MockRead DeterministicSocketData::GetNextRead() { 466 MockRead DeterministicSocketData::GetNextRead() {
467 current_read_ = StaticSocketDataProvider::PeekRead(); 467 current_read_ = StaticSocketDataProvider::PeekRead();
468 EXPECT_LE(sequence_number_, current_read_.sequence_number); 468 EXPECT_LE(sequence_number_, current_read_.sequence_number);
469 469
470 // Synchronous read while stopped is an error 470 // Synchronous read while stopped is an error
471 if (stopped() && !current_read_.async) { 471 if (stopped() && current_read_.mode == SYNCHRONOUS) {
472 LOG(ERROR) << "Unable to perform synchronous IO while stopped"; 472 LOG(ERROR) << "Unable to perform synchronous IO while stopped";
473 return MockRead(false, ERR_UNEXPECTED); 473 return MockRead(SYNCHRONOUS, ERR_UNEXPECTED);
474 } 474 }
475 475
476 // Async read which will be called back in a future step. 476 // Async read which will be called back in a future step.
477 if (sequence_number_ < current_read_.sequence_number) { 477 if (sequence_number_ < current_read_.sequence_number) {
478 NET_TRACE(INFO, " *** ") << "Stage " << sequence_number_ 478 NET_TRACE(INFO, " *** ") << "Stage " << sequence_number_
479 << ": I/O Pending"; 479 << ": I/O Pending";
480 MockRead result = MockRead(false, ERR_IO_PENDING); 480 MockRead result = MockRead(SYNCHRONOUS, ERR_IO_PENDING);
481 if (!current_read_.async) { 481 if (current_read_.mode == SYNCHRONOUS) {
482 LOG(ERROR) << "Unable to perform synchronous read: " 482 LOG(ERROR) << "Unable to perform synchronous read: "
483 << current_read_.sequence_number 483 << current_read_.sequence_number
484 << " at stage: " << sequence_number_; 484 << " at stage: " << sequence_number_;
485 result = MockRead(false, ERR_UNEXPECTED); 485 result = MockRead(SYNCHRONOUS, ERR_UNEXPECTED);
486 } 486 }
487 if (print_debug_) 487 if (print_debug_)
488 DumpMockRead(result); 488 DumpMockRead(result);
489 return result; 489 return result;
490 } 490 }
491 491
492 NET_TRACE(INFO, " *** ") << "Stage " << sequence_number_ 492 NET_TRACE(INFO, " *** ") << "Stage " << sequence_number_
493 << ": Read " << read_index(); 493 << ": Read " << read_index();
494 if (print_debug_) 494 if (print_debug_)
495 DumpMockRead(current_read_); 495 DumpMockRead(current_read_);
496 496
497 // Increment the sequence number if IO is complete 497 // Increment the sequence number if IO is complete
498 if (!current_read_.async) 498 if (current_read_.mode == SYNCHRONOUS)
499 NextStep(); 499 NextStep();
500 500
501 DCHECK_NE(ERR_IO_PENDING, current_read_.result); 501 DCHECK_NE(ERR_IO_PENDING, current_read_.result);
502 StaticSocketDataProvider::GetNextRead(); 502 StaticSocketDataProvider::GetNextRead();
503 503
504 return current_read_; 504 return current_read_;
505 } 505 }
506 506
507 MockWriteResult DeterministicSocketData::OnWrite(const std::string& data) { 507 MockWriteResult DeterministicSocketData::OnWrite(const std::string& data) {
508 const MockWrite& next_write = StaticSocketDataProvider::PeekWrite(); 508 const MockWrite& next_write = StaticSocketDataProvider::PeekWrite();
509 current_write_ = next_write; 509 current_write_ = next_write;
510 510
511 // Synchronous write while stopped is an error 511 // Synchronous write while stopped is an error
512 if (stopped() && !next_write.async) { 512 if (stopped() && next_write.mode == SYNCHRONOUS) {
513 LOG(ERROR) << "Unable to perform synchronous IO while stopped"; 513 LOG(ERROR) << "Unable to perform synchronous IO while stopped";
514 return MockWriteResult(false, ERR_UNEXPECTED); 514 return MockWriteResult(SYNCHRONOUS, ERR_UNEXPECTED);
515 } 515 }
516 516
517 // Async write which will be called back in a future step. 517 // Async write which will be called back in a future step.
518 if (sequence_number_ < next_write.sequence_number) { 518 if (sequence_number_ < next_write.sequence_number) {
519 NET_TRACE(INFO, " *** ") << "Stage " << sequence_number_ 519 NET_TRACE(INFO, " *** ") << "Stage " << sequence_number_
520 << ": I/O Pending"; 520 << ": I/O Pending";
521 if (!next_write.async) { 521 if (next_write.mode == SYNCHRONOUS) {
522 LOG(ERROR) << "Unable to perform synchronous write: " 522 LOG(ERROR) << "Unable to perform synchronous write: "
523 << next_write.sequence_number << " at stage: " << sequence_number_; 523 << next_write.sequence_number << " at stage: " << sequence_number_;
524 return MockWriteResult(false, ERR_UNEXPECTED); 524 return MockWriteResult(SYNCHRONOUS, ERR_UNEXPECTED);
525 } 525 }
526 } else { 526 } else {
527 NET_TRACE(INFO, " *** ") << "Stage " << sequence_number_ 527 NET_TRACE(INFO, " *** ") << "Stage " << sequence_number_
528 << ": Write " << write_index(); 528 << ": Write " << write_index();
529 } 529 }
530 530
531 if (print_debug_) 531 if (print_debug_)
532 DumpMockRead(next_write); 532 DumpMockRead(next_write);
533 533
534 // Move to the next step if I/O is synchronous, since the operation will 534 // Move to the next step if I/O is synchronous, since the operation will
535 // complete when this method returns. 535 // complete when this method returns.
536 if (!next_write.async) 536 if (next_write.mode == SYNCHRONOUS)
537 NextStep(); 537 NextStep();
538 538
539 // This is either a sync write for this step, or an async write. 539 // This is either a sync write for this step, or an async write.
540 return StaticSocketDataProvider::OnWrite(data); 540 return StaticSocketDataProvider::OnWrite(data);
541 } 541 }
542 542
543 void DeterministicSocketData::Reset() { 543 void DeterministicSocketData::Reset() {
544 NET_TRACE(INFO, " *** ") << "Stage " 544 NET_TRACE(INFO, " *** ") << "Stage "
545 << sequence_number_ << ": Reset()"; 545 << sequence_number_ << ": Reset()";
546 sequence_number_ = 0; 546 sequence_number_ = 0;
(...skipping 170 matching lines...) Expand 10 before | Expand all | Expand 10 after
717 } 717 }
718 718
719 MockTCPClientSocket::MockTCPClientSocket(const AddressList& addresses, 719 MockTCPClientSocket::MockTCPClientSocket(const AddressList& addresses,
720 net::NetLog* net_log, 720 net::NetLog* net_log,
721 SocketDataProvider* data) 721 SocketDataProvider* data)
722 : MockClientSocket(net_log), 722 : MockClientSocket(net_log),
723 addresses_(addresses), 723 addresses_(addresses),
724 data_(data), 724 data_(data),
725 read_offset_(0), 725 read_offset_(0),
726 num_bytes_read_(0), 726 num_bytes_read_(0),
727 read_data_(false, ERR_UNEXPECTED), 727 read_data_(SYNCHRONOUS, ERR_UNEXPECTED),
728 need_read_data_(true), 728 need_read_data_(true),
729 peer_closed_connection_(false), 729 peer_closed_connection_(false),
730 pending_buf_(NULL), 730 pending_buf_(NULL),
731 pending_buf_len_(0), 731 pending_buf_len_(0),
732 was_used_to_convey_data_(false) { 732 was_used_to_convey_data_(false) {
733 DCHECK(data_); 733 DCHECK(data_);
734 data_->Reset(); 734 data_->Reset();
735 } 735 }
736 736
737 MockTCPClientSocket::~MockTCPClientSocket() {} 737 MockTCPClientSocket::~MockTCPClientSocket() {}
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
776 DCHECK_GT(buf_len, 0); 776 DCHECK_GT(buf_len, 0);
777 777
778 if (!connected_) 778 if (!connected_)
779 return ERR_UNEXPECTED; 779 return ERR_UNEXPECTED;
780 780
781 std::string data(buf->data(), buf_len); 781 std::string data(buf->data(), buf_len);
782 MockWriteResult write_result = data_->OnWrite(data); 782 MockWriteResult write_result = data_->OnWrite(data);
783 783
784 was_used_to_convey_data_ = true; 784 was_used_to_convey_data_ = true;
785 785
786 if (write_result.async) { 786 if (write_result.mode == ASYNC) {
787 RunCallbackAsync(callback, write_result.result); 787 RunCallbackAsync(callback, write_result.result);
788 return ERR_IO_PENDING; 788 return ERR_IO_PENDING;
789 } 789 }
790 790
791 return write_result.result; 791 return write_result.result;
792 } 792 }
793 793
794 int MockTCPClientSocket::Connect(const CompletionCallback& callback) { 794 int MockTCPClientSocket::Connect(const CompletionCallback& callback) {
795 if (connected_) 795 if (connected_)
796 return OK; 796 return OK;
797 connected_ = true; 797 connected_ = true;
798 peer_closed_connection_ = false; 798 peer_closed_connection_ = false;
799 if (data_->connect_data().async) { 799 if (data_->connect_data().mode == ASYNC) {
800 RunCallbackAsync(callback, data_->connect_data().result); 800 RunCallbackAsync(callback, data_->connect_data().result);
801 return ERR_IO_PENDING; 801 return ERR_IO_PENDING;
802 } 802 }
803 return data_->connect_data().result; 803 return data_->connect_data().result;
804 } 804 }
805 805
806 void MockTCPClientSocket::Disconnect() { 806 void MockTCPClientSocket::Disconnect() {
807 MockClientSocket::Disconnect(); 807 MockClientSocket::Disconnect();
808 pending_callback_.Reset(); 808 pending_callback_.Reset();
809 } 809 }
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
847 // You can't complete a read with another ERR_IO_PENDING status code. 847 // You can't complete a read with another ERR_IO_PENDING status code.
848 DCHECK_NE(ERR_IO_PENDING, data.result); 848 DCHECK_NE(ERR_IO_PENDING, data.result);
849 // Since we've been waiting for data, need_read_data_ should be true. 849 // Since we've been waiting for data, need_read_data_ should be true.
850 DCHECK(need_read_data_); 850 DCHECK(need_read_data_);
851 851
852 read_data_ = data; 852 read_data_ = data;
853 need_read_data_ = false; 853 need_read_data_ = false;
854 854
855 // The caller is simulating that this IO completes right now. Don't 855 // The caller is simulating that this IO completes right now. Don't
856 // let CompleteRead() schedule a callback. 856 // let CompleteRead() schedule a callback.
857 read_data_.async = false; 857 read_data_.mode = SYNCHRONOUS;
858 858
859 CompletionCallback callback = pending_callback_; 859 CompletionCallback callback = pending_callback_;
860 int rv = CompleteRead(); 860 int rv = CompleteRead();
861 RunCallback(callback, rv); 861 RunCallback(callback, rv);
862 } 862 }
863 863
864 int MockTCPClientSocket::CompleteRead() { 864 int MockTCPClientSocket::CompleteRead() {
865 DCHECK(pending_buf_); 865 DCHECK(pending_buf_);
866 DCHECK(pending_buf_len_ > 0); 866 DCHECK(pending_buf_len_ > 0);
867 867
(...skipping 18 matching lines...) Expand all
886 num_bytes_read_ += result; 886 num_bytes_read_ += result;
887 if (read_offset_ == read_data_.data_len) { 887 if (read_offset_ == read_data_.data_len) {
888 need_read_data_ = true; 888 need_read_data_ = true;
889 read_offset_ = 0; 889 read_offset_ = 0;
890 } 890 }
891 } else { 891 } else {
892 result = 0; // EOF 892 result = 0; // EOF
893 } 893 }
894 } 894 }
895 895
896 if (read_data_.async) { 896 if (read_data_.mode == ASYNC) {
897 DCHECK(!callback.is_null()); 897 DCHECK(!callback.is_null());
898 RunCallbackAsync(callback, result); 898 RunCallbackAsync(callback, result);
899 return ERR_IO_PENDING; 899 return ERR_IO_PENDING;
900 } 900 }
901 return result; 901 return result;
902 } 902 }
903 903
904 DeterministicMockTCPClientSocket::DeterministicMockTCPClientSocket( 904 DeterministicMockTCPClientSocket::DeterministicMockTCPClientSocket(
905 net::NetLog* net_log, DeterministicSocketData* data) 905 net::NetLog* net_log, DeterministicSocketData* data)
906 : MockClientSocket(net_log), 906 : MockClientSocket(net_log),
(...skipping 17 matching lines...) Expand all
924 int DeterministicMockTCPClientSocket::CompleteRead() { 924 int DeterministicMockTCPClientSocket::CompleteRead() {
925 DCHECK_GT(read_buf_len_, 0); 925 DCHECK_GT(read_buf_len_, 0);
926 DCHECK_LE(read_data_.data_len, read_buf_len_); 926 DCHECK_LE(read_data_.data_len, read_buf_len_);
927 DCHECK(read_buf_); 927 DCHECK(read_buf_);
928 928
929 was_used_to_convey_data_ = true; 929 was_used_to_convey_data_ = true;
930 930
931 if (read_data_.result == ERR_IO_PENDING) 931 if (read_data_.result == ERR_IO_PENDING)
932 read_data_ = data_->GetNextRead(); 932 read_data_ = data_->GetNextRead();
933 DCHECK_NE(ERR_IO_PENDING, read_data_.result); 933 DCHECK_NE(ERR_IO_PENDING, read_data_.result);
934 // If read_data_.async is true, we do not need to wait, since this is already 934 // If read_data_.mode is ASYNC, we do not need to wait, since this is already
935 // the callback. Therefore we don't even bother to check it. 935 // the callback. Therefore we don't even bother to check it.
936 int result = read_data_.result; 936 int result = read_data_.result;
937 937
938 if (read_data_.data_len > 0) { 938 if (read_data_.data_len > 0) {
939 DCHECK(read_data_.data); 939 DCHECK(read_data_.data);
940 result = std::min(read_buf_len_, read_data_.data_len); 940 result = std::min(read_buf_len_, read_data_.data_len);
941 memcpy(read_buf_->data(), read_data_.data, result); 941 memcpy(read_buf_->data(), read_data_.data, result);
942 } 942 }
943 943
944 if (read_pending_) { 944 if (read_pending_) {
945 read_pending_ = false; 945 read_pending_ = false;
946 read_callback_.Run(result); 946 read_callback_.Run(result);
947 } 947 }
948 948
949 return result; 949 return result;
950 } 950 }
951 951
952 int DeterministicMockTCPClientSocket::Write( 952 int DeterministicMockTCPClientSocket::Write(
953 IOBuffer* buf, int buf_len, const CompletionCallback& callback) { 953 IOBuffer* buf, int buf_len, const CompletionCallback& callback) {
954 DCHECK(buf); 954 DCHECK(buf);
955 DCHECK_GT(buf_len, 0); 955 DCHECK_GT(buf_len, 0);
956 956
957 if (!connected_) 957 if (!connected_)
958 return ERR_UNEXPECTED; 958 return ERR_UNEXPECTED;
959 959
960 std::string data(buf->data(), buf_len); 960 std::string data(buf->data(), buf_len);
961 MockWriteResult write_result = data_->OnWrite(data); 961 MockWriteResult write_result = data_->OnWrite(data);
962 962
963 if (write_result.async) { 963 if (write_result.mode == ASYNC) {
964 write_callback_ = callback; 964 write_callback_ = callback;
965 write_result_ = write_result.result; 965 write_result_ = write_result.result;
966 DCHECK(!write_callback_.is_null()); 966 DCHECK(!write_callback_.is_null());
967 write_pending_ = true; 967 write_pending_ = true;
968 return ERR_IO_PENDING; 968 return ERR_IO_PENDING;
969 } 969 }
970 970
971 was_used_to_convey_data_ = true; 971 was_used_to_convey_data_ = true;
972 write_pending_ = false; 972 write_pending_ = false;
973 return write_result.result; 973 return write_result.result;
974 } 974 }
975 975
976 int DeterministicMockTCPClientSocket::Read( 976 int DeterministicMockTCPClientSocket::Read(
977 IOBuffer* buf, int buf_len, const CompletionCallback& callback) { 977 IOBuffer* buf, int buf_len, const CompletionCallback& callback) {
978 if (!connected_) 978 if (!connected_)
979 return ERR_UNEXPECTED; 979 return ERR_UNEXPECTED;
980 980
981 read_data_ = data_->GetNextRead(); 981 read_data_ = data_->GetNextRead();
982 // The buffer should always be big enough to contain all the MockRead data. To 982 // The buffer should always be big enough to contain all the MockRead data. To
983 // use small buffers, split the data into multiple MockReads. 983 // use small buffers, split the data into multiple MockReads.
984 DCHECK_LE(read_data_.data_len, buf_len); 984 DCHECK_LE(read_data_.data_len, buf_len);
985 985
986 read_buf_ = buf; 986 read_buf_ = buf;
987 read_buf_len_ = buf_len; 987 read_buf_len_ = buf_len;
988 read_callback_ = callback; 988 read_callback_ = callback;
989 989
990 if (read_data_.async || (read_data_.result == ERR_IO_PENDING)) { 990 if (read_data_.mode == ASYNC || (read_data_.result == ERR_IO_PENDING)) {
991 read_pending_ = true; 991 read_pending_ = true;
992 DCHECK(!read_callback_.is_null()); 992 DCHECK(!read_callback_.is_null());
993 return ERR_IO_PENDING; 993 return ERR_IO_PENDING;
994 } 994 }
995 995
996 was_used_to_convey_data_ = true; 996 was_used_to_convey_data_ = true;
997 return CompleteRead(); 997 return CompleteRead();
998 } 998 }
999 999
1000 // TODO(erikchen): Support connect sequencing. 1000 // TODO(erikchen): Support connect sequencing.
1001 int DeterministicMockTCPClientSocket::Connect( 1001 int DeterministicMockTCPClientSocket::Connect(
1002 const CompletionCallback& callback) { 1002 const CompletionCallback& callback) {
1003 if (connected_) 1003 if (connected_)
1004 return OK; 1004 return OK;
1005 connected_ = true; 1005 connected_ = true;
1006 if (data_->connect_data().async) { 1006 if (data_->connect_data().mode == ASYNC) {
1007 RunCallbackAsync(callback, data_->connect_data().result); 1007 RunCallbackAsync(callback, data_->connect_data().result);
1008 return ERR_IO_PENDING; 1008 return ERR_IO_PENDING;
1009 } 1009 }
1010 return data_->connect_data().result; 1010 return data_->connect_data().result;
1011 } 1011 }
1012 1012
1013 void DeterministicMockTCPClientSocket::Disconnect() { 1013 void DeterministicMockTCPClientSocket::Disconnect() {
1014 MockClientSocket::Disconnect(); 1014 MockClientSocket::Disconnect();
1015 } 1015 }
1016 1016
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after
1080 const CompletionCallback& callback) { 1080 const CompletionCallback& callback) {
1081 return transport_->socket()->Write(buf, buf_len, callback); 1081 return transport_->socket()->Write(buf, buf_len, callback);
1082 } 1082 }
1083 1083
1084 int MockSSLClientSocket::Connect(const CompletionCallback& callback) { 1084 int MockSSLClientSocket::Connect(const CompletionCallback& callback) {
1085 int rv = transport_->socket()->Connect( 1085 int rv = transport_->socket()->Connect(
1086 base::Bind(&ConnectCallback, base::Unretained(this), callback)); 1086 base::Bind(&ConnectCallback, base::Unretained(this), callback));
1087 if (rv == OK) { 1087 if (rv == OK) {
1088 if (data_->connect.result == OK) 1088 if (data_->connect.result == OK)
1089 connected_ = true; 1089 connected_ = true;
1090 if (data_->connect.async) { 1090 if (data_->connect.mode == ASYNC) {
1091 RunCallbackAsync(callback, data_->connect.result); 1091 RunCallbackAsync(callback, data_->connect.result);
1092 return ERR_IO_PENDING; 1092 return ERR_IO_PENDING;
1093 } 1093 }
1094 return data_->connect.result; 1094 return data_->connect.result;
1095 } 1095 }
1096 return rv; 1096 return rv;
1097 } 1097 }
1098 1098
1099 void MockSSLClientSocket::Disconnect() { 1099 void MockSSLClientSocket::Disconnect() {
1100 MockClientSocket::Disconnect(); 1100 MockClientSocket::Disconnect();
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after
1189 1189
1190 void MockSSLClientSocket::OnReadComplete(const MockRead& data) { 1190 void MockSSLClientSocket::OnReadComplete(const MockRead& data) {
1191 NOTIMPLEMENTED(); 1191 NOTIMPLEMENTED();
1192 } 1192 }
1193 1193
1194 MockUDPClientSocket::MockUDPClientSocket(SocketDataProvider* data, 1194 MockUDPClientSocket::MockUDPClientSocket(SocketDataProvider* data,
1195 net::NetLog* net_log) 1195 net::NetLog* net_log)
1196 : connected_(false), 1196 : connected_(false),
1197 data_(data), 1197 data_(data),
1198 read_offset_(0), 1198 read_offset_(0),
1199 read_data_(false, ERR_UNEXPECTED), 1199 read_data_(SYNCHRONOUS, ERR_UNEXPECTED),
1200 need_read_data_(true), 1200 need_read_data_(true),
1201 pending_buf_(NULL), 1201 pending_buf_(NULL),
1202 pending_buf_len_(0), 1202 pending_buf_len_(0),
1203 net_log_(net::NetLog::Source(), net_log), 1203 net_log_(net::NetLog::Source(), net_log),
1204 ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)) { 1204 ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)) {
1205 DCHECK(data_); 1205 DCHECK(data_);
1206 data_->Reset(); 1206 data_->Reset();
1207 } 1207 }
1208 1208
1209 MockUDPClientSocket::~MockUDPClientSocket() {} 1209 MockUDPClientSocket::~MockUDPClientSocket() {}
(...skipping 30 matching lines...) Expand all
1240 const CompletionCallback& callback) { 1240 const CompletionCallback& callback) {
1241 DCHECK(buf); 1241 DCHECK(buf);
1242 DCHECK_GT(buf_len, 0); 1242 DCHECK_GT(buf_len, 0);
1243 1243
1244 if (!connected_) 1244 if (!connected_)
1245 return ERR_UNEXPECTED; 1245 return ERR_UNEXPECTED;
1246 1246
1247 std::string data(buf->data(), buf_len); 1247 std::string data(buf->data(), buf_len);
1248 MockWriteResult write_result = data_->OnWrite(data); 1248 MockWriteResult write_result = data_->OnWrite(data);
1249 1249
1250 if (write_result.async) { 1250 if (write_result.mode == ASYNC) {
1251 RunCallbackAsync(callback, write_result.result); 1251 RunCallbackAsync(callback, write_result.result);
1252 return ERR_IO_PENDING; 1252 return ERR_IO_PENDING;
1253 } 1253 }
1254 return write_result.result; 1254 return write_result.result;
1255 } 1255 }
1256 1256
1257 bool MockUDPClientSocket::SetReceiveBufferSize(int32 size) { 1257 bool MockUDPClientSocket::SetReceiveBufferSize(int32 size) {
1258 return true; 1258 return true;
1259 } 1259 }
1260 1260
(...skipping 30 matching lines...) Expand all
1291 // You can't complete a read with another ERR_IO_PENDING status code. 1291 // You can't complete a read with another ERR_IO_PENDING status code.
1292 DCHECK_NE(ERR_IO_PENDING, data.result); 1292 DCHECK_NE(ERR_IO_PENDING, data.result);
1293 // Since we've been waiting for data, need_read_data_ should be true. 1293 // Since we've been waiting for data, need_read_data_ should be true.
1294 DCHECK(need_read_data_); 1294 DCHECK(need_read_data_);
1295 1295
1296 read_data_ = data; 1296 read_data_ = data;
1297 need_read_data_ = false; 1297 need_read_data_ = false;
1298 1298
1299 // The caller is simulating that this IO completes right now. Don't 1299 // The caller is simulating that this IO completes right now. Don't
1300 // let CompleteRead() schedule a callback. 1300 // let CompleteRead() schedule a callback.
1301 read_data_.async = false; 1301 read_data_.mode = SYNCHRONOUS;
1302 1302
1303 net::CompletionCallback callback = pending_callback_; 1303 net::CompletionCallback callback = pending_callback_;
1304 int rv = CompleteRead(); 1304 int rv = CompleteRead();
1305 RunCallback(callback, rv); 1305 RunCallback(callback, rv);
1306 } 1306 }
1307 1307
1308 int MockUDPClientSocket::CompleteRead() { 1308 int MockUDPClientSocket::CompleteRead() {
1309 DCHECK(pending_buf_); 1309 DCHECK(pending_buf_);
1310 DCHECK(pending_buf_len_ > 0); 1310 DCHECK(pending_buf_len_ > 0);
1311 1311
(...skipping 15 matching lines...) Expand all
1327 read_offset_ += result; 1327 read_offset_ += result;
1328 if (read_offset_ == read_data_.data_len) { 1328 if (read_offset_ == read_data_.data_len) {
1329 need_read_data_ = true; 1329 need_read_data_ = true;
1330 read_offset_ = 0; 1330 read_offset_ = 0;
1331 } 1331 }
1332 } else { 1332 } else {
1333 result = 0; // EOF 1333 result = 0; // EOF
1334 } 1334 }
1335 } 1335 }
1336 1336
1337 if (read_data_.async) { 1337 if (read_data_.mode == ASYNC) {
1338 DCHECK(!callback.is_null()); 1338 DCHECK(!callback.is_null());
1339 RunCallbackAsync(callback, result); 1339 RunCallbackAsync(callback, result);
1340 return ERR_IO_PENDING; 1340 return ERR_IO_PENDING;
1341 } 1341 }
1342 return result; 1342 return result;
1343 } 1343 }
1344 1344
1345 void MockUDPClientSocket::RunCallbackAsync(const CompletionCallback& callback, 1345 void MockUDPClientSocket::RunCallbackAsync(const CompletionCallback& callback,
1346 int result) { 1346 int result) {
1347 MessageLoop::current()->PostTask( 1347 MessageLoop::current()->PostTask(
(...skipping 264 matching lines...) Expand 10 before | Expand all | Expand 10 after
1612 1612
1613 const char kSOCKS5OkRequest[] = 1613 const char kSOCKS5OkRequest[] =
1614 { 0x05, 0x01, 0x00, 0x03, 0x04, 'h', 'o', 's', 't', 0x00, 0x50 }; 1614 { 0x05, 0x01, 0x00, 0x03, 0x04, 'h', 'o', 's', 't', 0x00, 0x50 };
1615 const int kSOCKS5OkRequestLength = arraysize(kSOCKS5OkRequest); 1615 const int kSOCKS5OkRequestLength = arraysize(kSOCKS5OkRequest);
1616 1616
1617 const char kSOCKS5OkResponse[] = 1617 const char kSOCKS5OkResponse[] =
1618 { 0x05, 0x00, 0x00, 0x01, 127, 0, 0, 1, 0x00, 0x50 }; 1618 { 0x05, 0x00, 0x00, 0x01, 127, 0, 0, 1, 0x00, 0x50 };
1619 const int kSOCKS5OkResponseLength = arraysize(kSOCKS5OkResponse); 1619 const int kSOCKS5OkResponseLength = arraysize(kSOCKS5OkResponse);
1620 1620
1621 } // namespace net 1621 } // namespace net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698