blob: b3d1040d6d111ca3d05d09e65ebf973d29e2ef27 [file] [log] [blame]
[email protected]4b559b4d2011-04-14 17:37:141// Copyright (c) 2011 The Chromium Authors. All rights reserved.
[email protected]f61c3972010-12-23 09:54:152// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5// This test suite uses SSLClientSocket to test the implementation of
6// SSLServerSocket. In order to establish connections between the sockets
7// we need two additional classes:
8// 1. FakeSocket
9// Connects SSL socket to FakeDataChannel. This class is just a stub.
10//
11// 2. FakeDataChannel
12// Implements the actual exchange of data between two FakeSockets.
13//
14// Implementations of these two classes are included in this file.
15
16#include "net/socket/ssl_server_socket.h"
17
18#include <queue>
19
[email protected]f61c3972010-12-23 09:54:1520#include "base/file_path.h"
21#include "base/file_util.h"
[email protected]f61c3972010-12-23 09:54:1522#include "base/path_service.h"
[email protected]4b559b4d2011-04-14 17:37:1423#include "crypto/nss_util.h"
24#include "crypto/rsa_private_key.h"
[email protected]f61c3972010-12-23 09:54:1525#include "net/base/address_list.h"
[email protected]bd707a62011-03-14 23:29:3426#include "net/base/cert_status_flags.h"
[email protected]f61c3972010-12-23 09:54:1527#include "net/base/cert_verifier.h"
28#include "net/base/host_port_pair.h"
29#include "net/base/io_buffer.h"
[email protected]e7f74da2011-04-19 23:49:3530#include "net/base/ip_endpoint.h"
[email protected]f61c3972010-12-23 09:54:1531#include "net/base/net_errors.h"
32#include "net/base/net_log.h"
33#include "net/base/ssl_config_service.h"
[email protected]4dc832e2011-04-28 22:04:2434#include "net/base/ssl_info.h"
[email protected]f61c3972010-12-23 09:54:1535#include "net/base/x509_certificate.h"
[email protected]f61c3972010-12-23 09:54:1536#include "net/socket/client_socket_factory.h"
37#include "net/socket/socket_test_util.h"
38#include "net/socket/ssl_client_socket.h"
[email protected]3268023f2011-05-05 00:08:1039#include "net/socket/stream_socket.h"
[email protected]f61c3972010-12-23 09:54:1540#include "testing/gtest/include/gtest/gtest.h"
41#include "testing/platform_test.h"
42
43namespace net {
44
45namespace {
46
47class FakeDataChannel {
48 public:
49 FakeDataChannel() : read_callback_(NULL), read_buf_len_(0) {
50 }
51
52 virtual int Read(IOBuffer* buf, int buf_len,
53 CompletionCallback* callback) {
54 if (data_.empty()) {
55 read_callback_ = callback;
56 read_buf_ = buf;
57 read_buf_len_ = buf_len;
58 return net::ERR_IO_PENDING;
59 }
60 return PropogateData(buf, buf_len);
61 }
62
63 virtual int Write(IOBuffer* buf, int buf_len,
64 CompletionCallback* callback) {
65 data_.push(new net::DrainableIOBuffer(buf, buf_len));
66 DoReadCallback();
67 return buf_len;
68 }
69
70 private:
71 void DoReadCallback() {
72 if (!read_callback_)
73 return;
74
75 int copied = PropogateData(read_buf_, read_buf_len_);
76 net::CompletionCallback* callback = read_callback_;
77 read_callback_ = NULL;
78 read_buf_ = NULL;
79 read_buf_len_ = 0;
80 callback->Run(copied);
81 }
82
83 int PropogateData(scoped_refptr<net::IOBuffer> read_buf, int read_buf_len) {
84 scoped_refptr<net::DrainableIOBuffer> buf = data_.front();
85 int copied = std::min(buf->BytesRemaining(), read_buf_len);
86 memcpy(read_buf->data(), buf->data(), copied);
87 buf->DidConsume(copied);
88
89 if (!buf->BytesRemaining())
90 data_.pop();
91 return copied;
92 }
93
94 net::CompletionCallback* read_callback_;
95 scoped_refptr<net::IOBuffer> read_buf_;
96 int read_buf_len_;
97
98 std::queue<scoped_refptr<net::DrainableIOBuffer> > data_;
99
100 DISALLOW_COPY_AND_ASSIGN(FakeDataChannel);
101};
102
[email protected]3268023f2011-05-05 00:08:10103class FakeSocket : public StreamSocket {
[email protected]f61c3972010-12-23 09:54:15104 public:
105 FakeSocket(FakeDataChannel* incoming_channel,
106 FakeDataChannel* outgoing_channel)
107 : incoming_(incoming_channel),
108 outgoing_(outgoing_channel) {
109 }
110
111 virtual ~FakeSocket() {
112
113 }
114
115 virtual int Read(IOBuffer* buf, int buf_len,
116 CompletionCallback* callback) {
117 return incoming_->Read(buf, buf_len, callback);
118 }
119
120 virtual int Write(IOBuffer* buf, int buf_len,
121 CompletionCallback* callback) {
122 return outgoing_->Write(buf, buf_len, callback);
123 }
124
125 virtual bool SetReceiveBufferSize(int32 size) {
126 return true;
127 }
128
129 virtual bool SetSendBufferSize(int32 size) {
130 return true;
131 }
132
133 virtual int Connect(CompletionCallback* callback) {
134 return net::OK;
135 }
136
137 virtual void Disconnect() {}
138
139 virtual bool IsConnected() const {
140 return true;
141 }
142
143 virtual bool IsConnectedAndIdle() const {
144 return true;
145 }
146
147 virtual int GetPeerAddress(AddressList* address) const {
148 net::IPAddressNumber ip_address(4);
149 *address = net::AddressList(ip_address, 0, false);
150 return net::OK;
151 }
152
[email protected]e7f74da2011-04-19 23:49:35153 virtual int GetLocalAddress(IPEndPoint* address) const {
154 net::IPAddressNumber ip_address(4);
155 *address = net::IPEndPoint(ip_address, 0);
156 return net::OK;
157 }
158
[email protected]f61c3972010-12-23 09:54:15159 virtual const BoundNetLog& NetLog() const {
160 return net_log_;
161 }
162
163 virtual void SetSubresourceSpeculation() {}
164 virtual void SetOmniboxSpeculation() {}
165
166 virtual bool WasEverUsed() const {
167 return true;
168 }
169
170 virtual bool UsingTCPFastOpen() const {
171 return false;
172 }
173
174 private:
175 net::BoundNetLog net_log_;
176 FakeDataChannel* incoming_;
177 FakeDataChannel* outgoing_;
178
179 DISALLOW_COPY_AND_ASSIGN(FakeSocket);
180};
181
182} // namespace
183
184// Verify the correctness of the test helper classes first.
185TEST(FakeSocketTest, DataTransfer) {
186 // Establish channels between two sockets.
187 FakeDataChannel channel_1;
188 FakeDataChannel channel_2;
189 FakeSocket client(&channel_1, &channel_2);
190 FakeSocket server(&channel_2, &channel_1);
191
192 const char kTestData[] = "testing123";
193 const int kTestDataSize = strlen(kTestData);
194 const int kReadBufSize = 1024;
195 scoped_refptr<net::IOBuffer> write_buf = new net::StringIOBuffer(kTestData);
196 scoped_refptr<net::IOBuffer> read_buf = new net::IOBuffer(kReadBufSize);
197
198 // Write then read.
199 EXPECT_EQ(kTestDataSize, server.Write(write_buf, kTestDataSize, NULL));
200 EXPECT_EQ(kTestDataSize, client.Read(read_buf, kReadBufSize, NULL));
201 EXPECT_EQ(0, memcmp(kTestData, read_buf->data(), kTestDataSize));
202
203 // Read then write.
204 TestCompletionCallback callback;
205 EXPECT_EQ(net::ERR_IO_PENDING,
206 server.Read(read_buf, kReadBufSize, &callback));
207 EXPECT_EQ(kTestDataSize, client.Write(write_buf, kTestDataSize, NULL));
208 EXPECT_EQ(kTestDataSize, callback.WaitForResult());
209 EXPECT_EQ(0, memcmp(kTestData, read_buf->data(), kTestDataSize));
210}
211
212class SSLServerSocketTest : public PlatformTest {
213 public:
214 SSLServerSocketTest()
215 : socket_factory_(net::ClientSocketFactory::GetDefaultFactory()) {
216 }
217
218 protected:
219 void Initialize() {
220 FakeSocket* fake_client_socket = new FakeSocket(&channel_1_, &channel_2_);
221 FakeSocket* fake_server_socket = new FakeSocket(&channel_2_, &channel_1_);
222
223 FilePath certs_dir;
224 PathService::Get(base::DIR_SOURCE_ROOT, &certs_dir);
225 certs_dir = certs_dir.AppendASCII("net");
226 certs_dir = certs_dir.AppendASCII("data");
227 certs_dir = certs_dir.AppendASCII("ssl");
228 certs_dir = certs_dir.AppendASCII("certificates");
229
230 FilePath cert_path = certs_dir.AppendASCII("unittest.selfsigned.der");
231 std::string cert_der;
232 ASSERT_TRUE(file_util::ReadFileToString(cert_path, &cert_der));
233
234 scoped_refptr<net::X509Certificate> cert =
235 X509Certificate::CreateFromBytes(cert_der.data(), cert_der.size());
236
237 FilePath key_path = certs_dir.AppendASCII("unittest.key.bin");
238 std::string key_string;
239 ASSERT_TRUE(file_util::ReadFileToString(key_path, &key_string));
240 std::vector<uint8> key_vector(
241 reinterpret_cast<const uint8*>(key_string.data()),
242 reinterpret_cast<const uint8*>(key_string.data() +
243 key_string.length()));
244
[email protected]4b559b4d2011-04-14 17:37:14245 scoped_ptr<crypto::RSAPrivateKey> private_key(
246 crypto::RSAPrivateKey::CreateFromPrivateKeyInfo(key_vector));
[email protected]f61c3972010-12-23 09:54:15247
248 net::SSLConfig ssl_config;
249 ssl_config.false_start_enabled = false;
[email protected]f61c3972010-12-23 09:54:15250 ssl_config.ssl3_enabled = true;
251 ssl_config.tls1_enabled = true;
[email protected]f61c3972010-12-23 09:54:15252
253 // Certificate provided by the host doesn't need authority.
254 net::SSLConfig::CertAndStatus cert_and_status;
[email protected]4dc832e2011-04-28 22:04:24255 cert_and_status.cert_status = CERT_STATUS_AUTHORITY_INVALID;
[email protected]f61c3972010-12-23 09:54:15256 cert_and_status.cert = cert;
257 ssl_config.allowed_bad_certs.push_back(cert_and_status);
258
259 net::HostPortPair host_and_pair("unittest", 0);
260 client_socket_.reset(
261 socket_factory_->CreateSSLClientSocket(
262 fake_client_socket, host_and_pair, ssl_config, NULL,
263 &cert_verifier_));
264 server_socket_.reset(net::CreateSSLServerSocket(fake_server_socket,
265 cert, private_key.get(),
266 net::SSLConfig()));
267 }
268
269 FakeDataChannel channel_1_;
270 FakeDataChannel channel_2_;
271 scoped_ptr<net::SSLClientSocket> client_socket_;
272 scoped_ptr<net::SSLServerSocket> server_socket_;
273 net::ClientSocketFactory* socket_factory_;
274 net::CertVerifier cert_verifier_;
275};
276
277// SSLServerSocket is only implemented using NSS.
278#if defined(USE_NSS) || defined(OS_WIN) || defined(OS_MACOSX)
279
280// This test only executes creation of client and server sockets. This is to
281// test that creation of sockets doesn't crash and have minimal code to run
282// under valgrind in order to help debugging memory problems.
283TEST_F(SSLServerSocketTest, Initialize) {
284 Initialize();
285}
286
287// This test executes Connect() of SSLClientSocket and Accept() of
288// SSLServerSocket to make sure handshaking between the two sockets are
289// completed successfully.
290TEST_F(SSLServerSocketTest, Handshake) {
291 Initialize();
292
[email protected]f61c3972010-12-23 09:54:15293 TestCompletionCallback connect_callback;
294 TestCompletionCallback accept_callback;
295
296 int server_ret = server_socket_->Accept(&accept_callback);
297 EXPECT_TRUE(server_ret == net::OK || server_ret == net::ERR_IO_PENDING);
298
299 int client_ret = client_socket_->Connect(&connect_callback);
300 EXPECT_TRUE(client_ret == net::OK || client_ret == net::ERR_IO_PENDING);
301
302 if (client_ret == net::ERR_IO_PENDING) {
303 EXPECT_EQ(net::OK, connect_callback.WaitForResult());
304 }
305 if (server_ret == net::ERR_IO_PENDING) {
306 EXPECT_EQ(net::OK, accept_callback.WaitForResult());
307 }
[email protected]4dc832e2011-04-28 22:04:24308
309 // Make sure the cert status is expected.
310 SSLInfo ssl_info;
311 client_socket_->GetSSLInfo(&ssl_info);
312 EXPECT_EQ(CERT_STATUS_AUTHORITY_INVALID, ssl_info.cert_status);
[email protected]f61c3972010-12-23 09:54:15313}
314
315TEST_F(SSLServerSocketTest, DataTransfer) {
316 Initialize();
317
[email protected]f61c3972010-12-23 09:54:15318 TestCompletionCallback connect_callback;
319 TestCompletionCallback accept_callback;
320
321 // Establish connection.
322 int client_ret = client_socket_->Connect(&connect_callback);
[email protected]302b6272011-01-19 01:27:22323 ASSERT_TRUE(client_ret == net::OK || client_ret == net::ERR_IO_PENDING);
[email protected]f61c3972010-12-23 09:54:15324
325 int server_ret = server_socket_->Accept(&accept_callback);
[email protected]302b6272011-01-19 01:27:22326 ASSERT_TRUE(server_ret == net::OK || server_ret == net::ERR_IO_PENDING);
[email protected]f61c3972010-12-23 09:54:15327
328 if (client_ret == net::ERR_IO_PENDING) {
[email protected]302b6272011-01-19 01:27:22329 ASSERT_EQ(net::OK, connect_callback.WaitForResult());
[email protected]f61c3972010-12-23 09:54:15330 }
331 if (server_ret == net::ERR_IO_PENDING) {
[email protected]302b6272011-01-19 01:27:22332 ASSERT_EQ(net::OK, accept_callback.WaitForResult());
[email protected]f61c3972010-12-23 09:54:15333 }
334
335 const int kReadBufSize = 1024;
336 scoped_refptr<net::StringIOBuffer> write_buf =
337 new net::StringIOBuffer("testing123");
338 scoped_refptr<net::IOBuffer> read_buf = new net::IOBuffer(kReadBufSize);
339
340 // Write then read.
341 TestCompletionCallback write_callback;
342 TestCompletionCallback read_callback;
343 server_ret = server_socket_->Write(write_buf, write_buf->size(),
344 &write_callback);
345 EXPECT_TRUE(server_ret > 0 || server_ret == net::ERR_IO_PENDING);
346 client_ret = client_socket_->Read(read_buf, kReadBufSize, &read_callback);
347 EXPECT_TRUE(client_ret > 0 || client_ret == net::ERR_IO_PENDING);
348
349 if (server_ret == net::ERR_IO_PENDING) {
350 EXPECT_GT(write_callback.WaitForResult(), 0);
351 }
352 if (client_ret == net::ERR_IO_PENDING) {
353 EXPECT_GT(read_callback.WaitForResult(), 0);
354 }
355 EXPECT_EQ(0, memcmp(write_buf->data(), read_buf->data(), write_buf->size()));
356
357 // Read then write.
358 write_buf = new net::StringIOBuffer("hello123");
359 server_ret = server_socket_->Read(read_buf, kReadBufSize, &read_callback);
360 EXPECT_TRUE(server_ret > 0 || server_ret == net::ERR_IO_PENDING);
361 client_ret = client_socket_->Write(write_buf, write_buf->size(),
362 &write_callback);
363 EXPECT_TRUE(client_ret > 0 || client_ret == net::ERR_IO_PENDING);
364
365 if (server_ret == net::ERR_IO_PENDING) {
366 EXPECT_GT(read_callback.WaitForResult(), 0);
367 }
368 if (client_ret == net::ERR_IO_PENDING) {
369 EXPECT_GT(write_callback.WaitForResult(), 0);
370 }
371 EXPECT_EQ(0, memcmp(write_buf->data(), read_buf->data(), write_buf->size()));
372}
373#endif
374
375} // namespace net