1170809Sdelphij#!/bin/sh
2170809Sdelphij#
3170809Sdelphij# $NetBSD: h_funcs.subr,v 1.5 2006/11/09 16:20:06 jmmv Exp $
4170809Sdelphij#
5170809Sdelphij# Copyright (c) 2005, 2006 The NetBSD Foundation, Inc.
6170809Sdelphij# All rights reserved.
7170809Sdelphij#
8170809Sdelphij# This code is derived from software contributed to The NetBSD Foundation
9170809Sdelphij# by Julio M. Merino Vidal, developed as part of Google's Summer of Code
10170809Sdelphij# 2005 program.
11170809Sdelphij#
12170809Sdelphij# Redistribution and use in source and binary forms, with or without
13170809Sdelphij# modification, are permitted provided that the following conditions
14170809Sdelphij# are met:
15170809Sdelphij# 1. Redistributions of source code must retain the above copyright
16170809Sdelphij#    notice, this list of conditions and the following disclaimer.
17170809Sdelphij# 2. Redistributions in binary form must reproduce the above copyright
18170809Sdelphij#    notice, this list of conditions and the following disclaimer in the
19170809Sdelphij#    documentation and/or other materials provided with the distribution.
20170809Sdelphij#
21170809Sdelphij# THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
22170809Sdelphij# ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
23170809Sdelphij# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24170809Sdelphij# PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
25170809Sdelphij# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26170809Sdelphij# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27170809Sdelphij# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28170809Sdelphij# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29170809Sdelphij# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30170809Sdelphij# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31170809Sdelphij# POSSIBILITY OF SUCH DAMAGE.
32170809Sdelphij#
33170809Sdelphij# $FreeBSD$
34170809Sdelphij#
35170809Sdelphij
36170809Sdelphij#
37170809Sdelphij# Helper functions for tests written in shell script.
38170809Sdelphij#
39170809Sdelphij
40170809SdelphijProg_Name=${0##*/}
41170809SdelphijSrc_Dir=$(pwd)
42170809SdelphijUnprived_User=
43170809SdelphijVerbose=2
44170809SdelphijWork_Dir=$(pwd)/tmp
45170809Sdelphij
46170809Sdelphij# -------------------------------------------------------------------------
47170809Sdelphij
48170809Sdelphij# die
49170809Sdelphij#
50170809Sdelphij#	Called by tests when a command fails unexpectedly.  Terminates
51170809Sdelphij#	execution and tries to clean up the mount point.
52170809Sdelphij#
53170809Sdelphijdie() {
54170809Sdelphij	if [ -d ${Work_Dir} ]; then
55170809Sdelphij		cd ${Src_Dir}
56170809Sdelphij		umount ${Work_Dir}
57170809Sdelphij		rmdir ${Work_Dir}
58170809Sdelphij	fi
59170809Sdelphij	[ ${Verbose} -eq 2 ] && err "Test ended unexpectedly"
60170809Sdelphij	[ ${Verbose} -eq 1 ] && echo " failed."
61170809Sdelphij	exit 1
62170809Sdelphij}
63170809Sdelphij
64170809Sdelphij# -------------------------------------------------------------------------
65170809Sdelphij
66170809Sdelphij# err message
67170809Sdelphij#
68170809Sdelphij#	Shows the given error message and terminates the program.
69170809Sdelphij#
70170809Sdelphijerr() {
71170809Sdelphij	echo "${Prog_Name}: $*" 1>&2
72170809Sdelphij	exit 1
73170809Sdelphij}
74170809Sdelphij
75170809Sdelphij# -------------------------------------------------------------------------
76170809Sdelphij
77170809Sdelphij# test_mount [args]
78170809Sdelphij#
79170809Sdelphij#	Mounts tmpfs over ${Work_Dir} and changes the current directory
80170809Sdelphij#	to the mount point.  Optional arguments may be passed to the
81170809Sdelphij#	mount command.
82170809Sdelphij#
83170809Sdelphijtest_mount() {
84170809Sdelphij	mkdir ${Work_Dir} || die
85170809Sdelphij	if [ $# -gt 0 ]; then
86170809Sdelphij		mount -t tmpfs "$@" tmpfs ${Work_Dir} || die
87170809Sdelphij	else
88170809Sdelphij		mount -t tmpfs tmpfs ${Work_Dir} || die
89170809Sdelphij	fi
90170809Sdelphij	cd ${Work_Dir}
91170809Sdelphij}
92170809Sdelphij
93170809Sdelphij# -------------------------------------------------------------------------
94170809Sdelphij
95170809Sdelphij# test_name message
96170809Sdelphij#
97170809Sdelphij#	Prints a message about what a test is going to do.
98170809Sdelphij#
99170809Sdelphijtest_name() {
100170809Sdelphij	[ ${Verbose} -gt 1 ] && echo "    $*..."
101170809Sdelphij}
102170809Sdelphij
103170809Sdelphij# -------------------------------------------------------------------------
104170809Sdelphij
105170809Sdelphij# test_unmount
106170809Sdelphij#
107170809Sdelphij#	Unmounts the file system mounted by test_mount.
108170809Sdelphij#
109170809Sdelphijtest_unmount() {
110170809Sdelphij	cd -
111170809Sdelphij	umount ${Work_Dir} || die
112170809Sdelphij	rmdir ${Work_Dir} || die
113170809Sdelphij}
114170809Sdelphij
115170809Sdelphij# -------------------------------------------------------------------------
116170809Sdelphij
117170809Sdelphij# kqueue_monitor expected_nevents file1 [.. fileN]
118170809Sdelphij#
119170809Sdelphij#	Monitors the commands given through stdin (one per line) using
120170809Sdelphij#	kqueue and stores the events raised in a log that can be later
121170809Sdelphij#	verified with kqueue_check.
122170809Sdelphij#
123170809Sdelphijkqueue_monitor() {
124170809Sdelphij	nev=${1}; shift
125170809Sdelphij	test_name "Running kqueue-monitored commands and expecting" \
126170809Sdelphij	    "${nev} events"
127170809Sdelphij	${Src_Dir}/h_tools kqueue ${*} >kqueue.log || return 1
128170809Sdelphij	got=$(wc -l kqueue.log | awk '{ print $1 }')
129170809Sdelphij	test ${got} -eq ${nev}
130170809Sdelphij}
131170809Sdelphij
132170809Sdelphij# -------------------------------------------------------------------------
133170809Sdelphij
134170809Sdelphij# kqueue_check file event
135170809Sdelphij#
136170809Sdelphij#	Checks if kqueue raised the given event when monitoring the
137170809Sdelphij#	given file.
138170809Sdelphij#
139170809Sdelphijkqueue_check() {
140170809Sdelphij	grep "^${1} - ${2}$" kqueue.log >/dev/null
141170809Sdelphij}
142170809Sdelphij
143170809Sdelphij# -------------------------------------------------------------------------
144170809Sdelphij
145170809Sdelphijmain() {
146170809Sdelphij	local args
147170809Sdelphij
148170809Sdelphij	[ $(id -un) = root ] || err "Must be run as root"
149170809Sdelphij
150170809Sdelphij	args=$(getopt u:v:w: $*)
151170809Sdelphij	if [ $? -ne 0 ]; then
152170809Sdelphij		echo "Usage: ${Prog_Name} [-u unprived_user] [-v level] " \
153170809Sdelphij		    "[-w root_dir]" 1>&2
154170809Sdelphij		return 1
155170809Sdelphij	fi
156170809Sdelphij	set -- ${args}
157170809Sdelphij	while [ $# -gt 0 ]; do
158170809Sdelphij		case "$1" in
159170809Sdelphij			-u)
160170809Sdelphij				Unprived_User="$2"; shift
161170809Sdelphij				;;
162170809Sdelphij			-v)
163170809Sdelphij				Verbose="$2"; shift
164170809Sdelphij				;;
165170809Sdelphij			-w)
166170809Sdelphij				Work_Dir="$2"; shift
167170809Sdelphij				;;
168170809Sdelphij			--)
169170809Sdelphij				shift; break
170170809Sdelphij				;;
171170809Sdelphij		esac
172170809Sdelphij		shift
173170809Sdelphij	done
174170809Sdelphij
175170809Sdelphij	[ ${Verbose} -eq 1 ] && echo -n "${Prog_Name}:"
176170809Sdelphij	[ ${Verbose} -eq 2 ] && echo "${Prog_Name}: Running tests"
177170809Sdelphij	test_run
178170809Sdelphij	[ ${Verbose} -eq 1 ] && echo " ok."
179170809Sdelphij	[ ${Verbose} -eq 2 ] && echo "${Prog_Name}: All tests were successful"
180170809Sdelphij
181170809Sdelphij	return 0
182170809Sdelphij}
183170809Sdelphij
184170809Sdelphijmain "$@"
185