blob: 52da4b28041bce049dfb64734ad6d0adf91be188 [file] [log] [blame]
# Copyright 2019 The Chromium OS Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
import logging
from autotest_lib.client.common_lib import error
from autotest_lib.client.cros.enterprise import enterprise_policy_base
class policy_NativePrintersBulkAccessMode(
"""Verify behavior of NativePrinters user policy."""
version = 1
def initialize(self, **kwargs):
def _initialize_test_constants(self):
"""Construct policy values as needed."""
PRINTERS_HASH = ('7a052c5e4f23c159668148df2a3c202bed4d65749cab5ecd0f'
'a7db211c12a3b8') #sha256
PRINTERS2_HASH = ('0d7344c989893cb97484d6111bf497a999333c177e1c991c06'
'db664c58e4b81a') #sha256
# These strings are printer ids, defined in PRINTERS_URL.
self.DEFINED_IDS = set(['wl', 'bl', 'other', 'both'])
# These strings are printer ids, defined in PRINTERS2_URL.
self.DEFINED_IDS2 = set(['wl2', 'bl2', 'other2', 'both2'])
# Whitelist and blacklist, common for both sets of printers
self.WHITELIST = ['both', 'both2', 'wl', 'wl2', 'otherwl']
self.BLACKLIST = ['both', 'both2', 'bl', 'bl2', 'otherbl']
self.user_policies = {
'NativePrintersBulkConfiguration': {'url': PRINTERS_URL,
'NativePrintersBulkWhitelist': self.WHITELIST,
'NativePrintersBulkBlacklist': self.BLACKLIST}
self.device_policies = {
'DeviceNativePrinters': {'url': PRINTERS2_URL,
'hash': PRINTERS2_HASH},
'DeviceNativePrintersWhitelist': self.WHITELIST,
'DeviceNativePrintersBlacklist': self.BLACKLIST}
self.USER_ACCESS_MODE_NAME = 'NativePrintersBulkAccessMode'
self.DEVICE_ACCESS_MODE_NAME = 'DeviceNativePrintersAccessMode'
self.ACCESS_MODE_VALUES = {'allowall': 2,
'whitelist': 1,
'blacklist': 0}
def _get_printer_ids(self):
Use autotest_private to read the ids of listed printers.
@returns: a set of ids of printers that would be seen by a user under
Print Destinations.
'window.__printers = null; '
'chrome.autotestPrivate.getPrinterList(function(printers) {'
' window.__printers = printers;'
'window.__printers !== null')
printers =
'window.__printers')'Printers found: %s', printers)
if not isinstance(printers, list):
raise error.TestFail('Received response is not a list!')
ID_KEY = u'printerId'
ids = set()
for printer in printers:
if ID_KEY in printer:
raise error.TestFail('Missing %s field!' % ID_KEY)'Found ids: %s', ids)
if len(ids) < len(printers):
raise error.TestFail('Received response contains duplicates!')
return ids
def _resultant_set_of_ids(self, access_mode, set_of_all_ids):
Calculates resultant set of printers identifiers depending on given
@param access_mode: one of strings: 'allowall', 'whitelist',
'blacklist' or None (policy not set).
@param set_of_all_ids: set of all printer identifiers.
@returns: a set of ids of printers.
if access_mode is None:
return set()
if access_mode == 'blacklist':
return set_of_all_ids - set(self.BLACKLIST)
if access_mode == 'whitelist':
return set_of_all_ids & set(self.WHITELIST)
if access_mode == 'allowall':
return set_of_all_ids
raise Exception("Incorrect value of access mode")
def _test_bulk_native_printers(self, case_device, case_user):
@param case_device: access mode set in device policies (one of strings:
'allowall', 'whitelist', 'blacklist') or None (policy not set).
@param case_user: access mode set in user policies (one of strings:
'allowall', 'whitelist', 'blacklist') or None (policy not set).
printers = self._get_printer_ids()
expected = self._resultant_set_of_ids(case_device, self.DEFINED_IDS2)
expected |= self._resultant_set_of_ids(case_user, self.DEFINED_IDS)
if printers != expected:
raise error.TestFail('Did not see expected printer output! '
'Expected %s, found %s.' % (expected,
def run_once(self, case):
Entry point of this test.
@param case: a tuple of two access modes, the first one is for device
policies, the second one is for user policies. Each access mode
equals one of the strings: 'allowall', 'whitelist', 'blacklist'
or is set to None (what means 'policy not set').
case_device, case_user = case
if case_device is None:
self.device_policies = {}
enroll = False
assert(case_device in self.ACCESS_MODE_VALUES)
self.device_policies[self.DEVICE_ACCESS_MODE_NAME] =\
enroll = True
if case_user is None:
self.user_policies = {}
assert(case_user in self.ACCESS_MODE_VALUES)
self.user_policies[self.USER_ACCESS_MODE_NAME] =\
user_policies=self.user_policies, enroll=enroll)
self._test_bulk_native_printers(case_device, case_user)