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 "base/utf_string_conversions.h" |
| 6 #include "chrome/browser/automation/automation_cookie_util.h" |
| 7 #include "chrome/browser/extensions/extension_apitest.h" |
| 8 #include "chrome/browser/extensions/extension_host.h" |
| 9 #include "chrome/browser/profiles/profile.h" |
| 10 #include "chrome/browser/ui/browser.h" |
| 11 #include "chrome/common/chrome_switches.h" |
| 12 #include "chrome/test/ui_test_utils.h" |
| 13 #include "content/browser/renderer_host/render_view_host.h" |
| 14 #include "content/browser/tab_contents/tab_contents.h" |
| 15 #include "net/base/mock_host_resolver.h" |
| 16 |
| 17 namespace { |
| 18 |
| 19 class IsolatedAppApiTest : public ExtensionApiTest { |
| 20 public: |
| 21 // Returns whether the given tab's current URL has the given cookie. |
| 22 bool WARN_UNUSED_RESULT HasCookie(TabContents* contents, std::string cookie) { |
| 23 int value_size; |
| 24 std::string actual_cookie; |
| 25 automation_cookie_util::GetCookies(contents->GetURL(), contents, |
| 26 &value_size, &actual_cookie); |
| 27 return actual_cookie.find(cookie) != std::string::npos; |
| 28 } |
| 29 }; |
| 30 |
| 31 } // namespace |
| 32 |
| 33 // Tests that cookies set within an isolated app are not visible to normal |
| 34 // pages or other apps. |
| 35 IN_PROC_BROWSER_TEST_F(IsolatedAppApiTest, CookieIsolation) { |
| 36 CommandLine::ForCurrentProcess()->AppendSwitch( |
| 37 switches::kDisablePopupBlocking); |
| 38 CommandLine::ForCurrentProcess()->AppendSwitch( |
| 39 switches::kEnableExperimentalAppManifests); |
| 40 |
| 41 host_resolver()->AddRule("*", "127.0.0.1"); |
| 42 ASSERT_TRUE(test_server()->Start()); |
| 43 |
| 44 ASSERT_TRUE(LoadExtension(test_data_dir_.AppendASCII("isolated_apps/app1"))); |
| 45 ASSERT_TRUE(LoadExtension(test_data_dir_.AppendASCII("isolated_apps/app2"))); |
| 46 |
| 47 // The app under test acts on URLs whose host is "localhost", |
| 48 // so the URLs we navigate to must have host "localhost". |
| 49 GURL base_url = test_server()->GetURL( |
| 50 "files/extensions/api_test/isolated_apps/"); |
| 51 GURL::Replacements replace_host; |
| 52 std::string host_str("localhost"); // Must stay in scope with replace_host. |
| 53 replace_host.SetHostStr(host_str); |
| 54 base_url = base_url.ReplaceComponents(replace_host); |
| 55 |
| 56 browser()->NewTab(); |
| 57 ui_test_utils::NavigateToURL(browser(), base_url.Resolve("app1/main.html")); |
| 58 browser()->NewTab(); |
| 59 ui_test_utils::NavigateToURL(browser(), base_url.Resolve("app2/main.html")); |
| 60 browser()->NewTab(); |
| 61 ui_test_utils::NavigateToURL(browser(), |
| 62 base_url.Resolve("non_app/main.html")); |
| 63 |
| 64 // Ensure first two tabs have installed apps. |
| 65 TabContents* tab1 = browser()->GetTabContentsAt(1); |
| 66 TabContents* tab2 = browser()->GetTabContentsAt(2); |
| 67 TabContents* tab3 = browser()->GetTabContentsAt(3); |
| 68 ASSERT_TRUE(tab1->render_view_host()->installed_app() != NULL); |
| 69 ASSERT_TRUE(tab2->render_view_host()->installed_app() != NULL); |
| 70 ASSERT_TRUE(tab3->render_view_host()->installed_app() == NULL); |
| 71 |
| 72 // Check that each tab sees its own cookie. |
| 73 ASSERT_TRUE(HasCookie(tab1, "app1=3")); |
| 74 ASSERT_TRUE(HasCookie(tab2, "app2=4")); |
| 75 ASSERT_TRUE(HasCookie(tab3, "normalPage=5")); |
| 76 |
| 77 // Check that app1 tab cannot see the other cookies. |
| 78 ASSERT_FALSE(HasCookie(tab1, "app2")); |
| 79 ASSERT_FALSE(HasCookie(tab1, "normalPage")); |
| 80 |
| 81 // Check that app2 tab cannot see the other cookies. |
| 82 ASSERT_FALSE(HasCookie(tab2, "app1")); |
| 83 ASSERT_FALSE(HasCookie(tab2, "normalPage")); |
| 84 |
| 85 // Check that normal tab cannot see the other cookies. |
| 86 ASSERT_FALSE(HasCookie(tab3, "app1")); |
| 87 ASSERT_FALSE(HasCookie(tab3, "app2")); |
| 88 |
| 89 // Check that the non_app iframe cookie is associated with app1 and not the |
| 90 // normal tab. (For now, iframes are always rendered in their parent |
| 91 // process, even if they aren't in the app manifest.) |
| 92 ASSERT_TRUE(HasCookie(tab1, "nonAppFrame=6")); |
| 93 ASSERT_FALSE(HasCookie(tab3, "nonAppFrame")); |
| 94 } |
| 95 |
| 96 // Without the --enable-experimental-app-manifests flag, all the tabs |
| 97 // should see each others' cookies. |
| 98 IN_PROC_BROWSER_TEST_F(IsolatedAppApiTest, CookieIsolationRequiresFlag) { |
| 99 CommandLine::ForCurrentProcess()->AppendSwitch( |
| 100 switches::kDisablePopupBlocking); |
| 101 |
| 102 host_resolver()->AddRule("*", "127.0.0.1"); |
| 103 ASSERT_TRUE(test_server()->Start()); |
| 104 |
| 105 ASSERT_TRUE(LoadExtension(test_data_dir_.AppendASCII("isolated_apps/app1"))); |
| 106 ASSERT_TRUE(LoadExtension(test_data_dir_.AppendASCII("isolated_apps/app2"))); |
| 107 |
| 108 // The app under test acts on URLs whose host is "localhost", |
| 109 // so the URLs we navigate to must have host "localhost". |
| 110 GURL base_url = test_server()->GetURL( |
| 111 "files/extensions/api_test/isolated_apps/"); |
| 112 GURL::Replacements replace_host; |
| 113 std::string host_str("localhost"); // Must stay in scope with replace_host. |
| 114 replace_host.SetHostStr(host_str); |
| 115 base_url = base_url.ReplaceComponents(replace_host); |
| 116 |
| 117 browser()->NewTab(); |
| 118 ui_test_utils::NavigateToURL(browser(), base_url.Resolve("app1/main.html")); |
| 119 browser()->NewTab(); |
| 120 ui_test_utils::NavigateToURL(browser(), base_url.Resolve("app2/main.html")); |
| 121 browser()->NewTab(); |
| 122 ui_test_utils::NavigateToURL(browser(), |
| 123 base_url.Resolve("non_app/main.html")); |
| 124 |
| 125 // Check that tabs see each others' cookies. |
| 126 ASSERT_TRUE(HasCookie(browser()->GetTabContentsAt(1), "app2=4")); |
| 127 ASSERT_TRUE(HasCookie(browser()->GetTabContentsAt(1), "normalPage=5")); |
| 128 ASSERT_TRUE(HasCookie(browser()->GetTabContentsAt(1), "nonAppFrame=6")); |
| 129 ASSERT_TRUE(HasCookie(browser()->GetTabContentsAt(2), "app1=3")); |
| 130 ASSERT_TRUE(HasCookie(browser()->GetTabContentsAt(2), "normalPage=5")); |
| 131 ASSERT_TRUE(HasCookie(browser()->GetTabContentsAt(2), "nonAppFrame=6")); |
| 132 ASSERT_TRUE(HasCookie(browser()->GetTabContentsAt(3), "app1=3")); |
| 133 ASSERT_TRUE(HasCookie(browser()->GetTabContentsAt(3), "app2=4")); |
| 134 ASSERT_TRUE(HasCookie(browser()->GetTabContentsAt(3), "nonAppFrame=6")); |
| 135 } |
OLD | NEW |