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/utf_string_conversions.h" | 5 #include "base/utf_string_conversions.h" |
6 #include "chrome/browser/automation/automation_util.h" | 6 #include "chrome/browser/automation/automation_util.h" |
7 #include "chrome/browser/extensions/extension_apitest.h" | 7 #include "chrome/browser/extensions/extension_apitest.h" |
8 #include "chrome/browser/extensions/extension_host.h" | 8 #include "chrome/browser/extensions/extension_host.h" |
9 #include "chrome/browser/extensions/extension_service.h" | 9 #include "chrome/browser/extensions/extension_service.h" |
10 #include "chrome/browser/profiles/profile.h" | 10 #include "chrome/browser/profiles/profile.h" |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
67 }; | 67 }; |
68 | 68 |
69 } // namespace | 69 } // namespace |
70 | 70 |
71 // Tests that cookies set within an isolated app are not visible to normal | 71 // Tests that cookies set within an isolated app are not visible to normal |
72 // pages or other apps. | 72 // pages or other apps. |
73 // | 73 // |
74 // TODO(ajwong): Also test what happens if an app spans multiple sites in its | 74 // TODO(ajwong): Also test what happens if an app spans multiple sites in its |
75 // extent. These origins should also be isolated, but still have origin-based | 75 // extent. These origins should also be isolated, but still have origin-based |
76 // separation as you would expect. | 76 // separation as you would expect. |
77 // | |
78 // TODO(ajwong): Add test for session storage. In one tab, navigate to a | |
79 // normal page and set X=ss_normal. Then navigate to an isolated URL in the | |
80 // same origin and verify X does not exist. Set X=ss_isolated. Navigate back to | |
81 // a normal webpage, and verify X is still ss_normal. Navigate to the isolate | |
82 // URL and verify that X is ss_isolated. | |
83 IN_PROC_BROWSER_TEST_F(IsolatedAppTest, CookieIsolation) { | 77 IN_PROC_BROWSER_TEST_F(IsolatedAppTest, CookieIsolation) { |
84 host_resolver()->AddRule("*", "127.0.0.1"); | 78 host_resolver()->AddRule("*", "127.0.0.1"); |
85 ASSERT_TRUE(test_server()->Start()); | 79 ASSERT_TRUE(test_server()->Start()); |
86 | 80 |
87 ASSERT_TRUE(LoadExtension(test_data_dir_.AppendASCII("isolated_apps/app1"))); | 81 ASSERT_TRUE(LoadExtension(test_data_dir_.AppendASCII("isolated_apps/app1"))); |
88 ASSERT_TRUE(LoadExtension(test_data_dir_.AppendASCII("isolated_apps/app2"))); | 82 ASSERT_TRUE(LoadExtension(test_data_dir_.AppendASCII("isolated_apps/app2"))); |
89 | 83 |
90 // The app under test acts on URLs whose host is "localhost", | 84 // The app under test acts on URLs whose host is "localhost", |
91 // so the URLs we navigate to must have host "localhost". | 85 // so the URLs we navigate to must have host "localhost". |
92 GURL base_url = test_server()->GetURL( | 86 GURL base_url = test_server()->GetURL( |
93 "files/extensions/isolated_apps/"); | 87 "files/extensions/isolated_apps/"); |
94 GURL::Replacements replace_host; | 88 GURL::Replacements replace_host; |
95 std::string host_str("localhost"); // Must stay in scope with replace_host. | 89 std::string host_str("localhost"); // Must stay in scope with replace_host. |
96 replace_host.SetHostStr(host_str); | 90 replace_host.SetHostStr(host_str); |
97 base_url = base_url.ReplaceComponents(replace_host); | 91 base_url = base_url.ReplaceComponents(replace_host); |
98 | 92 |
99 ui_test_utils::NavigateToURLWithDisposition( | 93 ui_test_utils::NavigateToURLWithDisposition( |
100 browser(), base_url.Resolve("app1/main.html"), | 94 browser(), base_url.Resolve("app1/main.html"), |
101 CURRENT_TAB, ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION); | 95 CURRENT_TAB, ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION); |
102 ui_test_utils::NavigateToURLWithDisposition( | 96 ui_test_utils::NavigateToURLWithDisposition( |
103 browser(), base_url.Resolve("app2/main.html"), | 97 browser(), base_url.Resolve("app2/main.html"), |
104 NEW_FOREGROUND_TAB, ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION); | 98 NEW_FOREGROUND_TAB, ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION); |
105 ui_test_utils::NavigateToURLWithDisposition( | 99 ui_test_utils::NavigateToURLWithDisposition( |
106 browser(), base_url.Resolve("non_app/main.html"), | 100 browser(), base_url.Resolve("non_app/main.html"), |
107 NEW_FOREGROUND_TAB, ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION); | 101 NEW_FOREGROUND_TAB, ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION); |
108 | 102 |
109 ASSERT_EQ(3, browser()->tab_count()); | 103 ASSERT_EQ(3, browser()->tab_count()); |
110 | 104 |
111 // Ensure first two tabs have installed apps. | 105 // Ensure first two tabs have installed apps. |
112 WebContents* tab1 = chrome::GetWebContentsAt(browser(), 0); | 106 WebContents* tab0 = chrome::GetWebContentsAt(browser(), 0); |
113 WebContents* tab2 = chrome::GetWebContentsAt(browser(), 1); | 107 WebContents* tab1 = chrome::GetWebContentsAt(browser(), 1); |
114 WebContents* tab3 = chrome::GetWebContentsAt(browser(), 2); | 108 WebContents* tab2 = chrome::GetWebContentsAt(browser(), 2); |
| 109 ASSERT_TRUE(GetInstalledApp(tab0)); |
115 ASSERT_TRUE(GetInstalledApp(tab1)); | 110 ASSERT_TRUE(GetInstalledApp(tab1)); |
116 ASSERT_TRUE(GetInstalledApp(tab2)); | 111 ASSERT_TRUE(!GetInstalledApp(tab2)); |
117 ASSERT_TRUE(!GetInstalledApp(tab3)); | |
118 | 112 |
119 // Check that tabs see cannot each other's localStorage even though they are | 113 // Check that tabs see cannot each other's localStorage even though they are |
120 // in the same origin. | 114 // in the same origin. |
121 RenderViewHost* app1_rvh = tab1->GetRenderViewHost(); | 115 RenderViewHost* app1_rvh = tab0->GetRenderViewHost(); |
122 RenderViewHost* app2_rvh = tab2->GetRenderViewHost(); | 116 RenderViewHost* app2_rvh = tab1->GetRenderViewHost(); |
123 RenderViewHost* non_app_rvh = tab3->GetRenderViewHost(); | 117 RenderViewHost* non_app_rvh = tab2->GetRenderViewHost(); |
124 ASSERT_TRUE(ExecuteJavaScript( | 118 ASSERT_TRUE(ExecuteJavaScript( |
125 app1_rvh, L"", L"window.localStorage.setItem('testdata', 'ls_app1');")); | 119 app1_rvh, L"", L"window.localStorage.setItem('testdata', 'ls_app1');")); |
126 ASSERT_TRUE(ExecuteJavaScript( | 120 ASSERT_TRUE(ExecuteJavaScript( |
127 app2_rvh, L"", L"window.localStorage.setItem('testdata', 'ls_app2');")); | 121 app2_rvh, L"", L"window.localStorage.setItem('testdata', 'ls_app2');")); |
128 ASSERT_TRUE(ExecuteJavaScript( | 122 ASSERT_TRUE(ExecuteJavaScript( |
129 non_app_rvh, L"", | 123 non_app_rvh, L"", |
130 L"window.localStorage.setItem('testdata', 'ls_normal');")); | 124 L"window.localStorage.setItem('testdata', 'ls_normal');")); |
131 | 125 |
132 ASSERT_TRUE(ExecuteJavaScript( | 126 ASSERT_TRUE(ExecuteJavaScript( |
133 app1_rvh, L"", L"window.localStorage.getItem('testdata');")); | 127 app1_rvh, L"", L"window.localStorage.getItem('testdata');")); |
134 | 128 |
135 const std::wstring& kRetrieveLocalStorage = | 129 const std::wstring& kRetrieveLocalStorage = |
136 WrapForJavascriptAndExtract(L"window.localStorage.getItem('testdata')"); | 130 WrapForJavascriptAndExtract(L"window.localStorage.getItem('testdata')"); |
137 std::string result; | 131 std::string result; |
138 ASSERT_TRUE(ExecuteJavaScriptAndExtractString( | 132 ASSERT_TRUE(ExecuteJavaScriptAndExtractString( |
139 app1_rvh, L"", kRetrieveLocalStorage.c_str(), &result)); | 133 app1_rvh, L"", kRetrieveLocalStorage.c_str(), &result)); |
140 EXPECT_EQ("ls_app1", result); | 134 EXPECT_EQ("ls_app1", result); |
141 ASSERT_TRUE(ExecuteJavaScriptAndExtractString( | 135 ASSERT_TRUE(ExecuteJavaScriptAndExtractString( |
142 app2_rvh, L"", kRetrieveLocalStorage.c_str(), &result)); | 136 app2_rvh, L"", kRetrieveLocalStorage.c_str(), &result)); |
143 EXPECT_EQ("ls_app2", result); | 137 EXPECT_EQ("ls_app2", result); |
144 ASSERT_TRUE(ExecuteJavaScriptAndExtractString( | 138 ASSERT_TRUE(ExecuteJavaScriptAndExtractString( |
145 non_app_rvh, L"", kRetrieveLocalStorage.c_str(), &result)); | 139 non_app_rvh, L"", kRetrieveLocalStorage.c_str(), &result)); |
146 EXPECT_EQ("ls_normal", result); | 140 EXPECT_EQ("ls_normal", result); |
147 | 141 |
148 // Check that each tab sees its own cookie. | 142 // Check that each tab sees its own cookie. |
149 EXPECT_TRUE(HasCookie(tab1, "app1=3")); | 143 EXPECT_TRUE(HasCookie(tab0, "app1=3")); |
150 EXPECT_TRUE(HasCookie(tab2, "app2=4")); | 144 EXPECT_TRUE(HasCookie(tab1, "app2=4")); |
151 EXPECT_TRUE(HasCookie(tab3, "normalPage=5")); | 145 EXPECT_TRUE(HasCookie(tab2, "normalPage=5")); |
152 | 146 |
153 // Check that app1 tab cannot see the other cookies. | 147 // Check that app1 tab cannot see the other cookies. |
154 EXPECT_FALSE(HasCookie(tab1, "app2")); | 148 EXPECT_FALSE(HasCookie(tab0, "app2")); |
| 149 EXPECT_FALSE(HasCookie(tab0, "normalPage")); |
| 150 |
| 151 // Check that app2 tab cannot see the other cookies. |
| 152 EXPECT_FALSE(HasCookie(tab1, "app1")); |
155 EXPECT_FALSE(HasCookie(tab1, "normalPage")); | 153 EXPECT_FALSE(HasCookie(tab1, "normalPage")); |
156 | 154 |
157 // Check that app2 tab cannot see the other cookies. | 155 // Check that normal tab cannot see the other cookies. |
158 EXPECT_FALSE(HasCookie(tab2, "app1")); | 156 EXPECT_FALSE(HasCookie(tab2, "app1")); |
159 EXPECT_FALSE(HasCookie(tab2, "normalPage")); | 157 EXPECT_FALSE(HasCookie(tab2, "app2")); |
160 | |
161 // Check that normal tab cannot see the other cookies. | |
162 EXPECT_FALSE(HasCookie(tab3, "app1")); | |
163 EXPECT_FALSE(HasCookie(tab3, "app2")); | |
164 | 158 |
165 // Check that the non_app iframe cookie is associated with app1 and not the | 159 // Check that the non_app iframe cookie is associated with app1 and not the |
166 // normal tab. (For now, iframes are always rendered in their parent | 160 // normal tab. (For now, iframes are always rendered in their parent |
167 // process, even if they aren't in the app manifest.) | 161 // process, even if they aren't in the app manifest.) |
168 EXPECT_TRUE(HasCookie(tab1, "nonAppFrame=6")); | 162 EXPECT_TRUE(HasCookie(tab0, "nonAppFrame=6")); |
169 EXPECT_FALSE(HasCookie(tab3, "nonAppFrame")); | 163 EXPECT_FALSE(HasCookie(tab2, "nonAppFrame")); |
170 | 164 |
171 // Check that isolation persists even if the tab crashes and is reloaded. | 165 // Check that isolation persists even if the tab crashes and is reloaded. |
172 chrome::SelectNumberedTab(browser(), 0); | 166 chrome::SelectNumberedTab(browser(), 0); |
173 content::CrashTab(tab1); | 167 content::CrashTab(tab0); |
174 content::WindowedNotificationObserver observer( | 168 content::WindowedNotificationObserver observer( |
175 content::NOTIFICATION_LOAD_STOP, | 169 content::NOTIFICATION_LOAD_STOP, |
176 content::Source<NavigationController>( | 170 content::Source<NavigationController>( |
177 &chrome::GetActiveWebContents(browser())->GetController())); | 171 &chrome::GetActiveWebContents(browser())->GetController())); |
178 chrome::Reload(browser(), CURRENT_TAB); | 172 chrome::Reload(browser(), CURRENT_TAB); |
179 observer.Wait(); | 173 observer.Wait(); |
180 EXPECT_TRUE(HasCookie(tab1, "app1=3")); | 174 EXPECT_TRUE(HasCookie(tab0, "app1=3")); |
181 EXPECT_FALSE(HasCookie(tab1, "app2")); | 175 EXPECT_FALSE(HasCookie(tab0, "app2")); |
182 EXPECT_FALSE(HasCookie(tab1, "normalPage")); | 176 EXPECT_FALSE(HasCookie(tab0, "normalPage")); |
183 | 177 |
184 } | 178 } |
185 | 179 |
186 // Ensure that cookies are not isolated if the isolated apps are not installed. | 180 // Ensure that cookies are not isolated if the isolated apps are not installed. |
187 IN_PROC_BROWSER_TEST_F(IsolatedAppTest, NoCookieIsolationWithoutApp) { | 181 IN_PROC_BROWSER_TEST_F(IsolatedAppTest, NoCookieIsolationWithoutApp) { |
188 host_resolver()->AddRule("*", "127.0.0.1"); | 182 host_resolver()->AddRule("*", "127.0.0.1"); |
189 ASSERT_TRUE(test_server()->Start()); | 183 ASSERT_TRUE(test_server()->Start()); |
190 | 184 |
191 // The app under test acts on URLs whose host is "localhost", | 185 // The app under test acts on URLs whose host is "localhost", |
192 // so the URLs we navigate to must have host "localhost". | 186 // so the URLs we navigate to must have host "localhost". |
(...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
297 chrome::GetWebContentsAt(browser(), 2)->GetRenderProcessHost()->GetI
D()); | 291 chrome::GetWebContentsAt(browser(), 2)->GetRenderProcessHost()->GetI
D()); |
298 EXPECT_NE(process_id_0, | 292 EXPECT_NE(process_id_0, |
299 chrome::GetWebContentsAt(browser(), 3)->GetRenderProcessHost()->GetI
D()); | 293 chrome::GetWebContentsAt(browser(), 3)->GetRenderProcessHost()->GetI
D()); |
300 | 294 |
301 // Navigating the second tab out of the app should cause a process swap. | 295 // Navigating the second tab out of the app should cause a process swap. |
302 const GURL& non_app_url(base_url.Resolve("non_app/main.html")); | 296 const GURL& non_app_url(base_url.Resolve("non_app/main.html")); |
303 NavigateInRenderer(chrome::GetWebContentsAt(browser(), 1), non_app_url); | 297 NavigateInRenderer(chrome::GetWebContentsAt(browser(), 1), non_app_url); |
304 EXPECT_NE(process_id_1, | 298 EXPECT_NE(process_id_1, |
305 chrome::GetWebContentsAt(browser(), 1)->GetRenderProcessHost()->GetI
D()); | 299 chrome::GetWebContentsAt(browser(), 1)->GetRenderProcessHost()->GetI
D()); |
306 } | 300 } |
| 301 |
| 302 IN_PROC_BROWSER_TEST_F(IsolatedAppTest, SessionStorage) { |
| 303 host_resolver()->AddRule("*", "127.0.0.1"); |
| 304 ASSERT_TRUE(test_server()->Start()); |
| 305 |
| 306 ASSERT_TRUE(LoadExtension(test_data_dir_.AppendASCII("isolated_apps/app1"))); |
| 307 ASSERT_TRUE(LoadExtension(test_data_dir_.AppendASCII("isolated_apps/app2"))); |
| 308 |
| 309 // The app under test acts on URLs whose host is "localhost", |
| 310 // so the URLs we navigate to must have host "localhost". |
| 311 GURL base_url = test_server()->GetURL( |
| 312 "files/extensions/isolated_apps/"); |
| 313 GURL::Replacements replace_host; |
| 314 std::string host_str("localhost"); // Must stay in scope with replace_host. |
| 315 replace_host.SetHostStr(host_str); |
| 316 base_url = base_url.ReplaceComponents(replace_host); |
| 317 |
| 318 // Enter some state into sessionStorage three times on the same origin, but |
| 319 // for three URLs that correspond to app1, app2, and a non-isolated site. |
| 320 ui_test_utils::NavigateToURLWithDisposition( |
| 321 browser(), base_url.Resolve("app1/main.html"), |
| 322 CURRENT_TAB, ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION); |
| 323 ASSERT_TRUE(ExecuteJavaScript( |
| 324 chrome::GetWebContentsAt(browser(), 0)->GetRenderViewHost(), |
| 325 L"", |
| 326 L"window.sessionStorage.setItem('testdata', 'ss_app1');")); |
| 327 |
| 328 ui_test_utils::NavigateToURLWithDisposition( |
| 329 browser(), base_url.Resolve("app2/main.html"), |
| 330 CURRENT_TAB, ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION); |
| 331 ASSERT_TRUE(ExecuteJavaScript( |
| 332 chrome::GetWebContentsAt(browser(), 0)->GetRenderViewHost(), |
| 333 L"", |
| 334 L"window.sessionStorage.setItem('testdata', 'ss_app2');")); |
| 335 |
| 336 ui_test_utils::NavigateToURLWithDisposition( |
| 337 browser(), base_url.Resolve("non_app/main.html"), |
| 338 CURRENT_TAB, ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION); |
| 339 ASSERT_TRUE(ExecuteJavaScript( |
| 340 chrome::GetWebContentsAt(browser(), 0)->GetRenderViewHost(), |
| 341 L"", |
| 342 L"window.sessionStorage.setItem('testdata', 'ss_normal');")); |
| 343 |
| 344 // Now, ensure that the sessionStorage is correctly partitioned, and persists |
| 345 // when we navigate around all over the dang place. |
| 346 const std::wstring& kRetrieveSessionStorage = |
| 347 WrapForJavascriptAndExtract( |
| 348 L"window.sessionStorage.getItem('testdata')"); |
| 349 std::string result; |
| 350 ui_test_utils::NavigateToURLWithDisposition( |
| 351 browser(), base_url.Resolve("app1/main.html"), |
| 352 CURRENT_TAB, ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION); |
| 353 ASSERT_TRUE(ExecuteJavaScriptAndExtractString( |
| 354 chrome::GetWebContentsAt(browser(), 0)->GetRenderViewHost(), |
| 355 L"", kRetrieveSessionStorage.c_str(), &result)); |
| 356 EXPECT_EQ("ss_app1", result); |
| 357 |
| 358 ui_test_utils::NavigateToURLWithDisposition( |
| 359 browser(), base_url.Resolve("app2/main.html"), |
| 360 CURRENT_TAB, ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION); |
| 361 ASSERT_TRUE(ExecuteJavaScriptAndExtractString( |
| 362 chrome::GetWebContentsAt(browser(), 0)->GetRenderViewHost(), |
| 363 L"", kRetrieveSessionStorage.c_str(), &result)); |
| 364 EXPECT_EQ("ss_app2", result); |
| 365 |
| 366 ui_test_utils::NavigateToURLWithDisposition( |
| 367 browser(), base_url.Resolve("non_app/main.html"), |
| 368 CURRENT_TAB, ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION); |
| 369 ASSERT_TRUE(ExecuteJavaScriptAndExtractString( |
| 370 chrome::GetWebContentsAt(browser(), 0)->GetRenderViewHost(), |
| 371 L"", kRetrieveSessionStorage.c_str(), &result)); |
| 372 EXPECT_EQ("ss_normal", result); |
| 373 } |
OLD | NEW |