blob: ca2c884675b8256ed77be0d7b46b17fe20ab0edf [file] [log] [blame]
[email protected]f61c3972010-12-23 09:54:151// 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
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
20#include "base/crypto/rsa_private_key.h"
21#include "base/file_path.h"
22#include "base/file_util.h"
23#include "base/nss_util.h"
24#include "base/path_service.h"
25#include "net/base/address_list.h"
26#include "net/base/cert_verifier.h"
27#include "net/base/host_port_pair.h"
28#include "net/base/io_buffer.h"
29#include "net/base/net_errors.h"
30#include "net/base/net_log.h"
31#include "net/base/ssl_config_service.h"
32#include "net/base/x509_certificate.h"
33#include "net/socket/client_socket.h"
34#include "net/socket/client_socket_factory.h"
35#include "net/socket/socket_test_util.h"
36#include "net/socket/ssl_client_socket.h"
37#include "testing/gtest/include/gtest/gtest.h"
38#include "testing/platform_test.h"
39
40namespace net {
41
42namespace {
43
44class FakeDataChannel {
45 public:
46 FakeDataChannel() : read_callback_(NULL), read_buf_len_(0) {
47 }
48
49 virtual int Read(IOBuffer* buf, int buf_len,
50 CompletionCallback* callback) {
51 if (data_.empty()) {
52 read_callback_ = callback;
53 read_buf_ = buf;
54 read_buf_len_ = buf_len;
55 return net::ERR_IO_PENDING;
56 }
57 return PropogateData(buf, buf_len);
58 }
59
60 virtual int Write(IOBuffer* buf, int buf_len,
61 CompletionCallback* callback) {
62 data_.push(new net::DrainableIOBuffer(buf, buf_len));
63 DoReadCallback();
64 return buf_len;
65 }
66
67 private:
68 void DoReadCallback() {
69 if (!read_callback_)
70 return;
71
72 int copied = PropogateData(read_buf_, read_buf_len_);
73 net::CompletionCallback* callback = read_callback_;
74 read_callback_ = NULL;
75 read_buf_ = NULL;
76 read_buf_len_ = 0;
77 callback->Run(copied);
78 }
79
80 int PropogateData(scoped_refptr<net::IOBuffer> read_buf, int read_buf_len) {
81 scoped_refptr<net::DrainableIOBuffer> buf = data_.front();
82 int copied = std::min(buf->BytesRemaining(), read_buf_len);
83 memcpy(read_buf->data(), buf->data(), copied);
84 buf->DidConsume(copied);
85
86 if (!buf->BytesRemaining())
87 data_.pop();
88 return copied;
89 }
90
91 net::CompletionCallback* read_callback_;
92 scoped_refptr<net::IOBuffer> read_buf_;
93 int read_buf_len_;
94
95 std::queue<scoped_refptr<net::DrainableIOBuffer> > data_;
96
97 DISALLOW_COPY_AND_ASSIGN(FakeDataChannel);
98};
99
100class FakeSocket : public ClientSocket {
101 public:
102 FakeSocket(FakeDataChannel* incoming_channel,
103 FakeDataChannel* outgoing_channel)
104 : incoming_(incoming_channel),
105 outgoing_(outgoing_channel) {
106 }
107
108 virtual ~FakeSocket() {
109
110 }
111
112 virtual int Read(IOBuffer* buf, int buf_len,
113 CompletionCallback* callback) {
114 return incoming_->Read(buf, buf_len, callback);
115 }
116
117 virtual int Write(IOBuffer* buf, int buf_len,
118 CompletionCallback* callback) {
119 return outgoing_->Write(buf, buf_len, callback);
120 }
121
122 virtual bool SetReceiveBufferSize(int32 size) {
123 return true;
124 }
125
126 virtual bool SetSendBufferSize(int32 size) {
127 return true;
128 }
129
130 virtual int Connect(CompletionCallback* callback) {
131 return net::OK;
132 }
133
134 virtual void Disconnect() {}
135
136 virtual bool IsConnected() const {
137 return true;
138 }
139
140 virtual bool IsConnectedAndIdle() const {
141 return true;
142 }
143
144 virtual int GetPeerAddress(AddressList* address) const {
145 net::IPAddressNumber ip_address(4);
146 *address = net::AddressList(ip_address, 0, false);
147 return net::OK;
148 }
149
150 virtual const BoundNetLog& NetLog() const {
151 return net_log_;
152 }
153
154 virtual void SetSubresourceSpeculation() {}
155 virtual void SetOmniboxSpeculation() {}
156
157 virtual bool WasEverUsed() const {
158 return true;
159 }
160
161 virtual bool UsingTCPFastOpen() const {
162 return false;
163 }
164
165 private:
166 net::BoundNetLog net_log_;
167 FakeDataChannel* incoming_;
168 FakeDataChannel* outgoing_;
169
170 DISALLOW_COPY_AND_ASSIGN(FakeSocket);
171};
172
173} // namespace
174
175// Verify the correctness of the test helper classes first.
176TEST(FakeSocketTest, DataTransfer) {
177 // Establish channels between two sockets.
178 FakeDataChannel channel_1;
179 FakeDataChannel channel_2;
180 FakeSocket client(&channel_1, &channel_2);
181 FakeSocket server(&channel_2, &channel_1);
182
183 const char kTestData[] = "testing123";
184 const int kTestDataSize = strlen(kTestData);
185 const int kReadBufSize = 1024;
186 scoped_refptr<net::IOBuffer> write_buf = new net::StringIOBuffer(kTestData);
187 scoped_refptr<net::IOBuffer> read_buf = new net::IOBuffer(kReadBufSize);
188
189 // Write then read.
190 EXPECT_EQ(kTestDataSize, server.Write(write_buf, kTestDataSize, NULL));
191 EXPECT_EQ(kTestDataSize, client.Read(read_buf, kReadBufSize, NULL));
192 EXPECT_EQ(0, memcmp(kTestData, read_buf->data(), kTestDataSize));
193
194 // Read then write.
195 TestCompletionCallback callback;
196 EXPECT_EQ(net::ERR_IO_PENDING,
197 server.Read(read_buf, kReadBufSize, &callback));
198 EXPECT_EQ(kTestDataSize, client.Write(write_buf, kTestDataSize, NULL));
199 EXPECT_EQ(kTestDataSize, callback.WaitForResult());
200 EXPECT_EQ(0, memcmp(kTestData, read_buf->data(), kTestDataSize));
201}
202
203class SSLServerSocketTest : public PlatformTest {
204 public:
205 SSLServerSocketTest()
206 : socket_factory_(net::ClientSocketFactory::GetDefaultFactory()) {
207 }
208
209 protected:
210 void Initialize() {
211 FakeSocket* fake_client_socket = new FakeSocket(&channel_1_, &channel_2_);
212 FakeSocket* fake_server_socket = new FakeSocket(&channel_2_, &channel_1_);
213
214 FilePath certs_dir;
215 PathService::Get(base::DIR_SOURCE_ROOT, &certs_dir);
216 certs_dir = certs_dir.AppendASCII("net");
217 certs_dir = certs_dir.AppendASCII("data");
218 certs_dir = certs_dir.AppendASCII("ssl");
219 certs_dir = certs_dir.AppendASCII("certificates");
220
221 FilePath cert_path = certs_dir.AppendASCII("unittest.selfsigned.der");
222 std::string cert_der;
223 ASSERT_TRUE(file_util::ReadFileToString(cert_path, &cert_der));
224
225 scoped_refptr<net::X509Certificate> cert =
226 X509Certificate::CreateFromBytes(cert_der.data(), cert_der.size());
227
228 FilePath key_path = certs_dir.AppendASCII("unittest.key.bin");
229 std::string key_string;
230 ASSERT_TRUE(file_util::ReadFileToString(key_path, &key_string));
231 std::vector<uint8> key_vector(
232 reinterpret_cast<const uint8*>(key_string.data()),
233 reinterpret_cast<const uint8*>(key_string.data() +
234 key_string.length()));
235
236 scoped_ptr<base::RSAPrivateKey> private_key(
237 base::RSAPrivateKey::CreateFromPrivateKeyInfo(key_vector));
238
239 net::SSLConfig ssl_config;
240 ssl_config.false_start_enabled = false;
241 ssl_config.snap_start_enabled = false;
242 ssl_config.ssl3_enabled = true;
243 ssl_config.tls1_enabled = true;
244 ssl_config.session_resume_disabled = true;
245
246 // Certificate provided by the host doesn't need authority.
247 net::SSLConfig::CertAndStatus cert_and_status;
248 cert_and_status.cert_status = net::ERR_CERT_AUTHORITY_INVALID;
249 cert_and_status.cert = cert;
250 ssl_config.allowed_bad_certs.push_back(cert_and_status);
251
252 net::HostPortPair host_and_pair("unittest", 0);
253 client_socket_.reset(
254 socket_factory_->CreateSSLClientSocket(
255 fake_client_socket, host_and_pair, ssl_config, NULL,
256 &cert_verifier_));
257 server_socket_.reset(net::CreateSSLServerSocket(fake_server_socket,
258 cert, private_key.get(),
259 net::SSLConfig()));
260 }
261
262 FakeDataChannel channel_1_;
263 FakeDataChannel channel_2_;
264 scoped_ptr<net::SSLClientSocket> client_socket_;
265 scoped_ptr<net::SSLServerSocket> server_socket_;
266 net::ClientSocketFactory* socket_factory_;
267 net::CertVerifier cert_verifier_;
268};
269
270// SSLServerSocket is only implemented using NSS.
271#if defined(USE_NSS) || defined(OS_WIN) || defined(OS_MACOSX)
272
273// This test only executes creation of client and server sockets. This is to
274// test that creation of sockets doesn't crash and have minimal code to run
275// under valgrind in order to help debugging memory problems.
276TEST_F(SSLServerSocketTest, Initialize) {
277 Initialize();
278}
279
280// This test executes Connect() of SSLClientSocket and Accept() of
281// SSLServerSocket to make sure handshaking between the two sockets are
282// completed successfully.
283TEST_F(SSLServerSocketTest, Handshake) {
284 Initialize();
285
[email protected]f61c3972010-12-23 09:54:15286 TestCompletionCallback connect_callback;
287 TestCompletionCallback accept_callback;
288
289 int server_ret = server_socket_->Accept(&accept_callback);
290 EXPECT_TRUE(server_ret == net::OK || server_ret == net::ERR_IO_PENDING);
291
292 int client_ret = client_socket_->Connect(&connect_callback);
293 EXPECT_TRUE(client_ret == net::OK || client_ret == net::ERR_IO_PENDING);
294
295 if (client_ret == net::ERR_IO_PENDING) {
296 EXPECT_EQ(net::OK, connect_callback.WaitForResult());
297 }
298 if (server_ret == net::ERR_IO_PENDING) {
299 EXPECT_EQ(net::OK, accept_callback.WaitForResult());
300 }
301}
302
303TEST_F(SSLServerSocketTest, DataTransfer) {
304 Initialize();
305
[email protected]f61c3972010-12-23 09:54:15306 TestCompletionCallback connect_callback;
307 TestCompletionCallback accept_callback;
308
309 // Establish connection.
310 int client_ret = client_socket_->Connect(&connect_callback);
[email protected]302b6272011-01-19 01:27:22311 ASSERT_TRUE(client_ret == net::OK || client_ret == net::ERR_IO_PENDING);
[email protected]f61c3972010-12-23 09:54:15312
313 int server_ret = server_socket_->Accept(&accept_callback);
[email protected]302b6272011-01-19 01:27:22314 ASSERT_TRUE(server_ret == net::OK || server_ret == net::ERR_IO_PENDING);
[email protected]f61c3972010-12-23 09:54:15315
316 if (client_ret == net::ERR_IO_PENDING) {
[email protected]302b6272011-01-19 01:27:22317 ASSERT_EQ(net::OK, connect_callback.WaitForResult());
[email protected]f61c3972010-12-23 09:54:15318 }
319 if (server_ret == net::ERR_IO_PENDING) {
[email protected]302b6272011-01-19 01:27:22320 ASSERT_EQ(net::OK, accept_callback.WaitForResult());
[email protected]f61c3972010-12-23 09:54:15321 }
322
323 const int kReadBufSize = 1024;
324 scoped_refptr<net::StringIOBuffer> write_buf =
325 new net::StringIOBuffer("testing123");
326 scoped_refptr<net::IOBuffer> read_buf = new net::IOBuffer(kReadBufSize);
327
328 // Write then read.
329 TestCompletionCallback write_callback;
330 TestCompletionCallback read_callback;
331 server_ret = server_socket_->Write(write_buf, write_buf->size(),
332 &write_callback);
333 EXPECT_TRUE(server_ret > 0 || server_ret == net::ERR_IO_PENDING);
334 client_ret = client_socket_->Read(read_buf, kReadBufSize, &read_callback);
335 EXPECT_TRUE(client_ret > 0 || client_ret == net::ERR_IO_PENDING);
336
337 if (server_ret == net::ERR_IO_PENDING) {
338 EXPECT_GT(write_callback.WaitForResult(), 0);
339 }
340 if (client_ret == net::ERR_IO_PENDING) {
341 EXPECT_GT(read_callback.WaitForResult(), 0);
342 }
343 EXPECT_EQ(0, memcmp(write_buf->data(), read_buf->data(), write_buf->size()));
344
345 // Read then write.
346 write_buf = new net::StringIOBuffer("hello123");
347 server_ret = server_socket_->Read(read_buf, kReadBufSize, &read_callback);
348 EXPECT_TRUE(server_ret > 0 || server_ret == net::ERR_IO_PENDING);
349 client_ret = client_socket_->Write(write_buf, write_buf->size(),
350 &write_callback);
351 EXPECT_TRUE(client_ret > 0 || client_ret == net::ERR_IO_PENDING);
352
353 if (server_ret == net::ERR_IO_PENDING) {
354 EXPECT_GT(read_callback.WaitForResult(), 0);
355 }
356 if (client_ret == net::ERR_IO_PENDING) {
357 EXPECT_GT(write_callback.WaitForResult(), 0);
358 }
359 EXPECT_EQ(0, memcmp(write_buf->data(), read_buf->data(), write_buf->size()));
360}
361#endif
362
363} // namespace net