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_URLBlacklist(enterprise_policy_base.EnterprisePolicyTest): 12 """Test effect of URLBlacklist policy on Chrome OS behavior. 13 14 Navigate to all the websites in the ALL_URLS_LIST. Verify that the 15 websites specified by the URLBlackList policy are blocked. Throw a warning 16 if the user message on the blocked page is incorrect. 17 18 Two test cases (SinglePage_Blocked, MultiplePages_Blocked) are designed to 19 verify that the URLs specified in the URLBlacklist policy are blocked. 20 The third test case (NotSet_Allowed) is designed to verify that none of 21 the URLs are blocked since the URLBlacklist policy is set to None 22 23 The test case shall pass if the URLs that are part of the URLBlacklist 24 policy value are blocked. The test case shall also pass if the URLs that 25 are not part of the URLBlacklist policy value are allowed. 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_URLBlacklist, 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 = 'URLBlacklist' 40 self.URL_BASE = '%s/%s' % (self.WEB_HOST, 'website') 41 self.ALL_URLS_LIST = [self.URL_BASE + website for website in 42 ['/website1.html', 43 '/website2.html', 44 '/website3.html']] 45 self.SINGLE_BLACKLISTED_FILE = self.ALL_URLS_LIST[:1] 46 self.MULTIPLE_BLACKLISTED_FILES = self.ALL_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_Allowed': None, 52 'SinglePage_Blocked': self.SINGLE_BLACKLISTED_FILE, 53 'MultiplePages_Blocked': self.MULTIPLE_BLACKLISTED_FILES, 54 } 55 self.SUPPORTING_POLICIES = {'URLWhitelist': None} 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_blacklist(self, policy_value): 106 """Verify CrOS enforces URLBlacklist policy value. 107 108 Navigate to all the websites in the ALL_URLS_LIST. Verify that 109 the websites specified in the URLWhitelist policy value are 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.ALL_URLS_LIST: 119 url_is_blocked = self._is_url_blocked(url) 120 if policy_value: 121 if url in policy_value and not url_is_blocked: 122 raise error.TestFail('The URL %s should have been blocked' 123 ' by policy, but was allowed.' % url) 124 elif url_is_blocked: 125 raise error.TestFail('The URL %s should have been allowed' 126 'by policy, but was blocked.' % url) 127 128 129 def run_test_case(self, case): 130 """Setup and run the test configured for the specified test case. 131 132 @param case: Name of the test case to run. 133 """ 134 case_value = self.TEST_CASES[case] 135 self.setup_case(self.POLICY_NAME, case_value, self.SUPPORTING_POLICIES) 136 self._test_url_blacklist(case_value) 137