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/url_request/url_request_ftp_job.h" | 5 #include "net/url_request/url_request_ftp_job.h" |
6 | 6 |
7 #include "base/memory/ref_counted.h" | 7 #include "base/memory/ref_counted.h" |
8 #include "base/memory/scoped_vector.h" | 8 #include "base/memory/scoped_vector.h" |
9 #include "base/run_loop.h" | 9 #include "base/run_loop.h" |
10 #include "googleurl/src/gurl.h" | 10 #include "googleurl/src/gurl.h" |
11 #include "net/http/http_transaction_unittest.h" | 11 #include "net/http/http_transaction_unittest.h" |
12 #include "net/proxy/proxy_config_service.h" | 12 #include "net/proxy/proxy_config_service.h" |
13 #include "net/socket/socket_test_util.h" | 13 #include "net/socket/socket_test_util.h" |
14 #include "net/url_request/ftp_protocol_handler.h" | |
14 #include "net/url_request/url_request.h" | 15 #include "net/url_request/url_request.h" |
15 #include "net/url_request/url_request_context.h" | 16 #include "net/url_request/url_request_context.h" |
17 #include "net/url_request/url_request_job_factory_impl.h" | |
16 #include "net/url_request/url_request_status.h" | 18 #include "net/url_request/url_request_status.h" |
17 #include "net/url_request/url_request_test_util.h" | 19 #include "net/url_request/url_request_test_util.h" |
20 #include "testing/gmock/include/gmock/gmock.h" | |
18 #include "testing/gtest/include/gtest/gtest.h" | 21 #include "testing/gtest/include/gtest/gtest.h" |
19 | 22 |
20 namespace net { | 23 namespace net { |
21 | 24 |
25 class FtpTestURLRequestContext : public TestURLRequestContext { | |
26 public: | |
27 FtpTestURLRequestContext(ClientSocketFactory* socket_factory, | |
28 ProxyService* proxy_service, | |
29 NetworkDelegate* network_delegate, | |
30 FtpTransactionFactory* ftp_transaction_factory) | |
31 : TestURLRequestContext(true), | |
32 ftp_protocol_handler_(new FtpProtocolHandler(ftp_transaction_factory)) { | |
33 set_client_socket_factory(socket_factory); | |
34 context_storage_.set_proxy_service(proxy_service); | |
35 set_network_delegate(network_delegate); | |
36 URLRequestJobFactoryImpl* job_factory = new URLRequestJobFactoryImpl; | |
37 job_factory->SetProtocolHandler("ftp", ftp_protocol_handler_); | |
38 context_storage_.set_job_factory(job_factory); | |
39 Init(); | |
40 } | |
41 | |
42 FtpAuthCache* GetFtpAuthCache() { | |
43 return &ftp_protocol_handler_->ftp_auth_cache_; | |
44 } | |
45 | |
46 private: | |
47 FtpProtocolHandler* ftp_protocol_handler_; | |
48 }; | |
49 | |
22 namespace { | 50 namespace { |
23 | 51 |
24 class SimpleProxyConfigService : public ProxyConfigService { | 52 class SimpleProxyConfigService : public ProxyConfigService { |
25 public: | 53 public: |
26 SimpleProxyConfigService() { | 54 SimpleProxyConfigService() { |
27 // Any FTP requests that ever go through HTTP paths are proxied requests. | 55 // Any FTP requests that ever go through HTTP paths are proxied requests. |
28 config_.proxy_rules().ParseFromString("ftp=localhost"); | 56 config_.proxy_rules().ParseFromString("ftp=localhost"); |
29 } | 57 } |
30 | 58 |
31 virtual void AddObserver(Observer* observer) OVERRIDE { | 59 virtual void AddObserver(Observer* observer) OVERRIDE { |
(...skipping 19 matching lines...) Expand all Loading... | |
51 | 79 |
52 private: | 80 private: |
53 ProxyConfig config_; | 81 ProxyConfig config_; |
54 Observer* observer_; | 82 Observer* observer_; |
55 }; | 83 }; |
56 | 84 |
57 // Inherit from URLRequestFtpJob to expose the priority and some | 85 // Inherit from URLRequestFtpJob to expose the priority and some |
58 // other hidden functions. | 86 // other hidden functions. |
59 class TestURLRequestFtpJob : public URLRequestFtpJob { | 87 class TestURLRequestFtpJob : public URLRequestFtpJob { |
60 public: | 88 public: |
61 explicit TestURLRequestFtpJob(URLRequest* request) | 89 TestURLRequestFtpJob(URLRequest* request, |
62 : URLRequestFtpJob(request, NULL, | 90 FtpTransactionFactory* ftp_factory, |
63 request->context()->ftp_transaction_factory(), | 91 FtpAuthCache* ftp_auth_cache) |
64 request->context()->ftp_auth_cache()) {} | 92 : URLRequestFtpJob(request, NULL, ftp_factory, ftp_auth_cache) {} |
65 | 93 |
66 using URLRequestFtpJob::SetPriority; | 94 using URLRequestFtpJob::SetPriority; |
67 using URLRequestFtpJob::Start; | 95 using URLRequestFtpJob::Start; |
68 using URLRequestFtpJob::Kill; | 96 using URLRequestFtpJob::Kill; |
69 using URLRequestFtpJob::priority; | 97 using URLRequestFtpJob::priority; |
70 | 98 |
71 protected: | 99 protected: |
72 virtual ~TestURLRequestFtpJob() {} | 100 virtual ~TestURLRequestFtpJob() {} |
73 }; | 101 }; |
74 | 102 |
103 class MockFtpTransactionFactory : public FtpTransactionFactory { | |
Paweł Hajdan Jr.
2013/04/29 18:45:26
Please don't use gmock for FTP unit tests.
| |
104 public: | |
105 MOCK_METHOD0(CreateTransaction, FtpTransaction*()); | |
106 MOCK_METHOD1(Suspend, void(bool suspend)); | |
107 }; | |
108 | |
75 // Fixture for priority-related tests. Priority matters when there is | 109 // Fixture for priority-related tests. Priority matters when there is |
76 // an HTTP proxy. | 110 // an HTTP proxy. |
77 class URLRequestFtpJobPriorityTest : public testing::Test { | 111 class URLRequestFtpJobPriorityTest : public testing::Test { |
78 protected: | 112 protected: |
79 URLRequestFtpJobPriorityTest() | 113 URLRequestFtpJobPriorityTest() |
80 : proxy_service_(new SimpleProxyConfigService, NULL, NULL), | 114 : proxy_service_(new SimpleProxyConfigService, NULL, NULL), |
81 req_(GURL("ftp://ftp.example.com"), &delegate_, &context_, NULL) { | 115 req_(GURL("ftp://ftp.example.com"), &delegate_, &context_, NULL) { |
82 context_.set_proxy_service(&proxy_service_); | 116 context_.set_proxy_service(&proxy_service_); |
83 context_.set_http_transaction_factory(&network_layer_); | 117 context_.set_http_transaction_factory(&network_layer_); |
84 } | 118 } |
85 | 119 |
86 ProxyService proxy_service_; | 120 ProxyService proxy_service_; |
87 MockNetworkLayer network_layer_; | 121 MockNetworkLayer network_layer_; |
122 MockFtpTransactionFactory ftp_factory_; | |
123 FtpAuthCache ftp_auth_cache_; | |
88 TestURLRequestContext context_; | 124 TestURLRequestContext context_; |
89 TestDelegate delegate_; | 125 TestDelegate delegate_; |
90 TestURLRequest req_; | 126 TestURLRequest req_; |
91 }; | 127 }; |
92 | 128 |
93 // Make sure that SetPriority actually sets the URLRequestFtpJob's | 129 // Make sure that SetPriority actually sets the URLRequestFtpJob's |
94 // priority, both before and after start. | 130 // priority, both before and after start. |
95 TEST_F(URLRequestFtpJobPriorityTest, SetPriorityBasic) { | 131 TEST_F(URLRequestFtpJobPriorityTest, SetPriorityBasic) { |
96 scoped_refptr<TestURLRequestFtpJob> job(new TestURLRequestFtpJob(&req_)); | 132 scoped_refptr<TestURLRequestFtpJob> job(new TestURLRequestFtpJob( |
133 &req_, &ftp_factory_, &ftp_auth_cache_)); | |
97 EXPECT_EQ(DEFAULT_PRIORITY, job->priority()); | 134 EXPECT_EQ(DEFAULT_PRIORITY, job->priority()); |
98 | 135 |
99 job->SetPriority(LOWEST); | 136 job->SetPriority(LOWEST); |
100 EXPECT_EQ(LOWEST, job->priority()); | 137 EXPECT_EQ(LOWEST, job->priority()); |
101 | 138 |
102 job->SetPriority(LOW); | 139 job->SetPriority(LOW); |
103 EXPECT_EQ(LOW, job->priority()); | 140 EXPECT_EQ(LOW, job->priority()); |
104 | 141 |
105 job->Start(); | 142 job->Start(); |
106 EXPECT_EQ(LOW, job->priority()); | 143 EXPECT_EQ(LOW, job->priority()); |
107 | 144 |
108 job->SetPriority(MEDIUM); | 145 job->SetPriority(MEDIUM); |
109 EXPECT_EQ(MEDIUM, job->priority()); | 146 EXPECT_EQ(MEDIUM, job->priority()); |
110 } | 147 } |
111 | 148 |
112 // Make sure that URLRequestFtpJob passes on its priority to its | 149 // Make sure that URLRequestFtpJob passes on its priority to its |
113 // transaction on start. | 150 // transaction on start. |
114 TEST_F(URLRequestFtpJobPriorityTest, SetTransactionPriorityOnStart) { | 151 TEST_F(URLRequestFtpJobPriorityTest, SetTransactionPriorityOnStart) { |
115 scoped_refptr<TestURLRequestFtpJob> job(new TestURLRequestFtpJob(&req_)); | 152 scoped_refptr<TestURLRequestFtpJob> job(new TestURLRequestFtpJob( |
153 &req_, &ftp_factory_, &ftp_auth_cache_)); | |
116 job->SetPriority(LOW); | 154 job->SetPriority(LOW); |
117 | 155 |
118 EXPECT_FALSE(network_layer_.last_transaction()); | 156 EXPECT_FALSE(network_layer_.last_transaction()); |
119 | 157 |
120 job->Start(); | 158 job->Start(); |
121 | 159 |
122 ASSERT_TRUE(network_layer_.last_transaction()); | 160 ASSERT_TRUE(network_layer_.last_transaction()); |
123 EXPECT_EQ(LOW, network_layer_.last_transaction()->priority()); | 161 EXPECT_EQ(LOW, network_layer_.last_transaction()->priority()); |
124 } | 162 } |
125 | 163 |
126 // Make sure that URLRequestFtpJob passes on its priority updates to | 164 // Make sure that URLRequestFtpJob passes on its priority updates to |
127 // its transaction. | 165 // its transaction. |
128 TEST_F(URLRequestFtpJobPriorityTest, SetTransactionPriority) { | 166 TEST_F(URLRequestFtpJobPriorityTest, SetTransactionPriority) { |
129 scoped_refptr<TestURLRequestFtpJob> job(new TestURLRequestFtpJob(&req_)); | 167 scoped_refptr<TestURLRequestFtpJob> job(new TestURLRequestFtpJob( |
168 &req_, &ftp_factory_, &ftp_auth_cache_)); | |
130 job->SetPriority(LOW); | 169 job->SetPriority(LOW); |
131 job->Start(); | 170 job->Start(); |
132 ASSERT_TRUE(network_layer_.last_transaction()); | 171 ASSERT_TRUE(network_layer_.last_transaction()); |
133 EXPECT_EQ(LOW, network_layer_.last_transaction()->priority()); | 172 EXPECT_EQ(LOW, network_layer_.last_transaction()->priority()); |
134 | 173 |
135 job->SetPriority(HIGHEST); | 174 job->SetPriority(HIGHEST); |
136 EXPECT_EQ(HIGHEST, network_layer_.last_transaction()->priority()); | 175 EXPECT_EQ(HIGHEST, network_layer_.last_transaction()->priority()); |
137 } | 176 } |
138 | 177 |
139 // Make sure that URLRequestFtpJob passes on its priority updates to | 178 // Make sure that URLRequestFtpJob passes on its priority updates to |
140 // newly-created transactions after the first one. | 179 // newly-created transactions after the first one. |
141 TEST_F(URLRequestFtpJobPriorityTest, SetSubsequentTransactionPriority) { | 180 TEST_F(URLRequestFtpJobPriorityTest, SetSubsequentTransactionPriority) { |
142 scoped_refptr<TestURLRequestFtpJob> job(new TestURLRequestFtpJob(&req_)); | 181 scoped_refptr<TestURLRequestFtpJob> job(new TestURLRequestFtpJob( |
182 &req_, &ftp_factory_, &ftp_auth_cache_)); | |
143 job->Start(); | 183 job->Start(); |
144 | 184 |
145 job->SetPriority(LOW); | 185 job->SetPriority(LOW); |
146 ASSERT_TRUE(network_layer_.last_transaction()); | 186 ASSERT_TRUE(network_layer_.last_transaction()); |
147 EXPECT_EQ(LOW, network_layer_.last_transaction()->priority()); | 187 EXPECT_EQ(LOW, network_layer_.last_transaction()->priority()); |
148 | 188 |
149 job->Kill(); | 189 job->Kill(); |
150 network_layer_.ClearLastTransaction(); | 190 network_layer_.ClearLastTransaction(); |
151 | 191 |
152 // Creates a second transaction. | 192 // Creates a second transaction. |
153 job->Start(); | 193 job->Start(); |
154 ASSERT_TRUE(network_layer_.last_transaction()); | 194 ASSERT_TRUE(network_layer_.last_transaction()); |
155 EXPECT_EQ(LOW, network_layer_.last_transaction()->priority()); | 195 EXPECT_EQ(LOW, network_layer_.last_transaction()->priority()); |
156 } | 196 } |
157 | 197 |
158 class FtpTestURLRequestContext : public TestURLRequestContext { | |
159 public: | |
160 FtpTestURLRequestContext(ClientSocketFactory* socket_factory, | |
161 ProxyService* proxy_service, | |
162 NetworkDelegate* network_delegate) | |
163 : TestURLRequestContext(true) { | |
164 set_client_socket_factory(socket_factory); | |
165 context_storage_.set_proxy_service(proxy_service); | |
166 set_network_delegate(network_delegate); | |
167 Init(); | |
168 } | |
169 }; | |
170 | |
171 class URLRequestFtpJobTest : public testing::Test { | 198 class URLRequestFtpJobTest : public testing::Test { |
172 public: | 199 public: |
173 URLRequestFtpJobTest() | 200 URLRequestFtpJobTest() |
174 : proxy_service_(new ProxyService( | 201 : proxy_service_(new ProxyService( |
175 new SimpleProxyConfigService, NULL, NULL)), | 202 new SimpleProxyConfigService, NULL, NULL)), |
176 request_context_(&socket_factory_, | 203 request_context_(&socket_factory_, |
177 proxy_service_, | 204 proxy_service_, |
178 &network_delegate_) { | 205 &network_delegate_, |
206 &ftp_transaction_factory_) { | |
179 } | 207 } |
180 | 208 |
181 virtual ~URLRequestFtpJobTest() { | 209 virtual ~URLRequestFtpJobTest() { |
182 // Clean up any remaining tasks that mess up unrelated tests. | 210 // Clean up any remaining tasks that mess up unrelated tests. |
183 base::RunLoop run_loop; | 211 base::RunLoop run_loop; |
184 run_loop.RunUntilIdle(); | 212 run_loop.RunUntilIdle(); |
185 } | 213 } |
186 | 214 |
187 void AddSocket(MockRead* reads, size_t reads_size, | 215 void AddSocket(MockRead* reads, size_t reads_size, |
188 MockWrite* writes, size_t writes_size) { | 216 MockWrite* writes, size_t writes_size) { |
189 DeterministicSocketData* socket_data = new DeterministicSocketData( | 217 DeterministicSocketData* socket_data = new DeterministicSocketData( |
190 reads, reads_size, writes, writes_size); | 218 reads, reads_size, writes, writes_size); |
191 socket_data->set_connect_data(MockConnect(SYNCHRONOUS, OK)); | 219 socket_data->set_connect_data(MockConnect(SYNCHRONOUS, OK)); |
192 socket_data->StopAfter(reads_size + writes_size - 1); | 220 socket_data->StopAfter(reads_size + writes_size - 1); |
193 socket_factory_.AddSocketDataProvider(socket_data); | 221 socket_factory_.AddSocketDataProvider(socket_data); |
194 | 222 |
195 socket_data_.push_back(socket_data); | 223 socket_data_.push_back(socket_data); |
196 } | 224 } |
197 | 225 |
198 URLRequestContext* request_context() { return &request_context_; } | 226 FtpTestURLRequestContext* request_context() { return &request_context_; } |
199 TestNetworkDelegate* network_delegate() { return &network_delegate_; } | 227 TestNetworkDelegate* network_delegate() { return &network_delegate_; } |
200 DeterministicSocketData* socket_data(size_t index) { | 228 DeterministicSocketData* socket_data(size_t index) { |
201 return socket_data_[index]; | 229 return socket_data_[index]; |
202 } | 230 } |
203 | 231 |
204 private: | 232 private: |
205 ScopedVector<DeterministicSocketData> socket_data_; | 233 ScopedVector<DeterministicSocketData> socket_data_; |
206 DeterministicMockClientSocketFactory socket_factory_; | 234 DeterministicMockClientSocketFactory socket_factory_; |
207 TestNetworkDelegate network_delegate_; | 235 TestNetworkDelegate network_delegate_; |
236 ::testing::StrictMock<MockFtpTransactionFactory> ftp_transaction_factory_; | |
208 | 237 |
209 // Owned by |request_context_|: | 238 // Owned by |request_context_|: |
210 ProxyService* proxy_service_; | 239 ProxyService* proxy_service_; |
211 | 240 |
212 FtpTestURLRequestContext request_context_; | 241 FtpTestURLRequestContext request_context_; |
213 }; | 242 }; |
214 | 243 |
215 TEST_F(URLRequestFtpJobTest, FtpProxyRequest) { | 244 TEST_F(URLRequestFtpJobTest, FtpProxyRequest) { |
216 MockWrite writes[] = { | 245 MockWrite writes[] = { |
217 MockWrite(ASYNC, 0, "GET ftp://ftp.example.com/ HTTP/1.1\r\n" | 246 MockWrite(ASYNC, 0, "GET ftp://ftp.example.com/ HTTP/1.1\r\n" |
(...skipping 214 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
432 MockRead(ASYNC, 11, "HTTP/1.1 200 OK\r\n"), | 461 MockRead(ASYNC, 11, "HTTP/1.1 200 OK\r\n"), |
433 MockRead(ASYNC, 12, "Content-Length: 10\r\n\r\n"), | 462 MockRead(ASYNC, 12, "Content-Length: 10\r\n\r\n"), |
434 MockRead(ASYNC, 13, "test2.html"), | 463 MockRead(ASYNC, 13, "test2.html"), |
435 }; | 464 }; |
436 | 465 |
437 AddSocket(reads, arraysize(reads), writes, arraysize(writes)); | 466 AddSocket(reads, arraysize(reads), writes, arraysize(writes)); |
438 | 467 |
439 GURL url("ftp://ftp.example.com"); | 468 GURL url("ftp://ftp.example.com"); |
440 | 469 |
441 // Make sure cached FTP credentials are not used for proxy authentication. | 470 // Make sure cached FTP credentials are not used for proxy authentication. |
442 request_context()->ftp_auth_cache()->Add( | 471 request_context()->GetFtpAuthCache()->Add( |
443 url.GetOrigin(), | 472 url.GetOrigin(), |
444 AuthCredentials(ASCIIToUTF16("userdonotuse"), | 473 AuthCredentials(ASCIIToUTF16("userdonotuse"), |
445 ASCIIToUTF16("passworddonotuse"))); | 474 ASCIIToUTF16("passworddonotuse"))); |
446 | 475 |
447 TestDelegate request_delegate; | 476 TestDelegate request_delegate; |
448 request_delegate.set_credentials( | 477 request_delegate.set_credentials( |
449 AuthCredentials(ASCIIToUTF16("proxyuser"), ASCIIToUTF16("proxypass"))); | 478 AuthCredentials(ASCIIToUTF16("proxyuser"), ASCIIToUTF16("proxypass"))); |
450 URLRequest url_request(url, | 479 URLRequest url_request(url, |
451 &request_delegate, | 480 &request_delegate, |
452 request_context(), | 481 request_context(), |
(...skipping 194 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
647 EXPECT_TRUE(url_request2.status().is_success()); | 676 EXPECT_TRUE(url_request2.status().is_success()); |
648 EXPECT_EQ(2, network_delegate()->completed_requests()); | 677 EXPECT_EQ(2, network_delegate()->completed_requests()); |
649 EXPECT_EQ(0, network_delegate()->error_count()); | 678 EXPECT_EQ(0, network_delegate()->error_count()); |
650 EXPECT_FALSE(request_delegate2.auth_required_called()); | 679 EXPECT_FALSE(request_delegate2.auth_required_called()); |
651 EXPECT_EQ("test2.html", request_delegate2.data_received()); | 680 EXPECT_EQ("test2.html", request_delegate2.data_received()); |
652 } | 681 } |
653 | 682 |
654 } // namespace | 683 } // namespace |
655 | 684 |
656 } // namespace net | 685 } // namespace net |
OLD | NEW |