xref: /OK3568_Linux_fs/buildroot/support/testing/tests/init/test_systemd_selinux.py (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1import os
2
3import infra.basetest
4
5
6class TestSELinuxSystemd(infra.basetest.BRTest):
7    config = \
8        """
9        BR2_x86_64=y
10        BR2_x86_corei7=y
11        BR2_TOOLCHAIN_EXTERNAL=y
12        BR2_INIT_SYSTEMD=y
13        BR2_LINUX_KERNEL=y
14        BR2_LINUX_KERNEL_CUSTOM_VERSION=y
15        BR2_LINUX_KERNEL_CUSTOM_VERSION_VALUE="5.8.12"
16        BR2_LINUX_KERNEL_USE_CUSTOM_CONFIG=y
17        BR2_LINUX_KERNEL_CUSTOM_CONFIG_FILE="board/qemu/x86_64/linux.config"
18        BR2_PACKAGE_LIBSELINUX=y
19        BR2_PACKAGE_REFPOLICY=y
20        """
21
22    def wait_boot(self):
23        # The complete boot with systemd takes more time than what the default multipler permits
24        self.emulator.timeout_multiplier *= 10
25        self.emulator.login()
26
27    def run_tests(self, fstype):
28        kernel = os.path.join(self.builddir, "images", "bzImage")
29        rootfs = os.path.join(self.builddir, "images", "rootfs.{}".format(fstype))
30
31        self.emulator.boot(arch="x86_64", kernel=kernel,
32                           kernel_cmdline=["root=/dev/vda", "rootfstype={}".format(fstype),
33                                           "console=ttyS0", "security=selinux"],
34                           options=["-cpu", "Nehalem",
35                                    "-drive", "file={},if=virtio,format=raw".format(rootfs)])
36        self.wait_boot()
37
38        # Test the reported SELinux mode.
39        out, ret = self.emulator.run("getenforce")
40        self.assertEqual(ret, 0)
41        self.assertEqual(out[0], "Permissive")
42
43        # Check the extended arguments are correctly set.
44        out, ret = self.emulator.run("ls -dZ /")
45        self.assertEqual(ret, 0)
46        self.assertEqual(out[0].split()[0], "system_u:object_r:root_t")
47
48        # Check init's attributes.
49        out, ret = self.emulator.run("cat /proc/1/attr/current")
50        self.assertEqual(ret, 0)
51        self.assertEqual(out[0], "system_u:system_r:init_t\0")
52
53
54class TestSELinuxSystemdExt4(TestSELinuxSystemd):
55    config = TestSELinuxSystemd.config + \
56        """
57        BR2_TARGET_ROOTFS_EXT2=y
58        BR2_TARGET_ROOTFS_EXT2_4=y
59        BR2_TARGET_ROOTFS_EXT2_SIZE="100M"
60        """
61
62    def test_run(self):
63        self.run_tests("ext4")
64
65
66class TestSELinuxSystemdSquashfs(TestSELinuxSystemd):
67    config = TestSELinuxSystemd.config + \
68        """
69        BR2_TARGET_ROOTFS_SQUASHFS=y
70        BR2_LINUX_KERNEL_CONFIG_FRAGMENT_FILES="{}"
71        """.format(
72            infra.filepath("tests/init/test_systemd_selinux/linux-squashfs.fragment"),
73        )
74
75    def test_run(self):
76        self.run_tests("squashfs")
77