summaryrefslogtreecommitdiff
path: root/usr/src/test/util-tests/tests/grep_xpg4/grep_test.ksh
blob: 8724cbcb01339a2009bd1855e0290ad9cb39310a (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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
#! /usr/bin/ksh
#
#
# This file and its contents are supplied under the terms of the
# Common Development and Distribution License ("CDDL"), version 1.0.
# You may only use this file in accordance with the terms of version
# 1.0 of the CDDL.
#
# A full copy of the text of the CDDL should have accompanied this
# source.  A copy of the CDDL is also available via the Internet at
# http://www.illumos.org/license/CDDL.
#

#
# Copyright 2017 Nexenta Systems, Inc. All rights reserved.
# Copyright 2020 Peter Tribble.
# Copyright 2020 Oxide Computer Company
#

XGREP=${XGREP:=/usr/bin/grep}

MY_TESTS=${MY_TESTS:=/opt/util-tests}
FILEDIR=$MY_TESTS/tests/files/grep
OUTFILE=/tmp/grep_test.out.$$
FLAGSFILE=/tmp/grep_flags.$$

#
# Set the tests to a known multi-byte locale to start with.
#
export LC_ALL=C.UTF-8

fail() {
	echo $1
	exit -1
}

test_fail() {
	echo "$*"
	((failures++))
}

#
# Run through a set of tests once for each value in $FLAGSFILE. Arguments are:
# - expected exit status
# - the file pass this is
# - the remaining arguments to grep
#
run_tests() {
	i=0
	exp=$1
	shift
	pass=$1
	shift

	echo "$FLAGS" > $FLAGSFILE
	while read flags; do
		difffile="gout.$pass.$i"
		print -n "test $total: grep $flags: "
		((total++))
		((i++))
		$XGREP $flags "$@" > $OUTFILE
		err=$?
		if [[ $err -ne $exp ]]; then
			test_fail "failed on exit: $err"
			continue
		fi

		if [[ $exp -eq 0 ]]; then
			if [[ ! -f "$difffile" ]]; then
				test_fail "missing output file $difffile"
				continue
			fi

			if [[ -n "$(diff $OUTFILE $difffile)" ]]; then
				print "$(diff $OUTFILE $difffile)"
				test_fail "output is different from $difffile"
				continue
			fi
		fi
		echo "passed"
	done < $FLAGSFILE
}

run_tests_stdin() {
	i=0
	exp=$1
	pass=$2
	pattern=$3
	file=$4

	echo "$FLAGS" > $FLAGSFILE
	while read flags; do
		difffile="gout.$pass.$i"
		print -n "test $total: grep (stdin) $flags: "
		((total++))
		((i++))
		$XGREP $flags $pattern < $file > $OUTFILE
		err=$?
		if [[ $err -ne $exp ]]; then
			test_fail "failed on exit: $err"
			continue
		fi

		if [[ $exp -eq 0 ]]; then
			if [[ ! -f "$difffile" ]]; then
				test_fail "missing output file $difffile"
				continue
			fi

			if [[ -n "$(diff $OUTFILE $difffile)" ]]; then
				print "$(diff $OUTFILE $difffile)"
				test_fail "output is different"
				continue
			fi
		fi
		echo "passed"
	done < $FLAGSFILE
}
total=0
failures=0

FLAGS="
-n
-c
-q
-v
-nv
-vc
-A 5
-nA 5
-cA 5
-qA 5
-vA 5
-nvA 5
-vcA 5
-B 5
-nB 5
-cB 5
-qB 5
-vB 5
-nvB 5
-vcB 5
-C 5
-nC 5
-cC 5
-qC 5
-vC 5
-nvC 5
-vcC 5
-B 5 -A 2
-nB 5 -A 2
-cB 5 -A 2
-qB 5 -A 2
-vB 5 -A 2
-nvB 5 -A 2
-vcB 5 -A 2
-B 5 -A 2 -C 5
-nB 5 -A 2 -C 5
-cB 5 -A 2 -C 5
-qB 5 -A 2 -C 5
-vB 5 -A 2 -C 5
-nvB 5 -A 2 -C 5
-vcB 5 -A 2 -C 5
-5
-n -5
-c -5
-q -5
-v -5
-nv -5
-vc -5
-50000
-n -50000
-c -50000
-q -50000
-v -50000
-nv -50000
-vc -50000
-C 5 -B 4 -A 2
-nC 5 -B 4 -A 2
-cC 5 -B 4 -A 2
-qC 5 -B 4 -A 2
-vC 5 -B 4 -A 2
-nvC 5 -B 4 -A 2
-vcC 5 -B 4 -A 2
-b
-bh
-bn
-bnh
-ib
-ibnH
-b -C 1
-bn -C 1
-o
-no
-cno
-bo
-bno
-cbno
-ho
-xo
-o -C 3"

cd $FILEDIR || fail "failed to cd to $FILEDIR"

#
# Test Pass 1: General flags with a basic pattern, checked against all
# three primary forms of grep: grep, egrep, and fgrep. We do this in two
# different passes. In one we're in a default multi-byte locale and in
# the other we're in a single-byte locale. This ensures that we get
# basic coverage of the use_bmg path in grep.
#
run_tests 0 t1 a test0 test1 test2 test3 test4 test5 test6 test7
run_tests 0 t1 -E a test0 test1 test2 test3 test4 test5 test6 test7
run_tests 0 t1 -F a test0 test1 test2 test3 test4 test5 test6 test7
export LC_ALL=C
run_tests 0 t1 a test0 test1 test2 test3 test4 test5 test6 test7
run_tests 0 t1 -E a test0 test1 test2 test3 test4 test5 test6 test7
run_tests 0 t1 -F a test0 test1 test2 test3 test4 test5 test6 test7
export LC_ALL=C.UTF-8

FLAGS="-nE"
run_tests 0 t2 ".*" testnl

FLAGS="-B 1
-vA 1
-vB 1"
run_tests 0 t3 a testnl

FLAGS="-h
-H"
run_tests 0 t4 a test0

# need a directory with predictable contents
rm -fr /tmp/test0
mkdir /tmp/test0
cp test0 /tmp/test0

FLAGS="-r
-hr"
run_tests 0 t5 a /tmp/test0
rm -rf /tmp/test0

#
# Test Group 6: Test -l and -L which are supposed to match the file or
# not. We break this into cases that should always pass and those that
# should fail. The first group should always pass.
#
FLAGS="-l
-L
-il
-vl
-vil
-hl
-hL
-Hl
-HL
-cl
-cL
-nl
-nL
-l -A5 -B5
-L -C5
-nHvl
-l -L -l
-L -l
-l -L
-L -l -L"
run_tests 0 t6 foo test.lL.0 test.lL.1

#
# Test Group 7: -l and -L variants that should cause us to not match or
# fail for another reason.
#
FLAGS="-vL
-viL
-nHvL"
run_tests 1 t7 foo test.lL.0 test.lL.1

#
# Test Group 8, 9: Here we are testing the ability for --label to
# properly replace the name of a file from standard in, but not
# replacing that of a file that's normally specified. Set 8 verifies the
# former while set 9 verifies the latter.
#
FLAGS="-H
-H --label=zelda
--label=zelda
-h --label=zelda
-n --label=zelda
-H -n --label=zelda"
run_tests_stdin 0 t8 a test0
run_tests 0 t9 a test0

#
# Test group 10, using multiple regexps.
#
FLAGS="-o
-E -o
-o -n
-E -o -n
-o -n -b
-E -o -n -b
-E -o -n -H
-E -o -n -H -b"
run_tests 0 t10 -e '[fF]oo' -e 's[df]+;' test.o.0

#
# Test group 11, using multibyte files.
#
OLDLOC=$LC_ALL
export LC_ALL=C.UTF-8
FLAGS="
-o
-n
-n -o
-H -n -o
-l
-c"
run_tests 0 t11 -e ï -e à test.mb.0
export LC_ALL=$OLDLOC

#
# Clean up temporary files.
#
rm -f $FLAGSFILE $OUTFILE

if [[ "$failures" -ne 0 ]]; then
	printf "%u tests failed\n" "$failures" 2>&1
	exit -1
fi
exit 0