1# Copyright 2018 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 6import os 7 8from autotest_lib.client.bin import utils 9from autotest_lib.client.common_lib import error 10from autotest_lib.client.cros.enterprise import enterprise_policy_base 11 12 13class policy_ExternalStorageReadOnly( 14 enterprise_policy_base.EnterprisePolicyTest): 15 version = 1 16 17 POLICY_NAME = 'ExternalStorageReadOnly' 18 TEST_CASES = { 19 'True_Block': True, 20 'False_Allow': False, 21 'NotSet_Allow': None 22 } 23 24 TEST_FILE = os.path.join(os.sep, 'media', 'removable', 'STATE', 'test') 25 26 def cleanup(self): 27 """Delete the test file, if it was created.""" 28 try: 29 os.remove(self.TEST_FILE) 30 except OSError: 31 # The remove call fails if the file isn't created, but that's ok. 32 pass 33 34 super(policy_ExternalStorageReadOnly, self).cleanup() 35 36 37 def _test_external_storage(self, policy_value): 38 """ 39 Verify the behavior of the ExternalStorageReadOnly policy. 40 41 Attempt to create TEST_FILE on the external storage. This should fail 42 if the policy is set to True and succeed otherwise. 43 44 @param policy_value: policy value for this case. 45 46 @raises error.TestFail: If the permissions of the /media/removable 47 directory do not match the policy behavior. 48 49 """ 50 # Attempt to modify the external storage by creating a file. 51 try: 52 return_code = utils.run('touch %s' % self.TEST_FILE) 53 except error.CmdError: 54 if not policy_value: 55 raise error.TestFail('External storage not readonly but ' 56 'unable to write to storage') 57 else: 58 if policy_value: 59 raise error.TestFail('External storage was readonly but ' 60 'external storage was modified') 61 62 63 def run_once(self, case): 64 """ 65 Setup and run the test configured for the specified test case. 66 67 @param case: Name of the test case to run. 68 69 """ 70 case_value = self.TEST_CASES[case] 71 self.setup_case(user_policies={self.POLICY_NAME: case_value}) 72 self._test_external_storage(case_value) 73