Upstream version 6.35.121.0
[platform/framework/web/crosswalk.git] / src / net / quic / test_tools / mock_crypto_client_stream.cc
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/quic_client_session_base.h"
8 #include "net/quic/quic_session_key.h"
9 #include "testing/gtest/include/gtest/gtest.h"
10
11 namespace net {
12
13 MockCryptoClientStream::MockCryptoClientStream(
14     const QuicSessionKey& server_key,
15     QuicClientSessionBase* session,
16     ProofVerifyContext* verify_context,
17     QuicCryptoClientConfig* crypto_config,
18     HandshakeMode handshake_mode,
19     const ProofVerifyDetails* proof_verify_details)
20     : QuicCryptoClientStream(server_key, session, verify_context,
21                              crypto_config),
22       handshake_mode_(handshake_mode),
23       proof_verify_details_(proof_verify_details) {
24 }
25
26 MockCryptoClientStream::~MockCryptoClientStream() {
27 }
28
29 void MockCryptoClientStream::OnHandshakeMessage(
30     const CryptoHandshakeMessage& message) {
31   CloseConnection(QUIC_CRYPTO_MESSAGE_AFTER_HANDSHAKE_COMPLETE);
32 }
33
34 bool MockCryptoClientStream::CryptoConnect() {
35   switch (handshake_mode_) {
36     case ZERO_RTT: {
37       encryption_established_ = true;
38       handshake_confirmed_ = false;
39       session()->OnCryptoHandshakeEvent(
40           QuicSession::ENCRYPTION_FIRST_ESTABLISHED);
41       break;
42     }
43
44     case CONFIRM_HANDSHAKE: {
45       encryption_established_ = true;
46       handshake_confirmed_ = true;
47       if (proof_verify_details_) {
48         client_session()->OnProofVerifyDetailsAvailable(*proof_verify_details_);
49       }
50       SetConfigNegotiated();
51       session()->OnCryptoHandshakeEvent(QuicSession::HANDSHAKE_CONFIRMED);
52       break;
53     }
54
55     case COLD_START: {
56       handshake_confirmed_ = false;
57       encryption_established_ = false;
58       break;
59     }
60   }
61   return true;
62 }
63
64 void MockCryptoClientStream::SendOnCryptoHandshakeEvent(
65     QuicSession::CryptoHandshakeEvent event) {
66   encryption_established_ = true;
67   if (event == QuicSession::HANDSHAKE_CONFIRMED) {
68     handshake_confirmed_ = true;
69     SetConfigNegotiated();
70   }
71   session()->OnCryptoHandshakeEvent(event);
72 }
73
74 void MockCryptoClientStream::SetConfigNegotiated() {
75   ASSERT_FALSE(session()->config()->negotiated());
76   QuicTagVector cgst;
77   cgst.push_back(kINAR);
78   cgst.push_back(kQBIC);
79   session()->config()->set_congestion_control(cgst, kQBIC);
80   session()->config()->set_idle_connection_state_lifetime(
81       QuicTime::Delta::FromSeconds(2 * kDefaultTimeoutSecs),
82       QuicTime::Delta::FromSeconds(kDefaultTimeoutSecs));
83   session()->config()->set_max_streams_per_connection(
84       2 * kDefaultMaxStreamsPerConnection, kDefaultMaxStreamsPerConnection);
85
86   CryptoHandshakeMessage msg;
87   session()->config()->ToHandshakeMessage(&msg);
88   string error_details;
89   const QuicErrorCode error =
90       session()->config()->ProcessClientHello(msg, &error_details);
91   ASSERT_EQ(QUIC_NO_ERROR, error);
92   ASSERT_TRUE(session()->config()->negotiated());
93 }
94
95 QuicClientSessionBase* MockCryptoClientStream::client_session() {
96   return reinterpret_cast<QuicClientSessionBase*>(session());
97 }
98
99 }  // namespace net