1#!/bin/sh
2#
3# $NetBSD: h_funcs.subr,v 1.5 2013/03/17 01:16:45 jmmv Exp $
4#
5# Copyright (c) 2005, 2006, 2007 The NetBSD Foundation, Inc.
6# All rights reserved.
7#
8# Redistribution and use in source and binary forms, with or without
9# modification, are permitted provided that the following conditions
10# are met:
11# 1. Redistributions of source code must retain the above copyright
12#    notice, this list of conditions and the following disclaimer.
13# 2. Redistributions in binary form must reproduce the above copyright
14#    notice, this list of conditions and the following disclaimer in the
15#    documentation and/or other materials provided with the distribution.
16#
17# THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
18# ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
19# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20# PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
21# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27# POSSIBILITY OF SUCH DAMAGE.
28#
29
30Mount_Point=
31
32#
33# test_mount [args]
34#
35#	Mounts tmpfs over ${Mount_Point} and changes the current directory
36#	to the mount point.  Optional arguments may be passed to the
37#	mount command.
38#
39test_mount() {
40	require_fs tmpfs
41
42	Mount_Point=$(pwd)/mntpt
43	atf_check -s eq:0 -o empty -e empty mkdir "${Mount_Point}"
44	echo "mount -t tmpfs ${*} tmpfs ${Mount_Point}"
45	mount -t tmpfs "${@}" tmpfs "${Mount_Point}" 2>mounterr
46	if [ "${?}" -ne 0 ]; then
47		cat mounterr 1>&2
48		if grep 'Operation not supported' mounterr > /dev/null; then
49			atf_skip "tmpfs not supported"
50		fi
51		atf_fail "Failed to mount a tmpfs file system"
52	fi
53	cd "${Mount_Point}"
54}
55
56#
57# test_unmount
58#
59#	Unmounts the file system mounted by test_mount.
60#
61test_unmount() {
62	# Begin FreeBSD
63	_test_unmount
64	exit_code=$?
65	atf_check_equal "$exit_code" "0"
66	return $exit_code
67	# End FreeBSD
68	cd - >/dev/null
69	atf_check -s eq:0 -o empty -e empty umount ${Mount_Point}
70	atf_check -s eq:0 -o empty -e empty rmdir ${Mount_Point}
71	Mount_Point=
72}
73
74# Begin FreeBSD
75_test_unmount() {
76	if [ -z "${Mount_Point}" -o ! -d "${Mount_Point}" ]; then
77		return 0
78	fi
79
80	cd - >/dev/null
81	umount ${Mount_Point}
82	rmdir ${Mount_Point}
83	Mount_Point=
84}
85# End FreeBSD
86
87#
88# kqueue_monitor expected_nevents file1 [.. fileN]
89#
90#	Monitors the commands given through stdin (one per line) using
91#	kqueue and stores the events raised in a log that can be later
92#	verified with kqueue_check.
93#
94kqueue_monitor() {
95	nev=${1}; shift
96	echo "Running kqueue-monitored commands and expecting" \
97	    "${nev} events"
98	$(atf_get_srcdir)/h_tools kqueue ${*} >kqueue.log || \
99	    atf_fail "Could not launch kqueue monitor"
100	got=$(wc -l kqueue.log | awk '{ print $1 }')
101	test ${got} -eq ${nev} || \
102	    atf_fail "Got ${got} events but expected ${nev}"
103}
104
105#
106# kqueue_check file event
107#
108#	Checks if kqueue raised the given event when monitoring the
109#	given file.
110#
111kqueue_check() {
112	echo "Checking if ${1} received ${2}"
113	grep "^${1} - ${2}$" kqueue.log >/dev/null || \
114	    atf_fail "${1} did not receive ${2}"
115}
116