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 5 import time 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 11 12 class policy_JavaScriptBlockedForUrls( 13 enterprise_policy_base.EnterprisePolicyTest): 14 """Test JavaScriptBlockedForUrls policy effect on CrOS look & feel. 15 16 This test verifies the behavior of Chrome OS with a range of valid values 17 for the JavaScriptBlockedForUrls user policy, covered by four named test 18 cases: NotSet_Allow, SingleUrl_Block, MultipleUrls_Allow, and 19 MultipleUrls_Block. 20 21 When the policy value is None (as in test case=NotSet_Allow), then 22 JavaScript execution be allowed on any page. When the policy value is set 23 to a single URL pattern (as in test case=SingleUrl_Block), then 24 JavaScript execution will be blocked on any page that matches that 25 pattern. When set to multiple URL patterns (as case=MultipleUrls_Allow 26 and MultipleUrls_Block) then JavaScript execution will be blocked on any 27 page with an URL that matches any of the listed patterns. 28 29 Two test cases (NotSet_Allow, MultipleUrls_Allow) are designed to allow 30 JavaScript execution the test page. The other two test cases 31 (NotSet_Allow, MultipleUrls_Block) are designed to block JavaScript 32 execution on the test page. 33 34 Note this test has a dependency on the DefaultJavaScriptSetting user 35 policy, which is tested partially herein and in the test 36 policy_JavaScriptAllowedForUrls. For this test, we set 37 DefaultJavaScriptSetting=1. This allows JavaScript execution on all pages 38 except those with a URL matching a pattern in JavaScriptBlockedForUrls. 39 In the test policy_JavaScriptAllowedForUrls, we set 40 DefaultJavaScriptSetting=2. That test blocks JavaScript execution on all 41 pages except those with an URL matching a pattern in 42 JavaScriptAllowedForUrls. 43 44 """ 45 version = 1 46 47 def initialize(self, **kwargs): 48 self._initialize_test_constants() 49 super(policy_JavaScriptBlockedForUrls, self).initialize(**kwargs) 50 self.start_webserver() 51 52 53 def _initialize_test_constants(self): 54 """Initialize test-specific constants, some from class constants.""" 55 self.POLICY_NAME = 'JavaScriptBlockedForUrls' 56 self.TEST_FILE = 'js_test.html' 57 self.TEST_URL = '%s/%s' % (self.WEB_HOST, self.TEST_FILE) 58 self.TEST_CASES = { 59 'NotSet_Allow': None, 60 'SingleUrl_Block': [self.WEB_HOST], 61 'MultipleUrls_Allow': ['http://www.bing.com', 62 'https://www.yahoo.com'], 63 'MultipleUrls_Block': ['http://www.bing.com', 64 self.TEST_URL, 65 'https://www.yahoo.com'] 66 } 67 68 self.STARTUP_URLS = ['chrome://policy', 'chrome://settings'] 69 self.SUPPORTING_POLICIES = { 70 'DefaultJavaScriptSetting': 1, 71 'BookmarkBarEnabled': False, 72 'RestoreOnStartupURLs': self.STARTUP_URLS, 73 'RestoreOnStartup': 4 74 } 75 76 77 def _can_execute_javascript(self, tab): 78 """Determine whether JavaScript is allowed to run on the given page. 79 80 @param tab: browser tab containing JavaScript to run. 81 82 """ 83 try: 84 utils.poll_for_condition( 85 lambda: tab.EvaluateJavaScript('jsAllowed', timeout=2), 86 exception=error.TestError('Test page is not ready.')) 87 return True 88 except: 89 return False 90 91 92 def _test_javascript_blocked_for_urls(self, policy_value): 93 """Verify CrOS enforces the JavaScriptBlockedForUrls policy. 94 95 When JavaScriptBlockedForUrls is undefined, JavaScript execution shall 96 be allowed on all pages. When JavaScriptBlockedForUrls contains one or 97 more URL patterns, JavaScript execution shall be blocked only on the 98 pages whose URL matches any of the listed patterns. 99 100 Note: This test does not use self.navigate_to_url(), because it can 101 not depend on methods that evaluate or execute JavaScript. 102 103 @param policy_value: policy value for this case. 104 105 """ 106 tab = self.cr.browser.tabs.New() 107 tab.Activate() 108 tab.Navigate(self.TEST_URL) 109 time.sleep(1) 110 111 utils.poll_for_condition( 112 lambda: tab.url == self.TEST_URL, 113 exception=error.TestError('Test page is not ready.')) 114 javascript_is_allowed = self._can_execute_javascript(tab) 115 116 if policy_value is not None and (self.WEB_HOST in policy_value or 117 self.TEST_URL in policy_value): 118 # If |WEB_HOST| is in |policy_value|, then JavaScript execution 119 # should be blocked. If execution is allowed, raise an error. 120 if javascript_is_allowed: 121 raise error.TestFail('JavaScript should be blocked.') 122 else: 123 if not javascript_is_allowed: 124 raise error.TestFail('JavaScript should be allowed.') 125 tab.Close() 126 127 128 def run_test_case(self, case): 129 """Setup and run the test configured for the specified test case. 130 131 @param case: Name of the test case to run. 132 133 """ 134 case_value = self.TEST_CASES[case] 135 self.setup_case(self.POLICY_NAME, case_value, self.SUPPORTING_POLICIES) 136 self._test_javascript_blocked_for_urls(case_value) 137