blob: 2c000728668fb3aaed0e579d8a1fa0081c7e4e4a [file] [log] [blame]
[email protected]dd3fd0e2012-11-04 05:14:401// 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]5db452202014-08-19 05:22:159#include "base/base64.h"
[email protected]f21ec372014-07-02 07:15:1210#include "base/files/file_path.h"
[email protected]4d283b32013-10-17 12:57:2711#include "base/rand_util.h"
[email protected]0d10b592013-02-14 16:09:2612#include "net/base/capturing_net_log.h"
[email protected]8ee611b2012-11-20 01:48:1213#include "net/base/test_completion_callback.h"
[email protected]f21ec372014-07-02 07:15:1214#include "net/base/test_data_directory.h"
15#include "net/cert/cert_verify_result.h"
[email protected]5db452202014-08-19 05:22:1516#include "net/http/transport_security_state.h"
[email protected]0bbeb6972013-05-23 04:10:2117#include "net/quic/crypto/aes_128_gcm_12_encrypter.h"
[email protected]dd3fd0e2012-11-04 05:14:4018#include "net/quic/crypto/crypto_protocol.h"
[email protected]f21ec372014-07-02 07:15:1219#include "net/quic/crypto/proof_verifier_chromium.h"
[email protected]4df69842013-02-27 06:32:1620#include "net/quic/crypto/quic_decrypter.h"
21#include "net/quic/crypto/quic_encrypter.h"
[email protected]17bf15c2014-03-14 10:08:0422#include "net/quic/crypto/quic_server_info.h"
[email protected]ed3fc15d2013-03-08 18:37:4423#include "net/quic/test_tools/crypto_test_utils.h"
[email protected]899951652013-05-16 12:52:3924#include "net/quic/test_tools/quic_client_session_peer.h"
[email protected]dd3fd0e2012-11-04 05:14:4025#include "net/quic/test_tools/quic_test_utils.h"
[email protected]c58a83ec2014-04-20 22:21:5026#include "net/quic/test_tools/simple_quic_framer.h"
[email protected]4d283b32013-10-17 12:57:2727#include "net/socket/socket_test_util.h"
[email protected]5db452202014-08-19 05:22:1528#include "net/spdy/spdy_test_utils.h"
[email protected]f21ec372014-07-02 07:15:1229#include "net/test/cert_test_util.h"
[email protected]18ccfdb2013-08-15 00:13:4430#include "net/udp/datagram_client_socket.h"
[email protected]dd3fd0e2012-11-04 05:14:4031
32using testing::_;
33
34namespace net {
35namespace test {
36namespace {
37
[email protected]f21ec372014-07-02 07:15:1238const char kServerHostname[] = "www.example.org";
[email protected]e4c3ea62014-03-15 00:45:1439const uint16 kServerPort = 80;
[email protected]41d6b172013-01-29 16:10:5740
[email protected]4d640792013-12-18 22:21:0841class QuicClientSessionTest : public ::testing::TestWithParam<QuicVersion> {
[email protected]dd3fd0e2012-11-04 05:14:4042 protected:
43 QuicClientSessionTest()
[email protected]6d515822014-08-22 01:58:0644 : connection_(
[email protected]5d45daa2014-01-02 21:07:4645 new PacketSavingConnection(false, SupportedVersions(GetParam()))),
[email protected]6d515822014-08-22 01:58:0646 session_(connection_, GetSocket().Pass(), NULL, NULL,
[email protected]5db452202014-08-19 05:22:1547 &transport_security_state_,
[email protected]3021a5f2014-07-23 01:40:4048 make_scoped_ptr((QuicServerInfo*)NULL),
49 QuicServerId(kServerHostname, kServerPort, false,
50 PRIVACY_MODE_DISABLED),
51 DefaultQuicConfig(), &crypto_config_,
[email protected]65768442014-06-06 23:37:0352 base::MessageLoop::current()->message_loop_proxy().get(),
53 &net_log_) {
[email protected]3021a5f2014-07-23 01:40:4054 session_.InitializeSession();
[email protected]47a71542013-05-17 07:58:5455 session_.config()->SetDefaults();
[email protected]ef95114d2013-04-17 17:57:0156 crypto_config_.SetDefaults();
[email protected]dd3fd0e2012-11-04 05:14:4057 }
58
[email protected]4d283b32013-10-17 12:57:2759 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]ed3fc15d2013-03-08 18:37:4470 void CompleteCryptoHandshake() {
71 ASSERT_EQ(ERR_IO_PENDING,
[email protected]11c05872013-08-20 02:04:1272 session_.CryptoConnect(false, callback_.callback()));
[email protected]e8ff26842013-03-22 21:02:0573 CryptoTestUtils::HandshakeWithFakeServer(
74 connection_, session_.GetCryptoStream());
[email protected]ed3fc15d2013-03-08 18:37:4475 ASSERT_EQ(OK, callback_.WaitForResult());
[email protected]ed3fc15d2013-03-08 18:37:4476 }
77
[email protected]dd3fd0e2012-11-04 05:14:4078 PacketSavingConnection* connection_;
[email protected]ed3fc15d2013-03-08 18:37:4479 CapturingNetLog net_log_;
[email protected]4d283b32013-10-17 12:57:2780 MockClientSocketFactory socket_factory_;
81 StaticSocketDataProvider socket_data_;
[email protected]5db452202014-08-19 05:22:1582 TransportSecurityState transport_security_state_;
[email protected]dd3fd0e2012-11-04 05:14:4083 QuicClientSession session_;
[email protected]ed3fc15d2013-03-08 18:37:4484 MockClock clock_;
85 MockRandom random_;
[email protected]dd3fd0e2012-11-04 05:14:4086 QuicConnectionVisitorInterface* visitor_;
[email protected]8ee611b2012-11-20 01:48:1287 TestCompletionCallback callback_;
[email protected]ef95114d2013-04-17 17:57:0188 QuicCryptoClientConfig crypto_config_;
[email protected]dd3fd0e2012-11-04 05:14:4089};
90
[email protected]4d640792013-12-18 22:21:0891INSTANTIATE_TEST_CASE_P(Tests, QuicClientSessionTest,
92 ::testing::ValuesIn(QuicSupportedVersions()));
93
94TEST_P(QuicClientSessionTest, CryptoConnect) {
[email protected]ed3fc15d2013-03-08 18:37:4495 CompleteCryptoHandshake();
[email protected]8ee611b2012-11-20 01:48:1296}
97
[email protected]4d640792013-12-18 22:21:0898TEST_P(QuicClientSessionTest, MaxNumStreams) {
[email protected]ed3fc15d2013-03-08 18:37:4499 CompleteCryptoHandshake();
[email protected]dd3fd0e2012-11-04 05:14:40100
101 std::vector<QuicReliableClientStream*> streams;
102 for (size_t i = 0; i < kDefaultMaxStreamsPerConnection; i++) {
[email protected]457d6952013-12-13 09:24:58103 QuicReliableClientStream* stream = session_.CreateOutgoingDataStream();
[email protected]dd3fd0e2012-11-04 05:14:40104 EXPECT_TRUE(stream);
[email protected]f702d572012-12-04 15:56:20105 streams.push_back(stream);
[email protected]dd3fd0e2012-11-04 05:14:40106 }
[email protected]457d6952013-12-13 09:24:58107 EXPECT_FALSE(session_.CreateOutgoingDataStream());
[email protected]dd3fd0e2012-11-04 05:14:40108
109 // Close a stream and ensure I can now open a new one.
110 session_.CloseStream(streams[0]->id());
[email protected]457d6952013-12-13 09:24:58111 EXPECT_TRUE(session_.CreateOutgoingDataStream());
[email protected]dd3fd0e2012-11-04 05:14:40112}
113
[email protected]4d640792013-12-18 22:21:08114TEST_P(QuicClientSessionTest, MaxNumStreamsViaRequest) {
[email protected]0b2294d32013-08-02 00:46:36115 CompleteCryptoHandshake();
116
117 std::vector<QuicReliableClientStream*> streams;
118 for (size_t i = 0; i < kDefaultMaxStreamsPerConnection; i++) {
[email protected]457d6952013-12-13 09:24:58119 QuicReliableClientStream* stream = session_.CreateOutgoingDataStream();
[email protected]0b2294d32013-08-02 00:46:36120 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]4d640792013-12-18 22:21:08138TEST_P(QuicClientSessionTest, GoAwayReceived) {
[email protected]8ba81212013-05-03 13:11:48139 CompleteCryptoHandshake();
[email protected]9db443912013-02-25 05:27:03140
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]457d6952013-12-13 09:24:58144 EXPECT_EQ(NULL, session_.CreateOutgoingDataStream());
[email protected]9db443912013-02-25 05:27:03145}
146
[email protected]5db452202014-08-19 05:22:15147TEST_P(QuicClientSessionTest, CanPool) {
[email protected]f21ec372014-07-02 07:15:12148 // Load a cert that is valid for:
149 // www.example.org
150 // mail.example.org
151 // www.example.com
[email protected]f21ec372014-07-02 07:15:12152
[email protected]f21ec372014-07-02 07:15:12153 ProofVerifyDetailsChromium details;
154 details.cert_verify_result.verified_cert =
[email protected]5db452202014-08-19 05:22:15155 ImportCertFromFile(GetTestCertsDirectory(), "spdy_pooling.pem");
dcheng4227c6d2014-08-25 23:58:18156 ASSERT_TRUE(details.cert_verify_result.verified_cert.get());
[email protected]f21ec372014-07-02 07:15:12157
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]5db452202014-08-19 05:22:15168TEST_P(QuicClientSessionTest, ConnectionPooledWithTlsChannelId) {
[email protected]f21ec372014-07-02 07:15:12169 // Load a cert that is valid for:
170 // www.example.org
171 // mail.example.org
172 // www.example.com
[email protected]f21ec372014-07-02 07:15:12173
[email protected]f21ec372014-07-02 07:15:12174 ProofVerifyDetailsChromium details;
175 details.cert_verify_result.verified_cert =
[email protected]5db452202014-08-19 05:22:15176 ImportCertFromFile(GetTestCertsDirectory(), "spdy_pooling.pem");
dcheng4227c6d2014-08-25 23:58:18177 ASSERT_TRUE(details.cert_verify_result.verified_cert.get());
[email protected]f21ec372014-07-02 07:15:12178
179 session_.OnProofVerifyDetailsAvailable(details);
180 CompleteCryptoHandshake();
[email protected]ed42a1e2014-07-16 23:17:47181 QuicClientSessionPeer::SetChannelIDSent(&session_, true);
[email protected]f21ec372014-07-02 07:15:12182
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]5db452202014-08-19 05:22:15189TEST_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
dcheng4227c6d2014-08-25 23:58:18203 ASSERT_TRUE(details.cert_verify_result.verified_cert.get());
[email protected]5db452202014-08-19 05:22:15204
205 session_.OnProofVerifyDetailsAvailable(details);
206 CompleteCryptoHandshake();
207 QuicClientSessionPeer::SetChannelIDSent(&session_, true);
208
209 EXPECT_FALSE(session_.CanPool("mail.example.org"));
210}
211
212TEST_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
dcheng4227c6d2014-08-25 23:58:18225 ASSERT_TRUE(details.cert_verify_result.verified_cert.get());
[email protected]5db452202014-08-19 05:22:15226
227 session_.OnProofVerifyDetailsAvailable(details);
228 CompleteCryptoHandshake();
229 QuicClientSessionPeer::SetChannelIDSent(&session_, true);
230
231 EXPECT_TRUE(session_.CanPool("mail.example.org"));
232}
233
[email protected]dd3fd0e2012-11-04 05:14:40234} // namespace
235} // namespace test
236} // namespace net