0bedbecff9fafd39b9484214d20abe6d0bd25ba4
[platform/upstream/dosfstools.git] / tests / test-mkfs
1 #!/bin/sh
2 # Copyright (C) 2016  Andreas Bombe <aeb@debian.org>
3 #
4 # This program is free software: you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License as published by
6 # the Free Software Foundation, either version 3 of the License, or
7 # (at your option) any later version.
8 #
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 # GNU General Public License for more details.
13 #
14 # You should have received a copy of the GNU General Public License
15 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
16 #
17 #
18 # This script expects a testname.mkfs file as its sole argument. It must
19 # be a shell snippet setting the variables ARGS, SIZE and CMP_LIMIT. ARGS and
20 # SIZE are used in the mkfs invocation and the result is compared against a
21 # reference image generated from the hexdump in testname.xxd. The comparison
22 # is stopped after CMP_LIMIT since freshly build FAT filesystems are just
23 # zeros after the initial info sectors, FATs and root directory.
24
25
26 run_mkfs () {
27         "../src/mkfs.fat" "$@"
28 }
29
30 run_fsck () {
31         "../src/fsck.fat" "$@"
32 }
33
34
35 if [ $# -ne 1 ]; then
36         echo "$0 called with wrong number of arguments"
37         exit 99
38 fi
39 testname=$(basename "$1" .mkfs)
40
41
42 if [ "$XXD_FOUND" != "yes" ]; then
43         echo "xxd not available, required by test"
44         exit 77  # report test skipped
45 fi
46
47
48 . "$1" || exit 99
49 echo "Test $testname"
50
51 # make sure there aren't files remaining from earlier run
52 rm -f "${testname}.out" "${testname}.refimg"
53
54 xxd -r "${srcdir}/${testname}.xxd" "${testname}.refimg" || exit 99
55 run_mkfs -C -v --invariant $ARGS "${testname}.out" $SIZE || exit 99
56
57 echo
58 echo "Comparing..."
59 limitarg=
60 if [ -n "$CMP_LIMIT" ]; then
61         limitarg="--bytes=$CMP_LIMIT"
62 fi
63 cmp $limitarg "${testname}.out" "${testname}.refimg"
64 success=$?
65
66 if [ $success -eq 0 ]; then
67         echo
68         echo "Testing fsck..."
69         run_fsck -n "${testname}.out"
70         success=$?
71 fi
72
73 rm -f "${testname}.out" "${testname}.refimg"
74
75 if [ $success -eq 2 ]; then
76         # cmp reported error
77         exit 99
78 fi
79 exit $success