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 "base/compiler_specific.h" |
| 6 #include "base/memory/ref_counted.h" |
| 7 #include "base/memory/scoped_ptr.h" |
5 #include "base/strings/string_util.h" | 8 #include "base/strings/string_util.h" |
6 #include "base/strings/utf_string_conversions.h" | 9 #include "base/strings/utf_string_conversions.h" |
| 10 #include "chrome/browser/chrome_notification_types.h" |
7 #include "chrome/browser/ui/browser.h" | 11 #include "chrome/browser/ui/browser.h" |
| 12 #include "chrome/browser/ui/login/login_prompt.h" |
8 #include "chrome/browser/ui/tabs/tab_strip_model.h" | 13 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
9 #include "chrome/test/base/in_process_browser_test.h" | 14 #include "chrome/test/base/in_process_browser_test.h" |
10 #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" |
| 17 #include "content/public/browser/notification_details.h" |
| 18 #include "content/public/browser/notification_registrar.h" |
| 19 #include "content/public/browser/notification_source.h" |
| 20 #include "content/public/browser/web_contents.h" |
11 #include "content/public/test/browser_test_utils.h" | 21 #include "content/public/test/browser_test_utils.h" |
12 #include "net/base/test_data_directory.h" | 22 #include "net/base/test_data_directory.h" |
13 #include "net/test/spawned_test_server/spawned_test_server.h" | 23 #include "net/test/spawned_test_server/spawned_test_server.h" |
| 24 #include "url/gurl.h" |
14 | 25 |
15 namespace { | 26 namespace { |
16 | 27 |
17 class WebSocketBrowserTest : public InProcessBrowserTest { | 28 class WebSocketBrowserTest : public InProcessBrowserTest { |
18 public: | 29 public: |
19 WebSocketBrowserTest() | 30 WebSocketBrowserTest() |
20 : ws_server_(net::SpawnedTestServer::TYPE_WS, | 31 : ws_server_(net::SpawnedTestServer::TYPE_WS, |
21 net::SpawnedTestServer::kLocalhost, | 32 net::SpawnedTestServer::kLocalhost, |
22 net::GetWebSocketTestDataDirectory()), | 33 net::GetWebSocketTestDataDirectory()), |
23 wss_server_(net::SpawnedTestServer::TYPE_WSS, | 34 wss_server_(net::SpawnedTestServer::TYPE_WSS, |
24 SSLOptions(SSLOptions::CERT_OK), | 35 SSLOptions(SSLOptions::CERT_OK), |
25 net::GetWebSocketTestDataDirectory()) { | 36 net::GetWebSocketTestDataDirectory()) {} |
| 37 |
| 38 protected: |
| 39 // Prepare the title watcher. |
| 40 virtual void SetUpOnMainThread() OVERRIDE { |
| 41 watcher_.reset(new content::TitleWatcher( |
| 42 browser()->tab_strip_model()->GetActiveWebContents(), |
| 43 base::ASCIIToUTF16("PASS"))); |
| 44 watcher_->AlsoWaitForTitle(base::ASCIIToUTF16("FAIL")); |
26 } | 45 } |
27 | 46 |
28 protected: | 47 virtual void CleanUpOnMainThread() OVERRIDE { watcher_.reset(); } |
| 48 |
| 49 std::string WaitAndGetTitle() { |
| 50 return base::UTF16ToUTF8(watcher_->WaitAndGetTitle()); |
| 51 } |
| 52 |
29 net::SpawnedTestServer ws_server_; | 53 net::SpawnedTestServer ws_server_; |
30 net::SpawnedTestServer wss_server_; | 54 net::SpawnedTestServer wss_server_; |
31 | 55 |
32 private: | 56 private: |
33 typedef net::SpawnedTestServer::SSLOptions SSLOptions; | 57 typedef net::SpawnedTestServer::SSLOptions SSLOptions; |
| 58 scoped_ptr<content::TitleWatcher> watcher_; |
34 | 59 |
35 DISALLOW_COPY_AND_ASSIGN(WebSocketBrowserTest); | 60 DISALLOW_COPY_AND_ASSIGN(WebSocketBrowserTest); |
36 }; | 61 }; |
37 | 62 |
| 63 // Framework for tests using the connect_to.html page served by a separate HTTP |
| 64 // server. |
| 65 class WebSocketBrowserConnectToTest : public WebSocketBrowserTest { |
| 66 protected: |
| 67 WebSocketBrowserConnectToTest() |
| 68 : http_server_(net::SpawnedTestServer::TYPE_HTTP, |
| 69 net::SpawnedTestServer::kLocalhost, |
| 70 net::GetWebSocketTestDataDirectory()) {} |
| 71 |
| 72 // The title watcher and HTTP server are set up automatically by the test |
| 73 // framework. Each test case still needs to configure and start the |
| 74 // WebSocket server(s) it needs. |
| 75 virtual void SetUpOnMainThread() OVERRIDE { |
| 76 WebSocketBrowserTest::SetUpOnMainThread(); |
| 77 ASSERT_TRUE(http_server_.StartInBackground()); |
| 78 } |
| 79 |
| 80 // Supply a ws: or wss: URL to connect to. |
| 81 void ConnectTo(GURL url) { |
| 82 ASSERT_TRUE(http_server_.BlockUntilStarted()); |
| 83 std::string query("url=" + url.spec()); |
| 84 GURL::Replacements replacements; |
| 85 replacements.SetQueryStr(query); |
| 86 ui_test_utils::NavigateToURL(browser(), |
| 87 http_server_.GetURL("files/connect_to.html") |
| 88 .ReplaceComponents(replacements)); |
| 89 } |
| 90 |
| 91 private: |
| 92 net::SpawnedTestServer http_server_; |
| 93 }; |
| 94 |
| 95 // Automatically fill in any login prompts that appear with the supplied |
| 96 // credentials. |
| 97 class AutoLogin : public content::NotificationObserver { |
| 98 public: |
| 99 AutoLogin(const std::string& username, |
| 100 const std::string& password, |
| 101 content::NavigationController* navigation_controller) |
| 102 : username_(base::UTF8ToUTF16(username)), |
| 103 password_(base::UTF8ToUTF16(password)), |
| 104 logged_in_(false) { |
| 105 registrar_.Add( |
| 106 this, |
| 107 chrome::NOTIFICATION_AUTH_NEEDED, |
| 108 content::Source<content::NavigationController>(navigation_controller)); |
| 109 } |
| 110 |
| 111 // NotificationObserver implementation |
| 112 virtual void Observe(int type, |
| 113 const content::NotificationSource& source, |
| 114 const content::NotificationDetails& details) OVERRIDE { |
| 115 DCHECK_EQ(chrome::NOTIFICATION_AUTH_NEEDED, type); |
| 116 scoped_refptr<LoginHandler> login_handler = |
| 117 content::Details<LoginNotificationDetails>(details)->handler(); |
| 118 login_handler->SetAuth(username_, password_); |
| 119 logged_in_ = true; |
| 120 } |
| 121 |
| 122 bool logged_in() const { return logged_in_; } |
| 123 |
| 124 private: |
| 125 const base::string16 username_; |
| 126 const base::string16 password_; |
| 127 bool logged_in_; |
| 128 |
| 129 content::NotificationRegistrar registrar_; |
| 130 }; |
| 131 |
38 // Test that the browser can handle a WebSocket frame split into multiple TCP | 132 // Test that the browser can handle a WebSocket frame split into multiple TCP |
39 // segments. | 133 // segments. |
40 IN_PROC_BROWSER_TEST_F(WebSocketBrowserTest, WebSocketSplitSegments) { | 134 IN_PROC_BROWSER_TEST_F(WebSocketBrowserTest, WebSocketSplitSegments) { |
41 // Launch a WebSocket server. | 135 // Launch a WebSocket server. |
42 ASSERT_TRUE(ws_server_.Start()); | 136 ASSERT_TRUE(ws_server_.Start()); |
43 | 137 |
44 // Setup page title observer. | |
45 content::WebContents* tab = | |
46 browser()->tab_strip_model()->GetActiveWebContents(); | |
47 content::TitleWatcher watcher(tab, base::ASCIIToUTF16("PASS")); | |
48 watcher.AlsoWaitForTitle(base::ASCIIToUTF16("FAIL")); | |
49 | |
50 // Visit a HTTP page for testing. | 138 // Visit a HTTP page for testing. |
51 std::string scheme("http"); | 139 std::string scheme("http"); |
52 GURL::Replacements replacements; | 140 GURL::Replacements replacements; |
53 replacements.SetSchemeStr(scheme); | 141 replacements.SetSchemeStr(scheme); |
54 ui_test_utils::NavigateToURL( | 142 ui_test_utils::NavigateToURL( |
55 browser(), | 143 browser(), |
56 ws_server_.GetURL( | 144 ws_server_.GetURL( |
57 "split_packet_check.html").ReplaceComponents(replacements)); | 145 "split_packet_check.html").ReplaceComponents(replacements)); |
58 | 146 |
59 const base::string16 result = watcher.WaitAndGetTitle(); | 147 EXPECT_EQ("PASS", WaitAndGetTitle()); |
60 EXPECT_TRUE(EqualsASCII(result, "PASS")); | |
61 } | 148 } |
62 | 149 |
63 IN_PROC_BROWSER_TEST_F(WebSocketBrowserTest, SecureWebSocketSplitRecords) { | 150 IN_PROC_BROWSER_TEST_F(WebSocketBrowserTest, SecureWebSocketSplitRecords) { |
64 // Launch a secure WebSocket server. | 151 // Launch a secure WebSocket server. |
65 ASSERT_TRUE(wss_server_.Start()); | 152 ASSERT_TRUE(wss_server_.Start()); |
66 | 153 |
67 // Setup page title observer. | |
68 content::WebContents* tab = | |
69 browser()->tab_strip_model()->GetActiveWebContents(); | |
70 content::TitleWatcher watcher(tab, base::ASCIIToUTF16("PASS")); | |
71 watcher.AlsoWaitForTitle(base::ASCIIToUTF16("FAIL")); | |
72 | |
73 // Visit a HTTPS page for testing. | 154 // Visit a HTTPS page for testing. |
74 std::string scheme("https"); | 155 std::string scheme("https"); |
75 GURL::Replacements replacements; | 156 GURL::Replacements replacements; |
76 replacements.SetSchemeStr(scheme); | 157 replacements.SetSchemeStr(scheme); |
77 ui_test_utils::NavigateToURL( | 158 ui_test_utils::NavigateToURL( |
78 browser(), | 159 browser(), |
79 wss_server_.GetURL( | 160 wss_server_.GetURL( |
80 "split_packet_check.html").ReplaceComponents(replacements)); | 161 "split_packet_check.html").ReplaceComponents(replacements)); |
81 | 162 |
82 const base::string16 result = watcher.WaitAndGetTitle(); | 163 EXPECT_EQ("PASS", WaitAndGetTitle()); |
83 EXPECT_TRUE(EqualsASCII(result, "PASS")); | 164 } |
| 165 |
| 166 IN_PROC_BROWSER_TEST_F(WebSocketBrowserTest, WebSocketBasicAuthInHTTPURL) { |
| 167 // Launch a basic-auth-protected WebSocket server. |
| 168 ws_server_.set_websocket_basic_auth(true); |
| 169 ASSERT_TRUE(ws_server_.Start()); |
| 170 |
| 171 // Visit a HTTP page for testing. |
| 172 std::string scheme("http"); |
| 173 GURL::Replacements replacements; |
| 174 replacements.SetSchemeStr(scheme); |
| 175 ui_test_utils::NavigateToURL( |
| 176 browser(), |
| 177 ws_server_.GetURLWithUserAndPassword("connect_check.html", "test", "test") |
| 178 .ReplaceComponents(replacements)); |
| 179 |
| 180 EXPECT_EQ("PASS", WaitAndGetTitle()); |
| 181 } |
| 182 |
| 183 IN_PROC_BROWSER_TEST_F(WebSocketBrowserTest, WebSocketBasicAuthInHTTPSURL) { |
| 184 // Launch a basic-auth-protected secure WebSocket server. |
| 185 wss_server_.set_websocket_basic_auth(true); |
| 186 ASSERT_TRUE(wss_server_.Start()); |
| 187 |
| 188 // Visit a HTTPS page for testing. |
| 189 std::string scheme("https"); |
| 190 GURL::Replacements replacements; |
| 191 replacements.SetSchemeStr(scheme); |
| 192 ui_test_utils::NavigateToURL( |
| 193 browser(), |
| 194 wss_server_.GetURLWithUserAndPassword( |
| 195 "connect_check.html", "test", "test") |
| 196 .ReplaceComponents(replacements)); |
| 197 |
| 198 EXPECT_EQ("PASS", WaitAndGetTitle()); |
| 199 } |
| 200 |
| 201 // This test verifies that login details entered by the user into the login |
| 202 // prompt to authenticate the main page are re-used for WebSockets from the same |
| 203 // origin. |
| 204 IN_PROC_BROWSER_TEST_F(WebSocketBrowserTest, WebSocketBasicAuthPrompt) { |
| 205 // Launch a basic-auth-protected WebSocket server. |
| 206 ws_server_.set_websocket_basic_auth(true); |
| 207 ASSERT_TRUE(ws_server_.Start()); |
| 208 |
| 209 content::NavigationController* navigation_controller = |
| 210 &browser()->tab_strip_model()->GetActiveWebContents()->GetController(); |
| 211 AutoLogin auto_login("test", "test", navigation_controller); |
| 212 |
| 213 // Visit a HTTP page for testing. |
| 214 std::string scheme("http"); |
| 215 GURL::Replacements replacements; |
| 216 replacements.SetSchemeStr(scheme); |
| 217 ui_test_utils::NavigateToURL( |
| 218 browser(), |
| 219 ws_server_.GetURL("connect_check.html").ReplaceComponents(replacements)); |
| 220 |
| 221 EXPECT_TRUE(auto_login.logged_in()); |
| 222 EXPECT_EQ("PASS", WaitAndGetTitle()); |
| 223 } |
| 224 |
| 225 IN_PROC_BROWSER_TEST_F(WebSocketBrowserConnectToTest, |
| 226 WebSocketBasicAuthInWSURL) { |
| 227 // Launch a basic-auth-protected WebSocket server. |
| 228 ws_server_.set_websocket_basic_auth(true); |
| 229 ASSERT_TRUE(ws_server_.Start()); |
| 230 |
| 231 ConnectTo(ws_server_.GetURLWithUserAndPassword( |
| 232 "echo-with-no-extension", "test", "test")); |
| 233 |
| 234 EXPECT_EQ("PASS", WaitAndGetTitle()); |
| 235 } |
| 236 |
| 237 IN_PROC_BROWSER_TEST_F(WebSocketBrowserConnectToTest, |
| 238 WebSocketBasicAuthInWSURLBadCreds) { |
| 239 // Launch a basic-auth-protected WebSocket server. |
| 240 ws_server_.set_websocket_basic_auth(true); |
| 241 ASSERT_TRUE(ws_server_.Start()); |
| 242 |
| 243 ConnectTo(ws_server_.GetURLWithUserAndPassword( |
| 244 "echo-with-no-extension", "wrong-user", "wrong-password")); |
| 245 |
| 246 EXPECT_EQ("FAIL", WaitAndGetTitle()); |
| 247 } |
| 248 |
| 249 IN_PROC_BROWSER_TEST_F(WebSocketBrowserConnectToTest, |
| 250 WebSocketBasicAuthNoCreds) { |
| 251 // Launch a basic-auth-protected WebSocket server. |
| 252 ws_server_.set_websocket_basic_auth(true); |
| 253 ASSERT_TRUE(ws_server_.Start()); |
| 254 |
| 255 ConnectTo(ws_server_.GetURL("echo-with-no-extension")); |
| 256 |
| 257 EXPECT_EQ("FAIL", WaitAndGetTitle()); |
84 } | 258 } |
85 | 259 |
86 } // namespace | 260 } // namespace |
OLD | NEW |