• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright 2015 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 common
6from autotest_lib.client.common_lib import error
7from autotest_lib.server import test
8
9
10class brillo_Invariants(test.test):
11    """Check for things that should look the same on all Brillo devices."""
12    version = 1
13
14
15    def assert_path_test(self, path, test, negative=False):
16        """Performs a test against a path.
17
18        See the man page for test(1) for valid tests (e.g. -e, -b, -d).
19
20        @param path: the path to check.
21        @param test: the test to perform, without leading dash.
22        @param negative: if True, test for the negative.
23        """
24        self.host.run('test %s -%s %s' % ('!' if negative else '', test, path))
25
26
27    def assert_selinux_context(self, path, ctx):
28        """Checks the selinux context of a path.
29
30        @param path: the path to check.
31        @param ctx: the selinux context to check for.
32
33        @raises error.TestFail
34        """
35        # Example output of 'ls -LZ /dev/block/by-name/misc' is:
36        # u:object_r:misc_block_device:s0 /dev/block/by-name/misc
37        tokens = self.host.run_output('ls -LZ %s' % path).split()
38        path_ctx = tokens[0]
39        if not ctx in path_ctx:
40            raise error.TestFail('Context "%s" for path "%s" does not '
41                                 'contain "%s"' % (path_ctx, path, ctx))
42
43
44    def check_fstab_name(self):
45        """Checks that the fstab file has the name /fstab.<ro.hardware>.
46        """
47        hardware = self.host.run_output('getprop ro.hardware')
48        self.assert_path_test('/fstab.%s' % hardware, 'e')
49        self.assert_path_test('/fstab.device', 'e', negative=True)
50
51
52    def check_block_devices(self):
53        """Checks required devices in /dev/block/by-name/ and their context.
54        """
55        ctx = 'boot_block_device'
56        self.assert_selinux_context('/dev/block/by-name/boot_a', ctx)
57        self.assert_selinux_context('/dev/block/by-name/boot_b', ctx)
58        ctx = 'system_block_device'
59        self.assert_selinux_context('/dev/block/by-name/system_a', ctx)
60        self.assert_selinux_context('/dev/block/by-name/system_b', ctx)
61        ctx = 'misc_block_device'
62        self.assert_selinux_context('/dev/block/by-name/misc', ctx)
63        self.assert_path_test('/dev/block/by-name/userdata', 'b')
64
65
66    def run_once(self, host=None):
67        """Check for things that should look the same on all Brillo devices.
68
69        @param dut: host object representing the device under test.
70        """
71        self.host = host
72        self.check_fstab_name()
73        self.check_block_devices()
74