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. 4 5import logging 6 7from autotest_lib.client.common_lib import error 8from autotest_lib.client.cros.enterprise import enterprise_policy_base 9 10 11class policy_NativePrintersBulkAccessMode( 12 enterprise_policy_base.EnterprisePolicyTest): 13 """Verify behavior of NativePrinters user policy.""" 14 version = 1 15 16 def initialize(self, **kwargs): 17 """Initialize.""" 18 self._initialize_test_constants() 19 self._initialize_enterprise_policy_test() 20 21 22 def _initialize_test_constants(self): 23 """Construct policy values as needed.""" 24 PRINTERS_URL = ('https://storage.googleapis.com/chromiumos-test-assets' 25 '-public/enterprise/printers.json') 26 PRINTERS_HASH = ('7a052c5e4f23c159668148df2a3c202bed4d65749cab5ecd0f' 27 'a7db211c12a3b8') #sha256 28 PRINTERS2_URL = ('https://storage.googleapis.com/chromiumos-test-assets' 29 '-public/enterprise/printers2.json') 30 PRINTERS2_HASH = ('0d7344c989893cb97484d6111bf497a999333c177e1c991c06' 31 'db664c58e4b81a') #sha256 32 # These strings are printer ids, defined in PRINTERS_URL. 33 self.DEFINED_IDS = set(['wl', 'bl', 'other', 'both']) 34 # These strings are printer ids, defined in PRINTERS2_URL. 35 self.DEFINED_IDS2 = set(['wl2', 'bl2', 'other2', 'both2']) 36 # Whitelist and blacklist, common for both sets of printers 37 self.WHITELIST = ['both', 'both2', 'wl', 'wl2', 'otherwl'] 38 self.BLACKLIST = ['both', 'both2', 'bl', 'bl2', 'otherbl'] 39 40 self.user_policies = { 41 'NativePrintersBulkConfiguration': {'url': PRINTERS_URL, 42 'hash': PRINTERS_HASH}, 43 'NativePrintersBulkWhitelist': self.WHITELIST, 44 'NativePrintersBulkBlacklist': self.BLACKLIST} 45 46 self.device_policies = { 47 'DevicePrinters': { 48 'url': PRINTERS2_URL, 49 'hash': PRINTERS2_HASH 50 }, 51 'DevicePrintersAllowlist': self.WHITELIST, 52 'DevicePrintersBlocklist': self.BLACKLIST 53 } 54 55 self.USER_ACCESS_MODE_NAME = 'NativePrintersBulkAccessMode' 56 self.DEVICE_ACCESS_MODE_NAME = 'DevicePrintersAccessMode' 57 self.ACCESS_MODE_VALUES = {'allowall': 2, 58 'whitelist': 1, 59 'blacklist': 0} 60 61 62 def _get_printer_ids(self): 63 """ 64 Use autotest_private to read the ids of listed printers. 65 66 @returns: a set of ids of printers that would be seen by a user under 67 Print Destinations. 68 69 """ 70 self.cr.autotest_ext.ExecuteJavaScript( 71 'window.__printers = null; ' 72 'chrome.autotestPrivate.getPrinterList(function(printers) {' 73 ' window.__printers = printers;' 74 '});') 75 self.cr.autotest_ext.WaitForJavaScriptCondition( 76 'window.__printers !== null') 77 printers = self.cr.autotest_ext.EvaluateJavaScript( 78 'window.__printers') 79 logging.info('Printers found: %s', printers) 80 81 if not isinstance(printers, list): 82 raise error.TestFail('Received response is not a list!') 83 84 ID_KEY = u'printerId' 85 ids = set() 86 for printer in printers: 87 if ID_KEY in printer: 88 ids.add(printer[ID_KEY].encode('ascii')) 89 else: 90 raise error.TestFail('Missing %s field!' % ID_KEY) 91 logging.info('Found ids: %s', ids) 92 93 if len(ids) < len(printers): 94 raise error.TestFail('Received response contains duplicates!') 95 96 return ids 97 98 99 def _resultant_set_of_ids(self, access_mode, set_of_all_ids): 100 """ 101 Calculates resultant set of printers identifiers depending on given 102 access_mode. 103 104 @param access_mode: one of strings: 'allowall', 'whitelist', 105 'blacklist' or None (policy not set). 106 @param set_of_all_ids: set of all printer identifiers. 107 108 @returns: a set of ids of printers. 109 110 """ 111 if access_mode is None: 112 return set() 113 if access_mode == 'blacklist': 114 return set_of_all_ids - set(self.BLACKLIST) 115 if access_mode == 'whitelist': 116 return set_of_all_ids & set(self.WHITELIST) 117 if access_mode == 'allowall': 118 return set_of_all_ids 119 raise Exception("Incorrect value of access mode") 120 121 122 def _test_bulk_native_printers(self, case_device, case_user): 123 """ 124 125 @param case_device: access mode set in device policies (one of strings: 126 'allowall', 'whitelist', 'blacklist') or None (policy not set). 127 @param case_user: access mode set in user policies (one of strings: 128 'allowall', 'whitelist', 'blacklist') or None (policy not set). 129 130 """ 131 printers = self._get_printer_ids() 132 expected = self._resultant_set_of_ids(case_device, self.DEFINED_IDS2) 133 expected |= self._resultant_set_of_ids(case_user, self.DEFINED_IDS) 134 if printers != expected: 135 raise error.TestFail('Did not see expected printer output! ' 136 'Expected %s, found %s.' % (expected, 137 printers)) 138 139 140 def run_once(self, case): 141 """ 142 Entry point of this test. 143 144 @param case: a tuple of two access modes, the first one is for device 145 policies, the second one is for user policies. Each access mode 146 equals one of the strings: 'allowall', 'whitelist', 'blacklist' 147 or is set to None (what means 'policy not set'). 148 149 """ 150 case_device, case_user = case 151 152 if case_device is None: 153 self.device_policies = {} 154 enroll = False 155 else: 156 assert(case_device in self.ACCESS_MODE_VALUES) 157 self.device_policies[self.DEVICE_ACCESS_MODE_NAME] =\ 158 self.ACCESS_MODE_VALUES[case_device] 159 enroll = True 160 161 if case_user is None: 162 self.user_policies = {} 163 else: 164 assert(case_user in self.ACCESS_MODE_VALUES) 165 self.user_policies[self.USER_ACCESS_MODE_NAME] =\ 166 self.ACCESS_MODE_VALUES[case_user] 167 168 self.setup_case(device_policies=self.device_policies, 169 user_policies=self.user_policies, enroll=enroll) 170 self._test_bulk_native_printers(case_device, case_user) 171