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