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()
28 connection_(new PacketSavingConnection(1, addr_, true)),
29 session_(new TestSession(connection_, DefaultQuicConfig(), true)),
30 stream_(new QuicCryptoClientStream(kServerHostname, session_.get(),
32 session_->SetCryptoStream(stream_.get());
33 crypto_config_.SetDefaults();
36 void CompleteCryptoHandshake() {
37 EXPECT_TRUE(stream_->CryptoConnect());
38 CryptoTestUtils::HandshakeWithFakeServer(connection_, stream_.get());
41 void ConstructHandshakeMessage() {
43 message_data_.reset(framer.ConstructHandshakeMessage(message_));
47 PacketSavingConnection* connection_;
48 scoped_ptr<TestSession> session_;
49 scoped_ptr<QuicCryptoClientStream> stream_;
50 CryptoHandshakeMessage message_;
51 scoped_ptr<QuicData> message_data_;
52 QuicCryptoClientConfig crypto_config_;
55 TEST_F(QuicCryptoClientStreamTest, NotInitiallyConected) {
56 EXPECT_FALSE(stream_->encryption_established());
57 EXPECT_FALSE(stream_->handshake_confirmed());
60 TEST_F(QuicCryptoClientStreamTest, ConnectedAfterSHLO) {
61 CompleteCryptoHandshake();
62 EXPECT_TRUE(stream_->encryption_established());
63 EXPECT_TRUE(stream_->handshake_confirmed());
66 TEST_F(QuicCryptoClientStreamTest, MessageAfterHandshake) {
67 CompleteCryptoHandshake();
69 EXPECT_CALL(*connection_, SendConnectionClose(
70 QUIC_CRYPTO_MESSAGE_AFTER_HANDSHAKE_COMPLETE));
71 message_.set_tag(kCHLO);
72 ConstructHandshakeMessage();
73 stream_->ProcessData(message_data_->data(), message_data_->length());
76 TEST_F(QuicCryptoClientStreamTest, BadMessageType) {
77 EXPECT_TRUE(stream_->CryptoConnect());
79 message_.set_tag(kCHLO);
80 ConstructHandshakeMessage();
82 EXPECT_CALL(*connection_, SendConnectionCloseWithDetails(
83 QUIC_INVALID_CRYPTO_MESSAGE_TYPE, "Expected REJ"));
84 stream_->ProcessData(message_data_->data(), message_data_->length());
87 TEST_F(QuicCryptoClientStreamTest, NegotiatedParameters) {
88 CompleteCryptoHandshake();
90 const QuicConfig* config = session_->config();
91 EXPECT_EQ(kQBIC, config->congestion_control());
92 EXPECT_EQ(kDefaultTimeoutSecs,
93 config->idle_connection_state_lifetime().ToSeconds());
94 EXPECT_EQ(kDefaultMaxStreamsPerConnection,
95 config->max_streams_per_connection());
96 EXPECT_EQ(0, config->keepalive_timeout().ToSeconds());
98 const QuicCryptoNegotiatedParameters& crypto_params(
99 stream_->crypto_negotiated_params());
100 EXPECT_EQ(kAESG, crypto_params.aead);
101 EXPECT_EQ(kC255, crypto_params.key_exchange);
104 TEST_F(QuicCryptoClientStreamTest, InvalidHostname) {
105 stream_.reset(new QuicCryptoClientStream("invalid", session_.get(),
107 session_->SetCryptoStream(stream_.get());
109 CompleteCryptoHandshake();
110 EXPECT_TRUE(stream_->encryption_established());
111 EXPECT_TRUE(stream_->handshake_confirmed());
114 TEST_F(QuicCryptoClientStreamTest, ExpiredServerConfig) {
115 // Seed the config with a cached server config.
116 CompleteCryptoHandshake();
118 connection_ = new PacketSavingConnection(1, addr_, true);
119 session_.reset(new TestSession(connection_, DefaultQuicConfig(), true));
120 stream_.reset(new QuicCryptoClientStream(kServerHostname, session_.get(),
123 session_->SetCryptoStream(stream_.get());
124 session_->config()->SetDefaults();
126 // Advance time 5 years to ensure that we pass the expiry time of the cached
128 reinterpret_cast<MockClock*>(const_cast<QuicClock*>(connection_->clock()))
129 ->AdvanceTime(QuicTime::Delta::FromSeconds(60 * 60 * 24 * 365 * 5));
131 // Check that a client hello was sent and that CryptoConnect doesn't fail
133 EXPECT_TRUE(stream_->CryptoConnect());
134 ASSERT_EQ(1u, connection_->packets_.size());