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