• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 os
6
7from autotest_lib.client.bin import utils
8from autotest_lib.client.common_lib import error
9from autotest_lib.client.cros.enterprise import enterprise_policy_base
10
11
12class policy_ExternalStorageReadOnly(
13        enterprise_policy_base.EnterprisePolicyTest):
14    version = 1
15
16    POLICY_NAME = 'ExternalStorageReadOnly'
17    TEST_CASES = {
18        'True_Block': True,
19        'False_Allow': False,
20        'NotSet_Allow': None
21    }
22
23    TEST_DIR = os.path.join(os.sep, 'media', 'removable', 'STATE')
24    TEST_FILE = os.path.join(TEST_DIR, '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    def _test_external_storage(self, policy_value):
37        """
38        Verify the behavior of the ExternalStorageReadOnly policy.
39
40        Attempt to create TEST_FILE on the external storage. This should fail
41        if the policy is set to True and succeed otherwise.
42
43        @param policy_value: policy value for this case.
44
45        @raises error.TestFail: If the permissions of the /media/removable
46            directory do not match the policy behavior.
47
48        """
49        # Attempt to modify the external storage by creating a file.
50        if not os.path.isdir(self.TEST_DIR):
51            raise error.TestWarn('USB Missing. Exiting')
52        if os.path.isfile(self.TEST_FILE):
53            raise error.TestWarn('Test file existed prior to test.')
54        utils.run('touch %s' % self.TEST_FILE, ignore_status=True)
55        if policy_value and os.path.isfile(self.TEST_FILE):
56            raise error.TestFail('External storage set to read-only but '
57                                 'was able to write to storage.')
58        elif not policy_value and not os.path.isfile(self.TEST_FILE):
59            raise error.TestFail('External storage not read-only but '
60                                 'unable to write to storage.')
61
62    def run_once(self, case):
63        """
64        Setup and run the test configured for the specified test case.
65
66        @param case: Name of the test case to run.
67
68        """
69        case_value = self.TEST_CASES[case]
70        self.setup_case(user_policies={self.POLICY_NAME: case_value})
71        self._test_external_storage(case_value)
72