Skip to content

Commit 0b15f19

Browse files
committed
Include tests to test pwqcheck
Signed-off-by: Zaiba Sanglikar <zaiba.sanglikar@toshiba-tsip.com>
1 parent ed738c9 commit 0b15f19

File tree

5 files changed

+354
-0
lines changed

5 files changed

+354
-0
lines changed

Makefile

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,17 @@ remove_lib_wrapped:
307307
remove_locales_wrapped:
308308
for f in $(LANGUAGES); do $(RM) $(DESTDIR)$(LOCALEDIR)/$$f/LC_MESSAGES/$(PACKAGE).mo; done
309309

310+
test:
311+
@echo "Running tests..."
312+
# Set LD_LIBRARY_PATH to include the current directory
313+
@export LDFLAGS="-Wl,-rpath,$(CURDIR)"; \
314+
export LD_LIBRARY_PATH=$(CURDIR):$$LD_LIBRARY_PATH; \
315+
for test in tests/*.sh; do \
316+
echo "Executing $$test..."; \
317+
bash $$test || echo "$$test failed"; \
318+
done
319+
@echo "Tests completed!"
320+
310321
clean:
311322
$(RM) $(PROJ) $(POTFILE) $(MOFILES) *.o
312323

tests/test-pwqcheck-basic.sh

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

tests/test-pwqcheck-length.sh

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

tests/test-pwqcheck-multi.sh

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

tests/test-pwqcheck-similarity.sh

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

0 commit comments

Comments
 (0)