xref: /illumos-gate/usr/src/test/util-tests/tests/grep_xpg4/grep_test.ksh (revision ffb6483089015eb90be1f5e7fc2a96c9929546a6)
1#! /usr/bin/ksh
2#
3#
4# This file and its contents are supplied under the terms of the
5# Common Development and Distribution License ("CDDL"), version 1.0.
6# You may only use this file in accordance with the terms of version
7# 1.0 of the CDDL.
8#
9# A full copy of the text of the CDDL should have accompanied this
10# source.  A copy of the CDDL is also available via the Internet at
11# http://www.illumos.org/license/CDDL.
12#
13
14#
15# Copyright 2017 Nexenta Systems, Inc. All rights reserved.
16# Copyright 2020 Peter Tribble.
17# Copyright 2020 Oxide Computer Company
18#
19
20XGREP=${XGREP:=/usr/bin/grep}
21FILEDIR=$MY_TESTS/tests/files/grep
22OUTFILE=/tmp/grep_test.out.$$
23FLAGSFILE=/tmp/grep_flags.$$
24
25#
26# Set the tests to a known multi-byte locale to start with.
27#
28export LC_ALL=C.UTF-8
29
30fail() {
31	echo $1
32	exit -1
33}
34
35test_fail() {
36	echo "$*"
37	((failures++))
38}
39
40#
41# Run through a set of tests once for each value in $FLAGSFILE. Arguments are:
42# - expected exit status
43# - the file pass this is
44# - the remaining arguments to grep
45#
46run_tests() {
47	i=0
48	exp=$1
49	shift
50	pass=$1
51	shift
52
53	echo "$FLAGS" > $FLAGSFILE
54	while read flags; do
55		difffile="gout.$pass.$i"
56		print -n "test $total: grep $flags: "
57		((total++))
58		((i++))
59		$XGREP $flags "$@" > $OUTFILE
60		err=$?
61		if [[ $err -ne $exp ]]; then
62			test_fail "failed on exit: $err"
63			continue
64		fi
65
66		if [[ $exp -eq 0 ]]; then
67			if [[ ! -f "$difffile" ]]; then
68				test_fail "missing output file $difffile"
69				continue
70			fi
71
72			if [[ -n "$(diff $OUTFILE $difffile)" ]]; then
73				print "$(diff $OUTFILE $difffile)"
74				test_fail "output is different from $difffile"
75				continue
76			fi
77		fi
78		echo "passed"
79	done < $FLAGSFILE
80}
81
82run_tests_stdin() {
83	i=0
84	exp=$1
85	pass=$2
86	pattern=$3
87	file=$4
88
89	echo "$FLAGS" > $FLAGSFILE
90	while read flags; do
91		difffile="gout.$pass.$i"
92		print -n "test $total: grep (stdin) $flags: "
93		((total++))
94		((i++))
95		$XGREP $flags $pattern < $file > $OUTFILE
96		err=$?
97		if [[ $err -ne $exp ]]; then
98			test_fail "failed on exit: $err"
99			continue
100		fi
101
102		if [[ $exp -eq 0 ]]; then
103			if [[ ! -f "$difffile" ]]; then
104				test_fail "missing output file $difffile"
105				continue
106			fi
107
108			if [[ -n "$(diff $OUTFILE $difffile)" ]]; then
109				print "$(diff $OUTFILE $difffile)"
110				test_fail "output is different"
111				continue
112			fi
113		fi
114		echo "passed"
115	done < $FLAGSFILE
116}
117total=0
118failures=0
119
120FLAGS="
121-n
122-c
123-q
124-v
125-nv
126-vc
127-A 5
128-nA 5
129-cA 5
130-qA 5
131-vA 5
132-nvA 5
133-vcA 5
134-B 5
135-nB 5
136-cB 5
137-qB 5
138-vB 5
139-nvB 5
140-vcB 5
141-C 5
142-nC 5
143-cC 5
144-qC 5
145-vC 5
146-nvC 5
147-vcC 5
148-B 5 -A 2
149-nB 5 -A 2
150-cB 5 -A 2
151-qB 5 -A 2
152-vB 5 -A 2
153-nvB 5 -A 2
154-vcB 5 -A 2
155-B 5 -A 2 -C 5
156-nB 5 -A 2 -C 5
157-cB 5 -A 2 -C 5
158-qB 5 -A 2 -C 5
159-vB 5 -A 2 -C 5
160-nvB 5 -A 2 -C 5
161-vcB 5 -A 2 -C 5
162-5
163-n -5
164-c -5
165-q -5
166-v -5
167-nv -5
168-vc -5
169-50000
170-n -50000
171-c -50000
172-q -50000
173-v -50000
174-nv -50000
175-vc -50000
176-C 5 -B 4 -A 2
177-nC 5 -B 4 -A 2
178-cC 5 -B 4 -A 2
179-qC 5 -B 4 -A 2
180-vC 5 -B 4 -A 2
181-nvC 5 -B 4 -A 2
182-vcC 5 -B 4 -A 2
183-b
184-bh
185-bn
186-bnh
187-ib
188-ibnH
189-b -C 1
190-bn -C 1
191-o
192-no
193-cno
194-bo
195-bno
196-cbno
197-ho
198-xo
199-o -C 3"
200
201cd $FILEDIR || fail "failed to cd to $FILEDIR"
202
203#
204# Test Pass 1: General flags with a basic pattern, checked against all
205# three primary forms of grep: grep, egrep, and fgrep. We do this in two
206# different passes. In one we're in a default multi-byte locale and in
207# the other we're in a single-byte locale. This ensures that we get
208# basic coverage of the use_bmg path in grep.
209#
210run_tests 0 t1 a test0 test1 test2 test3 test4 test5 test6 test7
211run_tests 0 t1 -E a test0 test1 test2 test3 test4 test5 test6 test7
212run_tests 0 t1 -F a test0 test1 test2 test3 test4 test5 test6 test7
213export LC_ALL=C
214run_tests 0 t1 a test0 test1 test2 test3 test4 test5 test6 test7
215run_tests 0 t1 -E a test0 test1 test2 test3 test4 test5 test6 test7
216run_tests 0 t1 -F a test0 test1 test2 test3 test4 test5 test6 test7
217export LC_ALL=C.UTF-8
218
219FLAGS="-nE"
220run_tests 0 t2 ".*" testnl
221
222FLAGS="-B 1
223-vA 1
224-vB 1"
225run_tests 0 t3 a testnl
226
227FLAGS="-h
228-H"
229run_tests 0 t4 a test0
230
231# need a directory with predictable contents
232rm -fr /tmp/test0
233mkdir /tmp/test0
234cp test0 /tmp/test0
235
236FLAGS="-r
237-hr"
238run_tests 0 t5 a /tmp/test0
239rm -rf /tmp/test0
240
241#
242# Test Group 6: Test -l and -L which are supposed to match the file or
243# not. We break this into cases that should always pass and those that
244# should fail. The first group should always pass.
245#
246FLAGS="-l
247-L
248-il
249-vl
250-vil
251-hl
252-hL
253-Hl
254-HL
255-cl
256-cL
257-nl
258-nL
259-l -A5 -B5
260-L -C5
261-nHvl
262-l -L -l
263-L -l
264-l -L
265-L -l -L"
266run_tests 0 t6 foo test.lL.0 test.lL.1
267
268#
269# Test Group 7: -l and -L variants that should cause us to not match or
270# fail for another reason.
271#
272FLAGS="-vL
273-viL
274-nHvL"
275run_tests 1 t7 foo test.lL.0 test.lL.1
276
277#
278# Test Group 8, 9: Here we are testing the ability for --label to
279# properly replace the name of a file from standard in, but not
280# replacing that of a file that's normally specified. Set 8 verifies the
281# former while set 9 verifies the latter.
282#
283FLAGS="-H
284-H --label=zelda
285--label=zelda
286-h --label=zelda
287-n --label=zelda
288-H -n --label=zelda"
289run_tests_stdin 0 t8 a test0
290run_tests 0 t9 a test0
291
292#
293# Test group 10, using multiple regexps.
294#
295FLAGS="-o
296-E -o
297-o -n
298-E -o -n
299-o -n -b
300-E -o -n -b
301-E -o -n -H
302-E -o -n -H -b"
303run_tests 0 t10 -e '[fF]oo' -e 's[df]+;' test.o.0
304
305#
306# Test group 11, using multibyte files.
307#
308OLDLOC=$LC_ALL
309export LC_ALL=C.UTF-8
310FLAGS="
311-o
312-n
313-n -o
314-H -n -o
315-l
316-c"
317run_tests 0 t11 -e ï -e à test.mb.0
318export LC_ALL=$OLDLOC
319
320#
321# Clean up temporary files.
322#
323rm -f $FLAGSFILE $OUTFILE
324
325if [[ "$failures" -ne 0 ]]; then
326	printf "%u tests failed\n" "$failures" 2>&1
327	exit -1
328fi
329exit 0
330