unit_tests/check_clamd.sh
7f0d1148
 #!/bin/sh 
caebe685
 #set -x
 killclamd() {
5ef337bf
 	test -f /tmp/clamd-test.pid || return
1367e121
 	pid=`cat /tmp/clamd-test.pid`
5ef337bf
 	kill -0 $pid && kill $pid
caebe685
 	pippo=0
 	while test -f /tmp/clamd-test.pid; do
5ef337bf
 		sleep 1
1367e121
 		pippo=`expr $pippo + 1`
caebe685
 		if test $pippo -gt 9; then
 			kill -KILL $pid
 			rm /tmp/clamd-test.pid
 		fi
 	done
 }
2a75d710
 die() {
caebe685
 	killclamd
dfc0c031
 	rm -rf test-db test-clamd-viraction.conf test-clamd.log	test-clamd-heur-pred.conf clamd-test.socket
232c4b70
 	exit $1
 }
7f0d1148
 run_clamd_test() {
 	conf_file=$1
 	shift
 	rm -f clamdscan.log
 	../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 --quiet --config-file $conf_file $* --log=clamdscan.log
 	if test $? = 2; then 
 		echo "Failed to run clamdscan!" >&2;
 		die 3;	
 	fi
caebe685
 	killclamd
7f0d1148
 }
232c4b70
 
c1a133a0
 run_clamd_fdpass_test() {
 	conf_file=$1
 	shift
 	rm -f clamdscan.log
 	../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;
 	fi
caebe685
 	killclamd
c1a133a0
 }
 
232c4b70
 mkdir -p test-db
 cat <<EOF >test-db/test.hdb
 aa15bcf478d165efd2065190eb473bcb:544:ClamAV-Test-File
 EOF
7f0d1148
 cp $srcdir/input/daily.ftm test-db/
 cp $srcdir/input/daily.pdb test-db/
232c4b70
 
7f0d1148
 # Test that all testfiles are detected
232c4b70
 FILES=../test/clam*
7f0d1148
 run_clamd_test $srcdir/test-clamd.conf $FILES
232c4b70
 NFILES=`ls -1 $FILES | wc -l`
073a33e1
 NINFECTED=`grep "Infected files" clamdscan.log | cut -f2 -d:|sed -e 's/ //g'`
 if test "$NFILES" -ne "0$NINFECTED"; then
232c4b70
 	echo "clamd did not detect all testfiles correctly!" >&2;
 	grep OK clamdscan.log >&2;
 	die 4;
 fi
7f0d1148
 
 # Test VirusEvent feature
b6927a7c
 cat <$srcdir/test-clamd.conf >test-clamd-viraction.conf
751f8470
 echo "VirusEvent `pwd`/$srcdir/virusaction-test.sh `pwd` \"Virus found: %v\"" >>test-clamd-viraction.conf
 rm -f test-clamd.log
7f0d1148
 run_clamd_test test-clamd-viraction.conf ../test/clam.exe
dfc0c031
 grep "Virus found: ClamAV-Test-File.UNOFFICIAL" test-clamd.log >/dev/null 2>/dev/null; 
 if test ! $? ; then
 	echo "Virusaction test failed!" 
751f8470
 	cat test-clamd.log
7f0d1148
 	die 5;
 fi
 
 # Test HeuristicScanPrecedence feature
b6927a7c
 cat <$srcdir/test-clamd.conf >test-clamd-heur-pred.conf
7f0d1148
 run_clamd_test test-clamd-heur-pred.conf clam-phish-exe
dfc0c031
 grep "ClamAV-Test-File" clamdscan.log >/dev/null 2>/dev/null;
 if test ! $?; then
7f0d1148
 	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
dfc0c031
 grep "Phishing.Heuristics.Email.SpoofedDomain" clamdscan.log >/dev/null 2>/dev/null;
 if test ! $?; then
7f0d1148
 	echo "HeuristicScanPrecedence on test failed!" >&2;
 	cat clamdscan.log;
 	die 6;
751f8470
 fi
c1a133a0
 
 if grep "^#define HAVE_FD_PASSING 1" ../clamav-config.h >/dev/null; then
 	run_clamd_fdpass_test $srcdir/test-clamd.conf ../test/clam.exe
 	grep "ClamAV-Test-File" clamdscan.log >/dev/null 2>/dev/null;
 	if test ! $?; then
 		echo "FDpassing test failed!" >&2;
 		cat clamdscan.log;
 		die 7;
 	fi
 else
 	echo "No FD passing support, skipping test"
 fi
232c4b70
 die 0;