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/test_tools/crypto_test_utils.h"
13 #include "net/quic/test_tools/quic_test_utils.h"
14 #include "net/quic/test_tools/simple_quic_framer.h"
15 #include "testing/gmock/include/gmock/gmock.h"
16 #include "testing/gtest/include/gtest/gtest.h"
22 const char kServerHostname
[] = "example.com";
24 class QuicCryptoClientStreamTest
: public ::testing::Test
{
26 QuicCryptoClientStreamTest()
27 : connection_(new PacketSavingConnection(false)),
28 session_(new TestSession(connection_
, DefaultQuicConfig())),
29 stream_(new QuicCryptoClientStream(kServerHostname
, session_
.get(),
31 session_
->SetCryptoStream(stream_
.get());
32 crypto_config_
.SetDefaults();
35 void CompleteCryptoHandshake() {
36 EXPECT_TRUE(stream_
->CryptoConnect());
37 CryptoTestUtils::HandshakeWithFakeServer(connection_
, stream_
.get());
40 void ConstructHandshakeMessage() {
42 message_data_
.reset(framer
.ConstructHandshakeMessage(message_
));
45 PacketSavingConnection
* connection_
;
46 scoped_ptr
<TestSession
> session_
;
47 scoped_ptr
<QuicCryptoClientStream
> stream_
;
48 CryptoHandshakeMessage message_
;
49 scoped_ptr
<QuicData
> message_data_
;
50 QuicCryptoClientConfig crypto_config_
;
53 TEST_F(QuicCryptoClientStreamTest
, NotInitiallyConected
) {
54 EXPECT_FALSE(stream_
->encryption_established());
55 EXPECT_FALSE(stream_
->handshake_confirmed());
58 TEST_F(QuicCryptoClientStreamTest
, ConnectedAfterSHLO
) {
59 CompleteCryptoHandshake();
60 EXPECT_TRUE(stream_
->encryption_established());
61 EXPECT_TRUE(stream_
->handshake_confirmed());
64 TEST_F(QuicCryptoClientStreamTest
, MessageAfterHandshake
) {
65 CompleteCryptoHandshake();
67 EXPECT_CALL(*connection_
, SendConnectionClose(
68 QUIC_CRYPTO_MESSAGE_AFTER_HANDSHAKE_COMPLETE
));
69 message_
.set_tag(kCHLO
);
70 ConstructHandshakeMessage();
71 stream_
->ProcessRawData(message_data_
->data(), message_data_
->length());
74 TEST_F(QuicCryptoClientStreamTest
, BadMessageType
) {
75 EXPECT_TRUE(stream_
->CryptoConnect());
77 message_
.set_tag(kCHLO
);
78 ConstructHandshakeMessage();
80 EXPECT_CALL(*connection_
, SendConnectionCloseWithDetails(
81 QUIC_INVALID_CRYPTO_MESSAGE_TYPE
, "Expected REJ"));
82 stream_
->ProcessRawData(message_data_
->data(), message_data_
->length());
85 TEST_F(QuicCryptoClientStreamTest
, NegotiatedParameters
) {
86 CompleteCryptoHandshake();
88 const QuicConfig
* config
= session_
->config();
89 EXPECT_EQ(FLAGS_enable_quic_pacing
? kPACE
: kQBIC
,
90 config
->congestion_control());
91 EXPECT_EQ(kDefaultTimeoutSecs
,
92 config
->idle_connection_state_lifetime().ToSeconds());
93 EXPECT_EQ(kDefaultMaxStreamsPerConnection
,
94 config
->max_streams_per_connection());
95 EXPECT_EQ(0, config
->keepalive_timeout().ToSeconds());
97 const QuicCryptoNegotiatedParameters
& crypto_params(
98 stream_
->crypto_negotiated_params());
99 EXPECT_EQ(kAESG
, crypto_params
.aead
);
100 EXPECT_EQ(kC255
, crypto_params
.key_exchange
);
103 TEST_F(QuicCryptoClientStreamTest
, InvalidHostname
) {
104 stream_
.reset(new QuicCryptoClientStream("invalid", session_
.get(),
106 session_
->SetCryptoStream(stream_
.get());
108 CompleteCryptoHandshake();
109 EXPECT_TRUE(stream_
->encryption_established());
110 EXPECT_TRUE(stream_
->handshake_confirmed());
113 TEST_F(QuicCryptoClientStreamTest
, ExpiredServerConfig
) {
114 // Seed the config with a cached server config.
115 CompleteCryptoHandshake();
117 connection_
= new PacketSavingConnection(true);
118 session_
.reset(new TestSession(connection_
, DefaultQuicConfig()));
119 stream_
.reset(new QuicCryptoClientStream(kServerHostname
, session_
.get(),
122 session_
->SetCryptoStream(stream_
.get());
123 session_
->config()->SetDefaults();
125 // Advance time 5 years to ensure that we pass the expiry time of the cached
127 reinterpret_cast<MockClock
*>(const_cast<QuicClock
*>(connection_
->clock()))
128 ->AdvanceTime(QuicTime::Delta::FromSeconds(60 * 60 * 24 * 365 * 5));
130 // Check that a client hello was sent and that CryptoConnect doesn't fail
132 EXPECT_TRUE(stream_
->CryptoConnect());
133 ASSERT_EQ(1u, connection_
->packets_
.size());