OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 // This test suite uses SSLClientSocket to test the implementation of | 5 // This test suite uses SSLClientSocket to test the implementation of |
6 // SSLServerSocket. In order to establish connections between the sockets | 6 // SSLServerSocket. In order to establish connections between the sockets |
7 // we need two additional classes: | 7 // we need two additional classes: |
8 // 1. FakeSocket | 8 // 1. FakeSocket |
9 // Connects SSL socket to FakeDataChannel. This class is just a stub. | 9 // Connects SSL socket to FakeDataChannel. This class is just a stub. |
10 // | 10 // |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
51 namespace net { | 51 namespace net { |
52 | 52 |
53 namespace { | 53 namespace { |
54 | 54 |
55 class FakeDataChannel { | 55 class FakeDataChannel { |
56 public: | 56 public: |
57 FakeDataChannel() | 57 FakeDataChannel() |
58 : read_buf_len_(0), | 58 : read_buf_len_(0), |
59 closed_(false), | 59 closed_(false), |
60 write_called_after_close_(false), | 60 write_called_after_close_(false), |
61 weak_factory_(this) { | 61 weak_factory_(this) {} |
62 } | |
63 | 62 |
64 int Read(IOBuffer* buf, int buf_len, const CompletionCallback& callback) { | 63 int Read(IOBuffer* buf, int buf_len, const CompletionCallback& callback) { |
65 if (closed_) | 64 if (closed_) |
66 return 0; | 65 return 0; |
67 if (data_.empty()) { | 66 if (data_.empty()) { |
68 read_callback_ = callback; | 67 read_callback_ = callback; |
69 read_buf_ = buf; | 68 read_buf_ = buf; |
70 read_buf_len_ = buf_len; | 69 read_buf_len_ = buf_len; |
71 return net::ERR_IO_PENDING; | 70 return net::ERR_IO_PENDING; |
72 } | 71 } |
73 return PropogateData(buf, buf_len); | 72 return PropogateData(buf, buf_len); |
74 } | 73 } |
75 | 74 |
76 int Write(IOBuffer* buf, int buf_len, const CompletionCallback& callback) { | 75 int Write(IOBuffer* buf, int buf_len, const CompletionCallback& callback) { |
77 if (closed_) { | 76 if (closed_) { |
78 if (write_called_after_close_) | 77 if (write_called_after_close_) |
79 return net::ERR_CONNECTION_RESET; | 78 return net::ERR_CONNECTION_RESET; |
80 write_called_after_close_ = true; | 79 write_called_after_close_ = true; |
81 write_callback_ = callback; | 80 write_callback_ = callback; |
82 base::MessageLoop::current()->PostTask( | 81 base::MessageLoop::current()->PostTask( |
83 FROM_HERE, base::Bind(&FakeDataChannel::DoWriteCallback, | 82 FROM_HERE, |
84 weak_factory_.GetWeakPtr())); | 83 base::Bind(&FakeDataChannel::DoWriteCallback, |
| 84 weak_factory_.GetWeakPtr())); |
85 return net::ERR_IO_PENDING; | 85 return net::ERR_IO_PENDING; |
86 } | 86 } |
87 data_.push(new net::DrainableIOBuffer(buf, buf_len)); | 87 data_.push(new net::DrainableIOBuffer(buf, buf_len)); |
88 base::MessageLoop::current()->PostTask( | 88 base::MessageLoop::current()->PostTask( |
89 FROM_HERE, base::Bind(&FakeDataChannel::DoReadCallback, | 89 FROM_HERE, |
90 weak_factory_.GetWeakPtr())); | 90 base::Bind(&FakeDataChannel::DoReadCallback, |
| 91 weak_factory_.GetWeakPtr())); |
91 return buf_len; | 92 return buf_len; |
92 } | 93 } |
93 | 94 |
94 // Closes the FakeDataChannel. After Close() is called, Read() returns 0, | 95 // Closes the FakeDataChannel. After Close() is called, Read() returns 0, |
95 // indicating EOF, and Write() fails with ERR_CONNECTION_RESET. Note that | 96 // indicating EOF, and Write() fails with ERR_CONNECTION_RESET. Note that |
96 // after the FakeDataChannel is closed, the first Write() call completes | 97 // after the FakeDataChannel is closed, the first Write() call completes |
97 // asynchronously, which is necessary to reproduce bug 127822. | 98 // asynchronously, which is necessary to reproduce bug 127822. |
98 void Close() { | 99 void Close() { closed_ = true; } |
99 closed_ = true; | |
100 } | |
101 | 100 |
102 private: | 101 private: |
103 void DoReadCallback() { | 102 void DoReadCallback() { |
104 if (read_callback_.is_null() || data_.empty()) | 103 if (read_callback_.is_null() || data_.empty()) |
105 return; | 104 return; |
106 | 105 |
107 int copied = PropogateData(read_buf_, read_buf_len_); | 106 int copied = PropogateData(read_buf_, read_buf_len_); |
108 CompletionCallback callback = read_callback_; | 107 CompletionCallback callback = read_callback_; |
109 read_callback_.Reset(); | 108 read_callback_.Reset(); |
110 read_buf_ = NULL; | 109 read_buf_ = NULL; |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
150 | 149 |
151 base::WeakPtrFactory<FakeDataChannel> weak_factory_; | 150 base::WeakPtrFactory<FakeDataChannel> weak_factory_; |
152 | 151 |
153 DISALLOW_COPY_AND_ASSIGN(FakeDataChannel); | 152 DISALLOW_COPY_AND_ASSIGN(FakeDataChannel); |
154 }; | 153 }; |
155 | 154 |
156 class FakeSocket : public StreamSocket { | 155 class FakeSocket : public StreamSocket { |
157 public: | 156 public: |
158 FakeSocket(FakeDataChannel* incoming_channel, | 157 FakeSocket(FakeDataChannel* incoming_channel, |
159 FakeDataChannel* outgoing_channel) | 158 FakeDataChannel* outgoing_channel) |
160 : incoming_(incoming_channel), | 159 : incoming_(incoming_channel), outgoing_(outgoing_channel) {} |
161 outgoing_(outgoing_channel) { | |
162 } | |
163 | 160 |
164 virtual ~FakeSocket() { | 161 virtual ~FakeSocket() {} |
165 } | |
166 | 162 |
167 virtual int Read(IOBuffer* buf, int buf_len, | 163 virtual int Read(IOBuffer* buf, |
| 164 int buf_len, |
168 const CompletionCallback& callback) OVERRIDE { | 165 const CompletionCallback& callback) OVERRIDE { |
169 // Read random number of bytes. | 166 // Read random number of bytes. |
170 buf_len = rand() % buf_len + 1; | 167 buf_len = rand() % buf_len + 1; |
171 return incoming_->Read(buf, buf_len, callback); | 168 return incoming_->Read(buf, buf_len, callback); |
172 } | 169 } |
173 | 170 |
174 virtual int Write(IOBuffer* buf, int buf_len, | 171 virtual int Write(IOBuffer* buf, |
| 172 int buf_len, |
175 const CompletionCallback& callback) OVERRIDE { | 173 const CompletionCallback& callback) OVERRIDE { |
176 // Write random number of bytes. | 174 // Write random number of bytes. |
177 buf_len = rand() % buf_len + 1; | 175 buf_len = rand() % buf_len + 1; |
178 return outgoing_->Write(buf, buf_len, callback); | 176 return outgoing_->Write(buf, buf_len, callback); |
179 } | 177 } |
180 | 178 |
181 virtual int SetReceiveBufferSize(int32 size) OVERRIDE { | 179 virtual int SetReceiveBufferSize(int32 size) OVERRIDE { return net::OK; } |
182 return net::OK; | |
183 } | |
184 | 180 |
185 virtual int SetSendBufferSize(int32 size) OVERRIDE { | 181 virtual int SetSendBufferSize(int32 size) OVERRIDE { return net::OK; } |
186 return net::OK; | |
187 } | |
188 | 182 |
189 virtual int Connect(const CompletionCallback& callback) OVERRIDE { | 183 virtual int Connect(const CompletionCallback& callback) OVERRIDE { |
190 return net::OK; | 184 return net::OK; |
191 } | 185 } |
192 | 186 |
193 virtual void Disconnect() OVERRIDE { | 187 virtual void Disconnect() OVERRIDE { |
194 incoming_->Close(); | 188 incoming_->Close(); |
195 outgoing_->Close(); | 189 outgoing_->Close(); |
196 } | 190 } |
197 | 191 |
198 virtual bool IsConnected() const OVERRIDE { | 192 virtual bool IsConnected() const OVERRIDE { return true; } |
199 return true; | |
200 } | |
201 | 193 |
202 virtual bool IsConnectedAndIdle() const OVERRIDE { | 194 virtual bool IsConnectedAndIdle() const OVERRIDE { return true; } |
203 return true; | |
204 } | |
205 | 195 |
206 virtual int GetPeerAddress(IPEndPoint* address) const OVERRIDE { | 196 virtual int GetPeerAddress(IPEndPoint* address) const OVERRIDE { |
207 net::IPAddressNumber ip_address(net::kIPv4AddressSize); | 197 net::IPAddressNumber ip_address(net::kIPv4AddressSize); |
208 *address = net::IPEndPoint(ip_address, 0 /*port*/); | 198 *address = net::IPEndPoint(ip_address, 0 /*port*/); |
209 return net::OK; | 199 return net::OK; |
210 } | 200 } |
211 | 201 |
212 virtual int GetLocalAddress(IPEndPoint* address) const OVERRIDE { | 202 virtual int GetLocalAddress(IPEndPoint* address) const OVERRIDE { |
213 net::IPAddressNumber ip_address(4); | 203 net::IPAddressNumber ip_address(4); |
214 *address = net::IPEndPoint(ip_address, 0); | 204 *address = net::IPEndPoint(ip_address, 0); |
215 return net::OK; | 205 return net::OK; |
216 } | 206 } |
217 | 207 |
218 virtual const BoundNetLog& NetLog() const OVERRIDE { | 208 virtual const BoundNetLog& NetLog() const OVERRIDE { return net_log_; } |
219 return net_log_; | |
220 } | |
221 | 209 |
222 virtual void SetSubresourceSpeculation() OVERRIDE {} | 210 virtual void SetSubresourceSpeculation() OVERRIDE {} |
223 virtual void SetOmniboxSpeculation() OVERRIDE {} | 211 virtual void SetOmniboxSpeculation() OVERRIDE {} |
224 | 212 |
225 virtual bool WasEverUsed() const OVERRIDE { | 213 virtual bool WasEverUsed() const OVERRIDE { return true; } |
226 return true; | |
227 } | |
228 | 214 |
229 virtual bool UsingTCPFastOpen() const OVERRIDE { | 215 virtual bool UsingTCPFastOpen() const OVERRIDE { return false; } |
230 return false; | |
231 } | |
232 | 216 |
233 | 217 virtual bool WasNpnNegotiated() const OVERRIDE { return false; } |
234 virtual bool WasNpnNegotiated() const OVERRIDE { | |
235 return false; | |
236 } | |
237 | 218 |
238 virtual NextProto GetNegotiatedProtocol() const OVERRIDE { | 219 virtual NextProto GetNegotiatedProtocol() const OVERRIDE { |
239 return kProtoUnknown; | 220 return kProtoUnknown; |
240 } | 221 } |
241 | 222 |
242 virtual bool GetSSLInfo(SSLInfo* ssl_info) OVERRIDE { | 223 virtual bool GetSSLInfo(SSLInfo* ssl_info) OVERRIDE { return false; } |
243 return false; | |
244 } | |
245 | 224 |
246 private: | 225 private: |
247 net::BoundNetLog net_log_; | 226 net::BoundNetLog net_log_; |
248 FakeDataChannel* incoming_; | 227 FakeDataChannel* incoming_; |
249 FakeDataChannel* outgoing_; | 228 FakeDataChannel* outgoing_; |
250 | 229 |
251 DISALLOW_COPY_AND_ASSIGN(FakeSocket); | 230 DISALLOW_COPY_AND_ASSIGN(FakeSocket); |
252 }; | 231 }; |
253 | 232 |
254 } // namespace | 233 } // namespace |
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
337 // Certificate provided by the host doesn't need authority. | 316 // Certificate provided by the host doesn't need authority. |
338 net::SSLConfig::CertAndStatus cert_and_status; | 317 net::SSLConfig::CertAndStatus cert_and_status; |
339 cert_and_status.cert_status = CERT_STATUS_AUTHORITY_INVALID; | 318 cert_and_status.cert_status = CERT_STATUS_AUTHORITY_INVALID; |
340 cert_and_status.der_cert = cert_der; | 319 cert_and_status.der_cert = cert_der; |
341 ssl_config.allowed_bad_certs.push_back(cert_and_status); | 320 ssl_config.allowed_bad_certs.push_back(cert_and_status); |
342 | 321 |
343 net::HostPortPair host_and_pair("unittest", 0); | 322 net::HostPortPair host_and_pair("unittest", 0); |
344 net::SSLClientSocketContext context; | 323 net::SSLClientSocketContext context; |
345 context.cert_verifier = cert_verifier_.get(); | 324 context.cert_verifier = cert_verifier_.get(); |
346 context.transport_security_state = transport_security_state_.get(); | 325 context.transport_security_state = transport_security_state_.get(); |
347 client_socket_ = | 326 client_socket_ = socket_factory_->CreateSSLClientSocket( |
348 socket_factory_->CreateSSLClientSocket( | 327 client_connection.Pass(), host_and_pair, ssl_config, context); |
349 client_connection.Pass(), host_and_pair, ssl_config, context); | |
350 server_socket_ = net::CreateSSLServerSocket( | 328 server_socket_ = net::CreateSSLServerSocket( |
351 server_socket.Pass(), | 329 server_socket.Pass(), cert.get(), private_key.get(), net::SSLConfig()); |
352 cert.get(), private_key.get(), net::SSLConfig()); | |
353 } | 330 } |
354 | 331 |
355 FakeDataChannel channel_1_; | 332 FakeDataChannel channel_1_; |
356 FakeDataChannel channel_2_; | 333 FakeDataChannel channel_2_; |
357 scoped_ptr<net::SSLClientSocket> client_socket_; | 334 scoped_ptr<net::SSLClientSocket> client_socket_; |
358 scoped_ptr<net::SSLServerSocket> server_socket_; | 335 scoped_ptr<net::SSLServerSocket> server_socket_; |
359 net::ClientSocketFactory* socket_factory_; | 336 net::ClientSocketFactory* socket_factory_; |
360 scoped_ptr<net::MockCertVerifier> cert_verifier_; | 337 scoped_ptr<net::MockCertVerifier> cert_verifier_; |
361 scoped_ptr<net::TransportSecurityState> transport_security_state_; | 338 scoped_ptr<net::TransportSecurityState> transport_security_state_; |
362 }; | 339 }; |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
414 | 391 |
415 client_ret = connect_callback.GetResult(client_ret); | 392 client_ret = connect_callback.GetResult(client_ret); |
416 ASSERT_EQ(net::OK, client_ret); | 393 ASSERT_EQ(net::OK, client_ret); |
417 server_ret = handshake_callback.GetResult(server_ret); | 394 server_ret = handshake_callback.GetResult(server_ret); |
418 ASSERT_EQ(net::OK, server_ret); | 395 ASSERT_EQ(net::OK, server_ret); |
419 | 396 |
420 const int kReadBufSize = 1024; | 397 const int kReadBufSize = 1024; |
421 scoped_refptr<net::StringIOBuffer> write_buf = | 398 scoped_refptr<net::StringIOBuffer> write_buf = |
422 new net::StringIOBuffer("testing123"); | 399 new net::StringIOBuffer("testing123"); |
423 scoped_refptr<net::DrainableIOBuffer> read_buf = | 400 scoped_refptr<net::DrainableIOBuffer> read_buf = |
424 new net::DrainableIOBuffer(new net::IOBuffer(kReadBufSize), | 401 new net::DrainableIOBuffer(new net::IOBuffer(kReadBufSize), kReadBufSize); |
425 kReadBufSize); | |
426 | 402 |
427 // Write then read. | 403 // Write then read. |
428 TestCompletionCallback write_callback; | 404 TestCompletionCallback write_callback; |
429 TestCompletionCallback read_callback; | 405 TestCompletionCallback read_callback; |
430 server_ret = server_socket_->Write( | 406 server_ret = server_socket_->Write( |
431 write_buf.get(), write_buf->size(), write_callback.callback()); | 407 write_buf.get(), write_buf->size(), write_callback.callback()); |
432 EXPECT_TRUE(server_ret > 0 || server_ret == net::ERR_IO_PENDING); | 408 EXPECT_TRUE(server_ret > 0 || server_ret == net::ERR_IO_PENDING); |
433 client_ret = client_socket_->Read( | 409 client_ret = client_socket_->Read( |
434 read_buf.get(), read_buf->BytesRemaining(), read_callback.callback()); | 410 read_buf.get(), read_buf->BytesRemaining(), read_callback.callback()); |
435 EXPECT_TRUE(client_ret > 0 || client_ret == net::ERR_IO_PENDING); | 411 EXPECT_TRUE(client_ret > 0 || client_ret == net::ERR_IO_PENDING); |
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
522 | 498 |
523 // The client writes some data. This should not cause an infinite loop. | 499 // The client writes some data. This should not cause an infinite loop. |
524 client_ret = client_socket_->Write( | 500 client_ret = client_socket_->Write( |
525 write_buf.get(), write_buf->size(), write_callback.callback()); | 501 write_buf.get(), write_buf->size(), write_callback.callback()); |
526 EXPECT_TRUE(client_ret > 0 || client_ret == net::ERR_IO_PENDING); | 502 EXPECT_TRUE(client_ret > 0 || client_ret == net::ERR_IO_PENDING); |
527 | 503 |
528 client_ret = write_callback.GetResult(client_ret); | 504 client_ret = write_callback.GetResult(client_ret); |
529 EXPECT_GT(client_ret, 0); | 505 EXPECT_GT(client_ret, 0); |
530 | 506 |
531 base::MessageLoop::current()->PostDelayedTask( | 507 base::MessageLoop::current()->PostDelayedTask( |
532 FROM_HERE, base::MessageLoop::QuitClosure(), | 508 FROM_HERE, |
| 509 base::MessageLoop::QuitClosure(), |
533 base::TimeDelta::FromMilliseconds(10)); | 510 base::TimeDelta::FromMilliseconds(10)); |
534 base::MessageLoop::current()->Run(); | 511 base::MessageLoop::current()->Run(); |
535 } | 512 } |
536 | 513 |
537 // This test executes ExportKeyingMaterial() on the client and server sockets, | 514 // This test executes ExportKeyingMaterial() on the client and server sockets, |
538 // after connecting them, and verifies that the results match. | 515 // after connecting them, and verifies that the results match. |
539 // This test will fail if False Start is enabled (see crbug.com/90208). | 516 // This test will fail if False Start is enabled (see crbug.com/90208). |
540 TEST_F(SSLServerSocketTest, ExportKeyingMaterial) { | 517 TEST_F(SSLServerSocketTest, ExportKeyingMaterial) { |
541 Initialize(); | 518 Initialize(); |
542 | 519 |
(...skipping 10 matching lines...) Expand all Loading... |
553 ASSERT_EQ(net::OK, connect_callback.WaitForResult()); | 530 ASSERT_EQ(net::OK, connect_callback.WaitForResult()); |
554 } | 531 } |
555 if (server_ret == net::ERR_IO_PENDING) { | 532 if (server_ret == net::ERR_IO_PENDING) { |
556 ASSERT_EQ(net::OK, handshake_callback.WaitForResult()); | 533 ASSERT_EQ(net::OK, handshake_callback.WaitForResult()); |
557 } | 534 } |
558 | 535 |
559 const int kKeyingMaterialSize = 32; | 536 const int kKeyingMaterialSize = 32; |
560 const char* kKeyingLabel = "EXPERIMENTAL-server-socket-test"; | 537 const char* kKeyingLabel = "EXPERIMENTAL-server-socket-test"; |
561 const char* kKeyingContext = ""; | 538 const char* kKeyingContext = ""; |
562 unsigned char server_out[kKeyingMaterialSize]; | 539 unsigned char server_out[kKeyingMaterialSize]; |
563 int rv = server_socket_->ExportKeyingMaterial(kKeyingLabel, | 540 int rv = server_socket_->ExportKeyingMaterial( |
564 false, kKeyingContext, | 541 kKeyingLabel, false, kKeyingContext, server_out, sizeof(server_out)); |
565 server_out, sizeof(server_out)); | |
566 ASSERT_EQ(net::OK, rv); | 542 ASSERT_EQ(net::OK, rv); |
567 | 543 |
568 unsigned char client_out[kKeyingMaterialSize]; | 544 unsigned char client_out[kKeyingMaterialSize]; |
569 rv = client_socket_->ExportKeyingMaterial(kKeyingLabel, | 545 rv = client_socket_->ExportKeyingMaterial( |
570 false, kKeyingContext, | 546 kKeyingLabel, false, kKeyingContext, client_out, sizeof(client_out)); |
571 client_out, sizeof(client_out)); | |
572 ASSERT_EQ(net::OK, rv); | 547 ASSERT_EQ(net::OK, rv); |
573 EXPECT_EQ(0, memcmp(server_out, client_out, sizeof(server_out))); | 548 EXPECT_EQ(0, memcmp(server_out, client_out, sizeof(server_out))); |
574 | 549 |
575 const char* kKeyingLabelBad = "EXPERIMENTAL-server-socket-test-bad"; | 550 const char* kKeyingLabelBad = "EXPERIMENTAL-server-socket-test-bad"; |
576 unsigned char client_bad[kKeyingMaterialSize]; | 551 unsigned char client_bad[kKeyingMaterialSize]; |
577 rv = client_socket_->ExportKeyingMaterial(kKeyingLabelBad, | 552 rv = client_socket_->ExportKeyingMaterial( |
578 false, kKeyingContext, | 553 kKeyingLabelBad, false, kKeyingContext, client_bad, sizeof(client_bad)); |
579 client_bad, sizeof(client_bad)); | |
580 ASSERT_EQ(rv, net::OK); | 554 ASSERT_EQ(rv, net::OK); |
581 EXPECT_NE(0, memcmp(server_out, client_bad, sizeof(server_out))); | 555 EXPECT_NE(0, memcmp(server_out, client_bad, sizeof(server_out))); |
582 } | 556 } |
583 #endif | 557 #endif |
584 | 558 |
585 } // namespace net | 559 } // namespace net |
OLD | NEW |