|
| 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 |
0 commit comments