2
# These tests will stop running as soon as a failure is seen since they tend to build
3
# on the actions and results of the prior tests.
6
# This test requires introspection
7
if [ ! -d /sys/kernel/security/apparmor ]; then
8
echo "WARNING: /sys/kernel/security/apparmor does not exist. Skipping tests"
9
echo "requiring introspection."
14
basedir=$(mktemp -d -t aa-cache-XXXXXX)
15
trap "rm -rf $basedir" EXIT
16
mkdir -p $basedir/cache
18
ARGS="--base $basedir --skip-kernel-load"
21
cp caching.profile $basedir/$profile
23
# Detect and slow down cache test when filesystem can't represent nanosecond delays.
28
TIMES=$(stat $basedir/test1 $basedir/test2 -c %z | cut -d" " -f2 | cut -d. -f2 | sort -u | wc -l)
29
if [ $TIMES -ne 2 ]; then
30
echo "WARNING: $basedir lacks nanosecond timestamp resolution, falling back to slower test"
33
rm -f $basedir/test1 $basedir/test2
35
echo -n "Profiles are not cached by default: "
36
../apparmor_parser $ARGS -q -r $basedir/$profile
37
[ -f $basedir/cache/$profile ] && echo "FAIL ($basedir/cache/$profile exists)" && exit 1
40
echo -n "Profiles are not cached when using --skip-cache: "
41
../apparmor_parser $ARGS -q --write-cache --skip-cache -r $basedir/$profile
42
[ -f $basedir/cache/$profile ] && echo "FAIL ($basedir/cache/$profile exists)" && exit 1
47
echo -n "Profiles are cached when requested: "
48
../apparmor_parser $ARGS -q --write-cache -r $basedir/$profile
49
[ ! -f $basedir/cache/$profile ] && echo "FAIL ($basedir/cache/$profile does not exist)" && exit 1
52
echo -n "Kernel features are written to cache: "
53
[ ! -f $basedir/cache/.features ] && echo "FAIL ($basedir/cache/.features missing)" && exit 1
54
read CF < $basedir/cache/.features || true
55
read KF < /sys/kernel/security/apparmor/features || true
56
[ "$CF" != "$KF" ] && echo "FAIL (feature text mismatch: cache '$CF' vs kernel '$KF')" && exit 1
59
echo -n "Cache is loaded when it exists and features match: "
60
../apparmor_parser $ARGS -v -r $basedir/$profile | grep -q 'Cached reload succeeded' || { echo "FAIL"; exit 1; }
63
echo -n "Cache is not loaded when skipping is requested: "
64
../apparmor_parser $ARGS -v --skip-read-cache -r $basedir/$profile | grep -q 'Replacement succeeded for' || { echo "FAIL"; exit 1; }
65
../apparmor_parser $ARGS -v --skip-cache -r $basedir/$profile | grep -q 'Replacement succeeded for' || { echo "FAIL"; exit 1; }
68
echo -n "Cache reading is skipped when features do not match cache: "
69
echo -n "monkey" > $basedir/cache/.features
70
../apparmor_parser $ARGS -v -r $basedir/$profile | grep -q 'Replacement succeeded for' || { echo "FAIL"; exit 1; }
73
echo -n "Cache writing is skipped when features do not match cache: "
74
rm $basedir/cache/$profile
75
../apparmor_parser $ARGS -v --write-cache -r $basedir/$profile | grep -q 'Replacement succeeded for' || { echo "FAIL"; exit 1; }
76
[ -f $basedir/cache/$profile ] && echo "FAIL ($basedir/cache/$profile exists)" && exit 1
79
echo -n "Profiles are cached when requested (again): "
80
rm -f $basedir/cache/.features || true
81
rm -f $basedir/cache/$profile || true
82
../apparmor_parser $ARGS -q --write-cache -r $basedir/$profile
83
[ ! -f $basedir/cache/$profile ] && echo "FAIL ($basedir/cache/$profile does not exist)" && exit 1
86
echo -n "Cache reading is skipped when profile is newer: "
88
touch $basedir/$profile
89
../apparmor_parser $ARGS -v -r $basedir/$profile | grep -q 'Replacement succeeded for' || { echo "FAIL"; exit 1; }
92
echo -n "Cache is used when cache is newer: "
94
touch $basedir/cache/$profile
95
../apparmor_parser $ARGS -v -r $basedir/$profile | grep -q 'Cached reload succeeded' || { echo "FAIL"; exit 1; }
98
echo -n "Cache reading is skipped when parser is newer: "
100
cp ../apparmor_parser $basedir/parser/
101
$basedir/parser/apparmor_parser $ARGS -v -r $basedir/$profile | grep -q 'Replacement succeeded for' || { echo "FAIL"; exit 1; }
104
echo -n "Cache reading is skipped when parser in \$PATH is newer: "
105
(PATH=$basedir/parser/ /bin/sh -c "apparmor_parser $ARGS -v -r $basedir/$profile") | grep -q 'Replacement succeeded for' || { echo "FAIL"; exit 1; }