~yolanda.robla/ubuntu/saucy/clamav/dep-8-tests

« back to all changes in this revision

Viewing changes to unit_tests/check_common.sh

  • Committer: Bazaar Package Importer
  • Author(s): Scott Kitterman
  • Date: 2009-11-02 23:27:19 UTC
  • mfrom: (0.35.9 sid)
  • Revision ID: james.westby@ubuntu.com-20091102232719-61ay35095dhbuxfm
Tags: 0.95.3+dfsg-1ubuntu1
* Merge from debian unstable, remaining changes:
  - Drop build-dep on electric-fence (in Universe)
  - Build-dep on libltdl3-dev instead of libltdl-dev for updating earlier
    releases more easily
  - Add apparmor profiles for clamd and freshclam along with maintainer
    script changes

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/bin/sh
 
2
 
 
3
# Solaris's /bin/sh is not a POSIX shell, and
 
4
# it quits when cd fails, even if it is followed by a ||
 
5
# So enable -e only on POSIX shells
 
6
(cd /nonexistentdir 2>/dev/null || true) && set -e
 
7
 
 
8
WRAPPER=${WRAPPER-}
 
9
TOP=`pwd`/..
 
10
CLAMSCAN=$TOP/clamscan/clamscan
 
11
CLAMD=$TOP/clamd/clamd
 
12
CHECK_CLAMD=$TOP/unit_tests/check_clamd
 
13
CLAMDSCAN=$TOP/clamdscan/clamdscan
 
14
TESTFILES=$TOP/test/clam*
 
15
NFILES=`ls -1 $TESTFILES | wc -l`
 
16
 
 
17
killclamd() {
 
18
    test -f clamd-test.pid &&
 
19
    pid=`cat clamd-test.pid 2>/dev/null` &&
 
20
    test -n "$pid" &&
 
21
    kill -0 $pid 2>/dev/null &&
 
22
    kill $pid 2>/dev/null &&
 
23
    kill -0 $pid 2>/dev/null &&
 
24
    sleep 1 &&
 
25
    kill -0 $pid 2>/dev/null &&
 
26
    sleep 9 &&
 
27
    kill -0 $pid 2>/dev/null &&
 
28
    echo "Killing stuck clamd!" &&
 
29
    kill -KILL $pid && exit 109 || true
 
30
}
 
31
 
 
32
error()
 
33
{
 
34
        echo >&2
 
35
        echo "***" >&2
 
36
        echo "*** $1" >&2
 
37
        echo "***" >&2
 
38
}
 
39
 
 
40
die()
 
41
{
 
42
        error "$1"
 
43
        test -f valgrind.log && cat valgrind.log || true
 
44
        killclamd
 
45
        exit 42
 
46
}
 
47
 
 
48
# Setup test directory to avoid temporary and output file clashes
 
49
test_start() {
 
50
    ulimit -t 120; ulimit -d 512000; ulimit -v 512000;
 
51
    (cd test-$1 2>/dev/null && killclamd || true)
 
52
    rm -rf test-$1
 
53
    mkdir test-$1
 
54
    cd test-$1
 
55
    mkdir test-db
 
56
    cat <<EOF >test-db/test.hdb
 
57
aa15bcf478d165efd2065190eb473bcb:544:ClamAV-Test-File
 
58
EOF
 
59
    cat <<EOF >test-clamd.conf
 
60
LogFile `pwd`/clamd-test.log
 
61
LogFileMaxSize 0
 
62
LogTime yes
 
63
Debug yes
 
64
LogClean yes
 
65
LogVerbose yes
 
66
PidFile `pwd`/clamd-test.pid
 
67
DatabaseDirectory `pwd`/test-db
 
68
LocalSocket clamd-test.socket
 
69
TCPAddr 127.0.0.1
 
70
# using different port here to avoid conflicts with system clamd daemon
 
71
TCPSocket 331$1
 
72
ExitOnOOM yes
 
73
DetectPUA yes
 
74
ScanPDF yes
 
75
CommandReadTimeout 1
 
76
MaxQueue 800
 
77
MaxConnectionQueueLength 1024
 
78
EOF
 
79
}
 
80
 
 
81
# arg1: expected exitcode
 
82
test_run() {
 
83
   expected=$1
 
84
   shift
 
85
   set +e
 
86
   $TOP/libtool --mode=execute $WRAPPER $*
 
87
   val=$?
 
88
   if test $val -ne $expected; then
 
89
       error "Failed to run $*, expected $expected exitcode, but was $val" >&2;
 
90
       return 0;
 
91
   fi
 
92
   set -e
 
93
   return 1;
 
94
}
 
95
 
 
96
# Run a test and return its exitcode
 
97
test_run_check() {
 
98
    set +e
 
99
    $TOP/libtool --mode=execute $WRAPPER $*
 
100
    val=$?
 
101
    set -e
 
102
    return $?;
 
103
}
 
104
 
 
105
# test successfully finished, remove test dir
 
106
test_end() {
 
107
    killclamd
 
108
    cd ..
 
109
    rm -rf test-$1
 
110
}
 
111
 
 
112
scan_failed() {
 
113
    if test "X$unrar_disabled" = "X1" && test `grep -v '\.rar' $1 | grep OK | wc -l` -eq 0
 
114
    then
 
115
        error "UNRAR is disabled, won't be able to detect unrar files!"
 
116
    else
 
117
        cat $1
 
118
        die "$2";
 
119
    fi
 
120
}
 
121
 
 
122
# ----------- valgrind wrapper 
 
123
init_valgrind() {
 
124
    test "x$VG" = "x1" || { echo "*** valgrind tests skipped by default, use 'make check VG=1' to activate"; exit 77; }
 
125
    export VALGRIND=`which ${VALGRIND-valgrind}`
 
126
    export VALGRIND_COMMON_FLAGS="-v --trace-children=yes --suppressions=$abs_srcdir/valgrind.supp --log-file=valgrind.log --error-exitcode=123 $GENSUPP"
 
127
    export VALGRIND_FLAGS="$VALGRIND_COMMON_FLAGS --track-fds=yes --leak-check=full"
 
128
    export VALGRIND_FLAGS_RACE="$VALGRIND_COMMON_FLAGS --tool=helgrind"
 
129
    test -n "$VALGRIND" || { echo "*** valgrind not found, skipping test"; exit 77; }
 
130
    test -x "$VALGRIND" || { echo "*** valgrind not executable, skipping test"; exit 77; }
 
131
}
 
132
 
 
133
init_helgrind() {
 
134
    init_valgrind
 
135
}
 
136
 
 
137
end_valgrind() {
 
138
    NRUNS=`grep -a "ERROR SUMMARY" valgrind.log | wc -l`
 
139
    if test $NRUNS -ne `grep -a "ERROR SUMMARY: 0 errors" valgrind.log | wc -l` || 
 
140
        test `grep -a "FATAL:" valgrind.log|wc -l` -ne 0; then
 
141
        cat valgrind.log
 
142
        die "Valgrind tests failed"
 
143
    fi
 
144
}
 
145
 
 
146
# ----------- clamscan tests --------------------------------------------------------
 
147
test_clamscan() {
 
148
    test_start $1
 
149
    if test_run 1 $CLAMSCAN --quiet -dtest-db/test.hdb $TESTFILES --log=clamscan.log; then
 
150
        scan_failed clamscan.log "clamscan didn't detect all testfiles correctly"
 
151
    fi
 
152
    NINFECTED=`grep "Infected files" clamscan.log | cut -f2 -d: | sed -e 's/ //g'`
 
153
    if test "$NFILES" -ne "0$NINFECTED"; then
 
154
        scan_failed clamscan.log "clamscan didn't detect all testfiles correctly"
 
155
    fi
 
156
 
 
157
    cat <<EOF >test-db/test.pdb
 
158
H:example.com
 
159
EOF
 
160
    if test_run 0 $CLAMSCAN --quiet -dtest-db $abs_srcdir/input/phish-test-* --log=clamscan2.log; then
 
161
        cat clamscan2.log;
 
162
        die "Failed to run clamscan (phish-test)";
 
163
    fi
 
164
 
 
165
    if test_run 1 $CLAMSCAN --quiet --phishing-ssl --phishing-cloak -dtest-db $abs_srcdir/input/phish-test-* --log=clamscan3.log; then
 
166
        cat clamscan3.log;
 
167
        die "Failed to run clamscan (phish-test2)";
 
168
    fi
 
169
 
 
170
    grep "phish-test-ssl: Phishing.Heuristics.Email.SSL-Spoof FOUND" clamscan3.log >/dev/null || die "phish-test1 failed";
 
171
    grep "phish-test-cloak: Phishing.Heuristics.Email.Cloaked.Null FOUND" clamscan3.log >/dev/null || die "phish-test2 failed";
 
172
    test_end $1
 
173
}
 
174
 
 
175
# ----------- clamd tests --------------------------------------------------------
 
176
start_clamd()
 
177
{
 
178
    cp $abs_srcdir/input/daily.pdb test-db/daily.pdb
 
179
    if test_run 0 $CLAMD -c test-clamd.conf --help >clamd-test.log; then
 
180
        die "Failed to run clamd --help";
 
181
    fi
 
182
    grep "Clam AntiVirus Daemon" clamd-test.log >/dev/null || die "Wrong --help reply from clamd!";
 
183
    if test_run 0 $CLAMD -c test-clamd.conf >clamd-test.log 2>&1; then
 
184
        cat clamd-test.log
 
185
        die "Failed to run clamd";
 
186
    fi
 
187
}
 
188
 
 
189
run_clamdscan_fileonly() {
 
190
    rm -f clamdscan.log clamdscan-multiscan.log
 
191
    $CLAMDSCAN --version --config-file=test-clamd.conf | grep "^ClamAV" >/dev/null || die "clamdscan can't get version of clamd!";
 
192
    set +e
 
193
    $CLAMDSCAN --quiet --config-file=test-clamd.conf $* --log=clamdscan.log
 
194
    if test $? = 2; then
 
195
        die "Failed to run clamdscan!"
 
196
    fi
 
197
    $CLAMDSCAN --quiet --config-file=test-clamd.conf $* -m --log=clamdscan-multiscan.log
 
198
    if test $? = 2; then
 
199
        die "Failed to run clamdscan (multiscan)!"
 
200
    fi
 
201
    set -e
 
202
}
 
203
 
 
204
run_clamdscan() {
 
205
    run_clamdscan_fileonly $*
 
206
    rm -f clamdscan-fdpass.log clamdscan-multiscan-fdpass.log clamdscan-stream.log clamdscan-multiscan-stream.log
 
207
    set +e
 
208
    $CLAMDSCAN --quiet --config-file=test-clamd.conf $* --fdpass --log=clamdscan-fdpass.log
 
209
    if test $? = 2; then 
 
210
        die "Failed to run clamdscan (fdpass)!"
 
211
    fi
 
212
    $CLAMDSCAN --quiet --config-file=test-clamd.conf $* -m --fdpass --log=clamdscan-multiscan-fdpass.log
 
213
    if test $? = 2; then 
 
214
        die "Failed to run clamdscan (fdpass + multiscan)!"
 
215
    fi
 
216
    $CLAMDSCAN --quiet --config-file=test-clamd.conf $* --stream --log=clamdscan-stream.log
 
217
    if test $? = 2; then 
 
218
        die "Failed to run clamdscan (instream)!"
 
219
    fi
 
220
    $CLAMDSCAN --quiet --config-file=test-clamd.conf $* -m --stream --log=clamdscan-multiscan-stream.log
 
221
    if test $? = 2; then 
 
222
        die "Failed to run clamdscan (instream + multiscan)!"
 
223
    fi
 
224
    set -e
 
225
}
 
226
 
 
227
run_reload_test()
 
228
{
 
229
        echo "ClamAV-RELOAD-Test" >reload-testfile
 
230
        run_clamdscan reload-testfile
 
231
        # it is not supposed to detect until we actually put the
 
232
        # signature there and reload!
 
233
        grep "ClamAV-RELOAD-TestFile" clamdscan.log >/dev/null 2>/dev/null && die "RELOAD test(1) failed!"
 
234
        echo "ClamAV-RELOAD-TestFile:0:0:436c616d41562d52454c4f41442d54657374" >test-db/new.ndb
 
235
        $CLAMDSCAN --reload --config-file=test-clamd.conf || die "clamdscan says reload failed!"
 
236
        run_clamdscan reload-testfile
 
237
        failed=0
 
238
        grep "ClamAV-RELOAD-TestFile" clamdscan.log >/dev/null 2>/dev/null || die "RELOAD test failed! (after reload)"
 
239
        grep "ClamAV-RELOAD-TestFile" clamdscan-multiscan.log >/dev/null 2>/dev/null || die "RELOAD test failed! (after reload, multiscan)"
 
240
}
 
241
 
 
242
run_clamdscan_fdpass() {
 
243
    set +e
 
244
    $CLAMDSCAN --quiet --fdpass --config-file=test-clamd.conf - <$1 --log=clamdscan.log
 
245
    if test $? = 2; then
 
246
        die "Failed to run clamdscan (fdpass)!"
 
247
    fi
 
248
    set -e
 
249
}
 
250
 
 
251
test_clamd1() {
 
252
    test_start $1
 
253
    start_clamd
 
254
    # Test that all testfiles are detected
 
255
    run_clamdscan $TESTFILES
 
256
    NINFECTED=`grep "Infected files" clamdscan.log | cut -f2 -d:|sed -e 's/ //g'`
 
257
    NINFECTED_MULTI=`grep "Infected files" clamdscan-multiscan.log | cut -f2 -d:|sed -e 's/ //g'`
 
258
    NINFECTED_FDPASS=`grep "Infected files" clamdscan-fdpass.log | cut -f2 -d:|sed -e 's/ //g'`
 
259
    NINFECTED_MULTI_FDPASS=`grep "Infected files" clamdscan-multiscan-fdpass.log | cut -f2 -d:|sed -e 's/ //g'`
 
260
    NINFECTED_STREAM=`grep "Infected files" clamdscan-stream.log | cut -f2 -d:|sed -e 's/ //g'`
 
261
    NINFECTED_MULTI_STREAM=`grep "Infected files" clamdscan-multiscan-stream.log | cut -f2 -d:|sed -e 's/ //g'`
 
262
    if test "$NFILES" -ne "0$NINFECTED"; then
 
263
        scan_failed clamdscan.log "clamd did not detect all testfiles correctly!"
 
264
    fi
 
265
    if test "$NFILES" -ne "0$NINFECTED_MULTI"; then
 
266
        scan_failed clamdscan-multiscan.log "clamd did not detect all testfiles correctly in multiscan mode!"
 
267
    fi
 
268
    if test "$NFILES" -ne "0$NINFECTED_FDPASS"; then
 
269
        scan_failed clamdscan-multiscan.log "clamd did not detect all testfiles correctly in fdpass mode!"
 
270
    fi
 
271
    if test "$NFILES" -ne "0$NINFECTED_MULTI_FDPASS"; then
 
272
        scan_failed clamdscan-multiscan.log "clamd did not detect all testfiles correctly in fdpass+multiscan mode!"
 
273
    fi
 
274
    # Test HeuristicScanPrecedence off feature
 
275
    run_clamdscan ../clam-phish-exe
 
276
    grep "ClamAV-Test-File" clamdscan.log >/dev/null 2>/dev/null;
 
277
    if test $? -ne 0; then
 
278
        cat clamdscan.log
 
279
        die "HeuristicScanPrecedence off test failed!"
 
280
    fi
 
281
    test_end $1
 
282
}
 
283
 
 
284
test_clamd2() {
 
285
    test_start $1
 
286
    start_clamd
 
287
    # Run clamd test suite
 
288
    test_run_check $CHECK_CLAMD
 
289
    val=$?
 
290
 
 
291
    # Test RELOAD command
 
292
    run_reload_test
 
293
 
 
294
    test_end $1
 
295
    exit $?
 
296
}
 
297
 
 
298
test_clamd3() {
 
299
    test_start $1
 
300
    echo "VirusEvent $abs_srcdir/virusaction-test.sh `pwd` \"Virus found: %v\"" >>test-clamd.conf
 
301
    echo "HeuristicScanPrecedence yes" >>test-clamd.conf
 
302
    start_clamd
 
303
    # Test HeuristicScanPrecedence feature
 
304
    run_clamdscan ../clam-phish-exe
 
305
    grep "Phishing.Heuristics.Email.SpoofedDomain" clamdscan.log >/dev/null 2>/dev/null ||
 
306
        { cat clamdscan.log; die "HeuristicScanPrecedence on test failed!"; }
 
307
 
 
308
    if grep "^#define HAVE_FD_PASSING 1" $TOP/clamav-config.h >/dev/null; then
 
309
        run_clamdscan_fdpass $TOP/test/clam.exe
 
310
        grep "ClamAV-Test-File" clamdscan.log >/dev/null 2>/dev/null ||
 
311
        { cat clamdscan.log; die "FDpassing test failed!";}
 
312
    else
 
313
        echo "*** No file descriptor passing support, skipping test"
 
314
    fi
 
315
 
 
316
    # Test VirusEvent feature
 
317
    run_clamdscan_fileonly $TOP/test/clam.exe
 
318
    test -f test-clamd.log || sleep 1
 
319
    grep "Virus found: ClamAV-Test-File.UNOFFICIAL" test-clamd.log >/dev/null 2>/dev/null ||
 
320
        { cat test-clamd.log || true; die "Virusaction test failed"; }
 
321
 
 
322
    test_end $1
 
323
}