xref: /OK3568_Linux_fs/buildroot/support/testing/tests/package/test_sudo.py (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1import os
2
3import infra.basetest
4
5
6class TestSudo(infra.basetest.BRTest):
7    config = infra.basetest.BASIC_TOOLCHAIN_CONFIG + \
8        """
9        BR2_PACKAGE_SUDO=y
10        BR2_TARGET_ROOTFS_CPIO=y
11        # BR2_TARGET_ROOTFS_TAR is not set
12        """
13
14    def test_run(self):
15        img = os.path.join(self.builddir, "images", "rootfs.cpio")
16        self.emulator.boot(arch="armv5",
17                           kernel="builtin",
18                           options=["-initrd", img])
19        self.emulator.login()
20
21        # -D    don't set a password
22        # -h    set home directory
23        # -H    don't create home directory
24        # -s    set shell
25        self.assertRunOk("adduser -D -h /tmp -H -s /bin/sh sudotest")
26
27        self.assertRunOk("echo 'sudotest ALL=(ALL) NOPASSWD: ALL' >> /etc/sudoers")
28
29        output, exit_code = self.emulator.run("su - sudotest -c 'echo hello world'")
30        self.assertEqual(output, ["hello world"])
31
32        output, exit_code = self.emulator.run("su - sudotest -c 'sudo echo hello world'")
33        self.assertEqual(exit_code, 0)
34        self.assertEqual(output, ["hello world"])
35