xref: /freebsd/contrib/netbsd-tests/usr.bin/grep/t_grep.sh (revision 1070477cc8b7a88b1fd2b1ba810a1fff761799a1)
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 -rS 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 zgrep_combined_flags
218zgrep_combined_flags_head()
219{
220	atf_set "descr" "Checks for zgrep wrapper problems with combined flags (PR 247126)"
221}
222zgrep_combined_flags_body()
223{
224	echo 'foo bar' > test
225
226	atf_check -o inline:"foo bar\n" zgrep -we foo test
227	# Avoid hang on reading from stdin in the failure case
228	atf_check -o inline:"foo bar\n" zgrep -wefoo test < /dev/null
229}
230
231atf_test_case zgrep_eflag
232zgrep_eflag_head()
233{
234	atf_set "descr" "Checks for zgrep wrapper problems with -e PATTERN (PR 247126)"
235}
236zgrep_eflag_body()
237{
238	echo 'foo bar' > test
239
240	# Avoid hang on reading from stdin in the failure case
241	atf_check -o inline:"foo bar\n" zgrep -e 'foo bar' test < /dev/null
242	atf_check -o inline:"foo bar\n" zgrep --regexp='foo bar' test < /dev/null
243}
244
245atf_test_case zgrep_fflag
246zgrep_fflag_head()
247{
248	atf_set "descr" "Checks for zgrep wrapper problems with -f FILE (PR 247126)"
249}
250zgrep_fflag_body()
251{
252	echo foo > pattern
253	echo foobar > test
254
255	# Avoid hang on reading from stdin in the failure case
256	atf_check -o inline:"foobar\n" zgrep -f pattern test </dev/null
257	atf_check -o inline:"foobar\n" zgrep --file=pattern test </dev/null
258}
259
260atf_test_case zgrep_long_eflag
261zgrep_long_eflag_head()
262{
263	atf_set "descr" "Checks for zgrep wrapper problems with --ignore-case reading from stdin (PR 247126)"
264}
265zgrep_long_eflag_body()
266{
267	echo foobar > test
268
269	atf_check -o inline:"foobar\n" zgrep -e foo --ignore-case < test
270}
271
272atf_test_case zgrep_multiple_eflags
273zgrep_multiple_eflags_head()
274{
275	atf_set "descr" "Checks for zgrep wrapper problems with multiple -e flags (PR 247126)"
276}
277zgrep_multiple_eflags_body()
278{
279	echo foobar > test
280
281	atf_check -o inline:"foobar\n" zgrep -e foo -e xxx test
282}
283
284atf_test_case zgrep_empty_eflag
285zgrep_empty_eflag_head()
286{
287	atf_set "descr" "Checks for zgrep wrapper problems with empty -e flags pattern (PR 247126)"
288}
289zgrep_empty_eflag_body()
290{
291	echo foobar > test
292
293	atf_check -o inline:"foobar\n" zgrep -e '' test
294}
295
296atf_test_case nonexistent
297nonexistent_head()
298{
299	atf_set "descr" "Checks that -s flag suppresses error" \
300	                "messages about nonexistent files"
301}
302nonexistent_body()
303{
304	atf_check -s ne:0 grep -s foobar nonexistent
305}
306
307atf_test_case context2
308context2_head()
309{
310	atf_set "descr" "Checks displaying context with -z flag"
311}
312context2_body()
313{
314	printf "haddock\000cod\000plaice\000" > test1
315	printf "mackeral\000cod\000crab\000" > test2
316
317	atf_check -o file:"$(atf_get_srcdir)/d_context2_a.out" \
318	    grep -z -A1 cod test1 test2
319
320	atf_check -o file:"$(atf_get_srcdir)/d_context2_b.out" \
321	    grep -z -B1 cod test1 test2
322
323	atf_check -o file:"$(atf_get_srcdir)/d_context2_c.out" \
324	    grep -z -C1 cod test1 test2
325}
326# Begin FreeBSD
327
328# What grep(1) are we working with?
329# - 0 : bsdgrep
330# - 1 : gnu grep 2.51 (base)
331# - 2 : gnu grep (ports)
332GREP_TYPE_BSD=0
333GREP_TYPE_GNU_FREEBSD=1
334GREP_TYPE_GNU=2
335GREP_TYPE_UNKNOWN=3
336
337grep_type()
338{
339	local grep_version=$(grep --version)
340
341	case "$grep_version" in
342	*"BSD grep"*)
343		return $GREP_TYPE_BSD
344		;;
345	*"GNU grep"*)
346		case "$grep_version" in
347		*2.5.1-FreeBSD*)
348			return $GREP_TYPE_GNU_FREEBSD
349			;;
350		*)
351			return $GREP_TYPE_GNU
352			;;
353		esac
354		;;
355	esac
356	atf_fail "unknown grep type: $grep_version"
357}
358
359atf_test_case oflag_zerolen
360oflag_zerolen_head()
361{
362	atf_set "descr" "Check behavior of zero-length matches with -o flag (PR 195763)"
363}
364oflag_zerolen_body()
365{
366	grep_type
367	if [ $? -eq $GREP_TYPE_GNU_FREEBSD ]; then
368		atf_expect_fail "this test doesn't pass with gnu grep in base"
369	fi
370
371	atf_check -o file:"$(atf_get_srcdir)/d_oflag_zerolen_a.out" \
372	    grep -Eo '(^|:)0*' "$(atf_get_srcdir)/d_oflag_zerolen_a.in"
373
374	atf_check -o file:"$(atf_get_srcdir)/d_oflag_zerolen_b.out" \
375	    grep -Eo '(^|:)0*' "$(atf_get_srcdir)/d_oflag_zerolen_b.in"
376
377	atf_check -o file:"$(atf_get_srcdir)/d_oflag_zerolen_c.out" \
378	    grep -Eo '[[:alnum:]]*' "$(atf_get_srcdir)/d_oflag_zerolen_c.in"
379
380	atf_check -o empty grep -Eo '' "$(atf_get_srcdir)/d_oflag_zerolen_d.in"
381
382	atf_check -o file:"$(atf_get_srcdir)/d_oflag_zerolen_e.out" \
383	    grep -o -e 'ab' -e 'bc' "$(atf_get_srcdir)/d_oflag_zerolen_e.in"
384
385	atf_check -o file:"$(atf_get_srcdir)/d_oflag_zerolen_e.out" \
386	    grep -o -e 'bc' -e 'ab' "$(atf_get_srcdir)/d_oflag_zerolen_e.in"
387}
388
389atf_test_case xflag
390xflag_head()
391{
392	atf_set "descr" "Check that we actually get a match with -x flag (PR 180990)"
393}
394xflag_body()
395{
396	echo 128 > match_file
397	seq 1 128 > pattern_file
398	grep -xf pattern_file match_file
399}
400
401atf_test_case color
402color_head()
403{
404	atf_set "descr" "Check --color support"
405}
406color_body()
407{
408	grep_type
409	if [ $? -eq $GREP_TYPE_GNU_FREEBSD ]; then
410		atf_expect_fail "this test doesn't pass with gnu grep in base"
411	fi
412
413	echo 'abcd*' > grepfile
414	echo 'abc$' >> grepfile
415	echo '^abc' >> grepfile
416
417	atf_check -o file:"$(atf_get_srcdir)/d_color_a.out" \
418	    grep --color=auto -e '.*' -e 'a' "$(atf_get_srcdir)/d_color_a.in"
419
420	atf_check -o file:"$(atf_get_srcdir)/d_color_b.out" \
421	    grep --color=auto -f grepfile "$(atf_get_srcdir)/d_color_b.in"
422
423	atf_check -o file:"$(atf_get_srcdir)/d_color_c.out" \
424	    grep --color=always -f grepfile "$(atf_get_srcdir)/d_color_b.in"
425}
426
427atf_test_case f_file_empty
428f_file_empty_head()
429{
430	atf_set "descr" "Check for handling of a null byte in empty file, specified by -f (PR 202022)"
431}
432f_file_empty_body()
433{
434	printf "\0\n" > nulpat
435
436	atf_check -s exit:1 grep -f nulpat "$(atf_get_srcdir)/d_f_file_empty.in"
437}
438
439atf_test_case escmap
440escmap_head()
441{
442	atf_set "descr" "Check proper handling of escaped vs. unescaped dot expressions (PR 175314)"
443}
444escmap_body()
445{
446	atf_check -s exit:1 grep -o 'f.o\.' "$(atf_get_srcdir)/d_escmap.in"
447	atf_check -o not-empty grep -o 'f.o.' "$(atf_get_srcdir)/d_escmap.in"
448}
449
450atf_test_case egrep_empty_invalid
451egrep_empty_invalid_head()
452{
453	atf_set "descr" "Check for handling of an invalid empty pattern (PR 194823)"
454}
455egrep_empty_invalid_body()
456{
457	atf_check -e ignore -s not-exit:0 egrep '{' /dev/null
458}
459
460atf_test_case zerolen
461zerolen_head()
462{
463	atf_set "descr" "Check for successful zero-length matches with ^$"
464}
465zerolen_body()
466{
467	printf "Eggs\n\nCheese" > test1
468
469	atf_check -o inline:"\n" grep -e "^$" test1
470
471	atf_check -o inline:"Eggs\nCheese\n" grep -v -e "^$" test1
472}
473
474atf_test_case wflag_emptypat
475wflag_emptypat_head()
476{
477	atf_set "descr" "Check for proper handling of -w with an empty pattern (PR 105221)"
478}
479wflag_emptypat_body()
480{
481	printf "" > test1
482	printf "\n" > test2
483	printf "qaz" > test3
484	printf " qaz\n" > test4
485
486	atf_check -s exit:1 -o empty grep -w -e "" test1
487
488	atf_check -o file:test2 grep -vw -e "" test2
489
490	atf_check -s exit:1 -o empty grep -w -e "" test3
491
492	atf_check -o file:test4 grep -vw -e "" test4
493}
494
495atf_test_case xflag_emptypat
496xflag_emptypat_body()
497{
498	printf "" > test1
499	printf "\n" > test2
500	printf "qaz" > test3
501	printf " qaz\n" > test4
502
503	atf_check -s exit:1 -o empty grep -x -e "" test1
504
505	atf_check -o file:test2 grep -x -e "" test2
506
507	atf_check -s exit:1 -o empty grep -x -e "" test3
508
509	atf_check -s exit:1 -o empty grep -x -e "" test4
510
511	total=$(wc -l /COPYRIGHT | sed 's/[^0-9]//g')
512
513	# Simple checks that grep -x with an empty pattern isn't matching every
514	# line.  The exact counts aren't important, as long as they don't
515	# match the total line count and as long as they don't match each other.
516	atf_check -o save:xpositive.count grep -Fxc '' /COPYRIGHT
517	atf_check -o save:xnegative.count grep -Fvxc '' /COPYRIGHT
518
519	atf_check -o not-inline:"${total}" cat xpositive.count
520	atf_check -o not-inline:"${total}" cat xnegative.count
521
522	atf_check -o not-file:xnegative.count cat xpositive.count
523}
524
525atf_test_case xflag_emptypat_plus
526xflag_emptypat_plus_body()
527{
528	printf "foo\n\nbar\n\nbaz\n" > target
529	printf "foo\n \nbar\n \nbaz\n" > target_spacelines
530	printf "foo\nbar\nbaz\n" > matches
531	printf " \n \n" > spacelines
532
533	printf "foo\n\nbar\n\nbaz\n" > patlist1
534	printf "foo\n\nba\n\nbaz\n" > patlist2
535
536	sed -e '/bar/d' target > matches_not2
537
538	# Normal handling first
539	atf_check -o file:target grep -Fxf patlist1 target
540	atf_check -o file:matches grep -Fxf patlist1 target_spacelines
541	atf_check -o file:matches_not2 grep -Fxf patlist2 target
542
543	# -v handling
544	atf_check -s exit:1 -o empty grep -Fvxf patlist1 target
545	atf_check -o file:spacelines grep -Fxvf patlist1 target_spacelines
546}
547
548atf_test_case emptyfile
549emptyfile_descr()
550{
551	atf_set "descr" "Check for proper handling of empty pattern files (PR 253209)"
552}
553emptyfile_body()
554{
555	:> epatfile
556	echo "blubb" > subj
557
558	# From PR 253209, bsdgrep was short-circuiting completely on an empty
559	# file, but we should have still been processing lines.
560	atf_check -s exit:1 -o empty fgrep -f epatfile subj
561	atf_check -o file:subj fgrep -vf epatfile subj
562}
563
564atf_test_case excessive_matches
565excessive_matches_head()
566{
567	atf_set "descr" "Check for proper handling of lines with excessive matches (PR 218811)"
568}
569excessive_matches_body()
570{
571	grep_type
572	if [ $? -eq $GREP_TYPE_GNU_FREEBSD ]; then
573		atf_expect_fail "this test does not pass with GNU grep in base"
574	fi
575
576	for i in $(jot 4096); do
577		printf "x" >> test.in
578	done
579
580	atf_check -s exit:0 -x '[ $(grep -o x test.in | wc -l) -eq 4096 ]'
581	atf_check -s exit:1 -x 'grep -on x test.in | grep -v "1:x"'
582}
583
584atf_test_case fgrep_sanity
585fgrep_sanity_head()
586{
587	atf_set "descr" "Check for fgrep sanity, literal expressions only"
588}
589fgrep_sanity_body()
590{
591	printf "Foo" > test1
592
593	atf_check -o inline:"Foo\n" fgrep -e "Foo" test1
594
595	atf_check -s exit:1 -o empty fgrep -e "Fo." test1
596}
597
598atf_test_case egrep_sanity
599egrep_sanity_head()
600{
601	atf_set "descr" "Check for egrep sanity, EREs only"
602}
603egrep_sanity_body()
604{
605	printf "Foobar(ed)" > test1
606	printf "M{1}" > test2
607
608	atf_check -o inline:"Foo\n" egrep -o -e "F.." test1
609
610	atf_check -o inline:"Foobar\n" egrep -o -e "F[a-z]*" test1
611
612	atf_check -o inline:"Fo\n" egrep -o -e "F(o|p)" test1
613
614	atf_check -o inline:"(ed)\n" egrep -o -e "\(ed\)" test1
615
616	atf_check -o inline:"M\n" egrep -o -e "M{1}" test2
617
618	atf_check -o inline:"M{1}\n" egrep -o -e "M\{1\}" test2
619}
620
621atf_test_case grep_sanity
622grep_sanity_head()
623{
624	atf_set "descr" "Check for basic grep sanity, BREs only"
625}
626grep_sanity_body()
627{
628	printf "Foobar(ed)" > test1
629	printf "M{1}" > test2
630
631	atf_check -o inline:"Foo\n" grep -o -e "F.." test1
632
633	atf_check -o inline:"Foobar\n" grep -o -e "F[a-z]*" test1
634
635	atf_check -o inline:"Fo\n" grep -o -e "F\(o\)" test1
636
637	atf_check -o inline:"(ed)\n" grep -o -e "(ed)" test1
638
639	atf_check -o inline:"M{1}\n" grep -o -e "M{1}" test2
640
641	atf_check -o inline:"M\n" grep -o -e "M\{1\}" test2
642}
643
644atf_test_case wv_combo_break
645wv_combo_break_head()
646{
647	atf_set "descr" "Check for incorrectly matching lines with both -w and -v flags (PR 218467)"
648}
649wv_combo_break_body()
650{
651	printf "x xx\n" > test1
652	printf "xx x\n" > test2
653
654	atf_check -o file:test1 grep -w "x" test1
655	atf_check -o file:test2 grep -w "x" test2
656
657	atf_check -s exit:1 grep -v -w "x" test1
658	atf_check -s exit:1 grep -v -w "x" test2
659}
660
661atf_test_case ocolor_metadata
662ocolor_metadata_head()
663{
664	atf_set "descr" "Check for -n/-b producing per-line metadata output"
665}
666ocolor_metadata_body()
667{
668	grep_type
669	if [ $? -eq $GREP_TYPE_GNU_FREEBSD ]; then
670		atf_expect_fail "this test does not pass with GNU grep in base"
671	fi
672
673	printf "xxx\nyyyy\nzzz\nfoobarbaz\n" > test1
674	check_expr="^[^:]*[0-9][^:]*:[^:]+$"
675
676	atf_check -o inline:"1:1:xx\n" grep -bon "xx$" test1
677
678	atf_check -o inline:"2:4:yyyy\n" grep -bn "yy" test1
679
680	atf_check -o inline:"2:6:yy\n" grep -bon "yy$" test1
681
682	# These checks ensure that grep isn't producing bogus line numbering
683	# in the middle of a line.
684	atf_check -s exit:1 -x \
685	    "grep -Eon 'x|y|z|f' test1 | grep -Ev '${check_expr}'"
686
687	atf_check -s exit:1 -x \
688	    "grep -En 'x|y|z|f' --color=always test1 | grep -Ev '${check_expr}'"
689
690	atf_check -s exit:1 -x \
691	    "grep -Eon 'x|y|z|f' --color=always test1 | grep -Ev '${check_expr}'"
692}
693
694atf_test_case grep_nomatch_flags
695grep_nomatch_flags_head()
696{
697	atf_set "descr" "Check for no match (-c, -l, -L, -q) flags not producing line matches or context (PR 219077)"
698}
699
700grep_nomatch_flags_body()
701{
702	grep_type
703
704	if [ $? -eq $GREP_TYPE_GNU_FREEBSD ]; then
705		atf_expect_fail "this test does not pass with GNU grep in base"
706	fi
707
708	printf "A\nB\nC\n" > test1
709
710	atf_check -o inline:"1\n" grep -c -C 1 -e "B" test1
711	atf_check -o inline:"1\n" grep -c -B 1 -e "B" test1
712	atf_check -o inline:"1\n" grep -c -A 1 -e "B" test1
713	atf_check -o inline:"1\n" grep -c -C 1 -e "B" test1
714
715	atf_check -o inline:"test1\n" grep -l -e "B" test1
716	atf_check -o inline:"test1\n" grep -l -B 1 -e "B" test1
717	atf_check -o inline:"test1\n" grep -l -A 1 -e "B" test1
718	atf_check -o inline:"test1\n" grep -l -C 1 -e "B" test1
719
720	atf_check -o inline:"test1\n" grep -L -e "D" test1
721
722	atf_check -o empty grep -q -e "B" test1
723	atf_check -o empty grep -q -B 1 -e "B" test1
724	atf_check -o empty grep -q -A 1 -e "B" test1
725	atf_check -o empty grep -q -C 1 -e "B" test1
726}
727
728atf_test_case badcontext
729badcontext_head()
730{
731	atf_set "descr" "Check for handling of invalid context arguments"
732}
733badcontext_body()
734{
735	printf "A\nB\nC\n" > test1
736
737	atf_check -s not-exit:0 -e ignore grep -A "-1" "B" test1
738
739	atf_check -s not-exit:0 -e ignore grep -B "-1" "B" test1
740
741	atf_check -s not-exit:0 -e ignore grep -C "-1" "B" test1
742
743	atf_check -s not-exit:0 -e ignore grep -A "B" "B" test1
744
745	atf_check -s not-exit:0 -e ignore grep -B "B" "B" test1
746
747	atf_check -s not-exit:0 -e ignore grep -C "B" "B" test1
748}
749
750atf_test_case binary_flags
751binary_flags_head()
752{
753	atf_set "descr" "Check output for binary flags (-a, -I, -U, --binary-files)"
754}
755binary_flags_body()
756{
757	printf "A\000B\000C" > test1
758	printf "A\n\000B\n\000C" > test2
759	binmatchtext="Binary file test1 matches\n"
760
761	# Binaries not treated as text (default, -U)
762	atf_check -o inline:"${binmatchtext}" grep 'B' test1
763	atf_check -o inline:"${binmatchtext}" grep 'B' -C 1 test1
764
765	atf_check -o inline:"${binmatchtext}" grep -U 'B' test1
766	atf_check -o inline:"${binmatchtext}" grep -U 'B' -C 1 test1
767
768	# Binary, -a, no newlines
769	atf_check -o inline:"A\000B\000C\n" grep -a 'B' test1
770	atf_check -o inline:"A\000B\000C\n" grep -a 'B' -C 1 test1
771
772	# Binary, -a, newlines
773	atf_check -o inline:"\000B\n" grep -a 'B' test2
774	atf_check -o inline:"A\n\000B\n\000C\n" grep -a 'B' -C 1 test2
775
776	# Binary files ignored
777	atf_check -s exit:1 grep -I 'B' test2
778
779	# --binary-files equivalence
780	atf_check -o inline:"${binmatchtext}" grep --binary-files=binary 'B' test1
781	atf_check -o inline:"A\000B\000C\n" grep --binary-files=text 'B' test1
782	atf_check -s exit:1 grep --binary-files=without-match 'B' test2
783}
784
785atf_test_case mmap
786mmap_head()
787{
788	atf_set "descr" "Check basic matching with --mmap flag"
789}
790mmap_body()
791{
792	grep_type
793	if [ $? -eq $GREP_TYPE_GNU ]; then
794		atf_expect_fail "gnu grep from ports has no --mmap option"
795	fi
796
797	printf "A\nB\nC\n" > test1
798
799	atf_check -s exit:0 -o inline:"B\n" grep --mmap -oe "B" test1
800	atf_check -s exit:1 grep --mmap -e "Z" test1
801}
802
803atf_test_case matchall
804matchall_head()
805{
806	atf_set "descr" "Check proper behavior of matching all with an empty string"
807}
808matchall_body()
809{
810	printf "" > test1
811	printf "A" > test2
812	printf "A\nB" > test3
813
814	atf_check -o inline:"test2:A\ntest3:A\ntest3:B\n" grep "" test1 test2 test3
815	atf_check -o inline:"test3:A\ntest3:B\ntest2:A\n" grep "" test3 test1 test2
816	atf_check -o inline:"test2:A\ntest3:A\ntest3:B\n" grep "" test2 test3 test1
817
818	atf_check -s exit:1 grep "" test1
819}
820
821atf_test_case fgrep_multipattern
822fgrep_multipattern_head()
823{
824	atf_set "descr" "Check proper behavior with multiple patterns supplied to fgrep"
825}
826fgrep_multipattern_body()
827{
828	printf "Foo\nBar\nBaz" > test1
829
830	atf_check -o inline:"Foo\nBaz\n" grep -F -e "Foo" -e "Baz" test1
831	atf_check -o inline:"Foo\nBaz\n" grep -F -e "Baz" -e "Foo" test1
832	atf_check -o inline:"Bar\nBaz\n" grep -F -e "Bar" -e "Baz" test1
833}
834
835atf_test_case fgrep_icase
836fgrep_icase_head()
837{
838	atf_set "descr" "Check proper handling of -i supplied to fgrep"
839}
840fgrep_icase_body()
841{
842	printf "Foo\nBar\nBaz" > test1
843
844	atf_check -o inline:"Foo\nBaz\n" grep -Fi -e "foo" -e "baz" test1
845	atf_check -o inline:"Foo\nBaz\n" grep -Fi -e "baz" -e "foo" test1
846	atf_check -o inline:"Bar\nBaz\n" grep -Fi -e "bar" -e "baz" test1
847	atf_check -o inline:"Bar\nBaz\n" grep -Fi -e "BAR" -e "bAz" test1
848}
849
850atf_test_case fgrep_oflag
851fgrep_oflag_head()
852{
853	atf_set "descr" "Check proper handling of -o supplied to fgrep"
854}
855fgrep_oflag_body()
856{
857	printf "abcdefghi\n" > test1
858
859	atf_check -o inline:"a\n" grep -Fo "a" test1
860	atf_check -o inline:"i\n" grep -Fo "i" test1
861	atf_check -o inline:"abc\n" grep -Fo "abc" test1
862	atf_check -o inline:"fgh\n" grep -Fo "fgh" test1
863	atf_check -o inline:"cde\n" grep -Fo "cde" test1
864	atf_check -o inline:"bcd\n" grep -Fo -e "bcd" -e "cde" test1
865	atf_check -o inline:"bcd\nefg\n" grep -Fo -e "bcd" -e "efg" test1
866
867	atf_check -s exit:1 grep -Fo "xabc" test1
868	atf_check -s exit:1 grep -Fo "abcx" test1
869	atf_check -s exit:1 grep -Fo "xghi" test1
870	atf_check -s exit:1 grep -Fo "ghix" test1
871	atf_check -s exit:1 grep -Fo "abcdefghiklmnopqrstuvwxyz" test1
872}
873
874atf_test_case cflag
875cflag_head()
876{
877	atf_set "descr" "Check proper handling of -c"
878}
879cflag_body()
880{
881	printf "a\nb\nc\n" > test1
882
883	atf_check -o inline:"1\n" grep -Ec "a" test1
884	atf_check -o inline:"2\n" grep -Ec "a|b" test1
885	atf_check -o inline:"3\n" grep -Ec "a|b|c" test1
886
887	atf_check -o inline:"test1:2\n" grep -EHc "a|b" test1
888}
889
890atf_test_case mflag
891mflag_head()
892{
893	atf_set "descr" "Check proper handling of -m"
894}
895mflag_body()
896{
897	printf "a\nb\nc\nd\ne\nf\n" > test1
898
899	atf_check -o inline:"1\n" grep -m 1 -Ec "a" test1
900	atf_check -o inline:"2\n" grep -m 2 -Ec "a|b" test1
901	atf_check -o inline:"3\n" grep -m 3 -Ec "a|b|c|f" test1
902
903	atf_check -o inline:"test1:2\n" grep -m 2 -EHc "a|b|e|f" test1
904}
905
906atf_test_case mflag_trail_ctx
907mflag_trail_ctx_head()
908{
909	atf_set "descr" "Check proper handling of -m with trailing context (PR 253350)"
910}
911mflag_trail_ctx_body()
912{
913	printf "foo\nfoo\nbar\nfoo\nbar\nfoo\nbar\n" > test1
914
915	# Should pick up the next line after matching the first.
916	atf_check -o inline:"foo\nfoo\n" grep -A1 -m1 foo test1
917
918	# Make sure the trailer is picked up as a non-match!
919	atf_check -o inline:"1:foo\n2-foo\n" grep -A1 -nm1 foo test1
920}
921
922atf_test_case zgrep_multiple_files
923zgrep_multiple_files_head()
924{
925	atf_set "descr" "Ensures that zgrep functions properly with multiple files"
926}
927zgrep_multiple_files_body()
928{
929	echo foo > test1
930	echo foo > test2
931	atf_check -o inline:"test1:foo\ntest2:foo\n" zgrep foo test1 test2
932
933	echo bar > test1
934	atf_check -o inline:"test2:foo\n" zgrep foo test1 test2
935
936	echo bar > test2
937	atf_check -s exit:1 zgrep foo test1 test2
938}
939
940atf_test_case zgrep_recursive
941zgrep_multiple_files_head()
942{
943	atf_set "descr" "Checks for zgrep wrapper recursion"
944}
945zgrep_recursive_body()
946{
947	atf_expect_fail "unimplemented zgrep wrapper script functionality"
948
949	mkdir -p tester1
950	echo foobar > tester1/test
951	atf_check -o inline:"tester1/test:foobar\n" zgrep -r foo tester1
952
953	mkdir -p tester2
954	echo foobar > tester2/test1
955	echo foobar > tester2/test2
956	atf_check -o inline:"tester2/test1:foobar\ntester2/test2:foobar\n" zgrep -r foo tester2
957}
958# End FreeBSD
959
960atf_init_test_cases()
961{
962	atf_add_test_case basic
963	atf_add_test_case binary
964	atf_add_test_case recurse
965	atf_add_test_case recurse_symlink
966	atf_add_test_case word_regexps
967	atf_add_test_case begin_end
968	atf_add_test_case ignore_case
969	atf_add_test_case invert
970	atf_add_test_case whole_line
971	atf_add_test_case negative
972	atf_add_test_case context
973	atf_add_test_case file_exp
974	atf_add_test_case egrep
975	atf_add_test_case zgrep
976	atf_add_test_case zgrep_combined_flags
977	atf_add_test_case zgrep_eflag
978	atf_add_test_case zgrep_empty_eflag
979	atf_add_test_case zgrep_fflag
980	atf_add_test_case zgrep_long_eflag
981	atf_add_test_case zgrep_multiple_eflags
982	atf_add_test_case nonexistent
983	atf_add_test_case context2
984# Begin FreeBSD
985	atf_add_test_case oflag_zerolen
986	atf_add_test_case xflag
987	atf_add_test_case color
988	atf_add_test_case f_file_empty
989	atf_add_test_case escmap
990	atf_add_test_case egrep_empty_invalid
991	atf_add_test_case zerolen
992	atf_add_test_case wflag_emptypat
993	atf_add_test_case xflag_emptypat
994	atf_add_test_case xflag_emptypat_plus
995	atf_add_test_case emptyfile
996	atf_add_test_case excessive_matches
997	atf_add_test_case wv_combo_break
998	atf_add_test_case fgrep_sanity
999	atf_add_test_case egrep_sanity
1000	atf_add_test_case grep_sanity
1001	atf_add_test_case ocolor_metadata
1002	atf_add_test_case grep_nomatch_flags
1003	atf_add_test_case binary_flags
1004	atf_add_test_case badcontext
1005	atf_add_test_case mmap
1006	atf_add_test_case matchall
1007	atf_add_test_case fgrep_multipattern
1008	atf_add_test_case fgrep_icase
1009	atf_add_test_case fgrep_oflag
1010	atf_add_test_case cflag
1011	atf_add_test_case mflag
1012	atf_add_test_case mflag_trail_ctx
1013	atf_add_test_case zgrep_multiple_files
1014	atf_add_test_case zgrep_recursive
1015# End FreeBSD
1016}
1017