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

Side by Side Diff: remoting/host/security_key/security_key_auth_handler_posix.cc

Issue 2562473002: Fixing SecurityKeySocket unit test issues and cleanup (Closed)
Patch Set: Addressing CR Feedback #2 Created 4 years 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
« no previous file with comments | « no previous file | remoting/host/security_key/security_key_auth_handler_posix_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 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 "remoting/host/security_key/security_key_auth_handler.h" 5 #include "remoting/host/security_key/security_key_auth_handler.h"
6 6
7 #include <unistd.h> 7 #include <unistd.h>
8 8
9 #include <cstdint> 9 #include <cstdint>
10 #include <map> 10 #include <map>
(...skipping 174 matching lines...) Expand 10 before | Expand all | Expand 10 after
185 new net::UnixDomainServerSocket(base::Bind(MatchUid), false)); 185 new net::UnixDomainServerSocket(base::Bind(MatchUid), false));
186 int rv = auth_socket_->BindAndListen(g_security_key_socket_name.Get().value(), 186 int rv = auth_socket_->BindAndListen(g_security_key_socket_name.Get().value(),
187 /*backlog=*/1); 187 /*backlog=*/1);
188 if (rv != net::OK) { 188 if (rv != net::OK) {
189 LOG(ERROR) << "Failed to open socket for auth requests: '" << rv << "'"; 189 LOG(ERROR) << "Failed to open socket for auth requests: '" << rv << "'";
190 return; 190 return;
191 } 191 }
192 DoAccept(); 192 DoAccept();
193 } 193 }
194 194
195 bool SecurityKeyAuthHandlerPosix::IsValidConnectionId( 195 bool SecurityKeyAuthHandlerPosix::IsValidConnectionId(int connection_id) const {
196 int security_key_connection_id) const { 196 return GetSocketForConnectionId(connection_id) != active_sockets_.end();
197 return GetSocketForConnectionId(security_key_connection_id) !=
198 active_sockets_.end();
199 } 197 }
200 198
201 void SecurityKeyAuthHandlerPosix::SendClientResponse( 199 void SecurityKeyAuthHandlerPosix::SendClientResponse(
202 int security_key_connection_id, 200 int connection_id,
203 const std::string& response) { 201 const std::string& response) {
204 ActiveSockets::const_iterator iter = 202 ActiveSockets::const_iterator iter = GetSocketForConnectionId(connection_id);
205 GetSocketForConnectionId(security_key_connection_id);
206 if (iter != active_sockets_.end()) { 203 if (iter != active_sockets_.end()) {
207 iter->second->SendResponse(response); 204 iter->second->SendResponse(response);
208 } else { 205 } else {
209 LOG(WARNING) << "Unknown gnubby-auth data connection: '" 206 LOG(WARNING) << "Unknown gnubby-auth connection id: " << connection_id;
210 << security_key_connection_id << "'";
211 } 207 }
212 } 208 }
213 209
214 void SecurityKeyAuthHandlerPosix::SendErrorAndCloseConnection( 210 void SecurityKeyAuthHandlerPosix::SendErrorAndCloseConnection(int id) {
215 int security_key_connection_id) { 211 ActiveSockets::const_iterator iter = GetSocketForConnectionId(id);
216 ActiveSockets::const_iterator iter =
217 GetSocketForConnectionId(security_key_connection_id);
218 if (iter != active_sockets_.end()) { 212 if (iter != active_sockets_.end()) {
219 HOST_LOG << "Sending security key error"; 213 HOST_LOG << "Sending security key error";
220 SendErrorAndCloseActiveSocket(iter); 214 SendErrorAndCloseActiveSocket(iter);
221 } else { 215 } else {
222 LOG(WARNING) << "Unknown gnubby-auth data connection: '" 216 LOG(WARNING) << "Unknown gnubby-auth connection id: " << id;
223 << security_key_connection_id << "'";
224 } 217 }
225 } 218 }
226 219
227 void SecurityKeyAuthHandlerPosix::SetSendMessageCallback( 220 void SecurityKeyAuthHandlerPosix::SetSendMessageCallback(
228 const SendMessageCallback& callback) { 221 const SendMessageCallback& callback) {
229 send_message_callback_ = callback; 222 send_message_callback_ = callback;
230 } 223 }
231 224
232 size_t SecurityKeyAuthHandlerPosix::GetActiveConnectionCountForTest() const { 225 size_t SecurityKeyAuthHandlerPosix::GetActiveConnectionCountForTest() const {
233 return active_sockets_.size(); 226 return active_sockets_.size();
234 } 227 }
235 228
236 void SecurityKeyAuthHandlerPosix::SetRequestTimeoutForTest( 229 void SecurityKeyAuthHandlerPosix::SetRequestTimeoutForTest(
237 base::TimeDelta timeout) { 230 base::TimeDelta timeout) {
238 request_timeout_ = timeout; 231 request_timeout_ = timeout;
239 } 232 }
240 233
241 void SecurityKeyAuthHandlerPosix::DoAccept() { 234 void SecurityKeyAuthHandlerPosix::DoAccept() {
242 DCHECK(thread_checker_.CalledOnValidThread()); 235 DCHECK(thread_checker_.CalledOnValidThread());
243 int result = auth_socket_->Accept( 236 int result = auth_socket_->Accept(
244 &accept_socket_, base::Bind(&SecurityKeyAuthHandlerPosix::OnAccepted, 237 &accept_socket_, base::Bind(&SecurityKeyAuthHandlerPosix::OnAccepted,
245 base::Unretained(this))); 238 base::Unretained(this)));
246 if (result != net::ERR_IO_PENDING) 239 if (result != net::ERR_IO_PENDING) {
247 OnAccepted(result); 240 OnAccepted(result);
241 }
248 } 242 }
249 243
250 void SecurityKeyAuthHandlerPosix::OnAccepted(int result) { 244 void SecurityKeyAuthHandlerPosix::OnAccepted(int result) {
251 DCHECK(thread_checker_.CalledOnValidThread()); 245 DCHECK(thread_checker_.CalledOnValidThread());
252 DCHECK_NE(net::ERR_IO_PENDING, result); 246 DCHECK_NE(net::ERR_IO_PENDING, result);
253 247
254 if (result < 0) { 248 if (result < 0) {
255 LOG(ERROR) << "Error in accepting a new connection"; 249 LOG(ERROR) << "Error in accepting a new connection";
256 return; 250 return;
257 } 251 }
258 252
259 int security_key_connection_id = ++last_connection_id_; 253 int security_key_connection_id = ++last_connection_id_;
260 SecurityKeySocket* socket = new SecurityKeySocket( 254 SecurityKeySocket* socket = new SecurityKeySocket(
261 std::move(accept_socket_), request_timeout_, 255 std::move(accept_socket_), request_timeout_,
262 base::Bind(&SecurityKeyAuthHandlerPosix::RequestTimedOut, 256 base::Bind(&SecurityKeyAuthHandlerPosix::RequestTimedOut,
263 base::Unretained(this), security_key_connection_id)); 257 base::Unretained(this), security_key_connection_id));
264 active_sockets_[security_key_connection_id] = base::WrapUnique(socket); 258 active_sockets_[security_key_connection_id] = base::WrapUnique(socket);
265 socket->StartReadingRequest( 259 socket->StartReadingRequest(
266 base::Bind(&SecurityKeyAuthHandlerPosix::OnReadComplete, 260 base::Bind(&SecurityKeyAuthHandlerPosix::OnReadComplete,
267 base::Unretained(this), security_key_connection_id)); 261 base::Unretained(this), security_key_connection_id));
268 262
269 // Continue accepting new connections. 263 // Continue accepting new connections.
270 DoAccept(); 264 DoAccept();
271 } 265 }
272 266
273 void SecurityKeyAuthHandlerPosix::OnReadComplete( 267 void SecurityKeyAuthHandlerPosix::OnReadComplete(int connection_id) {
274 int security_key_connection_id) {
275 DCHECK(thread_checker_.CalledOnValidThread()); 268 DCHECK(thread_checker_.CalledOnValidThread());
276 269
277 ActiveSockets::const_iterator iter = 270 ActiveSockets::const_iterator iter = active_sockets_.find(connection_id);
278 active_sockets_.find(security_key_connection_id);
279 DCHECK(iter != active_sockets_.end()); 271 DCHECK(iter != active_sockets_.end());
280 std::string request_data; 272 std::string request_data;
281 if (!iter->second->GetAndClearRequestData(&request_data)) { 273 if (!iter->second->GetAndClearRequestData(&request_data)) {
282 SendErrorAndCloseActiveSocket(iter); 274 SendErrorAndCloseActiveSocket(iter);
283 return; 275 return;
284 } 276 }
285 277
286 HOST_LOG << "Received security key request: " << GetCommandCode(request_data); 278 HOST_LOG << "Received security key request: " << GetCommandCode(request_data);
287 send_message_callback_.Run(security_key_connection_id, request_data); 279 send_message_callback_.Run(connection_id, request_data);
288 280
289 iter->second->StartReadingRequest( 281 iter->second->StartReadingRequest(
290 base::Bind(&SecurityKeyAuthHandlerPosix::OnReadComplete, 282 base::Bind(&SecurityKeyAuthHandlerPosix::OnReadComplete,
291 base::Unretained(this), security_key_connection_id)); 283 base::Unretained(this), connection_id));
292 } 284 }
293 285
294 SecurityKeyAuthHandlerPosix::ActiveSockets::const_iterator 286 SecurityKeyAuthHandlerPosix::ActiveSockets::const_iterator
295 SecurityKeyAuthHandlerPosix::GetSocketForConnectionId( 287 SecurityKeyAuthHandlerPosix::GetSocketForConnectionId(int connection_id) const {
296 int security_key_connection_id) const { 288 return active_sockets_.find(connection_id);
297 return active_sockets_.find(security_key_connection_id);
298 } 289 }
299 290
300 void SecurityKeyAuthHandlerPosix::SendErrorAndCloseActiveSocket( 291 void SecurityKeyAuthHandlerPosix::SendErrorAndCloseActiveSocket(
301 const ActiveSockets::const_iterator& iter) { 292 const ActiveSockets::const_iterator& iter) {
302 iter->second->SendSshError(); 293 iter->second->SendSshError();
303 active_sockets_.erase(iter); 294 active_sockets_.erase(iter);
304 } 295 }
305 296
306 void SecurityKeyAuthHandlerPosix::RequestTimedOut( 297 void SecurityKeyAuthHandlerPosix::RequestTimedOut(int connection_id) {
307 int security_key_connection_id) { 298 HOST_LOG << "SecurityKey request timed out: " << connection_id;
308 HOST_LOG << "SecurityKey request timed out"; 299 ActiveSockets::const_iterator iter = active_sockets_.find(connection_id);
309 ActiveSockets::const_iterator iter = 300 if (iter != active_sockets_.end()) {
310 active_sockets_.find(security_key_connection_id);
311 if (iter != active_sockets_.end())
312 SendErrorAndCloseActiveSocket(iter); 301 SendErrorAndCloseActiveSocket(iter);
302 }
313 } 303 }
314 304
315 } // namespace remoting 305 } // namespace remoting
OLDNEW
« no previous file with comments | « no previous file | remoting/host/security_key/security_key_auth_handler_posix_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698