| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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 | 10 |
| (...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 109 DVLOG(1) << "Async: " << r.async | 109 DVLOG(1) << "Async: " << r.async |
| 110 << "\nResult: " << r.result; | 110 << "\nResult: " << r.result; |
| 111 DumpData(r.data, r.data_len); | 111 DumpData(r.data, r.data_len); |
| 112 const char* stop = (r.sequence_number & MockRead::STOPLOOP) ? " (STOP)" : ""; | 112 const char* stop = (r.sequence_number & MockRead::STOPLOOP) ? " (STOP)" : ""; |
| 113 DVLOG(1) << "Stage: " << (r.sequence_number & ~MockRead::STOPLOOP) << stop | 113 DVLOG(1) << "Stage: " << (r.sequence_number & ~MockRead::STOPLOOP) << stop |
| 114 << "\nTime: " << r.time_stamp.ToInternalValue(); | 114 << "\nTime: " << r.time_stamp.ToInternalValue(); |
| 115 } | 115 } |
| 116 | 116 |
| 117 } // namespace | 117 } // namespace |
| 118 | 118 |
| 119 MockClientSocket::MockClientSocket(net::NetLog* net_log) | |
| 120 : ALLOW_THIS_IN_INITIALIZER_LIST(method_factory_(this)), | |
| 121 connected_(false), | |
| 122 net_log_(NetLog::Source(), net_log) { | |
| 123 } | |
| 124 | |
| 125 void MockClientSocket::GetSSLInfo(net::SSLInfo* ssl_info) { | |
| 126 NOTREACHED(); | |
| 127 } | |
| 128 | |
| 129 void MockClientSocket::GetSSLCertRequestInfo( | |
| 130 net::SSLCertRequestInfo* cert_request_info) { | |
| 131 } | |
| 132 | |
| 133 SSLClientSocket::NextProtoStatus | |
| 134 MockClientSocket::GetNextProto(std::string* proto) { | |
| 135 proto->clear(); | |
| 136 return SSLClientSocket::kNextProtoUnsupported; | |
| 137 } | |
| 138 | |
| 139 void MockClientSocket::Disconnect() { | |
| 140 connected_ = false; | |
| 141 } | |
| 142 | |
| 143 bool MockClientSocket::IsConnected() const { | |
| 144 return connected_; | |
| 145 } | |
| 146 | |
| 147 bool MockClientSocket::IsConnectedAndIdle() const { | |
| 148 return connected_; | |
| 149 } | |
| 150 | |
| 151 int MockClientSocket::GetPeerAddress(AddressList* address) const { | |
| 152 return net::SystemHostResolverProc("localhost", ADDRESS_FAMILY_UNSPECIFIED, | |
| 153 0, address, NULL); | |
| 154 } | |
| 155 | |
| 156 void MockClientSocket::RunCallbackAsync(net::CompletionCallback* callback, | |
| 157 int result) { | |
| 158 MessageLoop::current()->PostTask(FROM_HERE, | |
| 159 method_factory_.NewRunnableMethod( | |
| 160 &MockClientSocket::RunCallback, callback, result)); | |
| 161 } | |
| 162 | |
| 163 void MockClientSocket::RunCallback(net::CompletionCallback* callback, | |
| 164 int result) { | |
| 165 if (callback) | |
| 166 callback->Run(result); | |
| 167 } | |
| 168 | |
| 169 MockTCPClientSocket::MockTCPClientSocket(const net::AddressList& addresses, | |
| 170 net::NetLog* net_log, | |
| 171 net::SocketDataProvider* data) | |
| 172 : MockClientSocket(net_log), | |
| 173 addresses_(addresses), | |
| 174 data_(data), | |
| 175 read_offset_(0), | |
| 176 read_data_(false, net::ERR_UNEXPECTED), | |
| 177 need_read_data_(true), | |
| 178 peer_closed_connection_(false), | |
| 179 pending_buf_(NULL), | |
| 180 pending_buf_len_(0), | |
| 181 pending_callback_(NULL), | |
| 182 was_used_to_convey_data_(false) { | |
| 183 DCHECK(data_); | |
| 184 data_->Reset(); | |
| 185 } | |
| 186 | |
| 187 int MockTCPClientSocket::Connect(net::CompletionCallback* callback) { | |
| 188 if (connected_) | |
| 189 return net::OK; | |
| 190 connected_ = true; | |
| 191 peer_closed_connection_ = false; | |
| 192 if (data_->connect_data().async) { | |
| 193 RunCallbackAsync(callback, data_->connect_data().result); | |
| 194 return net::ERR_IO_PENDING; | |
| 195 } | |
| 196 return data_->connect_data().result; | |
| 197 } | |
| 198 | |
| 199 void MockTCPClientSocket::Disconnect() { | |
| 200 MockClientSocket::Disconnect(); | |
| 201 pending_callback_ = NULL; | |
| 202 } | |
| 203 | |
| 204 bool MockTCPClientSocket::IsConnected() const { | |
| 205 return connected_ && !peer_closed_connection_; | |
| 206 } | |
| 207 | |
| 208 int MockTCPClientSocket::Read(net::IOBuffer* buf, int buf_len, | |
| 209 net::CompletionCallback* callback) { | |
| 210 if (!connected_) | |
| 211 return net::ERR_UNEXPECTED; | |
| 212 | |
| 213 // If the buffer is already in use, a read is already in progress! | |
| 214 DCHECK(pending_buf_ == NULL); | |
| 215 | |
| 216 // Store our async IO data. | |
| 217 pending_buf_ = buf; | |
| 218 pending_buf_len_ = buf_len; | |
| 219 pending_callback_ = callback; | |
| 220 | |
| 221 if (need_read_data_) { | |
| 222 read_data_ = data_->GetNextRead(); | |
| 223 if (read_data_.result == ERR_TEST_PEER_CLOSE_AFTER_NEXT_MOCK_READ) { | |
| 224 // This MockRead is just a marker to instruct us to set | |
| 225 // peer_closed_connection_. Skip it and get the next one. | |
| 226 read_data_ = data_->GetNextRead(); | |
| 227 peer_closed_connection_ = true; | |
| 228 } | |
| 229 // ERR_IO_PENDING means that the SocketDataProvider is taking responsibility | |
| 230 // to complete the async IO manually later (via OnReadComplete). | |
| 231 if (read_data_.result == ERR_IO_PENDING) { | |
| 232 DCHECK(callback); // We need to be using async IO in this case. | |
| 233 return ERR_IO_PENDING; | |
| 234 } | |
| 235 need_read_data_ = false; | |
| 236 } | |
| 237 | |
| 238 return CompleteRead(); | |
| 239 } | |
| 240 | |
| 241 int MockTCPClientSocket::Write(net::IOBuffer* buf, int buf_len, | |
| 242 net::CompletionCallback* callback) { | |
| 243 DCHECK(buf); | |
| 244 DCHECK_GT(buf_len, 0); | |
| 245 | |
| 246 if (!connected_) | |
| 247 return net::ERR_UNEXPECTED; | |
| 248 | |
| 249 std::string data(buf->data(), buf_len); | |
| 250 net::MockWriteResult write_result = data_->OnWrite(data); | |
| 251 | |
| 252 was_used_to_convey_data_ = true; | |
| 253 | |
| 254 if (write_result.async) { | |
| 255 RunCallbackAsync(callback, write_result.result); | |
| 256 return net::ERR_IO_PENDING; | |
| 257 } | |
| 258 | |
| 259 return write_result.result; | |
| 260 } | |
| 261 | |
| 262 void MockTCPClientSocket::OnReadComplete(const MockRead& data) { | |
| 263 // There must be a read pending. | |
| 264 DCHECK(pending_buf_); | |
| 265 // You can't complete a read with another ERR_IO_PENDING status code. | |
| 266 DCHECK_NE(ERR_IO_PENDING, data.result); | |
| 267 // Since we've been waiting for data, need_read_data_ should be true. | |
| 268 DCHECK(need_read_data_); | |
| 269 | |
| 270 read_data_ = data; | |
| 271 need_read_data_ = false; | |
| 272 | |
| 273 // The caller is simulating that this IO completes right now. Don't | |
| 274 // let CompleteRead() schedule a callback. | |
| 275 read_data_.async = false; | |
| 276 | |
| 277 net::CompletionCallback* callback = pending_callback_; | |
| 278 int rv = CompleteRead(); | |
| 279 RunCallback(callback, rv); | |
| 280 } | |
| 281 | |
| 282 int MockTCPClientSocket::CompleteRead() { | |
| 283 DCHECK(pending_buf_); | |
| 284 DCHECK(pending_buf_len_ > 0); | |
| 285 | |
| 286 was_used_to_convey_data_ = true; | |
| 287 | |
| 288 // Save the pending async IO data and reset our |pending_| state. | |
| 289 net::IOBuffer* buf = pending_buf_; | |
| 290 int buf_len = pending_buf_len_; | |
| 291 net::CompletionCallback* callback = pending_callback_; | |
| 292 pending_buf_ = NULL; | |
| 293 pending_buf_len_ = 0; | |
| 294 pending_callback_ = NULL; | |
| 295 | |
| 296 int result = read_data_.result; | |
| 297 DCHECK(result != ERR_IO_PENDING); | |
| 298 | |
| 299 if (read_data_.data) { | |
| 300 if (read_data_.data_len - read_offset_ > 0) { | |
| 301 result = std::min(buf_len, read_data_.data_len - read_offset_); | |
| 302 memcpy(buf->data(), read_data_.data + read_offset_, result); | |
| 303 read_offset_ += result; | |
| 304 if (read_offset_ == read_data_.data_len) { | |
| 305 need_read_data_ = true; | |
| 306 read_offset_ = 0; | |
| 307 } | |
| 308 } else { | |
| 309 result = 0; // EOF | |
| 310 } | |
| 311 } | |
| 312 | |
| 313 if (read_data_.async) { | |
| 314 DCHECK(callback); | |
| 315 RunCallbackAsync(callback, result); | |
| 316 return net::ERR_IO_PENDING; | |
| 317 } | |
| 318 return result; | |
| 319 } | |
| 320 | |
| 321 DeterministicMockTCPClientSocket::DeterministicMockTCPClientSocket( | |
| 322 net::NetLog* net_log, net::DeterministicSocketData* data) | |
| 323 : MockClientSocket(net_log), | |
| 324 write_pending_(false), | |
| 325 write_callback_(NULL), | |
| 326 write_result_(0), | |
| 327 read_data_(), | |
| 328 read_buf_(NULL), | |
| 329 read_buf_len_(0), | |
| 330 read_pending_(false), | |
| 331 read_callback_(NULL), | |
| 332 data_(data), | |
| 333 was_used_to_convey_data_(false) {} | |
| 334 | |
| 335 void DeterministicMockTCPClientSocket::OnReadComplete(const MockRead& data) {} | |
| 336 | |
| 337 // TODO(erikchen): Support connect sequencing. | |
| 338 int DeterministicMockTCPClientSocket::Connect( | |
| 339 net::CompletionCallback* callback) { | |
| 340 if (connected_) | |
| 341 return net::OK; | |
| 342 connected_ = true; | |
| 343 if (data_->connect_data().async) { | |
| 344 RunCallbackAsync(callback, data_->connect_data().result); | |
| 345 return net::ERR_IO_PENDING; | |
| 346 } | |
| 347 return data_->connect_data().result; | |
| 348 } | |
| 349 | |
| 350 void DeterministicMockTCPClientSocket::Disconnect() { | |
| 351 MockClientSocket::Disconnect(); | |
| 352 } | |
| 353 | |
| 354 bool DeterministicMockTCPClientSocket::IsConnected() const { | |
| 355 return connected_; | |
| 356 } | |
| 357 | |
| 358 int DeterministicMockTCPClientSocket::Write( | |
| 359 net::IOBuffer* buf, int buf_len, net::CompletionCallback* callback) { | |
| 360 DCHECK(buf); | |
| 361 DCHECK_GT(buf_len, 0); | |
| 362 | |
| 363 if (!connected_) | |
| 364 return net::ERR_UNEXPECTED; | |
| 365 | |
| 366 std::string data(buf->data(), buf_len); | |
| 367 net::MockWriteResult write_result = data_->OnWrite(data); | |
| 368 | |
| 369 if (write_result.async) { | |
| 370 write_callback_ = callback; | |
| 371 write_result_ = write_result.result; | |
| 372 DCHECK(write_callback_ != NULL); | |
| 373 write_pending_ = true; | |
| 374 return net::ERR_IO_PENDING; | |
| 375 } | |
| 376 | |
| 377 was_used_to_convey_data_ = true; | |
| 378 write_pending_ = false; | |
| 379 return write_result.result; | |
| 380 } | |
| 381 | |
| 382 int DeterministicMockTCPClientSocket::Read( | |
| 383 net::IOBuffer* buf, int buf_len, net::CompletionCallback* callback) { | |
| 384 if (!connected_) | |
| 385 return net::ERR_UNEXPECTED; | |
| 386 | |
| 387 read_data_ = data_->GetNextRead(); | |
| 388 // The buffer should always be big enough to contain all the MockRead data. To | |
| 389 // use small buffers, split the data into multiple MockReads. | |
| 390 DCHECK_LE(read_data_.data_len, buf_len); | |
| 391 | |
| 392 read_buf_ = buf; | |
| 393 read_buf_len_ = buf_len; | |
| 394 read_callback_ = callback; | |
| 395 | |
| 396 if (read_data_.async || (read_data_.result == ERR_IO_PENDING)) { | |
| 397 read_pending_ = true; | |
| 398 DCHECK(read_callback_); | |
| 399 return ERR_IO_PENDING; | |
| 400 } | |
| 401 | |
| 402 was_used_to_convey_data_ = true; | |
| 403 return CompleteRead(); | |
| 404 } | |
| 405 | |
| 406 void DeterministicMockTCPClientSocket::CompleteWrite() { | |
| 407 was_used_to_convey_data_ = true; | |
| 408 write_pending_ = false; | |
| 409 write_callback_->Run(write_result_); | |
| 410 } | |
| 411 | |
| 412 int DeterministicMockTCPClientSocket::CompleteRead() { | |
| 413 DCHECK_GT(read_buf_len_, 0); | |
| 414 DCHECK_LE(read_data_.data_len, read_buf_len_); | |
| 415 DCHECK(read_buf_); | |
| 416 | |
| 417 was_used_to_convey_data_ = true; | |
| 418 | |
| 419 if (read_data_.result == ERR_IO_PENDING) | |
| 420 read_data_ = data_->GetNextRead(); | |
| 421 DCHECK_NE(ERR_IO_PENDING, read_data_.result); | |
| 422 // If read_data_.async is true, we do not need to wait, since this is already | |
| 423 // the callback. Therefore we don't even bother to check it. | |
| 424 int result = read_data_.result; | |
| 425 | |
| 426 if (read_data_.data_len > 0) { | |
| 427 DCHECK(read_data_.data); | |
| 428 result = std::min(read_buf_len_, read_data_.data_len); | |
| 429 memcpy(read_buf_->data(), read_data_.data, result); | |
| 430 } | |
| 431 | |
| 432 if (read_pending_) { | |
| 433 read_pending_ = false; | |
| 434 read_callback_->Run(result); | |
| 435 } | |
| 436 | |
| 437 return result; | |
| 438 } | |
| 439 | |
| 440 class MockSSLClientSocket::ConnectCallback | |
| 441 : public net::CompletionCallbackImpl<MockSSLClientSocket::ConnectCallback> { | |
| 442 public: | |
| 443 ConnectCallback(MockSSLClientSocket *ssl_client_socket, | |
| 444 net::CompletionCallback* user_callback, | |
| 445 int rv) | |
| 446 : ALLOW_THIS_IN_INITIALIZER_LIST( | |
| 447 net::CompletionCallbackImpl<MockSSLClientSocket::ConnectCallback>( | |
| 448 this, &ConnectCallback::Wrapper)), | |
| 449 ssl_client_socket_(ssl_client_socket), | |
| 450 user_callback_(user_callback), | |
| 451 rv_(rv) { | |
| 452 } | |
| 453 | |
| 454 private: | |
| 455 void Wrapper(int rv) { | |
| 456 if (rv_ == net::OK) | |
| 457 ssl_client_socket_->connected_ = true; | |
| 458 user_callback_->Run(rv_); | |
| 459 delete this; | |
| 460 } | |
| 461 | |
| 462 MockSSLClientSocket* ssl_client_socket_; | |
| 463 net::CompletionCallback* user_callback_; | |
| 464 int rv_; | |
| 465 }; | |
| 466 | |
| 467 MockSSLClientSocket::MockSSLClientSocket( | |
| 468 net::ClientSocketHandle* transport_socket, | |
| 469 const HostPortPair& host_port_pair, | |
| 470 const net::SSLConfig& ssl_config, | |
| 471 SSLHostInfo* ssl_host_info, | |
| 472 net::SSLSocketDataProvider* data) | |
| 473 : MockClientSocket(transport_socket->socket()->NetLog().net_log()), | |
| 474 transport_(transport_socket), | |
| 475 data_(data), | |
| 476 is_npn_state_set_(false), | |
| 477 new_npn_value_(false) { | |
| 478 DCHECK(data_); | |
| 479 delete ssl_host_info; // we take ownership but don't use it. | |
| 480 } | |
| 481 | |
| 482 MockSSLClientSocket::~MockSSLClientSocket() { | |
| 483 Disconnect(); | |
| 484 } | |
| 485 | |
| 486 int MockSSLClientSocket::Connect(net::CompletionCallback* callback) { | |
| 487 ConnectCallback* connect_callback = new ConnectCallback( | |
| 488 this, callback, data_->connect.result); | |
| 489 int rv = transport_->socket()->Connect(connect_callback); | |
| 490 if (rv == net::OK) { | |
| 491 delete connect_callback; | |
| 492 if (data_->connect.result == net::OK) | |
| 493 connected_ = true; | |
| 494 if (data_->connect.async) { | |
| 495 RunCallbackAsync(callback, data_->connect.result); | |
| 496 return net::ERR_IO_PENDING; | |
| 497 } | |
| 498 return data_->connect.result; | |
| 499 } | |
| 500 return rv; | |
| 501 } | |
| 502 | |
| 503 void MockSSLClientSocket::Disconnect() { | |
| 504 MockClientSocket::Disconnect(); | |
| 505 if (transport_->socket() != NULL) | |
| 506 transport_->socket()->Disconnect(); | |
| 507 } | |
| 508 | |
| 509 bool MockSSLClientSocket::IsConnected() const { | |
| 510 return transport_->socket()->IsConnected(); | |
| 511 } | |
| 512 | |
| 513 bool MockSSLClientSocket::WasEverUsed() const { | |
| 514 return transport_->socket()->WasEverUsed(); | |
| 515 } | |
| 516 | |
| 517 bool MockSSLClientSocket::UsingTCPFastOpen() const { | |
| 518 return transport_->socket()->UsingTCPFastOpen(); | |
| 519 } | |
| 520 | |
| 521 int MockSSLClientSocket::Read(net::IOBuffer* buf, int buf_len, | |
| 522 net::CompletionCallback* callback) { | |
| 523 return transport_->socket()->Read(buf, buf_len, callback); | |
| 524 } | |
| 525 | |
| 526 int MockSSLClientSocket::Write(net::IOBuffer* buf, int buf_len, | |
| 527 net::CompletionCallback* callback) { | |
| 528 return transport_->socket()->Write(buf, buf_len, callback); | |
| 529 } | |
| 530 | |
| 531 void MockSSLClientSocket::GetSSLInfo(net::SSLInfo* ssl_info) { | |
| 532 ssl_info->Reset(); | |
| 533 } | |
| 534 | |
| 535 void MockSSLClientSocket::GetSSLCertRequestInfo( | |
| 536 net::SSLCertRequestInfo* cert_request_info) { | |
| 537 DCHECK(cert_request_info); | |
| 538 if (data_->cert_request_info) { | |
| 539 cert_request_info->host_and_port = | |
| 540 data_->cert_request_info->host_and_port; | |
| 541 cert_request_info->client_certs = data_->cert_request_info->client_certs; | |
| 542 } else { | |
| 543 cert_request_info->Reset(); | |
| 544 } | |
| 545 } | |
| 546 | |
| 547 SSLClientSocket::NextProtoStatus MockSSLClientSocket::GetNextProto( | |
| 548 std::string* proto) { | |
| 549 *proto = data_->next_proto; | |
| 550 return data_->next_proto_status; | |
| 551 } | |
| 552 | |
| 553 bool MockSSLClientSocket::was_npn_negotiated() const { | |
| 554 if (is_npn_state_set_) | |
| 555 return new_npn_value_; | |
| 556 return data_->was_npn_negotiated; | |
| 557 } | |
| 558 | |
| 559 bool MockSSLClientSocket::set_was_npn_negotiated(bool negotiated) { | |
| 560 is_npn_state_set_ = true; | |
| 561 return new_npn_value_ = negotiated; | |
| 562 } | |
| 563 | |
| 564 StaticSocketDataProvider::StaticSocketDataProvider() | 119 StaticSocketDataProvider::StaticSocketDataProvider() |
| 565 : reads_(NULL), | 120 : reads_(NULL), |
| 566 read_index_(0), | 121 read_index_(0), |
| 567 read_count_(0), | 122 read_count_(0), |
| 568 writes_(NULL), | 123 writes_(NULL), |
| 569 write_index_(0), | 124 write_index_(0), |
| 570 write_count_(0) { | 125 write_count_(0) { |
| 571 } | 126 } |
| 572 | 127 |
| 573 StaticSocketDataProvider::StaticSocketDataProvider(MockRead* reads, | 128 StaticSocketDataProvider::StaticSocketDataProvider(MockRead* reads, |
| 574 size_t reads_count, | 129 size_t reads_count, |
| 575 MockWrite* writes, | 130 MockWrite* writes, |
| 576 size_t writes_count) | 131 size_t writes_count) |
| 577 : reads_(reads), | 132 : reads_(reads), |
| 578 read_index_(0), | 133 read_index_(0), |
| 579 read_count_(reads_count), | 134 read_count_(reads_count), |
| 580 writes_(writes), | 135 writes_(writes), |
| 581 write_index_(0), | 136 write_index_(0), |
| 582 write_count_(writes_count) { | 137 write_count_(writes_count) { |
| 583 } | 138 } |
| 584 | 139 |
| 585 StaticSocketDataProvider::~StaticSocketDataProvider() {} | 140 StaticSocketDataProvider::~StaticSocketDataProvider() {} |
| 586 | 141 |
| 142 const MockRead& StaticSocketDataProvider::PeekRead() const { |
| 143 DCHECK(!at_read_eof()); |
| 144 return reads_[read_index_]; |
| 145 } |
| 146 |
| 147 const MockWrite& StaticSocketDataProvider::PeekWrite() const { |
| 148 DCHECK(!at_write_eof()); |
| 149 return writes_[write_index_]; |
| 150 } |
| 151 |
| 152 const MockRead& StaticSocketDataProvider::PeekRead(size_t index) const { |
| 153 DCHECK_LT(index, read_count_); |
| 154 return reads_[index]; |
| 155 } |
| 156 |
| 157 const MockWrite& StaticSocketDataProvider::PeekWrite(size_t index) const { |
| 158 DCHECK_LT(index, write_count_); |
| 159 return writes_[index]; |
| 160 } |
| 161 |
| 587 MockRead StaticSocketDataProvider::GetNextRead() { | 162 MockRead StaticSocketDataProvider::GetNextRead() { |
| 588 DCHECK(!at_read_eof()); | 163 DCHECK(!at_read_eof()); |
| 589 reads_[read_index_].time_stamp = base::Time::Now(); | 164 reads_[read_index_].time_stamp = base::Time::Now(); |
| 590 return reads_[read_index_++]; | 165 return reads_[read_index_++]; |
| 591 } | 166 } |
| 592 | 167 |
| 593 MockWriteResult StaticSocketDataProvider::OnWrite(const std::string& data) { | 168 MockWriteResult StaticSocketDataProvider::OnWrite(const std::string& data) { |
| 594 if (!writes_) { | 169 if (!writes_) { |
| 595 // Not using mock writes; succeed synchronously. | 170 // Not using mock writes; succeed synchronously. |
| 596 return MockWriteResult(false, data.length()); | 171 return MockWriteResult(false, data.length()); |
| (...skipping 18 matching lines...) Expand all Loading... |
| 615 std::string actual_data(data.substr(0, w->data_len)); | 190 std::string actual_data(data.substr(0, w->data_len)); |
| 616 EXPECT_EQ(expected_data, actual_data); | 191 EXPECT_EQ(expected_data, actual_data); |
| 617 if (expected_data != actual_data) | 192 if (expected_data != actual_data) |
| 618 return MockWriteResult(false, net::ERR_UNEXPECTED); | 193 return MockWriteResult(false, net::ERR_UNEXPECTED); |
| 619 if (result == net::OK) | 194 if (result == net::OK) |
| 620 result = w->data_len; | 195 result = w->data_len; |
| 621 } | 196 } |
| 622 return MockWriteResult(w->async, result); | 197 return MockWriteResult(w->async, result); |
| 623 } | 198 } |
| 624 | 199 |
| 625 const MockRead& StaticSocketDataProvider::PeekRead() const { | |
| 626 DCHECK(!at_read_eof()); | |
| 627 return reads_[read_index_]; | |
| 628 } | |
| 629 | |
| 630 const MockWrite& StaticSocketDataProvider::PeekWrite() const { | |
| 631 DCHECK(!at_write_eof()); | |
| 632 return writes_[write_index_]; | |
| 633 } | |
| 634 | |
| 635 const MockRead& StaticSocketDataProvider::PeekRead(size_t index) const { | |
| 636 DCHECK_LT(index, read_count_); | |
| 637 return reads_[index]; | |
| 638 } | |
| 639 | |
| 640 const MockWrite& StaticSocketDataProvider::PeekWrite(size_t index) const { | |
| 641 DCHECK_LT(index, write_count_); | |
| 642 return writes_[index]; | |
| 643 } | |
| 644 | |
| 645 void StaticSocketDataProvider::Reset() { | 200 void StaticSocketDataProvider::Reset() { |
| 646 read_index_ = 0; | 201 read_index_ = 0; |
| 647 write_index_ = 0; | 202 write_index_ = 0; |
| 648 } | 203 } |
| 649 | 204 |
| 650 DynamicSocketDataProvider::DynamicSocketDataProvider() | 205 DynamicSocketDataProvider::DynamicSocketDataProvider() |
| 651 : short_read_limit_(0), | 206 : short_read_limit_(0), |
| 652 allow_unconsumed_reads_(false) { | 207 allow_unconsumed_reads_(false) { |
| 653 } | 208 } |
| 654 | 209 |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 695 : StaticSocketDataProvider(reads, reads_count, writes, writes_count), | 250 : StaticSocketDataProvider(reads, reads_count, writes, writes_count), |
| 696 write_delay_(write_delay), | 251 write_delay_(write_delay), |
| 697 ALLOW_THIS_IN_INITIALIZER_LIST(factory_(this)) { | 252 ALLOW_THIS_IN_INITIALIZER_LIST(factory_(this)) { |
| 698 DCHECK_GE(write_delay_, 0); | 253 DCHECK_GE(write_delay_, 0); |
| 699 set_connect_data(connect); | 254 set_connect_data(connect); |
| 700 } | 255 } |
| 701 | 256 |
| 702 DelayedSocketData::~DelayedSocketData() { | 257 DelayedSocketData::~DelayedSocketData() { |
| 703 } | 258 } |
| 704 | 259 |
| 260 void DelayedSocketData::ForceNextRead() { |
| 261 write_delay_ = 0; |
| 262 CompleteRead(); |
| 263 } |
| 264 |
| 705 MockRead DelayedSocketData::GetNextRead() { | 265 MockRead DelayedSocketData::GetNextRead() { |
| 706 if (write_delay_ > 0) | 266 if (write_delay_ > 0) |
| 707 return MockRead(true, ERR_IO_PENDING); | 267 return MockRead(true, ERR_IO_PENDING); |
| 708 return StaticSocketDataProvider::GetNextRead(); | 268 return StaticSocketDataProvider::GetNextRead(); |
| 709 } | 269 } |
| 710 | 270 |
| 711 MockWriteResult DelayedSocketData::OnWrite(const std::string& data) { | 271 MockWriteResult DelayedSocketData::OnWrite(const std::string& data) { |
| 712 MockWriteResult rv = StaticSocketDataProvider::OnWrite(data); | 272 MockWriteResult rv = StaticSocketDataProvider::OnWrite(data); |
| 713 // Now that our write has completed, we can allow reads to continue. | 273 // Now that our write has completed, we can allow reads to continue. |
| 714 if (!--write_delay_) | 274 if (!--write_delay_) |
| 715 MessageLoop::current()->PostDelayedTask(FROM_HERE, | 275 MessageLoop::current()->PostDelayedTask(FROM_HERE, |
| 716 factory_.NewRunnableMethod(&DelayedSocketData::CompleteRead), 100); | 276 factory_.NewRunnableMethod(&DelayedSocketData::CompleteRead), 100); |
| 717 return rv; | 277 return rv; |
| 718 } | 278 } |
| 719 | 279 |
| 720 void DelayedSocketData::Reset() { | 280 void DelayedSocketData::Reset() { |
| 721 set_socket(NULL); | 281 set_socket(NULL); |
| 722 factory_.RevokeAll(); | 282 factory_.RevokeAll(); |
| 723 StaticSocketDataProvider::Reset(); | 283 StaticSocketDataProvider::Reset(); |
| 724 } | 284 } |
| 725 | 285 |
| 726 void DelayedSocketData::CompleteRead() { | 286 void DelayedSocketData::CompleteRead() { |
| 727 if (socket()) | 287 if (socket()) |
| 728 socket()->OnReadComplete(GetNextRead()); | 288 socket()->OnReadComplete(GetNextRead()); |
| 729 } | 289 } |
| 730 | 290 |
| 731 void DelayedSocketData::ForceNextRead() { | |
| 732 write_delay_ = 0; | |
| 733 CompleteRead(); | |
| 734 } | |
| 735 | |
| 736 OrderedSocketData::OrderedSocketData( | 291 OrderedSocketData::OrderedSocketData( |
| 737 MockRead* reads, size_t reads_count, MockWrite* writes, size_t writes_count) | 292 MockRead* reads, size_t reads_count, MockWrite* writes, size_t writes_count) |
| 738 : StaticSocketDataProvider(reads, reads_count, writes, writes_count), | 293 : StaticSocketDataProvider(reads, reads_count, writes, writes_count), |
| 739 sequence_number_(0), loop_stop_stage_(0), callback_(NULL), | 294 sequence_number_(0), loop_stop_stage_(0), callback_(NULL), |
| 740 blocked_(false), ALLOW_THIS_IN_INITIALIZER_LIST(factory_(this)) { | 295 blocked_(false), ALLOW_THIS_IN_INITIALIZER_LIST(factory_(this)) { |
| 741 } | 296 } |
| 742 | 297 |
| 743 OrderedSocketData::OrderedSocketData( | 298 OrderedSocketData::OrderedSocketData( |
| 744 const MockConnect& connect, | 299 const MockConnect& connect, |
| 745 MockRead* reads, size_t reads_count, | 300 MockRead* reads, size_t reads_count, |
| 746 MockWrite* writes, size_t writes_count) | 301 MockWrite* writes, size_t writes_count) |
| 747 : StaticSocketDataProvider(reads, reads_count, writes, writes_count), | 302 : StaticSocketDataProvider(reads, reads_count, writes, writes_count), |
| 748 sequence_number_(0), loop_stop_stage_(0), callback_(NULL), | 303 sequence_number_(0), loop_stop_stage_(0), callback_(NULL), |
| 749 blocked_(false), ALLOW_THIS_IN_INITIALIZER_LIST(factory_(this)) { | 304 blocked_(false), ALLOW_THIS_IN_INITIALIZER_LIST(factory_(this)) { |
| 750 set_connect_data(connect); | 305 set_connect_data(connect); |
| 751 } | 306 } |
| 752 | 307 |
| 308 void OrderedSocketData::EndLoop() { |
| 309 // If we've already stopped the loop, don't do it again until we've advanced |
| 310 // to the next sequence_number. |
| 311 NET_TRACE(INFO, " *** ") << "Stage " << sequence_number_ << ": EndLoop()"; |
| 312 if (loop_stop_stage_ > 0) { |
| 313 const MockRead& next_read = StaticSocketDataProvider::PeekRead(); |
| 314 if ((next_read.sequence_number & ~MockRead::STOPLOOP) > |
| 315 loop_stop_stage_) { |
| 316 NET_TRACE(INFO, " *** ") << "Stage " << sequence_number_ |
| 317 << ": Clearing stop index"; |
| 318 loop_stop_stage_ = 0; |
| 319 } else { |
| 320 return; |
| 321 } |
| 322 } |
| 323 // Record the sequence_number at which we stopped the loop. |
| 324 NET_TRACE(INFO, " *** ") << "Stage " << sequence_number_ |
| 325 << ": Posting Quit at read " << read_index(); |
| 326 loop_stop_stage_ = sequence_number_; |
| 327 if (callback_) |
| 328 callback_->RunWithParams(Tuple1<int>(ERR_IO_PENDING)); |
| 329 } |
| 330 |
| 753 MockRead OrderedSocketData::GetNextRead() { | 331 MockRead OrderedSocketData::GetNextRead() { |
| 754 factory_.RevokeAll(); | 332 factory_.RevokeAll(); |
| 755 blocked_ = false; | 333 blocked_ = false; |
| 756 const MockRead& next_read = StaticSocketDataProvider::PeekRead(); | 334 const MockRead& next_read = StaticSocketDataProvider::PeekRead(); |
| 757 if (next_read.sequence_number & MockRead::STOPLOOP) | 335 if (next_read.sequence_number & MockRead::STOPLOOP) |
| 758 EndLoop(); | 336 EndLoop(); |
| 759 if ((next_read.sequence_number & ~MockRead::STOPLOOP) <= | 337 if ((next_read.sequence_number & ~MockRead::STOPLOOP) <= |
| 760 sequence_number_++) { | 338 sequence_number_++) { |
| 761 NET_TRACE(INFO, " *** ") << "Stage " << sequence_number_ - 1 | 339 NET_TRACE(INFO, " *** ") << "Stage " << sequence_number_ - 1 |
| 762 << ": Read " << read_index(); | 340 << ": Read " << read_index(); |
| (...skipping 29 matching lines...) Expand all Loading... |
| 792 void OrderedSocketData::Reset() { | 370 void OrderedSocketData::Reset() { |
| 793 NET_TRACE(INFO, " *** ") << "Stage " | 371 NET_TRACE(INFO, " *** ") << "Stage " |
| 794 << sequence_number_ << ": Reset()"; | 372 << sequence_number_ << ": Reset()"; |
| 795 sequence_number_ = 0; | 373 sequence_number_ = 0; |
| 796 loop_stop_stage_ = 0; | 374 loop_stop_stage_ = 0; |
| 797 set_socket(NULL); | 375 set_socket(NULL); |
| 798 factory_.RevokeAll(); | 376 factory_.RevokeAll(); |
| 799 StaticSocketDataProvider::Reset(); | 377 StaticSocketDataProvider::Reset(); |
| 800 } | 378 } |
| 801 | 379 |
| 802 void OrderedSocketData::EndLoop() { | |
| 803 // If we've already stopped the loop, don't do it again until we've advanced | |
| 804 // to the next sequence_number. | |
| 805 NET_TRACE(INFO, " *** ") << "Stage " << sequence_number_ << ": EndLoop()"; | |
| 806 if (loop_stop_stage_ > 0) { | |
| 807 const MockRead& next_read = StaticSocketDataProvider::PeekRead(); | |
| 808 if ((next_read.sequence_number & ~MockRead::STOPLOOP) > | |
| 809 loop_stop_stage_) { | |
| 810 NET_TRACE(INFO, " *** ") << "Stage " << sequence_number_ | |
| 811 << ": Clearing stop index"; | |
| 812 loop_stop_stage_ = 0; | |
| 813 } else { | |
| 814 return; | |
| 815 } | |
| 816 } | |
| 817 // Record the sequence_number at which we stopped the loop. | |
| 818 NET_TRACE(INFO, " *** ") << "Stage " << sequence_number_ | |
| 819 << ": Posting Quit at read " << read_index(); | |
| 820 loop_stop_stage_ = sequence_number_; | |
| 821 if (callback_) | |
| 822 callback_->RunWithParams(Tuple1<int>(ERR_IO_PENDING)); | |
| 823 } | |
| 824 | |
| 825 void OrderedSocketData::CompleteRead() { | 380 void OrderedSocketData::CompleteRead() { |
| 826 if (socket()) { | 381 if (socket()) { |
| 827 NET_TRACE(INFO, " *** ") << "Stage " << sequence_number_; | 382 NET_TRACE(INFO, " *** ") << "Stage " << sequence_number_; |
| 828 socket()->OnReadComplete(GetNextRead()); | 383 socket()->OnReadComplete(GetNextRead()); |
| 829 } | 384 } |
| 830 } | 385 } |
| 831 | 386 |
| 832 OrderedSocketData::~OrderedSocketData() {} | 387 OrderedSocketData::~OrderedSocketData() {} |
| 833 | 388 |
| 834 DeterministicSocketData::DeterministicSocketData(MockRead* reads, | 389 DeterministicSocketData::DeterministicSocketData(MockRead* reads, |
| 835 size_t reads_count, MockWrite* writes, size_t writes_count) | 390 size_t reads_count, MockWrite* writes, size_t writes_count) |
| 836 : StaticSocketDataProvider(reads, reads_count, writes, writes_count), | 391 : StaticSocketDataProvider(reads, reads_count, writes, writes_count), |
| 837 sequence_number_(0), | 392 sequence_number_(0), |
| 838 current_read_(), | 393 current_read_(), |
| 839 current_write_(), | 394 current_write_(), |
| 840 stopping_sequence_number_(0), | 395 stopping_sequence_number_(0), |
| 841 stopped_(false), | 396 stopped_(false), |
| 842 print_debug_(false) {} | 397 print_debug_(false) {} |
| 843 | 398 |
| 844 MockRead DeterministicSocketData::GetNextRead() { | 399 DeterministicSocketData::~DeterministicSocketData() {} |
| 845 current_read_ = StaticSocketDataProvider::PeekRead(); | |
| 846 EXPECT_LE(sequence_number_, current_read_.sequence_number); | |
| 847 | |
| 848 // Synchronous read while stopped is an error | |
| 849 if (stopped() && !current_read_.async) { | |
| 850 LOG(ERROR) << "Unable to perform synchronous IO while stopped"; | |
| 851 return MockRead(false, ERR_UNEXPECTED); | |
| 852 } | |
| 853 | |
| 854 // Async read which will be called back in a future step. | |
| 855 if (sequence_number_ < current_read_.sequence_number) { | |
| 856 NET_TRACE(INFO, " *** ") << "Stage " << sequence_number_ | |
| 857 << ": I/O Pending"; | |
| 858 MockRead result = MockRead(false, ERR_IO_PENDING); | |
| 859 if (!current_read_.async) { | |
| 860 LOG(ERROR) << "Unable to perform synchronous read: " | |
| 861 << current_read_.sequence_number | |
| 862 << " at stage: " << sequence_number_; | |
| 863 result = MockRead(false, ERR_UNEXPECTED); | |
| 864 } | |
| 865 if (print_debug_) | |
| 866 DumpMockRead(result); | |
| 867 return result; | |
| 868 } | |
| 869 | |
| 870 NET_TRACE(INFO, " *** ") << "Stage " << sequence_number_ | |
| 871 << ": Read " << read_index(); | |
| 872 if (print_debug_) | |
| 873 DumpMockRead(current_read_); | |
| 874 | |
| 875 // Increment the sequence number if IO is complete | |
| 876 if (!current_read_.async) | |
| 877 NextStep(); | |
| 878 | |
| 879 DCHECK_NE(ERR_IO_PENDING, current_read_.result); | |
| 880 StaticSocketDataProvider::GetNextRead(); | |
| 881 | |
| 882 return current_read_; | |
| 883 } | |
| 884 | |
| 885 MockWriteResult DeterministicSocketData::OnWrite(const std::string& data) { | |
| 886 const MockWrite& next_write = StaticSocketDataProvider::PeekWrite(); | |
| 887 current_write_ = next_write; | |
| 888 | |
| 889 // Synchronous write while stopped is an error | |
| 890 if (stopped() && !next_write.async) { | |
| 891 LOG(ERROR) << "Unable to perform synchronous IO while stopped"; | |
| 892 return MockWriteResult(false, ERR_UNEXPECTED); | |
| 893 } | |
| 894 | |
| 895 // Async write which will be called back in a future step. | |
| 896 if (sequence_number_ < next_write.sequence_number) { | |
| 897 NET_TRACE(INFO, " *** ") << "Stage " << sequence_number_ | |
| 898 << ": I/O Pending"; | |
| 899 if (!next_write.async) { | |
| 900 LOG(ERROR) << "Unable to perform synchronous write: " | |
| 901 << next_write.sequence_number << " at stage: " << sequence_number_; | |
| 902 return MockWriteResult(false, ERR_UNEXPECTED); | |
| 903 } | |
| 904 } else { | |
| 905 NET_TRACE(INFO, " *** ") << "Stage " << sequence_number_ | |
| 906 << ": Write " << write_index(); | |
| 907 } | |
| 908 | |
| 909 if (print_debug_) | |
| 910 DumpMockRead(next_write); | |
| 911 | |
| 912 // Move to the next step if I/O is synchronous, since the operation will | |
| 913 // complete when this method returns. | |
| 914 if (!next_write.async) | |
| 915 NextStep(); | |
| 916 | |
| 917 // This is either a sync write for this step, or an async write. | |
| 918 return StaticSocketDataProvider::OnWrite(data); | |
| 919 } | |
| 920 | |
| 921 void DeterministicSocketData::Reset() { | |
| 922 NET_TRACE(INFO, " *** ") << "Stage " | |
| 923 << sequence_number_ << ": Reset()"; | |
| 924 sequence_number_ = 0; | |
| 925 StaticSocketDataProvider::Reset(); | |
| 926 NOTREACHED(); | |
| 927 } | |
| 928 | |
| 929 void DeterministicSocketData::RunFor(int steps) { | |
| 930 StopAfter(steps); | |
| 931 Run(); | |
| 932 } | |
| 933 | 400 |
| 934 void DeterministicSocketData::Run() { | 401 void DeterministicSocketData::Run() { |
| 935 SetStopped(false); | 402 SetStopped(false); |
| 936 int counter = 0; | 403 int counter = 0; |
| 937 // Continue to consume data until all data has run out, or the stopped_ flag | 404 // Continue to consume data until all data has run out, or the stopped_ flag |
| 938 // has been set. Consuming data requires two separate operations -- running | 405 // has been set. Consuming data requires two separate operations -- running |
| 939 // the tasks in the message loop, and explicitly invoking the read/write | 406 // the tasks in the message loop, and explicitly invoking the read/write |
| 940 // callbacks (simulating network I/O). We check our conditions between each, | 407 // callbacks (simulating network I/O). We check our conditions between each, |
| 941 // since they can change in either. | 408 // since they can change in either. |
| 942 while ((!at_write_eof() || !at_read_eof()) && !stopped()) { | 409 while ((!at_write_eof() || !at_read_eof()) && !stopped()) { |
| 943 if (counter % 2 == 0) | 410 if (counter % 2 == 0) |
| 944 MessageLoop::current()->RunAllPending(); | 411 MessageLoop::current()->RunAllPending(); |
| 945 if (counter % 2 == 1) { | 412 if (counter % 2 == 1) { |
| 946 InvokeCallbacks(); | 413 InvokeCallbacks(); |
| 947 } | 414 } |
| 948 counter++; | 415 counter++; |
| 949 } | 416 } |
| 950 // We're done consuming new data, but it is possible there are still some | 417 // We're done consuming new data, but it is possible there are still some |
| 951 // pending callbacks which we expect to complete before returning. | 418 // pending callbacks which we expect to complete before returning. |
| 952 while (socket_ && (socket_->write_pending() || socket_->read_pending()) && | 419 while (socket_ && (socket_->write_pending() || socket_->read_pending()) && |
| 953 !stopped()) { | 420 !stopped()) { |
| 954 InvokeCallbacks(); | 421 InvokeCallbacks(); |
| 955 MessageLoop::current()->RunAllPending(); | 422 MessageLoop::current()->RunAllPending(); |
| 956 } | 423 } |
| 957 SetStopped(false); | 424 SetStopped(false); |
| 958 } | 425 } |
| 959 | 426 |
| 427 void DeterministicSocketData::RunFor(int steps) { |
| 428 StopAfter(steps); |
| 429 Run(); |
| 430 } |
| 431 |
| 432 void DeterministicSocketData::SetStop(int seq) { |
| 433 DCHECK_LT(sequence_number_, seq); |
| 434 stopping_sequence_number_ = seq; |
| 435 stopped_ = false; |
| 436 } |
| 437 |
| 438 void DeterministicSocketData::StopAfter(int seq) { |
| 439 SetStop(sequence_number_ + seq); |
| 440 } |
| 441 |
| 442 MockRead DeterministicSocketData::GetNextRead() { |
| 443 current_read_ = StaticSocketDataProvider::PeekRead(); |
| 444 EXPECT_LE(sequence_number_, current_read_.sequence_number); |
| 445 |
| 446 // Synchronous read while stopped is an error |
| 447 if (stopped() && !current_read_.async) { |
| 448 LOG(ERROR) << "Unable to perform synchronous IO while stopped"; |
| 449 return MockRead(false, ERR_UNEXPECTED); |
| 450 } |
| 451 |
| 452 // Async read which will be called back in a future step. |
| 453 if (sequence_number_ < current_read_.sequence_number) { |
| 454 NET_TRACE(INFO, " *** ") << "Stage " << sequence_number_ |
| 455 << ": I/O Pending"; |
| 456 MockRead result = MockRead(false, ERR_IO_PENDING); |
| 457 if (!current_read_.async) { |
| 458 LOG(ERROR) << "Unable to perform synchronous read: " |
| 459 << current_read_.sequence_number |
| 460 << " at stage: " << sequence_number_; |
| 461 result = MockRead(false, ERR_UNEXPECTED); |
| 462 } |
| 463 if (print_debug_) |
| 464 DumpMockRead(result); |
| 465 return result; |
| 466 } |
| 467 |
| 468 NET_TRACE(INFO, " *** ") << "Stage " << sequence_number_ |
| 469 << ": Read " << read_index(); |
| 470 if (print_debug_) |
| 471 DumpMockRead(current_read_); |
| 472 |
| 473 // Increment the sequence number if IO is complete |
| 474 if (!current_read_.async) |
| 475 NextStep(); |
| 476 |
| 477 DCHECK_NE(ERR_IO_PENDING, current_read_.result); |
| 478 StaticSocketDataProvider::GetNextRead(); |
| 479 |
| 480 return current_read_; |
| 481 } |
| 482 |
| 483 MockWriteResult DeterministicSocketData::OnWrite(const std::string& data) { |
| 484 const MockWrite& next_write = StaticSocketDataProvider::PeekWrite(); |
| 485 current_write_ = next_write; |
| 486 |
| 487 // Synchronous write while stopped is an error |
| 488 if (stopped() && !next_write.async) { |
| 489 LOG(ERROR) << "Unable to perform synchronous IO while stopped"; |
| 490 return MockWriteResult(false, ERR_UNEXPECTED); |
| 491 } |
| 492 |
| 493 // Async write which will be called back in a future step. |
| 494 if (sequence_number_ < next_write.sequence_number) { |
| 495 NET_TRACE(INFO, " *** ") << "Stage " << sequence_number_ |
| 496 << ": I/O Pending"; |
| 497 if (!next_write.async) { |
| 498 LOG(ERROR) << "Unable to perform synchronous write: " |
| 499 << next_write.sequence_number << " at stage: " << sequence_number_; |
| 500 return MockWriteResult(false, ERR_UNEXPECTED); |
| 501 } |
| 502 } else { |
| 503 NET_TRACE(INFO, " *** ") << "Stage " << sequence_number_ |
| 504 << ": Write " << write_index(); |
| 505 } |
| 506 |
| 507 if (print_debug_) |
| 508 DumpMockRead(next_write); |
| 509 |
| 510 // Move to the next step if I/O is synchronous, since the operation will |
| 511 // complete when this method returns. |
| 512 if (!next_write.async) |
| 513 NextStep(); |
| 514 |
| 515 // This is either a sync write for this step, or an async write. |
| 516 return StaticSocketDataProvider::OnWrite(data); |
| 517 } |
| 518 |
| 519 void DeterministicSocketData::Reset() { |
| 520 NET_TRACE(INFO, " *** ") << "Stage " |
| 521 << sequence_number_ << ": Reset()"; |
| 522 sequence_number_ = 0; |
| 523 StaticSocketDataProvider::Reset(); |
| 524 NOTREACHED(); |
| 525 } |
| 526 |
| 960 void DeterministicSocketData::InvokeCallbacks() { | 527 void DeterministicSocketData::InvokeCallbacks() { |
| 961 if (socket_ && socket_->write_pending() && | 528 if (socket_ && socket_->write_pending() && |
| 962 (current_write().sequence_number == sequence_number())) { | 529 (current_write().sequence_number == sequence_number())) { |
| 963 socket_->CompleteWrite(); | 530 socket_->CompleteWrite(); |
| 964 NextStep(); | 531 NextStep(); |
| 965 return; | 532 return; |
| 966 } | 533 } |
| 967 if (socket_ && socket_->read_pending() && | 534 if (socket_ && socket_->read_pending() && |
| 968 (current_read().sequence_number == sequence_number())) { | 535 (current_read().sequence_number == sequence_number())) { |
| 969 socket_->CompleteRead(); | 536 socket_->CompleteRead(); |
| 970 NextStep(); | 537 NextStep(); |
| 971 return; | 538 return; |
| 972 } | 539 } |
| 973 } | 540 } |
| 974 | 541 |
| 975 void DeterministicSocketData::NextStep() { | 542 void DeterministicSocketData::NextStep() { |
| 976 // Invariant: Can never move *past* the stopping step. | 543 // Invariant: Can never move *past* the stopping step. |
| 977 DCHECK_LT(sequence_number_, stopping_sequence_number_); | 544 DCHECK_LT(sequence_number_, stopping_sequence_number_); |
| 978 sequence_number_++; | 545 sequence_number_++; |
| 979 if (sequence_number_ == stopping_sequence_number_) | 546 if (sequence_number_ == stopping_sequence_number_) |
| 980 SetStopped(true); | 547 SetStopped(true); |
| 981 } | 548 } |
| 982 | 549 |
| 983 | |
| 984 MockClientSocketFactory::MockClientSocketFactory() {} | 550 MockClientSocketFactory::MockClientSocketFactory() {} |
| 985 | 551 |
| 986 MockClientSocketFactory::~MockClientSocketFactory() {} | 552 MockClientSocketFactory::~MockClientSocketFactory() {} |
| 987 | 553 |
| 988 void MockClientSocketFactory::AddSocketDataProvider( | 554 void MockClientSocketFactory::AddSocketDataProvider( |
| 989 SocketDataProvider* data) { | 555 SocketDataProvider* data) { |
| 990 mock_data_.Add(data); | 556 mock_data_.Add(data); |
| 991 } | 557 } |
| 992 | 558 |
| 993 void MockClientSocketFactory::AddSSLSocketDataProvider( | 559 void MockClientSocketFactory::AddSSLSocketDataProvider( |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1031 SSLHostInfo* ssl_host_info, | 597 SSLHostInfo* ssl_host_info, |
| 1032 CertVerifier* cert_verifier, | 598 CertVerifier* cert_verifier, |
| 1033 DnsCertProvenanceChecker* dns_cert_checker) { | 599 DnsCertProvenanceChecker* dns_cert_checker) { |
| 1034 MockSSLClientSocket* socket = | 600 MockSSLClientSocket* socket = |
| 1035 new MockSSLClientSocket(transport_socket, host_and_port, ssl_config, | 601 new MockSSLClientSocket(transport_socket, host_and_port, ssl_config, |
| 1036 ssl_host_info, mock_ssl_data_.GetNext()); | 602 ssl_host_info, mock_ssl_data_.GetNext()); |
| 1037 ssl_client_sockets_.push_back(socket); | 603 ssl_client_sockets_.push_back(socket); |
| 1038 return socket; | 604 return socket; |
| 1039 } | 605 } |
| 1040 | 606 |
| 1041 DeterministicMockClientSocketFactory::DeterministicMockClientSocketFactory() {} | 607 MockClientSocket::MockClientSocket(net::NetLog* net_log) |
| 1042 | 608 : ALLOW_THIS_IN_INITIALIZER_LIST(method_factory_(this)), |
| 1043 DeterministicMockClientSocketFactory::~DeterministicMockClientSocketFactory() {} | 609 connected_(false), |
| 1044 | 610 net_log_(NetLog::Source(), net_log) { |
| 1045 void DeterministicMockClientSocketFactory::AddSocketDataProvider( | 611 } |
| 1046 DeterministicSocketData* data) { | 612 |
| 1047 mock_data_.Add(data); | 613 bool MockClientSocket::SetReceiveBufferSize(int32 size) { |
| 1048 } | 614 return true; |
| 1049 | 615 } |
| 1050 void DeterministicMockClientSocketFactory::AddSSLSocketDataProvider( | 616 |
| 1051 SSLSocketDataProvider* data) { | 617 bool MockClientSocket::SetSendBufferSize(int32 size) { |
| 1052 mock_ssl_data_.Add(data); | 618 return true; |
| 1053 } | 619 } |
| 1054 | 620 |
| 1055 void DeterministicMockClientSocketFactory::ResetNextMockIndexes() { | 621 void MockClientSocket::Disconnect() { |
| 1056 mock_data_.ResetNextIndex(); | 622 connected_ = false; |
| 1057 mock_ssl_data_.ResetNextIndex(); | 623 } |
| 1058 } | 624 |
| 1059 | 625 bool MockClientSocket::IsConnected() const { |
| 1060 MockSSLClientSocket* DeterministicMockClientSocketFactory:: | 626 return connected_; |
| 1061 GetMockSSLClientSocket(size_t index) const { | 627 } |
| 1062 DCHECK_LT(index, ssl_client_sockets_.size()); | 628 |
| 1063 return ssl_client_sockets_[index]; | 629 bool MockClientSocket::IsConnectedAndIdle() const { |
| 1064 } | 630 return connected_; |
| 1065 | 631 } |
| 1066 ClientSocket* DeterministicMockClientSocketFactory::CreateTCPClientSocket( | 632 |
| 1067 const AddressList& addresses, | 633 int MockClientSocket::GetPeerAddress(AddressList* address) const { |
| 1068 net::NetLog* net_log, | 634 return net::SystemHostResolverProc("localhost", ADDRESS_FAMILY_UNSPECIFIED, |
| 1069 const net::NetLog::Source& source) { | 635 0, address, NULL); |
| 1070 DeterministicSocketData* data_provider = mock_data().GetNext(); | 636 } |
| 1071 DeterministicMockTCPClientSocket* socket = | 637 |
| 1072 new DeterministicMockTCPClientSocket(net_log, data_provider); | 638 const BoundNetLog& MockClientSocket::NetLog() const { |
| 1073 data_provider->set_socket(socket->AsWeakPtr()); | 639 return net_log_; |
| 1074 tcp_client_sockets().push_back(socket); | 640 } |
| 1075 return socket; | 641 |
| 1076 } | 642 void MockClientSocket::GetSSLInfo(net::SSLInfo* ssl_info) { |
| 1077 | 643 NOTREACHED(); |
| 1078 SSLClientSocket* DeterministicMockClientSocketFactory::CreateSSLClientSocket( | 644 } |
| 1079 ClientSocketHandle* transport_socket, | 645 |
| 1080 const HostPortPair& host_and_port, | 646 void MockClientSocket::GetSSLCertRequestInfo( |
| 1081 const SSLConfig& ssl_config, | 647 net::SSLCertRequestInfo* cert_request_info) { |
| 648 } |
| 649 |
| 650 SSLClientSocket::NextProtoStatus |
| 651 MockClientSocket::GetNextProto(std::string* proto) { |
| 652 proto->clear(); |
| 653 return SSLClientSocket::kNextProtoUnsupported; |
| 654 } |
| 655 |
| 656 MockClientSocket::~MockClientSocket() {} |
| 657 |
| 658 void MockClientSocket::RunCallbackAsync(net::CompletionCallback* callback, |
| 659 int result) { |
| 660 MessageLoop::current()->PostTask(FROM_HERE, |
| 661 method_factory_.NewRunnableMethod( |
| 662 &MockClientSocket::RunCallback, callback, result)); |
| 663 } |
| 664 |
| 665 void MockClientSocket::RunCallback(net::CompletionCallback* callback, |
| 666 int result) { |
| 667 if (callback) |
| 668 callback->Run(result); |
| 669 } |
| 670 |
| 671 MockTCPClientSocket::MockTCPClientSocket(const net::AddressList& addresses, |
| 672 net::NetLog* net_log, |
| 673 net::SocketDataProvider* data) |
| 674 : MockClientSocket(net_log), |
| 675 addresses_(addresses), |
| 676 data_(data), |
| 677 read_offset_(0), |
| 678 read_data_(false, net::ERR_UNEXPECTED), |
| 679 need_read_data_(true), |
| 680 peer_closed_connection_(false), |
| 681 pending_buf_(NULL), |
| 682 pending_buf_len_(0), |
| 683 pending_callback_(NULL), |
| 684 was_used_to_convey_data_(false) { |
| 685 DCHECK(data_); |
| 686 data_->Reset(); |
| 687 } |
| 688 |
| 689 int MockTCPClientSocket::Read(net::IOBuffer* buf, int buf_len, |
| 690 net::CompletionCallback* callback) { |
| 691 if (!connected_) |
| 692 return net::ERR_UNEXPECTED; |
| 693 |
| 694 // If the buffer is already in use, a read is already in progress! |
| 695 DCHECK(pending_buf_ == NULL); |
| 696 |
| 697 // Store our async IO data. |
| 698 pending_buf_ = buf; |
| 699 pending_buf_len_ = buf_len; |
| 700 pending_callback_ = callback; |
| 701 |
| 702 if (need_read_data_) { |
| 703 read_data_ = data_->GetNextRead(); |
| 704 if (read_data_.result == ERR_TEST_PEER_CLOSE_AFTER_NEXT_MOCK_READ) { |
| 705 // This MockRead is just a marker to instruct us to set |
| 706 // peer_closed_connection_. Skip it and get the next one. |
| 707 read_data_ = data_->GetNextRead(); |
| 708 peer_closed_connection_ = true; |
| 709 } |
| 710 // ERR_IO_PENDING means that the SocketDataProvider is taking responsibility |
| 711 // to complete the async IO manually later (via OnReadComplete). |
| 712 if (read_data_.result == ERR_IO_PENDING) { |
| 713 DCHECK(callback); // We need to be using async IO in this case. |
| 714 return ERR_IO_PENDING; |
| 715 } |
| 716 need_read_data_ = false; |
| 717 } |
| 718 |
| 719 return CompleteRead(); |
| 720 } |
| 721 |
| 722 int MockTCPClientSocket::Write(net::IOBuffer* buf, int buf_len, |
| 723 net::CompletionCallback* callback) { |
| 724 DCHECK(buf); |
| 725 DCHECK_GT(buf_len, 0); |
| 726 |
| 727 if (!connected_) |
| 728 return net::ERR_UNEXPECTED; |
| 729 |
| 730 std::string data(buf->data(), buf_len); |
| 731 net::MockWriteResult write_result = data_->OnWrite(data); |
| 732 |
| 733 was_used_to_convey_data_ = true; |
| 734 |
| 735 if (write_result.async) { |
| 736 RunCallbackAsync(callback, write_result.result); |
| 737 return net::ERR_IO_PENDING; |
| 738 } |
| 739 |
| 740 return write_result.result; |
| 741 } |
| 742 |
| 743 int MockTCPClientSocket::Connect(net::CompletionCallback* callback) { |
| 744 if (connected_) |
| 745 return net::OK; |
| 746 connected_ = true; |
| 747 peer_closed_connection_ = false; |
| 748 if (data_->connect_data().async) { |
| 749 RunCallbackAsync(callback, data_->connect_data().result); |
| 750 return net::ERR_IO_PENDING; |
| 751 } |
| 752 return data_->connect_data().result; |
| 753 } |
| 754 |
| 755 void MockTCPClientSocket::Disconnect() { |
| 756 MockClientSocket::Disconnect(); |
| 757 pending_callback_ = NULL; |
| 758 } |
| 759 |
| 760 bool MockTCPClientSocket::IsConnected() const { |
| 761 return connected_ && !peer_closed_connection_; |
| 762 } |
| 763 |
| 764 bool MockTCPClientSocket::IsConnectedAndIdle() const { |
| 765 return IsConnected(); |
| 766 } |
| 767 |
| 768 bool MockTCPClientSocket::WasEverUsed() const { |
| 769 return was_used_to_convey_data_; |
| 770 } |
| 771 |
| 772 bool MockTCPClientSocket::UsingTCPFastOpen() const { |
| 773 return false; |
| 774 } |
| 775 |
| 776 void MockTCPClientSocket::OnReadComplete(const MockRead& data) { |
| 777 // There must be a read pending. |
| 778 DCHECK(pending_buf_); |
| 779 // You can't complete a read with another ERR_IO_PENDING status code. |
| 780 DCHECK_NE(ERR_IO_PENDING, data.result); |
| 781 // Since we've been waiting for data, need_read_data_ should be true. |
| 782 DCHECK(need_read_data_); |
| 783 |
| 784 read_data_ = data; |
| 785 need_read_data_ = false; |
| 786 |
| 787 // The caller is simulating that this IO completes right now. Don't |
| 788 // let CompleteRead() schedule a callback. |
| 789 read_data_.async = false; |
| 790 |
| 791 net::CompletionCallback* callback = pending_callback_; |
| 792 int rv = CompleteRead(); |
| 793 RunCallback(callback, rv); |
| 794 } |
| 795 |
| 796 int MockTCPClientSocket::CompleteRead() { |
| 797 DCHECK(pending_buf_); |
| 798 DCHECK(pending_buf_len_ > 0); |
| 799 |
| 800 was_used_to_convey_data_ = true; |
| 801 |
| 802 // Save the pending async IO data and reset our |pending_| state. |
| 803 net::IOBuffer* buf = pending_buf_; |
| 804 int buf_len = pending_buf_len_; |
| 805 net::CompletionCallback* callback = pending_callback_; |
| 806 pending_buf_ = NULL; |
| 807 pending_buf_len_ = 0; |
| 808 pending_callback_ = NULL; |
| 809 |
| 810 int result = read_data_.result; |
| 811 DCHECK(result != ERR_IO_PENDING); |
| 812 |
| 813 if (read_data_.data) { |
| 814 if (read_data_.data_len - read_offset_ > 0) { |
| 815 result = std::min(buf_len, read_data_.data_len - read_offset_); |
| 816 memcpy(buf->data(), read_data_.data + read_offset_, result); |
| 817 read_offset_ += result; |
| 818 if (read_offset_ == read_data_.data_len) { |
| 819 need_read_data_ = true; |
| 820 read_offset_ = 0; |
| 821 } |
| 822 } else { |
| 823 result = 0; // EOF |
| 824 } |
| 825 } |
| 826 |
| 827 if (read_data_.async) { |
| 828 DCHECK(callback); |
| 829 RunCallbackAsync(callback, result); |
| 830 return net::ERR_IO_PENDING; |
| 831 } |
| 832 return result; |
| 833 } |
| 834 |
| 835 DeterministicMockTCPClientSocket::DeterministicMockTCPClientSocket( |
| 836 net::NetLog* net_log, net::DeterministicSocketData* data) |
| 837 : MockClientSocket(net_log), |
| 838 write_pending_(false), |
| 839 write_callback_(NULL), |
| 840 write_result_(0), |
| 841 read_data_(), |
| 842 read_buf_(NULL), |
| 843 read_buf_len_(0), |
| 844 read_pending_(false), |
| 845 read_callback_(NULL), |
| 846 data_(data), |
| 847 was_used_to_convey_data_(false) {} |
| 848 |
| 849 DeterministicMockTCPClientSocket::~DeterministicMockTCPClientSocket() {} |
| 850 |
| 851 void DeterministicMockTCPClientSocket::CompleteWrite() { |
| 852 was_used_to_convey_data_ = true; |
| 853 write_pending_ = false; |
| 854 write_callback_->Run(write_result_); |
| 855 } |
| 856 |
| 857 int DeterministicMockTCPClientSocket::CompleteRead() { |
| 858 DCHECK_GT(read_buf_len_, 0); |
| 859 DCHECK_LE(read_data_.data_len, read_buf_len_); |
| 860 DCHECK(read_buf_); |
| 861 |
| 862 was_used_to_convey_data_ = true; |
| 863 |
| 864 if (read_data_.result == ERR_IO_PENDING) |
| 865 read_data_ = data_->GetNextRead(); |
| 866 DCHECK_NE(ERR_IO_PENDING, read_data_.result); |
| 867 // If read_data_.async is true, we do not need to wait, since this is already |
| 868 // the callback. Therefore we don't even bother to check it. |
| 869 int result = read_data_.result; |
| 870 |
| 871 if (read_data_.data_len > 0) { |
| 872 DCHECK(read_data_.data); |
| 873 result = std::min(read_buf_len_, read_data_.data_len); |
| 874 memcpy(read_buf_->data(), read_data_.data, result); |
| 875 } |
| 876 |
| 877 if (read_pending_) { |
| 878 read_pending_ = false; |
| 879 read_callback_->Run(result); |
| 880 } |
| 881 |
| 882 return result; |
| 883 } |
| 884 |
| 885 int DeterministicMockTCPClientSocket::Write( |
| 886 net::IOBuffer* buf, int buf_len, net::CompletionCallback* callback) { |
| 887 DCHECK(buf); |
| 888 DCHECK_GT(buf_len, 0); |
| 889 |
| 890 if (!connected_) |
| 891 return net::ERR_UNEXPECTED; |
| 892 |
| 893 std::string data(buf->data(), buf_len); |
| 894 net::MockWriteResult write_result = data_->OnWrite(data); |
| 895 |
| 896 if (write_result.async) { |
| 897 write_callback_ = callback; |
| 898 write_result_ = write_result.result; |
| 899 DCHECK(write_callback_ != NULL); |
| 900 write_pending_ = true; |
| 901 return net::ERR_IO_PENDING; |
| 902 } |
| 903 |
| 904 was_used_to_convey_data_ = true; |
| 905 write_pending_ = false; |
| 906 return write_result.result; |
| 907 } |
| 908 |
| 909 int DeterministicMockTCPClientSocket::Read( |
| 910 net::IOBuffer* buf, int buf_len, net::CompletionCallback* callback) { |
| 911 if (!connected_) |
| 912 return net::ERR_UNEXPECTED; |
| 913 |
| 914 read_data_ = data_->GetNextRead(); |
| 915 // The buffer should always be big enough to contain all the MockRead data. To |
| 916 // use small buffers, split the data into multiple MockReads. |
| 917 DCHECK_LE(read_data_.data_len, buf_len); |
| 918 |
| 919 read_buf_ = buf; |
| 920 read_buf_len_ = buf_len; |
| 921 read_callback_ = callback; |
| 922 |
| 923 if (read_data_.async || (read_data_.result == ERR_IO_PENDING)) { |
| 924 read_pending_ = true; |
| 925 DCHECK(read_callback_); |
| 926 return ERR_IO_PENDING; |
| 927 } |
| 928 |
| 929 was_used_to_convey_data_ = true; |
| 930 return CompleteRead(); |
| 931 } |
| 932 |
| 933 // TODO(erikchen): Support connect sequencing. |
| 934 int DeterministicMockTCPClientSocket::Connect( |
| 935 net::CompletionCallback* callback) { |
| 936 if (connected_) |
| 937 return net::OK; |
| 938 connected_ = true; |
| 939 if (data_->connect_data().async) { |
| 940 RunCallbackAsync(callback, data_->connect_data().result); |
| 941 return net::ERR_IO_PENDING; |
| 942 } |
| 943 return data_->connect_data().result; |
| 944 } |
| 945 |
| 946 void DeterministicMockTCPClientSocket::Disconnect() { |
| 947 MockClientSocket::Disconnect(); |
| 948 } |
| 949 |
| 950 bool DeterministicMockTCPClientSocket::IsConnected() const { |
| 951 return connected_; |
| 952 } |
| 953 |
| 954 bool DeterministicMockTCPClientSocket::IsConnectedAndIdle() const { |
| 955 return IsConnected(); |
| 956 } |
| 957 |
| 958 bool DeterministicMockTCPClientSocket::WasEverUsed() const { |
| 959 return was_used_to_convey_data_; |
| 960 } |
| 961 |
| 962 bool DeterministicMockTCPClientSocket::UsingTCPFastOpen() const { |
| 963 return false; |
| 964 } |
| 965 |
| 966 void DeterministicMockTCPClientSocket::OnReadComplete(const MockRead& data) {} |
| 967 |
| 968 class MockSSLClientSocket::ConnectCallback |
| 969 : public net::CompletionCallbackImpl<MockSSLClientSocket::ConnectCallback> { |
| 970 public: |
| 971 ConnectCallback(MockSSLClientSocket *ssl_client_socket, |
| 972 net::CompletionCallback* user_callback, |
| 973 int rv) |
| 974 : ALLOW_THIS_IN_INITIALIZER_LIST( |
| 975 net::CompletionCallbackImpl<MockSSLClientSocket::ConnectCallback>( |
| 976 this, &ConnectCallback::Wrapper)), |
| 977 ssl_client_socket_(ssl_client_socket), |
| 978 user_callback_(user_callback), |
| 979 rv_(rv) { |
| 980 } |
| 981 |
| 982 private: |
| 983 void Wrapper(int rv) { |
| 984 if (rv_ == net::OK) |
| 985 ssl_client_socket_->connected_ = true; |
| 986 user_callback_->Run(rv_); |
| 987 delete this; |
| 988 } |
| 989 |
| 990 MockSSLClientSocket* ssl_client_socket_; |
| 991 net::CompletionCallback* user_callback_; |
| 992 int rv_; |
| 993 }; |
| 994 |
| 995 MockSSLClientSocket::MockSSLClientSocket( |
| 996 net::ClientSocketHandle* transport_socket, |
| 997 const HostPortPair& host_port_pair, |
| 998 const net::SSLConfig& ssl_config, |
| 1082 SSLHostInfo* ssl_host_info, | 999 SSLHostInfo* ssl_host_info, |
| 1083 CertVerifier* cert_verifier, | 1000 net::SSLSocketDataProvider* data) |
| 1084 DnsCertProvenanceChecker* dns_cert_checker) { | 1001 : MockClientSocket(transport_socket->socket()->NetLog().net_log()), |
| 1085 MockSSLClientSocket* socket = | 1002 transport_(transport_socket), |
| 1086 new MockSSLClientSocket(transport_socket, host_and_port, ssl_config, | 1003 data_(data), |
| 1087 ssl_host_info, mock_ssl_data_.GetNext()); | 1004 is_npn_state_set_(false), |
| 1088 ssl_client_sockets_.push_back(socket); | 1005 new_npn_value_(false) { |
| 1089 return socket; | 1006 DCHECK(data_); |
| 1007 delete ssl_host_info; // we take ownership but don't use it. |
| 1008 } |
| 1009 |
| 1010 MockSSLClientSocket::~MockSSLClientSocket() { |
| 1011 Disconnect(); |
| 1012 } |
| 1013 |
| 1014 int MockSSLClientSocket::Read(net::IOBuffer* buf, int buf_len, |
| 1015 net::CompletionCallback* callback) { |
| 1016 return transport_->socket()->Read(buf, buf_len, callback); |
| 1017 } |
| 1018 |
| 1019 int MockSSLClientSocket::Write(net::IOBuffer* buf, int buf_len, |
| 1020 net::CompletionCallback* callback) { |
| 1021 return transport_->socket()->Write(buf, buf_len, callback); |
| 1022 } |
| 1023 |
| 1024 int MockSSLClientSocket::Connect(net::CompletionCallback* callback) { |
| 1025 ConnectCallback* connect_callback = new ConnectCallback( |
| 1026 this, callback, data_->connect.result); |
| 1027 int rv = transport_->socket()->Connect(connect_callback); |
| 1028 if (rv == net::OK) { |
| 1029 delete connect_callback; |
| 1030 if (data_->connect.result == net::OK) |
| 1031 connected_ = true; |
| 1032 if (data_->connect.async) { |
| 1033 RunCallbackAsync(callback, data_->connect.result); |
| 1034 return net::ERR_IO_PENDING; |
| 1035 } |
| 1036 return data_->connect.result; |
| 1037 } |
| 1038 return rv; |
| 1039 } |
| 1040 |
| 1041 void MockSSLClientSocket::Disconnect() { |
| 1042 MockClientSocket::Disconnect(); |
| 1043 if (transport_->socket() != NULL) |
| 1044 transport_->socket()->Disconnect(); |
| 1045 } |
| 1046 |
| 1047 bool MockSSLClientSocket::IsConnected() const { |
| 1048 return transport_->socket()->IsConnected(); |
| 1049 } |
| 1050 |
| 1051 bool MockSSLClientSocket::WasEverUsed() const { |
| 1052 return transport_->socket()->WasEverUsed(); |
| 1053 } |
| 1054 |
| 1055 bool MockSSLClientSocket::UsingTCPFastOpen() const { |
| 1056 return transport_->socket()->UsingTCPFastOpen(); |
| 1057 } |
| 1058 |
| 1059 void MockSSLClientSocket::GetSSLInfo(net::SSLInfo* ssl_info) { |
| 1060 ssl_info->Reset(); |
| 1061 } |
| 1062 |
| 1063 void MockSSLClientSocket::GetSSLCertRequestInfo( |
| 1064 net::SSLCertRequestInfo* cert_request_info) { |
| 1065 DCHECK(cert_request_info); |
| 1066 if (data_->cert_request_info) { |
| 1067 cert_request_info->host_and_port = |
| 1068 data_->cert_request_info->host_and_port; |
| 1069 cert_request_info->client_certs = data_->cert_request_info->client_certs; |
| 1070 } else { |
| 1071 cert_request_info->Reset(); |
| 1072 } |
| 1073 } |
| 1074 |
| 1075 SSLClientSocket::NextProtoStatus MockSSLClientSocket::GetNextProto( |
| 1076 std::string* proto) { |
| 1077 *proto = data_->next_proto; |
| 1078 return data_->next_proto_status; |
| 1079 } |
| 1080 |
| 1081 bool MockSSLClientSocket::was_npn_negotiated() const { |
| 1082 if (is_npn_state_set_) |
| 1083 return new_npn_value_; |
| 1084 return data_->was_npn_negotiated; |
| 1085 } |
| 1086 |
| 1087 bool MockSSLClientSocket::set_was_npn_negotiated(bool negotiated) { |
| 1088 is_npn_state_set_ = true; |
| 1089 return new_npn_value_ = negotiated; |
| 1090 } |
| 1091 |
| 1092 void MockSSLClientSocket::OnReadComplete(const MockRead& data) { |
| 1093 NOTIMPLEMENTED(); |
| 1090 } | 1094 } |
| 1091 | 1095 |
| 1092 TestSocketRequest::TestSocketRequest( | 1096 TestSocketRequest::TestSocketRequest( |
| 1093 std::vector<TestSocketRequest*>* request_order, | 1097 std::vector<TestSocketRequest*>* request_order, |
| 1094 size_t* completion_count) | 1098 size_t* completion_count) |
| 1095 : request_order_(request_order), | 1099 : request_order_(request_order), |
| 1096 completion_count_(completion_count) { | 1100 completion_count_(completion_count) { |
| 1097 DCHECK(request_order); | 1101 DCHECK(request_order); |
| 1098 DCHECK(completion_count); | 1102 DCHECK(completion_count); |
| 1099 } | 1103 } |
| (...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1208 int max_sockets_per_group, | 1212 int max_sockets_per_group, |
| 1209 ClientSocketPoolHistograms* histograms, | 1213 ClientSocketPoolHistograms* histograms, |
| 1210 ClientSocketFactory* socket_factory) | 1214 ClientSocketFactory* socket_factory) |
| 1211 : TCPClientSocketPool(max_sockets, max_sockets_per_group, histograms, | 1215 : TCPClientSocketPool(max_sockets, max_sockets_per_group, histograms, |
| 1212 NULL, NULL, NULL), | 1216 NULL, NULL, NULL), |
| 1213 client_socket_factory_(socket_factory), | 1217 client_socket_factory_(socket_factory), |
| 1214 release_count_(0), | 1218 release_count_(0), |
| 1215 cancel_count_(0) { | 1219 cancel_count_(0) { |
| 1216 } | 1220 } |
| 1217 | 1221 |
| 1222 MockTCPClientSocketPool::~MockTCPClientSocketPool() {} |
| 1223 |
| 1218 int MockTCPClientSocketPool::RequestSocket(const std::string& group_name, | 1224 int MockTCPClientSocketPool::RequestSocket(const std::string& group_name, |
| 1219 const void* socket_params, | 1225 const void* socket_params, |
| 1220 RequestPriority priority, | 1226 RequestPriority priority, |
| 1221 ClientSocketHandle* handle, | 1227 ClientSocketHandle* handle, |
| 1222 CompletionCallback* callback, | 1228 CompletionCallback* callback, |
| 1223 const BoundNetLog& net_log) { | 1229 const BoundNetLog& net_log) { |
| 1224 ClientSocket* socket = client_socket_factory_->CreateTCPClientSocket( | 1230 ClientSocket* socket = client_socket_factory_->CreateTCPClientSocket( |
| 1225 AddressList(), net_log.net_log(), net::NetLog::Source()); | 1231 AddressList(), net_log.net_log(), net::NetLog::Source()); |
| 1226 MockConnectJob* job = new MockConnectJob(socket, handle, callback); | 1232 MockConnectJob* job = new MockConnectJob(socket, handle, callback); |
| 1227 job_list_.push_back(job); | 1233 job_list_.push_back(job); |
| (...skipping 12 matching lines...) Expand all Loading... |
| 1240 } | 1246 } |
| 1241 } | 1247 } |
| 1242 | 1248 |
| 1243 void MockTCPClientSocketPool::ReleaseSocket(const std::string& group_name, | 1249 void MockTCPClientSocketPool::ReleaseSocket(const std::string& group_name, |
| 1244 ClientSocket* socket, int id) { | 1250 ClientSocket* socket, int id) { |
| 1245 EXPECT_EQ(1, id); | 1251 EXPECT_EQ(1, id); |
| 1246 release_count_++; | 1252 release_count_++; |
| 1247 delete socket; | 1253 delete socket; |
| 1248 } | 1254 } |
| 1249 | 1255 |
| 1250 MockTCPClientSocketPool::~MockTCPClientSocketPool() {} | 1256 DeterministicMockClientSocketFactory::DeterministicMockClientSocketFactory() {} |
| 1257 |
| 1258 DeterministicMockClientSocketFactory::~DeterministicMockClientSocketFactory() {} |
| 1259 |
| 1260 void DeterministicMockClientSocketFactory::AddSocketDataProvider( |
| 1261 DeterministicSocketData* data) { |
| 1262 mock_data_.Add(data); |
| 1263 } |
| 1264 |
| 1265 void DeterministicMockClientSocketFactory::AddSSLSocketDataProvider( |
| 1266 SSLSocketDataProvider* data) { |
| 1267 mock_ssl_data_.Add(data); |
| 1268 } |
| 1269 |
| 1270 void DeterministicMockClientSocketFactory::ResetNextMockIndexes() { |
| 1271 mock_data_.ResetNextIndex(); |
| 1272 mock_ssl_data_.ResetNextIndex(); |
| 1273 } |
| 1274 |
| 1275 MockSSLClientSocket* DeterministicMockClientSocketFactory:: |
| 1276 GetMockSSLClientSocket(size_t index) const { |
| 1277 DCHECK_LT(index, ssl_client_sockets_.size()); |
| 1278 return ssl_client_sockets_[index]; |
| 1279 } |
| 1280 |
| 1281 ClientSocket* DeterministicMockClientSocketFactory::CreateTCPClientSocket( |
| 1282 const AddressList& addresses, |
| 1283 net::NetLog* net_log, |
| 1284 const net::NetLog::Source& source) { |
| 1285 DeterministicSocketData* data_provider = mock_data().GetNext(); |
| 1286 DeterministicMockTCPClientSocket* socket = |
| 1287 new DeterministicMockTCPClientSocket(net_log, data_provider); |
| 1288 data_provider->set_socket(socket->AsWeakPtr()); |
| 1289 tcp_client_sockets().push_back(socket); |
| 1290 return socket; |
| 1291 } |
| 1292 |
| 1293 SSLClientSocket* DeterministicMockClientSocketFactory::CreateSSLClientSocket( |
| 1294 ClientSocketHandle* transport_socket, |
| 1295 const HostPortPair& host_and_port, |
| 1296 const SSLConfig& ssl_config, |
| 1297 SSLHostInfo* ssl_host_info, |
| 1298 CertVerifier* cert_verifier, |
| 1299 DnsCertProvenanceChecker* dns_cert_checker) { |
| 1300 MockSSLClientSocket* socket = |
| 1301 new MockSSLClientSocket(transport_socket, host_and_port, ssl_config, |
| 1302 ssl_host_info, mock_ssl_data_.GetNext()); |
| 1303 ssl_client_sockets_.push_back(socket); |
| 1304 return socket; |
| 1305 } |
| 1251 | 1306 |
| 1252 MockSOCKSClientSocketPool::MockSOCKSClientSocketPool( | 1307 MockSOCKSClientSocketPool::MockSOCKSClientSocketPool( |
| 1253 int max_sockets, | 1308 int max_sockets, |
| 1254 int max_sockets_per_group, | 1309 int max_sockets_per_group, |
| 1255 ClientSocketPoolHistograms* histograms, | 1310 ClientSocketPoolHistograms* histograms, |
| 1256 TCPClientSocketPool* tcp_pool) | 1311 TCPClientSocketPool* tcp_pool) |
| 1257 : SOCKSClientSocketPool(max_sockets, max_sockets_per_group, histograms, | 1312 : SOCKSClientSocketPool(max_sockets, max_sockets_per_group, histograms, |
| 1258 NULL, tcp_pool, NULL), | 1313 NULL, tcp_pool, NULL), |
| 1259 tcp_pool_(tcp_pool) { | 1314 tcp_pool_(tcp_pool) { |
| 1260 } | 1315 } |
| 1261 | 1316 |
| 1317 MockSOCKSClientSocketPool::~MockSOCKSClientSocketPool() {} |
| 1318 |
| 1262 int MockSOCKSClientSocketPool::RequestSocket(const std::string& group_name, | 1319 int MockSOCKSClientSocketPool::RequestSocket(const std::string& group_name, |
| 1263 const void* socket_params, | 1320 const void* socket_params, |
| 1264 RequestPriority priority, | 1321 RequestPriority priority, |
| 1265 ClientSocketHandle* handle, | 1322 ClientSocketHandle* handle, |
| 1266 CompletionCallback* callback, | 1323 CompletionCallback* callback, |
| 1267 const BoundNetLog& net_log) { | 1324 const BoundNetLog& net_log) { |
| 1268 return tcp_pool_->RequestSocket(group_name, socket_params, priority, handle, | 1325 return tcp_pool_->RequestSocket(group_name, socket_params, priority, handle, |
| 1269 callback, net_log); | 1326 callback, net_log); |
| 1270 } | 1327 } |
| 1271 | 1328 |
| 1272 void MockSOCKSClientSocketPool::CancelRequest( | 1329 void MockSOCKSClientSocketPool::CancelRequest( |
| 1273 const std::string& group_name, | 1330 const std::string& group_name, |
| 1274 ClientSocketHandle* handle) { | 1331 ClientSocketHandle* handle) { |
| 1275 return tcp_pool_->CancelRequest(group_name, handle); | 1332 return tcp_pool_->CancelRequest(group_name, handle); |
| 1276 } | 1333 } |
| 1277 | 1334 |
| 1278 void MockSOCKSClientSocketPool::ReleaseSocket(const std::string& group_name, | 1335 void MockSOCKSClientSocketPool::ReleaseSocket(const std::string& group_name, |
| 1279 ClientSocket* socket, int id) { | 1336 ClientSocket* socket, int id) { |
| 1280 return tcp_pool_->ReleaseSocket(group_name, socket, id); | 1337 return tcp_pool_->ReleaseSocket(group_name, socket, id); |
| 1281 } | 1338 } |
| 1282 | 1339 |
| 1283 MockSOCKSClientSocketPool::~MockSOCKSClientSocketPool() {} | |
| 1284 | |
| 1285 const char kSOCKS5GreetRequest[] = { 0x05, 0x01, 0x00 }; | 1340 const char kSOCKS5GreetRequest[] = { 0x05, 0x01, 0x00 }; |
| 1286 const int kSOCKS5GreetRequestLength = arraysize(kSOCKS5GreetRequest); | 1341 const int kSOCKS5GreetRequestLength = arraysize(kSOCKS5GreetRequest); |
| 1287 | 1342 |
| 1288 const char kSOCKS5GreetResponse[] = { 0x05, 0x00 }; | 1343 const char kSOCKS5GreetResponse[] = { 0x05, 0x00 }; |
| 1289 const int kSOCKS5GreetResponseLength = arraysize(kSOCKS5GreetResponse); | 1344 const int kSOCKS5GreetResponseLength = arraysize(kSOCKS5GreetResponse); |
| 1290 | 1345 |
| 1291 const char kSOCKS5OkRequest[] = | 1346 const char kSOCKS5OkRequest[] = |
| 1292 { 0x05, 0x01, 0x00, 0x03, 0x04, 'h', 'o', 's', 't', 0x00, 0x50 }; | 1347 { 0x05, 0x01, 0x00, 0x03, 0x04, 'h', 'o', 's', 't', 0x00, 0x50 }; |
| 1293 const int kSOCKS5OkRequestLength = arraysize(kSOCKS5OkRequest); | 1348 const int kSOCKS5OkRequestLength = arraysize(kSOCKS5OkRequest); |
| 1294 | 1349 |
| 1295 const char kSOCKS5OkResponse[] = | 1350 const char kSOCKS5OkResponse[] = |
| 1296 { 0x05, 0x00, 0x00, 0x01, 127, 0, 0, 1, 0x00, 0x50 }; | 1351 { 0x05, 0x00, 0x00, 0x01, 127, 0, 0, 1, 0x00, 0x50 }; |
| 1297 const int kSOCKS5OkResponseLength = arraysize(kSOCKS5OkResponse); | 1352 const int kSOCKS5OkResponseLength = arraysize(kSOCKS5OkResponse); |
| 1298 | 1353 |
| 1299 } // namespace net | 1354 } // namespace net |
| OLD | NEW |