Skip to contentSkip to conversation

support/testing: add test for root password

PATCH Superseded Victor Huesca 2 participants 3 messages Jul 19, 2019 – Aug 12, 2019

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 support/testing: add test for root password

Add support to test that the root passowrd is working as expected.
- Buildtime test: Check the hash present in the /etc/shadow.
- Runtime test: Build an armv7 image and try to login with a password.

---
Changes v1 --> v2:
- Fix coding style to pass flake8 test

---
 .../testing/tests/core/test_root_password.py  | 36 +++++++++++++++++++
 1 file changed, 36 insertions(+)
 create mode 100644 support/testing/tests/core/test_root_password.py
support/testing/tests/core/test_root_password.py new file +36 -0
new file mode 100644
index 0000000000..c6f6b76bfd
@@ -0,0 +1,36 @@
+import os
+import infra.basetest
+from crypt import crypt
+
+
+class TestRootPassword(infra.basetest.BRTest):
+    password = "foo"
+    config = infra.basetest.BASIC_TOOLCHAIN_CONFIG + \
+        """
+        BR2_TARGET_ENABLE_ROOT_LOGIN=y
+        BR2_TARGET_GENERIC_ROOT_PASSWD="{}"
+        """.format(password)
+
+    def test_run(self):
+        # 1. Test by looking hash in the /etc/shadow
+        shadow = os.path.join(self.builddir, "target", "etc", "shadow")
+        with open(shadow, "r") as f:
+            users = f.readlines()
+            for user in users:
+                s = user.split(":")
+                n, h = s[0], s[1]
+                if n == "root":
+                    # Fail if the account is disabled or no password is required
+                    self.assertTrue(h not in ["", "*"])
+                    # Fail if the has isn't right
+                    self.assertTrue(crypt(self.password, h) == h)
+
+        # 2. Test by attempting to login
+        try:
+            cpio_file = os.path.join(self.builddir, "images", "rootfs.cpio")
+            self.emulator.boot(arch="armv7",
+                               kernel="builtin",
+                               options=["-initrd", cpio_file])
+            self.emulator.login(self.password)
+        except SystemError:
+            self.fail("Unable to login with the password")
Signature · 1 line
2.21.0
#1 lore raw

PATCH v2 1/1 support/testing: add test for root password

Hello,

Looks good in general, but there is a major issue that prevents the target to
boot: the generation of cpio is not enabled.
Also a few nits.

Quoted 4 lines · Victor Huesca

Add support to test that the root passowrd is working as expected.
- Buildtime test: Check the hash present in the /etc/shadow.
- Runtime test: Build an armv7 image and try to login with a password.

Quoted 8 lines

---
Changes v1 --> v2:
- Fix coding style to pass flake8 test

---
 .../testing/tests/core/test_root_password.py  | 36 +++++++++++++++++++
 1 file changed, 36 insertions(+)
 create mode 100644 support/testing/tests/core/test_root_password.py

The update to .gitlab-ci.yml is missing. You can generate the change with 'make .gitlab-ci.yml' and commit.

Quoted 8 lines
support/testing/tests/core/test_root_password.py new file +1 -0
new file mode 100644
index 0000000000..c6f6b76bfd
@@ -0,0 +1,36 @@
+import os

support/testing/tests/core/test_root_password.py line 10

Quoted 10 lines
support/testing/tests/core/test_root_password.py new file +9 -0
+import infra.basetest
+from crypt import crypt
+
+
+class TestRootPassword(infra.basetest.BRTest):
+    password = "foo"
+    config = infra.basetest.BASIC_TOOLCHAIN_CONFIG + \
+        """
 
+        BR2_TARGET_ENABLE_ROOT_LOGIN=y

Not really needed, it ends up defaulting to 'y'.

Quoted 1 line

+ BR2_TARGET_GENERIC_ROOT_PASSWD="{}"

Here are missing:
BR2_TARGET_ROOTFS_CPIO=y
# BR2_TARGET_ROOTFS_TAR is not set
See test_dropbear.py for an example.

Quoted 4 lines
support/testing/tests/core/test_root_password.py new file +4 -0
+        """.format(password)
+
+    def test_run(self):
+        # 1. Test by looking hash in the /etc/shadow

support/testing/tests/core/test_root_password.py line 25

Quoted 10 lines
support/testing/tests/core/test_root_password.py new file +10 -0
+        shadow = os.path.join(self.builddir, "target", "etc", "shadow")
+        with open(shadow, "r") as f:
+            users = f.readlines()
+            for user in users:
+                s = user.split(":")
+                n, h = s[0], s[1]
+                if n == "root":
+                    # Fail if the account is disabled or no password is required
+                    self.assertTrue(h not in ["", "*"])
+                    # Fail if the has isn't right

typo: has -> hash

Quoted 1 line

+ self.assertTrue(crypt(self.password, h) == h)

You could use assertEqual instead.

support/testing/tests/core/test_root_password.py line 36

Quoted 10 lines
support/testing/tests/core/test_root_password.py new file +10 -0
+
+        # 2. Test by attempting to login
+        try:
+            cpio_file = os.path.join(self.builddir, "images", "rootfs.cpio")
+            self.emulator.boot(arch="armv7",
+                               kernel="builtin",
+                               options=["-initrd", cpio_file])
+            self.emulator.login(self.password)
+        except SystemError:
+            self.fail("Unable to login with the password")

I was about to suggest to remove the try/except.
If we replace the block of code with only the 3 lines currently inside the 'try' the infra will:
- raise 'SystemError: System does not boot' if the target dow not boot
- raise 'SystemError: Cannot login' if the login fails
With the block you used, it will raise
'AssertionError: Unable to login with the password' for both cases.

But later I understood what you did.
Without the try/except we end up with an ERROR, while with the try/except we end up with a FAIL, it is conceptually more correct.

So I agree that the code you wrote is better. Let's keep it as is.

When we eventually use different exceptions for each failure on the infra itself your code will be ready to be changed to
except infra.exceptions.LoginError:
See http://patchwork.ozlabs.org/patch/1043199/ from Thomas S.

Regards,
Ricardo

#2 lore raw

PATCH v2 1/1 support/testing: add test for root password

Hi,

Thank you for this detailed feedback, I'll prepare a v3.

Quoted 41 lines · Ricardo Martincoski

Hello,

Looks good in general, but there is a major issue that prevents the target to
boot: the generation of cpio is not enabled.
Also a few nits.

Quoted 4 lines · Victor Huesca

Add support to test that the root passowrd is working as expected.
- Buildtime test: Check the hash present in the /etc/shadow.
- Runtime test: Build an armv7 image and try to login with a password.

Quoted 8 lines

---
Changes v1 --> v2:
- Fix coding style to pass flake8 test

---
 .../testing/tests/core/test_root_password.py  | 36 +++++++++++++++++++
 1 file changed, 36 insertions(+)
 create mode 100644 support/testing/tests/core/test_root_password.py

The update to .gitlab-ci.yml is missing. You can generate the change with 'make .gitlab-ci.yml' and commit.

Quoted 16 lines
support/testing/tests/core/test_root_password.py new file +9 -0
new file mode 100644
index 0000000000..c6f6b76bfd
@@ -0,0 +1,36 @@
+import os
+import infra.basetest
+from crypt import crypt
+
+
+class TestRootPassword(infra.basetest.BRTest):
+    password = "foo"
+    config = infra.basetest.BASIC_TOOLCHAIN_CONFIG + \
+        """
Quoted 3 lines

support/testing/tests/core/test_root_password.py line 10

Quoted 1 line
support/testing/tests/core/test_root_password.py new file +1 -0
+        BR2_TARGET_ENABLE_ROOT_LOGIN=y

Not really needed, it ends up defaulting to 'y'.

Isn't it more clear to explicitly set it to 'y' ?

Quoted 8 lines

support/testing/tests/core/test_root_password.py line 11

Quoted 2 lines
support/testing/tests/core/test_root_password.py new file +1 -0
+        BR2_TARGET_GENERIC_ROOT_PASSWD="{}"

Here are missing:
BR2_TARGET_ROOTFS_CPIO=y
# BR2_TARGET_ROOTFS_TAR is not set
See test_dropbear.py for an example.

Yes I saw that and had a patch ready to submit for that.

Quoted 15 lines
Quoted 4 lines
support/testing/tests/core/test_root_password.py new file +4 -0
+        """.format(password)
+
+    def test_run(self):
+        # 1. Test by looking hash in the /etc/shadow

support/testing/tests/core/test_root_password.py line 25

Quoted 10 lines
support/testing/tests/core/test_root_password.py new file +10 -0
+        shadow = os.path.join(self.builddir, "target", "etc", "shadow")
+        with open(shadow, "r") as f:
+            users = f.readlines()
+            for user in users:
+                s = user.split(":")
+                n, h = s[0], s[1]
+                if n == "root":
+                    # Fail if the account is disabled or no password is required
+                    self.assertTrue(h not in ["", "*"])
+                    # Fail if the has isn't right
Quoted 2 lines

typo: has -> hash

Oups, thanks

Quoted 4 lines

support/testing/tests/core/test_root_password.py line 26

Quoted 1 line
support/testing/tests/core/test_root_password.py new file +1 -0
+                    self.assertTrue(crypt(self.password, h) == h)

You could use assertEqual instead.

Ack

Quoted 23 lines

support/testing/tests/core/test_root_password.py line 36

Quoted 10 lines
support/testing/tests/core/test_root_password.py new file +10 -0
+
+        # 2. Test by attempting to login
+        try:
+            cpio_file = os.path.join(self.builddir, "images", "rootfs.cpio")
+            self.emulator.boot(arch="armv7",
+                               kernel="builtin",
+                               options=["-initrd", cpio_file])
+            self.emulator.login(self.password)
+        except SystemError:
+            self.fail("Unable to login with the password")

I was about to suggest to remove the try/except.
If we replace the block of code with only the 3 lines currently inside the 'try' the infra will:
- raise 'SystemError: System does not boot' if the target dow not boot
- raise 'SystemError: Cannot login' if the login fails
With the block you used, it will raise
'AssertionError: Unable to login with the password' for both cases.

But later I understood what you did.
Without the try/except we end up with an ERROR, while with the try/except we end up with a FAIL, it is conceptually more correct.

Quoted 7 lines

So I agree that the code you wrote is better. Let's keep it as is.

When we eventually use different exceptions for each failure on the infra itself your code will be ready to be changed to
except infra.exceptions.LoginError:
See http://patchwork.ozlabs.org/patch/1043199/ from Thomas S.

I'll keep an eye on this patch.

Quoted 3 lines

Regards,
Ricardo

Signature · 3 lines
Victor Huesca, Bootlin
Embedded Linux and kernel engineering
https://bootlin.com