~ubuntu-branches/ubuntu/wily/apparmor/wily

« back to all changes in this revision

Viewing changes to tests/regression/subdomain/prologue.inc

  • Committer: Bazaar Package Importer
  • Author(s): Kees Cook
  • Date: 2011-04-27 10:38:07 UTC
  • mfrom: (5.1.118 natty)
  • Revision ID: james.westby@ubuntu.com-20110427103807-ym3rhwys6o84ith0
Tags: 2.6.1-2
debian/copyright: clarify for some full organization names.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# vim:syntax=sh
2
 
#
3
 
# prologue.inc
4
 
#
5
 
# Test infrastructure support.
6
 
#
7
 
# This file should be included by each test case
8
 
# It does a lot of hidden 'magic',  Downside is that
9
 
# this magic makes debugging fauling tests more difficult.
10
 
# Running the test with the '-r' option can help.
11
 
#
12
 
# Userchangeable variables (tmpdir etc) should be specified in
13
 
# uservars.inc
14
 
15
 
# Cleanup is automatically performed by epilogue.inc
16
 
#
17
 
# For this file, functions are first, entry point code is at end, see "MAIN"
18
 
 
19
 
fatalerror()
20
 
{
21
 
        # global _fatal
22
 
        if [ -z "$_fatal" ]
23
 
        then
24
 
                _fatal=true     # avoid cascading fatal errors
25
 
                echo "Fatal Error ($testname): $*" >&2
26
 
                exit 127
27
 
        fi
28
 
}
29
 
 
30
 
testerror()
31
 
{
32
 
        fatalerror "Unable to run test sub-executable"
33
 
}
34
 
 
35
 
testfailed()
36
 
{
37
 
        # global num_testfailures teststatus
38
 
        num_testfailures=$(($num_testfailures + 1))
39
 
        teststatus="fail"
40
 
}
41
 
 
42
 
error_handler()
43
 
{
44
 
        #invoke exit_handler to cleanup
45
 
        exit_handler
46
 
 
47
 
        fatalerror "Unexpected shell error. Run with -x to debug"
48
 
}
49
 
 
50
 
# invoked whenever we exit (normally, interrupt 
51
 
# or exit due to testfailure()/error_handler()
52
 
exit_handler()
53
 
{
54
 
        # global bin
55
 
        if [ -d "$bin" ]
56
 
        then
57
 
                . $bin/epilogue.inc
58
 
        fi
59
 
}
60
 
 
61
 
genrunscript()
62
 
{
63
 
        # create a log so we can run test again if -retain specified
64
 
 
65
 
        local runfile
66
 
        #global tmpdir profile outfile
67
 
        
68
 
        if [ "$retaintmpdir" = "true" ]
69
 
        then
70
 
                runfile=$tmpdir/runtest
71
 
                echo "$subdomain ${parser_args} < $profile" > $runfile
72
 
                echo "$testexec \"$@\" 2>&1 > $outfile" >> $runfile
73
 
                echo "echo $testname: \`cat $outfile\`" >> $runfile
74
 
                echo "$subdomain ${parser_args} -R < $profile" >> $runfile
75
 
        fi
76
 
}
77
 
 
78
 
resolve_symlink()
79
 
{
80
 
        if [ -z "${__NO_TRAP_ERR}" ]
81
 
        then
82
 
                trap "error_handler" ERR
83
 
        fi
84
 
 
85
 
        local link linkdir targetdir targetname
86
 
 
87
 
        link=$1
88
 
 
89
 
        while [ -h ${link} ]
90
 
        do
91
 
                if [ -x /usr/bin/readlink ] ; then 
92
 
                        target=$(/usr/bin/readlink ${link})
93
 
                else 
94
 
                        # I'm sure there's a more perlish way to do this
95
 
                        target=$( perl -e "printf (\"%s\n\", readlink(\"${link}\"));") 
96
 
                        #target=$( perl -e "if (\$foo = readlink(\"${link}\")){ \
97
 
                        #                       printf (\"%s\n\", \$foo) \
98
 
                        #                   } else { \
99
 
                        #                       print \"${link}\n\"; \
100
 
                        #                   };")
101
 
                fi
102
 
                case "${target}" in
103
 
                        /*)  link=${target}
104
 
                             ;;
105
 
                        *) linkdir=$(dirname ${link})
106
 
                           targetdir=$(dirname ${target})
107
 
                           targetname=$(basename ${target})
108
 
                           linkdir=$(cd ${linkdir}/${targetdir} ; pwd)
109
 
                           link=${linkdir}/${targetname}
110
 
                           ;;
111
 
                esac
112
 
                                
113
 
        done
114
 
 
115
 
        if [ -e ${link} ]
116
 
        then
117
 
                echo ${link}
118
 
                return 0
119
 
        else
120
 
                return 1
121
 
        fi
122
 
}
123
 
 
124
 
resolve_libs()
125
 
{
126
 
        if [ -z "${__NO_TRAP_ERR}" ]
127
 
        then
128
 
                trap "error_handler" ERR
129
 
        fi
130
 
 
131
 
        local exec mode libpath libs
132
 
        # global dynlibs
133
 
 
134
 
        exec=$1
135
 
 
136
 
        if [ ! -x $1 ]
137
 
        then
138
 
                fatalerror "invalid test executable $test"
139
 
        fi
140
 
 
141
 
        # Suck. SuSE's ldd has a line "linux-gate.so.1 =>  (0xffffe000)"
142
 
        #       Red Hat's ldd has "/lib/ld-linux.so.2 (0x007b1000)"
143
 
        # good ${DIETY}, what gross kludgage.
144
 
        libs=$(ldd $exec | egrep -v "linux-(vdso(32|64)|gate).so.1" | sed 's~^.*=> \(/.*\) (.*$~\1~' | awk '{print $1}')
145
 
 
146
 
        dynlibs="/etc/ld.so.cache:r"
147
 
 
148
 
        # bleah, this is cheeseball. on systems with a stackguard
149
 
        # compiler, we also need access to /dev/urandom
150
 
        for i in $libs /dev/urandom
151
 
        do
152
 
                mode=rm
153
 
                # resolve possible symlinks before checking for ld pattern
154
 
                # this is necessary because some architectures (zSeries)
155
 
                # use nonconforming ld symlink names, like ld64.so
156
 
                libpath=`resolve_symlink $i`            
157
 
                case $libpath in
158
 
                /lib/ld[.-]*) mode=${mode}px
159
 
                           ;;
160
 
                /lib64/ld[.-]*) mode=${mode}px
161
 
                           ;;
162
 
                *) mode=${mode}ix
163
 
                           ;;
164
 
                esac
165
 
                dynlibs="$dynlibs ${libpath}:${mode}"
166
 
        done
167
 
}
168
 
 
169
 
runtestbg()
170
 
{
171
 
        if [ -z "${__NO_TRAP_ERR}" ]
172
 
        then
173
 
                trap "error_handler" ERR
174
 
        fi
175
 
 
176
 
        # global _testdesc _pfmode _pid outfile
177
 
        
178
 
        _testdesc=$1
179
 
        _pfmode=$2
180
 
        shift 2
181
 
        
182
 
        genrunscript "$@"
183
 
        
184
 
        $testexec "$@" > $outfile 2>&1 &
185
 
        
186
 
        _pid=$!
187
 
}
188
 
 
189
 
checktestbg()
190
 
{
191
 
        # global _pid _rc outfile 
192
 
        local rc
193
 
 
194
 
        wait $_pid
195
 
        rc=$?
196
 
        if [ $rc -gt 128 ]
197
 
        then
198
 
                echo "SIGNAL$(($rc - 128))" > $outfile
199
 
        fi
200
 
        checktestfg
201
 
}
202
 
 
203
 
runtestfg()
204
 
{
205
 
        # global _testdesc _pfmode outfile
206
 
        _testdesc=$1
207
 
        _pfmode=$2
208
 
        shift 2
209
 
        
210
 
        genrunscript "$@"
211
 
        
212
 
        $testexec "$@" > $outfile 2>&1
213
 
        test_rc=$?
214
 
        if [ $test_rc -gt 128 ]
215
 
        then
216
 
                echo "SIGNAL$(($test_rc - 128))" > $outfile
217
 
        fi
218
 
}
219
 
 
220
 
checktestfg()
221
 
{
222
 
        # global _pfmode _testdesc outfile teststatus testname
223
 
        local ret expectedsig killedsig
224
 
 
225
 
        ret=`cat $outfile 2>/dev/null`
226
 
        teststatus=pass
227
 
        
228
 
        case "$ret" in
229
 
                PASS)   if [ "$_pfmode" != "pass" ]
230
 
                        then
231
 
                                echo "Error: ${testname} passed. Test '${_testdesc}' was expected to '${_pfmode}'"
232
 
                                testfailed
233
 
                        fi
234
 
                        ;;
235
 
                FAIL*)  if [ "$_pfmode" != "fail" ]
236
 
                        then
237
 
                                echo "Error: ${testname} failed. Test '${_testdesc}' was expected to '${_pfmode}'. Reason for failure '${ret}'"
238
 
                                testfailed
239
 
                        fi
240
 
                        ;;
241
 
                SIGNAL*) killedsig=`echo $ret | sed 's/SIGNAL//'`
242
 
                        case "$_pfmode" in
243
 
                        signal*) expectedsig=`echo $_pfmode | sed 's/signal//'`
244
 
                                if [ -n "${expectedsig}" -a ${expectedsig} != ${killedsig} ]
245
 
                                then
246
 
                                        echo "Error: ${testname} failed. Test '${_testdesc}' was expected to terminate with signal ${expectedsig}. Instead it terminated with signal ${killedsig}"
247
 
                                        testfailed
248
 
                                fi
249
 
                                ;;
250
 
                        *)      echo "Error: ${testname} failed. Test '${_testdesc}' was expected to '${_pfmode}'. Reason for failure 'killed by signal ${killedsig}'"
251
 
                                testfailed
252
 
                                ;;      
253
 
                        esac
254
 
                        ;;
255
 
                *)      testerror
256
 
                        ;;
257
 
        esac
258
 
}
259
 
 
260
 
runchecktest()
261
 
{
262
 
        if [ -z "${__NO_TRAP_ERR}" ]
263
 
        then
264
 
                trap "error_handler" ERR
265
 
        fi
266
 
 
267
 
        runtestfg "$@"
268
 
        checktestfg
269
 
}
270
 
 
271
 
runchecktest_errno()
272
 
{
273
 
        local errno=$(perl -MPOSIX -e 'printf "%d\n", '$1';')
274
 
        shift 1
275
 
 
276
 
        if [ -z "${__NO_TRAP_ERR}" ]
277
 
        then
278
 
                trap "error_handler" ERR
279
 
        fi
280
 
 
281
 
        runtestfg "$@"
282
 
        if [ "$test_rc" == "$errno" ] ; then
283
 
                checktestfg
284
 
        else
285
 
                echo "Error: ${testname} failed. Test '${_testdesc}' was expected to '${_pfmode}'. Reason for failure expect errno ${errno} != ${test_rc}"
286
 
                testfailed
287
 
        fi
288
 
}
289
 
 
290
 
emit_profile()
291
 
{
292
 
        if [ -z "${__NO_TRAP_ERR}" ]    
293
 
        then
294
 
                trap "error_handler" ERR
295
 
        fi
296
 
 
297
 
        local subprofile wflag
298
 
        #global name outfile profile dynlibs profilenames
299
 
 
300
 
        subprofile=0
301
 
        wflag=""
302
 
 
303
 
        case "$1" in 
304
 
                *^*) wflag="--nowarn"
305
 
                     subprofile=1
306
 
                     ;;
307
 
        esac
308
 
 
309
 
        mkflags="${wflag} ${escapeflag}"
310
 
 
311
 
        name=$1; perm=$2; shift 2
312
 
 
313
 
        if [ "$subprofile" -eq 1 -o "$nodefaults" -eq 1 ]
314
 
        then
315
 
                # skip dynamic libs for subprofiles
316
 
                $bin/mkprofile.pl ${mkflags} $name ${outfile}:w "$@" >> $profile
317
 
        else
318
 
                $bin/mkprofile.pl ${mkflags} $name ${name}:${perm} $dynlibs ${outfile}:w "$@" >> $profile
319
 
        fi
320
 
 
321
 
        echo $name >> $profilenames
322
 
}
323
 
                
324
 
genprofile()
325
 
{
326
 
if [ -z "${__NO_TRAP_ERR}" ]
327
 
then
328
 
        trap "error_handler" ERR
329
 
fi
330
 
        
331
 
        local num_emitted imagename hat args arg names1 names2
332
 
        #global complainflag escapeflag nodefaults profile profilenames
333
 
 
334
 
        complainflag=""
335
 
        escapeflag=""
336
 
        nodefaults=0
337
 
        while /bin/true 
338
 
        do
339
 
                case "$1" in 
340
 
                        "-C") complainflag="-C"
341
 
                              ;;
342
 
                        "-E") escapeflag="-E"
343
 
                              ;;
344
 
                        "-N") nodefaults=1
345
 
                              ;;
346
 
                        *) break
347
 
                           ;;
348
 
                esac
349
 
                shift
350
 
        done
351
 
 
352
 
        # save previous profile
353
 
        if [ -f $profile ] 
354
 
        then
355
 
                mv $profile ${profile}.old
356
 
                mv $profilenames ${profilenames}.old
357
 
        fi
358
 
 
359
 
        num_emitted=0
360
 
 
361
 
        while /bin/true
362
 
        do
363
 
                imagename=$test
364
 
                imageperm=rix
365
 
 
366
 
                # image/subhat allows overriding of the default
367
 
                # imagename which is based on the testname
368
 
                #
369
 
                # it is most often used after --, in fact it is basically
370
 
                # mandatory after --
371
 
                case "$1" in
372
 
                        image=*) imagename=`echo $1 | sed 's/^image=[rix]*//'`
373
 
                                 if [ ! -x "$imagename" ]
374
 
                                 then
375
 
                                        fatalerror "invalid imagename specified in input '$1'"
376
 
                                 fi
377
 
                                 perm=`echo $1 | sed -n 's/^image=\([rix]*\).*$/\1/p'`
378
 
                                 if [ -n "$perm" ]
379
 
                                 then
380
 
                                        imageperm=$perm
381
 
                                 fi
382
 
                                 num_emitted=0
383
 
                                 shift
384
 
                                 ;;
385
 
                        subhat=*) fatalerror "'subhat=hatname' is no longer supported ('$1')"
386
 
                                 shift
387
 
                                 ;;
388
 
                esac
389
 
 
390
 
                num_args=0
391
 
                while [ $# -gt 0 ]
392
 
                do
393
 
                        arg="$1"
394
 
                        shift
395
 
 
396
 
                        # -- is the seperator between profiles
397
 
                        if [ "$arg" == "--" ]
398
 
                        then
399
 
                                eval emit_profile \"$imagename\" \"$imageperm\" \
400
 
                                        $(for i in $(seq 0 $((${num_args} - 1))) ; do echo \"\${args[${i}]}\" ; done)
401
 
                                num_emitted=$((num_emitted + 1))
402
 
                                num_args=0
403
 
                                continue 2
404
 
                        else
405
 
                                args[${num_args}]=${arg}
406
 
                                num_args=$(($num_args + 1))
407
 
                        fi
408
 
                done
409
 
 
410
 
                # output what is in args, or force empty profile
411
 
                if [ -n "$args" -o $num_emitted -eq 0 ] ; then
412
 
                        eval emit_profile \"$imagename\" \"$imageperm\" \
413
 
                                $(for i in $(seq 0 $((${num_args} - 1))) ; do echo \"\${args[${i}]}\" ; done)
414
 
                fi
415
 
 
416
 
                break
417
 
        done
418
 
 
419
 
        # if old and new profiles consist of the same entries
420
 
        # we can do a replace, else remove/reload
421
 
        if [ $profileloaded -eq 1 ]
422
 
        then
423
 
                names1=$tmpdir/sorted1
424
 
                names2=$tmpdir/sorted2
425
 
                sort $profilenames > $names1
426
 
                sort ${profilenames}.old > $names2
427
 
 
428
 
                if cmp -s $names1 $names2
429
 
                then
430
 
                        replaceprofile
431
 
                else    
432
 
                        removeprofile ${profile}.old
433
 
                        loadprofile
434
 
                fi
435
 
 
436
 
                rm -f $names1 $names2
437
 
        
438
 
        else
439
 
                loadprofile
440
 
        fi
441
 
 
442
 
        rm -f ${profile}.old ${profilenames}.old
443
 
}
444
 
 
445
 
loadprofile()
446
 
{
447
 
        #global complainflaf profile profileloaded
448
 
 
449
 
        $subdomain ${parser_args} $complainflag < $profile > /dev/null
450
 
        if [ $? -ne 0 ]
451
 
        then
452
 
                removeprofile
453
 
                fatalerror "Unable to load profile $profile"
454
 
        else
455
 
                profileloaded=1
456
 
        fi
457
 
}
458
 
 
459
 
replaceprofile()
460
 
{
461
 
        #global complainflag profile
462
 
 
463
 
        $subdomain ${parser_args} -r $complainflag < $profile > /dev/null
464
 
        if [ $? -ne 0 ]
465
 
        then
466
 
                fatalerror "Unable to replace profile $profile"
467
 
        fi
468
 
}
469
 
 
470
 
removeprofile()
471
 
{
472
 
        local remprofile
473
 
        #global profile profileloaded
474
 
 
475
 
        if [ -f "$1" ]
476
 
        then
477
 
                remprofile=$1
478
 
        else
479
 
                remprofile=$profile
480
 
        fi
481
 
 
482
 
        $subdomain ${parser_args} -R < $remprofile > /dev/null
483
 
        if [ $? -ne 0 ]
484
 
        then
485
 
                fatalerror "Unable to remove profile $remprofile"
486
 
        else
487
 
                profileloaded=0
488
 
        fi
489
 
}
490
 
 
491
 
settest()
492
 
{
493
 
        if [ -z "${__NO_TRAP_ERR}" ]
494
 
        then
495
 
                trap "error_handler" ERR
496
 
        fi
497
 
 
498
 
        #global test testname testexec outfile profileloaded
499
 
 
500
 
        #testname is the basename of the test, i,e 'open'
501
 
        #test is the full path to the test executable.
502
 
        #testexec is the path than will be run, normally this is the same
503
 
        #  as $test, but occasionally, you may want to invoke a wrapper which
504
 
        #  will run the test. In this case 'settest <testname> "wrapper {}'
505
 
        #  will result in testexec invoking wrapper. {} will be replaced with 
506
 
        #  $test 
507
 
 
508
 
        testname=$1
509
 
 
510
 
        if [ $# -eq 1 ]
511
 
        then
512
 
                test=$bin/$1
513
 
                testexec=$test
514
 
        elif [ $# -eq 2 ]
515
 
        then
516
 
                test=$bin/$1
517
 
                testexec=`echo $2 | sed "s~{}~$test~"`
518
 
        else
519
 
                fatalerror "settest, illegal usage"
520
 
        fi
521
 
 
522
 
        outfile=$tmpdir/output.$1
523
 
 
524
 
        if [ -x $test ]
525
 
        then
526
 
                # build list of dynamic libraries required by this executable
527
 
                resolve_libs $test
528
 
        fi
529
 
 
530
 
        # Remove any current profile if loaded
531
 
        if [ $profileloaded -eq 1 ]
532
 
        then
533
 
                removeprofile
534
 
        fi
535
 
}
536
 
 
537
 
# ----------------------------------------------------------------------------
538
 
 
539
 
# MAIN
540
 
 
541
 
trap "exit_handler" EXIT
542
 
trap "error_handler" ERR 2> /dev/null
543
 
if [ $? -ne 0 ]
544
 
then
545
 
        __NO_TRAP_ERR="true"
546
 
fi
547
 
 
548
 
 
549
 
if [ `whoami` != "root" ]
550
 
then
551
 
        fatalerror "Must be root to run $0"
552
 
fi
553
 
 
554
 
if [ ! -d "$bin" ]
555
 
then
556
 
        fatalerror "$0 requires \$bin pointing to binary directory"
557
 
fi
558
 
 
559
 
# parse arguments. 
560
 
# -r/-retain: flag to retain last failing testcase in tmpdir
561
 
if [ "$1" == "-retain" -o "$1" == "-r" ]
562
 
then
563
 
        retaintmpdir=true
564
 
else
565
 
        retaintmpdir=false
566
 
fi
567
 
 
568
 
# load user changeable variables
569
 
. $bin/uservars.inc
570
 
 
571
 
if [ ! -x $subdomain ]
572
 
then
573
 
        fatalerror "AppArmor parser '$subdomain' is not executable"
574
 
fi
575
 
 
576
 
profileloaded=0
577
 
 
578
 
tmpdir=$(mktemp -d $tmpdir-XXXXXX)
579
 
chmod 755 ${tmpdir}
580
 
export tmpdir
581
 
 
582
 
#set initial testname based on name of script
583
 
settest `basename $0 | sed 's/\.sh$//'`
584
 
 
585
 
profile=$tmpdir/profile
586
 
profilenames=$tmpdir/profile.names
587
 
num_testfailures=0      # exit code of script is set to #failures