OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "net/quic/test_tools/crypto_test_utils.h" | 5 #include "net/quic/test_tools/crypto_test_utils.h" |
6 | 6 |
7 #include "net/quic/crypto/channel_id.h" | 7 #include "net/quic/crypto/channel_id.h" |
8 #include "net/quic/crypto/common_cert_set.h" | 8 #include "net/quic/crypto/common_cert_set.h" |
9 #include "net/quic/crypto/crypto_handshake.h" | 9 #include "net/quic/crypto/crypto_handshake.h" |
10 #include "net/quic/crypto/quic_crypto_server_config.h" | 10 #include "net/quic/crypto/quic_crypto_server_config.h" |
(...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
123 return true; | 123 return true; |
124 } | 124 } |
125 if (c >= 'A' && c <= 'F') { | 125 if (c >= 'A' && c <= 'F') { |
126 *value = c - 'A' + 10; | 126 *value = c - 'A' + 10; |
127 return true; | 127 return true; |
128 } | 128 } |
129 return false; | 129 return false; |
130 } | 130 } |
131 | 131 |
132 // A ChannelIDSource that works in asynchronous mode unless the |callback| | 132 // A ChannelIDSource that works in asynchronous mode unless the |callback| |
133 // argument to GetChannelIDKey is NULL. | 133 // argument to GetChannelIDKey is nullptr. |
134 class AsyncTestChannelIDSource : public ChannelIDSource, | 134 class AsyncTestChannelIDSource : public ChannelIDSource, |
135 public CryptoTestUtils::CallbackSource { | 135 public CryptoTestUtils::CallbackSource { |
136 public: | 136 public: |
137 // Takes ownership of |sync_source|, a synchronous ChannelIDSource. | 137 // Takes ownership of |sync_source|, a synchronous ChannelIDSource. |
138 explicit AsyncTestChannelIDSource(ChannelIDSource* sync_source) | 138 explicit AsyncTestChannelIDSource(ChannelIDSource* sync_source) |
139 : sync_source_(sync_source) {} | 139 : sync_source_(sync_source) {} |
140 virtual ~AsyncTestChannelIDSource() {} | 140 virtual ~AsyncTestChannelIDSource() {} |
141 | 141 |
142 // ChannelIDSource implementation. | 142 // ChannelIDSource implementation. |
143 virtual QuicAsyncStatus GetChannelIDKey( | 143 virtual QuicAsyncStatus GetChannelIDKey( |
144 const string& hostname, | 144 const string& hostname, |
145 scoped_ptr<ChannelIDKey>* channel_id_key, | 145 scoped_ptr<ChannelIDKey>* channel_id_key, |
146 ChannelIDSourceCallback* callback) OVERRIDE { | 146 ChannelIDSourceCallback* callback) OVERRIDE { |
147 // Synchronous mode. | 147 // Synchronous mode. |
148 if (!callback) { | 148 if (!callback) { |
149 return sync_source_->GetChannelIDKey(hostname, channel_id_key, NULL); | 149 return sync_source_->GetChannelIDKey(hostname, channel_id_key, nullptr); |
150 } | 150 } |
151 | 151 |
152 // Asynchronous mode. | 152 // Asynchronous mode. |
153 QuicAsyncStatus status = | 153 QuicAsyncStatus status = |
154 sync_source_->GetChannelIDKey(hostname, &channel_id_key_, NULL); | 154 sync_source_->GetChannelIDKey(hostname, &channel_id_key_, nullptr); |
155 if (status != QUIC_SUCCESS) { | 155 if (status != QUIC_SUCCESS) { |
156 return QUIC_FAILURE; | 156 return QUIC_FAILURE; |
157 } | 157 } |
158 callback_.reset(callback); | 158 callback_.reset(callback); |
159 return QUIC_PENDING; | 159 return QUIC_PENDING; |
160 } | 160 } |
161 | 161 |
162 // CallbackSource implementation. | 162 // CallbackSource implementation. |
163 virtual void RunPendingCallbacks() OVERRIDE { | 163 virtual void RunPendingCallbacks() OVERRIDE { |
164 if (callback_.get()) { | 164 if (callback_.get()) { |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
220 QuicCryptoClientConfig crypto_config; | 220 QuicCryptoClientConfig crypto_config; |
221 | 221 |
222 client_session.config()->SetDefaults(); | 222 client_session.config()->SetDefaults(); |
223 crypto_config.SetDefaults(); | 223 crypto_config.SetDefaults(); |
224 if (!options.dont_verify_certs) { | 224 if (!options.dont_verify_certs) { |
225 // TODO(wtc): replace this with ProofVerifierForTesting() when we have | 225 // TODO(wtc): replace this with ProofVerifierForTesting() when we have |
226 // a working ProofSourceForTesting(). | 226 // a working ProofSourceForTesting(). |
227 crypto_config.SetProofVerifier(FakeProofVerifierForTesting()); | 227 crypto_config.SetProofVerifier(FakeProofVerifierForTesting()); |
228 } | 228 } |
229 bool is_https = false; | 229 bool is_https = false; |
230 AsyncTestChannelIDSource* async_channel_id_source = NULL; | 230 AsyncTestChannelIDSource* async_channel_id_source = nullptr; |
231 if (options.channel_id_enabled) { | 231 if (options.channel_id_enabled) { |
232 is_https = true; | 232 is_https = true; |
233 | 233 |
234 ChannelIDSource* source = ChannelIDSourceForTesting(); | 234 ChannelIDSource* source = ChannelIDSourceForTesting(); |
235 if (options.channel_id_source_async) { | 235 if (options.channel_id_source_async) { |
236 async_channel_id_source = new AsyncTestChannelIDSource(source); | 236 async_channel_id_source = new AsyncTestChannelIDSource(source); |
237 source = async_channel_id_source; | 237 source = async_channel_id_source; |
238 } | 238 } |
239 crypto_config.SetChannelIDSource(source); | 239 crypto_config.SetChannelIDSource(source); |
240 } | 240 } |
241 QuicServerId server_id(kServerHostname, kServerPort, is_https, | 241 QuicServerId server_id(kServerHostname, kServerPort, is_https, |
242 PRIVACY_MODE_DISABLED); | 242 PRIVACY_MODE_DISABLED); |
243 QuicCryptoClientStream client(server_id, &client_session, | 243 QuicCryptoClientStream client(server_id, &client_session, |
244 ProofVerifyContextForTesting(), | 244 ProofVerifyContextForTesting(), |
245 &crypto_config); | 245 &crypto_config); |
246 client_session.SetCryptoStream(&client); | 246 client_session.SetCryptoStream(&client); |
247 | 247 |
248 CHECK(client.CryptoConnect()); | 248 CHECK(client.CryptoConnect()); |
249 CHECK_EQ(1u, client_conn->packets_.size()); | 249 CHECK_EQ(1u, client_conn->packets_.size()); |
250 | 250 |
251 CommunicateHandshakeMessagesAndRunCallbacks( | 251 CommunicateHandshakeMessagesAndRunCallbacks( |
252 client_conn, &client, server_conn, server, async_channel_id_source); | 252 client_conn, &client, server_conn, server, async_channel_id_source); |
253 | 253 |
254 CompareClientAndServerKeys(&client, server); | 254 CompareClientAndServerKeys(&client, server); |
255 | 255 |
256 if (options.channel_id_enabled) { | 256 if (options.channel_id_enabled) { |
257 scoped_ptr<ChannelIDKey> channel_id_key; | 257 scoped_ptr<ChannelIDKey> channel_id_key; |
258 QuicAsyncStatus status = | 258 QuicAsyncStatus status = crypto_config.channel_id_source()->GetChannelIDKey( |
259 crypto_config.channel_id_source()->GetChannelIDKey(kServerHostname, | 259 kServerHostname, &channel_id_key, nullptr); |
260 &channel_id_key, | |
261 NULL); | |
262 EXPECT_EQ(QUIC_SUCCESS, status); | 260 EXPECT_EQ(QUIC_SUCCESS, status); |
263 EXPECT_EQ(channel_id_key->SerializeKey(), | 261 EXPECT_EQ(channel_id_key->SerializeKey(), |
264 server->crypto_negotiated_params().channel_id); | 262 server->crypto_negotiated_params().channel_id); |
265 EXPECT_EQ(options.channel_id_source_async, | 263 EXPECT_EQ(options.channel_id_source_async, |
266 client.WasChannelIDSourceCallbackRun()); | 264 client.WasChannelIDSourceCallbackRun()); |
267 } | 265 } |
268 | 266 |
269 return client.num_sent_client_hellos(); | 267 return client.num_sent_client_hellos(); |
270 } | 268 } |
271 | 269 |
272 // static | 270 // static |
273 void CryptoTestUtils::SetupCryptoServerConfigForTest( | 271 void CryptoTestUtils::SetupCryptoServerConfigForTest( |
274 const QuicClock* clock, | 272 const QuicClock* clock, |
275 QuicRandom* rand, | 273 QuicRandom* rand, |
276 QuicConfig* config, | 274 QuicConfig* config, |
277 QuicCryptoServerConfig* crypto_config) { | 275 QuicCryptoServerConfig* crypto_config) { |
278 config->SetDefaults(); | 276 config->SetDefaults(); |
279 QuicCryptoServerConfig::ConfigOptions options; | 277 QuicCryptoServerConfig::ConfigOptions options; |
280 options.channel_id_enabled = true; | 278 options.channel_id_enabled = true; |
281 scoped_ptr<CryptoHandshakeMessage> scfg( | 279 scoped_ptr<CryptoHandshakeMessage> scfg( |
282 crypto_config->AddDefaultConfig(rand, clock, options)); | 280 crypto_config->AddDefaultConfig(rand, clock, options)); |
283 } | 281 } |
284 | 282 |
285 // static | 283 // static |
286 void CryptoTestUtils::CommunicateHandshakeMessages( | 284 void CryptoTestUtils::CommunicateHandshakeMessages( |
287 PacketSavingConnection* a_conn, | 285 PacketSavingConnection* a_conn, |
288 QuicCryptoStream* a, | 286 QuicCryptoStream* a, |
289 PacketSavingConnection* b_conn, | 287 PacketSavingConnection* b_conn, |
290 QuicCryptoStream* b) { | 288 QuicCryptoStream* b) { |
291 CommunicateHandshakeMessagesAndRunCallbacks(a_conn, a, b_conn, b, NULL); | 289 CommunicateHandshakeMessagesAndRunCallbacks(a_conn, a, b_conn, b, nullptr); |
292 } | 290 } |
293 | 291 |
294 // static | 292 // static |
295 void CryptoTestUtils::CommunicateHandshakeMessagesAndRunCallbacks( | 293 void CryptoTestUtils::CommunicateHandshakeMessagesAndRunCallbacks( |
296 PacketSavingConnection* a_conn, | 294 PacketSavingConnection* a_conn, |
297 QuicCryptoStream* a, | 295 QuicCryptoStream* a, |
298 PacketSavingConnection* b_conn, | 296 PacketSavingConnection* b_conn, |
299 QuicCryptoStream* b, | 297 QuicCryptoStream* b, |
300 CallbackSource* callback_source) { | 298 CallbackSource* callback_source) { |
301 size_t a_i = 0, b_i = 0; | 299 size_t a_i = 0, b_i = 0; |
(...skipping 271 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
573 } | 571 } |
574 | 572 |
575 // static | 573 // static |
576 CryptoHandshakeMessage CryptoTestUtils::BuildMessage(const char* message_tag, | 574 CryptoHandshakeMessage CryptoTestUtils::BuildMessage(const char* message_tag, |
577 va_list ap) { | 575 va_list ap) { |
578 CryptoHandshakeMessage msg; | 576 CryptoHandshakeMessage msg; |
579 msg.set_tag(ParseTag(message_tag)); | 577 msg.set_tag(ParseTag(message_tag)); |
580 | 578 |
581 for (;;) { | 579 for (;;) { |
582 const char* tagstr = va_arg(ap, const char*); | 580 const char* tagstr = va_arg(ap, const char*); |
583 if (tagstr == NULL) { | 581 if (tagstr == nullptr) { |
584 break; | 582 break; |
585 } | 583 } |
586 | 584 |
587 if (tagstr[0] == '$') { | 585 if (tagstr[0] == '$') { |
588 // Special value. | 586 // Special value. |
589 const char* const special = tagstr + 1; | 587 const char* const special = tagstr + 1; |
590 if (strcmp(special, "padding") == 0) { | 588 if (strcmp(special, "padding") == 0) { |
591 const int min_bytes = va_arg(ap, int); | 589 const int min_bytes = va_arg(ap, int); |
592 msg.set_minimum_size(min_bytes); | 590 msg.set_minimum_size(min_bytes); |
593 } else { | 591 } else { |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
629 scoped_ptr<QuicData> bytes(CryptoFramer::ConstructHandshakeMessage(msg)); | 627 scoped_ptr<QuicData> bytes(CryptoFramer::ConstructHandshakeMessage(msg)); |
630 scoped_ptr<CryptoHandshakeMessage> parsed( | 628 scoped_ptr<CryptoHandshakeMessage> parsed( |
631 CryptoFramer::ParseMessage(bytes->AsStringPiece())); | 629 CryptoFramer::ParseMessage(bytes->AsStringPiece())); |
632 CHECK(parsed.get()); | 630 CHECK(parsed.get()); |
633 | 631 |
634 return *parsed; | 632 return *parsed; |
635 } | 633 } |
636 | 634 |
637 } // namespace test | 635 } // namespace test |
638 } // namespace net | 636 } // namespace net |
OLD | NEW |