summaryrefslogtreecommitdiffstats
path: root/test/basic-images.test
blob: 9b53b454e371a2b2c73a130fb7b147d37c5fc6d0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
#!/bin/bash

test_description="Basic Image Tests"

testdir="$(readlink -f $(dirname "${0}"))"
genimage="$(pwd)/genimage"

PATH="$PATH:/sbin:/usr/sbin"

set -- -v "$@"


. "${testdir}/sharness.sh"

filelist_orig="$(pwd)/file-list.orig"
filelist_test="$(pwd)/file-list.test"
root_orig="$(pwd)/root"
root_test="$(pwd)/root.test"

setup_data() {
	umask 0022
	mkdir -p "${root_orig}"/{foo,bar,baz,"with spaces"}/{1,2,3}
	touch "${root_orig}"/{foo,bar,baz,"with spaces"}/{1,2,3}/{one,two}
	find "${root_orig}" -print0 | xargs -0 touch -c -d "2011-11-11 UTC"
	find "${root_orig}"/ -mindepth 1 -printf "%P\n" | sort  > "${filelist_orig}"

	cp "${testdir}"/*.conf* .
}

run_genimage() {
	if [ "$verbose" = "t" ]; then
		vargs="--loglevel=3"
	fi
	rm -rf tmp images "${root_test}"
	mkdir "${root_test}" images
	# create a larger output image to make sure it is recreated properly
	dd if=/dev/zero of="images/${2}" bs=1M seek=30 count=1
	"${genimage}" \
		${vargs} \
		--outputpath=images \
		--inputpath=input \
		--rootpath=root \
		--tmppath=tmp \
		--config "${1}"
}

func_check() {
	local ret="$?"
	set +x
	if [ "${ret}" != 0 ]; then
		echo "Failed to execute '${FUNCNAME[1]}'!" >&2
		return "${ret}"
	fi
}

check_filelist() {
	test_cmp "${filelist_orig}" "${filelist_test}"
}

check_root() {
	diff -ru "${root_orig}" "${root_test}"
}

get_size() {
	local file="${1}"
	if [ ! -f "${file}" ]; then
		echo "Failed to check file size: '${file}' does not exist!"
		return 1
	fi
	set -- $(du -b "${file}")
	size="${1}"
}

check_size_range() {
	local size
	get_size "${1}" || return
	if [ "${size}" -lt "${2}" -o "${size}" -gt "${3}" ]; then
		echo "Incorrect file size for '${1}': expected min: ${2} max: ${3} found: ${size}"
		return 1
	fi
}

check_size() {
	local size
	get_size "${1}" || return
	if [ "${size}" -ne "${2}" ]; then
		echo "Incorrect file size for '${1}': expected: ${2} found: ${size}"
		return 1
	fi
}

exec_test_set_prereq() {
	command -v "${1}" > /dev/null && test_set_prereq "${1/./_}"
}

setup_data

set -o pipefail

exec_test_set_prereq cpio
test_expect_success cpio "cpio" "
	run_genimage cpio.config test.cpio &&
	zcat images/test.cpio | cpio --extract -t | grep -v '^\.$'  | sort > '${filelist_test}' &&
	check_size_range images/test.cpio 400 550 &&
	check_filelist
"

exec_test_set_prereq mkcramfs
test_expect_success mkcramfs "cramfs" "
	run_genimage cramfs.config test.cramfs &&
	check_size images/test.cramfs 4096
"

compare_label() {
	if [ "${1}" != "${2}" ]; then
		echo "Filesystem Label does not match: exprected: '${2}' found '${1}'"
		return 1
	fi
}

check_ext() {
	[ "$verbose" = "t" ] && set -x
	# UUID is randomly generated
	uuid="Filesystem UUID"
	# Hash Seed is randomly generated
	seed="Directory Hash Seed:"
	# checksum depends on random data
	csum1="Checksum: \|Group 0: (Blocks 1-4095) csum"
	# format change
	csum2="Group 0: (Blocks 1-4095) \\[ITABLE_ZEROED\\]\|  Checksum .*, unused inodes 205"
	dumpe2fs "${1}" | grep -v "^\($uuid\|$seed\|$csum1\|$csum2\)" > "dump" &&
	# fixup for ext3 journal size with old tune2fs
	sed -i 's/^\(Journal size:             \)1029k$/\11024k/' "dump" &&
	# output format changed with some version
	sed -i 's/^\(  Block bitmap at .*\),\( Inode bitmap.*\)$/\1\n \2/' "dump" &&
	test_cmp "${testdir}/${2}.dump" "dump" &&
	e2fsck -nf "${1}" &&
	# old versions of debugfs cannot dump '/'
	debugfs -R "ls -p" "${1}" | (
		IFS=/
		while read a b c d e x f; do
			case "${x}" in
			.|..|lost+found|"") continue ;;
			esac
			debugfs -R "rdump \"${x}\" \"${root_test}\"" "${1}" || break
		done
	) &&
	check_size "${1}" "${3}" &&
	check_root
	func_check
}

exec_test_set_prereq genext2fs
exec_test_set_prereq e2fsck
test_expect_success genext2fs,e2fsck "ext2" "
	run_genimage ext2.config test.ext2 &&
	check_ext images/test.ext2 ext2test 4194304
"

test_expect_success genext2fs,e2fsck "ext3" "
	run_genimage ext3.config test.ext3 &&
	check_ext images/test.ext3 ext3test 4194304
"

test_expect_success genext2fs,e2fsck "ext4" "
	run_genimage ext4.config test.ext4 &&
	check_ext images/test.ext4 ext4test 4194304
"

# make sure mke2fs supports '-d root-directory'
[ "$(mke2fs |& sed -n 's/.*\(-d \).*/\1/p')" = "-d " ] && test_set_prereq mke2fs
test_expect_success mke2fs,e2fsck "mke2fs" "
	run_genimage mke2fs.config mke2fs.ext4 &&
	check_ext images/mke2fs.ext4 mke2fs 33554432
"

setup_test_images() {
	rm -rf input &&
	mkdir input &&
	dd if=/dev/zero of=input/part1.img bs=512 count=7 &&
	dd if=/dev/zero of=input/part2.img bs=512 count=11
}

exec_test_set_prereq dd
test_expect_success "flash" "
	setup_test_images &&
	run_genimage flash.config test.flash &&
	md5sum -c '${testdir}/flash.md5'
"

exec_test_set_prereq fdisk
exec_test_set_prereq sfdisk
test_expect_success fdisk,sfdisk "hdimage" "
	setup_test_images &&
	run_genimage hdimage.config test.hdimage &&
	check_size images/test.hdimage 9442816 &&
	# check the this identifier
	fdisk -l images/test.hdimage | grep identifier: > hdimage.fdisk &&
	# check partitions; filter output to handle different sfdisk versions
	sfdisk -d images/test.hdimage 2>/dev/null | grep '^images/' | \
		sed -e 's/  *//g' -e 's;Id=;type=;' >> hdimage.fdisk &&
	test_cmp '${testdir}/hdimage.fdisk' hdimage.fdisk &&
	check_size images/test.hdimage-2 11539968 &&
	# check the this identifier
	fdisk -l images/test.hdimage-2 | grep identifier: > hdimage.fdisk-2 &&
	# check partitions; filter output to handle different sfdisk versions
	sfdisk -d images/test.hdimage-2 2>/dev/null | grep '^images/' | \
		sed -e 's/  *//g' -e 's;Id=;type=;' -e '/start=0,size=0,type=0/d' >> hdimage.fdisk-2 &&
	test_cmp '${testdir}/hdimage.fdisk-2' hdimage.fdisk-2
"

test_expect_success "hdimage2" "
	setup_test_images &&
	test_must_fail run_genimage hdimage2.config test.hdimage
"

sfdisk -h | grep -q gpt && test_set_prereq sfdisk-gpt
fdisk -h | grep -q gpt && test_set_prereq fdisk-gpt
test_expect_success fdisk-gpt,sfdisk-gpt "hdimage4" "
	setup_test_images &&
	run_genimage hdimage4.config test.hdimage &&
	check_size images/test.hdimage 7356928 &&
	# check the this identifier
	fdisk -l images/test.hdimage | grep identifier: > hdimage4.fdisk &&
	# check partitions; filter output to handle different sfdisk versions
	sfdisk -d images/test.hdimage 2>/dev/null | grep '^images/' | \
		sed -e 's/  *//g' -e 's;Id=;type=;' >> hdimage4.fdisk &&
	test_cmp '${testdir}/hdimage4.fdisk' hdimage4.fdisk
"

exec_test_set_prereq genisoimage
test_expect_success genisoimage "iso" "
	run_genimage iso.config test.iso &&
	check_size_range images/test.iso 300000 400000
"

exec_test_set_prereq mkfs.jffs2
test_expect_success mkfs_jffs2 "jffs2" "
	run_genimage jffs2.config test.jffs2 &&
	md5sum -c '${testdir}/jffs2.md5'
"

exec_test_set_prereq dd
exec_test_set_prereq diff
exec_test_set_prereq qemu-img
test_expect_success dd,diff,qemu-img "qemu" "
	setup_test_images &&
	run_genimage qemu.config test.qcow &&
	qemu-img check images/test.qcow &&
	zcat '${testdir}/qemu.qcow.gz' > qemu.qcow &&
	qemu-img compare images/test.qcow qemu.qcow
"

exec_test_set_prereq mksquashfs
test_expect_success mksquashfs "squashfs" "
	run_genimage squashfs.config test.squashfs &&
	check_size_range images/test.squashfs 4000 4100 &&
	unsquashfs -ls images/test.squashfs  | sed -n '/squashfs-root/s;squashfs-root/;;p' | sort > '${filelist_test}' &&
	check_filelist
"

exec_test_set_prereq tar
test_expect_success tar "tar" "
	run_genimage tar.config test.tar.gz &&
	check_size_range images/test.tar.gz 500 600 &&
	zcat images/test.tar.gz | tar -t | sed -n -e 's;/$;;' -e 's;^\./\(..*\)$;\1;p' | sort > '${filelist_test}' &&
	check_filelist
"

exec_test_set_prereq mkfs.ubifs
test_expect_success mkfs_ubifs "ubifs" "
	run_genimage ubifs.config test.ubifs &&
	check_size_range images/test.ubifs 200000 300000
"

setup_ubi_images() {
	rm -rf input &&
	mkdir input &&
	cp images/test.ubifs input/
}

exec_test_set_prereq ubinize
test_expect_success ubinize "ubi" "
	setup_ubi_images &&
	run_genimage ubi.config test.ubi &&
	check_size_range images/test.ubi 550000 600000
"

exec_test_set_prereq dd
exec_test_set_prereq mkdosfs
exec_test_set_prereq mcopy
test_expect_success dd,mkdosfs,mcopy "vfat" "
	run_genimage vfat.config test.vfat &&
	check_size images/test.vfat 4193280
	MTOOLS_SKIP_CHECK=1 mdir -/ -f -b -i images/test.vfat / | sed -e 's;^::/;;' -e 's;/$;;' | sort > '${filelist_test}' &&
	check_filelist
"

setup_fit_its() {
	setup_test_images &&
	cp ${testdir}/fit.its input/
}

exec_test_set_prereq mkimage
exec_test_set_prereq dtc
test_expect_success mkimage,dtc "fit" "
	setup_fit_its &&
	run_genimage fit.config test.fit
"

test_done

# vim: syntax=sh