• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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        """Initialize this test."""
33        self._initialize_test_constants()
34        super(policy_URLBlacklist, 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 = 'URLBlacklist'
41        self.URL_BASE = '%s/%s' % (self.WEB_HOST, 'website')
42        self.ALL_URLS_LIST = [self.URL_BASE + website for website in
43                              ['/website1.html',
44                               '/website2.html',
45                               '/website3.html']]
46        self.SINGLE_BLACKLISTED_FILE = self.ALL_URLS_LIST[:1]
47        self.MULTIPLE_BLACKLISTED_FILES = self.ALL_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_Allowed': None,
53            'SinglePage_Blocked': self.SINGLE_BLACKLISTED_FILE,
54            'MultiplePages_Blocked': self.MULTIPLE_BLACKLISTED_FILES,
55        }
56        self.SUPPORTING_POLICIES = {'URLWhitelist': None}
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_blacklist(self, policy_value):
107        """Verify CrOS enforces URLBlacklist policy value.
108
109        Navigate to all the websites in the ALL_URLS_LIST. Verify that
110        the websites specified in the URLWhitelist policy value are 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.ALL_URLS_LIST:
120            url_is_blocked = self._is_url_blocked(url)
121            if policy_value:
122                if url in policy_value and not url_is_blocked:
123                    raise error.TestFail('The URL %s should have been blocked'
124                                         ' by policy, but was allowed.' % url)
125            elif url_is_blocked:
126                raise error.TestFail('The URL %s should have been allowed'
127                                     'by policy, but was blocked.' % url)
128
129
130    def run_once(self, case):
131        """Setup and run the test configured for the specified test case.
132
133        @param case: Name of the test case to run.
134        """
135        case_value = self.TEST_CASES[case]
136        self.SUPPORTING_POLICIES[self.POLICY_NAME] = case_value
137        self.setup_case(user_policies=self.SUPPORTING_POLICIES)
138        self._test_url_blacklist(case_value)
139