Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / net / quic / quic_chromium_client_session_test.cc
blob959776b864eaeb189249afe77b01b58efcbba81a
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_.OnGoAway(QuicGoAwayFrame(QUIC_PEER_GOING_AWAY, 1u, "Going away."));
157 EXPECT_EQ(nullptr, session_.CreateOutgoingDynamicStream());
160 TEST_P(QuicChromiumClientSessionTest, CanPool) {
161 // Load a cert that is valid for:
162 // www.example.org
163 // mail.example.org
164 // www.example.com
166 ProofVerifyDetailsChromium details;
167 details.cert_verify_result.verified_cert =
168 ImportCertFromFile(GetTestCertsDirectory(), "spdy_pooling.pem");
169 ASSERT_TRUE(details.cert_verify_result.verified_cert.get());
171 session_.OnProofVerifyDetailsAvailable(details);
172 CompleteCryptoHandshake();
174 EXPECT_TRUE(session_.CanPool("www.example.org", PRIVACY_MODE_DISABLED));
175 EXPECT_FALSE(session_.CanPool("www.example.org", PRIVACY_MODE_ENABLED));
176 EXPECT_TRUE(session_.CanPool("mail.example.org", PRIVACY_MODE_DISABLED));
177 EXPECT_TRUE(session_.CanPool("mail.example.com", PRIVACY_MODE_DISABLED));
178 EXPECT_FALSE(session_.CanPool("mail.google.com", PRIVACY_MODE_DISABLED));
181 TEST_P(QuicChromiumClientSessionTest, ConnectionPooledWithTlsChannelId) {
182 // Load a cert that is valid for:
183 // www.example.org
184 // mail.example.org
185 // www.example.com
187 ProofVerifyDetailsChromium details;
188 details.cert_verify_result.verified_cert =
189 ImportCertFromFile(GetTestCertsDirectory(), "spdy_pooling.pem");
190 ASSERT_TRUE(details.cert_verify_result.verified_cert.get());
192 session_.OnProofVerifyDetailsAvailable(details);
193 CompleteCryptoHandshake();
194 QuicChromiumClientSessionPeer::SetChannelIDSent(&session_, true);
196 EXPECT_TRUE(session_.CanPool("www.example.org", PRIVACY_MODE_DISABLED));
197 EXPECT_TRUE(session_.CanPool("mail.example.org", PRIVACY_MODE_DISABLED));
198 EXPECT_FALSE(session_.CanPool("mail.example.com", PRIVACY_MODE_DISABLED));
199 EXPECT_FALSE(session_.CanPool("mail.google.com", PRIVACY_MODE_DISABLED));
202 TEST_P(QuicChromiumClientSessionTest, ConnectionNotPooledWithDifferentPin) {
203 uint8 primary_pin = 1;
204 uint8 backup_pin = 2;
205 uint8 bad_pin = 3;
206 AddPin(&transport_security_state_, "mail.example.org", primary_pin,
207 backup_pin);
209 ProofVerifyDetailsChromium details;
210 details.cert_verify_result.verified_cert =
211 ImportCertFromFile(GetTestCertsDirectory(), "spdy_pooling.pem");
212 details.cert_verify_result.is_issued_by_known_root = true;
213 details.cert_verify_result.public_key_hashes.push_back(
214 GetTestHashValue(bad_pin));
216 ASSERT_TRUE(details.cert_verify_result.verified_cert.get());
218 session_.OnProofVerifyDetailsAvailable(details);
219 CompleteCryptoHandshake();
220 QuicChromiumClientSessionPeer::SetChannelIDSent(&session_, true);
222 EXPECT_FALSE(session_.CanPool("mail.example.org", PRIVACY_MODE_DISABLED));
225 TEST_P(QuicChromiumClientSessionTest, ConnectionPooledWithMatchingPin) {
226 uint8 primary_pin = 1;
227 uint8 backup_pin = 2;
228 AddPin(&transport_security_state_, "mail.example.org", primary_pin,
229 backup_pin);
231 ProofVerifyDetailsChromium details;
232 details.cert_verify_result.verified_cert =
233 ImportCertFromFile(GetTestCertsDirectory(), "spdy_pooling.pem");
234 details.cert_verify_result.is_issued_by_known_root = true;
235 details.cert_verify_result.public_key_hashes.push_back(
236 GetTestHashValue(primary_pin));
238 ASSERT_TRUE(details.cert_verify_result.verified_cert.get());
240 session_.OnProofVerifyDetailsAvailable(details);
241 CompleteCryptoHandshake();
242 QuicChromiumClientSessionPeer::SetChannelIDSent(&session_, true);
244 EXPECT_TRUE(session_.CanPool("mail.example.org", PRIVACY_MODE_DISABLED));
247 } // namespace
248 } // namespace test
249 } // namespace net