Skip to content

tests: add comprehensive unit tests for is_valid_hash() function#1529

Merged
alejandro-colomar merged 9 commits intoshadow-maint:masterfrom
ikerexxe:test-chkhash
Feb 17, 2026
Merged

tests: add comprehensive unit tests for is_valid_hash() function#1529
alejandro-colomar merged 9 commits intoshadow-maint:masterfrom
ikerexxe:test-chkhash

Conversation

@ikerexxe
Copy link
Copy Markdown
Collaborator

@ikerexxe ikerexxe commented Feb 6, 2026

Add comprehensive unit testing for the is_valid_hash() function in lib/chkhash.c, which is critical for preventing invalid password hashes from being written to /etc/shadow.

The is_valid_hash() function had no unit test coverage despite being security-critical. Recent issues and PRs (#1483, #1520, #1486, #1505) highlighted the need for robust testing to prevent regressions that could lock users out of their accounts.

Finally, I've modified a misleading comment for yescrypt hash lenght.

@ikerexxe
Copy link
Copy Markdown
Collaborator Author

ikerexxe commented Feb 6, 2026

@AdamWill this is something that you asked for in one of the PRs that you opened when fixing hash validation. Pinging you just in case you'd like to take a look

@AdamWill
Copy link
Copy Markdown
Contributor

AdamWill commented Feb 6, 2026

Thanks a lot! This is great. I think it's missing checks of bare ! and *, right, which was one of the cases that needed fixing? But other than that it looks good, covers the points we've found so far, and is well structured so it'd be easy to extend for future cases.

Comment thread lib/chkhash.c
Comment thread tests/unit/test_chkhash.c Outdated
Comment thread tests/unit/test_chkhash.c
Comment thread tests/unit/test_chkhash.c
Comment thread tests/unit/test_chkhash.c Outdated
Fix misleading comment that stated "43-char (minimum) hash" when
the actual regex pattern requires exactly 43 characters. Update
comment to accurately reflect the implementation behavior.

Signed-off-by: Iker Pedrosa <ipedrosa@redhat.com>
Reviewed-by: Alejandro Colomar <alx@kernel.org>
Introduce unit testing infrastructure for the `is_valid_hash()`
function. Add yescrypt algorithm validation tests.

Signed-off-by: Iker Pedrosa <ipedrosa@redhat.com>
Add comprehensive bcrypt algorithm validation tests covering all
variants ($2a$, $2b$, $2x$, $2y$) with different cost factors.

Signed-off-by: Iker Pedrosa <ipedrosa@redhat.com>
Add comprehensive SHA-512 algorithm validation tests covering rounds and
salt cases.

Signed-off-by: Iker Pedrosa <ipedrosa@redhat.com>
Add comprehensive SHA-256 algorithm validation tests covering rounds and
salt cases.

Signed-off-by: Iker Pedrosa <ipedrosa@redhat.com>
Add basic MD5 and DES algorithm validation tests.

Signed-off-by: Iker Pedrosa <ipedrosa@redhat.com>
*, ! and empty strings are special valid cases for shadow's second
field. Add test cases for them.

Signed-off-by: Iker Pedrosa <ipedrosa@redhat.com>
Signed-off-by: Iker Pedrosa <ipedrosa@redhat.com>
Add comprehensive negative testing condition validation:
- Invalid algorithm prefixes and hash length validation
- Invalid delimiter handling
- Invalid salt characters and rounds parameter testing

Signed-off-by: Iker Pedrosa <ipedrosa@redhat.com>
@ikerexxe
Copy link
Copy Markdown
Collaborator Author

ikerexxe commented Feb 9, 2026

Thanks a lot! This is great. I think it's missing checks of bare ! and *, right, which was one of the cases that needed fixing?

That's already covered in test_is_valid_hash_ok_special()

Copy link
Copy Markdown
Collaborator

@alejandro-colomar alejandro-colomar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! LGTM.

@alejandro-colomar alejandro-colomar merged commit 0970533 into shadow-maint:master Feb 17, 2026
12 checks passed
@ikerexxe ikerexxe deleted the test-chkhash branch February 18, 2026 08:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants