kumquat-buildroot/support/testing/tests/package/test_polkit.py
Adam Duskett db1ded1084 support/testing: add polkit tests
This test script tests polkit with and without systemd.

The Systemd test does the following:
  - The brtest user attempts to restart the systemd-timesyncd service and is
    denied.

  - A systemd-timesyncd-restart.rules file provided by polkit-rules-test
    is copied from /root/ to /etc/polkit-1/rules.d

  - The brtest user attempts to restart the systemd-timesyncd service and should
    now succeed.

The initd test does the following:
- The brtest user attempts to run the test application "hello-polkit" with the
  command "pkexec hello-polkit" and is denied.

- A hello-polkit.rules file provided by polkit-rules-test is copied from /root/
  to /etc/polkit-1/rules.d

- The brtest user attempts to re-run the test hello-polkit binary with
  "pkexec hello-polkit" and succeeds.

Signed-off-by: Adam Duskett <aduskett@gmail.com>
Signed-off-by: Thomas Petazzoni <thomas.petazzoni@bootlin.com>
2021-07-24 23:29:21 +02:00

71 lines
2.3 KiB
Python

import os
import infra.basetest
class TestPolkitInfra(infra.basetest.BRTest):
br2_external = [infra.filepath("tests/package/br2-external/polkit")]
config = \
"""
BR2_arm=y
BR2_cortex_a9=y
BR2_ARM_ENABLE_VFP=y
BR2_TOOLCHAIN_EXTERNAL=y
BR2_TOOLCHAIN_EXTERNAL_BOOTLIN=y
BR2_TARGET_ROOTFS_CPIO=y
BR2_PACKAGE_POLKIT=y
BR2_PACKAGE_POLKIT_RULES_TEST=y
"""
def base_test_run(self):
cpio_file = os.path.join(self.builddir, "images", "rootfs.cpio")
self.emulator.boot(arch="armv7", kernel="builtin",
options=["-initrd", cpio_file])
self.emulator.login()
class TestPolkitSystemd(TestPolkitInfra):
config = \
"""
{}
BR2_INIT_SYSTEMD=y
BR2_PACKAGE_SYSTEMD_POLKIT=y
BR2_TARGET_GENERIC_GETTY_PORT="ttyAMA0"
# BR2_TARGET_ROOTFS_TAR is not set
""".format(TestPolkitInfra.config)
def test_run(self):
TestPolkitInfra.base_test_run(self)
cmd = "su brtest -c '/bin/systemctl restart systemd-timesyncd.service'"
_, exit_code = self.emulator.run(cmd, 10)
self.assertEqual(exit_code, 1)
cmd = "mv /root/systemd-timesyncd-restart.rules /etc/polkit-1/rules.d"
_, exit_code = self.emulator.run(cmd, 10)
self.assertEqual(exit_code, 0)
cmd = "su brtest -c '/bin/systemctl restart systemd-timesyncd.service'"
_, exit_code = self.emulator.run(cmd, 10)
self.assertEqual(exit_code, 0)
class TestPolkitInitd(TestPolkitInfra):
config = TestPolkitInfra.config
def test_run(self):
TestPolkitInfra.base_test_run(self)
cmd = "su brtest -c 'pkexec hello-polkit'"
output, exit_code = self.emulator.run(cmd, 10)
self.assertEqual(exit_code, 127)
self.assertEqual(output[0], "Error executing command as another user: Not authorized")
cmd = "mv /root/hello-polkit.rules /etc/polkit-1/rules.d/hello-polkit.rules"
_, exit_code = self.emulator.run(cmd, 10)
self.assertEqual(exit_code, 0)
cmd = "su brtest -c 'pkexec hello-polkit'"
output, exit_code = self.emulator.run(cmd, 10)
self.assertEqual(exit_code, 0)
self.assertEqual(output[0], "Hello polkit!")