1 # Copyright 2017 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 glob 6 import logging 7 import os 8 import utils 9 10 from autotest_lib.client.common_lib import error 11 from autotest_lib.client.cros.enterprise import enterprise_policy_base 12 from autotest_lib.client.cros.input_playback import input_playback 13 14 POLL_TIMEOUT = 5 15 POLL_FREQUENCY = 0.5 16 17 18 class policy_DisableScreenshots( 19 enterprise_policy_base.EnterprisePolicyTest): 20 """ 21 Test DisableScreenshots policy effect on ChromerOS behavior. 22 23 This test verifies the behavior of Chrome OS with a set of valid values 24 for the DisableScreenshots user policy ie, the policy value is set to True, 25 False or is Unset. 26 These valid values are covered by the test cases: DisableScreenshot_Block, 27 NotSet_Allow and False_Allow. 28 29 When the policy value is None or is set to False (as in the cases 30 NotSet_Allow and False_Allow), then screenshots will be captured on pressing 31 the Ctrl and F5 keyboard buttons. When the value is set to True (as in case 32 DisableScreenshot_Block), screenshot capture is disabled. 33 34 """ 35 version = 1 36 37 def initialize(self, **kwargs): 38 """Emulate a keyboard in order to play back the screenshot shortcut.""" 39 self._initialize_test_constants() 40 super(policy_DisableScreenshots, self).initialize(**kwargs) 41 self.player = input_playback.InputPlayback() 42 self.player.emulate(input_type='keyboard') 43 self.player.find_connected_inputs() 44 45 46 def _initialize_test_constants(self): 47 """Initialize test-specific constants, some from class constants.""" 48 self.POLICY_NAME = 'DisableScreenshots' 49 self._DOWNLOADS = '/home/chronos/user/Downloads/' 50 self._SCREENSHOT_PATTERN = 'Screenshot*' 51 self._SCREENSHOT_FILENAME = self._DOWNLOADS + self._SCREENSHOT_PATTERN 52 53 self.TEST_CASES = { 54 'DisableScreenshot_Block': True, 55 'False_Allow': False, 56 'NotSet_Allow': None 57 } 58 self.STARTUP_URLS = ['chrome://policy', 'chrome://settings'] 59 self.SUPPORTING_POLICIES = { 60 'RestoreOnStartupURLs': self.STARTUP_URLS, 61 'RestoreOnStartup': 4 62 } 63 64 65 def _capture_screenshot(self): 66 """Capture a screenshot by pressing Ctrl +F5.""" 67 self.player.blocking_playback_of_default_file( 68 input_type='keyboard', filename='keyboard_ctrl+f5') 69 70 71 def _screenshot_file_exists(self): 72 """ 73 Return True if screenshot was captured. else returns False. 74 75 @returns boolean indicating if screenshot file was saved or not. 76 77 """ 78 try: 79 utils.poll_for_condition( 80 lambda: len(glob.glob(self._SCREENSHOT_FILENAME)) > 0, 81 timeout=POLL_TIMEOUT, 82 sleep_interval=POLL_FREQUENCY) 83 except utils.TimeoutError: 84 logging.info('Screenshot file not found.') 85 return False 86 87 logging.info('Screenshot file found.') 88 return True 89 90 91 def _delete_screenshot_files(self): 92 """Delete existing screenshot files, if any.""" 93 for filename in glob.glob(self._SCREENSHOT_FILENAME): 94 os.remove(filename) 95 96 97 def cleanup(self): 98 """Cleanup files created in this test, if any and close the player.""" 99 self._delete_screenshot_files() 100 self.player.close() 101 super(policy_DisableScreenshots, self).cleanup() 102 103 104 def _test_screenshot_disabled(self, policy_value): 105 """ 106 Verify CrOS enforces the DisableScreenshots policy. 107 108 When DisableScreenshots policy value is undefined, screenshots shall 109 be captured via the keyboard shortcut Ctrl + F5. 110 When DisableScreenshots policy is set to True screenshots shall not 111 be captured. 112 113 @param policy_value: policy value for this case. 114 115 """ 116 logging.info('Deleting preexisting Screenshot files.') 117 self._delete_screenshot_files() 118 119 self._capture_screenshot() 120 screenshot_file_captured = self._screenshot_file_exists() 121 if policy_value: 122 if screenshot_file_captured: 123 raise error.TestFail('Screenshot should not be captured') 124 elif not screenshot_file_captured: 125 raise error.TestFail('Screenshot should be captured') 126 127 128 def run_once(self, case): 129 """ 130 Setup and run the test configured for the specified test case. 131 132 @param case: Name of the test case to run. 133 134 """ 135 case_value = self.TEST_CASES[case] 136 self.SUPPORTING_POLICIES[self.POLICY_NAME] = case_value 137 self.setup_case(user_policies=self.SUPPORTING_POLICIES) 138 self._test_screenshot_disabled(case_value) 139