~ubuntu-branches/ubuntu/quantal/gnutls26/quantal

« back to all changes in this revision

Viewing changes to lib/gl/tests/init.sh

  • Committer: Bazaar Package Importer
  • Author(s): Steve Langasek
  • Date: 2011-05-20 13:07:18 UTC
  • mfrom: (12.1.11 sid)
  • Revision ID: james.westby@ubuntu.com-20110520130718-db41dybbanzfvlji
Tags: 2.10.5-1ubuntu1
* Merge from Debian unstable, remaining changes:
  - Fix build failure with --no-add-needed.
  - Build for multiarch.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# source this file; set up for tests
 
2
 
 
3
# Copyright (C) 2009, 2010 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
# Using this file in a test
 
19
# =========================
 
20
#
 
21
# The typical skeleton of a test looks like this:
 
22
#
 
23
#   #!/bin/sh
 
24
#   . "${srcdir=.}/init.sh"; path_prepend_ .
 
25
#   Execute some commands.
 
26
#   Note that these commands are executed in a subdirectory, therefore you
 
27
#   need to prepend "../" to relative filenames in the build directory.
 
28
#   Note that the "path_prepend_ ." is useful only if the body of your
 
29
#   test invokes programs residing in the initial directory.
 
30
#   For example, if the programs you want to test are in src/, and this test
 
31
#   script is named tests/test-1, then you would use "path_prepend_ ../src",
 
32
#   or perhaps export PATH='$(abs_top_builddir)/src$(PATH_SEPARATOR)'"$$PATH"
 
33
#   to all tests via automake's TESTS_ENVIRONMENT.
 
34
#   Set the exit code 0 for success, 77 for skipped, or 1 or other for failure.
 
35
#   Use the skip_ and fail_ functions to print a diagnostic and then exit
 
36
#   with the corresponding exit code.
 
37
#   Exit $?
 
38
 
 
39
# Executing a test that uses this file
 
40
# ====================================
 
41
#
 
42
# Running a single test:
 
43
#   $ make check TESTS=test-foo.sh
 
44
#
 
45
# Running a single test, with verbose output:
 
46
#   $ make check TESTS=test-foo.sh VERBOSE=yes
 
47
#
 
48
# Running a single test, with single-stepping:
 
49
#   1. Go into a sub-shell:
 
50
#   $ bash
 
51
#   2. Set relevant environment variables from TESTS_ENVIRONMENT in the
 
52
#      Makefile:
 
53
#   $ export srcdir=../../tests # this is an example
 
54
#   3. Execute the commands from the test, copy&pasting them one by one:
 
55
#   $ . "$srcdir/init.sh"; path_prepend_ .
 
56
#   ...
 
57
#   4. Finally
 
58
#   $ exit
 
59
 
 
60
ME_=`expr "./$0" : '.*/\(.*\)$'`
 
61
 
 
62
# We use a trap below for cleanup.  This requires us to go through
 
63
# hoops to get the right exit status transported through the handler.
 
64
# So use `Exit STATUS' instead of `exit STATUS' inside of the tests.
 
65
# Turn off errexit here so that we don't trip the bug with OSF1/Tru64
 
66
# sh inside this function.
 
67
Exit () { set +e; (exit $1); exit $1; }
 
68
 
 
69
# Print warnings (e.g., about skipped and failed tests) to this file number.
 
70
# Override by defining to say, 9, in init.cfg, and putting say,
 
71
# "export ...ENVVAR_SETTINGS...; exec 9>&2; $(SHELL)" in the definition
 
72
# of TESTS_ENVIRONMENT in your tests/Makefile.am file.
 
73
# This is useful when using automake's parallel tests mode, to print
 
74
# the reason for skip/failure to console, rather than to the .log files.
 
75
: ${stderr_fileno_=2}
 
76
 
 
77
warn_() { echo "$@" 1>&$stderr_fileno_; }
 
78
fail_() { warn_ "$ME_: failed test: $@"; Exit 1; }
 
79
skip_() { warn_ "$ME_: skipped test: $@"; Exit 77; }
 
80
framework_failure_() { warn_ "$ME_: set-up failure: $@"; Exit 99; }
 
81
 
 
82
# We require $(...) support unconditionally.
 
83
# We require a few additional shell features only when $EXEEXT is nonempty,
 
84
# in order to support automatic $EXEEXT emulation:
 
85
# - hyphen-containing alias names
 
86
# - we prefer to use ${var#...} substitution, rather than having
 
87
#   to work around lack of support for that feature.
 
88
# The following code attempts to find a shell with support for these features.
 
89
# If the current shell passes the test, we're done.  Otherwise, test other
 
90
# shells until we find one that passes.  If one is found, re-exec it.
 
91
# If no acceptable shell is found, skip the current test.
 
92
#
 
93
# Use "9" to indicate success (rather than 0), in case some shell acts
 
94
# like Solaris 10's /bin/sh but exits successfully instead of with status 2.
 
95
 
 
96
gl_shell_test_script_='
 
97
test $(echo y) = y || exit 1
 
98
test -z "$EXEEXT" && exit 9
 
99
shopt -s expand_aliases
 
100
alias a-b="echo zoo"
 
101
v=abx
 
102
     test ${v%x} = ab \
 
103
  && test ${v#a} = bx \
 
104
  && test $(a-b) = zoo \
 
105
  && exit 9
 
106
'
 
107
 
 
108
if test "x$1" = "x--no-reexec"; then
 
109
  shift
 
110
else
 
111
  # 'eval'ing the above code makes Solaris 10's /bin/sh exit with $? set to 2.
 
112
  # It does not evaluate any of the code after the "unexpected" `('.  Thus,
 
113
  # we must run it in a subshell.
 
114
  ( eval "$gl_shell_test_script_" ) > /dev/null 2>&1
 
115
  if test $? = 9; then
 
116
    : # The current shell is adequate.  No re-exec required.
 
117
  else
 
118
    # Search for a shell that meets our requirements.
 
119
    for re_shell_ in "${CONFIG_SHELL:-no_shell}" /bin/sh bash dash zsh pdksh fail
 
120
    do
 
121
      test "$re_shell_" = no_shell && continue
 
122
      test "$re_shell_" = fail && skip_ failed to find an adequate shell
 
123
      "$re_shell_" -c "$gl_shell_test_script_" 2>/dev/null
 
124
      if test $? = 9; then
 
125
        # Found an acceptable shell.
 
126
        exec "$re_shell_" "$0" --no-reexec "$@"
 
127
        echo "$ME_: exec failed" 1>&2
 
128
        exit 127
 
129
      fi
 
130
    done
 
131
  fi
 
132
fi
 
133
 
 
134
test -n "$EXEEXT" && shopt -s expand_aliases
 
135
 
 
136
# Enable glibc's malloc-perturbing option.
 
137
# This is cheap and useful for exposing code that depends on the fact that
 
138
# malloc-related functions often return memory that is mostly zeroed.
 
139
# If you have the time and cycles, use valgrind to do an even better job.
 
140
: ${MALLOC_PERTURB_=87}
 
141
export MALLOC_PERTURB_
 
142
 
 
143
# This is a stub function that is run upon trap (upon regular exit and
 
144
# interrupt).  Override it with a per-test function, e.g., to unmount
 
145
# a partition, or to undo any other global state changes.
 
146
cleanup_() { :; }
 
147
 
 
148
if ( diff --version < /dev/null 2>&1 | grep GNU ) > /dev/null 2>&1; then
 
149
  compare() { diff -u "$@"; }
 
150
elif ( cmp --version < /dev/null 2>&1 | grep GNU ) > /dev/null 2>&1; then
 
151
  compare() { cmp -s "$@"; }
 
152
else
 
153
  compare() { cmp "$@"; }
 
154
fi
 
155
 
 
156
# An arbitrary prefix to help distinguish test directories.
 
157
testdir_prefix_() { printf gt; }
 
158
 
 
159
# Run the user-overridable cleanup_ function, remove the temporary
 
160
# directory and exit with the incoming value of $?.
 
161
remove_tmp_()
 
162
{
 
163
  __st=$?
 
164
  cleanup_
 
165
  # cd out of the directory we're about to remove
 
166
  cd "$initial_cwd_" || cd / || cd /tmp
 
167
  chmod -R u+rwx "$test_dir_"
 
168
  # If removal fails and exit status was to be 0, then change it to 1.
 
169
  rm -rf "$test_dir_" || { test $__st = 0 && __st=1; }
 
170
  exit $__st
 
171
}
 
172
 
 
173
# Given a directory name, DIR, if every entry in it that matches *.exe
 
174
# contains only the specified bytes (see the case stmt below), then print
 
175
# a space-separated list of those names and return 0.  Otherwise, don't
 
176
# print anything and return 1.  Naming constraints apply also to DIR.
 
177
find_exe_basenames_()
 
178
{
 
179
  feb_dir_=$1
 
180
  feb_fail_=0
 
181
  feb_result_=
 
182
  feb_sp_=
 
183
  for feb_file_ in $feb_dir_/*.exe; do
 
184
    case $feb_file_ in
 
185
      *[!-a-zA-Z/0-9_.+]*) feb_fail_=1; break;;
 
186
      *) # Remove leading file name components as well as the .exe suffix.
 
187
         feb_file_=${feb_file_##*/}
 
188
         feb_file_=${feb_file_%.exe}
 
189
         feb_result_="$feb_result_$feb_sp_$feb_file_";;
 
190
    esac
 
191
    feb_sp_=' '
 
192
  done
 
193
  test $feb_fail_ = 0 && printf %s "$feb_result_"
 
194
  return $feb_fail_
 
195
}
 
196
 
 
197
# Consider the files in directory, $1.
 
198
# For each file name of the form PROG.exe, create an alias named
 
199
# PROG that simply invokes PROG.exe, then return 0.  If any selected
 
200
# file name or the directory name, $1, contains an unexpected character,
 
201
# define no function and return 1.
 
202
create_exe_shims_()
 
203
{
 
204
  case $EXEEXT in
 
205
    '') return 0 ;;
 
206
    .exe) ;;
 
207
    *) echo "$0: unexpected \$EXEEXT value: $EXEEXT" 1>&2; return 1 ;;
 
208
  esac
 
209
 
 
210
  base_names_=`find_exe_basenames_ $1` \
 
211
    || { echo "$0 (exe_shim): skipping directory: $1" 1>&2; return 1; }
 
212
 
 
213
  if test -n "$base_names_"; then
 
214
    for base_ in $base_names_; do
 
215
      alias "$base_"="$base_$EXEEXT"
 
216
    done
 
217
  fi
 
218
 
 
219
  return 0
 
220
}
 
221
 
 
222
# Use this function to prepend to PATH an absolute name for each
 
223
# specified, possibly-$initial_cwd_-relative, directory.
 
224
path_prepend_()
 
225
{
 
226
  while test $# != 0; do
 
227
    path_dir_=$1
 
228
    case $path_dir_ in
 
229
      '') fail_ "invalid path dir: '$1'";;
 
230
      /*) abs_path_dir_=$path_dir_;;
 
231
      *) abs_path_dir_=`cd "$initial_cwd_/$path_dir_" && echo "$PWD"` \
 
232
           || fail_ "invalid path dir: $path_dir_";;
 
233
    esac
 
234
    case $abs_path_dir_ in
 
235
      *:*) fail_ "invalid path dir: '$abs_path_dir_'";;
 
236
    esac
 
237
    PATH="$abs_path_dir_:$PATH"
 
238
 
 
239
    # Create an alias, FOO, for each FOO.exe in this directory.
 
240
    create_exe_shims_ "$abs_path_dir_" \
 
241
      || fail_ "something failed (above): $abs_path_dir_"
 
242
    shift
 
243
  done
 
244
  export PATH
 
245
}
 
246
 
 
247
setup_()
 
248
{
 
249
  test "$VERBOSE" = yes && set -x
 
250
 
 
251
  initial_cwd_=$PWD
 
252
 
 
253
  pfx_=`testdir_prefix_`
 
254
  test_dir_=`mktempd_ "$initial_cwd_" "$pfx_-$ME_.XXXX"` \
 
255
    || fail_ "failed to create temporary directory in $initial_cwd_"
 
256
  cd "$test_dir_"
 
257
 
 
258
  # These trap statements ensure that the temporary directory, $test_dir_,
 
259
  # is removed upon exit as well as upon receipt of any of the listed signals.
 
260
  trap remove_tmp_ 0
 
261
  for sig_ in 1 2 3 13 15; do
 
262
    eval "trap 'Exit $(expr $sig_ + 128)' $sig_"
 
263
  done
 
264
}
 
265
 
 
266
# Create a temporary directory, much like mktemp -d does.
 
267
# Written by Jim Meyering.
 
268
#
 
269
# Usage: mktempd_ /tmp phoey.XXXXXXXXXX
 
270
#
 
271
# First, try to use the mktemp program.
 
272
# Failing that, we'll roll our own mktemp-like function:
 
273
#  - try to get random bytes from /dev/urandom
 
274
#  - failing that, generate output from a combination of quickly-varying
 
275
#      sources and gzip.  Ignore non-varying gzip header, and extract
 
276
#      "random" bits from there.
 
277
#  - given those bits, map to file-name bytes using tr, and try to create
 
278
#      the desired directory.
 
279
#  - make only $MAX_TRIES_ attempts
 
280
 
 
281
# Helper function.  Print $N pseudo-random bytes from a-zA-Z0-9.
 
282
rand_bytes_()
 
283
{
 
284
  n_=$1
 
285
 
 
286
  # Maybe try openssl rand -base64 $n_prime_|tr '+/=\012' abcd first?
 
287
  # But if they have openssl, they probably have mktemp, too.
 
288
 
 
289
  chars_=abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789
 
290
  dev_rand_=/dev/urandom
 
291
  if test -r "$dev_rand_"; then
 
292
    # Note: 256-length($chars_) == 194; 3 copies of $chars_ is 186 + 8 = 194.
 
293
    dd ibs=$n_ count=1 if=$dev_rand_ 2>/dev/null \
 
294
      | LC_ALL=C tr -c $chars_ 01234567$chars_$chars_$chars_
 
295
    return
 
296
  fi
 
297
 
 
298
  n_plus_50_=`expr $n_ + 50`
 
299
  cmds_='date; date +%N; free; who -a; w; ps auxww; ps ef; netstat -n'
 
300
  data_=` (eval "$cmds_") 2>&1 | gzip `
 
301
 
 
302
  # Ensure that $data_ has length at least 50+$n_
 
303
  while :; do
 
304
    len_=`echo "$data_"|wc -c`
 
305
    test $n_plus_50_ -le $len_ && break;
 
306
    data_=` (echo "$data_"; eval "$cmds_") 2>&1 | gzip `
 
307
  done
 
308
 
 
309
  echo "$data_" \
 
310
    | dd bs=1 skip=50 count=$n_ 2>/dev/null \
 
311
    | LC_ALL=C tr -c $chars_ 01234567$chars_$chars_$chars_
 
312
}
 
313
 
 
314
mktempd_()
 
315
{
 
316
  case $# in
 
317
  2);;
 
318
  *) fail_ "Usage: $ME DIR TEMPLATE";;
 
319
  esac
 
320
 
 
321
  destdir_=$1
 
322
  template_=$2
 
323
 
 
324
  MAX_TRIES_=4
 
325
 
 
326
  # Disallow any trailing slash on specified destdir:
 
327
  # it would subvert the post-mktemp "case"-based destdir test.
 
328
  case $destdir_ in
 
329
  /) ;;
 
330
  */) fail_ "invalid destination dir: remove trailing slash(es)";;
 
331
  esac
 
332
 
 
333
  case $template_ in
 
334
  *XXXX) ;;
 
335
  *) fail_ "invalid template: $template_ (must have a suffix of at least 4 X's)";;
 
336
  esac
 
337
 
 
338
  fail=0
 
339
 
 
340
  # First, try to use mktemp.
 
341
  d=`unset TMPDIR; mktemp -d -t -p "$destdir_" "$template_" 2>/dev/null` \
 
342
    || fail=1
 
343
 
 
344
  # The resulting name must be in the specified directory.
 
345
  case $d in "$destdir_"*);; *) fail=1;; esac
 
346
 
 
347
  # It must have created the directory.
 
348
  test -d "$d" || fail=1
 
349
 
 
350
  # It must have 0700 permissions.  Handle sticky "S" bits.
 
351
  perms=`ls -dgo "$d" 2>/dev/null|tr S -` || fail=1
 
352
  case $perms in drwx------*) ;; *) fail=1;; esac
 
353
 
 
354
  test $fail = 0 && {
 
355
    echo "$d"
 
356
    return
 
357
  }
 
358
 
 
359
  # If we reach this point, we'll have to create a directory manually.
 
360
 
 
361
  # Get a copy of the template without its suffix of X's.
 
362
  base_template_=`echo "$template_"|sed 's/XX*$//'`
 
363
 
 
364
  # Calculate how many X's we've just removed.
 
365
  template_length_=`echo "$template_" | wc -c`
 
366
  nx_=`echo "$base_template_" | wc -c`
 
367
  nx_=`expr $template_length_ - $nx_`
 
368
 
 
369
  err_=
 
370
  i_=1
 
371
  while :; do
 
372
    X_=`rand_bytes_ $nx_`
 
373
    candidate_dir_="$destdir_/$base_template_$X_"
 
374
    err_=`mkdir -m 0700 "$candidate_dir_" 2>&1` \
 
375
      && { echo "$candidate_dir_"; return; }
 
376
    test $MAX_TRIES_ -le $i_ && break;
 
377
    i_=`expr $i_ + 1`
 
378
  done
 
379
  fail_ "$err_"
 
380
}
 
381
 
 
382
# If you want to override the testdir_prefix_ function,
 
383
# or to add more utility functions, use this file.
 
384
test -f "$srcdir/init.cfg" \
 
385
  && . "$srcdir/init.cfg"
 
386
 
 
387
setup_ "$@"