xref: /freebsd/usr.bin/mkimg/tests/mkimg_test.sh (revision 22cf89c938886d14f5796fc49f9f020c23ea8eaf)
1
2mkimg_blksz_list="512 4096"
3mkimg_format_list="qcow qcow2 raw vhd vhdf vhdx vmdk"
4mkimg_geom_list="1x1 63x255"
5mkimg_scheme_list="apm bsd ebr gpt mbr"
6
7bootcode()
8{
9    case $1 in
10      bsd)	echo 8192 ;;
11      gpt|mbr)	echo 512 ;;
12      *)	echo 0 ;;
13    esac
14    return 0
15}
16
17mkcontents()
18{
19    local byte count name
20
21    byte=$1
22    count=$2
23
24    name=_tmp-$byte-$count.bin
25    jot -b $byte $(($count/2)) > $name
26    echo $name
27    return 0
28}
29
30makeimage()
31{
32    local blksz bootarg bootsz format geom nhds nsecs partarg pfx scheme
33
34    format=$1
35    scheme=$2
36    blksz=$3
37    geom=$4
38    pfx=$5
39    shift 5
40
41    nsecs=${geom%x*}
42    nhds=${geom#*x}
43
44    bootsz=`bootcode $scheme`
45    if test $bootsz -gt 0; then
46	bootarg="-b `mkcontents B $bootsz`"
47    else
48	bootarg=""
49    fi
50
51    partarg=""
52    for P in $*; do
53	partarg="$partarg -p $P"
54    done
55    if test -z "$partarg"; then
56	local swap ufs
57	swap="-p freebsd-swap::128K"
58	ufs="-p freebsd-ufs:=$(atf_get_srcdir)/partition_data_4M.bin"
59	partarg="$ufs $swap"
60    fi
61
62    imagename=$pfx-$geom-$blksz-$scheme.$format
63
64    mkimg -y -f $format -o $imagename -s $scheme -P $blksz -H $nhds -T $nsecs \
65	    $bootarg $partarg
66    echo $imagename
67    return 0
68}
69
70mkimg_rebase()
71{
72    local baseline image result tmpfile update
73
74    image=$1
75    result=$2
76
77    baseline=$image.hex
78    update=yes
79
80    if test -f $baseline; then
81	tmpfile=_tmp-baseline
82	sed -e '/^#.*/D' < $baseline > $tmpfile
83	if diff -u $tmpfile $result; then
84	    update=no
85	fi
86    fi
87
88    if test $update = yes; then
89	# Prevent keyword expansion when writing the keyword.
90	(echo -n '# $'; echo -n FreeBSD; echo '$') > $baseline
91	cat $result >> $baseline
92    fi
93
94    rm $image $result _tmp-*
95    return 0
96}
97
98mkimg_test()
99{
100    local blksz format geom scheme
101
102    geom=$1
103    blksz=$2
104    scheme=$3
105    format=$4
106
107    case $scheme in
108      ebr|mbr)
109	bsd=`makeimage raw bsd $blksz $geom _tmp`
110	partinfo="freebsd:=$bsd"
111	;;
112      *)
113	partinfo=""
114	;;
115    esac
116    image=`makeimage $format $scheme $blksz $geom img $partinfo`
117    result=$image.out
118    hexdump -C $image > $result
119    if test "x$mkimg_update_baseline" = "xyes"; then
120	mkimg_rebase $image $result
121    else
122	baseline=`atf_get_srcdir`/$image
123	atf_check -s exit:0 diff -u $baseline $result
124    fi
125    return 0
126}
127
128atf_test_case rebase
129rebase_body()
130{
131    local nm
132
133    mkimg_update_baseline=yes
134    for nm in $mkimg_tests; do
135	${nm}_body
136    done
137    return 0
138}
139
140atf_init_test_cases()
141{
142    local B F G S nm
143
144    for G in $mkimg_geom_list; do
145	for B in $mkimg_blksz_list; do
146	    for S in $mkimg_scheme_list; do
147		for F in $mkimg_format_list; do
148		    nm="${S}_${G}_${B}_${F}"
149		    atf_test_case $nm
150		    eval "${nm}_body() { mkimg_test $G $B $S $F; }"
151		    mkimg_tests="${mkimg_tests} ${nm}"
152		    atf_add_test_case $nm
153		done
154	    done
155	done
156    done
157
158    # XXX hack to make updating the baseline easier
159    if test "${__RUNNING_INSIDE_ATF_RUN}" != "internal-yes-value"; then
160	atf_add_test_case rebase
161    fi
162}
163
164