| 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 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 117 *value = c - 'a' + 10; | 117 *value = c - 'a' + 10; |
| 118 return true; | 118 return true; |
| 119 } | 119 } |
| 120 if (c >= 'A' && c <= 'F') { | 120 if (c >= 'A' && c <= 'F') { |
| 121 *value = c - 'A' + 10; | 121 *value = c - 'A' + 10; |
| 122 return true; | 122 return true; |
| 123 } | 123 } |
| 124 return false; | 124 return false; |
| 125 } | 125 } |
| 126 | 126 |
| 127 // A ChannelIDSource that works in asynchronous mode unless the |callback| |
| 128 // argument to GetChannelIDKey is NULL. |
| 129 class AsyncTestChannelIDSource : public ChannelIDSource, |
| 130 public CryptoTestUtils::WorkSource { |
| 131 public: |
| 132 // Takes ownership of |sync_source|. |
| 133 explicit AsyncTestChannelIDSource(ChannelIDSource* sync_source) |
| 134 : sync_source_(sync_source) {} |
| 135 virtual ~AsyncTestChannelIDSource() {} |
| 136 |
| 137 // ChannelIDSource implementation. |
| 138 virtual QuicAsyncStatus GetChannelIDKey( |
| 139 const string& hostname, |
| 140 scoped_ptr<ChannelIDKey>* channel_id_key, |
| 141 ChannelIDSourceCallback* callback) OVERRIDE { |
| 142 // Synchronous mode. |
| 143 if (!callback) { |
| 144 return sync_source_->GetChannelIDKey(hostname, channel_id_key, NULL); |
| 145 } |
| 146 |
| 147 // Asynchronous mode. |
| 148 QuicAsyncStatus status = |
| 149 sync_source_->GetChannelIDKey(hostname, &channel_id_key_, NULL); |
| 150 if (status != QUIC_SUCCESS) { |
| 151 return QUIC_FAILURE; |
| 152 } |
| 153 callback_.reset(callback); |
| 154 return QUIC_PENDING; |
| 155 } |
| 156 |
| 157 // WorkSource implementation. |
| 158 virtual void DoPendingWork() OVERRIDE { |
| 159 if (callback_.get()) { |
| 160 callback_->Run(&channel_id_key_); |
| 161 callback_.reset(); |
| 162 } |
| 163 } |
| 164 |
| 165 private: |
| 166 scoped_ptr<ChannelIDSource> sync_source_; |
| 167 scoped_ptr<ChannelIDSourceCallback> callback_; |
| 168 scoped_ptr<ChannelIDKey> channel_id_key_; |
| 169 }; |
| 170 |
| 127 } // anonymous namespace | 171 } // anonymous namespace |
| 128 | 172 |
| 129 CryptoTestUtils::FakeClientOptions::FakeClientOptions() | 173 CryptoTestUtils::FakeClientOptions::FakeClientOptions() |
| 130 : dont_verify_certs(false), | 174 : dont_verify_certs(false), |
| 131 channel_id_enabled(false) { | 175 channel_id_enabled(false), |
| 176 channel_id_source_async(false) { |
| 132 } | 177 } |
| 133 | 178 |
| 134 // static | 179 // static |
| 135 int CryptoTestUtils::HandshakeWithFakeServer( | 180 int CryptoTestUtils::HandshakeWithFakeServer( |
| 136 PacketSavingConnection* client_conn, | 181 PacketSavingConnection* client_conn, |
| 137 QuicCryptoClientStream* client) { | 182 QuicCryptoClientStream* client) { |
| 138 PacketSavingConnection* server_conn = | 183 PacketSavingConnection* server_conn = |
| 139 new PacketSavingConnection(true, client_conn->supported_versions()); | 184 new PacketSavingConnection(true, client_conn->supported_versions()); |
| 140 TestSession server_session(server_conn, DefaultQuicConfig()); | 185 TestSession server_session(server_conn, DefaultQuicConfig()); |
| 141 | 186 |
| (...skipping 27 matching lines...) Expand all Loading... |
| 169 QuicCryptoClientConfig crypto_config; | 214 QuicCryptoClientConfig crypto_config; |
| 170 | 215 |
| 171 client_session.config()->SetDefaults(); | 216 client_session.config()->SetDefaults(); |
| 172 crypto_config.SetDefaults(); | 217 crypto_config.SetDefaults(); |
| 173 if (!options.dont_verify_certs) { | 218 if (!options.dont_verify_certs) { |
| 174 // TODO(wtc): replace this with ProofVerifierForTesting() when we have | 219 // TODO(wtc): replace this with ProofVerifierForTesting() when we have |
| 175 // a working ProofSourceForTesting(). | 220 // a working ProofSourceForTesting(). |
| 176 crypto_config.SetProofVerifier(FakeProofVerifierForTesting()); | 221 crypto_config.SetProofVerifier(FakeProofVerifierForTesting()); |
| 177 } | 222 } |
| 178 bool is_https = false; | 223 bool is_https = false; |
| 224 AsyncTestChannelIDSource* async_channel_id_source = NULL; |
| 179 if (options.channel_id_enabled) { | 225 if (options.channel_id_enabled) { |
| 180 is_https = true; | 226 is_https = true; |
| 181 crypto_config.SetChannelIDSource(ChannelIDSourceForTesting()); | 227 |
| 228 ChannelIDSource* source = ChannelIDSourceForTesting(); |
| 229 if (options.channel_id_source_async) { |
| 230 async_channel_id_source = new AsyncTestChannelIDSource(source); |
| 231 source = async_channel_id_source; |
| 232 } |
| 233 crypto_config.SetChannelIDSource(source); |
| 182 } | 234 } |
| 183 QuicServerId server_id(kServerHostname, kServerPort, is_https, | 235 QuicServerId server_id(kServerHostname, kServerPort, is_https, |
| 184 PRIVACY_MODE_DISABLED); | 236 PRIVACY_MODE_DISABLED); |
| 185 QuicCryptoClientStream client(server_id, &client_session, | 237 QuicCryptoClientStream client(server_id, &client_session, |
| 186 ProofVerifyContextForTesting(), | 238 ProofVerifyContextForTesting(), |
| 187 &crypto_config); | 239 &crypto_config); |
| 188 client_session.SetCryptoStream(&client); | 240 client_session.SetCryptoStream(&client); |
| 189 | 241 |
| 190 CHECK(client.CryptoConnect()); | 242 CHECK(client.CryptoConnect()); |
| 191 CHECK_EQ(1u, client_conn->packets_.size()); | 243 CHECK_EQ(1u, client_conn->packets_.size()); |
| 192 | 244 |
| 193 CommunicateHandshakeMessages(client_conn, &client, server_conn, server); | 245 CommunicateHandshakeMessagesAndDoWork( |
| 246 client_conn, &client, server_conn, server, async_channel_id_source); |
| 194 | 247 |
| 195 CompareClientAndServerKeys(&client, server); | 248 CompareClientAndServerKeys(&client, server); |
| 196 | 249 |
| 197 if (options.channel_id_enabled) { | 250 if (options.channel_id_enabled) { |
| 198 scoped_ptr<ChannelIDKey> channel_id_key; | 251 scoped_ptr<ChannelIDKey> channel_id_key; |
| 199 QuicAsyncStatus status = | 252 QuicAsyncStatus status = |
| 200 crypto_config.channel_id_source()->GetChannelIDKey(kServerHostname, | 253 crypto_config.channel_id_source()->GetChannelIDKey(kServerHostname, |
| 201 &channel_id_key, | 254 &channel_id_key, |
| 202 NULL); | 255 NULL); |
| 203 EXPECT_EQ(QUIC_SUCCESS, status); | 256 EXPECT_EQ(QUIC_SUCCESS, status); |
| (...skipping 16 matching lines...) Expand all Loading... |
| 220 scoped_ptr<CryptoHandshakeMessage> scfg( | 273 scoped_ptr<CryptoHandshakeMessage> scfg( |
| 221 crypto_config->AddDefaultConfig(rand, clock, options)); | 274 crypto_config->AddDefaultConfig(rand, clock, options)); |
| 222 } | 275 } |
| 223 | 276 |
| 224 // static | 277 // static |
| 225 void CryptoTestUtils::CommunicateHandshakeMessages( | 278 void CryptoTestUtils::CommunicateHandshakeMessages( |
| 226 PacketSavingConnection* a_conn, | 279 PacketSavingConnection* a_conn, |
| 227 QuicCryptoStream* a, | 280 QuicCryptoStream* a, |
| 228 PacketSavingConnection* b_conn, | 281 PacketSavingConnection* b_conn, |
| 229 QuicCryptoStream* b) { | 282 QuicCryptoStream* b) { |
| 283 CommunicateHandshakeMessagesAndDoWork(a_conn, a, b_conn, b, NULL); |
| 284 } |
| 285 |
| 286 // static |
| 287 void CryptoTestUtils::CommunicateHandshakeMessagesAndDoWork( |
| 288 PacketSavingConnection* a_conn, |
| 289 QuicCryptoStream* a, |
| 290 PacketSavingConnection* b_conn, |
| 291 QuicCryptoStream* b, |
| 292 WorkSource* work_source) { |
| 230 size_t a_i = 0, b_i = 0; | 293 size_t a_i = 0, b_i = 0; |
| 231 while (!a->handshake_confirmed()) { | 294 while (!a->handshake_confirmed()) { |
| 295 if (work_source) { |
| 296 work_source->DoPendingWork(); |
| 297 } |
| 232 ASSERT_GT(a_conn->packets_.size(), a_i); | 298 ASSERT_GT(a_conn->packets_.size(), a_i); |
| 233 LOG(INFO) << "Processing " << a_conn->packets_.size() - a_i | 299 LOG(INFO) << "Processing " << a_conn->packets_.size() - a_i |
| 234 << " packets a->b"; | 300 << " packets a->b"; |
| 235 MovePackets(a_conn, &a_i, b, b_conn); | 301 MovePackets(a_conn, &a_i, b, b_conn); |
| 236 | 302 |
| 303 if (work_source) { |
| 304 work_source->DoPendingWork(); |
| 305 } |
| 237 ASSERT_GT(b_conn->packets_.size(), b_i); | 306 ASSERT_GT(b_conn->packets_.size(), b_i); |
| 238 LOG(INFO) << "Processing " << b_conn->packets_.size() - b_i | 307 LOG(INFO) << "Processing " << b_conn->packets_.size() - b_i |
| 239 << " packets b->a"; | 308 << " packets b->a"; |
| 240 if (b_conn->packets_.size() - b_i == 2) { | |
| 241 LOG(INFO) << "here"; | |
| 242 } | |
| 243 MovePackets(b_conn, &b_i, a, a_conn); | 309 MovePackets(b_conn, &b_i, a, a_conn); |
| 244 } | 310 } |
| 245 } | 311 } |
| 246 | 312 |
| 247 // static | 313 // static |
| 248 pair<size_t, size_t> CryptoTestUtils::AdvanceHandshake( | 314 pair<size_t, size_t> CryptoTestUtils::AdvanceHandshake( |
| 249 PacketSavingConnection* a_conn, | 315 PacketSavingConnection* a_conn, |
| 250 QuicCryptoStream* a, | 316 QuicCryptoStream* a, |
| 251 size_t a_i, | 317 size_t a_i, |
| 252 PacketSavingConnection* b_conn, | 318 PacketSavingConnection* b_conn, |
| (...skipping 272 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 525 scoped_ptr<QuicData> bytes(CryptoFramer::ConstructHandshakeMessage(msg)); | 591 scoped_ptr<QuicData> bytes(CryptoFramer::ConstructHandshakeMessage(msg)); |
| 526 scoped_ptr<CryptoHandshakeMessage> parsed( | 592 scoped_ptr<CryptoHandshakeMessage> parsed( |
| 527 CryptoFramer::ParseMessage(bytes->AsStringPiece())); | 593 CryptoFramer::ParseMessage(bytes->AsStringPiece())); |
| 528 CHECK(parsed.get()); | 594 CHECK(parsed.get()); |
| 529 | 595 |
| 530 return *parsed; | 596 return *parsed; |
| 531 } | 597 } |
| 532 | 598 |
| 533 } // namespace test | 599 } // namespace test |
| 534 } // namespace net | 600 } // namespace net |
| OLD | NEW |