xref: /freebsd/contrib/netbsd-tests/usr.bin/grep/t_grep.sh (revision 0e957942835a8b1deec77006194e295bbc1b8e1d)
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" \
8457718be8SEnji Cooper	    grep -r 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
21757718be8SEnji Cooperatf_test_case nonexistent
21857718be8SEnji Coopernonexistent_head()
21957718be8SEnji Cooper{
22057718be8SEnji Cooper	atf_set "descr" "Checks that -s flag suppresses error" \
22157718be8SEnji Cooper	                "messages about nonexistent files"
22257718be8SEnji Cooper}
22357718be8SEnji Coopernonexistent_body()
22457718be8SEnji Cooper{
22557718be8SEnji Cooper	atf_check -s ne:0 grep -s foobar nonexistent
22657718be8SEnji Cooper}
22757718be8SEnji Cooper
22857718be8SEnji Cooperatf_test_case context2
22957718be8SEnji Coopercontext2_head()
23057718be8SEnji Cooper{
23157718be8SEnji Cooper	atf_set "descr" "Checks displaying context with -z flag"
23257718be8SEnji Cooper}
23357718be8SEnji Coopercontext2_body()
23457718be8SEnji Cooper{
23557718be8SEnji Cooper	printf "haddock\000cod\000plaice\000" > test1
23657718be8SEnji Cooper	printf "mackeral\000cod\000crab\000" > test2
23757718be8SEnji Cooper
23857718be8SEnji Cooper	atf_check -o file:"$(atf_get_srcdir)/d_context2_a.out" \
23957718be8SEnji Cooper	    grep -z -A1 cod test1 test2
24057718be8SEnji Cooper
24157718be8SEnji Cooper	atf_check -o file:"$(atf_get_srcdir)/d_context2_b.out" \
24257718be8SEnji Cooper	    grep -z -B1 cod test1 test2
24357718be8SEnji Cooper
24457718be8SEnji Cooper	atf_check -o file:"$(atf_get_srcdir)/d_context2_c.out" \
24557718be8SEnji Cooper	    grep -z -C1 cod test1 test2
24657718be8SEnji Cooper}
247799c5faaSEd Maste# Begin FreeBSD
2489f67a481SEnji Cooper
2499f67a481SEnji Cooper# What grep(1) are we working with?
2509f67a481SEnji Cooper# - 0 : bsdgrep
2519f67a481SEnji Cooper# - 1 : gnu grep 2.51 (base)
2529f67a481SEnji Cooper# - 2 : gnu grep (ports)
2539f67a481SEnji CooperGREP_TYPE_BSD=0
2549f67a481SEnji CooperGREP_TYPE_GNU_FREEBSD=1
2559f67a481SEnji CooperGREP_TYPE_GNU=2
2569f67a481SEnji CooperGREP_TYPE_UNKNOWN=3
2579f67a481SEnji Cooper
2589f67a481SEnji Coopergrep_type()
2599f67a481SEnji Cooper{
2609f67a481SEnji Cooper	local grep_version=$(grep --version)
2619f67a481SEnji Cooper
2629f67a481SEnji Cooper	case "$grep_version" in
2639f67a481SEnji Cooper	*"BSD grep"*)
2649f67a481SEnji Cooper		return $GREP_TYPE_BSD
2659f67a481SEnji Cooper		;;
2669f67a481SEnji Cooper	*"GNU grep"*)
2679f67a481SEnji Cooper		case "$grep_version" in
2689f67a481SEnji Cooper		*2.5.1-FreeBSD*)
2699f67a481SEnji Cooper			return $GREP_TYPE_GNU_FREEBSD
2709f67a481SEnji Cooper			;;
2719f67a481SEnji Cooper		*)
2729f67a481SEnji Cooper			return $GREP_TYPE_GNU
2739f67a481SEnji Cooper			;;
2749f67a481SEnji Cooper		esac
2759f67a481SEnji Cooper		;;
2769f67a481SEnji Cooper	esac
2779f67a481SEnji Cooper	atf_fail "unknown grep type: $grep_version"
2789f67a481SEnji Cooper}
2799f67a481SEnji Cooper
280799c5faaSEd Masteatf_test_case oflag_zerolen
281799c5faaSEd Masteoflag_zerolen_head()
282799c5faaSEd Maste{
283799c5faaSEd Maste	atf_set "descr" "Check behavior of zero-length matches with -o flag (PR 195763)"
284799c5faaSEd Maste}
285799c5faaSEd Masteoflag_zerolen_body()
286799c5faaSEd Maste{
2879f67a481SEnji Cooper	grep_type
2889f67a481SEnji Cooper	if [ $? -eq $GREP_TYPE_GNU_FREEBSD ]; then
2899f67a481SEnji Cooper		atf_expect_fail "this test doesn't pass with gnu grep in base"
2909f67a481SEnji Cooper	fi
2919f67a481SEnji Cooper
292799c5faaSEd Maste	atf_check -o file:"$(atf_get_srcdir)/d_oflag_zerolen_a.out" \
293799c5faaSEd Maste	    grep -Eo '(^|:)0*' "$(atf_get_srcdir)/d_oflag_zerolen_a.in"
294799c5faaSEd Maste
295799c5faaSEd Maste	atf_check -o file:"$(atf_get_srcdir)/d_oflag_zerolen_b.out" \
296799c5faaSEd Maste	    grep -Eo '(^|:)0*' "$(atf_get_srcdir)/d_oflag_zerolen_b.in"
297799c5faaSEd Maste
298799c5faaSEd Maste	atf_check -o file:"$(atf_get_srcdir)/d_oflag_zerolen_c.out" \
299799c5faaSEd Maste	    grep -Eo '[[:alnum:]]*' "$(atf_get_srcdir)/d_oflag_zerolen_c.in"
300799c5faaSEd Maste
301799c5faaSEd Maste	atf_check -o empty grep -Eo '' "$(atf_get_srcdir)/d_oflag_zerolen_d.in"
302799c5faaSEd Maste
303799c5faaSEd Maste	atf_check -o file:"$(atf_get_srcdir)/d_oflag_zerolen_e.out" \
304799c5faaSEd Maste	    grep -o -e 'ab' -e 'bc' "$(atf_get_srcdir)/d_oflag_zerolen_e.in"
305799c5faaSEd Maste
306799c5faaSEd Maste	atf_check -o file:"$(atf_get_srcdir)/d_oflag_zerolen_e.out" \
307799c5faaSEd Maste	    grep -o -e 'bc' -e 'ab' "$(atf_get_srcdir)/d_oflag_zerolen_e.in"
308799c5faaSEd Maste}
309799c5faaSEd Maste
310799c5faaSEd Masteatf_test_case xflag
311799c5faaSEd Mastexflag_head()
312799c5faaSEd Maste{
313799c5faaSEd Maste	atf_set "descr" "Check that we actually get a match with -x flag (PR 180990)"
314799c5faaSEd Maste}
315799c5faaSEd Mastexflag_body()
316799c5faaSEd Maste{
317799c5faaSEd Maste	echo 128 > match_file
318799c5faaSEd Maste	seq 1 128 > pattern_file
319799c5faaSEd Maste	grep -xf pattern_file match_file
320799c5faaSEd Maste}
321799c5faaSEd Maste
322799c5faaSEd Masteatf_test_case color
323799c5faaSEd Mastecolor_head()
324799c5faaSEd Maste{
325799c5faaSEd Maste	atf_set "descr" "Check --color support"
326799c5faaSEd Maste}
327799c5faaSEd Mastecolor_body()
328799c5faaSEd Maste{
3299f67a481SEnji Cooper	grep_type
3309f67a481SEnji Cooper	if [ $? -eq $GREP_TYPE_GNU_FREEBSD ]; then
3319f67a481SEnji Cooper		atf_expect_fail "this test doesn't pass with gnu grep in base"
3329f67a481SEnji Cooper	fi
3339f67a481SEnji Cooper
334799c5faaSEd Maste	echo 'abcd*' > grepfile
335799c5faaSEd Maste	echo 'abc$' >> grepfile
336799c5faaSEd Maste	echo '^abc' >> grepfile
337799c5faaSEd Maste
338799c5faaSEd Maste	atf_check -o file:"$(atf_get_srcdir)/d_color_a.out" \
339799c5faaSEd Maste	    grep --color=auto -e '.*' -e 'a' "$(atf_get_srcdir)/d_color_a.in"
340799c5faaSEd Maste
341799c5faaSEd Maste	atf_check -o file:"$(atf_get_srcdir)/d_color_b.out" \
342799c5faaSEd Maste	    grep --color=auto -f grepfile "$(atf_get_srcdir)/d_color_b.in"
343799c5faaSEd Maste
344799c5faaSEd Maste	atf_check -o file:"$(atf_get_srcdir)/d_color_c.out" \
345799c5faaSEd Maste	    grep --color=always -f grepfile "$(atf_get_srcdir)/d_color_b.in"
346799c5faaSEd Maste}
347799c5faaSEd Maste
348799c5faaSEd Masteatf_test_case f_file_empty
349799c5faaSEd Mastef_file_empty_head()
350799c5faaSEd Maste{
351799c5faaSEd Maste	atf_set "descr" "Check for handling of a null byte in empty file, specified by -f (PR 202022)"
352799c5faaSEd Maste}
353799c5faaSEd Mastef_file_empty_body()
354799c5faaSEd Maste{
355799c5faaSEd Maste	printf "\0\n" > nulpat
356799c5faaSEd Maste
357799c5faaSEd Maste	atf_check -s exit:1 grep -f nulpat "$(atf_get_srcdir)/d_f_file_empty.in"
358799c5faaSEd Maste}
359799c5faaSEd Maste
360799c5faaSEd Masteatf_test_case escmap
361799c5faaSEd Masteescmap_head()
362799c5faaSEd Maste{
363799c5faaSEd Maste	atf_set "descr" "Check proper handling of escaped vs. unescaped dot expressions (PR 175314)"
364799c5faaSEd Maste}
365799c5faaSEd Masteescmap_body()
366799c5faaSEd Maste{
367799c5faaSEd Maste	atf_check -s exit:1 grep -o 'f.o\.' "$(atf_get_srcdir)/d_escmap.in"
368799c5faaSEd Maste	atf_check -o not-empty grep -o 'f.o.' "$(atf_get_srcdir)/d_escmap.in"
369799c5faaSEd Maste}
370799c5faaSEd Maste
371799c5faaSEd Masteatf_test_case egrep_empty_invalid
372799c5faaSEd Masteegrep_empty_invalid_head()
373799c5faaSEd Maste{
374799c5faaSEd Maste	atf_set "descr" "Check for handling of an invalid empty pattern (PR 194823)"
375799c5faaSEd Maste}
376799c5faaSEd Masteegrep_empty_invalid_body()
377799c5faaSEd Maste{
378befd089cSEd Maste	atf_check -e ignore -s not-exit:0 egrep '{' /dev/null
379799c5faaSEd Maste}
380e06ffa32SEd Maste
381e06ffa32SEd Masteatf_test_case zerolen
382e06ffa32SEd Mastezerolen_head()
383e06ffa32SEd Maste{
384e06ffa32SEd Maste	atf_set "descr" "Check for successful zero-length matches with ^$"
385e06ffa32SEd Maste}
386e06ffa32SEd Mastezerolen_body()
387e06ffa32SEd Maste{
388e06ffa32SEd Maste	printf "Eggs\n\nCheese" > test1
389e06ffa32SEd Maste
390e06ffa32SEd Maste	atf_check -o inline:"\n" grep -e "^$" test1
391e06ffa32SEd Maste
392e06ffa32SEd Maste	atf_check -o inline:"Eggs\nCheese\n" grep -v -e "^$" test1
393e06ffa32SEd Maste}
3945199917cSEnji Cooper
395a4f3f02bSEd Masteatf_test_case wflag_emptypat
396a4f3f02bSEd Mastewflag_emptypat_head()
397a4f3f02bSEd Maste{
398a4f3f02bSEd Maste	atf_set "descr" "Check for proper handling of -w with an empty pattern (PR 105221)"
399a4f3f02bSEd Maste}
400a4f3f02bSEd Mastewflag_emptypat_body()
401a4f3f02bSEd Maste{
402a4f3f02bSEd Maste	printf "" > test1
403a4f3f02bSEd Maste	printf "\n" > test2
404a4f3f02bSEd Maste	printf "qaz" > test3
405a4f3f02bSEd Maste	printf " qaz\n" > test4
406a4f3f02bSEd Maste
407a4f3f02bSEd Maste	atf_check -s exit:1 -o empty grep -w -e "" test1
408a4f3f02bSEd Maste
409a4f3f02bSEd Maste	atf_check -o file:test2 grep -w -e "" test2
410a4f3f02bSEd Maste
411a4f3f02bSEd Maste	atf_check -s exit:1 -o empty grep -w -e "" test3
412a4f3f02bSEd Maste
413a4f3f02bSEd Maste	atf_check -o file:test4 grep -w -e "" test4
414a4f3f02bSEd Maste}
415a4f3f02bSEd Maste
416fe8c9d5bSEd Masteatf_test_case excessive_matches
417fe8c9d5bSEd Masteexcessive_matches_head()
418fe8c9d5bSEd Maste{
419fe8c9d5bSEd Maste	atf_set "descr" "Check for proper handling of lines with excessive matches (PR 218811)"
420fe8c9d5bSEd Maste}
421fe8c9d5bSEd Masteexcessive_matches_body()
422fe8c9d5bSEd Maste{
423fe8c9d5bSEd Maste	grep_type
424fe8c9d5bSEd Maste	if [ $? -eq $GREP_TYPE_GNU_FREEBSD ]; then
425fe8c9d5bSEd Maste		atf_expect_fail "this test does not pass with GNU grep in base"
426fe8c9d5bSEd Maste	fi
427fe8c9d5bSEd Maste
428fe8c9d5bSEd Maste	for i in $(jot 4096); do
429fe8c9d5bSEd Maste		printf "x" >> test.in
430fe8c9d5bSEd Maste	done
431fe8c9d5bSEd Maste
432fe8c9d5bSEd Maste	atf_check -s exit:0 -x '[ $(grep -o x test.in | wc -l) -eq 4096 ]'
4336d635d3bSEd Maste	atf_check -s exit:1 -x 'grep -on x test.in | grep -v "1:x"'
434fe8c9d5bSEd Maste}
435fe8c9d5bSEd Maste
4365199917cSEnji Cooperatf_test_case fgrep_sanity
4375199917cSEnji Cooperfgrep_sanity_head()
4385199917cSEnji Cooper{
4395199917cSEnji Cooper	atf_set "descr" "Check for fgrep sanity, literal expressions only"
4405199917cSEnji Cooper}
4415199917cSEnji Cooperfgrep_sanity_body()
4425199917cSEnji Cooper{
4435199917cSEnji Cooper	printf "Foo" > test1
4445199917cSEnji Cooper
4455199917cSEnji Cooper	atf_check -o inline:"Foo\n" fgrep -e "Foo" test1
4465199917cSEnji Cooper
4475199917cSEnji Cooper	atf_check -s exit:1 -o empty fgrep -e "Fo." test1
4485199917cSEnji Cooper}
4495199917cSEnji Cooper
4505199917cSEnji Cooperatf_test_case egrep_sanity
4515199917cSEnji Cooperegrep_sanity_head()
4525199917cSEnji Cooper{
4535199917cSEnji Cooper	atf_set "descr" "Check for egrep sanity, EREs only"
4545199917cSEnji Cooper}
4555199917cSEnji Cooperegrep_sanity_body()
4565199917cSEnji Cooper{
4575199917cSEnji Cooper	printf "Foobar(ed)" > test1
4585199917cSEnji Cooper	printf "M{1}" > test2
4595199917cSEnji Cooper
4605199917cSEnji Cooper	atf_check -o inline:"Foo\n" egrep -o -e "F.." test1
4615199917cSEnji Cooper
4625199917cSEnji Cooper	atf_check -o inline:"Foobar\n" egrep -o -e "F[a-z]*" test1
4635199917cSEnji Cooper
4645199917cSEnji Cooper	atf_check -o inline:"Fo\n" egrep -o -e "F(o|p)" test1
4655199917cSEnji Cooper
4665199917cSEnji Cooper	atf_check -o inline:"(ed)\n" egrep -o -e "\(ed\)" test1
4675199917cSEnji Cooper
4685199917cSEnji Cooper	atf_check -o inline:"M\n" egrep -o -e "M{1}" test2
4695199917cSEnji Cooper
4705199917cSEnji Cooper	atf_check -o inline:"M{1}\n" egrep -o -e "M\{1\}" test2
4715199917cSEnji Cooper}
4725199917cSEnji Cooper
4735199917cSEnji Cooperatf_test_case grep_sanity
4745199917cSEnji Coopergrep_sanity_head()
4755199917cSEnji Cooper{
4765199917cSEnji Cooper	atf_set "descr" "Check for basic grep sanity, BREs only"
4775199917cSEnji Cooper}
4785199917cSEnji Coopergrep_sanity_body()
4795199917cSEnji Cooper{
4805199917cSEnji Cooper	printf "Foobar(ed)" > test1
4815199917cSEnji Cooper	printf "M{1}" > test2
4825199917cSEnji Cooper
4835199917cSEnji Cooper	atf_check -o inline:"Foo\n" grep -o -e "F.." test1
4845199917cSEnji Cooper
4855199917cSEnji Cooper	atf_check -o inline:"Foobar\n" grep -o -e "F[a-z]*" test1
4865199917cSEnji Cooper
4875199917cSEnji Cooper	atf_check -o inline:"Fo\n" grep -o -e "F\(o\)" test1
4885199917cSEnji Cooper
4895199917cSEnji Cooper	atf_check -o inline:"(ed)\n" grep -o -e "(ed)" test1
4905199917cSEnji Cooper
4915199917cSEnji Cooper	atf_check -o inline:"M{1}\n" grep -o -e "M{1}" test2
4925199917cSEnji Cooper
4935199917cSEnji Cooper	atf_check -o inline:"M\n" grep -o -e "M\{1\}" test2
4945199917cSEnji Cooper}
495945fc991SEd Maste
496945fc991SEd Masteatf_test_case wv_combo_break
497945fc991SEd Mastewv_combo_break_head()
498945fc991SEd Maste{
499945fc991SEd Maste	atf_set "descr" "Check for incorrectly matching lines with both -w and -v flags (PR 218467)"
500945fc991SEd Maste}
501945fc991SEd Mastewv_combo_break_body()
502945fc991SEd Maste{
503945fc991SEd Maste	printf "x xx\n" > test1
504945fc991SEd Maste	printf "xx x\n" > test2
505945fc991SEd Maste
506945fc991SEd Maste	atf_check -o file:test1 grep -w "x" test1
507945fc991SEd Maste	atf_check -o file:test2 grep -w "x" test2
508945fc991SEd Maste
509945fc991SEd Maste	atf_check -s exit:1 grep -v -w "x" test1
510945fc991SEd Maste	atf_check -s exit:1 grep -v -w "x" test2
511945fc991SEd Maste}
512e2127de8SEd Maste
5136d635d3bSEd Masteatf_test_case ocolor_metadata
5146d635d3bSEd Masteocolor_metadata_head()
5156d635d3bSEd Maste{
5166d635d3bSEd Maste	atf_set "descr" "Check for -n/-b producing per-line metadata output"
5176d635d3bSEd Maste}
5186d635d3bSEd Masteocolor_metadata_body()
5196d635d3bSEd Maste{
5206d635d3bSEd Maste	grep_type
5216d635d3bSEd Maste	if [ $? -eq $GREP_TYPE_GNU_FREEBSD ]; then
5226d635d3bSEd Maste		atf_expect_fail "this test does not pass with GNU grep in base"
5236d635d3bSEd Maste	fi
5246d635d3bSEd Maste
5256d635d3bSEd Maste	printf "xxx\nyyyy\nzzz\nfoobarbaz\n" > test1
5266d635d3bSEd Maste	check_expr="^[^:]*[0-9][^:]*:[^:]+$"
5276d635d3bSEd Maste
5286d635d3bSEd Maste	atf_check -o inline:"1:1:xx\n" grep -bon "xx$" test1
5296d635d3bSEd Maste
5306d635d3bSEd Maste	atf_check -o inline:"2:4:yyyy\n" grep -bn "yy" test1
5316d635d3bSEd Maste
5326d635d3bSEd Maste	atf_check -o inline:"2:6:yy\n" grep -bon "yy$" test1
5336d635d3bSEd Maste
5346d635d3bSEd Maste	# These checks ensure that grep isn't producing bogus line numbering
5356d635d3bSEd Maste	# in the middle of a line.
5366d635d3bSEd Maste	atf_check -s exit:1 -x \
5376d635d3bSEd Maste	    "grep -Eon 'x|y|z|f' test1 | grep -Ev '${check_expr}'"
5386d635d3bSEd Maste
5396d635d3bSEd Maste	atf_check -s exit:1 -x \
5406d635d3bSEd Maste	    "grep -En 'x|y|z|f' --color=always test1 | grep -Ev '${check_expr}'"
5416d635d3bSEd Maste
5426d635d3bSEd Maste	atf_check -s exit:1 -x \
5436d635d3bSEd Maste	    "grep -Eon 'x|y|z|f' --color=always test1 | grep -Ev '${check_expr}'"
5446d635d3bSEd Maste}
5456d635d3bSEd Maste
546e2127de8SEd Masteatf_test_case grep_nomatch_flags
547e2127de8SEd Mastegrep_nomatch_flags_head()
548e2127de8SEd Maste{
549e2127de8SEd Maste	atf_set "descr" "Check for no match (-c, -l, -L, -q) flags not producing line matches or context (PR 219077)"
550e2127de8SEd Maste}
551e2127de8SEd Maste
552e2127de8SEd Mastegrep_nomatch_flags_body()
553e2127de8SEd Maste{
554e2127de8SEd Maste	printf "A\nB\nC\n" > test1
555e2127de8SEd Maste
556e2127de8SEd Maste	atf_check -o inline:"1\n" grep -c -C 1 -e "B" test1
557e2127de8SEd Maste	atf_check -o inline:"1\n" grep -c -B 1 -e "B" test1
558e2127de8SEd Maste	atf_check -o inline:"1\n" grep -c -A 1 -e "B" test1
559e2127de8SEd Maste	atf_check -o inline:"1\n" grep -c -C 1 -e "B" test1
560e2127de8SEd Maste
561e2127de8SEd Maste	atf_check -o inline:"test1\n" grep -l -e "B" test1
562e2127de8SEd Maste	atf_check -o inline:"test1\n" grep -l -B 1 -e "B" test1
563e2127de8SEd Maste	atf_check -o inline:"test1\n" grep -l -A 1 -e "B" test1
564e2127de8SEd Maste	atf_check -o inline:"test1\n" grep -l -C 1 -e "B" test1
565e2127de8SEd Maste
566e2127de8SEd Maste	atf_check -s exit:1 -o inline:"test1\n" grep -L -e "D" test1
567e2127de8SEd Maste
568e2127de8SEd Maste	atf_check -o empty grep -q -e "B" test1
569e2127de8SEd Maste	atf_check -o empty grep -q -B 1 -e "B" test1
570e2127de8SEd Maste	atf_check -o empty grep -q -A 1 -e "B" test1
571e2127de8SEd Maste	atf_check -o empty grep -q -C 1 -e "B" test1
572e2127de8SEd Maste}
573b5fc583cSEd Maste
574b5fc583cSEd Masteatf_test_case badcontext
575b5fc583cSEd Mastebadcontext_head()
576b5fc583cSEd Maste{
577b5fc583cSEd Maste	atf_set "descr" "Check for handling of invalid context arguments"
578b5fc583cSEd Maste}
579b5fc583cSEd Mastebadcontext_body()
580b5fc583cSEd Maste{
581b5fc583cSEd Maste	printf "A\nB\nC\n" > test1
582b5fc583cSEd Maste
583b5fc583cSEd Maste	atf_check -s not-exit:0 -e ignore grep -A "-1" "B" test1
584b5fc583cSEd Maste
585b5fc583cSEd Maste	atf_check -s not-exit:0 -e ignore grep -B "-1" "B" test1
586b5fc583cSEd Maste
587b5fc583cSEd Maste	atf_check -s not-exit:0 -e ignore grep -C "-1" "B" test1
588b5fc583cSEd Maste
589b5fc583cSEd Maste	atf_check -s not-exit:0 -e ignore grep -A "B" "B" test1
590b5fc583cSEd Maste
591b5fc583cSEd Maste	atf_check -s not-exit:0 -e ignore grep -B "B" "B" test1
592b5fc583cSEd Maste
593b5fc583cSEd Maste	atf_check -s not-exit:0 -e ignore grep -C "B" "B" test1
594b5fc583cSEd Maste}
595f701bdc5SEd Maste
596f701bdc5SEd Masteatf_test_case binary_flags
597f701bdc5SEd Mastebinary_flags_head()
598f701bdc5SEd Maste{
599f701bdc5SEd Maste	atf_set "descr" "Check output for binary flags (-a, -I, -U, --binary-files)"
600f701bdc5SEd Maste}
601f701bdc5SEd Mastebinary_flags_body()
602f701bdc5SEd Maste{
603f701bdc5SEd Maste	printf "A\000B\000C" > test1
604f701bdc5SEd Maste	printf "A\n\000B\n\000C" > test2
605f701bdc5SEd Maste	binmatchtext="Binary file test1 matches\n"
606f701bdc5SEd Maste
607f701bdc5SEd Maste	# Binaries not treated as text (default, -U)
608f701bdc5SEd Maste	atf_check -o inline:"${binmatchtext}" grep 'B' test1
609f701bdc5SEd Maste	atf_check -o inline:"${binmatchtext}" grep 'B' -C 1 test1
610f701bdc5SEd Maste
611f701bdc5SEd Maste	atf_check -o inline:"${binmatchtext}" grep -U 'B' test1
612f701bdc5SEd Maste	atf_check -o inline:"${binmatchtext}" grep -U 'B' -C 1 test1
613f701bdc5SEd Maste
614f701bdc5SEd Maste	# Binary, -a, no newlines
615f701bdc5SEd Maste	atf_check -o inline:"A\000B\000C\n" grep -a 'B' test1
616f701bdc5SEd Maste	atf_check -o inline:"A\000B\000C\n" grep -a 'B' -C 1 test1
617f701bdc5SEd Maste
618f701bdc5SEd Maste	# Binary, -a, newlines
619f701bdc5SEd Maste	atf_check -o inline:"\000B\n" grep -a 'B' test2
620f701bdc5SEd Maste	atf_check -o inline:"A\n\000B\n\000C\n" grep -a 'B' -C 1 test2
621f701bdc5SEd Maste
622f701bdc5SEd Maste	# Binary files ignored
623f701bdc5SEd Maste	atf_check -s exit:1 grep -I 'B' test2
624f701bdc5SEd Maste
625f701bdc5SEd Maste	# --binary-files equivalence
626f701bdc5SEd Maste	atf_check -o inline:"${binmatchtext}" grep --binary-files=binary 'B' test1
627f701bdc5SEd Maste	atf_check -o inline:"A\000B\000C\n" grep --binary-files=text 'B' test1
628f701bdc5SEd Maste	atf_check -s exit:1 grep --binary-files=without-match 'B' test2
629f701bdc5SEd Maste}
630d7b65474SEd Maste
631d7b65474SEd Masteatf_test_case mmap
632d7b65474SEd Mastemmap_head()
633d7b65474SEd Maste{
634d7b65474SEd Maste	atf_set "descr" "Check basic matching with --mmap flag"
635d7b65474SEd Maste}
636d7b65474SEd Mastemmap_body()
637d7b65474SEd Maste{
638d7b65474SEd Maste	grep_type
639d7b65474SEd Maste	if [ $? -eq $GREP_TYPE_GNU ]; then
640d7b65474SEd Maste		atf_expect_fail "gnu grep from ports has no --mmap option"
641d7b65474SEd Maste	fi
642d7b65474SEd Maste
643d7b65474SEd Maste	printf "A\nB\nC\n" > test1
644d7b65474SEd Maste
645d7b65474SEd Maste	atf_check -s exit:0 -o inline:"B\n" grep --mmap -oe "B" test1
646d7b65474SEd Maste	atf_check -s exit:1 grep --mmap -e "Z" test1
647d7b65474SEd Maste}
648d7b65474SEd Maste
649d7b65474SEd Masteatf_test_case mmap_eof_not_eol
650d7b65474SEd Mastemmap_eof_not_eol_head()
651d7b65474SEd Maste{
652d7b65474SEd Maste	atf_set "descr" "Check --mmap flag handling of encountering EOF without EOL (PR 165471, 219402)"
653d7b65474SEd Maste}
654d7b65474SEd Mastemmap_eof_not_eol_body()
655d7b65474SEd Maste{
656d7b65474SEd Maste	grep_type
657d7b65474SEd Maste	if [ $? -eq $GREP_TYPE_GNU ]; then
658d7b65474SEd Maste		atf_expect_fail "gnu grep from ports has no --mmap option"
659d7b65474SEd Maste	fi
660d7b65474SEd Maste
66169a3d9e7SEnji Cooper	atf_expect_fail "relies on jemalloc feature no longer available; needs to be rewritten - bug 220309"
66269a3d9e7SEnji Cooper
663d7b65474SEd Maste	printf "ABC" > test1
664d7b65474SEd Maste	jot -b " "  -s "" 4096 >> test2
665d7b65474SEd Maste
666d7b65474SEd Maste	atf_check -s exit:0 -o inline:"B\n" grep --mmap -oe "B" test1
667d7b65474SEd Maste	# Dependency on jemalloc(3) to detect buffer overflow, otherwise this
668d7b65474SEd Maste	# unreliably produces a SIGSEGV or SIGBUS
669d7b65474SEd Maste	atf_check -s exit:0 -o not-empty \
670d7b65474SEd Maste	    env MALLOC_CONF="redzone:true" grep --mmap -e " " test2
671d7b65474SEd Maste}
672*0e957942SKyle Evans
673*0e957942SKyle Evansatf_test_case matchall
674*0e957942SKyle Evansmatchall_head()
675*0e957942SKyle Evans{
676*0e957942SKyle Evans	atf_set "descr" "Check proper behavior of matching all with an empty string"
677*0e957942SKyle Evans}
678*0e957942SKyle Evansmatchall_body()
679*0e957942SKyle Evans{
680*0e957942SKyle Evans	printf "" > test1
681*0e957942SKyle Evans	printf "A" > test2
682*0e957942SKyle Evans	printf "A\nB" > test3
683*0e957942SKyle Evans
684*0e957942SKyle Evans	atf_check -o inline:"test2:A\ntest3:A\ntest3:B\n" grep "" test1 test2 test3
685*0e957942SKyle Evans	atf_check -o inline:"test3:A\ntest3:B\ntest2:A\n" grep "" test3 test1 test2
686*0e957942SKyle Evans	atf_check -o inline:"test2:A\ntest3:A\ntest3:B\n" grep "" test2 test3 test1
687*0e957942SKyle Evans
688*0e957942SKyle Evans	atf_check -s exit:1 grep "" test1
689*0e957942SKyle Evans}
690799c5faaSEd Maste# End FreeBSD
69157718be8SEnji Cooper
69257718be8SEnji Cooperatf_init_test_cases()
69357718be8SEnji Cooper{
69457718be8SEnji Cooper	atf_add_test_case basic
69557718be8SEnji Cooper	atf_add_test_case binary
69657718be8SEnji Cooper	atf_add_test_case recurse
69757718be8SEnji Cooper	atf_add_test_case recurse_symlink
69857718be8SEnji Cooper	atf_add_test_case word_regexps
69957718be8SEnji Cooper	atf_add_test_case begin_end
70057718be8SEnji Cooper	atf_add_test_case ignore_case
70157718be8SEnji Cooper	atf_add_test_case invert
70257718be8SEnji Cooper	atf_add_test_case whole_line
70357718be8SEnji Cooper	atf_add_test_case negative
70457718be8SEnji Cooper	atf_add_test_case context
70557718be8SEnji Cooper	atf_add_test_case file_exp
70657718be8SEnji Cooper	atf_add_test_case egrep
70757718be8SEnji Cooper	atf_add_test_case zgrep
70857718be8SEnji Cooper	atf_add_test_case nonexistent
70957718be8SEnji Cooper	atf_add_test_case context2
710799c5faaSEd Maste# Begin FreeBSD
711799c5faaSEd Maste	atf_add_test_case oflag_zerolen
712799c5faaSEd Maste	atf_add_test_case xflag
713799c5faaSEd Maste	atf_add_test_case color
714799c5faaSEd Maste	atf_add_test_case f_file_empty
715799c5faaSEd Maste	atf_add_test_case escmap
716799c5faaSEd Maste	atf_add_test_case egrep_empty_invalid
717e06ffa32SEd Maste	atf_add_test_case zerolen
718a4f3f02bSEd Maste	atf_add_test_case wflag_emptypat
719fe8c9d5bSEd Maste	atf_add_test_case excessive_matches
720945fc991SEd Maste	atf_add_test_case wv_combo_break
7215199917cSEnji Cooper	atf_add_test_case fgrep_sanity
7225199917cSEnji Cooper	atf_add_test_case egrep_sanity
7235199917cSEnji Cooper	atf_add_test_case grep_sanity
7246d635d3bSEd Maste	atf_add_test_case ocolor_metadata
725e2127de8SEd Maste	atf_add_test_case grep_nomatch_flags
726f701bdc5SEd Maste	atf_add_test_case binary_flags
727b5fc583cSEd Maste	atf_add_test_case badcontext
728d7b65474SEd Maste	atf_add_test_case mmap
729d7b65474SEd Maste	atf_add_test_case mmap_eof_not_eol
730*0e957942SKyle Evans	atf_add_test_case matchall
731799c5faaSEd Maste# End FreeBSD
73257718be8SEnji Cooper}
733