| 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/tools/quic/test_tools/quic_test_client.h" | 5 #include "net/tools/quic/test_tools/quic_test_client.h" |
| 6 | 6 |
| 7 #include "base/time/time.h" | 7 #include "base/time/time.h" |
| 8 #include "net/base/completion_callback.h" | 8 #include "net/base/completion_callback.h" |
| 9 #include "net/base/net_errors.h" | 9 #include "net/base/net_errors.h" |
| 10 #include "net/cert/cert_verify_result.h" | 10 #include "net/cert/cert_verify_result.h" |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 75 } | 75 } |
| 76 | 76 |
| 77 const string& common_name() const { return common_name_; } | 77 const string& common_name() const { return common_name_; } |
| 78 | 78 |
| 79 private: | 79 private: |
| 80 string common_name_; | 80 string common_name_; |
| 81 }; | 81 }; |
| 82 | 82 |
| 83 } // anonymous namespace | 83 } // anonymous namespace |
| 84 | 84 |
| 85 BalsaHeaders* MungeHeaders(const BalsaHeaders* const_headers, | 85 BalsaHeaders* MungeHeaders(const BalsaHeaders* const_headers) { |
| 86 bool secure) { | |
| 87 StringPiece uri = const_headers->request_uri(); | 86 StringPiece uri = const_headers->request_uri(); |
| 88 if (uri.empty()) { | 87 if (uri.empty()) { |
| 89 return nullptr; | 88 return nullptr; |
| 90 } | 89 } |
| 91 if (const_headers->request_method() == "CONNECT") { | 90 if (const_headers->request_method() == "CONNECT") { |
| 92 return nullptr; | 91 return nullptr; |
| 93 } | 92 } |
| 94 BalsaHeaders* headers = new BalsaHeaders; | 93 BalsaHeaders* headers = new BalsaHeaders; |
| 95 headers->CopyFrom(*const_headers); | 94 headers->CopyFrom(*const_headers); |
| 96 if (!uri.starts_with("https://") && | 95 if (!uri.starts_with("https://") && |
| 97 !uri.starts_with("http://")) { | 96 !uri.starts_with("http://")) { |
| 98 // If we have a relative URL, set some defaults. | 97 // If we have a relative URL, set some defaults. |
| 99 string full_uri = secure ? "https://www.google.com" : | 98 string full_uri = "https://www.google.com"; |
| 100 "http://www.google.com"; | |
| 101 full_uri.append(uri.as_string()); | 99 full_uri.append(uri.as_string()); |
| 102 headers->SetRequestUri(full_uri); | 100 headers->SetRequestUri(full_uri); |
| 103 } | 101 } |
| 104 return headers; | 102 return headers; |
| 105 } | 103 } |
| 106 | 104 |
| 107 MockableQuicClient::MockableQuicClient( | 105 MockableQuicClient::MockableQuicClient( |
| 108 IPEndPoint server_address, | 106 IPEndPoint server_address, |
| 109 const QuicServerId& server_id, | 107 const QuicServerId& server_id, |
| 110 const QuicVersionVector& supported_versions, | 108 const QuicVersionVector& supported_versions, |
| 111 EpollServer* epoll_server) | 109 EpollServer* epoll_server) |
| 112 : MockableQuicClient(server_address, | 110 : MockableQuicClient(server_address, |
| 113 server_id, | 111 server_id, |
| 114 QuicConfig(), | 112 QuicConfig(), |
| 115 supported_versions, | 113 supported_versions, |
| 116 epoll_server) {} | 114 epoll_server) {} |
| 117 | 115 |
| 118 MockableQuicClient::MockableQuicClient( | 116 MockableQuicClient::MockableQuicClient( |
| 119 IPEndPoint server_address, | 117 IPEndPoint server_address, |
| 120 const QuicServerId& server_id, | 118 const QuicServerId& server_id, |
| 121 const QuicConfig& config, | 119 const QuicConfig& config, |
| 122 const QuicVersionVector& supported_versions, | 120 const QuicVersionVector& supported_versions, |
| 123 EpollServer* epoll_server) | 121 EpollServer* epoll_server) |
| 124 : QuicClient(server_address, | 122 : QuicClient(server_address, |
| 125 server_id, | 123 server_id, |
| 126 supported_versions, | 124 supported_versions, |
| 127 config, | 125 config, |
| 128 epoll_server), | 126 epoll_server, |
| 127 new RecordingProofVerifier()), |
| 129 override_connection_id_(0), | 128 override_connection_id_(0), |
| 130 test_writer_(nullptr) {} | 129 test_writer_(nullptr) {} |
| 131 | 130 |
| 132 MockableQuicClient::~MockableQuicClient() { | 131 MockableQuicClient::~MockableQuicClient() { |
| 133 if (connected()) { | 132 if (connected()) { |
| 134 Disconnect(); | 133 Disconnect(); |
| 135 } | 134 } |
| 136 } | 135 } |
| 137 | 136 |
| 138 QuicPacketWriter* MockableQuicClient::CreateQuicPacketWriter() { | 137 QuicPacketWriter* MockableQuicClient::CreateQuicPacketWriter() { |
| (...skipping 15 matching lines...) Expand all Loading... |
| 154 CHECK(test_writer_ == nullptr); | 153 CHECK(test_writer_ == nullptr); |
| 155 test_writer_ = writer; | 154 test_writer_ = writer; |
| 156 } | 155 } |
| 157 | 156 |
| 158 void MockableQuicClient::UseConnectionId(QuicConnectionId connection_id) { | 157 void MockableQuicClient::UseConnectionId(QuicConnectionId connection_id) { |
| 159 override_connection_id_ = connection_id; | 158 override_connection_id_ = connection_id; |
| 160 } | 159 } |
| 161 | 160 |
| 162 QuicTestClient::QuicTestClient(IPEndPoint server_address, | 161 QuicTestClient::QuicTestClient(IPEndPoint server_address, |
| 163 const string& server_hostname, | 162 const string& server_hostname, |
| 164 bool secure, | |
| 165 const QuicVersionVector& supported_versions) | 163 const QuicVersionVector& supported_versions) |
| 166 : QuicTestClient(server_address, | 164 : QuicTestClient(server_address, |
| 167 server_hostname, | 165 server_hostname, |
| 168 secure, | |
| 169 QuicConfig(), | 166 QuicConfig(), |
| 170 supported_versions) {} | 167 supported_versions) {} |
| 171 | 168 |
| 172 QuicTestClient::QuicTestClient(IPEndPoint server_address, | 169 QuicTestClient::QuicTestClient(IPEndPoint server_address, |
| 173 const string& server_hostname, | 170 const string& server_hostname, |
| 174 bool secure, | |
| 175 const QuicConfig& config, | 171 const QuicConfig& config, |
| 176 const QuicVersionVector& supported_versions) | 172 const QuicVersionVector& supported_versions) |
| 177 : client_(new MockableQuicClient(server_address, | 173 : client_(new MockableQuicClient(server_address, |
| 178 QuicServerId(server_hostname, | 174 QuicServerId(server_hostname, |
| 179 server_address.port(), | 175 server_address.port(), |
| 180 secure, | |
| 181 PRIVACY_MODE_DISABLED), | 176 PRIVACY_MODE_DISABLED), |
| 182 config, | 177 config, |
| 183 supported_versions, | 178 supported_versions, |
| 184 &epoll_server_)), | 179 &epoll_server_)), |
| 185 allow_bidirectional_data_(false) { | 180 allow_bidirectional_data_(false) { |
| 186 Initialize(secure); | 181 Initialize(); |
| 187 } | 182 } |
| 188 | 183 |
| 189 QuicTestClient::QuicTestClient() : allow_bidirectional_data_(false) {} | 184 QuicTestClient::QuicTestClient() : allow_bidirectional_data_(false) {} |
| 190 | 185 |
| 191 QuicTestClient::~QuicTestClient() { | 186 QuicTestClient::~QuicTestClient() { |
| 192 if (stream_) { | 187 if (stream_) { |
| 193 stream_->set_visitor(nullptr); | 188 stream_->set_visitor(nullptr); |
| 194 } | 189 } |
| 195 client_->Disconnect(); | 190 client_->Disconnect(); |
| 196 } | 191 } |
| 197 | 192 |
| 198 void QuicTestClient::Initialize(bool secure) { | 193 void QuicTestClient::Initialize() { |
| 199 priority_ = 3; | 194 priority_ = 3; |
| 200 connect_attempted_ = false; | 195 connect_attempted_ = false; |
| 201 secure_ = secure; | |
| 202 auto_reconnect_ = false; | 196 auto_reconnect_ = false; |
| 203 buffer_body_ = true; | 197 buffer_body_ = true; |
| 204 fec_policy_ = FEC_PROTECT_OPTIONAL; | 198 fec_policy_ = FEC_PROTECT_OPTIONAL; |
| 205 proof_verifier_ = nullptr; | |
| 206 ClearPerRequestState(); | 199 ClearPerRequestState(); |
| 207 ExpectCertificates(secure_); | |
| 208 // As chrome will generally do this, we want it to be the default when it's | 200 // As chrome will generally do this, we want it to be the default when it's |
| 209 // not overridden. | 201 // not overridden. |
| 210 if (!client_->config()->HasSetBytesForConnectionIdToSend()) { | 202 if (!client_->config()->HasSetBytesForConnectionIdToSend()) { |
| 211 client_->config()->SetBytesForConnectionIdToSend(0); | 203 client_->config()->SetBytesForConnectionIdToSend(0); |
| 212 } | 204 } |
| 213 } | 205 } |
| 214 | 206 |
| 215 void QuicTestClient::ExpectCertificates(bool on) { | |
| 216 if (on) { | |
| 217 proof_verifier_ = new RecordingProofVerifier; | |
| 218 client_->SetProofVerifier(proof_verifier_); | |
| 219 } else { | |
| 220 proof_verifier_ = nullptr; | |
| 221 client_->SetProofVerifier(nullptr); | |
| 222 } | |
| 223 } | |
| 224 | |
| 225 void QuicTestClient::SetUserAgentID(const string& user_agent_id) { | 207 void QuicTestClient::SetUserAgentID(const string& user_agent_id) { |
| 226 client_->SetUserAgentID(user_agent_id); | 208 client_->SetUserAgentID(user_agent_id); |
| 227 } | 209 } |
| 228 | 210 |
| 229 ssize_t QuicTestClient::SendRequest(const string& uri) { | 211 ssize_t QuicTestClient::SendRequest(const string& uri) { |
| 230 HTTPMessage message; | 212 HTTPMessage message; |
| 231 FillInRequest(uri, &message); | 213 FillInRequest(uri, &message); |
| 232 return SendMessage(message); | 214 return SendMessage(message); |
| 233 } | 215 } |
| 234 | 216 |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 287 ssize_t QuicTestClient::SendMessage(const HTTPMessage& message) { | 269 ssize_t QuicTestClient::SendMessage(const HTTPMessage& message) { |
| 288 stream_ = nullptr; // Always force creation of a stream for SendMessage. | 270 stream_ = nullptr; // Always force creation of a stream for SendMessage. |
| 289 | 271 |
| 290 // If we're not connected, try to find an sni hostname. | 272 // If we're not connected, try to find an sni hostname. |
| 291 if (!connected()) { | 273 if (!connected()) { |
| 292 GURL url(message.headers()->request_uri().as_string()); | 274 GURL url(message.headers()->request_uri().as_string()); |
| 293 if (!url.host().empty()) { | 275 if (!url.host().empty()) { |
| 294 client_->set_server_id( | 276 client_->set_server_id( |
| 295 QuicServerId(url.host(), | 277 QuicServerId(url.host(), |
| 296 url.EffectiveIntPort(), | 278 url.EffectiveIntPort(), |
| 297 url.SchemeIs("https"), | |
| 298 PRIVACY_MODE_DISABLED)); | 279 PRIVACY_MODE_DISABLED)); |
| 299 } | 280 } |
| 300 } | 281 } |
| 301 | 282 |
| 302 // TODO(rtenneti): Add support for HTTPMessage::body_chunks(). | 283 // TODO(rtenneti): Add support for HTTPMessage::body_chunks(). |
| 303 // CHECK(message.body_chunks().empty()) | 284 // CHECK(message.body_chunks().empty()) |
| 304 // << "HTTPMessage::body_chunks not supported"; | 285 // << "HTTPMessage::body_chunks not supported"; |
| 305 | 286 |
| 306 scoped_ptr<BalsaHeaders> munged_headers(MungeHeaders(message.headers(), | 287 scoped_ptr<BalsaHeaders> munged_headers(MungeHeaders(message.headers())); |
| 307 secure_)); | |
| 308 ssize_t ret = GetOrCreateStreamAndSendRequest( | 288 ssize_t ret = GetOrCreateStreamAndSendRequest( |
| 309 (munged_headers.get() ? munged_headers.get() : message.headers()), | 289 (munged_headers.get() ? munged_headers.get() : message.headers()), |
| 310 message.body(), message.has_complete_message(), nullptr); | 290 message.body(), message.has_complete_message(), nullptr); |
| 311 WaitForWriteToFlush(); | 291 WaitForWriteToFlush(); |
| 312 return ret; | 292 return ret; |
| 313 } | 293 } |
| 314 | 294 |
| 315 ssize_t QuicTestClient::SendData(const string& data, bool last_data) { | 295 ssize_t QuicTestClient::SendData(const string& data, bool last_data) { |
| 316 return SendData(data, last_data, nullptr); | 296 return SendData(data, last_data, nullptr); |
| 317 } | 297 } |
| (...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 396 return stream_; | 376 return stream_; |
| 397 } | 377 } |
| 398 | 378 |
| 399 QuicErrorCode QuicTestClient::connection_error() { | 379 QuicErrorCode QuicTestClient::connection_error() { |
| 400 return client()->connection_error(); | 380 return client()->connection_error(); |
| 401 } | 381 } |
| 402 | 382 |
| 403 MockableQuicClient* QuicTestClient::client() { return client_.get(); } | 383 MockableQuicClient* QuicTestClient::client() { return client_.get(); } |
| 404 | 384 |
| 405 const string& QuicTestClient::cert_common_name() const { | 385 const string& QuicTestClient::cert_common_name() const { |
| 406 return reinterpret_cast<RecordingProofVerifier*>(proof_verifier_) | 386 return reinterpret_cast<RecordingProofVerifier*>(client_->proof_verifier()) |
| 407 ->common_name(); | 387 ->common_name(); |
| 408 } | 388 } |
| 409 | 389 |
| 410 QuicTagValueMap QuicTestClient::GetServerConfig() const { | 390 QuicTagValueMap QuicTestClient::GetServerConfig() const { |
| 411 QuicCryptoClientConfig* config = client_->crypto_config(); | 391 QuicCryptoClientConfig* config = client_->crypto_config(); |
| 412 QuicCryptoClientConfig::CachedState* state = | 392 QuicCryptoClientConfig::CachedState* state = |
| 413 config->LookupOrCreate(client_->server_id()); | 393 config->LookupOrCreate(client_->server_id()); |
| 414 const CryptoHandshakeMessage* handshake_msg = state->GetServerConfig(); | 394 const CryptoHandshakeMessage* handshake_msg = state->GetServerConfig(); |
| 415 if (handshake_msg != nullptr) { | 395 if (handshake_msg != nullptr) { |
| 416 return handshake_msg->tag_value_map(); | 396 return handshake_msg->tag_value_map(); |
| (...skipping 218 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 635 message->headers()->SetRequestVersion( | 615 message->headers()->SetRequestVersion( |
| 636 HTTPMessage::VersionToString(HttpConstants::HTTP_1_1)); | 616 HTTPMessage::VersionToString(HttpConstants::HTTP_1_1)); |
| 637 message->headers()->SetRequestMethod( | 617 message->headers()->SetRequestMethod( |
| 638 HTTPMessage::MethodToString(HttpConstants::GET)); | 618 HTTPMessage::MethodToString(HttpConstants::GET)); |
| 639 message->headers()->SetRequestUri(uri); | 619 message->headers()->SetRequestUri(uri); |
| 640 } | 620 } |
| 641 | 621 |
| 642 } // namespace test | 622 } // namespace test |
| 643 } // namespace tools | 623 } // namespace tools |
| 644 } // namespace net | 624 } // namespace net |
| OLD | NEW |