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.
5 #include "net/quic/quic_client_session.h"
9 #include "base/base64.h"
10 #include "base/files/file_path.h"
11 #include "base/rand_util.h"
12 #include "net/base/capturing_net_log.h"
13 #include "net/base/test_completion_callback.h"
14 #include "net/base/test_data_directory.h"
15 #include "net/cert/cert_verify_result.h"
16 #include "net/http/transport_security_state.h"
17 #include "net/quic/crypto/aes_128_gcm_12_encrypter.h"
18 #include "net/quic/crypto/crypto_protocol.h"
19 #include "net/quic/crypto/proof_verifier_chromium.h"
20 #include "net/quic/crypto/quic_decrypter.h"
21 #include "net/quic/crypto/quic_encrypter.h"
22 #include "net/quic/crypto/quic_server_info.h"
23 #include "net/quic/test_tools/crypto_test_utils.h"
24 #include "net/quic/test_tools/quic_client_session_peer.h"
25 #include "net/quic/test_tools/quic_test_utils.h"
26 #include "net/quic/test_tools/simple_quic_framer.h"
27 #include "net/socket/socket_test_util.h"
28 #include "net/spdy/spdy_test_utils.h"
29 #include "net/test/cert_test_util.h"
30 #include "net/udp/datagram_client_socket.h"
38 const char kServerHostname
[] = "www.example.org";
39 const uint16 kServerPort
= 80;
41 class QuicClientSessionTest
: public ::testing::TestWithParam
<QuicVersion
> {
43 QuicClientSessionTest()
45 new PacketSavingConnection(false, SupportedVersions(GetParam()))),
49 &transport_security_state_
,
50 make_scoped_ptr((QuicServerInfo
*)nullptr),
53 base::TimeTicks::Now(),
54 base::MessageLoop::current()->message_loop_proxy().get(),
56 session_
.InitializeSession(QuicServerId(kServerHostname
, kServerPort
,
58 PRIVACY_MODE_DISABLED
),
59 &crypto_config_
, nullptr);
60 // Advance the time, because timers do not like uninitialized times.
61 connection_
->AdvanceTime(QuicTime::Delta::FromSeconds(1));
64 void TearDown() override
{ session_
.CloseSessionOnError(ERR_ABORTED
); }
66 scoped_ptr
<DatagramClientSocket
> GetSocket() {
67 socket_factory_
.AddSocketDataProvider(&socket_data_
);
68 return socket_factory_
.CreateDatagramClientSocket(
69 DatagramSocket::DEFAULT_BIND
, base::Bind(&base::RandInt
),
70 &net_log_
, NetLog::Source());
73 void CompleteCryptoHandshake() {
74 ASSERT_EQ(ERR_IO_PENDING
,
75 session_
.CryptoConnect(false, callback_
.callback()));
76 CryptoTestUtils::HandshakeWithFakeServer(
77 connection_
, session_
.GetCryptoStream());
78 ASSERT_EQ(OK
, callback_
.WaitForResult());
81 PacketSavingConnection
* connection_
;
82 CapturingNetLog net_log_
;
83 MockClientSocketFactory socket_factory_
;
84 StaticSocketDataProvider socket_data_
;
85 TransportSecurityState transport_security_state_
;
86 QuicClientSession session_
;
89 QuicConnectionVisitorInterface
* visitor_
;
90 TestCompletionCallback callback_
;
91 QuicCryptoClientConfig crypto_config_
;
94 INSTANTIATE_TEST_CASE_P(Tests
, QuicClientSessionTest
,
95 ::testing::ValuesIn(QuicSupportedVersions()));
97 TEST_P(QuicClientSessionTest
, CryptoConnect
) {
98 CompleteCryptoHandshake();
101 TEST_P(QuicClientSessionTest
, MaxNumStreams
) {
102 CompleteCryptoHandshake();
104 std::vector
<QuicReliableClientStream
*> streams
;
105 for (size_t i
= 0; i
< kDefaultMaxStreamsPerConnection
; i
++) {
106 QuicReliableClientStream
* stream
= session_
.CreateOutgoingDataStream();
108 streams
.push_back(stream
);
110 EXPECT_FALSE(session_
.CreateOutgoingDataStream());
112 // Close a stream and ensure I can now open a new one.
113 session_
.CloseStream(streams
[0]->id());
114 EXPECT_TRUE(session_
.CreateOutgoingDataStream());
117 TEST_P(QuicClientSessionTest
, MaxNumStreamsViaRequest
) {
118 CompleteCryptoHandshake();
120 std::vector
<QuicReliableClientStream
*> streams
;
121 for (size_t i
= 0; i
< kDefaultMaxStreamsPerConnection
; i
++) {
122 QuicReliableClientStream
* stream
= session_
.CreateOutgoingDataStream();
124 streams
.push_back(stream
);
127 QuicReliableClientStream
* stream
;
128 QuicClientSession::StreamRequest stream_request
;
129 TestCompletionCallback callback
;
130 ASSERT_EQ(ERR_IO_PENDING
,
131 stream_request
.StartRequest(session_
.GetWeakPtr(), &stream
,
132 callback
.callback()));
134 // Close a stream and ensure I can now open a new one.
135 session_
.CloseStream(streams
[0]->id());
136 ASSERT_TRUE(callback
.have_result());
137 EXPECT_EQ(OK
, callback
.WaitForResult());
138 EXPECT_TRUE(stream
!= nullptr);
141 TEST_P(QuicClientSessionTest
, GoAwayReceived
) {
142 CompleteCryptoHandshake();
144 // After receiving a GoAway, I should no longer be able to create outgoing
146 session_
.OnGoAway(QuicGoAwayFrame(QUIC_PEER_GOING_AWAY
, 1u, "Going away."));
147 EXPECT_EQ(nullptr, session_
.CreateOutgoingDataStream());
150 TEST_P(QuicClientSessionTest
, CanPool
) {
151 // Load a cert that is valid for:
156 ProofVerifyDetailsChromium details
;
157 details
.cert_verify_result
.verified_cert
=
158 ImportCertFromFile(GetTestCertsDirectory(), "spdy_pooling.pem");
159 ASSERT_TRUE(details
.cert_verify_result
.verified_cert
.get());
161 session_
.OnProofVerifyDetailsAvailable(details
);
162 CompleteCryptoHandshake();
164 EXPECT_TRUE(session_
.CanPool("www.example.org", PRIVACY_MODE_DISABLED
));
165 EXPECT_FALSE(session_
.CanPool("www.example.org", PRIVACY_MODE_ENABLED
));
166 EXPECT_TRUE(session_
.CanPool("mail.example.org", PRIVACY_MODE_DISABLED
));
167 EXPECT_TRUE(session_
.CanPool("mail.example.com", PRIVACY_MODE_DISABLED
));
168 EXPECT_FALSE(session_
.CanPool("mail.google.com", PRIVACY_MODE_DISABLED
));
171 TEST_P(QuicClientSessionTest
, ConnectionPooledWithTlsChannelId
) {
172 // Load a cert that is valid for:
177 ProofVerifyDetailsChromium details
;
178 details
.cert_verify_result
.verified_cert
=
179 ImportCertFromFile(GetTestCertsDirectory(), "spdy_pooling.pem");
180 ASSERT_TRUE(details
.cert_verify_result
.verified_cert
.get());
182 session_
.OnProofVerifyDetailsAvailable(details
);
183 CompleteCryptoHandshake();
184 QuicClientSessionPeer::SetChannelIDSent(&session_
, true);
186 EXPECT_TRUE(session_
.CanPool("www.example.org", PRIVACY_MODE_DISABLED
));
187 EXPECT_TRUE(session_
.CanPool("mail.example.org", PRIVACY_MODE_DISABLED
));
188 EXPECT_FALSE(session_
.CanPool("mail.example.com", PRIVACY_MODE_DISABLED
));
189 EXPECT_FALSE(session_
.CanPool("mail.google.com", PRIVACY_MODE_DISABLED
));
192 TEST_P(QuicClientSessionTest
, ConnectionNotPooledWithDifferentPin
) {
193 uint8 primary_pin
= 1;
194 uint8 backup_pin
= 2;
196 AddPin(&transport_security_state_
, "mail.example.org", primary_pin
,
199 ProofVerifyDetailsChromium details
;
200 details
.cert_verify_result
.verified_cert
=
201 ImportCertFromFile(GetTestCertsDirectory(), "spdy_pooling.pem");
202 details
.cert_verify_result
.is_issued_by_known_root
= true;
203 details
.cert_verify_result
.public_key_hashes
.push_back(
204 GetTestHashValue(bad_pin
));
206 ASSERT_TRUE(details
.cert_verify_result
.verified_cert
.get());
208 session_
.OnProofVerifyDetailsAvailable(details
);
209 CompleteCryptoHandshake();
210 QuicClientSessionPeer::SetChannelIDSent(&session_
, true);
212 EXPECT_FALSE(session_
.CanPool("mail.example.org", PRIVACY_MODE_DISABLED
));
215 TEST_P(QuicClientSessionTest
, ConnectionPooledWithMatchingPin
) {
216 uint8 primary_pin
= 1;
217 uint8 backup_pin
= 2;
218 AddPin(&transport_security_state_
, "mail.example.org", primary_pin
,
221 ProofVerifyDetailsChromium details
;
222 details
.cert_verify_result
.verified_cert
=
223 ImportCertFromFile(GetTestCertsDirectory(), "spdy_pooling.pem");
224 details
.cert_verify_result
.is_issued_by_known_root
= true;
225 details
.cert_verify_result
.public_key_hashes
.push_back(
226 GetTestHashValue(primary_pin
));
228 ASSERT_TRUE(details
.cert_verify_result
.verified_cert
.get());
230 session_
.OnProofVerifyDetailsAvailable(details
);
231 CompleteCryptoHandshake();
232 QuicClientSessionPeer::SetChannelIDSent(&session_
, true);
234 EXPECT_TRUE(session_
.CanPool("mail.example.org", PRIVACY_MODE_DISABLED
));