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 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
67 } | 67 } |
68 | 68 |
69 const string& common_name() const { return common_name_; } | 69 const string& common_name() const { return common_name_; } |
70 | 70 |
71 private: | 71 private: |
72 string common_name_; | 72 string common_name_; |
73 }; | 73 }; |
74 | 74 |
75 } // anonymous namespace | 75 } // anonymous namespace |
76 | 76 |
77 BalsaHeaders* MungeHeaders(const BalsaHeaders* const_headers, | 77 BalsaHeaders* MungeHeaders(const BalsaHeaders* const_headers, bool secure) { |
78 bool secure) { | |
79 StringPiece uri = const_headers->request_uri(); | 78 StringPiece uri = const_headers->request_uri(); |
80 if (uri.empty()) { | 79 if (uri.empty()) { |
81 return NULL; | 80 return NULL; |
82 } | 81 } |
83 if (const_headers->request_method() == "CONNECT") { | 82 if (const_headers->request_method() == "CONNECT") { |
84 return NULL; | 83 return NULL; |
85 } | 84 } |
86 BalsaHeaders* headers = new BalsaHeaders; | 85 BalsaHeaders* headers = new BalsaHeaders; |
87 headers->CopyFrom(*const_headers); | 86 headers->CopyFrom(*const_headers); |
88 if (!uri.starts_with("https://") && | 87 if (!uri.starts_with("https://") && !uri.starts_with("http://")) { |
89 !uri.starts_with("http://")) { | |
90 // If we have a relative URL, set some defaults. | 88 // If we have a relative URL, set some defaults. |
91 string full_uri = secure ? "https://www.google.com" : | 89 string full_uri = |
92 "http://www.google.com"; | 90 secure ? "https://www.google.com" : "http://www.google.com"; |
93 full_uri.append(uri.as_string()); | 91 full_uri.append(uri.as_string()); |
94 headers->SetRequestUri(full_uri); | 92 headers->SetRequestUri(full_uri); |
95 } | 93 } |
96 return headers; | 94 return headers; |
97 } | 95 } |
98 | 96 |
99 MockableQuicClient::MockableQuicClient( | 97 MockableQuicClient::MockableQuicClient( |
100 IPEndPoint server_address, | 98 IPEndPoint server_address, |
101 const QuicServerId& server_id, | 99 const QuicServerId& server_id, |
102 const QuicVersionVector& supported_versions, | 100 const QuicVersionVector& supported_versions, |
103 uint32 initial_flow_control_window) | 101 uint32 initial_flow_control_window) |
104 : QuicClient(server_address, | 102 : QuicClient(server_address, |
105 server_id, | 103 server_id, |
106 supported_versions, | 104 supported_versions, |
107 false, | 105 false, |
108 initial_flow_control_window), | 106 initial_flow_control_window), |
109 override_connection_id_(0), | 107 override_connection_id_(0), |
110 test_writer_(NULL) {} | 108 test_writer_(NULL) { |
| 109 } |
111 | 110 |
112 MockableQuicClient::MockableQuicClient( | 111 MockableQuicClient::MockableQuicClient( |
113 IPEndPoint server_address, | 112 IPEndPoint server_address, |
114 const QuicServerId& server_id, | 113 const QuicServerId& server_id, |
115 const QuicConfig& config, | 114 const QuicConfig& config, |
116 const QuicVersionVector& supported_versions, | 115 const QuicVersionVector& supported_versions, |
117 uint32 initial_flow_control_window) | 116 uint32 initial_flow_control_window) |
118 : QuicClient(server_address, | 117 : QuicClient(server_address, |
119 server_id, | 118 server_id, |
120 config, | 119 config, |
121 supported_versions, | 120 supported_versions, |
122 initial_flow_control_window), | 121 initial_flow_control_window), |
123 override_connection_id_(0), | 122 override_connection_id_(0), |
124 test_writer_(NULL) {} | 123 test_writer_(NULL) { |
| 124 } |
125 | 125 |
126 MockableQuicClient::~MockableQuicClient() { | 126 MockableQuicClient::~MockableQuicClient() { |
127 if (connected()) { | 127 if (connected()) { |
128 Disconnect(); | 128 Disconnect(); |
129 } | 129 } |
130 } | 130 } |
131 | 131 |
132 QuicPacketWriter* MockableQuicClient::CreateQuicPacketWriter() { | 132 QuicPacketWriter* MockableQuicClient::CreateQuicPacketWriter() { |
133 QuicPacketWriter* writer = QuicClient::CreateQuicPacketWriter(); | 133 QuicPacketWriter* writer = QuicClient::CreateQuicPacketWriter(); |
134 if (!test_writer_) { | 134 if (!test_writer_) { |
135 return writer; | 135 return writer; |
136 } | 136 } |
137 test_writer_->set_writer(writer); | 137 test_writer_->set_writer(writer); |
138 return test_writer_; | 138 return test_writer_; |
139 } | 139 } |
140 | 140 |
141 QuicConnectionId MockableQuicClient::GenerateConnectionId() { | 141 QuicConnectionId MockableQuicClient::GenerateConnectionId() { |
142 return override_connection_id_ ? override_connection_id_ | 142 return override_connection_id_ ? override_connection_id_ |
143 : QuicClient::GenerateConnectionId(); | 143 : QuicClient::GenerateConnectionId(); |
144 } | 144 } |
145 | 145 |
146 // Takes ownership of writer. | 146 // Takes ownership of writer. |
147 void MockableQuicClient::UseWriter(QuicPacketWriterWrapper* writer) { | 147 void MockableQuicClient::UseWriter(QuicPacketWriterWrapper* writer) { |
148 CHECK(test_writer_ == NULL); | 148 CHECK(test_writer_ == NULL); |
149 test_writer_ = writer; | 149 test_writer_ = writer; |
150 } | 150 } |
151 | 151 |
152 void MockableQuicClient::UseConnectionId(QuicConnectionId connection_id) { | 152 void MockableQuicClient::UseConnectionId(QuicConnectionId connection_id) { |
153 override_connection_id_ = connection_id; | 153 override_connection_id_ = connection_id; |
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
223 if (on) { | 223 if (on) { |
224 proof_verifier_ = new RecordingProofVerifier; | 224 proof_verifier_ = new RecordingProofVerifier; |
225 client_->SetProofVerifier(proof_verifier_); | 225 client_->SetProofVerifier(proof_verifier_); |
226 } else { | 226 } else { |
227 proof_verifier_ = NULL; | 227 proof_verifier_ = NULL; |
228 client_->SetProofVerifier(NULL); | 228 client_->SetProofVerifier(NULL); |
229 } | 229 } |
230 } | 230 } |
231 | 231 |
232 ssize_t QuicTestClient::SendRequest(const string& uri) { | 232 ssize_t QuicTestClient::SendRequest(const string& uri) { |
233 HTTPMessage message(HttpConstants::HTTP_1_1, | 233 HTTPMessage message(HttpConstants::HTTP_1_1, HttpConstants::GET, uri); |
234 HttpConstants::GET, | |
235 uri); | |
236 return SendMessage(message); | 234 return SendMessage(message); |
237 } | 235 } |
238 | 236 |
239 ssize_t QuicTestClient::SendMessage(const HTTPMessage& message) { | 237 ssize_t QuicTestClient::SendMessage(const HTTPMessage& message) { |
240 stream_ = NULL; // Always force creation of a stream for SendMessage. | 238 stream_ = NULL; // Always force creation of a stream for SendMessage. |
241 | 239 |
242 // If we're not connected, try to find an sni hostname. | 240 // If we're not connected, try to find an sni hostname. |
243 if (!connected()) { | 241 if (!connected()) { |
244 GURL url(message.headers()->request_uri().as_string()); | 242 GURL url(message.headers()->request_uri().as_string()); |
245 if (!url.host().empty()) { | 243 if (!url.host().empty()) { |
246 client_->set_server_id( | 244 client_->set_server_id(QuicServerId(url.host(), |
247 QuicServerId(url.host(), | 245 url.EffectiveIntPort(), |
248 url.EffectiveIntPort(), | 246 url.SchemeIs("https"), |
249 url.SchemeIs("https"), | 247 PRIVACY_MODE_DISABLED)); |
250 PRIVACY_MODE_DISABLED)); | |
251 } | 248 } |
252 } | 249 } |
253 | 250 |
254 QuicSpdyClientStream* stream = GetOrCreateStream(); | 251 QuicSpdyClientStream* stream = GetOrCreateStream(); |
255 if (!stream) { return 0; } | 252 if (!stream) { |
| 253 return 0; |
| 254 } |
256 | 255 |
257 scoped_ptr<BalsaHeaders> munged_headers(MungeHeaders(message.headers(), | 256 scoped_ptr<BalsaHeaders> munged_headers( |
258 secure_)); | 257 MungeHeaders(message.headers(), secure_)); |
259 ssize_t ret = GetOrCreateStream()->SendRequest( | 258 ssize_t ret = GetOrCreateStream()->SendRequest( |
260 munged_headers.get() ? *munged_headers.get() : *message.headers(), | 259 munged_headers.get() ? *munged_headers.get() : *message.headers(), |
261 message.body(), | 260 message.body(), |
262 message.has_complete_message()); | 261 message.has_complete_message()); |
263 WaitForWriteToFlush(); | 262 WaitForWriteToFlush(); |
264 return ret; | 263 return ret; |
265 } | 264 } |
266 | 265 |
267 ssize_t QuicTestClient::SendData(string data, bool last_data) { | 266 ssize_t QuicTestClient::SendData(string data, bool last_data) { |
268 QuicSpdyClientStream* stream = GetOrCreateStream(); | 267 QuicSpdyClientStream* stream = GetOrCreateStream(); |
269 if (!stream) { return 0; } | 268 if (!stream) { |
| 269 return 0; |
| 270 } |
270 GetOrCreateStream()->SendBody(data, last_data); | 271 GetOrCreateStream()->SendBody(data, last_data); |
271 WaitForWriteToFlush(); | 272 WaitForWriteToFlush(); |
272 return data.length(); | 273 return data.length(); |
273 } | 274 } |
274 | 275 |
275 QuicPacketCreator::Options* QuicTestClient::options() { | 276 QuicPacketCreator::Options* QuicTestClient::options() { |
276 return client_->options(); | 277 return client_->options(); |
277 } | 278 } |
278 | 279 |
279 bool QuicTestClient::response_complete() const { | 280 bool QuicTestClient::response_complete() const { |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
338 reinterpret_cast<QuicSpdyClientStream*>(stream_)->set_priority(priority_); | 339 reinterpret_cast<QuicSpdyClientStream*>(stream_)->set_priority(priority_); |
339 } | 340 } |
340 | 341 |
341 return stream_; | 342 return stream_; |
342 } | 343 } |
343 | 344 |
344 QuicErrorCode QuicTestClient::connection_error() { | 345 QuicErrorCode QuicTestClient::connection_error() { |
345 return client()->session()->error(); | 346 return client()->session()->error(); |
346 } | 347 } |
347 | 348 |
348 MockableQuicClient* QuicTestClient::client() { return client_.get(); } | 349 MockableQuicClient* QuicTestClient::client() { |
| 350 return client_.get(); |
| 351 } |
349 | 352 |
350 const string& QuicTestClient::cert_common_name() const { | 353 const string& QuicTestClient::cert_common_name() const { |
351 return reinterpret_cast<RecordingProofVerifier*>(proof_verifier_) | 354 return reinterpret_cast<RecordingProofVerifier*>(proof_verifier_) |
352 ->common_name(); | 355 ->common_name(); |
353 } | 356 } |
354 | 357 |
355 QuicTagValueMap QuicTestClient::GetServerConfig() const { | 358 QuicTagValueMap QuicTestClient::GetServerConfig() const { |
356 QuicCryptoClientConfig* config = | 359 QuicCryptoClientConfig* config = |
357 QuicClientPeer::GetCryptoConfig(client_.get()); | 360 QuicClientPeer::GetCryptoConfig(client_.get()); |
358 QuicCryptoClientConfig::CachedState* state = | 361 QuicCryptoClientConfig::CachedState* state = |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
404 response_header_size_ = 0; | 407 response_header_size_ = 0; |
405 response_body_size_ = 0; | 408 response_body_size_ = 0; |
406 } | 409 } |
407 | 410 |
408 void QuicTestClient::WaitForResponseForMs(int timeout_ms) { | 411 void QuicTestClient::WaitForResponseForMs(int timeout_ms) { |
409 int64 timeout_us = timeout_ms * base::Time::kMicrosecondsPerMillisecond; | 412 int64 timeout_us = timeout_ms * base::Time::kMicrosecondsPerMillisecond; |
410 int64 old_timeout_us = client()->epoll_server()->timeout_in_us(); | 413 int64 old_timeout_us = client()->epoll_server()->timeout_in_us(); |
411 if (timeout_us > 0) { | 414 if (timeout_us > 0) { |
412 client()->epoll_server()->set_timeout_in_us(timeout_us); | 415 client()->epoll_server()->set_timeout_in_us(timeout_us); |
413 } | 416 } |
414 const QuicClock* clock = | 417 const QuicClock* clock = QuicConnectionPeer::GetHelper( |
415 QuicConnectionPeer::GetHelper(client()->session()->connection())-> | 418 client()->session()->connection())->GetClock(); |
416 GetClock(); | 419 QuicTime end_waiting_time = |
417 QuicTime end_waiting_time = clock->Now().Add( | 420 clock->Now().Add(QuicTime::Delta::FromMicroseconds(timeout_us)); |
418 QuicTime::Delta::FromMicroseconds(timeout_us)); | |
419 while (stream_ != NULL && | 421 while (stream_ != NULL && |
420 !client_->session()->IsClosedStream(stream_->id()) && | 422 !client_->session()->IsClosedStream(stream_->id()) && |
421 (timeout_us < 0 || clock->Now() < end_waiting_time)) { | 423 (timeout_us < 0 || clock->Now() < end_waiting_time)) { |
422 client_->WaitForEvents(); | 424 client_->WaitForEvents(); |
423 } | 425 } |
424 if (timeout_us > 0) { | 426 if (timeout_us > 0) { |
425 client()->epoll_server()->set_timeout_in_us(old_timeout_us); | 427 client()->epoll_server()->set_timeout_in_us(old_timeout_us); |
426 } | 428 } |
427 } | 429 } |
428 | 430 |
429 void QuicTestClient::WaitForInitialResponseForMs(int timeout_ms) { | 431 void QuicTestClient::WaitForInitialResponseForMs(int timeout_ms) { |
430 int64 timeout_us = timeout_ms * base::Time::kMicrosecondsPerMillisecond; | 432 int64 timeout_us = timeout_ms * base::Time::kMicrosecondsPerMillisecond; |
431 int64 old_timeout_us = client()->epoll_server()->timeout_in_us(); | 433 int64 old_timeout_us = client()->epoll_server()->timeout_in_us(); |
432 if (timeout_us > 0) { | 434 if (timeout_us > 0) { |
433 client()->epoll_server()->set_timeout_in_us(timeout_us); | 435 client()->epoll_server()->set_timeout_in_us(timeout_us); |
434 } | 436 } |
435 const QuicClock* clock = | 437 const QuicClock* clock = QuicConnectionPeer::GetHelper( |
436 QuicConnectionPeer::GetHelper(client()->session()->connection())-> | 438 client()->session()->connection())->GetClock(); |
437 GetClock(); | 439 QuicTime end_waiting_time = |
438 QuicTime end_waiting_time = clock->Now().Add( | 440 clock->Now().Add(QuicTime::Delta::FromMicroseconds(timeout_us)); |
439 QuicTime::Delta::FromMicroseconds(timeout_us)); | |
440 while (stream_ != NULL && | 441 while (stream_ != NULL && |
441 !client_->session()->IsClosedStream(stream_->id()) && | 442 !client_->session()->IsClosedStream(stream_->id()) && |
442 stream_->stream_bytes_read() == 0 && | 443 stream_->stream_bytes_read() == 0 && |
443 (timeout_us < 0 || clock->Now() < end_waiting_time)) { | 444 (timeout_us < 0 || clock->Now() < end_waiting_time)) { |
444 client_->WaitForEvents(); | 445 client_->WaitForEvents(); |
445 } | 446 } |
446 if (timeout_us > 0) { | 447 if (timeout_us > 0) { |
447 client()->epoll_server()->set_timeout_in_us(old_timeout_us); | 448 client()->epoll_server()->set_timeout_in_us(old_timeout_us); |
448 } | 449 } |
449 } | 450 } |
450 | 451 |
451 ssize_t QuicTestClient::Send(const void *buffer, size_t size) { | 452 ssize_t QuicTestClient::Send(const void* buffer, size_t size) { |
452 return SendData(string(static_cast<const char*>(buffer), size), false); | 453 return SendData(string(static_cast<const char*>(buffer), size), false); |
453 } | 454 } |
454 | 455 |
455 bool QuicTestClient::response_headers_complete() const { | 456 bool QuicTestClient::response_headers_complete() const { |
456 if (stream_ != NULL) { | 457 if (stream_ != NULL) { |
457 return stream_->headers_decompressed(); | 458 return stream_->headers_decompressed(); |
458 } else { | 459 } else { |
459 return response_headers_complete_; | 460 return response_headers_complete_; |
460 } | 461 } |
461 } | 462 } |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
502 | 503 |
503 void QuicTestClient::UseWriter(QuicPacketWriterWrapper* writer) { | 504 void QuicTestClient::UseWriter(QuicPacketWriterWrapper* writer) { |
504 client_->UseWriter(writer); | 505 client_->UseWriter(writer); |
505 } | 506 } |
506 | 507 |
507 void QuicTestClient::UseConnectionId(QuicConnectionId connection_id) { | 508 void QuicTestClient::UseConnectionId(QuicConnectionId connection_id) { |
508 DCHECK(!connected()); | 509 DCHECK(!connected()); |
509 client_->UseConnectionId(connection_id); | 510 client_->UseConnectionId(connection_id); |
510 } | 511 } |
511 | 512 |
512 ssize_t QuicTestClient::SendAndWaitForResponse(const void *buffer, | 513 ssize_t QuicTestClient::SendAndWaitForResponse(const void* buffer, |
513 size_t size) { | 514 size_t size) { |
514 LOG(DFATAL) << "Not implemented"; | 515 LOG(DFATAL) << "Not implemented"; |
515 return 0; | 516 return 0; |
516 } | 517 } |
517 | 518 |
518 void QuicTestClient::Bind(IPEndPoint* local_address) { | 519 void QuicTestClient::Bind(IPEndPoint* local_address) { |
519 DLOG(WARNING) << "Bind will be done during connect"; | 520 DLOG(WARNING) << "Bind will be done during connect"; |
520 } | 521 } |
521 | 522 |
522 string QuicTestClient::SerializeMessage(const HTTPMessage& message) { | 523 string QuicTestClient::SerializeMessage(const HTTPMessage& message) { |
(...skipping 21 matching lines...) Expand all Loading... |
544 | 545 |
545 void QuicTestClient::WaitForWriteToFlush() { | 546 void QuicTestClient::WaitForWriteToFlush() { |
546 while (connected() && client()->session()->HasDataToWrite()) { | 547 while (connected() && client()->session()->HasDataToWrite()) { |
547 client_->WaitForEvents(); | 548 client_->WaitForEvents(); |
548 } | 549 } |
549 } | 550 } |
550 | 551 |
551 } // namespace test | 552 } // namespace test |
552 } // namespace tools | 553 } // namespace tools |
553 } // namespace net | 554 } // namespace net |
OLD | NEW |