quic_client_session_test.cc revision 116680a4aac90f2aa7413d9095a592090648e557
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/tools/quic/quic_client_session.h"
6
7#include <vector>
8
9#include "net/base/ip_endpoint.h"
10#include "net/quic/crypto/aes_128_gcm_12_encrypter.h"
11#include "net/quic/quic_flags.h"
12#include "net/quic/test_tools/crypto_test_utils.h"
13#include "net/quic/test_tools/quic_session_peer.h"
14#include "net/quic/test_tools/quic_test_utils.h"
15#include "net/tools/quic/quic_spdy_client_stream.h"
16#include "testing/gtest/include/gtest/gtest.h"
17
18using net::test::CryptoTestUtils;
19using net::test::DefaultQuicConfig;
20using net::test::PacketSavingConnection;
21using net::test::QuicSessionPeer;
22using net::test::SupportedVersions;
23using net::test::ValueRestore;
24using testing::_;
25
26namespace net {
27namespace tools {
28namespace test {
29namespace {
30
31const char kServerHostname[] = "www.example.com";
32const uint16 kPort = 80;
33
34class ToolsQuicClientSessionTest
35    : public ::testing::TestWithParam<QuicVersion> {
36 protected:
37  ToolsQuicClientSessionTest()
38      : connection_(new PacketSavingConnection(false,
39                                               SupportedVersions(GetParam()))) {
40    crypto_config_.SetDefaults();
41    session_.reset(new QuicClientSession(DefaultQuicConfig(), connection_));
42    session_->InitializeSession(
43        QuicServerId(kServerHostname, kPort, false, PRIVACY_MODE_DISABLED),
44        &crypto_config_);
45    session_->config()->SetDefaults();
46  }
47
48  void CompleteCryptoHandshake() {
49    ASSERT_TRUE(session_->CryptoConnect());
50    CryptoTestUtils::HandshakeWithFakeServer(
51        connection_, session_->GetCryptoStream());
52  }
53
54  PacketSavingConnection* connection_;
55  scoped_ptr<QuicClientSession> session_;
56  QuicCryptoClientConfig crypto_config_;
57};
58
59INSTANTIATE_TEST_CASE_P(Tests, ToolsQuicClientSessionTest,
60                        ::testing::ValuesIn(QuicSupportedVersions()));
61
62TEST_P(ToolsQuicClientSessionTest, CryptoConnect) {
63  CompleteCryptoHandshake();
64}
65
66TEST_P(ToolsQuicClientSessionTest, MaxNumStreams) {
67  session_->config()->set_max_streams_per_connection(1, 1);
68  // FLAGS_max_streams_per_connection = 1;
69  // Initialize crypto before the client session will create a stream.
70  CompleteCryptoHandshake();
71
72  QuicSpdyClientStream* stream =
73      session_->CreateOutgoingDataStream();
74  ASSERT_TRUE(stream);
75  EXPECT_FALSE(session_->CreateOutgoingDataStream());
76
77  // Close a stream and ensure I can now open a new one.
78  session_->CloseStream(stream->id());
79  stream = session_->CreateOutgoingDataStream();
80  EXPECT_TRUE(stream);
81}
82
83TEST_P(ToolsQuicClientSessionTest, GoAwayReceived) {
84  CompleteCryptoHandshake();
85
86  // After receiving a GoAway, I should no longer be able to create outgoing
87  // streams.
88  session_->OnGoAway(QuicGoAwayFrame(QUIC_PEER_GOING_AWAY, 1u, "Going away."));
89  EXPECT_EQ(NULL, session_->CreateOutgoingDataStream());
90}
91
92TEST_P(ToolsQuicClientSessionTest, SetFecProtectionFromConfig) {
93  ValueRestore<bool> old_flag(&FLAGS_enable_quic_fec, true);
94
95  // Set FEC config in client's connection options.
96  QuicTagVector copt;
97  copt.push_back(kFHDR);
98  session_->config()->SetConnectionOptionsToSend(copt);
99
100  // Doing the handshake should set up FEC config correctly.
101  CompleteCryptoHandshake();
102
103  // Verify that headers stream is always protected and data streams are
104  // optionally protected.
105  EXPECT_EQ(FEC_PROTECT_ALWAYS,
106            QuicSessionPeer::GetHeadersStream(session_.get())->fec_policy());
107  QuicSpdyClientStream* stream = session_->CreateOutgoingDataStream();
108  ASSERT_TRUE(stream);
109  EXPECT_EQ(FEC_PROTECT_OPTIONAL, stream->fec_policy());
110}
111
112}  // namespace
113}  // namespace test
114}  // namespace tools
115}  // namespace net
116