OSDN Git Service

maint: remove now-unused fat-related functions
[android-x86/external-parted.git] / tests / t-lib-helpers.sh
1 # Functions sourced via testing framework.
2
3 getlimits_()
4 {
5     eval $(getlimits)
6     test "$INT_MAX" ||
7     error_ "Error running getlimits"
8 }
9
10 require_acl_()
11 {
12   getfacl --version < /dev/null > /dev/null 2>&1 \
13     && setfacl --version < /dev/null > /dev/null 2>&1 \
14       || skip_ "This test requires getfacl and setfacl."
15
16   id -u bin > /dev/null 2>&1 \
17     || skip_ "This test requires a local user named bin."
18 }
19
20 require_hfs_()
21 {
22   mkfs.hfs 2>&1 | grep '^usage:' \
23     || skip_ "This test requires HFS support."
24 }
25
26 # Skip this test if we're not in SELinux "enforcing" mode.
27 require_selinux_enforcing_()
28 {
29   test "$(getenforce)" = Enforcing \
30     || skip_ "This test is useful only with SELinux in Enforcing mode."
31 }
32
33
34 require_openat_support_()
35 {
36   # Skip this test if your system has neither the openat-style functions
37   # nor /proc/self/fd support with which to emulate them.
38   test -z "$CONFIG_HEADER" \
39     && skip_ 'internal error: CONFIG_HEADER not defined'
40
41   _skip=yes
42   grep '^#define HAVE_OPENAT' "$CONFIG_HEADER" > /dev/null && _skip=no
43   test -d /proc/self/fd && _skip=no
44   if test $_skip = yes; then
45     skip_ 'this system lacks openat support'
46   fi
47 }
48
49 require_ulimit_()
50 {
51   ulimit_works=yes
52   # Expect to be able to exec a program in 10MB of virtual memory,
53   # but not in 20KB.  I chose "date".  It must not be a shell built-in
54   # function, so you can't use echo, printf, true, etc.
55   # Of course, in coreutils, I could use $top_builddir/src/true,
56   # but this should be able to work for other projects, too.
57   ( ulimit -v 10000; date ) > /dev/null 2>&1 || ulimit_works=no
58   ( ulimit -v 20;    date ) > /dev/null 2>&1 && ulimit_works=no
59
60   test $ulimit_works = no \
61     && skip_ "this shell lacks ulimit support"
62 }
63
64 require_readable_root_()
65 {
66   test -r / || skip_ "/ is not readable"
67 }
68
69 # Skip the current test if strace is not available or doesn't work
70 # with the named syscall.  Usage: require_strace_ unlink
71 require_strace_()
72 {
73   test $# = 1 || framework_failure
74
75   strace -V < /dev/null > /dev/null 2>&1 ||
76     skip_ 'no strace program'
77
78   strace -qe "$1" echo > /dev/null 2>&1 ||
79     skip_ 'strace -qe "'"$1"'" does not work'
80 }
81
82 # Require a controlling input `terminal'.
83 require_controlling_input_terminal_()
84 {
85   tty -s || have_input_tty=no
86   test -t 0 || have_input_tty=no
87   if test "$have_input_tty" = no; then
88     skip_ 'requires controlling input terminal
89 This test must have a controlling input "terminal", so it may not be
90 run via "batch", "at", or "ssh".  On some systems, it may not even be
91 run in the background.'
92   fi
93 }
94
95 uid_is_privileged_()
96 {
97   # Make sure id -u succeeds.
98   my_uid=$(id -u) \
99     || { echo "$0: cannot run \`id -u'" 1>&2; return 1; }
100
101   # Make sure it gives valid output.
102   case $my_uid in
103     0) ;;
104     *[!0-9]*)
105       echo "$0: invalid output (\`$my_uid') from \`id -u'" 1>&2
106       return 1 ;;
107     *) return 1 ;;
108   esac
109 }
110
111 get_process_status_()
112 {
113   sed -n '/^State:[      ]*\([[:alpha:]]\).*/s//\1/p' /proc/$1/status
114 }
115
116 # Convert an ls-style permission string, like drwxr----x and -rw-r-x-wx
117 # to the equivalent chmod --mode (-m) argument, (=,u=rwx,g=r,o=x and
118 # =,u=rw,g=rx,o=wx).  Ignore ACLs.
119 rwx_to_mode_()
120 {
121   case $# in
122     1) rwx=$1;;
123     *) echo "$0: wrong number of arguments" 1>&2
124       echo "Usage: $0 ls-style-mode-string" 1>&2
125       return;;
126   esac
127
128   case $rwx in
129     [ld-][rwx-][rwx-][rwxsS-][rwx-][rwx-][rwxsS-][rwx-][rwx-][rwxtT-]) ;;
130     [ld-][rwx-][rwx-][rwxsS-][rwx-][rwx-][rwxsS-][rwx-][rwx-][rwxtT-][+.]) ;;
131     *) echo "$0: invalid mode string: $rwx" 1>&2; return;;
132   esac
133
134   # Perform these conversions:
135   # S  s
136   # s  xs
137   # T  t
138   # t  xt
139   # The `T' and `t' ones are only valid for `other'.
140   s='s/S/@/;s/s/x@/;s/@/s/'
141   t='s/T/@/;s/t/x@/;s/@/t/'
142
143   u=`echo $rwx|sed 's/^.\(...\).*/,u=\1/;s/-//g;s/^,u=$//;'$s`
144   g=`echo $rwx|sed 's/^....\(...\).*/,g=\1/;s/-//g;s/^,g=$//;'$s`
145   o=`echo $rwx|sed 's/^.......\(...\).*/,o=\1/;s/-//g;s/^,o=$//;'$s';'$t`
146   echo "=$u$g$o"
147 }
148
149 require_selinux_()
150 {
151   case `ls -Zd .` in
152     '? .'|'unlabeled .')
153       skip_ "this system (or maybe just" \
154         "the current file system) lacks SELinux support"
155     ;;
156   esac
157 }
158
159 very_expensive_()
160 {
161   if test "$RUN_VERY_EXPENSIVE_TESTS" != yes; then
162     skip_ 'very expensive: disabled by default
163 This test is very expensive, so it is disabled by default.
164 To run it anyway, rerun make check with the RUN_VERY_EXPENSIVE_TESTS
165 environment variable set to yes.  E.g.,
166
167   env RUN_VERY_EXPENSIVE_TESTS=yes make check
168 '
169   fi
170 }
171
172 expensive_()
173 {
174   if test "$RUN_EXPENSIVE_TESTS" != yes; then
175     skip_ 'expensive: disabled by default
176 This test is relatively expensive, so it is disabled by default.
177 To run it anyway, rerun make check with the RUN_EXPENSIVE_TESTS
178 environment variable set to yes.  E.g.,
179
180   env RUN_EXPENSIVE_TESTS=yes make check
181 '
182   fi
183 }
184
185 require_root_()
186 {
187   uid_is_privileged_ || skip_ "must be run as root"
188   NON_ROOT_USERNAME=${NON_ROOT_USERNAME=nobody}
189   NON_ROOT_GROUP=${NON_ROOT_GROUP=$(id -g $NON_ROOT_USERNAME)}
190 }
191
192 skip_if_root_() { uid_is_privileged_ && skip_ "must be run as non-root"; }
193 error_() { echo "$0: $@" 1>&2; Exit 1; }
194 framework_failure() { error_ 'failure in testing framework'; }
195
196 # Set `groups' to a space-separated list of at least two groups
197 # of which the user is a member.
198 require_membership_in_two_groups_()
199 {
200   test $# = 0 || framework_failure
201
202   groups=${COREUTILS_GROUPS-`(id -G || /usr/xpg4/bin/id -G) 2>/dev/null`}
203   case "$groups" in
204     *' '*) ;;
205     *) skip_ 'requires membership in two groups
206 this test requires that you be a member of more than one group,
207 but running `id -G'\'' either failed or found just one.  If you really
208 are a member of at least two groups, then rerun this test with
209 COREUTILS_GROUPS set in your environment to the space-separated list
210 of group names or numbers.  E.g.,
211
212   env COREUTILS_GROUPS='users cdrom' make check
213
214 '
215      ;;
216   esac
217 }
218
219 # Is /proc/$PID/status supported?
220 require_proc_pid_status_()
221 {
222     sleep 2 &
223     local pid=$!
224     sleep .5
225     grep '^State:[       ]*[S]' /proc/$pid/status > /dev/null 2>&1 ||
226     skip_ "/proc/$pid/status: missing or 'different'"
227     kill $pid
228 }
229
230 # Does the current (working-dir) file system support sparse files?
231 require_sparse_support_()
232 {
233   test $# = 0 || framework_failure
234   # Test whether we can create a sparse file.
235   # For example, on Darwin6.5 with a file system of type hfs, it's not possible.
236   # NTFS requires 128K before a hole appears in a sparse file.
237   t=sparse.$$
238   dd bs=1 seek=128K of=$t < /dev/null 2> /dev/null
239   set x `du -sk $t`
240   kb_size=$2
241   rm -f $t
242   if test $kb_size -ge 128; then
243     skip_ 'this file system does not support sparse files'
244   fi
245 }
246
247 mkfifo_or_skip_()
248 {
249   test $# = 1 || framework_failure
250   if ! mkfifo "$1"; then
251     # Make an exception of this case -- usually we interpret framework-creation
252     # failure as a test failure.  However, in this case, when running on a SunOS
253     # system using a disk NFS mounted from OpenBSD, the above fails like this:
254     # mkfifo: cannot make fifo `fifo-10558': Not owner
255     skip_ 'NOTICE: unable to create test prerequisites'
256   fi
257 }
258
259 # Disable the current test if the working directory seems to have
260 # the setgid bit set.
261 skip_if_setgid_()
262 {
263   setgid_tmpdir=setgid-$$
264   (umask 77; mkdir $setgid_tmpdir)
265   perms=$(stat --printf %A $setgid_tmpdir)
266   rmdir $setgid_tmpdir
267   case $perms in
268     drwx------);;
269     drwxr-xr-x);;  # Windows98 + DJGPP 2.03
270     *) skip_ 'this directory has the setgid bit set';;
271   esac
272 }
273
274 skip_if_mcstransd_is_running_()
275 {
276   test $# = 0 || framework_failure
277
278   # When mcstransd is running, you'll see only the 3-component
279   # version of file-system context strings.  Detect that,
280   # and if it's running, skip this test.
281   __ctx=$(stat --printf='%C\n' .) || framework_failure
282   case $__ctx in
283     *:*:*:*) ;; # four components is ok
284     *) # anything else probably means mcstransd is running
285         skip_ "unexpected context '$__ctx'; turn off mcstransd" ;;
286   esac
287 }
288
289 # Skip the current test if umask doesn't work as usual.
290 # This test should be run in the temporary directory that ends
291 # up being removed via the trap commands.
292 working_umask_or_skip_()
293 {
294   umask 022
295   touch file1 file2
296   chmod 644 file2
297   perms=`ls -l file1 file2 | sed 's/ .*//' | uniq`
298   rm -f file1 file2
299
300   case $perms in
301   *'
302   '*) skip_ 'your build directory has unusual umask semantics'
303   esac
304 }
305
306 emit_superuser_warning()
307 {
308   uid=`id -u` || uid=1
309   test "$uid" != 0 &&
310     echo 'WARNING: You are not superuser.  Watch out for permissions.' || :
311 }
312
313 require_mdadm_()
314 {
315   mdadm --version || skip_ "find mdadm executable"
316 }
317
318 # Will look for an md number that is not in use and create a md device with
319 # that number.  If the system has more than 9 md devices, it will fail.
320 mdadm_create_linear_device_()
321 {
322   lo_dev=$1
323   mdd=$G_dev_/md0
324   for i in 0 1 2 3 4 5 6 7 8 9 ; do
325     mdd=$G_dev_/md$i
326     mdadm  --create --force $mdd --level=linear --raid-devices=1 $lo_dev \
327         > /dev/null 2>&1 \
328       && break
329
330     if [ $i -eq 9 ]; then echo $mdd ; return 1 ; fi
331   done
332
333   echo $mdd
334   return 0
335 }
336
337 # Often, when parted cannot use the specified size or start/endpoints
338 # of a partition, it outputs a warning or error like this:
339 #
340 # Error: You requested a partition from 512B to 50.7kB (...).
341 # The closest location we can manage is 17.4kB to 33.8kB (...).
342 #
343 # But those numbers depend on sector size, so
344 # replace the specific values with place-holders,
345 # so tests do not depend on sector size.
346 normalize_part_diag_()
347 {
348   local file=$1
349   sed 's/ [0-9.k]*B to [0-9.k]*B (sectors .*$/ X to Y./' $file > $file.t \
350     && mv $file.t $file && return 0
351   return 1
352 }
353
354 require_xfs_()
355 {
356   mkfs.xfs -V || skip_ "this test requires XFS support"
357 }
358
359 require_dvhtool_()
360 {
361   dvhtool --help \
362     || skip_ 'dvhtool is required for this test'
363 }
364
365 # Helper function: wait 2s (via .1s increments) for FILE to appear.
366 # Usage: wait_for_dev_to_appear_ /dev/sdg
367 # Return 0 upon success, 1 upon failure.
368 wait_for_dev_to_appear_()
369 {
370   local file=$1
371   local i=0
372   local incr=1
373   while :; do
374     ls "$file" > /dev/null 2>&1 && return 0
375     sleep .1 2>/dev/null || { sleep 1; incr=10; }
376     i=$(expr $i + $incr); test $i = 20 && break
377   done
378   return 1
379 }
380
381 # Like the above, but don't hard-code the max timeout.
382 wait_for_dev_to_disappear_()
383 {
384   local file=$1
385   local n_sec=$2
386   local i=0
387   local incr=1
388   while :; do
389     ls "$file" > /dev/null 2>&1 || return 0
390     sleep .1 2>/dev/null || { sleep 1; incr=10; }
391     i=$(expr $i + $incr); test $i -ge $(expr $n_sec \* 10) && break
392   done
393   return 1
394 }
395
396 device_mapper_required_()
397 {
398   . "$abs_top_srcdir/tests/lvm-utils.sh" \
399        || fail_ "device mapper setup failed"
400 }