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