xref: /freebsd/contrib/netbsd-tests/usr.bin/grep/t_grep.sh (revision 1070477cc8b7a88b1fd2b1ba810a1fff761799a1)
1f40f3adcSEnji Cooper# $NetBSD: t_grep.sh,v 1.3 2017/01/14 20:43:52 christos Exp $
257718be8SEnji Cooper#
357718be8SEnji Cooper# Copyright (c) 2008, 2009 The NetBSD Foundation, Inc.
457718be8SEnji Cooper# All rights reserved.
557718be8SEnji Cooper#
657718be8SEnji Cooper# Redistribution and use in source and binary forms, with or without
757718be8SEnji Cooper# modification, are permitted provided that the following conditions
857718be8SEnji Cooper# are met:
957718be8SEnji Cooper# 1. Redistributions of source code must retain the above copyright
1057718be8SEnji Cooper#    notice, this list of conditions and the following disclaimer.
1157718be8SEnji Cooper# 2. Redistributions in binary form must reproduce the above copyright
1257718be8SEnji Cooper#    notice, this list of conditions and the following disclaimer in the
1357718be8SEnji Cooper#    documentation and/or other materials provided with the distribution.
1457718be8SEnji Cooper#
1557718be8SEnji Cooper# THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
1657718be8SEnji Cooper# ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
1757718be8SEnji Cooper# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
1857718be8SEnji Cooper# PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
1957718be8SEnji Cooper# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2057718be8SEnji Cooper# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2157718be8SEnji Cooper# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2257718be8SEnji Cooper# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
2357718be8SEnji Cooper# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
2457718be8SEnji Cooper# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
2557718be8SEnji Cooper# POSSIBILITY OF SUCH DAMAGE.
2657718be8SEnji Cooper#
2757718be8SEnji Cooper
2857718be8SEnji Cooperatf_test_case basic
2957718be8SEnji Cooperbasic_head()
3057718be8SEnji Cooper{
3157718be8SEnji Cooper	atf_set "descr" "Checks basic functionality"
3257718be8SEnji Cooper}
3357718be8SEnji Cooperbasic_body()
3457718be8SEnji Cooper{
3557718be8SEnji Cooper	atf_check -o file:"$(atf_get_srcdir)/d_basic.out" -x \
3657718be8SEnji Cooper	    'jot 10000 | grep 123'
3757718be8SEnji Cooper}
3857718be8SEnji Cooper
3957718be8SEnji Cooperatf_test_case binary
4057718be8SEnji Cooperbinary_head()
4157718be8SEnji Cooper{
4257718be8SEnji Cooper	atf_set "descr" "Checks handling of binary files"
4357718be8SEnji Cooper}
4457718be8SEnji Cooperbinary_body()
4557718be8SEnji Cooper{
461115e598SEnji Cooper	dd if=/dev/zero count=1 of=test.file
471115e598SEnji Cooper	echo -n "foobar" >> test.file
481115e598SEnji Cooper	atf_check -o file:"$(atf_get_srcdir)/d_binary.out" grep foobar test.file
4957718be8SEnji Cooper}
5057718be8SEnji Cooper
5157718be8SEnji Cooperatf_test_case recurse
5257718be8SEnji Cooperrecurse_head()
5357718be8SEnji Cooper{
5457718be8SEnji Cooper	atf_set "descr" "Checks recursive searching"
5557718be8SEnji Cooper}
5657718be8SEnji Cooperrecurse_body()
5757718be8SEnji Cooper{
5857718be8SEnji Cooper	mkdir -p recurse/a/f recurse/d
5957718be8SEnji Cooper	echo -e "cod\ndover sole\nhaddock\nhalibut\npilchard" > recurse/d/fish
6057718be8SEnji Cooper	echo -e "cod\nhaddock\nplaice" > recurse/a/f/favourite-fish
6157718be8SEnji Cooper
62eaae77f8SAlan Somers	atf_check -o file:"$(atf_get_srcdir)/d_recurse.out" -x "grep -r haddock recurse | sort"
6357718be8SEnji Cooper}
6457718be8SEnji Cooper
6557718be8SEnji Cooperatf_test_case recurse_symlink
6657718be8SEnji Cooperrecurse_symlink_head()
6757718be8SEnji Cooper{
6857718be8SEnji Cooper	atf_set "descr" "Checks symbolic link recursion"
6957718be8SEnji Cooper}
7057718be8SEnji Cooperrecurse_symlink_body()
7157718be8SEnji Cooper{
729f67a481SEnji Cooper	# Begin FreeBSD
739f67a481SEnji Cooper	grep_type
749f67a481SEnji Cooper	if [ $? -eq $GREP_TYPE_GNU ]; then
759f67a481SEnji Cooper		atf_expect_fail "this test doesn't pass with gnu grep from ports"
769f67a481SEnji Cooper	fi
779f67a481SEnji Cooper	# End FreeBSD
7857718be8SEnji Cooper	mkdir -p test/c/d
7957718be8SEnji Cooper	(cd test/c/d && ln -s ../d .)
8057718be8SEnji Cooper	echo "Test string" > test/c/match
8157718be8SEnji Cooper
8257718be8SEnji Cooper	atf_check -o file:"$(atf_get_srcdir)/d_recurse_symlink.out" \
8357718be8SEnji Cooper	    -e file:"$(atf_get_srcdir)/d_recurse_symlink.err" \
84a700bef1SMark Johnston	    grep -rS string test
8557718be8SEnji Cooper}
8657718be8SEnji Cooper
8757718be8SEnji Cooperatf_test_case word_regexps
8857718be8SEnji Cooperword_regexps_head()
8957718be8SEnji Cooper{
9057718be8SEnji Cooper	atf_set "descr" "Checks word-regexps"
9157718be8SEnji Cooper}
9257718be8SEnji Cooperword_regexps_body()
9357718be8SEnji Cooper{
9457718be8SEnji Cooper	atf_check -o file:"$(atf_get_srcdir)/d_word_regexps.out" \
9557718be8SEnji Cooper	    grep -w separated $(atf_get_srcdir)/d_input
96945fc991SEd Maste
97945fc991SEd Maste	# Begin FreeBSD
98945fc991SEd Maste	printf "xmatch pmatch\n" > test1
99945fc991SEd Maste
100945fc991SEd Maste	atf_check -o inline:"pmatch\n" grep -Eow "(match )?pmatch" test1
101945fc991SEd Maste	# End FreeBSD
10257718be8SEnji Cooper}
10357718be8SEnji Cooper
10457718be8SEnji Cooperatf_test_case begin_end
10557718be8SEnji Cooperbegin_end_head()
10657718be8SEnji Cooper{
10757718be8SEnji Cooper	atf_set "descr" "Checks handling of line beginnings and ends"
10857718be8SEnji Cooper}
10957718be8SEnji Cooperbegin_end_body()
11057718be8SEnji Cooper{
11157718be8SEnji Cooper	atf_check -o file:"$(atf_get_srcdir)/d_begin_end_a.out" \
11257718be8SEnji Cooper	    grep ^Front "$(atf_get_srcdir)/d_input"
11357718be8SEnji Cooper
11457718be8SEnji Cooper	atf_check -o file:"$(atf_get_srcdir)/d_begin_end_b.out" \
11557718be8SEnji Cooper	    grep ending$ "$(atf_get_srcdir)/d_input"
11657718be8SEnji Cooper}
11757718be8SEnji Cooper
11857718be8SEnji Cooperatf_test_case ignore_case
11957718be8SEnji Cooperignore_case_head()
12057718be8SEnji Cooper{
12157718be8SEnji Cooper	atf_set "descr" "Checks ignore-case option"
12257718be8SEnji Cooper}
12357718be8SEnji Cooperignore_case_body()
12457718be8SEnji Cooper{
12557718be8SEnji Cooper	atf_check -o file:"$(atf_get_srcdir)/d_ignore_case.out" \
12657718be8SEnji Cooper	    grep -i Upper "$(atf_get_srcdir)/d_input"
12757718be8SEnji Cooper}
12857718be8SEnji Cooper
12957718be8SEnji Cooperatf_test_case invert
13057718be8SEnji Cooperinvert_head()
13157718be8SEnji Cooper{
13257718be8SEnji Cooper	atf_set "descr" "Checks selecting non-matching lines with -v option"
13357718be8SEnji Cooper}
13457718be8SEnji Cooperinvert_body()
13557718be8SEnji Cooper{
13657718be8SEnji Cooper	atf_check -o file:"$(atf_get_srcdir)/d_invert.out" \
13757718be8SEnji Cooper	    grep -v fish "$(atf_get_srcdir)/d_invert.in"
13857718be8SEnji Cooper}
13957718be8SEnji Cooper
14057718be8SEnji Cooperatf_test_case whole_line
14157718be8SEnji Cooperwhole_line_head()
14257718be8SEnji Cooper{
14357718be8SEnji Cooper	atf_set "descr" "Checks whole-line matching with -x flag"
14457718be8SEnji Cooper}
14557718be8SEnji Cooperwhole_line_body()
14657718be8SEnji Cooper{
14757718be8SEnji Cooper	atf_check -o file:"$(atf_get_srcdir)/d_whole_line.out" \
14857718be8SEnji Cooper	    grep -x matchme "$(atf_get_srcdir)/d_input"
14957718be8SEnji Cooper}
15057718be8SEnji Cooper
15157718be8SEnji Cooperatf_test_case negative
15257718be8SEnji Coopernegative_head()
15357718be8SEnji Cooper{
15457718be8SEnji Cooper	atf_set "descr" "Checks handling of files with no matches"
15557718be8SEnji Cooper}
15657718be8SEnji Coopernegative_body()
15757718be8SEnji Cooper{
15857718be8SEnji Cooper	atf_check -s ne:0 grep "not a hope in hell" "$(atf_get_srcdir)/d_input"
15957718be8SEnji Cooper}
16057718be8SEnji Cooper
16157718be8SEnji Cooperatf_test_case context
16257718be8SEnji Coopercontext_head()
16357718be8SEnji Cooper{
16457718be8SEnji Cooper	atf_set "descr" "Checks displaying context with -A, -B and -C flags"
16557718be8SEnji Cooper}
16657718be8SEnji Coopercontext_body()
16757718be8SEnji Cooper{
16857718be8SEnji Cooper	cp $(atf_get_srcdir)/d_context_*.* .
16957718be8SEnji Cooper
17057718be8SEnji Cooper	atf_check -o file:d_context_a.out grep -C2 bamboo d_context_a.in
17157718be8SEnji Cooper	atf_check -o file:d_context_b.out grep -A3 tilt d_context_a.in
17257718be8SEnji Cooper	atf_check -o file:d_context_c.out grep -B4 Whig d_context_a.in
17357718be8SEnji Cooper	atf_check -o file:d_context_d.out grep -C1 pig d_context_a.in d_context_b.in
174a4f3f02bSEd Maste	atf_check -o file:d_context_e.out \
175a4f3f02bSEd Maste	    grep -E -C1 '(banana|monkey)' d_context_e.in
176a4f3f02bSEd Maste	atf_check -o file:d_context_f.out \
177a4f3f02bSEd Maste	    grep -Ev -B2 '(banana|monkey|fruit)' d_context_e.in
178a4f3f02bSEd Maste	atf_check -o file:d_context_g.out \
179a4f3f02bSEd Maste	    grep -Ev -A1 '(banana|monkey|fruit)' d_context_e.in
18057718be8SEnji Cooper}
18157718be8SEnji Cooper
18257718be8SEnji Cooperatf_test_case file_exp
18357718be8SEnji Cooperfile_exp_head()
18457718be8SEnji Cooper{
18557718be8SEnji Cooper	atf_set "descr" "Checks reading expressions from file"
18657718be8SEnji Cooper}
18757718be8SEnji Cooperfile_exp_body()
18857718be8SEnji Cooper{
18957718be8SEnji Cooper	atf_check -o file:"$(atf_get_srcdir)/d_file_exp.out" -x \
19057718be8SEnji Cooper	    'jot 21 -1 1.00 | grep -f '"$(atf_get_srcdir)"'/d_file_exp.in'
19157718be8SEnji Cooper}
19257718be8SEnji Cooper
19357718be8SEnji Cooperatf_test_case egrep
19457718be8SEnji Cooperegrep_head()
19557718be8SEnji Cooper{
19657718be8SEnji Cooper	atf_set "descr" "Checks matching special characters with egrep"
19757718be8SEnji Cooper}
19857718be8SEnji Cooperegrep_body()
19957718be8SEnji Cooper{
20057718be8SEnji Cooper	atf_check -o file:"$(atf_get_srcdir)/d_egrep.out" \
20157718be8SEnji Cooper		egrep '\?|\*$$' "$(atf_get_srcdir)/d_input"
20257718be8SEnji Cooper}
20357718be8SEnji Cooper
20457718be8SEnji Cooperatf_test_case zgrep
20557718be8SEnji Cooperzgrep_head()
20657718be8SEnji Cooper{
20757718be8SEnji Cooper	atf_set "descr" "Checks handling of gzipped files with zgrep"
20857718be8SEnji Cooper}
20957718be8SEnji Cooperzgrep_body()
21057718be8SEnji Cooper{
21157718be8SEnji Cooper	cp "$(atf_get_srcdir)/d_input" .
21257718be8SEnji Cooper	gzip d_input || atf_fail "gzip failed"
21357718be8SEnji Cooper
21457718be8SEnji Cooper	atf_check -o file:"$(atf_get_srcdir)/d_zgrep.out" zgrep -h line d_input.gz
21557718be8SEnji Cooper}
21657718be8SEnji Cooper
217c4cbf1fbSCraig Leresatf_test_case zgrep_combined_flags
218c4cbf1fbSCraig Lereszgrep_combined_flags_head()
219c4cbf1fbSCraig Leres{
220c4cbf1fbSCraig Leres	atf_set "descr" "Checks for zgrep wrapper problems with combined flags (PR 247126)"
221c4cbf1fbSCraig Leres}
222c4cbf1fbSCraig Lereszgrep_combined_flags_body()
223c4cbf1fbSCraig Leres{
224c4cbf1fbSCraig Leres	echo 'foo bar' > test
225c4cbf1fbSCraig Leres
226c4cbf1fbSCraig Leres	atf_check -o inline:"foo bar\n" zgrep -we foo test
227c4cbf1fbSCraig Leres	# Avoid hang on reading from stdin in the failure case
228c4cbf1fbSCraig Leres	atf_check -o inline:"foo bar\n" zgrep -wefoo test < /dev/null
229c4cbf1fbSCraig Leres}
230c4cbf1fbSCraig Leres
231c4cbf1fbSCraig Leresatf_test_case zgrep_eflag
232c4cbf1fbSCraig Lereszgrep_eflag_head()
233c4cbf1fbSCraig Leres{
234c4cbf1fbSCraig Leres	atf_set "descr" "Checks for zgrep wrapper problems with -e PATTERN (PR 247126)"
235c4cbf1fbSCraig Leres}
236c4cbf1fbSCraig Lereszgrep_eflag_body()
237c4cbf1fbSCraig Leres{
238c4cbf1fbSCraig Leres	echo 'foo bar' > test
239c4cbf1fbSCraig Leres
240c4cbf1fbSCraig Leres	# Avoid hang on reading from stdin in the failure case
241c4cbf1fbSCraig Leres	atf_check -o inline:"foo bar\n" zgrep -e 'foo bar' test < /dev/null
242c4cbf1fbSCraig Leres	atf_check -o inline:"foo bar\n" zgrep --regexp='foo bar' test < /dev/null
243c4cbf1fbSCraig Leres}
244c4cbf1fbSCraig Leres
245c4cbf1fbSCraig Leresatf_test_case zgrep_fflag
246c4cbf1fbSCraig Lereszgrep_fflag_head()
247c4cbf1fbSCraig Leres{
248c4cbf1fbSCraig Leres	atf_set "descr" "Checks for zgrep wrapper problems with -f FILE (PR 247126)"
249c4cbf1fbSCraig Leres}
250c4cbf1fbSCraig Lereszgrep_fflag_body()
251c4cbf1fbSCraig Leres{
252c4cbf1fbSCraig Leres	echo foo > pattern
253c4cbf1fbSCraig Leres	echo foobar > test
254c4cbf1fbSCraig Leres
255c4cbf1fbSCraig Leres	# Avoid hang on reading from stdin in the failure case
256c4cbf1fbSCraig Leres	atf_check -o inline:"foobar\n" zgrep -f pattern test </dev/null
257c4cbf1fbSCraig Leres	atf_check -o inline:"foobar\n" zgrep --file=pattern test </dev/null
258c4cbf1fbSCraig Leres}
259c4cbf1fbSCraig Leres
260c4cbf1fbSCraig Leresatf_test_case zgrep_long_eflag
261c4cbf1fbSCraig Lereszgrep_long_eflag_head()
262c4cbf1fbSCraig Leres{
263c4cbf1fbSCraig Leres	atf_set "descr" "Checks for zgrep wrapper problems with --ignore-case reading from stdin (PR 247126)"
264c4cbf1fbSCraig Leres}
265c4cbf1fbSCraig Lereszgrep_long_eflag_body()
266c4cbf1fbSCraig Leres{
267c4cbf1fbSCraig Leres	echo foobar > test
268c4cbf1fbSCraig Leres
269c4cbf1fbSCraig Leres	atf_check -o inline:"foobar\n" zgrep -e foo --ignore-case < test
270c4cbf1fbSCraig Leres}
271c4cbf1fbSCraig Leres
272c4cbf1fbSCraig Leresatf_test_case zgrep_multiple_eflags
273c4cbf1fbSCraig Lereszgrep_multiple_eflags_head()
274c4cbf1fbSCraig Leres{
275c4cbf1fbSCraig Leres	atf_set "descr" "Checks for zgrep wrapper problems with multiple -e flags (PR 247126)"
276c4cbf1fbSCraig Leres}
277c4cbf1fbSCraig Lereszgrep_multiple_eflags_body()
278c4cbf1fbSCraig Leres{
279c4cbf1fbSCraig Leres	echo foobar > test
280c4cbf1fbSCraig Leres
281c4cbf1fbSCraig Leres	atf_check -o inline:"foobar\n" zgrep -e foo -e xxx test
282c4cbf1fbSCraig Leres}
283c4cbf1fbSCraig Leres
284c4cbf1fbSCraig Leresatf_test_case zgrep_empty_eflag
285c4cbf1fbSCraig Lereszgrep_empty_eflag_head()
286c4cbf1fbSCraig Leres{
287c4cbf1fbSCraig Leres	atf_set "descr" "Checks for zgrep wrapper problems with empty -e flags pattern (PR 247126)"
288c4cbf1fbSCraig Leres}
289c4cbf1fbSCraig Lereszgrep_empty_eflag_body()
290c4cbf1fbSCraig Leres{
291c4cbf1fbSCraig Leres	echo foobar > test
292c4cbf1fbSCraig Leres
293c4cbf1fbSCraig Leres	atf_check -o inline:"foobar\n" zgrep -e '' test
294c4cbf1fbSCraig Leres}
295c4cbf1fbSCraig Leres
29657718be8SEnji Cooperatf_test_case nonexistent
29757718be8SEnji Coopernonexistent_head()
29857718be8SEnji Cooper{
29957718be8SEnji Cooper	atf_set "descr" "Checks that -s flag suppresses error" \
30057718be8SEnji Cooper	                "messages about nonexistent files"
30157718be8SEnji Cooper}
30257718be8SEnji Coopernonexistent_body()
30357718be8SEnji Cooper{
30457718be8SEnji Cooper	atf_check -s ne:0 grep -s foobar nonexistent
30557718be8SEnji Cooper}
30657718be8SEnji Cooper
30757718be8SEnji Cooperatf_test_case context2
30857718be8SEnji Coopercontext2_head()
30957718be8SEnji Cooper{
31057718be8SEnji Cooper	atf_set "descr" "Checks displaying context with -z flag"
31157718be8SEnji Cooper}
31257718be8SEnji Coopercontext2_body()
31357718be8SEnji Cooper{
31457718be8SEnji Cooper	printf "haddock\000cod\000plaice\000" > test1
31557718be8SEnji Cooper	printf "mackeral\000cod\000crab\000" > test2
31657718be8SEnji Cooper
31757718be8SEnji Cooper	atf_check -o file:"$(atf_get_srcdir)/d_context2_a.out" \
31857718be8SEnji Cooper	    grep -z -A1 cod test1 test2
31957718be8SEnji Cooper
32057718be8SEnji Cooper	atf_check -o file:"$(atf_get_srcdir)/d_context2_b.out" \
32157718be8SEnji Cooper	    grep -z -B1 cod test1 test2
32257718be8SEnji Cooper
32357718be8SEnji Cooper	atf_check -o file:"$(atf_get_srcdir)/d_context2_c.out" \
32457718be8SEnji Cooper	    grep -z -C1 cod test1 test2
32557718be8SEnji Cooper}
326799c5faaSEd Maste# Begin FreeBSD
3279f67a481SEnji Cooper
3289f67a481SEnji Cooper# What grep(1) are we working with?
3299f67a481SEnji Cooper# - 0 : bsdgrep
3309f67a481SEnji Cooper# - 1 : gnu grep 2.51 (base)
3319f67a481SEnji Cooper# - 2 : gnu grep (ports)
3329f67a481SEnji CooperGREP_TYPE_BSD=0
3339f67a481SEnji CooperGREP_TYPE_GNU_FREEBSD=1
3349f67a481SEnji CooperGREP_TYPE_GNU=2
3359f67a481SEnji CooperGREP_TYPE_UNKNOWN=3
3369f67a481SEnji Cooper
3379f67a481SEnji Coopergrep_type()
3389f67a481SEnji Cooper{
3399f67a481SEnji Cooper	local grep_version=$(grep --version)
3409f67a481SEnji Cooper
3419f67a481SEnji Cooper	case "$grep_version" in
3429f67a481SEnji Cooper	*"BSD grep"*)
3439f67a481SEnji Cooper		return $GREP_TYPE_BSD
3449f67a481SEnji Cooper		;;
3459f67a481SEnji Cooper	*"GNU grep"*)
3469f67a481SEnji Cooper		case "$grep_version" in
3479f67a481SEnji Cooper		*2.5.1-FreeBSD*)
3489f67a481SEnji Cooper			return $GREP_TYPE_GNU_FREEBSD
3499f67a481SEnji Cooper			;;
3509f67a481SEnji Cooper		*)
3519f67a481SEnji Cooper			return $GREP_TYPE_GNU
3529f67a481SEnji Cooper			;;
3539f67a481SEnji Cooper		esac
3549f67a481SEnji Cooper		;;
3559f67a481SEnji Cooper	esac
3569f67a481SEnji Cooper	atf_fail "unknown grep type: $grep_version"
3579f67a481SEnji Cooper}
3589f67a481SEnji Cooper
359799c5faaSEd Masteatf_test_case oflag_zerolen
360799c5faaSEd Masteoflag_zerolen_head()
361799c5faaSEd Maste{
362799c5faaSEd Maste	atf_set "descr" "Check behavior of zero-length matches with -o flag (PR 195763)"
363799c5faaSEd Maste}
364799c5faaSEd Masteoflag_zerolen_body()
365799c5faaSEd Maste{
3669f67a481SEnji Cooper	grep_type
3679f67a481SEnji Cooper	if [ $? -eq $GREP_TYPE_GNU_FREEBSD ]; then
3689f67a481SEnji Cooper		atf_expect_fail "this test doesn't pass with gnu grep in base"
3699f67a481SEnji Cooper	fi
3709f67a481SEnji Cooper
371799c5faaSEd Maste	atf_check -o file:"$(atf_get_srcdir)/d_oflag_zerolen_a.out" \
372799c5faaSEd Maste	    grep -Eo '(^|:)0*' "$(atf_get_srcdir)/d_oflag_zerolen_a.in"
373799c5faaSEd Maste
374799c5faaSEd Maste	atf_check -o file:"$(atf_get_srcdir)/d_oflag_zerolen_b.out" \
375799c5faaSEd Maste	    grep -Eo '(^|:)0*' "$(atf_get_srcdir)/d_oflag_zerolen_b.in"
376799c5faaSEd Maste
377799c5faaSEd Maste	atf_check -o file:"$(atf_get_srcdir)/d_oflag_zerolen_c.out" \
378799c5faaSEd Maste	    grep -Eo '[[:alnum:]]*' "$(atf_get_srcdir)/d_oflag_zerolen_c.in"
379799c5faaSEd Maste
380799c5faaSEd Maste	atf_check -o empty grep -Eo '' "$(atf_get_srcdir)/d_oflag_zerolen_d.in"
381799c5faaSEd Maste
382799c5faaSEd Maste	atf_check -o file:"$(atf_get_srcdir)/d_oflag_zerolen_e.out" \
383799c5faaSEd Maste	    grep -o -e 'ab' -e 'bc' "$(atf_get_srcdir)/d_oflag_zerolen_e.in"
384799c5faaSEd Maste
385799c5faaSEd Maste	atf_check -o file:"$(atf_get_srcdir)/d_oflag_zerolen_e.out" \
386799c5faaSEd Maste	    grep -o -e 'bc' -e 'ab' "$(atf_get_srcdir)/d_oflag_zerolen_e.in"
387799c5faaSEd Maste}
388799c5faaSEd Maste
389799c5faaSEd Masteatf_test_case xflag
390799c5faaSEd Mastexflag_head()
391799c5faaSEd Maste{
392799c5faaSEd Maste	atf_set "descr" "Check that we actually get a match with -x flag (PR 180990)"
393799c5faaSEd Maste}
394799c5faaSEd Mastexflag_body()
395799c5faaSEd Maste{
396799c5faaSEd Maste	echo 128 > match_file
397799c5faaSEd Maste	seq 1 128 > pattern_file
398799c5faaSEd Maste	grep -xf pattern_file match_file
399799c5faaSEd Maste}
400799c5faaSEd Maste
401799c5faaSEd Masteatf_test_case color
402799c5faaSEd Mastecolor_head()
403799c5faaSEd Maste{
404799c5faaSEd Maste	atf_set "descr" "Check --color support"
405799c5faaSEd Maste}
406799c5faaSEd Mastecolor_body()
407799c5faaSEd Maste{
4089f67a481SEnji Cooper	grep_type
4099f67a481SEnji Cooper	if [ $? -eq $GREP_TYPE_GNU_FREEBSD ]; then
4109f67a481SEnji Cooper		atf_expect_fail "this test doesn't pass with gnu grep in base"
4119f67a481SEnji Cooper	fi
4129f67a481SEnji Cooper
413799c5faaSEd Maste	echo 'abcd*' > grepfile
414799c5faaSEd Maste	echo 'abc$' >> grepfile
415799c5faaSEd Maste	echo '^abc' >> grepfile
416799c5faaSEd Maste
417799c5faaSEd Maste	atf_check -o file:"$(atf_get_srcdir)/d_color_a.out" \
418799c5faaSEd Maste	    grep --color=auto -e '.*' -e 'a' "$(atf_get_srcdir)/d_color_a.in"
419799c5faaSEd Maste
420799c5faaSEd Maste	atf_check -o file:"$(atf_get_srcdir)/d_color_b.out" \
421799c5faaSEd Maste	    grep --color=auto -f grepfile "$(atf_get_srcdir)/d_color_b.in"
422799c5faaSEd Maste
423799c5faaSEd Maste	atf_check -o file:"$(atf_get_srcdir)/d_color_c.out" \
424799c5faaSEd Maste	    grep --color=always -f grepfile "$(atf_get_srcdir)/d_color_b.in"
425799c5faaSEd Maste}
426799c5faaSEd Maste
427799c5faaSEd Masteatf_test_case f_file_empty
428799c5faaSEd Mastef_file_empty_head()
429799c5faaSEd Maste{
430799c5faaSEd Maste	atf_set "descr" "Check for handling of a null byte in empty file, specified by -f (PR 202022)"
431799c5faaSEd Maste}
432799c5faaSEd Mastef_file_empty_body()
433799c5faaSEd Maste{
434799c5faaSEd Maste	printf "\0\n" > nulpat
435799c5faaSEd Maste
436799c5faaSEd Maste	atf_check -s exit:1 grep -f nulpat "$(atf_get_srcdir)/d_f_file_empty.in"
437799c5faaSEd Maste}
438799c5faaSEd Maste
439799c5faaSEd Masteatf_test_case escmap
440799c5faaSEd Masteescmap_head()
441799c5faaSEd Maste{
442799c5faaSEd Maste	atf_set "descr" "Check proper handling of escaped vs. unescaped dot expressions (PR 175314)"
443799c5faaSEd Maste}
444799c5faaSEd Masteescmap_body()
445799c5faaSEd Maste{
446799c5faaSEd Maste	atf_check -s exit:1 grep -o 'f.o\.' "$(atf_get_srcdir)/d_escmap.in"
447799c5faaSEd Maste	atf_check -o not-empty grep -o 'f.o.' "$(atf_get_srcdir)/d_escmap.in"
448799c5faaSEd Maste}
449799c5faaSEd Maste
450799c5faaSEd Masteatf_test_case egrep_empty_invalid
451799c5faaSEd Masteegrep_empty_invalid_head()
452799c5faaSEd Maste{
453799c5faaSEd Maste	atf_set "descr" "Check for handling of an invalid empty pattern (PR 194823)"
454799c5faaSEd Maste}
455799c5faaSEd Masteegrep_empty_invalid_body()
456799c5faaSEd Maste{
457befd089cSEd Maste	atf_check -e ignore -s not-exit:0 egrep '{' /dev/null
458799c5faaSEd Maste}
459e06ffa32SEd Maste
460e06ffa32SEd Masteatf_test_case zerolen
461e06ffa32SEd Mastezerolen_head()
462e06ffa32SEd Maste{
463e06ffa32SEd Maste	atf_set "descr" "Check for successful zero-length matches with ^$"
464e06ffa32SEd Maste}
465e06ffa32SEd Mastezerolen_body()
466e06ffa32SEd Maste{
467e06ffa32SEd Maste	printf "Eggs\n\nCheese" > test1
468e06ffa32SEd Maste
469e06ffa32SEd Maste	atf_check -o inline:"\n" grep -e "^$" test1
470e06ffa32SEd Maste
471e06ffa32SEd Maste	atf_check -o inline:"Eggs\nCheese\n" grep -v -e "^$" test1
472e06ffa32SEd Maste}
4735199917cSEnji Cooper
474a4f3f02bSEd Masteatf_test_case wflag_emptypat
475a4f3f02bSEd Mastewflag_emptypat_head()
476a4f3f02bSEd Maste{
477a4f3f02bSEd Maste	atf_set "descr" "Check for proper handling of -w with an empty pattern (PR 105221)"
478a4f3f02bSEd Maste}
479a4f3f02bSEd Mastewflag_emptypat_body()
480a4f3f02bSEd Maste{
481a4f3f02bSEd Maste	printf "" > test1
482a4f3f02bSEd Maste	printf "\n" > test2
483a4f3f02bSEd Maste	printf "qaz" > test3
484a4f3f02bSEd Maste	printf " qaz\n" > test4
485a4f3f02bSEd Maste
486a4f3f02bSEd Maste	atf_check -s exit:1 -o empty grep -w -e "" test1
487a4f3f02bSEd Maste
488f823c6dcSKyle Evans	atf_check -o file:test2 grep -vw -e "" test2
489a4f3f02bSEd Maste
490a4f3f02bSEd Maste	atf_check -s exit:1 -o empty grep -w -e "" test3
491a4f3f02bSEd Maste
492f823c6dcSKyle Evans	atf_check -o file:test4 grep -vw -e "" test4
493a4f3f02bSEd Maste}
494a4f3f02bSEd Maste
49538325e2aSKyle Evansatf_test_case xflag_emptypat
49638325e2aSKyle Evansxflag_emptypat_body()
49738325e2aSKyle Evans{
49838325e2aSKyle Evans	printf "" > test1
49938325e2aSKyle Evans	printf "\n" > test2
50038325e2aSKyle Evans	printf "qaz" > test3
50138325e2aSKyle Evans	printf " qaz\n" > test4
50238325e2aSKyle Evans
50338325e2aSKyle Evans	atf_check -s exit:1 -o empty grep -x -e "" test1
50438325e2aSKyle Evans
50538325e2aSKyle Evans	atf_check -o file:test2 grep -x -e "" test2
50638325e2aSKyle Evans
50738325e2aSKyle Evans	atf_check -s exit:1 -o empty grep -x -e "" test3
50838325e2aSKyle Evans
50938325e2aSKyle Evans	atf_check -s exit:1 -o empty grep -x -e "" test4
51038325e2aSKyle Evans
51138325e2aSKyle Evans	total=$(wc -l /COPYRIGHT | sed 's/[^0-9]//g')
51238325e2aSKyle Evans
51338325e2aSKyle Evans	# Simple checks that grep -x with an empty pattern isn't matching every
51438325e2aSKyle Evans	# line.  The exact counts aren't important, as long as they don't
51538325e2aSKyle Evans	# match the total line count and as long as they don't match each other.
51638325e2aSKyle Evans	atf_check -o save:xpositive.count grep -Fxc '' /COPYRIGHT
51738325e2aSKyle Evans	atf_check -o save:xnegative.count grep -Fvxc '' /COPYRIGHT
51838325e2aSKyle Evans
51938325e2aSKyle Evans	atf_check -o not-inline:"${total}" cat xpositive.count
52038325e2aSKyle Evans	atf_check -o not-inline:"${total}" cat xnegative.count
52138325e2aSKyle Evans
52238325e2aSKyle Evans	atf_check -o not-file:xnegative.count cat xpositive.count
52338325e2aSKyle Evans}
52438325e2aSKyle Evans
52538325e2aSKyle Evansatf_test_case xflag_emptypat_plus
52638325e2aSKyle Evansxflag_emptypat_plus_body()
52738325e2aSKyle Evans{
52838325e2aSKyle Evans	printf "foo\n\nbar\n\nbaz\n" > target
52938325e2aSKyle Evans	printf "foo\n \nbar\n \nbaz\n" > target_spacelines
53038325e2aSKyle Evans	printf "foo\nbar\nbaz\n" > matches
53138325e2aSKyle Evans	printf " \n \n" > spacelines
53238325e2aSKyle Evans
53338325e2aSKyle Evans	printf "foo\n\nbar\n\nbaz\n" > patlist1
53438325e2aSKyle Evans	printf "foo\n\nba\n\nbaz\n" > patlist2
53538325e2aSKyle Evans
53638325e2aSKyle Evans	sed -e '/bar/d' target > matches_not2
53738325e2aSKyle Evans
53838325e2aSKyle Evans	# Normal handling first
53938325e2aSKyle Evans	atf_check -o file:target grep -Fxf patlist1 target
54038325e2aSKyle Evans	atf_check -o file:matches grep -Fxf patlist1 target_spacelines
54138325e2aSKyle Evans	atf_check -o file:matches_not2 grep -Fxf patlist2 target
54238325e2aSKyle Evans
54338325e2aSKyle Evans	# -v handling
54438325e2aSKyle Evans	atf_check -s exit:1 -o empty grep -Fvxf patlist1 target
54538325e2aSKyle Evans	atf_check -o file:spacelines grep -Fxvf patlist1 target_spacelines
54638325e2aSKyle Evans}
54738325e2aSKyle Evans
548f823c6dcSKyle Evansatf_test_case emptyfile
549f823c6dcSKyle Evansemptyfile_descr()
550f823c6dcSKyle Evans{
551f823c6dcSKyle Evans	atf_set "descr" "Check for proper handling of empty pattern files (PR 253209)"
552f823c6dcSKyle Evans}
553f823c6dcSKyle Evansemptyfile_body()
554f823c6dcSKyle Evans{
555f823c6dcSKyle Evans	:> epatfile
556f823c6dcSKyle Evans	echo "blubb" > subj
557f823c6dcSKyle Evans
558f823c6dcSKyle Evans	# From PR 253209, bsdgrep was short-circuiting completely on an empty
559f823c6dcSKyle Evans	# file, but we should have still been processing lines.
560f823c6dcSKyle Evans	atf_check -s exit:1 -o empty fgrep -f epatfile subj
561f823c6dcSKyle Evans	atf_check -o file:subj fgrep -vf epatfile subj
562f823c6dcSKyle Evans}
563f823c6dcSKyle Evans
564fe8c9d5bSEd Masteatf_test_case excessive_matches
565fe8c9d5bSEd Masteexcessive_matches_head()
566fe8c9d5bSEd Maste{
567fe8c9d5bSEd Maste	atf_set "descr" "Check for proper handling of lines with excessive matches (PR 218811)"
568fe8c9d5bSEd Maste}
569fe8c9d5bSEd Masteexcessive_matches_body()
570fe8c9d5bSEd Maste{
571fe8c9d5bSEd Maste	grep_type
572fe8c9d5bSEd Maste	if [ $? -eq $GREP_TYPE_GNU_FREEBSD ]; then
573fe8c9d5bSEd Maste		atf_expect_fail "this test does not pass with GNU grep in base"
574fe8c9d5bSEd Maste	fi
575fe8c9d5bSEd Maste
576fe8c9d5bSEd Maste	for i in $(jot 4096); do
577fe8c9d5bSEd Maste		printf "x" >> test.in
578fe8c9d5bSEd Maste	done
579fe8c9d5bSEd Maste
580fe8c9d5bSEd Maste	atf_check -s exit:0 -x '[ $(grep -o x test.in | wc -l) -eq 4096 ]'
5816d635d3bSEd Maste	atf_check -s exit:1 -x 'grep -on x test.in | grep -v "1:x"'
582fe8c9d5bSEd Maste}
583fe8c9d5bSEd Maste
5845199917cSEnji Cooperatf_test_case fgrep_sanity
5855199917cSEnji Cooperfgrep_sanity_head()
5865199917cSEnji Cooper{
5875199917cSEnji Cooper	atf_set "descr" "Check for fgrep sanity, literal expressions only"
5885199917cSEnji Cooper}
5895199917cSEnji Cooperfgrep_sanity_body()
5905199917cSEnji Cooper{
5915199917cSEnji Cooper	printf "Foo" > test1
5925199917cSEnji Cooper
5935199917cSEnji Cooper	atf_check -o inline:"Foo\n" fgrep -e "Foo" test1
5945199917cSEnji Cooper
5955199917cSEnji Cooper	atf_check -s exit:1 -o empty fgrep -e "Fo." test1
5965199917cSEnji Cooper}
5975199917cSEnji Cooper
5985199917cSEnji Cooperatf_test_case egrep_sanity
5995199917cSEnji Cooperegrep_sanity_head()
6005199917cSEnji Cooper{
6015199917cSEnji Cooper	atf_set "descr" "Check for egrep sanity, EREs only"
6025199917cSEnji Cooper}
6035199917cSEnji Cooperegrep_sanity_body()
6045199917cSEnji Cooper{
6055199917cSEnji Cooper	printf "Foobar(ed)" > test1
6065199917cSEnji Cooper	printf "M{1}" > test2
6075199917cSEnji Cooper
6085199917cSEnji Cooper	atf_check -o inline:"Foo\n" egrep -o -e "F.." test1
6095199917cSEnji Cooper
6105199917cSEnji Cooper	atf_check -o inline:"Foobar\n" egrep -o -e "F[a-z]*" test1
6115199917cSEnji Cooper
6125199917cSEnji Cooper	atf_check -o inline:"Fo\n" egrep -o -e "F(o|p)" test1
6135199917cSEnji Cooper
6145199917cSEnji Cooper	atf_check -o inline:"(ed)\n" egrep -o -e "\(ed\)" test1
6155199917cSEnji Cooper
6165199917cSEnji Cooper	atf_check -o inline:"M\n" egrep -o -e "M{1}" test2
6175199917cSEnji Cooper
6185199917cSEnji Cooper	atf_check -o inline:"M{1}\n" egrep -o -e "M\{1\}" test2
6195199917cSEnji Cooper}
6205199917cSEnji Cooper
6215199917cSEnji Cooperatf_test_case grep_sanity
6225199917cSEnji Coopergrep_sanity_head()
6235199917cSEnji Cooper{
6245199917cSEnji Cooper	atf_set "descr" "Check for basic grep sanity, BREs only"
6255199917cSEnji Cooper}
6265199917cSEnji Coopergrep_sanity_body()
6275199917cSEnji Cooper{
6285199917cSEnji Cooper	printf "Foobar(ed)" > test1
6295199917cSEnji Cooper	printf "M{1}" > test2
6305199917cSEnji Cooper
6315199917cSEnji Cooper	atf_check -o inline:"Foo\n" grep -o -e "F.." test1
6325199917cSEnji Cooper
6335199917cSEnji Cooper	atf_check -o inline:"Foobar\n" grep -o -e "F[a-z]*" test1
6345199917cSEnji Cooper
6355199917cSEnji Cooper	atf_check -o inline:"Fo\n" grep -o -e "F\(o\)" test1
6365199917cSEnji Cooper
6375199917cSEnji Cooper	atf_check -o inline:"(ed)\n" grep -o -e "(ed)" test1
6385199917cSEnji Cooper
6395199917cSEnji Cooper	atf_check -o inline:"M{1}\n" grep -o -e "M{1}" test2
6405199917cSEnji Cooper
6415199917cSEnji Cooper	atf_check -o inline:"M\n" grep -o -e "M\{1\}" test2
6425199917cSEnji Cooper}
643945fc991SEd Maste
644945fc991SEd Masteatf_test_case wv_combo_break
645945fc991SEd Mastewv_combo_break_head()
646945fc991SEd Maste{
647945fc991SEd Maste	atf_set "descr" "Check for incorrectly matching lines with both -w and -v flags (PR 218467)"
648945fc991SEd Maste}
649945fc991SEd Mastewv_combo_break_body()
650945fc991SEd Maste{
651945fc991SEd Maste	printf "x xx\n" > test1
652945fc991SEd Maste	printf "xx x\n" > test2
653945fc991SEd Maste
654945fc991SEd Maste	atf_check -o file:test1 grep -w "x" test1
655945fc991SEd Maste	atf_check -o file:test2 grep -w "x" test2
656945fc991SEd Maste
657945fc991SEd Maste	atf_check -s exit:1 grep -v -w "x" test1
658945fc991SEd Maste	atf_check -s exit:1 grep -v -w "x" test2
659945fc991SEd Maste}
660e2127de8SEd Maste
6616d635d3bSEd Masteatf_test_case ocolor_metadata
6626d635d3bSEd Masteocolor_metadata_head()
6636d635d3bSEd Maste{
6646d635d3bSEd Maste	atf_set "descr" "Check for -n/-b producing per-line metadata output"
6656d635d3bSEd Maste}
6666d635d3bSEd Masteocolor_metadata_body()
6676d635d3bSEd Maste{
6686d635d3bSEd Maste	grep_type
6696d635d3bSEd Maste	if [ $? -eq $GREP_TYPE_GNU_FREEBSD ]; then
6706d635d3bSEd Maste		atf_expect_fail "this test does not pass with GNU grep in base"
6716d635d3bSEd Maste	fi
6726d635d3bSEd Maste
6736d635d3bSEd Maste	printf "xxx\nyyyy\nzzz\nfoobarbaz\n" > test1
6746d635d3bSEd Maste	check_expr="^[^:]*[0-9][^:]*:[^:]+$"
6756d635d3bSEd Maste
6766d635d3bSEd Maste	atf_check -o inline:"1:1:xx\n" grep -bon "xx$" test1
6776d635d3bSEd Maste
6786d635d3bSEd Maste	atf_check -o inline:"2:4:yyyy\n" grep -bn "yy" test1
6796d635d3bSEd Maste
6806d635d3bSEd Maste	atf_check -o inline:"2:6:yy\n" grep -bon "yy$" test1
6816d635d3bSEd Maste
6826d635d3bSEd Maste	# These checks ensure that grep isn't producing bogus line numbering
6836d635d3bSEd Maste	# in the middle of a line.
6846d635d3bSEd Maste	atf_check -s exit:1 -x \
6856d635d3bSEd Maste	    "grep -Eon 'x|y|z|f' test1 | grep -Ev '${check_expr}'"
6866d635d3bSEd Maste
6876d635d3bSEd Maste	atf_check -s exit:1 -x \
6886d635d3bSEd Maste	    "grep -En 'x|y|z|f' --color=always test1 | grep -Ev '${check_expr}'"
6896d635d3bSEd Maste
6906d635d3bSEd Maste	atf_check -s exit:1 -x \
6916d635d3bSEd Maste	    "grep -Eon 'x|y|z|f' --color=always test1 | grep -Ev '${check_expr}'"
6926d635d3bSEd Maste}
6936d635d3bSEd Maste
694e2127de8SEd Masteatf_test_case grep_nomatch_flags
695e2127de8SEd Mastegrep_nomatch_flags_head()
696e2127de8SEd Maste{
697e2127de8SEd Maste	atf_set "descr" "Check for no match (-c, -l, -L, -q) flags not producing line matches or context (PR 219077)"
698e2127de8SEd Maste}
699e2127de8SEd Maste
700e2127de8SEd Mastegrep_nomatch_flags_body()
701e2127de8SEd Maste{
70238325e2aSKyle Evans	grep_type
70338325e2aSKyle Evans
70438325e2aSKyle Evans	if [ $? -eq $GREP_TYPE_GNU_FREEBSD ]; then
70538325e2aSKyle Evans		atf_expect_fail "this test does not pass with GNU grep in base"
70638325e2aSKyle Evans	fi
70738325e2aSKyle Evans
708e2127de8SEd Maste	printf "A\nB\nC\n" > test1
709e2127de8SEd Maste
710e2127de8SEd Maste	atf_check -o inline:"1\n" grep -c -C 1 -e "B" test1
711e2127de8SEd Maste	atf_check -o inline:"1\n" grep -c -B 1 -e "B" test1
712e2127de8SEd Maste	atf_check -o inline:"1\n" grep -c -A 1 -e "B" test1
713e2127de8SEd Maste	atf_check -o inline:"1\n" grep -c -C 1 -e "B" test1
714e2127de8SEd Maste
715e2127de8SEd Maste	atf_check -o inline:"test1\n" grep -l -e "B" test1
716e2127de8SEd Maste	atf_check -o inline:"test1\n" grep -l -B 1 -e "B" test1
717e2127de8SEd Maste	atf_check -o inline:"test1\n" grep -l -A 1 -e "B" test1
718e2127de8SEd Maste	atf_check -o inline:"test1\n" grep -l -C 1 -e "B" test1
719e2127de8SEd Maste
72038325e2aSKyle Evans	atf_check -o inline:"test1\n" grep -L -e "D" test1
721e2127de8SEd Maste
722e2127de8SEd Maste	atf_check -o empty grep -q -e "B" test1
723e2127de8SEd Maste	atf_check -o empty grep -q -B 1 -e "B" test1
724e2127de8SEd Maste	atf_check -o empty grep -q -A 1 -e "B" test1
725e2127de8SEd Maste	atf_check -o empty grep -q -C 1 -e "B" test1
726e2127de8SEd Maste}
727b5fc583cSEd Maste
728b5fc583cSEd Masteatf_test_case badcontext
729b5fc583cSEd Mastebadcontext_head()
730b5fc583cSEd Maste{
731b5fc583cSEd Maste	atf_set "descr" "Check for handling of invalid context arguments"
732b5fc583cSEd Maste}
733b5fc583cSEd Mastebadcontext_body()
734b5fc583cSEd Maste{
735b5fc583cSEd Maste	printf "A\nB\nC\n" > test1
736b5fc583cSEd Maste
737b5fc583cSEd Maste	atf_check -s not-exit:0 -e ignore grep -A "-1" "B" test1
738b5fc583cSEd Maste
739b5fc583cSEd Maste	atf_check -s not-exit:0 -e ignore grep -B "-1" "B" test1
740b5fc583cSEd Maste
741b5fc583cSEd Maste	atf_check -s not-exit:0 -e ignore grep -C "-1" "B" test1
742b5fc583cSEd Maste
743b5fc583cSEd Maste	atf_check -s not-exit:0 -e ignore grep -A "B" "B" test1
744b5fc583cSEd Maste
745b5fc583cSEd Maste	atf_check -s not-exit:0 -e ignore grep -B "B" "B" test1
746b5fc583cSEd Maste
747b5fc583cSEd Maste	atf_check -s not-exit:0 -e ignore grep -C "B" "B" test1
748b5fc583cSEd Maste}
749f701bdc5SEd Maste
750f701bdc5SEd Masteatf_test_case binary_flags
751f701bdc5SEd Mastebinary_flags_head()
752f701bdc5SEd Maste{
753f701bdc5SEd Maste	atf_set "descr" "Check output for binary flags (-a, -I, -U, --binary-files)"
754f701bdc5SEd Maste}
755f701bdc5SEd Mastebinary_flags_body()
756f701bdc5SEd Maste{
757f701bdc5SEd Maste	printf "A\000B\000C" > test1
758f701bdc5SEd Maste	printf "A\n\000B\n\000C" > test2
759f701bdc5SEd Maste	binmatchtext="Binary file test1 matches\n"
760f701bdc5SEd Maste
761f701bdc5SEd Maste	# Binaries not treated as text (default, -U)
762f701bdc5SEd Maste	atf_check -o inline:"${binmatchtext}" grep 'B' test1
763f701bdc5SEd Maste	atf_check -o inline:"${binmatchtext}" grep 'B' -C 1 test1
764f701bdc5SEd Maste
765f701bdc5SEd Maste	atf_check -o inline:"${binmatchtext}" grep -U 'B' test1
766f701bdc5SEd Maste	atf_check -o inline:"${binmatchtext}" grep -U 'B' -C 1 test1
767f701bdc5SEd Maste
768f701bdc5SEd Maste	# Binary, -a, no newlines
769f701bdc5SEd Maste	atf_check -o inline:"A\000B\000C\n" grep -a 'B' test1
770f701bdc5SEd Maste	atf_check -o inline:"A\000B\000C\n" grep -a 'B' -C 1 test1
771f701bdc5SEd Maste
772f701bdc5SEd Maste	# Binary, -a, newlines
773f701bdc5SEd Maste	atf_check -o inline:"\000B\n" grep -a 'B' test2
774f701bdc5SEd Maste	atf_check -o inline:"A\n\000B\n\000C\n" grep -a 'B' -C 1 test2
775f701bdc5SEd Maste
776f701bdc5SEd Maste	# Binary files ignored
777f701bdc5SEd Maste	atf_check -s exit:1 grep -I 'B' test2
778f701bdc5SEd Maste
779f701bdc5SEd Maste	# --binary-files equivalence
780f701bdc5SEd Maste	atf_check -o inline:"${binmatchtext}" grep --binary-files=binary 'B' test1
781f701bdc5SEd Maste	atf_check -o inline:"A\000B\000C\n" grep --binary-files=text 'B' test1
782f701bdc5SEd Maste	atf_check -s exit:1 grep --binary-files=without-match 'B' test2
783f701bdc5SEd Maste}
784d7b65474SEd Maste
785d7b65474SEd Masteatf_test_case mmap
786d7b65474SEd Mastemmap_head()
787d7b65474SEd Maste{
788d7b65474SEd Maste	atf_set "descr" "Check basic matching with --mmap flag"
789d7b65474SEd Maste}
790d7b65474SEd Mastemmap_body()
791d7b65474SEd Maste{
792d7b65474SEd Maste	grep_type
793d7b65474SEd Maste	if [ $? -eq $GREP_TYPE_GNU ]; then
794d7b65474SEd Maste		atf_expect_fail "gnu grep from ports has no --mmap option"
795d7b65474SEd Maste	fi
796d7b65474SEd Maste
797d7b65474SEd Maste	printf "A\nB\nC\n" > test1
798d7b65474SEd Maste
799d7b65474SEd Maste	atf_check -s exit:0 -o inline:"B\n" grep --mmap -oe "B" test1
800d7b65474SEd Maste	atf_check -s exit:1 grep --mmap -e "Z" test1
801d7b65474SEd Maste}
802d7b65474SEd Maste
8030e957942SKyle Evansatf_test_case matchall
8040e957942SKyle Evansmatchall_head()
8050e957942SKyle Evans{
8060e957942SKyle Evans	atf_set "descr" "Check proper behavior of matching all with an empty string"
8070e957942SKyle Evans}
8080e957942SKyle Evansmatchall_body()
8090e957942SKyle Evans{
8100e957942SKyle Evans	printf "" > test1
8110e957942SKyle Evans	printf "A" > test2
8120e957942SKyle Evans	printf "A\nB" > test3
8130e957942SKyle Evans
8140e957942SKyle Evans	atf_check -o inline:"test2:A\ntest3:A\ntest3:B\n" grep "" test1 test2 test3
8150e957942SKyle Evans	atf_check -o inline:"test3:A\ntest3:B\ntest2:A\n" grep "" test3 test1 test2
8160e957942SKyle Evans	atf_check -o inline:"test2:A\ntest3:A\ntest3:B\n" grep "" test2 test3 test1
8170e957942SKyle Evans
8180e957942SKyle Evans	atf_check -s exit:1 grep "" test1
8190e957942SKyle Evans}
820c552f48bSKyle Evans
821c552f48bSKyle Evansatf_test_case fgrep_multipattern
822c552f48bSKyle Evansfgrep_multipattern_head()
823c552f48bSKyle Evans{
824c552f48bSKyle Evans	atf_set "descr" "Check proper behavior with multiple patterns supplied to fgrep"
825c552f48bSKyle Evans}
826c552f48bSKyle Evansfgrep_multipattern_body()
827c552f48bSKyle Evans{
828c552f48bSKyle Evans	printf "Foo\nBar\nBaz" > test1
829c552f48bSKyle Evans
830c552f48bSKyle Evans	atf_check -o inline:"Foo\nBaz\n" grep -F -e "Foo" -e "Baz" test1
831c552f48bSKyle Evans	atf_check -o inline:"Foo\nBaz\n" grep -F -e "Baz" -e "Foo" test1
832c552f48bSKyle Evans	atf_check -o inline:"Bar\nBaz\n" grep -F -e "Bar" -e "Baz" test1
833c552f48bSKyle Evans}
834c552f48bSKyle Evans
835c552f48bSKyle Evansatf_test_case fgrep_icase
836c552f48bSKyle Evansfgrep_icase_head()
837c552f48bSKyle Evans{
838c552f48bSKyle Evans	atf_set "descr" "Check proper handling of -i supplied to fgrep"
839c552f48bSKyle Evans}
840c552f48bSKyle Evansfgrep_icase_body()
841c552f48bSKyle Evans{
842c552f48bSKyle Evans	printf "Foo\nBar\nBaz" > test1
843c552f48bSKyle Evans
844c552f48bSKyle Evans	atf_check -o inline:"Foo\nBaz\n" grep -Fi -e "foo" -e "baz" test1
845c552f48bSKyle Evans	atf_check -o inline:"Foo\nBaz\n" grep -Fi -e "baz" -e "foo" test1
846c552f48bSKyle Evans	atf_check -o inline:"Bar\nBaz\n" grep -Fi -e "bar" -e "baz" test1
847c552f48bSKyle Evans	atf_check -o inline:"Bar\nBaz\n" grep -Fi -e "BAR" -e "bAz" test1
848c552f48bSKyle Evans}
849c552f48bSKyle Evans
850c552f48bSKyle Evansatf_test_case fgrep_oflag
851c552f48bSKyle Evansfgrep_oflag_head()
852c552f48bSKyle Evans{
853c552f48bSKyle Evans	atf_set "descr" "Check proper handling of -o supplied to fgrep"
854c552f48bSKyle Evans}
855c552f48bSKyle Evansfgrep_oflag_body()
856c552f48bSKyle Evans{
857c552f48bSKyle Evans	printf "abcdefghi\n" > test1
858c552f48bSKyle Evans
859c552f48bSKyle Evans	atf_check -o inline:"a\n" grep -Fo "a" test1
860c552f48bSKyle Evans	atf_check -o inline:"i\n" grep -Fo "i" test1
861c552f48bSKyle Evans	atf_check -o inline:"abc\n" grep -Fo "abc" test1
862c552f48bSKyle Evans	atf_check -o inline:"fgh\n" grep -Fo "fgh" test1
863c552f48bSKyle Evans	atf_check -o inline:"cde\n" grep -Fo "cde" test1
864c552f48bSKyle Evans	atf_check -o inline:"bcd\n" grep -Fo -e "bcd" -e "cde" test1
865c552f48bSKyle Evans	atf_check -o inline:"bcd\nefg\n" grep -Fo -e "bcd" -e "efg" test1
866c552f48bSKyle Evans
867c552f48bSKyle Evans	atf_check -s exit:1 grep -Fo "xabc" test1
868c552f48bSKyle Evans	atf_check -s exit:1 grep -Fo "abcx" test1
869c552f48bSKyle Evans	atf_check -s exit:1 grep -Fo "xghi" test1
870c552f48bSKyle Evans	atf_check -s exit:1 grep -Fo "ghix" test1
871c552f48bSKyle Evans	atf_check -s exit:1 grep -Fo "abcdefghiklmnopqrstuvwxyz" test1
872c552f48bSKyle Evans}
873ce024bdcSKyle Evans
874ce024bdcSKyle Evansatf_test_case cflag
875ce024bdcSKyle Evanscflag_head()
876ce024bdcSKyle Evans{
877ce024bdcSKyle Evans	atf_set "descr" "Check proper handling of -c"
878ce024bdcSKyle Evans}
879ce024bdcSKyle Evanscflag_body()
880ce024bdcSKyle Evans{
881ce024bdcSKyle Evans	printf "a\nb\nc\n" > test1
882ce024bdcSKyle Evans
883ce024bdcSKyle Evans	atf_check -o inline:"1\n" grep -Ec "a" test1
884ce024bdcSKyle Evans	atf_check -o inline:"2\n" grep -Ec "a|b" test1
885ce024bdcSKyle Evans	atf_check -o inline:"3\n" grep -Ec "a|b|c" test1
886ce024bdcSKyle Evans
887ce024bdcSKyle Evans	atf_check -o inline:"test1:2\n" grep -EHc "a|b" test1
888ce024bdcSKyle Evans}
88952cf8965SKyle Evans
89052cf8965SKyle Evansatf_test_case mflag
89152cf8965SKyle Evansmflag_head()
89252cf8965SKyle Evans{
89352cf8965SKyle Evans	atf_set "descr" "Check proper handling of -m"
89452cf8965SKyle Evans}
89552cf8965SKyle Evansmflag_body()
89652cf8965SKyle Evans{
89752cf8965SKyle Evans	printf "a\nb\nc\nd\ne\nf\n" > test1
89852cf8965SKyle Evans
89952cf8965SKyle Evans	atf_check -o inline:"1\n" grep -m 1 -Ec "a" test1
90052cf8965SKyle Evans	atf_check -o inline:"2\n" grep -m 2 -Ec "a|b" test1
90152cf8965SKyle Evans	atf_check -o inline:"3\n" grep -m 3 -Ec "a|b|c|f" test1
90252cf8965SKyle Evans
90352cf8965SKyle Evans	atf_check -o inline:"test1:2\n" grep -m 2 -EHc "a|b|e|f" test1
90452cf8965SKyle Evans}
90563c8336dSEric van Gyzen
9063e2d96acSKyle Evansatf_test_case mflag_trail_ctx
9073e2d96acSKyle Evansmflag_trail_ctx_head()
9083e2d96acSKyle Evans{
9093e2d96acSKyle Evans	atf_set "descr" "Check proper handling of -m with trailing context (PR 253350)"
9103e2d96acSKyle Evans}
9113e2d96acSKyle Evansmflag_trail_ctx_body()
9123e2d96acSKyle Evans{
9133e2d96acSKyle Evans	printf "foo\nfoo\nbar\nfoo\nbar\nfoo\nbar\n" > test1
9143e2d96acSKyle Evans
9153e2d96acSKyle Evans	# Should pick up the next line after matching the first.
9163e2d96acSKyle Evans	atf_check -o inline:"foo\nfoo\n" grep -A1 -m1 foo test1
9173e2d96acSKyle Evans
9183e2d96acSKyle Evans	# Make sure the trailer is picked up as a non-match!
9193e2d96acSKyle Evans	atf_check -o inline:"1:foo\n2-foo\n" grep -A1 -nm1 foo test1
9203e2d96acSKyle Evans}
9213e2d96acSKyle Evans
92263c8336dSEric van Gyzenatf_test_case zgrep_multiple_files
92363c8336dSEric van Gyzenzgrep_multiple_files_head()
92463c8336dSEric van Gyzen{
92563c8336dSEric van Gyzen	atf_set "descr" "Ensures that zgrep functions properly with multiple files"
92663c8336dSEric van Gyzen}
92763c8336dSEric van Gyzenzgrep_multiple_files_body()
92863c8336dSEric van Gyzen{
92963c8336dSEric van Gyzen	echo foo > test1
93063c8336dSEric van Gyzen	echo foo > test2
93163c8336dSEric van Gyzen	atf_check -o inline:"test1:foo\ntest2:foo\n" zgrep foo test1 test2
93263c8336dSEric van Gyzen
93363c8336dSEric van Gyzen	echo bar > test1
93463c8336dSEric van Gyzen	atf_check -o inline:"test2:foo\n" zgrep foo test1 test2
93563c8336dSEric van Gyzen
93663c8336dSEric van Gyzen	echo bar > test2
93763c8336dSEric van Gyzen	atf_check -s exit:1 zgrep foo test1 test2
93863c8336dSEric van Gyzen}
939*1070477cSCraig Leres
940*1070477cSCraig Leresatf_test_case zgrep_recursive
941*1070477cSCraig Lereszgrep_multiple_files_head()
942*1070477cSCraig Leres{
943*1070477cSCraig Leres	atf_set "descr" "Checks for zgrep wrapper recursion"
944*1070477cSCraig Leres}
945*1070477cSCraig Lereszgrep_recursive_body()
946*1070477cSCraig Leres{
947*1070477cSCraig Leres	atf_expect_fail "unimplemented zgrep wrapper script functionality"
948*1070477cSCraig Leres
949*1070477cSCraig Leres	mkdir -p tester1
950*1070477cSCraig Leres	echo foobar > tester1/test
951*1070477cSCraig Leres	atf_check -o inline:"tester1/test:foobar\n" zgrep -r foo tester1
952*1070477cSCraig Leres
953*1070477cSCraig Leres	mkdir -p tester2
954*1070477cSCraig Leres	echo foobar > tester2/test1
955*1070477cSCraig Leres	echo foobar > tester2/test2
956*1070477cSCraig Leres	atf_check -o inline:"tester2/test1:foobar\ntester2/test2:foobar\n" zgrep -r foo tester2
957*1070477cSCraig Leres}
958799c5faaSEd Maste# End FreeBSD
95957718be8SEnji Cooper
96057718be8SEnji Cooperatf_init_test_cases()
96157718be8SEnji Cooper{
96257718be8SEnji Cooper	atf_add_test_case basic
96357718be8SEnji Cooper	atf_add_test_case binary
96457718be8SEnji Cooper	atf_add_test_case recurse
96557718be8SEnji Cooper	atf_add_test_case recurse_symlink
96657718be8SEnji Cooper	atf_add_test_case word_regexps
96757718be8SEnji Cooper	atf_add_test_case begin_end
96857718be8SEnji Cooper	atf_add_test_case ignore_case
96957718be8SEnji Cooper	atf_add_test_case invert
97057718be8SEnji Cooper	atf_add_test_case whole_line
97157718be8SEnji Cooper	atf_add_test_case negative
97257718be8SEnji Cooper	atf_add_test_case context
97357718be8SEnji Cooper	atf_add_test_case file_exp
97457718be8SEnji Cooper	atf_add_test_case egrep
97557718be8SEnji Cooper	atf_add_test_case zgrep
976c4cbf1fbSCraig Leres	atf_add_test_case zgrep_combined_flags
977c4cbf1fbSCraig Leres	atf_add_test_case zgrep_eflag
978c4cbf1fbSCraig Leres	atf_add_test_case zgrep_empty_eflag
979c4cbf1fbSCraig Leres	atf_add_test_case zgrep_fflag
980c4cbf1fbSCraig Leres	atf_add_test_case zgrep_long_eflag
981c4cbf1fbSCraig Leres	atf_add_test_case zgrep_multiple_eflags
98257718be8SEnji Cooper	atf_add_test_case nonexistent
98357718be8SEnji Cooper	atf_add_test_case context2
984799c5faaSEd Maste# Begin FreeBSD
985799c5faaSEd Maste	atf_add_test_case oflag_zerolen
986799c5faaSEd Maste	atf_add_test_case xflag
987799c5faaSEd Maste	atf_add_test_case color
988799c5faaSEd Maste	atf_add_test_case f_file_empty
989799c5faaSEd Maste	atf_add_test_case escmap
990799c5faaSEd Maste	atf_add_test_case egrep_empty_invalid
991e06ffa32SEd Maste	atf_add_test_case zerolen
992a4f3f02bSEd Maste	atf_add_test_case wflag_emptypat
99338325e2aSKyle Evans	atf_add_test_case xflag_emptypat
99438325e2aSKyle Evans	atf_add_test_case xflag_emptypat_plus
995f823c6dcSKyle Evans	atf_add_test_case emptyfile
996fe8c9d5bSEd Maste	atf_add_test_case excessive_matches
997945fc991SEd Maste	atf_add_test_case wv_combo_break
9985199917cSEnji Cooper	atf_add_test_case fgrep_sanity
9995199917cSEnji Cooper	atf_add_test_case egrep_sanity
10005199917cSEnji Cooper	atf_add_test_case grep_sanity
10016d635d3bSEd Maste	atf_add_test_case ocolor_metadata
1002e2127de8SEd Maste	atf_add_test_case grep_nomatch_flags
1003f701bdc5SEd Maste	atf_add_test_case binary_flags
1004b5fc583cSEd Maste	atf_add_test_case badcontext
1005d7b65474SEd Maste	atf_add_test_case mmap
10060e957942SKyle Evans	atf_add_test_case matchall
1007c552f48bSKyle Evans	atf_add_test_case fgrep_multipattern
1008c552f48bSKyle Evans	atf_add_test_case fgrep_icase
1009c552f48bSKyle Evans	atf_add_test_case fgrep_oflag
1010ce024bdcSKyle Evans	atf_add_test_case cflag
101152cf8965SKyle Evans	atf_add_test_case mflag
10123e2d96acSKyle Evans	atf_add_test_case mflag_trail_ctx
101363c8336dSEric van Gyzen	atf_add_test_case zgrep_multiple_files
1014*1070477cSCraig Leres	atf_add_test_case zgrep_recursive
1015799c5faaSEd Maste# End FreeBSD
101657718be8SEnji Cooper}
1017