Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(761)

Side by Side Diff: net/socket/socket_test_util.cc

Issue 2593063003: Add Socket::ReadIfReady() (Closed)
Patch Set: Fix tests for real Created 3 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
davidben 2017/02/01 22:25:57 mmenke may be a better reviewer for this file. I'm
xunjieli 2017/02/03 16:35:33 Done.
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"
(...skipping 680 matching lines...) Expand 10 before | Expand all | Expand 10 after
692 692
693 NET_TRACE(1, " *** ") << " Completing socket write for: " 693 NET_TRACE(1, " *** ") << " Completing socket write for: "
694 << data.sequence_number; 694 << data.sequence_number;
695 socket()->OnWriteComplete(rv); 695 socket()->OnWriteComplete(rv);
696 NET_TRACE(1, " *** ") << "Done"; 696 NET_TRACE(1, " *** ") << "Done";
697 } 697 }
698 698
699 SequencedSocketData::~SequencedSocketData() { 699 SequencedSocketData::~SequencedSocketData() {
700 } 700 }
701 701
702 MockClientSocketFactory::MockClientSocketFactory() {} 702 MockClientSocketFactory::MockClientSocketFactory()
703 : enable_read_if_ready_(false) {}
703 704
704 MockClientSocketFactory::~MockClientSocketFactory() {} 705 MockClientSocketFactory::~MockClientSocketFactory() {}
705 706
706 void MockClientSocketFactory::AddSocketDataProvider( 707 void MockClientSocketFactory::AddSocketDataProvider(
707 SocketDataProvider* data) { 708 SocketDataProvider* data) {
708 mock_data_.Add(data); 709 mock_data_.Add(data);
709 } 710 }
710 711
711 void MockClientSocketFactory::AddSSLSocketDataProvider( 712 void MockClientSocketFactory::AddSSLSocketDataProvider(
712 SSLSocketDataProvider* data) { 713 SSLSocketDataProvider* data) {
(...skipping 23 matching lines...) Expand all
736 737
737 std::unique_ptr<StreamSocket> 738 std::unique_ptr<StreamSocket>
738 MockClientSocketFactory::CreateTransportClientSocket( 739 MockClientSocketFactory::CreateTransportClientSocket(
739 const AddressList& addresses, 740 const AddressList& addresses,
740 std::unique_ptr<SocketPerformanceWatcher> socket_performance_watcher, 741 std::unique_ptr<SocketPerformanceWatcher> socket_performance_watcher,
741 NetLog* net_log, 742 NetLog* net_log,
742 const NetLogSource& source) { 743 const NetLogSource& source) {
743 SocketDataProvider* data_provider = mock_data_.GetNext(); 744 SocketDataProvider* data_provider = mock_data_.GetNext();
744 std::unique_ptr<MockTCPClientSocket> socket( 745 std::unique_ptr<MockTCPClientSocket> socket(
745 new MockTCPClientSocket(addresses, net_log, data_provider)); 746 new MockTCPClientSocket(addresses, net_log, data_provider));
747 if (enable_read_if_ready_)
748 socket->set_enable_read_if_ready(enable_read_if_ready_);
746 return std::move(socket); 749 return std::move(socket);
747 } 750 }
748 751
749 std::unique_ptr<SSLClientSocket> MockClientSocketFactory::CreateSSLClientSocket( 752 std::unique_ptr<SSLClientSocket> MockClientSocketFactory::CreateSSLClientSocket(
750 std::unique_ptr<ClientSocketHandle> transport_socket, 753 std::unique_ptr<ClientSocketHandle> transport_socket,
751 const HostPortPair& host_and_port, 754 const HostPortPair& host_and_port,
752 const SSLConfig& ssl_config, 755 const SSLConfig& ssl_config,
753 const SSLClientSocketContext& context) { 756 const SSLClientSocketContext& context) {
754 SSLSocketDataProvider* next_ssl_data = mock_ssl_data_.GetNext(); 757 SSLSocketDataProvider* next_ssl_data = mock_ssl_data_.GetNext();
755 if (!next_ssl_data->next_protos_expected_in_ssl_config.empty()) { 758 if (!next_ssl_data->next_protos_expected_in_ssl_config.empty()) {
(...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after
875 SocketDataProvider* data) 878 SocketDataProvider* data)
876 : MockClientSocket(NetLogWithSource::Make(net_log, NetLogSourceType::NONE)), 879 : MockClientSocket(NetLogWithSource::Make(net_log, NetLogSourceType::NONE)),
877 addresses_(addresses), 880 addresses_(addresses),
878 data_(data), 881 data_(data),
879 read_offset_(0), 882 read_offset_(0),
880 read_data_(SYNCHRONOUS, ERR_UNEXPECTED), 883 read_data_(SYNCHRONOUS, ERR_UNEXPECTED),
881 need_read_data_(true), 884 need_read_data_(true),
882 peer_closed_connection_(false), 885 peer_closed_connection_(false),
883 pending_read_buf_(NULL), 886 pending_read_buf_(NULL),
884 pending_read_buf_len_(0), 887 pending_read_buf_len_(0),
885 was_used_to_convey_data_(false) { 888 was_used_to_convey_data_(false),
889 enable_read_if_ready_(false),
890 pending_read_type_(NONE),
891 weak_factory_(this) {
886 DCHECK(data_); 892 DCHECK(data_);
887 peer_addr_ = data->connect_data().peer_addr; 893 peer_addr_ = data->connect_data().peer_addr;
888 data_->Initialize(this); 894 data_->Initialize(this);
889 } 895 }
890 896
891 MockTCPClientSocket::~MockTCPClientSocket() { 897 MockTCPClientSocket::~MockTCPClientSocket() {
892 if (data_) 898 if (data_)
893 data_->DetachSocket(); 899 data_->DetachSocket();
894 } 900 }
895 901
896 int MockTCPClientSocket::Read(IOBuffer* buf, int buf_len, 902 int MockTCPClientSocket::Read(IOBuffer* buf, int buf_len,
897 const CompletionCallback& callback) { 903 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! 904 // If the buffer is already in use, a read is already in progress!
902 DCHECK(!pending_read_buf_); 905 DCHECK(!pending_read_buf_);
davidben 2017/02/01 22:25:57 DCHECK_EQ(NONE, pending_read_type_); to match?
xunjieli 2017/02/03 16:35:33 Acknowledged. After applying the suggested Read()/
903 906
904 // Store our async IO data. 907 int rv = ReadCommon(buf, buf_len, callback, /*use_read_if_ready=*/false);
905 pending_read_buf_ = buf; 908 if (rv != ERR_IO_PENDING)
906 pending_read_buf_len_ = buf_len; 909 return rv;
907 pending_read_callback_ = callback; 910 pending_read_type_ = READ;
911 return ERR_IO_PENDING;
912 }
908 913
909 if (need_read_data_) { 914 int MockTCPClientSocket::ReadIfReady(IOBuffer* buf,
910 read_data_ = data_->OnRead(); 915 int buf_len,
911 if (read_data_.result == ERR_CONNECTION_CLOSED) { 916 const CompletionCallback& callback) {
912 // This MockRead is just a marker to instruct us to set 917 DCHECK_EQ(NONE, pending_read_type_);
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 918
932 return CompleteRead(); 919 if (!enable_read_if_ready_)
920 return ERR_READ_IF_READY_NOT_IMPLEMENTED;
921
922 int rv = ReadCommon(buf, buf_len, callback, /*use_read_if_ready=*/false);
923 pending_read_buf_ = nullptr;
924 pending_read_buf_len_ = 0;
925 if (rv != ERR_IO_PENDING)
926 return rv;
927 pending_read_type_ = READ_IF_READY;
928 return ERR_IO_PENDING;
933 } 929 }
934 930
935 int MockTCPClientSocket::Write(IOBuffer* buf, int buf_len, 931 int MockTCPClientSocket::Write(IOBuffer* buf, int buf_len,
936 const CompletionCallback& callback) { 932 const CompletionCallback& callback) {
937 DCHECK(buf); 933 DCHECK(buf);
938 DCHECK_GT(buf_len, 0); 934 DCHECK_GT(buf_len, 0);
939 935
940 if (!connected_ || !data_) 936 if (!connected_ || !data_)
941 return ERR_UNEXPECTED; 937 return ERR_UNEXPECTED;
942 938
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after
1036 EXPECT_FALSE(IsConnected()) << "Can't enable fast open after connect."; 1032 EXPECT_FALSE(IsConnected()) << "Can't enable fast open after connect.";
1037 1033
1038 data_->OnEnableTCPFastOpenIfSupported(); 1034 data_->OnEnableTCPFastOpenIfSupported();
1039 } 1035 }
1040 1036
1041 bool MockTCPClientSocket::GetSSLInfo(SSLInfo* ssl_info) { 1037 bool MockTCPClientSocket::GetSSLInfo(SSLInfo* ssl_info) {
1042 return false; 1038 return false;
1043 } 1039 }
1044 1040
1045 void MockTCPClientSocket::OnReadComplete(const MockRead& data) { 1041 void MockTCPClientSocket::OnReadComplete(const MockRead& data) {
1042 DCHECK_NE(NONE, pending_read_type_);
1043
1046 // If |data_| has been destroyed, safest to just do nothing. 1044 // If |data_| has been destroyed, safest to just do nothing.
1047 if (!data_) 1045 if (!data_)
1048 return; 1046 return;
1049 1047
1050 // There must be a read pending. 1048 // There must be a read pending.
1051 DCHECK(pending_read_buf_.get()); 1049 DCHECK(!pending_read_callback_.is_null());
1052 // You can't complete a read with another ERR_IO_PENDING status code. 1050 // You can't complete a read with another ERR_IO_PENDING status code.
1053 DCHECK_NE(ERR_IO_PENDING, data.result); 1051 DCHECK_NE(ERR_IO_PENDING, data.result);
1054 // Since we've been waiting for data, need_read_data_ should be true. 1052 // Since we've been waiting for data, need_read_data_ should be true.
1055 DCHECK(need_read_data_); 1053 DCHECK(need_read_data_);
1056 1054
1057 read_data_ = data; 1055 read_data_ = data;
1058 need_read_data_ = false; 1056 need_read_data_ = false;
1059 1057
1058 CompletionCallback callback = pending_read_callback_;
1059 pending_read_callback_.Reset();
1060
1060 // The caller is simulating that this IO completes right now. Don't 1061 // The caller is simulating that this IO completes right now. Don't
1061 // let CompleteRead() schedule a callback. 1062 // let CompleteRead() schedule a callback.
1062 read_data_.mode = SYNCHRONOUS; 1063 read_data_.mode = SYNCHRONOUS;
1063 1064 if (pending_read_type_ == READ_IF_READY) {
1064 CompletionCallback callback = pending_read_callback_; 1065 RunReadCallback(callback, read_data_.result > 0 ? OK : read_data_.result);
1065 int rv = CompleteRead(); 1066 } else {
1066 RunCallback(callback, rv); 1067 int rv = CompleteRead(pending_read_type_);
1068 RunReadCallback(callback, rv);
1069 }
1067 } 1070 }
1068 1071
1069 void MockTCPClientSocket::OnWriteComplete(int rv) { 1072 void MockTCPClientSocket::OnWriteComplete(int rv) {
1070 // If |data_| has been destroyed, safest to just do nothing. 1073 // If |data_| has been destroyed, safest to just do nothing.
1071 if (!data_) 1074 if (!data_)
1072 return; 1075 return;
1073 1076
1074 // There must be a read pending. 1077 // There must be a read pending.
1075 DCHECK(!pending_write_callback_.is_null()); 1078 DCHECK(!pending_write_callback_.is_null());
1076 CompletionCallback callback = pending_write_callback_; 1079 CompletionCallback callback = pending_write_callback_;
1077 RunCallback(callback, rv); 1080 RunCallback(callback, rv);
1078 } 1081 }
1079 1082
1080 void MockTCPClientSocket::OnConnectComplete(const MockConnect& data) { 1083 void MockTCPClientSocket::OnConnectComplete(const MockConnect& data) {
1081 // If |data_| has been destroyed, safest to just do nothing. 1084 // If |data_| has been destroyed, safest to just do nothing.
1082 if (!data_) 1085 if (!data_)
1083 return; 1086 return;
1084 1087
1085 CompletionCallback callback = pending_connect_callback_; 1088 CompletionCallback callback = pending_connect_callback_;
1086 RunCallback(callback, data.result); 1089 RunCallback(callback, data.result);
1087 } 1090 }
1088 1091
1089 void MockTCPClientSocket::OnDataProviderDestroyed() { 1092 void MockTCPClientSocket::OnDataProviderDestroyed() {
1090 data_ = nullptr; 1093 data_ = nullptr;
1091 } 1094 }
1092 1095
1093 int MockTCPClientSocket::CompleteRead() { 1096 int MockTCPClientSocket::ReadCommon(IOBuffer* buf,
1097 int buf_len,
1098 const CompletionCallback& callback,
1099 bool use_read_if_ready) {
davidben 2017/02/01 22:25:57 This parameter doesn't seem to be used. Did you me
xunjieli 2017/02/03 16:35:33 Acknowledged. After applying the suggested Read()/
1100 if (!connected_ || !data_)
1101 return ERR_UNEXPECTED;
1102
1103 // If the buffer is already in use, a read is already in progress!
1104 DCHECK(!pending_read_buf_);
1105 DCHECK_EQ(NONE, pending_read_type_);
1106
1107 // Store our async IO data.
1108 pending_read_buf_ = buf;
1109 pending_read_buf_len_ = buf_len;
1110 pending_read_callback_ = callback;
1111
1112 if (need_read_data_) {
1113 read_data_ = data_->OnRead();
1114 if (read_data_.result == ERR_CONNECTION_CLOSED) {
1115 // This MockRead is just a marker to instruct us to set
1116 // peer_closed_connection_.
1117 peer_closed_connection_ = true;
1118 }
1119 if (read_data_.result == ERR_TEST_PEER_CLOSE_AFTER_NEXT_MOCK_READ) {
1120 // This MockRead is just a marker to instruct us to set
1121 // peer_closed_connection_. Skip it and get the next one.
1122 read_data_ = data_->OnRead();
1123 peer_closed_connection_ = true;
1124 }
1125 // ERR_IO_PENDING means that the SocketDataProvider is taking responsibility
1126 // to complete the async IO manually later (via OnReadComplete).
1127 if (read_data_.result == ERR_IO_PENDING) {
1128 // We need to be using async IO in this case.
1129 DCHECK(!callback.is_null());
1130 return ERR_IO_PENDING;
1131 }
1132 need_read_data_ = false;
1133 }
1134
1135 return CompleteRead(false);
1136 }
1137
1138 int MockTCPClientSocket::CompleteRead(bool read_if_ready_used) {
1094 DCHECK(pending_read_buf_.get()); 1139 DCHECK(pending_read_buf_.get());
1095 DCHECK(pending_read_buf_len_ > 0); 1140 DCHECK(pending_read_buf_len_ > 0);
1096 1141
1097 was_used_to_convey_data_ = true; 1142 was_used_to_convey_data_ = true;
1098 1143
1144 if (read_if_ready_used && read_data_.mode == ASYNC) {
1145 CompletionCallback callback = pending_read_callback_;
1146 pending_read_callback_.Reset();
1147 DCHECK(!callback.is_null());
1148 RunReadCallbackAsync(callback,
1149 read_data_.result > 0 ? OK : read_data_.result);
1150 return ERR_IO_PENDING;
1151 }
1099 // Save the pending async IO data and reset our |pending_| state. 1152 // Save the pending async IO data and reset our |pending_| state.
1100 scoped_refptr<IOBuffer> buf = pending_read_buf_; 1153 scoped_refptr<IOBuffer> buf = pending_read_buf_;
1101 int buf_len = pending_read_buf_len_; 1154 int buf_len = pending_read_buf_len_;
1102 CompletionCallback callback = pending_read_callback_; 1155 CompletionCallback callback = pending_read_callback_;
1103 pending_read_buf_ = NULL; 1156 pending_read_buf_ = NULL;
1104 pending_read_buf_len_ = 0; 1157 pending_read_buf_len_ = 0;
1105 pending_read_callback_.Reset(); 1158 pending_read_callback_.Reset();
1106 1159
1107 int result = read_data_.result; 1160 int result = read_data_.result;
1108 DCHECK(result != ERR_IO_PENDING); 1161 DCHECK(result != ERR_IO_PENDING);
1109 1162
1110 if (read_data_.data) { 1163 if (read_data_.data) {
1111 if (read_data_.data_len - read_offset_ > 0) { 1164 if (read_data_.data_len - read_offset_ > 0) {
1112 result = std::min(buf_len, read_data_.data_len - read_offset_); 1165 result = std::min(buf_len, read_data_.data_len - read_offset_);
1113 memcpy(buf->data(), read_data_.data + read_offset_, result); 1166 memcpy(buf->data(), read_data_.data + read_offset_, result);
1114 read_offset_ += result; 1167 read_offset_ += result;
1115 if (read_offset_ == read_data_.data_len) { 1168 if (read_offset_ == read_data_.data_len) {
1116 need_read_data_ = true; 1169 need_read_data_ = true;
1117 read_offset_ = 0; 1170 read_offset_ = 0;
1118 } 1171 }
1119 } else { 1172 } else {
1120 result = 0; // EOF 1173 result = 0; // EOF
1121 } 1174 }
1122 } 1175 }
1123 1176
1124 if (read_data_.mode == ASYNC) { 1177 if (read_data_.mode == ASYNC) {
1125 DCHECK(!callback.is_null()); 1178 DCHECK(!callback.is_null());
1126 RunCallbackAsync(callback, result); 1179 RunReadCallbackAsync(callback, result);
1127 return ERR_IO_PENDING; 1180 return ERR_IO_PENDING;
1128 } 1181 }
1129 return result; 1182 return result;
1130 } 1183 }
1131 1184
1185 void MockTCPClientSocket::RunReadCallback(const CompletionCallback& callback,
1186 int result) {
1187 pending_read_type_ = NONE;
1188 MockClientSocket::RunCallback(callback, result);
1189 }
1190
1191 void MockTCPClientSocket::RunReadCallbackAsync(
1192 const CompletionCallback& callback,
1193 int result) {
1194 base::ThreadTaskRunnerHandle::Get()->PostTask(
1195 FROM_HERE, base::Bind(&MockTCPClientSocket::RunReadCallback,
1196 weak_factory_.GetWeakPtr(), callback, result));
1197 }
1198
1132 // static 1199 // static
1133 void MockSSLClientSocket::ConnectCallback( 1200 void MockSSLClientSocket::ConnectCallback(
1134 MockSSLClientSocket* ssl_client_socket, 1201 MockSSLClientSocket* ssl_client_socket,
1135 const CompletionCallback& callback, 1202 const CompletionCallback& callback,
1136 int rv) { 1203 int rv) {
1137 if (rv == OK) 1204 if (rv == OK)
1138 ssl_client_socket->connected_ = true; 1205 ssl_client_socket->connected_ = true;
1139 callback.Run(rv); 1206 callback.Run(rv);
1140 } 1207 }
1141 1208
(...skipping 15 matching lines...) Expand all
1157 1224
1158 MockSSLClientSocket::~MockSSLClientSocket() { 1225 MockSSLClientSocket::~MockSSLClientSocket() {
1159 Disconnect(); 1226 Disconnect();
1160 } 1227 }
1161 1228
1162 int MockSSLClientSocket::Read(IOBuffer* buf, int buf_len, 1229 int MockSSLClientSocket::Read(IOBuffer* buf, int buf_len,
1163 const CompletionCallback& callback) { 1230 const CompletionCallback& callback) {
1164 return transport_->socket()->Read(buf, buf_len, callback); 1231 return transport_->socket()->Read(buf, buf_len, callback);
1165 } 1232 }
1166 1233
1234 int MockSSLClientSocket::ReadIfReady(IOBuffer* buf,
1235 int buf_len,
1236 const CompletionCallback& callback) {
1237 return transport_->socket()->ReadIfReady(buf, buf_len, callback);
1238 }
1239
1167 int MockSSLClientSocket::Write(IOBuffer* buf, int buf_len, 1240 int MockSSLClientSocket::Write(IOBuffer* buf, int buf_len,
1168 const CompletionCallback& callback) { 1241 const CompletionCallback& callback) {
1169 return transport_->socket()->Write(buf, buf_len, callback); 1242 return transport_->socket()->Write(buf, buf_len, callback);
1170 } 1243 }
1171 1244
1172 int MockSSLClientSocket::Connect(const CompletionCallback& callback) { 1245 int MockSSLClientSocket::Connect(const CompletionCallback& callback) {
1173 int rv = transport_->socket()->Connect( 1246 int rv = transport_->socket()->Connect(
1174 base::Bind(&ConnectCallback, base::Unretained(this), callback)); 1247 base::Bind(&ConnectCallback, base::Unretained(this), callback));
1175 if (rv == OK) { 1248 if (rv == OK) {
1176 if (data_->connect.result == OK) 1249 if (data_->connect.result == OK)
(...skipping 567 matching lines...) Expand 10 before | Expand all | Expand 10 after
1744 } 1817 }
1745 1818
1746 int64_t CountWriteBytes(const MockWrite writes[], size_t writes_size) { 1819 int64_t CountWriteBytes(const MockWrite writes[], size_t writes_size) {
1747 int64_t total = 0; 1820 int64_t total = 0;
1748 for (const MockWrite* write = writes; write != writes + writes_size; ++write) 1821 for (const MockWrite* write = writes; write != writes + writes_size; ++write)
1749 total += write->data_len; 1822 total += write->data_len;
1750 return total; 1823 return total;
1751 } 1824 }
1752 1825
1753 } // namespace net 1826 } // namespace net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698