]> git.ipfire.org Git - thirdparty/shadow.git/commitdiff
Tests: Add a new user with an invalid UID
authoraborah-sudo <aborah@redhat.com>
Tue, 31 Mar 2026 04:16:02 +0000 (09:46 +0530)
committerIker Pedrosa <ikerpedrosam@gmail.com>
Tue, 31 Mar 2026 14:24:45 +0000 (16:24 +0200)
This is the transformation to Python of the test located in
`tests/usertools/01/13_useradd_negative_UID.test`,
`tests/usertools/01/14_useradd_out_of_range_UID.test`
which checks that `useradd` can not add a new user with invalid UID

tests/system/tests/test_useradd.py

index 5654fdba5a639de5e378f187b9cd41010dac2217..ab20818a68028a6555556e5c505458b6aec5dee3 100644 (file)
@@ -414,3 +414,40 @@ def test_useradd__valid_group_as_primary(shadow: Shadow, group_name: str, group_
     assert (
         passwd_entry.gid == group_entry.gid
     ), f"User's GID ({passwd_entry.gid}) should match group's GID ({group_entry.gid})"
+
+
+@pytest.mark.topology(KnownTopology.Shadow)
+@pytest.mark.parametrize(
+    "uid_value, expected_error",
+    [
+        pytest.param(-1, "useradd: invalid user ID '-1'", id="negative_uid"),
+        pytest.param(4294967296, "useradd: invalid user ID '4294967296'", id="out_of_range_uid"),
+    ],
+)
+def test_useradd__invalid_uid(shadow: Shadow, uid_value: int, expected_error: str):
+    """
+    :title: Add a new user with an invalid UID
+    :steps:
+        1. Attempt to create user with invalid UID
+        2. Verify command fails with appropriate error code and message
+        3. Check passwd, group entries
+        4. Check home directory
+    :expectedresults:
+        1. useradd command fails
+        2. Return code is 3 (invalid argument)
+        3. No entries are added to passwd, group
+        4. No home directory is created
+    :customerscenario: False
+    """
+    with pytest.raises(ProcessError) as exc_info:
+        shadow.useradd(f"test1 -u {uid_value}")
+
+    actual_rc = getattr(exc_info.value, "rc", getattr(exc_info.value, "returncode", None))
+    assert actual_rc == 3, f"Expected return code 3 (invalid argument), got {actual_rc}"
+
+    error_output = exc_info.value.stderr.strip() if exc_info.value.stderr else ""
+    assert error_output == expected_error, f"Expected error message '{expected_error}', got '{error_output}'"
+
+    assert shadow.tools.getent.passwd("test1") is None, "User test1 should not be found in passwd"
+    assert shadow.tools.getent.group("test1") is None, "Group test1 should not be found"
+    assert not shadow.fs.exists("/home/test1"), "Home directory should not be created"