Chromium Code Reviews| 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/proxy_config_service.h" | 13 #include "net/proxy/proxy_config_service.h" |
| 13 #include "net/socket/socket_test_util.h" | 14 #include "net/socket/socket_test_util.h" |
| 15 #include "net/url_request/ftp_protocol_handler.h" | |
| 14 #include "net/url_request/url_request.h" | 16 #include "net/url_request/url_request.h" |
| 15 #include "net/url_request/url_request_context.h" | 17 #include "net/url_request/url_request_context.h" |
| 18 #include "net/url_request/url_request_job_factory_impl.h" | |
| 16 #include "net/url_request/url_request_status.h" | 19 #include "net/url_request/url_request_status.h" |
| 17 #include "net/url_request/url_request_test_util.h" | 20 #include "net/url_request/url_request_test_util.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_.get(); | |
| 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 { | |
| 104 public: | |
| 105 FtpTransaction* CreateTransaction() { | |
|
mmenke1
2013/04/29 21:17:23
virtual OVERRIDE
| |
| 106 return NULL; | |
| 107 } | |
| 108 | |
| 109 void Suspend(bool suspend) {} | |
|
mmenke1
2013/04/29 21:17:23
virtual OVERRIDE
| |
| 110 }; | |
| 111 | |
| 75 // Fixture for priority-related tests. Priority matters when there is | 112 // Fixture for priority-related tests. Priority matters when there is |
| 76 // an HTTP proxy. | 113 // an HTTP proxy. |
| 77 class URLRequestFtpJobPriorityTest : public testing::Test { | 114 class URLRequestFtpJobPriorityTest : public testing::Test { |
| 78 protected: | 115 protected: |
| 79 URLRequestFtpJobPriorityTest() | 116 URLRequestFtpJobPriorityTest() |
| 80 : proxy_service_(new SimpleProxyConfigService, NULL, NULL), | 117 : proxy_service_(new SimpleProxyConfigService, NULL, NULL), |
| 81 req_(GURL("ftp://ftp.example.com"), &delegate_, &context_, NULL) { | 118 req_(GURL("ftp://ftp.example.com"), &delegate_, &context_, NULL) { |
| 82 context_.set_proxy_service(&proxy_service_); | 119 context_.set_proxy_service(&proxy_service_); |
| 83 context_.set_http_transaction_factory(&network_layer_); | 120 context_.set_http_transaction_factory(&network_layer_); |
| 84 } | 121 } |
| 85 | 122 |
| 86 ProxyService proxy_service_; | 123 ProxyService proxy_service_; |
| 87 MockNetworkLayer network_layer_; | 124 MockNetworkLayer network_layer_; |
| 125 MockFtpTransactionFactory ftp_factory_; | |
| 126 FtpAuthCache ftp_auth_cache_; | |
| 88 TestURLRequestContext context_; | 127 TestURLRequestContext context_; |
| 89 TestDelegate delegate_; | 128 TestDelegate delegate_; |
| 90 TestURLRequest req_; | 129 TestURLRequest req_; |
| 91 }; | 130 }; |
| 92 | 131 |
| 93 // Make sure that SetPriority actually sets the URLRequestFtpJob's | 132 // Make sure that SetPriority actually sets the URLRequestFtpJob's |
| 94 // priority, both before and after start. | 133 // priority, both before and after start. |
| 95 TEST_F(URLRequestFtpJobPriorityTest, SetPriorityBasic) { | 134 TEST_F(URLRequestFtpJobPriorityTest, SetPriorityBasic) { |
| 96 scoped_refptr<TestURLRequestFtpJob> job(new TestURLRequestFtpJob(&req_)); | 135 scoped_refptr<TestURLRequestFtpJob> job(new TestURLRequestFtpJob( |
| 136 &req_, &ftp_factory_, &ftp_auth_cache_)); | |
| 97 EXPECT_EQ(DEFAULT_PRIORITY, job->priority()); | 137 EXPECT_EQ(DEFAULT_PRIORITY, job->priority()); |
| 98 | 138 |
| 99 job->SetPriority(LOWEST); | 139 job->SetPriority(LOWEST); |
| 100 EXPECT_EQ(LOWEST, job->priority()); | 140 EXPECT_EQ(LOWEST, job->priority()); |
| 101 | 141 |
| 102 job->SetPriority(LOW); | 142 job->SetPriority(LOW); |
| 103 EXPECT_EQ(LOW, job->priority()); | 143 EXPECT_EQ(LOW, job->priority()); |
| 104 | 144 |
| 105 job->Start(); | 145 job->Start(); |
| 106 EXPECT_EQ(LOW, job->priority()); | 146 EXPECT_EQ(LOW, job->priority()); |
| 107 | 147 |
| 108 job->SetPriority(MEDIUM); | 148 job->SetPriority(MEDIUM); |
| 109 EXPECT_EQ(MEDIUM, job->priority()); | 149 EXPECT_EQ(MEDIUM, job->priority()); |
| 110 } | 150 } |
| 111 | 151 |
| 112 // Make sure that URLRequestFtpJob passes on its priority to its | 152 // Make sure that URLRequestFtpJob passes on its priority to its |
| 113 // transaction on start. | 153 // transaction on start. |
| 114 TEST_F(URLRequestFtpJobPriorityTest, SetTransactionPriorityOnStart) { | 154 TEST_F(URLRequestFtpJobPriorityTest, SetTransactionPriorityOnStart) { |
| 115 scoped_refptr<TestURLRequestFtpJob> job(new TestURLRequestFtpJob(&req_)); | 155 scoped_refptr<TestURLRequestFtpJob> job(new TestURLRequestFtpJob( |
| 156 &req_, &ftp_factory_, &ftp_auth_cache_)); | |
| 116 job->SetPriority(LOW); | 157 job->SetPriority(LOW); |
| 117 | 158 |
| 118 EXPECT_FALSE(network_layer_.last_transaction()); | 159 EXPECT_FALSE(network_layer_.last_transaction()); |
| 119 | 160 |
| 120 job->Start(); | 161 job->Start(); |
| 121 | 162 |
| 122 ASSERT_TRUE(network_layer_.last_transaction()); | 163 ASSERT_TRUE(network_layer_.last_transaction()); |
| 123 EXPECT_EQ(LOW, network_layer_.last_transaction()->priority()); | 164 EXPECT_EQ(LOW, network_layer_.last_transaction()->priority()); |
| 124 } | 165 } |
| 125 | 166 |
| 126 // Make sure that URLRequestFtpJob passes on its priority updates to | 167 // Make sure that URLRequestFtpJob passes on its priority updates to |
| 127 // its transaction. | 168 // its transaction. |
| 128 TEST_F(URLRequestFtpJobPriorityTest, SetTransactionPriority) { | 169 TEST_F(URLRequestFtpJobPriorityTest, SetTransactionPriority) { |
| 129 scoped_refptr<TestURLRequestFtpJob> job(new TestURLRequestFtpJob(&req_)); | 170 scoped_refptr<TestURLRequestFtpJob> job(new TestURLRequestFtpJob( |
| 171 &req_, &ftp_factory_, &ftp_auth_cache_)); | |
| 130 job->SetPriority(LOW); | 172 job->SetPriority(LOW); |
| 131 job->Start(); | 173 job->Start(); |
| 132 ASSERT_TRUE(network_layer_.last_transaction()); | 174 ASSERT_TRUE(network_layer_.last_transaction()); |
| 133 EXPECT_EQ(LOW, network_layer_.last_transaction()->priority()); | 175 EXPECT_EQ(LOW, network_layer_.last_transaction()->priority()); |
| 134 | 176 |
| 135 job->SetPriority(HIGHEST); | 177 job->SetPriority(HIGHEST); |
| 136 EXPECT_EQ(HIGHEST, network_layer_.last_transaction()->priority()); | 178 EXPECT_EQ(HIGHEST, network_layer_.last_transaction()->priority()); |
| 137 } | 179 } |
| 138 | 180 |
| 139 // Make sure that URLRequestFtpJob passes on its priority updates to | 181 // Make sure that URLRequestFtpJob passes on its priority updates to |
| 140 // newly-created transactions after the first one. | 182 // newly-created transactions after the first one. |
| 141 TEST_F(URLRequestFtpJobPriorityTest, SetSubsequentTransactionPriority) { | 183 TEST_F(URLRequestFtpJobPriorityTest, SetSubsequentTransactionPriority) { |
| 142 scoped_refptr<TestURLRequestFtpJob> job(new TestURLRequestFtpJob(&req_)); | 184 scoped_refptr<TestURLRequestFtpJob> job(new TestURLRequestFtpJob( |
| 185 &req_, &ftp_factory_, &ftp_auth_cache_)); | |
| 143 job->Start(); | 186 job->Start(); |
| 144 | 187 |
| 145 job->SetPriority(LOW); | 188 job->SetPriority(LOW); |
| 146 ASSERT_TRUE(network_layer_.last_transaction()); | 189 ASSERT_TRUE(network_layer_.last_transaction()); |
| 147 EXPECT_EQ(LOW, network_layer_.last_transaction()->priority()); | 190 EXPECT_EQ(LOW, network_layer_.last_transaction()->priority()); |
| 148 | 191 |
| 149 job->Kill(); | 192 job->Kill(); |
| 150 network_layer_.ClearLastTransaction(); | 193 network_layer_.ClearLastTransaction(); |
| 151 | 194 |
| 152 // Creates a second transaction. | 195 // Creates a second transaction. |
| 153 job->Start(); | 196 job->Start(); |
| 154 ASSERT_TRUE(network_layer_.last_transaction()); | 197 ASSERT_TRUE(network_layer_.last_transaction()); |
| 155 EXPECT_EQ(LOW, network_layer_.last_transaction()->priority()); | 198 EXPECT_EQ(LOW, network_layer_.last_transaction()->priority()); |
| 156 } | 199 } |
| 157 | 200 |
| 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 { | 201 class URLRequestFtpJobTest : public testing::Test { |
| 172 public: | 202 public: |
| 173 URLRequestFtpJobTest() | 203 URLRequestFtpJobTest() |
| 174 : proxy_service_(new ProxyService( | 204 : proxy_service_(new ProxyService( |
| 175 new SimpleProxyConfigService, NULL, NULL)), | 205 new SimpleProxyConfigService, NULL, NULL)), |
| 176 request_context_(&socket_factory_, | 206 request_context_(&socket_factory_, |
| 177 proxy_service_, | 207 proxy_service_, |
| 178 &network_delegate_) { | 208 &network_delegate_, |
| 209 &ftp_transaction_factory_) { | |
| 179 } | 210 } |
| 180 | 211 |
| 181 virtual ~URLRequestFtpJobTest() { | 212 virtual ~URLRequestFtpJobTest() { |
| 182 // Clean up any remaining tasks that mess up unrelated tests. | 213 // Clean up any remaining tasks that mess up unrelated tests. |
| 183 base::RunLoop run_loop; | 214 base::RunLoop run_loop; |
| 184 run_loop.RunUntilIdle(); | 215 run_loop.RunUntilIdle(); |
| 185 } | 216 } |
| 186 | 217 |
| 187 void AddSocket(MockRead* reads, size_t reads_size, | 218 void AddSocket(MockRead* reads, size_t reads_size, |
| 188 MockWrite* writes, size_t writes_size) { | 219 MockWrite* writes, size_t writes_size) { |
| 189 DeterministicSocketData* socket_data = new DeterministicSocketData( | 220 DeterministicSocketData* socket_data = new DeterministicSocketData( |
| 190 reads, reads_size, writes, writes_size); | 221 reads, reads_size, writes, writes_size); |
| 191 socket_data->set_connect_data(MockConnect(SYNCHRONOUS, OK)); | 222 socket_data->set_connect_data(MockConnect(SYNCHRONOUS, OK)); |
| 192 socket_data->StopAfter(reads_size + writes_size - 1); | 223 socket_data->StopAfter(reads_size + writes_size - 1); |
| 193 socket_factory_.AddSocketDataProvider(socket_data); | 224 socket_factory_.AddSocketDataProvider(socket_data); |
| 194 | 225 |
| 195 socket_data_.push_back(socket_data); | 226 socket_data_.push_back(socket_data); |
| 196 } | 227 } |
| 197 | 228 |
| 198 URLRequestContext* request_context() { return &request_context_; } | 229 FtpTestURLRequestContext* request_context() { return &request_context_; } |
| 199 TestNetworkDelegate* network_delegate() { return &network_delegate_; } | 230 TestNetworkDelegate* network_delegate() { return &network_delegate_; } |
| 200 DeterministicSocketData* socket_data(size_t index) { | 231 DeterministicSocketData* socket_data(size_t index) { |
| 201 return socket_data_[index]; | 232 return socket_data_[index]; |
| 202 } | 233 } |
| 203 | 234 |
| 204 private: | 235 private: |
| 205 ScopedVector<DeterministicSocketData> socket_data_; | 236 ScopedVector<DeterministicSocketData> socket_data_; |
| 206 DeterministicMockClientSocketFactory socket_factory_; | 237 DeterministicMockClientSocketFactory socket_factory_; |
| 207 TestNetworkDelegate network_delegate_; | 238 TestNetworkDelegate network_delegate_; |
| 239 MockFtpTransactionFactory ftp_transaction_factory_; | |
| 208 | 240 |
| 209 // Owned by |request_context_|: | 241 // Owned by |request_context_|: |
| 210 ProxyService* proxy_service_; | 242 ProxyService* proxy_service_; |
| 211 | 243 |
| 212 FtpTestURLRequestContext request_context_; | 244 FtpTestURLRequestContext request_context_; |
| 213 }; | 245 }; |
| 214 | 246 |
| 215 TEST_F(URLRequestFtpJobTest, FtpProxyRequest) { | 247 TEST_F(URLRequestFtpJobTest, FtpProxyRequest) { |
| 216 MockWrite writes[] = { | 248 MockWrite writes[] = { |
| 217 MockWrite(ASYNC, 0, "GET ftp://ftp.example.com/ HTTP/1.1\r\n" | 249 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"), | 464 MockRead(ASYNC, 11, "HTTP/1.1 200 OK\r\n"), |
| 433 MockRead(ASYNC, 12, "Content-Length: 10\r\n\r\n"), | 465 MockRead(ASYNC, 12, "Content-Length: 10\r\n\r\n"), |
| 434 MockRead(ASYNC, 13, "test2.html"), | 466 MockRead(ASYNC, 13, "test2.html"), |
| 435 }; | 467 }; |
| 436 | 468 |
| 437 AddSocket(reads, arraysize(reads), writes, arraysize(writes)); | 469 AddSocket(reads, arraysize(reads), writes, arraysize(writes)); |
| 438 | 470 |
| 439 GURL url("ftp://ftp.example.com"); | 471 GURL url("ftp://ftp.example.com"); |
| 440 | 472 |
| 441 // Make sure cached FTP credentials are not used for proxy authentication. | 473 // Make sure cached FTP credentials are not used for proxy authentication. |
| 442 request_context()->ftp_auth_cache()->Add( | 474 request_context()->GetFtpAuthCache()->Add( |
| 443 url.GetOrigin(), | 475 url.GetOrigin(), |
| 444 AuthCredentials(ASCIIToUTF16("userdonotuse"), | 476 AuthCredentials(ASCIIToUTF16("userdonotuse"), |
| 445 ASCIIToUTF16("passworddonotuse"))); | 477 ASCIIToUTF16("passworddonotuse"))); |
| 446 | 478 |
| 447 TestDelegate request_delegate; | 479 TestDelegate request_delegate; |
| 448 request_delegate.set_credentials( | 480 request_delegate.set_credentials( |
| 449 AuthCredentials(ASCIIToUTF16("proxyuser"), ASCIIToUTF16("proxypass"))); | 481 AuthCredentials(ASCIIToUTF16("proxyuser"), ASCIIToUTF16("proxypass"))); |
| 450 URLRequest url_request(url, | 482 URLRequest url_request(url, |
| 451 &request_delegate, | 483 &request_delegate, |
| 452 request_context(), | 484 request_context(), |
| (...skipping 194 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 647 EXPECT_TRUE(url_request2.status().is_success()); | 679 EXPECT_TRUE(url_request2.status().is_success()); |
| 648 EXPECT_EQ(2, network_delegate()->completed_requests()); | 680 EXPECT_EQ(2, network_delegate()->completed_requests()); |
| 649 EXPECT_EQ(0, network_delegate()->error_count()); | 681 EXPECT_EQ(0, network_delegate()->error_count()); |
| 650 EXPECT_FALSE(request_delegate2.auth_required_called()); | 682 EXPECT_FALSE(request_delegate2.auth_required_called()); |
| 651 EXPECT_EQ("test2.html", request_delegate2.data_received()); | 683 EXPECT_EQ("test2.html", request_delegate2.data_received()); |
| 652 } | 684 } |
| 653 | 685 |
| 654 } // namespace | 686 } // namespace |
| 655 | 687 |
| 656 } // namespace net | 688 } // namespace net |
| OLD | NEW |