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