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