OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | |
Matt Perry
2011/01/26 20:09:23
One other thing that might be worth testing is an
Charlie Reis
2011/03/01 21:33:11
Good idea-- I've added that. We plan to keep ifra
| |
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/browser_thread.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/renderer_host/render_view_host.h" | |
11 #include "chrome/browser/tab_contents/tab_contents.h" | |
12 #include "chrome/browser/ui/browser.h" | |
13 #include "chrome/common/chrome_switches.h" | |
14 #include "chrome/common/net/url_request_context_getter.h" | |
15 #include "chrome/test/ui_test_utils.h" | |
16 #include "net/base/cookie_store.h" | |
17 #include "net/base/mock_host_resolver.h" | |
18 | |
19 namespace { | |
20 | |
21 void GetCookiesOnIOThread( | |
22 const GURL& url, | |
23 const scoped_refptr<URLRequestContextGetter>& context_getter, | |
24 base::WaitableEvent* event, | |
25 std::string* cookies) { | |
26 *cookies = context_getter->GetCookieStore()->GetCookies(url); | |
27 event->Signal(); | |
28 } | |
29 | |
30 class IsolatedAppApiTest : public ExtensionApiTest { | |
31 public: | |
32 // Gets the cookie string and size for the given URL, using the context | |
33 // associated with the currently visible page or app in the given | |
34 // TabContents. | |
35 void GetCookies(const GURL& url, TabContents* contents, int* value_size, | |
36 std::string* value) { | |
37 *value_size = -1; | |
38 if (url.is_valid() && contents) { | |
39 // Since we are on the UI thread don't call GetURLRequestContext(). | |
40 // Be sure to get the request context specific to the current app. | |
41 const Extension* installed_app = | |
42 contents->render_view_host()->installed_app(); | |
43 scoped_refptr<URLRequestContextGetter> context_getter = | |
44 contents->profile()->GetRequestContext(installed_app); | |
45 | |
46 base::WaitableEvent event(true /* manual reset */, | |
47 false /* not initially signaled */); | |
48 CHECK(BrowserThread::PostTask( | |
49 BrowserThread::IO, FROM_HERE, | |
50 NewRunnableFunction(&GetCookiesOnIOThread, | |
51 url, context_getter, &event, value))); | |
52 event.Wait(); | |
53 | |
54 *value_size = static_cast<int>(value->size()); | |
Matt Perry
2011/01/26 20:09:23
seems like this param is unused
Charlie Reis
2011/03/01 21:33:11
True. I was keeping it around from the TestingAut
| |
55 } | |
56 } | |
57 | |
58 // Returns whether the given tab's current URL has the given cookie. | |
59 bool WARN_UNUSED_RESULT HasCookie(TabContents* contents, std::string cookie) { | |
60 int cookie_size; | |
61 std::string actual_cookie; | |
62 GetCookies(contents->GetURL(), contents, &cookie_size, &actual_cookie); | |
63 return actual_cookie.find(cookie) != std::string::npos; | |
64 } | |
65 }; | |
66 | |
67 } // namespace | |
68 | |
69 // Tests that cookies set within an isolated app are not visible to normal | |
70 // pages or other apps. | |
71 IN_PROC_BROWSER_TEST_F(IsolatedAppApiTest, CookieIsolation) { | |
72 CommandLine::ForCurrentProcess()->AppendSwitch( | |
73 switches::kDisablePopupBlocking); | |
74 CommandLine::ForCurrentProcess()->AppendSwitch( | |
75 switches::kEnableExperimentalAppManifests); | |
76 | |
77 host_resolver()->AddRule("*", "127.0.0.1"); | |
78 ASSERT_TRUE(test_server()->Start()); | |
79 | |
80 ASSERT_TRUE(LoadExtension(test_data_dir_.AppendASCII("isolated_apps/app1"))); | |
81 ASSERT_TRUE(LoadExtension(test_data_dir_.AppendASCII("isolated_apps/app2"))); | |
82 | |
83 // The app under test acts on URLs whose host is "localhost", | |
84 // so the URLs we navigate to must have host "localhost". | |
85 GURL base_url = test_server()->GetURL( | |
86 "files/extensions/api_test/isolated_apps/"); | |
87 GURL::Replacements replace_host; | |
88 std::string host_str("localhost"); // must stay in scope with replace_host | |
89 replace_host.SetHostStr(host_str); | |
90 base_url = base_url.ReplaceComponents(replace_host); | |
91 | |
92 browser()->NewTab(); | |
93 ui_test_utils::NavigateToURL(browser(), base_url.Resolve("app1/main.html")); | |
94 browser()->NewTab(); | |
95 ui_test_utils::NavigateToURL(browser(), base_url.Resolve("app2/main.html")); | |
96 browser()->NewTab(); | |
97 ui_test_utils::NavigateToURL(browser(), | |
98 base_url.Resolve("non_app/main.html")); | |
99 | |
100 // Ensure first two tabs have installed apps | |
Matt Perry
2011/01/26 20:09:23
nit: end with .
Charlie Reis
2011/03/01 21:33:11
Done.
| |
101 TabContents* tab1 = browser()->GetTabContentsAt(1); | |
102 TabContents* tab2 = browser()->GetTabContentsAt(2); | |
103 TabContents* tab3 = browser()->GetTabContentsAt(3); | |
104 ASSERT_TRUE(tab1->render_view_host()->installed_app() != NULL); | |
105 ASSERT_TRUE(tab2->render_view_host()->installed_app() != NULL); | |
106 ASSERT_TRUE(tab3->render_view_host()->installed_app() == NULL); | |
107 | |
108 // Check that each tab sees its own cookie. | |
109 ASSERT_TRUE(HasCookie(tab1, "app1=3")); | |
110 ASSERT_TRUE(HasCookie(tab2, "app2=4")); | |
111 ASSERT_TRUE(HasCookie(tab3, "normalPage=5")); | |
112 | |
113 // Check that app1 tab cannot see the other cookies. | |
114 ASSERT_FALSE(HasCookie(tab1, "app2")); | |
115 ASSERT_FALSE(HasCookie(tab1, "normalPage")); | |
116 | |
117 // Check that app2 tab cannot see the other cookies. | |
118 ASSERT_FALSE(HasCookie(tab2, "app1")); | |
119 ASSERT_FALSE(HasCookie(tab2, "normalPage")); | |
120 | |
121 // Check that normal tab cannot see the other cookies. | |
122 ASSERT_FALSE(HasCookie(tab3, "app1")); | |
123 ASSERT_FALSE(HasCookie(tab3, "app2")); | |
124 } | |
125 | |
126 // Without the --enable-experimental-app-manifests flag, all the tabs | |
127 // should see each others' cookies. | |
128 IN_PROC_BROWSER_TEST_F(IsolatedAppApiTest, CookieIsolationRequiresFlag) { | |
129 CommandLine::ForCurrentProcess()->AppendSwitch( | |
130 switches::kDisablePopupBlocking); | |
131 | |
132 host_resolver()->AddRule("*", "127.0.0.1"); | |
133 ASSERT_TRUE(test_server()->Start()); | |
134 | |
135 ASSERT_TRUE(LoadExtension(test_data_dir_.AppendASCII("isolated_apps/app1"))); | |
136 ASSERT_TRUE(LoadExtension(test_data_dir_.AppendASCII("isolated_apps/app2"))); | |
137 | |
138 // The app under test acts on URLs whose host is "localhost", | |
139 // so the URLs we navigate to must have host "localhost". | |
140 GURL base_url = test_server()->GetURL( | |
141 "files/extensions/api_test/isolated_apps/"); | |
142 GURL::Replacements replace_host; | |
143 std::string host_str("localhost"); // must stay in scope with replace_host | |
144 replace_host.SetHostStr(host_str); | |
145 base_url = base_url.ReplaceComponents(replace_host); | |
146 | |
147 browser()->NewTab(); | |
148 ui_test_utils::NavigateToURL(browser(), base_url.Resolve("app1/main.html")); | |
149 browser()->NewTab(); | |
150 ui_test_utils::NavigateToURL(browser(), base_url.Resolve("app2/main.html")); | |
151 browser()->NewTab(); | |
152 ui_test_utils::NavigateToURL(browser(), | |
153 base_url.Resolve("non_app/main.html")); | |
154 | |
155 // Check that tabs see each others' cookies. | |
156 ASSERT_TRUE(HasCookie(browser()->GetTabContentsAt(1), "app2=4")); | |
157 ASSERT_TRUE(HasCookie(browser()->GetTabContentsAt(1), "normalPage=5")); | |
158 ASSERT_TRUE(HasCookie(browser()->GetTabContentsAt(2), "app1=3")); | |
159 ASSERT_TRUE(HasCookie(browser()->GetTabContentsAt(2), "normalPage=5")); | |
160 ASSERT_TRUE(HasCookie(browser()->GetTabContentsAt(3), "app1=3")); | |
161 ASSERT_TRUE(HasCookie(browser()->GetTabContentsAt(3), "app2=4")); | |
162 } | |
OLD | NEW |