avoid spurious test failure when library is built without ACL support
[gnulib.git] / tests / test-copy-acl.sh
1 #!/bin/sh
2
3 # Show all commands when run with environment variable VERBOSE=yes.
4 test -z "$VERBOSE" || set -x
5
6 test "$USE_ACL" = 0 &&
7   {
8     echo "Skipping test: insufficient ACL support"
9     exit 77
10   }
11
12 # func_tmpdir
13 # creates a temporary directory.
14 # Sets variable
15 # - tmp             pathname of freshly created temporary directory
16 func_tmpdir ()
17 {
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}
22   {
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   } ||
28   {
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   } ||
37   {
38     echo "$0: cannot create a temporary directory in $TMPDIR" >&2
39     exit 1
40   }
41 }
42
43 func_tmpdir
44 builddir=`pwd`
45 cd "$builddir" ||
46   {
47     echo "$0: cannot determine build directory (unreadable parent dir?)" >&2
48     exit 1
49   }
50 # Switch to a temporary directory, to increase the likelihood that ACLs are
51 # supported on the current file system. (/tmp is usually locally mounted,
52 # whereas the build dir is sometimes NFS-mounted.)
53 ( cd "$tmp"
54
55   # Prepare tmpfile0.
56   rm -f tmpfile[0-9] tmpaclout[0-2]
57   echo "Simple contents" > tmpfile0
58   chmod 600 tmpfile0
59
60   # Classification of the platform according to the programs available for
61   # manipulating ACLs.
62   # Possible values are:
63   #   linux, cygwin, freebsd, solaris, hpux, osf1, aix, macosx, irix, none.
64   # TODO: Support also native Win32 platforms (mingw).
65   acl_flavor=none
66   if (getfacl tmpfile0 >/dev/null) 2>/dev/null; then
67     # Platforms with the getfacl and setfacl programs.
68     # Linux, FreeBSD, Solaris, Cygwin.
69     if (setfacl --help >/dev/null) 2>/dev/null; then
70       # Linux, Cygwin.
71       if (LC_ALL=C setfacl --help | grep ' --set-file' >/dev/null) 2>/dev/null; then
72         # Linux.
73         acl_flavor=linux
74       else
75         acl_flavor=cygwin
76       fi
77     else
78       # FreeBSD, Solaris.
79       if (LC_ALL=C setfacl 2>&1 | grep '\-x entries' >/dev/null) 2>/dev/null; then
80         # FreeBSD.
81         acl_flavor=freebsd
82       else
83         # Solaris.
84         acl_flavor=solaris
85       fi
86     fi
87   else
88     if (lsacl / >/dev/null) 2>/dev/null; then
89       # Platforms with the lsacl and chacl programs.
90       # HP-UX, sometimes also IRIX.
91       acl_flavor=hpux
92     else
93       if (getacl tmpfile0 >/dev/null) 2>/dev/null; then
94         # Tru64.
95         acl_flavor=osf1
96       else
97         if (aclget tmpfile0 >/dev/null) 2>/dev/null; then
98           # AIX.
99           acl_flavor=aix
100         else
101           if (fsaclctl -v >/dev/null) 2>/dev/null; then
102             # MacOS X.
103             acl_flavor=macosx
104           else
105             if test -f /sbin/chacl; then
106               # IRIX.
107               acl_flavor=irix
108             fi
109           fi
110         fi
111       fi
112     fi
113   fi
114
115   # Define a function to test for the same ACLs, from the point of view of
116   # the programs.
117   # func_test_same_acls file1 file2
118   case $acl_flavor in
119     linux | cygwin | freebsd | solaris)
120       func_test_same_acls ()
121       {
122         getfacl "$1" | sed -e "s/$1/FILENAME/g" > tmpaclout1
123         getfacl "$2" | sed -e "s/$2/FILENAME/g" > tmpaclout2
124         cmp tmpaclout1 tmpaclout2 > /dev/null
125       }
126       ;;
127     hpux)
128       func_test_same_acls ()
129       {
130         lsacl "$1" | sed -e "s/$1/FILENAME/g" > tmpaclout1
131         lsacl "$2" | sed -e "s/$2/FILENAME/g" > tmpaclout2
132         cmp tmpaclout1 tmpaclout2 > /dev/null
133       }
134       ;;
135     osf1)
136       func_test_same_acls ()
137       {
138         getacl "$1" | sed -e "s/$1/FILENAME/g" > tmpaclout1
139         getacl "$2" | sed -e "s/$2/FILENAME/g" > tmpaclout2
140         cmp tmpaclout1 tmpaclout2 > /dev/null
141       }
142       ;;
143     aix)
144       func_test_same_acls ()
145       {
146         aclget "$1" > tmpaclout1
147         aclget "$2" > tmpaclout2
148         cmp tmpaclout1 tmpaclout2 > /dev/null
149       }
150       ;;
151     macosx)
152       func_test_same_acls ()
153       {
154         /bin/ls -le "$1" | sed -e "s/$1/FILENAME/g" > tmpaclout1
155         /bin/ls -le "$2" | sed -e "s/$2/FILENAME/g" > tmpaclout2
156         cmp tmpaclout1 tmpaclout2 > /dev/null
157       }
158       ;;
159     irix)
160       func_test_same_acls ()
161       {
162         /bin/ls -lD "$1" | sed -e "s/$1/FILENAME/g" > tmpaclout1
163         /bin/ls -lD "$2" | sed -e "s/$2/FILENAME/g" > tmpaclout2
164         cmp tmpaclout1 tmpaclout2 > /dev/null
165       }
166       ;;
167     none)
168       func_test_same_acls ()
169       {
170         :
171       }
172       ;;
173   esac
174
175   # func_test_copy file1 file2
176   # copies file1 to file2 and verifies the permissions and ACLs are the same
177   # on both.
178   func_test_copy ()
179   {
180     echo "Simple contents" > "$2"
181     chmod 600 "$2"
182     "$builddir"/test-copy-acl${EXEEXT} "$1" "$2" || exit 1
183     "$builddir"/test-sameacls${EXEEXT} "$1" "$2" || exit 1
184     func_test_same_acls                "$1" "$2" || exit 1
185   }
186
187   func_test_copy tmpfile0 tmpfile1
188
189   if test $acl_flavor != none; then
190     # Use a user and group id different from the current one, to avoid
191     # redundant/ambiguous ACLs.
192     myuid=`id -u`
193     mygid=`id -g`
194     auid=1
195     if test "$auid" = "$myuid"; then auid=2; fi
196     agid=1
197     if test "$agid" = "$mygid"; then agid=2; fi
198
199     case $acl_flavor in
200       linux | freebsd | solaris)
201
202         # Set an ACL for a user.
203         setfacl -m user:$auid:1 tmpfile0
204
205         func_test_copy tmpfile0 tmpfile2
206
207         # Set an ACL for a group.
208         setfacl -m group:$agid:4 tmpfile0
209
210         func_test_copy tmpfile0 tmpfile3
211
212         # Set an ACL for other.
213         case $acl_flavor in
214           freebsd) setfacl -m other::4 tmpfile0 ;;
215           solaris) chmod o+r tmpfile0 ;;
216           *)       setfacl -m other:4 tmpfile0 ;;
217         esac
218
219         func_test_copy tmpfile0 tmpfile4
220
221         # Remove the ACL for the user.
222         case $acl_flavor in
223           linux)   setfacl -x user:$auid tmpfile0 ;;
224           freebsd) setfacl -x user:$auid:1 tmpfile0 ;;
225           *)       setfacl -d user:$auid:1 tmpfile0 ;;
226         esac
227
228         func_test_copy tmpfile0 tmpfile5
229
230         # Remove the ACL for other.
231         case $acl_flavor in
232           linux | solaris) ;; # impossible
233           freebsd) setfacl -x other::4 tmpfile0 ;;
234           *)       setfacl -d other:4 tmpfile0 ;;
235         esac
236
237         func_test_copy tmpfile0 tmpfile6
238
239         # Remove the ACL for the group.
240         case $acl_flavor in
241           linux)   setfacl -x group:$agid tmpfile0 ;;
242           freebsd) setfacl -x group:$agid:4 tmpfile0 ;;
243           *)       setfacl -d group:$agid:4 tmpfile0 ;;
244         esac
245
246         func_test_copy tmpfile0 tmpfile7
247
248         # Delete all optional ACLs.
249         case $acl_flavor in
250           linux | freebsd)
251             setfacl -m user:$auid:1 tmpfile0
252             setfacl -b tmpfile0
253             ;;
254           *)
255             setfacl -s user::6,group::0,other:0 tmpfile0 ;;
256         esac
257
258         func_test_copy tmpfile0 tmpfile8
259
260         # Copy ACLs from a file that has no ACLs.
261         echo > tmpfile9
262         chmod a+x tmpfile9
263         case $acl_flavor in
264           linux)   getfacl tmpfile9 | setfacl --set-file=- tmpfile0 ;;
265           freebsd) ;;
266           *)       getfacl tmpfile9 | setfacl -f - tmpfile0 ;;
267         esac
268         rm -f tmpfile9
269
270         func_test_copy tmpfile0 tmpfile9
271
272         ;;
273
274       cygwin)
275
276         # Set an ACL for a group.
277         setfacl -m group:0:1 tmpfile0
278
279         func_test_copy tmpfile0 tmpfile2
280
281         # Set an ACL for other.
282         setfacl -m other:4 tmpfile0
283
284         func_test_copy tmpfile0 tmpfile4
285
286         # Remove the ACL for the group.
287         setfacl -d group:0 tmpfile0
288
289         func_test_copy tmpfile0 tmpfile5
290
291         # Remove the ACL for other.
292         setfacl -d other:4 tmpfile0
293
294         func_test_copy tmpfile0 tmpfile6
295
296         # Delete all optional ACLs.
297         setfacl -s user::6,group::0,other:0 tmpfile0
298
299         func_test_copy tmpfile0 tmpfile8
300
301         # Copy ACLs from a file that has no ACLs.
302         echo > tmpfile9
303         chmod a+x tmpfile9
304         getfacl tmpfile9 | setfacl -f - tmpfile0
305         rm -f tmpfile9
306
307         func_test_copy tmpfile0 tmpfile9
308
309         ;;
310
311       hpux)
312
313         # Set an ACL for a user.
314         orig=`lsacl tmpfile0 | sed -e 's/ tmpfile0$//'`
315         chacl -r "${orig}($auid.%,--x)" tmpfile0
316
317         func_test_copy tmpfile0 tmpfile2
318
319         # Set an ACL for a group.
320         orig=`lsacl tmpfile0 | sed -e 's/ tmpfile0$//'`
321         chacl -r "${orig}(%.$agid,r--)" tmpfile0
322
323         func_test_copy tmpfile0 tmpfile3
324
325         # Set an ACL for other.
326         orig=`lsacl tmpfile0 | sed -e 's/ tmpfile0$//'`
327         chacl -r "${orig}(%.%,r--)" tmpfile0
328
329         func_test_copy tmpfile0 tmpfile4
330
331         # Remove the ACL for the user.
332         chacl -d "($auid.%,--x)" tmpfile0
333
334         func_test_copy tmpfile0 tmpfile5
335
336         # Remove the ACL for the group.
337         chacl -d "(%.$agid,r--)" tmpfile0
338
339         func_test_copy tmpfile0 tmpfile6
340
341         # Delete all optional ACLs.
342         chacl -z tmpfile0
343
344         func_test_copy tmpfile0 tmpfile8
345
346         # Copy ACLs from a file that has no ACLs.
347         echo > tmpfile9
348         chmod a+x tmpfile9
349         orig=`lsacl tmpfile9 | sed -e 's/ tmpfile9$//'`
350         rm -f tmpfile9
351         chacl -r "${orig}" tmpfile0
352
353         func_test_copy tmpfile0 tmpfile9
354
355         ;;
356
357       osf1)
358
359         # Set an ACL for a user.
360         setacl -u user:$auid:1 tmpfile0
361
362         func_test_copy tmpfile0 tmpfile2
363
364         # Set an ACL for a group.
365         setacl -u group:$agid:4 tmpfile0
366
367         func_test_copy tmpfile0 tmpfile3
368
369         # Set an ACL for other.
370         setacl -u other::4 tmpfile0
371
372         func_test_copy tmpfile0 tmpfile4
373
374         # Remove the ACL for the user.
375         setacl -x user:$auid:1 tmpfile0
376
377         func_test_copy tmpfile0 tmpfile5
378
379         if false; then # would give an error "can't set ACL: Invalid argument"
380           # Remove the ACL for other.
381           setacl -x other::4 tmpfile0
382
383           func_test_copy tmpfile0 tmpfile6
384         fi
385
386         # Remove the ACL for the group.
387         setacl -x group:$agid:4 tmpfile0
388
389         func_test_copy tmpfile0 tmpfile7
390
391         # Delete all optional ACLs.
392         setacl -u user:$auid:1 tmpfile0
393         setacl -b tmpfile0
394
395         func_test_copy tmpfile0 tmpfile8
396
397         # Copy ACLs from a file that has no ACLs.
398         echo > tmpfile9
399         chmod a+x tmpfile9
400         getacl tmpfile9 > tmpaclout0
401         setacl -b -U tmpaclout0 tmpfile0
402         rm -f tmpfile9
403
404         func_test_copy tmpfile0 tmpfile9
405
406         ;;
407
408       aix)
409
410         # Set an ACL for a user.
411         { aclget tmpfile0 | sed -e 's/disabled$/enabled/'; echo "        permit --x u:$auid"; } | aclput tmpfile0
412
413         func_test_copy tmpfile0 tmpfile2
414
415         # Set an ACL for a group.
416         { aclget tmpfile0 | sed -e 's/disabled$/enabled/'; echo "        permit r-- g:$agid"; } | aclput tmpfile0
417
418         func_test_copy tmpfile0 tmpfile3
419
420         # Set an ACL for other.
421         chmod o+r tmpfile0
422
423         func_test_copy tmpfile0 tmpfile4
424
425         # Remove the ACL for the user.
426         aclget tmpfile0 | grep -v ' u:[^ ]*$' | aclput tmpfile0
427
428         func_test_copy tmpfile0 tmpfile5
429
430         # Remove the ACL for the group.
431         aclget tmpfile0 | grep -v ' g:[^ ]*$' | aclput tmpfile0
432
433         func_test_copy tmpfile0 tmpfile7
434
435         # Delete all optional ACLs.
436         aclget tmpfile0 | sed -e 's/enabled$/disabled/' | sed -e '/disabled$/q' | aclput tmpfile0
437
438         func_test_copy tmpfile0 tmpfile8
439
440         # Copy ACLs from a file that has no ACLs.
441         echo > tmpfile9
442         chmod a+x tmpfile9
443         aclget tmpfile9 | aclput tmpfile0
444         rm -f tmpfile9
445
446         func_test_copy tmpfile0 tmpfile9
447
448         ;;
449
450       macosx)
451
452         # Set an ACL for a user.
453         /bin/chmod +a "user:daemon allow execute" tmpfile0
454
455         func_test_copy tmpfile0 tmpfile2
456
457         # Set an ACL for a group.
458         /bin/chmod +a "group:daemon allow read" tmpfile0
459
460         func_test_copy tmpfile0 tmpfile3
461
462         # Set an ACL for other.
463         chmod o+r tmpfile0
464
465         func_test_copy tmpfile0 tmpfile4
466
467         # Remove the ACL for the user.
468         /bin/chmod -a "user:daemon allow execute" tmpfile0
469
470         func_test_copy tmpfile0 tmpfile5
471
472         # Remove the ACL for the group.
473         /bin/chmod -a "group:daemon allow read" tmpfile0
474
475         func_test_copy tmpfile0 tmpfile7
476
477         # Delete all optional ACLs.
478         /bin/chmod -N tmpfile0
479
480         func_test_copy tmpfile0 tmpfile8
481
482         # Copy ACLs from a file that has no ACLs.
483         echo > tmpfile9
484         chmod a+x tmpfile9
485         { /bin/ls -le tmpfile9 | sed -n -e 's/^ [0-9][0-9]*: //p'; echo; } | /bin/chmod -E tmpfile0
486         rm -f tmpfile9
487
488         func_test_copy tmpfile0 tmpfile9
489
490         ;;
491
492       irix)
493
494         # Set an ACL for a user.
495         /sbin/chacl user::rw-,group::---,other::---,user:$auid:--x tmpfile0
496
497         func_test_copy tmpfile0 tmpfile2
498
499         # Set an ACL for a group.
500         /sbin/chacl user::rw-,group::---,other::---,user:$auid:--x,group:$agid:r-- tmpfile0
501
502         func_test_copy tmpfile0 tmpfile3
503
504         # Set an ACL for other.
505         /sbin/chacl user::rw-,group::---,user:$auid:--x,group:$agid:r--,other::r-- tmpfile0
506
507         func_test_copy tmpfile0 tmpfile4
508
509         # Remove the ACL for the user.
510         /sbin/chacl user::rw-,group::---,group:$agid:r--,other::r-- tmpfile0
511
512         func_test_copy tmpfile0 tmpfile5
513
514         # Remove the ACL for the group.
515         /sbin/chacl user::rw-,group::---,other::r-- tmpfile0
516
517         func_test_copy tmpfile0 tmpfile7
518
519         ;;
520
521     esac
522   fi
523
524   rm -f tmpfile[0-9] tmpaclout[0-2]
525 ) || exit 1
526
527 rm -rf "$tmp"
528 exit 0