OLD | NEW |
---|---|
(Empty) | |
1 #!/usr/bin/env python | |
2 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | |
3 # Use of this source code is governed by a BSD-style license that can be | |
4 # found in the LICENSE file. | |
5 | |
6 import os | |
7 import logging | |
8 | |
9 import pyauto_functional # Must be imported before pyauto | |
10 import pyauto | |
11 | |
12 | |
13 class CookiesTest(pyauto.PyUITest): | |
14 """Tests for Cookies.""" | |
15 | |
16 def __init__(self, methodName='runTest'): | |
17 super(CookiesTest, self).__init__(methodName) | |
18 self.test_host = os.environ.get('COOKIES_TEST_HOST', 'google.com') | |
19 | |
20 def setUp(self): | |
21 pyauto.PyUITest.setUp(self); | |
22 # Set the startup preference to "open the new tab page", if the startup | |
23 # preference is "continue where I left off", session cookies will be saved. | |
24 self.SetPrefs(pyauto.kRestoreOnStartup, 5); | |
25 | |
26 def _CookieCheckIncognitoWindow(self, url, cookies_enabled=True): | |
27 """Check the cookie for the given URL in an incognito window.""" | |
28 # Navigate to the URL in an incognito window and verify no cookie is set. | |
29 self.assertFalse(self.GetCookie(pyauto.GURL(url)), | |
30 msg='Cannot run with pre-existing cookies') | |
31 self.RunCommand(pyauto.IDC_NEW_INCOGNITO_WINDOW) | |
32 self.assertFalse(self.GetCookie(pyauto.GURL(url), 1), | |
33 msg='Fresh incognito window should not have cookies') | |
34 self.NavigateToURL(url, 1, 0) | |
35 if cookies_enabled: | |
36 self.assertTrue(self.GetCookie(pyauto.GURL(url), 1), | |
37 msg='Cookies not set in incognito window') | |
38 else: | |
39 self.assertFalse(self.GetCookie(pyauto.GURL(url), 1), | |
40 msg='Cookies not blocked in incognito window') | |
41 self.assertFalse(self.GetCookie(pyauto.GURL(url)), | |
42 msg='Incognito mode cookies leaking to regular profile') | |
scottmg
2012/08/24 22:59:26
did this get lost?
jam
2012/08/25 00:23:24
yeah you're right, added.
| |
43 self.CloseBrowserWindow(1); | |
44 self.RunCommand(pyauto.IDC_NEW_INCOGNITO_WINDOW) | |
45 self.assertFalse(self.GetCookie(pyauto.GURL(url), 1), | |
46 msg='Cookies persisting between incognito sessions') | |
47 self.CloseBrowserWindow(1); | |
48 | |
49 def testSetCookies(self): | |
50 """Test setting cookies and getting the value.""" | |
51 cookie_url = pyauto.GURL(self.GetFileURLForDataPath('title1.html')) | |
52 cookie_val = 'foo=bar' | |
53 self.assertFalse(self.GetCookie(cookie_url), | |
54 msg='There should be no cookies for %s' % cookie_url) | |
55 self.SetCookie(cookie_url, cookie_val) | |
56 self.assertEqual(cookie_val, self.GetCookie(cookie_url), | |
57 msg='Could not find the cookie value foo=bar') | |
58 | |
59 def testCookiesHttp(self): | |
60 """Test cookies set over HTTP for incognito and regular windows.""" | |
61 http_url = 'http://%s' % self.test_host | |
62 self.assertFalse(self.GetCookie(pyauto.GURL(http_url)), | |
63 msg='There should be no cookies for %s' % http_url) | |
64 # Incognito window | |
65 self._CookieCheckIncognitoWindow(http_url) | |
66 # Regular window | |
67 self.NavigateToURL(http_url) | |
68 cookie_data = self.GetCookie(pyauto.GURL(http_url)) | |
69 self.assertTrue(cookie_data, | |
70 msg='Cookie did not exist after loading %s' % http_url) | |
71 # Restart and verify that the cookie persists. | |
72 self.RestartBrowser(clear_profile=False) | |
73 self.assertTrue(self.GetCookie(pyauto.GURL(http_url)), | |
74 msg='Cookie did not persist after restarting session.') | |
75 | |
76 def testCookiesHttps(self): | |
77 """Test cookies set over HTTPS for incognito and regular windows.""" | |
78 https_url = 'https://%s' % self.test_host | |
79 self.assertFalse(self.GetCookie(pyauto.GURL(https_url)), | |
80 msg='There should be no cookies for %s' % https_url) | |
81 # Incognito window | |
82 self._CookieCheckIncognitoWindow(https_url) | |
83 # Regular window | |
84 self.NavigateToURL(https_url) | |
85 cookie_data = self.GetCookie(pyauto.GURL(https_url)) | |
86 self.assertTrue(cookie_data, | |
87 msg='Cookie did not exist after loading %s' % https_url) | |
88 # Restart and verify that the cookie persists. | |
89 self.RestartBrowser(clear_profile=False) | |
90 self.assertTrue(self.GetCookie(pyauto.GURL(https_url)), | |
91 msg='Cookie did not persist after restarting session.') | |
92 | |
93 def testCookiesFile(self): | |
94 """Test cookies set from file:// url for incognito and regular windows.""" | |
95 file_url = self.GetFileURLForDataPath('setcookie.html') | |
96 self.assertFalse(self.GetCookie(pyauto.GURL(file_url)), | |
97 msg='There should be no cookie for file url %s' % file_url) | |
98 # Incognito window | |
99 self._CookieCheckIncognitoWindow(file_url) | |
100 # Regular window | |
101 self.NavigateToURL(file_url) | |
102 self.assertEqual('name=Good', self.GetCookie(pyauto.GURL(file_url)), | |
103 msg='Cookie does not exist after navigating to the page.') | |
scottmg
2012/08/24 22:59:26
these are all negative tests (that file:// blocks)
jam
2012/08/25 00:23:24
not sure what you mean? this tests is checking tha
| |
104 # Restart and verify that cookie persists | |
105 self.RestartBrowser(clear_profile=False) | |
106 self.assertEqual('name=Good', self.GetCookie(pyauto.GURL(file_url)), | |
107 msg='Cookie did not persist after restarting session.') | |
108 | |
109 def testBlockCookies(self): | |
110 """Verify that cookies are being blocked.""" | |
111 file_url = self.GetFileURLForDataPath('setcookie.html') | |
112 http_url = 'http://%s' % self.test_host | |
113 https_url = 'https://%s' % self.test_host | |
114 self.assertFalse(self.GetCookie(pyauto.GURL(file_url)), | |
115 msg='There should be no cookie for file url %s' % file_url) | |
116 | |
117 # Set the preference to block all cookies. | |
118 self.SetPrefs(pyauto.kDefaultContentSettings, {u'cookies': 2}) | |
119 # Regular window | |
120 self.NavigateToURL(http_url) | |
121 self.AppendTab(pyauto.GURL(https_url)) | |
122 self.AppendTab(pyauto.GURL(file_url)) | |
123 self.assertFalse(self.GetCookie(pyauto.GURL(file_url)), | |
124 msg='Cookies are not blocked.') | |
125 self.assertFalse(self.GetCookie(pyauto.GURL(http_url)), | |
126 msg='Cookies are not blocked.') | |
127 self.assertFalse(self.GetCookie(pyauto.GURL(https_url)), | |
128 msg='Cookies are not blocked.') | |
129 | |
130 # Incognito window | |
131 self._CookieCheckIncognitoWindow(http_url, cookies_enabled=False) | |
132 | |
133 # Restart and verify that cookie setting persists and there are no cookies. | |
134 self.SetPrefs(pyauto.kRestoreOnStartup, 1) | |
135 self.RestartBrowser(clear_profile=False) | |
136 self.assertEquals({u'cookies': 2}, | |
137 self.GetPrefsInfo().Prefs(pyauto.kDefaultContentSettings), | |
138 msg='Cookie setting did not persist after restarting session.') | |
139 self.assertFalse(self.GetCookie(pyauto.GURL(file_url)), | |
140 msg='Cookies are not blocked.') | |
141 self.assertFalse(self.GetCookie(pyauto.GURL(http_url)), | |
142 msg='Cookies are not blocked.') | |
143 self.assertFalse(self.GetCookie(pyauto.GURL(https_url)), | |
144 msg='Cookies are not blocked.') | |
145 | |
146 def testAllowCookiesUsingExceptions(self): | |
147 """Verify that cookies can be allowed and set using exceptions for | |
148 particular website(s) when all others are blocked.""" | |
149 http_url = 'http://%s' % self.test_host | |
150 self.assertFalse(self.GetCookie(pyauto.GURL(http_url)), | |
151 msg='There should be no cookies on %s' % http_url) | |
152 | |
153 # Set the preference to block all cookies. | |
154 self.SetPrefs(pyauto.kDefaultContentSettings, {u'cookies': 2}) | |
155 | |
156 self.NavigateToURL(http_url) | |
157 # Check that no cookies are stored. | |
158 self.assertFalse(self.GetCookie(pyauto.GURL(http_url)), | |
159 msg='A cookie was found when it should not have been.') | |
160 | |
161 # Creating an exception to allow cookies from http://www.google.com. | |
162 self.SetPrefs(pyauto.kContentSettingsPatternPairs, | |
163 {'[*.]%s,*' % self.test_host: { 'cookies': 1}}) | |
164 # Navigate to google.com and check if cookies are set. | |
165 self.NavigateToURL(http_url) | |
166 self.assertTrue(self.GetCookie(pyauto.GURL(http_url)), | |
167 msg='Cookies are not set for the exception.') | |
168 | |
169 def testBlockCookiesUsingExceptions(self): | |
170 """Verify that cookies can be blocked for a specific website | |
171 using exceptions.""" | |
172 http_url = 'http://%s' % self.test_host | |
173 file_url = self.GetFileURLForDataPath('setcookie.html') | |
174 self.assertFalse(self.GetCookie(pyauto.GURL(http_url)), | |
175 msg='There should be no cookies on %s' % http_url) | |
176 self.assertFalse(self.GetCookie(pyauto.GURL(file_url)), | |
177 msg='There should be no cookies on %s' % file_url) | |
178 | |
179 # Create an exception to block cookies from http://www.google.com | |
180 self.SetPrefs(pyauto.kContentSettingsPatternPairs, | |
181 {'[*.]%s,*' % self.test_host: { 'cookies': 2}}) | |
182 | |
183 # Navigate to google.com and check if cookies are blocked. | |
184 self.NavigateToURL(http_url) | |
185 self.assertFalse(self.GetCookie(pyauto.GURL(http_url)), | |
186 msg='Cookies are being set for the exception.') | |
187 | |
188 # Check if cookies are being set for other websites/webpages. | |
189 self.AppendTab(pyauto.GURL(file_url)) | |
190 self.assertEqual('name=Good', self.GetCookie(pyauto.GURL(file_url)), | |
191 msg='Unable to find cookie name=Good') | |
192 | |
193 def testAllowCookiesForASessionUsingExceptions(self): | |
194 """Verify that cookies can be allowed and set using exceptions for | |
195 particular website(s) only for a session when all others are blocked.""" | |
196 http_url = 'http://%s' % self.test_host | |
197 self.assertFalse(self.GetCookie(pyauto.GURL(http_url)), | |
198 msg='There should be no cookies on %s' % http_url) | |
199 | |
200 # Set the preference to block all cookies. | |
201 self.SetPrefs(pyauto.kDefaultContentSettings, {u'cookies': 2}) | |
202 | |
203 self.NavigateToURL(http_url) | |
204 # Check that no cookies are stored. | |
205 self.assertFalse(self.GetCookie(pyauto.GURL(http_url)), | |
206 msg='Cookies were found for the url %s' % http_url) | |
207 | |
208 # Creating an exception to allow cookies for a session for google.com. | |
209 self.SetPrefs(pyauto.kContentSettingsPatternPairs, | |
210 {'[*.]%s,*' % self.test_host: { 'cookies': 4}}) | |
211 | |
212 # Navigate to google.com and check if cookies are set. | |
213 self.NavigateToURL(http_url) | |
214 self.assertTrue(self.GetCookie(pyauto.GURL(http_url)), | |
215 msg='Cookies are not set for the exception.') | |
216 # Restart the browser to check that the cookie doesn't persist. | |
217 # (This fails on ChromeOS because kRestoreOnStartup is ignored and | |
218 # the startup preference is always "continue where I left off.") | |
219 if not self.IsChromeOS(): | |
220 self.RestartBrowser(clear_profile=False) | |
221 self.assertFalse(self.GetCookie(pyauto.GURL(http_url)), | |
222 msg='Cookie persisted after restarting session.') | |
223 | |
224 if __name__ == '__main__': | |
225 pyauto_functional.Main() | |
OLD | NEW |