xref: /freebsd/contrib/pjdfstest/tests/misc.sh (revision 86c9d9918f1db7cdd968b60f8902466887bcd9e9)
1# $FreeBSD$
2
3ntest=1
4
5case "${dir}" in
6/*)
7	maindir="${dir}/../.."
8	;;
9*)
10	maindir="`pwd`/${dir}/../.."
11	;;
12esac
13fstest="${maindir}/pjdfstest"
14. ${maindir}/tests/conf
15
16expect()
17{
18	e="${1}"
19	shift
20	r=`${fstest} $* 2>/dev/null | tail -1`
21	echo "${r}" | ${GREP} -Eq '^'${e}'$'
22	if [ $? -eq 0 ]; then
23		if [ -z "${todomsg}" ]; then
24			echo "ok ${ntest}"
25		else
26			echo "ok ${ntest} # TODO ${todomsg}"
27		fi
28	else
29		if [ -z "${todomsg}" ]; then
30			echo "not ok ${ntest} - tried '$*', expected ${e}, got ${r}"
31		else
32			echo "not ok ${ntest} # TODO ${todomsg}"
33		fi
34	fi
35	todomsg=""
36	ntest=$((ntest+1))
37}
38
39jexpect()
40{
41	s="${1}"
42	d="${2}"
43	e="${3}"
44	shift 3
45	r=`jail -s ${s} / pjdfstest 127.0.0.1 /bin/sh -c "cd ${d} && ${fstest} $* 2>/dev/null" | tail -1`
46	echo "${r}" | ${GREP} -Eq '^'${e}'$'
47	if [ $? -eq 0 ]; then
48		if [ -z "${todomsg}" ]; then
49			echo "ok ${ntest}"
50		else
51			echo "ok ${ntest} # TODO ${todomsg}"
52		fi
53	else
54		if [ -z "${todomsg}" ]; then
55			echo "not ok ${ntest} - tried '$*', expected ${e}, got ${r}"
56		else
57			echo "not ok ${ntest} # TODO ${todomsg}"
58		fi
59	fi
60	todomsg=""
61	ntest=$((ntest+1))
62}
63
64test_check()
65{
66	if [ $* ]; then
67		if [ -z "${todomsg}" ]; then
68			echo "ok ${ntest}"
69		else
70			echo "ok ${ntest} # TODO ${todomsg}"
71		fi
72	else
73		if [ -z "${todomsg}" ]; then
74			echo "not ok ${ntest}"
75		else
76			echo "not ok ${ntest} # TODO ${todomsg}"
77		fi
78	fi
79	todomsg=""
80	ntest=$((ntest+1))
81}
82
83todo()
84{
85	if [ "${os}" = "${1}" -o "${os}:${fs}" = "${1}" ]; then
86		todomsg="${2}"
87	fi
88}
89
90namegen()
91{
92	echo "pjdfstest_`dd if=/dev/urandom bs=1k count=1 2>/dev/null | openssl md5 | awk '{print $NF}'`"
93}
94
95namegen_len()
96{
97	len="${1}"
98
99	name=""
100	while :; do
101		namepart="`dd if=/dev/urandom bs=64 count=1 2>/dev/null | openssl md5 | awk '{print $NF}'`"
102		name="${name}${namepart}"
103		curlen=`printf "%s" "${name}" | wc -c`
104		[ ${curlen} -lt ${len} ] || break
105	done
106	name=`echo "${name}" | cut -b -${len}`
107	printf "%s" "${name}"
108}
109
110# POSIX:
111# {NAME_MAX}
112#     Maximum number of bytes in a filename (not including terminating null).
113namegen_max()
114{
115	name_max=`${fstest} pathconf . _PC_NAME_MAX`
116	namegen_len ${name_max}
117}
118
119# POSIX:
120# {PATH_MAX}
121#     Maximum number of bytes in a pathname, including the terminating null character.
122dirgen_max()
123{
124	name_max=`${fstest} pathconf . _PC_NAME_MAX`
125	complen=$((name_max/2))
126	path_max=`${fstest} pathconf . _PC_PATH_MAX`
127	# "...including the terminating null character."
128	path_max=$((path_max-1))
129
130	name=""
131	while :; do
132		name="${name}`namegen_len ${complen}`/"
133		curlen=`printf "%s" "${name}" | wc -c`
134		[ ${curlen} -lt ${path_max} ] || break
135	done
136	name=`echo "${name}" | cut -b -${path_max}`
137	name=`echo "${name}" | sed -E 's@/$@x@'`
138	printf "%s" "${name}"
139}
140
141quick_exit()
142{
143	echo "1..1"
144	echo "ok 1"
145	exit 0
146}
147
148supported()
149{
150	case "${1}" in
151	lchmod)
152		if [ "${os}" != "FreeBSD" ]; then
153			return 1
154		fi
155		;;
156	chflags)
157		if [ "${os}" != "FreeBSD" ]; then
158			return 1
159		fi
160		;;
161	chflags_SF_SNAPSHOT)
162		if [ "${os}" != "FreeBSD" -o "${fs}" != "UFS" ]; then
163			return 1
164		fi
165		;;
166	esac
167	return 0
168}
169
170require()
171{
172	if supported ${1}; then
173		return
174	fi
175	quick_exit
176}
177
178# usage:
179#	create_file <type> <name>
180#	create_file <type> <name> <mode>
181#	create_file <type> <name> <uid> <gid>
182#	create_file <type> <name> <mode> <uid> <gid>
183create_file() {
184	type="${1}"
185	name="${2}"
186
187	case "${type}" in
188	none)
189		return
190		;;
191	regular)
192		expect 0 create ${name} 0644
193		;;
194	dir)
195		expect 0 mkdir ${name} 0755
196		;;
197	fifo)
198		expect 0 mkfifo ${name} 0644
199		;;
200	block)
201		expect 0 mknod ${name} b 0644 1 2
202		;;
203	char)
204		expect 0 mknod ${name} c 0644 1 2
205		;;
206	socket)
207		expect 0 bind ${name}
208		;;
209	symlink)
210		expect 0 symlink test ${name}
211		;;
212	esac
213	if [ -n "${3}" -a -n "${4}" -a -n "${5}" ]; then
214		expect 0 lchmod ${name} ${3}
215		expect 0 lchown ${name} ${4} ${5}
216	elif [ -n "${3}" -a -n "${4}" ]; then
217		expect 0 lchown ${name} ${3} ${4}
218	elif [ -n "${3}" ]; then
219		expect 0 lchmod ${name} ${3}
220	fi
221}
222
223# Tests for whether or not a filesystem is mounted with a particular option
224# with -o, e.g. `mount -o noexec`.
225#
226# Parameters:
227# - mount_option - noatime, noexec, etc.
228#
229# Returns:
230# - 0 if mounted with the option.
231# - 1 otherwise.
232has_mount_option()
233{
234	local IFS=,
235	local mount_opt
236
237	local mount_option_search=$1
238
239	# XXX: mountpoint is defined in .../tests/sys/pjdfstest/tests/conf
240	for mount_opt in $(mount -d -p | awk '$2 == "'$mountpoint'" { print $4 }'); do
241		if [ "$mount_opt" = "$mount_option_search" ]; then
242			return 0
243		fi
244	done
245	return 1
246}
247
248# Filesystem must be mounted with -o exec
249requires_exec()
250{
251	if has_mount_option noexec; then
252		echo "1..0 # SKIP filesystem mounted with -o noexec"
253		exit 0
254	fi
255}
256