OSDN Git Service

2013.10.24
[uclinux-h8/uClinux-dist.git] / freeswan / klips / test / functions.sh
1 #!/bin/sh
2
3 consolediff() {
4     cleanups="cat OUTPUT/console.txt "
5
6     for fixup in `echo $REF_CONSOLE_FIXUPS`
7     do
8         if [ -f $FIXUPDIR/$fixup ]
9         then
10             case $fixup in
11                 *.sed) cleanups="$cleanups | sed -f $FIXUPDIR/$fixup";;
12                 *.pl)  cleanups="$cleanups | perl $FIXUPDIR/$fixup";;
13                 *.awk) cleanups="$cleanups | awk -f $FIXUPDIR/$fixup";;
14                     *) echo Unknown fixup type: $fixup;;
15             esac
16         fi
17     done
18
19     rm -f OUTPUT/console-fixed.txt OUTPUT/console.diff
20     $CONSOLEDIFFDEBUG && echo Cleanups is $cleanups
21     eval $cleanups >OUTPUT/console-fixed.txt
22     if diff -u -w -b -B $REF_CONSOLE_OUTPUT OUTPUT/console-fixed.txt >OUTPUT/console.diff
23     then
24         echo "Console output matched"
25     else
26         echo "Console output differed"
27         success=false
28     fi
29 }
30
31 compat_variables() {
32     if [ -z "$REF_CONSOLE_OUTPUT" ] && [ -n "$REFCONSOLEOUTPUT" ]
33     then
34         REF_CONSOLE_OUTPUT=$REFCONSOLEOUTPUT
35     fi
36
37     if [ -z "$REF_CONSOLE_FIXUPS" ] && [ -n "$REFCONSOLEFIXUPS" ]
38     then
39         REF_CONSOLE_FIXUPS=$REFCONSOLEFIXUPS
40     fi
41
42     if [ -z "$REF_PUB_OUTPUT" ] && [ -n "$REFPUBOUTPUT" ]
43     then
44         REF_PUB_OUTPUT=$REFPUBOUTPUT
45     fi
46
47     if [ -z "$REF_PRIV_OUTPUT" ] && [ -n "$REFPRIVOUTPUT" ]
48     then
49         REF_PRIV_OUTPUT=$REFPRIVOUTPUT
50     fi
51 }
52
53 # this is called to set additional variables that depend upon testparams.sh
54 prerunsetup() {
55     HOSTSTART=$POOLSPACE/$TESTHOST/start.sh
56
57     compat_variables;
58 }
59
60 #
61 # record results records the status of each test in 
62 #   $REGRESSRESULTS/$testname/status
63 #
64 # If the status is negative, then the "OUTPUT" directory of the test is
65 # copied to $REGRESSRESULTS/$testname/OUTPUT as well.
66 #
67 # The file $testname/description.txt if it exists is copied as well.
68 #
69 # If $REGRESSRESULTS is not set, then nothing is done.
70
71 # See testing/utils/regress-summarizeresults.pl for a tool to build a nice
72 # report from these files. 
73 #
74 # See testing/utils/regress-nightly.sh and regress-stage2.sh for code
75 # that sets up $REGRESSRESULTS.
76
77 # usage: recordresults testname testtype status
78 #
79 recordresults() {
80     testname=$1
81     testtype=$2
82     success=$3
83     if [ -n "$REGRESSRESULTS" ]
84     then
85         mkdir -p $REGRESSRESULTS/$testname
86         
87         # note that 0/1 is shell sense.
88         case $success in
89             0) success=true;;
90             1) success=false;;
91             true)  success=true;;
92             false) sucesss=false;;
93             succeed) success=true;;
94             fail)  success=false;;
95             yes)   success=true;;
96             no)    success=false;;
97             *) echo 'functions.sh:recordresults()' Bad value for success: $success >&2;
98                 exit 2;
99          esac
100
101          echo $success >$REGRESSRESULTS/$testname/status
102
103          if [ -f $testname/description.txt ]
104          then
105             cp $testname/description.txt $REGRESSRESULTS/$testname
106          fi
107
108          if [ -n "$TEST_PURPOSE" ]
109          then
110             case $TEST_PURPOSE in
111             regress) echo ${TEST_PROB_REPORT} >$REGRESSRESULTS/$testname/regress.txt;;
112                goal) echo ${TEST_GOAL_ITEM}   >$REGRESSRESULTS/$testname/goal.txt;;
113             exploit) echo ${TEST_EXPLOIT_URL} >$REGRESSRESULTS/$testname/exploit.txt;;
114             esac
115          fi
116
117          if $success
118          then
119             :
120          else
121             mkdir -p $REGRESSRESULTS/$testname/OUTPUT
122             tar cf - $testname/OUTPUT | (cd $REGRESSRESULTS && tar xf - )
123          fi
124     fi
125 }
126
127 # The following variables need to be set before calling the tests
128
129 #    TESTNAME          - the name of the test
130 #    SCRIPT            - a script to load on the console
131 #    PRIVINPUT         - a pcap file to feed on private side
132 #    PUBINPUT          - a pcap file to feed on the public side
133 #
134 #  If set, then the public and private packet output will be captured,
135 #  turned into ASCII with tcpdump, and diff'ed against these files.
136 #    REF_PRIVO_UTPUT   - for private side
137 #    REF_PUB_OUTPUT    - for public side
138 #    TCPDUMPARGS     - extra args for TCPDUMP.
139 #
140 #  If set, then the console output will be diff'ed against this file:
141 #    REF_CONSOLE_OUTPUT          
142 #  
143 #  The console output may need to be sanitized. The list of fixups from
144 # REF_CONSOLE_FIXUPS will be appled from "fixups". The extension is used to
145 # determine what program to use.
146 #
147 #  Some additional options to control the network emulator
148 #    EXITONEMPTY=--exitonempty   - if pcap file end should signal end of test
149 #    ARPREPLY=--arpreply         - if ARPs should be answered
150
151     
152 netjigtest() {
153
154     prerunsetup
155
156     success=true
157
158     PRIVOUTPUT=''
159     PUBOUTPUT=''
160     
161     mkdir -p OUTPUT
162
163     NJARGS=''
164
165     if [ -n "$PRIVINPUT" ]
166     then
167         NJARGS="$NJARGS --playprivate $PRIVINPUT"
168     fi
169
170     if [ -n "$PUBINPUT" ]
171     then
172         NJARGS="$NJARGS --playpublic $PUBINPUT"
173     fi
174
175     if [ -n "$REF_PRIV_OUTPUT" ]
176     then
177         PRIVOUTPUT=`basename $REF_PRIV_OUTPUT .txt `
178         NJARGS="$NJARGS --recordprivate OUTPUT/$PRIVOUTPUT.pcap"
179     fi
180
181     if [ -n "$REF_PUB_OUTPUT" ]
182     then
183         PUBOUTPUT=`basename $REF_PUB_OUTPUT .txt`
184         NJARGS="$NJARGS --recordpublic OUTPUT/$PUBOUTPUT.pcap"
185     fi
186
187     if [ -n "$NETJIGARGS" ]
188     then
189         NJARGS="$NJARGS $NETJIGARGS"
190     fi
191
192     rm -f OUTPUT/console.txt
193     $NETJIGDEBUG && echo $NJ --tcpdump $ARPREPLY $EXITONEMPTY $NJARGS --startup "expect -f $UTILS/host-test.tcl $HOSTSTART ${SCRIPT} " 
194     $NJ --tcpdump $ARPREPLY $EXITONEMPTY `eval echo $NJARGS` --startup "expect -f $UTILS/host-test.tcl $HOSTSTART ${SCRIPT} >OUTPUT/console.txt" 
195
196     uml_mconsole $TESTHOST halt
197
198     if [ -z "$REF_PRIV_FILTER" ]
199     then
200         REF_PRIV_FILTER=cat
201     fi
202
203     if [ -n "$PRIVOUTPUT" ]
204     then
205         rm -f OUTPUT/$PRIVOUTPUT.txt
206         echo $TCPDUMP -t $TCPDUMPFLAGS -r OUTPUT/$PRIVOUTPUT.pcap '>'OUTPUT/$PRIVOUTPUT.txt
207         eval $TCPDUMP -t $TCPDUMPFLAGS -r OUTPUT/$PRIVOUTPUT.pcap | $REF_PRIV_FILTER >OUTPUT/$PRIVOUTPUT.txt
208
209         rm -f OUTPUT/$PRIVOUTPUT.diff
210         if diff -u -w -b -B $REF_PRIV_OUTPUT OUTPUT/$PRIVOUTPUT.txt >OUTPUT/$PRIVOUTPUT.diff
211         then
212             echo "Private side output matched"
213         else
214             echo "Private side output failed"
215             success=false
216         fi
217     fi
218
219
220     if [ -z "$REF_PUB_FILTER" ]
221     then
222         REF_PUB_FILTER=cat
223     fi
224
225     if [ -n "$PUBOUTPUT" ]
226     then
227         rm -f OUTPUT/$PUBOUTPUT.txt
228         echo $TCPDUMP -t $TCPDUMPFLAGS -r OUTPUT/$PUBOUTPUT.pcap '>'OUTPUT/$PUBOUTPUT.txt
229         eval $TCPDUMP -t $TCPDUMPFLAGS -r OUTPUT/$PUBOUTPUT.pcap | $REF_PUB_FILTER >|OUTPUT/$PUBOUTPUT.txt
230
231         rm -f OUTPUT/$PUBOUTPUT.diff
232         if diff -u -w -b -B $REF_PUB_OUTPUT OUTPUT/$PUBOUTPUT.txt >OUTPUT/$PUBOUTPUT.diff
233         then
234             echo "Public  side output matched"
235         else
236             echo "Public  side output failed"
237             success=false
238         fi
239     fi
240
241     if [ -n "$REF_CONSOLE_OUTPUT" ]
242     then
243         consolediff
244     fi
245     if $success
246     then
247         exit 0
248     else
249         exit 1
250     fi
251 }
252
253     
254 umltest() {
255     mkdir -p OUTPUT
256     success=true
257
258     prerunsetup
259
260     rm -f OUTPUT/console.txt
261     expect -f $UTILS/host-test.tcl $HOSTSTART ${SCRIPT} >OUTPUT/console.txt
262
263     if [ -n "$REFCONSOLEOUTPUT" ]
264     then
265         consolediff
266     fi
267     if $success
268     then
269         exit 0
270     else
271         exit 1
272     fi
273 }
274
275
276 klipstest() {
277     testdir=$1
278     testtype=$2
279
280     echo '*******  KLIPS RUNNING' $testdir '*******' 
281
282     if [ ! -r $testdir/testparams.sh ]
283     then
284         echo '      ' No configuration
285         return
286     fi
287     ( cd $testdir && . ./testparams.sh && netjigtest )
288     stat=$?
289     recordresults $testdir $testtype $stat
290     if [ $stat = 0 ]
291     then
292         echo '*******  PASSED '$testdir' ********'
293     else
294         echo '*******  FAILED '$testdir' ********'
295     fi
296 }
297
298 ctltest() {
299     testdir=$1
300     testtype=$2
301
302     echo '*******  KERN  RUNNING' $testdir '*******' 
303
304     if [ ! -r $testdir/testparams.sh ]
305     then
306         echo '      ' No configuration
307         return
308     fi
309     ( cd $testdir && . ./testparams.sh && umltest )
310     stat=$?
311     recordresults $testdir $testtype $stat
312     if [ $stat = 0 ]
313     then
314         echo '*******  PASSED '$testdir' ********'
315     else
316         echo '*******  FAILED '$testdir' ********'
317     fi
318 }
319
320 skiptest() {
321     testdir=$1
322     testtype=$2
323
324     echo '*******  NOT   RUNNING' $testdir '*******' 
325 }