[email protected] | dd3fd0e | 2012-11-04 05:14:40 | [diff] [blame] | 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_client_session.h" |
| 6 | |
| 7 | #include <vector> |
| 8 | |
[email protected] | 4d283b3 | 2013-10-17 12:57:27 | [diff] [blame] | 9 | #include "base/rand_util.h" |
[email protected] | 0d10b59 | 2013-02-14 16:09:26 | [diff] [blame] | 10 | #include "net/base/capturing_net_log.h" |
[email protected] | 8ee611b | 2012-11-20 01:48:12 | [diff] [blame] | 11 | #include "net/base/test_completion_callback.h" |
[email protected] | 0bbeb697 | 2013-05-23 04:10:21 | [diff] [blame] | 12 | #include "net/quic/crypto/aes_128_gcm_12_encrypter.h" |
[email protected] | dd3fd0e | 2012-11-04 05:14:40 | [diff] [blame] | 13 | #include "net/quic/crypto/crypto_protocol.h" |
[email protected] | 4df6984 | 2013-02-27 06:32:16 | [diff] [blame] | 14 | #include "net/quic/crypto/quic_decrypter.h" |
| 15 | #include "net/quic/crypto/quic_encrypter.h" |
[email protected] | 17bf15c | 2014-03-14 10:08:04 | [diff] [blame] | 16 | #include "net/quic/crypto/quic_server_info.h" |
[email protected] | cbd731e | 2013-10-24 00:20:39 | [diff] [blame] | 17 | #include "net/quic/quic_default_packet_writer.h" |
[email protected] | ed3fc15d | 2013-03-08 18:37:44 | [diff] [blame] | 18 | #include "net/quic/test_tools/crypto_test_utils.h" |
[email protected] | 89995165 | 2013-05-16 12:52:39 | [diff] [blame] | 19 | #include "net/quic/test_tools/quic_client_session_peer.h" |
[email protected] | dd3fd0e | 2012-11-04 05:14:40 | [diff] [blame] | 20 | #include "net/quic/test_tools/quic_test_utils.h" |
[email protected] | c58a83ec | 2014-04-20 22:21:50 | [diff] [blame] | 21 | #include "net/quic/test_tools/simple_quic_framer.h" |
[email protected] | 4d283b3 | 2013-10-17 12:57:27 | [diff] [blame] | 22 | #include "net/socket/socket_test_util.h" |
[email protected] | 18ccfdb | 2013-08-15 00:13:44 | [diff] [blame] | 23 | #include "net/udp/datagram_client_socket.h" |
[email protected] | dd3fd0e | 2012-11-04 05:14:40 | [diff] [blame] | 24 | |
| 25 | using testing::_; |
| 26 | |
| 27 | namespace net { |
| 28 | namespace test { |
| 29 | namespace { |
| 30 | |
[email protected] | 41d6b17 | 2013-01-29 16:10:57 | [diff] [blame] | 31 | const char kServerHostname[] = "www.example.com"; |
[email protected] | e4c3ea6 | 2014-03-15 00:45:14 | [diff] [blame] | 32 | const uint16 kServerPort = 80; |
[email protected] | 41d6b17 | 2013-01-29 16:10:57 | [diff] [blame] | 33 | |
[email protected] | cbd731e | 2013-10-24 00:20:39 | [diff] [blame] | 34 | class TestPacketWriter : public QuicDefaultPacketWriter { |
| 35 | public: |
[email protected] | c58a83ec | 2014-04-20 22:21:50 | [diff] [blame] | 36 | TestPacketWriter(QuicVersion version) : version_(version) { |
[email protected] | cbd731e | 2013-10-24 00:20:39 | [diff] [blame] | 37 | } |
| 38 | |
| 39 | // QuicPacketWriter |
| 40 | virtual WriteResult WritePacket( |
| 41 | const char* buffer, size_t buf_len, |
| 42 | const IPAddressNumber& self_address, |
[email protected] | c5e1aca | 2014-01-30 04:03:04 | [diff] [blame] | 43 | const IPEndPoint& peer_address) OVERRIDE { |
[email protected] | c58a83ec | 2014-04-20 22:21:50 | [diff] [blame] | 44 | SimpleQuicFramer framer(SupportedVersions(version_)); |
[email protected] | cbd731e | 2013-10-24 00:20:39 | [diff] [blame] | 45 | QuicEncryptedPacket packet(buffer, buf_len); |
| 46 | EXPECT_TRUE(framer.ProcessPacket(packet)); |
[email protected] | c58a83ec | 2014-04-20 22:21:50 | [diff] [blame] | 47 | header_ = framer.header(); |
[email protected] | cbd731e | 2013-10-24 00:20:39 | [diff] [blame] | 48 | return WriteResult(WRITE_STATUS_OK, packet.length()); |
| 49 | } |
| 50 | |
| 51 | virtual bool IsWriteBlockedDataBuffered() const OVERRIDE { |
| 52 | // Chrome sockets' Write() methods buffer the data until the Write is |
| 53 | // permitted. |
| 54 | return true; |
| 55 | } |
| 56 | |
| 57 | // Returns the header from the last packet written. |
| 58 | const QuicPacketHeader& header() { return header_; } |
| 59 | |
| 60 | private: |
[email protected] | c58a83ec | 2014-04-20 22:21:50 | [diff] [blame] | 61 | QuicVersion version_; |
[email protected] | cbd731e | 2013-10-24 00:20:39 | [diff] [blame] | 62 | QuicPacketHeader header_; |
| 63 | }; |
| 64 | |
[email protected] | 4d64079 | 2013-12-18 22:21:08 | [diff] [blame] | 65 | class QuicClientSessionTest : public ::testing::TestWithParam<QuicVersion> { |
[email protected] | dd3fd0e | 2012-11-04 05:14:40 | [diff] [blame] | 66 | protected: |
| 67 | QuicClientSessionTest() |
[email protected] | c58a83ec | 2014-04-20 22:21:50 | [diff] [blame] | 68 | : writer_(new TestPacketWriter(GetParam())), |
[email protected] | 5d45daa | 2014-01-02 21:07:46 | [diff] [blame] | 69 | connection_( |
| 70 | new PacketSavingConnection(false, SupportedVersions(GetParam()))), |
[email protected] | 89f79a4 | 2014-03-19 11:30:01 | [diff] [blame] | 71 | session_(connection_, GetSocket().Pass(), writer_.Pass(), NULL, NULL, |
| 72 | make_scoped_ptr((QuicServerInfo*)NULL), |
[email protected] | 257f24f | 2014-04-01 09:15:37 | [diff] [blame] | 73 | QuicServerId(kServerHostname, kServerPort, false, |
| 74 | PRIVACY_MODE_DISABLED), |
[email protected] | ce7bb141 | 2014-05-17 15:51:33 | [diff] [blame] | 75 | DefaultQuicConfig(), kInitialFlowControlWindowForTest, |
| 76 | &crypto_config_, &net_log_) { |
[email protected] | 47a7154 | 2013-05-17 07:58:54 | [diff] [blame] | 77 | session_.config()->SetDefaults(); |
[email protected] | ef95114d | 2013-04-17 17:57:01 | [diff] [blame] | 78 | crypto_config_.SetDefaults(); |
[email protected] | dd3fd0e | 2012-11-04 05:14:40 | [diff] [blame] | 79 | } |
| 80 | |
[email protected] | 4d283b3 | 2013-10-17 12:57:27 | [diff] [blame] | 81 | virtual void TearDown() OVERRIDE { |
| 82 | session_.CloseSessionOnError(ERR_ABORTED); |
| 83 | } |
| 84 | |
| 85 | scoped_ptr<DatagramClientSocket> GetSocket() { |
| 86 | socket_factory_.AddSocketDataProvider(&socket_data_); |
| 87 | return socket_factory_.CreateDatagramClientSocket( |
| 88 | DatagramSocket::DEFAULT_BIND, base::Bind(&base::RandInt), |
| 89 | &net_log_, NetLog::Source()); |
| 90 | } |
| 91 | |
[email protected] | ed3fc15d | 2013-03-08 18:37:44 | [diff] [blame] | 92 | void CompleteCryptoHandshake() { |
| 93 | ASSERT_EQ(ERR_IO_PENDING, |
[email protected] | 11c0587 | 2013-08-20 02:04:12 | [diff] [blame] | 94 | session_.CryptoConnect(false, callback_.callback())); |
[email protected] | e8ff2684 | 2013-03-22 21:02:05 | [diff] [blame] | 95 | CryptoTestUtils::HandshakeWithFakeServer( |
| 96 | connection_, session_.GetCryptoStream()); |
[email protected] | ed3fc15d | 2013-03-08 18:37:44 | [diff] [blame] | 97 | ASSERT_EQ(OK, callback_.WaitForResult()); |
[email protected] | ed3fc15d | 2013-03-08 18:37:44 | [diff] [blame] | 98 | } |
| 99 | |
[email protected] | cbd731e | 2013-10-24 00:20:39 | [diff] [blame] | 100 | scoped_ptr<QuicDefaultPacketWriter> writer_; |
[email protected] | dd3fd0e | 2012-11-04 05:14:40 | [diff] [blame] | 101 | PacketSavingConnection* connection_; |
[email protected] | ed3fc15d | 2013-03-08 18:37:44 | [diff] [blame] | 102 | CapturingNetLog net_log_; |
[email protected] | 4d283b3 | 2013-10-17 12:57:27 | [diff] [blame] | 103 | MockClientSocketFactory socket_factory_; |
| 104 | StaticSocketDataProvider socket_data_; |
[email protected] | dd3fd0e | 2012-11-04 05:14:40 | [diff] [blame] | 105 | QuicClientSession session_; |
[email protected] | ed3fc15d | 2013-03-08 18:37:44 | [diff] [blame] | 106 | MockClock clock_; |
| 107 | MockRandom random_; |
[email protected] | dd3fd0e | 2012-11-04 05:14:40 | [diff] [blame] | 108 | QuicConnectionVisitorInterface* visitor_; |
[email protected] | 8ee611b | 2012-11-20 01:48:12 | [diff] [blame] | 109 | TestCompletionCallback callback_; |
[email protected] | ef95114d | 2013-04-17 17:57:01 | [diff] [blame] | 110 | QuicCryptoClientConfig crypto_config_; |
[email protected] | dd3fd0e | 2012-11-04 05:14:40 | [diff] [blame] | 111 | }; |
| 112 | |
[email protected] | 4d64079 | 2013-12-18 22:21:08 | [diff] [blame] | 113 | INSTANTIATE_TEST_CASE_P(Tests, QuicClientSessionTest, |
| 114 | ::testing::ValuesIn(QuicSupportedVersions())); |
| 115 | |
| 116 | TEST_P(QuicClientSessionTest, CryptoConnect) { |
[email protected] | ed3fc15d | 2013-03-08 18:37:44 | [diff] [blame] | 117 | CompleteCryptoHandshake(); |
[email protected] | 8ee611b | 2012-11-20 01:48:12 | [diff] [blame] | 118 | } |
| 119 | |
[email protected] | 4d64079 | 2013-12-18 22:21:08 | [diff] [blame] | 120 | TEST_P(QuicClientSessionTest, MaxNumStreams) { |
[email protected] | ed3fc15d | 2013-03-08 18:37:44 | [diff] [blame] | 121 | CompleteCryptoHandshake(); |
[email protected] | dd3fd0e | 2012-11-04 05:14:40 | [diff] [blame] | 122 | |
| 123 | std::vector<QuicReliableClientStream*> streams; |
| 124 | for (size_t i = 0; i < kDefaultMaxStreamsPerConnection; i++) { |
[email protected] | 457d695 | 2013-12-13 09:24:58 | [diff] [blame] | 125 | QuicReliableClientStream* stream = session_.CreateOutgoingDataStream(); |
[email protected] | dd3fd0e | 2012-11-04 05:14:40 | [diff] [blame] | 126 | EXPECT_TRUE(stream); |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 127 | streams.push_back(stream); |
[email protected] | dd3fd0e | 2012-11-04 05:14:40 | [diff] [blame] | 128 | } |
[email protected] | 457d695 | 2013-12-13 09:24:58 | [diff] [blame] | 129 | EXPECT_FALSE(session_.CreateOutgoingDataStream()); |
[email protected] | dd3fd0e | 2012-11-04 05:14:40 | [diff] [blame] | 130 | |
| 131 | // Close a stream and ensure I can now open a new one. |
| 132 | session_.CloseStream(streams[0]->id()); |
[email protected] | 457d695 | 2013-12-13 09:24:58 | [diff] [blame] | 133 | EXPECT_TRUE(session_.CreateOutgoingDataStream()); |
[email protected] | dd3fd0e | 2012-11-04 05:14:40 | [diff] [blame] | 134 | } |
| 135 | |
[email protected] | 4d64079 | 2013-12-18 22:21:08 | [diff] [blame] | 136 | TEST_P(QuicClientSessionTest, MaxNumStreamsViaRequest) { |
[email protected] | 0b2294d3 | 2013-08-02 00:46:36 | [diff] [blame] | 137 | CompleteCryptoHandshake(); |
| 138 | |
| 139 | std::vector<QuicReliableClientStream*> streams; |
| 140 | for (size_t i = 0; i < kDefaultMaxStreamsPerConnection; i++) { |
[email protected] | 457d695 | 2013-12-13 09:24:58 | [diff] [blame] | 141 | QuicReliableClientStream* stream = session_.CreateOutgoingDataStream(); |
[email protected] | 0b2294d3 | 2013-08-02 00:46:36 | [diff] [blame] | 142 | EXPECT_TRUE(stream); |
| 143 | streams.push_back(stream); |
| 144 | } |
| 145 | |
| 146 | QuicReliableClientStream* stream; |
| 147 | QuicClientSession::StreamRequest stream_request; |
| 148 | TestCompletionCallback callback; |
| 149 | ASSERT_EQ(ERR_IO_PENDING, |
| 150 | stream_request.StartRequest(session_.GetWeakPtr(), &stream, |
| 151 | callback.callback())); |
| 152 | |
| 153 | // Close a stream and ensure I can now open a new one. |
| 154 | session_.CloseStream(streams[0]->id()); |
| 155 | ASSERT_TRUE(callback.have_result()); |
| 156 | EXPECT_EQ(OK, callback.WaitForResult()); |
| 157 | EXPECT_TRUE(stream != NULL); |
| 158 | } |
| 159 | |
[email protected] | 4d64079 | 2013-12-18 22:21:08 | [diff] [blame] | 160 | TEST_P(QuicClientSessionTest, GoAwayReceived) { |
[email protected] | 8ba8121 | 2013-05-03 13:11:48 | [diff] [blame] | 161 | CompleteCryptoHandshake(); |
[email protected] | 9db44391 | 2013-02-25 05:27:03 | [diff] [blame] | 162 | |
| 163 | // After receiving a GoAway, I should no longer be able to create outgoing |
| 164 | // streams. |
| 165 | session_.OnGoAway(QuicGoAwayFrame(QUIC_PEER_GOING_AWAY, 1u, "Going away.")); |
[email protected] | 457d695 | 2013-12-13 09:24:58 | [diff] [blame] | 166 | EXPECT_EQ(NULL, session_.CreateOutgoingDataStream()); |
[email protected] | 9db44391 | 2013-02-25 05:27:03 | [diff] [blame] | 167 | } |
| 168 | |
[email protected] | dd3fd0e | 2012-11-04 05:14:40 | [diff] [blame] | 169 | } // namespace |
| 170 | } // namespace test |
| 171 | } // namespace net |