1# Copyright 2016 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
6import utils
7
8from autotest_lib.client.common_lib import error
9from autotest_lib.client.cros.enterprise import enterprise_policy_base
10from autotest_lib.client.cros.audio import audio_helper
11
12
13class policy_PluginsBlockedForUrls(
14        enterprise_policy_base.EnterprisePolicyTest):
15    """Test PluginsBlockedForUrls policy effect on CrOS behavior.
16
17    This test verifies the behavior of Chrome OS with a set of valid values
18    for the PluginsBlockedForUrls user policy, when DefaultPluginsSetting=1
19    (i.e., allow running of plugins by default, except on sites listed in
20    PluginsBlockedForUrls). These valid values are covered by 3 test cases:
21    SiteBlocked_Block, SiteNotBlocked_Run, and NotSet_Run.
22
23    This test is also configured with DisablePluginFinder=True and
24    AllowOutdatedPlugins=False.
25
26    When the policy value is None (as in case NotSet_Run), then running of
27    plugins is allowed on every site. When the value is set to one or more
28    URLs (as in SiteBlocked_Block and SiteNotBlocked_Run), plugins are run
29    on every site except for those sites whose domain matches any of the
30    listed URLs.
31
32    A related test, policy_PluginsBlockedForUrls, has DefaultPluginsSetting=2
33    (i.e., block running of plugins by default, except on sites in domains
34    listed in PluginsAllowedForUrls).
35    """
36    version = 1
37
38    def initialize(self, **kwargs):
39        """Initialize this test."""
40        self._initialize_test_constants()
41        super(policy_PluginsBlockedForUrls, self).initialize(**kwargs)
42        self.start_webserver()
43
44
45    def _initialize_test_constants(self):
46        """Initialize test-specific constants, some from class constants."""
47        self.POLICY_NAME = 'PluginsBlockedForUrls'
48        self.TEST_FILE = 'plugin_status.html'
49        self.TEST_URL = '%s/%s' % (self.WEB_HOST, self.TEST_FILE)
50        self.INCLUDES_BLOCKED_URL = ['http://www.bing.com', self.WEB_HOST,
51                                     'https://www.yahoo.com']
52        self.EXCLUDES_BLOCKED_URL = ['http://www.bing.com',
53                                     'https://www.irs.gov/',
54                                     'https://www.yahoo.com']
55        self.TEST_CASES = {
56            'SiteBlocked_Block': self.INCLUDES_BLOCKED_URL,
57            'SiteNotBlocked_Run': self.EXCLUDES_BLOCKED_URL,
58            'NotSet_Run': None
59        }
60        self.STARTUP_URLS = ['chrome://policy', 'chrome://settings']
61        self.SUPPORTING_POLICIES = {
62            'DefaultPluginsSetting': 1,
63            'DisablePluginFinder': True,
64            'AllowOutdatedPlugins': False,
65            'AlwaysAuthorizePlugins': False,
66            'BookmarkBarEnabled': True,
67            'EditBookmarksEnabled': True,
68            'RestoreOnStartupURLs': self.STARTUP_URLS,
69            'RestoreOnStartup': 4
70        }
71
72
73    def _wait_for_page_ready(self, tab):
74        """Wait for JavaScript on page in |tab| to set the pageReady flag.
75
76        @param tab: browser tab with page to load.
77        """
78        utils.poll_for_condition(
79            lambda: tab.EvaluateJavaScript('pageReady'),
80            exception=error.TestError('Test page is not ready.'))
81
82
83    def _stop_flash_if_running(self, timeout_sec=10):
84        """Terminate all Shockwave Flash processes.
85
86        @param timeout_sec: maximum seconds to wait for processes to die.
87        @raises: error.AutoservPidAlreadyDeadError if Flash process is dead.
88        @raises: utils.TimeoutError if Flash processes are still running
89                 after timeout_sec.
90        """
91        def kill_flash_process():
92            """Kill all running flash processes."""
93            pids = utils.get_process_list('chrome', '--type=ppapi')
94            for pid in pids:
95                try:
96                    utils.nuke_pid(int(pid))
97                except error.AutoservPidAlreadyDeadError:
98                    pass
99            return pids
100
101        utils.poll_for_condition(lambda: kill_flash_process() == [],
102                                 timeout=timeout_sec)
103
104
105    def _is_flash_running(self):
106        """Check if a Shockwave Flash process is running.
107
108        @returns: True if one or more flash processes are running.
109        """
110        flash_pids = utils.get_process_list('chrome', '--type=ppapi')
111        return flash_pids != []
112
113
114    def _test_plugins_blocked_for_urls(self, policy_value):
115        """Verify CrOS enforces the PluginsBlockedForUrls policy.
116
117        When PluginsBlockedForUrls is undefined, plugins shall be run on
118        all pages. When PluginsBlockedForUrls contains one or more URLs,
119        plugins shall be run on all pages except those whose domain matches
120        any of the listed URLs.
121
122        @param policy_value: policy value expected.
123        """
124        # Set a low audio volume to avoid annoying people during tests.
125        audio_helper.set_volume_levels(10, 100)
126
127        # Kill any running Shockwave Flash processes.
128        self._stop_flash_if_running()
129
130        # Open page with an embedded flash file.
131        tab = self.navigate_to_url(self.TEST_URL)
132        self._wait_for_page_ready(tab)
133
134        # Check if Shockwave Flash process is running.
135        plugin_is_running = self._is_flash_running()
136        logging.info('plugin_is_running: %r', plugin_is_running)
137
138        # String |WEB_HOST| will be found in string |policy_value| for
139        # cases that expect the plugin to be run.
140        if policy_value is not None and self.WEB_HOST in policy_value:
141            if plugin_is_running:
142                raise error.TestFail('Plugins should not run.')
143        else:
144            if not plugin_is_running:
145                raise error.TestFail('Plugins should run.')
146        tab.Close()
147
148
149    def run_once(self, case):
150        """Setup and run the test configured for the specified test case.
151
152        @param case: Name of the test case to run.
153        """
154        case_value = self.TEST_CASES[case]
155        self.SUPPORTING_POLICIES[self.POLICY_NAME] = case_value
156        self.setup_case(user_policies=self.SUPPORTING_POLICIES)
157        self._test_plugins_blocked_for_urls(case_value)
158