Debian Codegraph @

test/integration/scripts/integration-test.sh

raw
1 #!/bin/bash
2 # Rspamd Integration Test using rspamc
3 # This script tests fuzzy storage, Bayes learning, and scanning via rspamc
4
5 set -e
6
7 # Disable leak detection for rspamc utility to avoid false positives
8 export ASAN_OPTIONS=detect_leaks=0
9
10 # Configuration
11 RSPAMD_HOST=${RSPAMD_HOST:-localhost}
12 CONTROLLER_PORT=${CONTROLLER_PORT:-50002}
13 PROXY_PORT=${PROXY_PORT:-50004}
14 PASSWORD=${PASSWORD:-q1}
15 PARALLEL=${PARALLEL:-10}
16 TRAIN_RATIO=${TRAIN_RATIO:-0.1}
17 TEST_PROXY=${TEST_PROXY:-true}
18
19 # Directories
20 # When running inside container via stdin, BASH_SOURCE won't work properly
21 if [ -d "/corpus" ]; then
22 # Running inside container
23 CORPUS_DIR="${CORPUS_DIR:-/corpus}"
24 DATA_DIR="${DATA_DIR:-/data}"
25 else
26 # Running on host
27 SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
28 DATA_DIR="$SCRIPT_DIR/../data"
29 CORPUS_DIR="${CORPUS_DIR:-$SCRIPT_DIR/../../functional/messages}"
30 fi
31
32 # Create working directory
33 mkdir -p "$DATA_DIR"
34
35 echo "=== Rspamd Integration Test ==="
36 echo ""
37 echo "Configuration:"
38 echo " Host: $RSPAMD_HOST"
39 echo " Controller port: $CONTROLLER_PORT"
40 echo " Proxy port: $PROXY_PORT"
41 echo " Parallelism: $PARALLEL"
42 echo " Corpus: $CORPUS_DIR"
43 echo ""
44
45 # Check if rspamc is available
46 if ! command -v rspamc &> /dev/null; then
47 echo "ERROR: rspamc not found. Running inside docker container..."
48 exec docker compose exec -T \
49 -e RSPAMD_HOST="$RSPAMD_HOST" \
50 -e CONTROLLER_PORT="$CONTROLLER_PORT" \
51 -e PROXY_PORT="$PROXY_PORT" \
52 -e PASSWORD="$PASSWORD" \
53 -e PARALLEL="$PARALLEL" \
54 -e TRAIN_RATIO="$TRAIN_RATIO" \
55 -e TEST_PROXY="$TEST_PROXY" \
56 -e ASAN_OPTIONS="detect_leaks=0" \
57 rspamd bash -s < "$0"
58 fi
59
60 # Check if Rspamd is running
61 echo "Checking Rspamd status..."
62 if ! rspamc -h "$RSPAMD_HOST:$CONTROLLER_PORT" -P "$PASSWORD" stat &> /dev/null; then
63 echo "ERROR: Cannot connect to Rspamd at $RSPAMD_HOST:$CONTROLLER_PORT"
64 exit 1
65 fi
66 echo "✓ Rspamd is running"
67 echo ""
68
69 # Find all email files, filtering by size (min 200 bytes to have enough tokens)
70 echo "Finding email files in $CORPUS_DIR..."
71 MIN_SIZE=200 # bytes, roughly 11+ tokens for Bayes
72
73 # Find files with minimum size and verify they're readable
74 mapfile -t EMAIL_FILES < <(find "$CORPUS_DIR" -type f -readable -size +${MIN_SIZE}c \( -name "*.eml" -o -name "*.msg" -o -name "*.txt" \))
75 TOTAL_EMAILS=${#EMAIL_FILES[@]}
76
77 if [ $TOTAL_EMAILS -eq 0 ]; then
78 echo "ERROR: No email files found in $CORPUS_DIR (min size: $MIN_SIZE bytes)"
79 exit 1
80 fi
81
82 echo "Found $TOTAL_EMAILS email files (filtered by min size: $MIN_SIZE bytes)"
83 echo ""
84
85 # Calculate split sizes (using bash arithmetic)
86 FUZZY_SIZE=$(awk "BEGIN {printf \"%.0f\", $TOTAL_EMAILS * $TRAIN_RATIO}")
87 BAYES_SIZE=$(awk "BEGIN {printf \"%.0f\", $TOTAL_EMAILS * $TRAIN_RATIO}")
88
89 # Split corpus into lists (no copying needed)
90 echo "Splitting corpus into training sets..."
91 shuf -e "${EMAIL_FILES[@]}" > "$DATA_DIR/shuffled_files.txt"
92
93 # Fuzzy training set
94 head -n "$FUZZY_SIZE" "$DATA_DIR/shuffled_files.txt" > "$DATA_DIR/fuzzy_train_list.txt"
95 FUZZY_COUNT=$(wc -l < "$DATA_DIR/fuzzy_train_list.txt")
96
97 # Bayes training set (spam)
98 tail -n +$((FUZZY_SIZE + 1)) "$DATA_DIR/shuffled_files.txt" | head -n "$BAYES_SIZE" > "$DATA_DIR/bayes_spam_list.txt"
99 SPAM_COUNT=$(wc -l < "$DATA_DIR/bayes_spam_list.txt")
100
101 # Bayes training set (ham)
102 tail -n +$((FUZZY_SIZE + BAYES_SIZE + 1)) "$DATA_DIR/shuffled_files.txt" | head -n "$BAYES_SIZE" > "$DATA_DIR/bayes_ham_list.txt"
103 HAM_COUNT=$(wc -l < "$DATA_DIR/bayes_ham_list.txt")
104
105 echo "Corpus split:"
106 echo " Fuzzy training: $FUZZY_COUNT emails"
107 echo " Bayes SPAM training: $SPAM_COUNT emails"
108 echo " Bayes HAM training: $HAM_COUNT emails"
109 echo " Test set: $TOTAL_EMAILS emails"
110 echo ""
111
112 # Training phase
113 echo "============================================================"
114 echo "TRAINING PHASE"
115 echo "============================================================"
116 echo ""
117
118 # Train fuzzy storage
119 echo "Training Fuzzy storage ($FUZZY_COUNT emails, flag=1)..."
120 if [ $FUZZY_COUNT -gt 0 ]; then
121 while IFS= read -r file; do
122 rspamc -h "$RSPAMD_HOST:$CONTROLLER_PORT" -P "$PASSWORD" \
123 fuzzy_add "$file" -f 1 -w 10
124 done < "$DATA_DIR/fuzzy_train_list.txt" 2>&1 | tee "$DATA_DIR/fuzzy_train.log"
125 echo "✓ Fuzzy training complete"
126 else
127 echo "⚠ No files to train"
128 fi
129 echo ""
130
131 # Train Bayes spam
132 echo "Training Bayes SPAM ($SPAM_COUNT emails)..."
133 if [ $SPAM_COUNT -gt 0 ]; then
134 while IFS= read -r file; do
135 rspamc -h "$RSPAMD_HOST:$CONTROLLER_PORT" -P "$PASSWORD" \
136 learn_spam "$file"
137 done < "$DATA_DIR/bayes_spam_list.txt" 2>&1 | tee "$DATA_DIR/bayes_spam.log"
138 echo "✓ Bayes SPAM training complete"
139 else
140 echo "⚠ No files to train"
141 fi
142 echo ""
143
144 # Train Bayes ham
145 echo "Training Bayes HAM ($HAM_COUNT emails)..."
146 if [ $HAM_COUNT -gt 0 ]; then
147 while IFS= read -r file; do
148 rspamc -h "$RSPAMD_HOST:$CONTROLLER_PORT" -P "$PASSWORD" \
149 learn_ham "$file"
150 done < "$DATA_DIR/bayes_ham_list.txt" 2>&1 | tee "$DATA_DIR/bayes_ham.log"
151 echo "✓ Bayes HAM training complete"
152 else
153 echo "⚠ No files to train"
154 fi
155 echo ""
156
157 # Wait for training to settle
158 echo "Waiting for training to settle..."
159 sleep 5
160 echo ""
161
162 # Scanning phase
163 echo "============================================================"
164 echo "SCANNING PHASE (via controller)"
165 echo "============================================================"
166 echo ""
167
168 echo "Scanning $TOTAL_EMAILS emails (parallelism: $PARALLEL)..."
169 # Scan the same files we used for training (from shuffled list)
170 # Use xargs with -a to read from file and avoid argument list too
171 if xargs -a "$DATA_DIR/shuffled_files.txt" rspamc -h "$RSPAMD_HOST:$CONTROLLER_PORT" \
172 -P "$PASSWORD" -n "$PARALLEL" -j > "$DATA_DIR/results.json" 2> "$DATA_DIR/scan_errors.log"; then
173 echo "✓ Scanning complete"
174 else
175 EXIT_CODE=$?
176 echo "✗ Scanning FAILED with exit code $EXIT_CODE"
177 echo ""
178 echo "=== Scan Error Output ==="
179 cat "$DATA_DIR/scan_errors.log"
180 echo ""
181 echo "=== Partial Results (if any) ==="
182 head -20 "$DATA_DIR/results.json" 2>/dev/null || echo "(no output)"
183 exit 1
184 fi
185 echo ""
186
187 # Analyze results
188 echo "============================================================"
189 echo "ANALYSIS"
190 echo "============================================================"
191 echo ""
192
193 # Count detections using grep and jq (or grep if jq not available)
194 if command -v jq &> /dev/null; then
195 # Use jq for JSON parsing
196 TOTAL=$(jq 'length' "$DATA_DIR/results.json")
197 FUZZY_COUNT=$(jq '[.[] | select(.symbols | keys[] | startswith("FUZZY_"))] | length' "$DATA_DIR/results.json")
198 BAYES_SPAM_COUNT=$(jq '[.[] | select(.symbols.BAYES_SPAM)] | length' "$DATA_DIR/results.json")
199 BAYES_HAM_COUNT=$(jq '[.[] | select(.symbols.BAYES_HAM)] | length' "$DATA_DIR/results.json")
200 else
201 # Fallback to grep
202 TOTAL=$(grep -c '"symbols"' "$DATA_DIR/results.json" || echo 0)
203 FUZZY_COUNT=$(grep -c '"FUZZY_' "$DATA_DIR/results.json" || echo 0)
204 BAYES_SPAM_COUNT=$(grep -c '"BAYES_SPAM"' "$DATA_DIR/results.json" || echo 0)
205 BAYES_HAM_COUNT=$(grep -c '"BAYES_HAM"' "$DATA_DIR/results.json" || echo 0)
206 fi
207
208 if [ "$TOTAL" -eq 0 ]; then
209 echo "ERROR: No valid results"
210 exit 1
211 fi
212
213 # Ensure counts are numeric (default to 0 if empty or non-numeric)
214 FUZZY_COUNT=$(echo "$FUZZY_COUNT" | grep -E '^[0-9]+$' || echo 0)
215 BAYES_SPAM_COUNT=$(echo "$BAYES_SPAM_COUNT" | grep -E '^[0-9]+$' || echo 0)
216 BAYES_HAM_COUNT=$(echo "$BAYES_HAM_COUNT" | grep -E '^[0-9]+$' || echo 0)
217 TOTAL=$(echo "$TOTAL" | grep -E '^[0-9]+$' || echo 0)
218
219 # Calculate percentages using awk (pass variables safely)
220 FUZZY_RATE=$(awk -v count="$FUZZY_COUNT" -v total="$TOTAL" 'BEGIN {printf "%.1f", (count / total) * 100}')
221 BAYES_SPAM_RATE=$(awk -v count="$BAYES_SPAM_COUNT" -v total="$TOTAL" 'BEGIN {printf "%.1f", (count / total) * 100}')
222 BAYES_HAM_RATE=$(awk -v count="$BAYES_HAM_COUNT" -v total="$TOTAL" 'BEGIN {printf "%.1f", (count / total) * 100}')
223
224 echo "Total scanned: $TOTAL"
225 echo "Fuzzy detections: $FUZZY_COUNT ($FUZZY_RATE%)"
226 echo "Bayes SPAM: $BAYES_SPAM_COUNT ($BAYES_SPAM_RATE%)"
227 echo "Bayes HAM: $BAYES_HAM_COUNT ($BAYES_HAM_RATE%)"
228 echo ""
229
230 # Show sample result for debugging
231 if command -v jq &> /dev/null && [ -f "$DATA_DIR/results.json" ]; then
232 echo "Sample scan result (first entry):"
233 jq '.[0] | {symbols: .symbols | keys, score: .score}' "$DATA_DIR/results.json" 2>/dev/null || echo " (cannot parse)"
234 echo ""
235 fi
236
237 # Validation (fuzzy should detect ~10% since we trained on 10%)
238 echo "Validation:"
239 FUZZY_RATE_INT=$(echo "$FUZZY_RATE" | cut -d. -f1)
240 BAYES_SPAM_RATE_INT=$(echo "$BAYES_SPAM_RATE" | cut -d. -f1)
241
242 if [ "$FUZZY_RATE_INT" -ge 5 ] && [ "$FUZZY_RATE_INT" -le 15 ]; then
243 echo " ✓ Fuzzy detection rate: PASS"
244 else
245 echo " ✗ Fuzzy detection rate: FAIL (expected ~10%, got $FUZZY_RATE%)"
246 fi
247
248 if [ "$BAYES_SPAM_RATE_INT" -ge 5 ]; then
249 echo " ✓ Bayes detection: PASS"
250 else
251 echo " ✗ Bayes detection: FAIL (got $BAYES_SPAM_RATE%)"
252 fi
253
254 # Test via proxy if requested
255 if [ "$TEST_PROXY" = "true" ]; then
256 echo ""
257 echo "============================================================"
258 echo "PROXY TEST"
259 echo "============================================================"
260 echo ""
261
262 echo "Testing via proxy worker ($PROXY_PORT)..."
263 # Use same file list approach as controller test to avoid permission issues
264 if xargs -a "$DATA_DIR/shuffled_files.txt" rspamc -h "$RSPAMD_HOST:$PROXY_PORT" \
265 -n "$PARALLEL" -j > "$DATA_DIR/proxy_results.json" 2> "$DATA_DIR/proxy_errors.log"; then
266 echo "✓ Proxy test complete"
267 echo "Results saved to $DATA_DIR/proxy_results.json"
268 else
269 EXIT_CODE=$?
270 echo "✗ Proxy test FAILED with exit code $EXIT_CODE"
271 echo ""
272 echo "=== Proxy Error Output ==="
273 cat "$DATA_DIR/proxy_errors.log"
274 echo ""
275 echo "=== Proxy Results (if any) ==="
276 cat "$DATA_DIR/proxy_results.json" 2>/dev/null || echo "(no output)"
277 exit 1
278 fi
279 fi
280
281 echo ""
282 echo "============================================================"
283 echo "TEST COMPLETE"
284 echo "============================================================"
285 echo ""
286 echo "Results saved to:"
287 echo " - $DATA_DIR/results.json"
288 echo " - $DATA_DIR/fuzzy_train.log"
289 echo " - $DATA_DIR/bayes_spam.log"
290 echo " - $DATA_DIR/bayes_ham.log"