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
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
|
// 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/url_request/url_request_ftp_job.h"
#include <utility>
#include <vector>
#include "base/memory/ref_counted.h"
#include "base/memory/scoped_ptr.h"
#include "base/run_loop.h"
#include "net/base/host_port_pair.h"
#include "net/base/load_states.h"
#include "net/base/request_priority.h"
#include "net/ftp/ftp_auth_cache.h"
#include "net/http/http_transaction_test_util.h"
#include "net/proxy/mock_proxy_resolver.h"
#include "net/proxy/proxy_config_service.h"
#include "net/proxy/proxy_config_service_fixed.h"
#include "net/socket/socket_test_util.h"
#include "net/url_request/ftp_protocol_handler.h"
#include "net/url_request/url_request.h"
#include "net/url_request/url_request_context.h"
#include "net/url_request/url_request_job_factory_impl.h"
#include "net/url_request/url_request_status.h"
#include "net/url_request/url_request_test_util.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "url/gurl.h"
using base::ASCIIToUTF16;
namespace net {
namespace {
class MockProxyResolverFactory : public ProxyResolverFactory {
public:
MockProxyResolverFactory()
: ProxyResolverFactory(false), resolver_(nullptr) {}
int CreateProxyResolver(
const scoped_refptr<ProxyResolverScriptData>& pac_script,
scoped_ptr<ProxyResolver>* resolver,
const CompletionCallback& callback,
scoped_ptr<Request>* request) override {
EXPECT_FALSE(resolver_);
scoped_ptr<MockAsyncProxyResolver> owned_resolver(
new MockAsyncProxyResolver());
resolver_ = owned_resolver.get();
*resolver = std::move(owned_resolver);
return OK;
}
MockAsyncProxyResolver* resolver() { return resolver_; }
private:
MockAsyncProxyResolver* resolver_;
};
} // namespace
class FtpTestURLRequestContext : public TestURLRequestContext {
public:
FtpTestURLRequestContext(ClientSocketFactory* socket_factory,
scoped_ptr<ProxyService> proxy_service,
NetworkDelegate* network_delegate,
FtpTransactionFactory* ftp_transaction_factory)
: TestURLRequestContext(true),
ftp_protocol_handler_(new FtpProtocolHandler(ftp_transaction_factory)) {
set_client_socket_factory(socket_factory);
context_storage_.set_proxy_service(std::move(proxy_service));
set_network_delegate(network_delegate);
scoped_ptr<URLRequestJobFactoryImpl> job_factory =
make_scoped_ptr(new URLRequestJobFactoryImpl);
job_factory->SetProtocolHandler("ftp",
make_scoped_ptr(ftp_protocol_handler_));
context_storage_.set_job_factory(std::move(job_factory));
Init();
}
FtpAuthCache* GetFtpAuthCache() {
return ftp_protocol_handler_->ftp_auth_cache_.get();
}
void set_proxy_service(scoped_ptr<ProxyService> proxy_service) {
context_storage_.set_proxy_service(std::move(proxy_service));
}
private:
FtpProtocolHandler* ftp_protocol_handler_;
};
namespace {
class SimpleProxyConfigService : public ProxyConfigService {
public:
SimpleProxyConfigService() {
// Any FTP requests that ever go through HTTP paths are proxied requests.
config_.proxy_rules().ParseFromString("ftp=localhost");
}
void AddObserver(Observer* observer) override { observer_ = observer; }
void RemoveObserver(Observer* observer) override {
if (observer_ == observer) {
observer_ = NULL;
}
}
ConfigAvailability GetLatestProxyConfig(ProxyConfig* config) override {
*config = config_;
return CONFIG_VALID;
}
void IncrementConfigId() {
config_.set_id(config_.id() + 1);
observer_->OnProxyConfigChanged(config_, ProxyConfigService::CONFIG_VALID);
}
private:
ProxyConfig config_;
Observer* observer_;
};
// Inherit from URLRequestFtpJob to expose the priority and some
// other hidden functions.
class TestURLRequestFtpJob : public URLRequestFtpJob {
public:
TestURLRequestFtpJob(URLRequest* request,
FtpTransactionFactory* ftp_factory,
FtpAuthCache* ftp_auth_cache)
: URLRequestFtpJob(request, NULL, ftp_factory, ftp_auth_cache) {}
~TestURLRequestFtpJob() override {}
using URLRequestFtpJob::SetPriority;
using URLRequestFtpJob::Start;
using URLRequestFtpJob::Kill;
using URLRequestFtpJob::priority;
protected:
};
class MockFtpTransactionFactory : public FtpTransactionFactory {
public:
scoped_ptr<FtpTransaction> CreateTransaction() override {
return scoped_ptr<FtpTransaction>();
}
void Suspend(bool suspend) override {}
};
// Fixture for priority-related tests. Priority matters when there is
// an HTTP proxy.
class URLRequestFtpJobPriorityTest : public testing::Test {
protected:
URLRequestFtpJobPriorityTest()
: proxy_service_(make_scoped_ptr(new SimpleProxyConfigService),
NULL,
NULL),
req_(context_.CreateRequest(GURL("ftp://ftp.example.com"),
DEFAULT_PRIORITY,
&delegate_)) {
context_.set_proxy_service(&proxy_service_);
context_.set_http_transaction_factory(&network_layer_);
}
ProxyService proxy_service_;
MockNetworkLayer network_layer_;
MockFtpTransactionFactory ftp_factory_;
FtpAuthCache ftp_auth_cache_;
TestURLRequestContext context_;
TestDelegate delegate_;
scoped_ptr<URLRequest> req_;
};
// Make sure that SetPriority actually sets the URLRequestFtpJob's
// priority, both before and after start.
TEST_F(URLRequestFtpJobPriorityTest, SetPriorityBasic) {
scoped_ptr<TestURLRequestFtpJob> job(
new TestURLRequestFtpJob(req_.get(), &ftp_factory_, &ftp_auth_cache_));
EXPECT_EQ(DEFAULT_PRIORITY, job->priority());
job->SetPriority(LOWEST);
EXPECT_EQ(LOWEST, job->priority());
job->SetPriority(LOW);
EXPECT_EQ(LOW, job->priority());
job->Start();
EXPECT_EQ(LOW, job->priority());
job->SetPriority(MEDIUM);
EXPECT_EQ(MEDIUM, job->priority());
}
// Make sure that URLRequestFtpJob passes on its priority to its
// transaction on start.
TEST_F(URLRequestFtpJobPriorityTest, SetTransactionPriorityOnStart) {
scoped_ptr<TestURLRequestFtpJob> job(
new TestURLRequestFtpJob(req_.get(), &ftp_factory_, &ftp_auth_cache_));
job->SetPriority(LOW);
EXPECT_FALSE(network_layer_.last_transaction());
job->Start();
ASSERT_TRUE(network_layer_.last_transaction());
EXPECT_EQ(LOW, network_layer_.last_transaction()->priority());
}
// Make sure that URLRequestFtpJob passes on its priority updates to
// its transaction.
TEST_F(URLRequestFtpJobPriorityTest, SetTransactionPriority) {
scoped_ptr<TestURLRequestFtpJob> job(
new TestURLRequestFtpJob(req_.get(), &ftp_factory_, &ftp_auth_cache_));
job->SetPriority(LOW);
job->Start();
ASSERT_TRUE(network_layer_.last_transaction());
EXPECT_EQ(LOW, network_layer_.last_transaction()->priority());
job->SetPriority(HIGHEST);
EXPECT_EQ(HIGHEST, network_layer_.last_transaction()->priority());
}
// Make sure that URLRequestFtpJob passes on its priority updates to
// newly-created transactions after the first one.
TEST_F(URLRequestFtpJobPriorityTest, SetSubsequentTransactionPriority) {
scoped_ptr<TestURLRequestFtpJob> job(
new TestURLRequestFtpJob(req_.get(), &ftp_factory_, &ftp_auth_cache_));
job->Start();
job->SetPriority(LOW);
ASSERT_TRUE(network_layer_.last_transaction());
EXPECT_EQ(LOW, network_layer_.last_transaction()->priority());
job->Kill();
network_layer_.ClearLastTransaction();
// Creates a second transaction.
job->Start();
ASSERT_TRUE(network_layer_.last_transaction());
EXPECT_EQ(LOW, network_layer_.last_transaction()->priority());
}
class URLRequestFtpJobTest : public testing::Test {
public:
URLRequestFtpJobTest()
: request_context_(&socket_factory_,
make_scoped_ptr(new ProxyService(
make_scoped_ptr(new SimpleProxyConfigService),
NULL,
NULL)),
&network_delegate_,
&ftp_transaction_factory_) {}
~URLRequestFtpJobTest() override {
// Clean up any remaining tasks that mess up unrelated tests.
base::RunLoop().RunUntilIdle();
}
void AddSocket(MockRead* reads, size_t reads_size,
MockWrite* writes, size_t writes_size) {
scoped_ptr<SequencedSocketData> socket_data(make_scoped_ptr(
new SequencedSocketData(reads, reads_size, writes, writes_size)));
socket_data->set_connect_data(MockConnect(SYNCHRONOUS, OK));
socket_factory_.AddSocketDataProvider(socket_data.get());
socket_data_.push_back(std::move(socket_data));
}
FtpTestURLRequestContext* request_context() { return &request_context_; }
TestNetworkDelegate* network_delegate() { return &network_delegate_; }
private:
std::vector<scoped_ptr<SequencedSocketData>> socket_data_;
MockClientSocketFactory socket_factory_;
TestNetworkDelegate network_delegate_;
MockFtpTransactionFactory ftp_transaction_factory_;
FtpTestURLRequestContext request_context_;
};
TEST_F(URLRequestFtpJobTest, FtpProxyRequest) {
MockWrite writes[] = {
MockWrite(ASYNC, 0, "GET ftp://ftp.example.com/ HTTP/1.1\r\n"
"Host: ftp.example.com\r\n"
"Proxy-Connection: keep-alive\r\n\r\n"),
};
MockRead reads[] = {
MockRead(ASYNC, 1, "HTTP/1.1 200 OK\r\n"),
MockRead(ASYNC, 2, "Content-Length: 9\r\n\r\n"),
MockRead(ASYNC, 3, "test.html"),
};
AddSocket(reads, arraysize(reads), writes, arraysize(writes));
TestDelegate request_delegate;
scoped_ptr<URLRequest> url_request(request_context()->CreateRequest(
GURL("ftp://ftp.example.com/"), DEFAULT_PRIORITY, &request_delegate));
url_request->Start();
ASSERT_TRUE(url_request->is_pending());
// The TestDelegate will by default quit the message loop on completion.
base::RunLoop().Run();
EXPECT_TRUE(url_request->status().is_success());
EXPECT_TRUE(url_request->proxy_server().Equals(
HostPortPair::FromString("localhost:80")));
EXPECT_EQ(1, network_delegate()->completed_requests());
EXPECT_EQ(0, network_delegate()->error_count());
EXPECT_FALSE(request_delegate.auth_required_called());
EXPECT_EQ("test.html", request_delegate.data_received());
}
// Regression test for http://crbug.com/237526.
TEST_F(URLRequestFtpJobTest, FtpProxyRequestOrphanJob) {
scoped_ptr<MockProxyResolverFactory> owned_resolver_factory(
new MockProxyResolverFactory());
MockProxyResolverFactory* resolver_factory = owned_resolver_factory.get();
// Use a PAC URL so that URLRequestFtpJob's |pac_request_| field is non-NULL.
request_context()->set_proxy_service(make_scoped_ptr(new ProxyService(
make_scoped_ptr(new ProxyConfigServiceFixed(
ProxyConfig::CreateFromCustomPacURL(GURL("http://foo")))),
std::move(owned_resolver_factory), nullptr)));
TestDelegate request_delegate;
scoped_ptr<URLRequest> url_request(request_context()->CreateRequest(
GURL("ftp://ftp.example.com/"), DEFAULT_PRIORITY, &request_delegate));
url_request->Start();
// Verify PAC request is in progress.
EXPECT_EQ(net::LoadState::LOAD_STATE_RESOLVING_PROXY_FOR_URL,
url_request->GetLoadState().state);
EXPECT_EQ(1u, resolver_factory->resolver()->pending_requests().size());
EXPECT_EQ(0u, resolver_factory->resolver()->cancelled_requests().size());
// Destroying the request should cancel the PAC request.
url_request.reset();
EXPECT_EQ(0u, resolver_factory->resolver()->pending_requests().size());
EXPECT_EQ(1u, resolver_factory->resolver()->cancelled_requests().size());
}
// Make sure PAC requests are cancelled on request cancellation. Requests can
// hang around a bit without being deleted in the cancellation case, so the
// above test is not sufficient.
TEST_F(URLRequestFtpJobTest, FtpProxyRequestCancelRequest) {
scoped_ptr<MockProxyResolverFactory> owned_resolver_factory(
new MockProxyResolverFactory());
MockProxyResolverFactory* resolver_factory = owned_resolver_factory.get();
// Use a PAC URL so that URLRequestFtpJob's |pac_request_| field is non-NULL.
request_context()->set_proxy_service(make_scoped_ptr(new ProxyService(
make_scoped_ptr(new ProxyConfigServiceFixed(
ProxyConfig::CreateFromCustomPacURL(GURL("http://foo")))),
std::move(owned_resolver_factory), nullptr)));
TestDelegate request_delegate;
scoped_ptr<URLRequest> url_request(request_context()->CreateRequest(
GURL("ftp://ftp.example.com/"), DEFAULT_PRIORITY, &request_delegate));
// Verify PAC request is in progress.
url_request->Start();
EXPECT_EQ(net::LoadState::LOAD_STATE_RESOLVING_PROXY_FOR_URL,
url_request->GetLoadState().state);
EXPECT_EQ(1u, resolver_factory->resolver()->pending_requests().size());
EXPECT_EQ(0u, resolver_factory->resolver()->cancelled_requests().size());
// Cancelling the request should cancel the PAC request.
url_request->Cancel();
EXPECT_EQ(net::LoadState::LOAD_STATE_IDLE, url_request->GetLoadState().state);
EXPECT_EQ(0u, resolver_factory->resolver()->pending_requests().size());
EXPECT_EQ(1u, resolver_factory->resolver()->cancelled_requests().size());
}
TEST_F(URLRequestFtpJobTest, FtpProxyRequestNeedProxyAuthNoCredentials) {
MockWrite writes[] = {
MockWrite(ASYNC, 0, "GET ftp://ftp.example.com/ HTTP/1.1\r\n"
"Host: ftp.example.com\r\n"
"Proxy-Connection: keep-alive\r\n\r\n"),
};
MockRead reads[] = {
// No credentials.
MockRead(ASYNC, 1, "HTTP/1.1 407 Proxy Authentication Required\r\n"),
MockRead(ASYNC, 2, "Proxy-Authenticate: Basic "
"realm=\"MyRealm1\"\r\n"),
MockRead(ASYNC, 3, "Content-Length: 9\r\n\r\n"),
MockRead(ASYNC, 4, "test.html"),
};
AddSocket(reads, arraysize(reads), writes, arraysize(writes));
TestDelegate request_delegate;
scoped_ptr<URLRequest> url_request(request_context()->CreateRequest(
GURL("ftp://ftp.example.com/"), DEFAULT_PRIORITY, &request_delegate));
url_request->Start();
ASSERT_TRUE(url_request->is_pending());
// The TestDelegate will by default quit the message loop on completion.
base::RunLoop().Run();
EXPECT_TRUE(url_request->status().is_success());
EXPECT_TRUE(url_request->proxy_server().Equals(
HostPortPair::FromString("localhost:80")));
EXPECT_EQ(1, network_delegate()->completed_requests());
EXPECT_EQ(0, network_delegate()->error_count());
EXPECT_TRUE(request_delegate.auth_required_called());
EXPECT_EQ("test.html", request_delegate.data_received());
}
TEST_F(URLRequestFtpJobTest, FtpProxyRequestNeedProxyAuthWithCredentials) {
MockWrite writes[] = {
MockWrite(ASYNC, 0, "GET ftp://ftp.example.com/ HTTP/1.1\r\n"
"Host: ftp.example.com\r\n"
"Proxy-Connection: keep-alive\r\n\r\n"),
MockWrite(ASYNC, 5, "GET ftp://ftp.example.com/ HTTP/1.1\r\n"
"Host: ftp.example.com\r\n"
"Proxy-Connection: keep-alive\r\n"
"Proxy-Authorization: Basic bXl1c2VyOm15cGFzcw==\r\n\r\n"),
};
MockRead reads[] = {
// No credentials.
MockRead(ASYNC, 1, "HTTP/1.1 407 Proxy Authentication Required\r\n"),
MockRead(ASYNC, 2, "Proxy-Authenticate: Basic "
"realm=\"MyRealm1\"\r\n"),
MockRead(ASYNC, 3, "Content-Length: 9\r\n\r\n"),
MockRead(ASYNC, 4, "test.html"),
// Second response.
MockRead(ASYNC, 6, "HTTP/1.1 200 OK\r\n"),
MockRead(ASYNC, 7, "Content-Length: 10\r\n\r\n"),
MockRead(ASYNC, 8, "test2.html"),
};
AddSocket(reads, arraysize(reads), writes, arraysize(writes));
TestDelegate request_delegate;
request_delegate.set_credentials(
AuthCredentials(ASCIIToUTF16("myuser"), ASCIIToUTF16("mypass")));
scoped_ptr<URLRequest> url_request(request_context()->CreateRequest(
GURL("ftp://ftp.example.com/"), DEFAULT_PRIORITY, &request_delegate));
url_request->Start();
ASSERT_TRUE(url_request->is_pending());
// The TestDelegate will by default quit the message loop on completion.
base::RunLoop().Run();
EXPECT_TRUE(url_request->status().is_success());
EXPECT_EQ(1, network_delegate()->completed_requests());
EXPECT_EQ(0, network_delegate()->error_count());
EXPECT_TRUE(request_delegate.auth_required_called());
EXPECT_EQ("test2.html", request_delegate.data_received());
}
TEST_F(URLRequestFtpJobTest, FtpProxyRequestNeedServerAuthNoCredentials) {
MockWrite writes[] = {
MockWrite(ASYNC, 0, "GET ftp://ftp.example.com/ HTTP/1.1\r\n"
"Host: ftp.example.com\r\n"
"Proxy-Connection: keep-alive\r\n\r\n"),
};
MockRead reads[] = {
// No credentials.
MockRead(ASYNC, 1, "HTTP/1.1 401 Unauthorized\r\n"),
MockRead(ASYNC, 2, "WWW-Authenticate: Basic "
"realm=\"MyRealm1\"\r\n"),
MockRead(ASYNC, 3, "Content-Length: 9\r\n\r\n"),
MockRead(ASYNC, 4, "test.html"),
};
AddSocket(reads, arraysize(reads), writes, arraysize(writes));
TestDelegate request_delegate;
scoped_ptr<URLRequest> url_request(request_context()->CreateRequest(
GURL("ftp://ftp.example.com/"), DEFAULT_PRIORITY, &request_delegate));
url_request->Start();
ASSERT_TRUE(url_request->is_pending());
// The TestDelegate will by default quit the message loop on completion.
base::RunLoop().Run();
EXPECT_TRUE(url_request->status().is_success());
EXPECT_EQ(1, network_delegate()->completed_requests());
EXPECT_EQ(0, network_delegate()->error_count());
EXPECT_TRUE(request_delegate.auth_required_called());
EXPECT_EQ("test.html", request_delegate.data_received());
}
TEST_F(URLRequestFtpJobTest, FtpProxyRequestNeedServerAuthWithCredentials) {
MockWrite writes[] = {
MockWrite(ASYNC, 0, "GET ftp://ftp.example.com/ HTTP/1.1\r\n"
"Host: ftp.example.com\r\n"
"Proxy-Connection: keep-alive\r\n\r\n"),
MockWrite(ASYNC, 5, "GET ftp://ftp.example.com/ HTTP/1.1\r\n"
"Host: ftp.example.com\r\n"
"Proxy-Connection: keep-alive\r\n"
"Authorization: Basic bXl1c2VyOm15cGFzcw==\r\n\r\n"),
};
MockRead reads[] = {
// No credentials.
MockRead(ASYNC, 1, "HTTP/1.1 401 Unauthorized\r\n"),
MockRead(ASYNC, 2, "WWW-Authenticate: Basic "
"realm=\"MyRealm1\"\r\n"),
MockRead(ASYNC, 3, "Content-Length: 9\r\n\r\n"),
MockRead(ASYNC, 4, "test.html"),
// Second response.
MockRead(ASYNC, 6, "HTTP/1.1 200 OK\r\n"),
MockRead(ASYNC, 7, "Content-Length: 10\r\n\r\n"),
MockRead(ASYNC, 8, "test2.html"),
};
AddSocket(reads, arraysize(reads), writes, arraysize(writes));
TestDelegate request_delegate;
request_delegate.set_credentials(
AuthCredentials(ASCIIToUTF16("myuser"), ASCIIToUTF16("mypass")));
scoped_ptr<URLRequest> url_request(request_context()->CreateRequest(
GURL("ftp://ftp.example.com/"), DEFAULT_PRIORITY, &request_delegate));
url_request->Start();
ASSERT_TRUE(url_request->is_pending());
// The TestDelegate will by default quit the message loop on completion.
base::RunLoop().Run();
EXPECT_TRUE(url_request->status().is_success());
EXPECT_EQ(1, network_delegate()->completed_requests());
EXPECT_EQ(0, network_delegate()->error_count());
EXPECT_TRUE(request_delegate.auth_required_called());
EXPECT_EQ("test2.html", request_delegate.data_received());
}
TEST_F(URLRequestFtpJobTest, FtpProxyRequestNeedProxyAndServerAuth) {
MockWrite writes[] = {
MockWrite(ASYNC, 0, "GET ftp://ftp.example.com/ HTTP/1.1\r\n"
"Host: ftp.example.com\r\n"
"Proxy-Connection: keep-alive\r\n\r\n"),
MockWrite(ASYNC, 5, "GET ftp://ftp.example.com/ HTTP/1.1\r\n"
"Host: ftp.example.com\r\n"
"Proxy-Connection: keep-alive\r\n"
"Proxy-Authorization: Basic "
"cHJveHl1c2VyOnByb3h5cGFzcw==\r\n\r\n"),
MockWrite(ASYNC, 10, "GET ftp://ftp.example.com/ HTTP/1.1\r\n"
"Host: ftp.example.com\r\n"
"Proxy-Connection: keep-alive\r\n"
"Proxy-Authorization: Basic "
"cHJveHl1c2VyOnByb3h5cGFzcw==\r\n"
"Authorization: Basic bXl1c2VyOm15cGFzcw==\r\n\r\n"),
};
MockRead reads[] = {
// No credentials.
MockRead(ASYNC, 1, "HTTP/1.1 407 Proxy Authentication Required\r\n"),
MockRead(ASYNC, 2, "Proxy-Authenticate: Basic "
"realm=\"MyRealm1\"\r\n"),
MockRead(ASYNC, 3, "Content-Length: 9\r\n\r\n"),
MockRead(ASYNC, 4, "test.html"),
// Second response.
MockRead(ASYNC, 6, "HTTP/1.1 401 Unauthorized\r\n"),
MockRead(ASYNC, 7, "WWW-Authenticate: Basic "
"realm=\"MyRealm1\"\r\n"),
MockRead(ASYNC, 8, "Content-Length: 9\r\n\r\n"),
MockRead(ASYNC, 9, "test.html"),
// Third response.
MockRead(ASYNC, 11, "HTTP/1.1 200 OK\r\n"),
MockRead(ASYNC, 12, "Content-Length: 10\r\n\r\n"),
MockRead(ASYNC, 13, "test2.html"),
};
AddSocket(reads, arraysize(reads), writes, arraysize(writes));
GURL url("ftp://ftp.example.com");
// Make sure cached FTP credentials are not used for proxy authentication.
request_context()->GetFtpAuthCache()->Add(
url.GetOrigin(),
AuthCredentials(ASCIIToUTF16("userdonotuse"),
ASCIIToUTF16("passworddonotuse")));
TestDelegate request_delegate;
request_delegate.set_quit_on_auth_required(true);
scoped_ptr<URLRequest> url_request(request_context()->CreateRequest(
url, DEFAULT_PRIORITY, &request_delegate));
url_request->Start();
ASSERT_TRUE(url_request->is_pending());
// Run until proxy auth is requested.
base::RunLoop().Run();
ASSERT_TRUE(request_delegate.auth_required_called());
EXPECT_EQ(0, network_delegate()->completed_requests());
EXPECT_EQ(0, network_delegate()->error_count());
url_request->SetAuth(
AuthCredentials(ASCIIToUTF16("proxyuser"), ASCIIToUTF16("proxypass")));
// Run until server auth is requested.
base::RunLoop().Run();
EXPECT_TRUE(url_request->status().is_success());
EXPECT_EQ(0, network_delegate()->completed_requests());
EXPECT_EQ(0, network_delegate()->error_count());
url_request->SetAuth(
AuthCredentials(ASCIIToUTF16("myuser"), ASCIIToUTF16("mypass")));
// The TestDelegate will by default quit the message loop on completion.
base::RunLoop().Run();
EXPECT_TRUE(url_request->status().is_success());
EXPECT_EQ(1, network_delegate()->completed_requests());
EXPECT_EQ(0, network_delegate()->error_count());
EXPECT_TRUE(request_delegate.auth_required_called());
EXPECT_EQ("test2.html", request_delegate.data_received());
}
TEST_F(URLRequestFtpJobTest, FtpProxyRequestDoNotSaveCookies) {
MockWrite writes[] = {
MockWrite(ASYNC, 0, "GET ftp://ftp.example.com/ HTTP/1.1\r\n"
"Host: ftp.example.com\r\n"
"Proxy-Connection: keep-alive\r\n\r\n"),
};
MockRead reads[] = {
MockRead(ASYNC, 1, "HTTP/1.1 200 OK\r\n"),
MockRead(ASYNC, 2, "Content-Length: 9\r\n"),
MockRead(ASYNC, 3, "Set-Cookie: name=value\r\n\r\n"),
MockRead(ASYNC, 4, "test.html"),
};
AddSocket(reads, arraysize(reads), writes, arraysize(writes));
TestDelegate request_delegate;
scoped_ptr<URLRequest> url_request(request_context()->CreateRequest(
GURL("ftp://ftp.example.com/"), DEFAULT_PRIORITY, &request_delegate));
url_request->Start();
ASSERT_TRUE(url_request->is_pending());
// The TestDelegate will by default quit the message loop on completion.
base::RunLoop().Run();
EXPECT_TRUE(url_request->status().is_success());
EXPECT_EQ(1, network_delegate()->completed_requests());
EXPECT_EQ(0, network_delegate()->error_count());
// Make sure we do not accept cookies.
EXPECT_EQ(0, network_delegate()->set_cookie_count());
EXPECT_FALSE(request_delegate.auth_required_called());
EXPECT_EQ("test.html", request_delegate.data_received());
}
TEST_F(URLRequestFtpJobTest, FtpProxyRequestDoNotFollowRedirects) {
MockWrite writes[] = {
MockWrite(SYNCHRONOUS, 0, "GET ftp://ftp.example.com/ HTTP/1.1\r\n"
"Host: ftp.example.com\r\n"
"Proxy-Connection: keep-alive\r\n\r\n"),
};
MockRead reads[] = {
MockRead(SYNCHRONOUS, 1, "HTTP/1.1 302 Found\r\n"),
MockRead(ASYNC, 2, "Location: http://other.example.com/\r\n\r\n"),
};
AddSocket(reads, arraysize(reads), writes, arraysize(writes));
TestDelegate request_delegate;
scoped_ptr<URLRequest> url_request(request_context()->CreateRequest(
GURL("ftp://ftp.example.com/"), DEFAULT_PRIORITY, &request_delegate));
url_request->Start();
EXPECT_TRUE(url_request->is_pending());
// The TestDelegate will by default quit the message loop on completion.
base::RunLoop().Run();
EXPECT_EQ(1, network_delegate()->completed_requests());
EXPECT_EQ(1, network_delegate()->error_count());
EXPECT_FALSE(url_request->status().is_success());
EXPECT_EQ(ERR_UNSAFE_REDIRECT, url_request->status().error());
}
// We should re-use socket for requests using the same scheme, host, and port.
TEST_F(URLRequestFtpJobTest, FtpProxyRequestReuseSocket) {
MockWrite writes[] = {
MockWrite(ASYNC, 0, "GET ftp://ftp.example.com/first HTTP/1.1\r\n"
"Host: ftp.example.com\r\n"
"Proxy-Connection: keep-alive\r\n\r\n"),
MockWrite(ASYNC, 4, "GET ftp://ftp.example.com/second HTTP/1.1\r\n"
"Host: ftp.example.com\r\n"
"Proxy-Connection: keep-alive\r\n\r\n"),
};
MockRead reads[] = {
MockRead(ASYNC, 1, "HTTP/1.1 200 OK\r\n"),
MockRead(ASYNC, 2, "Content-Length: 10\r\n\r\n"),
MockRead(ASYNC, 3, "test1.html"),
MockRead(ASYNC, 5, "HTTP/1.1 200 OK\r\n"),
MockRead(ASYNC, 6, "Content-Length: 10\r\n\r\n"),
MockRead(ASYNC, 7, "test2.html"),
};
AddSocket(reads, arraysize(reads), writes, arraysize(writes));
TestDelegate request_delegate1;
scoped_ptr<URLRequest> url_request1(
request_context()->CreateRequest(GURL("ftp://ftp.example.com/first"),
DEFAULT_PRIORITY, &request_delegate1));
url_request1->Start();
ASSERT_TRUE(url_request1->is_pending());
// The TestDelegate will by default quit the message loop on completion.
base::RunLoop().Run();
EXPECT_TRUE(url_request1->status().is_success());
EXPECT_TRUE(url_request1->proxy_server().Equals(
HostPortPair::FromString("localhost:80")));
EXPECT_EQ(1, network_delegate()->completed_requests());
EXPECT_EQ(0, network_delegate()->error_count());
EXPECT_FALSE(request_delegate1.auth_required_called());
EXPECT_EQ("test1.html", request_delegate1.data_received());
TestDelegate request_delegate2;
scoped_ptr<URLRequest> url_request2(
request_context()->CreateRequest(GURL("ftp://ftp.example.com/second"),
DEFAULT_PRIORITY, &request_delegate2));
url_request2->Start();
ASSERT_TRUE(url_request2->is_pending());
// The TestDelegate will by default quit the message loop on completion.
base::RunLoop().Run();
EXPECT_TRUE(url_request2->status().is_success());
EXPECT_EQ(2, network_delegate()->completed_requests());
EXPECT_EQ(0, network_delegate()->error_count());
EXPECT_FALSE(request_delegate2.auth_required_called());
EXPECT_EQ("test2.html", request_delegate2.data_received());
}
// We should not re-use socket when there are two requests to the same host,
// but one is FTP and the other is HTTP.
TEST_F(URLRequestFtpJobTest, FtpProxyRequestDoNotReuseSocket) {
MockWrite writes1[] = {
MockWrite(ASYNC, 0, "GET ftp://ftp.example.com/first HTTP/1.1\r\n"
"Host: ftp.example.com\r\n"
"Proxy-Connection: keep-alive\r\n\r\n"),
};
MockWrite writes2[] = {
MockWrite(ASYNC, 0, "GET /second HTTP/1.1\r\n"
"Host: ftp.example.com\r\n"
"Connection: keep-alive\r\n"
"User-Agent:\r\n"
"Accept-Encoding: gzip, deflate\r\n"
"Accept-Language: en-us,fr\r\n\r\n"),
};
MockRead reads1[] = {
MockRead(ASYNC, 1, "HTTP/1.1 200 OK\r\n"),
MockRead(ASYNC, 2, "Content-Length: 10\r\n\r\n"),
MockRead(ASYNC, 3, "test1.html"),
};
MockRead reads2[] = {
MockRead(ASYNC, 1, "HTTP/1.1 200 OK\r\n"),
MockRead(ASYNC, 2, "Content-Length: 10\r\n\r\n"),
MockRead(ASYNC, 3, "test2.html"),
};
AddSocket(reads1, arraysize(reads1), writes1, arraysize(writes1));
AddSocket(reads2, arraysize(reads2), writes2, arraysize(writes2));
TestDelegate request_delegate1;
scoped_ptr<URLRequest> url_request1(
request_context()->CreateRequest(GURL("ftp://ftp.example.com/first"),
DEFAULT_PRIORITY, &request_delegate1));
url_request1->Start();
ASSERT_TRUE(url_request1->is_pending());
// The TestDelegate will by default quit the message loop on completion.
base::RunLoop().Run();
EXPECT_TRUE(url_request1->status().is_success());
EXPECT_EQ(1, network_delegate()->completed_requests());
EXPECT_EQ(0, network_delegate()->error_count());
EXPECT_FALSE(request_delegate1.auth_required_called());
EXPECT_EQ("test1.html", request_delegate1.data_received());
TestDelegate request_delegate2;
scoped_ptr<URLRequest> url_request2(
request_context()->CreateRequest(GURL("http://ftp.example.com/second"),
DEFAULT_PRIORITY, &request_delegate2));
url_request2->Start();
ASSERT_TRUE(url_request2->is_pending());
// The TestDelegate will by default quit the message loop on completion.
base::RunLoop().Run();
EXPECT_TRUE(url_request2->status().is_success());
EXPECT_EQ(2, network_delegate()->completed_requests());
EXPECT_EQ(0, network_delegate()->error_count());
EXPECT_FALSE(request_delegate2.auth_required_called());
EXPECT_EQ("test2.html", request_delegate2.data_received());
}
} // namespace
} // namespace net
|