0

Jingle unit tests for issue 87336

BUG=87336
TEST=jingle_unittests --gtest_filter=ProxyResolvingClientSocketTest.ReportsBadProxies


Review URL: http://codereview.chromium.org/7802002

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@101303 0039d316-1c4b-4281-b951-d872f2087c98
This commit is contained in:
asanka@chromium.org
2011-09-15 16:31:36 +00:00
parent 7c167e7f72
commit 50f71d90a7
4 changed files with 75 additions and 18 deletions

@ -19,6 +19,7 @@
namespace notifier { namespace notifier {
ProxyResolvingClientSocket::ProxyResolvingClientSocket( ProxyResolvingClientSocket::ProxyResolvingClientSocket(
net::ClientSocketFactory* socket_factory,
const scoped_refptr<net::URLRequestContextGetter>& request_context_getter, const scoped_refptr<net::URLRequestContextGetter>& request_context_getter,
const net::SSLConfig& ssl_config, const net::SSLConfig& ssl_config,
const net::HostPortPair& dest_host_port_pair) const net::HostPortPair& dest_host_port_pair)
@ -42,7 +43,7 @@ ProxyResolvingClientSocket::ProxyResolvingClientSocket(
request_context_getter->GetURLRequestContext(); request_context_getter->GetURLRequestContext();
DCHECK(request_context); DCHECK(request_context);
net::HttpNetworkSession::Params session_params; net::HttpNetworkSession::Params session_params;
session_params.client_socket_factory = NULL; session_params.client_socket_factory = socket_factory;
session_params.host_resolver = request_context->host_resolver(); session_params.host_resolver = request_context->host_resolver();
session_params.cert_verifier = request_context->cert_verifier(); session_params.cert_verifier = request_context->cert_verifier();
// TODO(rkn): This is NULL because OriginBoundCertService is not thread safe. // TODO(rkn): This is NULL because OriginBoundCertService is not thread safe.

@ -21,6 +21,7 @@
#include "net/socket/stream_socket.h" #include "net/socket/stream_socket.h"
namespace net { namespace net {
class ClientSocketFactory;
class ClientSocketHandle; class ClientSocketHandle;
class HttpNetworkSession; class HttpNetworkSession;
class URLRequestContextGetter; class URLRequestContextGetter;
@ -31,11 +32,15 @@ namespace notifier {
class ProxyResolvingClientSocket : public net::StreamSocket { class ProxyResolvingClientSocket : public net::StreamSocket {
public: public:
// Constructs a new ProxyResolvingClientSocket. |socket_factory| is the
// ClientSocketFactory that will be used by the underlying HttpNetworkSession.
// If |socket_factory| is NULL, the default socket factory
// (net::ClientSocketFactory::GetDefaultFactory()) will be used.
ProxyResolvingClientSocket( ProxyResolvingClientSocket(
const scoped_refptr<net::URLRequestContextGetter>& net::ClientSocketFactory* socket_factory,
request_context_getter, const scoped_refptr<net::URLRequestContextGetter>& request_context_getter,
const net::SSLConfig& ssl_config, const net::SSLConfig& ssl_config,
const net::HostPortPair& dest_host_port_pair); const net::HostPortPair& dest_host_port_pair);
virtual ~ProxyResolvingClientSocket(); virtual ~ProxyResolvingClientSocket();
// net::StreamSocket implementation. // net::StreamSocket implementation.

@ -6,9 +6,11 @@
#include "base/basictypes.h" #include "base/basictypes.h"
#include "base/message_loop.h" #include "base/message_loop.h"
#include "net/base/mock_host_resolver.h"
#include "net/base/test_completion_callback.h"
#include "net/socket/socket_test_util.h"
#include "net/url_request/url_request_context_getter.h" #include "net/url_request/url_request_context_getter.h"
#include "net/url_request/url_request_test_util.h" #include "net/url_request/url_request_test_util.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h" #include "testing/gtest/include/gtest/gtest.h"
namespace { namespace {
@ -24,7 +26,7 @@ class TestURLRequestContextGetter : public net::URLRequestContextGetter {
// net::URLRequestContextGetter: // net::URLRequestContextGetter:
virtual net::URLRequestContext* GetURLRequestContext() { virtual net::URLRequestContext* GetURLRequestContext() {
if (!context_) if (!context_)
context_ = new TestURLRequestContext(); CreateURLRequestContext();
return context_.get(); return context_.get();
} }
virtual scoped_refptr<base::MessageLoopProxy> GetIOMessageLoopProxy() const { virtual scoped_refptr<base::MessageLoopProxy> GetIOMessageLoopProxy() const {
@ -32,6 +34,13 @@ class TestURLRequestContextGetter : public net::URLRequestContextGetter {
} }
private: private:
void CreateURLRequestContext() {
context_ = new TestURLRequestContext();
context_->set_host_resolver(new net::MockHostResolver());
context_->set_proxy_service(net::ProxyService::CreateFixedFromPacResult(
"PROXY bad:99; PROXY maybe:80; DIRECT"));
}
scoped_refptr<net::URLRequestContext> context_; scoped_refptr<net::URLRequestContext> context_;
scoped_refptr<base::MessageLoopProxy> message_loop_proxy_; scoped_refptr<base::MessageLoopProxy> message_loop_proxy_;
}; };
@ -42,9 +51,7 @@ namespace notifier {
class ProxyResolvingClientSocketTest : public testing::Test { class ProxyResolvingClientSocketTest : public testing::Test {
protected: protected:
ProxyResolvingClientSocketTest() ProxyResolvingClientSocketTest()
: url_request_context_getter_(new TestURLRequestContextGetter()), : url_request_context_getter_(new TestURLRequestContextGetter()) {}
connect_callback_(ALLOW_THIS_IN_INITIALIZER_LIST(this),
&ProxyResolvingClientSocketTest::NetCallback) { }
virtual ~ProxyResolvingClientSocketTest() {} virtual ~ProxyResolvingClientSocketTest() {}
@ -54,28 +61,71 @@ class ProxyResolvingClientSocketTest : public testing::Test {
message_loop_.RunAllPending(); message_loop_.RunAllPending();
} }
MOCK_METHOD1(NetCallback, void(int status));
// Needed by XmppConnection. // Needed by XmppConnection.
MessageLoopForIO message_loop_; MessageLoopForIO message_loop_;
scoped_refptr<TestURLRequestContextGetter> url_request_context_getter_; scoped_refptr<TestURLRequestContextGetter> url_request_context_getter_;
net::CompletionCallbackImpl<ProxyResolvingClientSocketTest> connect_callback_;
}; };
// TODO(sanjeevr): Fix this test on Linux. // TODO(sanjeevr): Fix this test on Linux.
TEST_F(ProxyResolvingClientSocketTest, DISABLED_ConnectError) { TEST_F(ProxyResolvingClientSocketTest, DISABLED_ConnectError) {
net::HostPortPair dest("0.0.0.0", 0); net::HostPortPair dest("0.0.0.0", 0);
ProxyResolvingClientSocket proxy_resolving_socket( ProxyResolvingClientSocket proxy_resolving_socket(
NULL,
url_request_context_getter_, url_request_context_getter_,
net::SSLConfig(), net::SSLConfig(),
dest); dest);
TestCompletionCallback callback;
int status = proxy_resolving_socket.Connect(&callback);
// Connect always returns ERR_IO_PENDING because it is always asynchronous.
EXPECT_EQ(net::ERR_IO_PENDING, status);
status = callback.WaitForResult();
// ProxyResolvingClientSocket::Connect() will always return an error of // ProxyResolvingClientSocket::Connect() will always return an error of
// ERR_ADDRESS_INVALID for a 0 IP address. // ERR_ADDRESS_INVALID for a 0 IP address.
EXPECT_CALL(*this, NetCallback(net::ERR_ADDRESS_INVALID)).Times(1); EXPECT_EQ(net::ERR_ADDRESS_INVALID, status);
int status = proxy_resolving_socket.Connect(&connect_callback_); }
// Connect always returns ERR_IO_PENDING because it is always asynchronous.
EXPECT_EQ(status, net::ERR_IO_PENDING); TEST_F(ProxyResolvingClientSocketTest, ReportsBadProxies) {
message_loop_.RunAllPending(); net::HostPortPair dest("example.com", 443);
net::MockClientSocketFactory socket_factory;
net::StaticSocketDataProvider socket_data1;
socket_data1.set_connect_data(
net::MockConnect(true, net::ERR_ADDRESS_UNREACHABLE));
socket_factory.AddSocketDataProvider(&socket_data1);
net::MockRead reads[] = {
net::MockRead("HTTP/1.1 200 Success\r\n\r\n")
};
net::MockWrite writes[] = {
net::MockWrite("CONNECT example.com:443 HTTP/1.1\r\n"
"Host: example.com:443\r\n"
"Proxy-Connection: keep-alive\r\n\r\n")
};
net::StaticSocketDataProvider socket_data2(reads, arraysize(reads),
writes, arraysize(writes));
socket_data2.set_connect_data(net::MockConnect(true, net::OK));
socket_factory.AddSocketDataProvider(&socket_data2);
ProxyResolvingClientSocket proxy_resolving_socket(
&socket_factory,
url_request_context_getter_,
net::SSLConfig(),
dest);
TestCompletionCallback callback;
int status = proxy_resolving_socket.Connect(&callback);
EXPECT_EQ(net::ERR_IO_PENDING, status);
status = callback.WaitForResult();
EXPECT_EQ(net::OK, status);
net::URLRequestContext* context =
url_request_context_getter_->GetURLRequestContext();
const net::ProxyRetryInfoMap& retry_info =
context->proxy_service()->proxy_retry_info();
EXPECT_EQ(1u, retry_info.size());
net::ProxyRetryInfoMap::const_iterator iter = retry_info.find("bad:99");
EXPECT_TRUE(iter != retry_info.end());
} }
// TODO(sanjeevr): Add more unit-tests. // TODO(sanjeevr): Add more unit-tests.

@ -31,6 +31,7 @@ XmppClientSocketFactory::~XmppClientSocketFactory() {}
net::StreamSocket* XmppClientSocketFactory::CreateTransportClientSocket( net::StreamSocket* XmppClientSocketFactory::CreateTransportClientSocket(
const net::HostPortPair& host_and_port) { const net::HostPortPair& host_and_port) {
net::StreamSocket* transport_socket = new ProxyResolvingClientSocket( net::StreamSocket* transport_socket = new ProxyResolvingClientSocket(
NULL,
request_context_getter_, request_context_getter_,
ssl_config_, ssl_config_,
host_and_port); host_and_port);