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 <string> | 5 #include <string> |
6 | 6 |
7 #include "base/strings/string_util.h" | 7 #include "base/strings/string_util.h" |
8 #include "base/strings/utf_string_conversions.h" | 8 #include "base/strings/utf_string_conversions.h" |
9 #include "chrome/browser/chrome_notification_types.h" | 9 #include "chrome/browser/chrome_notification_types.h" |
10 #include "chrome/browser/ui/browser.h" | 10 #include "chrome/browser/ui/browser.h" |
11 #include "chrome/browser/ui/login/login_prompt.h" | 11 #include "chrome/browser/ui/login/login_prompt.h" |
12 #include "chrome/browser/ui/login/login_prompt_test_utils.h" | 12 #include "chrome/browser/ui/login/login_prompt_test_utils.h" |
13 #include "chrome/browser/ui/tabs/tab_strip_model.h" | 13 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
14 #include "chrome/test/base/in_process_browser_test.h" | 14 #include "chrome/test/base/in_process_browser_test.h" |
15 #include "chrome/test/base/ui_test_utils.h" | 15 #include "chrome/test/base/ui_test_utils.h" |
16 #include "content/public/browser/navigation_controller.h" | 16 #include "content/public/browser/navigation_controller.h" |
17 #include "content/public/browser/notification_details.h" | 17 #include "content/public/browser/notification_details.h" |
18 #include "content/public/browser/notification_registrar.h" | 18 #include "content/public/browser/notification_registrar.h" |
19 #include "content/public/browser/notification_source.h" | 19 #include "content/public/browser/notification_source.h" |
20 #include "content/public/browser/web_contents.h" | 20 #include "content/public/browser/web_contents.h" |
21 #include "content/public/test/browser_test_utils.h" | 21 #include "content/public/test/browser_test_utils.h" |
22 #include "net/base/test_data_directory.h" | 22 #include "net/base/test_data_directory.h" |
| 23 #include "net/test/embedded_test_server/embedded_test_server.h" |
23 #include "net/test/spawned_test_server/spawned_test_server.h" | 24 #include "net/test/spawned_test_server/spawned_test_server.h" |
24 #include "url/gurl.h" | 25 #include "url/gurl.h" |
25 | 26 |
26 namespace { | 27 namespace { |
27 | 28 |
28 class WebSocketBrowserTest : public InProcessBrowserTest { | 29 class WebSocketBrowserTest : public InProcessBrowserTest { |
29 public: | 30 public: |
30 WebSocketBrowserTest() | 31 WebSocketBrowserTest() |
31 : ws_server_(net::SpawnedTestServer::TYPE_WS, | 32 : ws_server_(net::SpawnedTestServer::TYPE_WS, |
32 net::SpawnedTestServer::kLocalhost, | 33 net::SpawnedTestServer::kLocalhost, |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
73 typedef net::SpawnedTestServer::SSLOptions SSLOptions; | 74 typedef net::SpawnedTestServer::SSLOptions SSLOptions; |
74 scoped_ptr<content::TitleWatcher> watcher_; | 75 scoped_ptr<content::TitleWatcher> watcher_; |
75 | 76 |
76 DISALLOW_COPY_AND_ASSIGN(WebSocketBrowserTest); | 77 DISALLOW_COPY_AND_ASSIGN(WebSocketBrowserTest); |
77 }; | 78 }; |
78 | 79 |
79 // Framework for tests using the connect_to.html page served by a separate HTTP | 80 // Framework for tests using the connect_to.html page served by a separate HTTP |
80 // server. | 81 // server. |
81 class WebSocketBrowserConnectToTest : public WebSocketBrowserTest { | 82 class WebSocketBrowserConnectToTest : public WebSocketBrowserTest { |
82 protected: | 83 protected: |
83 WebSocketBrowserConnectToTest() | 84 WebSocketBrowserConnectToTest() { |
84 : http_server_(net::SpawnedTestServer::TYPE_HTTP, | 85 http_server_.ServeFilesFromSourceDirectory( |
85 net::SpawnedTestServer::kLocalhost, | 86 net::GetWebSocketTestDataDirectory()); |
86 net::GetWebSocketTestDataDirectory()) {} | 87 } |
87 | 88 |
88 // The title watcher and HTTP server are set up automatically by the test | 89 // The title watcher and HTTP server are set up automatically by the test |
89 // framework. Each test case still needs to configure and start the | 90 // framework. Each test case still needs to configure and start the |
90 // WebSocket server(s) it needs. | 91 // WebSocket server(s) it needs. |
91 void SetUpOnMainThread() override { | 92 void SetUpOnMainThread() override { |
92 WebSocketBrowserTest::SetUpOnMainThread(); | 93 WebSocketBrowserTest::SetUpOnMainThread(); |
93 ASSERT_TRUE(http_server_.StartInBackground()); | 94 ASSERT_TRUE(http_server_.Start()); |
94 } | 95 } |
95 | 96 |
96 // Supply a ws: or wss: URL to connect to. | 97 // Supply a ws: or wss: URL to connect to. |
97 void ConnectTo(GURL url) { | 98 void ConnectTo(GURL url) { |
98 ASSERT_TRUE(http_server_.BlockUntilStarted()); | 99 ASSERT_TRUE(http_server_.Started()); |
99 std::string query("url=" + url.spec()); | 100 std::string query("url=" + url.spec()); |
100 GURL::Replacements replacements; | 101 GURL::Replacements replacements; |
101 replacements.SetQueryStr(query); | 102 replacements.SetQueryStr(query); |
102 ui_test_utils::NavigateToURL(browser(), | 103 ui_test_utils::NavigateToURL(browser(), |
103 http_server_.GetURL("files/connect_to.html") | 104 http_server_.GetURL("/connect_to.html") |
104 .ReplaceComponents(replacements)); | 105 .ReplaceComponents(replacements)); |
105 } | 106 } |
106 | 107 |
107 private: | 108 private: |
108 net::SpawnedTestServer http_server_; | 109 net::EmbeddedTestServer http_server_; |
109 }; | 110 }; |
110 | 111 |
111 // Automatically fill in any login prompts that appear with the supplied | 112 // Automatically fill in any login prompts that appear with the supplied |
112 // credentials. | 113 // credentials. |
113 class AutoLogin : public content::NotificationObserver { | 114 class AutoLogin : public content::NotificationObserver { |
114 public: | 115 public: |
115 AutoLogin(const std::string& username, | 116 AutoLogin(const std::string& username, |
116 const std::string& password, | 117 const std::string& password, |
117 content::NavigationController* navigation_controller) | 118 content::NavigationController* navigation_controller) |
118 : username_(base::UTF8ToUTF16(username)), | 119 : username_(base::UTF8ToUTF16(username)), |
(...skipping 172 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
291 IN_PROC_BROWSER_TEST_F(WebSocketBrowserTest, SSLConnectionLimit) { | 292 IN_PROC_BROWSER_TEST_F(WebSocketBrowserTest, SSLConnectionLimit) { |
292 ASSERT_TRUE(wss_server_.Start()); | 293 ASSERT_TRUE(wss_server_.Start()); |
293 | 294 |
294 NavigateToHTTPS("multiple-connections.html"); | 295 NavigateToHTTPS("multiple-connections.html"); |
295 | 296 |
296 EXPECT_EQ("PASS", WaitAndGetTitle()); | 297 EXPECT_EQ("PASS", WaitAndGetTitle()); |
297 } | 298 } |
298 | 299 |
299 // Regression test for crbug.com/903553005 | 300 // Regression test for crbug.com/903553005 |
300 IN_PROC_BROWSER_TEST_F(WebSocketBrowserTest, WebSocketAppliesHSTS) { | 301 IN_PROC_BROWSER_TEST_F(WebSocketBrowserTest, WebSocketAppliesHSTS) { |
301 net::SpawnedTestServer https_server( | 302 net::EmbeddedTestServer https_server(net::EmbeddedTestServer::TYPE_HTTPS); |
302 net::SpawnedTestServer::TYPE_HTTPS, | 303 https_server.SetSSLConfig( |
303 net::SpawnedTestServer::SSLOptions( | 304 net::EmbeddedTestServer::CERT_COMMON_NAME_IS_DOMAIN); |
304 net::SpawnedTestServer::SSLOptions::CERT_COMMON_NAME_IS_DOMAIN), | 305 https_server.ServeFilesFromSourceDirectory("chrome/test/data"); |
305 base::FilePath(FILE_PATH_LITERAL("chrome/test/data"))); | |
306 net::SpawnedTestServer wss_server( | 306 net::SpawnedTestServer wss_server( |
307 net::SpawnedTestServer::TYPE_WSS, | 307 net::SpawnedTestServer::TYPE_WSS, |
308 net::SpawnedTestServer::SSLOptions( | 308 net::SpawnedTestServer::SSLOptions( |
309 net::SpawnedTestServer::SSLOptions::CERT_COMMON_NAME_IS_DOMAIN), | 309 net::SpawnedTestServer::SSLOptions::CERT_COMMON_NAME_IS_DOMAIN), |
310 net::GetWebSocketTestDataDirectory()); | 310 net::GetWebSocketTestDataDirectory()); |
311 // This test sets HSTS on localhost. To avoid being redirected to https, start | 311 // This test sets HSTS on localhost. To avoid being redirected to https, start |
312 // the http server on 127.0.0.1 instead. | 312 // the http server on 127.0.0.1 instead. |
313 net::SpawnedTestServer http_server( | 313 net::EmbeddedTestServer http_server; |
314 net::SpawnedTestServer::TYPE_HTTP, net::SpawnedTestServer::kLocalhost, | 314 http_server.ServeFilesFromSourceDirectory("chrome/test/data"); |
315 base::FilePath(FILE_PATH_LITERAL("chrome/test/data"))); | 315 ASSERT_TRUE(https_server.Start()); |
316 ASSERT_TRUE(https_server.StartInBackground()); | 316 ASSERT_TRUE(http_server.Start()); |
317 ASSERT_TRUE(http_server.StartInBackground()); | |
318 ASSERT_TRUE(wss_server.StartInBackground()); | 317 ASSERT_TRUE(wss_server.StartInBackground()); |
319 ASSERT_TRUE(https_server.BlockUntilStarted()); | |
320 | 318 |
321 // Set HSTS on localhost. | 319 // Set HSTS on localhost. |
322 content::TitleWatcher title_watcher( | 320 content::TitleWatcher title_watcher( |
323 browser()->tab_strip_model()->GetActiveWebContents(), | 321 browser()->tab_strip_model()->GetActiveWebContents(), |
324 base::ASCIIToUTF16("SET")); | 322 base::ASCIIToUTF16("SET")); |
325 ui_test_utils::NavigateToURL( | 323 ui_test_utils::NavigateToURL(browser(), |
326 browser(), https_server.GetURL("files/websocket/set-hsts.html")); | 324 https_server.GetURL("/websocket/set-hsts.html")); |
327 const base::string16 result = title_watcher.WaitAndGetTitle(); | 325 const base::string16 result = title_watcher.WaitAndGetTitle(); |
328 EXPECT_TRUE(base::EqualsASCII(result, "SET")); | 326 EXPECT_TRUE(base::EqualsASCII(result, "SET")); |
329 | 327 |
330 // Verify that it applies to WebSockets. | 328 // Verify that it applies to WebSockets. |
331 ASSERT_TRUE(wss_server.BlockUntilStarted()); | 329 ASSERT_TRUE(wss_server.BlockUntilStarted()); |
332 GURL wss_url = wss_server.GetURL("echo-with-no-extension"); | 330 GURL wss_url = wss_server.GetURL("echo-with-no-extension"); |
333 std::string scheme("ws"); | 331 std::string scheme("ws"); |
334 GURL::Replacements scheme_replacement; | 332 GURL::Replacements scheme_replacement; |
335 scheme_replacement.SetSchemeStr(scheme); | 333 scheme_replacement.SetSchemeStr(scheme); |
336 GURL ws_url = wss_url.ReplaceComponents(scheme_replacement); | 334 GURL ws_url = wss_url.ReplaceComponents(scheme_replacement); |
337 | 335 |
338 // An https: URL won't work here here because the mixed content policy | 336 // An https: URL won't work here here because the mixed content policy |
339 // disallows connections to unencrypted WebSockets from encrypted pages. | 337 // disallows connections to unencrypted WebSockets from encrypted pages. |
340 ASSERT_TRUE(http_server.BlockUntilStarted()); | |
341 GURL http_url = | 338 GURL http_url = |
342 http_server.GetURL("files/websocket/check-hsts.html#" + ws_url.spec()); | 339 http_server.GetURL("/websocket/check-hsts.html#" + ws_url.spec()); |
343 | 340 |
344 ui_test_utils::NavigateToURL(browser(), http_url); | 341 ui_test_utils::NavigateToURL(browser(), http_url); |
345 | 342 |
346 EXPECT_EQ("PASS", WaitAndGetTitle()); | 343 EXPECT_EQ("PASS", WaitAndGetTitle()); |
347 } | 344 } |
348 | 345 |
349 } // namespace | 346 } // namespace |
OLD | NEW |