unit_tests/check_clamd.sh
c82d362e
 #!/bin/sh 
b8edbb88
 CLAMD_WRAPPER=${CLAMD_WRAPPER-}
f45111e5
 CLAMD_TEST_UNIQ1=${CLAMD_TEST_UNIQ1-1}
 CLAMD_TEST_UNIQ2=${CLAMD_TEST_UNIQ2-2}
 TOP="../.."
 LTEXEC="$TOP/libtool --mode=execute"
caebe685
 killclamd() {
f45111e5
 	test -f clamd-test.pid || return
 	pid=`cat clamd-test.pid 2>/dev/null`
c82d362e
 	if test "X$pid" = "X"; then
 		# file can be removed between the 'test' and 'cat',
 		# it happened a few times for me
 		return
 	fi
 	kill -0 $pid 2>/dev/null || return
 	kill $pid
346f7a4e
 	pippo=0
 	while kill -0 $pid 2>/dev/null; do
 		sleep 1
650ea26e
 		pippo=`expr $pippo + 1`
346f7a4e
 		if test $pippo -gt 9; then
 			kill -KILL $pid
b82eea8d
 			echo "Clamd didn't quit";
 			rm -f clamd-test.pid
 			exit 4;
346f7a4e
 		fi
 	done
f45111e5
 	rm -f clamd-test.pid
caebe685
 }
346f7a4e
 
f45111e5
 die()
 {
caebe685
 	killclamd
232c4b70
 	exit $1
 }
346f7a4e
 
 error()
 {
 	echo >&2
 	echo "***" >&2
 	echo "*** $1" >&2
 	echo "***" >&2
 }
 
133dd2e9
 scan_failed() {
 	if test "X$unrar_disabled" = "X1" && test `grep -v '\.rar' $1 | grep OK | wc -l` -eq 0
 	then
 		error "UNRAR is disabled, won't be able to detect unrar files!";
 	else
 		error  $2;
 		die 2;
 	fi
 }
 
346f7a4e
 start_clamd()
 {
5fedbe0a
 	rm -f clamd-test.log ../clamd-test1.log ../clamd-test2.log
fb6fe4f5
 	$LTEXEC $CLAMD_WRAPPER $TOP/clamd/clamd -c $1 --help >clamd-test.log 2>&1 || 
 		{ error "Failed to start clamd --help!"; cat clamd-test.log; die 1; }
456e31a3
 	grep "Clam AntiVirus Daemon" clamd-test.log >/dev/null ||
 		{ error "Wrong --help reply from clamd!"; die 1; }
fb6fe4f5
 	$LTEXEC $CLAMD_WRAPPER $TOP/clamd/clamd -c $1 >clamd-test.log 2>&1 || 
 		{ error "Failed to start clamd!"; cat clamd-test.log; die 1; }
346f7a4e
 }
 
3e2b5046
 run_clamdscan_fileonly() {
b8edbb88
 	rm -f clamdscan.log clamdscan-multiscan.log
6a791429
 	$TOP/clamdscan/clamdscan --version --config-file=test-clamd.conf 2>&1|grep "^ClamAV" >/dev/null || 
f45111e5
 		{ error "clamdscan can't get version of clamd!"; die 1;}
6a791429
 	$TOP/clamdscan/clamdscan --quiet --config-file=test-clamd.conf $* --log=clamdscan.log
346f7a4e
 	if test $? = 2; then 
 		error "Failed to run clamdscan!"
 		cat clamdscan.log
f45111e5
 		die 1
b8edbb88
 	fi
6a791429
 	$TOP/clamdscan/clamdscan --quiet --config-file=test-clamd.conf $* -m --log=clamdscan-multiscan.log
7f0d1148
 	if test $? = 2; then 
346f7a4e
 		error "Failed to run clamdscan (multiscan)!"
f45111e5
 		cat clamdscan-multiscan.log
 		die 1
7f0d1148
 	fi
 }
232c4b70
 
3e2b5046
 run_clamdscan() {
 	run_clamdscan_fileonly $*
 	rm -f clamdscan-fdpass.log clamdscan-multiscan-fdpass.log
 	$TOP/clamdscan/clamdscan --quiet --config-file=test-clamd.conf $* --fdpass --log=clamdscan-fdpass.log
 	if test $? = 2; then 
 		error "Failed to run clamdscan (fdpass)!"
e114b109
 		cat clamdscan-fdpass.log
3e2b5046
 		die 1
 	fi
 	$TOP/clamdscan/clamdscan --quiet --config-file=test-clamd.conf $* -m --fdpass --log=clamdscan-multiscan-fdpass.log
 	if test $? = 2; then 
 		error "Failed to run clamdscan (fdpass + multiscan)!"
e114b109
 		cat clamdscan-multiscan-fdpass.log
 		die 1
 	fi
 	$TOP/clamdscan/clamdscan --quiet --config-file=test-clamd.conf $* --stream --log=clamdscan-stream.log
 	if test $? = 2; then 
 		error "Failed to run clamdscan (instream)!"
 		cat clamdscan-stream.log
 		die 1
 	fi
 	$TOP/clamdscan/clamdscan --quiet --config-file=test-clamd.conf $* -m --stream --log=clamdscan-multiscan-stream.log
 	if test $? = 2; then 
 		error "Failed to run clamdscan (instream + multiscan)!"
 		cat clamdscan-multiscan-stream.log
3e2b5046
 		die 1
 	fi
 }
 
346f7a4e
 run_reload_test()
 {
 	rm -f reload-testfile
 	echo "ClamAV-RELOAD-Test" >reload-testfile
f45111e5
 	run_clamdscan reload-testfile
346f7a4e
 	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
e791981b
 		die 10
346f7a4e
 	fi
 	echo "ClamAV-RELOAD-TestFile:0:0:436c616d41562d52454c4f41442d54657374" >test-db/new.ndb
6a791429
 	$TOP/clamdscan/clamdscan --reload --config-file=test-clamd.conf
e791981b
 	if test $? -ne 0; then
 		error "clamdscan says reload failed!"
 		die 11
 	fi
f45111e5
 	run_clamdscan reload-testfile
346f7a4e
 	grep "ClamAV-RELOAD-TestFile" clamdscan.log >/dev/null 2>/dev/null;
400decda
 	failed=0
346f7a4e
 	if test $? -ne 0; then
 		error "RELOAD test failed! (after reload)"
 		cat clamdscan.log
400decda
 		failed=1
346f7a4e
 	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)"
f45111e5
 		cat clamdscan-multiscan.log
400decda
 		failed=1
 	fi
 	if test "$failed" = "1"; then
 		echo "RELOAD tests failed!"
 		die 12
346f7a4e
 	fi
 	rm -f reload-testfile
 }
 
 run_clamdscan_fdpass() {
c1a133a0
 	rm -f clamdscan.log
6a791429
 	$TOP/clamdscan/clamdscan --quiet --fdpass --config-file=test-clamd.conf - <$1 --log=clamdscan.log
c1a133a0
 	if test $? = 2; then
f45111e5
 		error "Failed to run clamdscan (fdpass)!"
 		cat clamdscan.log
e791981b
 		die 14
c1a133a0
 	fi
 }
 
f45111e5
 # We run multiple clamd tests in parallel, each in its own directory
1642ffba
 prepare_clamd()
 {
 	cd clamdtest$1 
 	# Set up test DBdir
 	rm -rf test-db
 	mkdir -p test-db
 	cat <<EOF >test-db/test.hdb
232c4b70
 aa15bcf478d165efd2065190eb473bcb:544:ClamAV-Test-File
 EOF
1642ffba
 	cp $abs_srcdir/input/daily.ftm test-db/
 	cp $abs_srcdir/input/daily.pdb test-db/
650ea26e
 	$AWK "{ sub(/X/,\"$1\"); sub(/CWD/,\"`pwd`\"); print }" $abs_srcdir/test-clamd.conf >test-clamd.conf
1642ffba
 }
f45111e5
 
1642ffba
 rm -rf clamdtest$CLAMD_TEST_UNIQ1 clamdtest$CLAMD_TEST_UNIQ2
 mkdir clamdtest$CLAMD_TEST_UNIQ1 clamdtest$CLAMD_TEST_UNIQ2 || 
 	{ echo "Unable to create temporary directories!"; exit 1; }
456e31a3
 
f45111e5
 # Prepare configuration for clamd #1 and #2
1642ffba
 (prepare_clamd $CLAMD_TEST_UNIQ1)
 (prepare_clamd $CLAMD_TEST_UNIQ2)
f45111e5
 # Add clamd #2 specific configuration
 echo "VirusEvent $abs_srcdir/virusaction-test.sh `pwd`/clamdtest$CLAMD_TEST_UNIQ2 \"Virus found: %v\"" >>clamdtest$CLAMD_TEST_UNIQ2/test-clamd.conf
 echo "HeuristicScanPrecedence yes" >>clamdtest$CLAMD_TEST_UNIQ2/test-clamd.conf
456e31a3
 grep -v LogFile clamdtest$CLAMD_TEST_UNIQ2/test-clamd.conf >tmp__
 mv tmp__ clamdtest$CLAMD_TEST_UNIQ2/test-clamd.conf
f45111e5
 
 # Start clamd #1 tests
 (cd clamdtest$CLAMD_TEST_UNIQ1 
 start_clamd test-clamd.conf
346f7a4e
 
7f0d1148
 # Test that all testfiles are detected
f45111e5
 FILES=$TOP/test/clam*
 run_clamdscan $FILES
232c4b70
 NFILES=`ls -1 $FILES | wc -l`
073a33e1
 NINFECTED=`grep "Infected files" clamdscan.log | cut -f2 -d:|sed -e 's/ //g'`
b8edbb88
 NINFECTED_MULTI=`grep "Infected files" clamdscan-multiscan.log | cut -f2 -d:|sed -e 's/ //g'`
3e2b5046
 NINFECTED_FDPASS=`grep "Infected files" clamdscan-fdpass.log | cut -f2 -d:|sed -e 's/ //g'`
 NINFECTED_MULTI_FDPASS=`grep "Infected files" clamdscan-multiscan-fdpass.log | cut -f2 -d:|sed -e 's/ //g'`
e114b109
 NINFECTED_STREAM=`grep "Infected files" clamdscan-stream.log | cut -f2 -d:|sed -e 's/ //g'`
 NINFECTED_MULTI_STREAM=`grep "Infected files" clamdscan-multiscan-stream.log | cut -f2 -d:|sed -e 's/ //g'`
073a33e1
 if test "$NFILES" -ne "0$NINFECTED"; then
346f7a4e
 	grep OK clamdscan.log
133dd2e9
 	scan_failed clamdscan.log "clamd did not detect all testfiles correctly!"
232c4b70
 fi
b8edbb88
 if test "$NFILES" -ne "0$NINFECTED_MULTI"; then
346f7a4e
 	grep OK clamdscan-multiscan.log
133dd2e9
 	scan_failed clamdscan-multiscan.log "clamd did not detect all testfiles correctly in multiscan mode!"
346f7a4e
 fi
3e2b5046
 if test "$NFILES" -ne "0$NINFECTED_FDPASS"; then
 	grep OK clamdscan-fdpass.log
 	scan_failed clamdscan-multiscan.log "clamd did not detect all testfiles correctly in fdpass mode!"
 fi
 if test "$NFILES" -ne "0$NINFECTED_MULTI_FDPASS"; then
 	grep OK clamdscan-multiscan-fdpass.log
 	scan_failed clamdscan-multiscan.log "clamd did not detect all testfiles correctly in fdpass+multiscan mode!"
 fi
346f7a4e
 
3e2b5046
 $TOP/unit_tests/check_clamd
 ecode=$?
 if test $ecode -ne 77 && test $ecode -ne 0; then
     error "Failed clamd protocol test!"
     die 1
 fi
346f7a4e
 # Test HeuristicScanPrecedence off feature
f45111e5
 run_clamdscan ../clam-phish-exe
346f7a4e
 grep "ClamAV-Test-File" clamdscan.log >/dev/null 2>/dev/null;
 if test $? -ne 0; then
 	error "HeuristicScanPrecedence off test failed!"
 	cat clamdscan.log
f45111e5
 	die 4
b8edbb88
 fi
f45111e5
 die 0
 )&
 pid1=$!
7f0d1148
 
f45111e5
 # Start clamd #2 tests
 (cd clamdtest$CLAMD_TEST_UNIQ2
 start_clamd test-clamd.conf
346f7a4e
 
7f0d1148
 # Test VirusEvent feature
3e2b5046
 run_clamdscan_fileonly $TOP/test/clam.exe
dfc0c031
 grep "Virus found: ClamAV-Test-File.UNOFFICIAL" test-clamd.log >/dev/null 2>/dev/null; 
346f7a4e
 if test $? -ne 0; then
 	error "Virusaction test failed!" 
751f8470
 	cat test-clamd.log
f45111e5
 	die 2
7f0d1148
 fi
 
 # Test HeuristicScanPrecedence feature
f45111e5
 run_clamdscan ../clam-phish-exe
dfc0c031
 grep "Phishing.Heuristics.Email.SpoofedDomain" clamdscan.log >/dev/null 2>/dev/null;
346f7a4e
 if test $? -ne 0; then
 	error "HeuristicScanPrecedence on test failed!"
 	cat clamdscan.log
f45111e5
 	die 3
751f8470
 fi
c1a133a0
 
f45111e5
 if grep "^#define HAVE_FD_PASSING 1" $TOP/clamav-config.h >/dev/null; then
 	run_clamdscan_fdpass $TOP/test/clam.exe
c1a133a0
 	grep "ClamAV-Test-File" clamdscan.log >/dev/null 2>/dev/null;
346f7a4e
 	if test $? -ne 0; then
 		error "FDpassing test failed!"
c1a133a0
 		cat clamdscan.log;
f45111e5
 		die 4
c1a133a0
 	fi
 else
346f7a4e
 	echo "*** No file descriptor passing support, skipping test"
c1a133a0
 fi
f45111e5
 
 # Test RELOAD command
 run_reload_test
 
 die 0
 )&
73ba72eb
 
f45111e5
 pid2=$!
 
 wait $pid1
 exitcode1=$?
 wait $pid2
 exitcode2=$?
73ba72eb
 rm -rf clamdtest$CLAMD_TEST_UNIQ1 clamdtest$CLAMD_TEST_UNIQ2 test-db accdenied
d7f43f9f
 if (test $exitcode1 -ne 0 && test $exitcode1 -ne 127) || (test $exitcode2 -ne 0	&& test $exitcode2 -ne 127); then
f45111e5
 	exit 1
 fi
 exit 0