|
| 1 | +#!/usr/bin/env bash |
| 2 | + |
| 3 | +set -uo pipefail |
| 4 | + |
| 5 | +TEST_DIR="$(mktemp -d "/private/tmp/virus-scan-tests.XXXXXX")" |
| 6 | +readonly TEST_DIR |
| 7 | +REPOSITORY_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)" |
| 8 | +readonly REPOSITORY_DIR |
| 9 | +readonly SCRIPT_PATH="${REPOSITORY_DIR}/scripts/virus-scan" |
| 10 | + |
| 11 | +failures=0 |
| 12 | + |
| 13 | +cleanup() { |
| 14 | + if [[ -d "${TEST_DIR}" ]]; then |
| 15 | + rm -r -- "${TEST_DIR}" |
| 16 | + fi |
| 17 | +} |
| 18 | +trap cleanup EXIT |
| 19 | + |
| 20 | +fail() { |
| 21 | + printf 'not ok - %s\n' "$1" >&2 |
| 22 | + failures=$((failures + 1)) |
| 23 | +} |
| 24 | + |
| 25 | +assert_status() { |
| 26 | + local expected="$1" |
| 27 | + local actual="$2" |
| 28 | + local description="$3" |
| 29 | + |
| 30 | + if [[ "${actual}" -ne "${expected}" ]]; then |
| 31 | + fail "${description}: expected exit ${expected}, got ${actual}" |
| 32 | + fi |
| 33 | +} |
| 34 | + |
| 35 | +assert_file_contains() { |
| 36 | + local file="$1" |
| 37 | + local pattern="$2" |
| 38 | + local description="$3" |
| 39 | + |
| 40 | + if [[ ! -f "${file}" ]] || ! grep -Eq -- "${pattern}" "${file}"; then |
| 41 | + fail "${description}" |
| 42 | + fi |
| 43 | +} |
| 44 | + |
| 45 | +create_fake_binaries() { |
| 46 | + local bin_dir="$1" |
| 47 | + |
| 48 | + mkdir -p "${bin_dir}" |
| 49 | + |
| 50 | + cat > "${bin_dir}/clamd" <<'EOF' |
| 51 | +#!/usr/bin/env bash |
| 52 | +set -u |
| 53 | +
|
| 54 | +if [[ "${FAKE_START_MODE:-success}" == "failure" ]]; then |
| 55 | + exit 2 |
| 56 | +fi |
| 57 | +
|
| 58 | +config_file="" |
| 59 | +while [[ $# -gt 0 ]]; do |
| 60 | + case "$1" in |
| 61 | + -c|--config-file) |
| 62 | + config_file="$2" |
| 63 | + shift 2 |
| 64 | + ;; |
| 65 | + *) |
| 66 | + shift |
| 67 | + ;; |
| 68 | + esac |
| 69 | +done |
| 70 | +
|
| 71 | +cp "${config_file}" "${TEST_STATE}/clamd.conf" |
| 72 | +printf '%s\n' "$$" > "${TEST_STATE}/clamd.pid" |
| 73 | +trap 'touch "${TEST_STATE}/clamd.stopped"; exit 0' TERM INT |
| 74 | +while :; do |
| 75 | + sleep 0.05 |
| 76 | +done |
| 77 | +EOF |
| 78 | + |
| 79 | + cat > "${bin_dir}/clamdscan" <<'EOF' |
| 80 | +#!/usr/bin/env bash |
| 81 | +set -u |
| 82 | +
|
| 83 | +printf '%q ' "$@" >> "${TEST_STATE}/clamdscan.calls" |
| 84 | +printf '\n' >> "${TEST_STATE}/clamdscan.calls" |
| 85 | +
|
| 86 | +for argument in "$@"; do |
| 87 | + if [[ "${argument}" == --ping* ]]; then |
| 88 | + for _ in {1..50}; do |
| 89 | + [[ -f "${TEST_STATE}/clamd.pid" ]] && exit 0 |
| 90 | + sleep 0.01 |
| 91 | + done |
| 92 | + exit 2 |
| 93 | + fi |
| 94 | +done |
| 95 | +
|
| 96 | +exit "${FAKE_SCAN_EXIT:-0}" |
| 97 | +EOF |
| 98 | + |
| 99 | + chmod +x "${bin_dir}/clamd" "${bin_dir}/clamdscan" |
| 100 | +} |
| 101 | + |
| 102 | +run_scan_case() { |
| 103 | + local name="$1" |
| 104 | + local scan_exit="$2" |
| 105 | + local start_mode="$3" |
| 106 | + local expected_exit="$4" |
| 107 | + local state_dir="${TEST_DIR}/${name}" |
| 108 | + local bin_dir="${state_dir}/bin" |
| 109 | + local work_dir="${state_dir}/work" |
| 110 | + local output_file="${state_dir}/output" |
| 111 | + local actual_exit |
| 112 | + |
| 113 | + mkdir -p \ |
| 114 | + "${state_dir}/database" \ |
| 115 | + "${state_dir}/tmp" \ |
| 116 | + "${work_dir}/.composer-cache" \ |
| 117 | + "${work_dir}/node_modules_cache" |
| 118 | + create_fake_binaries "${bin_dir}" |
| 119 | + |
| 120 | + set +e |
| 121 | + ( |
| 122 | + cd "${work_dir}" || exit 99 |
| 123 | + PATH="${bin_dir}:${PATH}" \ |
| 124 | + TEST_STATE="${state_dir}" \ |
| 125 | + TMPDIR="${state_dir}/tmp" \ |
| 126 | + CLAMAV_DB_DIR="${state_dir}/database" \ |
| 127 | + FAKE_SCAN_EXIT="${scan_exit}" \ |
| 128 | + FAKE_START_MODE="${start_mode}" \ |
| 129 | + bash "${SCRIPT_PATH}" |
| 130 | + ) > "${output_file}" 2>&1 |
| 131 | + actual_exit=$? |
| 132 | + set -e |
| 133 | + |
| 134 | + assert_status "${expected_exit}" "${actual_exit}" "${name}" |
| 135 | +} |
| 136 | + |
| 137 | +run_scan_case clean 0 success 0 |
| 138 | +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' |
| 140 | +assert_file_contains "${TEST_DIR}/clean/clamd.conf" 'ExcludePath .*\\.composer-cache' 'composer cache exclusion is configured for clamd' |
| 141 | +assert_file_contains "${TEST_DIR}/clean/clamd.conf" 'ExcludePath .*node_modules_cache' 'node modules cache exclusion is configured for clamd' |
| 142 | +assert_file_contains "${TEST_DIR}/clean/output" 'Clean - no viruses found' 'clean scan reports success' |
| 143 | +[[ -f "${TEST_DIR}/clean/clamd.stopped" ]] || fail 'clean scan stops the temporary daemon' |
| 144 | + |
| 145 | +run_scan_case infected 1 success 1 |
| 146 | +assert_file_contains "${TEST_DIR}/infected/output" 'INFECTED FILE FOUND' 'infected scan reports malware' |
| 147 | +[[ -f "${TEST_DIR}/infected/clamd.stopped" ]] || fail 'infected scan stops the temporary daemon' |
| 148 | + |
| 149 | +run_scan_case scan_error 2 success 0 |
| 150 | +assert_file_contains "${TEST_DIR}/scan_error/output" 'Virus scanner internal error' 'scan errors remain fail-open' |
| 151 | +[[ -f "${TEST_DIR}/scan_error/clamd.stopped" ]] || fail 'scan errors stop the temporary daemon' |
| 152 | + |
| 153 | +run_scan_case startup_error 0 failure 0 |
| 154 | +assert_file_contains "${TEST_DIR}/startup_error/output" 'Virus scanner internal error' 'daemon startup errors remain fail-open' |
| 155 | + |
| 156 | +assert_file_contains "${REPOSITORY_DIR}/Dockerfile" 'clamav-daemon' 'container installs the ClamAV daemon package' |
| 157 | + |
| 158 | +if [[ "${failures}" -gt 0 ]]; then |
| 159 | + printf '%s test assertion(s) failed\n' "${failures}" >&2 |
| 160 | + exit 1 |
| 161 | +fi |
| 162 | + |
| 163 | +printf 'ok - virus-scan behavior\n' |
0 commit comments