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 5import logging 6 7from autotest_lib.client.common_lib import error 8from autotest_lib.client.cros.enterprise import enterprise_policy_base 9 10 11class policy_URLWhitelist(enterprise_policy_base.EnterprisePolicyTest): 12 """Test effect of URLWhitleist policy on Chrome OS behavior. 13 14 Navigate to all the websites in the BLOCKED_URLS_LIST. Verify that the 15 websites specified by the URLWhitelist policy value are allowed. Also 16 verify that the websites not in the URLWhitelist policy value are blocked. 17 18 Two test cases (SinglePage_Allowed, MultiplePages_Allowed) are designed to 19 verify that the URLs specified in the URLWhitelist policy are allowed. 20 The third test case (NotSet_Blocked) is designed to verify that all of 21 the URLs are blocked since the URLWhitelistlist policy is set to None. 22 23 The test case shall pass if the URLs that are part of the URLWhitelist 24 policy value are allowed. The test case shall also pass if the URLs that 25 are not part of the URLWhitelist policy value are blocked. The test case 26 shall fail if the above behavior is not enforced. 27 28 """ 29 version = 1 30 31 def initialize(self, **kwargs): 32 self._initialize_test_constants() 33 super(policy_URLWhitelist, self).initialize(**kwargs) 34 self.start_webserver() 35 36 37 def _initialize_test_constants(self): 38 """Initialize test-specific constants, some from class constants.""" 39 self.POLICY_NAME = 'URLWhitelist' 40 self.URL_BASE = '%s/%s' % (self.WEB_HOST, 'website') 41 self.BLOCKED_URLS_LIST = [self.URL_BASE + website for website in 42 ['/website1.html', 43 '/website2.html', 44 '/website3.html']] 45 self.SINGLE_WHITELISTED_FILE = self.BLOCKED_URLS_LIST[:1] 46 self.MULTIPLE_WHITELISTED_FILES = self.BLOCKED_URLS_LIST[:2] 47 self.BLOCKED_USER_MESSAGE = 'Webpage Blocked' 48 self.BLOCKED_ERROR_MESSAGE = 'ERR_BLOCKED_BY_ADMINISTRATOR' 49 50 self.TEST_CASES = { 51 'NotSet_Blocked': None, 52 'SinglePage_Allowed': self.SINGLE_WHITELISTED_FILE, 53 'MultiplePages_Allowed': self.MULTIPLE_WHITELISTED_FILES 54 } 55 self.SUPPORTING_POLICIES = {'URLBlacklist': self.BLOCKED_URLS_LIST} 56 57 58 def _scrape_text_from_webpage(self, tab): 59 """Return a list of filtered text on the web page. 60 61 @param tab: tab containing the website to be parsed. 62 @raises: TestFail if the expected text was not found on the page. 63 """ 64 parsed_message_string = '' 65 parsed_message_list = [] 66 page_scrape_cmd = 'document.getElementById("main-message").innerText;' 67 try: 68 parsed_message_string = tab.EvaluateJavaScript(page_scrape_cmd) 69 except Exception as err: 70 raise error.TestFail('Unable to find the expected ' 71 'text content on the test ' 72 'page: %s\n %r'%(tab.url, err)) 73 logging.info('Parsed message:%s', parsed_message_string) 74 parsed_message_list = [str(word) for word in 75 parsed_message_string.split('\n') if word] 76 return parsed_message_list 77 78 79 def _is_url_blocked(self, url): 80 """Return True if the URL is blocked else returns False. 81 82 @param url: The URL to be checked whether it is blocked. 83 """ 84 parsed_message_list = [] 85 tab = self.navigate_to_url(url) 86 parsed_message_list = self._scrape_text_from_webpage(tab) 87 if len(parsed_message_list) == 2 and \ 88 parsed_message_list[0] == 'Website enabled' and \ 89 parsed_message_list[1] == 'Website is enabled': 90 return False 91 92 # Check if accurate user error message is shown on the error page. 93 if parsed_message_list[0] != self.BLOCKED_USER_MESSAGE or \ 94 parsed_message_list[1] != self.BLOCKED_ERROR_MESSAGE: 95 logging.warning('The Blocked page user notification ' 96 'messages, %s and %s are not displayed on ' 97 'the blocked page. The messages may have ' 98 'been modified. Please check and update the ' 99 'messages in this file accordingly.', 100 self.BLOCKED_USER_MESSAGE, 101 self.BLOCKED_ERROR_MESSAGE) 102 return True 103 104 105 def _test_url_whitelist(self, policy_value): 106 """Verify CrOS enforces URLWhitelist policy value. 107 108 Navigate to all the websites in the BLOCKED_URLS_LIST. Verify that 109 the websites specified by the URLWhitelist policy value allowed. 110 Also verify that the websites not in the URLWhitelist policy value 111 are blocked. 112 113 @param policy_value: policy value expected. 114 115 @raises: TestFail if url is blocked/not blocked based on the 116 corresponding policy values. 117 """ 118 for url in self.BLOCKED_URLS_LIST: 119 url_is_blocked = self._is_url_blocked(url) 120 if policy_value: 121 if url in policy_value and url_is_blocked: 122 raise error.TestFail('The URL %s should have been ' 123 'allowed by policy, but it ' 124 'was blocked.' % url) 125 elif url not in policy_value and not url_is_blocked: 126 raise error.TestFail('The URL %s should have been ' 127 'blocked by policy, but it ' 128 'was allowed.' % url) 129 130 elif not url_is_blocked: 131 raise error.TestFail('The URL %s should have been blocked' 132 'by policy, but it was allowed.' % url) 133 134 135 def run_test_case(self, case): 136 """Setup and run the test configured for the specified test case. 137 138 @param case: Name of the test case to run. 139 """ 140 case_value = self.TEST_CASES[case] 141 self.setup_case(self.POLICY_NAME, case_value, self.SUPPORTING_POLICIES) 142 self._test_url_whitelist(case_value) 143