1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "net/tools/quic/test_tools/quic_test_client.h"
#include "base/time/time.h"
#include "net/base/completion_callback.h"
#include "net/base/net_errors.h"
#include "net/cert/cert_verify_result.h"
#include "net/cert/x509_certificate.h"
#include "net/quic/crypto/proof_verifier.h"
#include "net/quic/quic_session_key.h"
#include "net/quic/test_tools/quic_connection_peer.h"
#include "net/tools/balsa/balsa_headers.h"
#include "net/tools/quic/quic_epoll_connection_helper.h"
#include "net/tools/quic/quic_packet_writer_wrapper.h"
#include "net/tools/quic/quic_spdy_client_stream.h"
#include "net/tools/quic/test_tools/http_message.h"
#include "net/tools/quic/test_tools/quic_client_peer.h"
#include "url/gurl.h"
using base::StringPiece;
using net::test::QuicConnectionPeer;
using std::string;
using std::vector;
namespace net {
namespace tools {
namespace test {
namespace {
// RecordingProofVerifier accepts any certificate chain and records the common
// name of the leaf.
class RecordingProofVerifier : public ProofVerifier {
public:
// ProofVerifier interface.
virtual Status VerifyProof(const string& hostname,
const string& server_config,
const vector<string>& certs,
const string& signature,
const net::ProofVerifyContext* context,
string* error_details,
scoped_ptr<ProofVerifyDetails>* details,
ProofVerifierCallback* callback) OVERRIDE {
delete callback;
common_name_.clear();
if (certs.empty()) {
return FAILURE;
}
// Convert certs to X509Certificate.
vector<StringPiece> cert_pieces(certs.size());
for (unsigned i = 0; i < certs.size(); i++) {
cert_pieces[i] = StringPiece(certs[i]);
}
scoped_refptr<net::X509Certificate> cert =
net::X509Certificate::CreateFromDERCertChain(cert_pieces);
if (!cert.get()) {
return FAILURE;
}
common_name_ = cert->subject().GetDisplayName();
return SUCCESS;
}
const string& common_name() const { return common_name_; }
private:
string common_name_;
};
} // anonymous namespace
BalsaHeaders* MungeHeaders(const BalsaHeaders* const_headers,
bool secure) {
StringPiece uri = const_headers->request_uri();
if (uri.empty()) {
return NULL;
}
if (const_headers->request_method() == "CONNECT") {
return NULL;
}
BalsaHeaders* headers = new BalsaHeaders;
headers->CopyFrom(*const_headers);
if (!uri.starts_with("https://") &&
!uri.starts_with("http://")) {
// If we have a relative URL, set some defaults.
string full_uri = secure ? "https://www.google.com" :
"http://www.google.com";
full_uri.append(uri.as_string());
headers->SetRequestUri(full_uri);
}
return headers;
}
// A quic client which allows mocking out writes.
class MockableQuicClient : public QuicClient {
public:
MockableQuicClient(IPEndPoint server_address,
const QuicSessionKey& server_key,
const QuicVersionVector& supported_versions)
: QuicClient(server_address, server_key, supported_versions, false),
override_connection_id_(0),
test_writer_(NULL) {}
MockableQuicClient(IPEndPoint server_address,
const QuicSessionKey& server_key,
const QuicConfig& config,
const QuicVersionVector& supported_versions)
: QuicClient(server_address, server_key, config, supported_versions),
override_connection_id_(0),
test_writer_(NULL) {}
virtual ~MockableQuicClient() {
if (connected()) {
Disconnect();
}
}
virtual QuicPacketWriter* CreateQuicPacketWriter() OVERRIDE {
QuicPacketWriter* writer = QuicClient::CreateQuicPacketWriter();
if (!test_writer_) {
return writer;
}
test_writer_->set_writer(writer);
return test_writer_;
}
virtual QuicConnectionId GenerateConnectionId() OVERRIDE {
return override_connection_id_ ? override_connection_id_
: QuicClient::GenerateConnectionId();
}
// Takes ownership of writer.
void UseWriter(QuicPacketWriterWrapper* writer) { test_writer_ = writer; }
void UseConnectionId(QuicConnectionId connection_id) {
override_connection_id_ = connection_id;
}
private:
QuicConnectionId override_connection_id_; // ConnectionId to use, if nonzero
QuicPacketWriterWrapper* test_writer_;
};
QuicTestClient::QuicTestClient(IPEndPoint address,
const QuicSessionKey& server_key,
const QuicVersionVector& supported_versions)
: client_(new MockableQuicClient(address, server_key, supported_versions)) {
Initialize(address, server_key, true);
}
QuicTestClient::QuicTestClient(IPEndPoint address,
const QuicSessionKey& server_key,
bool secure,
const QuicVersionVector& supported_versions)
: client_(new MockableQuicClient(address, server_key, supported_versions)) {
Initialize(address, server_key, secure);
}
QuicTestClient::QuicTestClient(IPEndPoint address,
const QuicSessionKey& server_key,
bool secure,
const QuicConfig& config,
const QuicVersionVector& supported_versions)
: client_(new MockableQuicClient(address, server_key, config,
supported_versions)) {
Initialize(address, server_key, secure);
}
void QuicTestClient::Initialize(IPEndPoint address,
const QuicSessionKey& server_key,
bool secure) {
server_address_ = address;
server_key_ = server_key;
priority_ = 3;
connect_attempted_ = false;
secure_ = secure;
auto_reconnect_ = false;
buffer_body_ = true;
proof_verifier_ = NULL;
ClearPerRequestState();
ExpectCertificates(secure_);
}
QuicTestClient::~QuicTestClient() {
if (stream_) {
stream_->set_visitor(NULL);
}
}
void QuicTestClient::ExpectCertificates(bool on) {
if (on) {
proof_verifier_ = new RecordingProofVerifier;
client_->SetProofVerifier(proof_verifier_);
} else {
proof_verifier_ = NULL;
client_->SetProofVerifier(NULL);
}
}
ssize_t QuicTestClient::SendRequest(const string& uri) {
HTTPMessage message(HttpConstants::HTTP_1_1, HttpConstants::GET, uri);
return SendMessage(message);
}
ssize_t QuicTestClient::SendMessage(const HTTPMessage& message) {
stream_ = NULL; // Always force creation of a stream for SendMessage.
// If we're not connected, try to find an sni hostname.
if (!connected()) {
GURL url(message.headers()->request_uri().as_string());
if (!url.host().empty()) {
client_->set_server_key(
QuicSessionKey(url.host(), url.EffectiveIntPort(),
url.SchemeIs("https") ? true : false));
}
}
QuicSpdyClientStream* stream = GetOrCreateStream();
if (!stream) { return 0; }
scoped_ptr<BalsaHeaders> munged_headers(MungeHeaders(message.headers(),
secure_));
ssize_t ret = GetOrCreateStream()->SendRequest(
munged_headers.get() ? *munged_headers.get() : *message.headers(),
message.body(),
message.has_complete_message());
WaitForWriteToFlush();
return ret;
}
ssize_t QuicTestClient::SendData(string data, bool last_data) {
QuicSpdyClientStream* stream = GetOrCreateStream();
if (!stream) { return 0; }
GetOrCreateStream()->SendBody(data, last_data);
WaitForWriteToFlush();
return data.length();
}
QuicPacketCreator::Options* QuicTestClient::options() {
return client_->options();
}
string QuicTestClient::SendCustomSynchronousRequest(
const HTTPMessage& message) {
SendMessage(message);
WaitForResponse();
return response_;
}
string QuicTestClient::SendSynchronousRequest(const string& uri) {
if (SendRequest(uri) == 0) {
DLOG(ERROR) << "Failed the request for uri:" << uri;
return "";
}
WaitForResponse();
return response_;
}
QuicSpdyClientStream* QuicTestClient::GetOrCreateStream() {
if (!connect_attempted_ || auto_reconnect_) {
if (!connected()) {
Connect();
}
if (!connected()) {
return NULL;
}
}
if (!stream_) {
stream_ = client_->CreateReliableClientStream();
if (stream_ == NULL) {
return NULL;
}
stream_->set_visitor(this);
reinterpret_cast<QuicSpdyClientStream*>(stream_)->set_priority(priority_);
}
return stream_;
}
QuicErrorCode QuicTestClient::connection_error() {
return client()->session()->error();
}
QuicClient* QuicTestClient::client() { return client_.get(); }
const string& QuicTestClient::cert_common_name() const {
return reinterpret_cast<RecordingProofVerifier*>(proof_verifier_)
->common_name();
}
QuicTagValueMap QuicTestClient::GetServerConfig() const {
net::QuicCryptoClientConfig* config =
QuicClientPeer::GetCryptoConfig(client_.get());
net::QuicCryptoClientConfig::CachedState* state =
config->LookupOrCreate(client_->server_key());
const net::CryptoHandshakeMessage* handshake_msg = state->GetServerConfig();
if (handshake_msg != NULL) {
return handshake_msg->tag_value_map();
} else {
return QuicTagValueMap();
}
}
bool QuicTestClient::connected() const {
return client_->connected();
}
void QuicTestClient::WaitForResponse() {
if (stream_ == NULL) {
// The client has likely disconnected.
return;
}
client_->WaitForStreamToClose(stream_->id());
}
void QuicTestClient::Connect() {
DCHECK(!connected());
if (!connect_attempted_) {
client_->Initialize();
}
client_->Connect();
connect_attempted_ = true;
}
void QuicTestClient::ResetConnection() {
Disconnect();
Connect();
}
void QuicTestClient::Disconnect() {
client_->Disconnect();
connect_attempted_ = false;
}
IPEndPoint QuicTestClient::LocalSocketAddress() const {
return client_->client_address();
}
void QuicTestClient::ClearPerRequestState() {
stream_error_ = QUIC_STREAM_NO_ERROR;
stream_ = NULL;
response_ = "";
response_complete_ = false;
response_headers_complete_ = false;
headers_.Clear();
bytes_read_ = 0;
bytes_written_ = 0;
response_header_size_ = 0;
response_body_size_ = 0;
}
void QuicTestClient::WaitForResponseForMs(int timeout_ms) {
int64 timeout_us = timeout_ms * base::Time::kMicrosecondsPerMillisecond;
int64 old_timeout_us = client()->epoll_server()->timeout_in_us();
if (timeout_us > 0) {
client()->epoll_server()->set_timeout_in_us(timeout_us);
}
const QuicClock* clock =
QuicConnectionPeer::GetHelper(client()->session()->connection())->
GetClock();
QuicTime end_waiting_time = clock->Now().Add(
QuicTime::Delta::FromMicroseconds(timeout_us));
while (stream_ != NULL &&
!client_->session()->IsClosedStream(stream_->id()) &&
(timeout_us < 0 || clock->Now() < end_waiting_time)) {
client_->WaitForEvents();
}
if (timeout_us > 0) {
client()->epoll_server()->set_timeout_in_us(old_timeout_us);
}
}
void QuicTestClient::WaitForInitialResponseForMs(int timeout_ms) {
int64 timeout_us = timeout_ms * base::Time::kMicrosecondsPerMillisecond;
int64 old_timeout_us = client()->epoll_server()->timeout_in_us();
if (timeout_us > 0) {
client()->epoll_server()->set_timeout_in_us(timeout_us);
}
const QuicClock* clock =
QuicConnectionPeer::GetHelper(client()->session()->connection())->
GetClock();
QuicTime end_waiting_time = clock->Now().Add(
QuicTime::Delta::FromMicroseconds(timeout_us));
while (stream_ != NULL &&
!client_->session()->IsClosedStream(stream_->id()) &&
stream_->stream_bytes_read() == 0 &&
(timeout_us < 0 || clock->Now() < end_waiting_time)) {
client_->WaitForEvents();
}
if (timeout_us > 0) {
client()->epoll_server()->set_timeout_in_us(old_timeout_us);
}
}
ssize_t QuicTestClient::Send(const void *buffer, size_t size) {
return SendData(string(static_cast<const char*>(buffer), size), false);
}
bool QuicTestClient::response_headers_complete() const {
if (stream_ != NULL) {
return stream_->headers_decompressed();
} else {
return response_headers_complete_;
}
}
const BalsaHeaders* QuicTestClient::response_headers() const {
if (stream_ != NULL) {
return &stream_->headers();
} else {
return &headers_;
}
}
int QuicTestClient::response_size() const {
return bytes_read_;
}
size_t QuicTestClient::bytes_read() const {
return bytes_read_;
}
size_t QuicTestClient::bytes_written() const {
return bytes_written_;
}
void QuicTestClient::OnClose(QuicDataStream* stream) {
if (stream_ != stream) {
return;
}
if (buffer_body()) {
// TODO(fnk): The stream still buffers the whole thing. Fix that.
response_ = stream_->data();
}
response_complete_ = true;
response_headers_complete_ = stream_->headers_decompressed();
headers_.CopyFrom(stream_->headers());
stream_error_ = stream_->stream_error();
bytes_read_ = stream_->stream_bytes_read() + stream_->header_bytes_read();
bytes_written_ =
stream_->stream_bytes_written() + stream_->header_bytes_written();
response_header_size_ = headers_.GetSizeForWriteBuffer();
response_body_size_ = stream_->data().size();
stream_ = NULL;
}
void QuicTestClient::UseWriter(QuicPacketWriterWrapper* writer) {
client_->UseWriter(writer);
}
void QuicTestClient::UseConnectionId(QuicConnectionId connection_id) {
DCHECK(!connected());
client_->UseConnectionId(connection_id);
}
void QuicTestClient::WaitForWriteToFlush() {
while (connected() && client()->session()->HasDataToWrite()) {
client_->WaitForEvents();
}
}
} // namespace test
} // namespace tools
} // namespace net
|