tests: avoid false failure with unicode decomposed file systems
[coreutils.git] / tests / cp / no-ctx.sh
blob16b86b4cb07efd2ebbc186a0e92f15af3149ac4d
1 #!/bin/sh
2 # Ensure we handle file systems returning no SELinux context,
3 # which triggered a segmentation fault in coreutils-8.22.
4 # This test is skipped on systems that lack LD_PRELOAD support; that's fine.
5 # Similarly, on a system that lacks lgetfilecon altogether, skipping it is fine.
7 # Copyright (C) 2014-2024 Free Software Foundation, Inc.
9 # This program is free software: you can redistribute it and/or modify
10 # it under the terms of the GNU General Public License as published by
11 # the Free Software Foundation, either version 3 of the License, or
12 # (at your option) any later version.
14 # This program is distributed in the hope that it will be useful,
15 # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 # GNU General Public License for more details.
19 # You should have received a copy of the GNU General Public License
20 # along with this program. If not, see <https://www.gnu.org/licenses/>.
22 . "${srcdir=.}/tests/init.sh"; path_prepend_ ./src
23 print_ver_ cp
24 require_gcc_shared_
25 require_selinux_
27 # Replace each getfilecon and lgetfilecon call with a call to these stubs.
28 cat > k.c <<'EOF' || framework_failure_
29 #include <stdio.h>
30 #include <selinux/selinux.h>
31 #include <errno.h>
33 int getfilecon (const char *path, char **con)
35 /* Leave a marker so we can identify if the function was intercepted. */
36 fclose(fopen("preloaded", "w"));
38 errno=ENODATA;
39 return -1;
42 int getfilecon_raw (const char *path, char **con)
43 { return getfilecon (path, con); }
45 int lgetfilecon (const char *path, char **con)
46 { return getfilecon (path, con); }
48 int lgetfilecon_raw (const char *path, char **con)
49 { return getfilecon (path, con); }
50 EOF
52 # Then compile/link it:
53 gcc_shared_ k.c k.so \
54 || skip_ 'failed to build SELinux shared library'
56 touch file_src
58 # New file with SELinux context optionally included
59 LD_PRELOAD=$LD_PRELOAD:./k.so cp -a file_src file_dst || fail=1
61 # Existing file with SELinux context optionally included
62 LD_PRELOAD=$LD_PRELOAD:./k.so cp -a file_src file_dst || fail=1
64 # ENODATA should give an immediate error when required to preserve ctx
65 # This is debatable, and maybe we should not fail when no context available?
66 ( export LD_PRELOAD=$LD_PRELOAD:./k.so
67 returns_ 1 cp --preserve=context file_src file_dst ) || fail=1
69 test -e preloaded || skip_ 'LD_PRELOAD interception failed'
71 Exit $fail