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