blob: 11276d1f7bed027b4bdcadcc12c1f91556c3ae8a [file] [log] [blame]
[email protected]1bc6f5e2012-03-15 00:20:581// Copyright (c) 2012 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
[email protected]55ee0e52011-07-21 18:29:4418#include <stdlib.h>
19
[email protected]f61c3972010-12-23 09:54:1520#include <queue>
21
[email protected]55ee0e52011-07-21 18:29:4422#include "base/compiler_specific.h"
[email protected]f61c3972010-12-23 09:54:1523#include "base/file_path.h"
24#include "base/file_util.h"
[email protected]55ee0e52011-07-21 18:29:4425#include "base/message_loop.h"
[email protected]f61c3972010-12-23 09:54:1526#include "base/path_service.h"
[email protected]4b559b4d2011-04-14 17:37:1427#include "crypto/nss_util.h"
28#include "crypto/rsa_private_key.h"
[email protected]f61c3972010-12-23 09:54:1529#include "net/base/address_list.h"
[email protected]bd707a62011-03-14 23:29:3430#include "net/base/cert_status_flags.h"
[email protected]f61c3972010-12-23 09:54:1531#include "net/base/cert_verifier.h"
[email protected]6ea7b152011-12-21 21:21:1332#include "net/base/completion_callback.h"
[email protected]f61c3972010-12-23 09:54:1533#include "net/base/host_port_pair.h"
34#include "net/base/io_buffer.h"
[email protected]e7f74da2011-04-19 23:49:3535#include "net/base/ip_endpoint.h"
[email protected]f61c3972010-12-23 09:54:1536#include "net/base/net_errors.h"
37#include "net/base/net_log.h"
38#include "net/base/ssl_config_service.h"
[email protected]4dc832e2011-04-28 22:04:2439#include "net/base/ssl_info.h"
[email protected]f61c3972010-12-23 09:54:1540#include "net/base/x509_certificate.h"
[email protected]f61c3972010-12-23 09:54:1541#include "net/socket/client_socket_factory.h"
42#include "net/socket/socket_test_util.h"
43#include "net/socket/ssl_client_socket.h"
[email protected]3268023f2011-05-05 00:08:1044#include "net/socket/stream_socket.h"
[email protected]f61c3972010-12-23 09:54:1545#include "testing/gtest/include/gtest/gtest.h"
46#include "testing/platform_test.h"
47
48namespace net {
49
50namespace {
51
52class FakeDataChannel {
53 public:
[email protected]55ee0e52011-07-21 18:29:4454 FakeDataChannel()
[email protected]83039bb2011-12-09 18:43:5555 : read_buf_len_(0),
56 ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)) {
[email protected]f61c3972010-12-23 09:54:1557 }
58
59 virtual int Read(IOBuffer* buf, int buf_len,
[email protected]3f55aa12011-12-07 02:03:3360 const CompletionCallback& callback) {
61 if (data_.empty()) {
[email protected]f61c3972010-12-23 09:54:1562 read_callback_ = callback;
63 read_buf_ = buf;
64 read_buf_len_ = buf_len;
65 return net::ERR_IO_PENDING;
66 }
67 return PropogateData(buf, buf_len);
68 }
69
70 virtual int Write(IOBuffer* buf, int buf_len,
[email protected]83039bb2011-12-09 18:43:5571 const CompletionCallback& callback) {
[email protected]f61c3972010-12-23 09:54:1572 data_.push(new net::DrainableIOBuffer(buf, buf_len));
[email protected]55ee0e52011-07-21 18:29:4473 MessageLoop::current()->PostTask(
[email protected]83039bb2011-12-09 18:43:5574 FROM_HERE, base::Bind(&FakeDataChannel::DoReadCallback,
75 weak_factory_.GetWeakPtr()));
[email protected]f61c3972010-12-23 09:54:1576 return buf_len;
77 }
78
79 private:
80 void DoReadCallback() {
[email protected]83039bb2011-12-09 18:43:5581 if (read_callback_.is_null() || data_.empty())
[email protected]f61c3972010-12-23 09:54:1582 return;
83
84 int copied = PropogateData(read_buf_, read_buf_len_);
[email protected]83039bb2011-12-09 18:43:5585 CompletionCallback callback = read_callback_;
86 read_callback_.Reset();
87 read_buf_ = NULL;
88 read_buf_len_ = 0;
89 callback.Run(copied);
[email protected]f61c3972010-12-23 09:54:1590 }
91
92 int PropogateData(scoped_refptr<net::IOBuffer> read_buf, int read_buf_len) {
93 scoped_refptr<net::DrainableIOBuffer> buf = data_.front();
94 int copied = std::min(buf->BytesRemaining(), read_buf_len);
95 memcpy(read_buf->data(), buf->data(), copied);
96 buf->DidConsume(copied);
97
98 if (!buf->BytesRemaining())
99 data_.pop();
100 return copied;
101 }
102
[email protected]83039bb2011-12-09 18:43:55103 CompletionCallback read_callback_;
[email protected]f61c3972010-12-23 09:54:15104 scoped_refptr<net::IOBuffer> read_buf_;
105 int read_buf_len_;
106
107 std::queue<scoped_refptr<net::DrainableIOBuffer> > data_;
108
[email protected]83039bb2011-12-09 18:43:55109 base::WeakPtrFactory<FakeDataChannel> weak_factory_;
[email protected]55ee0e52011-07-21 18:29:44110
[email protected]f61c3972010-12-23 09:54:15111 DISALLOW_COPY_AND_ASSIGN(FakeDataChannel);
112};
113
[email protected]3268023f2011-05-05 00:08:10114class FakeSocket : public StreamSocket {
[email protected]f61c3972010-12-23 09:54:15115 public:
116 FakeSocket(FakeDataChannel* incoming_channel,
117 FakeDataChannel* outgoing_channel)
118 : incoming_(incoming_channel),
119 outgoing_(outgoing_channel) {
120 }
121
122 virtual ~FakeSocket() {
[email protected]f61c3972010-12-23 09:54:15123 }
124
125 virtual int Read(IOBuffer* buf, int buf_len,
[email protected]83039bb2011-12-09 18:43:55126 const CompletionCallback& callback) OVERRIDE {
[email protected]3f55aa12011-12-07 02:03:33127 // Read random number of bytes.
128 buf_len = rand() % buf_len + 1;
129 return incoming_->Read(buf, buf_len, callback);
130 }
[email protected]f61c3972010-12-23 09:54:15131
132 virtual int Write(IOBuffer* buf, int buf_len,
[email protected]83039bb2011-12-09 18:43:55133 const CompletionCallback& callback) OVERRIDE {
[email protected]55ee0e52011-07-21 18:29:44134 // Write random number of bytes.
135 buf_len = rand() % buf_len + 1;
[email protected]f61c3972010-12-23 09:54:15136 return outgoing_->Write(buf, buf_len, callback);
137 }
138
[email protected]83039bb2011-12-09 18:43:55139 virtual bool SetReceiveBufferSize(int32 size) OVERRIDE {
[email protected]f61c3972010-12-23 09:54:15140 return true;
141 }
142
[email protected]83039bb2011-12-09 18:43:55143 virtual bool SetSendBufferSize(int32 size) OVERRIDE {
[email protected]f61c3972010-12-23 09:54:15144 return true;
145 }
146
[email protected]83039bb2011-12-09 18:43:55147 virtual int Connect(const CompletionCallback& callback) OVERRIDE {
[email protected]dbf036f2011-12-06 23:33:24148 return net::OK;
149 }
[email protected]f61c3972010-12-23 09:54:15150
[email protected]83039bb2011-12-09 18:43:55151 virtual void Disconnect() OVERRIDE {}
[email protected]f61c3972010-12-23 09:54:15152
[email protected]83039bb2011-12-09 18:43:55153 virtual bool IsConnected() const OVERRIDE {
[email protected]f61c3972010-12-23 09:54:15154 return true;
155 }
156
[email protected]83039bb2011-12-09 18:43:55157 virtual bool IsConnectedAndIdle() const OVERRIDE {
[email protected]f61c3972010-12-23 09:54:15158 return true;
159 }
160
[email protected]83039bb2011-12-09 18:43:55161 virtual int GetPeerAddress(AddressList* address) const OVERRIDE {
[email protected]f61c3972010-12-23 09:54:15162 net::IPAddressNumber ip_address(4);
[email protected]fe89ea72011-05-12 02:02:40163 *address = net::AddressList::CreateFromIPAddress(ip_address, 0 /*port*/);
[email protected]f61c3972010-12-23 09:54:15164 return net::OK;
165 }
166
[email protected]83039bb2011-12-09 18:43:55167 virtual int GetLocalAddress(IPEndPoint* address) const OVERRIDE {
[email protected]e7f74da2011-04-19 23:49:35168 net::IPAddressNumber ip_address(4);
169 *address = net::IPEndPoint(ip_address, 0);
170 return net::OK;
171 }
172
[email protected]83039bb2011-12-09 18:43:55173 virtual const BoundNetLog& NetLog() const OVERRIDE {
[email protected]f61c3972010-12-23 09:54:15174 return net_log_;
175 }
176
[email protected]83039bb2011-12-09 18:43:55177 virtual void SetSubresourceSpeculation() OVERRIDE {}
178 virtual void SetOmniboxSpeculation() OVERRIDE {}
[email protected]f61c3972010-12-23 09:54:15179
[email protected]83039bb2011-12-09 18:43:55180 virtual bool WasEverUsed() const OVERRIDE {
[email protected]f61c3972010-12-23 09:54:15181 return true;
182 }
183
[email protected]83039bb2011-12-09 18:43:55184 virtual bool UsingTCPFastOpen() const OVERRIDE {
[email protected]f61c3972010-12-23 09:54:15185 return false;
186 }
187
[email protected]83039bb2011-12-09 18:43:55188 virtual int64 NumBytesRead() const OVERRIDE {
[email protected]5e6efa52011-06-27 17:26:41189 return -1;
190 }
191
[email protected]83039bb2011-12-09 18:43:55192 virtual base::TimeDelta GetConnectTimeMicros() const OVERRIDE {
[email protected]5e6efa52011-06-27 17:26:41193 return base::TimeDelta::FromMicroseconds(-1);
194 }
195
[email protected]f61c3972010-12-23 09:54:15196 private:
197 net::BoundNetLog net_log_;
198 FakeDataChannel* incoming_;
199 FakeDataChannel* outgoing_;
200
201 DISALLOW_COPY_AND_ASSIGN(FakeSocket);
202};
203
204} // namespace
205
206// Verify the correctness of the test helper classes first.
207TEST(FakeSocketTest, DataTransfer) {
208 // Establish channels between two sockets.
209 FakeDataChannel channel_1;
210 FakeDataChannel channel_2;
211 FakeSocket client(&channel_1, &channel_2);
212 FakeSocket server(&channel_2, &channel_1);
213
214 const char kTestData[] = "testing123";
215 const int kTestDataSize = strlen(kTestData);
216 const int kReadBufSize = 1024;
217 scoped_refptr<net::IOBuffer> write_buf = new net::StringIOBuffer(kTestData);
218 scoped_refptr<net::IOBuffer> read_buf = new net::IOBuffer(kReadBufSize);
219
220 // Write then read.
[email protected]83039bb2011-12-09 18:43:55221 int written = server.Write(write_buf, kTestDataSize, CompletionCallback());
[email protected]55ee0e52011-07-21 18:29:44222 EXPECT_GT(written, 0);
223 EXPECT_LE(written, kTestDataSize);
224
[email protected]83039bb2011-12-09 18:43:55225 int read = client.Read(read_buf, kReadBufSize, CompletionCallback());
[email protected]55ee0e52011-07-21 18:29:44226 EXPECT_GT(read, 0);
227 EXPECT_LE(read, written);
228 EXPECT_EQ(0, memcmp(kTestData, read_buf->data(), read));
[email protected]f61c3972010-12-23 09:54:15229
230 // Read then write.
[email protected]83039bb2011-12-09 18:43:55231 TestCompletionCallback callback;
[email protected]f61c3972010-12-23 09:54:15232 EXPECT_EQ(net::ERR_IO_PENDING,
[email protected]83039bb2011-12-09 18:43:55233 server.Read(read_buf, kReadBufSize, callback.callback()));
[email protected]55ee0e52011-07-21 18:29:44234
[email protected]83039bb2011-12-09 18:43:55235 written = client.Write(write_buf, kTestDataSize, CompletionCallback());
[email protected]55ee0e52011-07-21 18:29:44236 EXPECT_GT(written, 0);
237 EXPECT_LE(written, kTestDataSize);
238
239 read = callback.WaitForResult();
240 EXPECT_GT(read, 0);
241 EXPECT_LE(read, written);
242 EXPECT_EQ(0, memcmp(kTestData, read_buf->data(), read));
[email protected]f61c3972010-12-23 09:54:15243}
244
245class SSLServerSocketTest : public PlatformTest {
246 public:
247 SSLServerSocketTest()
248 : socket_factory_(net::ClientSocketFactory::GetDefaultFactory()) {
249 }
250
251 protected:
252 void Initialize() {
253 FakeSocket* fake_client_socket = new FakeSocket(&channel_1_, &channel_2_);
254 FakeSocket* fake_server_socket = new FakeSocket(&channel_2_, &channel_1_);
255
256 FilePath certs_dir;
257 PathService::Get(base::DIR_SOURCE_ROOT, &certs_dir);
258 certs_dir = certs_dir.AppendASCII("net");
259 certs_dir = certs_dir.AppendASCII("data");
260 certs_dir = certs_dir.AppendASCII("ssl");
261 certs_dir = certs_dir.AppendASCII("certificates");
262
263 FilePath cert_path = certs_dir.AppendASCII("unittest.selfsigned.der");
264 std::string cert_der;
265 ASSERT_TRUE(file_util::ReadFileToString(cert_path, &cert_der));
266
267 scoped_refptr<net::X509Certificate> cert =
268 X509Certificate::CreateFromBytes(cert_der.data(), cert_der.size());
269
270 FilePath key_path = certs_dir.AppendASCII("unittest.key.bin");
271 std::string key_string;
272 ASSERT_TRUE(file_util::ReadFileToString(key_path, &key_string));
273 std::vector<uint8> key_vector(
274 reinterpret_cast<const uint8*>(key_string.data()),
275 reinterpret_cast<const uint8*>(key_string.data() +
276 key_string.length()));
277
[email protected]4b559b4d2011-04-14 17:37:14278 scoped_ptr<crypto::RSAPrivateKey> private_key(
279 crypto::RSAPrivateKey::CreateFromPrivateKeyInfo(key_vector));
[email protected]f61c3972010-12-23 09:54:15280
281 net::SSLConfig ssl_config;
[email protected]2fb7e3ba2011-06-22 19:24:38282 ssl_config.cached_info_enabled = false;
[email protected]f61c3972010-12-23 09:54:15283 ssl_config.false_start_enabled = false;
[email protected]2619d3312011-07-20 23:50:34284 ssl_config.origin_bound_certs_enabled = false;
[email protected]f61c3972010-12-23 09:54:15285 ssl_config.ssl3_enabled = true;
286 ssl_config.tls1_enabled = true;
[email protected]f61c3972010-12-23 09:54:15287
288 // Certificate provided by the host doesn't need authority.
289 net::SSLConfig::CertAndStatus cert_and_status;
[email protected]4dc832e2011-04-28 22:04:24290 cert_and_status.cert_status = CERT_STATUS_AUTHORITY_INVALID;
[email protected]3d5c1bd2011-07-20 02:14:01291 cert_and_status.der_cert = cert_der;
[email protected]f61c3972010-12-23 09:54:15292 ssl_config.allowed_bad_certs.push_back(cert_and_status);
293
294 net::HostPortPair host_and_pair("unittest", 0);
[email protected]feb79bcd2011-07-21 16:55:17295 net::SSLClientSocketContext context;
296 context.cert_verifier = &cert_verifier_;
[email protected]f61c3972010-12-23 09:54:15297 client_socket_.reset(
298 socket_factory_->CreateSSLClientSocket(
[email protected]feb79bcd2011-07-21 16:55:17299 fake_client_socket, host_and_pair, ssl_config, NULL, context));
[email protected]f61c3972010-12-23 09:54:15300 server_socket_.reset(net::CreateSSLServerSocket(fake_server_socket,
301 cert, private_key.get(),
302 net::SSLConfig()));
303 }
304
305 FakeDataChannel channel_1_;
306 FakeDataChannel channel_2_;
307 scoped_ptr<net::SSLClientSocket> client_socket_;
308 scoped_ptr<net::SSLServerSocket> server_socket_;
309 net::ClientSocketFactory* socket_factory_;
310 net::CertVerifier cert_verifier_;
311};
312
313// SSLServerSocket is only implemented using NSS.
314#if defined(USE_NSS) || defined(OS_WIN) || defined(OS_MACOSX)
315
316// This test only executes creation of client and server sockets. This is to
317// test that creation of sockets doesn't crash and have minimal code to run
318// under valgrind in order to help debugging memory problems.
319TEST_F(SSLServerSocketTest, Initialize) {
320 Initialize();
321}
322
[email protected]a7ac3c32011-06-17 19:10:15323// This test executes Connect() on SSLClientSocket and Handshake() on
324// SSLServerSocket to make sure handshaking between the two sockets is
[email protected]f61c3972010-12-23 09:54:15325// completed successfully.
326TEST_F(SSLServerSocketTest, Handshake) {
327 Initialize();
328
[email protected]83039bb2011-12-09 18:43:55329 TestCompletionCallback connect_callback;
[email protected]6ea7b152011-12-21 21:21:13330 TestCompletionCallback handshake_callback;
[email protected]f61c3972010-12-23 09:54:15331
[email protected]6ea7b152011-12-21 21:21:13332 int server_ret = server_socket_->Handshake(handshake_callback.callback());
[email protected]f61c3972010-12-23 09:54:15333 EXPECT_TRUE(server_ret == net::OK || server_ret == net::ERR_IO_PENDING);
334
[email protected]83039bb2011-12-09 18:43:55335 int client_ret = client_socket_->Connect(connect_callback.callback());
[email protected]f61c3972010-12-23 09:54:15336 EXPECT_TRUE(client_ret == net::OK || client_ret == net::ERR_IO_PENDING);
337
338 if (client_ret == net::ERR_IO_PENDING) {
339 EXPECT_EQ(net::OK, connect_callback.WaitForResult());
340 }
341 if (server_ret == net::ERR_IO_PENDING) {
[email protected]b0ff3f82011-07-23 05:12:39342 EXPECT_EQ(net::OK, handshake_callback.WaitForResult());
[email protected]f61c3972010-12-23 09:54:15343 }
[email protected]4dc832e2011-04-28 22:04:24344
345 // Make sure the cert status is expected.
346 SSLInfo ssl_info;
347 client_socket_->GetSSLInfo(&ssl_info);
348 EXPECT_EQ(CERT_STATUS_AUTHORITY_INVALID, ssl_info.cert_status);
[email protected]f61c3972010-12-23 09:54:15349}
350
351TEST_F(SSLServerSocketTest, DataTransfer) {
352 Initialize();
353
[email protected]83039bb2011-12-09 18:43:55354 TestCompletionCallback connect_callback;
[email protected]6ea7b152011-12-21 21:21:13355 TestCompletionCallback handshake_callback;
[email protected]f61c3972010-12-23 09:54:15356
357 // Establish connection.
[email protected]83039bb2011-12-09 18:43:55358 int client_ret = client_socket_->Connect(connect_callback.callback());
[email protected]302b6272011-01-19 01:27:22359 ASSERT_TRUE(client_ret == net::OK || client_ret == net::ERR_IO_PENDING);
[email protected]f61c3972010-12-23 09:54:15360
[email protected]6ea7b152011-12-21 21:21:13361 int server_ret = server_socket_->Handshake(handshake_callback.callback());
[email protected]302b6272011-01-19 01:27:22362 ASSERT_TRUE(server_ret == net::OK || server_ret == net::ERR_IO_PENDING);
[email protected]f61c3972010-12-23 09:54:15363
[email protected]febbbb52011-08-17 04:59:23364 client_ret = connect_callback.GetResult(client_ret);
365 ASSERT_EQ(net::OK, client_ret);
366 server_ret = handshake_callback.GetResult(server_ret);
367 ASSERT_EQ(net::OK, server_ret);
[email protected]f61c3972010-12-23 09:54:15368
369 const int kReadBufSize = 1024;
370 scoped_refptr<net::StringIOBuffer> write_buf =
371 new net::StringIOBuffer("testing123");
[email protected]febbbb52011-08-17 04:59:23372 scoped_refptr<net::DrainableIOBuffer> read_buf =
373 new net::DrainableIOBuffer(new net::IOBuffer(kReadBufSize),
374 kReadBufSize);
[email protected]f61c3972010-12-23 09:54:15375
376 // Write then read.
[email protected]83039bb2011-12-09 18:43:55377 TestCompletionCallback write_callback;
378 TestCompletionCallback read_callback;
[email protected]f61c3972010-12-23 09:54:15379 server_ret = server_socket_->Write(write_buf, write_buf->size(),
[email protected]83039bb2011-12-09 18:43:55380 write_callback.callback());
[email protected]f61c3972010-12-23 09:54:15381 EXPECT_TRUE(server_ret > 0 || server_ret == net::ERR_IO_PENDING);
[email protected]febbbb52011-08-17 04:59:23382 client_ret = client_socket_->Read(read_buf, read_buf->BytesRemaining(),
[email protected]83039bb2011-12-09 18:43:55383 read_callback.callback());
[email protected]f61c3972010-12-23 09:54:15384 EXPECT_TRUE(client_ret > 0 || client_ret == net::ERR_IO_PENDING);
385
[email protected]febbbb52011-08-17 04:59:23386 server_ret = write_callback.GetResult(server_ret);
387 EXPECT_GT(server_ret, 0);
388 client_ret = read_callback.GetResult(client_ret);
389 ASSERT_GT(client_ret, 0);
390
391 read_buf->DidConsume(client_ret);
392 while (read_buf->BytesConsumed() < write_buf->size()) {
393 client_ret = client_socket_->Read(read_buf, read_buf->BytesRemaining(),
[email protected]83039bb2011-12-09 18:43:55394 read_callback.callback());
[email protected]febbbb52011-08-17 04:59:23395 EXPECT_TRUE(client_ret > 0 || client_ret == net::ERR_IO_PENDING);
396 client_ret = read_callback.GetResult(client_ret);
397 ASSERT_GT(client_ret, 0);
398 read_buf->DidConsume(client_ret);
[email protected]f61c3972010-12-23 09:54:15399 }
[email protected]febbbb52011-08-17 04:59:23400 EXPECT_EQ(write_buf->size(), read_buf->BytesConsumed());
401 read_buf->SetOffset(0);
[email protected]f61c3972010-12-23 09:54:15402 EXPECT_EQ(0, memcmp(write_buf->data(), read_buf->data(), write_buf->size()));
403
404 // Read then write.
405 write_buf = new net::StringIOBuffer("hello123");
[email protected]febbbb52011-08-17 04:59:23406 server_ret = server_socket_->Read(read_buf, read_buf->BytesRemaining(),
[email protected]83039bb2011-12-09 18:43:55407 read_callback.callback());
[email protected]f61c3972010-12-23 09:54:15408 EXPECT_TRUE(server_ret > 0 || server_ret == net::ERR_IO_PENDING);
409 client_ret = client_socket_->Write(write_buf, write_buf->size(),
[email protected]83039bb2011-12-09 18:43:55410 write_callback.callback());
[email protected]f61c3972010-12-23 09:54:15411 EXPECT_TRUE(client_ret > 0 || client_ret == net::ERR_IO_PENDING);
412
[email protected]febbbb52011-08-17 04:59:23413 server_ret = read_callback.GetResult(server_ret);
414 ASSERT_GT(server_ret, 0);
415 client_ret = write_callback.GetResult(client_ret);
416 EXPECT_GT(client_ret, 0);
417
418 read_buf->DidConsume(server_ret);
419 while (read_buf->BytesConsumed() < write_buf->size()) {
420 server_ret = server_socket_->Read(read_buf, read_buf->BytesRemaining(),
[email protected]83039bb2011-12-09 18:43:55421 read_callback.callback());
[email protected]febbbb52011-08-17 04:59:23422 EXPECT_TRUE(server_ret > 0 || server_ret == net::ERR_IO_PENDING);
423 server_ret = read_callback.GetResult(server_ret);
424 ASSERT_GT(server_ret, 0);
425 read_buf->DidConsume(server_ret);
[email protected]f61c3972010-12-23 09:54:15426 }
[email protected]febbbb52011-08-17 04:59:23427 EXPECT_EQ(write_buf->size(), read_buf->BytesConsumed());
428 read_buf->SetOffset(0);
[email protected]f61c3972010-12-23 09:54:15429 EXPECT_EQ(0, memcmp(write_buf->data(), read_buf->data(), write_buf->size()));
430}
[email protected]b0ff3f82011-07-23 05:12:39431
432// This test executes ExportKeyingMaterial() on the client and server sockets,
433// after connecting them, and verifies that the results match.
434// This test will fail if False Start is enabled (see crbug.com/90208).
435TEST_F(SSLServerSocketTest, ExportKeyingMaterial) {
436 Initialize();
437
[email protected]83039bb2011-12-09 18:43:55438 TestCompletionCallback connect_callback;
[email protected]6ea7b152011-12-21 21:21:13439 TestCompletionCallback handshake_callback;
[email protected]b0ff3f82011-07-23 05:12:39440
[email protected]83039bb2011-12-09 18:43:55441 int client_ret = client_socket_->Connect(connect_callback.callback());
[email protected]b0ff3f82011-07-23 05:12:39442 ASSERT_TRUE(client_ret == net::OK || client_ret == net::ERR_IO_PENDING);
443
[email protected]6ea7b152011-12-21 21:21:13444 int server_ret = server_socket_->Handshake(handshake_callback.callback());
[email protected]b0ff3f82011-07-23 05:12:39445 ASSERT_TRUE(server_ret == net::OK || server_ret == net::ERR_IO_PENDING);
446
447 if (client_ret == net::ERR_IO_PENDING) {
448 ASSERT_EQ(net::OK, connect_callback.WaitForResult());
449 }
450 if (server_ret == net::ERR_IO_PENDING) {
451 ASSERT_EQ(net::OK, handshake_callback.WaitForResult());
452 }
453
454 const int kKeyingMaterialSize = 32;
455 const char* kKeyingLabel = "EXPERIMENTAL-server-socket-test";
456 const char* kKeyingContext = "";
457 unsigned char server_out[kKeyingMaterialSize];
[email protected]1bc6f5e2012-03-15 00:20:58458 int rv = server_socket_->ExportKeyingMaterial(kKeyingLabel,
459 false, kKeyingContext,
[email protected]b0ff3f82011-07-23 05:12:39460 server_out, sizeof(server_out));
461 ASSERT_EQ(rv, net::OK);
462
463 unsigned char client_out[kKeyingMaterialSize];
[email protected]1bc6f5e2012-03-15 00:20:58464 rv = client_socket_->ExportKeyingMaterial(kKeyingLabel,
465 false, kKeyingContext,
[email protected]b0ff3f82011-07-23 05:12:39466 client_out, sizeof(client_out));
467 ASSERT_EQ(rv, net::OK);
468 EXPECT_TRUE(memcmp(server_out, client_out, sizeof(server_out)) == 0);
469
470 const char* kKeyingLabelBad = "EXPERIMENTAL-server-socket-test-bad";
471 unsigned char client_bad[kKeyingMaterialSize];
[email protected]1bc6f5e2012-03-15 00:20:58472 rv = client_socket_->ExportKeyingMaterial(kKeyingLabelBad,
473 false, kKeyingContext,
[email protected]b0ff3f82011-07-23 05:12:39474 client_bad, sizeof(client_bad));
475 ASSERT_EQ(rv, net::OK);
476 EXPECT_TRUE(memcmp(server_out, client_bad, sizeof(server_out)) != 0);
477}
[email protected]f61c3972010-12-23 09:54:15478#endif
479
480} // namespace net