1#!/bin/sh 2# 3# $NetBSD: h_funcs.subr,v 1.5 2006/11/09 16:20:06 jmmv Exp $ 4# 5# Copyright (c) 2005, 2006 The NetBSD Foundation, Inc. 6# All rights reserved. 7# 8# This code is derived from software contributed to The NetBSD Foundation 9# by Julio M. Merino Vidal, developed as part of Google's Summer of Code 10# 2005 program. 11# 12# Redistribution and use in source and binary forms, with or without 13# modification, are permitted provided that the following conditions 14# are met: 15# 1. Redistributions of source code must retain the above copyright 16# notice, this list of conditions and the following disclaimer. 17# 2. Redistributions in binary form must reproduce the above copyright 18# notice, this list of conditions and the following disclaimer in the 19# documentation and/or other materials provided with the distribution. 20# 21# THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 22# ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 23# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 24# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 25# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 26# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 27# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 28# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 29# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 30# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 31# POSSIBILITY OF SUCH DAMAGE. 32# 33# $FreeBSD$ 34# 35 36# 37# Helper functions for tests written in shell script. 38# 39 40Prog_Name=${0##*/} 41Src_Dir=$(pwd) 42Unprived_User= 43Verbose=2 44Work_Dir=$(pwd)/tmp 45 46# ------------------------------------------------------------------------- 47 48# die 49# 50# Called by tests when a command fails unexpectedly. Terminates 51# execution and tries to clean up the mount point. 52# 53die() { 54 if [ -d ${Work_Dir} ]; then 55 cd ${Src_Dir} 56 umount ${Work_Dir} 57 rmdir ${Work_Dir} 58 fi 59 [ ${Verbose} -eq 2 ] && err "Test ended unexpectedly" 60 [ ${Verbose} -eq 1 ] && echo " failed." 61 exit 1 62} 63 64# ------------------------------------------------------------------------- 65 66# err message 67# 68# Shows the given error message and terminates the program. 69# 70err() { 71 echo "${Prog_Name}: $*" 1>&2 72 exit 1 73} 74 75# ------------------------------------------------------------------------- 76 77# test_mount [args] 78# 79# Mounts tmpfs over ${Work_Dir} and changes the current directory 80# to the mount point. Optional arguments may be passed to the 81# mount command. 82# 83test_mount() { 84 mkdir ${Work_Dir} || die 85 if [ $# -gt 0 ]; then 86 mount -t tmpfs "$@" tmpfs ${Work_Dir} || die 87 else 88 mount -t tmpfs tmpfs ${Work_Dir} || die 89 fi 90 cd ${Work_Dir} 91} 92 93# ------------------------------------------------------------------------- 94 95# test_name message 96# 97# Prints a message about what a test is going to do. 98# 99test_name() { 100 [ ${Verbose} -gt 1 ] && echo " $*..." 101} 102 103# ------------------------------------------------------------------------- 104 105# test_unmount 106# 107# Unmounts the file system mounted by test_mount. 108# 109test_unmount() { 110 cd - 111 umount ${Work_Dir} || die 112 rmdir ${Work_Dir} || die 113} 114 115# ------------------------------------------------------------------------- 116 117# kqueue_monitor expected_nevents file1 [.. fileN] 118# 119# Monitors the commands given through stdin (one per line) using 120# kqueue and stores the events raised in a log that can be later 121# verified with kqueue_check. 122# 123kqueue_monitor() { 124 nev=${1}; shift 125 test_name "Running kqueue-monitored commands and expecting" \ 126 "${nev} events" 127 ${Src_Dir}/h_tools kqueue ${*} >kqueue.log || return 1 128 got=$(wc -l kqueue.log | awk '{ print $1 }') 129 test ${got} -eq ${nev} 130} 131 132# ------------------------------------------------------------------------- 133 134# kqueue_check file event 135# 136# Checks if kqueue raised the given event when monitoring the 137# given file. 138# 139kqueue_check() { 140 grep "^${1} - ${2}$" kqueue.log >/dev/null 141} 142 143# ------------------------------------------------------------------------- 144 145main() { 146 local args 147 148 [ $(id -un) = root ] || err "Must be run as root" 149 150 args=$(getopt u:v:w: $*) 151 if [ $? -ne 0 ]; then 152 echo "Usage: ${Prog_Name} [-u unprived_user] [-v level] " \ 153 "[-w root_dir]" 1>&2 154 return 1 155 fi 156 set -- ${args} 157 while [ $# -gt 0 ]; do 158 case "$1" in 159 -u) 160 Unprived_User="$2"; shift 161 ;; 162 -v) 163 Verbose="$2"; shift 164 ;; 165 -w) 166 Work_Dir="$2"; shift 167 ;; 168 --) 169 shift; break 170 ;; 171 esac 172 shift 173 done 174 175 [ ${Verbose} -eq 1 ] && echo -n "${Prog_Name}:" 176 [ ${Verbose} -eq 2 ] && echo "${Prog_Name}: Running tests" 177 test_run 178 [ ${Verbose} -eq 1 ] && echo " ok." 179 [ ${Verbose} -eq 2 ] && echo "${Prog_Name}: All tests were successful" 180 181 return 0 182} 183 184main "$@" 185