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 9import logging 10 11from autotest_lib.client.common_lib import error 12from autotest_lib.client.cros import enterprise_policy_base 13 14POLICY_NAME = 'CookiesBlockedForUrls' 15URL_BASE = 'http://localhost' 16URL_PORT = 8080 17URL_HOST = '%s:%d'%(URL_BASE, URL_PORT) 18URL_RESOURCE = '/test_data/testWebsite1.html' 19TEST_URL = URL_HOST + URL_RESOURCE 20COOKIE_NAME = 'cookie1' 21COOKIE_BLOCKED_SINGLE_FILE_DATA = [URL_HOST] 22COOKIE_BLOCKED_MULTIPLE_FILES_DATA = ['http://google.com', URL_HOST, 23 'http://doesnotmatter.com'] 24COOKIE_ALLOWED_MULTIPLE_FILES_DATA = ['https://testingwebsite.html' 25 'https://somewebsite.com', 26 'http://doesnotmatter.com'] 27#Setting the DefaultCookiesSetting to the value 1, allows cookies on all sites. 28SUPPORTING_POLICIES = {'DefaultCookiesSetting': 1} 29 30 31class policy_CookiesBlockedForUrls(enterprise_policy_base.EnterprisePolicyTest): 32 """ 33 Test effect of the CookiesBlockedForUrls policy on Chrome OS behavior. 34 35 This test implicitly verifies one of the settings of the 36 DefaultCookiesSetting policy as well. When DefaultCookiesSetting is set to 37 1, cookies for all URLs shall be stored (i.e., shall be not blocked), except 38 for the URL patterns specified by the CookiesBlockedForUrls policy value. 39 The test verifies ChromeOS behaviour for different values of the 40 CookiesBlockedForUrls policy eg, for the policy value set to Not Set, Set 41 to a single url/host pattern or when the policy is set to multiple url/host 42 patterns. It also excercises an additional scenario i.e., it tests that 43 cookies are allowed for urls that are not part of the policy value. The 44 corresponding three test cases are NotSet_CookiesAllowed, 45 SingleUrl_CookiesBlocked, MultipleUrls_CookiesBlocked and 46 and MultipleUrls_CookiesAllowed. 47 48 """ 49 version = 1 50 TEST_CASES = { 51 'NotSet_CookiesAllowed': '', 52 'SingleUrl_CookiesBlocked': COOKIE_BLOCKED_SINGLE_FILE_DATA, 53 'MultipleUrls_CookiesBlocked': COOKIE_BLOCKED_MULTIPLE_FILES_DATA, 54 'MultipleUrls_CookiesAllowed' : COOKIE_ALLOWED_MULTIPLE_FILES_DATA 55 } 56 57 def initialize(self, args=()): 58 super(policy_CookiesBlockedForUrls, self).initialize(args) 59 self.start_webserver(URL_PORT) 60 61 def _is_cookie_blocked(self, url): 62 """ 63 Returns True if the cookie is blocked for the URL else returns False. 64 65 @param url: Url of the page which is loaded to check whether it's 66 cookie is blocked or stored. 67 68 """ 69 tab = self.navigate_to_url(url) 70 return tab.GetCookieByName(COOKIE_NAME) is None 71 72 def _test_CookiesBlockedForUrls(self, policy_value, policies_json): 73 """ 74 Verify CrOS enforces CookiesBlockedForUrls policy value. 75 76 When the CookiesBlockedForUrls policy is set to one or more urls/hosts, 77 check that cookies are blocked for the urls/urlpatterns listed in 78 the policy value. When set to None, check that cookies are allowed for 79 all URLs. 80 81 @param policy_value: policy value expected on chrome://policy page. 82 @param policies_json: policy JSON data to send to the fake DM server. 83 @raises: TestFail if cookies are blocked/not blocked based on the 84 corresponding policy values. 85 86 """ 87 logging.info('Running _test_CookiesBlockedForUrls(%s, %s)', 88 policy_value, policies_json) 89 self.setup_case(POLICY_NAME, policy_value, policies_json) 90 91 cookie_is_blocked = self._is_cookie_blocked(TEST_URL) 92 93 if policy_value and URL_HOST in policy_value: 94 if not cookie_is_blocked: 95 raise error.TestFail('Cookies should be blocked.') 96 else: 97 if cookie_is_blocked: 98 raise error.TestFail('Cookies should be allowed.') 99 100 def _run_test_case(self, case): 101 """ 102 Setup and run the test configured for the specified test case. 103 104 Set the expected |policy_value| and |policies_json| data based on the 105 test |case|. If the user specified an expected |value| in the command 106 line args, then use it to set the |policy_value| and blank out the 107 |policies_json|. 108 109 @param case: Name of the test case to run. 110 111 """ 112 policy_value = None 113 policies_json = None 114 115 if self.is_value_given: 116 # If |value| was given in the command line args, then set expected 117 # |policy_value| to the given value, and |policies_json| to None. 118 policy_value = self.value 119 policies_json = None 120 else: 121 # Otherwise, set expected |policy_value| and setup |policies_json| 122 # data to the values required by the specified test |case|. 123 policy_value = ','.join(self.TEST_CASES[case]) 124 policy_json = {'CookiesBlockedForUrls': self.TEST_CASES[case]} 125 policies_json = SUPPORTING_POLICIES.copy() 126 policies_json.update(policy_json) 127 128 # Run test using the values configured for the test case. 129 self._test_CookiesBlockedForUrls(policy_value, policies_json) 130 131 def run_once(self): 132 # The run_once() method is required by autotest. We call the base 133 # class run_once_impl() method, which handles command-line run modes, 134 # and pass in the standard _run_test_case() method of this test. 135 self.run_once_impl(self._run_test_case) 136