Home | History | Annotate | Download | only in quic
      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/test_tools/crypto_test_utils.h"
     12 #include "net/quic/test_tools/quic_test_utils.h"
     13 #include "net/tools/quic/quic_spdy_client_stream.h"
     14 #include "testing/gtest/include/gtest/gtest.h"
     15 
     16 using net::test::CryptoTestUtils;
     17 using net::test::DefaultQuicConfig;
     18 using net::test::PacketSavingConnection;
     19 using net::test::SupportedVersions;
     20 using testing::_;
     21 
     22 namespace net {
     23 namespace tools {
     24 namespace test {
     25 namespace {
     26 
     27 const char kServerHostname[] = "www.example.com";
     28 const uint16 kPort = 80;
     29 
     30 class ToolsQuicClientSessionTest
     31     : public ::testing::TestWithParam<QuicVersion> {
     32  protected:
     33   ToolsQuicClientSessionTest()
     34       : connection_(new PacketSavingConnection(false,
     35                                                SupportedVersions(GetParam()))) {
     36     crypto_config_.SetDefaults();
     37     session_.reset(new QuicClientSession(
     38         QuicServerId(kServerHostname, kPort, false, PRIVACY_MODE_DISABLED),
     39         DefaultQuicConfig(),
     40         connection_,
     41         &crypto_config_));
     42     session_->config()->SetDefaults();
     43   }
     44 
     45   void CompleteCryptoHandshake() {
     46     ASSERT_TRUE(session_->CryptoConnect());
     47     CryptoTestUtils::HandshakeWithFakeServer(
     48         connection_, session_->GetCryptoStream());
     49   }
     50 
     51   PacketSavingConnection* connection_;
     52   scoped_ptr<QuicClientSession> session_;
     53   QuicCryptoClientConfig crypto_config_;
     54 };
     55 
     56 INSTANTIATE_TEST_CASE_P(Tests, ToolsQuicClientSessionTest,
     57                         ::testing::ValuesIn(QuicSupportedVersions()));
     58 
     59 TEST_P(ToolsQuicClientSessionTest, CryptoConnect) {
     60   CompleteCryptoHandshake();
     61 }
     62 
     63 TEST_P(ToolsQuicClientSessionTest, MaxNumStreams) {
     64   session_->config()->set_max_streams_per_connection(1, 1);
     65   // FLAGS_max_streams_per_connection = 1;
     66   // Initialize crypto before the client session will create a stream.
     67   CompleteCryptoHandshake();
     68 
     69   QuicSpdyClientStream* stream =
     70       session_->CreateOutgoingDataStream();
     71   ASSERT_TRUE(stream);
     72   EXPECT_FALSE(session_->CreateOutgoingDataStream());
     73 
     74   // Close a stream and ensure I can now open a new one.
     75   session_->CloseStream(stream->id());
     76   stream = session_->CreateOutgoingDataStream();
     77   EXPECT_TRUE(stream);
     78 }
     79 
     80 TEST_P(ToolsQuicClientSessionTest, GoAwayReceived) {
     81   CompleteCryptoHandshake();
     82 
     83   // After receiving a GoAway, I should no longer be able to create outgoing
     84   // streams.
     85   session_->OnGoAway(QuicGoAwayFrame(QUIC_PEER_GOING_AWAY, 1u, "Going away."));
     86   EXPECT_EQ(NULL, session_->CreateOutgoingDataStream());
     87 }
     88 
     89 }  // namespace
     90 }  // namespace test
     91 }  // namespace tools
     92 }  // namespace net
     93