From: aborah-sudo Date: Tue, 31 Mar 2026 04:16:02 +0000 (+0530) Subject: Tests: Add a new user with an invalid UID X-Git-Url: http://git.ipfire.org/gitweb/?a=commitdiff_plain;h=a83f6d5cee2c1f79db7ec4c969f9d256695ae23f;p=thirdparty%2Fshadow.git Tests: Add a new user with an invalid UID 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 --- diff --git a/tests/system/tests/test_useradd.py b/tests/system/tests/test_useradd.py index 5654fdba5..ab20818a6 100644 --- a/tests/system/tests/test_useradd.py +++ b/tests/system/tests/test_useradd.py @@ -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"