1#!/bin/sh
2# test 'install -Z -D' and 'install -Z -d'
3# based on tests/mkdir/restorecon.sh
4
5# Copyright (C) 2013-2023 Free Software Foundation, Inc.
6
7# This program is free software: you can redistribute it and/or modify
8# it under the terms of the GNU General Public License as published by
9# the Free Software Foundation, either version 3 of the License, or
10# (at your option) any later version.
11
12# This program is distributed in the hope that it will be useful,
13# but WITHOUT ANY WARRANTY; without even the implied warranty of
14# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15# GNU General Public License for more details.
16
17# You should have received a copy of the GNU General Public License
18# along with this program.  If not, see <https://www.gnu.org/licenses/>.
19
20. "${srcdir=.}/tests/init.sh"; path_prepend_ ./src
21print_ver_ ginstall
22require_selinux_
23
24mkdir subdir || framework_failure_
25ctx='root:object_r:tmp_t'
26mls_enabled_ && ctx="$ctx:s0"
27chcon "$ctx" subdir || skip_ "Failed to set context: $ctx"
28cd subdir
29
30# Since in a tmp_t dir, dirs can be created as user_tmp_t ...
31touch standard || framework_failure_
32mkdir restored || framework_failure_
33if restorecon restored 2>/dev/null; then
34  # ... but when restored can be set to user_home_t
35  # So ensure the type for these mkdir -Z cases matches
36  # the directory type as set by restorecon.
37  ginstall -Z standard single || fail=1
38  ginstall -Z -d single_d || fail=1
39  # Run these as separate processes in case global context
40  # set for an arg, impacts on another arg
41  # TODO: Have the defaultcon() vary over these directories
42  for dst in single_d/existing/file multi/ple/file; do
43    ginstall -Z -D standard "$dst" || fail=1
44  done
45  restored_type=$(get_selinux_type 'restored')
46  test "$(get_selinux_type 'single')" = "$restored_type" || fail=1
47  test "$(get_selinux_type 'single_d')" = "$restored_type" || fail=1
48  test "$(get_selinux_type 'single_d/existing')" = "$restored_type" || fail=1
49  test "$(get_selinux_type 'multi')" = "$restored_type" || fail=1
50  test "$(get_selinux_type 'multi/ple')" = "$restored_type" || fail=1
51fi
52if test "$fail" = '1'; then
53  ls -UZd standard restored
54  ls -UZd single single_d single_d/existing multi multi/ple
55fi
56
57Exit $fail
58