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