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

« back to all changes in this revision

Viewing changes to .pc/0056-tests-Add-an-optional-final-check-to-checktestfg.patch/tests/regression/apparmor/prologue.inc

  • Committer: Package Import Robot
  • Author(s): Tyler Hicks, Tyler Hicks, Jamie Strandboge
  • Date: 2013-08-26 15:32:12 UTC
  • mfrom: (49.1.6 saucy-proposed)
  • Revision ID: package-import@ubuntu.com-20130826153212-5oehf9eda64lkpze
Tags: 2.8.0-0ubuntu25
[ Tyler Hicks ]
* Add support for mediation of D-Bus messages and services. AppArmor D-Bus
  rules are described in the apparmor.d(5) man page. dbus-daemon will use
  libapparmor to perform queries against the AppArmor policies to determine
  if a connection should be able to send messages to another connection, if
  a connection should be able to receive messages from another connection,
  and if a connection should be able to bind to a well-known name.
  - 0042-Fix-mount-rule-preprocessor-output.patch,
    0043-libapparmor-Safeguard-aa_getpeercon-buffer-reallocat.patch,
    0044-libapparmor-fix-return-value-of-aa_getpeercon_raw.patch,
    0045-libapparmor-Move-mode-parsing-into-separate-function.patch,
    0046-libapparmor-Parse-mode-from-confinement-string-in-ge.patch,
    0047-libapparmor-Make-aa_getpeercon_raw-similar-to-aa_get.patch,
    0048-libapparmor-Update-aa_getcon-man-page-to-reflect-get.patch:
    Backport parser and libapparmor pre-requisites for D-Bus mediation
  - 0049-parser-Update-man-page-for-DBus-rules.patch: Update apparmor.d man
    page
  - 0050-parser-Add-support-for-DBus-rules.patch,
    0051-parser-Regression-tests-for-DBus-rules.patch,
    0052-parser-Binary-profile-equality-tests-for-DBus-rules.patch: Add
    apparmor_parser support for D-Bus mediation rules
  - 0053-libapparmor-Export-a-label-based-query-interface.patch,
    debian/libapparmor1.symbols: Provide the libapparmor interface necessary
    for trusted helpers to make security decisions based upon AppArmor
    policy
  - 0054-libaalogparse-Parse-dbus-daemon-audit-messages.patch,
    0055-libaalogparse-Regression-tests-for-dbus-daemon-audit.patch: Allow
    applications to parse denials, generated by dbus-daemon, using
    libaalogparse and add a set of regression tests
  - 0056-tests-Add-an-optional-final-check-to-checktestfg.patch,
    0057-tests-Add-required-features-check.patch,
    0058-tests-Add-regression-tests-for-dbus.patch: Add regression tests
    which start their own dbus-daemon, load profiles containing D-Bus rules,
    and confine simple D-Bus service and client applications
  - 0059-dbus-rules-for-dbus-abstractions.patch: Add bus-specific, but
    otherwise permissive, D-Bus rules to the dbus and dbus-session
    abstractions. Confined applications that use D-Bus should already be
    including these abstractions in their profiles so this should be a
    seamless transition for those profiles.
* 0060-utils-make_clean_fixup.patch: Clean up the Python cache in the
  AppArmor tests directory
* 0061-profiles-dnsmasq-needs-dbus-abstraction.patch: Dnsmasq uses the
  system D-Bus when it is started with --enable-dbus, so its AppArmor
  profile needs to include the system bus abstraction
* 0062-fix-clone-test-on-arm.patch: Fix compiler error when building
  regression tests on ARM
* 0063-utils-ignore-unsupported-rules.patch: Utilities that use the
  Immunix::AppArmor perl module, such as aa-logprof and aa-genprof, error
  out when they encounter rules unsupported by the perl module. This patch
  ignores unsupported rules.

[ Jamie Strandboge ]
* debian/control: don't have easyprof Depends on apparmor-easyprof-ubuntu

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
        _pid=$!
 
152
        wait $_pid
 
153
        test_rc=$?
 
154
        if [ $test_rc -gt 128 ]
 
155
        then
 
156
                echo "SIGNAL$(($test_rc - 128))" > $outfile
 
157
        fi
 
158
}
 
159
 
 
160
checktestfg()
 
161
{
 
162
        # global _pfmode _known _testdesc outfile teststatus testname
 
163
        local ret expectedsig killedsig
 
164
 
 
165
        ret=`cat $outfile 2>/dev/null`
 
166
        teststatus=pass
 
167
        
 
168
        case "$ret" in
 
169
                PASS)   if [ "$_pfmode" != "pass" -a -z "${_known}" ]
 
170
                        then
 
171
                                echo "Error: ${testname} passed. Test '${_testdesc}' was expected to '${_pfmode}'"
 
172
                                testfailed
 
173
                                return
 
174
                        elif [ "$_pfmode" == "pass" -a -n "${_known}" ]
 
175
                        then
 
176
                                echo "Alert: ${testname} passed. Test '${_testdesc}' was marked as expected pass but known problem (xpass)"
 
177
                        fi
 
178
                        ;;
 
179
                FAIL*)  if [ "$_pfmode" != "fail" -a -z "${_known}" ]
 
180
                        then
 
181
                                echo "Error: ${testname} failed. Test '${_testdesc}' was expected to '${_pfmode}'. Reason for failure '${ret}'"
 
182
                                testfailed
 
183
                                return
 
184
                        elif [ "$_pfmode" == "fail" -a -n "${_known}" ]
 
185
                        then
 
186
                                echo "Alert: ${testname} failed. Test '${_testdesc}' was marked as expected fail but known problem (xfail)."
 
187
                        fi
 
188
                        ;;
 
189
                SIGNAL*) killedsig=`echo $ret | sed 's/SIGNAL//'`
 
190
                        case "$_pfmode" in
 
191
                        signal*) expectedsig=`echo ${_pfmode} | sed 's/signal//'`
 
192
                                if [ -n "${expectedsig}" -a ${expectedsig} != ${killedsig} ]
 
193
                                then
 
194
                                        echo "Error: ${testname} failed. Test '${_testdesc}' was expected to terminate with signal ${expectedsig}${_known}. Instead it terminated with signal ${killedsig}"
 
195
                                        testfailed
 
196
                                        return
 
197
                                fi
 
198
                                ;;
 
199
                        *)      echo "Error: ${testname} failed. Test '${_testdesc}' was expected to '${_pfmode}'${_known}. Reason for failure 'killed by signal ${killedsig}'"
 
200
                                testfailed
 
201
                                return
 
202
                                ;;      
 
203
                        esac
 
204
                        ;;
 
205
                *)      testerror
 
206
                        return
 
207
                        ;;
 
208
        esac
 
209
 
 
210
        if [ -n "$VERBOSE" ]; then
 
211
                echo "ok: ${_testdesc}"
 
212
        fi
 
213
}
 
214
 
 
215
runchecktest()
 
216
{
 
217
        if [ -z "${__NO_TRAP_ERR}" ]
 
218
        then
 
219
                trap "error_handler" ERR
 
220
        fi
 
221
 
 
222
        runtestfg "$@"
 
223
        checktestfg
 
224
}
 
225
 
 
226
runchecktest_errno()
 
227
{
 
228
        local errno=$(perl -MPOSIX -e 'printf "%d\n", '$1';')
 
229
        shift 1
 
230
 
 
231
        if [ -z "${__NO_TRAP_ERR}" ]
 
232
        then
 
233
                trap "error_handler" ERR
 
234
        fi
 
235
 
 
236
        runtestfg "$@"
 
237
        if [ "$test_rc" == "$errno" ] ; then
 
238
                checktestfg
 
239
        else
 
240
                echo "Error: ${testname} failed. Test '${_testdesc}' was expected to '${_pfmode}'${_known}. Reason for failure expect errno ${errno} != ${test_rc}"
 
241
                testfailed
 
242
        fi
 
243
}
 
244
 
 
245
emit_profile()
 
246
{
 
247
        if [ -z "${__NO_TRAP_ERR}" ]    
 
248
        then
 
249
                trap "error_handler" ERR
 
250
        fi
 
251
 
 
252
        #global name outfile profile profilenames
 
253
 
 
254
        name=$1; shift 1
 
255
 
 
256
        $bin/mkprofile.pl ${mkflags} "$name" ${outfile}:w "$@" >> $profile
 
257
 
 
258
        echo $name >> $profilenames
 
259
}
 
260
                
 
261
genprofile()
 
262
{
 
263
if [ -z "${__NO_TRAP_ERR}" ]
 
264
then
 
265
        trap "error_handler" ERR
 
266
fi
 
267
        
 
268
        local num_emitted imagename hat args arg names1 names2
 
269
        #global complainflag escapeflag nodefaults profile profilenames
 
270
 
 
271
        complainflag=""
 
272
        mkflags=""
 
273
        while /bin/true
 
274
        do
 
275
                case "$1" in
 
276
                        "-C") complainflag="-C"
 
277
                              ;;
 
278
                        "-E") mkflags="${mkflags} -E"
 
279
                              ;;
 
280
                        "-N") mkflags="${mkflags} -N"
 
281
                              ;;
 
282
                        "-I") mkflags="${mkflags} -I"
 
283
                              ;;
 
284
                        *) break
 
285
                           ;;
 
286
                esac
 
287
                shift
 
288
        done
 
289
 
 
290
        # save previous profile
 
291
        if [ -f $profile ]
 
292
        then
 
293
                mv $profile ${profile}.old
 
294
                mv $profilenames ${profilenames}.old
 
295
        fi
 
296
 
 
297
        num_emitted=0
 
298
 
 
299
        while /bin/true
 
300
        do
 
301
                imagename=$test
 
302
 
 
303
                # image/subhat allows overriding of the default
 
304
                # imagename which is based on the testname
 
305
                #
 
306
                # it is most often used after --, in fact it is basically
 
307
                # mandatory after --
 
308
                case "$1" in
 
309
                        image=*) imagename=`echo $1 | sed 's/^image=\([^:]*\).*$/\1/'`
 
310
                                 if [ ! -x "$imagename" ]
 
311
                                 then
 
312
                                        fatalerror "invalid imagename specified in input '$1'"
 
313
                                 fi
 
314
                                 num_emitted=0
 
315
                                 shift
 
316
                                 ;;
 
317
                        subhat=*) fatalerror "'subhat=hatname' is no longer supported ('$1')"
 
318
                                 shift
 
319
                                 ;;
 
320
                esac
 
321
 
 
322
                num_args=0
 
323
                while [ $# -gt 0 ]
 
324
                do
 
325
                        arg="$1"
 
326
                        shift
 
327
 
 
328
                        # -- is the separator between profiles
 
329
                        if [ "$arg" == "--" ]
 
330
                        then
 
331
                                eval emit_profile \"$imagename\" \
 
332
                                        $(for i in $(seq 0 $((${num_args} - 1))) ; do echo \"\${args[${i}]}\" ; done)
 
333
                                num_emitted=$((num_emitted + 1))
 
334
                                num_args=0
 
335
                                continue 2
 
336
                        else
 
337
                                args[${num_args}]=${arg}
 
338
                                num_args=$(($num_args + 1))
 
339
                        fi
 
340
                done
 
341
 
 
342
                # output what is in args, or force empty profile
 
343
                if [ -n "$args" -o $num_emitted -eq 0 ] ; then
 
344
                        eval emit_profile \"$imagename\" \
 
345
                                $(for i in $(seq 0 $((${num_args} - 1))) ; do echo \"\${args[${i}]}\" ; done)
 
346
                fi
 
347
 
 
348
                break
 
349
        done
 
350
 
 
351
        # if old and new profiles consist of the same entries
 
352
        # we can do a replace, else remove/reload
 
353
        if [ $profileloaded -eq 1 ]
 
354
        then
 
355
                names1=$tmpdir/sorted1
 
356
                names2=$tmpdir/sorted2
 
357
                sort $profilenames > $names1
 
358
                sort ${profilenames}.old > $names2
 
359
 
 
360
                if cmp -s $names1 $names2
 
361
                then
 
362
                        replaceprofile
 
363
                else    
 
364
                        removeprofile ${profile}.old
 
365
                        loadprofile
 
366
                fi
 
367
 
 
368
                rm -f $names1 $names2
 
369
        
 
370
        else
 
371
                loadprofile
 
372
        fi
 
373
 
 
374
        if [ -e ${sys_profiles} ] ; then
 
375
                #check to see if the profiles are actually loaded
 
376
                for f in `cat $profilenames` ; do
 
377
                        grep -Eq "^$f"' \([^)]+\)$' ${sys_profiles}
 
378
                        rc=$?
 
379
                        if [ $rc -ne 0 ] ; then
 
380
                                echo "Genprofile failed to load profile \"$f\""
 
381
                                exit 1
 
382
                        fi
 
383
                done
 
384
        fi
 
385
 
 
386
        rm -f ${profile}.old ${profilenames}.old
 
387
}
 
388
 
 
389
loadprofile()
 
390
{
 
391
        #global complainflaf profile profileloaded
 
392
 
 
393
        $subdomain ${parser_args} $complainflag $profile > /dev/null
 
394
        if [ $? -ne 0 ]
 
395
        then
 
396
                removeprofile
 
397
                fatalerror "Unable to load profile $profile"
 
398
        else
 
399
                profileloaded=1
 
400
        fi
 
401
}
 
402
 
 
403
replaceprofile()
 
404
{
 
405
        #global complainflag profile
 
406
 
 
407
        $subdomain ${parser_args} -r $complainflag $profile > /dev/null
 
408
        if [ $? -ne 0 ]
 
409
        then
 
410
                fatalerror "Unable to replace profile $profile"
 
411
        fi
 
412
}
 
413
 
 
414
removeprofile()
 
415
{
 
416
        local remprofile
 
417
        #global profile profileloaded
 
418
 
 
419
        if [ -f "$1" ]
 
420
        then
 
421
                remprofile=$1
 
422
        else
 
423
                remprofile=$profile
 
424
        fi
 
425
 
 
426
        $subdomain ${parser_args} -R $remprofile > /dev/null
 
427
        if [ $? -ne 0 ]
 
428
        then
 
429
                fatalerror "Unable to remove profile $remprofile"
 
430
        else
 
431
                profileloaded=0
 
432
        fi
 
433
}
 
434
 
 
435
settest()
 
436
{
 
437
        if [ -z "${__NO_TRAP_ERR}" ]
 
438
        then
 
439
                trap "error_handler" ERR
 
440
        fi
 
441
 
 
442
        #global test testname testexec outfile profileloaded
 
443
 
 
444
        #testname is the basename of the test, i,e 'open'
 
445
        #test is the full path to the test executable.
 
446
        #testexec is the path than will be run, normally this is the same
 
447
        #  as $test, but occasionally, you may want to invoke a wrapper which
 
448
        #  will run the test. In this case 'settest <testname> "wrapper {}'
 
449
        #  will result in testexec invoking wrapper. {} will be replaced with 
 
450
        #  $test 
 
451
 
 
452
        testname=$1
 
453
 
 
454
        if [ $# -eq 1 ]
 
455
        then
 
456
                test=$bin/$1
 
457
                testexec=$test
 
458
        elif [ $# -eq 2 ]
 
459
        then
 
460
                test=$bin/$1
 
461
                testexec=`echo $2 | sed "s~{}~$test~"`
 
462
        else
 
463
                fatalerror "settest, illegal usage"
 
464
        fi
 
465
 
 
466
        outfile=$tmpdir/output.$1
 
467
 
 
468
        # Remove any current profile if loaded
 
469
        if [ $profileloaded -eq 1 ]
 
470
        then
 
471
                removeprofile
 
472
        fi
 
473
}
 
474
 
 
475
# ----------------------------------------------------------------------------
 
476
 
 
477
# MAIN
 
478
 
 
479
trap "exit_handler" EXIT
 
480
trap "error_handler" ERR 2> /dev/null
 
481
if [ $? -ne 0 ]
 
482
then
 
483
        __NO_TRAP_ERR="true"
 
484
fi
 
485
 
 
486
 
 
487
if [ `whoami` != "root" ]
 
488
then
 
489
        fatalerror "Must be root to run $0"
 
490
fi
 
491
 
 
492
if [ ! -d "$bin" ]
 
493
then
 
494
        fatalerror "$0 requires \$bin pointing to binary directory"
 
495
fi
 
496
 
 
497
# parse arguments. 
 
498
# -r/-retain: flag to retain last failing testcase in tmpdir
 
499
if [ "$1" == "-retain" -o "$1" == "-r" ]
 
500
then
 
501
        retaintmpdir=true
 
502
else
 
503
        retaintmpdir=false
 
504
fi
 
505
 
 
506
# load user changeable variables
 
507
. $bin/uservars.inc
 
508
 
 
509
if [ ! -x $subdomain ]
 
510
then
 
511
        fatalerror "AppArmor parser '$subdomain' is not executable"
 
512
fi
 
513
 
 
514
profileloaded=0
 
515
 
 
516
tmpdir=$(mktemp -d $tmpdir-XXXXXX)
 
517
chmod 755 ${tmpdir}
 
518
export tmpdir
 
519
 
 
520
#set initial testname based on name of script
 
521
settest `basename $0 .sh`
 
522
 
 
523
profile=$tmpdir/profile
 
524
profilenames=$tmpdir/profile.names
 
525
num_testfailures=0      # exit code of script is set to #failures