1# Copyright 2016 The Chromium OS 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 5from autotest_lib.client.common_lib import error 6from autotest_lib.client.cros.enterprise import enterprise_policy_base 7 8 9class policy_CookiesSessionOnlyForUrls( 10 enterprise_policy_base.EnterprisePolicyTest): 11 """ 12 Test effect of CookiesSessionOnlyForUrls policy on Chrome OS. 13 14 The test verifies ChromeOS behaviour and appearance for a set of valid 15 values of the CookiesSessionOnlyForUrls user policy, when user policy 16 DefaultCookiesSetting=2 (block cookies for all URLs). Generally, cookies 17 shall not be stored for any visted page, except for those whose domain 18 matches an URL pattern specified in CookiesSessionOnlyForUrls. Also, 19 these URL patterns shall have their behavior set to 'Clear on exit', 20 indicating that they are marked for deletion when Chrome exits. 21 22 If the policy value Not set, then no visited page is allowed to store 23 cookies. In the same way, if the URL of the visited page is not listed in 24 the policy, then the visted page is not allowed to store cookies. If the 25 URL of the visited page is listed in the policy, then the page is allowed 26 to store cookies for the current session only. The corresponding test 27 cases are NotSet_Block, UrlNotIn_Block, and UrlIsIn_Allow. 28 29 Note that this test does not verify that cookies set to 'Clear on exit' 30 are actually deleted when the session ends. That functionality is tested 31 by the Chrome team. 32 33 """ 34 version = 1 35 36 def initialize(self, **kwargs): 37 self._initialize_test_constants() 38 super(policy_CookiesSessionOnlyForUrls, self).initialize(**kwargs) 39 self.start_webserver() 40 41 42 def _initialize_test_constants(self): 43 """Initialize test-specific constants, some from class constants.""" 44 self.POLICY_NAME = 'CookiesSessionOnlyForUrls' 45 self.COOKIE_NAME = 'cookie1' 46 self.TEST_FILE = 'cookie_status.html' 47 self.TEST_URL = '%s/%s' % (self.WEB_HOST, self.TEST_FILE) 48 self.COOKIE_EXCEPTIONS_PAGE = ( 49 'chrome://settings-frame/contentExceptions#cookies') 50 self.COOKIE_ALLOWED_MULTIPLE_URLS = ['https://testingwebsite.html', 51 self.WEB_HOST, 52 'http://doesnotmatter.com'] 53 self.COOKIE_BLOCKED_MULTIPLE_URLS = ['https://testingwebsite.html', 54 'https://somewebsite.com', 55 'http://doesnotmatter.com'] 56 self.TEST_CASES = { 57 'UrlIsIn_Allow': self.COOKIE_ALLOWED_MULTIPLE_URLS, 58 'UrlNotIn_Block': self.COOKIE_BLOCKED_MULTIPLE_URLS, 59 'NotSet_Block': None 60 } 61 self.SUPPORTING_POLICIES = {'DefaultCookiesSetting': 2} 62 63 64 def _is_cookie_blocked(self, url): 65 """ 66 Return True if cookie is blocked for the URL, else return False. 67 68 @param url: URL of the page to load. 69 70 """ 71 tab = self.navigate_to_url(url) 72 cookie_value = tab.GetCookieByName(self.COOKIE_NAME) 73 tab.Close() 74 return cookie_value is None 75 76 77 def _is_cookie_clear_on_exit(self, url): 78 """ 79 Return True if cookie for |url| has behavior set to 'Clear on exit'. 80 81 @param url: string url pattern for cookie exception. 82 @returns: True if cookie behavior is set to 'Clear on exit'. 83 """ 84 js_cmd = (''' 85 var exception_area=document.getElementById('content-settings-exceptions-area'); 86 var contents=exception_area.getElementsByClassName('content-area')[0]; 87 var contents_children = contents.children; 88 var cookie_idx = -1; 89 var cookie_behavior = ''; 90 for (var i=0; i<contents_children.length; i++) { 91 var content = contents_children[i]; 92 var type = content.getAttribute('contenttype'); 93 if (type == 'cookies') { 94 var cookie_items = content.getElementsByClassName('deletable-item'); 95 for (var j=0; j<cookie_items.length; j++) { 96 var cookie_item = cookie_items[j]; 97 var cookie_pattern = cookie_item.getElementsByClassName('exception-pattern')[0]; 98 var pattern = cookie_pattern.innerText.trim(); 99 var cookie_setting = cookie_item.getElementsByClassName('exception-setting')[0]; 100 var setting = cookie_setting.innerText.trim(); 101 if (pattern == '%s') { 102 cookie_idx = j; 103 cookie_behavior = setting; 104 break; 105 } 106 } 107 break; 108 } 109 if (cookie_idx >= 0) { break; } 110 } 111 cookie_behavior; 112 ''' % url) 113 tab = self.navigate_to_url(self.COOKIE_EXCEPTIONS_PAGE) 114 cookie_behavior = self.get_elements_from_page(tab, js_cmd) 115 tab.Close() 116 return cookie_behavior == 'Clear on exit' 117 118 119 def _test_cookies_allowed_for_urls(self, policy_value): 120 """ 121 Verify CrOS enforces CookiesSessionOnlyForUrls policy value. 122 123 When CookiesSessionOnlyForUrls policy is set to a list of one or more 124 more urls, verify that cookies are allowed for a page that matches a 125 URL pattern in the list, but are blocked for a page whose URL pattern 126 is not in the list. When set to None, verify that cookies are 127 blocked for all URLs. 128 129 @param policy_value: policy value expected. 130 131 @raises: TestFail if cookies are blocked/not blocked based on the 132 policy value. 133 134 """ 135 cookie_is_blocked = self._is_cookie_blocked(self.TEST_URL) 136 if policy_value and self.WEB_HOST in policy_value: 137 if cookie_is_blocked: 138 raise error.TestFail('Cookie should be allowed.') 139 else: 140 if not cookie_is_blocked: 141 raise error.TestFail('Cookie should be blocked.') 142 143 cookie_is_clear_on_exit = self._is_cookie_clear_on_exit(self.WEB_HOST) 144 if policy_value and self.WEB_HOST in policy_value: 145 if not cookie_is_clear_on_exit: 146 raise error.TestFail('Cookie should be Clear on exit.') 147 else: 148 if cookie_is_clear_on_exit: 149 raise error.TestFail('Cookie should not be Clear on exit.') 150 151 152 def run_test_case(self, case): 153 """ 154 Setup and run the test configured for the specified test case. 155 156 @param case: Name of the test case to run. 157 158 """ 159 case_value = self.TEST_CASES[case] 160 self.setup_case(self.POLICY_NAME, case_value, self.SUPPORTING_POLICIES) 161 self._test_cookies_allowed_for_urls(case_value) 162