[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] | 5db45220 | 2014-08-19 05:22:15 | [diff] [blame] | 9 | #include "base/base64.h" |
[email protected] | f21ec37 | 2014-07-02 07:15:12 | [diff] [blame] | 10 | #include "base/files/file_path.h" |
[email protected] | 4d283b3 | 2013-10-17 12:57:27 | [diff] [blame] | 11 | #include "base/rand_util.h" |
[email protected] | 0d10b59 | 2013-02-14 16:09:26 | [diff] [blame] | 12 | #include "net/base/capturing_net_log.h" |
[email protected] | 8ee611b | 2012-11-20 01:48:12 | [diff] [blame] | 13 | #include "net/base/test_completion_callback.h" |
[email protected] | f21ec37 | 2014-07-02 07:15:12 | [diff] [blame] | 14 | #include "net/base/test_data_directory.h" |
| 15 | #include "net/cert/cert_verify_result.h" |
[email protected] | 5db45220 | 2014-08-19 05:22:15 | [diff] [blame] | 16 | #include "net/http/transport_security_state.h" |
[email protected] | 0bbeb697 | 2013-05-23 04:10:21 | [diff] [blame] | 17 | #include "net/quic/crypto/aes_128_gcm_12_encrypter.h" |
[email protected] | dd3fd0e | 2012-11-04 05:14:40 | [diff] [blame] | 18 | #include "net/quic/crypto/crypto_protocol.h" |
[email protected] | f21ec37 | 2014-07-02 07:15:12 | [diff] [blame] | 19 | #include "net/quic/crypto/proof_verifier_chromium.h" |
[email protected] | 4df6984 | 2013-02-27 06:32:16 | [diff] [blame] | 20 | #include "net/quic/crypto/quic_decrypter.h" |
| 21 | #include "net/quic/crypto/quic_encrypter.h" |
[email protected] | 17bf15c | 2014-03-14 10:08:04 | [diff] [blame] | 22 | #include "net/quic/crypto/quic_server_info.h" |
[email protected] | ed3fc15d | 2013-03-08 18:37:44 | [diff] [blame] | 23 | #include "net/quic/test_tools/crypto_test_utils.h" |
[email protected] | 89995165 | 2013-05-16 12:52:39 | [diff] [blame] | 24 | #include "net/quic/test_tools/quic_client_session_peer.h" |
[email protected] | dd3fd0e | 2012-11-04 05:14:40 | [diff] [blame] | 25 | #include "net/quic/test_tools/quic_test_utils.h" |
[email protected] | c58a83ec | 2014-04-20 22:21:50 | [diff] [blame] | 26 | #include "net/quic/test_tools/simple_quic_framer.h" |
[email protected] | 4d283b3 | 2013-10-17 12:57:27 | [diff] [blame] | 27 | #include "net/socket/socket_test_util.h" |
[email protected] | 5db45220 | 2014-08-19 05:22:15 | [diff] [blame] | 28 | #include "net/spdy/spdy_test_utils.h" |
[email protected] | f21ec37 | 2014-07-02 07:15:12 | [diff] [blame] | 29 | #include "net/test/cert_test_util.h" |
[email protected] | 18ccfdb | 2013-08-15 00:13:44 | [diff] [blame] | 30 | #include "net/udp/datagram_client_socket.h" |
[email protected] | dd3fd0e | 2012-11-04 05:14:40 | [diff] [blame] | 31 | |
| 32 | using testing::_; |
| 33 | |
| 34 | namespace net { |
| 35 | namespace test { |
| 36 | namespace { |
| 37 | |
[email protected] | f21ec37 | 2014-07-02 07:15:12 | [diff] [blame] | 38 | const char kServerHostname[] = "www.example.org"; |
[email protected] | e4c3ea6 | 2014-03-15 00:45:14 | [diff] [blame] | 39 | const uint16 kServerPort = 80; |
[email protected] | 41d6b17 | 2013-01-29 16:10:57 | [diff] [blame] | 40 | |
[email protected] | 4d64079 | 2013-12-18 22:21:08 | [diff] [blame] | 41 | class QuicClientSessionTest : public ::testing::TestWithParam<QuicVersion> { |
[email protected] | dd3fd0e | 2012-11-04 05:14:40 | [diff] [blame] | 42 | protected: |
| 43 | QuicClientSessionTest() |
[email protected] | 6d51582 | 2014-08-22 01:58:06 | [diff] [blame] | 44 | : connection_( |
[email protected] | 5d45daa | 2014-01-02 21:07:46 | [diff] [blame] | 45 | new PacketSavingConnection(false, SupportedVersions(GetParam()))), |
[email protected] | 6d51582 | 2014-08-22 01:58:06 | [diff] [blame] | 46 | session_(connection_, GetSocket().Pass(), NULL, NULL, |
[email protected] | 5db45220 | 2014-08-19 05:22:15 | [diff] [blame] | 47 | &transport_security_state_, |
[email protected] | 3021a5f | 2014-07-23 01:40:40 | [diff] [blame] | 48 | make_scoped_ptr((QuicServerInfo*)NULL), |
| 49 | QuicServerId(kServerHostname, kServerPort, false, |
| 50 | PRIVACY_MODE_DISABLED), |
| 51 | DefaultQuicConfig(), &crypto_config_, |
[email protected] | 6576844 | 2014-06-06 23:37:03 | [diff] [blame] | 52 | base::MessageLoop::current()->message_loop_proxy().get(), |
| 53 | &net_log_) { |
[email protected] | 3021a5f | 2014-07-23 01:40:40 | [diff] [blame] | 54 | session_.InitializeSession(); |
[email protected] | 47a7154 | 2013-05-17 07:58:54 | [diff] [blame] | 55 | session_.config()->SetDefaults(); |
[email protected] | ef95114d | 2013-04-17 17:57:01 | [diff] [blame] | 56 | crypto_config_.SetDefaults(); |
[email protected] | dd3fd0e | 2012-11-04 05:14:40 | [diff] [blame] | 57 | } |
| 58 | |
[email protected] | 4d283b3 | 2013-10-17 12:57:27 | [diff] [blame] | 59 | virtual void TearDown() OVERRIDE { |
| 60 | session_.CloseSessionOnError(ERR_ABORTED); |
| 61 | } |
| 62 | |
| 63 | scoped_ptr<DatagramClientSocket> GetSocket() { |
| 64 | socket_factory_.AddSocketDataProvider(&socket_data_); |
| 65 | return socket_factory_.CreateDatagramClientSocket( |
| 66 | DatagramSocket::DEFAULT_BIND, base::Bind(&base::RandInt), |
| 67 | &net_log_, NetLog::Source()); |
| 68 | } |
| 69 | |
[email protected] | ed3fc15d | 2013-03-08 18:37:44 | [diff] [blame] | 70 | void CompleteCryptoHandshake() { |
| 71 | ASSERT_EQ(ERR_IO_PENDING, |
[email protected] | 11c0587 | 2013-08-20 02:04:12 | [diff] [blame] | 72 | session_.CryptoConnect(false, callback_.callback())); |
[email protected] | e8ff2684 | 2013-03-22 21:02:05 | [diff] [blame] | 73 | CryptoTestUtils::HandshakeWithFakeServer( |
| 74 | connection_, session_.GetCryptoStream()); |
[email protected] | ed3fc15d | 2013-03-08 18:37:44 | [diff] [blame] | 75 | ASSERT_EQ(OK, callback_.WaitForResult()); |
[email protected] | ed3fc15d | 2013-03-08 18:37:44 | [diff] [blame] | 76 | } |
| 77 | |
[email protected] | dd3fd0e | 2012-11-04 05:14:40 | [diff] [blame] | 78 | PacketSavingConnection* connection_; |
[email protected] | ed3fc15d | 2013-03-08 18:37:44 | [diff] [blame] | 79 | CapturingNetLog net_log_; |
[email protected] | 4d283b3 | 2013-10-17 12:57:27 | [diff] [blame] | 80 | MockClientSocketFactory socket_factory_; |
| 81 | StaticSocketDataProvider socket_data_; |
[email protected] | 5db45220 | 2014-08-19 05:22:15 | [diff] [blame] | 82 | TransportSecurityState transport_security_state_; |
[email protected] | dd3fd0e | 2012-11-04 05:14:40 | [diff] [blame] | 83 | QuicClientSession session_; |
[email protected] | ed3fc15d | 2013-03-08 18:37:44 | [diff] [blame] | 84 | MockClock clock_; |
| 85 | MockRandom random_; |
[email protected] | dd3fd0e | 2012-11-04 05:14:40 | [diff] [blame] | 86 | QuicConnectionVisitorInterface* visitor_; |
[email protected] | 8ee611b | 2012-11-20 01:48:12 | [diff] [blame] | 87 | TestCompletionCallback callback_; |
[email protected] | ef95114d | 2013-04-17 17:57:01 | [diff] [blame] | 88 | QuicCryptoClientConfig crypto_config_; |
[email protected] | dd3fd0e | 2012-11-04 05:14:40 | [diff] [blame] | 89 | }; |
| 90 | |
[email protected] | 4d64079 | 2013-12-18 22:21:08 | [diff] [blame] | 91 | INSTANTIATE_TEST_CASE_P(Tests, QuicClientSessionTest, |
| 92 | ::testing::ValuesIn(QuicSupportedVersions())); |
| 93 | |
| 94 | TEST_P(QuicClientSessionTest, CryptoConnect) { |
[email protected] | ed3fc15d | 2013-03-08 18:37:44 | [diff] [blame] | 95 | CompleteCryptoHandshake(); |
[email protected] | 8ee611b | 2012-11-20 01:48:12 | [diff] [blame] | 96 | } |
| 97 | |
[email protected] | 4d64079 | 2013-12-18 22:21:08 | [diff] [blame] | 98 | TEST_P(QuicClientSessionTest, MaxNumStreams) { |
[email protected] | ed3fc15d | 2013-03-08 18:37:44 | [diff] [blame] | 99 | CompleteCryptoHandshake(); |
[email protected] | dd3fd0e | 2012-11-04 05:14:40 | [diff] [blame] | 100 | |
| 101 | std::vector<QuicReliableClientStream*> streams; |
| 102 | for (size_t i = 0; i < kDefaultMaxStreamsPerConnection; i++) { |
[email protected] | 457d695 | 2013-12-13 09:24:58 | [diff] [blame] | 103 | QuicReliableClientStream* stream = session_.CreateOutgoingDataStream(); |
[email protected] | dd3fd0e | 2012-11-04 05:14:40 | [diff] [blame] | 104 | EXPECT_TRUE(stream); |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 105 | streams.push_back(stream); |
[email protected] | dd3fd0e | 2012-11-04 05:14:40 | [diff] [blame] | 106 | } |
[email protected] | 457d695 | 2013-12-13 09:24:58 | [diff] [blame] | 107 | EXPECT_FALSE(session_.CreateOutgoingDataStream()); |
[email protected] | dd3fd0e | 2012-11-04 05:14:40 | [diff] [blame] | 108 | |
| 109 | // Close a stream and ensure I can now open a new one. |
| 110 | session_.CloseStream(streams[0]->id()); |
[email protected] | 457d695 | 2013-12-13 09:24:58 | [diff] [blame] | 111 | EXPECT_TRUE(session_.CreateOutgoingDataStream()); |
[email protected] | dd3fd0e | 2012-11-04 05:14:40 | [diff] [blame] | 112 | } |
| 113 | |
[email protected] | 4d64079 | 2013-12-18 22:21:08 | [diff] [blame] | 114 | TEST_P(QuicClientSessionTest, MaxNumStreamsViaRequest) { |
[email protected] | 0b2294d3 | 2013-08-02 00:46:36 | [diff] [blame] | 115 | CompleteCryptoHandshake(); |
| 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] | 0b2294d3 | 2013-08-02 00:46:36 | [diff] [blame] | 120 | EXPECT_TRUE(stream); |
| 121 | streams.push_back(stream); |
| 122 | } |
| 123 | |
| 124 | QuicReliableClientStream* stream; |
| 125 | QuicClientSession::StreamRequest stream_request; |
| 126 | TestCompletionCallback callback; |
| 127 | ASSERT_EQ(ERR_IO_PENDING, |
| 128 | stream_request.StartRequest(session_.GetWeakPtr(), &stream, |
| 129 | callback.callback())); |
| 130 | |
| 131 | // Close a stream and ensure I can now open a new one. |
| 132 | session_.CloseStream(streams[0]->id()); |
| 133 | ASSERT_TRUE(callback.have_result()); |
| 134 | EXPECT_EQ(OK, callback.WaitForResult()); |
| 135 | EXPECT_TRUE(stream != NULL); |
| 136 | } |
| 137 | |
[email protected] | 4d64079 | 2013-12-18 22:21:08 | [diff] [blame] | 138 | TEST_P(QuicClientSessionTest, GoAwayReceived) { |
[email protected] | 8ba8121 | 2013-05-03 13:11:48 | [diff] [blame] | 139 | CompleteCryptoHandshake(); |
[email protected] | 9db44391 | 2013-02-25 05:27:03 | [diff] [blame] | 140 | |
| 141 | // After receiving a GoAway, I should no longer be able to create outgoing |
| 142 | // streams. |
| 143 | session_.OnGoAway(QuicGoAwayFrame(QUIC_PEER_GOING_AWAY, 1u, "Going away.")); |
[email protected] | 457d695 | 2013-12-13 09:24:58 | [diff] [blame] | 144 | EXPECT_EQ(NULL, session_.CreateOutgoingDataStream()); |
[email protected] | 9db44391 | 2013-02-25 05:27:03 | [diff] [blame] | 145 | } |
| 146 | |
[email protected] | 5db45220 | 2014-08-19 05:22:15 | [diff] [blame] | 147 | TEST_P(QuicClientSessionTest, CanPool) { |
[email protected] | f21ec37 | 2014-07-02 07:15:12 | [diff] [blame] | 148 | // Load a cert that is valid for: |
| 149 | // www.example.org |
| 150 | // mail.example.org |
| 151 | // www.example.com |
[email protected] | f21ec37 | 2014-07-02 07:15:12 | [diff] [blame] | 152 | |
[email protected] | f21ec37 | 2014-07-02 07:15:12 | [diff] [blame] | 153 | ProofVerifyDetailsChromium details; |
| 154 | details.cert_verify_result.verified_cert = |
[email protected] | 5db45220 | 2014-08-19 05:22:15 | [diff] [blame] | 155 | ImportCertFromFile(GetTestCertsDirectory(), "spdy_pooling.pem"); |
dcheng | 4227c6d | 2014-08-25 23:58:18 | [diff] [blame^] | 156 | ASSERT_TRUE(details.cert_verify_result.verified_cert.get()); |
[email protected] | f21ec37 | 2014-07-02 07:15:12 | [diff] [blame] | 157 | |
| 158 | session_.OnProofVerifyDetailsAvailable(details); |
| 159 | CompleteCryptoHandshake(); |
| 160 | |
| 161 | |
| 162 | EXPECT_TRUE(session_.CanPool("www.example.org")); |
| 163 | EXPECT_TRUE(session_.CanPool("mail.example.org")); |
| 164 | EXPECT_TRUE(session_.CanPool("mail.example.com")); |
| 165 | EXPECT_FALSE(session_.CanPool("mail.google.com")); |
| 166 | } |
| 167 | |
[email protected] | 5db45220 | 2014-08-19 05:22:15 | [diff] [blame] | 168 | TEST_P(QuicClientSessionTest, ConnectionPooledWithTlsChannelId) { |
[email protected] | f21ec37 | 2014-07-02 07:15:12 | [diff] [blame] | 169 | // Load a cert that is valid for: |
| 170 | // www.example.org |
| 171 | // mail.example.org |
| 172 | // www.example.com |
[email protected] | f21ec37 | 2014-07-02 07:15:12 | [diff] [blame] | 173 | |
[email protected] | f21ec37 | 2014-07-02 07:15:12 | [diff] [blame] | 174 | ProofVerifyDetailsChromium details; |
| 175 | details.cert_verify_result.verified_cert = |
[email protected] | 5db45220 | 2014-08-19 05:22:15 | [diff] [blame] | 176 | ImportCertFromFile(GetTestCertsDirectory(), "spdy_pooling.pem"); |
dcheng | 4227c6d | 2014-08-25 23:58:18 | [diff] [blame^] | 177 | ASSERT_TRUE(details.cert_verify_result.verified_cert.get()); |
[email protected] | f21ec37 | 2014-07-02 07:15:12 | [diff] [blame] | 178 | |
| 179 | session_.OnProofVerifyDetailsAvailable(details); |
| 180 | CompleteCryptoHandshake(); |
[email protected] | ed42a1e | 2014-07-16 23:17:47 | [diff] [blame] | 181 | QuicClientSessionPeer::SetChannelIDSent(&session_, true); |
[email protected] | f21ec37 | 2014-07-02 07:15:12 | [diff] [blame] | 182 | |
| 183 | EXPECT_TRUE(session_.CanPool("www.example.org")); |
| 184 | EXPECT_TRUE(session_.CanPool("mail.example.org")); |
| 185 | EXPECT_FALSE(session_.CanPool("mail.example.com")); |
| 186 | EXPECT_FALSE(session_.CanPool("mail.google.com")); |
| 187 | } |
| 188 | |
[email protected] | 5db45220 | 2014-08-19 05:22:15 | [diff] [blame] | 189 | TEST_P(QuicClientSessionTest, ConnectionNotPooledWithDifferentPin) { |
| 190 | uint8 primary_pin = 1; |
| 191 | uint8 backup_pin = 2; |
| 192 | uint8 bad_pin = 3; |
| 193 | AddPin(&transport_security_state_, "mail.example.org", primary_pin, |
| 194 | backup_pin); |
| 195 | |
| 196 | ProofVerifyDetailsChromium details; |
| 197 | details.cert_verify_result.verified_cert = |
| 198 | ImportCertFromFile(GetTestCertsDirectory(), "spdy_pooling.pem"); |
| 199 | details.cert_verify_result.is_issued_by_known_root = true; |
| 200 | details.cert_verify_result.public_key_hashes.push_back( |
| 201 | GetTestHashValue(bad_pin)); |
| 202 | |
dcheng | 4227c6d | 2014-08-25 23:58:18 | [diff] [blame^] | 203 | ASSERT_TRUE(details.cert_verify_result.verified_cert.get()); |
[email protected] | 5db45220 | 2014-08-19 05:22:15 | [diff] [blame] | 204 | |
| 205 | session_.OnProofVerifyDetailsAvailable(details); |
| 206 | CompleteCryptoHandshake(); |
| 207 | QuicClientSessionPeer::SetChannelIDSent(&session_, true); |
| 208 | |
| 209 | EXPECT_FALSE(session_.CanPool("mail.example.org")); |
| 210 | } |
| 211 | |
| 212 | TEST_P(QuicClientSessionTest, ConnectionPooledWithMatchingPin) { |
| 213 | uint8 primary_pin = 1; |
| 214 | uint8 backup_pin = 2; |
| 215 | AddPin(&transport_security_state_, "mail.example.org", primary_pin, |
| 216 | backup_pin); |
| 217 | |
| 218 | ProofVerifyDetailsChromium details; |
| 219 | details.cert_verify_result.verified_cert = |
| 220 | ImportCertFromFile(GetTestCertsDirectory(), "spdy_pooling.pem"); |
| 221 | details.cert_verify_result.is_issued_by_known_root = true; |
| 222 | details.cert_verify_result.public_key_hashes.push_back( |
| 223 | GetTestHashValue(primary_pin)); |
| 224 | |
dcheng | 4227c6d | 2014-08-25 23:58:18 | [diff] [blame^] | 225 | ASSERT_TRUE(details.cert_verify_result.verified_cert.get()); |
[email protected] | 5db45220 | 2014-08-19 05:22:15 | [diff] [blame] | 226 | |
| 227 | session_.OnProofVerifyDetailsAvailable(details); |
| 228 | CompleteCryptoHandshake(); |
| 229 | QuicClientSessionPeer::SetChannelIDSent(&session_, true); |
| 230 | |
| 231 | EXPECT_TRUE(session_.CanPool("mail.example.org")); |
| 232 | } |
| 233 | |
[email protected] | dd3fd0e | 2012-11-04 05:14:40 | [diff] [blame] | 234 | } // namespace |
| 235 | } // namespace test |
| 236 | } // namespace net |