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 720 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
731 : MockClientSocket(net_log), | 731 : MockClientSocket(net_log), |
732 addresses_(addresses), | 732 addresses_(addresses), |
733 data_(data), | 733 data_(data), |
734 read_offset_(0), | 734 read_offset_(0), |
735 num_bytes_read_(0), | 735 num_bytes_read_(0), |
736 read_data_(false, net::ERR_UNEXPECTED), | 736 read_data_(false, net::ERR_UNEXPECTED), |
737 need_read_data_(true), | 737 need_read_data_(true), |
738 peer_closed_connection_(false), | 738 peer_closed_connection_(false), |
739 pending_buf_(NULL), | 739 pending_buf_(NULL), |
740 pending_buf_len_(0), | 740 pending_buf_len_(0), |
741 pending_callback_(NULL), | 741 old_pending_callback_(NULL), |
742 was_used_to_convey_data_(false) { | 742 was_used_to_convey_data_(false) { |
743 DCHECK(data_); | 743 DCHECK(data_); |
744 data_->Reset(); | 744 data_->Reset(); |
745 } | 745 } |
746 | 746 |
| 747 MockTCPClientSocket::~MockTCPClientSocket() {} |
| 748 |
747 int MockTCPClientSocket::Read(net::IOBuffer* buf, int buf_len, | 749 int MockTCPClientSocket::Read(net::IOBuffer* buf, int buf_len, |
748 net::OldCompletionCallback* callback) { | 750 net::OldCompletionCallback* callback) { |
749 if (!connected_) | 751 if (!connected_) |
750 return net::ERR_UNEXPECTED; | 752 return net::ERR_UNEXPECTED; |
751 | 753 |
752 // If the buffer is already in use, a read is already in progress! | 754 // If the buffer is already in use, a read is already in progress! |
753 DCHECK(pending_buf_ == NULL); | 755 DCHECK(pending_buf_ == NULL); |
754 | 756 |
755 // Store our async IO data. | 757 // Store our async IO data. |
756 pending_buf_ = buf; | 758 pending_buf_ = buf; |
757 pending_buf_len_ = buf_len; | 759 pending_buf_len_ = buf_len; |
758 pending_callback_ = callback; | 760 old_pending_callback_ = callback; |
759 | 761 |
760 if (need_read_data_) { | 762 if (need_read_data_) { |
761 read_data_ = data_->GetNextRead(); | 763 read_data_ = data_->GetNextRead(); |
762 if (read_data_.result == ERR_TEST_PEER_CLOSE_AFTER_NEXT_MOCK_READ) { | 764 if (read_data_.result == ERR_TEST_PEER_CLOSE_AFTER_NEXT_MOCK_READ) { |
763 // This MockRead is just a marker to instruct us to set | 765 // This MockRead is just a marker to instruct us to set |
764 // peer_closed_connection_. Skip it and get the next one. | 766 // peer_closed_connection_. Skip it and get the next one. |
765 read_data_ = data_->GetNextRead(); | 767 read_data_ = data_->GetNextRead(); |
766 peer_closed_connection_ = true; | 768 peer_closed_connection_ = true; |
767 } | 769 } |
768 // ERR_IO_PENDING means that the SocketDataProvider is taking responsibility | 770 // ERR_IO_PENDING means that the SocketDataProvider is taking responsibility |
769 // to complete the async IO manually later (via OnReadComplete). | 771 // to complete the async IO manually later (via OnReadComplete). |
770 if (read_data_.result == ERR_IO_PENDING) { | 772 if (read_data_.result == ERR_IO_PENDING) { |
771 DCHECK(callback); // We need to be using async IO in this case. | 773 DCHECK(callback); // We need to be using async IO in this case. |
772 return ERR_IO_PENDING; | 774 return ERR_IO_PENDING; |
773 } | 775 } |
774 need_read_data_ = false; | 776 need_read_data_ = false; |
775 } | 777 } |
776 | 778 |
777 return CompleteRead(); | 779 return CompleteRead(); |
778 } | 780 } |
| 781 int MockTCPClientSocket::Read(net::IOBuffer* buf, int buf_len, |
| 782 const net::CompletionCallback& callback) { |
| 783 if (!connected_) |
| 784 return net::ERR_UNEXPECTED; |
| 785 |
| 786 // If the buffer is already in use, a read is already in progress! |
| 787 DCHECK(pending_buf_ == NULL); |
| 788 |
| 789 // Store our async IO data. |
| 790 pending_buf_ = buf; |
| 791 pending_buf_len_ = buf_len; |
| 792 pending_callback_ = callback; |
| 793 |
| 794 if (need_read_data_) { |
| 795 read_data_ = data_->GetNextRead(); |
| 796 if (read_data_.result == ERR_TEST_PEER_CLOSE_AFTER_NEXT_MOCK_READ) { |
| 797 // This MockRead is just a marker to instruct us to set |
| 798 // peer_closed_connection_. Skip it and get the next one. |
| 799 read_data_ = data_->GetNextRead(); |
| 800 peer_closed_connection_ = true; |
| 801 } |
| 802 // ERR_IO_PENDING means that the SocketDataProvider is taking responsibility |
| 803 // to complete the async IO manually later (via OnReadComplete). |
| 804 if (read_data_.result == ERR_IO_PENDING) { |
| 805 // We need to be using async IO in this case. |
| 806 DCHECK(!callback.is_null()); |
| 807 return ERR_IO_PENDING; |
| 808 } |
| 809 need_read_data_ = false; |
| 810 } |
| 811 |
| 812 return CompleteRead(); |
| 813 } |
779 | 814 |
780 int MockTCPClientSocket::Write(net::IOBuffer* buf, int buf_len, | 815 int MockTCPClientSocket::Write(net::IOBuffer* buf, int buf_len, |
781 net::OldCompletionCallback* callback) { | 816 net::OldCompletionCallback* callback) { |
782 DCHECK(buf); | 817 DCHECK(buf); |
783 DCHECK_GT(buf_len, 0); | 818 DCHECK_GT(buf_len, 0); |
784 | 819 |
785 if (!connected_) | 820 if (!connected_) |
786 return net::ERR_UNEXPECTED; | 821 return net::ERR_UNEXPECTED; |
787 | 822 |
788 std::string data(buf->data(), buf_len); | 823 std::string data(buf->data(), buf_len); |
(...skipping 29 matching lines...) Expand all Loading... |
818 if (data_->connect_data().async) { | 853 if (data_->connect_data().async) { |
819 RunCallbackAsync(callback, data_->connect_data().result); | 854 RunCallbackAsync(callback, data_->connect_data().result); |
820 return net::ERR_IO_PENDING; | 855 return net::ERR_IO_PENDING; |
821 } | 856 } |
822 | 857 |
823 return data_->connect_data().result; | 858 return data_->connect_data().result; |
824 } | 859 } |
825 | 860 |
826 void MockTCPClientSocket::Disconnect() { | 861 void MockTCPClientSocket::Disconnect() { |
827 MockClientSocket::Disconnect(); | 862 MockClientSocket::Disconnect(); |
828 pending_callback_ = NULL; | 863 old_pending_callback_ = NULL; |
| 864 pending_callback_.Reset(); |
829 } | 865 } |
830 | 866 |
831 bool MockTCPClientSocket::IsConnected() const { | 867 bool MockTCPClientSocket::IsConnected() const { |
832 return connected_ && !peer_closed_connection_; | 868 return connected_ && !peer_closed_connection_; |
833 } | 869 } |
834 | 870 |
835 bool MockTCPClientSocket::IsConnectedAndIdle() const { | 871 bool MockTCPClientSocket::IsConnectedAndIdle() const { |
836 return IsConnected(); | 872 return IsConnected(); |
837 } | 873 } |
838 | 874 |
(...skipping 30 matching lines...) Expand all Loading... |
869 // Since we've been waiting for data, need_read_data_ should be true. | 905 // Since we've been waiting for data, need_read_data_ should be true. |
870 DCHECK(need_read_data_); | 906 DCHECK(need_read_data_); |
871 | 907 |
872 read_data_ = data; | 908 read_data_ = data; |
873 need_read_data_ = false; | 909 need_read_data_ = false; |
874 | 910 |
875 // The caller is simulating that this IO completes right now. Don't | 911 // The caller is simulating that this IO completes right now. Don't |
876 // let CompleteRead() schedule a callback. | 912 // let CompleteRead() schedule a callback. |
877 read_data_.async = false; | 913 read_data_.async = false; |
878 | 914 |
879 net::OldCompletionCallback* callback = pending_callback_; | 915 if (old_pending_callback_) { |
880 int rv = CompleteRead(); | 916 net::OldCompletionCallback* callback = old_pending_callback_; |
881 RunOldCallback(callback, rv); | 917 int rv = CompleteRead(); |
| 918 RunOldCallback(callback, rv); |
| 919 } else { |
| 920 net::CompletionCallback callback = pending_callback_; |
| 921 int rv = CompleteRead(); |
| 922 RunCallback(callback, rv); |
| 923 } |
882 } | 924 } |
883 | 925 |
884 int MockTCPClientSocket::CompleteRead() { | 926 int MockTCPClientSocket::CompleteRead() { |
885 DCHECK(pending_buf_); | 927 DCHECK(pending_buf_); |
886 DCHECK(pending_buf_len_ > 0); | 928 DCHECK(pending_buf_len_ > 0); |
887 | 929 |
888 was_used_to_convey_data_ = true; | 930 was_used_to_convey_data_ = true; |
889 | 931 |
890 // Save the pending async IO data and reset our |pending_| state. | 932 // Save the pending async IO data and reset our |pending_| state. |
891 net::IOBuffer* buf = pending_buf_; | 933 net::IOBuffer* buf = pending_buf_; |
892 int buf_len = pending_buf_len_; | 934 int buf_len = pending_buf_len_; |
893 net::OldCompletionCallback* callback = pending_callback_; | 935 net::OldCompletionCallback* old_callback = old_pending_callback_; |
| 936 net::CompletionCallback callback = pending_callback_; |
894 pending_buf_ = NULL; | 937 pending_buf_ = NULL; |
895 pending_buf_len_ = 0; | 938 pending_buf_len_ = 0; |
896 pending_callback_ = NULL; | 939 old_pending_callback_ = NULL; |
| 940 pending_callback_.Reset(); |
897 | 941 |
898 int result = read_data_.result; | 942 int result = read_data_.result; |
899 DCHECK(result != ERR_IO_PENDING); | 943 DCHECK(result != ERR_IO_PENDING); |
900 | 944 |
901 if (read_data_.data) { | 945 if (read_data_.data) { |
902 if (read_data_.data_len - read_offset_ > 0) { | 946 if (read_data_.data_len - read_offset_ > 0) { |
903 result = std::min(buf_len, read_data_.data_len - read_offset_); | 947 result = std::min(buf_len, read_data_.data_len - read_offset_); |
904 memcpy(buf->data(), read_data_.data + read_offset_, result); | 948 memcpy(buf->data(), read_data_.data + read_offset_, result); |
905 read_offset_ += result; | 949 read_offset_ += result; |
906 num_bytes_read_ += result; | 950 num_bytes_read_ += result; |
907 if (read_offset_ == read_data_.data_len) { | 951 if (read_offset_ == read_data_.data_len) { |
908 need_read_data_ = true; | 952 need_read_data_ = true; |
909 read_offset_ = 0; | 953 read_offset_ = 0; |
910 } | 954 } |
911 } else { | 955 } else { |
912 result = 0; // EOF | 956 result = 0; // EOF |
913 } | 957 } |
914 } | 958 } |
915 | 959 |
916 if (read_data_.async) { | 960 if (read_data_.async) { |
917 DCHECK(callback); | 961 DCHECK(old_callback || !callback.is_null()); |
918 RunCallbackAsync(callback, result); | 962 if (old_callback) |
| 963 RunCallbackAsync(old_callback, result); |
| 964 else |
| 965 RunCallbackAsync(callback, result); |
919 return net::ERR_IO_PENDING; | 966 return net::ERR_IO_PENDING; |
920 } | 967 } |
921 return result; | 968 return result; |
922 } | 969 } |
923 | 970 |
924 DeterministicMockTCPClientSocket::DeterministicMockTCPClientSocket( | 971 DeterministicMockTCPClientSocket::DeterministicMockTCPClientSocket( |
925 net::NetLog* net_log, net::DeterministicSocketData* data) | 972 net::NetLog* net_log, net::DeterministicSocketData* data) |
926 : MockClientSocket(net_log), | 973 : MockClientSocket(net_log), |
927 write_pending_(false), | 974 write_pending_(false), |
928 write_callback_(NULL), | 975 write_callback_(NULL), |
929 write_result_(0), | 976 write_result_(0), |
930 read_data_(), | 977 read_data_(), |
931 read_buf_(NULL), | 978 read_buf_(NULL), |
932 read_buf_len_(0), | 979 read_buf_len_(0), |
933 read_pending_(false), | 980 read_pending_(false), |
934 read_callback_(NULL), | 981 old_read_callback_(NULL), |
935 data_(data), | 982 data_(data), |
936 was_used_to_convey_data_(false) {} | 983 was_used_to_convey_data_(false) {} |
937 | 984 |
938 DeterministicMockTCPClientSocket::~DeterministicMockTCPClientSocket() {} | 985 DeterministicMockTCPClientSocket::~DeterministicMockTCPClientSocket() {} |
939 | 986 |
940 void DeterministicMockTCPClientSocket::CompleteWrite() { | 987 void DeterministicMockTCPClientSocket::CompleteWrite() { |
941 was_used_to_convey_data_ = true; | 988 was_used_to_convey_data_ = true; |
942 write_pending_ = false; | 989 write_pending_ = false; |
943 write_callback_->Run(write_result_); | 990 write_callback_->Run(write_result_); |
944 } | 991 } |
(...skipping 13 matching lines...) Expand all Loading... |
958 int result = read_data_.result; | 1005 int result = read_data_.result; |
959 | 1006 |
960 if (read_data_.data_len > 0) { | 1007 if (read_data_.data_len > 0) { |
961 DCHECK(read_data_.data); | 1008 DCHECK(read_data_.data); |
962 result = std::min(read_buf_len_, read_data_.data_len); | 1009 result = std::min(read_buf_len_, read_data_.data_len); |
963 memcpy(read_buf_->data(), read_data_.data, result); | 1010 memcpy(read_buf_->data(), read_data_.data, result); |
964 } | 1011 } |
965 | 1012 |
966 if (read_pending_) { | 1013 if (read_pending_) { |
967 read_pending_ = false; | 1014 read_pending_ = false; |
968 read_callback_->Run(result); | 1015 if (old_read_callback_) |
| 1016 old_read_callback_->Run(result); |
| 1017 else |
| 1018 read_callback_.Run(result); |
969 } | 1019 } |
970 | 1020 |
971 return result; | 1021 return result; |
972 } | 1022 } |
973 | 1023 |
974 int DeterministicMockTCPClientSocket::Write( | 1024 int DeterministicMockTCPClientSocket::Write( |
975 net::IOBuffer* buf, int buf_len, net::OldCompletionCallback* callback) { | 1025 net::IOBuffer* buf, int buf_len, net::OldCompletionCallback* callback) { |
976 DCHECK(buf); | 1026 DCHECK(buf); |
977 DCHECK_GT(buf_len, 0); | 1027 DCHECK_GT(buf_len, 0); |
978 | 1028 |
(...skipping 21 matching lines...) Expand all Loading... |
1000 if (!connected_) | 1050 if (!connected_) |
1001 return net::ERR_UNEXPECTED; | 1051 return net::ERR_UNEXPECTED; |
1002 | 1052 |
1003 read_data_ = data_->GetNextRead(); | 1053 read_data_ = data_->GetNextRead(); |
1004 // 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 |
1005 // use small buffers, split the data into multiple MockReads. | 1055 // use small buffers, split the data into multiple MockReads. |
1006 DCHECK_LE(read_data_.data_len, buf_len); | 1056 DCHECK_LE(read_data_.data_len, buf_len); |
1007 | 1057 |
1008 read_buf_ = buf; | 1058 read_buf_ = buf; |
1009 read_buf_len_ = buf_len; | 1059 read_buf_len_ = buf_len; |
| 1060 old_read_callback_ = callback; |
| 1061 |
| 1062 if (read_data_.async || (read_data_.result == ERR_IO_PENDING)) { |
| 1063 read_pending_ = true; |
| 1064 DCHECK(old_read_callback_); |
| 1065 return ERR_IO_PENDING; |
| 1066 } |
| 1067 |
| 1068 was_used_to_convey_data_ = true; |
| 1069 return CompleteRead(); |
| 1070 } |
| 1071 int DeterministicMockTCPClientSocket::Read( |
| 1072 net::IOBuffer* buf, int buf_len, const net::CompletionCallback& callback) { |
| 1073 if (!connected_) |
| 1074 return net::ERR_UNEXPECTED; |
| 1075 |
| 1076 read_data_ = data_->GetNextRead(); |
| 1077 // The buffer should always be big enough to contain all the MockRead data. To |
| 1078 // use small buffers, split the data into multiple MockReads. |
| 1079 DCHECK_LE(read_data_.data_len, buf_len); |
| 1080 |
| 1081 read_buf_ = buf; |
| 1082 read_buf_len_ = buf_len; |
1010 read_callback_ = callback; | 1083 read_callback_ = callback; |
1011 | 1084 |
1012 if (read_data_.async || (read_data_.result == ERR_IO_PENDING)) { | 1085 if (read_data_.async || (read_data_.result == ERR_IO_PENDING)) { |
1013 read_pending_ = true; | 1086 read_pending_ = true; |
1014 DCHECK(read_callback_); | 1087 DCHECK(!read_callback_.is_null()); |
1015 return ERR_IO_PENDING; | 1088 return ERR_IO_PENDING; |
1016 } | 1089 } |
1017 | 1090 |
1018 was_used_to_convey_data_ = true; | 1091 was_used_to_convey_data_ = true; |
1019 return CompleteRead(); | 1092 return CompleteRead(); |
1020 } | 1093 } |
1021 | 1094 |
1022 // TODO(erikchen): Support connect sequencing. | 1095 // TODO(erikchen): Support connect sequencing. |
1023 int DeterministicMockTCPClientSocket::Connect( | 1096 int DeterministicMockTCPClientSocket::Connect( |
1024 net::OldCompletionCallback* callback) { | 1097 net::OldCompletionCallback* callback) { |
(...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1146 } | 1219 } |
1147 | 1220 |
1148 MockSSLClientSocket::~MockSSLClientSocket() { | 1221 MockSSLClientSocket::~MockSSLClientSocket() { |
1149 Disconnect(); | 1222 Disconnect(); |
1150 } | 1223 } |
1151 | 1224 |
1152 int MockSSLClientSocket::Read(net::IOBuffer* buf, int buf_len, | 1225 int MockSSLClientSocket::Read(net::IOBuffer* buf, int buf_len, |
1153 net::OldCompletionCallback* callback) { | 1226 net::OldCompletionCallback* callback) { |
1154 return transport_->socket()->Read(buf, buf_len, callback); | 1227 return transport_->socket()->Read(buf, buf_len, callback); |
1155 } | 1228 } |
| 1229 int MockSSLClientSocket::Read(net::IOBuffer* buf, int buf_len, |
| 1230 const net::CompletionCallback& callback) { |
| 1231 return transport_->socket()->Read(buf, buf_len, callback); |
| 1232 } |
1156 | 1233 |
1157 int MockSSLClientSocket::Write(net::IOBuffer* buf, int buf_len, | 1234 int MockSSLClientSocket::Write(net::IOBuffer* buf, int buf_len, |
1158 net::OldCompletionCallback* callback) { | 1235 net::OldCompletionCallback* callback) { |
1159 return transport_->socket()->Write(buf, buf_len, callback); | 1236 return transport_->socket()->Write(buf, buf_len, callback); |
1160 } | 1237 } |
1161 | 1238 |
1162 int MockSSLClientSocket::Connect(net::OldCompletionCallback* callback) { | 1239 int MockSSLClientSocket::Connect(net::OldCompletionCallback* callback) { |
1163 OldConnectCallback* connect_callback = new OldConnectCallback( | 1240 OldConnectCallback* connect_callback = new OldConnectCallback( |
1164 this, callback, data_->connect.result); | 1241 this, callback, data_->connect.result); |
1165 int rv = transport_->socket()->Connect(connect_callback); | 1242 int rv = transport_->socket()->Connect(connect_callback); |
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1258 | 1335 |
1259 MockUDPClientSocket::MockUDPClientSocket(SocketDataProvider* data, | 1336 MockUDPClientSocket::MockUDPClientSocket(SocketDataProvider* data, |
1260 net::NetLog* net_log) | 1337 net::NetLog* net_log) |
1261 : connected_(false), | 1338 : connected_(false), |
1262 data_(data), | 1339 data_(data), |
1263 read_offset_(0), | 1340 read_offset_(0), |
1264 read_data_(false, net::ERR_UNEXPECTED), | 1341 read_data_(false, net::ERR_UNEXPECTED), |
1265 need_read_data_(true), | 1342 need_read_data_(true), |
1266 pending_buf_(NULL), | 1343 pending_buf_(NULL), |
1267 pending_buf_len_(0), | 1344 pending_buf_len_(0), |
1268 pending_callback_(NULL), | 1345 old_pending_callback_(NULL), |
1269 net_log_(NetLog::Source(), net_log), | 1346 net_log_(NetLog::Source(), net_log), |
1270 ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)) { | 1347 ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)) { |
1271 DCHECK(data_); | 1348 DCHECK(data_); |
1272 data_->Reset(); | 1349 data_->Reset(); |
1273 } | 1350 } |
1274 | 1351 |
1275 MockUDPClientSocket::~MockUDPClientSocket() {} | 1352 MockUDPClientSocket::~MockUDPClientSocket() {} |
1276 | 1353 |
1277 int MockUDPClientSocket::Read(net::IOBuffer* buf, int buf_len, | 1354 int MockUDPClientSocket::Read(net::IOBuffer* buf, int buf_len, |
1278 net::OldCompletionCallback* callback) { | 1355 net::OldCompletionCallback* callback) { |
1279 if (!connected_) | 1356 if (!connected_) |
1280 return net::ERR_UNEXPECTED; | 1357 return net::ERR_UNEXPECTED; |
1281 | 1358 |
1282 // If the buffer is already in use, a read is already in progress! | 1359 // If the buffer is already in use, a read is already in progress! |
1283 DCHECK(pending_buf_ == NULL); | 1360 DCHECK(pending_buf_ == NULL); |
1284 | 1361 |
1285 // Store our async IO data. | 1362 // Store our async IO data. |
1286 pending_buf_ = buf; | 1363 pending_buf_ = buf; |
1287 pending_buf_len_ = buf_len; | 1364 pending_buf_len_ = buf_len; |
1288 pending_callback_ = callback; | 1365 old_pending_callback_ = callback; |
1289 | 1366 |
1290 if (need_read_data_) { | 1367 if (need_read_data_) { |
1291 read_data_ = data_->GetNextRead(); | 1368 read_data_ = data_->GetNextRead(); |
1292 // ERR_IO_PENDING means that the SocketDataProvider is taking responsibility | 1369 // ERR_IO_PENDING means that the SocketDataProvider is taking responsibility |
1293 // to complete the async IO manually later (via OnReadComplete). | 1370 // to complete the async IO manually later (via OnReadComplete). |
1294 if (read_data_.result == ERR_IO_PENDING) { | 1371 if (read_data_.result == ERR_IO_PENDING) { |
1295 DCHECK(callback); // We need to be using async IO in this case. | 1372 DCHECK(callback); // We need to be using async IO in this case. |
1296 return ERR_IO_PENDING; | 1373 return ERR_IO_PENDING; |
1297 } | 1374 } |
1298 need_read_data_ = false; | 1375 need_read_data_ = false; |
1299 } | 1376 } |
1300 | 1377 |
1301 return CompleteRead(); | 1378 return CompleteRead(); |
1302 } | 1379 } |
| 1380 int MockUDPClientSocket::Read(net::IOBuffer* buf, int buf_len, |
| 1381 const net::CompletionCallback& callback) { |
| 1382 if (!connected_) |
| 1383 return net::ERR_UNEXPECTED; |
| 1384 |
| 1385 // If the buffer is already in use, a read is already in progress! |
| 1386 DCHECK(pending_buf_ == NULL); |
| 1387 |
| 1388 // Store our async IO data. |
| 1389 pending_buf_ = buf; |
| 1390 pending_buf_len_ = buf_len; |
| 1391 pending_callback_ = callback; |
| 1392 |
| 1393 if (need_read_data_) { |
| 1394 read_data_ = data_->GetNextRead(); |
| 1395 // ERR_IO_PENDING means that the SocketDataProvider is taking responsibility |
| 1396 // to complete the async IO manually later (via OnReadComplete). |
| 1397 if (read_data_.result == ERR_IO_PENDING) { |
| 1398 // We need to be using async IO in this case. |
| 1399 DCHECK(!callback.is_null()); |
| 1400 return ERR_IO_PENDING; |
| 1401 } |
| 1402 need_read_data_ = false; |
| 1403 } |
| 1404 |
| 1405 return CompleteRead(); |
| 1406 } |
1303 | 1407 |
1304 int MockUDPClientSocket::Write(net::IOBuffer* buf, int buf_len, | 1408 int MockUDPClientSocket::Write(net::IOBuffer* buf, int buf_len, |
1305 net::OldCompletionCallback* callback) { | 1409 net::OldCompletionCallback* callback) { |
1306 DCHECK(buf); | 1410 DCHECK(buf); |
1307 DCHECK_GT(buf_len, 0); | 1411 DCHECK_GT(buf_len, 0); |
1308 | 1412 |
1309 if (!connected_) | 1413 if (!connected_) |
1310 return ERR_UNEXPECTED; | 1414 return ERR_UNEXPECTED; |
1311 | 1415 |
1312 std::string data(buf->data(), buf_len); | 1416 std::string data(buf->data(), buf_len); |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1358 // Since we've been waiting for data, need_read_data_ should be true. | 1462 // Since we've been waiting for data, need_read_data_ should be true. |
1359 DCHECK(need_read_data_); | 1463 DCHECK(need_read_data_); |
1360 | 1464 |
1361 read_data_ = data; | 1465 read_data_ = data; |
1362 need_read_data_ = false; | 1466 need_read_data_ = false; |
1363 | 1467 |
1364 // The caller is simulating that this IO completes right now. Don't | 1468 // The caller is simulating that this IO completes right now. Don't |
1365 // let CompleteRead() schedule a callback. | 1469 // let CompleteRead() schedule a callback. |
1366 read_data_.async = false; | 1470 read_data_.async = false; |
1367 | 1471 |
1368 net::OldCompletionCallback* callback = pending_callback_; | 1472 if (old_pending_callback_) { |
1369 int rv = CompleteRead(); | 1473 net::OldCompletionCallback* callback = old_pending_callback_; |
1370 RunCallback(callback, rv); | 1474 int rv = CompleteRead(); |
| 1475 RunOldCallback(callback, rv); |
| 1476 } else { |
| 1477 net::CompletionCallback callback = pending_callback_; |
| 1478 int rv = CompleteRead(); |
| 1479 RunCallback(callback, rv); |
| 1480 } |
1371 } | 1481 } |
1372 | 1482 |
1373 int MockUDPClientSocket::CompleteRead() { | 1483 int MockUDPClientSocket::CompleteRead() { |
1374 DCHECK(pending_buf_); | 1484 DCHECK(pending_buf_); |
1375 DCHECK(pending_buf_len_ > 0); | 1485 DCHECK(pending_buf_len_ > 0); |
1376 | 1486 |
1377 // Save the pending async IO data and reset our |pending_| state. | 1487 // Save the pending async IO data and reset our |pending_| state. |
1378 net::IOBuffer* buf = pending_buf_; | 1488 net::IOBuffer* buf = pending_buf_; |
1379 int buf_len = pending_buf_len_; | 1489 int buf_len = pending_buf_len_; |
1380 net::OldCompletionCallback* callback = pending_callback_; | 1490 net::OldCompletionCallback* old_callback = old_pending_callback_; |
| 1491 net::CompletionCallback callback = pending_callback_; |
1381 pending_buf_ = NULL; | 1492 pending_buf_ = NULL; |
1382 pending_buf_len_ = 0; | 1493 pending_buf_len_ = 0; |
1383 pending_callback_ = NULL; | 1494 old_pending_callback_ = NULL; |
| 1495 pending_callback_.Reset(); |
| 1496 pending_callback_.Reset(); |
1384 | 1497 |
1385 int result = read_data_.result; | 1498 int result = read_data_.result; |
1386 DCHECK(result != ERR_IO_PENDING); | 1499 DCHECK(result != ERR_IO_PENDING); |
1387 | 1500 |
1388 if (read_data_.data) { | 1501 if (read_data_.data) { |
1389 if (read_data_.data_len - read_offset_ > 0) { | 1502 if (read_data_.data_len - read_offset_ > 0) { |
1390 result = std::min(buf_len, read_data_.data_len - read_offset_); | 1503 result = std::min(buf_len, read_data_.data_len - read_offset_); |
1391 memcpy(buf->data(), read_data_.data + read_offset_, result); | 1504 memcpy(buf->data(), read_data_.data + read_offset_, result); |
1392 read_offset_ += result; | 1505 read_offset_ += result; |
1393 if (read_offset_ == read_data_.data_len) { | 1506 if (read_offset_ == read_data_.data_len) { |
1394 need_read_data_ = true; | 1507 need_read_data_ = true; |
1395 read_offset_ = 0; | 1508 read_offset_ = 0; |
1396 } | 1509 } |
1397 } else { | 1510 } else { |
1398 result = 0; // EOF | 1511 result = 0; // EOF |
1399 } | 1512 } |
1400 } | 1513 } |
1401 | 1514 |
1402 if (read_data_.async) { | 1515 if (read_data_.async) { |
1403 DCHECK(callback); | 1516 DCHECK(old_callback || !callback.is_null()); |
1404 RunCallbackAsync(callback, result); | 1517 if (old_callback) |
| 1518 RunCallbackAsync(old_callback, result); |
| 1519 else |
| 1520 RunCallbackAsync(callback, result); |
1405 return net::ERR_IO_PENDING; | 1521 return net::ERR_IO_PENDING; |
1406 } | 1522 } |
1407 return result; | 1523 return result; |
1408 } | 1524 } |
1409 | 1525 |
1410 void MockUDPClientSocket::RunCallbackAsync(net::OldCompletionCallback* callback, | 1526 void MockUDPClientSocket::RunCallbackAsync(net::OldCompletionCallback* callback, |
1411 int result) { | 1527 int result) { |
1412 MessageLoop::current()->PostTask(FROM_HERE, | 1528 MessageLoop::current()->PostTask(FROM_HERE, |
| 1529 base::Bind(&MockUDPClientSocket::RunOldCallback, |
| 1530 weak_factory_.GetWeakPtr(), callback, result)); |
| 1531 } |
| 1532 void MockUDPClientSocket::RunCallbackAsync( |
| 1533 const net::CompletionCallback& callback, int result) { |
| 1534 MessageLoop::current()->PostTask(FROM_HERE, |
1413 base::Bind(&MockUDPClientSocket::RunCallback, weak_factory_.GetWeakPtr(), | 1535 base::Bind(&MockUDPClientSocket::RunCallback, weak_factory_.GetWeakPtr(), |
1414 callback, result)); | 1536 callback, result)); |
1415 } | 1537 } |
1416 | 1538 |
1417 void MockUDPClientSocket::RunCallback(net::OldCompletionCallback* callback, | 1539 void MockUDPClientSocket::RunOldCallback(net::OldCompletionCallback* callback, |
1418 int result) { | 1540 int result) { |
1419 if (callback) | 1541 if (callback) |
1420 callback->Run(result); | 1542 callback->Run(result); |
1421 } | 1543 } |
| 1544 void MockUDPClientSocket::RunCallback(const net::CompletionCallback& callback, |
| 1545 int result) { |
| 1546 if (!callback.is_null()) |
| 1547 callback.Run(result); |
| 1548 } |
1422 | 1549 |
1423 TestSocketRequest::TestSocketRequest( | 1550 TestSocketRequest::TestSocketRequest( |
1424 std::vector<TestSocketRequest*>* request_order, | 1551 std::vector<TestSocketRequest*>* request_order, |
1425 size_t* completion_count) | 1552 size_t* completion_count) |
1426 : request_order_(request_order), | 1553 : request_order_(request_order), |
1427 completion_count_(completion_count) { | 1554 completion_count_(completion_count) { |
1428 DCHECK(request_order); | 1555 DCHECK(request_order); |
1429 DCHECK(completion_count); | 1556 DCHECK(completion_count); |
1430 } | 1557 } |
1431 | 1558 |
(...skipping 256 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1688 | 1815 |
1689 const char kSOCKS5OkRequest[] = | 1816 const char kSOCKS5OkRequest[] = |
1690 { 0x05, 0x01, 0x00, 0x03, 0x04, 'h', 'o', 's', 't', 0x00, 0x50 }; | 1817 { 0x05, 0x01, 0x00, 0x03, 0x04, 'h', 'o', 's', 't', 0x00, 0x50 }; |
1691 const int kSOCKS5OkRequestLength = arraysize(kSOCKS5OkRequest); | 1818 const int kSOCKS5OkRequestLength = arraysize(kSOCKS5OkRequest); |
1692 | 1819 |
1693 const char kSOCKS5OkResponse[] = | 1820 const char kSOCKS5OkResponse[] = |
1694 { 0x05, 0x00, 0x00, 0x01, 127, 0, 0, 1, 0x00, 0x50 }; | 1821 { 0x05, 0x00, 0x00, 0x01, 127, 0, 0, 1, 0x00, 0x50 }; |
1695 const int kSOCKS5OkResponseLength = arraysize(kSOCKS5OkResponse); | 1822 const int kSOCKS5OkResponseLength = arraysize(kSOCKS5OkResponse); |
1696 | 1823 |
1697 } // namespace net | 1824 } // namespace net |
OLD | NEW |