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" |
(...skipping 224 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
235 ASSERT_TRUE(url_request.is_pending()); | 235 ASSERT_TRUE(url_request.is_pending()); |
236 socket_data(0)->RunFor(4); | 236 socket_data(0)->RunFor(4); |
237 | 237 |
238 EXPECT_TRUE(url_request.status().is_success()); | 238 EXPECT_TRUE(url_request.status().is_success()); |
239 EXPECT_EQ(1, network_delegate()->completed_requests()); | 239 EXPECT_EQ(1, network_delegate()->completed_requests()); |
240 EXPECT_EQ(0, network_delegate()->error_count()); | 240 EXPECT_EQ(0, network_delegate()->error_count()); |
241 EXPECT_FALSE(request_delegate.auth_required_called()); | 241 EXPECT_FALSE(request_delegate.auth_required_called()); |
242 EXPECT_EQ("test.html", request_delegate.data_received()); | 242 EXPECT_EQ("test.html", request_delegate.data_received()); |
243 } | 243 } |
244 | 244 |
245 TEST_F(URLRequestFtpJobTest, FtpProxyRequestNeedAuth) { | 245 TEST_F(URLRequestFtpJobTest, FtpProxyRequestNeedProxyAuthNoCredentials) { |
246 MockWrite writes[] = { | 246 MockWrite writes[] = { |
247 MockWrite(ASYNC, 0, "GET ftp://ftp.example.com/ HTTP/1.1\r\n" | 247 MockWrite(ASYNC, 0, "GET ftp://ftp.example.com/ HTTP/1.1\r\n" |
248 "Host: ftp.example.com\r\n" | 248 "Host: ftp.example.com\r\n" |
249 "Proxy-Connection: keep-alive\r\n\r\n"), | 249 "Proxy-Connection: keep-alive\r\n\r\n"), |
250 }; | 250 }; |
251 MockRead reads[] = { | 251 MockRead reads[] = { |
252 // No credentials. | 252 // No credentials. |
253 MockRead(ASYNC, 1, "HTTP/1.1 407 Proxy Authentication Required\r\n"), | 253 MockRead(ASYNC, 1, "HTTP/1.1 407 Proxy Authentication Required\r\n"), |
254 MockRead(ASYNC, 2, "Proxy-Authenticate: Basic " | 254 MockRead(ASYNC, 2, "Proxy-Authenticate: Basic " |
255 "realm=\"MyRealm1\"\r\n"), | 255 "realm=\"MyRealm1\"\r\n"), |
256 MockRead(ASYNC, 3, "Content-Length: 9\r\n\r\n"), | 256 MockRead(ASYNC, 3, "Content-Length: 9\r\n\r\n"), |
257 MockRead(ASYNC, 4, "test.html"), | 257 MockRead(ASYNC, 4, "test.html"), |
258 }; | 258 }; |
259 | 259 |
260 AddSocket(reads, arraysize(reads), writes, arraysize(writes)); | 260 AddSocket(reads, arraysize(reads), writes, arraysize(writes)); |
261 | 261 |
262 TestDelegate request_delegate; | 262 TestDelegate request_delegate; |
263 URLRequest url_request(GURL("ftp://ftp.example.com/"), | 263 URLRequest url_request(GURL("ftp://ftp.example.com/"), |
264 &request_delegate, | 264 &request_delegate, |
265 request_context(), | 265 request_context(), |
266 network_delegate()); | 266 network_delegate()); |
267 url_request.Start(); | 267 url_request.Start(); |
268 ASSERT_TRUE(url_request.is_pending()); | 268 ASSERT_TRUE(url_request.is_pending()); |
269 socket_data(0)->RunFor(5); | 269 socket_data(0)->RunFor(5); |
270 | 270 |
271 EXPECT_TRUE(url_request.status().is_success()); | 271 EXPECT_TRUE(url_request.status().is_success()); |
272 EXPECT_EQ(1, network_delegate()->completed_requests()); | 272 EXPECT_EQ(1, network_delegate()->completed_requests()); |
273 EXPECT_EQ(0, network_delegate()->error_count()); | 273 EXPECT_EQ(0, network_delegate()->error_count()); |
274 EXPECT_FALSE(request_delegate.auth_required_called()); | 274 EXPECT_TRUE(request_delegate.auth_required_called()); |
275 EXPECT_EQ("test.html", request_delegate.data_received()); | 275 EXPECT_EQ("test.html", request_delegate.data_received()); |
276 } | 276 } |
277 | 277 |
| 278 TEST_F(URLRequestFtpJobTest, FtpProxyRequestNeedProxyAuthWithCredentials) { |
| 279 MockWrite writes[] = { |
| 280 MockWrite(ASYNC, 0, "GET ftp://ftp.example.com/ HTTP/1.1\r\n" |
| 281 "Host: ftp.example.com\r\n" |
| 282 "Proxy-Connection: keep-alive\r\n\r\n"), |
| 283 MockWrite(ASYNC, 5, "GET ftp://ftp.example.com/ HTTP/1.1\r\n" |
| 284 "Host: ftp.example.com\r\n" |
| 285 "Proxy-Connection: keep-alive\r\n" |
| 286 "Proxy-Authorization: Basic bXl1c2VyOm15cGFzcw==\r\n\r\n"), |
| 287 }; |
| 288 MockRead reads[] = { |
| 289 // No credentials. |
| 290 MockRead(ASYNC, 1, "HTTP/1.1 407 Proxy Authentication Required\r\n"), |
| 291 MockRead(ASYNC, 2, "Proxy-Authenticate: Basic " |
| 292 "realm=\"MyRealm1\"\r\n"), |
| 293 MockRead(ASYNC, 3, "Content-Length: 9\r\n\r\n"), |
| 294 MockRead(ASYNC, 4, "test.html"), |
| 295 |
| 296 // Second response. |
| 297 MockRead(ASYNC, 6, "HTTP/1.1 200 OK\r\n"), |
| 298 MockRead(ASYNC, 7, "Content-Length: 10\r\n\r\n"), |
| 299 MockRead(ASYNC, 8, "test2.html"), |
| 300 }; |
| 301 |
| 302 AddSocket(reads, arraysize(reads), writes, arraysize(writes)); |
| 303 |
| 304 TestDelegate request_delegate; |
| 305 request_delegate.set_credentials( |
| 306 AuthCredentials(ASCIIToUTF16("myuser"), ASCIIToUTF16("mypass"))); |
| 307 URLRequest url_request(GURL("ftp://ftp.example.com/"), |
| 308 &request_delegate, |
| 309 request_context(), |
| 310 network_delegate()); |
| 311 url_request.Start(); |
| 312 ASSERT_TRUE(url_request.is_pending()); |
| 313 socket_data(0)->RunFor(9); |
| 314 |
| 315 EXPECT_TRUE(url_request.status().is_success()); |
| 316 EXPECT_EQ(1, network_delegate()->completed_requests()); |
| 317 EXPECT_EQ(0, network_delegate()->error_count()); |
| 318 EXPECT_TRUE(request_delegate.auth_required_called()); |
| 319 EXPECT_EQ("test2.html", request_delegate.data_received()); |
| 320 } |
| 321 |
| 322 TEST_F(URLRequestFtpJobTest, FtpProxyRequestNeedServerAuthNoCredentials) { |
| 323 MockWrite writes[] = { |
| 324 MockWrite(ASYNC, 0, "GET ftp://ftp.example.com/ HTTP/1.1\r\n" |
| 325 "Host: ftp.example.com\r\n" |
| 326 "Proxy-Connection: keep-alive\r\n\r\n"), |
| 327 }; |
| 328 MockRead reads[] = { |
| 329 // No credentials. |
| 330 MockRead(ASYNC, 1, "HTTP/1.1 401 Unauthorized\r\n"), |
| 331 MockRead(ASYNC, 2, "WWW-Authenticate: Basic " |
| 332 "realm=\"MyRealm1\"\r\n"), |
| 333 MockRead(ASYNC, 3, "Content-Length: 9\r\n\r\n"), |
| 334 MockRead(ASYNC, 4, "test.html"), |
| 335 }; |
| 336 |
| 337 AddSocket(reads, arraysize(reads), writes, arraysize(writes)); |
| 338 |
| 339 TestDelegate request_delegate; |
| 340 URLRequest url_request(GURL("ftp://ftp.example.com/"), |
| 341 &request_delegate, |
| 342 request_context(), |
| 343 network_delegate()); |
| 344 url_request.Start(); |
| 345 ASSERT_TRUE(url_request.is_pending()); |
| 346 socket_data(0)->RunFor(5); |
| 347 |
| 348 EXPECT_TRUE(url_request.status().is_success()); |
| 349 EXPECT_EQ(1, network_delegate()->completed_requests()); |
| 350 EXPECT_EQ(0, network_delegate()->error_count()); |
| 351 EXPECT_TRUE(request_delegate.auth_required_called()); |
| 352 EXPECT_EQ("test.html", request_delegate.data_received()); |
| 353 } |
| 354 |
| 355 TEST_F(URLRequestFtpJobTest, FtpProxyRequestNeedServerAuthWithCredentials) { |
| 356 MockWrite writes[] = { |
| 357 MockWrite(ASYNC, 0, "GET ftp://ftp.example.com/ HTTP/1.1\r\n" |
| 358 "Host: ftp.example.com\r\n" |
| 359 "Proxy-Connection: keep-alive\r\n\r\n"), |
| 360 MockWrite(ASYNC, 5, "GET ftp://ftp.example.com/ HTTP/1.1\r\n" |
| 361 "Host: ftp.example.com\r\n" |
| 362 "Proxy-Connection: keep-alive\r\n" |
| 363 "Authorization: Basic bXl1c2VyOm15cGFzcw==\r\n\r\n"), |
| 364 }; |
| 365 MockRead reads[] = { |
| 366 // No credentials. |
| 367 MockRead(ASYNC, 1, "HTTP/1.1 401 Unauthorized\r\n"), |
| 368 MockRead(ASYNC, 2, "WWW-Authenticate: Basic " |
| 369 "realm=\"MyRealm1\"\r\n"), |
| 370 MockRead(ASYNC, 3, "Content-Length: 9\r\n\r\n"), |
| 371 MockRead(ASYNC, 4, "test.html"), |
| 372 |
| 373 // Second response. |
| 374 MockRead(ASYNC, 6, "HTTP/1.1 200 OK\r\n"), |
| 375 MockRead(ASYNC, 7, "Content-Length: 10\r\n\r\n"), |
| 376 MockRead(ASYNC, 8, "test2.html"), |
| 377 }; |
| 378 |
| 379 AddSocket(reads, arraysize(reads), writes, arraysize(writes)); |
| 380 |
| 381 TestDelegate request_delegate; |
| 382 request_delegate.set_credentials( |
| 383 AuthCredentials(ASCIIToUTF16("myuser"), ASCIIToUTF16("mypass"))); |
| 384 URLRequest url_request(GURL("ftp://ftp.example.com/"), |
| 385 &request_delegate, |
| 386 request_context(), |
| 387 network_delegate()); |
| 388 url_request.Start(); |
| 389 ASSERT_TRUE(url_request.is_pending()); |
| 390 socket_data(0)->RunFor(9); |
| 391 |
| 392 EXPECT_TRUE(url_request.status().is_success()); |
| 393 EXPECT_EQ(1, network_delegate()->completed_requests()); |
| 394 EXPECT_EQ(0, network_delegate()->error_count()); |
| 395 EXPECT_TRUE(request_delegate.auth_required_called()); |
| 396 EXPECT_EQ("test2.html", request_delegate.data_received()); |
| 397 } |
| 398 |
| 399 TEST_F(URLRequestFtpJobTest, FtpProxyRequestNeedProxyAndServerAuth) { |
| 400 MockWrite writes[] = { |
| 401 MockWrite(ASYNC, 0, "GET ftp://ftp.example.com/ HTTP/1.1\r\n" |
| 402 "Host: ftp.example.com\r\n" |
| 403 "Proxy-Connection: keep-alive\r\n\r\n"), |
| 404 MockWrite(ASYNC, 5, "GET ftp://ftp.example.com/ HTTP/1.1\r\n" |
| 405 "Host: ftp.example.com\r\n" |
| 406 "Proxy-Connection: keep-alive\r\n" |
| 407 "Proxy-Authorization: Basic " |
| 408 "cHJveHl1c2VyOnByb3h5cGFzcw==\r\n\r\n"), |
| 409 MockWrite(ASYNC, 10, "GET ftp://ftp.example.com/ HTTP/1.1\r\n" |
| 410 "Host: ftp.example.com\r\n" |
| 411 "Proxy-Connection: keep-alive\r\n" |
| 412 "Proxy-Authorization: Basic " |
| 413 "cHJveHl1c2VyOnByb3h5cGFzcw==\r\n" |
| 414 "Authorization: Basic bXl1c2VyOm15cGFzcw==\r\n\r\n"), |
| 415 }; |
| 416 MockRead reads[] = { |
| 417 // No credentials. |
| 418 MockRead(ASYNC, 1, "HTTP/1.1 407 Proxy Authentication Required\r\n"), |
| 419 MockRead(ASYNC, 2, "Proxy-Authenticate: Basic " |
| 420 "realm=\"MyRealm1\"\r\n"), |
| 421 MockRead(ASYNC, 3, "Content-Length: 9\r\n\r\n"), |
| 422 MockRead(ASYNC, 4, "test.html"), |
| 423 |
| 424 // Second response. |
| 425 MockRead(ASYNC, 6, "HTTP/1.1 401 Unauthorized\r\n"), |
| 426 MockRead(ASYNC, 7, "WWW-Authenticate: Basic " |
| 427 "realm=\"MyRealm1\"\r\n"), |
| 428 MockRead(ASYNC, 8, "Content-Length: 9\r\n\r\n"), |
| 429 MockRead(ASYNC, 9, "test.html"), |
| 430 |
| 431 // Third response. |
| 432 MockRead(ASYNC, 11, "HTTP/1.1 200 OK\r\n"), |
| 433 MockRead(ASYNC, 12, "Content-Length: 10\r\n\r\n"), |
| 434 MockRead(ASYNC, 13, "test2.html"), |
| 435 }; |
| 436 |
| 437 AddSocket(reads, arraysize(reads), writes, arraysize(writes)); |
| 438 |
| 439 GURL url("ftp://ftp.example.com"); |
| 440 |
| 441 // Make sure cached FTP credentials are not used for proxy authentication. |
| 442 request_context()->ftp_auth_cache()->Add( |
| 443 url.GetOrigin(), |
| 444 AuthCredentials(ASCIIToUTF16("userdonotuse"), |
| 445 ASCIIToUTF16("passworddonotuse"))); |
| 446 |
| 447 TestDelegate request_delegate; |
| 448 request_delegate.set_credentials( |
| 449 AuthCredentials(ASCIIToUTF16("proxyuser"), ASCIIToUTF16("proxypass"))); |
| 450 URLRequest url_request(url, |
| 451 &request_delegate, |
| 452 request_context(), |
| 453 network_delegate()); |
| 454 url_request.Start(); |
| 455 ASSERT_TRUE(url_request.is_pending()); |
| 456 socket_data(0)->RunFor(5); |
| 457 |
| 458 request_delegate.set_credentials( |
| 459 AuthCredentials(ASCIIToUTF16("myuser"), ASCIIToUTF16("mypass"))); |
| 460 socket_data(0)->RunFor(9); |
| 461 |
| 462 EXPECT_TRUE(url_request.status().is_success()); |
| 463 EXPECT_EQ(1, network_delegate()->completed_requests()); |
| 464 EXPECT_EQ(0, network_delegate()->error_count()); |
| 465 EXPECT_TRUE(request_delegate.auth_required_called()); |
| 466 EXPECT_EQ("test2.html", request_delegate.data_received()); |
| 467 } |
| 468 |
278 TEST_F(URLRequestFtpJobTest, FtpProxyRequestDoNotSaveCookies) { | 469 TEST_F(URLRequestFtpJobTest, FtpProxyRequestDoNotSaveCookies) { |
279 MockWrite writes[] = { | 470 MockWrite writes[] = { |
280 MockWrite(ASYNC, 0, "GET ftp://ftp.example.com/ HTTP/1.1\r\n" | 471 MockWrite(ASYNC, 0, "GET ftp://ftp.example.com/ HTTP/1.1\r\n" |
281 "Host: ftp.example.com\r\n" | 472 "Host: ftp.example.com\r\n" |
282 "Proxy-Connection: keep-alive\r\n\r\n"), | 473 "Proxy-Connection: keep-alive\r\n\r\n"), |
283 }; | 474 }; |
284 MockRead reads[] = { | 475 MockRead reads[] = { |
285 MockRead(ASYNC, 1, "HTTP/1.1 200 OK\r\n"), | 476 MockRead(ASYNC, 1, "HTTP/1.1 200 OK\r\n"), |
286 MockRead(ASYNC, 2, "Content-Length: 9\r\n"), | 477 MockRead(ASYNC, 2, "Content-Length: 9\r\n"), |
287 MockRead(ASYNC, 3, "Set-Cookie: name=value\r\n\r\n"), | 478 MockRead(ASYNC, 3, "Set-Cookie: name=value\r\n\r\n"), |
(...skipping 168 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
456 EXPECT_TRUE(url_request2.status().is_success()); | 647 EXPECT_TRUE(url_request2.status().is_success()); |
457 EXPECT_EQ(2, network_delegate()->completed_requests()); | 648 EXPECT_EQ(2, network_delegate()->completed_requests()); |
458 EXPECT_EQ(0, network_delegate()->error_count()); | 649 EXPECT_EQ(0, network_delegate()->error_count()); |
459 EXPECT_FALSE(request_delegate2.auth_required_called()); | 650 EXPECT_FALSE(request_delegate2.auth_required_called()); |
460 EXPECT_EQ("test2.html", request_delegate2.data_received()); | 651 EXPECT_EQ("test2.html", request_delegate2.data_received()); |
461 } | 652 } |
462 | 653 |
463 } // namespace | 654 } // namespace |
464 | 655 |
465 } // namespace net | 656 } // namespace net |
OLD | NEW |