Skip to content

Commit 59b6087

Browse files
committed
fix: report clamdscan file count
1 parent 87aef08 commit 59b6087

2 files changed

Lines changed: 85 additions & 3 deletions

File tree

scripts/virus-scan

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ fi
4444

4545
clamd_config="${runtime_dir}/clamd.conf"
4646
clamd_log="${runtime_dir}/clamd.log"
47+
scan_report="${runtime_dir}/clamdscan.report"
4748
clamd_pid=""
4849

4950
cleanup() {
@@ -100,10 +101,35 @@ fi
100101
clamdscan \
101102
--config-file="${clamd_config}" \
102103
--multiscan \
103-
--infected \
104-
"${PWD}"
104+
"${PWD}" > "${scan_report}" 2>&1
105105
virus_status=$?
106106

107+
scanned_files="$(
108+
awk '
109+
/: OK$/ || / FOUND$/ { count++ }
110+
END { print count + 0 }
111+
' "${scan_report}"
112+
)"
113+
114+
awk -v scanned_files="${scanned_files}" '
115+
/^----------- SCAN SUMMARY -----------$/ {
116+
summary_printed = 1
117+
print
118+
print "Scanned files: " scanned_files
119+
next
120+
}
121+
122+
/: OK$/ { next }
123+
124+
{ print }
125+
126+
END {
127+
if (!summary_printed) {
128+
print "Scanned files: " scanned_files
129+
}
130+
}
131+
' "${scan_report}"
132+
107133
echo "-------"
108134
echo ""
109135

tests/test-virus-scan.sh

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,16 @@ assert_file_contains() {
4242
fi
4343
}
4444

45+
assert_file_not_contains() {
46+
local file="$1"
47+
local pattern="$2"
48+
local description="$3"
49+
50+
if [[ ! -f "${file}" ]] || grep -Eq -- "${pattern}" "${file}"; then
51+
fail "${description}"
52+
fi
53+
}
54+
4555
create_fake_binaries() {
4656
local bin_dir="$1"
4757

@@ -93,6 +103,39 @@ for argument in "$@"; do
93103
fi
94104
done
95105
106+
case "${FAKE_SCAN_EXIT:-0}" in
107+
0)
108+
printf '%s\n' \
109+
"${TEST_STATE}/work/clean-one.php: OK" \
110+
"${TEST_STATE}/work/clean-two.php: OK" \
111+
'' \
112+
'----------- SCAN SUMMARY -----------' \
113+
'Infected files: 0' \
114+
'Time: 0.010 sec (0 m 0 s)'
115+
;;
116+
1)
117+
printf '%s\n' \
118+
"${TEST_STATE}/work/clean.php: OK" \
119+
"${TEST_STATE}/work/infected.php: Eicar-Test-Signature FOUND" \
120+
'' \
121+
'----------- SCAN SUMMARY -----------' \
122+
'Infected files: 1' \
123+
'Time: 0.010 sec (0 m 0 s)'
124+
;;
125+
2)
126+
printf '%s\n' \
127+
"${TEST_STATE}/work/unreadable.php: File path check failure: Permission denied. ERROR" \
128+
'' \
129+
'----------- SCAN SUMMARY -----------' \
130+
'Infected files: 0' \
131+
'Total errors: 1' \
132+
'Time: 0.010 sec (0 m 0 s)'
133+
;;
134+
3)
135+
printf '%s\n' 'ERROR: Could not connect to clamd.'
136+
;;
137+
esac
138+
96139
exit "${FAKE_SCAN_EXIT:-0}"
97140
EOF
98141

@@ -136,20 +179,33 @@ run_scan_case() {
136179

137180
run_scan_case clean 0 success 0
138181
assert_file_contains "${TEST_DIR}/clean/clamdscan.calls" '--multiscan' 'clean scan requests multiscan'
139-
assert_file_contains "${TEST_DIR}/clean/clamdscan.calls" '--infected' 'clean scan only reports infected files'
182+
assert_file_not_contains "${TEST_DIR}/clean/clamdscan.calls" '--infected' 'clean scan captures clean results for counting'
140183
assert_file_contains "${TEST_DIR}/clean/clamd.conf" 'ExcludePath .*\\.composer-cache' 'composer cache exclusion is configured for clamd'
141184
assert_file_contains "${TEST_DIR}/clean/clamd.conf" 'ExcludePath .*node_modules_cache' 'node modules cache exclusion is configured for clamd'
185+
assert_file_contains "${TEST_DIR}/clean/output" 'Scanned files: 2' 'clean scan reports the scanned file count'
186+
assert_file_not_contains "${TEST_DIR}/clean/output" 'clean-(one|two)\.php: OK' 'clean scan suppresses clean file paths'
142187
assert_file_contains "${TEST_DIR}/clean/output" 'Clean - no viruses found' 'clean scan reports success'
143188
[[ -f "${TEST_DIR}/clean/clamd.stopped" ]] || fail 'clean scan stops the temporary daemon'
144189

145190
run_scan_case infected 1 success 1
191+
assert_file_contains "${TEST_DIR}/infected/output" 'Scanned files: 2' 'infected scan reports the scanned file count'
192+
assert_file_contains "${TEST_DIR}/infected/output" 'infected\.php: Eicar-Test-Signature FOUND' 'infected scan reports the infected file'
193+
assert_file_not_contains "${TEST_DIR}/infected/output" 'clean\.php: OK' 'infected scan suppresses clean file paths'
146194
assert_file_contains "${TEST_DIR}/infected/output" 'INFECTED FILE FOUND' 'infected scan reports malware'
147195
[[ -f "${TEST_DIR}/infected/clamd.stopped" ]] || fail 'infected scan stops the temporary daemon'
148196

149197
run_scan_case scan_error 2 success 0
198+
assert_file_contains "${TEST_DIR}/scan_error/output" 'Scanned files: 0' 'scan errors report zero successful file scans'
199+
assert_file_contains "${TEST_DIR}/scan_error/output" 'unreadable\.php: .* ERROR' 'scan errors remain visible'
150200
assert_file_contains "${TEST_DIR}/scan_error/output" 'Virus scanner internal error' 'scan errors remain fail-open'
151201
[[ -f "${TEST_DIR}/scan_error/clamd.stopped" ]] || fail 'scan errors stop the temporary daemon'
152202

203+
run_scan_case scan_error_without_summary 3 success 0
204+
assert_file_contains "${TEST_DIR}/scan_error_without_summary/output" 'Scanned files: 0' 'scan errors without a summary report zero successful file scans'
205+
assert_file_contains "${TEST_DIR}/scan_error_without_summary/output" 'ERROR: Could not connect to clamd' 'scan errors without a summary remain visible'
206+
assert_file_contains "${TEST_DIR}/scan_error_without_summary/output" 'Virus scanner internal error' 'scan errors without a summary remain fail-open'
207+
[[ -f "${TEST_DIR}/scan_error_without_summary/clamd.stopped" ]] || fail 'scan errors without a summary stop the temporary daemon'
208+
153209
run_scan_case startup_error 0 failure 0
154210
assert_file_contains "${TEST_DIR}/startup_error/output" 'Virus scanner internal error' 'daemon startup errors remain fail-open'
155211

0 commit comments

Comments
 (0)