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/socket_stream/socket_stream.h" | 5 #include "net/socket_stream/socket_stream.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
10 #include "base/bind.h" | 10 #include "base/bind.h" |
11 #include "base/bind_helpers.h" | 11 #include "base/bind_helpers.h" |
12 #include "base/callback.h" | 12 #include "base/callback.h" |
13 #include "base/utf_string_conversions.h" | 13 #include "base/utf_string_conversions.h" |
14 #include "net/base/auth.h" | 14 #include "net/base/auth.h" |
15 #include "net/base/mock_host_resolver.h" | 15 #include "net/base/mock_host_resolver.h" |
16 #include "net/base/net_log.h" | 16 #include "net/base/net_log.h" |
17 #include "net/base/net_log_unittest.h" | 17 #include "net/base/net_log_unittest.h" |
18 #include "net/base/test_completion_callback.h" | 18 #include "net/base/test_completion_callback.h" |
| 19 #include "net/http/http_network_session.h" |
19 #include "net/proxy/proxy_service.h" | 20 #include "net/proxy/proxy_service.h" |
20 #include "net/socket/socket_test_util.h" | 21 #include "net/socket/socket_test_util.h" |
21 #include "net/url_request/url_request_test_util.h" | 22 #include "net/url_request/url_request_test_util.h" |
22 #include "testing/gtest/include/gtest/gtest.h" | 23 #include "testing/gtest/include/gtest/gtest.h" |
23 #include "testing/platform_test.h" | 24 #include "testing/platform_test.h" |
24 | 25 |
25 namespace { | 26 namespace { |
26 | 27 |
27 struct SocketStreamEvent { | 28 struct SocketStreamEvent { |
28 enum EventType { | 29 enum EventType { |
(...skipping 399 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
428 events[0].event_type); | 429 events[0].event_type); |
429 EXPECT_EQ(SocketStreamEvent::EVENT_AUTH_REQUIRED, events[1].event_type); | 430 EXPECT_EQ(SocketStreamEvent::EVENT_AUTH_REQUIRED, events[1].event_type); |
430 EXPECT_EQ(SocketStreamEvent::EVENT_CONNECTED, events[2].event_type); | 431 EXPECT_EQ(SocketStreamEvent::EVENT_CONNECTED, events[2].event_type); |
431 EXPECT_EQ(SocketStreamEvent::EVENT_ERROR, events[3].event_type); | 432 EXPECT_EQ(SocketStreamEvent::EVENT_ERROR, events[3].event_type); |
432 EXPECT_EQ(net::ERR_ABORTED, events[3].error_code); | 433 EXPECT_EQ(net::ERR_ABORTED, events[3].error_code); |
433 EXPECT_EQ(SocketStreamEvent::EVENT_CLOSE, events[4].event_type); | 434 EXPECT_EQ(SocketStreamEvent::EVENT_CLOSE, events[4].event_type); |
434 | 435 |
435 // TODO(eroman): Add back NetLogTest here... | 436 // TODO(eroman): Add back NetLogTest here... |
436 } | 437 } |
437 | 438 |
| 439 TEST_F(SocketStreamTest, BasicAuthProxyWithAuthCache) { |
| 440 MockClientSocketFactory mock_socket_factory; |
| 441 MockWrite data_writes[] = { |
| 442 // WebSocket(SocketStream) always uses CONNECT when it is configured to use |
| 443 // proxy so the port may not be 443. |
| 444 MockWrite("CONNECT example.com:80 HTTP/1.1\r\n" |
| 445 "Host: example.com\r\n" |
| 446 "Proxy-Connection: keep-alive\r\n" |
| 447 "Proxy-Authorization: Basic Zm9vOmJhcg==\r\n\r\n"), |
| 448 }; |
| 449 MockRead data_reads[] = { |
| 450 MockRead("HTTP/1.1 200 Connection Established\r\n"), |
| 451 MockRead("Proxy-agent: Apache/2.2.8\r\n"), |
| 452 MockRead("\r\n"), |
| 453 MockRead(ASYNC, ERR_IO_PENDING) |
| 454 }; |
| 455 StaticSocketDataProvider data(data_reads, arraysize(data_reads), |
| 456 data_writes, arraysize(data_writes)); |
| 457 mock_socket_factory.AddSocketDataProvider(&data); |
| 458 |
| 459 TestCompletionCallback test_callback; |
| 460 scoped_ptr<SocketStreamEventRecorder> delegate( |
| 461 new SocketStreamEventRecorder(test_callback.callback())); |
| 462 delegate->SetOnConnected(base::Bind(&SocketStreamEventRecorder::DoClose, |
| 463 base::Unretained(delegate.get()))); |
| 464 |
| 465 scoped_refptr<SocketStream> socket_stream( |
| 466 new SocketStream(GURL("ws://example.com/demo"), delegate.get())); |
| 467 |
| 468 TestURLRequestContextWithProxy context("myproxy:70"); |
| 469 HttpAuthCache* auth_cache = |
| 470 context.http_transaction_factory()->GetSession()->http_auth_cache(); |
| 471 auth_cache->Add(GURL("http://myproxy:70"), |
| 472 "MyRealm1", |
| 473 HttpAuth::AUTH_SCHEME_BASIC, |
| 474 "Basic realm=MyRealm1", |
| 475 AuthCredentials(ASCIIToUTF16("foo"), |
| 476 ASCIIToUTF16("bar")), |
| 477 "/"); |
| 478 |
| 479 socket_stream->set_context(&context); |
| 480 socket_stream->SetClientSocketFactory(&mock_socket_factory); |
| 481 |
| 482 socket_stream->Connect(); |
| 483 |
| 484 test_callback.WaitForResult(); |
| 485 |
| 486 const std::vector<SocketStreamEvent>& events = delegate->GetSeenEvents(); |
| 487 ASSERT_EQ(4U, events.size()); |
| 488 EXPECT_EQ(SocketStreamEvent::EVENT_START_OPEN_CONNECTION, |
| 489 events[0].event_type); |
| 490 EXPECT_EQ(SocketStreamEvent::EVENT_CONNECTED, events[1].event_type); |
| 491 EXPECT_EQ(net::ERR_ABORTED, events[2].error_code); |
| 492 EXPECT_EQ(SocketStreamEvent::EVENT_CLOSE, events[3].event_type); |
| 493 } |
| 494 |
| 495 TEST_F(SocketStreamTest, WSSBasicAuthProxyWithAuthCache) { |
| 496 MockClientSocketFactory mock_socket_factory; |
| 497 MockWrite data_writes1[] = { |
| 498 MockWrite("CONNECT example.com:443 HTTP/1.1\r\n" |
| 499 "Host: example.com\r\n" |
| 500 "Proxy-Connection: keep-alive\r\n" |
| 501 "Proxy-Authorization: Basic Zm9vOmJhcg==\r\n\r\n"), |
| 502 }; |
| 503 MockRead data_reads1[] = { |
| 504 MockRead("HTTP/1.1 200 Connection Established\r\n"), |
| 505 MockRead("Proxy-agent: Apache/2.2.8\r\n"), |
| 506 MockRead("\r\n"), |
| 507 MockRead(ASYNC, ERR_IO_PENDING) |
| 508 }; |
| 509 StaticSocketDataProvider data1(data_reads1, arraysize(data_reads1), |
| 510 data_writes1, arraysize(data_writes1)); |
| 511 mock_socket_factory.AddSocketDataProvider(&data1); |
| 512 |
| 513 SSLSocketDataProvider data2(ASYNC, OK); |
| 514 mock_socket_factory.AddSSLSocketDataProvider(&data2); |
| 515 |
| 516 TestCompletionCallback test_callback; |
| 517 scoped_ptr<SocketStreamEventRecorder> delegate( |
| 518 new SocketStreamEventRecorder(test_callback.callback())); |
| 519 delegate->SetOnConnected(base::Bind(&SocketStreamEventRecorder::DoClose, |
| 520 base::Unretained(delegate.get()))); |
| 521 |
| 522 scoped_refptr<SocketStream> socket_stream( |
| 523 new SocketStream(GURL("wss://example.com/demo"), delegate.get())); |
| 524 |
| 525 TestURLRequestContextWithProxy context("myproxy:70"); |
| 526 HttpAuthCache* auth_cache = |
| 527 context.http_transaction_factory()->GetSession()->http_auth_cache(); |
| 528 auth_cache->Add(GURL("http://myproxy:70"), |
| 529 "MyRealm1", |
| 530 HttpAuth::AUTH_SCHEME_BASIC, |
| 531 "Basic realm=MyRealm1", |
| 532 AuthCredentials(ASCIIToUTF16("foo"), |
| 533 ASCIIToUTF16("bar")), |
| 534 "/"); |
| 535 |
| 536 socket_stream->set_context(&context); |
| 537 socket_stream->SetClientSocketFactory(&mock_socket_factory); |
| 538 |
| 539 socket_stream->Connect(); |
| 540 |
| 541 test_callback.WaitForResult(); |
| 542 |
| 543 const std::vector<SocketStreamEvent>& events = delegate->GetSeenEvents(); |
| 544 ASSERT_EQ(4U, events.size()); |
| 545 EXPECT_EQ(SocketStreamEvent::EVENT_START_OPEN_CONNECTION, |
| 546 events[0].event_type); |
| 547 EXPECT_EQ(SocketStreamEvent::EVENT_CONNECTED, events[1].event_type); |
| 548 EXPECT_EQ(net::ERR_ABORTED, events[2].error_code); |
| 549 EXPECT_EQ(SocketStreamEvent::EVENT_CLOSE, events[3].event_type); |
| 550 } |
| 551 |
438 TEST_F(SocketStreamTest, IOPending) { | 552 TEST_F(SocketStreamTest, IOPending) { |
439 TestCompletionCallback test_callback; | 553 TestCompletionCallback test_callback; |
440 | 554 |
441 scoped_ptr<SocketStreamEventRecorder> delegate( | 555 scoped_ptr<SocketStreamEventRecorder> delegate( |
442 new SocketStreamEventRecorder(test_callback.callback())); | 556 new SocketStreamEventRecorder(test_callback.callback())); |
443 delegate->SetOnConnected(base::Bind( | 557 delegate->SetOnConnected(base::Bind( |
444 &SocketStreamTest::DoSendWebSocketHandshake, base::Unretained(this))); | 558 &SocketStreamTest::DoSendWebSocketHandshake, base::Unretained(this))); |
445 delegate->SetOnReceivedData(base::Bind( | 559 delegate->SetOnReceivedData(base::Bind( |
446 &SocketStreamTest::DoCloseFlushPendingWriteTest, | 560 &SocketStreamTest::DoCloseFlushPendingWriteTest, |
447 base::Unretained(this))); | 561 base::Unretained(this))); |
(...skipping 238 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
686 | 800 |
687 const std::vector<SocketStreamEvent>& events = delegate->GetSeenEvents(); | 801 const std::vector<SocketStreamEvent>& events = delegate->GetSeenEvents(); |
688 ASSERT_EQ(2U, events.size()); | 802 ASSERT_EQ(2U, events.size()); |
689 | 803 |
690 EXPECT_EQ(SocketStreamEvent::EVENT_ERROR, events[0].event_type); | 804 EXPECT_EQ(SocketStreamEvent::EVENT_ERROR, events[0].event_type); |
691 EXPECT_EQ(net::ERR_ACCESS_DENIED, events[0].error_code); | 805 EXPECT_EQ(net::ERR_ACCESS_DENIED, events[0].error_code); |
692 EXPECT_EQ(SocketStreamEvent::EVENT_CLOSE, events[1].event_type); | 806 EXPECT_EQ(SocketStreamEvent::EVENT_CLOSE, events[1].event_type); |
693 } | 807 } |
694 | 808 |
695 } // namespace net | 809 } // namespace net |
OLD | NEW |