xref: /freebsd/usr.sbin/makefs/tests/makefs_tests_common.sh (revision 49b49cda41feabe3439f7318e8bf40e3896c7bf4)
1#
2# Copyright 2015 EMC Corp.
3# All rights reserved.
4#
5# Redistribution and use in source and binary forms, with or without
6# modification, are permitted provided that the following conditions are
7# met:
8#
9# * Redistributions of source code must retain the above copyright
10#   notice, this list of conditions and the following disclaimer.
11# * Redistributions in binary form must reproduce the above copyright
12#   notice, this list of conditions and the following disclaimer in the
13#   documentation and/or other materials provided with the distribution.
14#
15# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
18# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
19# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
21# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26#
27# $FreeBSD$
28#
29
30KB=1024
31: ${TMPDIR=/tmp}
32# TODO: add mtree `time` support; get a lot of errors like this right now when
33# passing generating disk images with keyword mtree support, like:
34#
35# `[...]/mtree.spec:8: error: time: invalid value '1446458503'`
36#
37#DEFAULT_MTREE_KEYWORDS="type,mode,gid,uid,size,link,time"
38DEFAULT_MTREE_KEYWORDS="type,mode,gid,uid,size,link"
39TEST_IMAGE="$TMPDIR/test.img"
40TEST_INPUTS_DIR="$TMPDIR/inputs"
41TEST_MD_DEVICE_FILE="$TMPDIR/md.output"
42TEST_MOUNT_DIR="$TMPDIR/mnt"
43TEST_SPEC_FILE="$TMPDIR/mtree.spec"
44
45check_image_contents()
46{
47	local directories=$TEST_INPUTS_DIR
48	local excludes mtree_excludes_arg mtree_file
49	local mtree_keywords="$DEFAULT_MTREE_KEYWORDS"
50
51	while getopts "d:f:m:X:" flag; do
52		case "$flag" in
53		d)
54			directories="$directories $OPTARG"
55			;;
56		f)
57			mtree_file=$OPTARG
58			;;
59		m)
60			mtree_keywords=$OPTARG
61			;;
62		X)
63			excludes="$excludes $OPTARG"
64			;;
65		*)
66			echo "usage: check_image_contents [-d directory ...] [-f mtree-file] [-m mtree-keywords] [-X exclude]"
67			atf_fail "unhandled option: $flag"
68			;;
69		esac
70	done
71
72	if [ -n "$excludes" ]; then
73		echo "$excludes" | tr ' ' '\n' > excludes.txt
74		mtree_excludes_arg="-X excludes.txt"
75	fi
76
77	if [ -z "$mtree_file" ]; then
78		mtree_file=input_spec.mtree
79		for directory in $directories; do
80			mtree -c -k $mtree_keywords -p $directory $mtree_excludes_arg
81		done > $mtree_file
82	fi
83
84	echo "<---- Input spec BEGIN ---->"
85	cat $mtree_file
86	echo "<---- Input spec END ---->"
87	atf_check -e empty -o empty -s exit:0 \
88	    mtree -UW -f $mtree_file \
89		-p $TEST_MOUNT_DIR \
90		$mtree_excludes_arg
91}
92
93create_test_dirs()
94{
95	atf_check -e empty -s exit:0 mkdir -m 0777 -p $TEST_MOUNT_DIR
96	atf_check -e empty -s exit:0 mkdir -m 0777 -p $TEST_INPUTS_DIR
97}
98
99create_test_inputs()
100{
101	create_test_dirs
102
103	cd $TEST_INPUTS_DIR
104
105	atf_check -e empty -s exit:0 mkdir -m 0755 -p a/b/1
106	atf_check -e empty -s exit:0 ln -s a/b c
107	atf_check -e empty -s exit:0 touch d
108	atf_check -e empty -s exit:0 ln d e
109	atf_check -e empty -s exit:0 touch .f
110	atf_check -e empty -s exit:0 mkdir .g
111	# XXX: fifos on the filesystem don't match fifos created by makefs for
112	# some odd reason.
113	#atf_check -e empty -s exit:0 mkfifo h
114	atf_check -e ignore -s exit:0 dd if=/dev/zero of=i count=1000 bs=1
115	atf_check -e empty -s exit:0 touch klmn
116	atf_check -e empty -s exit:0 touch opqr
117	atf_check -e empty -s exit:0 touch stuv
118	atf_check -e empty -s exit:0 install -m 0755 /dev/null wxyz
119	atf_check -e empty -s exit:0 touch 0b00000001
120	atf_check -e empty -s exit:0 touch 0b00000010
121	atf_check -e empty -s exit:0 touch 0b00000011
122	atf_check -e empty -s exit:0 touch 0b00000100
123	atf_check -e empty -s exit:0 touch 0b00000101
124	atf_check -e empty -s exit:0 touch 0b00000110
125	atf_check -e empty -s exit:0 touch 0b00000111
126	atf_check -e empty -s exit:0 touch 0b00001000
127	atf_check -e empty -s exit:0 touch 0b00001001
128	atf_check -e empty -s exit:0 touch 0b00001010
129	atf_check -e empty -s exit:0 touch 0b00001011
130	atf_check -e empty -s exit:0 touch 0b00001100
131	atf_check -e empty -s exit:0 touch 0b00001101
132	atf_check -e empty -s exit:0 touch 0b00001110
133
134	for filesize in 1 512 $(( 2 * $KB )) $(( 10 * $KB )) $(( 512 * $KB )); \
135	do
136		atf_check -e ignore -o empty -s exit:0 \
137		    dd if=/dev/zero of=${filesize}.file bs=1 \
138		    count=1 oseek=${filesize} conv=sparse
139		files="${files} ${filesize}.file"
140	done
141
142	cd -
143}
144
145mount_image()
146{
147	atf_check -e empty -o save:$TEST_MD_DEVICE_FILE -s exit:0 \
148	    mdconfig -a -f $TEST_IMAGE
149	atf_check -e empty -o empty -s exit:0 \
150	    $MOUNT /dev/$(cat $TEST_MD_DEVICE_FILE) $TEST_MOUNT_DIR
151}
152
153