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
3 // found in the LICENSE file.
5 #include "net/quic/quic_crypto_client_stream.h"
7 #include "base/memory/scoped_ptr.h"
8 #include "net/quic/crypto/aes_128_gcm_12_encrypter.h"
9 #include "net/quic/crypto/quic_decrypter.h"
10 #include "net/quic/crypto/quic_encrypter.h"
11 #include "net/quic/quic_protocol.h"
12 #include "net/quic/quic_session_key.h"
13 #include "net/quic/test_tools/crypto_test_utils.h"
14 #include "net/quic/test_tools/quic_test_utils.h"
15 #include "net/quic/test_tools/simple_quic_framer.h"
16 #include "testing/gmock/include/gmock/gmock.h"
17 #include "testing/gtest/include/gtest/gtest.h"
23 const char kServerHostname
[] = "example.com";
24 const uint16 kServerPort
= 80;
26 class QuicCryptoClientStreamTest
: public ::testing::Test
{
28 QuicCryptoClientStreamTest()
29 : connection_(new PacketSavingConnection(false)),
30 session_(new TestClientSession(connection_
, DefaultQuicConfig())),
31 server_key_(kServerHostname
, kServerPort
, false, kPrivacyModeDisabled
),
32 stream_(new QuicCryptoClientStream(
33 server_key_
, session_
.get(), NULL
, &crypto_config_
)) {
34 session_
->SetCryptoStream(stream_
.get());
35 session_
->config()->SetDefaults();
36 crypto_config_
.SetDefaults();
39 void CompleteCryptoHandshake() {
40 EXPECT_TRUE(stream_
->CryptoConnect());
41 CryptoTestUtils::HandshakeWithFakeServer(connection_
, stream_
.get());
44 void ConstructHandshakeMessage() {
46 message_data_
.reset(framer
.ConstructHandshakeMessage(message_
));
49 PacketSavingConnection
* connection_
;
50 scoped_ptr
<TestClientSession
> session_
;
51 QuicSessionKey server_key_
;
52 scoped_ptr
<QuicCryptoClientStream
> stream_
;
53 CryptoHandshakeMessage message_
;
54 scoped_ptr
<QuicData
> message_data_
;
55 QuicCryptoClientConfig crypto_config_
;
58 TEST_F(QuicCryptoClientStreamTest
, NotInitiallyConected
) {
59 EXPECT_FALSE(stream_
->encryption_established());
60 EXPECT_FALSE(stream_
->handshake_confirmed());
63 TEST_F(QuicCryptoClientStreamTest
, ConnectedAfterSHLO
) {
64 CompleteCryptoHandshake();
65 EXPECT_TRUE(stream_
->encryption_established());
66 EXPECT_TRUE(stream_
->handshake_confirmed());
69 TEST_F(QuicCryptoClientStreamTest
, MessageAfterHandshake
) {
70 CompleteCryptoHandshake();
72 EXPECT_CALL(*connection_
, SendConnectionClose(
73 QUIC_CRYPTO_MESSAGE_AFTER_HANDSHAKE_COMPLETE
));
74 message_
.set_tag(kCHLO
);
75 ConstructHandshakeMessage();
76 stream_
->ProcessRawData(message_data_
->data(), message_data_
->length());
79 TEST_F(QuicCryptoClientStreamTest
, BadMessageType
) {
80 EXPECT_TRUE(stream_
->CryptoConnect());
82 message_
.set_tag(kCHLO
);
83 ConstructHandshakeMessage();
85 EXPECT_CALL(*connection_
, SendConnectionCloseWithDetails(
86 QUIC_INVALID_CRYPTO_MESSAGE_TYPE
, "Expected REJ"));
87 stream_
->ProcessRawData(message_data_
->data(), message_data_
->length());
90 TEST_F(QuicCryptoClientStreamTest
, NegotiatedParameters
) {
91 CompleteCryptoHandshake();
93 const QuicConfig
* config
= session_
->config();
94 EXPECT_EQ(FLAGS_enable_quic_pacing
? kPACE
: kQBIC
,
95 config
->congestion_control());
96 EXPECT_EQ(kDefaultTimeoutSecs
,
97 config
->idle_connection_state_lifetime().ToSeconds());
98 EXPECT_EQ(kDefaultMaxStreamsPerConnection
,
99 config
->max_streams_per_connection());
100 EXPECT_EQ(0, config
->keepalive_timeout().ToSeconds());
102 const QuicCryptoNegotiatedParameters
& crypto_params(
103 stream_
->crypto_negotiated_params());
104 EXPECT_EQ(crypto_config_
.aead
[0], crypto_params
.aead
);
105 EXPECT_EQ(crypto_config_
.kexs
[0], crypto_params
.key_exchange
);
108 TEST_F(QuicCryptoClientStreamTest
, InvalidHostname
) {
109 QuicSessionKey
server_key("invalid", 80, false, kPrivacyModeDisabled
);
110 stream_
.reset(new QuicCryptoClientStream(server_key
, session_
.get(), NULL
,
112 session_
->SetCryptoStream(stream_
.get());
114 CompleteCryptoHandshake();
115 EXPECT_TRUE(stream_
->encryption_established());
116 EXPECT_TRUE(stream_
->handshake_confirmed());
119 TEST_F(QuicCryptoClientStreamTest
, ExpiredServerConfig
) {
120 // Seed the config with a cached server config.
121 CompleteCryptoHandshake();
123 connection_
= new PacketSavingConnection(true);
124 session_
.reset(new TestClientSession(connection_
, DefaultQuicConfig()));
125 stream_
.reset(new QuicCryptoClientStream(server_key_
, session_
.get(), NULL
,
128 session_
->SetCryptoStream(stream_
.get());
129 session_
->config()->SetDefaults();
131 // Advance time 5 years to ensure that we pass the expiry time of the cached
133 reinterpret_cast<MockClock
*>(const_cast<QuicClock
*>(connection_
->clock()))
134 ->AdvanceTime(QuicTime::Delta::FromSeconds(60 * 60 * 24 * 365 * 5));
136 // Check that a client hello was sent and that CryptoConnect doesn't fail
138 EXPECT_TRUE(stream_
->CryptoConnect());
139 ASSERT_EQ(1u, connection_
->packets_
.size());