usleep tests: Avoid failure due to known Cygwin 3.5.3 bug.
[gnulib.git] / tests / test-copy-file.sh
blob9cd9e2093d6952bb0e1e2e67159f181f34c9ba92
1 #!/bin/sh
3 # Show all commands when run with environment variable VERBOSE=yes.
4 test -z "$VERBOSE" || set -x
6 # func_tmpdir
7 # creates a temporary directory.
8 # Sets variable
9 # - tmp pathname of freshly created temporary directory
10 func_tmpdir ()
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}"
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 } ||
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 } ||
32 echo "$0: cannot create a temporary directory in $TMPDIR" >&2
33 exit 1
37 func_tmpdir
38 # builddir may already be set by the script that invokes this one.
39 case "$builddir" in
40 '') builddir=`pwd` ;;
41 /* | ?:*) ;;
42 *) builddir=`pwd`/$builddir ;;
43 esac
44 cd "$builddir" ||
46 echo "$0: cannot determine build directory (unreadable parent dir?)" >&2
47 exit 1
49 # Switch to a temporary directory, to increase the likelihood that ACLs are
50 # supported on the current file system. (/tmp is usually locally mounted,
51 # whereas the build dir is sometimes NFS-mounted.)
52 ( cd "$tmp"
54 # Prepare tmpfile0.
55 rm -f tmpfile[0-9] tmpaclout[0-2]
56 echo "Simple contents" > tmpfile0
57 chmod 600 tmpfile0
59 # Classification of the platform according to the programs available for
60 # manipulating ACLs.
61 # Possible values are:
62 # linux, cygwin, freebsd, solaris, hpux, hpuxjfs, osf1, aix, macosx, irix, none.
63 # TODO: Support also native Windows platforms (mingw).
64 acl_flavor=none
65 if (getfacl tmpfile0 >/dev/null) 2>/dev/null; then
66 # Platforms with the getfacl and setfacl programs.
67 # Linux, FreeBSD, Solaris, Cygwin.
68 if (setfacl --help >/dev/null) 2>/dev/null; then
69 # Linux, Cygwin.
70 if (LC_ALL=C setfacl --help | grep ' --set-file' >/dev/null) 2>/dev/null; then
71 # Linux.
72 acl_flavor=linux
73 else
74 acl_flavor=cygwin
76 else
77 # FreeBSD, Solaris.
78 if (LC_ALL=C setfacl 2>&1 | grep '\-x entries' >/dev/null) 2>/dev/null; then
79 # FreeBSD.
80 acl_flavor=freebsd
81 else
82 # Solaris.
83 acl_flavor=solaris
86 else
87 if (lsacl / >/dev/null) 2>/dev/null; then
88 # Platforms with the lsacl and chacl programs.
89 # HP-UX, sometimes also IRIX.
90 if (getacl tmpfile0 >/dev/null) 2>/dev/null; then
91 # HP-UX 11.11 or newer.
92 acl_flavor=hpuxjfs
93 else
94 # HP-UX 11.00.
95 acl_flavor=hpux
97 else
98 if (getacl tmpfile0 >/dev/null) 2>/dev/null; then
99 # Tru64, NonStop Kernel.
100 if (getacl -m tmpfile0 >/dev/null) 2>/dev/null; then
101 # Tru64.
102 acl_flavor=osf1
103 else
104 # NonStop Kernel.
105 acl_flavor=nsk
107 else
108 if (aclget tmpfile0 >/dev/null) 2>/dev/null; then
109 # AIX.
110 acl_flavor=aix
111 else
112 if (fsaclctl -v >/dev/null) 2>/dev/null; then
113 # Mac OS X.
114 acl_flavor=macosx
115 else
116 if test -f /sbin/chacl; then
117 # IRIX.
118 acl_flavor=irix
126 # Define a function to test for the same ACLs, from the point of view of
127 # the programs.
128 # func_test_same_acls file1 file2
129 case $acl_flavor in
130 linux | cygwin | freebsd | solaris)
131 func_test_same_acls ()
133 getfacl "$1" | sed -e "s/$1/FILENAME/g" > tmpaclout1
134 getfacl "$2" | sed -e "s/$2/FILENAME/g" > tmpaclout2
135 cmp tmpaclout1 tmpaclout2 > /dev/null
138 hpux)
139 func_test_same_acls ()
141 lsacl "$1" | sed -e "s/$1/FILENAME/g" > tmpaclout1
142 lsacl "$2" | sed -e "s/$2/FILENAME/g" > tmpaclout2
143 cmp tmpaclout1 tmpaclout2 > /dev/null
146 hpuxjfs)
147 func_test_same_acls ()
149 { lsacl "$1" | sed -e "s/$1/FILENAME/g" > tmpaclout1
150 lsacl "$2" | sed -e "s/$2/FILENAME/g" > tmpaclout2
151 cmp tmpaclout1 tmpaclout2 > /dev/null
152 } &&
153 { getacl "$1" | sed -e "s/$1/FILENAME/g" > tmpaclout1
154 getacl "$2" | sed -e "s/$2/FILENAME/g" > tmpaclout2
155 cmp tmpaclout1 tmpaclout2 > /dev/null
159 osf1 | nsk)
160 func_test_same_acls ()
162 getacl "$1" | sed -e "s/$1/FILENAME/g" > tmpaclout1
163 getacl "$2" | sed -e "s/$2/FILENAME/g" > tmpaclout2
164 cmp tmpaclout1 tmpaclout2 > /dev/null
167 aix)
168 func_test_same_acls ()
170 aclget "$1" > tmpaclout1
171 aclget "$2" > tmpaclout2
172 cmp tmpaclout1 tmpaclout2 > /dev/null
175 macosx)
176 func_test_same_acls ()
178 /bin/ls -le "$1" | sed -e "s/$1/FILENAME/g" > tmpaclout1
179 /bin/ls -le "$2" | sed -e "s/$2/FILENAME/g" > tmpaclout2
180 cmp tmpaclout1 tmpaclout2 > /dev/null
183 irix)
184 func_test_same_acls ()
186 /bin/ls -lD "$1" | sed -e "s/$1/FILENAME/g" > tmpaclout1
187 /bin/ls -lD "$2" | sed -e "s/$2/FILENAME/g" > tmpaclout2
188 cmp tmpaclout1 tmpaclout2 > /dev/null
191 none)
192 func_test_same_acls ()
197 esac
199 # func_test_copy file1 file2
200 # copies file1 to file2 and verifies the permissions and ACLs are the same
201 # on both.
202 func_test_copy ()
204 ${CHECKER} "$builddir"/test-copy-file${EXEEXT} "$1" "$2" || exit 1
205 if test "$USE_ACL" != 0; then
206 ${CHECKER} "$builddir"/test-sameacls${EXEEXT} "$1" "$2" || exit 1
207 func_test_same_acls "$1" "$2" || exit 1
211 func_test_copy tmpfile0 tmpfile1
213 if test "$USE_ACL" != 0 && test $acl_flavor != none; then
214 # A POSIX compliant 'id' program.
215 if test -f /usr/xpg4/bin/id; then
216 ID=/usr/xpg4/bin/id
217 else
218 ID=id
220 # Use a user and group id different from the current one, to avoid
221 # redundant/ambiguous ACLs.
222 myuid=`$ID -u`
223 mygid=`$ID -g`
224 auid=1
225 if test "$auid" = "$myuid"; then auid=2; fi
226 agid=1
227 if test "$agid" = "$mygid"; then agid=2; fi
229 case $acl_flavor in
230 linux | freebsd | solaris)
232 # Set an ACL for a user.
233 setfacl -m user:$auid:1 tmpfile0
235 func_test_copy tmpfile0 tmpfile2
237 # Set an ACL for a group.
238 setfacl -m group:$agid:4 tmpfile0
240 func_test_copy tmpfile0 tmpfile3
242 # Set an ACL for other.
243 case $acl_flavor in
244 freebsd) setfacl -m other::4 tmpfile0 ;;
245 solaris) chmod o+r tmpfile0 ;;
246 *) setfacl -m other:4 tmpfile0 ;;
247 esac
249 func_test_copy tmpfile0 tmpfile4
251 # Remove the ACL for the user.
252 case $acl_flavor in
253 linux) setfacl -x user:$auid tmpfile0 ;;
254 freebsd) setfacl -x user:$auid:1 tmpfile0 ;;
255 *) setfacl -d user:$auid:1 tmpfile0 ;;
256 esac
258 func_test_copy tmpfile0 tmpfile5
260 # Remove the ACL for other.
261 case $acl_flavor in
262 linux | solaris) ;; # impossible
263 freebsd) setfacl -x other::4 tmpfile0 ;;
264 *) setfacl -d other:4 tmpfile0 ;;
265 esac
267 func_test_copy tmpfile0 tmpfile6
269 # Remove the ACL for the group.
270 case $acl_flavor in
271 linux) setfacl -x group:$agid tmpfile0 ;;
272 freebsd) setfacl -x group:$agid:4 tmpfile0 ;;
273 *) setfacl -d group:$agid:4 tmpfile0 ;;
274 esac
276 func_test_copy tmpfile0 tmpfile7
278 # Delete all optional ACLs.
279 case $acl_flavor in
280 linux | freebsd)
281 setfacl -m user:$auid:1 tmpfile0
282 setfacl -b tmpfile0
285 setfacl -s user::6,group::0,other:0 tmpfile0 ;;
286 esac
288 func_test_copy tmpfile0 tmpfile8
290 # Copy ACLs from a file that has no ACLs.
291 echo > tmpfile9
292 chmod a+x tmpfile9
293 case $acl_flavor in
294 linux) getfacl tmpfile9 | setfacl --set-file=- tmpfile0 ;;
295 freebsd) ;;
296 *) getfacl tmpfile9 | setfacl -f - tmpfile0 ;;
297 esac
298 rm -f tmpfile9
300 func_test_copy tmpfile0 tmpfile9
304 cygwin)
306 # Set an ACL for a group.
307 setfacl -m group:0:1 tmpfile0
309 func_test_copy tmpfile0 tmpfile2
311 # Set an ACL for other.
312 setfacl -m other:4 tmpfile0
314 func_test_copy tmpfile0 tmpfile4
316 # Remove the ACL for the group.
317 setfacl -d group:0 tmpfile0
319 func_test_copy tmpfile0 tmpfile5
321 # Remove the ACL for other.
322 setfacl -d other:4 tmpfile0
324 func_test_copy tmpfile0 tmpfile6
326 # Delete all optional ACLs.
327 setfacl -s user::6,group::0,other:0 tmpfile0
329 func_test_copy tmpfile0 tmpfile8
331 # Copy ACLs from a file that has no ACLs.
332 echo > tmpfile9
333 chmod a+x tmpfile9
334 getfacl tmpfile9 | setfacl -f - tmpfile0
335 rm -f tmpfile9
337 func_test_copy tmpfile0 tmpfile9
341 hpux)
343 # Set an ACL for a user.
344 orig=`lsacl tmpfile0 | sed -e 's/ tmpfile0$//'`
345 chacl -r "${orig}($auid.%,--x)" tmpfile0
347 func_test_copy tmpfile0 tmpfile2
349 # Set an ACL for a group.
350 orig=`lsacl tmpfile0 | sed -e 's/ tmpfile0$//'`
351 chacl -r "${orig}(%.$agid,r--)" tmpfile0
353 func_test_copy tmpfile0 tmpfile3
355 # Set an ACL for other.
356 orig=`lsacl tmpfile0 | sed -e 's/ tmpfile0$//'`
357 chacl -r "${orig}(%.%,r--)" tmpfile0
359 func_test_copy tmpfile0 tmpfile4
361 # Remove the ACL for the user.
362 chacl -d "($auid.%,--x)" tmpfile0
364 func_test_copy tmpfile0 tmpfile5
366 # Remove the ACL for the group.
367 chacl -d "(%.$agid,r--)" tmpfile0
369 func_test_copy tmpfile0 tmpfile6
371 # Delete all optional ACLs.
372 chacl -z tmpfile0
374 func_test_copy tmpfile0 tmpfile8
376 # Copy ACLs from a file that has no ACLs.
377 echo > tmpfile9
378 chmod a+x tmpfile9
379 orig=`lsacl tmpfile9 | sed -e 's/ tmpfile9$//'`
380 rm -f tmpfile9
381 chacl -r "${orig}" tmpfile0
383 func_test_copy tmpfile0 tmpfile9
387 hpuxjfs)
389 # Set an ACL for a user.
390 orig=`lsacl tmpfile0 | sed -e 's/ tmpfile0$//'`
391 chacl -r "${orig}($auid.%,--x)" tmpfile0 \
392 || setacl -m user:$auid:1 tmpfile0
394 func_test_copy tmpfile0 tmpfile2
396 # Set an ACL for a group.
397 orig=`lsacl tmpfile0 | sed -e 's/ tmpfile0$//'`
398 chacl -r "${orig}(%.$agid,r--)" tmpfile0 \
399 || setacl -m group:$agid:4 tmpfile0
401 func_test_copy tmpfile0 tmpfile3
403 # Set an ACL for other.
404 orig=`lsacl tmpfile0 | sed -e 's/ tmpfile0$//'`
405 chacl -r "${orig}(%.%,r--)" tmpfile0 \
406 || setacl -m other:4 tmpfile0
408 func_test_copy tmpfile0 tmpfile4
410 # Remove the ACL for the user.
411 chacl -d "($auid.%,--x)" tmpfile0 \
412 || setacl -d user:$auid tmpfile0
414 func_test_copy tmpfile0 tmpfile5
416 # Remove the ACL for the group.
417 chacl -d "(%.$agid,r--)" tmpfile0 \
418 || setacl -d group:$agid tmpfile0
420 func_test_copy tmpfile0 tmpfile6
422 # Delete all optional ACLs.
423 chacl -z tmpfile0 \
424 || { setacl -m user:$auid:1 tmpfile0
425 setacl -s user::6,group::0,class:7,other:0 tmpfile0
428 func_test_copy tmpfile0 tmpfile8
430 # Copy ACLs from a file that has no ACLs.
431 echo > tmpfile9
432 chmod a+x tmpfile9
433 orig=`lsacl tmpfile9 | sed -e 's/ tmpfile9$//'`
434 getacl tmpfile9 > tmpaclout0
435 rm -f tmpfile9
436 chacl -r "${orig}" tmpfile0 \
437 || setacl -f tmpaclout0 tmpfile0
439 func_test_copy tmpfile0 tmpfile9
443 osf1)
445 # Set an ACL for a user.
446 setacl -u user:$auid:1 tmpfile0
448 func_test_copy tmpfile0 tmpfile2
450 # Set an ACL for a group.
451 setacl -u group:$agid:4 tmpfile0
453 func_test_copy tmpfile0 tmpfile3
455 # Set an ACL for other.
456 setacl -u other::4 tmpfile0
458 func_test_copy tmpfile0 tmpfile4
460 # Remove the ACL for the user.
461 setacl -x user:$auid:1 tmpfile0
463 func_test_copy tmpfile0 tmpfile5
465 if false; then # would give an error "can't set ACL: Invalid argument"
466 # Remove the ACL for other.
467 setacl -x other::4 tmpfile0
469 func_test_copy tmpfile0 tmpfile6
472 # Remove the ACL for the group.
473 setacl -x group:$agid:4 tmpfile0
475 func_test_copy tmpfile0 tmpfile7
477 # Delete all optional ACLs.
478 setacl -u user:$auid:1 tmpfile0
479 setacl -b tmpfile0
481 func_test_copy tmpfile0 tmpfile8
483 # Copy ACLs from a file that has no ACLs.
484 echo > tmpfile9
485 chmod a+x tmpfile9
486 getacl tmpfile9 > tmpaclout0
487 setacl -b -U tmpaclout0 tmpfile0
488 rm -f tmpfile9
490 func_test_copy tmpfile0 tmpfile9
494 nsk)
496 # Set an ACL for a user.
497 setacl -m user:$auid:1 tmpfile0
499 func_test_copy tmpfile0 tmpfile2
501 # Set an ACL for a group.
502 setacl -m group:$agid:4 tmpfile0
504 func_test_copy tmpfile0 tmpfile3
506 # Set an ACL for other.
507 setacl -m other:4 tmpfile0
509 func_test_copy tmpfile0 tmpfile4
511 # Remove the ACL for the user.
512 setacl -d user:$auid tmpfile0
514 func_test_copy tmpfile0 tmpfile5
516 # Remove the ACL for the group.
517 setacl -d group:$agid tmpfile0
519 func_test_copy tmpfile0 tmpfile6
521 # Delete all optional ACLs.
522 setacl -m user:$auid:1 tmpfile0
523 setacl -s user::6,group::0,class:7,other:0 tmpfile0
525 func_test_copy tmpfile0 tmpfile8
527 # Copy ACLs from a file that has no ACLs.
528 echo > tmpfile9
529 chmod a+x tmpfile9
530 getacl tmpfile9 > tmpaclout0
531 setacl -f tmpaclout0 tmpfile0
532 rm -f tmpfile9
534 func_test_copy tmpfile0 tmpfile9
538 aix)
540 # Set an ACL for a user.
541 { aclget tmpfile0 | sed -e 's/disabled$/enabled/'; echo " permit --x u:$auid"; } | aclput tmpfile0
543 func_test_copy tmpfile0 tmpfile2
545 # Set an ACL for a group.
546 { aclget tmpfile0 | sed -e 's/disabled$/enabled/'; echo " permit r-- g:$agid"; } | aclput tmpfile0
548 func_test_copy tmpfile0 tmpfile3
550 # Set an ACL for other.
551 chmod o+r tmpfile0
553 func_test_copy tmpfile0 tmpfile4
555 # Remove the ACL for the user.
556 aclget tmpfile0 | grep -v ' u:[^ ]*$' | aclput tmpfile0
558 func_test_copy tmpfile0 tmpfile5
560 # Remove the ACL for the group.
561 aclget tmpfile0 | grep -v ' g:[^ ]*$' | aclput tmpfile0
563 func_test_copy tmpfile0 tmpfile7
565 # Delete all optional ACLs.
566 aclget tmpfile0 | sed -e 's/enabled$/disabled/' | sed -e '/disabled$/q' | aclput tmpfile0
568 func_test_copy tmpfile0 tmpfile8
570 # Copy ACLs from a file that has no ACLs.
571 echo > tmpfile9
572 chmod a+x tmpfile9
573 aclget tmpfile9 | aclput tmpfile0
574 rm -f tmpfile9
576 func_test_copy tmpfile0 tmpfile9
580 macosx)
582 # Set an ACL for a user.
583 /bin/chmod +a "user:daemon allow execute" tmpfile0
585 func_test_copy tmpfile0 tmpfile2
587 # Set an ACL for a group.
588 /bin/chmod +a "group:daemon allow read" tmpfile0
590 func_test_copy tmpfile0 tmpfile3
592 # Set an ACL for other.
593 chmod o+r tmpfile0
595 func_test_copy tmpfile0 tmpfile4
597 # Remove the ACL for the user.
598 /bin/chmod -a "user:daemon allow execute" tmpfile0
600 func_test_copy tmpfile0 tmpfile5
602 # Remove the ACL for the group.
603 /bin/chmod -a "group:daemon allow read" tmpfile0
605 func_test_copy tmpfile0 tmpfile7
607 # Delete all optional ACLs.
608 /bin/chmod -N tmpfile0
610 func_test_copy tmpfile0 tmpfile8
612 # Copy ACLs from a file that has no ACLs.
613 echo > tmpfile9
614 chmod a+x tmpfile9
615 { /bin/ls -le tmpfile9 | sed -n -e 's/^ [0-9][0-9]*: //p'; echo; } | /bin/chmod -E tmpfile0
616 rm -f tmpfile9
618 func_test_copy tmpfile0 tmpfile9
622 irix)
624 # Set an ACL for a user.
625 /sbin/chacl user::rw-,group::---,other::---,user:$auid:--x tmpfile0
627 func_test_copy tmpfile0 tmpfile2
629 # Set an ACL for a group.
630 /sbin/chacl user::rw-,group::---,other::---,user:$auid:--x,group:$agid:r-- tmpfile0
632 func_test_copy tmpfile0 tmpfile3
634 # Set an ACL for other.
635 /sbin/chacl user::rw-,group::---,user:$auid:--x,group:$agid:r--,other::r-- tmpfile0
637 func_test_copy tmpfile0 tmpfile4
639 # Remove the ACL for the user.
640 /sbin/chacl user::rw-,group::---,group:$agid:r--,other::r-- tmpfile0
642 func_test_copy tmpfile0 tmpfile5
644 # Remove the ACL for the group.
645 /sbin/chacl user::rw-,group::---,other::r-- tmpfile0
647 func_test_copy tmpfile0 tmpfile7
651 esac
654 rm -f tmpfile[0-9] tmpaclout[0-2]
655 ) || exit 1
657 rm -rf "$tmp"
658 exit 0