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 <utility> | 8 #include <utility> |
9 #include <vector> | 9 #include <vector> |
10 | 10 |
11 #include "base/bind.h" | 11 #include "base/bind.h" |
12 #include "base/bind_helpers.h" | 12 #include "base/bind_helpers.h" |
13 #include "base/callback_helpers.h" | |
13 #include "base/compiler_specific.h" | 14 #include "base/compiler_specific.h" |
14 #include "base/location.h" | 15 #include "base/location.h" |
15 #include "base/logging.h" | 16 #include "base/logging.h" |
16 #include "base/memory/ptr_util.h" | 17 #include "base/memory/ptr_util.h" |
17 #include "base/run_loop.h" | 18 #include "base/run_loop.h" |
18 #include "base/single_thread_task_runner.h" | 19 #include "base/single_thread_task_runner.h" |
19 #include "base/threading/thread_task_runner_handle.h" | 20 #include "base/threading/thread_task_runner_handle.h" |
20 #include "base/time/time.h" | 21 #include "base/time/time.h" |
21 #include "net/base/address_family.h" | 22 #include "net/base/address_family.h" |
22 #include "net/base/address_list.h" | 23 #include "net/base/address_list.h" |
(...skipping 669 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
692 | 693 |
693 NET_TRACE(1, " *** ") << " Completing socket write for: " | 694 NET_TRACE(1, " *** ") << " Completing socket write for: " |
694 << data.sequence_number; | 695 << data.sequence_number; |
695 socket()->OnWriteComplete(rv); | 696 socket()->OnWriteComplete(rv); |
696 NET_TRACE(1, " *** ") << "Done"; | 697 NET_TRACE(1, " *** ") << "Done"; |
697 } | 698 } |
698 | 699 |
699 SequencedSocketData::~SequencedSocketData() { | 700 SequencedSocketData::~SequencedSocketData() { |
700 } | 701 } |
701 | 702 |
702 MockClientSocketFactory::MockClientSocketFactory() {} | 703 MockClientSocketFactory::MockClientSocketFactory() |
704 : enable_read_if_ready_(false) {} | |
703 | 705 |
704 MockClientSocketFactory::~MockClientSocketFactory() {} | 706 MockClientSocketFactory::~MockClientSocketFactory() {} |
705 | 707 |
706 void MockClientSocketFactory::AddSocketDataProvider( | 708 void MockClientSocketFactory::AddSocketDataProvider( |
707 SocketDataProvider* data) { | 709 SocketDataProvider* data) { |
708 mock_data_.Add(data); | 710 mock_data_.Add(data); |
709 } | 711 } |
710 | 712 |
711 void MockClientSocketFactory::AddSSLSocketDataProvider( | 713 void MockClientSocketFactory::AddSSLSocketDataProvider( |
712 SSLSocketDataProvider* data) { | 714 SSLSocketDataProvider* data) { |
(...skipping 23 matching lines...) Expand all Loading... | |
736 | 738 |
737 std::unique_ptr<StreamSocket> | 739 std::unique_ptr<StreamSocket> |
738 MockClientSocketFactory::CreateTransportClientSocket( | 740 MockClientSocketFactory::CreateTransportClientSocket( |
739 const AddressList& addresses, | 741 const AddressList& addresses, |
740 std::unique_ptr<SocketPerformanceWatcher> socket_performance_watcher, | 742 std::unique_ptr<SocketPerformanceWatcher> socket_performance_watcher, |
741 NetLog* net_log, | 743 NetLog* net_log, |
742 const NetLogSource& source) { | 744 const NetLogSource& source) { |
743 SocketDataProvider* data_provider = mock_data_.GetNext(); | 745 SocketDataProvider* data_provider = mock_data_.GetNext(); |
744 std::unique_ptr<MockTCPClientSocket> socket( | 746 std::unique_ptr<MockTCPClientSocket> socket( |
745 new MockTCPClientSocket(addresses, net_log, data_provider)); | 747 new MockTCPClientSocket(addresses, net_log, data_provider)); |
748 if (enable_read_if_ready_) | |
749 socket->set_enable_read_if_ready(enable_read_if_ready_); | |
746 return std::move(socket); | 750 return std::move(socket); |
747 } | 751 } |
748 | 752 |
749 std::unique_ptr<SSLClientSocket> MockClientSocketFactory::CreateSSLClientSocket( | 753 std::unique_ptr<SSLClientSocket> MockClientSocketFactory::CreateSSLClientSocket( |
750 std::unique_ptr<ClientSocketHandle> transport_socket, | 754 std::unique_ptr<ClientSocketHandle> transport_socket, |
751 const HostPortPair& host_and_port, | 755 const HostPortPair& host_and_port, |
752 const SSLConfig& ssl_config, | 756 const SSLConfig& ssl_config, |
753 const SSLClientSocketContext& context) { | 757 const SSLClientSocketContext& context) { |
754 SSLSocketDataProvider* next_ssl_data = mock_ssl_data_.GetNext(); | 758 SSLSocketDataProvider* next_ssl_data = mock_ssl_data_.GetNext(); |
755 if (!next_ssl_data->next_protos_expected_in_ssl_config.empty()) { | 759 if (!next_ssl_data->next_protos_expected_in_ssl_config.empty()) { |
(...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
875 SocketDataProvider* data) | 879 SocketDataProvider* data) |
876 : MockClientSocket(NetLogWithSource::Make(net_log, NetLogSourceType::NONE)), | 880 : MockClientSocket(NetLogWithSource::Make(net_log, NetLogSourceType::NONE)), |
877 addresses_(addresses), | 881 addresses_(addresses), |
878 data_(data), | 882 data_(data), |
879 read_offset_(0), | 883 read_offset_(0), |
880 read_data_(SYNCHRONOUS, ERR_UNEXPECTED), | 884 read_data_(SYNCHRONOUS, ERR_UNEXPECTED), |
881 need_read_data_(true), | 885 need_read_data_(true), |
882 peer_closed_connection_(false), | 886 peer_closed_connection_(false), |
883 pending_read_buf_(NULL), | 887 pending_read_buf_(NULL), |
884 pending_read_buf_len_(0), | 888 pending_read_buf_len_(0), |
885 was_used_to_convey_data_(false) { | 889 was_used_to_convey_data_(false), |
890 enable_read_if_ready_(false) { | |
886 DCHECK(data_); | 891 DCHECK(data_); |
887 peer_addr_ = data->connect_data().peer_addr; | 892 peer_addr_ = data->connect_data().peer_addr; |
888 data_->Initialize(this); | 893 data_->Initialize(this); |
889 } | 894 } |
890 | 895 |
891 MockTCPClientSocket::~MockTCPClientSocket() { | 896 MockTCPClientSocket::~MockTCPClientSocket() { |
892 if (data_) | 897 if (data_) |
893 data_->DetachSocket(); | 898 data_->DetachSocket(); |
894 } | 899 } |
895 | 900 |
896 int MockTCPClientSocket::Read(IOBuffer* buf, int buf_len, | 901 int MockTCPClientSocket::Read(IOBuffer* buf, int buf_len, |
897 const CompletionCallback& callback) { | 902 const CompletionCallback& callback) { |
898 if (!connected_ || !data_) | |
899 return ERR_UNEXPECTED; | |
900 | |
901 // If the buffer is already in use, a read is already in progress! | 903 // If the buffer is already in use, a read is already in progress! |
902 DCHECK(!pending_read_buf_); | 904 DCHECK(!pending_read_buf_); |
905 // Use base::Unretained() is safe because whenever callback is run | |
Bence
2017/03/03 16:33:41
"callback is run ..., it ..." implies that it is t
xunjieli
2017/03/03 19:41:06
Done.
| |
906 // asynchrously, it takes the weak ptr of the base class. | |
Bence
2017/03/03 16:33:41
Is it in MockClientSocket::RunCallbackAsync()? Ca
xunjieli
2017/03/03 19:41:06
Done.
| |
907 int rv = ReadIfReadyImpl( | |
908 buf, buf_len, | |
909 base::Bind(&MockTCPClientSocket::RetryRead, base::Unretained(this))); | |
910 if (rv == ERR_IO_PENDING) { | |
911 pending_read_buf_ = buf; | |
912 pending_read_buf_len_ = buf_len; | |
913 pending_read_callback_ = callback; | |
914 } | |
915 return rv; | |
916 } | |
903 | 917 |
904 // Store our async IO data. | 918 int MockTCPClientSocket::ReadIfReady(IOBuffer* buf, |
905 pending_read_buf_ = buf; | 919 int buf_len, |
906 pending_read_buf_len_ = buf_len; | 920 const CompletionCallback& callback) { |
907 pending_read_callback_ = callback; | 921 DCHECK(!pending_read_if_ready_callback_); |
908 | 922 |
909 if (need_read_data_) { | 923 if (!enable_read_if_ready_) |
910 read_data_ = data_->OnRead(); | 924 return ERR_READ_IF_READY_NOT_IMPLEMENTED; |
911 if (read_data_.result == ERR_CONNECTION_CLOSED) { | 925 return ReadIfReadyImpl(buf, buf_len, callback); |
912 // This MockRead is just a marker to instruct us to set | |
913 // peer_closed_connection_. | |
914 peer_closed_connection_ = true; | |
915 } | |
916 if (read_data_.result == ERR_TEST_PEER_CLOSE_AFTER_NEXT_MOCK_READ) { | |
917 // This MockRead is just a marker to instruct us to set | |
918 // peer_closed_connection_. Skip it and get the next one. | |
919 read_data_ = data_->OnRead(); | |
920 peer_closed_connection_ = true; | |
921 } | |
922 // ERR_IO_PENDING means that the SocketDataProvider is taking responsibility | |
923 // to complete the async IO manually later (via OnReadComplete). | |
924 if (read_data_.result == ERR_IO_PENDING) { | |
925 // We need to be using async IO in this case. | |
926 DCHECK(!callback.is_null()); | |
927 return ERR_IO_PENDING; | |
928 } | |
929 need_read_data_ = false; | |
930 } | |
931 | |
932 return CompleteRead(); | |
933 } | 926 } |
934 | 927 |
935 int MockTCPClientSocket::Write(IOBuffer* buf, int buf_len, | 928 int MockTCPClientSocket::Write(IOBuffer* buf, int buf_len, |
936 const CompletionCallback& callback) { | 929 const CompletionCallback& callback) { |
937 DCHECK(buf); | 930 DCHECK(buf); |
938 DCHECK_GT(buf_len, 0); | 931 DCHECK_GT(buf_len, 0); |
939 | 932 |
940 if (!connected_ || !data_) | 933 if (!connected_ || !data_) |
941 return ERR_UNEXPECTED; | 934 return ERR_UNEXPECTED; |
942 | 935 |
(...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1041 bool MockTCPClientSocket::GetSSLInfo(SSLInfo* ssl_info) { | 1034 bool MockTCPClientSocket::GetSSLInfo(SSLInfo* ssl_info) { |
1042 return false; | 1035 return false; |
1043 } | 1036 } |
1044 | 1037 |
1045 void MockTCPClientSocket::OnReadComplete(const MockRead& data) { | 1038 void MockTCPClientSocket::OnReadComplete(const MockRead& data) { |
1046 // If |data_| has been destroyed, safest to just do nothing. | 1039 // If |data_| has been destroyed, safest to just do nothing. |
1047 if (!data_) | 1040 if (!data_) |
1048 return; | 1041 return; |
1049 | 1042 |
1050 // There must be a read pending. | 1043 // There must be a read pending. |
1051 DCHECK(pending_read_buf_.get()); | 1044 DCHECK(pending_read_if_ready_callback_); |
1052 // You can't complete a read with another ERR_IO_PENDING status code. | 1045 // You can't complete a read with another ERR_IO_PENDING status code. |
1053 DCHECK_NE(ERR_IO_PENDING, data.result); | 1046 DCHECK_NE(ERR_IO_PENDING, data.result); |
1054 // Since we've been waiting for data, need_read_data_ should be true. | 1047 // Since we've been waiting for data, need_read_data_ should be true. |
1055 DCHECK(need_read_data_); | 1048 DCHECK(need_read_data_); |
1056 | 1049 |
1057 read_data_ = data; | 1050 read_data_ = data; |
1058 need_read_data_ = false; | 1051 need_read_data_ = false; |
1059 | 1052 |
1060 // The caller is simulating that this IO completes right now. Don't | 1053 // The caller is simulating that this IO completes right now. Don't |
1061 // let CompleteRead() schedule a callback. | 1054 // let CompleteRead() schedule a callback. |
1062 read_data_.mode = SYNCHRONOUS; | 1055 read_data_.mode = SYNCHRONOUS; |
1063 | 1056 RunCallback(base::ResetAndReturn(&pending_read_if_ready_callback_), |
1064 CompletionCallback callback = pending_read_callback_; | 1057 read_data_.result > 0 ? OK : read_data_.result); |
1065 int rv = CompleteRead(); | |
1066 RunCallback(callback, rv); | |
1067 } | 1058 } |
1068 | 1059 |
1069 void MockTCPClientSocket::OnWriteComplete(int rv) { | 1060 void MockTCPClientSocket::OnWriteComplete(int rv) { |
1070 // If |data_| has been destroyed, safest to just do nothing. | 1061 // If |data_| has been destroyed, safest to just do nothing. |
1071 if (!data_) | 1062 if (!data_) |
1072 return; | 1063 return; |
1073 | 1064 |
1074 // There must be a read pending. | 1065 // There must be a read pending. |
1075 DCHECK(!pending_write_callback_.is_null()); | 1066 DCHECK(!pending_write_callback_.is_null()); |
1076 CompletionCallback callback = pending_write_callback_; | 1067 CompletionCallback callback = pending_write_callback_; |
1077 RunCallback(callback, rv); | 1068 RunCallback(callback, rv); |
1078 } | 1069 } |
1079 | 1070 |
1080 void MockTCPClientSocket::OnConnectComplete(const MockConnect& data) { | 1071 void MockTCPClientSocket::OnConnectComplete(const MockConnect& data) { |
1081 // If |data_| has been destroyed, safest to just do nothing. | 1072 // If |data_| has been destroyed, safest to just do nothing. |
1082 if (!data_) | 1073 if (!data_) |
1083 return; | 1074 return; |
1084 | 1075 |
1085 CompletionCallback callback = pending_connect_callback_; | 1076 CompletionCallback callback = pending_connect_callback_; |
1086 RunCallback(callback, data.result); | 1077 RunCallback(callback, data.result); |
1087 } | 1078 } |
1088 | 1079 |
1089 void MockTCPClientSocket::OnDataProviderDestroyed() { | 1080 void MockTCPClientSocket::OnDataProviderDestroyed() { |
1090 data_ = nullptr; | 1081 data_ = nullptr; |
1091 } | 1082 } |
1092 | 1083 |
1093 int MockTCPClientSocket::CompleteRead() { | 1084 void MockTCPClientSocket::RetryRead(int rv) { |
1085 DCHECK(pending_read_callback_); | |
1094 DCHECK(pending_read_buf_.get()); | 1086 DCHECK(pending_read_buf_.get()); |
1095 DCHECK(pending_read_buf_len_ > 0); | 1087 DCHECK_LT(0, pending_read_buf_len_); |
1088 | |
1089 if (rv == OK) { | |
1090 rv = ReadIfReadyImpl( | |
1091 pending_read_buf_.get(), pending_read_buf_len_, | |
1092 base::Bind(&MockTCPClientSocket::RetryRead, base::Unretained(this))); | |
1093 if (rv == ERR_IO_PENDING) | |
1094 return; | |
1095 } | |
1096 pending_read_buf_ = nullptr; | |
1097 pending_read_buf_len_ = 0; | |
1098 RunCallback(base::ResetAndReturn(&pending_read_callback_), rv); | |
1099 } | |
1100 | |
1101 int MockTCPClientSocket::ReadIfReadyImpl(IOBuffer* buf, | |
1102 int buf_len, | |
1103 const CompletionCallback& callback) { | |
1104 if (!connected_ || !data_) | |
1105 return ERR_UNEXPECTED; | |
1106 | |
1107 DCHECK(!pending_read_if_ready_callback_); | |
1108 | |
1109 if (need_read_data_) { | |
1110 read_data_ = data_->OnRead(); | |
1111 if (read_data_.result == ERR_CONNECTION_CLOSED) { | |
1112 // This MockRead is just a marker to instruct us to set | |
1113 // peer_closed_connection_. | |
1114 peer_closed_connection_ = true; | |
1115 } | |
1116 if (read_data_.result == ERR_TEST_PEER_CLOSE_AFTER_NEXT_MOCK_READ) { | |
1117 // This MockRead is just a marker to instruct us to set | |
1118 // peer_closed_connection_. Skip it and get the next one. | |
1119 read_data_ = data_->OnRead(); | |
1120 peer_closed_connection_ = true; | |
1121 } | |
1122 // ERR_IO_PENDING means that the SocketDataProvider is taking responsibility | |
1123 // to complete the async IO manually later (via OnReadComplete). | |
1124 if (read_data_.result == ERR_IO_PENDING) { | |
1125 // We need to be using async IO in this case. | |
1126 DCHECK(!callback.is_null()); | |
1127 pending_read_if_ready_callback_ = callback; | |
1128 return ERR_IO_PENDING; | |
1129 } | |
1130 need_read_data_ = false; | |
1131 } | |
1132 | |
1133 int result = read_data_.result; | |
1134 DCHECK_NE(ERR_IO_PENDING, result); | |
1135 if (read_data_.mode == ASYNC) { | |
1136 DCHECK(!callback.is_null()); | |
1137 read_data_.mode = SYNCHRONOUS; | |
1138 RunCallbackAsync(callback, result); | |
1139 return ERR_IO_PENDING; | |
1140 } | |
1096 | 1141 |
1097 was_used_to_convey_data_ = true; | 1142 was_used_to_convey_data_ = true; |
1098 | |
1099 // Save the pending async IO data and reset our |pending_| state. | |
1100 scoped_refptr<IOBuffer> buf = pending_read_buf_; | |
1101 int buf_len = pending_read_buf_len_; | |
1102 CompletionCallback callback = pending_read_callback_; | |
1103 pending_read_buf_ = NULL; | |
1104 pending_read_buf_len_ = 0; | |
1105 pending_read_callback_.Reset(); | |
1106 | |
1107 int result = read_data_.result; | |
1108 DCHECK(result != ERR_IO_PENDING); | |
1109 | |
1110 if (read_data_.data) { | 1143 if (read_data_.data) { |
1111 if (read_data_.data_len - read_offset_ > 0) { | 1144 if (read_data_.data_len - read_offset_ > 0) { |
1112 result = std::min(buf_len, read_data_.data_len - read_offset_); | 1145 result = std::min(buf_len, read_data_.data_len - read_offset_); |
1113 memcpy(buf->data(), read_data_.data + read_offset_, result); | 1146 memcpy(buf->data(), read_data_.data + read_offset_, result); |
1114 read_offset_ += result; | 1147 read_offset_ += result; |
1115 if (read_offset_ == read_data_.data_len) { | 1148 if (read_offset_ == read_data_.data_len) { |
1116 need_read_data_ = true; | 1149 need_read_data_ = true; |
1117 read_offset_ = 0; | 1150 read_offset_ = 0; |
1118 } | 1151 } |
1119 } else { | 1152 } else { |
1120 result = 0; // EOF | 1153 result = 0; // EOF |
1121 } | 1154 } |
1122 } | 1155 } |
1123 | |
1124 if (read_data_.mode == ASYNC) { | |
1125 DCHECK(!callback.is_null()); | |
1126 RunCallbackAsync(callback, result); | |
1127 return ERR_IO_PENDING; | |
1128 } | |
1129 return result; | 1156 return result; |
1130 } | 1157 } |
1131 | 1158 |
1132 // static | 1159 // static |
1133 void MockSSLClientSocket::ConnectCallback( | 1160 void MockSSLClientSocket::ConnectCallback( |
1134 MockSSLClientSocket* ssl_client_socket, | 1161 MockSSLClientSocket* ssl_client_socket, |
1135 const CompletionCallback& callback, | 1162 const CompletionCallback& callback, |
1136 int rv) { | 1163 int rv) { |
1137 if (rv == OK) | 1164 if (rv == OK) |
1138 ssl_client_socket->connected_ = true; | 1165 ssl_client_socket->connected_ = true; |
(...skipping 18 matching lines...) Expand all Loading... | |
1157 | 1184 |
1158 MockSSLClientSocket::~MockSSLClientSocket() { | 1185 MockSSLClientSocket::~MockSSLClientSocket() { |
1159 Disconnect(); | 1186 Disconnect(); |
1160 } | 1187 } |
1161 | 1188 |
1162 int MockSSLClientSocket::Read(IOBuffer* buf, int buf_len, | 1189 int MockSSLClientSocket::Read(IOBuffer* buf, int buf_len, |
1163 const CompletionCallback& callback) { | 1190 const CompletionCallback& callback) { |
1164 return transport_->socket()->Read(buf, buf_len, callback); | 1191 return transport_->socket()->Read(buf, buf_len, callback); |
1165 } | 1192 } |
1166 | 1193 |
1194 int MockSSLClientSocket::ReadIfReady(IOBuffer* buf, | |
1195 int buf_len, | |
1196 const CompletionCallback& callback) { | |
1197 return transport_->socket()->ReadIfReady(buf, buf_len, callback); | |
1198 } | |
1199 | |
1167 int MockSSLClientSocket::Write(IOBuffer* buf, int buf_len, | 1200 int MockSSLClientSocket::Write(IOBuffer* buf, int buf_len, |
1168 const CompletionCallback& callback) { | 1201 const CompletionCallback& callback) { |
1169 return transport_->socket()->Write(buf, buf_len, callback); | 1202 return transport_->socket()->Write(buf, buf_len, callback); |
1170 } | 1203 } |
1171 | 1204 |
1172 int MockSSLClientSocket::Connect(const CompletionCallback& callback) { | 1205 int MockSSLClientSocket::Connect(const CompletionCallback& callback) { |
1173 int rv = transport_->socket()->Connect( | 1206 int rv = transport_->socket()->Connect( |
1174 base::Bind(&ConnectCallback, base::Unretained(this), callback)); | 1207 base::Bind(&ConnectCallback, base::Unretained(this), callback)); |
1175 if (rv == OK) { | 1208 if (rv == OK) { |
1176 if (data_->connect.result == OK) | 1209 if (data_->connect.result == OK) |
(...skipping 579 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1756 } | 1789 } |
1757 | 1790 |
1758 int64_t CountWriteBytes(const MockWrite writes[], size_t writes_size) { | 1791 int64_t CountWriteBytes(const MockWrite writes[], size_t writes_size) { |
1759 int64_t total = 0; | 1792 int64_t total = 0; |
1760 for (const MockWrite* write = writes; write != writes + writes_size; ++write) | 1793 for (const MockWrite* write = writes; write != writes + writes_size; ++write) |
1761 total += write->data_len; | 1794 total += write->data_len; |
1762 return total; | 1795 return total; |
1763 } | 1796 } |
1764 | 1797 |
1765 } // namespace net | 1798 } // namespace net |
OLD | NEW |