~ubuntu-branches/ubuntu/saucy/apparmor/saucy-proposed

« back to all changes in this revision

Viewing changes to .pc/0020-coredump_tests.patch/tests/regression/apparmor/prologue.inc

  • Committer: Package Import Robot
  • Author(s): Steve Beattie, Micah Gersten, Steve Beattie
  • Date: 2012-10-09 12:44:56 UTC
  • Revision ID: package-import@ubuntu.com-20121009124456-ll1dupq93t5e5uah
Tags: 2.8.0-0ubuntu5
[ Micah Gersten ]
* Allow /etc/vdpau_wrapper.cfg r and /var/lib/xine/gxine.desktop r
  in the multimedia browser abstraction (LP: #1057642)
  - update profiles/apparmor.d/abstractions/ubuntu-browsers.d/multimedia

[ Steve Beattie ]
* debian/control: make libnotify-bin a Recommends rather than a
  Depends for use in server environments (LP: #1061879)
* debian/patches/0020-coredump_tests.patch: fix coredump regression
  tests (LP: #1050430)
* debian/patches/0021-webapps_abstraction.patch: add a few items
  triggered by using and installing webapps in firefox (LP: #1056418)
* debian/patches/0022-aa-decode-stdin.patch: fix aa-decode to process
  stdin correctly and decode encoded profiles names

Show diffs side-by-side

added added

removed removed

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