xref: /freebsd/contrib/netbsd-tests/usr.bin/grep/t_grep.sh (revision e3514747256465c52c3b2aedc9795f52c0d3efe9)
1# $NetBSD: t_grep.sh,v 1.3 2017/01/14 20:43:52 christos Exp $
2#
3# Copyright (c) 2008, 2009 The NetBSD Foundation, Inc.
4# All rights reserved.
5#
6# Redistribution and use in source and binary forms, with or without
7# modification, are permitted provided that the following conditions
8# are met:
9# 1. Redistributions of source code must retain the above copyright
10#    notice, this list of conditions and the following disclaimer.
11# 2. Redistributions in binary form must reproduce the above copyright
12#    notice, this list of conditions and the following disclaimer in the
13#    documentation and/or other materials provided with the distribution.
14#
15# THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
16# ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
17# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18# PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
19# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25# POSSIBILITY OF SUCH DAMAGE.
26#
27
28atf_test_case basic
29basic_head()
30{
31	atf_set "descr" "Checks basic functionality"
32}
33basic_body()
34{
35	atf_check -o file:"$(atf_get_srcdir)/d_basic.out" -x \
36	    'jot 10000 | grep 123'
37}
38
39atf_test_case binary
40binary_head()
41{
42	atf_set "descr" "Checks handling of binary files"
43}
44binary_body()
45{
46	dd if=/dev/zero count=1 of=test.file
47	echo -n "foobar" >> test.file
48	atf_check -o file:"$(atf_get_srcdir)/d_binary.out" grep foobar test.file
49}
50
51atf_test_case recurse
52recurse_head()
53{
54	atf_set "descr" "Checks recursive searching"
55}
56recurse_body()
57{
58	mkdir -p recurse/a/f recurse/d
59	echo -e "cod\ndover sole\nhaddock\nhalibut\npilchard" > recurse/d/fish
60	echo -e "cod\nhaddock\nplaice" > recurse/a/f/favourite-fish
61
62	atf_check -o file:"$(atf_get_srcdir)/d_recurse.out" -x "grep -r haddock recurse | sort"
63}
64
65atf_test_case recurse_symlink
66recurse_symlink_head()
67{
68	atf_set "descr" "Checks symbolic link recursion"
69}
70recurse_symlink_body()
71{
72	# Begin FreeBSD
73	grep_type
74	if [ $? -eq $GREP_TYPE_GNU ]; then
75		atf_expect_fail "this test doesn't pass with gnu grep from ports"
76	fi
77	# End FreeBSD
78	mkdir -p test/c/d
79	(cd test/c/d && ln -s ../d .)
80	echo "Test string" > test/c/match
81
82	atf_check -o file:"$(atf_get_srcdir)/d_recurse_symlink.out" \
83	    -e file:"$(atf_get_srcdir)/d_recurse_symlink.err" \
84	    grep -r string test
85}
86
87atf_test_case word_regexps
88word_regexps_head()
89{
90	atf_set "descr" "Checks word-regexps"
91}
92word_regexps_body()
93{
94	atf_check -o file:"$(atf_get_srcdir)/d_word_regexps.out" \
95	    grep -w separated $(atf_get_srcdir)/d_input
96
97	# Begin FreeBSD
98	printf "xmatch pmatch\n" > test1
99
100	atf_check -o inline:"pmatch\n" grep -Eow "(match )?pmatch" test1
101	# End FreeBSD
102}
103
104atf_test_case begin_end
105begin_end_head()
106{
107	atf_set "descr" "Checks handling of line beginnings and ends"
108}
109begin_end_body()
110{
111	atf_check -o file:"$(atf_get_srcdir)/d_begin_end_a.out" \
112	    grep ^Front "$(atf_get_srcdir)/d_input"
113
114	atf_check -o file:"$(atf_get_srcdir)/d_begin_end_b.out" \
115	    grep ending$ "$(atf_get_srcdir)/d_input"
116}
117
118atf_test_case ignore_case
119ignore_case_head()
120{
121	atf_set "descr" "Checks ignore-case option"
122}
123ignore_case_body()
124{
125	atf_check -o file:"$(atf_get_srcdir)/d_ignore_case.out" \
126	    grep -i Upper "$(atf_get_srcdir)/d_input"
127}
128
129atf_test_case invert
130invert_head()
131{
132	atf_set "descr" "Checks selecting non-matching lines with -v option"
133}
134invert_body()
135{
136	atf_check -o file:"$(atf_get_srcdir)/d_invert.out" \
137	    grep -v fish "$(atf_get_srcdir)/d_invert.in"
138}
139
140atf_test_case whole_line
141whole_line_head()
142{
143	atf_set "descr" "Checks whole-line matching with -x flag"
144}
145whole_line_body()
146{
147	atf_check -o file:"$(atf_get_srcdir)/d_whole_line.out" \
148	    grep -x matchme "$(atf_get_srcdir)/d_input"
149}
150
151atf_test_case negative
152negative_head()
153{
154	atf_set "descr" "Checks handling of files with no matches"
155}
156negative_body()
157{
158	atf_check -s ne:0 grep "not a hope in hell" "$(atf_get_srcdir)/d_input"
159}
160
161atf_test_case context
162context_head()
163{
164	atf_set "descr" "Checks displaying context with -A, -B and -C flags"
165}
166context_body()
167{
168	cp $(atf_get_srcdir)/d_context_*.* .
169
170	atf_check -o file:d_context_a.out grep -C2 bamboo d_context_a.in
171	atf_check -o file:d_context_b.out grep -A3 tilt d_context_a.in
172	atf_check -o file:d_context_c.out grep -B4 Whig d_context_a.in
173	atf_check -o file:d_context_d.out grep -C1 pig d_context_a.in d_context_b.in
174	atf_check -o file:d_context_e.out \
175	    grep -E -C1 '(banana|monkey)' d_context_e.in
176	atf_check -o file:d_context_f.out \
177	    grep -Ev -B2 '(banana|monkey|fruit)' d_context_e.in
178	atf_check -o file:d_context_g.out \
179	    grep -Ev -A1 '(banana|monkey|fruit)' d_context_e.in
180}
181
182atf_test_case file_exp
183file_exp_head()
184{
185	atf_set "descr" "Checks reading expressions from file"
186}
187file_exp_body()
188{
189	atf_check -o file:"$(atf_get_srcdir)/d_file_exp.out" -x \
190	    'jot 21 -1 1.00 | grep -f '"$(atf_get_srcdir)"'/d_file_exp.in'
191}
192
193atf_test_case egrep
194egrep_head()
195{
196	atf_set "descr" "Checks matching special characters with egrep"
197}
198egrep_body()
199{
200	atf_check -o file:"$(atf_get_srcdir)/d_egrep.out" \
201		egrep '\?|\*$$' "$(atf_get_srcdir)/d_input"
202}
203
204atf_test_case zgrep
205zgrep_head()
206{
207	atf_set "descr" "Checks handling of gzipped files with zgrep"
208}
209zgrep_body()
210{
211	cp "$(atf_get_srcdir)/d_input" .
212	gzip d_input || atf_fail "gzip failed"
213
214	atf_check -o file:"$(atf_get_srcdir)/d_zgrep.out" zgrep -h line d_input.gz
215}
216
217atf_test_case nonexistent
218nonexistent_head()
219{
220	atf_set "descr" "Checks that -s flag suppresses error" \
221	                "messages about nonexistent files"
222}
223nonexistent_body()
224{
225	atf_check -s ne:0 grep -s foobar nonexistent
226}
227
228atf_test_case context2
229context2_head()
230{
231	atf_set "descr" "Checks displaying context with -z flag"
232}
233context2_body()
234{
235	printf "haddock\000cod\000plaice\000" > test1
236	printf "mackeral\000cod\000crab\000" > test2
237
238	atf_check -o file:"$(atf_get_srcdir)/d_context2_a.out" \
239	    grep -z -A1 cod test1 test2
240
241	atf_check -o file:"$(atf_get_srcdir)/d_context2_b.out" \
242	    grep -z -B1 cod test1 test2
243
244	atf_check -o file:"$(atf_get_srcdir)/d_context2_c.out" \
245	    grep -z -C1 cod test1 test2
246}
247# Begin FreeBSD
248
249# What grep(1) are we working with?
250# - 0 : bsdgrep
251# - 1 : gnu grep 2.51 (base)
252# - 2 : gnu grep (ports)
253GREP_TYPE_BSD=0
254GREP_TYPE_GNU_FREEBSD=1
255GREP_TYPE_GNU=2
256GREP_TYPE_UNKNOWN=3
257
258grep_type()
259{
260	local grep_version=$(grep --version)
261
262	case "$grep_version" in
263	*"BSD grep"*)
264		return $GREP_TYPE_BSD
265		;;
266	*"GNU grep"*)
267		case "$grep_version" in
268		*2.5.1-FreeBSD*)
269			return $GREP_TYPE_GNU_FREEBSD
270			;;
271		*)
272			return $GREP_TYPE_GNU
273			;;
274		esac
275		;;
276	esac
277	atf_fail "unknown grep type: $grep_version"
278}
279
280atf_test_case oflag_zerolen
281oflag_zerolen_head()
282{
283	atf_set "descr" "Check behavior of zero-length matches with -o flag (PR 195763)"
284}
285oflag_zerolen_body()
286{
287	grep_type
288	if [ $? -eq $GREP_TYPE_GNU_FREEBSD ]; then
289		atf_expect_fail "this test doesn't pass with gnu grep in base"
290	fi
291
292	atf_check -o file:"$(atf_get_srcdir)/d_oflag_zerolen_a.out" \
293	    grep -Eo '(^|:)0*' "$(atf_get_srcdir)/d_oflag_zerolen_a.in"
294
295	atf_check -o file:"$(atf_get_srcdir)/d_oflag_zerolen_b.out" \
296	    grep -Eo '(^|:)0*' "$(atf_get_srcdir)/d_oflag_zerolen_b.in"
297
298	atf_check -o file:"$(atf_get_srcdir)/d_oflag_zerolen_c.out" \
299	    grep -Eo '[[:alnum:]]*' "$(atf_get_srcdir)/d_oflag_zerolen_c.in"
300
301	atf_check -o empty grep -Eo '' "$(atf_get_srcdir)/d_oflag_zerolen_d.in"
302
303	atf_check -o file:"$(atf_get_srcdir)/d_oflag_zerolen_e.out" \
304	    grep -o -e 'ab' -e 'bc' "$(atf_get_srcdir)/d_oflag_zerolen_e.in"
305
306	atf_check -o file:"$(atf_get_srcdir)/d_oflag_zerolen_e.out" \
307	    grep -o -e 'bc' -e 'ab' "$(atf_get_srcdir)/d_oflag_zerolen_e.in"
308}
309
310atf_test_case xflag
311xflag_head()
312{
313	atf_set "descr" "Check that we actually get a match with -x flag (PR 180990)"
314}
315xflag_body()
316{
317	echo 128 > match_file
318	seq 1 128 > pattern_file
319	grep -xf pattern_file match_file
320}
321
322atf_test_case color
323color_head()
324{
325	atf_set "descr" "Check --color support"
326}
327color_body()
328{
329	grep_type
330	if [ $? -eq $GREP_TYPE_GNU_FREEBSD ]; then
331		atf_expect_fail "this test doesn't pass with gnu grep in base"
332	fi
333
334	echo 'abcd*' > grepfile
335	echo 'abc$' >> grepfile
336	echo '^abc' >> grepfile
337
338	atf_check -o file:"$(atf_get_srcdir)/d_color_a.out" \
339	    grep --color=auto -e '.*' -e 'a' "$(atf_get_srcdir)/d_color_a.in"
340
341	atf_check -o file:"$(atf_get_srcdir)/d_color_b.out" \
342	    grep --color=auto -f grepfile "$(atf_get_srcdir)/d_color_b.in"
343
344	atf_check -o file:"$(atf_get_srcdir)/d_color_c.out" \
345	    grep --color=always -f grepfile "$(atf_get_srcdir)/d_color_b.in"
346}
347
348atf_test_case f_file_empty
349f_file_empty_head()
350{
351	atf_set "descr" "Check for handling of a null byte in empty file, specified by -f (PR 202022)"
352}
353f_file_empty_body()
354{
355	printf "\0\n" > nulpat
356
357	atf_check -s exit:1 grep -f nulpat "$(atf_get_srcdir)/d_f_file_empty.in"
358}
359
360atf_test_case escmap
361escmap_head()
362{
363	atf_set "descr" "Check proper handling of escaped vs. unescaped dot expressions (PR 175314)"
364}
365escmap_body()
366{
367	atf_check -s exit:1 grep -o 'f.o\.' "$(atf_get_srcdir)/d_escmap.in"
368	atf_check -o not-empty grep -o 'f.o.' "$(atf_get_srcdir)/d_escmap.in"
369}
370
371atf_test_case egrep_empty_invalid
372egrep_empty_invalid_head()
373{
374	atf_set "descr" "Check for handling of an invalid empty pattern (PR 194823)"
375}
376egrep_empty_invalid_body()
377{
378	atf_check -e ignore -s not-exit:0 egrep '{' /dev/null
379}
380
381atf_test_case zerolen
382zerolen_head()
383{
384	atf_set "descr" "Check for successful zero-length matches with ^$"
385}
386zerolen_body()
387{
388	printf "Eggs\n\nCheese" > test1
389
390	atf_check -o inline:"\n" grep -e "^$" test1
391
392	atf_check -o inline:"Eggs\nCheese\n" grep -v -e "^$" test1
393}
394
395atf_test_case wflag_emptypat
396wflag_emptypat_head()
397{
398	atf_set "descr" "Check for proper handling of -w with an empty pattern (PR 105221)"
399}
400wflag_emptypat_body()
401{
402	grep_type
403	if [ $? -eq $GREP_TYPE_GNU_FREEBSD ]; then
404		atf_expect_fail "this test does not pass with GNU grep in base"
405	fi
406
407	printf "" > test1
408	printf "\n" > test2
409	printf "qaz" > test3
410	printf " qaz\n" > test4
411
412	atf_check -s exit:1 -o empty grep -w -e "" test1
413
414	atf_check -o file:test2 grep -w -e "" test2
415
416	atf_check -s exit:1 -o empty grep -w -e "" test3
417
418	atf_check -o file:test4 grep -w -e "" test4
419}
420
421atf_test_case fgrep_sanity
422fgrep_sanity_head()
423{
424	atf_set "descr" "Check for fgrep sanity, literal expressions only"
425}
426fgrep_sanity_body()
427{
428	printf "Foo" > test1
429
430	atf_check -o inline:"Foo\n" fgrep -e "Foo" test1
431
432	atf_check -s exit:1 -o empty fgrep -e "Fo." test1
433}
434
435atf_test_case egrep_sanity
436egrep_sanity_head()
437{
438	atf_set "descr" "Check for egrep sanity, EREs only"
439}
440egrep_sanity_body()
441{
442	printf "Foobar(ed)" > test1
443	printf "M{1}" > test2
444
445	atf_check -o inline:"Foo\n" egrep -o -e "F.." test1
446
447	atf_check -o inline:"Foobar\n" egrep -o -e "F[a-z]*" test1
448
449	atf_check -o inline:"Fo\n" egrep -o -e "F(o|p)" test1
450
451	atf_check -o inline:"(ed)\n" egrep -o -e "\(ed\)" test1
452
453	atf_check -o inline:"M\n" egrep -o -e "M{1}" test2
454
455	atf_check -o inline:"M{1}\n" egrep -o -e "M\{1\}" test2
456}
457
458atf_test_case grep_sanity
459grep_sanity_head()
460{
461	atf_set "descr" "Check for basic grep sanity, BREs only"
462}
463grep_sanity_body()
464{
465	printf "Foobar(ed)" > test1
466	printf "M{1}" > test2
467
468	atf_check -o inline:"Foo\n" grep -o -e "F.." test1
469
470	atf_check -o inline:"Foobar\n" grep -o -e "F[a-z]*" test1
471
472	atf_check -o inline:"Fo\n" grep -o -e "F\(o\)" test1
473
474	atf_check -o inline:"(ed)\n" grep -o -e "(ed)" test1
475
476	atf_check -o inline:"M{1}\n" grep -o -e "M{1}" test2
477
478	atf_check -o inline:"M\n" grep -o -e "M\{1\}" test2
479}
480
481atf_test_case wv_combo_break
482wv_combo_break_head()
483{
484	atf_set "descr" "Check for incorrectly matching lines with both -w and -v flags (PR 218467)"
485}
486wv_combo_break_body()
487{
488	printf "x xx\n" > test1
489	printf "xx x\n" > test2
490
491	atf_check -o file:test1 grep -w "x" test1
492	atf_check -o file:test2 grep -w "x" test2
493
494	atf_check -s exit:1 grep -v -w "x" test1
495	atf_check -s exit:1 grep -v -w "x" test2
496}
497# End FreeBSD
498
499atf_init_test_cases()
500{
501	atf_add_test_case basic
502	atf_add_test_case binary
503	atf_add_test_case recurse
504	atf_add_test_case recurse_symlink
505	atf_add_test_case word_regexps
506	atf_add_test_case begin_end
507	atf_add_test_case ignore_case
508	atf_add_test_case invert
509	atf_add_test_case whole_line
510	atf_add_test_case negative
511	atf_add_test_case context
512	atf_add_test_case file_exp
513	atf_add_test_case egrep
514	atf_add_test_case zgrep
515	atf_add_test_case nonexistent
516	atf_add_test_case context2
517# Begin FreeBSD
518	atf_add_test_case oflag_zerolen
519	atf_add_test_case xflag
520	atf_add_test_case color
521	atf_add_test_case f_file_empty
522	atf_add_test_case escmap
523	atf_add_test_case egrep_empty_invalid
524	atf_add_test_case zerolen
525	atf_add_test_case wflag_emptypat
526	atf_add_test_case wv_combo_break
527	atf_add_test_case fgrep_sanity
528	atf_add_test_case egrep_sanity
529	atf_add_test_case grep_sanity
530# End FreeBSD
531}
532