test_polkit.py 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. rule_paths = [
  17. "/etc/polkit-1/rules.d",
  18. "/usr/share/polkit-1/rules.d"
  19. ]
  20. def base_test_run(self):
  21. cpio_file = os.path.join(self.builddir, "images", "rootfs.cpio")
  22. self.emulator.boot(arch="armv7", kernel="builtin",
  23. options=["-initrd", cpio_file])
  24. self.emulator.login()
  25. class TestPolkitSystemd(TestPolkitInfra):
  26. config = \
  27. """
  28. {}
  29. BR2_INIT_SYSTEMD=y
  30. BR2_PACKAGE_SYSTEMD_POLKIT=y
  31. BR2_TARGET_GENERIC_GETTY_PORT="ttyAMA0"
  32. # BR2_TARGET_ROOTFS_TAR is not set
  33. """.format(TestPolkitInfra.config)
  34. def test_run(self):
  35. TestPolkitInfra.base_test_run(self)
  36. rule_file = "systemd-timesyncd-restart.rules"
  37. for rule_path in TestPolkitInfra.rule_paths:
  38. cmd = "su brtest -c '/bin/systemctl restart systemd-timesyncd.service'"
  39. _, exit_code = self.emulator.run(cmd, 10)
  40. self.assertNotEqual(exit_code, 0)
  41. cmd = "cp /root/{file} {path}".format(file=rule_file, path=rule_path)
  42. _, exit_code = self.emulator.run(cmd, 10)
  43. self.assertEqual(exit_code, 0)
  44. cmd = "su brtest -c '/bin/systemctl restart systemd-timesyncd.service'"
  45. _, exit_code = self.emulator.run(cmd, 10)
  46. self.assertEqual(exit_code, 0)
  47. cmd = "rm {path}/{file}".format(file=rule_file, path=rule_path)
  48. _, exit_code = self.emulator.run(cmd, 10)
  49. self.assertEqual(exit_code, 0)
  50. class TestPolkitInitd(TestPolkitInfra):
  51. config = TestPolkitInfra.config
  52. def test_run(self):
  53. TestPolkitInfra.base_test_run(self)
  54. rule_file = "hello-polkit.rules"
  55. for rule_path in TestPolkitInfra.rule_paths:
  56. cmd = "su brtest -c 'pkexec hello-polkit'"
  57. output, exit_code = self.emulator.run(cmd, 10)
  58. self.assertEqual(exit_code, 127)
  59. self.assertEqual(output[0], "Error executing command as another user: Not authorized")
  60. cmd = "cp /root/{file} {path}/{file}".format(file=rule_file, path=rule_path)
  61. _, exit_code = self.emulator.run(cmd, 10)
  62. self.assertEqual(exit_code, 0)
  63. cmd = "su brtest -c 'pkexec hello-polkit'"
  64. output, exit_code = self.emulator.run(cmd, 10)
  65. self.assertEqual(exit_code, 0)
  66. self.assertEqual(output[0], "Hello polkit!")
  67. cmd = "rm {path}/{file}".format(file=rule_file, path=rule_path)
  68. _, exit_code = self.emulator.run(cmd, 10)
  69. self.assertEqual(exit_code, 0)