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" |
11 #include "net/cert/x509_certificate.h" | 11 #include "net/cert/x509_certificate.h" |
12 #include "net/quic/crypto/proof_verifier.h" | 12 #include "net/quic/crypto/proof_verifier.h" |
13 #include "net/quic/test_tools/quic_connection_peer.h" | 13 #include "net/quic/test_tools/quic_connection_peer.h" |
14 #include "net/tools/balsa/balsa_headers.h" | 14 #include "net/tools/balsa/balsa_headers.h" |
15 #include "net/tools/quic/quic_epoll_connection_helper.h" | 15 #include "net/tools/quic/quic_epoll_connection_helper.h" |
| 16 #include "net/tools/quic/quic_packet_writer_wrapper.h" |
16 #include "net/tools/quic/quic_spdy_client_stream.h" | 17 #include "net/tools/quic/quic_spdy_client_stream.h" |
17 #include "net/tools/quic/test_tools/http_message_test_utils.h" | 18 #include "net/tools/quic/test_tools/http_message_test_utils.h" |
18 #include "net/tools/quic/test_tools/quic_client_peer.h" | 19 #include "net/tools/quic/test_tools/quic_client_peer.h" |
19 #include "url/gurl.h" | 20 #include "url/gurl.h" |
20 | 21 |
21 using base::StringPiece; | 22 using base::StringPiece; |
22 using net::test::QuicConnectionPeer; | 23 using net::test::QuicConnectionPeer; |
23 using net::test::QuicTestWriter; | |
24 using std::string; | 24 using std::string; |
25 using std::vector; | 25 using std::vector; |
26 | 26 |
27 namespace { | 27 namespace { |
28 | 28 |
29 // RecordingProofVerifier accepts any certificate chain and records the common | 29 // RecordingProofVerifier accepts any certificate chain and records the common |
30 // name of the leaf. | 30 // name of the leaf. |
31 class RecordingProofVerifier : public net::ProofVerifier { | 31 class RecordingProofVerifier : public net::ProofVerifier { |
32 public: | 32 public: |
33 // ProofVerifier interface. | 33 // ProofVerifier interface. |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
89 // If we have a relative URL, set some defaults. | 89 // If we have a relative URL, set some defaults. |
90 string full_uri = secure ? "https://www.google.com" : | 90 string full_uri = secure ? "https://www.google.com" : |
91 "http://www.google.com"; | 91 "http://www.google.com"; |
92 full_uri.append(uri.as_string()); | 92 full_uri.append(uri.as_string()); |
93 headers->SetRequestUri(full_uri); | 93 headers->SetRequestUri(full_uri); |
94 } | 94 } |
95 return headers; | 95 return headers; |
96 } | 96 } |
97 | 97 |
98 // A quic client which allows mocking out writes. | 98 // A quic client which allows mocking out writes. |
99 class QuicEpollClient : public QuicClient { | 99 class MockableQuicClient : public QuicClient { |
100 public: | 100 public: |
101 typedef QuicClient Super; | 101 MockableQuicClient(IPEndPoint server_address, |
| 102 const string& server_hostname, |
| 103 const QuicVersionVector& supported_versions) |
| 104 : QuicClient(server_address, server_hostname, supported_versions, false), |
| 105 override_guid_(0), |
| 106 test_writer_(NULL) {} |
102 | 107 |
103 QuicEpollClient(IPEndPoint server_address, | 108 MockableQuicClient(IPEndPoint server_address, |
104 const string& server_hostname, | 109 const string& server_hostname, |
105 const QuicVersionVector& supported_versions) | 110 const QuicConfig& config, |
106 : Super(server_address, server_hostname, supported_versions, false), | 111 const QuicVersionVector& supported_versions) |
107 override_guid_(0), test_writer_(NULL) { | 112 : QuicClient(server_address, server_hostname, config, supported_versions), |
108 } | 113 override_guid_(0), |
| 114 test_writer_(NULL) {} |
109 | 115 |
110 QuicEpollClient(IPEndPoint server_address, | 116 virtual ~MockableQuicClient() { |
111 const string& server_hostname, | |
112 const QuicConfig& config, | |
113 const QuicVersionVector& supported_versions) | |
114 : Super(server_address, server_hostname, config, supported_versions), | |
115 override_guid_(0), test_writer_(NULL) { | |
116 } | |
117 | |
118 virtual ~QuicEpollClient() { | |
119 if (connected()) { | 117 if (connected()) { |
120 Disconnect(); | 118 Disconnect(); |
121 } | 119 } |
122 } | 120 } |
123 | 121 |
124 virtual QuicPacketWriter* CreateQuicPacketWriter() OVERRIDE { | 122 virtual QuicPacketWriter* CreateQuicPacketWriter() OVERRIDE { |
125 QuicPacketWriter* writer = Super::CreateQuicPacketWriter(); | 123 QuicPacketWriter* writer = QuicClient::CreateQuicPacketWriter(); |
126 if (!test_writer_) { | 124 if (!test_writer_) { |
127 return writer; | 125 return writer; |
128 } | 126 } |
129 test_writer_->set_writer(writer); | 127 test_writer_->set_writer(writer); |
130 return test_writer_; | 128 return test_writer_; |
131 } | 129 } |
132 | 130 |
133 virtual QuicGuid GenerateGuid() OVERRIDE { | 131 virtual QuicGuid GenerateGuid() OVERRIDE { |
134 return override_guid_ ? override_guid_ : Super::GenerateGuid(); | 132 return override_guid_ ? override_guid_ : QuicClient::GenerateGuid(); |
135 } | 133 } |
136 | 134 |
137 // Takes ownership of writer. | 135 // Takes ownership of writer. |
138 void UseWriter(QuicTestWriter* writer) { test_writer_ = writer; } | 136 void UseWriter(QuicPacketWriterWrapper* writer) { test_writer_ = writer; } |
139 | 137 |
140 void UseGuid(QuicGuid guid) { | 138 void UseGuid(QuicGuid guid) { override_guid_ = guid; } |
141 override_guid_ = guid; | |
142 } | |
143 | 139 |
144 private: | 140 private: |
145 QuicGuid override_guid_; // GUID to use, if nonzero | 141 QuicGuid override_guid_; // GUID to use, if nonzero |
146 QuicTestWriter* test_writer_; | 142 QuicPacketWriterWrapper* test_writer_; |
147 }; | 143 }; |
148 | 144 |
149 QuicTestClient::QuicTestClient(IPEndPoint address, const string& hostname, | 145 QuicTestClient::QuicTestClient(IPEndPoint address, const string& hostname, |
150 const QuicVersionVector& supported_versions) | 146 const QuicVersionVector& supported_versions) |
151 : client_(new QuicEpollClient(address, hostname, supported_versions)) { | 147 : client_(new MockableQuicClient(address, hostname, supported_versions)) { |
152 Initialize(address, hostname, true); | 148 Initialize(address, hostname, true); |
153 } | 149 } |
154 | 150 |
155 QuicTestClient::QuicTestClient(IPEndPoint address, | 151 QuicTestClient::QuicTestClient(IPEndPoint address, |
156 const string& hostname, | 152 const string& hostname, |
157 bool secure, | 153 bool secure, |
158 const QuicVersionVector& supported_versions) | 154 const QuicVersionVector& supported_versions) |
159 : client_(new QuicEpollClient(address, hostname, supported_versions)) { | 155 : client_(new MockableQuicClient(address, hostname, supported_versions)) { |
160 Initialize(address, hostname, secure); | 156 Initialize(address, hostname, secure); |
161 } | 157 } |
162 | 158 |
163 QuicTestClient::QuicTestClient(IPEndPoint address, | 159 QuicTestClient::QuicTestClient(IPEndPoint address, |
164 const string& hostname, | 160 const string& hostname, |
165 bool secure, | 161 bool secure, |
166 const QuicConfig& config, | 162 const QuicConfig& config, |
167 const QuicVersionVector& supported_versions) | 163 const QuicVersionVector& supported_versions) |
168 : client_(new QuicEpollClient(address, hostname, config, | 164 : client_(new MockableQuicClient( |
169 supported_versions)) { | 165 address, hostname, config, supported_versions)) { |
170 Initialize(address, hostname, secure); | 166 Initialize(address, hostname, secure); |
171 } | 167 } |
172 | 168 |
173 void QuicTestClient::Initialize(IPEndPoint address, | 169 void QuicTestClient::Initialize(IPEndPoint address, |
174 const string& hostname, | 170 const string& hostname, |
175 bool secure) { | 171 bool secure) { |
176 server_address_ = address; | 172 server_address_ = address; |
177 priority_ = 3; | 173 priority_ = 3; |
178 connect_attempted_ = false; | 174 connect_attempted_ = false; |
179 secure_ = secure; | 175 secure_ = secure; |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
230 } | 226 } |
231 | 227 |
232 ssize_t QuicTestClient::SendData(string data, bool last_data) { | 228 ssize_t QuicTestClient::SendData(string data, bool last_data) { |
233 QuicSpdyClientStream* stream = GetOrCreateStream(); | 229 QuicSpdyClientStream* stream = GetOrCreateStream(); |
234 if (!stream) { return 0; } | 230 if (!stream) { return 0; } |
235 GetOrCreateStream()->SendBody(data, last_data); | 231 GetOrCreateStream()->SendBody(data, last_data); |
236 WaitForWriteToFlush(); | 232 WaitForWriteToFlush(); |
237 return data.length(); | 233 return data.length(); |
238 } | 234 } |
239 | 235 |
| 236 QuicPacketCreator::Options* QuicTestClient::options() { |
| 237 return client_->options(); |
| 238 } |
| 239 |
240 string QuicTestClient::SendCustomSynchronousRequest( | 240 string QuicTestClient::SendCustomSynchronousRequest( |
241 const HTTPMessage& message) { | 241 const HTTPMessage& message) { |
242 SendMessage(message); | 242 SendMessage(message); |
243 WaitForResponse(); | 243 WaitForResponse(); |
244 return response_; | 244 return response_; |
245 } | 245 } |
246 | 246 |
247 string QuicTestClient::SendSynchronousRequest(const string& uri) { | 247 string QuicTestClient::SendSynchronousRequest(const string& uri) { |
248 if (SendRequest(uri) == 0) { | 248 if (SendRequest(uri) == 0) { |
249 DLOG(ERROR) << "Failed the request for uri:" << uri; | 249 DLOG(ERROR) << "Failed the request for uri:" << uri; |
(...skipping 17 matching lines...) Expand all Loading... |
267 if (stream_ == NULL) { | 267 if (stream_ == NULL) { |
268 return NULL; | 268 return NULL; |
269 } | 269 } |
270 stream_->set_visitor(this); | 270 stream_->set_visitor(this); |
271 reinterpret_cast<QuicSpdyClientStream*>(stream_)->set_priority(priority_); | 271 reinterpret_cast<QuicSpdyClientStream*>(stream_)->set_priority(priority_); |
272 } | 272 } |
273 | 273 |
274 return stream_; | 274 return stream_; |
275 } | 275 } |
276 | 276 |
| 277 QuicErrorCode QuicTestClient::connection_error() { |
| 278 return client()->session()->error(); |
| 279 } |
| 280 |
| 281 QuicClient* QuicTestClient::client() { return client_.get(); } |
| 282 |
277 const string& QuicTestClient::cert_common_name() const { | 283 const string& QuicTestClient::cert_common_name() const { |
278 return reinterpret_cast<RecordingProofVerifier*>(proof_verifier_) | 284 return reinterpret_cast<RecordingProofVerifier*>(proof_verifier_) |
279 ->common_name(); | 285 ->common_name(); |
280 } | 286 } |
281 | 287 |
282 QuicTagValueMap QuicTestClient::GetServerConfig() const { | 288 QuicTagValueMap QuicTestClient::GetServerConfig() const { |
283 net::QuicCryptoClientConfig* config = | 289 net::QuicCryptoClientConfig* config = |
284 QuicClientPeer::GetCryptoConfig(client_.get()); | 290 QuicClientPeer::GetCryptoConfig(client_.get()); |
285 net::QuicCryptoClientConfig::CachedState* state = | 291 net::QuicCryptoClientConfig::CachedState* state = |
286 config->LookupOrCreate(client_->server_hostname()); | 292 config->LookupOrCreate(client_->server_hostname()); |
(...skipping 141 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
428 headers_.CopyFrom(stream_->headers()); | 434 headers_.CopyFrom(stream_->headers()); |
429 stream_error_ = stream_->stream_error(); | 435 stream_error_ = stream_->stream_error(); |
430 bytes_read_ = stream_->stream_bytes_read() + stream_->header_bytes_read(); | 436 bytes_read_ = stream_->stream_bytes_read() + stream_->header_bytes_read(); |
431 bytes_written_ = | 437 bytes_written_ = |
432 stream_->stream_bytes_written() + stream_->header_bytes_written(); | 438 stream_->stream_bytes_written() + stream_->header_bytes_written(); |
433 response_header_size_ = headers_.GetSizeForWriteBuffer(); | 439 response_header_size_ = headers_.GetSizeForWriteBuffer(); |
434 response_body_size_ = stream_->data().size(); | 440 response_body_size_ = stream_->data().size(); |
435 stream_ = NULL; | 441 stream_ = NULL; |
436 } | 442 } |
437 | 443 |
438 void QuicTestClient::UseWriter(QuicTestWriter* writer) { | 444 void QuicTestClient::UseWriter(QuicPacketWriterWrapper* writer) { |
439 reinterpret_cast<QuicEpollClient*>(client_.get())->UseWriter(writer); | 445 client_->UseWriter(writer); |
440 } | 446 } |
441 | 447 |
442 void QuicTestClient::UseGuid(QuicGuid guid) { | 448 void QuicTestClient::UseGuid(QuicGuid guid) { |
443 DCHECK(!connected()); | 449 DCHECK(!connected()); |
444 reinterpret_cast<QuicEpollClient*>(client_.get())->UseGuid(guid); | 450 client_->UseGuid(guid); |
445 } | 451 } |
446 | 452 |
447 void QuicTestClient::WaitForWriteToFlush() { | 453 void QuicTestClient::WaitForWriteToFlush() { |
448 while (connected() && client()->session()->HasQueuedData()) { | 454 while (connected() && client()->session()->HasDataToWrite()) { |
449 client_->WaitForEvents(); | 455 client_->WaitForEvents(); |
450 } | 456 } |
451 } | 457 } |
452 | 458 |
453 } // namespace test | 459 } // namespace test |
454 } // namespace tools | 460 } // namespace tools |
455 } // namespace net | 461 } // namespace net |
OLD | NEW |