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_flags.h"
12 #include "net/quic/quic_protocol.h"
13 #include "net/quic/quic_server_id.h"
14 #include "net/quic/test_tools/crypto_test_utils.h"
15 #include "net/quic/test_tools/quic_test_utils.h"
16 #include "net/quic/test_tools/simple_quic_framer.h"
17 #include "testing/gmock/include/gmock/gmock.h"
18 #include "testing/gtest/include/gtest/gtest.h"
24 const char kServerHostname
[] = "example.com";
25 const uint16 kServerPort
= 80;
27 class QuicCryptoClientStreamTest
: public ::testing::Test
{
29 QuicCryptoClientStreamTest()
30 : connection_(new PacketSavingConnection(false)),
31 session_(new TestClientSession(connection_
, DefaultQuicConfig())),
32 server_id_(kServerHostname
, kServerPort
, false, PRIVACY_MODE_DISABLED
),
33 stream_(new QuicCryptoClientStream(
34 server_id_
, session_
.get(), NULL
, &crypto_config_
)) {
35 session_
->SetCryptoStream(stream_
.get());
36 session_
->config()->SetDefaults();
37 crypto_config_
.SetDefaults();
40 void CompleteCryptoHandshake() {
41 EXPECT_TRUE(stream_
->CryptoConnect());
42 CryptoTestUtils::HandshakeWithFakeServer(connection_
, stream_
.get());
45 void ConstructHandshakeMessage() {
47 message_data_
.reset(framer
.ConstructHandshakeMessage(message_
));
50 PacketSavingConnection
* connection_
;
51 scoped_ptr
<TestClientSession
> session_
;
52 QuicServerId server_id_
;
53 scoped_ptr
<QuicCryptoClientStream
> stream_
;
54 CryptoHandshakeMessage message_
;
55 scoped_ptr
<QuicData
> message_data_
;
56 QuicCryptoClientConfig crypto_config_
;
59 TEST_F(QuicCryptoClientStreamTest
, NotInitiallyConected
) {
60 EXPECT_FALSE(stream_
->encryption_established());
61 EXPECT_FALSE(stream_
->handshake_confirmed());
64 TEST_F(QuicCryptoClientStreamTest
, ConnectedAfterSHLO
) {
65 CompleteCryptoHandshake();
66 EXPECT_TRUE(stream_
->encryption_established());
67 EXPECT_TRUE(stream_
->handshake_confirmed());
70 TEST_F(QuicCryptoClientStreamTest
, MessageAfterHandshake
) {
71 CompleteCryptoHandshake();
73 EXPECT_CALL(*connection_
, SendConnectionClose(
74 QUIC_CRYPTO_MESSAGE_AFTER_HANDSHAKE_COMPLETE
));
75 message_
.set_tag(kCHLO
);
76 ConstructHandshakeMessage();
77 stream_
->ProcessRawData(message_data_
->data(), message_data_
->length());
80 TEST_F(QuicCryptoClientStreamTest
, BadMessageType
) {
81 EXPECT_TRUE(stream_
->CryptoConnect());
83 message_
.set_tag(kCHLO
);
84 ConstructHandshakeMessage();
86 EXPECT_CALL(*connection_
, SendConnectionCloseWithDetails(
87 QUIC_INVALID_CRYPTO_MESSAGE_TYPE
, "Expected REJ"));
88 stream_
->ProcessRawData(message_data_
->data(), message_data_
->length());
91 TEST_F(QuicCryptoClientStreamTest
, NegotiatedParameters
) {
92 CompleteCryptoHandshake();
94 const QuicConfig
* config
= session_
->config();
95 EXPECT_EQ(FLAGS_enable_quic_pacing
? kPACE
: kQBIC
,
96 config
->congestion_feedback());
97 EXPECT_EQ(kDefaultTimeoutSecs
,
98 config
->idle_connection_state_lifetime().ToSeconds());
99 EXPECT_EQ(kDefaultMaxStreamsPerConnection
,
100 config
->max_streams_per_connection());
101 EXPECT_EQ(0, config
->keepalive_timeout().ToSeconds());
103 const QuicCryptoNegotiatedParameters
& crypto_params(
104 stream_
->crypto_negotiated_params());
105 EXPECT_EQ(crypto_config_
.aead
[0], crypto_params
.aead
);
106 EXPECT_EQ(crypto_config_
.kexs
[0], crypto_params
.key_exchange
);
109 TEST_F(QuicCryptoClientStreamTest
, InvalidHostname
) {
110 QuicServerId
server_id("invalid", 80, false, PRIVACY_MODE_DISABLED
);
111 stream_
.reset(new QuicCryptoClientStream(server_id
, session_
.get(), NULL
,
113 session_
->SetCryptoStream(stream_
.get());
115 CompleteCryptoHandshake();
116 EXPECT_TRUE(stream_
->encryption_established());
117 EXPECT_TRUE(stream_
->handshake_confirmed());
120 TEST_F(QuicCryptoClientStreamTest
, ExpiredServerConfig
) {
121 // Seed the config with a cached server config.
122 CompleteCryptoHandshake();
124 connection_
= new PacketSavingConnection(true);
125 session_
.reset(new TestClientSession(connection_
, DefaultQuicConfig()));
126 stream_
.reset(new QuicCryptoClientStream(server_id_
, session_
.get(), NULL
,
129 session_
->SetCryptoStream(stream_
.get());
130 session_
->config()->SetDefaults();
132 // Advance time 5 years to ensure that we pass the expiry time of the cached
134 reinterpret_cast<MockClock
*>(const_cast<QuicClock
*>(connection_
->clock()))
135 ->AdvanceTime(QuicTime::Delta::FromSeconds(60 * 60 * 24 * 365 * 5));
137 // Check that a client hello was sent and that CryptoConnect doesn't fail
139 EXPECT_TRUE(stream_
->CryptoConnect());
140 ASSERT_EQ(1u, connection_
->packets_
.size());