Skip to content

Commit 0410d11

Browse files
Zaiba-Ssolardiz
authored andcommitted
Add tests for mode option of pwqfilter
Signed-off-by: Zaiba Sanglikar <[email protected]>
1 parent 6bd321a commit 0410d11

File tree

1 file changed

+161
-0
lines changed

1 file changed

+161
-0
lines changed

tests/test-pwqfilter-mode.sh

Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
#!/bin/sh
2+
#
3+
# Copyright (c) 2025 by Zaiba Sanglikar. See LICENSE.
4+
#
5+
# This script tests the password filter utility pwqfilter for
6+
# mode options lookup ,insert,status,create,test-fp-rate
7+
8+
if [ -t 1 ]; then
9+
# Colors for better visibility
10+
GREEN="$(printf "\033[0;32m")"
11+
RED="$(printf "\033[0;31m")"
12+
NC="$(printf "\033[0m")"
13+
else
14+
GREEN=''
15+
RED=''
16+
NC=''
17+
fi
18+
19+
20+
# Test function for pwqfilter operations
21+
test_pwqfilter_operation() {
22+
params="$1"
23+
expected="$2"
24+
description="$3"
25+
input="$4"
26+
27+
PWQFILTER_BIN="$(dirname "$0")/../pwqfilter"
28+
29+
printf "Testing %s: " "$description"
30+
if [ -z "$input" ]; then
31+
result=$($PWQFILTER_BIN $params 2>&1)
32+
else
33+
result=$(echo -e "$input" | $PWQFILTER_BIN $params 2>&1)
34+
35+
fi
36+
exit_code=$?
37+
38+
if [ "$expected" = "pass" -a "$exit_code" -eq 0 ] || \
39+
[ "$expected" = "fail" -a "$exit_code" -ne 0 ]; then
40+
printf "%sPASS%s\n" "$GREEN" "$NC"
41+
printf " Result: %s\n\n" "$result"
42+
return 0
43+
else
44+
printf "%sFAIL%s\n" "$RED" "$NC"
45+
printf " Parameters: %s\n" "${params:-'(none)'}"
46+
printf " Input: %s\n" "${input:-'(none)'}"
47+
printf " Expected: %s\n" "$expected"
48+
printf " Got exit code: %d\n" "$exit_code"
49+
printf " Output: %s\n\n" "$result"
50+
return 1
51+
fi
52+
}
53+
54+
# Function to test lookup functionality
55+
test_lookup() {
56+
filter_file="$1"
57+
input="$2"
58+
expected_result="$3"
59+
description="$4"
60+
61+
PWQFILTER_BIN="$(dirname "$0")/../pwqfilter"
62+
printf "Testing %s: " "$description"
63+
64+
# Perform lookup
65+
result=$(echo "$input" | $PWQFILTER_BIN --filter="$filter_file" --lookup 2>&1)
66+
exit_code=$?
67+
68+
# For "found" we expect exit code 0 and output matching input
69+
# For "not found" we expect no output (or empty string)
70+
if [ "$expected_result" = "pass" ] && [ "$exit_code" -eq 0 ] && [ "$result" = "$input" ]; then
71+
printf "%sPASS%s\n" "$GREEN" "$NC"
72+
printf " Input '%s' was found in filter as expected\n\n" "$input"
73+
printf " Result: %s\n\n" "$result"
74+
return 0
75+
elif [ "$expected_result" = "fail" ] && [ -z "$result" ]; then
76+
printf "%sPASS%s\n" "$GREEN" "$NC"
77+
printf " Input '%s' was not found in filter as expected\n\n" "$input"
78+
printf " Result: %s\n\n" "$result"
79+
return 0
80+
else
81+
printf "%sFAIL%s\n" "$RED" "$NC"
82+
printf " Filter file: %s\n" "$filter_file"
83+
printf " Input: %s\n" "$input"
84+
printf " Expected: %s\n" "$expected_result"
85+
printf " Got: %s\n" "$result"
86+
printf " Exit code: %d\n\n" "$exit_code"
87+
return 1
88+
fi
89+
}
90+
91+
# Create temporary directory
92+
tmp_dir=$(mktemp -d)
93+
trap 'rm -rf "${tmp_dir}"' EXIT
94+
95+
# Create password lists for testing
96+
cat > "${tmp_dir}/passwords.txt" << EOF
97+
password123
98+
qwerty
99+
letmein
100+
admin123
101+
welcome
102+
12345678
103+
EOF
104+
105+
106+
printf "\nRunning PWQFilter Tests...\n"
107+
printf "============================="
108+
109+
# Test Suite 1: Basic Mode Options
110+
printf "\nTesting Basic Mode Options:\n"
111+
112+
# Test --create mode
113+
test_pwqfilter_operation "--create=10 --output=${tmp_dir}/basic.filter" "pass" "Create filter with --create" "$(cat ${tmp_dir}/passwords.txt)"
114+
115+
# Test --status mode
116+
test_pwqfilter_operation "--status --filter=${tmp_dir}/basic.filter" "pass" "Check filter status with --status"
117+
118+
# Test --lookup mode (default)
119+
test_pwqfilter_operation "--filter=${tmp_dir}/basic.filter" "pass" "Look up passwords with --lookup (default)" "password123"
120+
121+
# Test nonexistent passwords
122+
test_pwqfilter_operation "--filter=${tmp_dir}/basic.filter" "fail" "Look up nonexistent passwords" "nonexistent123"
123+
124+
# Test --insert mode
125+
test_pwqfilter_operation "--insert --filter=${tmp_dir}/basic.filter --output=${tmp_dir}/updated.filter" "pass" "Insert entries with --insert" "newpassword123"
126+
127+
# Test --test-fp-rate mode
128+
test_pwqfilter_operation "--test-fp-rate --filter=${tmp_dir}/updated.filter" "pass" "Test false positive rate with --test-fp-rate"
129+
130+
131+
# Test Suite 2: Specific Lookup Tests
132+
printf "\nTesting Specific Lookup Functionality:\n"
133+
134+
# Create a filter with known passwords
135+
echo -e "\ntestpassword\nsecret123\nadmin123\nsolid" > "${tmp_dir}/known_passwords.txt"
136+
test_pwqfilter_operation "--create=10 --output=${tmp_dir}/lookup_test.filter" "pass" "Create filter for lookup testing" "$(cat ${tmp_dir}/known_passwords.txt)"
137+
138+
# Test lookup for passwords in the filter
139+
test_lookup "${tmp_dir}/lookup_test.filter" "testpassword" "pass" "Password in filter should be found"
140+
test_lookup "${tmp_dir}/lookup_test.filter" "admin123" "pass" "Password in filter should be found"
141+
test_lookup "${tmp_dir}/lookup_test.filter" "solid" "pass" "Password in filter should be found"
142+
143+
# Test lookup for passwords not in the filter
144+
test_lookup "${tmp_dir}/lookup_test.filter" "unknown123" "fail" "Password not in filter should not be found"
145+
test_lookup "${tmp_dir}/lookup_test.filter" "differentpassword" "fail" "Password not in filter should not be found"
146+
147+
# Test Suite 3: Combined Operations
148+
printf "\nTesting Combined Operations:\n"
149+
150+
# Test create + test-fp-rate
151+
test_pwqfilter_operation "--create=30 --output=${tmp_dir}/combined.filter --test-fp-rate" "pass" "Create filter and test FP rate in one command" "$(cat ${tmp_dir}/passwords.txt)"
152+
153+
# Test lookup + status
154+
test_pwqfilter_operation "--filter=${tmp_dir}/combined.filter --status" "pass" "Look up and check status" "password123"
155+
156+
# Test invalid combinations
157+
test_pwqfilter_operation "--create=10 --insert" "fail" "Invalid combination: create + insert" "test"
158+
159+
# Print summary
160+
printf "\nPWQFilter mode options tests completed.\n"
161+
printf "Check the results above to see which tests passed or failed.\n"

0 commit comments

Comments
 (0)