| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "net/socket/socket_test_util.h" | 5 #include "net/socket/socket_test_util.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <vector> | 8 #include <vector> |
| 9 | 9 |
| 10 #include "base/basictypes.h" | 10 #include "base/basictypes.h" |
| (...skipping 125 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 136 } | 136 } |
| 137 | 137 |
| 138 MockConnect::MockConnect(IoMode io_mode, int r, IPEndPoint addr) : | 138 MockConnect::MockConnect(IoMode io_mode, int r, IPEndPoint addr) : |
| 139 mode(io_mode), | 139 mode(io_mode), |
| 140 result(r), | 140 result(r), |
| 141 peer_addr(addr) { | 141 peer_addr(addr) { |
| 142 } | 142 } |
| 143 | 143 |
| 144 MockConnect::~MockConnect() {} | 144 MockConnect::~MockConnect() {} |
| 145 | 145 |
| 146 bool SocketDataProvider::IsIdle() const { |
| 147 return true; |
| 148 } |
| 149 |
| 150 SocketDataProvider::SocketDataProvider() : socket_(nullptr) {} |
| 151 |
| 152 SocketDataProvider::~SocketDataProvider() { |
| 153 if (socket_) |
| 154 socket_->OnDataProviderDestroyed(); |
| 155 } |
| 156 |
| 146 StaticSocketDataHelper::StaticSocketDataHelper(MockRead* reads, | 157 StaticSocketDataHelper::StaticSocketDataHelper(MockRead* reads, |
| 147 size_t reads_count, | 158 size_t reads_count, |
| 148 MockWrite* writes, | 159 MockWrite* writes, |
| 149 size_t writes_count) | 160 size_t writes_count) |
| 150 : reads_(reads), | 161 : reads_(reads), |
| 151 read_index_(0), | 162 read_index_(0), |
| 152 read_count_(reads_count), | 163 read_count_(reads_count), |
| 153 writes_(writes), | 164 writes_(writes), |
| 154 write_index_(0), | 165 write_index_(0), |
| 155 write_count_(writes_count) { | 166 write_count_(writes_count) { |
| (...skipping 125 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 281 } | 292 } |
| 282 | 293 |
| 283 SequencedSocketData::SequencedSocketData(MockRead* reads, | 294 SequencedSocketData::SequencedSocketData(MockRead* reads, |
| 284 size_t reads_count, | 295 size_t reads_count, |
| 285 MockWrite* writes, | 296 MockWrite* writes, |
| 286 size_t writes_count) | 297 size_t writes_count) |
| 287 : helper_(reads, reads_count, writes, writes_count), | 298 : helper_(reads, reads_count, writes, writes_count), |
| 288 sequence_number_(0), | 299 sequence_number_(0), |
| 289 read_state_(IDLE), | 300 read_state_(IDLE), |
| 290 write_state_(IDLE), | 301 write_state_(IDLE), |
| 302 busy_before_sync_reads_(false), |
| 291 weak_factory_(this) { | 303 weak_factory_(this) { |
| 292 // Check that reads and writes have a contiguous set of sequence numbers | 304 // Check that reads and writes have a contiguous set of sequence numbers |
| 293 // starting from 0 and working their way up, with no repeats and skipping | 305 // starting from 0 and working their way up, with no repeats and skipping |
| 294 // no values. | 306 // no values. |
| 295 size_t next_read = 0; | 307 size_t next_read = 0; |
| 296 size_t next_write = 0; | 308 size_t next_write = 0; |
| 297 int next_sequence_number = 0; | 309 int next_sequence_number = 0; |
| 298 while (next_read < reads_count || next_write < writes_count) { | 310 while (next_read < reads_count || next_write < writes_count) { |
| 299 if (next_read < reads_count && | 311 if (next_read < reads_count && |
| 300 reads[next_read].sequence_number == next_sequence_number) { | 312 reads[next_read].sequence_number == next_sequence_number) { |
| (...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 420 } | 432 } |
| 421 | 433 |
| 422 bool SequencedSocketData::AllReadDataConsumed() const { | 434 bool SequencedSocketData::AllReadDataConsumed() const { |
| 423 return helper_.AllReadDataConsumed(); | 435 return helper_.AllReadDataConsumed(); |
| 424 } | 436 } |
| 425 | 437 |
| 426 bool SequencedSocketData::AllWriteDataConsumed() const { | 438 bool SequencedSocketData::AllWriteDataConsumed() const { |
| 427 return helper_.AllWriteDataConsumed(); | 439 return helper_.AllWriteDataConsumed(); |
| 428 } | 440 } |
| 429 | 441 |
| 442 bool SequencedSocketData::IsIdle() const { |
| 443 // If |busy_before_sync_reads_| is not set, always considered idle. If |
| 444 // no reads left, or the next operation is a write, also consider it idle. |
| 445 if (!busy_before_sync_reads_ || helper_.AllReadDataConsumed() || |
| 446 helper_.PeekRead().sequence_number != sequence_number_) { |
| 447 return true; |
| 448 } |
| 449 |
| 450 // If the next operation is synchronous read, treat the socket as not idle. |
| 451 if (helper_.PeekRead().mode == SYNCHRONOUS) |
| 452 return false; |
| 453 return true; |
| 454 } |
| 455 |
| 430 bool SequencedSocketData::IsReadPaused() { | 456 bool SequencedSocketData::IsReadPaused() { |
| 431 return read_state_ == PAUSED; | 457 return read_state_ == PAUSED; |
| 432 } | 458 } |
| 433 | 459 |
| 434 void SequencedSocketData::CompleteRead() { | 460 void SequencedSocketData::CompleteRead() { |
| 435 if (read_state_ != PAUSED) { | 461 if (read_state_ != PAUSED) { |
| 436 ADD_FAILURE() << "Unable to CompleteRead when not paused."; | 462 ADD_FAILURE() << "Unable to CompleteRead when not paused."; |
| 437 return; | 463 return; |
| 438 } | 464 } |
| 439 read_state_ = COMPLETING; | 465 read_state_ = COMPLETING; |
| (...skipping 493 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 933 need_read_data_(true), | 959 need_read_data_(true), |
| 934 peer_closed_connection_(false), | 960 peer_closed_connection_(false), |
| 935 pending_read_buf_(NULL), | 961 pending_read_buf_(NULL), |
| 936 pending_read_buf_len_(0), | 962 pending_read_buf_len_(0), |
| 937 was_used_to_convey_data_(false) { | 963 was_used_to_convey_data_(false) { |
| 938 DCHECK(data_); | 964 DCHECK(data_); |
| 939 peer_addr_ = data->connect_data().peer_addr; | 965 peer_addr_ = data->connect_data().peer_addr; |
| 940 data_->Reset(); | 966 data_->Reset(); |
| 941 } | 967 } |
| 942 | 968 |
| 943 MockTCPClientSocket::~MockTCPClientSocket() {} | 969 MockTCPClientSocket::~MockTCPClientSocket() { |
| 970 if (data_) |
| 971 data_->set_socket(nullptr); |
| 972 } |
| 944 | 973 |
| 945 int MockTCPClientSocket::Read(IOBuffer* buf, int buf_len, | 974 int MockTCPClientSocket::Read(IOBuffer* buf, int buf_len, |
| 946 const CompletionCallback& callback) { | 975 const CompletionCallback& callback) { |
| 947 if (!connected_) | 976 if (!connected_ || !data_) |
| 948 return ERR_UNEXPECTED; | 977 return ERR_UNEXPECTED; |
| 949 | 978 |
| 950 // If the buffer is already in use, a read is already in progress! | 979 // If the buffer is already in use, a read is already in progress! |
| 951 DCHECK(pending_read_buf_.get() == NULL); | 980 DCHECK(pending_read_buf_.get() == NULL); |
| 952 | 981 |
| 953 // Store our async IO data. | 982 // Store our async IO data. |
| 954 pending_read_buf_ = buf; | 983 pending_read_buf_ = buf; |
| 955 pending_read_buf_len_ = buf_len; | 984 pending_read_buf_len_ = buf_len; |
| 956 pending_read_callback_ = callback; | 985 pending_read_callback_ = callback; |
| 957 | 986 |
| (...skipping 21 matching lines...) Expand all Loading... |
| 979 } | 1008 } |
| 980 | 1009 |
| 981 return CompleteRead(); | 1010 return CompleteRead(); |
| 982 } | 1011 } |
| 983 | 1012 |
| 984 int MockTCPClientSocket::Write(IOBuffer* buf, int buf_len, | 1013 int MockTCPClientSocket::Write(IOBuffer* buf, int buf_len, |
| 985 const CompletionCallback& callback) { | 1014 const CompletionCallback& callback) { |
| 986 DCHECK(buf); | 1015 DCHECK(buf); |
| 987 DCHECK_GT(buf_len, 0); | 1016 DCHECK_GT(buf_len, 0); |
| 988 | 1017 |
| 989 if (!connected_) | 1018 if (!connected_ || !data_) |
| 990 return ERR_UNEXPECTED; | 1019 return ERR_UNEXPECTED; |
| 991 | 1020 |
| 992 std::string data(buf->data(), buf_len); | 1021 std::string data(buf->data(), buf_len); |
| 993 MockWriteResult write_result = data_->OnWrite(data); | 1022 MockWriteResult write_result = data_->OnWrite(data); |
| 994 | 1023 |
| 995 was_used_to_convey_data_ = true; | 1024 was_used_to_convey_data_ = true; |
| 996 | 1025 |
| 997 // ERR_IO_PENDING is a signal that the socket data will call back | 1026 // ERR_IO_PENDING is a signal that the socket data will call back |
| 998 // asynchronously later. | 1027 // asynchronously later. |
| 999 if (write_result.result == ERR_IO_PENDING) { | 1028 if (write_result.result == ERR_IO_PENDING) { |
| (...skipping 17 matching lines...) Expand all Loading... |
| 1017 connection_attempts_.clear(); | 1046 connection_attempts_.clear(); |
| 1018 } | 1047 } |
| 1019 | 1048 |
| 1020 void MockTCPClientSocket::AddConnectionAttempts( | 1049 void MockTCPClientSocket::AddConnectionAttempts( |
| 1021 const ConnectionAttempts& attempts) { | 1050 const ConnectionAttempts& attempts) { |
| 1022 connection_attempts_.insert(connection_attempts_.begin(), attempts.begin(), | 1051 connection_attempts_.insert(connection_attempts_.begin(), attempts.begin(), |
| 1023 attempts.end()); | 1052 attempts.end()); |
| 1024 } | 1053 } |
| 1025 | 1054 |
| 1026 int MockTCPClientSocket::Connect(const CompletionCallback& callback) { | 1055 int MockTCPClientSocket::Connect(const CompletionCallback& callback) { |
| 1056 if (!data_) |
| 1057 return ERR_UNEXPECTED; |
| 1058 |
| 1027 if (connected_) | 1059 if (connected_) |
| 1028 return OK; | 1060 return OK; |
| 1029 connected_ = true; | 1061 connected_ = true; |
| 1030 peer_closed_connection_ = false; | 1062 peer_closed_connection_ = false; |
| 1031 | 1063 |
| 1032 int result = data_->connect_data().result; | 1064 int result = data_->connect_data().result; |
| 1033 IoMode mode = data_->connect_data().mode; | 1065 IoMode mode = data_->connect_data().mode; |
| 1034 | 1066 |
| 1035 if (result != OK && result != ERR_IO_PENDING) { | 1067 if (result != OK && result != ERR_IO_PENDING) { |
| 1036 IPEndPoint address; | 1068 IPEndPoint address; |
| (...skipping 11 matching lines...) Expand all Loading... |
| 1048 return ERR_IO_PENDING; | 1080 return ERR_IO_PENDING; |
| 1049 } | 1081 } |
| 1050 | 1082 |
| 1051 void MockTCPClientSocket::Disconnect() { | 1083 void MockTCPClientSocket::Disconnect() { |
| 1052 MockClientSocket::Disconnect(); | 1084 MockClientSocket::Disconnect(); |
| 1053 pending_connect_callback_.Reset(); | 1085 pending_connect_callback_.Reset(); |
| 1054 pending_read_callback_.Reset(); | 1086 pending_read_callback_.Reset(); |
| 1055 } | 1087 } |
| 1056 | 1088 |
| 1057 bool MockTCPClientSocket::IsConnected() const { | 1089 bool MockTCPClientSocket::IsConnected() const { |
| 1090 if (!data_) |
| 1091 return false; |
| 1058 return connected_ && !peer_closed_connection_; | 1092 return connected_ && !peer_closed_connection_; |
| 1059 } | 1093 } |
| 1060 | 1094 |
| 1061 bool MockTCPClientSocket::IsConnectedAndIdle() const { | 1095 bool MockTCPClientSocket::IsConnectedAndIdle() const { |
| 1062 return IsConnected(); | 1096 if (!data_) |
| 1097 return false; |
| 1098 return IsConnected() && data_->IsIdle(); |
| 1063 } | 1099 } |
| 1064 | 1100 |
| 1065 int MockTCPClientSocket::GetPeerAddress(IPEndPoint* address) const { | 1101 int MockTCPClientSocket::GetPeerAddress(IPEndPoint* address) const { |
| 1066 if (addresses_.empty()) | 1102 if (addresses_.empty()) |
| 1067 return MockClientSocket::GetPeerAddress(address); | 1103 return MockClientSocket::GetPeerAddress(address); |
| 1068 | 1104 |
| 1069 *address = addresses_[0]; | 1105 *address = addresses_[0]; |
| 1070 return OK; | 1106 return OK; |
| 1071 } | 1107 } |
| 1072 | 1108 |
| 1073 bool MockTCPClientSocket::WasEverUsed() const { | 1109 bool MockTCPClientSocket::WasEverUsed() const { |
| 1074 return was_used_to_convey_data_; | 1110 return was_used_to_convey_data_; |
| 1075 } | 1111 } |
| 1076 | 1112 |
| 1077 bool MockTCPClientSocket::UsingTCPFastOpen() const { | 1113 bool MockTCPClientSocket::UsingTCPFastOpen() const { |
| 1078 return false; | 1114 return false; |
| 1079 } | 1115 } |
| 1080 | 1116 |
| 1081 bool MockTCPClientSocket::WasNpnNegotiated() const { | 1117 bool MockTCPClientSocket::WasNpnNegotiated() const { |
| 1082 return false; | 1118 return false; |
| 1083 } | 1119 } |
| 1084 | 1120 |
| 1085 bool MockTCPClientSocket::GetSSLInfo(SSLInfo* ssl_info) { | 1121 bool MockTCPClientSocket::GetSSLInfo(SSLInfo* ssl_info) { |
| 1086 return false; | 1122 return false; |
| 1087 } | 1123 } |
| 1088 | 1124 |
| 1089 void MockTCPClientSocket::OnReadComplete(const MockRead& data) { | 1125 void MockTCPClientSocket::OnReadComplete(const MockRead& data) { |
| 1126 // If |data_| has been destroyed, safest to just do nothing. |
| 1127 if (!data_) |
| 1128 return; |
| 1129 |
| 1090 // There must be a read pending. | 1130 // There must be a read pending. |
| 1091 DCHECK(pending_read_buf_.get()); | 1131 DCHECK(pending_read_buf_.get()); |
| 1092 // You can't complete a read with another ERR_IO_PENDING status code. | 1132 // You can't complete a read with another ERR_IO_PENDING status code. |
| 1093 DCHECK_NE(ERR_IO_PENDING, data.result); | 1133 DCHECK_NE(ERR_IO_PENDING, data.result); |
| 1094 // Since we've been waiting for data, need_read_data_ should be true. | 1134 // Since we've been waiting for data, need_read_data_ should be true. |
| 1095 DCHECK(need_read_data_); | 1135 DCHECK(need_read_data_); |
| 1096 | 1136 |
| 1097 read_data_ = data; | 1137 read_data_ = data; |
| 1098 need_read_data_ = false; | 1138 need_read_data_ = false; |
| 1099 | 1139 |
| 1100 // The caller is simulating that this IO completes right now. Don't | 1140 // The caller is simulating that this IO completes right now. Don't |
| 1101 // let CompleteRead() schedule a callback. | 1141 // let CompleteRead() schedule a callback. |
| 1102 read_data_.mode = SYNCHRONOUS; | 1142 read_data_.mode = SYNCHRONOUS; |
| 1103 | 1143 |
| 1104 CompletionCallback callback = pending_read_callback_; | 1144 CompletionCallback callback = pending_read_callback_; |
| 1105 int rv = CompleteRead(); | 1145 int rv = CompleteRead(); |
| 1106 RunCallback(callback, rv); | 1146 RunCallback(callback, rv); |
| 1107 } | 1147 } |
| 1108 | 1148 |
| 1109 void MockTCPClientSocket::OnWriteComplete(int rv) { | 1149 void MockTCPClientSocket::OnWriteComplete(int rv) { |
| 1150 // If |data_| has been destroyed, safest to just do nothing. |
| 1151 if (!data_) |
| 1152 return; |
| 1153 |
| 1110 // There must be a read pending. | 1154 // There must be a read pending. |
| 1111 DCHECK(!pending_write_callback_.is_null()); | 1155 DCHECK(!pending_write_callback_.is_null()); |
| 1112 CompletionCallback callback = pending_write_callback_; | 1156 CompletionCallback callback = pending_write_callback_; |
| 1113 RunCallback(callback, rv); | 1157 RunCallback(callback, rv); |
| 1114 } | 1158 } |
| 1115 | 1159 |
| 1116 void MockTCPClientSocket::OnConnectComplete(const MockConnect& data) { | 1160 void MockTCPClientSocket::OnConnectComplete(const MockConnect& data) { |
| 1161 // If |data_| has been destroyed, safest to just do nothing. |
| 1162 if (!data_) |
| 1163 return; |
| 1164 |
| 1117 CompletionCallback callback = pending_connect_callback_; | 1165 CompletionCallback callback = pending_connect_callback_; |
| 1118 RunCallback(callback, data.result); | 1166 RunCallback(callback, data.result); |
| 1119 } | 1167 } |
| 1120 | 1168 |
| 1169 void MockTCPClientSocket::OnDataProviderDestroyed() { |
| 1170 data_ = nullptr; |
| 1171 } |
| 1172 |
| 1121 int MockTCPClientSocket::CompleteRead() { | 1173 int MockTCPClientSocket::CompleteRead() { |
| 1122 DCHECK(pending_read_buf_.get()); | 1174 DCHECK(pending_read_buf_.get()); |
| 1123 DCHECK(pending_read_buf_len_ > 0); | 1175 DCHECK(pending_read_buf_len_ > 0); |
| 1124 | 1176 |
| 1125 was_used_to_convey_data_ = true; | 1177 was_used_to_convey_data_ = true; |
| 1126 | 1178 |
| 1127 // Save the pending async IO data and reset our |pending_| state. | 1179 // Save the pending async IO data and reset our |pending_| state. |
| 1128 scoped_refptr<IOBuffer> buf = pending_read_buf_; | 1180 scoped_refptr<IOBuffer> buf = pending_read_buf_; |
| 1129 int buf_len = pending_read_buf_len_; | 1181 int buf_len = pending_read_buf_len_; |
| 1130 CompletionCallback callback = pending_read_callback_; | 1182 CompletionCallback callback = pending_read_callback_; |
| (...skipping 365 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1496 void MockSSLClientSocket::Disconnect() { | 1548 void MockSSLClientSocket::Disconnect() { |
| 1497 MockClientSocket::Disconnect(); | 1549 MockClientSocket::Disconnect(); |
| 1498 if (transport_->socket() != NULL) | 1550 if (transport_->socket() != NULL) |
| 1499 transport_->socket()->Disconnect(); | 1551 transport_->socket()->Disconnect(); |
| 1500 } | 1552 } |
| 1501 | 1553 |
| 1502 bool MockSSLClientSocket::IsConnected() const { | 1554 bool MockSSLClientSocket::IsConnected() const { |
| 1503 return transport_->socket()->IsConnected(); | 1555 return transport_->socket()->IsConnected(); |
| 1504 } | 1556 } |
| 1505 | 1557 |
| 1558 bool MockSSLClientSocket::IsConnectedAndIdle() const { |
| 1559 return transport_->socket()->IsConnectedAndIdle(); |
| 1560 } |
| 1561 |
| 1506 bool MockSSLClientSocket::WasEverUsed() const { | 1562 bool MockSSLClientSocket::WasEverUsed() const { |
| 1507 return transport_->socket()->WasEverUsed(); | 1563 return transport_->socket()->WasEverUsed(); |
| 1508 } | 1564 } |
| 1509 | 1565 |
| 1510 bool MockSSLClientSocket::UsingTCPFastOpen() const { | 1566 bool MockSSLClientSocket::UsingTCPFastOpen() const { |
| 1511 return transport_->socket()->UsingTCPFastOpen(); | 1567 return transport_->socket()->UsingTCPFastOpen(); |
| 1512 } | 1568 } |
| 1513 | 1569 |
| 1514 int MockSSLClientSocket::GetPeerAddress(IPEndPoint* address) const { | 1570 int MockSSLClientSocket::GetPeerAddress(IPEndPoint* address) const { |
| 1515 return transport_->socket()->GetPeerAddress(address); | 1571 return transport_->socket()->GetPeerAddress(address); |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1568 source_port_(123), | 1624 source_port_(123), |
| 1569 pending_read_buf_(NULL), | 1625 pending_read_buf_(NULL), |
| 1570 pending_read_buf_len_(0), | 1626 pending_read_buf_len_(0), |
| 1571 net_log_(BoundNetLog::Make(net_log, NetLog::SOURCE_NONE)), | 1627 net_log_(BoundNetLog::Make(net_log, NetLog::SOURCE_NONE)), |
| 1572 weak_factory_(this) { | 1628 weak_factory_(this) { |
| 1573 DCHECK(data_); | 1629 DCHECK(data_); |
| 1574 data_->Reset(); | 1630 data_->Reset(); |
| 1575 peer_addr_ = data->connect_data().peer_addr; | 1631 peer_addr_ = data->connect_data().peer_addr; |
| 1576 } | 1632 } |
| 1577 | 1633 |
| 1578 MockUDPClientSocket::~MockUDPClientSocket() {} | 1634 MockUDPClientSocket::~MockUDPClientSocket() { |
| 1635 if (data_) |
| 1636 data_->set_socket(nullptr); |
| 1637 } |
| 1579 | 1638 |
| 1580 int MockUDPClientSocket::Read(IOBuffer* buf, | 1639 int MockUDPClientSocket::Read(IOBuffer* buf, |
| 1581 int buf_len, | 1640 int buf_len, |
| 1582 const CompletionCallback& callback) { | 1641 const CompletionCallback& callback) { |
| 1583 if (!connected_) | 1642 if (!connected_ || !data_) |
| 1584 return ERR_UNEXPECTED; | 1643 return ERR_UNEXPECTED; |
| 1585 | 1644 |
| 1586 // If the buffer is already in use, a read is already in progress! | 1645 // If the buffer is already in use, a read is already in progress! |
| 1587 DCHECK(pending_read_buf_.get() == NULL); | 1646 DCHECK(pending_read_buf_.get() == NULL); |
| 1588 | 1647 |
| 1589 // Store our async IO data. | 1648 // Store our async IO data. |
| 1590 pending_read_buf_ = buf; | 1649 pending_read_buf_ = buf; |
| 1591 pending_read_buf_len_ = buf_len; | 1650 pending_read_buf_len_ = buf_len; |
| 1592 pending_read_callback_ = callback; | 1651 pending_read_callback_ = callback; |
| 1593 | 1652 |
| (...skipping 10 matching lines...) Expand all Loading... |
| 1604 } | 1663 } |
| 1605 | 1664 |
| 1606 return CompleteRead(); | 1665 return CompleteRead(); |
| 1607 } | 1666 } |
| 1608 | 1667 |
| 1609 int MockUDPClientSocket::Write(IOBuffer* buf, int buf_len, | 1668 int MockUDPClientSocket::Write(IOBuffer* buf, int buf_len, |
| 1610 const CompletionCallback& callback) { | 1669 const CompletionCallback& callback) { |
| 1611 DCHECK(buf); | 1670 DCHECK(buf); |
| 1612 DCHECK_GT(buf_len, 0); | 1671 DCHECK_GT(buf_len, 0); |
| 1613 | 1672 |
| 1614 if (!connected_) | 1673 if (!connected_ || !data_) |
| 1615 return ERR_UNEXPECTED; | 1674 return ERR_UNEXPECTED; |
| 1616 | 1675 |
| 1617 std::string data(buf->data(), buf_len); | 1676 std::string data(buf->data(), buf_len); |
| 1618 MockWriteResult write_result = data_->OnWrite(data); | 1677 MockWriteResult write_result = data_->OnWrite(data); |
| 1619 | 1678 |
| 1620 // ERR_IO_PENDING is a signal that the socket data will call back | 1679 // ERR_IO_PENDING is a signal that the socket data will call back |
| 1621 // asynchronously. | 1680 // asynchronously. |
| 1622 if (write_result.result == ERR_IO_PENDING) { | 1681 if (write_result.result == ERR_IO_PENDING) { |
| 1623 pending_write_callback_ = callback; | 1682 pending_write_callback_ = callback; |
| 1624 return ERR_IO_PENDING; | 1683 return ERR_IO_PENDING; |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1658 const BoundNetLog& MockUDPClientSocket::NetLog() const { | 1717 const BoundNetLog& MockUDPClientSocket::NetLog() const { |
| 1659 return net_log_; | 1718 return net_log_; |
| 1660 } | 1719 } |
| 1661 | 1720 |
| 1662 int MockUDPClientSocket::BindToNetwork( | 1721 int MockUDPClientSocket::BindToNetwork( |
| 1663 NetworkChangeNotifier::NetworkHandle network) { | 1722 NetworkChangeNotifier::NetworkHandle network) { |
| 1664 return ERR_NOT_IMPLEMENTED; | 1723 return ERR_NOT_IMPLEMENTED; |
| 1665 } | 1724 } |
| 1666 | 1725 |
| 1667 int MockUDPClientSocket::Connect(const IPEndPoint& address) { | 1726 int MockUDPClientSocket::Connect(const IPEndPoint& address) { |
| 1727 if (!data_) |
| 1728 return ERR_UNEXPECTED; |
| 1668 connected_ = true; | 1729 connected_ = true; |
| 1669 peer_addr_ = address; | 1730 peer_addr_ = address; |
| 1670 return data_->connect_data().result; | 1731 return data_->connect_data().result; |
| 1671 } | 1732 } |
| 1672 | 1733 |
| 1673 void MockUDPClientSocket::OnReadComplete(const MockRead& data) { | 1734 void MockUDPClientSocket::OnReadComplete(const MockRead& data) { |
| 1735 if (!data_) |
| 1736 return; |
| 1737 |
| 1674 // There must be a read pending. | 1738 // There must be a read pending. |
| 1675 DCHECK(pending_read_buf_.get()); | 1739 DCHECK(pending_read_buf_.get()); |
| 1676 // You can't complete a read with another ERR_IO_PENDING status code. | 1740 // You can't complete a read with another ERR_IO_PENDING status code. |
| 1677 DCHECK_NE(ERR_IO_PENDING, data.result); | 1741 DCHECK_NE(ERR_IO_PENDING, data.result); |
| 1678 // Since we've been waiting for data, need_read_data_ should be true. | 1742 // Since we've been waiting for data, need_read_data_ should be true. |
| 1679 DCHECK(need_read_data_); | 1743 DCHECK(need_read_data_); |
| 1680 | 1744 |
| 1681 read_data_ = data; | 1745 read_data_ = data; |
| 1682 need_read_data_ = false; | 1746 need_read_data_ = false; |
| 1683 | 1747 |
| 1684 // The caller is simulating that this IO completes right now. Don't | 1748 // The caller is simulating that this IO completes right now. Don't |
| 1685 // let CompleteRead() schedule a callback. | 1749 // let CompleteRead() schedule a callback. |
| 1686 read_data_.mode = SYNCHRONOUS; | 1750 read_data_.mode = SYNCHRONOUS; |
| 1687 | 1751 |
| 1688 CompletionCallback callback = pending_read_callback_; | 1752 CompletionCallback callback = pending_read_callback_; |
| 1689 int rv = CompleteRead(); | 1753 int rv = CompleteRead(); |
| 1690 RunCallback(callback, rv); | 1754 RunCallback(callback, rv); |
| 1691 } | 1755 } |
| 1692 | 1756 |
| 1693 void MockUDPClientSocket::OnWriteComplete(int rv) { | 1757 void MockUDPClientSocket::OnWriteComplete(int rv) { |
| 1758 if (!data_) |
| 1759 return; |
| 1760 |
| 1694 // There must be a read pending. | 1761 // There must be a read pending. |
| 1695 DCHECK(!pending_write_callback_.is_null()); | 1762 DCHECK(!pending_write_callback_.is_null()); |
| 1696 CompletionCallback callback = pending_write_callback_; | 1763 CompletionCallback callback = pending_write_callback_; |
| 1697 RunCallback(callback, rv); | 1764 RunCallback(callback, rv); |
| 1698 } | 1765 } |
| 1699 | 1766 |
| 1700 void MockUDPClientSocket::OnConnectComplete(const MockConnect& data) { | 1767 void MockUDPClientSocket::OnConnectComplete(const MockConnect& data) { |
| 1701 NOTIMPLEMENTED(); | 1768 NOTIMPLEMENTED(); |
| 1702 } | 1769 } |
| 1703 | 1770 |
| 1771 void MockUDPClientSocket::OnDataProviderDestroyed() { |
| 1772 data_ = nullptr; |
| 1773 } |
| 1774 |
| 1704 int MockUDPClientSocket::CompleteRead() { | 1775 int MockUDPClientSocket::CompleteRead() { |
| 1705 DCHECK(pending_read_buf_.get()); | 1776 DCHECK(pending_read_buf_.get()); |
| 1706 DCHECK(pending_read_buf_len_ > 0); | 1777 DCHECK(pending_read_buf_len_ > 0); |
| 1707 | 1778 |
| 1708 // Save the pending async IO data and reset our |pending_| state. | 1779 // Save the pending async IO data and reset our |pending_| state. |
| 1709 scoped_refptr<IOBuffer> buf = pending_read_buf_; | 1780 scoped_refptr<IOBuffer> buf = pending_read_buf_; |
| 1710 int buf_len = pending_read_buf_len_; | 1781 int buf_len = pending_read_buf_len_; |
| 1711 CompletionCallback callback = pending_read_callback_; | 1782 CompletionCallback callback = pending_read_callback_; |
| 1712 pending_read_buf_ = NULL; | 1783 pending_read_buf_ = NULL; |
| 1713 pending_read_buf_len_ = 0; | 1784 pending_read_buf_len_ = 0; |
| (...skipping 352 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2066 } | 2137 } |
| 2067 | 2138 |
| 2068 int64_t CountWriteBytes(const MockWrite writes[], size_t writes_size) { | 2139 int64_t CountWriteBytes(const MockWrite writes[], size_t writes_size) { |
| 2069 int64_t total = 0; | 2140 int64_t total = 0; |
| 2070 for (const MockWrite* write = writes; write != writes + writes_size; ++write) | 2141 for (const MockWrite* write = writes; write != writes + writes_size; ++write) |
| 2071 total += write->data_len; | 2142 total += write->data_len; |
| 2072 return total; | 2143 return total; |
| 2073 } | 2144 } |
| 2074 | 2145 |
| 2075 } // namespace net | 2146 } // namespace net |
| OLD | NEW |