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" | |
8 #include "base/strings/string_util.h" | 5 #include "base/strings/string_util.h" |
9 #include "base/strings/utf_string_conversions.h" | 6 #include "base/strings/utf_string_conversions.h" |
10 #include "chrome/browser/chrome_notification_types.h" | |
11 #include "chrome/browser/ui/browser.h" | 7 #include "chrome/browser/ui/browser.h" |
12 #include "chrome/browser/ui/login/login_prompt.h" | |
13 #include "chrome/browser/ui/tabs/tab_strip_model.h" | 8 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
14 #include "chrome/test/base/in_process_browser_test.h" | 9 #include "chrome/test/base/in_process_browser_test.h" |
15 #include "chrome/test/base/ui_test_utils.h" | 10 #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" | |
21 #include "content/public/test/browser_test_utils.h" | 11 #include "content/public/test/browser_test_utils.h" |
22 #include "net/base/test_data_directory.h" | 12 #include "net/base/test_data_directory.h" |
23 #include "net/test/spawned_test_server/spawned_test_server.h" | 13 #include "net/test/spawned_test_server/spawned_test_server.h" |
24 #include "url/gurl.h" | |
25 | 14 |
26 namespace { | 15 namespace { |
27 | 16 |
28 class WebSocketBrowserTest : public InProcessBrowserTest { | 17 class WebSocketBrowserTest : public InProcessBrowserTest { |
29 public: | 18 public: |
30 WebSocketBrowserTest() | 19 WebSocketBrowserTest() |
31 : ws_server_(net::SpawnedTestServer::TYPE_WS, | 20 : ws_server_(net::SpawnedTestServer::TYPE_WS, |
32 net::SpawnedTestServer::kLocalhost, | 21 net::SpawnedTestServer::kLocalhost, |
33 net::GetWebSocketTestDataDirectory()), | 22 net::GetWebSocketTestDataDirectory()), |
34 wss_server_(net::SpawnedTestServer::TYPE_WSS, | 23 wss_server_(net::SpawnedTestServer::TYPE_WSS, |
35 SSLOptions(SSLOptions::CERT_OK), | 24 SSLOptions(SSLOptions::CERT_OK), |
36 net::GetWebSocketTestDataDirectory()) {} | 25 net::GetWebSocketTestDataDirectory()) { |
| 26 } |
37 | 27 |
38 protected: | 28 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")); | |
45 } | |
46 | |
47 virtual void CleanUpOnMainThread() OVERRIDE { watcher_.reset(); } | |
48 | |
49 std::string WaitAndGetTitle() { | |
50 return base::UTF16ToUTF8(watcher_->WaitAndGetTitle()); | |
51 } | |
52 | |
53 net::SpawnedTestServer ws_server_; | 29 net::SpawnedTestServer ws_server_; |
54 net::SpawnedTestServer wss_server_; | 30 net::SpawnedTestServer wss_server_; |
55 | 31 |
56 private: | 32 private: |
57 typedef net::SpawnedTestServer::SSLOptions SSLOptions; | 33 typedef net::SpawnedTestServer::SSLOptions SSLOptions; |
58 scoped_ptr<content::TitleWatcher> watcher_; | |
59 | 34 |
60 DISALLOW_COPY_AND_ASSIGN(WebSocketBrowserTest); | 35 DISALLOW_COPY_AND_ASSIGN(WebSocketBrowserTest); |
61 }; | 36 }; |
62 | 37 |
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 DISALLOW_COPY_AND_ASSIGN(AutoLogin); | |
132 }; | |
133 | |
134 // Test that the browser can handle a WebSocket frame split into multiple TCP | 38 // Test that the browser can handle a WebSocket frame split into multiple TCP |
135 // segments. | 39 // segments. |
136 IN_PROC_BROWSER_TEST_F(WebSocketBrowserTest, WebSocketSplitSegments) { | 40 IN_PROC_BROWSER_TEST_F(WebSocketBrowserTest, WebSocketSplitSegments) { |
137 // Launch a WebSocket server. | 41 // Launch a WebSocket server. |
138 ASSERT_TRUE(ws_server_.Start()); | 42 ASSERT_TRUE(ws_server_.Start()); |
139 | 43 |
| 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 |
140 // Visit a HTTP page for testing. | 50 // Visit a HTTP page for testing. |
141 std::string scheme("http"); | 51 std::string scheme("http"); |
142 GURL::Replacements replacements; | 52 GURL::Replacements replacements; |
143 replacements.SetSchemeStr(scheme); | 53 replacements.SetSchemeStr(scheme); |
144 ui_test_utils::NavigateToURL( | 54 ui_test_utils::NavigateToURL( |
145 browser(), | 55 browser(), |
146 ws_server_.GetURL( | 56 ws_server_.GetURL( |
147 "split_packet_check.html").ReplaceComponents(replacements)); | 57 "split_packet_check.html").ReplaceComponents(replacements)); |
148 | 58 |
149 EXPECT_EQ("PASS", WaitAndGetTitle()); | 59 const base::string16 result = watcher.WaitAndGetTitle(); |
| 60 EXPECT_TRUE(EqualsASCII(result, "PASS")); |
150 } | 61 } |
151 | 62 |
152 IN_PROC_BROWSER_TEST_F(WebSocketBrowserTest, SecureWebSocketSplitRecords) { | 63 IN_PROC_BROWSER_TEST_F(WebSocketBrowserTest, SecureWebSocketSplitRecords) { |
153 // Launch a secure WebSocket server. | 64 // Launch a secure WebSocket server. |
154 ASSERT_TRUE(wss_server_.Start()); | 65 ASSERT_TRUE(wss_server_.Start()); |
155 | 66 |
| 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 |
156 // Visit a HTTPS page for testing. | 73 // Visit a HTTPS page for testing. |
157 std::string scheme("https"); | 74 std::string scheme("https"); |
158 GURL::Replacements replacements; | 75 GURL::Replacements replacements; |
159 replacements.SetSchemeStr(scheme); | 76 replacements.SetSchemeStr(scheme); |
160 ui_test_utils::NavigateToURL( | 77 ui_test_utils::NavigateToURL( |
161 browser(), | 78 browser(), |
162 wss_server_.GetURL( | 79 wss_server_.GetURL( |
163 "split_packet_check.html").ReplaceComponents(replacements)); | 80 "split_packet_check.html").ReplaceComponents(replacements)); |
164 | 81 |
165 EXPECT_EQ("PASS", WaitAndGetTitle()); | 82 const base::string16 result = watcher.WaitAndGetTitle(); |
166 } | 83 EXPECT_TRUE(EqualsASCII(result, "PASS")); |
167 | |
168 IN_PROC_BROWSER_TEST_F(WebSocketBrowserTest, WebSocketBasicAuthInHTTPURL) { | |
169 // Launch a basic-auth-protected WebSocket server. | |
170 ws_server_.set_websocket_basic_auth(true); | |
171 ASSERT_TRUE(ws_server_.Start()); | |
172 | |
173 // Visit a HTTP page for testing. | |
174 std::string scheme("http"); | |
175 GURL::Replacements replacements; | |
176 replacements.SetSchemeStr(scheme); | |
177 ui_test_utils::NavigateToURL( | |
178 browser(), | |
179 ws_server_.GetURLWithUserAndPassword("connect_check.html", "test", "test") | |
180 .ReplaceComponents(replacements)); | |
181 | |
182 EXPECT_EQ("PASS", WaitAndGetTitle()); | |
183 } | |
184 | |
185 IN_PROC_BROWSER_TEST_F(WebSocketBrowserTest, WebSocketBasicAuthInHTTPSURL) { | |
186 // Launch a basic-auth-protected secure WebSocket server. | |
187 wss_server_.set_websocket_basic_auth(true); | |
188 ASSERT_TRUE(wss_server_.Start()); | |
189 | |
190 // Visit a HTTPS page for testing. | |
191 std::string scheme("https"); | |
192 GURL::Replacements replacements; | |
193 replacements.SetSchemeStr(scheme); | |
194 ui_test_utils::NavigateToURL( | |
195 browser(), | |
196 wss_server_.GetURLWithUserAndPassword( | |
197 "connect_check.html", "test", "test") | |
198 .ReplaceComponents(replacements)); | |
199 | |
200 EXPECT_EQ("PASS", WaitAndGetTitle()); | |
201 } | |
202 | |
203 // This test verifies that login details entered by the user into the login | |
204 // prompt to authenticate the main page are re-used for WebSockets from the same | |
205 // origin. | |
206 IN_PROC_BROWSER_TEST_F(WebSocketBrowserTest, WebSocketBasicAuthPrompt) { | |
207 // Launch a basic-auth-protected WebSocket server. | |
208 ws_server_.set_websocket_basic_auth(true); | |
209 ASSERT_TRUE(ws_server_.Start()); | |
210 | |
211 content::NavigationController* navigation_controller = | |
212 &browser()->tab_strip_model()->GetActiveWebContents()->GetController(); | |
213 AutoLogin auto_login("test", "test", navigation_controller); | |
214 | |
215 // Visit a HTTP page for testing. | |
216 std::string scheme("http"); | |
217 GURL::Replacements replacements; | |
218 replacements.SetSchemeStr(scheme); | |
219 ui_test_utils::NavigateToURL( | |
220 browser(), | |
221 ws_server_.GetURL("connect_check.html").ReplaceComponents(replacements)); | |
222 | |
223 EXPECT_TRUE(auto_login.logged_in()); | |
224 EXPECT_EQ("PASS", WaitAndGetTitle()); | |
225 } | |
226 | |
227 IN_PROC_BROWSER_TEST_F(WebSocketBrowserConnectToTest, | |
228 WebSocketBasicAuthInWSURL) { | |
229 // Launch a basic-auth-protected WebSocket server. | |
230 ws_server_.set_websocket_basic_auth(true); | |
231 ASSERT_TRUE(ws_server_.Start()); | |
232 | |
233 ConnectTo(ws_server_.GetURLWithUserAndPassword( | |
234 "echo-with-no-extension", "test", "test")); | |
235 | |
236 EXPECT_EQ("PASS", WaitAndGetTitle()); | |
237 } | |
238 | |
239 IN_PROC_BROWSER_TEST_F(WebSocketBrowserConnectToTest, | |
240 WebSocketBasicAuthInWSURLBadCreds) { | |
241 // Launch a basic-auth-protected WebSocket server. | |
242 ws_server_.set_websocket_basic_auth(true); | |
243 ASSERT_TRUE(ws_server_.Start()); | |
244 | |
245 ConnectTo(ws_server_.GetURLWithUserAndPassword( | |
246 "echo-with-no-extension", "wrong-user", "wrong-password")); | |
247 | |
248 EXPECT_EQ("FAIL", WaitAndGetTitle()); | |
249 } | |
250 | |
251 IN_PROC_BROWSER_TEST_F(WebSocketBrowserConnectToTest, | |
252 WebSocketBasicAuthNoCreds) { | |
253 // Launch a basic-auth-protected WebSocket server. | |
254 ws_server_.set_websocket_basic_auth(true); | |
255 ASSERT_TRUE(ws_server_.Start()); | |
256 | |
257 ConnectTo(ws_server_.GetURL("echo-with-no-extension")); | |
258 | |
259 EXPECT_EQ("FAIL", WaitAndGetTitle()); | |
260 } | 84 } |
261 | 85 |
262 } // namespace | 86 } // namespace |
OLD | NEW |