OLD | NEW |
| (Empty) |
1 // Copyright (c) 2011 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 #include <sstream> | |
6 #include <string> | |
7 | |
8 #include "base/command_line.h" | |
9 #include "base/file_path.h" | |
10 #include "base/path_service.h" | |
11 #include "base/string_util.h" | |
12 #include "base/test/test_timeouts.h" | |
13 #include "chrome/browser/net/url_request_failed_dns_job.h" | |
14 #include "chrome/browser/net/url_request_mock_http_job.h" | |
15 #include "chrome/common/url_constants.h" | |
16 #include "chrome/test/automation/browser_proxy.h" | |
17 #include "chrome/test/automation/tab_proxy.h" | |
18 #include "chrome/test/ui/ui_test.h" | |
19 #include "net/base/net_util.h" | |
20 #include "net/test/test_server.h" | |
21 | |
22 namespace { | |
23 | |
24 class ResourceDispatcherTest : public UITest { | |
25 public: | |
26 void CheckTitleTest(const std::wstring& file, | |
27 const std::wstring& expected_title, | |
28 int expected_navigations) { | |
29 NavigateToURLBlockUntilNavigationsComplete( | |
30 URLRequestMockHTTPJob::GetMockUrl(FilePath::FromWStringHack(file)), | |
31 expected_navigations); | |
32 EXPECT_EQ(expected_title, GetActiveTabTitle()); | |
33 } | |
34 | |
35 protected: | |
36 ResourceDispatcherTest() : UITest() { | |
37 dom_automation_enabled_ = true; | |
38 } | |
39 }; | |
40 | |
41 TEST_F(ResourceDispatcherTest, SniffHTMLWithNoContentType) { | |
42 CheckTitleTest(L"content-sniffer-test0.html", | |
43 L"Content Sniffer Test 0", 1); | |
44 } | |
45 | |
46 TEST_F(ResourceDispatcherTest, RespectNoSniffDirective) { | |
47 CheckTitleTest(L"nosniff-test.html", L"", 1); | |
48 } | |
49 | |
50 TEST_F(ResourceDispatcherTest, DoNotSniffHTMLFromTextPlain) { | |
51 CheckTitleTest(L"content-sniffer-test1.html", L"", 1); | |
52 } | |
53 | |
54 TEST_F(ResourceDispatcherTest, DoNotSniffHTMLFromImageGIF) { | |
55 CheckTitleTest(L"content-sniffer-test2.html", L"", 1); | |
56 } | |
57 | |
58 TEST_F(ResourceDispatcherTest, SniffNoContentTypeNoData) { | |
59 CheckTitleTest(L"content-sniffer-test3.html", | |
60 L"Content Sniffer Test 3", 1); | |
61 EXPECT_EQ(1, GetTabCount()); | |
62 | |
63 // Make sure the download shelf is not showing. | |
64 scoped_refptr<BrowserProxy> browser(automation()->GetBrowserWindow(0)); | |
65 ASSERT_TRUE(browser.get()); | |
66 bool visible = false; | |
67 ASSERT_TRUE(browser->IsShelfVisible(&visible)); | |
68 EXPECT_FALSE(visible); | |
69 } | |
70 | |
71 TEST_F(ResourceDispatcherTest, ContentDispositionEmpty) { | |
72 CheckTitleTest(L"content-disposition-empty.html", L"success", 1); | |
73 } | |
74 | |
75 TEST_F(ResourceDispatcherTest, ContentDispositionInline) { | |
76 CheckTitleTest(L"content-disposition-inline.html", L"success", 1); | |
77 } | |
78 | |
79 // Test for bug #1091358. | |
80 // Flaky: http://crbug.com/62595 | |
81 TEST_F(ResourceDispatcherTest, FLAKY_SyncXMLHttpRequest) { | |
82 net::TestServer test_server(net::TestServer::TYPE_HTTP, | |
83 FilePath(FILE_PATH_LITERAL("chrome/test/data"))); | |
84 ASSERT_TRUE(test_server.Start()); | |
85 | |
86 scoped_refptr<BrowserProxy> browser_proxy(automation()->GetBrowserWindow(0)); | |
87 ASSERT_TRUE(browser_proxy.get()); | |
88 scoped_refptr<TabProxy> tab(browser_proxy->GetActiveTab()); | |
89 ASSERT_TRUE(tab.get()); | |
90 ASSERT_EQ(AUTOMATION_MSG_NAVIGATION_SUCCESS, | |
91 tab->NavigateToURL(test_server.GetURL( | |
92 "files/sync_xmlhttprequest.html"))); | |
93 | |
94 // Let's check the XMLHttpRequest ran successfully. | |
95 bool success = false; | |
96 EXPECT_TRUE(tab->ExecuteAndExtractBool(L"", | |
97 L"window.domAutomationController.send(DidSyncRequestSucceed());", | |
98 &success)); | |
99 EXPECT_TRUE(success); | |
100 } | |
101 | |
102 // http://code.google.com/p/chromium/issues/detail?id=62776 | |
103 TEST_F(ResourceDispatcherTest, FLAKY_SyncXMLHttpRequest_Disallowed) { | |
104 net::TestServer test_server(net::TestServer::TYPE_HTTP, | |
105 FilePath(FILE_PATH_LITERAL("chrome/test/data"))); | |
106 ASSERT_TRUE(test_server.Start()); | |
107 | |
108 scoped_refptr<BrowserProxy> browser_proxy(automation()->GetBrowserWindow(0)); | |
109 ASSERT_TRUE(browser_proxy.get()); | |
110 scoped_refptr<TabProxy> tab(browser_proxy->GetActiveTab()); | |
111 ASSERT_TRUE(tab.get()); | |
112 ASSERT_EQ(AUTOMATION_MSG_NAVIGATION_SUCCESS, | |
113 tab->NavigateToURL(test_server.GetURL( | |
114 "files/sync_xmlhttprequest_disallowed.html"))); | |
115 | |
116 // Let's check the XMLHttpRequest ran successfully. | |
117 bool success = false; | |
118 EXPECT_TRUE(tab->ExecuteAndExtractBool(L"", | |
119 L"window.domAutomationController.send(DidSucceed());", | |
120 &success)); | |
121 EXPECT_TRUE(success); | |
122 } | |
123 | |
124 // Test for bug #1159553 -- A synchronous xhr (whose content-type is | |
125 // downloadable) would trigger download and hang the renderer process, | |
126 // if executed while navigating to a new page. | |
127 // Disabled -- http://code.google.com/p/chromium/issues/detail?id=56264 | |
128 TEST_F(ResourceDispatcherTest, DISABLED_SyncXMLHttpRequest_DuringUnload) { | |
129 net::TestServer test_server(net::TestServer::TYPE_HTTP, | |
130 FilePath(FILE_PATH_LITERAL("chrome/test/data"))); | |
131 ASSERT_TRUE(test_server.Start()); | |
132 | |
133 scoped_refptr<BrowserProxy> browser_proxy(automation()->GetBrowserWindow(0)); | |
134 ASSERT_TRUE(browser_proxy.get()); | |
135 scoped_refptr<TabProxy> tab(browser_proxy->GetActiveTab()); | |
136 ASSERT_TRUE(tab.get()); | |
137 | |
138 ASSERT_EQ(AUTOMATION_MSG_NAVIGATION_SUCCESS, | |
139 tab->NavigateToURL(test_server.GetURL( | |
140 "files/sync_xmlhttprequest_during_unload.html"))); | |
141 | |
142 // Confirm that the page has loaded (since it changes its title during load). | |
143 std::wstring tab_title; | |
144 EXPECT_TRUE(tab->GetTabTitle(&tab_title)); | |
145 EXPECT_EQ(L"sync xhr on unload", tab_title); | |
146 | |
147 // Navigate to a new page, to dispatch unload event and trigger xhr. | |
148 // (the bug would make this step hang the renderer). | |
149 ASSERT_EQ(AUTOMATION_MSG_NAVIGATION_SUCCESS, | |
150 tab->NavigateToURL(test_server.GetURL("files/title2.html"))); | |
151 | |
152 // Check that the new page got loaded, and that no download was triggered. | |
153 EXPECT_TRUE(tab->GetTabTitle(&tab_title)); | |
154 EXPECT_EQ(L"Title Of Awesomeness", tab_title); | |
155 | |
156 bool shelf_is_visible = false; | |
157 scoped_refptr<BrowserProxy> browser(automation()->GetBrowserWindow(0)); | |
158 ASSERT_TRUE(browser.get()); | |
159 EXPECT_TRUE(browser->IsShelfVisible(&shelf_is_visible)); | |
160 EXPECT_FALSE(shelf_is_visible); | |
161 } | |
162 | |
163 // Tests that onunload is run for cross-site requests. (Bug 1114994) | |
164 TEST_F(ResourceDispatcherTest, CrossSiteOnunloadCookie) { | |
165 net::TestServer test_server(net::TestServer::TYPE_HTTP, | |
166 FilePath(FILE_PATH_LITERAL("chrome/test/data"))); | |
167 ASSERT_TRUE(test_server.Start()); | |
168 | |
169 scoped_refptr<BrowserProxy> browser_proxy(automation()->GetBrowserWindow(0)); | |
170 ASSERT_TRUE(browser_proxy.get()); | |
171 scoped_refptr<TabProxy> tab(browser_proxy->GetActiveTab()); | |
172 ASSERT_TRUE(tab.get()); | |
173 | |
174 GURL url(test_server.GetURL("files/onunload_cookie.html")); | |
175 ASSERT_EQ(AUTOMATION_MSG_NAVIGATION_SUCCESS, tab->NavigateToURL(url)); | |
176 | |
177 // Confirm that the page has loaded (since it changes its title during load). | |
178 std::wstring tab_title; | |
179 EXPECT_TRUE(tab->GetTabTitle(&tab_title)); | |
180 EXPECT_EQ(L"set cookie on unload", tab_title); | |
181 | |
182 // Navigate to a new cross-site page, to dispatch unload event and set the | |
183 // cookie. | |
184 CheckTitleTest(L"content-sniffer-test0.html", | |
185 L"Content Sniffer Test 0", 1); | |
186 | |
187 // Check that the cookie was set. | |
188 std::string value_result; | |
189 ASSERT_TRUE(tab->GetCookieByName(url, "onunloadCookie", &value_result)); | |
190 ASSERT_FALSE(value_result.empty()); | |
191 ASSERT_STREQ("foo", value_result.c_str()); | |
192 } | |
193 | |
194 #if !defined(OS_MACOSX) | |
195 // Tests that the onbeforeunload and onunload logic is shortcutted if the old | |
196 // renderer is gone. In that case, we don't want to wait for the old renderer | |
197 // to run the handlers. | |
198 // We need to disable this on Mac because the crash causes the OS CrashReporter | |
199 // process to kick in to analyze the poor dead renderer. Unfortunately, if the | |
200 // app isn't stripped of debug symbols, this takes about five minutes to | |
201 // complete and isn't conducive to quick turnarounds. As we don't currently | |
202 // strip the app on the build bots, this is bad times. | |
203 TEST_F(ResourceDispatcherTest, CrossSiteAfterCrash) { | |
204 // This test only works in multi-process mode | |
205 if (ProxyLauncher::in_process_renderer()) | |
206 return; | |
207 | |
208 scoped_refptr<BrowserProxy> browser_proxy(automation()->GetBrowserWindow(0)); | |
209 ASSERT_TRUE(browser_proxy.get()); | |
210 scoped_refptr<TabProxy> tab(browser_proxy->GetActiveTab()); | |
211 ASSERT_TRUE(tab.get()); | |
212 | |
213 // Cause the renderer to crash. | |
214 #if defined(OS_WIN) || defined(USE_LINUX_BREAKPAD) | |
215 expected_crashes_ = 1; | |
216 #endif | |
217 ASSERT_TRUE(tab->NavigateToURLAsync(GURL(chrome::kAboutCrashURL))); | |
218 // Wait for browser to notice the renderer crash. | |
219 base::PlatformThread::Sleep(TestTimeouts::action_timeout_ms()); | |
220 | |
221 // Navigate to a new cross-site page. The browser should not wait around for | |
222 // the old renderer's on{before}unload handlers to run. | |
223 CheckTitleTest(L"content-sniffer-test0.html", | |
224 L"Content Sniffer Test 0", 1); | |
225 } | |
226 #endif // !defined(OS_MACOSX) | |
227 | |
228 // Tests that cross-site navigations work when the new page does not go through | |
229 // the BufferedEventHandler (e.g., non-http{s} URLs). (Bug 1225872) | |
230 TEST_F(ResourceDispatcherTest, CrossSiteNavigationNonBuffered) { | |
231 scoped_refptr<BrowserProxy> browser_proxy(automation()->GetBrowserWindow(0)); | |
232 ASSERT_TRUE(browser_proxy.get()); | |
233 scoped_refptr<TabProxy> tab(browser_proxy->GetActiveTab()); | |
234 ASSERT_TRUE(tab.get()); | |
235 | |
236 // Start with an HTTP page. | |
237 CheckTitleTest(L"content-sniffer-test0.html", | |
238 L"Content Sniffer Test 0", 1); | |
239 | |
240 // Now load a file:// page, which does not use the BufferedEventHandler. | |
241 // Make sure that the page loads and displays a title, and doesn't get stuck. | |
242 FilePath test_file(test_data_directory_); | |
243 test_file = test_file.AppendASCII("title2.html"); | |
244 ASSERT_EQ(AUTOMATION_MSG_NAVIGATION_SUCCESS, | |
245 tab->NavigateToURL(net::FilePathToFileURL(test_file))); | |
246 EXPECT_EQ(L"Title Of Awesomeness", GetActiveTabTitle()); | |
247 } | |
248 | |
249 // Tests that a cross-site navigation to an error page (resulting in the link | |
250 // doctor page) still runs the onunload handler and can support navigations | |
251 // away from the link doctor page. (Bug 1235537) | |
252 TEST_F(ResourceDispatcherTest, CrossSiteNavigationErrorPage) { | |
253 net::TestServer test_server(net::TestServer::TYPE_HTTP, | |
254 FilePath(FILE_PATH_LITERAL("chrome/test/data"))); | |
255 ASSERT_TRUE(test_server.Start()); | |
256 | |
257 scoped_refptr<BrowserProxy> browser_proxy(automation()->GetBrowserWindow(0)); | |
258 ASSERT_TRUE(browser_proxy.get()); | |
259 scoped_refptr<TabProxy> tab(browser_proxy->GetActiveTab()); | |
260 ASSERT_TRUE(tab.get()); | |
261 | |
262 GURL url(test_server.GetURL("files/onunload_cookie.html")); | |
263 ASSERT_EQ(AUTOMATION_MSG_NAVIGATION_SUCCESS, tab->NavigateToURL(url)); | |
264 | |
265 // Confirm that the page has loaded (since it changes its title during load). | |
266 std::wstring tab_title; | |
267 EXPECT_TRUE(tab->GetTabTitle(&tab_title)); | |
268 EXPECT_EQ(L"set cookie on unload", tab_title); | |
269 | |
270 // Navigate to a new cross-site URL that results in an error page. | |
271 // TODO(creis): If this causes crashes or hangs, it might be for the same | |
272 // reason as ErrorPageTest::DNSError. See bug 1199491 and | |
273 // http://crbug.com/22877. | |
274 ASSERT_EQ(AUTOMATION_MSG_NAVIGATION_SUCCESS, | |
275 tab->NavigateToURLBlockUntilNavigationsComplete( | |
276 GURL(URLRequestFailedDnsJob::kTestUrl), 2)); | |
277 EXPECT_NE(L"set cookie on unload", GetActiveTabTitle()); | |
278 | |
279 // Check that the cookie was set, meaning that the onunload handler ran. | |
280 std::string value_result; | |
281 EXPECT_TRUE(tab->GetCookieByName(url, "onunloadCookie", &value_result)); | |
282 EXPECT_FALSE(value_result.empty()); | |
283 EXPECT_STREQ("foo", value_result.c_str()); | |
284 | |
285 // Check that renderer-initiated navigations still work. In a previous bug, | |
286 // the ResourceDispatcherHost would think that such navigations were | |
287 // cross-site, because we didn't clean up from the previous request. Since | |
288 // TabContents was in the NORMAL state, it would ignore the attempt to run | |
289 // the onunload handler, and the navigation would fail. | |
290 // (Test by redirecting to javascript:window.location='someURL'.) | |
291 GURL test_url(test_server.GetURL("files/title2.html")); | |
292 std::string redirect_url = "javascript:window.location='" + | |
293 test_url.possibly_invalid_spec() + "'"; | |
294 ASSERT_EQ(AUTOMATION_MSG_NAVIGATION_SUCCESS, | |
295 tab->NavigateToURL(GURL(redirect_url))); | |
296 EXPECT_TRUE(tab->GetTabTitle(&tab_title)); | |
297 EXPECT_EQ(L"Title Of Awesomeness", tab_title); | |
298 } | |
299 | |
300 TEST_F(ResourceDispatcherTest, CrossOriginRedirectBlocked) { | |
301 // We expect the following URL requests from this test: | |
302 // 1- http://mock.http/cross-origin-redirect-blocked.html | |
303 // 2- http://mock.http/redirect-to-title2.html | |
304 // 3- http://mock.http/title2.html | |
305 // | |
306 // If the redirect in #2 were not blocked, we'd also see a request | |
307 // for http://mock.http:4000/title2.html, and the title would be different. | |
308 CheckTitleTest(L"cross-origin-redirect-blocked.html", | |
309 L"Title Of More Awesomeness", 2); | |
310 } | |
311 | |
312 // Tests that ResourceDispatcherHostRequestInfo is updated correctly on failed | |
313 // requests, to prevent calling Read on a request that has already failed. | |
314 // See bug 40250. | |
315 TEST_F(ResourceDispatcherTest, CrossSiteFailedRequest) { | |
316 scoped_refptr<BrowserProxy> browser_proxy(automation()->GetBrowserWindow(0)); | |
317 ASSERT_TRUE(browser_proxy.get()); | |
318 scoped_refptr<TabProxy> tab(browser_proxy->GetActiveTab()); | |
319 ASSERT_TRUE(tab.get()); | |
320 | |
321 // Visit another URL first to trigger a cross-site navigation. | |
322 GURL url(chrome::kChromeUINewTabURL); | |
323 ASSERT_EQ(AUTOMATION_MSG_NAVIGATION_SUCCESS, tab->NavigateToURL(url)); | |
324 | |
325 // Visit a URL that fails without calling ResourceDispatcherHost::Read. | |
326 GURL broken_url("chrome://theme"); | |
327 ASSERT_EQ(AUTOMATION_MSG_NAVIGATION_SUCCESS, tab->NavigateToURL(broken_url)); | |
328 | |
329 // Make sure the navigation finishes. | |
330 std::wstring tab_title; | |
331 EXPECT_TRUE(tab->GetTabTitle(&tab_title)); | |
332 EXPECT_EQ(L"chrome://theme/ is not available", tab_title); | |
333 } | |
334 | |
335 } // namespace | |
OLD | NEW |