Imported Upstream version 0.18.1.1
[platform/upstream/gettext.git] / gettext-tools / gnulib-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     # A POSIX compliant 'id' program.
191     if test -f /usr/xpg4/bin/id; then
192       ID=/usr/xpg4/bin/id
193     else
194       ID=id
195     fi
196     # Use a user and group id different from the current one, to avoid
197     # redundant/ambiguous ACLs.
198     myuid=`$ID -u`
199     mygid=`$ID -g`
200     auid=1
201     if test "$auid" = "$myuid"; then auid=2; fi
202     agid=1
203     if test "$agid" = "$mygid"; then agid=2; fi
204
205     case $acl_flavor in
206       linux | freebsd | solaris)
207
208         # Set an ACL for a user.
209         setfacl -m user:$auid:1 tmpfile0
210
211         func_test_copy tmpfile0 tmpfile2
212
213         # Set an ACL for a group.
214         setfacl -m group:$agid:4 tmpfile0
215
216         func_test_copy tmpfile0 tmpfile3
217
218         # Set an ACL for other.
219         case $acl_flavor in
220           freebsd) setfacl -m other::4 tmpfile0 ;;
221           solaris) chmod o+r tmpfile0 ;;
222           *)       setfacl -m other:4 tmpfile0 ;;
223         esac
224
225         func_test_copy tmpfile0 tmpfile4
226
227         # Remove the ACL for the user.
228         case $acl_flavor in
229           linux)   setfacl -x user:$auid tmpfile0 ;;
230           freebsd) setfacl -x user:$auid:1 tmpfile0 ;;
231           *)       setfacl -d user:$auid:1 tmpfile0 ;;
232         esac
233
234         func_test_copy tmpfile0 tmpfile5
235
236         # Remove the ACL for other.
237         case $acl_flavor in
238           linux | solaris) ;; # impossible
239           freebsd) setfacl -x other::4 tmpfile0 ;;
240           *)       setfacl -d other:4 tmpfile0 ;;
241         esac
242
243         func_test_copy tmpfile0 tmpfile6
244
245         # Remove the ACL for the group.
246         case $acl_flavor in
247           linux)   setfacl -x group:$agid tmpfile0 ;;
248           freebsd) setfacl -x group:$agid:4 tmpfile0 ;;
249           *)       setfacl -d group:$agid:4 tmpfile0 ;;
250         esac
251
252         func_test_copy tmpfile0 tmpfile7
253
254         # Delete all optional ACLs.
255         case $acl_flavor in
256           linux | freebsd)
257             setfacl -m user:$auid:1 tmpfile0
258             setfacl -b tmpfile0
259             ;;
260           *)
261             setfacl -s user::6,group::0,other:0 tmpfile0 ;;
262         esac
263
264         func_test_copy tmpfile0 tmpfile8
265
266         # Copy ACLs from a file that has no ACLs.
267         echo > tmpfile9
268         chmod a+x tmpfile9
269         case $acl_flavor in
270           linux)   getfacl tmpfile9 | setfacl --set-file=- tmpfile0 ;;
271           freebsd) ;;
272           *)       getfacl tmpfile9 | setfacl -f - tmpfile0 ;;
273         esac
274         rm -f tmpfile9
275
276         func_test_copy tmpfile0 tmpfile9
277
278         ;;
279
280       cygwin)
281
282         # Set an ACL for a group.
283         setfacl -m group:0:1 tmpfile0
284
285         func_test_copy tmpfile0 tmpfile2
286
287         # Set an ACL for other.
288         setfacl -m other:4 tmpfile0
289
290         func_test_copy tmpfile0 tmpfile4
291
292         # Remove the ACL for the group.
293         setfacl -d group:0 tmpfile0
294
295         func_test_copy tmpfile0 tmpfile5
296
297         # Remove the ACL for other.
298         setfacl -d other:4 tmpfile0
299
300         func_test_copy tmpfile0 tmpfile6
301
302         # Delete all optional ACLs.
303         setfacl -s user::6,group::0,other:0 tmpfile0
304
305         func_test_copy tmpfile0 tmpfile8
306
307         # Copy ACLs from a file that has no ACLs.
308         echo > tmpfile9
309         chmod a+x tmpfile9
310         getfacl tmpfile9 | setfacl -f - tmpfile0
311         rm -f tmpfile9
312
313         func_test_copy tmpfile0 tmpfile9
314
315         ;;
316
317       hpux)
318
319         # Set an ACL for a user.
320         orig=`lsacl tmpfile0 | sed -e 's/ tmpfile0$//'`
321         chacl -r "${orig}($auid.%,--x)" tmpfile0
322
323         func_test_copy tmpfile0 tmpfile2
324
325         # Set an ACL for a group.
326         orig=`lsacl tmpfile0 | sed -e 's/ tmpfile0$//'`
327         chacl -r "${orig}(%.$agid,r--)" tmpfile0
328
329         func_test_copy tmpfile0 tmpfile3
330
331         # Set an ACL for other.
332         orig=`lsacl tmpfile0 | sed -e 's/ tmpfile0$//'`
333         chacl -r "${orig}(%.%,r--)" tmpfile0
334
335         func_test_copy tmpfile0 tmpfile4
336
337         # Remove the ACL for the user.
338         chacl -d "($auid.%,--x)" tmpfile0
339
340         func_test_copy tmpfile0 tmpfile5
341
342         # Remove the ACL for the group.
343         chacl -d "(%.$agid,r--)" tmpfile0
344
345         func_test_copy tmpfile0 tmpfile6
346
347         # Delete all optional ACLs.
348         chacl -z tmpfile0
349
350         func_test_copy tmpfile0 tmpfile8
351
352         # Copy ACLs from a file that has no ACLs.
353         echo > tmpfile9
354         chmod a+x tmpfile9
355         orig=`lsacl tmpfile9 | sed -e 's/ tmpfile9$//'`
356         rm -f tmpfile9
357         chacl -r "${orig}" tmpfile0
358
359         func_test_copy tmpfile0 tmpfile9
360
361         ;;
362
363       osf1)
364
365         # Set an ACL for a user.
366         setacl -u user:$auid:1 tmpfile0
367
368         func_test_copy tmpfile0 tmpfile2
369
370         # Set an ACL for a group.
371         setacl -u group:$agid:4 tmpfile0
372
373         func_test_copy tmpfile0 tmpfile3
374
375         # Set an ACL for other.
376         setacl -u other::4 tmpfile0
377
378         func_test_copy tmpfile0 tmpfile4
379
380         # Remove the ACL for the user.
381         setacl -x user:$auid:1 tmpfile0
382
383         func_test_copy tmpfile0 tmpfile5
384
385         if false; then # would give an error "can't set ACL: Invalid argument"
386           # Remove the ACL for other.
387           setacl -x other::4 tmpfile0
388
389           func_test_copy tmpfile0 tmpfile6
390         fi
391
392         # Remove the ACL for the group.
393         setacl -x group:$agid:4 tmpfile0
394
395         func_test_copy tmpfile0 tmpfile7
396
397         # Delete all optional ACLs.
398         setacl -u user:$auid:1 tmpfile0
399         setacl -b tmpfile0
400
401         func_test_copy tmpfile0 tmpfile8
402
403         # Copy ACLs from a file that has no ACLs.
404         echo > tmpfile9
405         chmod a+x tmpfile9
406         getacl tmpfile9 > tmpaclout0
407         setacl -b -U tmpaclout0 tmpfile0
408         rm -f tmpfile9
409
410         func_test_copy tmpfile0 tmpfile9
411
412         ;;
413
414       aix)
415
416         # Set an ACL for a user.
417         { aclget tmpfile0 | sed -e 's/disabled$/enabled/'; echo "        permit --x u:$auid"; } | aclput tmpfile0
418
419         func_test_copy tmpfile0 tmpfile2
420
421         # Set an ACL for a group.
422         { aclget tmpfile0 | sed -e 's/disabled$/enabled/'; echo "        permit r-- g:$agid"; } | aclput tmpfile0
423
424         func_test_copy tmpfile0 tmpfile3
425
426         # Set an ACL for other.
427         chmod o+r tmpfile0
428
429         func_test_copy tmpfile0 tmpfile4
430
431         # Remove the ACL for the user.
432         aclget tmpfile0 | grep -v ' u:[^ ]*$' | aclput tmpfile0
433
434         func_test_copy tmpfile0 tmpfile5
435
436         # Remove the ACL for the group.
437         aclget tmpfile0 | grep -v ' g:[^ ]*$' | aclput tmpfile0
438
439         func_test_copy tmpfile0 tmpfile7
440
441         # Delete all optional ACLs.
442         aclget tmpfile0 | sed -e 's/enabled$/disabled/' | sed -e '/disabled$/q' | aclput tmpfile0
443
444         func_test_copy tmpfile0 tmpfile8
445
446         # Copy ACLs from a file that has no ACLs.
447         echo > tmpfile9
448         chmod a+x tmpfile9
449         aclget tmpfile9 | aclput tmpfile0
450         rm -f tmpfile9
451
452         func_test_copy tmpfile0 tmpfile9
453
454         ;;
455
456       macosx)
457
458         # Set an ACL for a user.
459         /bin/chmod +a "user:daemon allow execute" tmpfile0
460
461         func_test_copy tmpfile0 tmpfile2
462
463         # Set an ACL for a group.
464         /bin/chmod +a "group:daemon allow read" tmpfile0
465
466         func_test_copy tmpfile0 tmpfile3
467
468         # Set an ACL for other.
469         chmod o+r tmpfile0
470
471         func_test_copy tmpfile0 tmpfile4
472
473         # Remove the ACL for the user.
474         /bin/chmod -a "user:daemon allow execute" tmpfile0
475
476         func_test_copy tmpfile0 tmpfile5
477
478         # Remove the ACL for the group.
479         /bin/chmod -a "group:daemon allow read" tmpfile0
480
481         func_test_copy tmpfile0 tmpfile7
482
483         # Delete all optional ACLs.
484         /bin/chmod -N tmpfile0
485
486         func_test_copy tmpfile0 tmpfile8
487
488         # Copy ACLs from a file that has no ACLs.
489         echo > tmpfile9
490         chmod a+x tmpfile9
491         { /bin/ls -le tmpfile9 | sed -n -e 's/^ [0-9][0-9]*: //p'; echo; } | /bin/chmod -E tmpfile0
492         rm -f tmpfile9
493
494         func_test_copy tmpfile0 tmpfile9
495
496         ;;
497
498       irix)
499
500         # Set an ACL for a user.
501         /sbin/chacl user::rw-,group::---,other::---,user:$auid:--x tmpfile0
502
503         func_test_copy tmpfile0 tmpfile2
504
505         # Set an ACL for a group.
506         /sbin/chacl user::rw-,group::---,other::---,user:$auid:--x,group:$agid:r-- tmpfile0
507
508         func_test_copy tmpfile0 tmpfile3
509
510         # Set an ACL for other.
511         /sbin/chacl user::rw-,group::---,user:$auid:--x,group:$agid:r--,other::r-- tmpfile0
512
513         func_test_copy tmpfile0 tmpfile4
514
515         # Remove the ACL for the user.
516         /sbin/chacl user::rw-,group::---,group:$agid:r--,other::r-- tmpfile0
517
518         func_test_copy tmpfile0 tmpfile5
519
520         # Remove the ACL for the group.
521         /sbin/chacl user::rw-,group::---,other::r-- tmpfile0
522
523         func_test_copy tmpfile0 tmpfile7
524
525         ;;
526
527     esac
528   fi
529
530   rm -f tmpfile[0-9] tmpaclout[0-2]
531 ) || exit 1
532
533 rm -rf "$tmp"
534 exit 0