Skip to content

Add tests for passwdqc pwqcheck #41

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,17 @@ remove_lib_wrapped:
remove_locales_wrapped:
for f in $(LANGUAGES); do $(RM) $(DESTDIR)$(LOCALEDIR)/$$f/LC_MESSAGES/$(PACKAGE).mo; done

check:
@echo "Running tests..."
# Set LD_LIBRARY_PATH to include the current directory
@export LDFLAGS="-Wl,-rpath,$(CURDIR)"; \
export LD_LIBRARY_PATH=$(CURDIR):$$LD_LIBRARY_PATH; \
Copy link
Member

Choose a reason for hiding this comment

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

I think this is excessive. For my manual testing, I simply set LD_LIBRARY_PATH=. and it just works.

The change of LDFLAGS wouldn't take effect at this point anyway, because the test scripts being invoked from here assume the tool has already been built.

for test in tests/*.sh; do \
echo "Executing $$test..."; \
bash $$test || echo "$$test failed"; \
done
@echo "Tests completed!"

clean:
$(RM) $(PROJ) $(POTFILE) $(MOFILES) *.o

Expand Down
76 changes: 76 additions & 0 deletions tests/test-pwqcheck-basic.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
#!/bin/bash
#
# Copyright (c) 2025 by Zaiba Sanglikar. See LICENSE.
#
#

if [ -t 1 ]; then
# Colors for better visibility
GREEN='\033[0;32m'
RED='\033[0;31m'
NC='\033[0m'
else
GREEN=''
RED=''
NC=''
fi

# Test function for basic password checks
test_basic_password() {
local password="$1"
local expected="$2"
local description="$3"

PWQCHECK_BIN="$(dirname "$0")/../pwqcheck"

echo -n "Testing $description: "
result=$(echo "$password" | "$PWQCHECK_BIN" -1 2>&1)
exit_code=$?

if [ "$expected" = "pass" -a "$exit_code" -eq 0 ] || \
[ "$expected" = "fail" -a "$exit_code" -ne 0 ]; then
echo -e "${GREEN}PASS${NC}"
echo " Password tested: '$password'"
echo " Result: $result"
echo
return 0
else
echo -e "${RED}FAIL${NC}"
echo "Password tested: $password"
echo "Expected: $expected"
echo "Got: $result"
echo
return 1
fi
}

echo "Running Basic Password Validation Tests..."

# Test Suite 1: Strong Passwords
echo -e "\nTesting Strong Passwords:"
test_basic_password "P@ssw0rd123!" "pass" "Standard strong password"
test_basic_password "Tr0ub4dor&3" "pass" "Complex password"
test_basic_password "iStayInloreAtHomeb7&street111" "pass" "Long passphrase"
test_basic_password "H3llo@W0rld2024" "pass" "Strong password with year"

# Test Suite 2: Common Weak Patterns
echo -e "\nTesting Weak Patterns:"
test_basic_password "password123" "fail" "Common password with numbers"
test_basic_password "qwerty" "fail" "Keyboard pattern"
test_basic_password "admin123" "fail" "Common admin password"
test_basic_password "letmein" "fail" "Common weak password"

# Test Suite 3: Mixed Complexity
echo -e "\nTesting Mixed Complexity:"
test_basic_password "MyP@ssw0rd" "pass" "Mixed case with symbols and numbers"
test_basic_password "Str0ng!P@ssphrase" "pass" "Strong with multiple special chars"
test_basic_password "C0mpl3x1ty!" "pass" "Complex but reasonable length"

# Test Suite 4: Edge Cases
echo -e "\nTesting Edge Cases:"
test_basic_password " " "fail" "Single space"
test_basic_password "" "fail" "Empty password"
test_basic_password "$(printf 'a%.0s' {1..71})" "fail" "Very long password"
test_basic_password "ljy8zk9aBJ3hA3TXAAMAQe61ytFohJM4SuPFbA4L1xDqV2JDE1n8BCnLN96evcJMWyTkr9y3" "pass" "Max length password"
test_basic_password "ljy8zk9aBJ3hA3TXAAMAQe61ytFohJM4SuPFbA4L1xDqV2JDE1n8BCnLN96evcJMWyTkr9y312345" "fail" "Max length exceed password"
echo -e "\nAll basic password validation tests completed!"
80 changes: 80 additions & 0 deletions tests/test-pwqcheck-length.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
#!/bin/bash
#
# Copyright (c) 2025 by Zaiba Sanglikar. See LICENSE.
#
#

if [ -t 1 ]; then
# Colors for better visibility
GREEN='\033[0;32m'
RED='\033[0;31m'
NC='\033[0m'
else
GREEN=''
RED=''
NC=''
fi

# Test helper function with improved output handling
test_password() {
local password="$1"
local min_params="$2"
local expected="$3"
local test_name="$4"

printf "Testing %-40s" "$test_name:"

PWQCHECK_BIN="$(dirname "$0")/../pwqcheck"

output=$(echo "$password" | "$PWQCHECK_BIN" -1 "min=$min_params" 2>&1)
status=$?

if [ "$expected" = "pass" -a $status -eq 0 ] || \
[ "$expected" = "fail" -a $status -ne 0 ]; then
echo -e "${GREEN}PASS${NC}"
echo " Password tested: '$password'"
echo " Result: $output"
echo
else
echo -e "${RED}FAIL${NC}"
echo " Password tested: '$password'"
echo " Expected: $expected"
echo " Got: $output"
echo " Exit status: $status"
echo
return 1
fi
}

# Main test suite
main() {
echo "=== Password Length Tests ==="
echo

# Test 1: Default minimum lengths
test_password "short" "24,12,8,7,6" "fail" "Short password"
test_password "ThisIsAVeryLongPasswordThatShouldPass123!" "24,12,8,7,6" "pass" "Long complex password"

# Test 2: Custom minimum lengths
test_password "pass123" "6,6,6,6,6" "pass" "Password with relaxed mins"
test_password "a" "6,6,6,6,6" "fail" "Single character password"

# Test 3: Different complexity levels
test_password "BearD&Tach" "8,8,8,8,8" "pass" "Simple but long password"
test_password "P@ssw0rd!" "8,8,8,8,8" "pass" "Complex password"

# Test 4: Edge cases
test_password "YakMeas1" "8,8,8,8,8" "pass" "Exactly minimum length"
test_password "7chars" "8,8,8,8,8" "fail" "Below minimum length"

# Test 5: Different complexity classes
echo "Testing complexity classes..."
test_password "FigRatMatBatSatWatPatCat" "24,12,8,7,6" "pass" "N0: 24-char basic password"
test_password "Complex12Pass" "24,12,8,7,6" "pass" "N1: 12-char mixed password"
test_password "P@ss8chr" "24,12,8,7,6" "pass" "N2: 8-char complex password"
test_password "P@s7chr" "24,12,8,7,6" "pass" "N3: 7-char complex password"
test_password "B!rd5#K" "24,12,8,7,6" "pass" "N4: 6-char highly complex password"
}

# Run the tests
main
124 changes: 124 additions & 0 deletions tests/test-pwqcheck-multi.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
#!/bin/bash
#
# Copyright (c) 2025 by Zaiba Sanglikar. See LICENSE.
#
#

if [ -t 1 ]; then
# Colors for better visibility
GREEN='\033[0;32m'
RED='\033[0;31m'
NC='\033[0m'
else
GREEN=''
RED=''
NC=''
fi

# Function to test multiple passwords
test_multiple_passwords() {
local test_name="$1"
local passwords="$2"
local expected_results="$3"

echo -e "\nRunning Test: ${test_name}"
echo "------------------------"

# Create a temporary file for test output
local temp_output
temp_output=$(mktemp)

PWQCHECK_BIN="$(dirname "$0")/../pwqcheck"

# Run pwqcheck with multiple passwords
echo -e "$passwords" | "$PWQCHECK_BIN" --multi -1 > "$temp_output" 2>&1

# Compare results
local actual_results
actual_results=$(cat "$temp_output")
if echo "$actual_results" | grep -q "$expected_results"; then
echo -e "${GREEN}PASS${NC}"
echo "Test output matches expected results"
else
echo -e "${RED}FAIL${NC}"
echo "Expected:"
echo "$expected_results"
echo "Got:"
cat "$temp_output"
fi

rm -f "$temp_output"
}

echo "Running Multiple Password Tests..."

# Test 1: Mix of valid and invalid passwords
test_multiple_passwords "Mixed Passwords" \
"StrongP@ss123!
weak
Tr0ub4dor&3
password123
C0mpl3x1ty!" \
"OK: StrongP@ss123!
Bad passphrase
OK: Tr0ub4dor&3
Bad passphrase
OK: C0mpl3x1ty!"

# Test 2: All valid passwords
test_multiple_passwords "All Valid Passwords" \
"P@ssw0rd123!
Tr0ub4dor&3
C0mpl3x1ty!
H3llo@W0rld" \
"OK: P@ssw0rd123!
OK: Tr0ub4dor&3
OK: C0mpl3x1ty!
OK: H3llo@W0rld"

# Test 3: All invalid passwords
test_multiple_passwords "All Invalid Passwords" \
"password123
qwerty
admin
letmein" \
"Bad passphrase
Bad passphrase
Bad passphrase
Bad passphrase"

# Test 4: Empty lines and special characters
test_multiple_passwords "Special Cases" \
"StrongP@ss123!
P@ssw0rd!
Tr0ub4dor&3" \
"OK: StrongP@ss123!
Bad passphrase
OK: P@ssw0rd!
Bad passphrase
OK: Tr0ub4dor&3"

# Test 5: With custom parameters
test_multiple_passwords "Custom Parameters" \
"short
medium12345
VeryLongP@ssword123!" \
"Bad passphrase
OK: medium12345
OK: VeryLongP@ssword123!"

echo -e "\nAll multiple password tests completed!"

# Test 6: Large number of passwords
echo -e "\nTesting large batch of passwords..."
{
for i in {1..50}; do
if [ $((i % 2)) -eq 0 ]; then
echo "StrongP@ss${i}!"
else
echo "weak${i}"
fi
done
} | "$PWQCHECK_BIN" --multi -1

echo "Large batch test completed!"
Loading
Loading