Upstream version 11.40.277.0
[platform/framework/web/crosswalk.git] / src / net / tools / quic / quic_client_session_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/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 "net/tools/quic/test_tools/quic_test_utils.h"
17 #include "testing/gtest/include/gtest/gtest.h"
18
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;
29 using testing::_;
30
31 namespace net {
32 namespace tools {
33 namespace test {
34 namespace {
35
36 const char kServerHostname[] = "www.example.org";
37 const uint16 kPort = 80;
38
39 class ToolsQuicClientSessionTest
40     : public ::testing::TestWithParam<QuicVersion> {
41  protected:
42   ToolsQuicClientSessionTest()
43       : connection_(
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),
49         &crypto_config_);
50   }
51
52   void CompleteCryptoHandshake() {
53     ASSERT_TRUE(session_->CryptoConnect());
54     CryptoTestUtils::HandshakeWithFakeServer(
55         connection_, session_->GetCryptoStream());
56   }
57
58   PacketSavingConnection* connection_;
59   scoped_ptr<QuicClientSession> session_;
60   QuicCryptoClientConfig crypto_config_;
61 };
62
63 INSTANTIATE_TEST_CASE_P(Tests, ToolsQuicClientSessionTest,
64                         ::testing::ValuesIn(QuicSupportedVersions()));
65
66 TEST_P(ToolsQuicClientSessionTest, CryptoConnect) {
67   CompleteCryptoHandshake();
68 }
69
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();
75
76   QuicSpdyClientStream* stream = session_->CreateOutgoingDataStream();
77   ASSERT_TRUE(stream);
78   EXPECT_FALSE(session_->CreateOutgoingDataStream());
79
80   // Close a stream and ensure I can now open a new one.
81   session_->CloseStream(stream->id());
82   stream = session_->CreateOutgoingDataStream();
83   EXPECT_TRUE(stream);
84 }
85
86 TEST_P(ToolsQuicClientSessionTest, GoAwayReceived) {
87   CompleteCryptoHandshake();
88
89   // After receiving a GoAway, I should no longer be able to create outgoing
90   // streams.
91   session_->OnGoAway(QuicGoAwayFrame(QUIC_PEER_GOING_AWAY, 1u, "Going away."));
92   EXPECT_EQ(nullptr, session_->CreateOutgoingDataStream());
93 }
94
95 TEST_P(ToolsQuicClientSessionTest, SetFecProtectionFromConfig) {
96   ValueRestore<bool> old_flag(&FLAGS_enable_quic_fec, true);
97
98   // Set FEC config in client's connection options.
99   QuicTagVector copt;
100   copt.push_back(kFHDR);
101   session_->config()->SetConnectionOptionsToSend(copt);
102
103   // Doing the handshake should set up FEC config correctly.
104   CompleteCryptoHandshake();
105
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();
111   ASSERT_TRUE(stream);
112   EXPECT_EQ(FEC_PROTECT_OPTIONAL, stream->fec_policy());
113 }
114
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);
121
122   EXPECT_CALL(*reinterpret_cast<MockConnection*>(session_->connection()),
123               ProcessUdpPacket(server_address, client_address, _))
124       .WillRepeatedly(
125           Invoke(reinterpret_cast<MockConnection*>(session_->connection()),
126                  &MockConnection::ReallyProcessUdpPacket));
127
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,
131                                            invalid_packet);
132
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,
139                                            valid_packet);
140 }
141
142 }  // namespace
143 }  // namespace test
144 }  // namespace tools
145 }  // namespace net