tests: factor 350 fail=0 initializations into test-lib.sh
[platform/upstream/coreutils.git] / tests / misc / chcon
1 #!/bin/sh
2 # exercise chcon
3
4 if test "$VERBOSE" = yes; then
5   set -x
6   chcon --version
7 fi
8
9 . $srcdir/test-lib.sh
10 require_root_
11 require_selinux_
12 skip_if_mcstransd_is_running_
13
14 mkdir -p d/sub/s2 || framework_failure
15 touch f g d/sub/1 d/sub/2 || framework_failure
16
17
18 # Set to a specified context.
19 # Use root:object_r:tmp_t:s0.  It is special in that
20 # it works even when mcstransd isn't running.
21 u1=root
22 r1=object_r
23 t1=tmp_t
24 range=s0
25 ctx=$u1:$r1:$t1:$range
26 chcon $ctx f || fail=1
27 stat --printf='f|%C\n' f > out || fail=1
28
29 # Use --reference.
30 chcon --ref=f g || fail=1
31 stat --printf='g|%C\n' g >> out || fail=1
32
33 # Change the individual parts of the context, one by one.
34 u2=user_u
35 r2=object_r
36 t2=file_t
37 for i in --user=$u2 --role=$r2 --type=$t2 --range=$range; do
38   chcon $i f || fail=1
39   stat --printf="f|$i|"'%C\n' f >> out || fail=1
40 done
41
42 # Same, but change back using the short-named options.
43 for i in -u$u1 -r$r1 -t$t1; do
44   chcon $i f || fail=1
45   stat --printf="f|$i|"'%C\n' f >> out || fail=1
46 done
47
48 cat <<EOF > exp || fail=1
49 f|$ctx
50 g|$ctx
51 f|--user=$u2|$u2:$r1:$t1:$range
52 f|--role=$r2|$u2:$r2:$t1:$range
53 f|--type=$t2|$u2:$r2:$t2:$range
54 f|--range=$range|$u2:$r2:$t2:$range
55 f|-uroot|root:object_r:file_t:$range
56 f|-robject_r|root:object_r:file_t:$range
57 f|-ttmp_t|root:object_r:tmp_t:$range
58 EOF
59
60 compare out exp || fail=1
61
62 chcon --verbose -u$u1 f > out || fail=1
63 echo 'changing security context of `f'\' > exp
64 compare out exp || fail=1
65
66 Exit $fail