Index: chrome/browser/extensions/isolated_app_apitest.cc |
diff --git a/chrome/browser/extensions/isolated_app_apitest.cc b/chrome/browser/extensions/isolated_app_apitest.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..493f4431b33f24f977bc5a2265f03f656b6483c8 |
--- /dev/null |
+++ b/chrome/browser/extensions/isolated_app_apitest.cc |
@@ -0,0 +1,133 @@ |
+// Copyright (c) 2011 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "base/utf_string_conversions.h" |
+#include "chrome/browser/extensions/extension_apitest.h" |
+#include "chrome/browser/extensions/extension_host.h" |
+#include "chrome/browser/profiles/profile.h" |
+#include "chrome/browser/renderer_host/render_view_host.h" |
+#include "chrome/browser/tab_contents/tab_contents.h" |
+#include "chrome/browser/ui/browser.h" |
+#include "chrome/common/chrome_switches.h" |
+#include "chrome/test/ui_test_utils.h" |
+#include "net/base/mock_host_resolver.h" |
+ |
+class IsolatedAppApiTest : public ExtensionApiTest { |
+}; |
+ |
+// Returns whether the given tab has a cookie with the given name=value pair. |
+static void QueryCookie(TabContents* contents, std::string cookie, |
+ bool* result) { |
+ ASSERT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractBool( |
Paweł Hajdan Jr.
2011/01/12 19:24:12
Do we really need to execute JS to get cookie valu
Charlie Reis
2011/01/19 19:10:26
Thanks-- that's nicer. I had to adapt it a bit fo
|
+ contents->render_view_host(), L"", |
+ L"var index = document.cookie.indexOf('" + UTF8ToWide(cookie) + L"');" |
+ L"window.domAutomationController.send(index != -1);", |
+ result)); |
+} |
+ |
+// Tests that cookies set within an isolated app are not visible to normal |
+// pages or other apps. |
+IN_PROC_BROWSER_TEST_F(IsolatedAppApiTest, CookieIsolation) { |
+ CommandLine::ForCurrentProcess()->AppendSwitch( |
+ switches::kDisablePopupBlocking); |
+ CommandLine::ForCurrentProcess()->AppendSwitch( |
+ switches::kEnableExperimentalAppManifests); |
+ |
+ host_resolver()->AddRule("*", "127.0.0.1"); |
+ ASSERT_TRUE(test_server()->Start()); |
+ |
+ ASSERT_TRUE(LoadExtension(test_data_dir_.AppendASCII("isolated_apps/app1"))); |
+ ASSERT_TRUE(LoadExtension(test_data_dir_.AppendASCII("isolated_apps/app2"))); |
+ |
+ // The app under test acts on URLs whose host is "localhost", |
+ // so the URLs we navigate to must have host "localhost". |
+ GURL base_url = test_server()->GetURL( |
+ "files/extensions/api_test/isolated_apps/"); |
+ GURL::Replacements replace_host; |
+ std::string host_str("localhost"); // must stay in scope with replace_host |
+ replace_host.SetHostStr(host_str); |
+ base_url = base_url.ReplaceComponents(replace_host); |
+ |
+ browser()->NewTab(); |
+ ui_test_utils::NavigateToURL(browser(), base_url.Resolve("app1/main.html")); |
+ browser()->NewTab(); |
+ ui_test_utils::NavigateToURL(browser(), base_url.Resolve("app2/main.html")); |
+ browser()->NewTab(); |
+ ui_test_utils::NavigateToURL(browser(), |
+ base_url.Resolve("non_app/main.html")); |
+ |
+ // Check that each tab sees its own cookie. |
+ bool result = false; |
+ QueryCookie(browser()->GetTabContentsAt(1), "app1=3", &result); |
+ ASSERT_TRUE(result); |
+ QueryCookie(browser()->GetTabContentsAt(2), "app2=4", &result); |
+ ASSERT_TRUE(result); |
+ QueryCookie(browser()->GetTabContentsAt(3), "normalPage=5", &result); |
+ ASSERT_TRUE(result); |
+ |
+ // Check that app1 tab cannot see the other cookies. |
+ // TODO(creis): Isolate apps from each other. |
+ QueryCookie(browser()->GetTabContentsAt(1), "app2", &result); |
+ ASSERT_FALSE(result); |
+ QueryCookie(browser()->GetTabContentsAt(1), "normalPage", &result); |
+ ASSERT_FALSE(result); |
+ |
+ // Check that app2 tab cannot see the other cookies. |
+ // TODO(creis): Isolate apps from each other. |
+ QueryCookie(browser()->GetTabContentsAt(2), "app1", &result); |
+ ASSERT_FALSE(result); |
+ QueryCookie(browser()->GetTabContentsAt(2), "normalPage", &result); |
+ ASSERT_FALSE(result); |
+ |
+ // Check that normal tab cannot see the other cookies. |
+ QueryCookie(browser()->GetTabContentsAt(3), "app1", &result); |
+ ASSERT_FALSE(result); |
+ QueryCookie(browser()->GetTabContentsAt(3), "app2", &result); |
+ ASSERT_FALSE(result); |
+} |
+ |
+// Without the --enable-experimental-app-manifests flag, all the tabs |
+// should see each others' cookies. |
+IN_PROC_BROWSER_TEST_F(IsolatedAppApiTest, CookieIsolationRequiresFlag) { |
+ CommandLine::ForCurrentProcess()->AppendSwitch( |
+ switches::kDisablePopupBlocking); |
+ |
+ host_resolver()->AddRule("*", "127.0.0.1"); |
+ ASSERT_TRUE(test_server()->Start()); |
+ |
+ ASSERT_TRUE(LoadExtension(test_data_dir_.AppendASCII("isolated_apps/app1"))); |
+ ASSERT_TRUE(LoadExtension(test_data_dir_.AppendASCII("isolated_apps/app2"))); |
+ |
+ // The app under test acts on URLs whose host is "localhost", |
+ // so the URLs we navigate to must have host "localhost". |
+ GURL base_url = test_server()->GetURL( |
+ "files/extensions/api_test/isolated_apps/"); |
+ GURL::Replacements replace_host; |
+ std::string host_str("localhost"); // must stay in scope with replace_host |
+ replace_host.SetHostStr(host_str); |
+ base_url = base_url.ReplaceComponents(replace_host); |
+ |
+ browser()->NewTab(); |
+ ui_test_utils::NavigateToURL(browser(), base_url.Resolve("app1/main.html")); |
+ browser()->NewTab(); |
+ ui_test_utils::NavigateToURL(browser(), base_url.Resolve("app2/main.html")); |
+ browser()->NewTab(); |
+ ui_test_utils::NavigateToURL(browser(), |
+ base_url.Resolve("non_app/main.html")); |
+ |
+ // Check that tabs see each others' cookies. |
+ bool result = false; |
+ QueryCookie(browser()->GetTabContentsAt(1), "app2=4", &result); |
+ ASSERT_TRUE(result); |
+ QueryCookie(browser()->GetTabContentsAt(1), "normalPage=5", &result); |
+ ASSERT_TRUE(result); |
+ QueryCookie(browser()->GetTabContentsAt(2), "app1=3", &result); |
+ ASSERT_TRUE(result); |
+ QueryCookie(browser()->GetTabContentsAt(2), "normalPage=5", &result); |
+ ASSERT_TRUE(result); |
+ QueryCookie(browser()->GetTabContentsAt(3), "app1=3", &result); |
+ ASSERT_TRUE(result); |
+ QueryCookie(browser()->GetTabContentsAt(3), "app2=4", &result); |
+ ASSERT_TRUE(result); |
+} |