| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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/websockets/websocket_stream.h" | 5 #include "net/websockets/websocket_stream.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <string> | 8 #include <string> |
| 9 #include <utility> | 9 #include <utility> |
| 10 #include <vector> | 10 #include <vector> |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 44 // any read/write data and so can't benefit from it anyway. The arrays are not | 44 // any read/write data and so can't benefit from it anyway. The arrays are not |
| 45 // copied. It is up to the caller to ensure they stay in scope until the test | 45 // copied. It is up to the caller to ensure they stay in scope until the test |
| 46 // ends. | 46 // ends. |
| 47 template <size_t reads_count, size_t writes_count> | 47 template <size_t reads_count, size_t writes_count> |
| 48 scoped_ptr<SequencedSocketData> BuildSocketData( | 48 scoped_ptr<SequencedSocketData> BuildSocketData( |
| 49 MockRead(&reads)[reads_count], | 49 MockRead(&reads)[reads_count], |
| 50 MockWrite(&writes)[writes_count]) { | 50 MockWrite(&writes)[writes_count]) { |
| 51 scoped_ptr<SequencedSocketData> socket_data( | 51 scoped_ptr<SequencedSocketData> socket_data( |
| 52 new SequencedSocketData(reads, reads_count, writes, writes_count)); | 52 new SequencedSocketData(reads, reads_count, writes, writes_count)); |
| 53 socket_data->set_connect_data(MockConnect(SYNCHRONOUS, OK)); | 53 socket_data->set_connect_data(MockConnect(SYNCHRONOUS, OK)); |
| 54 return socket_data.Pass(); | 54 return socket_data; |
| 55 } | 55 } |
| 56 | 56 |
| 57 // Builder for a SequencedSocketData that expects nothing. This does not | 57 // Builder for a SequencedSocketData that expects nothing. This does not |
| 58 // set the connect data, so the calling code must do that explicitly. | 58 // set the connect data, so the calling code must do that explicitly. |
| 59 scoped_ptr<SequencedSocketData> BuildNullSocketData() { | 59 scoped_ptr<SequencedSocketData> BuildNullSocketData() { |
| 60 return make_scoped_ptr(new SequencedSocketData(NULL, 0, NULL, 0)); | 60 return make_scoped_ptr(new SequencedSocketData(NULL, 0, NULL, 0)); |
| 61 } | 61 } |
| 62 | 62 |
| 63 class MockWeakTimer : public base::MockTimer, | 63 class MockWeakTimer : public base::MockTimer, |
| 64 public base::SupportsWeakPtr<MockWeakTimer> { | 64 public base::SupportsWeakPtr<MockWeakTimer> { |
| (...skipping 26 matching lines...) Expand all Loading... |
| 91 const std::string& socket_path, | 91 const std::string& socket_path, |
| 92 const std::vector<std::string>& sub_protocols, | 92 const std::vector<std::string>& sub_protocols, |
| 93 const url::Origin& origin, | 93 const url::Origin& origin, |
| 94 const std::string& extra_request_headers, | 94 const std::string& extra_request_headers, |
| 95 const std::string& response_body, | 95 const std::string& response_body, |
| 96 scoped_ptr<base::Timer> timer = scoped_ptr<base::Timer>()) { | 96 scoped_ptr<base::Timer> timer = scoped_ptr<base::Timer>()) { |
| 97 url_request_context_host_.SetExpectations( | 97 url_request_context_host_.SetExpectations( |
| 98 WebSocketStandardRequest(socket_path, socket_host, origin, | 98 WebSocketStandardRequest(socket_path, socket_host, origin, |
| 99 extra_request_headers), | 99 extra_request_headers), |
| 100 response_body); | 100 response_body); |
| 101 CreateAndConnectStream(socket_url, sub_protocols, origin, timer.Pass()); | 101 CreateAndConnectStream(socket_url, sub_protocols, origin, std::move(timer)); |
| 102 } | 102 } |
| 103 | 103 |
| 104 // |extra_request_headers| and |extra_response_headers| must end in "\r\n" or | 104 // |extra_request_headers| and |extra_response_headers| must end in "\r\n" or |
| 105 // errors like "Unable to perform synchronous IO while stopped" will occur. | 105 // errors like "Unable to perform synchronous IO while stopped" will occur. |
| 106 void CreateAndConnectStandard( | 106 void CreateAndConnectStandard( |
| 107 const std::string& socket_url, | 107 const std::string& socket_url, |
| 108 const std::string& socket_host, | 108 const std::string& socket_host, |
| 109 const std::string& socket_path, | 109 const std::string& socket_path, |
| 110 const std::vector<std::string>& sub_protocols, | 110 const std::vector<std::string>& sub_protocols, |
| 111 const url::Origin& origin, | 111 const url::Origin& origin, |
| 112 const std::string& extra_request_headers, | 112 const std::string& extra_request_headers, |
| 113 const std::string& extra_response_headers, | 113 const std::string& extra_response_headers, |
| 114 scoped_ptr<base::Timer> timer = scoped_ptr<base::Timer>()) { | 114 scoped_ptr<base::Timer> timer = scoped_ptr<base::Timer>()) { |
| 115 CreateAndConnectCustomResponse( | 115 CreateAndConnectCustomResponse( |
| 116 socket_url, socket_host, socket_path, sub_protocols, origin, | 116 socket_url, socket_host, socket_path, sub_protocols, origin, |
| 117 extra_request_headers, | 117 extra_request_headers, |
| 118 WebSocketStandardResponse(extra_response_headers), timer.Pass()); | 118 WebSocketStandardResponse(extra_response_headers), std::move(timer)); |
| 119 } | 119 } |
| 120 | 120 |
| 121 void CreateAndConnectRawExpectations( | 121 void CreateAndConnectRawExpectations( |
| 122 const std::string& socket_url, | 122 const std::string& socket_url, |
| 123 const std::vector<std::string>& sub_protocols, | 123 const std::vector<std::string>& sub_protocols, |
| 124 const url::Origin& origin, | 124 const url::Origin& origin, |
| 125 scoped_ptr<SequencedSocketData> socket_data, | 125 scoped_ptr<SequencedSocketData> socket_data, |
| 126 scoped_ptr<base::Timer> timer = scoped_ptr<base::Timer>()) { | 126 scoped_ptr<base::Timer> timer = scoped_ptr<base::Timer>()) { |
| 127 AddRawExpectations(socket_data.Pass()); | 127 AddRawExpectations(std::move(socket_data)); |
| 128 CreateAndConnectStream(socket_url, sub_protocols, origin, timer.Pass()); | 128 CreateAndConnectStream(socket_url, sub_protocols, origin, std::move(timer)); |
| 129 } | 129 } |
| 130 | 130 |
| 131 // Add additional raw expectations for sockets created before the final one. | 131 // Add additional raw expectations for sockets created before the final one. |
| 132 void AddRawExpectations(scoped_ptr<SequencedSocketData> socket_data) { | 132 void AddRawExpectations(scoped_ptr<SequencedSocketData> socket_data) { |
| 133 url_request_context_host_.AddRawExpectations(socket_data.Pass()); | 133 url_request_context_host_.AddRawExpectations(std::move(socket_data)); |
| 134 } | 134 } |
| 135 }; | 135 }; |
| 136 | 136 |
| 137 // There are enough tests of the Sec-WebSocket-Extensions header that they | 137 // There are enough tests of the Sec-WebSocket-Extensions header that they |
| 138 // deserve their own test fixture. | 138 // deserve their own test fixture. |
| 139 class WebSocketStreamCreateExtensionTest : public WebSocketStreamCreateTest { | 139 class WebSocketStreamCreateExtensionTest : public WebSocketStreamCreateTest { |
| 140 public: | 140 public: |
| 141 // Performs a standard connect, with the value of the Sec-WebSocket-Extensions | 141 // Performs a standard connect, with the value of the Sec-WebSocket-Extensions |
| 142 // header in the response set to |extensions_header_value|. Runs the event | 142 // header in the response set to |extensions_header_value|. Runs the event |
| 143 // loop to allow the connect to complete. | 143 // loop to allow the connect to complete. |
| (...skipping 653 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 797 EXPECT_FALSE(request_info_); | 797 EXPECT_FALSE(request_info_); |
| 798 EXPECT_FALSE(response_info_); | 798 EXPECT_FALSE(response_info_); |
| 799 } | 799 } |
| 800 | 800 |
| 801 // Connect failure must look just like negotiation failure. | 801 // Connect failure must look just like negotiation failure. |
| 802 TEST_F(WebSocketStreamCreateTest, ConnectionFailure) { | 802 TEST_F(WebSocketStreamCreateTest, ConnectionFailure) { |
| 803 scoped_ptr<SequencedSocketData> socket_data(BuildNullSocketData()); | 803 scoped_ptr<SequencedSocketData> socket_data(BuildNullSocketData()); |
| 804 socket_data->set_connect_data( | 804 socket_data->set_connect_data( |
| 805 MockConnect(SYNCHRONOUS, ERR_CONNECTION_REFUSED)); | 805 MockConnect(SYNCHRONOUS, ERR_CONNECTION_REFUSED)); |
| 806 CreateAndConnectRawExpectations("ws://localhost/", NoSubProtocols(), | 806 CreateAndConnectRawExpectations("ws://localhost/", NoSubProtocols(), |
| 807 LocalhostOrigin(), socket_data.Pass()); | 807 LocalhostOrigin(), std::move(socket_data)); |
| 808 WaitUntilConnectDone(); | 808 WaitUntilConnectDone(); |
| 809 EXPECT_TRUE(has_failed()); | 809 EXPECT_TRUE(has_failed()); |
| 810 EXPECT_EQ("Error in connection establishment: net::ERR_CONNECTION_REFUSED", | 810 EXPECT_EQ("Error in connection establishment: net::ERR_CONNECTION_REFUSED", |
| 811 failure_message()); | 811 failure_message()); |
| 812 EXPECT_FALSE(request_info_); | 812 EXPECT_FALSE(request_info_); |
| 813 EXPECT_FALSE(response_info_); | 813 EXPECT_FALSE(response_info_); |
| 814 } | 814 } |
| 815 | 815 |
| 816 // Connect timeout must look just like any other failure. | 816 // Connect timeout must look just like any other failure. |
| 817 TEST_F(WebSocketStreamCreateTest, ConnectionTimeout) { | 817 TEST_F(WebSocketStreamCreateTest, ConnectionTimeout) { |
| 818 scoped_ptr<SequencedSocketData> socket_data(BuildNullSocketData()); | 818 scoped_ptr<SequencedSocketData> socket_data(BuildNullSocketData()); |
| 819 socket_data->set_connect_data( | 819 socket_data->set_connect_data( |
| 820 MockConnect(ASYNC, ERR_CONNECTION_TIMED_OUT)); | 820 MockConnect(ASYNC, ERR_CONNECTION_TIMED_OUT)); |
| 821 CreateAndConnectRawExpectations("ws://localhost/", NoSubProtocols(), | 821 CreateAndConnectRawExpectations("ws://localhost/", NoSubProtocols(), |
| 822 LocalhostOrigin(), socket_data.Pass()); | 822 LocalhostOrigin(), std::move(socket_data)); |
| 823 WaitUntilConnectDone(); | 823 WaitUntilConnectDone(); |
| 824 EXPECT_TRUE(has_failed()); | 824 EXPECT_TRUE(has_failed()); |
| 825 EXPECT_EQ("Error in connection establishment: net::ERR_CONNECTION_TIMED_OUT", | 825 EXPECT_EQ("Error in connection establishment: net::ERR_CONNECTION_TIMED_OUT", |
| 826 failure_message()); | 826 failure_message()); |
| 827 } | 827 } |
| 828 | 828 |
| 829 // The server doesn't respond to the opening handshake. | 829 // The server doesn't respond to the opening handshake. |
| 830 TEST_F(WebSocketStreamCreateTest, HandshakeTimeout) { | 830 TEST_F(WebSocketStreamCreateTest, HandshakeTimeout) { |
| 831 scoped_ptr<SequencedSocketData> socket_data(BuildNullSocketData()); | 831 scoped_ptr<SequencedSocketData> socket_data(BuildNullSocketData()); |
| 832 socket_data->set_connect_data(MockConnect(SYNCHRONOUS, ERR_IO_PENDING)); | 832 socket_data->set_connect_data(MockConnect(SYNCHRONOUS, ERR_IO_PENDING)); |
| 833 scoped_ptr<MockWeakTimer> timer(new MockWeakTimer(false, false)); | 833 scoped_ptr<MockWeakTimer> timer(new MockWeakTimer(false, false)); |
| 834 base::WeakPtr<MockWeakTimer> weak_timer = timer->AsWeakPtr(); | 834 base::WeakPtr<MockWeakTimer> weak_timer = timer->AsWeakPtr(); |
| 835 CreateAndConnectRawExpectations("ws://localhost/", NoSubProtocols(), | 835 CreateAndConnectRawExpectations("ws://localhost/", NoSubProtocols(), |
| 836 LocalhostOrigin(), socket_data.Pass(), | 836 LocalhostOrigin(), std::move(socket_data), |
| 837 timer.Pass()); | 837 std::move(timer)); |
| 838 EXPECT_FALSE(has_failed()); | 838 EXPECT_FALSE(has_failed()); |
| 839 ASSERT_TRUE(weak_timer.get()); | 839 ASSERT_TRUE(weak_timer.get()); |
| 840 EXPECT_TRUE(weak_timer->IsRunning()); | 840 EXPECT_TRUE(weak_timer->IsRunning()); |
| 841 | 841 |
| 842 weak_timer->Fire(); | 842 weak_timer->Fire(); |
| 843 WaitUntilConnectDone(); | 843 WaitUntilConnectDone(); |
| 844 | 844 |
| 845 EXPECT_TRUE(has_failed()); | 845 EXPECT_TRUE(has_failed()); |
| 846 EXPECT_EQ("WebSocket opening handshake timed out", failure_message()); | 846 EXPECT_EQ("WebSocket opening handshake timed out", failure_message()); |
| 847 ASSERT_TRUE(weak_timer.get()); | 847 ASSERT_TRUE(weak_timer.get()); |
| 848 EXPECT_FALSE(weak_timer->IsRunning()); | 848 EXPECT_FALSE(weak_timer->IsRunning()); |
| 849 } | 849 } |
| 850 | 850 |
| 851 // When the connection establishes the timer should be stopped. | 851 // When the connection establishes the timer should be stopped. |
| 852 TEST_F(WebSocketStreamCreateTest, HandshakeTimerOnSuccess) { | 852 TEST_F(WebSocketStreamCreateTest, HandshakeTimerOnSuccess) { |
| 853 scoped_ptr<MockWeakTimer> timer(new MockWeakTimer(false, false)); | 853 scoped_ptr<MockWeakTimer> timer(new MockWeakTimer(false, false)); |
| 854 base::WeakPtr<MockWeakTimer> weak_timer = timer->AsWeakPtr(); | 854 base::WeakPtr<MockWeakTimer> weak_timer = timer->AsWeakPtr(); |
| 855 | 855 |
| 856 CreateAndConnectStandard("ws://localhost/", "localhost", "/", | 856 CreateAndConnectStandard("ws://localhost/", "localhost", "/", |
| 857 NoSubProtocols(), LocalhostOrigin(), "", "", | 857 NoSubProtocols(), LocalhostOrigin(), "", "", |
| 858 timer.Pass()); | 858 std::move(timer)); |
| 859 ASSERT_TRUE(weak_timer); | 859 ASSERT_TRUE(weak_timer); |
| 860 EXPECT_TRUE(weak_timer->IsRunning()); | 860 EXPECT_TRUE(weak_timer->IsRunning()); |
| 861 | 861 |
| 862 WaitUntilConnectDone(); | 862 WaitUntilConnectDone(); |
| 863 EXPECT_FALSE(has_failed()); | 863 EXPECT_FALSE(has_failed()); |
| 864 EXPECT_TRUE(stream_); | 864 EXPECT_TRUE(stream_); |
| 865 ASSERT_TRUE(weak_timer); | 865 ASSERT_TRUE(weak_timer); |
| 866 EXPECT_FALSE(weak_timer->IsRunning()); | 866 EXPECT_FALSE(weak_timer->IsRunning()); |
| 867 } | 867 } |
| 868 | 868 |
| 869 // When the connection fails the timer should be stopped. | 869 // When the connection fails the timer should be stopped. |
| 870 TEST_F(WebSocketStreamCreateTest, HandshakeTimerOnFailure) { | 870 TEST_F(WebSocketStreamCreateTest, HandshakeTimerOnFailure) { |
| 871 scoped_ptr<SequencedSocketData> socket_data(BuildNullSocketData()); | 871 scoped_ptr<SequencedSocketData> socket_data(BuildNullSocketData()); |
| 872 socket_data->set_connect_data( | 872 socket_data->set_connect_data( |
| 873 MockConnect(SYNCHRONOUS, ERR_CONNECTION_REFUSED)); | 873 MockConnect(SYNCHRONOUS, ERR_CONNECTION_REFUSED)); |
| 874 scoped_ptr<MockWeakTimer> timer(new MockWeakTimer(false, false)); | 874 scoped_ptr<MockWeakTimer> timer(new MockWeakTimer(false, false)); |
| 875 base::WeakPtr<MockWeakTimer> weak_timer = timer->AsWeakPtr(); | 875 base::WeakPtr<MockWeakTimer> weak_timer = timer->AsWeakPtr(); |
| 876 CreateAndConnectRawExpectations("ws://localhost/", NoSubProtocols(), | 876 CreateAndConnectRawExpectations("ws://localhost/", NoSubProtocols(), |
| 877 LocalhostOrigin(), socket_data.Pass(), | 877 LocalhostOrigin(), std::move(socket_data), |
| 878 timer.Pass()); | 878 std::move(timer)); |
| 879 ASSERT_TRUE(weak_timer.get()); | 879 ASSERT_TRUE(weak_timer.get()); |
| 880 EXPECT_TRUE(weak_timer->IsRunning()); | 880 EXPECT_TRUE(weak_timer->IsRunning()); |
| 881 | 881 |
| 882 WaitUntilConnectDone(); | 882 WaitUntilConnectDone(); |
| 883 EXPECT_TRUE(has_failed()); | 883 EXPECT_TRUE(has_failed()); |
| 884 EXPECT_EQ("Error in connection establishment: net::ERR_CONNECTION_REFUSED", | 884 EXPECT_EQ("Error in connection establishment: net::ERR_CONNECTION_REFUSED", |
| 885 failure_message()); | 885 failure_message()); |
| 886 ASSERT_TRUE(weak_timer.get()); | 886 ASSERT_TRUE(weak_timer.get()); |
| 887 EXPECT_FALSE(weak_timer->IsRunning()); | 887 EXPECT_FALSE(weak_timer->IsRunning()); |
| 888 } | 888 } |
| 889 | 889 |
| 890 // Cancellation during connect works. | 890 // Cancellation during connect works. |
| 891 TEST_F(WebSocketStreamCreateTest, CancellationDuringConnect) { | 891 TEST_F(WebSocketStreamCreateTest, CancellationDuringConnect) { |
| 892 scoped_ptr<SequencedSocketData> socket_data(BuildNullSocketData()); | 892 scoped_ptr<SequencedSocketData> socket_data(BuildNullSocketData()); |
| 893 socket_data->set_connect_data(MockConnect(SYNCHRONOUS, ERR_IO_PENDING)); | 893 socket_data->set_connect_data(MockConnect(SYNCHRONOUS, ERR_IO_PENDING)); |
| 894 CreateAndConnectRawExpectations("ws://localhost/", NoSubProtocols(), | 894 CreateAndConnectRawExpectations("ws://localhost/", NoSubProtocols(), |
| 895 LocalhostOrigin(), socket_data.Pass()); | 895 LocalhostOrigin(), std::move(socket_data)); |
| 896 stream_request_.reset(); | 896 stream_request_.reset(); |
| 897 // WaitUntilConnectDone doesn't work in this case. | 897 // WaitUntilConnectDone doesn't work in this case. |
| 898 base::RunLoop().RunUntilIdle(); | 898 base::RunLoop().RunUntilIdle(); |
| 899 EXPECT_FALSE(has_failed()); | 899 EXPECT_FALSE(has_failed()); |
| 900 EXPECT_FALSE(stream_); | 900 EXPECT_FALSE(stream_); |
| 901 } | 901 } |
| 902 | 902 |
| 903 // Cancellation during write of the request headers works. | 903 // Cancellation during write of the request headers works. |
| 904 TEST_F(WebSocketStreamCreateTest, CancellationDuringWrite) { | 904 TEST_F(WebSocketStreamCreateTest, CancellationDuringWrite) { |
| 905 // First write never completes. | 905 // First write never completes. |
| (...skipping 19 matching lines...) Expand all Loading... |
| 925 TEST_F(WebSocketStreamCreateTest, CancellationDuringRead) { | 925 TEST_F(WebSocketStreamCreateTest, CancellationDuringRead) { |
| 926 std::string request = | 926 std::string request = |
| 927 WebSocketStandardRequest("/", "localhost", LocalhostOrigin(), ""); | 927 WebSocketStandardRequest("/", "localhost", LocalhostOrigin(), ""); |
| 928 MockWrite writes[] = {MockWrite(ASYNC, 0, request.c_str())}; | 928 MockWrite writes[] = {MockWrite(ASYNC, 0, request.c_str())}; |
| 929 MockRead reads[] = { | 929 MockRead reads[] = { |
| 930 MockRead(SYNCHRONOUS, ERR_IO_PENDING, 1), | 930 MockRead(SYNCHRONOUS, ERR_IO_PENDING, 1), |
| 931 }; | 931 }; |
| 932 scoped_ptr<SequencedSocketData> socket_data(BuildSocketData(reads, writes)); | 932 scoped_ptr<SequencedSocketData> socket_data(BuildSocketData(reads, writes)); |
| 933 SequencedSocketData* socket_data_raw_ptr = socket_data.get(); | 933 SequencedSocketData* socket_data_raw_ptr = socket_data.get(); |
| 934 CreateAndConnectRawExpectations("ws://localhost/", NoSubProtocols(), | 934 CreateAndConnectRawExpectations("ws://localhost/", NoSubProtocols(), |
| 935 LocalhostOrigin(), socket_data.Pass()); | 935 LocalhostOrigin(), std::move(socket_data)); |
| 936 base::RunLoop().RunUntilIdle(); | 936 base::RunLoop().RunUntilIdle(); |
| 937 EXPECT_TRUE(socket_data_raw_ptr->AllReadDataConsumed()); | 937 EXPECT_TRUE(socket_data_raw_ptr->AllReadDataConsumed()); |
| 938 stream_request_.reset(); | 938 stream_request_.reset(); |
| 939 // WaitUntilConnectDone doesn't work in this case. | 939 // WaitUntilConnectDone doesn't work in this case. |
| 940 base::RunLoop().RunUntilIdle(); | 940 base::RunLoop().RunUntilIdle(); |
| 941 EXPECT_FALSE(has_failed()); | 941 EXPECT_FALSE(has_failed()); |
| 942 EXPECT_FALSE(stream_); | 942 EXPECT_FALSE(stream_); |
| 943 EXPECT_TRUE(request_info_); | 943 EXPECT_TRUE(request_info_); |
| 944 EXPECT_FALSE(response_info_); | 944 EXPECT_FALSE(response_info_); |
| 945 } | 945 } |
| (...skipping 20 matching lines...) Expand all Loading... |
| 966 // log the console message "Connection closed before receiving a handshake | 966 // log the console message "Connection closed before receiving a handshake |
| 967 // response". | 967 // response". |
| 968 TEST_F(WebSocketStreamCreateTest, NoResponse) { | 968 TEST_F(WebSocketStreamCreateTest, NoResponse) { |
| 969 std::string request = | 969 std::string request = |
| 970 WebSocketStandardRequest("/", "localhost", LocalhostOrigin(), ""); | 970 WebSocketStandardRequest("/", "localhost", LocalhostOrigin(), ""); |
| 971 MockWrite writes[] = {MockWrite(ASYNC, request.data(), request.size(), 0)}; | 971 MockWrite writes[] = {MockWrite(ASYNC, request.data(), request.size(), 0)}; |
| 972 MockRead reads[] = {MockRead(ASYNC, 0, 1)}; | 972 MockRead reads[] = {MockRead(ASYNC, 0, 1)}; |
| 973 scoped_ptr<SequencedSocketData> socket_data(BuildSocketData(reads, writes)); | 973 scoped_ptr<SequencedSocketData> socket_data(BuildSocketData(reads, writes)); |
| 974 SequencedSocketData* socket_data_raw_ptr = socket_data.get(); | 974 SequencedSocketData* socket_data_raw_ptr = socket_data.get(); |
| 975 CreateAndConnectRawExpectations("ws://localhost/", NoSubProtocols(), | 975 CreateAndConnectRawExpectations("ws://localhost/", NoSubProtocols(), |
| 976 LocalhostOrigin(), socket_data.Pass()); | 976 LocalhostOrigin(), std::move(socket_data)); |
| 977 base::RunLoop().RunUntilIdle(); | 977 base::RunLoop().RunUntilIdle(); |
| 978 EXPECT_TRUE(socket_data_raw_ptr->AllReadDataConsumed()); | 978 EXPECT_TRUE(socket_data_raw_ptr->AllReadDataConsumed()); |
| 979 EXPECT_TRUE(has_failed()); | 979 EXPECT_TRUE(has_failed()); |
| 980 EXPECT_FALSE(stream_); | 980 EXPECT_FALSE(stream_); |
| 981 EXPECT_FALSE(response_info_); | 981 EXPECT_FALSE(response_info_); |
| 982 EXPECT_EQ("Connection closed before receiving a handshake response", | 982 EXPECT_EQ("Connection closed before receiving a handshake response", |
| 983 failure_message()); | 983 failure_message()); |
| 984 } | 984 } |
| 985 | 985 |
| 986 TEST_F(WebSocketStreamCreateTest, SelfSignedCertificateFailure) { | 986 TEST_F(WebSocketStreamCreateTest, SelfSignedCertificateFailure) { |
| 987 ssl_data_.push_back(make_scoped_ptr( | 987 ssl_data_.push_back(make_scoped_ptr( |
| 988 new SSLSocketDataProvider(ASYNC, ERR_CERT_AUTHORITY_INVALID))); | 988 new SSLSocketDataProvider(ASYNC, ERR_CERT_AUTHORITY_INVALID))); |
| 989 ssl_data_[0]->cert = | 989 ssl_data_[0]->cert = |
| 990 ImportCertFromFile(GetTestCertsDirectory(), "unittest.selfsigned.der"); | 990 ImportCertFromFile(GetTestCertsDirectory(), "unittest.selfsigned.der"); |
| 991 ASSERT_TRUE(ssl_data_[0]->cert.get()); | 991 ASSERT_TRUE(ssl_data_[0]->cert.get()); |
| 992 scoped_ptr<SequencedSocketData> raw_socket_data(BuildNullSocketData()); | 992 scoped_ptr<SequencedSocketData> raw_socket_data(BuildNullSocketData()); |
| 993 CreateAndConnectRawExpectations("wss://localhost/", NoSubProtocols(), | 993 CreateAndConnectRawExpectations("wss://localhost/", NoSubProtocols(), |
| 994 LocalhostOrigin(), raw_socket_data.Pass()); | 994 LocalhostOrigin(), |
| 995 std::move(raw_socket_data)); |
| 995 // WaitUntilConnectDone doesn't work in this case. | 996 // WaitUntilConnectDone doesn't work in this case. |
| 996 base::RunLoop().RunUntilIdle(); | 997 base::RunLoop().RunUntilIdle(); |
| 997 EXPECT_FALSE(has_failed()); | 998 EXPECT_FALSE(has_failed()); |
| 998 ASSERT_TRUE(ssl_error_callbacks_); | 999 ASSERT_TRUE(ssl_error_callbacks_); |
| 999 ssl_error_callbacks_->CancelSSLRequest(ERR_CERT_AUTHORITY_INVALID, | 1000 ssl_error_callbacks_->CancelSSLRequest(ERR_CERT_AUTHORITY_INVALID, |
| 1000 &ssl_info_); | 1001 &ssl_info_); |
| 1001 WaitUntilConnectDone(); | 1002 WaitUntilConnectDone(); |
| 1002 EXPECT_TRUE(has_failed()); | 1003 EXPECT_TRUE(has_failed()); |
| 1003 } | 1004 } |
| 1004 | 1005 |
| 1005 TEST_F(WebSocketStreamCreateTest, SelfSignedCertificateSuccess) { | 1006 TEST_F(WebSocketStreamCreateTest, SelfSignedCertificateSuccess) { |
| 1006 scoped_ptr<SSLSocketDataProvider> ssl_data( | 1007 scoped_ptr<SSLSocketDataProvider> ssl_data( |
| 1007 new SSLSocketDataProvider(ASYNC, ERR_CERT_AUTHORITY_INVALID)); | 1008 new SSLSocketDataProvider(ASYNC, ERR_CERT_AUTHORITY_INVALID)); |
| 1008 ssl_data->cert = | 1009 ssl_data->cert = |
| 1009 ImportCertFromFile(GetTestCertsDirectory(), "unittest.selfsigned.der"); | 1010 ImportCertFromFile(GetTestCertsDirectory(), "unittest.selfsigned.der"); |
| 1010 ASSERT_TRUE(ssl_data->cert.get()); | 1011 ASSERT_TRUE(ssl_data->cert.get()); |
| 1011 ssl_data_.push_back(ssl_data.Pass()); | 1012 ssl_data_.push_back(std::move(ssl_data)); |
| 1012 ssl_data.reset(new SSLSocketDataProvider(ASYNC, OK)); | 1013 ssl_data.reset(new SSLSocketDataProvider(ASYNC, OK)); |
| 1013 ssl_data_.push_back(ssl_data.Pass()); | 1014 ssl_data_.push_back(std::move(ssl_data)); |
| 1014 url_request_context_host_.AddRawExpectations(BuildNullSocketData()); | 1015 url_request_context_host_.AddRawExpectations(BuildNullSocketData()); |
| 1015 CreateAndConnectStandard("wss://localhost/", "localhost", "/", | 1016 CreateAndConnectStandard("wss://localhost/", "localhost", "/", |
| 1016 NoSubProtocols(), LocalhostOrigin(), "", ""); | 1017 NoSubProtocols(), LocalhostOrigin(), "", ""); |
| 1017 // WaitUntilConnectDone doesn't work in this case. | 1018 // WaitUntilConnectDone doesn't work in this case. |
| 1018 base::RunLoop().RunUntilIdle(); | 1019 base::RunLoop().RunUntilIdle(); |
| 1019 ASSERT_TRUE(ssl_error_callbacks_); | 1020 ASSERT_TRUE(ssl_error_callbacks_); |
| 1020 ssl_error_callbacks_->ContinueSSLRequest(); | 1021 ssl_error_callbacks_->ContinueSSLRequest(); |
| 1021 WaitUntilConnectDone(); | 1022 WaitUntilConnectDone(); |
| 1022 EXPECT_FALSE(has_failed()); | 1023 EXPECT_FALSE(has_failed()); |
| 1023 EXPECT_TRUE(stream_); | 1024 EXPECT_TRUE(stream_); |
| (...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1154 std::string request = | 1155 std::string request = |
| 1155 WebSocketStandardRequest("/", "localhost", LocalhostOrigin(), ""); | 1156 WebSocketStandardRequest("/", "localhost", LocalhostOrigin(), ""); |
| 1156 MockRead reads[] = { | 1157 MockRead reads[] = { |
| 1157 MockRead(SYNCHRONOUS, 1, kTruncatedResponse), | 1158 MockRead(SYNCHRONOUS, 1, kTruncatedResponse), |
| 1158 MockRead(SYNCHRONOUS, ERR_CONNECTION_CLOSED, 2), | 1159 MockRead(SYNCHRONOUS, ERR_CONNECTION_CLOSED, 2), |
| 1159 }; | 1160 }; |
| 1160 MockWrite writes[] = {MockWrite(SYNCHRONOUS, 0, request.c_str())}; | 1161 MockWrite writes[] = {MockWrite(SYNCHRONOUS, 0, request.c_str())}; |
| 1161 scoped_ptr<SequencedSocketData> socket_data(BuildSocketData(reads, writes)); | 1162 scoped_ptr<SequencedSocketData> socket_data(BuildSocketData(reads, writes)); |
| 1162 socket_data->set_connect_data(MockConnect(SYNCHRONOUS, OK)); | 1163 socket_data->set_connect_data(MockConnect(SYNCHRONOUS, OK)); |
| 1163 CreateAndConnectRawExpectations("ws://localhost/", NoSubProtocols(), | 1164 CreateAndConnectRawExpectations("ws://localhost/", NoSubProtocols(), |
| 1164 LocalhostOrigin(), socket_data.Pass()); | 1165 LocalhostOrigin(), std::move(socket_data)); |
| 1165 WaitUntilConnectDone(); | 1166 WaitUntilConnectDone(); |
| 1166 EXPECT_TRUE(has_failed()); | 1167 EXPECT_TRUE(has_failed()); |
| 1167 } | 1168 } |
| 1168 | 1169 |
| 1169 TEST_F(WebSocketStreamCreateTest, HandleErrTunnelConnectionFailed) { | 1170 TEST_F(WebSocketStreamCreateTest, HandleErrTunnelConnectionFailed) { |
| 1170 static const char kConnectRequest[] = | 1171 static const char kConnectRequest[] = |
| 1171 "CONNECT localhost:80 HTTP/1.1\r\n" | 1172 "CONNECT localhost:80 HTTP/1.1\r\n" |
| 1172 "Host: localhost:80\r\n" | 1173 "Host: localhost:80\r\n" |
| 1173 "Proxy-Connection: keep-alive\r\n" | 1174 "Proxy-Connection: keep-alive\r\n" |
| 1174 "\r\n"; | 1175 "\r\n"; |
| 1175 | 1176 |
| 1176 static const char kProxyResponse[] = | 1177 static const char kProxyResponse[] = |
| 1177 "HTTP/1.1 403 Forbidden\r\n" | 1178 "HTTP/1.1 403 Forbidden\r\n" |
| 1178 "Content-Type: text/html\r\n" | 1179 "Content-Type: text/html\r\n" |
| 1179 "Content-Length: 9\r\n" | 1180 "Content-Length: 9\r\n" |
| 1180 "Connection: keep-alive\r\n" | 1181 "Connection: keep-alive\r\n" |
| 1181 "\r\n" | 1182 "\r\n" |
| 1182 "Forbidden"; | 1183 "Forbidden"; |
| 1183 | 1184 |
| 1184 MockRead reads[] = {MockRead(SYNCHRONOUS, 1, kProxyResponse)}; | 1185 MockRead reads[] = {MockRead(SYNCHRONOUS, 1, kProxyResponse)}; |
| 1185 MockWrite writes[] = {MockWrite(SYNCHRONOUS, 0, kConnectRequest)}; | 1186 MockWrite writes[] = {MockWrite(SYNCHRONOUS, 0, kConnectRequest)}; |
| 1186 scoped_ptr<SequencedSocketData> socket_data(BuildSocketData(reads, writes)); | 1187 scoped_ptr<SequencedSocketData> socket_data(BuildSocketData(reads, writes)); |
| 1187 url_request_context_host_.SetProxyConfig("https=proxy:8000"); | 1188 url_request_context_host_.SetProxyConfig("https=proxy:8000"); |
| 1188 CreateAndConnectRawExpectations("ws://localhost/", NoSubProtocols(), | 1189 CreateAndConnectRawExpectations("ws://localhost/", NoSubProtocols(), |
| 1189 LocalhostOrigin(), socket_data.Pass()); | 1190 LocalhostOrigin(), std::move(socket_data)); |
| 1190 WaitUntilConnectDone(); | 1191 WaitUntilConnectDone(); |
| 1191 EXPECT_TRUE(has_failed()); | 1192 EXPECT_TRUE(has_failed()); |
| 1192 EXPECT_EQ("Establishing a tunnel via proxy server failed.", | 1193 EXPECT_EQ("Establishing a tunnel via proxy server failed.", |
| 1193 failure_message()); | 1194 failure_message()); |
| 1194 } | 1195 } |
| 1195 | 1196 |
| 1196 } // namespace | 1197 } // namespace |
| 1197 } // namespace net | 1198 } // namespace net |
| OLD | NEW |