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="6.1.26"
16        BR2_LINUX_KERNEL_USE_CUSTOM_CONFIG=y
17        BR2_LINUX_KERNEL_CUSTOM_CONFIG_FILE="board/qemu/x86_64/linux.config"
18        BR2_LINUX_KERNEL_NEEDS_HOST_LIBELF=y
19        BR2_PACKAGE_LIBSELINUX=y
20        BR2_PACKAGE_REFPOLICY=y
21        """
22
23    def wait_boot(self):
24        # The complete boot with systemd takes more time than what the
25        # default typically allows
26        self.emulator.login(timeout=600)
27
28    def run_tests(self, fstype):
29        kernel = os.path.join(self.builddir, "images", "bzImage")
30        rootfs = os.path.join(self.builddir, "images", "rootfs.{}".format(fstype))
31
32        self.emulator.boot(arch="x86_64", kernel=kernel,
33                           kernel_cmdline=["root=/dev/vda", "rootfstype={}".format(fstype),
34                                           "console=ttyS0", "security=selinux"],
35                           options=["-cpu", "Nehalem",
36                                    "-drive", "file={},if=virtio,format=raw".format(rootfs)])
37        self.wait_boot()
38
39        # Test the reported SELinux mode.
40        out, ret = self.emulator.run("getenforce")
41        self.assertEqual(ret, 0)
42        self.assertEqual(out[0], "Permissive")
43
44        # Check the extended arguments are correctly set.
45        out, ret = self.emulator.run("ls -dZ /")
46        self.assertEqual(ret, 0)
47        self.assertEqual(out[0].split()[0], "system_u:object_r:root_t")
48
49        # Check init's attributes.
50        out, ret = self.emulator.run("cat /proc/1/attr/current")
51        self.assertEqual(ret, 0)
52        self.assertEqual(out[0], "system_u:system_r:init_t\0")
53
54
55class TestSELinuxSystemdExt4(TestSELinuxSystemd):
56    config = TestSELinuxSystemd.config + \
57        """
58        BR2_TARGET_ROOTFS_EXT2=y
59        BR2_TARGET_ROOTFS_EXT2_4=y
60        BR2_TARGET_ROOTFS_EXT2_SIZE="100M"
61        """
62
63    def test_run(self):
64        self.run_tests("ext4")
65
66
67class TestSELinuxSystemdSquashfs(TestSELinuxSystemd):
68    config = TestSELinuxSystemd.config + \
69        """
70        BR2_TARGET_ROOTFS_SQUASHFS=y
71        BR2_LINUX_KERNEL_CONFIG_FRAGMENT_FILES="{}"
72        """.format(
73            infra.filepath("tests/init/test_systemd_selinux/linux-squashfs.fragment"),
74        )
75
76    def test_run(self):
77        self.run_tests("squashfs")
78