diff --git a/unit_tests/check_clamd.sh b/unit_tests/check_clamd.sh index f42cc6d43..1ff2819bc 100755 --- a/unit_tests/check_clamd.sh +++ b/unit_tests/check_clamd.sh @@ -10,49 +10,105 @@ killclamd() { fi kill -0 $pid 2>/dev/null || return kill $pid - timeout=10 - (sleep $timeout && kill -0 $pid 2>/dev/null && kill -KILL $pid) & - sleeperpid=$! - kill -0 $pid 2>/dev/null && wait $pid - kill $sleeperpid 2>/dev/null + pippo=0 + while kill -0 $pid 2>/dev/null; do + sleep 1 + pippo=$((pippo+1)) + if test $pippo -gt 9; then + kill -KILL $pid + fi + done rm -f /tmp/clamd-test.pid } + die() { killclamd - rm -rf test-db test-clamd-viraction.conf test-clamd.log test-clamd-heur-pred.conf clamd-test.socket + rm -rf test-db test-clamd1.conf test-clamd2.conf test-clamd.log clamd-test.socket reload-testfile exit $1 } -run_clamd_test() { + +error() +{ + echo >&2 + echo "***" >&2 + echo "*** $1" >&2 + echo "***" >&2 +} + +start_clamd() +{ + rm -f /tmp/clamd-test.log + ../libtool --mode=execute $CLAMD_WRAPPER ../clamd/clamd -c $1 || + { error "Failed to start clamd!"; die 1;} +} + +run_clamdscan() { conf_file=$1 shift rm -f clamdscan.log clamdscan-multiscan.log - ../libtool --mode=execute $CLAMD_WRAPPER ../clamd/clamd -c $conf_file || { echo "Failed to start clamd!" >&2; die 1;} - ../clamdscan/clamdscan --version --config-file $conf_file 2>&1|grep "^ClamAV" >/dev/null || { echo "clamdscan can't get version of clamd!" >&2; die 2;} + ../clamdscan/clamdscan --version --config-file $conf_file 2>&1|grep "^ClamAV" >/dev/null || + { error "clamdscan can't get version of clamd!"; die 2;} ../clamdscan/clamdscan --quiet --config-file $conf_file $* --log=clamdscan.log - ../clamdscan/clamdscan --quiet --config-file $conf_file $* -m --log=clamdscan-multiscan.log - if test -x /bin/nc; then - echo RELOAD | nc -q 0 -n 127.0.0.1 3311 + if test $? = 2; then + error "Failed to run clamdscan!" + cat clamdscan.log + die 3; fi + ../clamdscan/clamdscan --quiet --config-file $conf_file $* -m --log=clamdscan-multiscan.log if test $? = 2; then - echo "Failed to run clamdscan!" >&2; + error "Failed to run clamdscan (multiscan)!" die 3; fi - killclamd } -run_clamd_fdpass_test() { +run_reload_test() +{ + # TODO consider using clamdscan when it'll have a reload feature + if test ! -x /bin/nc; then + echo "*** Netcat (nc) is not installed, skipping reload test" + return + fi + rm -f reload-testfile + echo "ClamAV-RELOAD-Test" >reload-testfile + run_clamdscan test-clamd1.conf reload-testfile + grep "ClamAV-RELOAD-TestFile" clamdscan.log >/dev/null 2>/dev/null; + if test $? -eq 0; then + # it is not supposed to detect until we actually put the + # signature there and reload! + error "RELOAD test failed!" + cat clamdscan.log + die 7; + fi + echo "ClamAV-RELOAD-TestFile:0:0:436c616d41562d52454c4f41442d54657374" >test-db/new.ndb + echo RELOAD | nc -q 0 -n 127.0.0.1 3311 + run_clamdscan test-clamd1.conf reload-testfile + grep "ClamAV-RELOAD-TestFile" clamdscan.log >/dev/null 2>/dev/null; + if test $? -ne 0; then + error "RELOAD test failed! (after reload)" + cat clamdscan.log + die 8; + fi + grep "ClamAV-RELOAD-TestFile" clamdscan-multiscan.log >/dev/null 2>/dev/null; + if test $? -ne 0; then + error "RELOAD test failed! (after reload, multiscan)" + die 9; + fi + rm -f reload-testfile +} + +run_clamdscan_fdpass() { conf_file=$1 shift rm -f clamdscan.log - ../libtool --mode=execute $CLAMD_WRAPPER ../clamd/clamd -c $conf_file || { echo "Failed to start clamd!" >&2; die 1;} ../clamdscan/clamdscan --quiet --fdpass --config-file $conf_file - <$1 --log=clamdscan.log if test $? = 2; then - echo "Failed to run clamdscan!" >&2; - die 3; + error "Failed to run clamdscan!" + die 9; fi - killclamd } +# Set up test DBdir +rm -rf test-db mkdir -p test-db cat <test-db/test.hdb aa15bcf478d165efd2065190eb473bcb:544:ClamAV-Test-File @@ -60,62 +116,79 @@ EOF cp $srcdir/input/daily.ftm test-db/ cp $srcdir/input/daily.pdb test-db/ +# Prepare for clamd #1 +# Use absolute path to dbdir, so that RELOAD works +cat <$srcdir/test-clamd.conf >test-clamd1.conf +echo "DatabaseDirectory `pwd`/test-db" >>test-clamd1.conf + +# Start clamd #1 +start_clamd test-clamd1.conf + # Test that all testfiles are detected FILES=../test/clam* -run_clamd_test $srcdir/test-clamd.conf $FILES +run_clamdscan test-clamd1.conf $FILES NFILES=`ls -1 $FILES | wc -l` NINFECTED=`grep "Infected files" clamdscan.log | cut -f2 -d:|sed -e 's/ //g'` NINFECTED_MULTI=`grep "Infected files" clamdscan-multiscan.log | cut -f2 -d:|sed -e 's/ //g'` if test "$NFILES" -ne "0$NINFECTED"; then - echo "clamd did not detect all testfiles correctly!" >&2; - grep OK clamdscan.log >&2; + error "clamd did not detect all testfiles correctly!" + grep OK clamdscan.log die 4; fi if test "$NFILES" -ne "0$NINFECTED_MULTI"; then - echo "clamd did not detect all testfiles correctly in multiscan mode!" >&2; - grep OK clamdscan-multiscan.log >&2; - die 4; + error "clamd did not detect all testfiles correctly in multiscan mode!" + grep OK clamdscan-multiscan.log + die 5; +fi + +# Test HeuristicScanPrecedence off feature +run_clamdscan test-clamd1.conf clam-phish-exe +grep "ClamAV-Test-File" clamdscan.log >/dev/null 2>/dev/null; +if test $? -ne 0; then + error "HeuristicScanPrecedence off test failed!" + cat clamdscan.log + die 6; fi +# Test RELOAD command +run_reload_test +killclamd + +# Prepare configuration for clamd #2 +cat test-clamd2.conf +echo "VirusEvent `pwd`/$srcdir/virusaction-test.sh `pwd` \"Virus found: %v\"" >>test-clamd2.conf +echo "HeuristicScanPrecedence yes" >>test-clamd2.conf + +# Start clamd #2 +start_clamd test-clamd2.conf + # Test VirusEvent feature -cat <$srcdir/test-clamd.conf >test-clamd-viraction.conf -echo "VirusEvent `pwd`/$srcdir/virusaction-test.sh `pwd` \"Virus found: %v\"" >>test-clamd-viraction.conf -rm -f test-clamd.log -run_clamd_test test-clamd-viraction.conf ../test/clam.exe +run_clamdscan test-clamd2.conf ../test/clam.exe grep "Virus found: ClamAV-Test-File.UNOFFICIAL" test-clamd.log >/dev/null 2>/dev/null; -if test ! $? ; then - echo "Virusaction test failed!" +if test $? -ne 0; then + error "Virusaction test failed!" cat test-clamd.log - die 5; + die 10; fi # Test HeuristicScanPrecedence feature -cat <$srcdir/test-clamd.conf >test-clamd-heur-pred.conf -run_clamd_test test-clamd-heur-pred.conf clam-phish-exe -grep "ClamAV-Test-File" clamdscan.log >/dev/null 2>/dev/null; -if test ! $?; then - echo "HeuristicScanPrecedence off test failed!" >&2; - cat clamdscan.log; - die 6; -fi -echo "HeuristicScanPrecedence yes" >>test-clamd-heur-pred.conf -run_clamd_test test-clamd-heur-pred.conf clam-phish-exe +run_clamdscan test-clamd2.conf clam-phish-exe grep "Phishing.Heuristics.Email.SpoofedDomain" clamdscan.log >/dev/null 2>/dev/null; -if test ! $?; then - echo "HeuristicScanPrecedence on test failed!" >&2; - cat clamdscan.log; - die 6; +if test $? -ne 0; then + error "HeuristicScanPrecedence on test failed!" + cat clamdscan.log + die 11; fi if grep "^#define HAVE_FD_PASSING 1" ../clamav-config.h >/dev/null; then - run_clamd_fdpass_test $srcdir/test-clamd.conf ../test/clam.exe + run_clamdscan_fdpass test-clamd2.conf ../test/clam.exe grep "ClamAV-Test-File" clamdscan.log >/dev/null 2>/dev/null; - if test ! $?; then - echo "FDpassing test failed!" >&2; + if test $? -ne 0; then + error "FDpassing test failed!" cat clamdscan.log; - die 7; + die 12; fi else - echo "No FD passing support, skipping test" + echo "*** No file descriptor passing support, skipping test" fi die 0; diff --git a/unit_tests/test-clamd.conf b/unit_tests/test-clamd.conf index 375446980..efc40eb00 100644 --- a/unit_tests/test-clamd.conf +++ b/unit_tests/test-clamd.conf @@ -3,7 +3,8 @@ LogTime yes LogClean yes LogVerbose yes PidFile /tmp/clamd-test.pid -DatabaseDirectory test-db +# We need to use absolute path for RELOAD, the script will fill this in +# DatabaseDirectory test-db LocalSocket clamd-test.socket TCPAddr 127.0.0.1 # using different port here to avoid conflicts with system clamd daemon