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 "base/thread_task_runner_handle.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/log/test_net_log.h"
18 #include "net/quic/crypto/aes_128_gcm_12_encrypter.h"
19 #include "net/quic/crypto/crypto_protocol.h"
20 #include "net/quic/crypto/proof_verifier_chromium.h"
21 #include "net/quic/crypto/quic_decrypter.h"
22 #include "net/quic/crypto/quic_encrypter.h"
23 #include "net/quic/crypto/quic_server_info.h"
24 #include "net/quic/test_tools/crypto_test_utils.h"
25 #include "net/quic/test_tools/quic_client_session_peer.h"
26 #include "net/quic/test_tools/quic_spdy_session_peer.h"
27 #include "net/quic/test_tools/quic_test_utils.h"
28 #include "net/quic/test_tools/simple_quic_framer.h"
29 #include "net/socket/socket_test_util.h"
30 #include "net/spdy/spdy_test_utils.h"
31 #include "net/test/cert_test_util.h"
32 #include "net/udp/datagram_client_socket.h"
40 const char kServerHostname
[] = "www.example.org";
41 const uint16 kServerPort
= 80;
43 class QuicClientSessionTest
: public ::testing::TestWithParam
<QuicVersion
> {
45 QuicClientSessionTest()
46 : connection_(new PacketSavingConnection(Perspective::IS_CLIENT
,
47 SupportedVersions(GetParam()))),
50 /*stream_factory=*/nullptr,
51 /*crypto_client_stream_factory=*/nullptr,
52 &transport_security_state_
,
53 make_scoped_ptr((QuicServerInfo
*)nullptr),
54 QuicServerId(kServerHostname
,
57 PRIVACY_MODE_DISABLED
),
58 /*cert_verify_flags=*/0,
62 base::TimeTicks::Now(),
63 base::ThreadTaskRunnerHandle::Get().get(),
65 session_
.Initialize();
66 // Advance the time, because timers do not like uninitialized times.
67 connection_
->AdvanceTime(QuicTime::Delta::FromSeconds(1));
70 void TearDown() override
{
71 session_
.CloseSessionOnError(ERR_ABORTED
, QUIC_INTERNAL_ERROR
);
74 scoped_ptr
<DatagramClientSocket
> GetSocket() {
75 socket_factory_
.AddSocketDataProvider(&socket_data_
);
76 return socket_factory_
.CreateDatagramClientSocket(
77 DatagramSocket::DEFAULT_BIND
, base::Bind(&base::RandInt
),
78 &net_log_
, NetLog::Source());
81 void CompleteCryptoHandshake() {
82 ASSERT_EQ(ERR_IO_PENDING
,
83 session_
.CryptoConnect(false, callback_
.callback()));
84 CryptoTestUtils::HandshakeWithFakeServer(
85 connection_
, session_
.GetCryptoStream());
86 ASSERT_EQ(OK
, callback_
.WaitForResult());
89 PacketSavingConnection
* connection_
;
91 MockClientSocketFactory socket_factory_
;
92 StaticSocketDataProvider socket_data_
;
93 TransportSecurityState transport_security_state_
;
94 QuicClientSession session_
;
97 QuicConnectionVisitorInterface
* visitor_
;
98 TestCompletionCallback callback_
;
99 QuicCryptoClientConfig crypto_config_
;
102 INSTANTIATE_TEST_CASE_P(Tests
, QuicClientSessionTest
,
103 ::testing::ValuesIn(QuicSupportedVersions()));
105 TEST_P(QuicClientSessionTest
, CryptoConnect
) {
106 CompleteCryptoHandshake();
109 TEST_P(QuicClientSessionTest
, MaxNumStreams
) {
110 CompleteCryptoHandshake();
112 std::vector
<QuicReliableClientStream
*> streams
;
113 for (size_t i
= 0; i
< kDefaultMaxStreamsPerConnection
; i
++) {
114 QuicReliableClientStream
* stream
= session_
.CreateOutgoingDynamicStream();
116 streams
.push_back(stream
);
118 EXPECT_FALSE(session_
.CreateOutgoingDynamicStream());
120 // Close a stream and ensure I can now open a new one.
121 session_
.CloseStream(streams
[0]->id());
122 EXPECT_TRUE(session_
.CreateOutgoingDynamicStream());
125 TEST_P(QuicClientSessionTest
, MaxNumStreamsViaRequest
) {
126 CompleteCryptoHandshake();
128 std::vector
<QuicReliableClientStream
*> streams
;
129 for (size_t i
= 0; i
< kDefaultMaxStreamsPerConnection
; i
++) {
130 QuicReliableClientStream
* stream
= session_
.CreateOutgoingDynamicStream();
132 streams
.push_back(stream
);
135 QuicReliableClientStream
* stream
;
136 QuicClientSession::StreamRequest stream_request
;
137 TestCompletionCallback callback
;
138 ASSERT_EQ(ERR_IO_PENDING
,
139 stream_request
.StartRequest(session_
.GetWeakPtr(), &stream
,
140 callback
.callback()));
142 // Close a stream and ensure I can now open a new one.
143 session_
.CloseStream(streams
[0]->id());
144 ASSERT_TRUE(callback
.have_result());
145 EXPECT_EQ(OK
, callback
.WaitForResult());
146 EXPECT_TRUE(stream
!= nullptr);
149 TEST_P(QuicClientSessionTest
, GoAwayReceived
) {
150 CompleteCryptoHandshake();
152 // After receiving a GoAway, I should no longer be able to create outgoing
154 session_
.OnGoAway(QuicGoAwayFrame(QUIC_PEER_GOING_AWAY
, 1u, "Going away."));
155 EXPECT_EQ(nullptr, session_
.CreateOutgoingDynamicStream());
158 TEST_P(QuicClientSessionTest
, CanPool
) {
159 // Load a cert that is valid for:
164 ProofVerifyDetailsChromium details
;
165 details
.cert_verify_result
.verified_cert
=
166 ImportCertFromFile(GetTestCertsDirectory(), "spdy_pooling.pem");
167 ASSERT_TRUE(details
.cert_verify_result
.verified_cert
.get());
169 session_
.OnProofVerifyDetailsAvailable(details
);
170 CompleteCryptoHandshake();
172 EXPECT_TRUE(session_
.CanPool("www.example.org", PRIVACY_MODE_DISABLED
));
173 EXPECT_FALSE(session_
.CanPool("www.example.org", PRIVACY_MODE_ENABLED
));
174 EXPECT_TRUE(session_
.CanPool("mail.example.org", PRIVACY_MODE_DISABLED
));
175 EXPECT_TRUE(session_
.CanPool("mail.example.com", PRIVACY_MODE_DISABLED
));
176 EXPECT_FALSE(session_
.CanPool("mail.google.com", PRIVACY_MODE_DISABLED
));
179 TEST_P(QuicClientSessionTest
, ConnectionPooledWithTlsChannelId
) {
180 // Load a cert that is valid for:
185 ProofVerifyDetailsChromium details
;
186 details
.cert_verify_result
.verified_cert
=
187 ImportCertFromFile(GetTestCertsDirectory(), "spdy_pooling.pem");
188 ASSERT_TRUE(details
.cert_verify_result
.verified_cert
.get());
190 session_
.OnProofVerifyDetailsAvailable(details
);
191 CompleteCryptoHandshake();
192 QuicClientSessionPeer::SetChannelIDSent(&session_
, true);
194 EXPECT_TRUE(session_
.CanPool("www.example.org", PRIVACY_MODE_DISABLED
));
195 EXPECT_TRUE(session_
.CanPool("mail.example.org", PRIVACY_MODE_DISABLED
));
196 EXPECT_FALSE(session_
.CanPool("mail.example.com", PRIVACY_MODE_DISABLED
));
197 EXPECT_FALSE(session_
.CanPool("mail.google.com", PRIVACY_MODE_DISABLED
));
200 TEST_P(QuicClientSessionTest
, ConnectionNotPooledWithDifferentPin
) {
201 uint8 primary_pin
= 1;
202 uint8 backup_pin
= 2;
204 AddPin(&transport_security_state_
, "mail.example.org", primary_pin
,
207 ProofVerifyDetailsChromium details
;
208 details
.cert_verify_result
.verified_cert
=
209 ImportCertFromFile(GetTestCertsDirectory(), "spdy_pooling.pem");
210 details
.cert_verify_result
.is_issued_by_known_root
= true;
211 details
.cert_verify_result
.public_key_hashes
.push_back(
212 GetTestHashValue(bad_pin
));
214 ASSERT_TRUE(details
.cert_verify_result
.verified_cert
.get());
216 session_
.OnProofVerifyDetailsAvailable(details
);
217 CompleteCryptoHandshake();
218 QuicClientSessionPeer::SetChannelIDSent(&session_
, true);
220 EXPECT_FALSE(session_
.CanPool("mail.example.org", PRIVACY_MODE_DISABLED
));
223 TEST_P(QuicClientSessionTest
, ConnectionPooledWithMatchingPin
) {
224 uint8 primary_pin
= 1;
225 uint8 backup_pin
= 2;
226 AddPin(&transport_security_state_
, "mail.example.org", primary_pin
,
229 ProofVerifyDetailsChromium details
;
230 details
.cert_verify_result
.verified_cert
=
231 ImportCertFromFile(GetTestCertsDirectory(), "spdy_pooling.pem");
232 details
.cert_verify_result
.is_issued_by_known_root
= true;
233 details
.cert_verify_result
.public_key_hashes
.push_back(
234 GetTestHashValue(primary_pin
));
236 ASSERT_TRUE(details
.cert_verify_result
.verified_cert
.get());
238 session_
.OnProofVerifyDetailsAvailable(details
);
239 CompleteCryptoHandshake();
240 QuicClientSessionPeer::SetChannelIDSent(&session_
, true);
242 EXPECT_TRUE(session_
.CanPool("mail.example.org", PRIVACY_MODE_DISABLED
));