test_polkit.py 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. import os
  2. import infra.basetest
  3. class TestPolkitInfra(infra.basetest.BRTest):
  4. br2_external = [infra.filepath("tests/package/br2-external/polkit")]
  5. config = \
  6. """
  7. BR2_arm=y
  8. BR2_cortex_a9=y
  9. BR2_ARM_ENABLE_VFP=y
  10. BR2_TOOLCHAIN_EXTERNAL=y
  11. BR2_TOOLCHAIN_EXTERNAL_BOOTLIN=y
  12. BR2_TARGET_ROOTFS_CPIO=y
  13. BR2_PACKAGE_POLKIT=y
  14. BR2_PACKAGE_POLKIT_RULES_TEST=y
  15. """
  16. def base_test_run(self):
  17. cpio_file = os.path.join(self.builddir, "images", "rootfs.cpio")
  18. self.emulator.boot(arch="armv7", kernel="builtin",
  19. options=["-initrd", cpio_file])
  20. self.emulator.login()
  21. class TestPolkitSystemd(TestPolkitInfra):
  22. config = \
  23. """
  24. {}
  25. BR2_INIT_SYSTEMD=y
  26. BR2_PACKAGE_SYSTEMD_POLKIT=y
  27. BR2_TARGET_GENERIC_GETTY_PORT="ttyAMA0"
  28. # BR2_TARGET_ROOTFS_TAR is not set
  29. """.format(TestPolkitInfra.config)
  30. def test_run(self):
  31. TestPolkitInfra.base_test_run(self)
  32. cmd = "su brtest -c '/bin/systemctl restart systemd-timesyncd.service'"
  33. _, exit_code = self.emulator.run(cmd, 10)
  34. self.assertEqual(exit_code, 1)
  35. cmd = "mv /root/systemd-timesyncd-restart.rules /etc/polkit-1/rules.d"
  36. _, exit_code = self.emulator.run(cmd, 10)
  37. self.assertEqual(exit_code, 0)
  38. cmd = "su brtest -c '/bin/systemctl restart systemd-timesyncd.service'"
  39. _, exit_code = self.emulator.run(cmd, 10)
  40. self.assertEqual(exit_code, 0)
  41. class TestPolkitInitd(TestPolkitInfra):
  42. config = TestPolkitInfra.config
  43. def test_run(self):
  44. TestPolkitInfra.base_test_run(self)
  45. cmd = "su brtest -c 'pkexec hello-polkit'"
  46. output, exit_code = self.emulator.run(cmd, 10)
  47. self.assertEqual(exit_code, 127)
  48. self.assertEqual(output[0], "Error executing command as another user: Not authorized")
  49. cmd = "mv /root/hello-polkit.rules /etc/polkit-1/rules.d/hello-polkit.rules"
  50. _, exit_code = self.emulator.run(cmd, 10)
  51. self.assertEqual(exit_code, 0)
  52. cmd = "su brtest -c 'pkexec hello-polkit'"
  53. output, exit_code = self.emulator.run(cmd, 10)
  54. self.assertEqual(exit_code, 0)
  55. self.assertEqual(output[0], "Hello polkit!")