uchar C++ tests: Fix build error on FreeBSD 12.
[gnulib.git] / tests / test-copy-acl.sh
blob9f69d143733f44b1a9a7aee19185b0f0fb89e6c6
1 #!/bin/sh
3 # Show all commands when run with environment variable VERBOSE=yes.
4 test -z "$VERBOSE" || set -x
6 test "$USE_ACL" = 0 &&
8 echo "Skipping test: insufficient ACL support"
9 exit 77
12 # func_tmpdir
13 # creates a temporary directory.
14 # Sets variable
15 # - tmp pathname of freshly created temporary directory
16 func_tmpdir ()
18 # Use the environment variable TMPDIR, falling back to /tmp. This allows
19 # users to specify a different temporary directory, for example, if their
20 # /tmp is filled up or too small.
21 : ${TMPDIR=/tmp}
23 # Use the mktemp program if available. If not available, hide the error
24 # message.
25 tmp=`(umask 077 && mktemp -d "$TMPDIR/glXXXXXX") 2>/dev/null` &&
26 test -n "$tmp" && test -d "$tmp"
27 } ||
29 # Use a simple mkdir command. It is guaranteed to fail if the directory
30 # already exists. $RANDOM is bash specific and expands to empty in shells
31 # other than bash, ksh and zsh. Its use does not increase security;
32 # rather, it minimizes the probability of failure in a very cluttered /tmp
33 # directory.
34 tmp=$TMPDIR/gl$$-$RANDOM
35 (umask 077 && mkdir "$tmp")
36 } ||
38 echo "$0: cannot create a temporary directory in $TMPDIR" >&2
39 exit 1
43 func_tmpdir
44 # builddir may already be set by the script that invokes this one.
45 case "$builddir" in
46 '') builddir=`pwd` ;;
47 /* | ?:*) ;;
48 *) builddir=`pwd`/$builddir ;;
49 esac
50 cd "$builddir" ||
52 echo "$0: cannot determine build directory (unreadable parent dir?)" >&2
53 exit 1
55 # Switch to a temporary directory, to increase the likelihood that ACLs are
56 # supported on the current file system. (/tmp is usually locally mounted,
57 # whereas the build dir is sometimes NFS-mounted.)
58 ( cd "$tmp"
60 # Prepare tmpfile0.
61 rm -f tmpfile[0-9] tmpaclout[0-2]
62 echo "Simple contents" > tmpfile0
63 chmod 600 tmpfile0
65 # Classification of the platform according to the programs available for
66 # manipulating ACLs.
67 # Possible values are:
68 # linux, cygwin, freebsd, solaris, hpux, hpuxjfs, osf1, aix, macosx, irix, none.
69 # TODO: Support also native Windows platforms (mingw).
70 acl_flavor=none
71 if (getfacl tmpfile0 >/dev/null) 2>/dev/null; then
72 # Platforms with the getfacl and setfacl programs.
73 # Linux, FreeBSD, Solaris, Cygwin.
74 if (setfacl --help >/dev/null) 2>/dev/null; then
75 # Linux, Cygwin.
76 if (LC_ALL=C setfacl --help | grep ' --set-file' >/dev/null) 2>/dev/null; then
77 # Linux.
78 acl_flavor=linux
79 else
80 acl_flavor=cygwin
82 else
83 # FreeBSD, Solaris.
84 if (LC_ALL=C setfacl 2>&1 | grep '\-x entries' >/dev/null) 2>/dev/null; then
85 # FreeBSD.
86 acl_flavor=freebsd
87 else
88 # Solaris.
89 acl_flavor=solaris
92 else
93 if (lsacl / >/dev/null) 2>/dev/null; then
94 # Platforms with the lsacl and chacl programs.
95 # HP-UX, sometimes also IRIX.
96 if (getacl tmpfile0 >/dev/null) 2>/dev/null; then
97 # HP-UX 11.11 or newer.
98 acl_flavor=hpuxjfs
99 else
100 # HP-UX 11.00.
101 acl_flavor=hpux
103 else
104 if (getacl tmpfile0 >/dev/null) 2>/dev/null; then
105 # Tru64, NonStop Kernel.
106 if (getacl -m tmpfile0 >/dev/null) 2>/dev/null; then
107 # Tru64.
108 acl_flavor=osf1
109 else
110 # NonStop Kernel.
111 acl_flavor=nsk
113 else
114 if (aclget tmpfile0 >/dev/null) 2>/dev/null; then
115 # AIX.
116 acl_flavor=aix
117 else
118 if (fsaclctl -v >/dev/null) 2>/dev/null; then
119 # Mac OS X.
120 acl_flavor=macosx
121 else
122 if test -f /sbin/chacl; then
123 # IRIX.
124 acl_flavor=irix
132 # Define a function to test for the same ACLs, from the point of view of
133 # the programs.
134 # func_test_same_acls file1 file2
135 case $acl_flavor in
136 linux | cygwin | freebsd | solaris)
137 func_test_same_acls ()
139 getfacl "$1" | sed -e "s/$1/FILENAME/g" > tmpaclout1
140 getfacl "$2" | sed -e "s/$2/FILENAME/g" > tmpaclout2
141 cmp tmpaclout1 tmpaclout2 > /dev/null
144 hpux)
145 func_test_same_acls ()
147 lsacl "$1" | sed -e "s/$1/FILENAME/g" > tmpaclout1
148 lsacl "$2" | sed -e "s/$2/FILENAME/g" > tmpaclout2
149 cmp tmpaclout1 tmpaclout2 > /dev/null
152 hpuxjfs)
153 func_test_same_acls ()
155 { lsacl "$1" | sed -e "s/$1/FILENAME/g" > tmpaclout1
156 lsacl "$2" | sed -e "s/$2/FILENAME/g" > tmpaclout2
157 cmp tmpaclout1 tmpaclout2 > /dev/null
158 } &&
159 { getacl "$1" | sed -e "s/$1/FILENAME/g" > tmpaclout1
160 getacl "$2" | sed -e "s/$2/FILENAME/g" > tmpaclout2
161 cmp tmpaclout1 tmpaclout2 > /dev/null
165 osf1 | nsk)
166 func_test_same_acls ()
168 getacl "$1" | sed -e "s/$1/FILENAME/g" > tmpaclout1
169 getacl "$2" | sed -e "s/$2/FILENAME/g" > tmpaclout2
170 cmp tmpaclout1 tmpaclout2 > /dev/null
173 aix)
174 func_test_same_acls ()
176 aclget "$1" > tmpaclout1
177 aclget "$2" > tmpaclout2
178 cmp tmpaclout1 tmpaclout2 > /dev/null
181 macosx)
182 func_test_same_acls ()
184 /bin/ls -le "$1" | sed -e "s/$1/FILENAME/g" > tmpaclout1
185 /bin/ls -le "$2" | sed -e "s/$2/FILENAME/g" > tmpaclout2
186 cmp tmpaclout1 tmpaclout2 > /dev/null
189 irix)
190 func_test_same_acls ()
192 /bin/ls -lD "$1" | sed -e "s/$1/FILENAME/g" > tmpaclout1
193 /bin/ls -lD "$2" | sed -e "s/$2/FILENAME/g" > tmpaclout2
194 cmp tmpaclout1 tmpaclout2 > /dev/null
197 none)
198 func_test_same_acls ()
203 esac
205 # func_test_copy file1 file2
206 # copies file1 to file2 and verifies the permissions and ACLs are the same
207 # on both.
208 func_test_copy ()
210 echo "Simple contents" > "$2"
211 chmod 600 "$2"
212 ${CHECKER} "$builddir"/test-copy-acl${EXEEXT} "$1" "$2" || exit 1
213 ${CHECKER} "$builddir"/test-sameacls${EXEEXT} "$1" "$2" || exit 1
214 func_test_same_acls "$1" "$2" || exit 1
217 func_test_copy tmpfile0 tmpfile1
219 if test $acl_flavor != none; then
220 # A POSIX compliant 'id' program.
221 if test -f /usr/xpg4/bin/id; then
222 ID=/usr/xpg4/bin/id
223 else
224 ID=id
226 # Use a user and group id different from the current one, to avoid
227 # redundant/ambiguous ACLs.
228 myuid=`$ID -u`
229 mygid=`$ID -g`
230 auid=1
231 if test "$auid" = "$myuid"; then auid=2; fi
232 agid=1
233 if test "$agid" = "$mygid"; then agid=2; fi
235 case $acl_flavor in
236 linux | freebsd | solaris)
238 # Set an ACL for a user.
239 setfacl -m user:$auid:1 tmpfile0
241 func_test_copy tmpfile0 tmpfile2
243 # Set an ACL for a group.
244 setfacl -m group:$agid:4 tmpfile0
246 func_test_copy tmpfile0 tmpfile3
248 # Set an ACL for other.
249 case $acl_flavor in
250 freebsd) setfacl -m other::4 tmpfile0 ;;
251 solaris) chmod o+r tmpfile0 ;;
252 *) setfacl -m other:4 tmpfile0 ;;
253 esac
255 func_test_copy tmpfile0 tmpfile4
257 # Remove the ACL for the user.
258 case $acl_flavor in
259 linux) setfacl -x user:$auid tmpfile0 ;;
260 freebsd) setfacl -x user:$auid:1 tmpfile0 ;;
261 *) setfacl -d user:$auid:1 tmpfile0 ;;
262 esac
264 func_test_copy tmpfile0 tmpfile5
266 # Remove the ACL for other.
267 case $acl_flavor in
268 linux | solaris) ;; # impossible
269 freebsd) setfacl -x other::4 tmpfile0 ;;
270 *) setfacl -d other:4 tmpfile0 ;;
271 esac
273 func_test_copy tmpfile0 tmpfile6
275 # Remove the ACL for the group.
276 case $acl_flavor in
277 linux) setfacl -x group:$agid tmpfile0 ;;
278 freebsd) setfacl -x group:$agid:4 tmpfile0 ;;
279 *) setfacl -d group:$agid:4 tmpfile0 ;;
280 esac
282 func_test_copy tmpfile0 tmpfile7
284 # Delete all optional ACLs.
285 case $acl_flavor in
286 linux | freebsd)
287 setfacl -m user:$auid:1 tmpfile0
288 setfacl -b tmpfile0
291 setfacl -s user::6,group::0,other:0 tmpfile0 ;;
292 esac
294 func_test_copy tmpfile0 tmpfile8
296 # Copy ACLs from a file that has no ACLs.
297 echo > tmpfile9
298 chmod a+x tmpfile9
299 case $acl_flavor in
300 linux) getfacl tmpfile9 | setfacl --set-file=- tmpfile0 ;;
301 freebsd) ;;
302 *) getfacl tmpfile9 | setfacl -f - tmpfile0 ;;
303 esac
304 rm -f tmpfile9
306 func_test_copy tmpfile0 tmpfile9
310 cygwin)
312 # Set an ACL for a group.
313 setfacl -m group:0:1 tmpfile0
315 func_test_copy tmpfile0 tmpfile2
317 # Set an ACL for other.
318 setfacl -m other:4 tmpfile0
320 func_test_copy tmpfile0 tmpfile4
322 # Remove the ACL for the group.
323 setfacl -d group:0 tmpfile0
325 func_test_copy tmpfile0 tmpfile5
327 # Remove the ACL for other.
328 setfacl -d other:4 tmpfile0
330 func_test_copy tmpfile0 tmpfile6
332 # Delete all optional ACLs.
333 setfacl -s user::6,group::0,other:0 tmpfile0
335 func_test_copy tmpfile0 tmpfile8
337 # Copy ACLs from a file that has no ACLs.
338 echo > tmpfile9
339 chmod a+x tmpfile9
340 getfacl tmpfile9 | setfacl -f - tmpfile0
341 rm -f tmpfile9
343 func_test_copy tmpfile0 tmpfile9
347 hpux)
349 # Set an ACL for a user.
350 orig=`lsacl tmpfile0 | sed -e 's/ tmpfile0$//'`
351 chacl -r "${orig}($auid.%,--x)" tmpfile0
353 func_test_copy tmpfile0 tmpfile2
355 # Set an ACL for a group.
356 orig=`lsacl tmpfile0 | sed -e 's/ tmpfile0$//'`
357 chacl -r "${orig}(%.$agid,r--)" tmpfile0
359 func_test_copy tmpfile0 tmpfile3
361 # Set an ACL for other.
362 orig=`lsacl tmpfile0 | sed -e 's/ tmpfile0$//'`
363 chacl -r "${orig}(%.%,r--)" tmpfile0
365 func_test_copy tmpfile0 tmpfile4
367 # Remove the ACL for the user.
368 chacl -d "($auid.%,--x)" tmpfile0
370 func_test_copy tmpfile0 tmpfile5
372 # Remove the ACL for the group.
373 chacl -d "(%.$agid,r--)" tmpfile0
375 func_test_copy tmpfile0 tmpfile6
377 # Delete all optional ACLs.
378 chacl -z tmpfile0
380 func_test_copy tmpfile0 tmpfile8
382 # Copy ACLs from a file that has no ACLs.
383 echo > tmpfile9
384 chmod a+x tmpfile9
385 orig=`lsacl tmpfile9 | sed -e 's/ tmpfile9$//'`
386 rm -f tmpfile9
387 chacl -r "${orig}" tmpfile0
389 func_test_copy tmpfile0 tmpfile9
393 hpuxjfs)
395 # Set an ACL for a user.
396 orig=`lsacl tmpfile0 | sed -e 's/ tmpfile0$//'`
397 chacl -r "${orig}($auid.%,--x)" tmpfile0 \
398 || setacl -m user:$auid:1 tmpfile0
400 func_test_copy tmpfile0 tmpfile2
402 # Set an ACL for a group.
403 orig=`lsacl tmpfile0 | sed -e 's/ tmpfile0$//'`
404 chacl -r "${orig}(%.$agid,r--)" tmpfile0 \
405 || setacl -m group:$agid:4 tmpfile0
407 func_test_copy tmpfile0 tmpfile3
409 # Set an ACL for other.
410 orig=`lsacl tmpfile0 | sed -e 's/ tmpfile0$//'`
411 chacl -r "${orig}(%.%,r--)" tmpfile0 \
412 || setacl -m other:4 tmpfile0
414 func_test_copy tmpfile0 tmpfile4
416 # Remove the ACL for the user.
417 chacl -d "($auid.%,--x)" tmpfile0 \
418 || setacl -d user:$auid tmpfile0
420 func_test_copy tmpfile0 tmpfile5
422 # Remove the ACL for the group.
423 chacl -d "(%.$agid,r--)" tmpfile0 \
424 || setacl -d group:$agid tmpfile0
426 func_test_copy tmpfile0 tmpfile6
428 # Delete all optional ACLs.
429 chacl -z tmpfile0 \
430 || { setacl -m user:$auid:1 tmpfile0
431 setacl -s user::6,group::0,class:7,other:0 tmpfile0
434 func_test_copy tmpfile0 tmpfile8
436 # Copy ACLs from a file that has no ACLs.
437 echo > tmpfile9
438 chmod a+x tmpfile9
439 orig=`lsacl tmpfile9 | sed -e 's/ tmpfile9$//'`
440 getacl tmpfile9 > tmpaclout0
441 rm -f tmpfile9
442 chacl -r "${orig}" tmpfile0 \
443 || setacl -f tmpaclout0 tmpfile0
445 func_test_copy tmpfile0 tmpfile9
449 osf1)
451 # Set an ACL for a user.
452 setacl -u user:$auid:1 tmpfile0
454 func_test_copy tmpfile0 tmpfile2
456 # Set an ACL for a group.
457 setacl -u group:$agid:4 tmpfile0
459 func_test_copy tmpfile0 tmpfile3
461 # Set an ACL for other.
462 setacl -u other::4 tmpfile0
464 func_test_copy tmpfile0 tmpfile4
466 # Remove the ACL for the user.
467 setacl -x user:$auid:1 tmpfile0
469 func_test_copy tmpfile0 tmpfile5
471 if false; then # would give an error "can't set ACL: Invalid argument"
472 # Remove the ACL for other.
473 setacl -x other::4 tmpfile0
475 func_test_copy tmpfile0 tmpfile6
478 # Remove the ACL for the group.
479 setacl -x group:$agid:4 tmpfile0
481 func_test_copy tmpfile0 tmpfile7
483 # Delete all optional ACLs.
484 setacl -u user:$auid:1 tmpfile0
485 setacl -b tmpfile0
487 func_test_copy tmpfile0 tmpfile8
489 # Copy ACLs from a file that has no ACLs.
490 echo > tmpfile9
491 chmod a+x tmpfile9
492 getacl tmpfile9 > tmpaclout0
493 setacl -b -U tmpaclout0 tmpfile0
494 rm -f tmpfile9
496 func_test_copy tmpfile0 tmpfile9
500 nsk)
502 # Set an ACL for a user.
503 setacl -m user:$auid:1 tmpfile0
505 func_test_copy tmpfile0 tmpfile2
507 # Set an ACL for a group.
508 setacl -m group:$agid:4 tmpfile0
510 func_test_copy tmpfile0 tmpfile3
512 # Set an ACL for other.
513 setacl -m other:4 tmpfile0
515 func_test_copy tmpfile0 tmpfile4
517 # Remove the ACL for the user.
518 setacl -d user:$auid tmpfile0
520 func_test_copy tmpfile0 tmpfile5
522 # Remove the ACL for the group.
523 setacl -d group:$agid tmpfile0
525 func_test_copy tmpfile0 tmpfile6
527 # Delete all optional ACLs.
528 setacl -m user:$auid:1 tmpfile0
529 setacl -s user::6,group::0,class:7,other:0 tmpfile0
531 func_test_copy tmpfile0 tmpfile8
533 # Copy ACLs from a file that has no ACLs.
534 echo > tmpfile9
535 chmod a+x tmpfile9
536 getacl tmpfile9 > tmpaclout0
537 setacl -f tmpaclout0 tmpfile0
538 rm -f tmpfile9
540 func_test_copy tmpfile0 tmpfile9
544 aix)
546 # Set an ACL for a user.
547 { aclget tmpfile0 | sed -e 's/disabled$/enabled/'; echo " permit --x u:$auid"; } | aclput tmpfile0
549 func_test_copy tmpfile0 tmpfile2
551 # Set an ACL for a group.
552 { aclget tmpfile0 | sed -e 's/disabled$/enabled/'; echo " permit r-- g:$agid"; } | aclput tmpfile0
554 func_test_copy tmpfile0 tmpfile3
556 # Set an ACL for other.
557 chmod o+r tmpfile0
559 func_test_copy tmpfile0 tmpfile4
561 # Remove the ACL for the user.
562 aclget tmpfile0 | grep -v ' u:[^ ]*$' | aclput tmpfile0
564 func_test_copy tmpfile0 tmpfile5
566 # Remove the ACL for the group.
567 aclget tmpfile0 | grep -v ' g:[^ ]*$' | aclput tmpfile0
569 func_test_copy tmpfile0 tmpfile7
571 # Delete all optional ACLs.
572 aclget tmpfile0 | sed -e 's/enabled$/disabled/' | sed -e '/disabled$/q' | aclput tmpfile0
574 func_test_copy tmpfile0 tmpfile8
576 # Copy ACLs from a file that has no ACLs.
577 echo > tmpfile9
578 chmod a+x tmpfile9
579 aclget tmpfile9 | aclput tmpfile0
580 rm -f tmpfile9
582 func_test_copy tmpfile0 tmpfile9
586 macosx)
588 # Set an ACL for a user.
589 /bin/chmod +a "user:daemon allow execute" tmpfile0
591 func_test_copy tmpfile0 tmpfile2
593 # Set an ACL for a group.
594 /bin/chmod +a "group:daemon allow read" tmpfile0
596 func_test_copy tmpfile0 tmpfile3
598 # Set an ACL for other.
599 chmod o+r tmpfile0
601 func_test_copy tmpfile0 tmpfile4
603 # Remove the ACL for the user.
604 /bin/chmod -a "user:daemon allow execute" tmpfile0
606 func_test_copy tmpfile0 tmpfile5
608 # Remove the ACL for the group.
609 /bin/chmod -a "group:daemon allow read" tmpfile0
611 func_test_copy tmpfile0 tmpfile7
613 # Delete all optional ACLs.
614 /bin/chmod -N tmpfile0
616 func_test_copy tmpfile0 tmpfile8
618 # Copy ACLs from a file that has no ACLs.
619 echo > tmpfile9
620 chmod a+x tmpfile9
621 { /bin/ls -le tmpfile9 | sed -n -e 's/^ [0-9][0-9]*: //p'; echo; } | /bin/chmod -E tmpfile0
622 rm -f tmpfile9
624 func_test_copy tmpfile0 tmpfile9
628 irix)
630 # Set an ACL for a user.
631 /sbin/chacl user::rw-,group::---,other::---,user:$auid:--x tmpfile0
633 func_test_copy tmpfile0 tmpfile2
635 # Set an ACL for a group.
636 /sbin/chacl user::rw-,group::---,other::---,user:$auid:--x,group:$agid:r-- tmpfile0
638 func_test_copy tmpfile0 tmpfile3
640 # Set an ACL for other.
641 /sbin/chacl user::rw-,group::---,user:$auid:--x,group:$agid:r--,other::r-- tmpfile0
643 func_test_copy tmpfile0 tmpfile4
645 # Remove the ACL for the user.
646 /sbin/chacl user::rw-,group::---,group:$agid:r--,other::r-- tmpfile0
648 func_test_copy tmpfile0 tmpfile5
650 # Remove the ACL for the group.
651 /sbin/chacl user::rw-,group::---,other::r-- tmpfile0
653 func_test_copy tmpfile0 tmpfile7
657 esac
660 rm -f tmpfile[0-9] tmpaclout[0-2]
661 ) || exit 1
663 rm -rf "$tmp"
664 exit 0