| 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 } | |
| 855 | 835 |
| 856 int MockTCPClientSocket::Connect(net::OldCompletionCallback* callback) { | 836 int MockTCPClientSocket::Connect(net::OldCompletionCallback* callback) { |
| 857 if (connected_) | 837 if (connected_) |
| 858 return net::OK; | 838 return net::OK; |
| 859 connected_ = true; | 839 connected_ = true; |
| 860 peer_closed_connection_ = false; | 840 peer_closed_connection_ = false; |
| 861 if (data_->connect_data().async) { | 841 if (data_->connect_data().async) { |
| 862 RunCallbackAsync(callback, data_->connect_data().result); | 842 RunCallbackAsync(callback, data_->connect_data().result); |
| 863 return net::ERR_IO_PENDING; | 843 return net::ERR_IO_PENDING; |
| 864 } | 844 } |
| (...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 985 RunCallbackAsync(callback, result); | 965 RunCallbackAsync(callback, result); |
| 986 return net::ERR_IO_PENDING; | 966 return net::ERR_IO_PENDING; |
| 987 } | 967 } |
| 988 return result; | 968 return result; |
| 989 } | 969 } |
| 990 | 970 |
| 991 DeterministicMockTCPClientSocket::DeterministicMockTCPClientSocket( | 971 DeterministicMockTCPClientSocket::DeterministicMockTCPClientSocket( |
| 992 net::NetLog* net_log, net::DeterministicSocketData* data) | 972 net::NetLog* net_log, net::DeterministicSocketData* data) |
| 993 : MockClientSocket(net_log), | 973 : MockClientSocket(net_log), |
| 994 write_pending_(false), | 974 write_pending_(false), |
| 995 old_write_callback_(NULL), | 975 write_callback_(NULL), |
| 996 write_result_(0), | 976 write_result_(0), |
| 997 read_data_(), | 977 read_data_(), |
| 998 read_buf_(NULL), | 978 read_buf_(NULL), |
| 999 read_buf_len_(0), | 979 read_buf_len_(0), |
| 1000 read_pending_(false), | 980 read_pending_(false), |
| 1001 old_read_callback_(NULL), | 981 old_read_callback_(NULL), |
| 1002 data_(data), | 982 data_(data), |
| 1003 was_used_to_convey_data_(false) {} | 983 was_used_to_convey_data_(false) {} |
| 1004 | 984 |
| 1005 DeterministicMockTCPClientSocket::~DeterministicMockTCPClientSocket() {} | 985 DeterministicMockTCPClientSocket::~DeterministicMockTCPClientSocket() {} |
| 1006 | 986 |
| 1007 void DeterministicMockTCPClientSocket::CompleteWrite() { | 987 void DeterministicMockTCPClientSocket::CompleteWrite() { |
| 1008 was_used_to_convey_data_ = true; | 988 was_used_to_convey_data_ = true; |
| 1009 write_pending_ = false; | 989 write_pending_ = false; |
| 1010 if (old_write_callback_) | 990 write_callback_->Run(write_result_); |
| 1011 old_write_callback_->Run(write_result_); | |
| 1012 else | |
| 1013 write_callback_.Run(write_result_); | |
| 1014 } | 991 } |
| 1015 | 992 |
| 1016 int DeterministicMockTCPClientSocket::CompleteRead() { | 993 int DeterministicMockTCPClientSocket::CompleteRead() { |
| 1017 DCHECK_GT(read_buf_len_, 0); | 994 DCHECK_GT(read_buf_len_, 0); |
| 1018 DCHECK_LE(read_data_.data_len, read_buf_len_); | 995 DCHECK_LE(read_data_.data_len, read_buf_len_); |
| 1019 DCHECK(read_buf_); | 996 DCHECK(read_buf_); |
| 1020 | 997 |
| 1021 was_used_to_convey_data_ = true; | 998 was_used_to_convey_data_ = true; |
| 1022 | 999 |
| 1023 if (read_data_.result == ERR_IO_PENDING) | 1000 if (read_data_.result == ERR_IO_PENDING) |
| (...skipping 13 matching lines...) Expand all Loading... |
| 1037 read_pending_ = false; | 1014 read_pending_ = false; |
| 1038 if (old_read_callback_) | 1015 if (old_read_callback_) |
| 1039 old_read_callback_->Run(result); | 1016 old_read_callback_->Run(result); |
| 1040 else | 1017 else |
| 1041 read_callback_.Run(result); | 1018 read_callback_.Run(result); |
| 1042 } | 1019 } |
| 1043 | 1020 |
| 1044 return result; | 1021 return result; |
| 1045 } | 1022 } |
| 1046 | 1023 |
| 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 |
| 1047 int DeterministicMockTCPClientSocket::Read( | 1048 int DeterministicMockTCPClientSocket::Read( |
| 1048 net::IOBuffer* buf, int buf_len, net::OldCompletionCallback* callback) { | 1049 net::IOBuffer* buf, int buf_len, net::OldCompletionCallback* callback) { |
| 1049 if (!connected_) | 1050 if (!connected_) |
| 1050 return net::ERR_UNEXPECTED; | 1051 return net::ERR_UNEXPECTED; |
| 1051 | 1052 |
| 1052 read_data_ = data_->GetNextRead(); | 1053 read_data_ = data_->GetNextRead(); |
| 1053 // The buffer should always be big enough to contain all the MockRead data. To | 1054 // The buffer should always be big enough to contain all the MockRead data. To |
| 1054 // use small buffers, split the data into multiple MockReads. | 1055 // use small buffers, split the data into multiple MockReads. |
| 1055 DCHECK_LE(read_data_.data_len, buf_len); | 1056 DCHECK_LE(read_data_.data_len, buf_len); |
| 1056 | 1057 |
| (...skipping 27 matching lines...) Expand all Loading... |
| 1084 if (read_data_.async || (read_data_.result == ERR_IO_PENDING)) { | 1085 if (read_data_.async || (read_data_.result == ERR_IO_PENDING)) { |
| 1085 read_pending_ = true; | 1086 read_pending_ = true; |
| 1086 DCHECK(!read_callback_.is_null()); | 1087 DCHECK(!read_callback_.is_null()); |
| 1087 return ERR_IO_PENDING; | 1088 return ERR_IO_PENDING; |
| 1088 } | 1089 } |
| 1089 | 1090 |
| 1090 was_used_to_convey_data_ = true; | 1091 was_used_to_convey_data_ = true; |
| 1091 return CompleteRead(); | 1092 return CompleteRead(); |
| 1092 } | 1093 } |
| 1093 | 1094 |
| 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 | |
| 1141 // TODO(erikchen): Support connect sequencing. | 1095 // TODO(erikchen): Support connect sequencing. |
| 1142 int DeterministicMockTCPClientSocket::Connect( | 1096 int DeterministicMockTCPClientSocket::Connect( |
| 1143 net::OldCompletionCallback* callback) { | 1097 net::OldCompletionCallback* callback) { |
| 1144 if (connected_) | 1098 if (connected_) |
| 1145 return net::OK; | 1099 return net::OK; |
| 1146 connected_ = true; | 1100 connected_ = true; |
| 1147 if (data_->connect_data().async) { | 1101 if (data_->connect_data().async) { |
| 1148 RunCallbackAsync(callback, data_->connect_data().result); | 1102 RunCallbackAsync(callback, data_->connect_data().result); |
| 1149 return net::ERR_IO_PENDING; | 1103 return net::ERR_IO_PENDING; |
| 1150 } | 1104 } |
| (...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1274 } | 1228 } |
| 1275 int MockSSLClientSocket::Read(net::IOBuffer* buf, int buf_len, | 1229 int MockSSLClientSocket::Read(net::IOBuffer* buf, int buf_len, |
| 1276 const net::CompletionCallback& callback) { | 1230 const net::CompletionCallback& callback) { |
| 1277 return transport_->socket()->Read(buf, buf_len, callback); | 1231 return transport_->socket()->Read(buf, buf_len, callback); |
| 1278 } | 1232 } |
| 1279 | 1233 |
| 1280 int MockSSLClientSocket::Write(net::IOBuffer* buf, int buf_len, | 1234 int MockSSLClientSocket::Write(net::IOBuffer* buf, int buf_len, |
| 1281 net::OldCompletionCallback* callback) { | 1235 net::OldCompletionCallback* callback) { |
| 1282 return transport_->socket()->Write(buf, buf_len, callback); | 1236 return transport_->socket()->Write(buf, buf_len, callback); |
| 1283 } | 1237 } |
| 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 } | |
| 1288 | 1238 |
| 1289 int MockSSLClientSocket::Connect(net::OldCompletionCallback* callback) { | 1239 int MockSSLClientSocket::Connect(net::OldCompletionCallback* callback) { |
| 1290 OldConnectCallback* connect_callback = new OldConnectCallback( | 1240 OldConnectCallback* connect_callback = new OldConnectCallback( |
| 1291 this, callback, data_->connect.result); | 1241 this, callback, data_->connect.result); |
| 1292 int rv = transport_->socket()->Connect(connect_callback); | 1242 int rv = transport_->socket()->Connect(connect_callback); |
| 1293 if (rv == net::OK) { | 1243 if (rv == net::OK) { |
| 1294 delete connect_callback; | 1244 delete connect_callback; |
| 1295 if (data_->connect.result == net::OK) | 1245 if (data_->connect.result == net::OK) |
| 1296 connected_ = true; | 1246 connected_ = true; |
| 1297 if (data_->connect.async) { | 1247 if (data_->connect.async) { |
| (...skipping 167 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1465 | 1415 |
| 1466 std::string data(buf->data(), buf_len); | 1416 std::string data(buf->data(), buf_len); |
| 1467 MockWriteResult write_result = data_->OnWrite(data); | 1417 MockWriteResult write_result = data_->OnWrite(data); |
| 1468 | 1418 |
| 1469 if (write_result.async) { | 1419 if (write_result.async) { |
| 1470 RunCallbackAsync(callback, write_result.result); | 1420 RunCallbackAsync(callback, write_result.result); |
| 1471 return ERR_IO_PENDING; | 1421 return ERR_IO_PENDING; |
| 1472 } | 1422 } |
| 1473 return write_result.result; | 1423 return write_result.result; |
| 1474 } | 1424 } |
| 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 } | |
| 1492 | 1425 |
| 1493 bool MockUDPClientSocket::SetReceiveBufferSize(int32 size) { | 1426 bool MockUDPClientSocket::SetReceiveBufferSize(int32 size) { |
| 1494 return true; | 1427 return true; |
| 1495 } | 1428 } |
| 1496 | 1429 |
| 1497 bool MockUDPClientSocket::SetSendBufferSize(int32 size) { | 1430 bool MockUDPClientSocket::SetSendBufferSize(int32 size) { |
| 1498 return true; | 1431 return true; |
| 1499 } | 1432 } |
| 1500 | 1433 |
| 1501 void MockUDPClientSocket::Close() { | 1434 void MockUDPClientSocket::Close() { |
| (...skipping 380 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1882 | 1815 |
| 1883 const char kSOCKS5OkRequest[] = | 1816 const char kSOCKS5OkRequest[] = |
| 1884 { 0x05, 0x01, 0x00, 0x03, 0x04, 'h', 'o', 's', 't', 0x00, 0x50 }; | 1817 { 0x05, 0x01, 0x00, 0x03, 0x04, 'h', 'o', 's', 't', 0x00, 0x50 }; |
| 1885 const int kSOCKS5OkRequestLength = arraysize(kSOCKS5OkRequest); | 1818 const int kSOCKS5OkRequestLength = arraysize(kSOCKS5OkRequest); |
| 1886 | 1819 |
| 1887 const char kSOCKS5OkResponse[] = | 1820 const char kSOCKS5OkResponse[] = |
| 1888 { 0x05, 0x00, 0x00, 0x01, 127, 0, 0, 1, 0x00, 0x50 }; | 1821 { 0x05, 0x00, 0x00, 0x01, 127, 0, 0, 1, 0x00, 0x50 }; |
| 1889 const int kSOCKS5OkResponseLength = arraysize(kSOCKS5OkResponse); | 1822 const int kSOCKS5OkResponseLength = arraysize(kSOCKS5OkResponse); |
| 1890 | 1823 |
| 1891 } // namespace net | 1824 } // namespace net |
| OLD | NEW |