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