1289694Sngie#
2289694Sngie# Copyright 2015 EMC Corp.
3289694Sngie# All rights reserved.
4289694Sngie#
5289694Sngie# Redistribution and use in source and binary forms, with or without
6289694Sngie# modification, are permitted provided that the following conditions are
7289694Sngie# met:
8289694Sngie#
9289694Sngie# * Redistributions of source code must retain the above copyright
10289694Sngie#   notice, this list of conditions and the following disclaimer.
11289694Sngie# * Redistributions in binary form must reproduce the above copyright
12289694Sngie#   notice, this list of conditions and the following disclaimer in the
13289694Sngie#   documentation and/or other materials provided with the distribution.
14289694Sngie#
15289694Sngie# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16289694Sngie# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17289694Sngie# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
18289694Sngie# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
19289694Sngie# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20289694Sngie# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
21289694Sngie# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22289694Sngie# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23289694Sngie# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24289694Sngie# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25289694Sngie# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26289694Sngie#
27289694Sngie# $FreeBSD$
28289694Sngie#
29289694Sngie
30290586SngieKB=1024
31290586Sngie: ${TMPDIR=/tmp}
32290594Sngie# TODO: add mtree `time` support; get a lot of errors like this right now when
33290594Sngie# passing generating disk images with keyword mtree support, like:
34290594Sngie#
35290594Sngie# `[...]/mtree.spec:8: error: time: invalid value '1446458503'`
36290594Sngie#
37290594Sngie#DEFAULT_MTREE_KEYWORDS="type,mode,gid,uid,size,link,time"
38290594SngieDEFAULT_MTREE_KEYWORDS="type,mode,gid,uid,size,link"
39290586SngieTEST_IMAGE="$TMPDIR/test.img"
40290586SngieTEST_INPUTS_DIR="$TMPDIR/inputs"
41290586SngieTEST_MD_DEVICE_FILE="$TMPDIR/md.output"
42290586SngieTEST_MOUNT_DIR="$TMPDIR/mnt"
43290586SngieTEST_SPEC_FILE="$TMPDIR/mtree.spec"
44289694Sngie
45290586Sngiecheck_image_contents()
46289694Sngie{
47290586Sngie	local directories=$TEST_INPUTS_DIR
48290586Sngie	local excludes mtree_excludes_arg mtree_file
49290594Sngie	local mtree_keywords="$DEFAULT_MTREE_KEYWORDS"
50289694Sngie
51290586Sngie	while getopts "d:f:m:X:" flag; do
52290586Sngie		case "$flag" in
53290586Sngie		d)
54290586Sngie			directories="$directories $OPTARG"
55290586Sngie			;;
56290586Sngie		f)
57290586Sngie			mtree_file=$OPTARG
58290586Sngie			;;
59290586Sngie		m)
60290586Sngie			mtree_keywords=$OPTARG
61290586Sngie			;;
62290586Sngie		X)
63290586Sngie			excludes="$excludes $OPTARG"
64290586Sngie			;;
65290586Sngie		*)
66290586Sngie			echo "usage: check_image_contents [-d directory ...] [-f mtree-file] [-m mtree-keywords] [-X exclude]"
67290586Sngie			atf_fail "unhandled option: $flag"
68290586Sngie			;;
69290586Sngie		esac
70290586Sngie	done
71289694Sngie
72290586Sngie	if [ -n "$excludes" ]; then
73290586Sngie		echo "$excludes" | tr ' ' '\n' > excludes.txt
74290586Sngie		mtree_excludes_arg="-X excludes.txt"
75290586Sngie	fi
76289694Sngie
77290586Sngie	if [ -z "$mtree_file" ]; then
78290586Sngie		mtree_file=input_spec.mtree
79290586Sngie		for directory in $directories; do
80290586Sngie			mtree -c -k $mtree_keywords -p $directory $mtree_excludes_arg
81290586Sngie		done > $mtree_file
82290586Sngie	fi
83289694Sngie
84290586Sngie	echo "<---- Input spec BEGIN ---->"
85290586Sngie	cat $mtree_file
86290586Sngie	echo "<---- Input spec END ---->"
87290586Sngie	atf_check -e empty -o empty -s exit:0 \
88290586Sngie	    mtree -UW -f $mtree_file \
89290586Sngie		-p $TEST_MOUNT_DIR \
90290586Sngie		$mtree_excludes_arg
91290586Sngie}
92290586Sngie
93290586Sngiecreate_test_dirs()
94290586Sngie{
95289694Sngie	atf_check -e empty -s exit:0 mkdir -m 0777 -p $TEST_MOUNT_DIR
96289694Sngie	atf_check -e empty -s exit:0 mkdir -m 0777 -p $TEST_INPUTS_DIR
97289694Sngie}
98289694Sngie
99289694Sngiecreate_test_inputs()
100289694Sngie{
101290586Sngie	create_test_dirs
102289694Sngie
103290586Sngie	cd $TEST_INPUTS_DIR
104290586Sngie
105289694Sngie	atf_check -e empty -s exit:0 mkdir -m 0755 -p a/b/1
106289694Sngie	atf_check -e empty -s exit:0 ln -s a/b c
107289694Sngie	atf_check -e empty -s exit:0 touch d
108289694Sngie	atf_check -e empty -s exit:0 ln d e
109289694Sngie	atf_check -e empty -s exit:0 touch .f
110289694Sngie	atf_check -e empty -s exit:0 mkdir .g
111290586Sngie	# XXX: fifos on the filesystem don't match fifos created by makefs for
112290586Sngie	# some odd reason.
113290586Sngie	#atf_check -e empty -s exit:0 mkfifo h
114289694Sngie	atf_check -e ignore -s exit:0 dd if=/dev/zero of=i count=1000 bs=1
115289694Sngie	atf_check -e empty -s exit:0 touch klmn
116289694Sngie	atf_check -e empty -s exit:0 touch opqr
117289694Sngie	atf_check -e empty -s exit:0 touch stuv
118289694Sngie	atf_check -e empty -s exit:0 install -m 0755 /dev/null wxyz
119289694Sngie	atf_check -e empty -s exit:0 touch 0b00000001
120289694Sngie	atf_check -e empty -s exit:0 touch 0b00000010
121289694Sngie	atf_check -e empty -s exit:0 touch 0b00000011
122289694Sngie	atf_check -e empty -s exit:0 touch 0b00000100
123289694Sngie	atf_check -e empty -s exit:0 touch 0b00000101
124289694Sngie	atf_check -e empty -s exit:0 touch 0b00000110
125289694Sngie	atf_check -e empty -s exit:0 touch 0b00000111
126289694Sngie	atf_check -e empty -s exit:0 touch 0b00001000
127289694Sngie	atf_check -e empty -s exit:0 touch 0b00001001
128289694Sngie	atf_check -e empty -s exit:0 touch 0b00001010
129289694Sngie	atf_check -e empty -s exit:0 touch 0b00001011
130289694Sngie	atf_check -e empty -s exit:0 touch 0b00001100
131289694Sngie	atf_check -e empty -s exit:0 touch 0b00001101
132289694Sngie	atf_check -e empty -s exit:0 touch 0b00001110
133290586Sngie
134290586Sngie	for filesize in 1 512 $(( 2 * $KB )) $(( 10 * $KB )) $(( 512 * $KB )); \
135290586Sngie	do
136290586Sngie		atf_check -e ignore -o empty -s exit:0 \
137290586Sngie		    dd if=/dev/zero of=${filesize}.file bs=1 \
138290586Sngie		    count=1 oseek=${filesize} conv=sparse
139290586Sngie		files="${files} ${filesize}.file"
140290586Sngie	done
141290586Sngie
142290586Sngie	cd -
143289694Sngie}
144290586Sngie
145290586Sngiemount_image()
146290586Sngie{
147290586Sngie	atf_check -e empty -o save:$TEST_MD_DEVICE_FILE -s exit:0 \
148290586Sngie	    mdconfig -a -f $TEST_IMAGE
149290586Sngie	atf_check -e empty -o empty -s exit:0 \
150290586Sngie	    $MOUNT /dev/$(cat $TEST_MD_DEVICE_FILE) $TEST_MOUNT_DIR
151290586Sngie}
152290586Sngie
153