ricea | 433bdab | 2015-01-26 07:25:37 | [diff] [blame] | 1 | // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | // End-to-end tests for WebSocket. |
| 6 | // |
| 7 | // A python server is (re)started for each test, which is moderately |
| 8 | // inefficient. However, it makes these tests a good fit for scenarios which |
| 9 | // require special server configurations. |
| 10 | |
tfarina | 8a2c66c2 | 2015-10-13 19:14:49 | [diff] [blame] | 11 | #include <stdint.h> |
ryansturm | 4bab0683 | 2016-03-03 23:41:07 | [diff] [blame] | 12 | |
danakj | 9c5cab5 | 2016-04-16 00:54:33 | [diff] [blame] | 13 | #include <memory> |
ricea | 433bdab | 2015-01-26 07:25:37 | [diff] [blame] | 14 | #include <string> |
| 15 | |
| 16 | #include "base/bind.h" |
| 17 | #include "base/bind_helpers.h" |
| 18 | #include "base/callback.h" |
skyostil | 4891b25b | 2015-06-11 11:43:45 | [diff] [blame] | 19 | #include "base/location.h" |
Avi Drissman | 13fc893 | 2015-12-20 04:40:46 | [diff] [blame] | 20 | #include "base/macros.h" |
danakj | 9c5cab5 | 2016-04-16 00:54:33 | [diff] [blame] | 21 | #include "base/memory/ptr_util.h" |
ricea | 433bdab | 2015-01-26 07:25:37 | [diff] [blame] | 22 | #include "base/run_loop.h" |
skyostil | 4891b25b | 2015-06-11 11:43:45 | [diff] [blame] | 23 | #include "base/single_thread_task_runner.h" |
Adam Rice | cb76ac6 | 2015-02-20 05:33:25 | [diff] [blame] | 24 | #include "base/strings/string_piece.h" |
gab | f767595f | 2016-05-11 18:50:35 | [diff] [blame] | 25 | #include "base/threading/thread_task_runner_handle.h" |
ricea | 433bdab | 2015-01-26 07:25:37 | [diff] [blame] | 26 | #include "net/base/auth.h" |
ryansturm | 7de050c | 2016-02-23 00:10:21 | [diff] [blame] | 27 | #include "net/base/proxy_delegate.h" |
ricea | 433bdab | 2015-01-26 07:25:37 | [diff] [blame] | 28 | #include "net/proxy/proxy_service.h" |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 29 | #include "net/test/embedded_test_server/embedded_test_server.h" |
ricea | 433bdab | 2015-01-26 07:25:37 | [diff] [blame] | 30 | #include "net/test/spawned_test_server/spawned_test_server.h" |
rsleevi | a69c79a | 2016-06-22 03:28:43 | [diff] [blame^] | 31 | #include "net/test/test_data_directory.h" |
ricea | 433bdab | 2015-01-26 07:25:37 | [diff] [blame] | 32 | #include "net/url_request/url_request_test_util.h" |
| 33 | #include "net/websockets/websocket_channel.h" |
| 34 | #include "net/websockets/websocket_event_interface.h" |
| 35 | #include "testing/gtest/include/gtest/gtest.h" |
mkwst | 4997ce8 | 2015-07-25 12:00:05 | [diff] [blame] | 36 | #include "url/origin.h" |
ricea | 433bdab | 2015-01-26 07:25:37 | [diff] [blame] | 37 | |
| 38 | namespace net { |
| 39 | |
| 40 | namespace { |
| 41 | |
| 42 | static const char kEchoServer[] = "echo-with-no-extension"; |
| 43 | |
Adam Rice | cb76ac6 | 2015-02-20 05:33:25 | [diff] [blame] | 44 | // Simplify changing URL schemes. |
| 45 | GURL ReplaceUrlScheme(const GURL& in_url, const base::StringPiece& scheme) { |
| 46 | GURL::Replacements replacements; |
| 47 | replacements.SetSchemeStr(scheme); |
| 48 | return in_url.ReplaceComponents(replacements); |
| 49 | } |
| 50 | |
ricea | 433bdab | 2015-01-26 07:25:37 | [diff] [blame] | 51 | // An implementation of WebSocketEventInterface that waits for and records the |
| 52 | // results of the connect. |
| 53 | class ConnectTestingEventInterface : public WebSocketEventInterface { |
| 54 | public: |
| 55 | ConnectTestingEventInterface(); |
| 56 | |
| 57 | void WaitForResponse(); |
| 58 | |
| 59 | bool failed() const { return failed_; } |
| 60 | |
| 61 | // Only set if the handshake failed, otherwise empty. |
| 62 | std::string failure_message() const; |
| 63 | |
| 64 | std::string selected_subprotocol() const; |
| 65 | |
| 66 | std::string extensions() const; |
| 67 | |
| 68 | // Implementation of WebSocketEventInterface. |
tyoshino | c06da56 | 2015-03-06 06:02:42 | [diff] [blame] | 69 | ChannelState OnAddChannelResponse(const std::string& selected_subprotocol, |
ricea | 433bdab | 2015-01-26 07:25:37 | [diff] [blame] | 70 | const std::string& extensions) override; |
| 71 | |
| 72 | ChannelState OnDataFrame(bool fin, |
| 73 | WebSocketMessageType type, |
| 74 | const std::vector<char>& data) override; |
| 75 | |
tfarina | 8a2c66c2 | 2015-10-13 19:14:49 | [diff] [blame] | 76 | ChannelState OnFlowControl(int64_t quota) override; |
ricea | 433bdab | 2015-01-26 07:25:37 | [diff] [blame] | 77 | |
| 78 | ChannelState OnClosingHandshake() override; |
| 79 | |
| 80 | ChannelState OnDropChannel(bool was_clean, |
tfarina | 8a2c66c2 | 2015-10-13 19:14:49 | [diff] [blame] | 81 | uint16_t code, |
ricea | 433bdab | 2015-01-26 07:25:37 | [diff] [blame] | 82 | const std::string& reason) override; |
| 83 | |
| 84 | ChannelState OnFailChannel(const std::string& message) override; |
| 85 | |
| 86 | ChannelState OnStartOpeningHandshake( |
danakj | 9c5cab5 | 2016-04-16 00:54:33 | [diff] [blame] | 87 | std::unique_ptr<WebSocketHandshakeRequestInfo> request) override; |
ricea | 433bdab | 2015-01-26 07:25:37 | [diff] [blame] | 88 | |
| 89 | ChannelState OnFinishOpeningHandshake( |
danakj | 9c5cab5 | 2016-04-16 00:54:33 | [diff] [blame] | 90 | std::unique_ptr<WebSocketHandshakeResponseInfo> response) override; |
ricea | 433bdab | 2015-01-26 07:25:37 | [diff] [blame] | 91 | |
| 92 | ChannelState OnSSLCertificateError( |
danakj | 9c5cab5 | 2016-04-16 00:54:33 | [diff] [blame] | 93 | std::unique_ptr<SSLErrorCallbacks> ssl_error_callbacks, |
ricea | 433bdab | 2015-01-26 07:25:37 | [diff] [blame] | 94 | const GURL& url, |
| 95 | const SSLInfo& ssl_info, |
| 96 | bool fatal) override; |
| 97 | |
| 98 | private: |
| 99 | void QuitNestedEventLoop(); |
| 100 | |
| 101 | // failed_ is true if the handshake failed (ie. OnFailChannel was called). |
| 102 | bool failed_; |
| 103 | std::string selected_subprotocol_; |
| 104 | std::string extensions_; |
| 105 | std::string failure_message_; |
| 106 | base::RunLoop run_loop_; |
| 107 | |
| 108 | DISALLOW_COPY_AND_ASSIGN(ConnectTestingEventInterface); |
| 109 | }; |
| 110 | |
tyoshino | c06da56 | 2015-03-06 06:02:42 | [diff] [blame] | 111 | ConnectTestingEventInterface::ConnectTestingEventInterface() : failed_(false) { |
ricea | 433bdab | 2015-01-26 07:25:37 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | void ConnectTestingEventInterface::WaitForResponse() { |
| 115 | run_loop_.Run(); |
| 116 | } |
| 117 | |
| 118 | std::string ConnectTestingEventInterface::failure_message() const { |
| 119 | return failure_message_; |
| 120 | } |
| 121 | |
| 122 | std::string ConnectTestingEventInterface::selected_subprotocol() const { |
| 123 | return selected_subprotocol_; |
| 124 | } |
| 125 | |
| 126 | std::string ConnectTestingEventInterface::extensions() const { |
| 127 | return extensions_; |
| 128 | } |
| 129 | |
| 130 | // Make the function definitions below less verbose. |
| 131 | typedef ConnectTestingEventInterface::ChannelState ChannelState; |
| 132 | |
| 133 | ChannelState ConnectTestingEventInterface::OnAddChannelResponse( |
ricea | 433bdab | 2015-01-26 07:25:37 | [diff] [blame] | 134 | const std::string& selected_subprotocol, |
| 135 | const std::string& extensions) { |
ricea | 433bdab | 2015-01-26 07:25:37 | [diff] [blame] | 136 | selected_subprotocol_ = selected_subprotocol; |
| 137 | extensions_ = extensions; |
| 138 | QuitNestedEventLoop(); |
tyoshino | c06da56 | 2015-03-06 06:02:42 | [diff] [blame] | 139 | return CHANNEL_ALIVE; |
ricea | 433bdab | 2015-01-26 07:25:37 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | ChannelState ConnectTestingEventInterface::OnDataFrame( |
| 143 | bool fin, |
| 144 | WebSocketMessageType type, |
| 145 | const std::vector<char>& data) { |
| 146 | return CHANNEL_ALIVE; |
| 147 | } |
| 148 | |
tfarina | 8a2c66c2 | 2015-10-13 19:14:49 | [diff] [blame] | 149 | ChannelState ConnectTestingEventInterface::OnFlowControl(int64_t quota) { |
ricea | 433bdab | 2015-01-26 07:25:37 | [diff] [blame] | 150 | return CHANNEL_ALIVE; |
| 151 | } |
| 152 | |
| 153 | ChannelState ConnectTestingEventInterface::OnClosingHandshake() { |
| 154 | return CHANNEL_ALIVE; |
| 155 | } |
| 156 | |
| 157 | ChannelState ConnectTestingEventInterface::OnDropChannel( |
| 158 | bool was_clean, |
tfarina | 8a2c66c2 | 2015-10-13 19:14:49 | [diff] [blame] | 159 | uint16_t code, |
ricea | 433bdab | 2015-01-26 07:25:37 | [diff] [blame] | 160 | const std::string& reason) { |
| 161 | return CHANNEL_DELETED; |
| 162 | } |
| 163 | |
| 164 | ChannelState ConnectTestingEventInterface::OnFailChannel( |
| 165 | const std::string& message) { |
| 166 | failed_ = true; |
| 167 | failure_message_ = message; |
| 168 | QuitNestedEventLoop(); |
| 169 | return CHANNEL_DELETED; |
| 170 | } |
| 171 | |
| 172 | ChannelState ConnectTestingEventInterface::OnStartOpeningHandshake( |
danakj | 9c5cab5 | 2016-04-16 00:54:33 | [diff] [blame] | 173 | std::unique_ptr<WebSocketHandshakeRequestInfo> request) { |
ricea | 433bdab | 2015-01-26 07:25:37 | [diff] [blame] | 174 | return CHANNEL_ALIVE; |
| 175 | } |
| 176 | |
| 177 | ChannelState ConnectTestingEventInterface::OnFinishOpeningHandshake( |
danakj | 9c5cab5 | 2016-04-16 00:54:33 | [diff] [blame] | 178 | std::unique_ptr<WebSocketHandshakeResponseInfo> response) { |
ricea | 433bdab | 2015-01-26 07:25:37 | [diff] [blame] | 179 | return CHANNEL_ALIVE; |
| 180 | } |
| 181 | |
| 182 | ChannelState ConnectTestingEventInterface::OnSSLCertificateError( |
danakj | 9c5cab5 | 2016-04-16 00:54:33 | [diff] [blame] | 183 | std::unique_ptr<SSLErrorCallbacks> ssl_error_callbacks, |
ricea | 433bdab | 2015-01-26 07:25:37 | [diff] [blame] | 184 | const GURL& url, |
| 185 | const SSLInfo& ssl_info, |
| 186 | bool fatal) { |
skyostil | 4891b25b | 2015-06-11 11:43:45 | [diff] [blame] | 187 | base::ThreadTaskRunnerHandle::Get()->PostTask( |
ricea | 433bdab | 2015-01-26 07:25:37 | [diff] [blame] | 188 | FROM_HERE, base::Bind(&SSLErrorCallbacks::CancelSSLRequest, |
| 189 | base::Owned(ssl_error_callbacks.release()), |
| 190 | ERR_SSL_PROTOCOL_ERROR, &ssl_info)); |
| 191 | return CHANNEL_ALIVE; |
| 192 | } |
| 193 | |
| 194 | void ConnectTestingEventInterface::QuitNestedEventLoop() { |
| 195 | run_loop_.Quit(); |
| 196 | } |
| 197 | |
| 198 | // A subclass of TestNetworkDelegate that additionally implements the |
| 199 | // OnResolveProxy callback and records the information passed to it. |
ryansturm | 7de050c | 2016-02-23 00:10:21 | [diff] [blame] | 200 | class TestProxyDelegateWithProxyInfo : public ProxyDelegate { |
ricea | 433bdab | 2015-01-26 07:25:37 | [diff] [blame] | 201 | public: |
ryansturm | 7de050c | 2016-02-23 00:10:21 | [diff] [blame] | 202 | TestProxyDelegateWithProxyInfo() {} |
ricea | 433bdab | 2015-01-26 07:25:37 | [diff] [blame] | 203 | |
| 204 | struct ResolvedProxyInfo { |
| 205 | GURL url; |
| 206 | ProxyInfo proxy_info; |
| 207 | }; |
| 208 | |
| 209 | const ResolvedProxyInfo& resolved_proxy_info() const { |
| 210 | return resolved_proxy_info_; |
| 211 | } |
| 212 | |
| 213 | protected: |
| 214 | void OnResolveProxy(const GURL& url, |
ryansturm | 4bab0683 | 2016-03-03 23:41:07 | [diff] [blame] | 215 | const std::string& method, |
ricea | 433bdab | 2015-01-26 07:25:37 | [diff] [blame] | 216 | int load_flags, |
| 217 | const ProxyService& proxy_service, |
| 218 | ProxyInfo* result) override { |
| 219 | resolved_proxy_info_.url = url; |
| 220 | resolved_proxy_info_.proxy_info = *result; |
| 221 | } |
| 222 | |
ryansturm | 7de050c | 2016-02-23 00:10:21 | [diff] [blame] | 223 | void OnTunnelConnectCompleted(const HostPortPair& endpoint, |
| 224 | const HostPortPair& proxy_server, |
| 225 | int net_error) override {} |
| 226 | void OnFallback(const ProxyServer& bad_proxy, int net_error) override {} |
ryansturm | 7de050c | 2016-02-23 00:10:21 | [diff] [blame] | 227 | void OnBeforeTunnelRequest(const HostPortPair& proxy_server, |
| 228 | HttpRequestHeaders* extra_headers) override {} |
| 229 | void OnTunnelHeadersReceived( |
| 230 | const HostPortPair& origin, |
| 231 | const HostPortPair& proxy_server, |
| 232 | const HttpResponseHeaders& response_headers) override {} |
| 233 | bool IsTrustedSpdyProxy(const net::ProxyServer& proxy_server) override { |
| 234 | return true; |
| 235 | } |
| 236 | |
ricea | 433bdab | 2015-01-26 07:25:37 | [diff] [blame] | 237 | private: |
| 238 | ResolvedProxyInfo resolved_proxy_info_; |
| 239 | |
ryansturm | 7de050c | 2016-02-23 00:10:21 | [diff] [blame] | 240 | DISALLOW_COPY_AND_ASSIGN(TestProxyDelegateWithProxyInfo); |
ricea | 433bdab | 2015-01-26 07:25:37 | [diff] [blame] | 241 | }; |
| 242 | |
| 243 | class WebSocketEndToEndTest : public ::testing::Test { |
| 244 | protected: |
| 245 | WebSocketEndToEndTest() |
Adam Rice | cb76ac6 | 2015-02-20 05:33:25 | [diff] [blame] | 246 | : event_interface_(), |
ryansturm | 7de050c | 2016-02-23 00:10:21 | [diff] [blame] | 247 | proxy_delegate_(new TestProxyDelegateWithProxyInfo), |
ricea | 433bdab | 2015-01-26 07:25:37 | [diff] [blame] | 248 | context_(true), |
Adam Rice | cb76ac6 | 2015-02-20 05:33:25 | [diff] [blame] | 249 | channel_(), |
ricea | 433bdab | 2015-01-26 07:25:37 | [diff] [blame] | 250 | initialised_context_(false) {} |
| 251 | |
| 252 | // Initialise the URLRequestContext. Normally done automatically by |
| 253 | // ConnectAndWait(). This method is for the use of tests that need the |
| 254 | // URLRequestContext initialised before calling ConnectAndWait(). |
| 255 | void InitialiseContext() { |
ryansturm | 7de050c | 2016-02-23 00:10:21 | [diff] [blame] | 256 | context_.set_proxy_delegate(proxy_delegate_.get()); |
ricea | 433bdab | 2015-01-26 07:25:37 | [diff] [blame] | 257 | context_.Init(); |
| 258 | initialised_context_ = true; |
| 259 | } |
| 260 | |
| 261 | // Send the connect request to |socket_url| and wait for a response. Returns |
| 262 | // true if the handshake succeeded. |
| 263 | bool ConnectAndWait(const GURL& socket_url) { |
| 264 | if (!initialised_context_) { |
| 265 | InitialiseContext(); |
| 266 | } |
mkwst | 4997ce8 | 2015-07-25 12:00:05 | [diff] [blame] | 267 | url::Origin origin(GURL("http://localhost")); |
Adam Rice | cb76ac6 | 2015-02-20 05:33:25 | [diff] [blame] | 268 | event_interface_ = new ConnectTestingEventInterface; |
| 269 | channel_.reset( |
danakj | 9c5cab5 | 2016-04-16 00:54:33 | [diff] [blame] | 270 | new WebSocketChannel(base::WrapUnique(event_interface_), &context_)); |
ricea | 5acb1faf7 | 2015-03-16 15:34:00 | [diff] [blame] | 271 | channel_->SendAddChannelRequest(GURL(socket_url), sub_protocols_, origin); |
ricea | 433bdab | 2015-01-26 07:25:37 | [diff] [blame] | 272 | event_interface_->WaitForResponse(); |
| 273 | return !event_interface_->failed(); |
| 274 | } |
| 275 | |
| 276 | ConnectTestingEventInterface* event_interface_; // owned by channel_ |
danakj | 9c5cab5 | 2016-04-16 00:54:33 | [diff] [blame] | 277 | std::unique_ptr<TestProxyDelegateWithProxyInfo> proxy_delegate_; |
ricea | 433bdab | 2015-01-26 07:25:37 | [diff] [blame] | 278 | TestURLRequestContext context_; |
danakj | 9c5cab5 | 2016-04-16 00:54:33 | [diff] [blame] | 279 | std::unique_ptr<WebSocketChannel> channel_; |
ricea | 5acb1faf7 | 2015-03-16 15:34:00 | [diff] [blame] | 280 | std::vector<std::string> sub_protocols_; |
ricea | 433bdab | 2015-01-26 07:25:37 | [diff] [blame] | 281 | bool initialised_context_; |
| 282 | }; |
| 283 | |
| 284 | // None of these tests work on Android. |
| 285 | // TODO(ricea): Make these tests work on Android. See crbug.com/441711. |
| 286 | #if defined(OS_ANDROID) |
| 287 | #define DISABLED_ON_ANDROID(test) DISABLED_##test |
| 288 | #else |
| 289 | #define DISABLED_ON_ANDROID(test) test |
| 290 | #endif |
| 291 | |
| 292 | // Basic test of connectivity. If this test fails, nothing else can be expected |
| 293 | // to work. |
| 294 | TEST_F(WebSocketEndToEndTest, DISABLED_ON_ANDROID(BasicSmokeTest)) { |
| 295 | SpawnedTestServer ws_server(SpawnedTestServer::TYPE_WS, |
| 296 | SpawnedTestServer::kLocalhost, |
| 297 | GetWebSocketTestDataDirectory()); |
| 298 | ASSERT_TRUE(ws_server.Start()); |
| 299 | EXPECT_TRUE(ConnectAndWait(ws_server.GetURL(kEchoServer))); |
| 300 | } |
| 301 | |
| 302 | // Test for issue crbug.com/433695 "Unencrypted WebSocket connection via |
| 303 | // authenticated proxy times out" |
| 304 | // TODO(ricea): Enable this when the issue is fixed. |
| 305 | TEST_F(WebSocketEndToEndTest, DISABLED_HttpsProxyUnauthedFails) { |
| 306 | SpawnedTestServer proxy_server(SpawnedTestServer::TYPE_BASIC_AUTH_PROXY, |
| 307 | SpawnedTestServer::kLocalhost, |
| 308 | base::FilePath()); |
| 309 | SpawnedTestServer ws_server(SpawnedTestServer::TYPE_WS, |
| 310 | SpawnedTestServer::kLocalhost, |
| 311 | GetWebSocketTestDataDirectory()); |
| 312 | ASSERT_TRUE(proxy_server.StartInBackground()); |
| 313 | ASSERT_TRUE(ws_server.StartInBackground()); |
| 314 | ASSERT_TRUE(proxy_server.BlockUntilStarted()); |
| 315 | ASSERT_TRUE(ws_server.BlockUntilStarted()); |
| 316 | std::string proxy_config = |
| 317 | "https=" + proxy_server.host_port_pair().ToString(); |
danakj | 9c5cab5 | 2016-04-16 00:54:33 | [diff] [blame] | 318 | std::unique_ptr<ProxyService> proxy_service( |
ricea | 433bdab | 2015-01-26 07:25:37 | [diff] [blame] | 319 | ProxyService::CreateFixed(proxy_config)); |
| 320 | ASSERT_TRUE(proxy_service); |
| 321 | context_.set_proxy_service(proxy_service.get()); |
| 322 | EXPECT_FALSE(ConnectAndWait(ws_server.GetURL(kEchoServer))); |
| 323 | EXPECT_EQ("Proxy authentication failed", event_interface_->failure_message()); |
| 324 | } |
| 325 | |
| 326 | TEST_F(WebSocketEndToEndTest, DISABLED_ON_ANDROID(HttpsWssProxyUnauthedFails)) { |
| 327 | SpawnedTestServer proxy_server(SpawnedTestServer::TYPE_BASIC_AUTH_PROXY, |
| 328 | SpawnedTestServer::kLocalhost, |
| 329 | base::FilePath()); |
| 330 | SpawnedTestServer wss_server(SpawnedTestServer::TYPE_WSS, |
| 331 | SpawnedTestServer::kLocalhost, |
| 332 | GetWebSocketTestDataDirectory()); |
| 333 | ASSERT_TRUE(proxy_server.StartInBackground()); |
| 334 | ASSERT_TRUE(wss_server.StartInBackground()); |
| 335 | ASSERT_TRUE(proxy_server.BlockUntilStarted()); |
| 336 | ASSERT_TRUE(wss_server.BlockUntilStarted()); |
| 337 | std::string proxy_config = |
| 338 | "https=" + proxy_server.host_port_pair().ToString(); |
danakj | 9c5cab5 | 2016-04-16 00:54:33 | [diff] [blame] | 339 | std::unique_ptr<ProxyService> proxy_service( |
ricea | 433bdab | 2015-01-26 07:25:37 | [diff] [blame] | 340 | ProxyService::CreateFixed(proxy_config)); |
| 341 | ASSERT_TRUE(proxy_service); |
| 342 | context_.set_proxy_service(proxy_service.get()); |
| 343 | EXPECT_FALSE(ConnectAndWait(wss_server.GetURL(kEchoServer))); |
| 344 | EXPECT_EQ("Proxy authentication failed", event_interface_->failure_message()); |
| 345 | } |
| 346 | |
| 347 | // Regression test for crbug/426736 "WebSocket connections not using configured |
| 348 | // system HTTPS Proxy". |
| 349 | TEST_F(WebSocketEndToEndTest, DISABLED_ON_ANDROID(HttpsProxyUsed)) { |
| 350 | SpawnedTestServer proxy_server(SpawnedTestServer::TYPE_BASIC_AUTH_PROXY, |
| 351 | SpawnedTestServer::kLocalhost, |
| 352 | base::FilePath()); |
| 353 | SpawnedTestServer ws_server(SpawnedTestServer::TYPE_WS, |
| 354 | SpawnedTestServer::kLocalhost, |
| 355 | GetWebSocketTestDataDirectory()); |
| 356 | ASSERT_TRUE(proxy_server.StartInBackground()); |
| 357 | ASSERT_TRUE(ws_server.StartInBackground()); |
| 358 | ASSERT_TRUE(proxy_server.BlockUntilStarted()); |
| 359 | ASSERT_TRUE(ws_server.BlockUntilStarted()); |
| 360 | std::string proxy_config = "https=" + |
| 361 | proxy_server.host_port_pair().ToString() + ";" + |
| 362 | "http=" + proxy_server.host_port_pair().ToString(); |
danakj | 9c5cab5 | 2016-04-16 00:54:33 | [diff] [blame] | 363 | std::unique_ptr<ProxyService> proxy_service( |
ricea | 433bdab | 2015-01-26 07:25:37 | [diff] [blame] | 364 | ProxyService::CreateFixed(proxy_config)); |
| 365 | context_.set_proxy_service(proxy_service.get()); |
| 366 | InitialiseContext(); |
| 367 | |
| 368 | // The test server doesn't have an unauthenticated proxy mode. WebSockets |
| 369 | // cannot provide auth information that isn't already cached, so it's |
| 370 | // necessary to preflight an HTTP request to authenticate against the proxy. |
ricea | 433bdab | 2015-01-26 07:25:37 | [diff] [blame] | 371 | // It doesn't matter what the URL is, as long as it is an HTTP navigation. |
| 372 | GURL http_page = |
Adam Rice | cb76ac6 | 2015-02-20 05:33:25 | [diff] [blame] | 373 | ReplaceUrlScheme(ws_server.GetURL("connect_check.html"), "http"); |
ricea | 433bdab | 2015-01-26 07:25:37 | [diff] [blame] | 374 | TestDelegate delegate; |
| 375 | delegate.set_credentials( |
| 376 | AuthCredentials(base::ASCIIToUTF16("foo"), base::ASCIIToUTF16("bar"))); |
| 377 | { |
danakj | 9c5cab5 | 2016-04-16 00:54:33 | [diff] [blame] | 378 | std::unique_ptr<URLRequest> request( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 379 | context_.CreateRequest(http_page, DEFAULT_PRIORITY, &delegate)); |
ricea | 433bdab | 2015-01-26 07:25:37 | [diff] [blame] | 380 | request->Start(); |
| 381 | // TestDelegate exits the message loop when the request completes by |
| 382 | // default. |
| 383 | base::RunLoop().Run(); |
| 384 | EXPECT_TRUE(delegate.auth_required_called()); |
| 385 | } |
| 386 | |
| 387 | GURL ws_url = ws_server.GetURL(kEchoServer); |
| 388 | EXPECT_TRUE(ConnectAndWait(ws_url)); |
ryansturm | 7de050c | 2016-02-23 00:10:21 | [diff] [blame] | 389 | const TestProxyDelegateWithProxyInfo::ResolvedProxyInfo& info = |
| 390 | proxy_delegate_->resolved_proxy_info(); |
ricea | 433bdab | 2015-01-26 07:25:37 | [diff] [blame] | 391 | EXPECT_EQ(ws_url, info.url); |
| 392 | EXPECT_TRUE(info.proxy_info.is_http()); |
| 393 | } |
| 394 | |
ricea | 23c3f94 | 2015-02-02 13:35:13 | [diff] [blame] | 395 | // This is a regression test for crbug.com/408061 Crash in |
| 396 | // net::WebSocketBasicHandshakeStream::Upgrade. |
| 397 | TEST_F(WebSocketEndToEndTest, DISABLED_ON_ANDROID(TruncatedResponse)) { |
| 398 | SpawnedTestServer ws_server(SpawnedTestServer::TYPE_WS, |
| 399 | SpawnedTestServer::kLocalhost, |
| 400 | GetWebSocketTestDataDirectory()); |
| 401 | ASSERT_TRUE(ws_server.Start()); |
| 402 | InitialiseContext(); |
| 403 | |
| 404 | GURL ws_url = ws_server.GetURL("truncated-headers"); |
| 405 | EXPECT_FALSE(ConnectAndWait(ws_url)); |
| 406 | } |
| 407 | |
Adam Rice | cb76ac6 | 2015-02-20 05:33:25 | [diff] [blame] | 408 | // Regression test for crbug.com/455215 "HSTS not applied to WebSocket" |
| 409 | TEST_F(WebSocketEndToEndTest, DISABLED_ON_ANDROID(HstsHttpsToWebSocket)) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 410 | EmbeddedTestServer https_server(net::EmbeddedTestServer::Type::TYPE_HTTPS); |
| 411 | https_server.SetSSLConfig( |
| 412 | net::EmbeddedTestServer::CERT_COMMON_NAME_IS_DOMAIN); |
| 413 | https_server.ServeFilesFromSourceDirectory("net/data/url_request_unittest"); |
| 414 | |
estark | a5da7670 | 2015-04-09 04:00:16 | [diff] [blame] | 415 | SpawnedTestServer::SSLOptions ssl_options( |
| 416 | SpawnedTestServer::SSLOptions::CERT_COMMON_NAME_IS_DOMAIN); |
Adam Rice | cb76ac6 | 2015-02-20 05:33:25 | [diff] [blame] | 417 | SpawnedTestServer wss_server(SpawnedTestServer::TYPE_WSS, ssl_options, |
| 418 | GetWebSocketTestDataDirectory()); |
estark | a5da7670 | 2015-04-09 04:00:16 | [diff] [blame] | 419 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 420 | ASSERT_TRUE(https_server.Start()); |
| 421 | ASSERT_TRUE(wss_server.Start()); |
Adam Rice | cb76ac6 | 2015-02-20 05:33:25 | [diff] [blame] | 422 | InitialiseContext(); |
| 423 | // Set HSTS via https: |
| 424 | TestDelegate delegate; |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 425 | GURL https_page = https_server.GetURL("/hsts-headers.html"); |
danakj | 9c5cab5 | 2016-04-16 00:54:33 | [diff] [blame] | 426 | std::unique_ptr<URLRequest> request( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 427 | context_.CreateRequest(https_page, DEFAULT_PRIORITY, &delegate)); |
Adam Rice | cb76ac6 | 2015-02-20 05:33:25 | [diff] [blame] | 428 | request->Start(); |
| 429 | // TestDelegate exits the message loop when the request completes. |
| 430 | base::RunLoop().Run(); |
| 431 | EXPECT_TRUE(request->status().is_success()); |
| 432 | |
| 433 | // Check HSTS with ws: |
| 434 | // Change the scheme from wss: to ws: to verify that it is switched back. |
| 435 | GURL ws_url = ReplaceUrlScheme(wss_server.GetURL(kEchoServer), "ws"); |
| 436 | EXPECT_TRUE(ConnectAndWait(ws_url)); |
| 437 | } |
| 438 | |
| 439 | TEST_F(WebSocketEndToEndTest, DISABLED_ON_ANDROID(HstsWebSocketToHttps)) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 440 | EmbeddedTestServer https_server(net::EmbeddedTestServer::Type::TYPE_HTTPS); |
| 441 | https_server.SetSSLConfig( |
| 442 | net::EmbeddedTestServer::CERT_COMMON_NAME_IS_DOMAIN); |
| 443 | https_server.ServeFilesFromSourceDirectory("net/data/url_request_unittest"); |
| 444 | |
estark | a5da7670 | 2015-04-09 04:00:16 | [diff] [blame] | 445 | SpawnedTestServer::SSLOptions ssl_options( |
| 446 | SpawnedTestServer::SSLOptions::CERT_COMMON_NAME_IS_DOMAIN); |
Adam Rice | cb76ac6 | 2015-02-20 05:33:25 | [diff] [blame] | 447 | SpawnedTestServer wss_server(SpawnedTestServer::TYPE_WSS, ssl_options, |
| 448 | GetWebSocketTestDataDirectory()); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 449 | ASSERT_TRUE(https_server.Start()); |
| 450 | ASSERT_TRUE(wss_server.Start()); |
Adam Rice | cb76ac6 | 2015-02-20 05:33:25 | [diff] [blame] | 451 | InitialiseContext(); |
| 452 | // Set HSTS via wss: |
| 453 | GURL wss_url = wss_server.GetURL("set-hsts"); |
| 454 | EXPECT_TRUE(ConnectAndWait(wss_url)); |
| 455 | |
| 456 | // Verify via http: |
| 457 | TestDelegate delegate; |
| 458 | GURL http_page = |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 459 | ReplaceUrlScheme(https_server.GetURL("/simple.html"), "http"); |
danakj | 9c5cab5 | 2016-04-16 00:54:33 | [diff] [blame] | 460 | std::unique_ptr<URLRequest> request( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 461 | context_.CreateRequest(http_page, DEFAULT_PRIORITY, &delegate)); |
Adam Rice | cb76ac6 | 2015-02-20 05:33:25 | [diff] [blame] | 462 | request->Start(); |
| 463 | // TestDelegate exits the message loop when the request completes. |
| 464 | base::RunLoop().Run(); |
| 465 | EXPECT_TRUE(request->status().is_success()); |
| 466 | EXPECT_TRUE(request->url().SchemeIs("https")); |
| 467 | } |
| 468 | |
| 469 | TEST_F(WebSocketEndToEndTest, DISABLED_ON_ANDROID(HstsWebSocketToWebSocket)) { |
estark | a5da7670 | 2015-04-09 04:00:16 | [diff] [blame] | 470 | SpawnedTestServer::SSLOptions ssl_options( |
| 471 | SpawnedTestServer::SSLOptions::CERT_COMMON_NAME_IS_DOMAIN); |
Adam Rice | cb76ac6 | 2015-02-20 05:33:25 | [diff] [blame] | 472 | SpawnedTestServer wss_server(SpawnedTestServer::TYPE_WSS, ssl_options, |
| 473 | GetWebSocketTestDataDirectory()); |
| 474 | ASSERT_TRUE(wss_server.Start()); |
| 475 | InitialiseContext(); |
| 476 | // Set HSTS via wss: |
| 477 | GURL wss_url = wss_server.GetURL("set-hsts"); |
| 478 | EXPECT_TRUE(ConnectAndWait(wss_url)); |
| 479 | |
| 480 | // Verify via wss: |
| 481 | GURL ws_url = ReplaceUrlScheme(wss_server.GetURL(kEchoServer), "ws"); |
| 482 | EXPECT_TRUE(ConnectAndWait(ws_url)); |
| 483 | } |
| 484 | |
ricea | 5acb1faf7 | 2015-03-16 15:34:00 | [diff] [blame] | 485 | // Regression test for crbug.com/180504 "WebSocket handshake fails when HTTP |
| 486 | // headers have trailing LWS". |
| 487 | TEST_F(WebSocketEndToEndTest, DISABLED_ON_ANDROID(TrailingWhitespace)) { |
| 488 | SpawnedTestServer ws_server(SpawnedTestServer::TYPE_WS, |
| 489 | SpawnedTestServer::kLocalhost, |
| 490 | GetWebSocketTestDataDirectory()); |
| 491 | ASSERT_TRUE(ws_server.Start()); |
| 492 | |
| 493 | GURL ws_url = ws_server.GetURL("trailing-whitespace"); |
| 494 | sub_protocols_.push_back("sip"); |
| 495 | EXPECT_TRUE(ConnectAndWait(ws_url)); |
| 496 | EXPECT_EQ("sip", event_interface_->selected_subprotocol()); |
| 497 | } |
| 498 | |
ricea | e1d6767 | 2015-03-19 10:10:17 | [diff] [blame] | 499 | // This is a regression test for crbug.com/169448 "WebSockets should support |
| 500 | // header continuations" |
| 501 | // TODO(ricea): HTTP continuation headers have been deprecated by RFC7230. If |
| 502 | // support for continuation headers is removed from Chrome, then this test will |
| 503 | // break and should be removed. |
| 504 | TEST_F(WebSocketEndToEndTest, DISABLED_ON_ANDROID(HeaderContinuations)) { |
| 505 | SpawnedTestServer ws_server(SpawnedTestServer::TYPE_WS, |
| 506 | SpawnedTestServer::kLocalhost, |
| 507 | GetWebSocketTestDataDirectory()); |
| 508 | ASSERT_TRUE(ws_server.Start()); |
| 509 | |
| 510 | GURL ws_url = ws_server.GetURL("header-continuation"); |
| 511 | |
| 512 | EXPECT_TRUE(ConnectAndWait(ws_url)); |
| 513 | EXPECT_EQ("permessage-deflate; server_max_window_bits=10", |
| 514 | event_interface_->extensions()); |
| 515 | } |
| 516 | |
ricea | 433bdab | 2015-01-26 07:25:37 | [diff] [blame] | 517 | } // namespace |
| 518 | |
| 519 | } // namespace net |