blob: 792d2d81161252a82c0fc1ed57461eec44a2960e [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"
[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"
30#include "net/base/net_errors.h"
31#include "net/base/net_log.h"
32#include "net/base/ssl_config_service.h"
33#include "net/base/x509_certificate.h"
34#include "net/socket/client_socket.h"
35#include "net/socket/client_socket_factory.h"
36#include "net/socket/socket_test_util.h"
37#include "net/socket/ssl_client_socket.h"
38#include "testing/gtest/include/gtest/gtest.h"
39#include "testing/platform_test.h"
40
41namespace net {
42
43namespace {
44
45class FakeDataChannel {
46 public:
47 FakeDataChannel() : read_callback_(NULL), read_buf_len_(0) {
48 }
49
50 virtual int Read(IOBuffer* buf, int buf_len,
51 CompletionCallback* callback) {
52 if (data_.empty()) {
53 read_callback_ = callback;
54 read_buf_ = buf;
55 read_buf_len_ = buf_len;
56 return net::ERR_IO_PENDING;
57 }
58 return PropogateData(buf, buf_len);
59 }
60
61 virtual int Write(IOBuffer* buf, int buf_len,
62 CompletionCallback* callback) {
63 data_.push(new net::DrainableIOBuffer(buf, buf_len));
64 DoReadCallback();
65 return buf_len;
66 }
67
68 private:
69 void DoReadCallback() {
70 if (!read_callback_)
71 return;
72
73 int copied = PropogateData(read_buf_, read_buf_len_);
74 net::CompletionCallback* callback = read_callback_;
75 read_callback_ = NULL;
76 read_buf_ = NULL;
77 read_buf_len_ = 0;
78 callback->Run(copied);
79 }
80
81 int PropogateData(scoped_refptr<net::IOBuffer> read_buf, int read_buf_len) {
82 scoped_refptr<net::DrainableIOBuffer> buf = data_.front();
83 int copied = std::min(buf->BytesRemaining(), read_buf_len);
84 memcpy(read_buf->data(), buf->data(), copied);
85 buf->DidConsume(copied);
86
87 if (!buf->BytesRemaining())
88 data_.pop();
89 return copied;
90 }
91
92 net::CompletionCallback* read_callback_;
93 scoped_refptr<net::IOBuffer> read_buf_;
94 int read_buf_len_;
95
96 std::queue<scoped_refptr<net::DrainableIOBuffer> > data_;
97
98 DISALLOW_COPY_AND_ASSIGN(FakeDataChannel);
99};
100
101class FakeSocket : public ClientSocket {
102 public:
103 FakeSocket(FakeDataChannel* incoming_channel,
104 FakeDataChannel* outgoing_channel)
105 : incoming_(incoming_channel),
106 outgoing_(outgoing_channel) {
107 }
108
109 virtual ~FakeSocket() {
110
111 }
112
113 virtual int Read(IOBuffer* buf, int buf_len,
114 CompletionCallback* callback) {
115 return incoming_->Read(buf, buf_len, callback);
116 }
117
118 virtual int Write(IOBuffer* buf, int buf_len,
119 CompletionCallback* callback) {
120 return outgoing_->Write(buf, buf_len, callback);
121 }
122
123 virtual bool SetReceiveBufferSize(int32 size) {
124 return true;
125 }
126
127 virtual bool SetSendBufferSize(int32 size) {
128 return true;
129 }
130
131 virtual int Connect(CompletionCallback* callback) {
132 return net::OK;
133 }
134
135 virtual void Disconnect() {}
136
137 virtual bool IsConnected() const {
138 return true;
139 }
140
141 virtual bool IsConnectedAndIdle() const {
142 return true;
143 }
144
145 virtual int GetPeerAddress(AddressList* address) const {
146 net::IPAddressNumber ip_address(4);
147 *address = net::AddressList(ip_address, 0, false);
148 return net::OK;
149 }
150
151 virtual const BoundNetLog& NetLog() const {
152 return net_log_;
153 }
154
155 virtual void SetSubresourceSpeculation() {}
156 virtual void SetOmniboxSpeculation() {}
157
158 virtual bool WasEverUsed() const {
159 return true;
160 }
161
162 virtual bool UsingTCPFastOpen() const {
163 return false;
164 }
165
166 private:
167 net::BoundNetLog net_log_;
168 FakeDataChannel* incoming_;
169 FakeDataChannel* outgoing_;
170
171 DISALLOW_COPY_AND_ASSIGN(FakeSocket);
172};
173
174} // namespace
175
176// Verify the correctness of the test helper classes first.
177TEST(FakeSocketTest, DataTransfer) {
178 // Establish channels between two sockets.
179 FakeDataChannel channel_1;
180 FakeDataChannel channel_2;
181 FakeSocket client(&channel_1, &channel_2);
182 FakeSocket server(&channel_2, &channel_1);
183
184 const char kTestData[] = "testing123";
185 const int kTestDataSize = strlen(kTestData);
186 const int kReadBufSize = 1024;
187 scoped_refptr<net::IOBuffer> write_buf = new net::StringIOBuffer(kTestData);
188 scoped_refptr<net::IOBuffer> read_buf = new net::IOBuffer(kReadBufSize);
189
190 // Write then read.
191 EXPECT_EQ(kTestDataSize, server.Write(write_buf, kTestDataSize, NULL));
192 EXPECT_EQ(kTestDataSize, client.Read(read_buf, kReadBufSize, NULL));
193 EXPECT_EQ(0, memcmp(kTestData, read_buf->data(), kTestDataSize));
194
195 // Read then write.
196 TestCompletionCallback callback;
197 EXPECT_EQ(net::ERR_IO_PENDING,
198 server.Read(read_buf, kReadBufSize, &callback));
199 EXPECT_EQ(kTestDataSize, client.Write(write_buf, kTestDataSize, NULL));
200 EXPECT_EQ(kTestDataSize, callback.WaitForResult());
201 EXPECT_EQ(0, memcmp(kTestData, read_buf->data(), kTestDataSize));
202}
203
204class SSLServerSocketTest : public PlatformTest {
205 public:
206 SSLServerSocketTest()
207 : socket_factory_(net::ClientSocketFactory::GetDefaultFactory()) {
208 }
209
210 protected:
211 void Initialize() {
212 FakeSocket* fake_client_socket = new FakeSocket(&channel_1_, &channel_2_);
213 FakeSocket* fake_server_socket = new FakeSocket(&channel_2_, &channel_1_);
214
215 FilePath certs_dir;
216 PathService::Get(base::DIR_SOURCE_ROOT, &certs_dir);
217 certs_dir = certs_dir.AppendASCII("net");
218 certs_dir = certs_dir.AppendASCII("data");
219 certs_dir = certs_dir.AppendASCII("ssl");
220 certs_dir = certs_dir.AppendASCII("certificates");
221
222 FilePath cert_path = certs_dir.AppendASCII("unittest.selfsigned.der");
223 std::string cert_der;
224 ASSERT_TRUE(file_util::ReadFileToString(cert_path, &cert_der));
225
226 scoped_refptr<net::X509Certificate> cert =
227 X509Certificate::CreateFromBytes(cert_der.data(), cert_der.size());
228
229 FilePath key_path = certs_dir.AppendASCII("unittest.key.bin");
230 std::string key_string;
231 ASSERT_TRUE(file_util::ReadFileToString(key_path, &key_string));
232 std::vector<uint8> key_vector(
233 reinterpret_cast<const uint8*>(key_string.data()),
234 reinterpret_cast<const uint8*>(key_string.data() +
235 key_string.length()));
236
237 scoped_ptr<base::RSAPrivateKey> private_key(
238 base::RSAPrivateKey::CreateFromPrivateKeyInfo(key_vector));
239
240 net::SSLConfig ssl_config;
241 ssl_config.false_start_enabled = false;
242 ssl_config.snap_start_enabled = false;
243 ssl_config.ssl3_enabled = true;
244 ssl_config.tls1_enabled = true;
[email protected]f61c3972010-12-23 09:54:15245
246 // Certificate provided by the host doesn't need authority.
247 net::SSLConfig::CertAndStatus cert_and_status;
[email protected]bd707a62011-03-14 23:29:34248 cert_and_status.cert_status = net::CERT_STATUS_AUTHORITY_INVALID;
[email protected]f61c3972010-12-23 09:54:15249 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