xref: /OK3568_Linux_fs/buildroot/support/testing/tests/core/test_root_password.py (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1import os
2import infra.basetest
3from crypt import crypt
4
5
6class TestRootPassword(infra.basetest.BRTest):
7    password = "foo"
8    config = infra.basetest.BASIC_TOOLCHAIN_CONFIG + \
9        """
10        BR2_TARGET_ROOTFS_CPIO=y
11        BR2_TARGET_ENABLE_ROOT_LOGIN=y
12        BR2_TARGET_GENERIC_ROOT_PASSWD="{}"
13        """.format(password)
14
15    def test_run(self):
16        # 1. Test by looking hash in the /etc/shadow
17        shadow = os.path.join(self.builddir, "target", "etc", "shadow")
18        with open(shadow, "r") as f:
19            users = f.readlines()
20            for user in users:
21                s = user.split(":")
22                n, h = s[0], s[1]
23                if n == "root":
24                    # Fail if the account is disabled or no password is required
25                    self.assertTrue(h not in ["", "*"])
26                    # Fail if the hash isn't right
27                    self.assertEqual(crypt(self.password, h), h)
28
29        # 2. Test by attempting to login
30        cpio_file = os.path.join(self.builddir, "images", "rootfs.cpio")
31        try:
32            self.emulator.boot(arch="armv7", kernel="builtin",
33                               options=["-initrd", cpio_file])
34            self.emulator.login(self.password)
35        except SystemError:
36            self.fail("Unable to login with the password")
37