test_systemd_selinux.py 2.5 KB

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