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/tools/quic/quic_client_session.h"
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 "net/tools/quic/test_tools/quic_test_utils.h"
17 #include "testing/gtest/include/gtest/gtest.h"
19 using net::test::CryptoTestUtils;
20 using net::test::DefaultQuicConfig;
21 using net::test::PacketSavingConnection;
22 using net::test::QuicSessionPeer;
23 using net::test::SupportedVersions;
24 using net::test::TestPeerIPAddress;
25 using net::test::ValueRestore;
26 using net::test::kTestPort;
27 using net::tools::test::MockConnection;
28 using testing::Invoke;
36 const char kServerHostname[] = "www.example.org";
37 const uint16 kPort = 80;
39 class ToolsQuicClientSessionTest
40 : public ::testing::TestWithParam<QuicVersion> {
42 ToolsQuicClientSessionTest()
44 new PacketSavingConnection(false, SupportedVersions(GetParam()))) {
45 session_.reset(new QuicClientSession(DefaultQuicConfig(), connection_,
46 /*is_secure=*/false));
47 session_->InitializeSession(
48 QuicServerId(kServerHostname, kPort, false, PRIVACY_MODE_DISABLED),
52 void CompleteCryptoHandshake() {
53 ASSERT_TRUE(session_->CryptoConnect());
54 CryptoTestUtils::HandshakeWithFakeServer(
55 connection_, session_->GetCryptoStream());
58 PacketSavingConnection* connection_;
59 scoped_ptr<QuicClientSession> session_;
60 QuicCryptoClientConfig crypto_config_;
63 INSTANTIATE_TEST_CASE_P(Tests, ToolsQuicClientSessionTest,
64 ::testing::ValuesIn(QuicSupportedVersions()));
66 TEST_P(ToolsQuicClientSessionTest, CryptoConnect) {
67 CompleteCryptoHandshake();
70 TEST_P(ToolsQuicClientSessionTest, MaxNumStreams) {
71 session_->config()->SetMaxStreamsPerConnection(1, 1);
72 // FLAGS_max_streams_per_connection = 1;
73 // Initialize crypto before the client session will create a stream.
74 CompleteCryptoHandshake();
76 QuicSpdyClientStream* stream = session_->CreateOutgoingDataStream();
78 EXPECT_FALSE(session_->CreateOutgoingDataStream());
80 // Close a stream and ensure I can now open a new one.
81 session_->CloseStream(stream->id());
82 stream = session_->CreateOutgoingDataStream();
86 TEST_P(ToolsQuicClientSessionTest, GoAwayReceived) {
87 CompleteCryptoHandshake();
89 // After receiving a GoAway, I should no longer be able to create outgoing
91 session_->OnGoAway(QuicGoAwayFrame(QUIC_PEER_GOING_AWAY, 1u, "Going away."));
92 EXPECT_EQ(nullptr, session_->CreateOutgoingDataStream());
95 TEST_P(ToolsQuicClientSessionTest, SetFecProtectionFromConfig) {
96 ValueRestore<bool> old_flag(&FLAGS_enable_quic_fec, true);
98 // Set FEC config in client's connection options.
100 copt.push_back(kFHDR);
101 session_->config()->SetConnectionOptionsToSend(copt);
103 // Doing the handshake should set up FEC config correctly.
104 CompleteCryptoHandshake();
106 // Verify that headers stream is always protected and data streams are
107 // optionally protected.
108 EXPECT_EQ(FEC_PROTECT_ALWAYS,
109 QuicSessionPeer::GetHeadersStream(session_.get())->fec_policy());
110 QuicSpdyClientStream* stream = session_->CreateOutgoingDataStream();
112 EXPECT_EQ(FEC_PROTECT_OPTIONAL, stream->fec_policy());
115 // Regression test for b/17206611.
116 TEST_P(ToolsQuicClientSessionTest, InvalidPacketReceived) {
117 // Create Packet with 0 length.
118 QuicEncryptedPacket invalid_packet(nullptr, 0, false);
119 IPEndPoint server_address(TestPeerIPAddress(), kTestPort);
120 IPEndPoint client_address(TestPeerIPAddress(), kTestPort);
122 EXPECT_CALL(*reinterpret_cast<MockConnection*>(session_->connection()),
123 ProcessUdpPacket(server_address, client_address, _))
125 Invoke(reinterpret_cast<MockConnection*>(session_->connection()),
126 &MockConnection::ReallyProcessUdpPacket));
128 // Validate that empty packets don't close the connection.
129 EXPECT_CALL(*connection_, SendConnectionCloseWithDetails(_, _)).Times(0);
130 session_->connection()->ProcessUdpPacket(client_address, server_address,
133 // Verifiy that small, invalid packets don't close the connection.
134 char buf[2] = {0x00, 0x01};
135 QuicEncryptedPacket valid_packet(buf, 2, false);
136 // Close connection shouldn't be called.
137 EXPECT_CALL(*connection_, SendConnectionCloseWithDetails(_, _)).Times(0);
138 session_->connection()->ProcessUdpPacket(client_address, server_address,