Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(268)

Side by Side Diff: net/socket/ssl_client_socket_unittest.cc

Issue 994263002: Rewrite session cache in OpenSSL ports. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: reebase Created 5 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « net/socket/ssl_client_socket_openssl.cc ('k') | net/socket/ssl_session_cache_openssl.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 #include "net/socket/ssl_client_socket.h" 5 #include "net/socket/ssl_client_socket.h"
6 6
7 #include "base/callback_helpers.h" 7 #include "base/callback_helpers.h"
8 #include "base/memory/ref_counted.h" 8 #include "base/memory/ref_counted.h"
9 #include "base/run_loop.h" 9 #include "base/run_loop.h"
10 #include "base/time/time.h" 10 #include "base/time/time.h"
(...skipping 330 matching lines...) Expand 10 before | Expand all | Expand 10 after
341 ~FakeBlockingStreamSocket() override {} 341 ~FakeBlockingStreamSocket() override {}
342 342
343 // Socket implementation: 343 // Socket implementation:
344 int Read(IOBuffer* buf, 344 int Read(IOBuffer* buf,
345 int buf_len, 345 int buf_len,
346 const CompletionCallback& callback) override; 346 const CompletionCallback& callback) override;
347 int Write(IOBuffer* buf, 347 int Write(IOBuffer* buf,
348 int buf_len, 348 int buf_len,
349 const CompletionCallback& callback) override; 349 const CompletionCallback& callback) override;
350 350
351 int pending_read_result() const { return pending_read_result_; }
352 IOBuffer* pending_read_buf() const { return pending_read_buf_.get(); }
353
351 // Blocks read results on the socket. Reads will not complete until 354 // Blocks read results on the socket. Reads will not complete until
352 // UnblockReadResult() has been called and a result is ready from the 355 // UnblockReadResult() has been called and a result is ready from the
353 // underlying transport. Note: if BlockReadResult() is called while there is a 356 // underlying transport. Note: if BlockReadResult() is called while there is a
354 // hanging asynchronous Read(), that Read is blocked. 357 // hanging asynchronous Read(), that Read is blocked.
355 void BlockReadResult(); 358 void BlockReadResult();
356 void UnblockReadResult(); 359 void UnblockReadResult();
357 360
358 // Waits for the blocked Read() call to be complete at the underlying 361 // Waits for the blocked Read() call to be complete at the underlying
359 // transport. 362 // transport.
360 void WaitForReadResult(); 363 void WaitForReadResult();
361 364
362 // Causes the next call to Write() to return ERR_IO_PENDING, not beginning the 365 // Causes the next call to Write() to return ERR_IO_PENDING, not beginning the
363 // underlying transport until UnblockWrite() has been called. Note: if there 366 // underlying transport until UnblockWrite() has been called. Note: if there
364 // is a pending asynchronous write, it is NOT blocked. For purposes of 367 // is a pending asynchronous write, it is NOT blocked. For purposes of
365 // blocking writes, data is considered to have reached the underlying 368 // blocking writes, data is considered to have reached the underlying
366 // transport as soon as Write() is called. 369 // transport as soon as Write() is called.
367 void BlockWrite(); 370 void BlockWrite();
368 void UnblockWrite(); 371 void UnblockWrite();
369 372
370 // Waits for the blocked Write() call to be scheduled. 373 // Waits for the blocked Write() call to be scheduled.
371 void WaitForWrite(); 374 void WaitForWrite();
372 375
373 private: 376 private:
374 // Handles completion from the underlying transport read. 377 // Handles completion from the underlying transport read.
375 void OnReadCompleted(int result); 378 void OnReadCompleted(int result);
376 379
377 // True if read callbacks are blocked. 380 // True if read callbacks are blocked.
378 bool should_block_read_; 381 bool should_block_read_;
379 382
383 // The buffer for the pending read, or NULL if not consumed.
384 scoped_refptr<IOBuffer> pending_read_buf_;
385
380 // The user callback for the pending read call. 386 // The user callback for the pending read call.
381 CompletionCallback pending_read_callback_; 387 CompletionCallback pending_read_callback_;
382 388
383 // The result for the blocked read callback, or ERR_IO_PENDING if not 389 // The result for the blocked read callback, or ERR_IO_PENDING if not
384 // completed. 390 // completed.
385 int pending_read_result_; 391 int pending_read_result_;
386 392
387 // WaitForReadResult() wait loop. 393 // WaitForReadResult() wait loop.
388 scoped_ptr<base::RunLoop> read_loop_; 394 scoped_ptr<base::RunLoop> read_loop_;
389 395
(...skipping 17 matching lines...) Expand all
407 scoped_ptr<StreamSocket> transport) 413 scoped_ptr<StreamSocket> transport)
408 : WrappedStreamSocket(transport.Pass()), 414 : WrappedStreamSocket(transport.Pass()),
409 should_block_read_(false), 415 should_block_read_(false),
410 pending_read_result_(ERR_IO_PENDING), 416 pending_read_result_(ERR_IO_PENDING),
411 should_block_write_(false), 417 should_block_write_(false),
412 pending_write_len_(-1) {} 418 pending_write_len_(-1) {}
413 419
414 int FakeBlockingStreamSocket::Read(IOBuffer* buf, 420 int FakeBlockingStreamSocket::Read(IOBuffer* buf,
415 int len, 421 int len,
416 const CompletionCallback& callback) { 422 const CompletionCallback& callback) {
423 DCHECK(!pending_read_buf_);
417 DCHECK(pending_read_callback_.is_null()); 424 DCHECK(pending_read_callback_.is_null());
418 DCHECK_EQ(ERR_IO_PENDING, pending_read_result_); 425 DCHECK_EQ(ERR_IO_PENDING, pending_read_result_);
419 DCHECK(!callback.is_null()); 426 DCHECK(!callback.is_null());
420 427
421 int rv = transport_->Read(buf, len, base::Bind( 428 int rv = transport_->Read(buf, len, base::Bind(
422 &FakeBlockingStreamSocket::OnReadCompleted, base::Unretained(this))); 429 &FakeBlockingStreamSocket::OnReadCompleted, base::Unretained(this)));
423 if (rv == ERR_IO_PENDING) { 430 if (rv == ERR_IO_PENDING) {
424 // Save the callback to be called later. 431 // Save the callback to be called later.
432 pending_read_buf_ = buf;
425 pending_read_callback_ = callback; 433 pending_read_callback_ = callback;
426 } else if (should_block_read_) { 434 } else if (should_block_read_) {
427 // Save the callback and read result to be called later. 435 // Save the callback and read result to be called later.
436 pending_read_buf_ = buf;
428 pending_read_callback_ = callback; 437 pending_read_callback_ = callback;
429 OnReadCompleted(rv); 438 OnReadCompleted(rv);
430 rv = ERR_IO_PENDING; 439 rv = ERR_IO_PENDING;
431 } 440 }
432 return rv; 441 return rv;
433 } 442 }
434 443
435 int FakeBlockingStreamSocket::Write(IOBuffer* buf, 444 int FakeBlockingStreamSocket::Write(IOBuffer* buf,
436 int len, 445 int len,
437 const CompletionCallback& callback) { 446 const CompletionCallback& callback) {
(...skipping 26 matching lines...) Expand all
464 void FakeBlockingStreamSocket::UnblockReadResult() { 473 void FakeBlockingStreamSocket::UnblockReadResult() {
465 DCHECK(should_block_read_); 474 DCHECK(should_block_read_);
466 should_block_read_ = false; 475 should_block_read_ = false;
467 476
468 // If the operation is still pending in the underlying transport, immediately 477 // If the operation is still pending in the underlying transport, immediately
469 // return - OnReadCompleted() will handle invoking the callback once the 478 // return - OnReadCompleted() will handle invoking the callback once the
470 // transport has completed. 479 // transport has completed.
471 if (pending_read_result_ == ERR_IO_PENDING) 480 if (pending_read_result_ == ERR_IO_PENDING)
472 return; 481 return;
473 int result = pending_read_result_; 482 int result = pending_read_result_;
483 pending_read_buf_ = nullptr;
474 pending_read_result_ = ERR_IO_PENDING; 484 pending_read_result_ = ERR_IO_PENDING;
475 base::ResetAndReturn(&pending_read_callback_).Run(result); 485 base::ResetAndReturn(&pending_read_callback_).Run(result);
476 } 486 }
477 487
478 void FakeBlockingStreamSocket::WaitForReadResult() { 488 void FakeBlockingStreamSocket::WaitForReadResult() {
479 DCHECK(should_block_read_); 489 DCHECK(should_block_read_);
480 DCHECK(!read_loop_); 490 DCHECK(!read_loop_);
481 491
482 if (pending_read_result_ != ERR_IO_PENDING) 492 if (pending_read_result_ != ERR_IO_PENDING)
483 return; 493 return;
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
531 if (should_block_read_) { 541 if (should_block_read_) {
532 // Store the result so that the callback can be invoked once Unblock() is 542 // Store the result so that the callback can be invoked once Unblock() is
533 // called. 543 // called.
534 pending_read_result_ = result; 544 pending_read_result_ = result;
535 545
536 // Stop the WaitForReadResult() call if any. 546 // Stop the WaitForReadResult() call if any.
537 if (read_loop_) 547 if (read_loop_)
538 read_loop_->Quit(); 548 read_loop_->Quit();
539 } else { 549 } else {
540 // Either the Read() was never blocked or UnblockReadResult() was called 550 // Either the Read() was never blocked or UnblockReadResult() was called
541 // before the Read() completed. Either way, run the callback. 551 // before the Read() completed. Either way, return the result to the caller.
552 pending_read_buf_ = nullptr;
542 base::ResetAndReturn(&pending_read_callback_).Run(result); 553 base::ResetAndReturn(&pending_read_callback_).Run(result);
543 } 554 }
544 } 555 }
545 556
546 // CountingStreamSocket wraps an existing StreamSocket and maintains a count of 557 // CountingStreamSocket wraps an existing StreamSocket and maintains a count of
547 // reads and writes on the socket. 558 // reads and writes on the socket.
548 class CountingStreamSocket : public WrappedStreamSocket { 559 class CountingStreamSocket : public WrappedStreamSocket {
549 public: 560 public:
550 explicit CountingStreamSocket(scoped_ptr<StreamSocket> transport) 561 explicit CountingStreamSocket(scoped_ptr<StreamSocket> transport)
551 : WrappedStreamSocket(transport.Pass()), 562 : WrappedStreamSocket(transport.Pass()),
(...skipping 2253 matching lines...) Expand 10 before | Expand all | Expand 10 after
2805 EXPECT_TRUE(sock->WasEverUsed()); 2816 EXPECT_TRUE(sock->WasEverUsed());
2806 2817
2807 // TODO(davidben): Read one byte to ensure the test server has responded and 2818 // TODO(davidben): Read one byte to ensure the test server has responded and
2808 // then assert IsConnectedAndIdle is false. This currently doesn't work 2819 // then assert IsConnectedAndIdle is false. This currently doesn't work
2809 // because neither SSLClientSocketNSS nor SSLClientSocketOpenSSL check their 2820 // because neither SSLClientSocketNSS nor SSLClientSocketOpenSSL check their
2810 // SSL implementation's internal buffers. Either call PR_Available and 2821 // SSL implementation's internal buffers. Either call PR_Available and
2811 // SSL_pending, although the former isn't actually implemented or perhaps 2822 // SSL_pending, although the former isn't actually implemented or perhaps
2812 // attempt to read one byte extra. 2823 // attempt to read one byte extra.
2813 } 2824 }
2814 2825
2826 // Tests that basic session resumption works.
2827 TEST_F(SSLClientSocketTest, SessionResumption) {
2828 SpawnedTestServer::SSLOptions ssl_options;
2829 ASSERT_TRUE(StartTestServer(ssl_options));
2830
2831 // First, perform a full handshake.
2832 SSLConfig ssl_config;
2833 TestCompletionCallback callback;
2834 scoped_ptr<StreamSocket> transport(
2835 new TCPClientSocket(addr(), &log_, NetLog::Source()));
2836 ASSERT_EQ(OK, callback.GetResult(transport->Connect(callback.callback())));
2837 scoped_ptr<SSLClientSocket> sock(CreateSSLClientSocket(
2838 transport.Pass(), test_server()->host_port_pair(), ssl_config));
2839 ASSERT_EQ(OK, callback.GetResult(sock->Connect(callback.callback())));
2840 SSLInfo ssl_info;
2841 ASSERT_TRUE(sock->GetSSLInfo(&ssl_info));
2842 EXPECT_EQ(SSLInfo::HANDSHAKE_FULL, ssl_info.handshake_type);
2843
2844 // The next connection should resume.
2845 transport.reset(new TCPClientSocket(addr(), &log_, NetLog::Source()));
2846 ASSERT_EQ(OK, callback.GetResult(transport->Connect(callback.callback())));
2847 sock = CreateSSLClientSocket(transport.Pass(),
2848 test_server()->host_port_pair(), ssl_config);
2849 ASSERT_EQ(OK, callback.GetResult(sock->Connect(callback.callback())));
2850 ASSERT_TRUE(sock->GetSSLInfo(&ssl_info));
2851 EXPECT_EQ(SSLInfo::HANDSHAKE_RESUME, ssl_info.handshake_type);
2852
2853 // Using a different HostPortPair uses a different session cache key.
2854 transport.reset(new TCPClientSocket(addr(), &log_, NetLog::Source()));
2855 ASSERT_EQ(OK, callback.GetResult(transport->Connect(callback.callback())));
2856 sock = CreateSSLClientSocket(transport.Pass(),
2857 HostPortPair("example.com", 443), ssl_config);
2858 ASSERT_EQ(OK, callback.GetResult(sock->Connect(callback.callback())));
2859 ASSERT_TRUE(sock->GetSSLInfo(&ssl_info));
2860 EXPECT_EQ(SSLInfo::HANDSHAKE_FULL, ssl_info.handshake_type);
2861
2862 SSLClientSocket::ClearSessionCache();
2863
2864 // After clearing the session cache, the next handshake doesn't resume.
2865 transport.reset(new TCPClientSocket(addr(), &log_, NetLog::Source()));
2866 ASSERT_EQ(OK, callback.GetResult(transport->Connect(callback.callback())));
2867 sock = CreateSSLClientSocket(transport.Pass(),
2868 test_server()->host_port_pair(), ssl_config);
2869 ASSERT_EQ(OK, callback.GetResult(sock->Connect(callback.callback())));
2870 ASSERT_TRUE(sock->GetSSLInfo(&ssl_info));
2871 EXPECT_EQ(SSLInfo::HANDSHAKE_FULL, ssl_info.handshake_type);
2872 }
2873
2874 // Tests that connections with certificate errors do not add entries to the
2875 // session cache.
2876 TEST_F(SSLClientSocketTest, CertificateErrorNoResume) {
2877 SpawnedTestServer::SSLOptions ssl_options;
2878 ASSERT_TRUE(StartTestServer(ssl_options));
2879
2880 cert_verifier_->set_default_result(ERR_CERT_COMMON_NAME_INVALID);
2881
2882 SSLConfig ssl_config;
2883 TestCompletionCallback callback;
2884 scoped_ptr<StreamSocket> transport(
2885 new TCPClientSocket(addr(), &log_, NetLog::Source()));
2886 ASSERT_EQ(OK, callback.GetResult(transport->Connect(callback.callback())));
2887 scoped_ptr<SSLClientSocket> sock(CreateSSLClientSocket(
2888 transport.Pass(), test_server()->host_port_pair(), ssl_config));
2889 EXPECT_EQ(ERR_CERT_COMMON_NAME_INVALID,
2890 callback.GetResult(sock->Connect(callback.callback())));
2891
2892 cert_verifier_->set_default_result(OK);
2893
2894 // The next connection should perform a full handshake.
2895 transport.reset(new TCPClientSocket(addr(), &log_, NetLog::Source()));
2896 ASSERT_EQ(OK, callback.GetResult(transport->Connect(callback.callback())));
2897 sock = CreateSSLClientSocket(transport.Pass(),
2898 test_server()->host_port_pair(), ssl_config);
2899 ASSERT_EQ(OK, callback.GetResult(sock->Connect(callback.callback())));
2900 SSLInfo ssl_info;
2901 ASSERT_TRUE(sock->GetSSLInfo(&ssl_info));
2902 EXPECT_EQ(SSLInfo::HANDSHAKE_FULL, ssl_info.handshake_type);
2903 }
2904
2815 // Tests that session caches are sharded by max_version. 2905 // Tests that session caches are sharded by max_version.
2816 TEST_F(SSLClientSocketTest, FallbackShardSessionCache) { 2906 TEST_F(SSLClientSocketTest, FallbackShardSessionCache) {
2817 SpawnedTestServer::SSLOptions ssl_options; 2907 SpawnedTestServer::SSLOptions ssl_options;
2818 ASSERT_TRUE(StartTestServer(ssl_options)); 2908 ASSERT_TRUE(StartTestServer(ssl_options));
2819 2909
2820 // Prepare a normal and fallback SSL config. 2910 // Prepare a normal and fallback SSL config.
2821 SSLConfig ssl_config; 2911 SSLConfig ssl_config;
2822 SSLConfig fallback_ssl_config; 2912 SSLConfig fallback_ssl_config;
2823 fallback_ssl_config.version_max = SSL_PROTOCOL_VERSION_TLS1; 2913 fallback_ssl_config.version_max = SSL_PROTOCOL_VERSION_TLS1;
2824 fallback_ssl_config.version_fallback = true; 2914 fallback_ssl_config.version_fallback = true;
(...skipping 273 matching lines...) Expand 10 before | Expand all | Expand 10 after
3098 transport2.Pass(), test_server()->host_port_pair(), client_config); 3188 transport2.Pass(), test_server()->host_port_pair(), client_config);
3099 ASSERT_TRUE(sock2.get()); 3189 ASSERT_TRUE(sock2.get());
3100 EXPECT_EQ(OK, callback.GetResult(sock2->Connect(callback.callback()))); 3190 EXPECT_EQ(OK, callback.GetResult(sock2->Connect(callback.callback())));
3101 3191
3102 // It should resume the session. 3192 // It should resume the session.
3103 SSLInfo ssl_info; 3193 SSLInfo ssl_info;
3104 EXPECT_TRUE(sock2->GetSSLInfo(&ssl_info)); 3194 EXPECT_TRUE(sock2->GetSSLInfo(&ssl_info));
3105 EXPECT_EQ(SSLInfo::HANDSHAKE_RESUME, ssl_info.handshake_type); 3195 EXPECT_EQ(SSLInfo::HANDSHAKE_RESUME, ssl_info.handshake_type);
3106 } 3196 }
3107 3197
3108 // Test that sessions are not resumable before receiving the server Finished 3198 // Test that False Started sessions are not resumable before receiving the
3109 // message. 3199 // server Finished message.
3110 TEST_F(SSLClientSocketFalseStartTest, NoSessionResumptionBeforeFinish) { 3200 TEST_F(SSLClientSocketFalseStartTest, NoSessionResumptionBeforeFinished) {
3111 if (!SupportsAESGCM()) { 3201 if (!SupportsAESGCM()) {
3112 LOG(WARNING) << "Skipping test because AES-GCM is not supported."; 3202 LOG(WARNING) << "Skipping test because AES-GCM is not supported.";
3113 return; 3203 return;
3114 } 3204 }
3115 3205
3116 // Start a server. 3206 // Start a server.
3117 SpawnedTestServer::SSLOptions server_options; 3207 SpawnedTestServer::SSLOptions server_options;
3118 server_options.key_exchanges = 3208 server_options.key_exchanges =
3119 SpawnedTestServer::SSLOptions::KEY_EXCHANGE_ECDHE_RSA; 3209 SpawnedTestServer::SSLOptions::KEY_EXCHANGE_ECDHE_RSA;
3120 server_options.bulk_ciphers = 3210 server_options.bulk_ciphers =
(...skipping 14 matching lines...) Expand all
3135 // still completes. 3225 // still completes.
3136 EXPECT_EQ(OK, callback.WaitForResult()); 3226 EXPECT_EQ(OK, callback.WaitForResult());
3137 3227
3138 // Continue to block the client (|sock1|) from processing the Finished 3228 // Continue to block the client (|sock1|) from processing the Finished
3139 // message, but allow it to arrive on the socket. This ensures that, from the 3229 // message, but allow it to arrive on the socket. This ensures that, from the
3140 // server's point of view, it has completed the handshake and added the 3230 // server's point of view, it has completed the handshake and added the
3141 // session to its session cache. 3231 // session to its session cache.
3142 // 3232 //
3143 // The actual read on |sock1| will not complete until the Finished message is 3233 // The actual read on |sock1| will not complete until the Finished message is
3144 // processed; however, pump the underlying transport so that it is read from 3234 // processed; however, pump the underlying transport so that it is read from
3145 // the socket. This still has the potential to race, but is generally unlikely 3235 // the socket. NOTE: This may flakily pass if the server's final flight
3146 // due to socket buffer sizes. 3236 // doesn't come in one Read.
3147 scoped_refptr<IOBuffer> buf(new IOBuffer(4096)); 3237 scoped_refptr<IOBuffer> buf(new IOBuffer(4096));
3148 int rv = sock1->Read(buf.get(), 4096, callback.callback()); 3238 int rv = sock1->Read(buf.get(), 4096, callback.callback());
3149 EXPECT_EQ(ERR_IO_PENDING, rv); 3239 EXPECT_EQ(ERR_IO_PENDING, rv);
3150 raw_transport1->WaitForReadResult(); 3240 raw_transport1->WaitForReadResult();
3151 3241
3152 // Drop the old socket. This is needed because the Python test server can't 3242 // Drop the old socket. This is needed because the Python test server can't
3153 // service two sockets in parallel. 3243 // service two sockets in parallel.
3154 sock1.reset(); 3244 sock1.reset();
3155 3245
3156 // Start a second connection. 3246 // Start a second connection.
3157 scoped_ptr<StreamSocket> transport2( 3247 scoped_ptr<StreamSocket> transport2(
3158 new TCPClientSocket(addr(), &log_, NetLog::Source())); 3248 new TCPClientSocket(addr(), &log_, NetLog::Source()));
3159 EXPECT_EQ(OK, callback.GetResult(transport2->Connect(callback.callback()))); 3249 EXPECT_EQ(OK, callback.GetResult(transport2->Connect(callback.callback())));
3160 scoped_ptr<SSLClientSocket> sock2 = CreateSSLClientSocket( 3250 scoped_ptr<SSLClientSocket> sock2 = CreateSSLClientSocket(
3161 transport2.Pass(), test_server()->host_port_pair(), client_config); 3251 transport2.Pass(), test_server()->host_port_pair(), client_config);
3162 EXPECT_EQ(OK, callback.GetResult(sock2->Connect(callback.callback()))); 3252 EXPECT_EQ(OK, callback.GetResult(sock2->Connect(callback.callback())));
3163 3253
3164 // No session resumption because the first connection never received a server 3254 // No session resumption because the first connection never received a server
3165 // Finished message. 3255 // Finished message.
3166 SSLInfo ssl_info; 3256 SSLInfo ssl_info;
3167 EXPECT_TRUE(sock2->GetSSLInfo(&ssl_info)); 3257 EXPECT_TRUE(sock2->GetSSLInfo(&ssl_info));
3168 EXPECT_EQ(SSLInfo::HANDSHAKE_FULL, ssl_info.handshake_type); 3258 EXPECT_EQ(SSLInfo::HANDSHAKE_FULL, ssl_info.handshake_type);
3169 } 3259 }
3170 3260
3261 // Test that False Started sessions are not resumable if the server Finished
3262 // message was bad.
3263 TEST_F(SSLClientSocketFalseStartTest, NoSessionResumptionBadFinished) {
3264 if (!SupportsAESGCM()) {
3265 LOG(WARNING) << "Skipping test because AES-GCM is not supported.";
3266 return;
3267 }
3268
3269 // Start a server.
3270 SpawnedTestServer::SSLOptions server_options;
3271 server_options.key_exchanges =
3272 SpawnedTestServer::SSLOptions::KEY_EXCHANGE_ECDHE_RSA;
3273 server_options.bulk_ciphers =
3274 SpawnedTestServer::SSLOptions::BULK_CIPHER_AES128GCM;
3275 server_options.enable_npn = true;
3276 ASSERT_TRUE(StartTestServer(server_options));
3277
3278 SSLConfig client_config;
3279 client_config.next_protos.push_back(kProtoHTTP11);
3280
3281 // Start a handshake up to the server Finished message.
3282 TestCompletionCallback callback;
3283 FakeBlockingStreamSocket* raw_transport1 = NULL;
3284 scoped_ptr<SSLClientSocket> sock1;
3285 ASSERT_NO_FATAL_FAILURE(CreateAndConnectUntilServerFinishedReceived(
3286 client_config, &callback, &raw_transport1, &sock1));
3287 // Although raw_transport1 has the server Finished blocked, the handshake
3288 // still completes.
3289 EXPECT_EQ(OK, callback.WaitForResult());
3290
3291 // Continue to block the client (|sock1|) from processing the Finished
3292 // message, but allow it to arrive on the socket. This ensures that, from the
3293 // server's point of view, it has completed the handshake and added the
3294 // session to its session cache.
3295 //
3296 // The actual read on |sock1| will not complete until the Finished message is
3297 // processed; however, pump the underlying transport so that it is read from
3298 // the socket.
3299 scoped_refptr<IOBuffer> buf(new IOBuffer(4096));
3300 int rv = sock1->Read(buf.get(), 4096, callback.callback());
3301 EXPECT_EQ(ERR_IO_PENDING, rv);
3302 raw_transport1->WaitForReadResult();
3303
3304 // The server's second leg, or part of it, is now received but not yet sent to
3305 // |sock1|. Before doing so, break the server's second leg.
3306 int bytes_read = raw_transport1->pending_read_result();
3307 ASSERT_LT(0, bytes_read);
3308 raw_transport1->pending_read_buf()->data()[bytes_read - 1]++;
3309
3310 // Unblock the Finished message. |sock1->Read| should now fail.
3311 raw_transport1->UnblockReadResult();
3312 EXPECT_EQ(ERR_SSL_PROTOCOL_ERROR, callback.GetResult(rv));
3313
3314 // Drop the old socket. This is needed because the Python test server can't
3315 // service two sockets in parallel.
3316 sock1.reset();
3317
3318 // Start a second connection.
3319 scoped_ptr<StreamSocket> transport2(
3320 new TCPClientSocket(addr(), &log_, NetLog::Source()));
3321 EXPECT_EQ(OK, callback.GetResult(transport2->Connect(callback.callback())));
3322 scoped_ptr<SSLClientSocket> sock2 = CreateSSLClientSocket(
3323 transport2.Pass(), test_server()->host_port_pair(), client_config);
3324 EXPECT_EQ(OK, callback.GetResult(sock2->Connect(callback.callback())));
3325
3326 // No session resumption because the first connection never received a server
3327 // Finished message.
3328 SSLInfo ssl_info;
3329 EXPECT_TRUE(sock2->GetSSLInfo(&ssl_info));
3330 EXPECT_EQ(SSLInfo::HANDSHAKE_FULL, ssl_info.handshake_type);
3331 }
3332
3171 // Connect to a server using channel id. It should allow the connection. 3333 // Connect to a server using channel id. It should allow the connection.
3172 TEST_F(SSLClientSocketChannelIDTest, SendChannelID) { 3334 TEST_F(SSLClientSocketChannelIDTest, SendChannelID) {
3173 SpawnedTestServer::SSLOptions ssl_options; 3335 SpawnedTestServer::SSLOptions ssl_options;
3174 3336
3175 ASSERT_TRUE(ConnectToTestServer(ssl_options)); 3337 ASSERT_TRUE(ConnectToTestServer(ssl_options));
3176 3338
3177 EnableChannelID(); 3339 EnableChannelID();
3178 SSLConfig ssl_config; 3340 SSLConfig ssl_config;
3179 ssl_config.channel_id_enabled = true; 3341 ssl_config.channel_id_enabled = true;
3180 3342
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
3223 ssl_config.channel_id_enabled = true; 3385 ssl_config.channel_id_enabled = true;
3224 3386
3225 int rv; 3387 int rv;
3226 ASSERT_TRUE(CreateAndConnectSSLClientSocket(ssl_config, &rv)); 3388 ASSERT_TRUE(CreateAndConnectSSLClientSocket(ssl_config, &rv));
3227 3389
3228 EXPECT_EQ(ERR_UNEXPECTED, rv); 3390 EXPECT_EQ(ERR_UNEXPECTED, rv);
3229 EXPECT_FALSE(sock_->IsConnected()); 3391 EXPECT_FALSE(sock_->IsConnected());
3230 } 3392 }
3231 3393
3232 } // namespace net 3394 } // namespace net
OLDNEW
« no previous file with comments | « net/socket/ssl_client_socket_openssl.cc ('k') | net/socket/ssl_session_cache_openssl.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698