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

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

Issue 2870030: Implement SSLClientSocketPool. (Closed)
Patch Set: Rebase and fix mac compile error Created 10 years, 5 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) 2010 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2010 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
10 #include "base/basictypes.h" 11 #include "base/basictypes.h"
11 #include "base/compiler_specific.h" 12 #include "base/compiler_specific.h"
12 #include "base/message_loop.h" 13 #include "base/message_loop.h"
13 #include "base/time.h" 14 #include "base/time.h"
14 #include "net/base/address_family.h" 15 #include "net/base/address_family.h"
16 #include "net/base/auth.h"
15 #include "net/base/host_resolver_proc.h" 17 #include "net/base/host_resolver_proc.h"
16 #include "net/base/ssl_info.h" 18 #include "net/base/ssl_info.h"
19 #include "net/http/http_network_session.h"
20 #include "net/http/http_request_headers.h"
21 #include "net/http/http_response_headers.h"
17 #include "net/socket/client_socket_pool_histograms.h" 22 #include "net/socket/client_socket_pool_histograms.h"
18 #include "net/socket/socket.h" 23 #include "net/socket/socket.h"
19 #include "testing/gtest/include/gtest/gtest.h" 24 #include "testing/gtest/include/gtest/gtest.h"
20 25
21 #define NET_TRACE(level, s) DLOG(level) << s << __FUNCTION__ << "() " 26 #define NET_TRACE(level, s) DLOG(level) << s << __FUNCTION__ << "() "
22 27
23 namespace net { 28 namespace net {
24 29
25 namespace { 30 namespace {
26 31
(...skipping 300 matching lines...) Expand 10 before | Expand all | Expand 10 after
327 user_callback_->Run(rv_); 332 user_callback_->Run(rv_);
328 delete this; 333 delete this;
329 } 334 }
330 335
331 MockSSLClientSocket* ssl_client_socket_; 336 MockSSLClientSocket* ssl_client_socket_;
332 net::CompletionCallback* user_callback_; 337 net::CompletionCallback* user_callback_;
333 int rv_; 338 int rv_;
334 }; 339 };
335 340
336 MockSSLClientSocket::MockSSLClientSocket( 341 MockSSLClientSocket::MockSSLClientSocket(
337 net::ClientSocket* transport_socket, 342 net::ClientSocketHandle* transport_socket,
338 const std::string& hostname, 343 const std::string& hostname,
339 const net::SSLConfig& ssl_config, 344 const net::SSLConfig& ssl_config,
340 net::SSLSocketDataProvider* data) 345 net::SSLSocketDataProvider* data)
341 : MockClientSocket(transport_socket->NetLog().net_log()), 346 : MockClientSocket(transport_socket->socket()->NetLog().net_log()),
342 transport_(transport_socket), 347 transport_(transport_socket),
343 data_(data) { 348 data_(data),
349 is_npn_state_set_(false) {
344 DCHECK(data_); 350 DCHECK(data_);
345 } 351 }
346 352
347 MockSSLClientSocket::~MockSSLClientSocket() { 353 MockSSLClientSocket::~MockSSLClientSocket() {
348 Disconnect(); 354 Disconnect();
349 } 355 }
350 356
351 int MockSSLClientSocket::Connect(net::CompletionCallback* callback) { 357 int MockSSLClientSocket::Connect(net::CompletionCallback* callback) {
352 ConnectCallback* connect_callback = new ConnectCallback( 358 ConnectCallback* connect_callback = new ConnectCallback(
353 this, callback, data_->connect.result); 359 this, callback, data_->connect.result);
354 int rv = transport_->Connect(connect_callback); 360 int rv = transport_->socket()->Connect(connect_callback);
355 if (rv == net::OK) { 361 if (rv == net::OK) {
356 delete connect_callback; 362 delete connect_callback;
357 if (data_->connect.async) { 363 if (data_->connect.async) {
358 RunCallbackAsync(callback, data_->connect.result); 364 RunCallbackAsync(callback, data_->connect.result);
359 return net::ERR_IO_PENDING; 365 return net::ERR_IO_PENDING;
360 } 366 }
361 if (data_->connect.result == net::OK) 367 if (data_->connect.result == net::OK)
362 connected_ = true; 368 connected_ = true;
363 return data_->connect.result; 369 return data_->connect.result;
364 } 370 }
365 return rv; 371 return rv;
366 } 372 }
367 373
368 void MockSSLClientSocket::Disconnect() { 374 void MockSSLClientSocket::Disconnect() {
369 MockClientSocket::Disconnect(); 375 MockClientSocket::Disconnect();
370 if (transport_ != NULL) 376 if (transport_->socket() != NULL)
371 transport_->Disconnect(); 377 transport_->socket()->Disconnect();
372 } 378 }
373 379
374 int MockSSLClientSocket::Read(net::IOBuffer* buf, int buf_len, 380 int MockSSLClientSocket::Read(net::IOBuffer* buf, int buf_len,
375 net::CompletionCallback* callback) { 381 net::CompletionCallback* callback) {
376 return transport_->Read(buf, buf_len, callback); 382 return transport_->socket()->Read(buf, buf_len, callback);
377 } 383 }
378 384
379 int MockSSLClientSocket::Write(net::IOBuffer* buf, int buf_len, 385 int MockSSLClientSocket::Write(net::IOBuffer* buf, int buf_len,
380 net::CompletionCallback* callback) { 386 net::CompletionCallback* callback) {
381 return transport_->Write(buf, buf_len, callback); 387 return transport_->socket()->Write(buf, buf_len, callback);
382 } 388 }
383 389
384 void MockSSLClientSocket::GetSSLInfo(net::SSLInfo* ssl_info) { 390 void MockSSLClientSocket::GetSSLInfo(net::SSLInfo* ssl_info) {
385 ssl_info->Reset(); 391 ssl_info->Reset();
386 } 392 }
387 393
388 SSLClientSocket::NextProtoStatus MockSSLClientSocket::GetNextProto( 394 SSLClientSocket::NextProtoStatus MockSSLClientSocket::GetNextProto(
389 std::string* proto) { 395 std::string* proto) {
390 *proto = data_->next_proto; 396 *proto = data_->next_proto;
391 return data_->next_proto_status; 397 return data_->next_proto_status;
392 } 398 }
393 399
394 bool MockSSLClientSocket::wasNpnNegotiated() const { 400 bool MockSSLClientSocket::wasNpnNegotiated() const {
401 if (is_npn_state_set_)
402 return new_npn_value_;
395 return data_->was_npn_negotiated; 403 return data_->was_npn_negotiated;
396 } 404 }
397 405
406 bool MockSSLClientSocket::setWasNpnNegotiated(bool negotiated) {
407 is_npn_state_set_ = true;
408 return new_npn_value_ = negotiated;
409 }
410
398 MockRead StaticSocketDataProvider::GetNextRead() { 411 MockRead StaticSocketDataProvider::GetNextRead() {
399 DCHECK(!at_read_eof()); 412 DCHECK(!at_read_eof());
400 reads_[read_index_].time_stamp = base::Time::Now(); 413 reads_[read_index_].time_stamp = base::Time::Now();
401 return reads_[read_index_++]; 414 return reads_[read_index_++];
402 } 415 }
403 416
404 MockWriteResult StaticSocketDataProvider::OnWrite(const std::string& data) { 417 MockWriteResult StaticSocketDataProvider::OnWrite(const std::string& data) {
405 if (!writes_) { 418 if (!writes_) {
406 // Not using mock writes; succeed synchronously. 419 // Not using mock writes; succeed synchronously.
407 return MockWriteResult(false, data.length()); 420 return MockWriteResult(false, data.length());
(...skipping 253 matching lines...) Expand 10 before | Expand all | Expand 10 after
661 const AddressList& addresses, net::NetLog* net_log) { 674 const AddressList& addresses, net::NetLog* net_log) {
662 SocketDataProvider* data_provider = mock_data_.GetNext(); 675 SocketDataProvider* data_provider = mock_data_.GetNext();
663 MockTCPClientSocket* socket = 676 MockTCPClientSocket* socket =
664 new MockTCPClientSocket(addresses, net_log, data_provider); 677 new MockTCPClientSocket(addresses, net_log, data_provider);
665 data_provider->set_socket(socket); 678 data_provider->set_socket(socket);
666 tcp_client_sockets_.push_back(socket); 679 tcp_client_sockets_.push_back(socket);
667 return socket; 680 return socket;
668 } 681 }
669 682
670 SSLClientSocket* MockClientSocketFactory::CreateSSLClientSocket( 683 SSLClientSocket* MockClientSocketFactory::CreateSSLClientSocket(
671 ClientSocket* transport_socket, 684 ClientSocketHandle* transport_socket,
672 const std::string& hostname, 685 const std::string& hostname,
673 const SSLConfig& ssl_config) { 686 const SSLConfig& ssl_config) {
674 MockSSLClientSocket* socket = 687 MockSSLClientSocket* socket =
675 new MockSSLClientSocket(transport_socket, hostname, ssl_config, 688 new MockSSLClientSocket(transport_socket, hostname, ssl_config,
676 mock_ssl_data_.GetNext()); 689 mock_ssl_data_.GetNext());
677 ssl_client_sockets_.push_back(socket); 690 ssl_client_sockets_.push_back(socket);
678 return socket; 691 return socket;
679 } 692 }
680 693
681 int TestSocketRequest::WaitForResult() { 694 int TestSocketRequest::WaitForResult() {
(...skipping 150 matching lines...) Expand 10 before | Expand all | Expand 10 after
832 845
833 void MockTCPClientSocketPool::ReleaseSocket(const std::string& group_name, 846 void MockTCPClientSocketPool::ReleaseSocket(const std::string& group_name,
834 ClientSocket* socket, int id) { 847 ClientSocket* socket, int id) {
835 EXPECT_EQ(1, id); 848 EXPECT_EQ(1, id);
836 release_count_++; 849 release_count_++;
837 delete socket; 850 delete socket;
838 } 851 }
839 852
840 MockTCPClientSocketPool::~MockTCPClientSocketPool() {} 853 MockTCPClientSocketPool::~MockTCPClientSocketPool() {}
841 854
855 MockSOCKSClientSocketPool::MockSOCKSClientSocketPool(
856 int max_sockets,
857 int max_sockets_per_group,
858 const scoped_refptr<ClientSocketPoolHistograms>& histograms,
859 const scoped_refptr<TCPClientSocketPool>& tcp_pool)
860 : SOCKSClientSocketPool(max_sockets, max_sockets_per_group, histograms,
861 NULL, tcp_pool, NULL),
862 tcp_pool_(tcp_pool) {
863 }
864
865 int MockSOCKSClientSocketPool::RequestSocket(const std::string& group_name,
866 const void* socket_params,
867 RequestPriority priority,
868 ClientSocketHandle* handle,
869 CompletionCallback* callback,
870 const BoundNetLog& net_log) {
871 return tcp_pool_->RequestSocket(group_name, socket_params, priority, handle,
872 callback, net_log);
873 }
874
875 void MockSOCKSClientSocketPool::CancelRequest(
876 const std::string& group_name,
877 const ClientSocketHandle* handle) {
878 return tcp_pool_->CancelRequest(group_name, handle);
879 }
880
881 void MockSOCKSClientSocketPool::ReleaseSocket(const std::string& group_name,
882 ClientSocket* socket, int id) {
883 return tcp_pool_->ReleaseSocket(group_name, socket, id);
884 }
885
886 MockSOCKSClientSocketPool::~MockSOCKSClientSocketPool() {}
887
888 MockHttpAuthController::MockHttpAuthController()
889 : HttpAuthController(HttpAuth::AUTH_PROXY, GURL(),
890 scoped_refptr<HttpNetworkSession>(NULL)),
891 data_(NULL),
892 data_index_(0),
893 data_count_(0) {
894 }
895
896 void MockHttpAuthController::SetMockAuthControllerData(
897 struct MockHttpAuthControllerData* data, size_t data_length) {
898 data_ = data;
899 data_count_ = data_length;
900 }
901
902 int MockHttpAuthController::MaybeGenerateAuthToken(
903 const HttpRequestInfo* request,
904 CompletionCallback* callback,
905 const BoundNetLog& net_log) {
906 return OK;
907 }
908
909 void MockHttpAuthController::AddAuthorizationHeader(
910 HttpRequestHeaders* authorization_headers) {
911 authorization_headers->AddHeadersFromString(CurrentData().auth_header);
912 }
913
914 int MockHttpAuthController::HandleAuthChallenge(
915 scoped_refptr<HttpResponseHeaders> headers,
916 bool do_not_send_server_auth,
917 bool establishing_tunnel,
918 const BoundNetLog& net_log) {
919 return OK;
920 }
921
922 void MockHttpAuthController::ResetAuth(const std::wstring& username,
923 const std::wstring& password) {
924 data_index_++;
925 }
926
927 bool MockHttpAuthController::HaveAuth() const {
928 return CurrentData().auth_header.size() != 0;
929 }
930
931 bool MockHttpAuthController::HaveAuthHandler() const {
932 return HaveAuth();
933 }
934
842 const char kSOCKS5GreetRequest[] = { 0x05, 0x01, 0x00 }; 935 const char kSOCKS5GreetRequest[] = { 0x05, 0x01, 0x00 };
843 const int kSOCKS5GreetRequestLength = arraysize(kSOCKS5GreetRequest); 936 const int kSOCKS5GreetRequestLength = arraysize(kSOCKS5GreetRequest);
844 937
845 const char kSOCKS5GreetResponse[] = { 0x05, 0x00 }; 938 const char kSOCKS5GreetResponse[] = { 0x05, 0x00 };
846 const int kSOCKS5GreetResponseLength = arraysize(kSOCKS5GreetResponse); 939 const int kSOCKS5GreetResponseLength = arraysize(kSOCKS5GreetResponse);
847 940
848 const char kSOCKS5OkRequest[] = 941 const char kSOCKS5OkRequest[] =
849 { 0x05, 0x01, 0x00, 0x03, 0x04, 'h', 'o', 's', 't', 0x00, 0x50 }; 942 { 0x05, 0x01, 0x00, 0x03, 0x04, 'h', 'o', 's', 't', 0x00, 0x50 };
850 const int kSOCKS5OkRequestLength = arraysize(kSOCKS5OkRequest); 943 const int kSOCKS5OkRequestLength = arraysize(kSOCKS5OkRequest);
851 944
852 const char kSOCKS5OkResponse[] = 945 const char kSOCKS5OkResponse[] =
853 { 0x05, 0x00, 0x00, 0x01, 127, 0, 0, 1, 0x00, 0x50 }; 946 { 0x05, 0x00, 0x00, 0x01, 127, 0, 0, 1, 0x00, 0x50 };
854 const int kSOCKS5OkResponseLength = arraysize(kSOCKS5OkResponse); 947 const int kSOCKS5OkResponseLength = arraysize(kSOCKS5OkResponse);
855 948
856 } // namespace net 949 } // namespace net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698