OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "base/bind.h" | |
6 #include "base/message_loop/message_loop.h" | |
7 #include "base/run_loop.h" | |
8 #include "blimp/net/blimp_connection.h" | |
9 #include "blimp/net/message_port.h" | |
10 #include "blimp/net/ssl_client_transport.h" | |
11 #include "net/base/address_list.h" | |
12 #include "net/base/ip_address.h" | |
13 #include "net/base/net_errors.h" | |
14 #include "net/socket/socket_test_util.h" | |
15 #include "testing/gmock/include/gmock/gmock.h" | |
16 #include "testing/gtest/include/gtest/gtest.h" | |
17 | |
18 namespace blimp { | |
19 namespace { | |
20 | |
21 const uint8_t kIPV4Address[] = {127, 0, 0, 1}; | |
22 const uint16_t kPort = 6667; | |
23 | |
24 } // namespace | |
25 | |
26 class SSLClientTransportTest : public testing::Test { | |
27 public: | |
28 SSLClientTransportTest() {} | |
29 | |
30 ~SSLClientTransportTest() override {} | |
31 | |
32 void TearDown() override { base::RunLoop().RunUntilIdle(); } | |
33 | |
34 MOCK_METHOD1(ConnectComplete, void(int)); | |
35 | |
36 protected: | |
37 // Methods for provisioning simulated connection outcomes. | |
38 void SetupTCPSyncSocketConnect(net::IPEndPoint endpoint, int result) { | |
39 tcp_connect_.set_connect_data( | |
40 net::MockConnect(net::SYNCHRONOUS, result, endpoint)); | |
41 socket_factory_.AddSocketDataProvider(&tcp_connect_); | |
42 } | |
43 | |
44 void SetupTCPAsyncSocketConnect(net::IPEndPoint endpoint, int result) { | |
45 tcp_connect_.set_connect_data( | |
46 net::MockConnect(net::ASYNC, result, endpoint)); | |
47 socket_factory_.AddSocketDataProvider(&tcp_connect_); | |
48 } | |
49 | |
50 void SetupSSLSyncSocketConnect(int result) { | |
51 ssl_connect_.reset( | |
52 new net::SSLSocketDataProvider(net::SYNCHRONOUS, result)); | |
53 socket_factory_.AddSSLSocketDataProvider(ssl_connect_.get()); | |
54 } | |
55 | |
56 void SetupSSLAsyncSocketConnect(int result) { | |
57 ssl_connect_.reset(new net::SSLSocketDataProvider(net::ASYNC, result)); | |
58 socket_factory_.AddSSLSocketDataProvider(ssl_connect_.get()); | |
59 } | |
60 | |
61 void ConfigureTransport(const net::IPEndPoint& ip_endpoint) { | |
62 // The mock does not interact with the cert directly, so just leave it null. | |
63 scoped_refptr<net::X509Certificate> cert; | |
64 transport_.reset(new SSLClientTransport(ip_endpoint, cert, &net_log_)); | |
65 transport_->SetClientSocketFactoryForTest(&socket_factory_); | |
66 } | |
67 | |
68 base::MessageLoop message_loop; | |
69 net::NetLog net_log_; | |
70 net::StaticSocketDataProvider tcp_connect_; | |
71 std::unique_ptr<net::SSLSocketDataProvider> ssl_connect_; | |
72 net::MockClientSocketFactory socket_factory_; | |
73 std::unique_ptr<SSLClientTransport> transport_; | |
74 }; | |
75 | |
76 TEST_F(SSLClientTransportTest, ConnectSyncOK) { | |
77 net::IPEndPoint endpoint(kIPV4Address, kPort); | |
78 ConfigureTransport(endpoint); | |
79 for (int i = 0; i < 5; ++i) { | |
80 EXPECT_CALL(*this, ConnectComplete(net::OK)); | |
81 SetupTCPSyncSocketConnect(endpoint, net::OK); | |
82 SetupSSLSyncSocketConnect(net::OK); | |
83 transport_->Connect(base::Bind(&SSLClientTransportTest::ConnectComplete, | |
84 base::Unretained(this))); | |
85 EXPECT_NE(nullptr, transport_->TakeMessagePort().get()); | |
86 base::RunLoop().RunUntilIdle(); | |
87 } | |
88 } | |
89 | |
90 TEST_F(SSLClientTransportTest, ConnectAsyncOK) { | |
91 net::IPEndPoint endpoint(kIPV4Address, kPort); | |
92 ConfigureTransport(endpoint); | |
93 for (int i = 0; i < 5; ++i) { | |
94 EXPECT_CALL(*this, ConnectComplete(net::OK)); | |
95 SetupTCPAsyncSocketConnect(endpoint, net::OK); | |
96 SetupSSLAsyncSocketConnect(net::OK); | |
97 transport_->Connect(base::Bind(&SSLClientTransportTest::ConnectComplete, | |
98 base::Unretained(this))); | |
99 base::RunLoop().RunUntilIdle(); | |
100 EXPECT_NE(nullptr, transport_->TakeMessagePort().get()); | |
101 } | |
102 } | |
103 | |
104 TEST_F(SSLClientTransportTest, ConnectSyncTCPError) { | |
105 net::IPEndPoint endpoint(kIPV4Address, kPort); | |
106 ConfigureTransport(endpoint); | |
107 EXPECT_CALL(*this, ConnectComplete(net::ERR_FAILED)); | |
108 SetupTCPSyncSocketConnect(endpoint, net::ERR_FAILED); | |
109 transport_->Connect(base::Bind(&SSLClientTransportTest::ConnectComplete, | |
110 base::Unretained(this))); | |
111 } | |
112 | |
113 TEST_F(SSLClientTransportTest, ConnectAsyncTCPError) { | |
114 net::IPEndPoint endpoint(kIPV4Address, kPort); | |
115 ConfigureTransport(endpoint); | |
116 EXPECT_CALL(*this, ConnectComplete(net::ERR_FAILED)); | |
117 SetupTCPAsyncSocketConnect(endpoint, net::ERR_FAILED); | |
118 transport_->Connect(base::Bind(&SSLClientTransportTest::ConnectComplete, | |
119 base::Unretained(this))); | |
120 } | |
121 | |
122 TEST_F(SSLClientTransportTest, ConnectSyncSSLError) { | |
123 net::IPEndPoint endpoint(kIPV4Address, kPort); | |
124 ConfigureTransport(endpoint); | |
125 EXPECT_CALL(*this, ConnectComplete(net::ERR_FAILED)); | |
126 SetupTCPSyncSocketConnect(endpoint, net::OK); | |
127 SetupSSLSyncSocketConnect(net::ERR_FAILED); | |
128 transport_->Connect(base::Bind(&SSLClientTransportTest::ConnectComplete, | |
129 base::Unretained(this))); | |
130 } | |
131 | |
132 TEST_F(SSLClientTransportTest, ConnectAsyncSSLError) { | |
133 net::IPEndPoint endpoint(kIPV4Address, kPort); | |
134 ConfigureTransport(endpoint); | |
135 EXPECT_CALL(*this, ConnectComplete(net::ERR_FAILED)); | |
136 SetupTCPAsyncSocketConnect(endpoint, net::OK); | |
137 SetupSSLAsyncSocketConnect(net::ERR_FAILED); | |
138 transport_->Connect(base::Bind(&SSLClientTransportTest::ConnectComplete, | |
139 base::Unretained(this))); | |
140 } | |
141 | |
142 TEST_F(SSLClientTransportTest, ConnectAfterError) { | |
143 net::IPEndPoint endpoint(kIPV4Address, kPort); | |
144 ConfigureTransport(endpoint); | |
145 | |
146 // TCP connection fails. | |
147 EXPECT_CALL(*this, ConnectComplete(net::ERR_FAILED)); | |
148 SetupTCPSyncSocketConnect(endpoint, net::ERR_FAILED); | |
149 transport_->Connect(base::Bind(&SSLClientTransportTest::ConnectComplete, | |
150 base::Unretained(this))); | |
151 base::RunLoop().RunUntilIdle(); | |
152 | |
153 // Subsequent TCP+SSL connections succeed. | |
154 EXPECT_CALL(*this, ConnectComplete(net::OK)); | |
155 SetupTCPSyncSocketConnect(endpoint, net::OK); | |
156 SetupSSLSyncSocketConnect(net::OK); | |
157 transport_->Connect(base::Bind(&SSLClientTransportTest::ConnectComplete, | |
158 base::Unretained(this))); | |
159 EXPECT_NE(nullptr, transport_->TakeMessagePort().get()); | |
160 base::RunLoop().RunUntilIdle(); | |
161 } | |
162 | |
163 } // namespace blimp | |
OLD | NEW |