Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(78)

Side by Side Diff: chrome/browser/extensions/isolated_app_browsertest.cc

Issue 10831116: Move SessionStorageNamespace entirely into NavigationController and support StoragePartitions. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: merged ToT Created 8 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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
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(
133 app1_rvh, L"", L"window.localStorage.getItem('testdata');"));
134
135 const std::wstring& kRetrieveLocalStorage = 126 const std::wstring& kRetrieveLocalStorage =
136 WrapForJavascriptAndExtract(L"window.localStorage.getItem('testdata')"); 127 WrapForJavascriptAndExtract(
128 L"window.localStorage.getItem('testdata') || 'badval'");
137 std::string result; 129 std::string result;
138 ASSERT_TRUE(ExecuteJavaScriptAndExtractString( 130 ASSERT_TRUE(ExecuteJavaScriptAndExtractString(
139 app1_rvh, L"", kRetrieveLocalStorage.c_str(), &result)); 131 app1_rvh, L"", kRetrieveLocalStorage.c_str(), &result));
140 EXPECT_EQ("ls_app1", result); 132 EXPECT_EQ("ls_app1", result);
141 ASSERT_TRUE(ExecuteJavaScriptAndExtractString( 133 ASSERT_TRUE(ExecuteJavaScriptAndExtractString(
142 app2_rvh, L"", kRetrieveLocalStorage.c_str(), &result)); 134 app2_rvh, L"", kRetrieveLocalStorage.c_str(), &result));
143 EXPECT_EQ("ls_app2", result); 135 EXPECT_EQ("ls_app2", result);
144 ASSERT_TRUE(ExecuteJavaScriptAndExtractString( 136 ASSERT_TRUE(ExecuteJavaScriptAndExtractString(
145 non_app_rvh, L"", kRetrieveLocalStorage.c_str(), &result)); 137 non_app_rvh, L"", kRetrieveLocalStorage.c_str(), &result));
146 EXPECT_EQ("ls_normal", result); 138 EXPECT_EQ("ls_normal", result);
147 139
148 // Check that each tab sees its own cookie. 140 // Check that each tab sees its own cookie.
149 EXPECT_TRUE(HasCookie(tab1, "app1=3")); 141 EXPECT_TRUE(HasCookie(tab0, "app1=3"));
150 EXPECT_TRUE(HasCookie(tab2, "app2=4")); 142 EXPECT_TRUE(HasCookie(tab1, "app2=4"));
151 EXPECT_TRUE(HasCookie(tab3, "normalPage=5")); 143 EXPECT_TRUE(HasCookie(tab2, "normalPage=5"));
152 144
153 // Check that app1 tab cannot see the other cookies. 145 // Check that app1 tab cannot see the other cookies.
154 EXPECT_FALSE(HasCookie(tab1, "app2")); 146 EXPECT_FALSE(HasCookie(tab0, "app2"));
147 EXPECT_FALSE(HasCookie(tab0, "normalPage"));
148
149 // Check that app2 tab cannot see the other cookies.
150 EXPECT_FALSE(HasCookie(tab1, "app1"));
155 EXPECT_FALSE(HasCookie(tab1, "normalPage")); 151 EXPECT_FALSE(HasCookie(tab1, "normalPage"));
156 152
157 // Check that app2 tab cannot see the other cookies. 153 // Check that normal tab cannot see the other cookies.
158 EXPECT_FALSE(HasCookie(tab2, "app1")); 154 EXPECT_FALSE(HasCookie(tab2, "app1"));
159 EXPECT_FALSE(HasCookie(tab2, "normalPage")); 155 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 156
165 // Check that the non_app iframe cookie is associated with app1 and not the 157 // 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 158 // normal tab. (For now, iframes are always rendered in their parent
167 // process, even if they aren't in the app manifest.) 159 // process, even if they aren't in the app manifest.)
168 EXPECT_TRUE(HasCookie(tab1, "nonAppFrame=6")); 160 EXPECT_TRUE(HasCookie(tab0, "nonAppFrame=6"));
169 EXPECT_FALSE(HasCookie(tab3, "nonAppFrame")); 161 EXPECT_FALSE(HasCookie(tab2, "nonAppFrame"));
170 162
171 // Check that isolation persists even if the tab crashes and is reloaded. 163 // Check that isolation persists even if the tab crashes and is reloaded.
172 chrome::SelectNumberedTab(browser(), 0); 164 chrome::SelectNumberedTab(browser(), 0);
173 content::CrashTab(tab1); 165 content::CrashTab(tab0);
174 content::WindowedNotificationObserver observer( 166 content::WindowedNotificationObserver observer(
175 content::NOTIFICATION_LOAD_STOP, 167 content::NOTIFICATION_LOAD_STOP,
176 content::Source<NavigationController>( 168 content::Source<NavigationController>(
177 &chrome::GetActiveWebContents(browser())->GetController())); 169 &chrome::GetActiveWebContents(browser())->GetController()));
178 chrome::Reload(browser(), CURRENT_TAB); 170 chrome::Reload(browser(), CURRENT_TAB);
179 observer.Wait(); 171 observer.Wait();
180 EXPECT_TRUE(HasCookie(tab1, "app1=3")); 172 EXPECT_TRUE(HasCookie(tab0, "app1=3"));
181 EXPECT_FALSE(HasCookie(tab1, "app2")); 173 EXPECT_FALSE(HasCookie(tab0, "app2"));
182 EXPECT_FALSE(HasCookie(tab1, "normalPage")); 174 EXPECT_FALSE(HasCookie(tab0, "normalPage"));
183 175
184 } 176 }
185 177
186 // Ensure that cookies are not isolated if the isolated apps are not installed. 178 // Ensure that cookies are not isolated if the isolated apps are not installed.
187 IN_PROC_BROWSER_TEST_F(IsolatedAppTest, NoCookieIsolationWithoutApp) { 179 IN_PROC_BROWSER_TEST_F(IsolatedAppTest, NoCookieIsolationWithoutApp) {
188 host_resolver()->AddRule("*", "127.0.0.1"); 180 host_resolver()->AddRule("*", "127.0.0.1");
189 ASSERT_TRUE(test_server()->Start()); 181 ASSERT_TRUE(test_server()->Start());
190 182
191 // The app under test acts on URLs whose host is "localhost", 183 // The app under test acts on URLs whose host is "localhost",
192 // so the URLs we navigate to must have host "localhost". 184 // so the URLs we navigate to must have host "localhost".
(...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after
297 chrome::GetWebContentsAt(browser(), 2)->GetRenderProcessHost()->GetI D()); 289 chrome::GetWebContentsAt(browser(), 2)->GetRenderProcessHost()->GetI D());
298 EXPECT_NE(process_id_0, 290 EXPECT_NE(process_id_0,
299 chrome::GetWebContentsAt(browser(), 3)->GetRenderProcessHost()->GetI D()); 291 chrome::GetWebContentsAt(browser(), 3)->GetRenderProcessHost()->GetI D());
300 292
301 // Navigating the second tab out of the app should cause a process swap. 293 // 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")); 294 const GURL& non_app_url(base_url.Resolve("non_app/main.html"));
303 NavigateInRenderer(chrome::GetWebContentsAt(browser(), 1), non_app_url); 295 NavigateInRenderer(chrome::GetWebContentsAt(browser(), 1), non_app_url);
304 EXPECT_NE(process_id_1, 296 EXPECT_NE(process_id_1,
305 chrome::GetWebContentsAt(browser(), 1)->GetRenderProcessHost()->GetI D()); 297 chrome::GetWebContentsAt(browser(), 1)->GetRenderProcessHost()->GetI D());
306 } 298 }
299
300 IN_PROC_BROWSER_TEST_F(IsolatedAppTest, SessionStorage) {
301 host_resolver()->AddRule("*", "127.0.0.1");
302 ASSERT_TRUE(test_server()->Start());
303
304 ASSERT_TRUE(LoadExtension(test_data_dir_.AppendASCII("isolated_apps/app1")));
305 ASSERT_TRUE(LoadExtension(test_data_dir_.AppendASCII("isolated_apps/app2")));
306
307 // The app under test acts on URLs whose host is "localhost",
308 // so the URLs we navigate to must have host "localhost".
309 GURL base_url = test_server()->GetURL(
310 "files/extensions/isolated_apps/");
311 GURL::Replacements replace_host;
312 std::string host_str("localhost"); // Must stay in scope with replace_host.
313 replace_host.SetHostStr(host_str);
314 base_url = base_url.ReplaceComponents(replace_host);
315
316 // Enter some state into sessionStorage three times on the same origin, but
317 // for three URLs that correspond to app1, app2, and a non-isolated site.
318 ui_test_utils::NavigateToURLWithDisposition(
319 browser(), base_url.Resolve("app1/main.html"),
320 CURRENT_TAB, ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION);
321 ASSERT_TRUE(ExecuteJavaScript(
322 chrome::GetWebContentsAt(browser(), 0)->GetRenderViewHost(),
323 L"",
324 L"window.sessionStorage.setItem('testdata', 'ss_app1');"));
325
326 ui_test_utils::NavigateToURLWithDisposition(
327 browser(), base_url.Resolve("app2/main.html"),
328 CURRENT_TAB, ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION);
329 ASSERT_TRUE(ExecuteJavaScript(
330 chrome::GetWebContentsAt(browser(), 0)->GetRenderViewHost(),
331 L"",
332 L"window.sessionStorage.setItem('testdata', 'ss_app2');"));
333
334 ui_test_utils::NavigateToURLWithDisposition(
335 browser(), base_url.Resolve("non_app/main.html"),
336 CURRENT_TAB, ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION);
337 ASSERT_TRUE(ExecuteJavaScript(
338 chrome::GetWebContentsAt(browser(), 0)->GetRenderViewHost(),
339 L"",
340 L"window.sessionStorage.setItem('testdata', 'ss_normal');"));
341
342 // Now, ensure that the sessionStorage is correctly partitioned, and persists
343 // when we navigate around all over the dang place.
344 const std::wstring& kRetrieveSessionStorage =
345 WrapForJavascriptAndExtract(
346 L"window.sessionStorage.getItem('testdata') || 'badval'");
347 std::string result;
348 ui_test_utils::NavigateToURLWithDisposition(
349 browser(), base_url.Resolve("app1/main.html"),
350 CURRENT_TAB, ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION);
351 ASSERT_TRUE(ExecuteJavaScriptAndExtractString(
352 chrome::GetWebContentsAt(browser(), 0)->GetRenderViewHost(),
353 L"", kRetrieveSessionStorage.c_str(), &result));
354 EXPECT_EQ("ss_app1", result);
355
356 ui_test_utils::NavigateToURLWithDisposition(
357 browser(), base_url.Resolve("app2/main.html"),
358 CURRENT_TAB, ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION);
359 ASSERT_TRUE(ExecuteJavaScriptAndExtractString(
360 chrome::GetWebContentsAt(browser(), 0)->GetRenderViewHost(),
361 L"", kRetrieveSessionStorage.c_str(), &result));
362 EXPECT_EQ("ss_app2", result);
363
364 ui_test_utils::NavigateToURLWithDisposition(
365 browser(), base_url.Resolve("non_app/main.html"),
366 CURRENT_TAB, ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION);
367 ASSERT_TRUE(ExecuteJavaScriptAndExtractString(
368 chrome::GetWebContentsAt(browser(), 0)->GetRenderViewHost(),
369 L"", kRetrieveSessionStorage.c_str(), &result));
370 EXPECT_EQ("ss_normal", result);
371 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698