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