db2fa76c991bc7073369ee8aadc86cf6b12e5e93
[gnulib.git] / tests / init.sh
1 # source this file; set up for tests
2
3 # Copyright (C) 2009 Free Software Foundation, Inc.
4
5 # This program is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation, either version 3 of the License, or
8 # (at your option) any later version.
9
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 # GNU General Public License for more details.
14
15 # You should have received a copy of the GNU General Public License
16 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
18 # We use a trap below for cleanup.  This requires us to go through
19 # hoops to get the right exit status transported through the handler.
20 # So use `Exit STATUS' instead of `exit STATUS' inside of the tests.
21 # Turn off errexit here so that we don't trip the bug with OSF1/Tru64
22 # sh inside this function.
23 Exit () { set +e; (exit $1); exit $1; }
24
25 fail_() { echo "$ME_: failed test: $@" 1>&2; Exit 1; }
26 skip_() { echo "$ME_: skipped test: $@" 1>&2; Exit 77; }
27
28 # This is a stub function that is run upon trap (upon regular exit and
29 # interrupt).  Override it with a per-test function, e.g., to unmount
30 # a partition, or to undo any other global state changes.
31 cleanup_() { :; }
32
33 if ( diff --version < /dev/null 2>&1 | grep GNU ) 2>&1 > /dev/null; then
34   compare() { diff -u "$@"; }
35 elif ( cmp --version < /dev/null 2>&1 | grep GNU ) 2>&1 > /dev/null; then
36   compare() { cmp -s "$@"; }
37 else
38   compare() { cmp "$@"; }
39 fi
40
41 # An arbitrary prefix to help distinguish test directories.
42 testdir_prefix_() { printf gt; }
43
44 # Run the user-overridable cleanup_ function, remove the temporary
45 # directory and exit with the incoming value of $?.
46 remove_tmp_()
47 {
48   __st=$?
49   cleanup_
50   # cd out of the directory we're about to remove
51   cd "$initial_cwd_" || cd / || cd /tmp
52   chmod -R u+rwx "$test_dir_"
53   # If removal fails and exit status was to be 0, then change it to 1.
54   rm -rf "$test_dir_" || { test $__st = 0 && __st=1; }
55   exit $__st
56 }
57
58 setup_()
59 {
60   test "$VERBOSE" = yes && set -x
61
62   # Honor one or more --set-path=. options (i.e., or --set-path=../src).
63   # Use this option to prepend to PATH an absolute name for the
64   # specified, possibly-relative, directory.
65   while test $# != 0; do
66     case $1 in
67       --set-path=*)
68         path_dir_=${1#--set-path=}
69         test -z "$path_dir_" && fail_ "missing argument to --set-path="
70         abs_path_dir_=`cd "$path_dir_" && echo "$PWD" || exit 1` \
71             || fail_ "invalid path dir: $path_dir"
72         PATH="$abs_path_dir_:$PATH"
73         export PATH
74         shift
75         ;;
76       *) fail_ "unrecognized option: $1"
77         ;;
78     esac
79   done
80
81   initial_cwd_=$PWD
82   ME_=`expr "./$0" : '.*/\(.*\)$'`
83
84   pfx_=`testdir_prefix_`
85   test_dir_=`mktempd_ "$initial_cwd_" "$pfx_-$ME_.XXXX"` \
86     || fail_ "failed to create temporary directory in $initial_cwd_"
87   cd "$test_dir_"
88
89   # This pair of trap statements ensures that the temporary directory,
90   # $test_dir_, is removed upon exit as well as upon catchable signal.
91   trap remove_tmp_ 0
92   trap 'Exit $?' 1 2 13 15
93 }
94
95 # Create a temporary directory, much like mktemp -d does.
96 # Written by Jim Meyering.
97 #
98 # Usage: mktempd_ /tmp phoey.XXXXXXXXXX
99 #
100 # First, try to use the mktemp program.
101 # Failing that, we'll roll our own mktemp-like function:
102 #  - try to get random bytes from /dev/urandom
103 #  - failing that, generate output from a combination of quickly-varying
104 #      sources and gzip.  Ignore non-varying gzip header, and extract
105 #      "random" bits from there.
106 #  - given those bits, map to file-name bytes using tr, and try to create
107 #      the desired directory.
108 #  - make only $MAX_TRIES_ attempts
109
110 # Helper function.  Print $N pseudo-random bytes from a-zA-Z0-9.
111 rand_bytes_()
112 {
113   n_=$1
114
115   # Maybe try openssl rand -base64 $n_prime_|tr '+/=\012' abcd first?
116   # But if they have openssl, they probably have mktemp, too.
117
118   chars_=abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789
119   dev_rand_=/dev/urandom
120   if test -r "$dev_rand_"; then
121     # Note: 256-length($chars_) == 194; 3 copies of $chars_ is 186 + 8 = 194.
122     dd ibs=$n_ count=1 if=$dev_rand_ 2>/dev/null \
123       | tr -c $chars_ 01234567$chars_$chars_$chars_
124     return
125   fi
126
127   n_plus_50_=`expr $n_ + 50`
128   cmds_='date; date +%N; free; who -a; w; ps auxww; ps ef; netstat -n'
129   data_=` (eval "$cmds_") 2>&1 | gzip `
130
131   # Ensure that $data_ has length at least 50+$n_
132   while :; do
133     len_=`echo "$data_"|wc -c`
134     test $n_plus_50_ -le $len_ && break;
135     data_=` (echo "$data_"; eval "$cmds_") 2>&1 | gzip `
136   done
137
138   echo "$data_" \
139     | dd bs=1 skip=50 count=$n_ 2>/dev/null \
140     | tr -c $chars_ 01234567$chars_$chars_$chars_
141 }
142
143 mktempd_()
144 {
145   case $# in
146   2);;
147   *) fail_ "Usage: $ME DIR TEMPLATE";;
148   esac
149
150   destdir_=$1
151   template_=$2
152
153   MAX_TRIES_=4
154
155   # Disallow any trailing slash on specified destdir:
156   # it would subvert the post-mktemp "case"-based destdir test.
157   case $destdir_ in
158   /) ;;
159   */) fail_ "invalid destination dir: remove trailing slash(es)";;
160   esac
161
162   case $template_ in
163   *XXXX) ;;
164   *) fail_ "invalid template: $template_ (must have a suffix of at least 4 X's)";;
165   esac
166
167   fail=0
168
169   # First, try to use mktemp.
170   d=`env -u TMPDIR mktemp -d -t -p "$destdir_" "$template_" 2>/dev/null` \
171     || fail=1
172
173   # The resulting name must be in the specified directory.
174   case $d in "$destdir_"*);; *) fail=1;; esac
175
176   # It must have created the directory.
177   test -d "$d" || fail=1
178
179   # It must have 0700 permissions.  Handle sticky "S" bits.
180   perms=`ls -dgo "$d" 2>/dev/null|tr S -` || fail=1
181   case $perms in drwx------*) ;; *) fail=1;; esac
182
183   test $fail = 0 && {
184     echo "$d"
185     return
186   }
187
188   # If we reach this point, we'll have to create a directory manually.
189
190   # Get a copy of the template without its suffix of X's.
191   base_template_=`echo "$template_"|sed 's/XX*$//'`
192
193   # Calculate how many X's we've just removed.
194   template_length_=`echo "$template_" | wc -c`
195   nx_=`echo "$base_template_" | wc -c`
196   nx_=`expr $template_length_ - $nx_`
197
198   err_=
199   i_=1
200   while :; do
201     X_=`rand_bytes_ $nx_`
202     candidate_dir_="$destdir_/$base_template_$X_"
203     err_=`mkdir -m 0700 "$candidate_dir_" 2>&1` \
204       && { echo "$candidate_dir_"; return; }
205     test $MAX_TRIES_ -le $i_ && break;
206     i_=`expr $i_ + 1`
207   done
208   fail_ "$err_"
209 }
210
211 # If you want to override the testdir_prefix_ function,
212 # or to add more utility functions, use this file.
213 test -f "$srcdir/init.cfg" \
214   && . "$srcdir/init.cfg"
215
216 setup_ "$@"