1# Copyright 2019 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. 4import utils 5 6from autotest_lib.client.common_lib import error 7from autotest_lib.client.cros.enterprise import enterprise_policy_base 8 9 10class policy_KeyPermissions( 11 enterprise_policy_base.EnterprisePolicyTest): 12 """ 13 Test effect of policy_KeyPermissions/allowCorporateKeyUsage policy. 14 15 This test will utilize the certs api extension to prep the certificate 16 and see if when the cert is 'selected' if the Certficate information GUI 17 object appears. 18 19 The wait_for_ui_obj and did_obj_not_load functions within self.ui will 20 raise testErrors if: 21 The object did not load in the wait_for_ui_obj() call. 22 The object did load in the did_obj_not_load() call. 23 24 """ 25 version = 1 26 QUERY = "document.querySelector('#{}').{}" 27 28 def click_button(self, button_id): 29 """Click an element given its ID.""" 30 cmd = self.QUERY.format(button_id, 'click()') 31 self.tab.ExecuteJavaScript(cmd) 32 33 def field_value(self, obj_id): 34 """Return the value of a text field.""" 35 cmd = self.QUERY.format(obj_id, 'value') 36 return self.tab.EvaluateJavaScript(cmd) 37 38 def wait_for_extension(self): 39 """Wait for the extension to install so we can open it.""" 40 def load_page(): 41 self.tab = self.navigate_to_url(self.EXTENSION_PAGE, self.tab) 42 return self.tab.EvaluateJavaScript( 43 "document.querySelector('#cert-enrollment') !== null") 44 45 utils.poll_for_condition( 46 load_page, 47 timeout=15, 48 sleep_interval=1, 49 desc='Timed out waiting for extension to install.') 50 51 def test_platform_keys(self, case): 52 """ 53 Test the chrome.enterprise.platformKeys API. 54 55 The following API methods are tested: 56 - getToken 57 - getCertificates 58 - importCertificate 59 - removeCertificate 60 61 """ 62 try: 63 self._click_generate() 64 except error.TestFail: 65 # On specific devices the very first time the extension page is 66 # loaded, it will not load the certs. This is a small workaround to 67 # fix that. 68 self.navigate_to_url('https://google.com', self.tab) 69 self._click_generate() 70 71 # Click all the buttons needed to get the cert ready. 72 self.click_button('sign') 73 self.click_button('create-cert') 74 self.click_button('import-cert') 75 self.click_button('list-certs') 76 self.click_button('select-certs') 77 78 # Test if the cert was allowed. 79 if case: 80 self.ui.wait_for_ui_obj('Certificate information') 81 else: 82 self.ui.did_obj_not_load('Certificate information') 83 84 def _click_generate(self): 85 self.wait_for_extension() 86 87 self.click_button('generate') 88 error_id = 'generate-error' 89 90 utils.poll_for_condition( 91 lambda: 'OK' in self.field_value(error_id), 92 timeout=45, 93 exception=error.TestFail( 94 'API error: %s' % self.field_value(error_id))) 95 96 def run_once(self, case=None): 97 """Setup and run the test configured for the specified test case.""" 98 99 EXTENSION_ID = 'hoppbgdeajkagempifacalpdapphfoai' 100 self.EXTENSION_PAGE = ('chrome-extension://%s/main.html' 101 % EXTENSION_ID) 102 self.tab = None 103 104 self.setup_case( 105 disable_default_apps=False, 106 user_policies={ 107 'ExtensionInstallForcelist': [EXTENSION_ID], 108 'DeveloperToolsAvailability': 1, 109 'KeyPermissions': 110 {'hoppbgdeajkagempifacalpdapphfoai': 111 {'allowCorporateKeyUsage': case}}}) 112 self.ui.start_ui_root(self.cr) 113 self.test_platform_keys(case) 114