Roll src/third_party/WebKit d9c6159:8139f33 (svn 201974:201975)
[chromium-blink-merge.git] / net / quic / quic_chromium_client_session_test.cc
blob91d1d1ab35d0e57891804400660c3b0aae1c0cf7
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_chromium_client_session.h"
7 #include <vector>
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_chromium_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"
34 using testing::_;
36 namespace net {
37 namespace test {
38 namespace {
40 const char kServerHostname[] = "www.example.org";
41 const uint16 kServerPort = 80;
43 class QuicChromiumClientSessionTest
44 : public ::testing::TestWithParam<QuicVersion> {
45 protected:
46 QuicChromiumClientSessionTest()
47 : connection_(new PacketSavingConnection(Perspective::IS_CLIENT,
48 SupportedVersions(GetParam()))),
49 session_(connection_,
50 GetSocket().Pass(),
51 /*stream_factory=*/nullptr,
52 /*crypto_client_stream_factory=*/nullptr,
53 &transport_security_state_,
54 make_scoped_ptr((QuicServerInfo*)nullptr),
55 QuicServerId(kServerHostname,
56 kServerPort,
57 /*is_secure=*/false,
58 PRIVACY_MODE_DISABLED),
59 /*cert_verify_flags=*/0,
60 DefaultQuicConfig(),
61 &crypto_config_,
62 "CONNECTION_UNKNOWN",
63 base::TimeTicks::Now(),
64 base::ThreadTaskRunnerHandle::Get().get(),
65 &net_log_) {
66 session_.Initialize();
67 // Advance the time, because timers do not like uninitialized times.
68 connection_->AdvanceTime(QuicTime::Delta::FromSeconds(1));
71 void TearDown() override {
72 session_.CloseSessionOnError(ERR_ABORTED, QUIC_INTERNAL_ERROR);
75 scoped_ptr<DatagramClientSocket> GetSocket() {
76 socket_factory_.AddSocketDataProvider(&socket_data_);
77 return socket_factory_.CreateDatagramClientSocket(
78 DatagramSocket::DEFAULT_BIND, base::Bind(&base::RandInt), &net_log_,
79 NetLog::Source());
82 void CompleteCryptoHandshake() {
83 ASSERT_EQ(ERR_IO_PENDING,
84 session_.CryptoConnect(false, callback_.callback()));
85 CryptoTestUtils::HandshakeWithFakeServer(connection_,
86 session_.GetCryptoStream());
87 ASSERT_EQ(OK, callback_.WaitForResult());
90 PacketSavingConnection* connection_;
91 TestNetLog net_log_;
92 MockClientSocketFactory socket_factory_;
93 StaticSocketDataProvider socket_data_;
94 TransportSecurityState transport_security_state_;
95 QuicChromiumClientSession session_;
96 MockClock clock_;
97 MockRandom random_;
98 QuicConnectionVisitorInterface* visitor_;
99 TestCompletionCallback callback_;
100 QuicCryptoClientConfig crypto_config_;
103 INSTANTIATE_TEST_CASE_P(Tests,
104 QuicChromiumClientSessionTest,
105 ::testing::ValuesIn(QuicSupportedVersions()));
107 TEST_P(QuicChromiumClientSessionTest, CryptoConnect) {
108 CompleteCryptoHandshake();
111 TEST_P(QuicChromiumClientSessionTest, MaxNumStreams) {
112 CompleteCryptoHandshake();
114 std::vector<QuicReliableClientStream*> streams;
115 for (size_t i = 0; i < kDefaultMaxStreamsPerConnection; i++) {
116 QuicReliableClientStream* stream = session_.CreateOutgoingDynamicStream();
117 EXPECT_TRUE(stream);
118 streams.push_back(stream);
120 EXPECT_FALSE(session_.CreateOutgoingDynamicStream());
122 // Close a stream and ensure I can now open a new one.
123 session_.CloseStream(streams[0]->id());
124 EXPECT_TRUE(session_.CreateOutgoingDynamicStream());
127 TEST_P(QuicChromiumClientSessionTest, MaxNumStreamsViaRequest) {
128 CompleteCryptoHandshake();
130 std::vector<QuicReliableClientStream*> streams;
131 for (size_t i = 0; i < kDefaultMaxStreamsPerConnection; i++) {
132 QuicReliableClientStream* stream = session_.CreateOutgoingDynamicStream();
133 EXPECT_TRUE(stream);
134 streams.push_back(stream);
137 QuicReliableClientStream* stream;
138 QuicChromiumClientSession::StreamRequest stream_request;
139 TestCompletionCallback callback;
140 ASSERT_EQ(ERR_IO_PENDING,
141 stream_request.StartRequest(session_.GetWeakPtr(), &stream,
142 callback.callback()));
144 // Close a stream and ensure I can now open a new one.
145 session_.CloseStream(streams[0]->id());
146 ASSERT_TRUE(callback.have_result());
147 EXPECT_EQ(OK, callback.WaitForResult());
148 EXPECT_TRUE(stream != nullptr);
151 TEST_P(QuicChromiumClientSessionTest, GoAwayReceived) {
152 CompleteCryptoHandshake();
154 // After receiving a GoAway, I should no longer be able to create outgoing
155 // streams.
156 session_.connection()->OnGoAwayFrame(
157 QuicGoAwayFrame(QUIC_PEER_GOING_AWAY, 1u, "Going away."));
158 EXPECT_EQ(nullptr, session_.CreateOutgoingDynamicStream());
161 TEST_P(QuicChromiumClientSessionTest, CanPool) {
162 // Load a cert that is valid for:
163 // www.example.org
164 // mail.example.org
165 // www.example.com
167 ProofVerifyDetailsChromium details;
168 details.cert_verify_result.verified_cert =
169 ImportCertFromFile(GetTestCertsDirectory(), "spdy_pooling.pem");
170 ASSERT_TRUE(details.cert_verify_result.verified_cert.get());
172 session_.OnProofVerifyDetailsAvailable(details);
173 CompleteCryptoHandshake();
175 EXPECT_TRUE(session_.CanPool("www.example.org", PRIVACY_MODE_DISABLED));
176 EXPECT_FALSE(session_.CanPool("www.example.org", PRIVACY_MODE_ENABLED));
177 EXPECT_TRUE(session_.CanPool("mail.example.org", PRIVACY_MODE_DISABLED));
178 EXPECT_TRUE(session_.CanPool("mail.example.com", PRIVACY_MODE_DISABLED));
179 EXPECT_FALSE(session_.CanPool("mail.google.com", PRIVACY_MODE_DISABLED));
182 TEST_P(QuicChromiumClientSessionTest, ConnectionPooledWithTlsChannelId) {
183 // Load a cert that is valid for:
184 // www.example.org
185 // mail.example.org
186 // www.example.com
188 ProofVerifyDetailsChromium details;
189 details.cert_verify_result.verified_cert =
190 ImportCertFromFile(GetTestCertsDirectory(), "spdy_pooling.pem");
191 ASSERT_TRUE(details.cert_verify_result.verified_cert.get());
193 session_.OnProofVerifyDetailsAvailable(details);
194 CompleteCryptoHandshake();
195 QuicChromiumClientSessionPeer::SetChannelIDSent(&session_, true);
197 EXPECT_TRUE(session_.CanPool("www.example.org", PRIVACY_MODE_DISABLED));
198 EXPECT_TRUE(session_.CanPool("mail.example.org", PRIVACY_MODE_DISABLED));
199 EXPECT_FALSE(session_.CanPool("mail.example.com", PRIVACY_MODE_DISABLED));
200 EXPECT_FALSE(session_.CanPool("mail.google.com", PRIVACY_MODE_DISABLED));
203 TEST_P(QuicChromiumClientSessionTest, ConnectionNotPooledWithDifferentPin) {
204 uint8 primary_pin = 1;
205 uint8 backup_pin = 2;
206 uint8 bad_pin = 3;
207 AddPin(&transport_security_state_, "mail.example.org", primary_pin,
208 backup_pin);
210 ProofVerifyDetailsChromium details;
211 details.cert_verify_result.verified_cert =
212 ImportCertFromFile(GetTestCertsDirectory(), "spdy_pooling.pem");
213 details.cert_verify_result.is_issued_by_known_root = true;
214 details.cert_verify_result.public_key_hashes.push_back(
215 GetTestHashValue(bad_pin));
217 ASSERT_TRUE(details.cert_verify_result.verified_cert.get());
219 session_.OnProofVerifyDetailsAvailable(details);
220 CompleteCryptoHandshake();
221 QuicChromiumClientSessionPeer::SetChannelIDSent(&session_, true);
223 EXPECT_FALSE(session_.CanPool("mail.example.org", PRIVACY_MODE_DISABLED));
226 TEST_P(QuicChromiumClientSessionTest, ConnectionPooledWithMatchingPin) {
227 uint8 primary_pin = 1;
228 uint8 backup_pin = 2;
229 AddPin(&transport_security_state_, "mail.example.org", primary_pin,
230 backup_pin);
232 ProofVerifyDetailsChromium details;
233 details.cert_verify_result.verified_cert =
234 ImportCertFromFile(GetTestCertsDirectory(), "spdy_pooling.pem");
235 details.cert_verify_result.is_issued_by_known_root = true;
236 details.cert_verify_result.public_key_hashes.push_back(
237 GetTestHashValue(primary_pin));
239 ASSERT_TRUE(details.cert_verify_result.verified_cert.get());
241 session_.OnProofVerifyDetailsAvailable(details);
242 CompleteCryptoHandshake();
243 QuicChromiumClientSessionPeer::SetChannelIDSent(&session_, true);
245 EXPECT_TRUE(session_.CanPool("mail.example.org", PRIVACY_MODE_DISABLED));
248 } // namespace
249 } // namespace test
250 } // namespace net