blob: e800cc6fd7621cbea4535c6757083d1d1a46c92a [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]a8e0ab9d2012-03-31 01:09:4431#include "net/base/cert_test_util.h"
[email protected]f61c3972010-12-23 09:54:1532#include "net/base/cert_verifier.h"
[email protected]6ea7b152011-12-21 21:21:1333#include "net/base/completion_callback.h"
[email protected]f61c3972010-12-23 09:54:1534#include "net/base/host_port_pair.h"
35#include "net/base/io_buffer.h"
[email protected]e7f74da2011-04-19 23:49:3536#include "net/base/ip_endpoint.h"
[email protected]f61c3972010-12-23 09:54:1537#include "net/base/net_errors.h"
38#include "net/base/net_log.h"
39#include "net/base/ssl_config_service.h"
[email protected]4dc832e2011-04-28 22:04:2440#include "net/base/ssl_info.h"
[email protected]f61c3972010-12-23 09:54:1541#include "net/base/x509_certificate.h"
[email protected]f61c3972010-12-23 09:54:1542#include "net/socket/client_socket_factory.h"
43#include "net/socket/socket_test_util.h"
44#include "net/socket/ssl_client_socket.h"
[email protected]3268023f2011-05-05 00:08:1045#include "net/socket/stream_socket.h"
[email protected]f61c3972010-12-23 09:54:1546#include "testing/gtest/include/gtest/gtest.h"
47#include "testing/platform_test.h"
48
49namespace net {
50
51namespace {
52
53class FakeDataChannel {
54 public:
[email protected]55ee0e52011-07-21 18:29:4455 FakeDataChannel()
[email protected]83039bb2011-12-09 18:43:5556 : read_buf_len_(0),
57 ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)) {
[email protected]f61c3972010-12-23 09:54:1558 }
59
60 virtual int Read(IOBuffer* buf, int buf_len,
[email protected]3f55aa12011-12-07 02:03:3361 const CompletionCallback& callback) {
62 if (data_.empty()) {
[email protected]f61c3972010-12-23 09:54:1563 read_callback_ = callback;
64 read_buf_ = buf;
65 read_buf_len_ = buf_len;
66 return net::ERR_IO_PENDING;
67 }
68 return PropogateData(buf, buf_len);
69 }
70
71 virtual int Write(IOBuffer* buf, int buf_len,
[email protected]83039bb2011-12-09 18:43:5572 const CompletionCallback& callback) {
[email protected]f61c3972010-12-23 09:54:1573 data_.push(new net::DrainableIOBuffer(buf, buf_len));
[email protected]55ee0e52011-07-21 18:29:4474 MessageLoop::current()->PostTask(
[email protected]83039bb2011-12-09 18:43:5575 FROM_HERE, base::Bind(&FakeDataChannel::DoReadCallback,
76 weak_factory_.GetWeakPtr()));
[email protected]f61c3972010-12-23 09:54:1577 return buf_len;
78 }
79
80 private:
81 void DoReadCallback() {
[email protected]83039bb2011-12-09 18:43:5582 if (read_callback_.is_null() || data_.empty())
[email protected]f61c3972010-12-23 09:54:1583 return;
84
85 int copied = PropogateData(read_buf_, read_buf_len_);
[email protected]83039bb2011-12-09 18:43:5586 CompletionCallback callback = read_callback_;
87 read_callback_.Reset();
88 read_buf_ = NULL;
89 read_buf_len_ = 0;
90 callback.Run(copied);
[email protected]f61c3972010-12-23 09:54:1591 }
92
93 int PropogateData(scoped_refptr<net::IOBuffer> read_buf, int read_buf_len) {
94 scoped_refptr<net::DrainableIOBuffer> buf = data_.front();
95 int copied = std::min(buf->BytesRemaining(), read_buf_len);
96 memcpy(read_buf->data(), buf->data(), copied);
97 buf->DidConsume(copied);
98
99 if (!buf->BytesRemaining())
100 data_.pop();
101 return copied;
102 }
103
[email protected]83039bb2011-12-09 18:43:55104 CompletionCallback read_callback_;
[email protected]f61c3972010-12-23 09:54:15105 scoped_refptr<net::IOBuffer> read_buf_;
106 int read_buf_len_;
107
108 std::queue<scoped_refptr<net::DrainableIOBuffer> > data_;
109
[email protected]83039bb2011-12-09 18:43:55110 base::WeakPtrFactory<FakeDataChannel> weak_factory_;
[email protected]55ee0e52011-07-21 18:29:44111
[email protected]f61c3972010-12-23 09:54:15112 DISALLOW_COPY_AND_ASSIGN(FakeDataChannel);
113};
114
[email protected]3268023f2011-05-05 00:08:10115class FakeSocket : public StreamSocket {
[email protected]f61c3972010-12-23 09:54:15116 public:
117 FakeSocket(FakeDataChannel* incoming_channel,
118 FakeDataChannel* outgoing_channel)
119 : incoming_(incoming_channel),
120 outgoing_(outgoing_channel) {
121 }
122
123 virtual ~FakeSocket() {
[email protected]f61c3972010-12-23 09:54:15124 }
125
126 virtual int Read(IOBuffer* buf, int buf_len,
[email protected]83039bb2011-12-09 18:43:55127 const CompletionCallback& callback) OVERRIDE {
[email protected]3f55aa12011-12-07 02:03:33128 // Read random number of bytes.
129 buf_len = rand() % buf_len + 1;
130 return incoming_->Read(buf, buf_len, callback);
131 }
[email protected]f61c3972010-12-23 09:54:15132
133 virtual int Write(IOBuffer* buf, int buf_len,
[email protected]83039bb2011-12-09 18:43:55134 const CompletionCallback& callback) OVERRIDE {
[email protected]55ee0e52011-07-21 18:29:44135 // Write random number of bytes.
136 buf_len = rand() % buf_len + 1;
[email protected]f61c3972010-12-23 09:54:15137 return outgoing_->Write(buf, buf_len, callback);
138 }
139
[email protected]83039bb2011-12-09 18:43:55140 virtual bool SetReceiveBufferSize(int32 size) OVERRIDE {
[email protected]f61c3972010-12-23 09:54:15141 return true;
142 }
143
[email protected]83039bb2011-12-09 18:43:55144 virtual bool SetSendBufferSize(int32 size) OVERRIDE {
[email protected]f61c3972010-12-23 09:54:15145 return true;
146 }
147
[email protected]83039bb2011-12-09 18:43:55148 virtual int Connect(const CompletionCallback& callback) OVERRIDE {
[email protected]dbf036f2011-12-06 23:33:24149 return net::OK;
150 }
[email protected]f61c3972010-12-23 09:54:15151
[email protected]83039bb2011-12-09 18:43:55152 virtual void Disconnect() OVERRIDE {}
[email protected]f61c3972010-12-23 09:54:15153
[email protected]83039bb2011-12-09 18:43:55154 virtual bool IsConnected() const OVERRIDE {
[email protected]f61c3972010-12-23 09:54:15155 return true;
156 }
157
[email protected]83039bb2011-12-09 18:43:55158 virtual bool IsConnectedAndIdle() const OVERRIDE {
[email protected]f61c3972010-12-23 09:54:15159 return true;
160 }
161
[email protected]83039bb2011-12-09 18:43:55162 virtual int GetPeerAddress(AddressList* address) const OVERRIDE {
[email protected]f61c3972010-12-23 09:54:15163 net::IPAddressNumber ip_address(4);
[email protected]fe89ea72011-05-12 02:02:40164 *address = net::AddressList::CreateFromIPAddress(ip_address, 0 /*port*/);
[email protected]f61c3972010-12-23 09:54:15165 return net::OK;
166 }
167
[email protected]83039bb2011-12-09 18:43:55168 virtual int GetLocalAddress(IPEndPoint* address) const OVERRIDE {
[email protected]e7f74da2011-04-19 23:49:35169 net::IPAddressNumber ip_address(4);
170 *address = net::IPEndPoint(ip_address, 0);
171 return net::OK;
172 }
173
[email protected]83039bb2011-12-09 18:43:55174 virtual const BoundNetLog& NetLog() const OVERRIDE {
[email protected]f61c3972010-12-23 09:54:15175 return net_log_;
176 }
177
[email protected]83039bb2011-12-09 18:43:55178 virtual void SetSubresourceSpeculation() OVERRIDE {}
179 virtual void SetOmniboxSpeculation() OVERRIDE {}
[email protected]f61c3972010-12-23 09:54:15180
[email protected]83039bb2011-12-09 18:43:55181 virtual bool WasEverUsed() const OVERRIDE {
[email protected]f61c3972010-12-23 09:54:15182 return true;
183 }
184
[email protected]83039bb2011-12-09 18:43:55185 virtual bool UsingTCPFastOpen() const OVERRIDE {
[email protected]f61c3972010-12-23 09:54:15186 return false;
187 }
188
[email protected]83039bb2011-12-09 18:43:55189 virtual int64 NumBytesRead() const OVERRIDE {
[email protected]5e6efa52011-06-27 17:26:41190 return -1;
191 }
192
[email protected]83039bb2011-12-09 18:43:55193 virtual base::TimeDelta GetConnectTimeMicros() const OVERRIDE {
[email protected]5e6efa52011-06-27 17:26:41194 return base::TimeDelta::FromMicroseconds(-1);
195 }
196
[email protected]f61c3972010-12-23 09:54:15197 private:
198 net::BoundNetLog net_log_;
199 FakeDataChannel* incoming_;
200 FakeDataChannel* outgoing_;
201
202 DISALLOW_COPY_AND_ASSIGN(FakeSocket);
203};
204
205} // namespace
206
207// Verify the correctness of the test helper classes first.
208TEST(FakeSocketTest, DataTransfer) {
209 // Establish channels between two sockets.
210 FakeDataChannel channel_1;
211 FakeDataChannel channel_2;
212 FakeSocket client(&channel_1, &channel_2);
213 FakeSocket server(&channel_2, &channel_1);
214
215 const char kTestData[] = "testing123";
216 const int kTestDataSize = strlen(kTestData);
217 const int kReadBufSize = 1024;
218 scoped_refptr<net::IOBuffer> write_buf = new net::StringIOBuffer(kTestData);
219 scoped_refptr<net::IOBuffer> read_buf = new net::IOBuffer(kReadBufSize);
220
221 // Write then read.
[email protected]83039bb2011-12-09 18:43:55222 int written = server.Write(write_buf, kTestDataSize, CompletionCallback());
[email protected]55ee0e52011-07-21 18:29:44223 EXPECT_GT(written, 0);
224 EXPECT_LE(written, kTestDataSize);
225
[email protected]83039bb2011-12-09 18:43:55226 int read = client.Read(read_buf, kReadBufSize, CompletionCallback());
[email protected]55ee0e52011-07-21 18:29:44227 EXPECT_GT(read, 0);
228 EXPECT_LE(read, written);
229 EXPECT_EQ(0, memcmp(kTestData, read_buf->data(), read));
[email protected]f61c3972010-12-23 09:54:15230
231 // Read then write.
[email protected]83039bb2011-12-09 18:43:55232 TestCompletionCallback callback;
[email protected]f61c3972010-12-23 09:54:15233 EXPECT_EQ(net::ERR_IO_PENDING,
[email protected]83039bb2011-12-09 18:43:55234 server.Read(read_buf, kReadBufSize, callback.callback()));
[email protected]55ee0e52011-07-21 18:29:44235
[email protected]83039bb2011-12-09 18:43:55236 written = client.Write(write_buf, kTestDataSize, CompletionCallback());
[email protected]55ee0e52011-07-21 18:29:44237 EXPECT_GT(written, 0);
238 EXPECT_LE(written, kTestDataSize);
239
240 read = callback.WaitForResult();
241 EXPECT_GT(read, 0);
242 EXPECT_LE(read, written);
243 EXPECT_EQ(0, memcmp(kTestData, read_buf->data(), read));
[email protected]f61c3972010-12-23 09:54:15244}
245
246class SSLServerSocketTest : public PlatformTest {
247 public:
248 SSLServerSocketTest()
[email protected]9f59fac2012-03-21 23:18:11249 : socket_factory_(net::ClientSocketFactory::GetDefaultFactory()),
250 cert_verifier_(net::CertVerifier::CreateDefault()) {
[email protected]f61c3972010-12-23 09:54:15251 }
252
253 protected:
254 void Initialize() {
255 FakeSocket* fake_client_socket = new FakeSocket(&channel_1_, &channel_2_);
256 FakeSocket* fake_server_socket = new FakeSocket(&channel_2_, &channel_1_);
257
[email protected]a8e0ab9d2012-03-31 01:09:44258 FilePath certs_dir(GetTestCertsDirectory());
[email protected]f61c3972010-12-23 09:54:15259
260 FilePath cert_path = certs_dir.AppendASCII("unittest.selfsigned.der");
261 std::string cert_der;
262 ASSERT_TRUE(file_util::ReadFileToString(cert_path, &cert_der));
263
264 scoped_refptr<net::X509Certificate> cert =
265 X509Certificate::CreateFromBytes(cert_der.data(), cert_der.size());
266
267 FilePath key_path = certs_dir.AppendASCII("unittest.key.bin");
268 std::string key_string;
269 ASSERT_TRUE(file_util::ReadFileToString(key_path, &key_string));
270 std::vector<uint8> key_vector(
271 reinterpret_cast<const uint8*>(key_string.data()),
272 reinterpret_cast<const uint8*>(key_string.data() +
273 key_string.length()));
274
[email protected]4b559b4d2011-04-14 17:37:14275 scoped_ptr<crypto::RSAPrivateKey> private_key(
276 crypto::RSAPrivateKey::CreateFromPrivateKeyInfo(key_vector));
[email protected]f61c3972010-12-23 09:54:15277
278 net::SSLConfig ssl_config;
[email protected]2fb7e3ba2011-06-22 19:24:38279 ssl_config.cached_info_enabled = false;
[email protected]f61c3972010-12-23 09:54:15280 ssl_config.false_start_enabled = false;
[email protected]9c4eff22012-03-20 22:42:29281 ssl_config.domain_bound_certs_enabled = false;
[email protected]f61c3972010-12-23 09:54:15282 ssl_config.ssl3_enabled = true;
283 ssl_config.tls1_enabled = true;
[email protected]f61c3972010-12-23 09:54:15284
285 // Certificate provided by the host doesn't need authority.
286 net::SSLConfig::CertAndStatus cert_and_status;
[email protected]4dc832e2011-04-28 22:04:24287 cert_and_status.cert_status = CERT_STATUS_AUTHORITY_INVALID;
[email protected]3d5c1bd2011-07-20 02:14:01288 cert_and_status.der_cert = cert_der;
[email protected]f61c3972010-12-23 09:54:15289 ssl_config.allowed_bad_certs.push_back(cert_and_status);
290
291 net::HostPortPair host_and_pair("unittest", 0);
[email protected]feb79bcd2011-07-21 16:55:17292 net::SSLClientSocketContext context;
[email protected]9f59fac2012-03-21 23:18:11293 context.cert_verifier = cert_verifier_.get();
[email protected]f61c3972010-12-23 09:54:15294 client_socket_.reset(
295 socket_factory_->CreateSSLClientSocket(
[email protected]feb79bcd2011-07-21 16:55:17296 fake_client_socket, host_and_pair, ssl_config, NULL, context));
[email protected]f61c3972010-12-23 09:54:15297 server_socket_.reset(net::CreateSSLServerSocket(fake_server_socket,
298 cert, private_key.get(),
299 net::SSLConfig()));
300 }
301
302 FakeDataChannel channel_1_;
303 FakeDataChannel channel_2_;
304 scoped_ptr<net::SSLClientSocket> client_socket_;
305 scoped_ptr<net::SSLServerSocket> server_socket_;
306 net::ClientSocketFactory* socket_factory_;
[email protected]9f59fac2012-03-21 23:18:11307 scoped_ptr<net::CertVerifier> cert_verifier_;
[email protected]f61c3972010-12-23 09:54:15308};
309
310// SSLServerSocket is only implemented using NSS.
311#if defined(USE_NSS) || defined(OS_WIN) || defined(OS_MACOSX)
312
313// This test only executes creation of client and server sockets. This is to
314// test that creation of sockets doesn't crash and have minimal code to run
315// under valgrind in order to help debugging memory problems.
316TEST_F(SSLServerSocketTest, Initialize) {
317 Initialize();
318}
319
[email protected]a7ac3c32011-06-17 19:10:15320// This test executes Connect() on SSLClientSocket and Handshake() on
321// SSLServerSocket to make sure handshaking between the two sockets is
[email protected]f61c3972010-12-23 09:54:15322// completed successfully.
323TEST_F(SSLServerSocketTest, Handshake) {
324 Initialize();
325
[email protected]83039bb2011-12-09 18:43:55326 TestCompletionCallback connect_callback;
[email protected]6ea7b152011-12-21 21:21:13327 TestCompletionCallback handshake_callback;
[email protected]f61c3972010-12-23 09:54:15328
[email protected]6ea7b152011-12-21 21:21:13329 int server_ret = server_socket_->Handshake(handshake_callback.callback());
[email protected]f61c3972010-12-23 09:54:15330 EXPECT_TRUE(server_ret == net::OK || server_ret == net::ERR_IO_PENDING);
331
[email protected]83039bb2011-12-09 18:43:55332 int client_ret = client_socket_->Connect(connect_callback.callback());
[email protected]f61c3972010-12-23 09:54:15333 EXPECT_TRUE(client_ret == net::OK || client_ret == net::ERR_IO_PENDING);
334
335 if (client_ret == net::ERR_IO_PENDING) {
336 EXPECT_EQ(net::OK, connect_callback.WaitForResult());
337 }
338 if (server_ret == net::ERR_IO_PENDING) {
[email protected]b0ff3f82011-07-23 05:12:39339 EXPECT_EQ(net::OK, handshake_callback.WaitForResult());
[email protected]f61c3972010-12-23 09:54:15340 }
[email protected]4dc832e2011-04-28 22:04:24341
342 // Make sure the cert status is expected.
343 SSLInfo ssl_info;
344 client_socket_->GetSSLInfo(&ssl_info);
345 EXPECT_EQ(CERT_STATUS_AUTHORITY_INVALID, ssl_info.cert_status);
[email protected]f61c3972010-12-23 09:54:15346}
347
348TEST_F(SSLServerSocketTest, DataTransfer) {
349 Initialize();
350
[email protected]83039bb2011-12-09 18:43:55351 TestCompletionCallback connect_callback;
[email protected]6ea7b152011-12-21 21:21:13352 TestCompletionCallback handshake_callback;
[email protected]f61c3972010-12-23 09:54:15353
354 // Establish connection.
[email protected]83039bb2011-12-09 18:43:55355 int client_ret = client_socket_->Connect(connect_callback.callback());
[email protected]302b6272011-01-19 01:27:22356 ASSERT_TRUE(client_ret == net::OK || client_ret == net::ERR_IO_PENDING);
[email protected]f61c3972010-12-23 09:54:15357
[email protected]6ea7b152011-12-21 21:21:13358 int server_ret = server_socket_->Handshake(handshake_callback.callback());
[email protected]302b6272011-01-19 01:27:22359 ASSERT_TRUE(server_ret == net::OK || server_ret == net::ERR_IO_PENDING);
[email protected]f61c3972010-12-23 09:54:15360
[email protected]febbbb52011-08-17 04:59:23361 client_ret = connect_callback.GetResult(client_ret);
362 ASSERT_EQ(net::OK, client_ret);
363 server_ret = handshake_callback.GetResult(server_ret);
364 ASSERT_EQ(net::OK, server_ret);
[email protected]f61c3972010-12-23 09:54:15365
366 const int kReadBufSize = 1024;
367 scoped_refptr<net::StringIOBuffer> write_buf =
368 new net::StringIOBuffer("testing123");
[email protected]febbbb52011-08-17 04:59:23369 scoped_refptr<net::DrainableIOBuffer> read_buf =
370 new net::DrainableIOBuffer(new net::IOBuffer(kReadBufSize),
371 kReadBufSize);
[email protected]f61c3972010-12-23 09:54:15372
373 // Write then read.
[email protected]83039bb2011-12-09 18:43:55374 TestCompletionCallback write_callback;
375 TestCompletionCallback read_callback;
[email protected]f61c3972010-12-23 09:54:15376 server_ret = server_socket_->Write(write_buf, write_buf->size(),
[email protected]83039bb2011-12-09 18:43:55377 write_callback.callback());
[email protected]f61c3972010-12-23 09:54:15378 EXPECT_TRUE(server_ret > 0 || server_ret == net::ERR_IO_PENDING);
[email protected]febbbb52011-08-17 04:59:23379 client_ret = client_socket_->Read(read_buf, read_buf->BytesRemaining(),
[email protected]83039bb2011-12-09 18:43:55380 read_callback.callback());
[email protected]f61c3972010-12-23 09:54:15381 EXPECT_TRUE(client_ret > 0 || client_ret == net::ERR_IO_PENDING);
382
[email protected]febbbb52011-08-17 04:59:23383 server_ret = write_callback.GetResult(server_ret);
384 EXPECT_GT(server_ret, 0);
385 client_ret = read_callback.GetResult(client_ret);
386 ASSERT_GT(client_ret, 0);
387
388 read_buf->DidConsume(client_ret);
389 while (read_buf->BytesConsumed() < write_buf->size()) {
390 client_ret = client_socket_->Read(read_buf, read_buf->BytesRemaining(),
[email protected]83039bb2011-12-09 18:43:55391 read_callback.callback());
[email protected]febbbb52011-08-17 04:59:23392 EXPECT_TRUE(client_ret > 0 || client_ret == net::ERR_IO_PENDING);
393 client_ret = read_callback.GetResult(client_ret);
394 ASSERT_GT(client_ret, 0);
395 read_buf->DidConsume(client_ret);
[email protected]f61c3972010-12-23 09:54:15396 }
[email protected]febbbb52011-08-17 04:59:23397 EXPECT_EQ(write_buf->size(), read_buf->BytesConsumed());
398 read_buf->SetOffset(0);
[email protected]f61c3972010-12-23 09:54:15399 EXPECT_EQ(0, memcmp(write_buf->data(), read_buf->data(), write_buf->size()));
400
401 // Read then write.
402 write_buf = new net::StringIOBuffer("hello123");
[email protected]febbbb52011-08-17 04:59:23403 server_ret = server_socket_->Read(read_buf, read_buf->BytesRemaining(),
[email protected]83039bb2011-12-09 18:43:55404 read_callback.callback());
[email protected]f61c3972010-12-23 09:54:15405 EXPECT_TRUE(server_ret > 0 || server_ret == net::ERR_IO_PENDING);
406 client_ret = client_socket_->Write(write_buf, write_buf->size(),
[email protected]83039bb2011-12-09 18:43:55407 write_callback.callback());
[email protected]f61c3972010-12-23 09:54:15408 EXPECT_TRUE(client_ret > 0 || client_ret == net::ERR_IO_PENDING);
409
[email protected]febbbb52011-08-17 04:59:23410 server_ret = read_callback.GetResult(server_ret);
411 ASSERT_GT(server_ret, 0);
412 client_ret = write_callback.GetResult(client_ret);
413 EXPECT_GT(client_ret, 0);
414
415 read_buf->DidConsume(server_ret);
416 while (read_buf->BytesConsumed() < write_buf->size()) {
417 server_ret = server_socket_->Read(read_buf, read_buf->BytesRemaining(),
[email protected]83039bb2011-12-09 18:43:55418 read_callback.callback());
[email protected]febbbb52011-08-17 04:59:23419 EXPECT_TRUE(server_ret > 0 || server_ret == net::ERR_IO_PENDING);
420 server_ret = read_callback.GetResult(server_ret);
421 ASSERT_GT(server_ret, 0);
422 read_buf->DidConsume(server_ret);
[email protected]f61c3972010-12-23 09:54:15423 }
[email protected]febbbb52011-08-17 04:59:23424 EXPECT_EQ(write_buf->size(), read_buf->BytesConsumed());
425 read_buf->SetOffset(0);
[email protected]f61c3972010-12-23 09:54:15426 EXPECT_EQ(0, memcmp(write_buf->data(), read_buf->data(), write_buf->size()));
427}
[email protected]b0ff3f82011-07-23 05:12:39428
429// This test executes ExportKeyingMaterial() on the client and server sockets,
430// after connecting them, and verifies that the results match.
431// This test will fail if False Start is enabled (see crbug.com/90208).
432TEST_F(SSLServerSocketTest, ExportKeyingMaterial) {
433 Initialize();
434
[email protected]83039bb2011-12-09 18:43:55435 TestCompletionCallback connect_callback;
[email protected]6ea7b152011-12-21 21:21:13436 TestCompletionCallback handshake_callback;
[email protected]b0ff3f82011-07-23 05:12:39437
[email protected]83039bb2011-12-09 18:43:55438 int client_ret = client_socket_->Connect(connect_callback.callback());
[email protected]b0ff3f82011-07-23 05:12:39439 ASSERT_TRUE(client_ret == net::OK || client_ret == net::ERR_IO_PENDING);
440
[email protected]6ea7b152011-12-21 21:21:13441 int server_ret = server_socket_->Handshake(handshake_callback.callback());
[email protected]b0ff3f82011-07-23 05:12:39442 ASSERT_TRUE(server_ret == net::OK || server_ret == net::ERR_IO_PENDING);
443
444 if (client_ret == net::ERR_IO_PENDING) {
445 ASSERT_EQ(net::OK, connect_callback.WaitForResult());
446 }
447 if (server_ret == net::ERR_IO_PENDING) {
448 ASSERT_EQ(net::OK, handshake_callback.WaitForResult());
449 }
450
451 const int kKeyingMaterialSize = 32;
452 const char* kKeyingLabel = "EXPERIMENTAL-server-socket-test";
453 const char* kKeyingContext = "";
454 unsigned char server_out[kKeyingMaterialSize];
[email protected]1bc6f5e2012-03-15 00:20:58455 int rv = server_socket_->ExportKeyingMaterial(kKeyingLabel,
456 false, kKeyingContext,
[email protected]b0ff3f82011-07-23 05:12:39457 server_out, sizeof(server_out));
458 ASSERT_EQ(rv, net::OK);
459
460 unsigned char client_out[kKeyingMaterialSize];
[email protected]1bc6f5e2012-03-15 00:20:58461 rv = client_socket_->ExportKeyingMaterial(kKeyingLabel,
462 false, kKeyingContext,
[email protected]b0ff3f82011-07-23 05:12:39463 client_out, sizeof(client_out));
464 ASSERT_EQ(rv, net::OK);
465 EXPECT_TRUE(memcmp(server_out, client_out, sizeof(server_out)) == 0);
466
467 const char* kKeyingLabelBad = "EXPERIMENTAL-server-socket-test-bad";
468 unsigned char client_bad[kKeyingMaterialSize];
[email protected]1bc6f5e2012-03-15 00:20:58469 rv = client_socket_->ExportKeyingMaterial(kKeyingLabelBad,
470 false, kKeyingContext,
[email protected]b0ff3f82011-07-23 05:12:39471 client_bad, sizeof(client_bad));
472 ASSERT_EQ(rv, net::OK);
473 EXPECT_TRUE(memcmp(server_out, client_bad, sizeof(server_out)) != 0);
474}
[email protected]f61c3972010-12-23 09:54:15475#endif
476
477} // namespace net