1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #include "net/quic/test_tools/mock_crypto_client_stream.h" 6 7 #include "net/quic/crypto/quic_decrypter.h" 8 #include "net/quic/quic_client_session_base.h" 9 #include "net/quic/quic_server_id.h" 10 #include "testing/gtest/include/gtest/gtest.h" 11 12 namespace net { 13 14 MockCryptoClientStream::MockCryptoClientStream( 15 const QuicServerId& server_id, 16 QuicClientSessionBase* session, 17 ProofVerifyContext* verify_context, 18 QuicCryptoClientConfig* crypto_config, 19 HandshakeMode handshake_mode, 20 const ProofVerifyDetails* proof_verify_details) 21 : QuicCryptoClientStream(server_id, session, verify_context, 22 crypto_config), 23 handshake_mode_(handshake_mode), 24 proof_verify_details_(proof_verify_details) { 25 } 26 27 MockCryptoClientStream::~MockCryptoClientStream() { 28 } 29 30 void MockCryptoClientStream::OnHandshakeMessage( 31 const CryptoHandshakeMessage& message) { 32 CloseConnection(QUIC_CRYPTO_MESSAGE_AFTER_HANDSHAKE_COMPLETE); 33 } 34 35 bool MockCryptoClientStream::CryptoConnect() { 36 switch (handshake_mode_) { 37 case ZERO_RTT: { 38 encryption_established_ = true; 39 handshake_confirmed_ = false; 40 session()->connection()->SetDecrypter(QuicDecrypter::Create(kNULL), 41 ENCRYPTION_INITIAL); 42 session()->OnCryptoHandshakeEvent( 43 QuicSession::ENCRYPTION_FIRST_ESTABLISHED); 44 break; 45 } 46 47 case CONFIRM_HANDSHAKE: { 48 encryption_established_ = true; 49 handshake_confirmed_ = true; 50 crypto_negotiated_params_.key_exchange = kC255; 51 crypto_negotiated_params_.aead = kAESG; 52 if (proof_verify_details_) { 53 client_session()->OnProofVerifyDetailsAvailable(*proof_verify_details_); 54 } 55 SetConfigNegotiated(); 56 session()->connection()->SetDecrypter(QuicDecrypter::Create(kNULL), 57 ENCRYPTION_FORWARD_SECURE); 58 session()->OnCryptoHandshakeEvent(QuicSession::HANDSHAKE_CONFIRMED); 59 break; 60 } 61 62 case COLD_START: { 63 handshake_confirmed_ = false; 64 encryption_established_ = false; 65 break; 66 } 67 } 68 return true; 69 } 70 71 void MockCryptoClientStream::SendOnCryptoHandshakeEvent( 72 QuicSession::CryptoHandshakeEvent event) { 73 encryption_established_ = true; 74 if (event == QuicSession::HANDSHAKE_CONFIRMED) { 75 handshake_confirmed_ = true; 76 SetConfigNegotiated(); 77 } 78 session()->OnCryptoHandshakeEvent(event); 79 } 80 81 void MockCryptoClientStream::SetConfigNegotiated() { 82 ASSERT_FALSE(session()->config()->negotiated()); 83 QuicTagVector cgst; 84 // TODO(rtenneti): Enable the following code after BBR code is checked in. 85 #if 0 86 cgst.push_back(kTBBR); 87 #endif 88 cgst.push_back(kQBIC); 89 session()->config()->set_congestion_feedback(cgst, kQBIC); 90 session()->config()->set_idle_connection_state_lifetime( 91 QuicTime::Delta::FromSeconds(2 * kMaximumIdleTimeoutSecs), 92 QuicTime::Delta::FromSeconds(kMaximumIdleTimeoutSecs)); 93 session()->config()->set_max_streams_per_connection( 94 2 * kDefaultMaxStreamsPerConnection, kDefaultMaxStreamsPerConnection); 95 96 CryptoHandshakeMessage msg; 97 session()->config()->ToHandshakeMessage(&msg); 98 string error_details; 99 const QuicErrorCode error = 100 session()->config()->ProcessPeerHello(msg, CLIENT, &error_details); 101 ASSERT_EQ(QUIC_NO_ERROR, error); 102 ASSERT_TRUE(session()->config()->negotiated()); 103 } 104 105 QuicClientSessionBase* MockCryptoClientStream::client_session() { 106 return reinterpret_cast<QuicClientSessionBase*>(session()); 107 } 108 109 } // namespace net 110