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