Skip to content

Commit e8f86b0

Browse files
Zaiba-Ssolardiz
authored andcommitted
Include tests to test pwqcheck
Signed-off-by: Zaiba Sanglikar <[email protected]>
1 parent efd0136 commit e8f86b0

File tree

4 files changed

+384
-0
lines changed

4 files changed

+384
-0
lines changed

tests/test-pwqcheck-basic.sh

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
#!/bin/bash
2+
#
3+
# Copyright (c) 2025 by Zaiba Sanglikar. See LICENSE.
4+
#
5+
#
6+
7+
if [ -t 1 ]; then
8+
# Colors for better visibility
9+
GREEN='\033[0;32m'
10+
RED='\033[0;31m'
11+
NC='\033[0m'
12+
else
13+
GREEN=''
14+
RED=''
15+
NC=''
16+
fi
17+
18+
# Test function for basic password checks
19+
test_basic_password() {
20+
local password="$1"
21+
local expected="$2"
22+
local description="$3"
23+
24+
PWQCHECK_BIN="$(dirname "$0")/../pwqcheck"
25+
26+
echo -n "Testing $description: "
27+
result=$(echo "$password" | "$PWQCHECK_BIN" -1 2>&1)
28+
exit_code=$?
29+
30+
if [ "$expected" = "pass" -a "$exit_code" -eq 0 ] || \
31+
[ "$expected" = "fail" -a "$exit_code" -ne 0 ]; then
32+
echo -e "${GREEN}PASS${NC}"
33+
echo " Password tested: '$password'"
34+
echo " Result: $result"
35+
echo
36+
return 0
37+
else
38+
echo -e "${RED}FAIL${NC}"
39+
echo "Password tested: $password"
40+
echo "Expected: $expected"
41+
echo "Got: $result"
42+
echo
43+
return 1
44+
fi
45+
}
46+
47+
echo "Running Basic Password Validation Tests..."
48+
49+
# Test Suite 1: Strong Passwords
50+
echo -e "\nTesting Strong Passwords:"
51+
test_basic_password "P@ssw0rd123!" "pass" "Standard strong password"
52+
test_basic_password "Tr0ub4dor&3" "pass" "Complex password"
53+
test_basic_password "iStayInloreAtHomeb7&street111" "pass" "Long passphrase"
54+
test_basic_password "H3llo@W0rld2024" "pass" "Strong password with year"
55+
56+
# Test Suite 2: Common Weak Patterns
57+
echo -e "\nTesting Weak Patterns:"
58+
test_basic_password "password123" "fail" "Common password with numbers"
59+
test_basic_password "qwerty" "fail" "Keyboard pattern"
60+
test_basic_password "admin123" "fail" "Common admin password"
61+
test_basic_password "letmein" "fail" "Common weak password"
62+
63+
# Test Suite 3: Mixed Complexity
64+
echo -e "\nTesting Mixed Complexity:"
65+
test_basic_password "MyP@ssw0rd" "pass" "Mixed case with symbols and numbers"
66+
test_basic_password "Str0ng!P@ssphrase" "pass" "Strong with multiple special chars"
67+
test_basic_password "C0mpl3x1ty!" "pass" "Complex but reasonable length"
68+
69+
# Test Suite 4: Edge Cases
70+
echo -e "\nTesting Edge Cases:"
71+
test_basic_password " " "fail" "Single space"
72+
test_basic_password "" "fail" "Empty password"
73+
test_basic_password "$(printf 'a%.0s' {1..71})" "fail" "Very long password"
74+
test_basic_password "ljy8zk9aBJ3hA3TXAAMAQe61ytFohJM4SuPFbA4L1xDqV2JDE1n8BCnLN96evcJMWyTkr9y3" "pass" "Max length password"
75+
test_basic_password "ljy8zk9aBJ3hA3TXAAMAQe61ytFohJM4SuPFbA4L1xDqV2JDE1n8BCnLN96evcJMWyTkr9y312345" "fail" "Max length exceed password"
76+
echo -e "\nAll basic password validation tests completed!"

tests/test-pwqcheck-length.sh

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
#!/bin/bash
2+
#
3+
# Copyright (c) 2025 by Zaiba Sanglikar. See LICENSE.
4+
#
5+
#
6+
7+
if [ -t 1 ]; then
8+
# Colors for better visibility
9+
GREEN='\033[0;32m'
10+
RED='\033[0;31m'
11+
NC='\033[0m'
12+
else
13+
GREEN=''
14+
RED=''
15+
NC=''
16+
fi
17+
18+
# Test helper function with improved output handling
19+
test_password() {
20+
local password="$1"
21+
local min_params="$2"
22+
local expected="$3"
23+
local test_name="$4"
24+
25+
printf "Testing %-40s" "$test_name:"
26+
27+
PWQCHECK_BIN="$(dirname "$0")/../pwqcheck"
28+
29+
output=$(echo "$password" | "$PWQCHECK_BIN" -1 "min=$min_params" 2>&1)
30+
status=$?
31+
32+
if [ "$expected" = "pass" -a $status -eq 0 ] || \
33+
[ "$expected" = "fail" -a $status -ne 0 ]; then
34+
echo -e "${GREEN}PASS${NC}"
35+
echo " Password tested: '$password'"
36+
echo " Result: $output"
37+
echo
38+
else
39+
echo -e "${RED}FAIL${NC}"
40+
echo " Password tested: '$password'"
41+
echo " Expected: $expected"
42+
echo " Got: $output"
43+
echo " Exit status: $status"
44+
echo
45+
return 1
46+
fi
47+
}
48+
49+
# Main test suite
50+
main() {
51+
echo "=== Password Length Tests ==="
52+
echo
53+
54+
# Test 1: Default minimum lengths
55+
test_password "short" "24,12,8,7,6" "fail" "Short password"
56+
test_password "ThisIsAVeryLongPasswordThatShouldPass123!" "24,12,8,7,6" "pass" "Long complex password"
57+
58+
# Test 2: Custom minimum lengths
59+
test_password "pass123" "6,6,6,6,6" "pass" "Password with relaxed mins"
60+
test_password "a" "6,6,6,6,6" "fail" "Single character password"
61+
62+
# Test 3: Different complexity levels
63+
test_password "BearD&Tach" "8,8,8,8,8" "pass" "Simple but long password"
64+
test_password "P@ssw0rd!" "8,8,8,8,8" "pass" "Complex password"
65+
66+
# Test 4: Edge cases
67+
test_password "YakMeas1" "8,8,8,8,8" "pass" "Exactly minimum length"
68+
test_password "7chars" "8,8,8,8,8" "fail" "Below minimum length"
69+
70+
# Test 5: Different complexity classes
71+
echo "Testing complexity classes..."
72+
test_password "FigRatMatBatSatWatPatCat" "24,12,8,7,6" "pass" "N0: 24-char basic password"
73+
test_password "Complex12Pass" "24,12,8,7,6" "pass" "N1: 12-char mixed password"
74+
test_password "P@ss8chr" "24,12,8,7,6" "pass" "N2: 8-char complex password"
75+
test_password "P@s7chr" "24,12,8,7,6" "pass" "N3: 7-char complex password"
76+
test_password "B!rd5#K" "24,12,8,7,6" "pass" "N4: 6-char highly complex password"
77+
}
78+
79+
# Run the tests
80+
main

tests/test-pwqcheck-multi.sh

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
#!/bin/bash
2+
#
3+
# Copyright (c) 2025 by Zaiba Sanglikar. See LICENSE.
4+
#
5+
#
6+
7+
if [ -t 1 ]; then
8+
# Colors for better visibility
9+
GREEN='\033[0;32m'
10+
RED='\033[0;31m'
11+
NC='\033[0m'
12+
else
13+
GREEN=''
14+
RED=''
15+
NC=''
16+
fi
17+
18+
# Function to test multiple passwords
19+
test_multiple_passwords() {
20+
local test_name="$1"
21+
local passwords="$2"
22+
local expected_results="$3"
23+
24+
echo -e "\nRunning Test: ${test_name}"
25+
echo "------------------------"
26+
27+
# Create a temporary file for test output
28+
local temp_output
29+
temp_output=$(mktemp)
30+
31+
PWQCHECK_BIN="$(dirname "$0")/../pwqcheck"
32+
33+
# Run pwqcheck with multiple passwords
34+
echo -e "$passwords" | "$PWQCHECK_BIN" --multi -1 > "$temp_output" 2>&1
35+
36+
# Compare results
37+
local actual_results
38+
actual_results=$(cat "$temp_output")
39+
if echo "$actual_results" | grep -q "$expected_results"; then
40+
echo -e "${GREEN}PASS${NC}"
41+
echo "Test output matches expected results"
42+
else
43+
echo -e "${RED}FAIL${NC}"
44+
echo "Expected:"
45+
echo "$expected_results"
46+
echo "Got:"
47+
cat "$temp_output"
48+
fi
49+
50+
rm -f "$temp_output"
51+
}
52+
53+
echo "Running Multiple Password Tests..."
54+
55+
# Test 1: Mix of valid and invalid passwords
56+
test_multiple_passwords "Mixed Passwords" \
57+
"StrongP@ss123!
58+
weak
59+
Tr0ub4dor&3
60+
password123
61+
C0mpl3x1ty!" \
62+
"OK: StrongP@ss123!
63+
Bad passphrase
64+
OK: Tr0ub4dor&3
65+
Bad passphrase
66+
OK: C0mpl3x1ty!"
67+
68+
# Test 2: All valid passwords
69+
test_multiple_passwords "All Valid Passwords" \
70+
"P@ssw0rd123!
71+
Tr0ub4dor&3
72+
C0mpl3x1ty!
73+
H3llo@W0rld" \
74+
"OK: P@ssw0rd123!
75+
OK: Tr0ub4dor&3
76+
OK: C0mpl3x1ty!
77+
OK: H3llo@W0rld"
78+
79+
# Test 3: All invalid passwords
80+
test_multiple_passwords "All Invalid Passwords" \
81+
"password123
82+
qwerty
83+
admin
84+
letmein" \
85+
"Bad passphrase
86+
Bad passphrase
87+
Bad passphrase
88+
Bad passphrase"
89+
90+
# Test 4: Empty lines and special characters
91+
test_multiple_passwords "Special Cases" \
92+
"StrongP@ss123!
93+
P@ssw0rd!
94+
Tr0ub4dor&3" \
95+
"OK: StrongP@ss123!
96+
Bad passphrase
97+
OK: P@ssw0rd!
98+
Bad passphrase
99+
OK: Tr0ub4dor&3"
100+
101+
# Test 5: With custom parameters
102+
test_multiple_passwords "Custom Parameters" \
103+
"short
104+
medium12345
105+
VeryLongP@ssword123!" \
106+
"Bad passphrase
107+
OK: medium12345
108+
OK: VeryLongP@ssword123!"
109+
110+
echo -e "\nAll multiple password tests completed!"
111+
112+
# Test 6: Large number of passwords
113+
echo -e "\nTesting large batch of passwords..."
114+
{
115+
for i in {1..50}; do
116+
if [ $((i % 2)) -eq 0 ]; then
117+
echo "StrongP@ss${i}!"
118+
else
119+
echo "weak${i}"
120+
fi
121+
done
122+
} | "$PWQCHECK_BIN" --multi -1
123+
124+
echo "Large batch test completed!"

tests/test-pwqcheck-similarity.sh

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
#!/bin/bash
2+
#
3+
# Copyright (c) 2025 by Zaiba Sanglikar. See LICENSE.
4+
#
5+
#
6+
7+
if [ -t 1 ]; then
8+
# Colors for better visibility
9+
GREEN='\033[0;32m'
10+
RED='\033[0;31m'
11+
NC='\033[0m'
12+
else
13+
GREEN=''
14+
RED=''
15+
NC=''
16+
fi
17+
18+
19+
# Function to run pwqcheck with two passwords
20+
test_passwords() {
21+
local new_pass="$1"
22+
local old_pass="$2"
23+
local expected_result="$3"
24+
local test_name="$4"
25+
26+
PWQCHECK_BIN="$(dirname "$0")/../pwqcheck"
27+
28+
# Run pwqcheck with both passwords and --similar=deny option
29+
result=$(printf "%s\n%s" "$new_pass" "$old_pass" | "$PWQCHECK_BIN" -2 similar=deny 2>&1)
30+
exit_code=$?
31+
32+
# Check if the result matches expected
33+
if [ $exit_code -eq "$expected_result" ]; then
34+
echo -e "${GREEN}✓ PASS:${NC} $test_name"
35+
else
36+
echo -e "${RED}✗ FAIL:${NC} $test_name"
37+
echo " Expected exit code: $expected_result, Got: $exit_code"
38+
echo " Output: $result"
39+
fi
40+
41+
}
42+
43+
#add a function to test when similar passwords are permitted
44+
test_passwords_permit() {
45+
46+
local new_pass="$1"
47+
local old_pass="$2"
48+
local expected_result="$3"
49+
local test_name="$4"
50+
51+
# Run pwqcheck with both passwords and --similar=permit option
52+
result=$(printf "%s\n%s" "$new_pass" "$old_pass" | "$PWQCHECK_BIN" -2 similar=permit 2>&1)
53+
exit_code=$?
54+
55+
# Check if the result matches expected
56+
if [ $exit_code -eq "$expected_result" ]; then
57+
echo -e "${GREEN}✓ PASS:${NC} $test_name"
58+
((TESTS_PASSED++))
59+
else
60+
echo -e "${RED}✗ FAIL:${NC} $test_name"
61+
echo " Expected exit code: $expected_result, Got: $exit_code"
62+
echo " Output: $result"
63+
fi
64+
}
65+
66+
# Main testing section
67+
echo "Running pwqcheck similarity tests with --similar=deny..."
68+
echo "===================================================="
69+
70+
# Test 1: Identical passwords (should fail with deny)
71+
test_passwords "ComplexPass123!" "ComplexPass123!" 1 \
72+
"Identical passwords should be rejected when similarity is denied"
73+
74+
# Test 2: Case variation (should fail with deny)
75+
test_passwords "ComplexPass123!" "complexpass123!" 1 \
76+
"Case variations should be rejected when similarity is denied"
77+
78+
# Test 3: Number substitution (should fail with deny)
79+
test_passwords "P@ssw0rd123!" "Password123!" 1 \
80+
"Common number substitutions should be rejected when similarity is denied"
81+
82+
# Test 4: Different passwords (should pass even with deny)
83+
test_passwords "ComplexPass123!" "TotallyDifferent456@" 0 \
84+
"Different passwords should be accepted even when similarity is denied"
85+
86+
echo
87+
echo "Running pwqcheck similarity tests with --similar=permit..."
88+
echo "====================================================="
89+
90+
# Test 5: Identical passwords (should pass with permit)
91+
test_passwords_permit "VeryComplexPass#789" "VeryComplexPass#789!" 0 \
92+
"Identical passwords should be accepted when similarity is permitted"
93+
94+
# Test 6: Case variation (should pass with permit)
95+
test_passwords_permit "ComplexPass123!" "complexpass123!" 0 \
96+
"Case variations should be accepted when similarity is permitted"
97+
98+
# Test 7: Number substitution (should pass with permit)
99+
test_passwords_permit "P@ssw0rd123!" "Password123!" 0 \
100+
"Common number substitutions should be accepted when similarity is permitted"
101+
102+
# Test 8: Different passwords (should pass with permit)
103+
test_passwords_permit "ComplexPass123!" "TotallyDifferent456@" 0 \
104+
"Different passwords should be accepted when similarity is permitted"

0 commit comments

Comments
 (0)