xref: /freebsd/usr.sbin/makefs/tests/makefs_tests_common.sh (revision c2a55efd74cccb3d4e7b9037b240ad062c203bb8)
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
27KB=1024
28: ${TMPDIR=/tmp}
29DEFAULT_MTREE_KEYWORDS="type,mode,gid,uid,size,link,time"
30MTREE="mtree -k $DEFAULT_MTREE_KEYWORDS"
31TEST_IMAGE="$TMPDIR/test.img"
32TEST_INPUTS_DIR="$TMPDIR/inputs"
33TEST_MD_DEVICE_FILE="$TMPDIR/md.output"
34TEST_MOUNT_DIR="$TMPDIR/mnt"
35TEST_SPEC_FILE="$TMPDIR/mtree.spec"
36
37check_image_contents()
38{
39	local directories=$TEST_INPUTS_DIR
40	local excludes mtree_excludes_arg mtree_file
41	local mtree_keywords="$DEFAULT_MTREE_KEYWORDS"
42
43	while getopts "d:f:m:X:" flag; do
44		case "$flag" in
45		d)
46			directories="$directories $OPTARG"
47			;;
48		f)
49			mtree_file=$OPTARG
50			;;
51		m)
52			mtree_keywords=$OPTARG
53			;;
54		X)
55			excludes="$excludes $OPTARG"
56			;;
57		*)
58			echo "usage: check_image_contents [-d directory ...] [-f mtree-file] [-m mtree-keywords] [-X exclude]"
59			atf_fail "unhandled option: $flag"
60			;;
61		esac
62	done
63
64	if [ -n "$excludes" ]; then
65		echo "$excludes" | tr ' ' '\n' > excludes.txt
66		mtree_excludes_arg="-X excludes.txt"
67	fi
68
69	if [ -z "$mtree_file" ]; then
70		mtree_file=input_spec.mtree
71		for directory in $directories; do
72			mtree -c -k $mtree_keywords -p $directory $mtree_excludes_arg
73		done > $mtree_file
74	fi
75
76	echo "<---- Input spec BEGIN ---->"
77	cat $mtree_file
78	echo "<---- Input spec END ---->"
79	atf_check mtree -UW -f $mtree_file \
80	    -p $TEST_MOUNT_DIR $mtree_excludes_arg
81}
82
83create_manifest_file()
84{
85	# Prefer a specification format that is easier to parse (-C)
86	mtree -k "$DEFAULT_MTREE_KEYWORDS" -cp "$TEST_INPUTS_DIR" | \
87	    mtree -C > "$TEST_SPEC_FILE"
88	sed -i "" "s|^\.|.${TEST_INPUTS_DIR##$TMPDIR}|g" "$TEST_SPEC_FILE"
89}
90
91create_test_dirs()
92{
93	atf_check mkdir -m 0777 -p $TEST_MOUNT_DIR
94	atf_check mkdir -m 0777 -p $TEST_INPUTS_DIR
95}
96
97create_test_inputs()
98{
99	create_test_dirs
100
101	cd $TEST_INPUTS_DIR
102
103	atf_check mkdir -m 0755 -p a/b/1
104	atf_check ln -s a/b c
105	atf_check touch d
106	atf_check ln d e
107	atf_check touch .f
108	atf_check mkdir .g
109	# XXX: fifos on the filesystem don't match fifos created by makefs for
110	# some odd reason.
111	#atf_check mkfifo h
112	atf_check -e ignore dd if=/dev/zero of=i count=1000 bs=1
113	atf_check touch klmn
114	atf_check touch opqr
115	atf_check touch stuv
116	atf_check install -m 0755 /dev/null wxyz
117	atf_check touch 0b00000001
118	atf_check touch 0b00000010
119	atf_check touch 0b00000011
120	atf_check touch 0b00000100
121	atf_check touch 0b00000101
122	atf_check touch 0b00000110
123	atf_check touch 0b00000111
124	atf_check touch 0b00001000
125	atf_check touch 0b00001001
126	atf_check touch 0b00001010
127	atf_check touch 0b00001011
128	atf_check touch 0b00001100
129	atf_check touch 0b00001101
130	atf_check touch 0b00001110
131
132	for filesize in 1 512 $(( 2 * $KB )) $(( 10 * $KB )) $(( 512 * $KB )); \
133	do
134		atf_check -e ignore dd if=/dev/zero of=${filesize}.file bs=1 \
135		    count=${filesize} conv=sparse
136		files="${files} ${filesize}.file"
137	done
138
139	cd -
140}
141
142mount_image()
143{
144	atf_check -o save:$TEST_MD_DEVICE_FILE mdconfig -a -f $TEST_IMAGE
145	atf_check $MOUNT ${1} /dev/$(cat $TEST_MD_DEVICE_FILE) $TEST_MOUNT_DIR
146}
147
148change_mtree_timestamp()
149{
150	filename="$1"
151	timestamp="$2"
152
153	sed -i "" "s/time=.*$/time=${timestamp}.0/g" "$filename"
154}
155