summaryrefslogtreecommitdiffstats
path: root/plugins/sudoers/regress/harness.in
blob: 9aa6e76775921e821fc7302e52434daae608ff5f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
#!/bin/sh
#
# Copyright (c) 2022 Todd C. Miller <Todd.Miller@sudo.ws>
#
# Permission to use, copy, modify, and distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#
# Simple test harness for sudoers tests.
# usage: harness [-v] test_group [test_name ...]
#
srcdir="@abs_srcdir@"
builddir="@abs_builddir@"
SHELL=@SHELL@
verbose=0
rval=0
ntests=0
errors=0

umask 022

if [ "$1" = "-v" ]; then
    verbose=1
    shift
fi

if [ $# -eq 0 ]; then
    echo "usage: harness test_group [test_name ...]" >&2
    exit 1
fi
group="$1"
shift
srcdir=${srcdir%"/regress"}
builddir=${builddir%"/regress"}

if [ ! -d "$srcdir/regress/$group" ]; then
    echo "missing test group: regress/$group" >&2
    exit 1
fi

case "$group" in
sudoers)
    mkdir -p "$builddir/regress/$group"
    if [ $# -eq 0 ]; then
	tests=
	for t in $srcdir/regress/$group/*.in; do
	    tests="$tests `basename $t .in`"
	done
	set -- $tests
    fi
    while [ $# -ne 0 ]; do
	test="$1"
	shift
	in="$srcdir/regress/sudoers/${test}.in"
	out="$builddir/regress/sudoers/${test}.out"
	out_ok="$srcdir/regress/sudoers/${test}.out.ok"
	toke="$builddir/regress/sudoers/${test}.toke"
	toke_ok="$srcdir/regress/sudoers/${test}.toke.ok"
	json="$builddir/regress/sudoers/${test}.json"
	json_ok="$srcdir/regress/sudoers/${test}.json.ok"
	ldif="$builddir/regress/sudoers/${test}.ldif"
	ldif_ok="$srcdir/regress/sudoers/${test}.ldif.ok"
	ldif2sudo="$builddir/regress/sudoers/${test}.ldif2sudo"
	ldif2sudo_ok="$srcdir/regress/sudoers/${test}.ldif2sudo.ok"
	sudo="$builddir/regress/sudoers/${test}.sudo"

	$builddir/testsudoers -dt <$in >$out 2>$toke || true
	ntests=`expr $ntests + 1`
	if cmp $out $out_ok >/dev/null; then
	    if [ $verbose -eq 1 ]; then
		echo "$group/$test (parse): OK"
	    fi
	else
	    errors=`expr $errors + 1`
	    echo "$group/$test (parse): FAIL"
	    diff $out $out_ok || true
	fi
	ntests=`expr $ntests + 1`
	if cmp $toke $toke_ok >/dev/null; then
	    if [ $verbose -eq 1 ]; then
		echo "$group/$test (toke): OK"
	    fi
	else
	    errors=`expr $errors + 1`
	    echo "$group/$test (toke): FAIL"
	    diff $toke $toke_ok || true
	fi

	$builddir/cvtsudoers -c "" -f json $in >$json 2>/dev/null || true
	ntests=`expr $ntests + 1`
	if cmp $json $json_ok >/dev/null; then
	    if [ $verbose -eq 1 ]; then
		echo "$group/$test (json): OK"
	    fi
	else
	    errors=`expr $errors + 1`
	    echo "$group/$test (json): FAIL"
	    diff $json $json_ok || true
	fi

	SUDOERS_BASE="ou=SUDOers,dc=sudo,dc=ws" \
	    $builddir/cvtsudoers -c "" -f ldif < $in >$ldif 2>/dev/null || true
	ntests=`expr $ntests + 1`
	if cmp $ldif $ldif_ok >/dev/null; then
	    if [ $verbose -eq 1 ]; then
		echo "$group/$test (ldif): OK"
	    fi
	else
	    errors=`expr $errors + 1`
	    echo "$group/$test: (ldif) FAIL"
	    diff $ldif $ldif_ok || true
	fi

	$builddir/cvtsudoers -c "" -f sudoers $in >$sudo 2>/dev/null || true
	ntests=`expr $ntests + 1`
	if $builddir/visudo -qcf $sudo; then
	    if [ $verbose -eq 1 ]; then
		echo "$group/$test (reparse): OK"
	    fi
	else
	    errors=`expr $errors + 1`
	    echo "$group/$test: (reparse) FAIL"
	    $builddir/visudo -cf $sudo || true
	fi

	if test -s $ldif_ok; then
	    $builddir/cvtsudoers -c "" -i ldif -f sudoers $ldif_ok >$ldif2sudo || true
	    ntests=`expr $ntests + 1`
	    if cmp $ldif2sudo $ldif2sudo_ok >/dev/null; then
		if [ $verbose -eq 1 ]; then
		    echo "$group/$test (ldif2sudo): OK"
		fi
	    else
		errors=`expr $errors + 1`
		echo "$group/$test: (ldif2sudo) FAIL"
		diff $ldif $ldif_ok || true
	    fi
	fi
    done
    ${AWK-awk} -v group=$group -v ntests=$ntests -v errors=$errors \
	'END {printf("%s: %d tests run, %d errors, %d%% success rate\n", group, ntests, errors, (ntests - errors) * 100 / ntests)}' < /dev/null
    if test $errors -ne 0; then
	rval=`expr $rval + $errors`
    fi
    ;;
*)
    TESTSUDOERS=$builddir/testsudoers; export TESTSUDOERS
    VISUDO=$builddir/visudo; export VISUDO
    CVTSUDOERS=$builddir/cvtsudoers; export CVTSUDOERS
    mkdir -p "regress/$group"
    if [ $# -eq 0 ]; then
	tests=
	for t in $srcdir/regress/$group/*.sh; do
	    tests="$tests `basename $t .sh`"
	done
	set -- $tests
    fi
    while [ $# -ne 0 ]; do
	test="$1"
	shift

	cmd="$srcdir/regress/$group/${test}.sh"
	out="$builddir/regress/$group/${test}.out"
	out_ok="$srcdir/regress/$group/${test}.out.ok"
	err="$builddir/regress/$group/${test}.err"
	err_ok="$srcdir/regress/$group/${test}.err.ok"
	status=0
	TESTDIR=$srcdir/regress/$group $SHELL $cmd >$out 2>$err || status=$?
	ntests=`expr $ntests + 1`
	if cmp $out $out_ok >/dev/null; then
	    if test $status -eq 0; then
		if [ $verbose -eq 1 ]; then
		    echo "$group/$test: OK"
		fi
	    else
		errors=`expr $errors + 1`
		echo "$group/$test (exit $status): FAIL"
	    fi
	else
	    errors=`expr $errors + 1`
	    echo "$group/$test: FAIL"
	    diff $out $out_ok || true
	fi
	ntests=`expr $ntests + 1`
	if test -s $err_ok; then
	    if cmp $err $err_ok >/dev/null; then
		if [ $verbose -eq 1 ]; then
		    echo "$group/$test (stderr): OK"
		fi
	    else
		errors=`expr $errors + 1`
		echo "$group/$test (stderr): FAIL"
		diff $err $err_ok || true
	    fi
	elif test -s $err; then
	    errors=`expr $errors + 1`
	    echo "$group/$test (stderr): FAIL"
	    cat $err 1>&2
	fi
    done
    ${AWK-awk} -v group=$group -v ntests=$ntests -v errors=$errors \
	'END {printf("%s: %d tests run, %d errors, %d%% success rate\n", group, ntests, errors, (ntests - errors) * 100 / ntests)}' < /dev/null
    if test $errors -ne 0; then
	rval=`expr $rval + $errors`
    fi
    ;;
esac

exit $rval