test_acl.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. import os
  2. import infra.basetest
  3. class TestAcl(infra.basetest.BRTest):
  4. # Note: this test requires a Kernel with a filesystem on /tmp
  5. # supporting ACLs. This is the case for the basetest reference
  6. # config. Kernel has CONFIG_TMPFS_POSIX_ACL=y, and /tmp is tmpfs
  7. # in the default Buildroot config.
  8. config = infra.basetest.BASIC_TOOLCHAIN_CONFIG + \
  9. """
  10. BR2_PACKAGE_ACL=y
  11. BR2_TARGET_ROOTFS_CPIO=y
  12. # BR2_TARGET_ROOTFS_TAR is not set
  13. """
  14. def test_run(self):
  15. cpio_file = os.path.join(self.builddir, "images", "rootfs.cpio")
  16. self.emulator.boot(arch="armv5",
  17. kernel="builtin",
  18. options=["-initrd", cpio_file])
  19. self.emulator.login()
  20. # Check the programs can execute.
  21. self.assertRunOk("getfacl --version")
  22. self.assertRunOk("setfacl --version")
  23. # Constants used in this test.
  24. test_user = "acltest"
  25. test_data = "Hello Buildroot!"
  26. test_file = "/tmp/file.txt"
  27. # Create a test user:
  28. # -D don't set a password
  29. # -h set home directory
  30. # -H don't create home directory
  31. # -s set shell to /bin/sh
  32. self.assertRunOk(f"adduser -D -h /tmp -H -s /bin/sh {test_user}")
  33. # Create a test file, and make sure the owner is "root" with
  34. # standard Unix permissions to read/write only for the owner.
  35. self.assertRunOk(f"echo '{test_data}' > {test_file}")
  36. self.assertRunOk(f"chown root:root {test_file}")
  37. self.assertRunOk(f"chmod 0600 {test_file}")
  38. # Check we have no ACL for the test user.
  39. getacl_cmd = f"getfacl -c -p {test_file}"
  40. out, ret = self.emulator.run(getacl_cmd)
  41. self.assertEqual(ret, 0)
  42. self.assertNotIn(f"user:{test_user}:", "\n".join(out))
  43. # Reading the file as the test user is expected to fail.
  44. test_read_cmd = f"su - {test_user} -c 'cat {test_file}'"
  45. _, ret = self.emulator.run(test_read_cmd)
  46. self.assertNotEqual(ret, 0)
  47. # We add a special read ACL for the test user.
  48. cmd = f"setfacl -m u:{test_user}:r {test_file}"
  49. self.assertRunOk(cmd)
  50. # Check we now have an ACL entry for the test user.
  51. out, ret = self.emulator.run(getacl_cmd)
  52. self.assertEqual(ret, 0)
  53. self.assertIn(f"user:{test_user}:", "\n".join(out))
  54. # Reading the file as the test user is now expected to
  55. # succeed.
  56. out, ret = self.emulator.run(test_read_cmd)
  57. self.assertEqual(ret, 0)
  58. self.assertEqual(out[0], test_data)
  59. # Attempting to write to the file as the test user is expected
  60. # to fail (since we put an ACL only for reading).
  61. cmd = f"su - {test_user} -c 'echo WriteTest > {test_file}'"
  62. _, ret = self.emulator.run(cmd)
  63. self.assertNotEqual(ret, 0)
  64. # Remove all ACLs. This could have been done with the command
  65. # "setfacl -b". Instead, we use the "chacl -B" command which
  66. # is doing the same. The reason is to slightly improve the
  67. # coverage of this test, by including an execution of "chacl".
  68. self.assertRunOk(f"chacl -B {test_file}")
  69. # Reading the file as the test user is expected to fail again.
  70. _, ret = self.emulator.run(test_read_cmd)
  71. self.assertNotEqual(ret, 0)