pty: Activate the signature wrapper of forkpty.
[gnulib.git] / tests / test-copy-file.sh
1 #!/bin/sh
2
3 # Show all commands when run with environment variable VERBOSE=yes.
4 test -z "$VERBOSE" || set -x
5
6 # func_tmpdir
7 # creates a temporary directory.
8 # Sets variable
9 # - tmp             pathname of freshly created temporary directory
10 func_tmpdir ()
11 {
12   # Use the environment variable TMPDIR, falling back to /tmp. This allows
13   # users to specify a different temporary directory, for example, if their
14   # /tmp is filled up or too small.
15   : ${TMPDIR=/tmp}
16   {
17     # Use the mktemp program if available. If not available, hide the error
18     # message.
19     tmp=`(umask 077 && mktemp -d "$TMPDIR/glXXXXXX") 2>/dev/null` &&
20     test -n "$tmp" && test -d "$tmp"
21   } ||
22   {
23     # Use a simple mkdir command. It is guaranteed to fail if the directory
24     # already exists.  $RANDOM is bash specific and expands to empty in shells
25     # other than bash, ksh and zsh.  Its use does not increase security;
26     # rather, it minimizes the probability of failure in a very cluttered /tmp
27     # directory.
28     tmp=$TMPDIR/gl$$-$RANDOM
29     (umask 077 && mkdir "$tmp")
30   } ||
31   {
32     echo "$0: cannot create a temporary directory in $TMPDIR" >&2
33     exit 1
34   }
35 }
36
37 func_tmpdir
38 builddir=`pwd`
39 cd "$builddir" ||
40   {
41     echo "$0: cannot determine build directory (unreadable parent dir?)" >&2
42     exit 1
43   }
44 # Switch to a temporary directory, to increase the likelihood that ACLs are
45 # supported on the current file system. (/tmp is usually locally mounted,
46 # whereas the build dir is sometimes NFS-mounted.)
47 ( cd "$tmp"
48
49   # Prepare tmpfile0.
50   rm -f tmpfile[0-9] tmpaclout[0-2]
51   echo "Simple contents" > tmpfile0
52   chmod 600 tmpfile0
53
54   # Classification of the platform according to the programs available for
55   # manipulating ACLs.
56   # Possible values are:
57   #   linux, cygwin, freebsd, solaris, hpux, hpuxjfs, osf1, aix, macosx, irix, none.
58   # TODO: Support also native Windows platforms (mingw).
59   acl_flavor=none
60   if (getfacl tmpfile0 >/dev/null) 2>/dev/null; then
61     # Platforms with the getfacl and setfacl programs.
62     # Linux, FreeBSD, Solaris, Cygwin.
63     if (setfacl --help >/dev/null) 2>/dev/null; then
64       # Linux, Cygwin.
65       if (LC_ALL=C setfacl --help | grep ' --set-file' >/dev/null) 2>/dev/null; then
66         # Linux.
67         acl_flavor=linux
68       else
69         acl_flavor=cygwin
70       fi
71     else
72       # FreeBSD, Solaris.
73       if (LC_ALL=C setfacl 2>&1 | grep '\-x entries' >/dev/null) 2>/dev/null; then
74         # FreeBSD.
75         acl_flavor=freebsd
76       else
77         # Solaris.
78         acl_flavor=solaris
79       fi
80     fi
81   else
82     if (lsacl / >/dev/null) 2>/dev/null; then
83       # Platforms with the lsacl and chacl programs.
84       # HP-UX, sometimes also IRIX.
85       if (getacl tmpfile0 >/dev/null) 2>/dev/null; then
86         # HP-UX 11.11 or newer.
87         acl_flavor=hpuxjfs
88       else
89         # HP-UX 11.00.
90         acl_flavor=hpux
91       fi
92     else
93       if (getacl tmpfile0 >/dev/null) 2>/dev/null; then
94         # Tru64, NonStop Kernel.
95         if (getacl -m tmpfile0 >/dev/null) 2>/dev/null; then
96           # Tru64.
97           acl_flavor=osf1
98         else
99           # NonStop Kernel.
100           acl_flavor=nsk
101         fi
102       else
103         if (aclget tmpfile0 >/dev/null) 2>/dev/null; then
104           # AIX.
105           acl_flavor=aix
106         else
107           if (fsaclctl -v >/dev/null) 2>/dev/null; then
108             # Mac OS X.
109             acl_flavor=macosx
110           else
111             if test -f /sbin/chacl; then
112               # IRIX.
113               acl_flavor=irix
114             fi
115           fi
116         fi
117       fi
118     fi
119   fi
120
121   # Define a function to test for the same ACLs, from the point of view of
122   # the programs.
123   # func_test_same_acls file1 file2
124   case $acl_flavor in
125     linux | cygwin | freebsd | solaris)
126       func_test_same_acls ()
127       {
128         getfacl "$1" | sed -e "s/$1/FILENAME/g" > tmpaclout1
129         getfacl "$2" | sed -e "s/$2/FILENAME/g" > tmpaclout2
130         cmp tmpaclout1 tmpaclout2 > /dev/null
131       }
132       ;;
133     hpux)
134       func_test_same_acls ()
135       {
136         lsacl "$1" | sed -e "s/$1/FILENAME/g" > tmpaclout1
137         lsacl "$2" | sed -e "s/$2/FILENAME/g" > tmpaclout2
138         cmp tmpaclout1 tmpaclout2 > /dev/null
139       }
140       ;;
141     hpuxjfs)
142       func_test_same_acls ()
143       {
144         { lsacl "$1" | sed -e "s/$1/FILENAME/g" > tmpaclout1
145           lsacl "$2" | sed -e "s/$2/FILENAME/g" > tmpaclout2
146           cmp tmpaclout1 tmpaclout2 > /dev/null
147         } &&
148         { getacl "$1" | sed -e "s/$1/FILENAME/g" > tmpaclout1
149           getacl "$2" | sed -e "s/$2/FILENAME/g" > tmpaclout2
150           cmp tmpaclout1 tmpaclout2 > /dev/null
151         }
152       }
153       ;;
154     osf1 | nsk)
155       func_test_same_acls ()
156       {
157         getacl "$1" | sed -e "s/$1/FILENAME/g" > tmpaclout1
158         getacl "$2" | sed -e "s/$2/FILENAME/g" > tmpaclout2
159         cmp tmpaclout1 tmpaclout2 > /dev/null
160       }
161       ;;
162     aix)
163       func_test_same_acls ()
164       {
165         aclget "$1" > tmpaclout1
166         aclget "$2" > tmpaclout2
167         cmp tmpaclout1 tmpaclout2 > /dev/null
168       }
169       ;;
170     macosx)
171       func_test_same_acls ()
172       {
173         /bin/ls -le "$1" | sed -e "s/$1/FILENAME/g" > tmpaclout1
174         /bin/ls -le "$2" | sed -e "s/$2/FILENAME/g" > tmpaclout2
175         cmp tmpaclout1 tmpaclout2 > /dev/null
176       }
177       ;;
178     irix)
179       func_test_same_acls ()
180       {
181         /bin/ls -lD "$1" | sed -e "s/$1/FILENAME/g" > tmpaclout1
182         /bin/ls -lD "$2" | sed -e "s/$2/FILENAME/g" > tmpaclout2
183         cmp tmpaclout1 tmpaclout2 > /dev/null
184       }
185       ;;
186     none)
187       func_test_same_acls ()
188       {
189         :
190       }
191       ;;
192   esac
193
194   # func_test_copy file1 file2
195   # copies file1 to file2 and verifies the permissions and ACLs are the same
196   # on both.
197   func_test_copy ()
198   {
199     "$builddir"/test-copy-file${EXEEXT} "$1" "$2" || exit 1
200     if test "$USE_ACL" != 0; then
201       "$builddir"/test-sameacls${EXEEXT} "$1" "$2" || exit 1
202       func_test_same_acls                "$1" "$2" || exit 1
203     fi
204   }
205
206   func_test_copy tmpfile0 tmpfile1
207
208   if test "$USE_ACL" != 0 && test $acl_flavor != none; then
209     # A POSIX compliant 'id' program.
210     if test -f /usr/xpg4/bin/id; then
211       ID=/usr/xpg4/bin/id
212     else
213       ID=id
214     fi
215     # Use a user and group id different from the current one, to avoid
216     # redundant/ambiguous ACLs.
217     myuid=`$ID -u`
218     mygid=`$ID -g`
219     auid=1
220     if test "$auid" = "$myuid"; then auid=2; fi
221     agid=1
222     if test "$agid" = "$mygid"; then agid=2; fi
223
224     case $acl_flavor in
225       linux | freebsd | solaris)
226
227         # Set an ACL for a user.
228         setfacl -m user:$auid:1 tmpfile0
229
230         func_test_copy tmpfile0 tmpfile2
231
232         # Set an ACL for a group.
233         setfacl -m group:$agid:4 tmpfile0
234
235         func_test_copy tmpfile0 tmpfile3
236
237         # Set an ACL for other.
238         case $acl_flavor in
239           freebsd) setfacl -m other::4 tmpfile0 ;;
240           solaris) chmod o+r tmpfile0 ;;
241           *)       setfacl -m other:4 tmpfile0 ;;
242         esac
243
244         func_test_copy tmpfile0 tmpfile4
245
246         # Remove the ACL for the user.
247         case $acl_flavor in
248           linux)   setfacl -x user:$auid tmpfile0 ;;
249           freebsd) setfacl -x user:$auid:1 tmpfile0 ;;
250           *)       setfacl -d user:$auid:1 tmpfile0 ;;
251         esac
252
253         func_test_copy tmpfile0 tmpfile5
254
255         # Remove the ACL for other.
256         case $acl_flavor in
257           linux | solaris) ;; # impossible
258           freebsd) setfacl -x other::4 tmpfile0 ;;
259           *)       setfacl -d other:4 tmpfile0 ;;
260         esac
261
262         func_test_copy tmpfile0 tmpfile6
263
264         # Remove the ACL for the group.
265         case $acl_flavor in
266           linux)   setfacl -x group:$agid tmpfile0 ;;
267           freebsd) setfacl -x group:$agid:4 tmpfile0 ;;
268           *)       setfacl -d group:$agid:4 tmpfile0 ;;
269         esac
270
271         func_test_copy tmpfile0 tmpfile7
272
273         # Delete all optional ACLs.
274         case $acl_flavor in
275           linux | freebsd)
276             setfacl -m user:$auid:1 tmpfile0
277             setfacl -b tmpfile0
278             ;;
279           *)
280             setfacl -s user::6,group::0,other:0 tmpfile0 ;;
281         esac
282
283         func_test_copy tmpfile0 tmpfile8
284
285         # Copy ACLs from a file that has no ACLs.
286         echo > tmpfile9
287         chmod a+x tmpfile9
288         case $acl_flavor in
289           linux)   getfacl tmpfile9 | setfacl --set-file=- tmpfile0 ;;
290           freebsd) ;;
291           *)       getfacl tmpfile9 | setfacl -f - tmpfile0 ;;
292         esac
293         rm -f tmpfile9
294
295         func_test_copy tmpfile0 tmpfile9
296
297         ;;
298
299       cygwin)
300
301         # Set an ACL for a group.
302         setfacl -m group:0:1 tmpfile0
303
304         func_test_copy tmpfile0 tmpfile2
305
306         # Set an ACL for other.
307         setfacl -m other:4 tmpfile0
308
309         func_test_copy tmpfile0 tmpfile4
310
311         # Remove the ACL for the group.
312         setfacl -d group:0 tmpfile0
313
314         func_test_copy tmpfile0 tmpfile5
315
316         # Remove the ACL for other.
317         setfacl -d other:4 tmpfile0
318
319         func_test_copy tmpfile0 tmpfile6
320
321         # Delete all optional ACLs.
322         setfacl -s user::6,group::0,other:0 tmpfile0
323
324         func_test_copy tmpfile0 tmpfile8
325
326         # Copy ACLs from a file that has no ACLs.
327         echo > tmpfile9
328         chmod a+x tmpfile9
329         getfacl tmpfile9 | setfacl -f - tmpfile0
330         rm -f tmpfile9
331
332         func_test_copy tmpfile0 tmpfile9
333
334         ;;
335
336       hpux)
337
338         # Set an ACL for a user.
339         orig=`lsacl tmpfile0 | sed -e 's/ tmpfile0$//'`
340         chacl -r "${orig}($auid.%,--x)" tmpfile0
341
342         func_test_copy tmpfile0 tmpfile2
343
344         # Set an ACL for a group.
345         orig=`lsacl tmpfile0 | sed -e 's/ tmpfile0$//'`
346         chacl -r "${orig}(%.$agid,r--)" tmpfile0
347
348         func_test_copy tmpfile0 tmpfile3
349
350         # Set an ACL for other.
351         orig=`lsacl tmpfile0 | sed -e 's/ tmpfile0$//'`
352         chacl -r "${orig}(%.%,r--)" tmpfile0
353
354         func_test_copy tmpfile0 tmpfile4
355
356         # Remove the ACL for the user.
357         chacl -d "($auid.%,--x)" tmpfile0
358
359         func_test_copy tmpfile0 tmpfile5
360
361         # Remove the ACL for the group.
362         chacl -d "(%.$agid,r--)" tmpfile0
363
364         func_test_copy tmpfile0 tmpfile6
365
366         # Delete all optional ACLs.
367         chacl -z tmpfile0
368
369         func_test_copy tmpfile0 tmpfile8
370
371         # Copy ACLs from a file that has no ACLs.
372         echo > tmpfile9
373         chmod a+x tmpfile9
374         orig=`lsacl tmpfile9 | sed -e 's/ tmpfile9$//'`
375         rm -f tmpfile9
376         chacl -r "${orig}" tmpfile0
377
378         func_test_copy tmpfile0 tmpfile9
379
380         ;;
381
382       hpuxjfs)
383
384         # Set an ACL for a user.
385         orig=`lsacl tmpfile0 | sed -e 's/ tmpfile0$//'`
386         chacl -r "${orig}($auid.%,--x)" tmpfile0 \
387           || setacl -m user:$auid:1 tmpfile0
388
389         func_test_copy tmpfile0 tmpfile2
390
391         # Set an ACL for a group.
392         orig=`lsacl tmpfile0 | sed -e 's/ tmpfile0$//'`
393         chacl -r "${orig}(%.$agid,r--)" tmpfile0 \
394           || setacl -m group:$agid:4 tmpfile0
395
396         func_test_copy tmpfile0 tmpfile3
397
398         # Set an ACL for other.
399         orig=`lsacl tmpfile0 | sed -e 's/ tmpfile0$//'`
400         chacl -r "${orig}(%.%,r--)" tmpfile0 \
401           || setacl -m other:4 tmpfile0
402
403         func_test_copy tmpfile0 tmpfile4
404
405         # Remove the ACL for the user.
406         chacl -d "($auid.%,--x)" tmpfile0 \
407           || setacl -d user:$auid tmpfile0
408
409         func_test_copy tmpfile0 tmpfile5
410
411         # Remove the ACL for the group.
412         chacl -d "(%.$agid,r--)" tmpfile0 \
413           || setacl -d group:$agid tmpfile0
414
415         func_test_copy tmpfile0 tmpfile6
416
417         # Delete all optional ACLs.
418         chacl -z tmpfile0 \
419           || { setacl -m user:$auid:1 tmpfile0
420                setacl -s user::6,group::0,class:7,other:0 tmpfile0
421              }
422
423         func_test_copy tmpfile0 tmpfile8
424
425         # Copy ACLs from a file that has no ACLs.
426         echo > tmpfile9
427         chmod a+x tmpfile9
428         orig=`lsacl tmpfile9 | sed -e 's/ tmpfile9$//'`
429         getacl tmpfile9 > tmpaclout0
430         rm -f tmpfile9
431         chacl -r "${orig}" tmpfile0 \
432           || setacl -f tmpaclout0 tmpfile0
433
434         func_test_copy tmpfile0 tmpfile9
435
436         ;;
437
438       osf1)
439
440         # Set an ACL for a user.
441         setacl -u user:$auid:1 tmpfile0
442
443         func_test_copy tmpfile0 tmpfile2
444
445         # Set an ACL for a group.
446         setacl -u group:$agid:4 tmpfile0
447
448         func_test_copy tmpfile0 tmpfile3
449
450         # Set an ACL for other.
451         setacl -u other::4 tmpfile0
452
453         func_test_copy tmpfile0 tmpfile4
454
455         # Remove the ACL for the user.
456         setacl -x user:$auid:1 tmpfile0
457
458         func_test_copy tmpfile0 tmpfile5
459
460         if false; then # would give an error "can't set ACL: Invalid argument"
461           # Remove the ACL for other.
462           setacl -x other::4 tmpfile0
463
464           func_test_copy tmpfile0 tmpfile6
465         fi
466
467         # Remove the ACL for the group.
468         setacl -x group:$agid:4 tmpfile0
469
470         func_test_copy tmpfile0 tmpfile7
471
472         # Delete all optional ACLs.
473         setacl -u user:$auid:1 tmpfile0
474         setacl -b tmpfile0
475
476         func_test_copy tmpfile0 tmpfile8
477
478         # Copy ACLs from a file that has no ACLs.
479         echo > tmpfile9
480         chmod a+x tmpfile9
481         getacl tmpfile9 > tmpaclout0
482         setacl -b -U tmpaclout0 tmpfile0
483         rm -f tmpfile9
484
485         func_test_copy tmpfile0 tmpfile9
486
487         ;;
488
489       nsk)
490
491         # Set an ACL for a user.
492         setacl -m user:$auid:1 tmpfile0
493
494         func_test_copy tmpfile0 tmpfile2
495
496         # Set an ACL for a group.
497         setacl -m group:$agid:4 tmpfile0
498
499         func_test_copy tmpfile0 tmpfile3
500
501         # Set an ACL for other.
502         setacl -m other:4 tmpfile0
503
504         func_test_copy tmpfile0 tmpfile4
505
506         # Remove the ACL for the user.
507         setacl -d user:$auid tmpfile0
508
509         func_test_copy tmpfile0 tmpfile5
510
511         # Remove the ACL for the group.
512         setacl -d group:$agid tmpfile0
513
514         func_test_copy tmpfile0 tmpfile6
515
516         # Delete all optional ACLs.
517         setacl -m user:$auid:1 tmpfile0
518         setacl -s user::6,group::0,class:7,other:0 tmpfile0
519
520         func_test_copy tmpfile0 tmpfile8
521
522         # Copy ACLs from a file that has no ACLs.
523         echo > tmpfile9
524         chmod a+x tmpfile9
525         getacl tmpfile9 > tmpaclout0
526         setacl -f tmpaclout0 tmpfile0
527         rm -f tmpfile9
528
529         func_test_copy tmpfile0 tmpfile9
530
531         ;;
532
533       aix)
534
535         # Set an ACL for a user.
536         { aclget tmpfile0 | sed -e 's/disabled$/enabled/'; echo "        permit --x u:$auid"; } | aclput tmpfile0
537
538         func_test_copy tmpfile0 tmpfile2
539
540         # Set an ACL for a group.
541         { aclget tmpfile0 | sed -e 's/disabled$/enabled/'; echo "        permit r-- g:$agid"; } | aclput tmpfile0
542
543         func_test_copy tmpfile0 tmpfile3
544
545         # Set an ACL for other.
546         chmod o+r tmpfile0
547
548         func_test_copy tmpfile0 tmpfile4
549
550         # Remove the ACL for the user.
551         aclget tmpfile0 | grep -v ' u:[^ ]*$' | aclput tmpfile0
552
553         func_test_copy tmpfile0 tmpfile5
554
555         # Remove the ACL for the group.
556         aclget tmpfile0 | grep -v ' g:[^ ]*$' | aclput tmpfile0
557
558         func_test_copy tmpfile0 tmpfile7
559
560         # Delete all optional ACLs.
561         aclget tmpfile0 | sed -e 's/enabled$/disabled/' | sed -e '/disabled$/q' | aclput tmpfile0
562
563         func_test_copy tmpfile0 tmpfile8
564
565         # Copy ACLs from a file that has no ACLs.
566         echo > tmpfile9
567         chmod a+x tmpfile9
568         aclget tmpfile9 | aclput tmpfile0
569         rm -f tmpfile9
570
571         func_test_copy tmpfile0 tmpfile9
572
573         ;;
574
575       macosx)
576
577         # Set an ACL for a user.
578         /bin/chmod +a "user:daemon allow execute" tmpfile0
579
580         func_test_copy tmpfile0 tmpfile2
581
582         # Set an ACL for a group.
583         /bin/chmod +a "group:daemon allow read" tmpfile0
584
585         func_test_copy tmpfile0 tmpfile3
586
587         # Set an ACL for other.
588         chmod o+r tmpfile0
589
590         func_test_copy tmpfile0 tmpfile4
591
592         # Remove the ACL for the user.
593         /bin/chmod -a "user:daemon allow execute" tmpfile0
594
595         func_test_copy tmpfile0 tmpfile5
596
597         # Remove the ACL for the group.
598         /bin/chmod -a "group:daemon allow read" tmpfile0
599
600         func_test_copy tmpfile0 tmpfile7
601
602         # Delete all optional ACLs.
603         /bin/chmod -N tmpfile0
604
605         func_test_copy tmpfile0 tmpfile8
606
607         # Copy ACLs from a file that has no ACLs.
608         echo > tmpfile9
609         chmod a+x tmpfile9
610         { /bin/ls -le tmpfile9 | sed -n -e 's/^ [0-9][0-9]*: //p'; echo; } | /bin/chmod -E tmpfile0
611         rm -f tmpfile9
612
613         func_test_copy tmpfile0 tmpfile9
614
615         ;;
616
617       irix)
618
619         # Set an ACL for a user.
620         /sbin/chacl user::rw-,group::---,other::---,user:$auid:--x tmpfile0
621
622         func_test_copy tmpfile0 tmpfile2
623
624         # Set an ACL for a group.
625         /sbin/chacl user::rw-,group::---,other::---,user:$auid:--x,group:$agid:r-- tmpfile0
626
627         func_test_copy tmpfile0 tmpfile3
628
629         # Set an ACL for other.
630         /sbin/chacl user::rw-,group::---,user:$auid:--x,group:$agid:r--,other::r-- tmpfile0
631
632         func_test_copy tmpfile0 tmpfile4
633
634         # Remove the ACL for the user.
635         /sbin/chacl user::rw-,group::---,group:$agid:r--,other::r-- tmpfile0
636
637         func_test_copy tmpfile0 tmpfile5
638
639         # Remove the ACL for the group.
640         /sbin/chacl user::rw-,group::---,other::r-- tmpfile0
641
642         func_test_copy tmpfile0 tmpfile7
643
644         ;;
645
646     esac
647   fi
648
649   rm -f tmpfile[0-9] tmpaclout[0-2]
650 ) || exit 1
651
652 rm -rf "$tmp"
653 exit 0