Upload Tizen:Base source
[framework/base/util-linux-ng.git] / tests / functions.sh
1 #
2 # Copyright (C) 2007 Karel Zak <kzak@redhat.com>
3 #
4 # This file is part of util-linux-ng.
5 #
6 # This file is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 2 of the License, or
9 # (at your option) any later version.
10 #
11 # This file is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 # GNU General Public License for more details.
15 #
16
17
18 function ts_abspath {
19         cd $1
20         pwd
21 }
22
23 function ts_skip_subtest {
24         echo " IGNORE ($1)"
25 }
26
27 function ts_skip {
28         ts_skip_subtest "$1"
29         if [ -n "$2" -a -b "$2" ]; then
30                 ts_device_deinit "$2"
31         fi
32         exit 0
33 }
34
35 function ts_skip_nonroot {
36         if [ $UID -ne 0 ]; then
37                 ts_skip "not root permissions"
38         fi
39 }
40
41 function ts_failed_subtest {
42         if [ x"$1" == x"" ]; then
43                 echo " FAILED ($TS_NS)"
44         else
45                 echo " FAILED ($1)"
46         fi
47 }
48
49 function ts_failed {
50         ts_failed_subtest "$1"
51         exit 1
52 }
53
54 function ts_ok_subtest {
55         if [ x"$1" == x"" ]; then
56                 echo " OK"
57         else
58                 echo " OK ($1)"
59         fi
60 }
61
62 function ts_ok {
63         ts_ok_subtest "$1"
64         exit 0
65 }
66
67 function ts_log {
68         echo "$1" >> $TS_OUTPUT
69         [ "$TS_VERBOSE" == "yes" ] && echo "$1"
70 }
71
72 function ts_has_option {
73         NAME="$1"
74         ALL="$2"
75         echo -n $ALL | sed 's/ //g' | awk 'BEGIN { FS="="; RS="--" } /('$NAME'$|'$NAME'=)/ { print "yes" }'
76 }
77
78 function ts_init_env {
79         local mydir=$(ts_abspath $(dirname $0))
80
81         export LANG="en_US.UTF-8"
82
83         TS_TOPDIR=$(ts_abspath $mydir/../../)
84         TS_SCRIPT="$mydir/$(basename $0)"
85         TS_SUBDIR=$(dirname $TS_SCRIPT)
86         TS_TESTNAME=$(basename $TS_SCRIPT)
87         TS_COMPONENT=$(basename $TS_SUBDIR)
88
89         TS_NSUBTESTS=0
90         TS_NSUBFAILED=0
91
92         TS_NS="$TS_COMPONENT/$TS_TESTNAME"
93         TS_SELF="$TS_SUBDIR"
94
95         TS_OUTDIR="$TS_TOPDIR/output/$TS_COMPONENT"
96         TS_OUTPUT="$TS_OUTDIR/$TS_TESTNAME"
97         TS_DIFFDIR="$TS_TOPDIR/diff/$TS_COMPONENT"
98         TS_DIFF="$TS_DIFFDIR/$TS_TESTNAME"
99         TS_EXPECTED="$TS_TOPDIR/expected/$TS_NS"
100         TS_MOUNTPOINT="$TS_OUTDIR/${TS_TESTNAME}-mnt"
101
102         TS_VERBOSE=$(ts_has_option "verbose" "$*")
103         TS_HAS_VOLUMEID="no"
104
105         BLKID_FILE="$TS_OUTDIR/${TS_TESTNAME}.blkidtab"
106
107         [ -d "$TS_OUTDIR" ]  || mkdir -p "$TS_OUTDIR"
108         [ -d "$TS_DIFFDIR" ] || mkdir -p "$TS_DIFFDIR"
109
110         declare -a TS_SUID_PROGS
111         declare -a TS_SUID_USER
112         declare -a TS_SUID_GROUP
113
114         . $TS_TOPDIR/commands.sh
115
116         export BLKID_FILE
117
118         if [ -x $TS_CMD_MOUNT ]; then
119                 ldd $TS_CMD_MOUNT | grep -q 'libvolume_id' &> /dev/null
120                 [ "$?" == "0" ] && TS_HAS_VOLUMEID="yes"
121         fi
122
123         rm -f $TS_OUTPUT
124         touch $TS_OUTPUT
125
126         if [ "$TS_VERBOSE" == "yes" ]; then
127                 echo
128                 echo "     script: $TS_SCRIPT"
129                 echo "    sub dir: $TS_SUBDIR"
130                 echo "    top dir: $TS_TOPDIR"
131                 echo "       self: $TS_SELF"
132                 echo "  test name: $TS_TESTNAME"
133                 echo "  test desc: $TS_DESC"
134                 echo "  component: $TS_COMPONENT"
135                 echo "  namespace: $TS_NS"
136                 echo "    verbose: $TS_VERBOSE"
137                 echo "     output: $TS_OUTPUT"
138                 echo "   expected: $TS_EXPECTED"
139                 echo " mountpoint: $TS_MOUNTPOINT"
140                 echo
141         fi
142 }
143
144 function ts_init_subtest {
145
146         TS_SUBNAME="$1"
147
148         TS_OUTPUT="$TS_OUTDIR/$TS_TESTNAME-$TS_SUBNAME"
149         TS_DIFF="$TS_DIFFDIR/$TS_TESTNAME-$TS_SUBNAME"
150         TS_EXPECTED="$TS_TOPDIR/expected/$TS_NS-$TS_SUBNAME"
151         TS_MOUNTPOINT="$TS_OUTDIR/${TS_TESTNAME-$TS_SUBNAME}-mnt"
152
153         [ $TS_NSUBTESTS -eq 0 ] && echo
154         TS_NSUBTESTS=$(( $TS_NSUBTESTS + 1 ))
155
156         printf "%16s: %-27s ..." "" "$TS_SUBNAME"
157 }
158
159 function ts_init {
160         local is_fake=$( ts_has_option "fake" "$*")
161
162         ts_init_env "$*"
163
164         printf "%13s: %-30s ..." "$TS_COMPONENT" "$TS_DESC"
165
166         [ "$is_fake" == "yes" ] && ts_skip "fake mode"
167 }
168
169 function ts_init_suid {
170         PROG="$1"
171         ct=${#TS_SUID_PROGS[*]}
172
173         # Save info about original setting
174         TS_SUID_PROGS[$ct]=$PROG
175         TS_SUID_USER[$ct]=$(stat --printf="%U" $PROG)
176         TS_SUID_GROUP[$ct]=$(stat --printf="%G" $PROG)
177
178         chown root.root $PROG &> /dev/null
179         chmod u+s $PROG &> /dev/null
180 }
181
182 function ts_gen_diff {
183         local res=0
184
185         if [ -s $TS_OUTPUT ]; then
186                 diff -u $TS_EXPECTED $TS_OUTPUT > $TS_DIFF
187                 [ -s $TS_DIFF ] && res=1
188         else
189                 res=1
190         fi
191         return $res
192 }
193
194 function ts_finalize_subtest {
195         local res=0
196
197         if [ -s $TS_EXPECTED ]; then
198                 ts_gen_diff
199                 if [ $? -eq 1 ]; then
200                         ts_failed_subtest "$1"
201                         res=1
202                 else
203                         ts_ok_subtest "$1"
204                 fi
205         else
206                 ts_skip_subtest "output undefined"
207         fi
208
209         [ $res -ne 0 ] && TS_NSUBFAILED=$(( $TS_NSUBFAILED + 1 ))
210         return $res
211 }
212
213 function ts_finalize {
214         for idx in $(seq 0 $((${#TS_SUID_PROGS[*]} - 1))); do
215                 PROG=${TS_SUID_PROGS[$idx]}
216                 chmod a-s $PROG &> /dev/null
217                 chown ${TS_SUID_USER[$idx]}.${TS_SUID_GROUP[$idx]} $PROG &> /dev/null
218         done
219
220         if [ $TS_NSUBTESTS -ne 0 ]; then
221                 printf "%11s..."
222                 if [ $TS_NSUBFAILED -ne 0 ]; then
223                         ts_failed "$TS_NSUBFAILED from $TS_NSUBTESTS sub-tests"
224                 else
225                         ts_ok "all $TS_NSUBTESTS sub-tests PASSED"
226                 fi
227         fi
228
229         if [ -s $TS_EXPECTED ]; then
230                 ts_gen_diff
231                 if [ $? -eq 1 ]; then
232                         ts_failed "$1"
233                 fi
234                 ts_ok "$1"
235         fi
236
237         ts_skip "output undefined"
238 }
239
240 function ts_die {
241         ts_log "$1"
242         if [ -n "$2" ] && [ -b "$2" ]; then
243                 ts_device_deinit "$2"
244                 ts_fstab_clean          # for sure... 
245         fi
246         ts_finalize
247 }
248
249 function ts_image_md5sum {
250         local img=${1:-"$TS_OUTDIR/${TS_TESTNAME}.img"}
251         echo $(md5sum "$img" | awk '{printf $1}') $(basename "$img")
252 }
253
254 function ts_image_init {
255         local mib=${1:-"5"}     # size in MiBs
256         local img=${2:-"$TS_OUTDIR/${TS_TESTNAME}.img"}
257         
258         dd if=/dev/zero of="$img" bs=1M count=$mib &> /dev/null
259         echo "$img"
260         return 0
261 }
262
263 function ts_device_init {
264         local img=$(ts_image_init)
265         local dev=$($TS_CMD_LOSETUP -s -f "$img")
266
267         if [ -z "$dev" ]; then
268                 ts_device_deinit $dev
269                 return 1                # error
270         fi
271
272         echo $dev
273         return 0                        # succes
274 }
275
276 function ts_device_deinit {
277         local DEV="$1"
278
279         if [ -b "$DEV" ]; then
280                 $TS_CMD_UMOUNT "$DEV" &> /dev/null
281                 $TS_CMD_LOSETUP -d "$DEV" &> /dev/null
282         fi
283 }
284
285 function ts_udev_dev_support {
286         if [ "$TS_HAS_VOLUMEID" == "yes" ] && [ ! -L "/dev/disk/$1/$2" ]; then
287                 return 1
288         fi
289         return 0
290 }
291
292 function ts_uuid_by_devname {
293         local DEV="$1"
294         local UUID=""
295         if [ -x "$TS_ECMD_BLKID" ]; then
296                 UUID=$($TS_ECMD_BLKID -c /dev/null -w /dev/null -s "UUID" $DEV | sed 's/.*UUID="//g; s/"//g')
297         elif [ -x "$TS_ECMD_VOLID" ]; then
298                 UUID=$($TS_ECMD_VOLID -u $DEV)
299         fi
300         echo $UUID
301 }
302
303 function ts_label_by_devname {
304         local DEV="$1"
305         local TYPE=""
306         if [ -x "$TS_ECMD_BLKID" ]; then
307                 LABEL=$($TS_ECMD_BLKID -c /dev/null -w /dev/null -s "LABEL" $DEV | sed 's/.*LABEL="//g; s/"//g')
308         elif [ -x "$TS_ECMD_VOLID" ]; then
309                 LABEL=$($TS_ECMD_VOLID -l $DEV)
310         fi
311         echo $LABEL
312 }
313
314 function ts_fstype_by_devname {
315         local DEV="$1"
316         local TYPE=""
317         if [ -x "$TS_ECMD_BLKID" ]; then
318                 TYPE=$($TS_ECMD_BLKID -c /dev/null -w /dev/null -s "TYPE" $DEV | sed 's/.*TYPE="//g; s/"//g')
319         elif [ -x "$TS_ECMD_VOLID" ]; then
320                 TYPE=$($TS_ECMD_VOLID -t $DEV)
321         fi
322         echo $TYPE
323 }
324
325 function ts_device_has {
326         local TAG="$1"
327         local VAL="$2"
328         local DEV="$3"
329         local vl=""
330
331         case $TAG in
332                 "TYPE") vl=$(ts_fstype_by_devname $DEV);;
333                 "LABEL") vl=$(ts_label_by_devname $DEV);;
334                 "UUID") vl=$(ts_uuid_by_devname $DEV);;
335                 *) return 1;;
336         esac
337
338         if [ "$vl" == "$VAL" ]; then
339                 return 0
340         fi
341         return 1
342 }
343
344 function ts_device_has_uuid {
345         ts_uuid_by_devname "$1" | egrep -q '^[0-9a-z]{8}-[0-9a-z]{4}-[0-9a-z]{4}-[0-9a-z]{4}-[0-9a-z]{12}$'
346         return $?
347 }
348
349 function ts_is_mounted {
350         local DEV=$1
351
352         grep -q $DEV /proc/mounts && return 0
353
354         if [ "${DEV#/dev/loop/}" != "$DEV" ]; then
355                 return grep -q "/dev/loop${DEV#/dev/loop/}" /proc/mounts
356         fi
357         return 1
358 }
359
360 function ts_swapoff {
361         local DEV="$1"
362
363         # swapoff doesn't exist in build tree
364         if [ ! -x "$TS_CMD_SWAPOFF" ]; then
365                 ln -sf $TS_CMD_SWAPON $TS_CMD_SWAPOFF
366                 REMSWAPOFF="true"
367         fi
368         $TS_CMD_SWAPOFF $DEV 2>&1 >> $TS_OUTPUT
369         if [ -n "$REMSWAPOFF" ]; then
370                 rm -f $TS_CMD_SWAPOFF
371         fi
372 }
373
374 function ts_fstab_open {
375         echo "# <!-- util-linux-ng test entry" >> /etc/fstab
376 }
377
378 function ts_fstab_close {
379         echo "# -->" >> /etc/fstab
380 }
381
382 function ts_fstab_addline {
383         local SPEC="$1"
384         local MNT=${2:-"$TS_MOUNTPOINT"}
385         local FS=${3:-"auto"}
386         local OPT=${4:-"defaults"}
387
388         echo "$SPEC   $MNT   $FS   $OPT   0   0" >> /etc/fstab
389 }
390
391 function ts_fstab_add {
392         ts_fstab_open
393         ts_fstab_addline $*
394         ts_fstab_close
395 }
396
397 function ts_fstab_clean {
398         sed --in-place "
399 /# <!-- util-linux-ng/!b
400 :a
401 /# -->/!{
402   N
403   ba
404 }
405 s/# <!-- util-linux-ng.*-->//;
406 /^$/d" /etc/fstab
407 }
408