- add sources.
[platform/framework/web/crosswalk.git] / src / net / quic / quic_crypto_client_stream_test.cc
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.
4
5 #include "net/quic/quic_crypto_client_stream.h"
6
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"
17
18 namespace net {
19 namespace test {
20 namespace {
21
22 const char kServerHostname[] = "example.com";
23
24 class QuicCryptoClientStreamTest : public ::testing::Test {
25  public:
26   QuicCryptoClientStreamTest()
27       : addr_(),
28         connection_(new PacketSavingConnection(1, addr_, true)),
29         session_(new TestSession(connection_, DefaultQuicConfig(), true)),
30         stream_(new QuicCryptoClientStream(kServerHostname, session_.get(),
31                                            &crypto_config_)) {
32     session_->SetCryptoStream(stream_.get());
33     crypto_config_.SetDefaults();
34   }
35
36   void CompleteCryptoHandshake() {
37     EXPECT_TRUE(stream_->CryptoConnect());
38     CryptoTestUtils::HandshakeWithFakeServer(connection_, stream_.get());
39   }
40
41   void ConstructHandshakeMessage() {
42     CryptoFramer framer;
43     message_data_.reset(framer.ConstructHandshakeMessage(message_));
44   }
45
46   IPEndPoint addr_;
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_;
53 };
54
55 TEST_F(QuicCryptoClientStreamTest, NotInitiallyConected) {
56   EXPECT_FALSE(stream_->encryption_established());
57   EXPECT_FALSE(stream_->handshake_confirmed());
58 }
59
60 TEST_F(QuicCryptoClientStreamTest, ConnectedAfterSHLO) {
61   CompleteCryptoHandshake();
62   EXPECT_TRUE(stream_->encryption_established());
63   EXPECT_TRUE(stream_->handshake_confirmed());
64 }
65
66 TEST_F(QuicCryptoClientStreamTest, MessageAfterHandshake) {
67   CompleteCryptoHandshake();
68
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());
74 }
75
76 TEST_F(QuicCryptoClientStreamTest, BadMessageType) {
77   EXPECT_TRUE(stream_->CryptoConnect());
78
79   message_.set_tag(kCHLO);
80   ConstructHandshakeMessage();
81
82   EXPECT_CALL(*connection_, SendConnectionCloseWithDetails(
83         QUIC_INVALID_CRYPTO_MESSAGE_TYPE, "Expected REJ"));
84   stream_->ProcessData(message_data_->data(), message_data_->length());
85 }
86
87 TEST_F(QuicCryptoClientStreamTest, NegotiatedParameters) {
88   CompleteCryptoHandshake();
89
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());
97
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);
102 }
103
104 TEST_F(QuicCryptoClientStreamTest, InvalidHostname) {
105   stream_.reset(new QuicCryptoClientStream("invalid", session_.get(),
106                                            &crypto_config_));
107   session_->SetCryptoStream(stream_.get());
108
109   CompleteCryptoHandshake();
110   EXPECT_TRUE(stream_->encryption_established());
111   EXPECT_TRUE(stream_->handshake_confirmed());
112 }
113
114 TEST_F(QuicCryptoClientStreamTest, ExpiredServerConfig) {
115   // Seed the config with a cached server config.
116   CompleteCryptoHandshake();
117
118   connection_ = new PacketSavingConnection(1, addr_, true);
119   session_.reset(new TestSession(connection_, DefaultQuicConfig(), true));
120   stream_.reset(new QuicCryptoClientStream(kServerHostname, session_.get(),
121                                            &crypto_config_));
122
123   session_->SetCryptoStream(stream_.get());
124   session_->config()->SetDefaults();
125
126   // Advance time 5 years to ensure that we pass the expiry time of the cached
127   // server config.
128   reinterpret_cast<MockClock*>(const_cast<QuicClock*>(connection_->clock()))
129       ->AdvanceTime(QuicTime::Delta::FromSeconds(60 * 60 * 24 * 365 * 5));
130
131   // Check that a client hello was sent and that CryptoConnect doesn't fail
132   // with an error.
133   EXPECT_TRUE(stream_->CryptoConnect());
134   ASSERT_EQ(1u, connection_->packets_.size());
135 }
136
137 }  // namespace
138 }  // namespace test
139 }  // namespace net