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