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