test_systemd_selinux.py 2.5 KB

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