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