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