OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "net/socket/socket_test_util.h" | 5 #include "net/socket/socket_test_util.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
10 #include "base/basictypes.h" | 10 #include "base/basictypes.h" |
(...skipping 814 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
825 | 825 |
826 was_used_to_convey_data_ = true; | 826 was_used_to_convey_data_ = true; |
827 | 827 |
828 if (write_result.async) { | 828 if (write_result.async) { |
829 RunCallbackAsync(callback, write_result.result); | 829 RunCallbackAsync(callback, write_result.result); |
830 return net::ERR_IO_PENDING; | 830 return net::ERR_IO_PENDING; |
831 } | 831 } |
832 | 832 |
833 return write_result.result; | 833 return write_result.result; |
834 } | 834 } |
| 835 int MockTCPClientSocket::Write(net::IOBuffer* buf, int buf_len, |
| 836 const net::CompletionCallback& callback) { |
| 837 DCHECK(buf); |
| 838 DCHECK_GT(buf_len, 0); |
| 839 |
| 840 if (!connected_) |
| 841 return net::ERR_UNEXPECTED; |
| 842 |
| 843 std::string data(buf->data(), buf_len); |
| 844 net::MockWriteResult write_result = data_->OnWrite(data); |
| 845 |
| 846 was_used_to_convey_data_ = true; |
| 847 |
| 848 if (write_result.async) { |
| 849 RunCallbackAsync(callback, write_result.result); |
| 850 return net::ERR_IO_PENDING; |
| 851 } |
| 852 |
| 853 return write_result.result; |
| 854 } |
835 | 855 |
836 int MockTCPClientSocket::Connect(net::OldCompletionCallback* callback) { | 856 int MockTCPClientSocket::Connect(net::OldCompletionCallback* callback) { |
837 if (connected_) | 857 if (connected_) |
838 return net::OK; | 858 return net::OK; |
839 connected_ = true; | 859 connected_ = true; |
840 peer_closed_connection_ = false; | 860 peer_closed_connection_ = false; |
841 if (data_->connect_data().async) { | 861 if (data_->connect_data().async) { |
842 RunCallbackAsync(callback, data_->connect_data().result); | 862 RunCallbackAsync(callback, data_->connect_data().result); |
843 return net::ERR_IO_PENDING; | 863 return net::ERR_IO_PENDING; |
844 } | 864 } |
(...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
965 RunCallbackAsync(callback, result); | 985 RunCallbackAsync(callback, result); |
966 return net::ERR_IO_PENDING; | 986 return net::ERR_IO_PENDING; |
967 } | 987 } |
968 return result; | 988 return result; |
969 } | 989 } |
970 | 990 |
971 DeterministicMockTCPClientSocket::DeterministicMockTCPClientSocket( | 991 DeterministicMockTCPClientSocket::DeterministicMockTCPClientSocket( |
972 net::NetLog* net_log, net::DeterministicSocketData* data) | 992 net::NetLog* net_log, net::DeterministicSocketData* data) |
973 : MockClientSocket(net_log), | 993 : MockClientSocket(net_log), |
974 write_pending_(false), | 994 write_pending_(false), |
975 write_callback_(NULL), | 995 old_write_callback_(NULL), |
976 write_result_(0), | 996 write_result_(0), |
977 read_data_(), | 997 read_data_(), |
978 read_buf_(NULL), | 998 read_buf_(NULL), |
979 read_buf_len_(0), | 999 read_buf_len_(0), |
980 read_pending_(false), | 1000 read_pending_(false), |
981 old_read_callback_(NULL), | 1001 old_read_callback_(NULL), |
982 data_(data), | 1002 data_(data), |
983 was_used_to_convey_data_(false) {} | 1003 was_used_to_convey_data_(false) {} |
984 | 1004 |
985 DeterministicMockTCPClientSocket::~DeterministicMockTCPClientSocket() {} | 1005 DeterministicMockTCPClientSocket::~DeterministicMockTCPClientSocket() {} |
986 | 1006 |
987 void DeterministicMockTCPClientSocket::CompleteWrite() { | 1007 void DeterministicMockTCPClientSocket::CompleteWrite() { |
988 was_used_to_convey_data_ = true; | 1008 was_used_to_convey_data_ = true; |
989 write_pending_ = false; | 1009 write_pending_ = false; |
990 write_callback_->Run(write_result_); | 1010 if (old_write_callback_) |
| 1011 old_write_callback_->Run(write_result_); |
| 1012 else |
| 1013 write_callback_.Run(write_result_); |
991 } | 1014 } |
992 | 1015 |
993 int DeterministicMockTCPClientSocket::CompleteRead() { | 1016 int DeterministicMockTCPClientSocket::CompleteRead() { |
994 DCHECK_GT(read_buf_len_, 0); | 1017 DCHECK_GT(read_buf_len_, 0); |
995 DCHECK_LE(read_data_.data_len, read_buf_len_); | 1018 DCHECK_LE(read_data_.data_len, read_buf_len_); |
996 DCHECK(read_buf_); | 1019 DCHECK(read_buf_); |
997 | 1020 |
998 was_used_to_convey_data_ = true; | 1021 was_used_to_convey_data_ = true; |
999 | 1022 |
1000 if (read_data_.result == ERR_IO_PENDING) | 1023 if (read_data_.result == ERR_IO_PENDING) |
(...skipping 13 matching lines...) Expand all Loading... |
1014 read_pending_ = false; | 1037 read_pending_ = false; |
1015 if (old_read_callback_) | 1038 if (old_read_callback_) |
1016 old_read_callback_->Run(result); | 1039 old_read_callback_->Run(result); |
1017 else | 1040 else |
1018 read_callback_.Run(result); | 1041 read_callback_.Run(result); |
1019 } | 1042 } |
1020 | 1043 |
1021 return result; | 1044 return result; |
1022 } | 1045 } |
1023 | 1046 |
1024 int DeterministicMockTCPClientSocket::Write( | |
1025 net::IOBuffer* buf, int buf_len, net::OldCompletionCallback* callback) { | |
1026 DCHECK(buf); | |
1027 DCHECK_GT(buf_len, 0); | |
1028 | |
1029 if (!connected_) | |
1030 return net::ERR_UNEXPECTED; | |
1031 | |
1032 std::string data(buf->data(), buf_len); | |
1033 net::MockWriteResult write_result = data_->OnWrite(data); | |
1034 | |
1035 if (write_result.async) { | |
1036 write_callback_ = callback; | |
1037 write_result_ = write_result.result; | |
1038 DCHECK(write_callback_ != NULL); | |
1039 write_pending_ = true; | |
1040 return net::ERR_IO_PENDING; | |
1041 } | |
1042 | |
1043 was_used_to_convey_data_ = true; | |
1044 write_pending_ = false; | |
1045 return write_result.result; | |
1046 } | |
1047 | |
1048 int DeterministicMockTCPClientSocket::Read( | 1047 int DeterministicMockTCPClientSocket::Read( |
1049 net::IOBuffer* buf, int buf_len, net::OldCompletionCallback* callback) { | 1048 net::IOBuffer* buf, int buf_len, net::OldCompletionCallback* callback) { |
1050 if (!connected_) | 1049 if (!connected_) |
1051 return net::ERR_UNEXPECTED; | 1050 return net::ERR_UNEXPECTED; |
1052 | 1051 |
1053 read_data_ = data_->GetNextRead(); | 1052 read_data_ = data_->GetNextRead(); |
1054 // The buffer should always be big enough to contain all the MockRead data. To | 1053 // The buffer should always be big enough to contain all the MockRead data. To |
1055 // use small buffers, split the data into multiple MockReads. | 1054 // use small buffers, split the data into multiple MockReads. |
1056 DCHECK_LE(read_data_.data_len, buf_len); | 1055 DCHECK_LE(read_data_.data_len, buf_len); |
1057 | 1056 |
(...skipping 27 matching lines...) Expand all Loading... |
1085 if (read_data_.async || (read_data_.result == ERR_IO_PENDING)) { | 1084 if (read_data_.async || (read_data_.result == ERR_IO_PENDING)) { |
1086 read_pending_ = true; | 1085 read_pending_ = true; |
1087 DCHECK(!read_callback_.is_null()); | 1086 DCHECK(!read_callback_.is_null()); |
1088 return ERR_IO_PENDING; | 1087 return ERR_IO_PENDING; |
1089 } | 1088 } |
1090 | 1089 |
1091 was_used_to_convey_data_ = true; | 1090 was_used_to_convey_data_ = true; |
1092 return CompleteRead(); | 1091 return CompleteRead(); |
1093 } | 1092 } |
1094 | 1093 |
| 1094 int DeterministicMockTCPClientSocket::Write( |
| 1095 net::IOBuffer* buf, int buf_len, net::OldCompletionCallback* callback) { |
| 1096 DCHECK(buf); |
| 1097 DCHECK_GT(buf_len, 0); |
| 1098 |
| 1099 if (!connected_) |
| 1100 return net::ERR_UNEXPECTED; |
| 1101 |
| 1102 std::string data(buf->data(), buf_len); |
| 1103 net::MockWriteResult write_result = data_->OnWrite(data); |
| 1104 |
| 1105 if (write_result.async) { |
| 1106 old_write_callback_ = callback; |
| 1107 write_result_ = write_result.result; |
| 1108 DCHECK(old_write_callback_ != NULL); |
| 1109 write_pending_ = true; |
| 1110 return net::ERR_IO_PENDING; |
| 1111 } |
| 1112 |
| 1113 was_used_to_convey_data_ = true; |
| 1114 write_pending_ = false; |
| 1115 return write_result.result; |
| 1116 } |
| 1117 int DeterministicMockTCPClientSocket::Write( |
| 1118 net::IOBuffer* buf, int buf_len, const net::CompletionCallback& callback) { |
| 1119 DCHECK(buf); |
| 1120 DCHECK_GT(buf_len, 0); |
| 1121 |
| 1122 if (!connected_) |
| 1123 return net::ERR_UNEXPECTED; |
| 1124 |
| 1125 std::string data(buf->data(), buf_len); |
| 1126 net::MockWriteResult write_result = data_->OnWrite(data); |
| 1127 |
| 1128 if (write_result.async) { |
| 1129 write_callback_ = callback; |
| 1130 write_result_ = write_result.result; |
| 1131 DCHECK(!write_callback_.is_null()); |
| 1132 write_pending_ = true; |
| 1133 return net::ERR_IO_PENDING; |
| 1134 } |
| 1135 |
| 1136 was_used_to_convey_data_ = true; |
| 1137 write_pending_ = false; |
| 1138 return write_result.result; |
| 1139 } |
| 1140 |
1095 // TODO(erikchen): Support connect sequencing. | 1141 // TODO(erikchen): Support connect sequencing. |
1096 int DeterministicMockTCPClientSocket::Connect( | 1142 int DeterministicMockTCPClientSocket::Connect( |
1097 net::OldCompletionCallback* callback) { | 1143 net::OldCompletionCallback* callback) { |
1098 if (connected_) | 1144 if (connected_) |
1099 return net::OK; | 1145 return net::OK; |
1100 connected_ = true; | 1146 connected_ = true; |
1101 if (data_->connect_data().async) { | 1147 if (data_->connect_data().async) { |
1102 RunCallbackAsync(callback, data_->connect_data().result); | 1148 RunCallbackAsync(callback, data_->connect_data().result); |
1103 return net::ERR_IO_PENDING; | 1149 return net::ERR_IO_PENDING; |
1104 } | 1150 } |
(...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1228 } | 1274 } |
1229 int MockSSLClientSocket::Read(net::IOBuffer* buf, int buf_len, | 1275 int MockSSLClientSocket::Read(net::IOBuffer* buf, int buf_len, |
1230 const net::CompletionCallback& callback) { | 1276 const net::CompletionCallback& callback) { |
1231 return transport_->socket()->Read(buf, buf_len, callback); | 1277 return transport_->socket()->Read(buf, buf_len, callback); |
1232 } | 1278 } |
1233 | 1279 |
1234 int MockSSLClientSocket::Write(net::IOBuffer* buf, int buf_len, | 1280 int MockSSLClientSocket::Write(net::IOBuffer* buf, int buf_len, |
1235 net::OldCompletionCallback* callback) { | 1281 net::OldCompletionCallback* callback) { |
1236 return transport_->socket()->Write(buf, buf_len, callback); | 1282 return transport_->socket()->Write(buf, buf_len, callback); |
1237 } | 1283 } |
| 1284 int MockSSLClientSocket::Write(net::IOBuffer* buf, int buf_len, |
| 1285 const net::CompletionCallback& callback) { |
| 1286 return transport_->socket()->Write(buf, buf_len, callback); |
| 1287 } |
1238 | 1288 |
1239 int MockSSLClientSocket::Connect(net::OldCompletionCallback* callback) { | 1289 int MockSSLClientSocket::Connect(net::OldCompletionCallback* callback) { |
1240 OldConnectCallback* connect_callback = new OldConnectCallback( | 1290 OldConnectCallback* connect_callback = new OldConnectCallback( |
1241 this, callback, data_->connect.result); | 1291 this, callback, data_->connect.result); |
1242 int rv = transport_->socket()->Connect(connect_callback); | 1292 int rv = transport_->socket()->Connect(connect_callback); |
1243 if (rv == net::OK) { | 1293 if (rv == net::OK) { |
1244 delete connect_callback; | 1294 delete connect_callback; |
1245 if (data_->connect.result == net::OK) | 1295 if (data_->connect.result == net::OK) |
1246 connected_ = true; | 1296 connected_ = true; |
1247 if (data_->connect.async) { | 1297 if (data_->connect.async) { |
(...skipping 167 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1415 | 1465 |
1416 std::string data(buf->data(), buf_len); | 1466 std::string data(buf->data(), buf_len); |
1417 MockWriteResult write_result = data_->OnWrite(data); | 1467 MockWriteResult write_result = data_->OnWrite(data); |
1418 | 1468 |
1419 if (write_result.async) { | 1469 if (write_result.async) { |
1420 RunCallbackAsync(callback, write_result.result); | 1470 RunCallbackAsync(callback, write_result.result); |
1421 return ERR_IO_PENDING; | 1471 return ERR_IO_PENDING; |
1422 } | 1472 } |
1423 return write_result.result; | 1473 return write_result.result; |
1424 } | 1474 } |
| 1475 int MockUDPClientSocket::Write(net::IOBuffer* buf, int buf_len, |
| 1476 const net::CompletionCallback& callback) { |
| 1477 DCHECK(buf); |
| 1478 DCHECK_GT(buf_len, 0); |
| 1479 |
| 1480 if (!connected_) |
| 1481 return ERR_UNEXPECTED; |
| 1482 |
| 1483 std::string data(buf->data(), buf_len); |
| 1484 MockWriteResult write_result = data_->OnWrite(data); |
| 1485 |
| 1486 if (write_result.async) { |
| 1487 RunCallbackAsync(callback, write_result.result); |
| 1488 return ERR_IO_PENDING; |
| 1489 } |
| 1490 return write_result.result; |
| 1491 } |
1425 | 1492 |
1426 bool MockUDPClientSocket::SetReceiveBufferSize(int32 size) { | 1493 bool MockUDPClientSocket::SetReceiveBufferSize(int32 size) { |
1427 return true; | 1494 return true; |
1428 } | 1495 } |
1429 | 1496 |
1430 bool MockUDPClientSocket::SetSendBufferSize(int32 size) { | 1497 bool MockUDPClientSocket::SetSendBufferSize(int32 size) { |
1431 return true; | 1498 return true; |
1432 } | 1499 } |
1433 | 1500 |
1434 void MockUDPClientSocket::Close() { | 1501 void MockUDPClientSocket::Close() { |
(...skipping 380 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1815 | 1882 |
1816 const char kSOCKS5OkRequest[] = | 1883 const char kSOCKS5OkRequest[] = |
1817 { 0x05, 0x01, 0x00, 0x03, 0x04, 'h', 'o', 's', 't', 0x00, 0x50 }; | 1884 { 0x05, 0x01, 0x00, 0x03, 0x04, 'h', 'o', 's', 't', 0x00, 0x50 }; |
1818 const int kSOCKS5OkRequestLength = arraysize(kSOCKS5OkRequest); | 1885 const int kSOCKS5OkRequestLength = arraysize(kSOCKS5OkRequest); |
1819 | 1886 |
1820 const char kSOCKS5OkResponse[] = | 1887 const char kSOCKS5OkResponse[] = |
1821 { 0x05, 0x00, 0x00, 0x01, 127, 0, 0, 1, 0x00, 0x50 }; | 1888 { 0x05, 0x00, 0x00, 0x01, 127, 0, 0, 1, 0x00, 0x50 }; |
1822 const int kSOCKS5OkResponseLength = arraysize(kSOCKS5OkResponse); | 1889 const int kSOCKS5OkResponseLength = arraysize(kSOCKS5OkResponse); |
1823 | 1890 |
1824 } // namespace net | 1891 } // namespace net |
OLD | NEW |