1# Copyright 2015 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 5# Copyright 2015 The Chromium OS Authors. All rights reserved. 6# Use of this source code is governed by a BSD-style license that can be 7# found in the LICENSE file. 8 9from autotest_lib.client.common_lib import error 10from autotest_lib.client.cros.enterprise import enterprise_policy_base 11 12 13class policy_CookiesAllowedForUrls(enterprise_policy_base.EnterprisePolicyTest): 14 """Test effect of the CookiesAllowedForUrls policy on Chrome OS behavior. 15 16 This test implicitly verifies one value of the DefaultCookiesSetting 17 policy as well. When the DefaultCookiesSetting policy value is set to 2, 18 cookies for all URLs shall not be stored (ie, shall be blocked), except 19 for the URL patterns specified by the CookiesAllowedForUrls policy. 20 21 The test verifies ChromeOS behaviour for different values of the 22 CookiesAllowedForUrls policy, i.e., for the policy value set to Not Set, 23 set to a single url/host pattern, or when the policy is set to multiple 24 url/host patterns. It also verifies that cookies are blocked for urls that 25 are not part of the policy value. 26 27 The corresponding three test cases are NotSet_CookiesBlocked, 28 SingleUrl_CookiesAllowed, MultipleUrls_CookiesAllowed, and 29 MultipleUrls_CookiesBlocked. 30 """ 31 version = 1 32 33 def initialize(self, **kwargs): 34 self._initialize_test_constants() 35 super(policy_CookiesAllowedForUrls, self).initialize(**kwargs) 36 self.start_webserver() 37 38 39 def _initialize_test_constants(self): 40 """Initialize test-specific constants, some from class constants.""" 41 self.POLICY_NAME = 'CookiesAllowedForUrls' 42 self.COOKIE_NAME = 'cookie1' 43 self.TEST_FILE = 'cookie_status.html' 44 self.TEST_URL = '%s/%s' % (self.WEB_HOST, self.TEST_FILE) 45 self.COOKIE_ALLOWED_SINGLE_FILE = [self.WEB_HOST] 46 self.COOKIE_ALLOWED_MULTIPLE_FILES = ['http://google.com', 47 self.WEB_HOST, 48 'http://doesnotmatter.com'] 49 self.COOKIE_BLOCKED_MULTIPLE_FILES = ['https://testingwebsite.html', 50 'https://somewebsite.com', 51 'http://doesnotmatter.com'] 52 self.TEST_CASES = { 53 'NotSet_Block': None, 54 'SingleUrl_Allow': self.COOKIE_ALLOWED_SINGLE_FILE, 55 'MultipleUrls_Allow': self.COOKIE_ALLOWED_MULTIPLE_FILES, 56 'MultipleUrls_Block': self.COOKIE_BLOCKED_MULTIPLE_FILES 57 } 58 self.SUPPORTING_POLICIES = {'DefaultCookiesSetting': 2} 59 60 61 def _is_cookie_blocked(self, url): 62 """Return True if cookie is blocked for the URL else return False. 63 64 @param url: Url of the page which is loaded to check whether it's 65 cookie is blocked or stored. 66 """ 67 tab = self.navigate_to_url(url) 68 return tab.GetCookieByName(self.COOKIE_NAME) is None 69 70 71 def _test_cookies_allowed_for_urls(self, policy_value): 72 """Verify CrOS enforces CookiesAllowedForUrls policy value. 73 74 When the CookiesAllowedForUrls policy is set to one or more urls/hosts, 75 check that cookies are not blocked for the urls/urlpatterns listed in 76 the policy value. When set to None, check that cookies are blocked for 77 all URLs. 78 79 @param policy_value: policy value for this case. 80 @raises: TestFail if cookies are blocked/not blocked based on the 81 corresponding policy values. 82 """ 83 cookie_is_blocked = self._is_cookie_blocked(self.TEST_URL) 84 85 if policy_value and self.WEB_HOST in policy_value: 86 if cookie_is_blocked: 87 raise error.TestFail('Cookies should be allowed.') 88 else: 89 if not cookie_is_blocked: 90 raise error.TestFail('Cookies should be blocked.') 91 92 93 def run_test_case(self, case): 94 """Setup and run the test configured for the specified test case. 95 96 Set the expected |policy_value| and |policies_dict| data defined for 97 the specified test |case|, and run the test. 98 99 @param case: Name of the test case to run. 100 """ 101 case_value = self.TEST_CASES[case] 102 self.setup_case(self.POLICY_NAME, case_value, self.SUPPORTING_POLICIES) 103 self._test_cookies_allowed_for_urls(case_value) 104