xref: /freebsd/contrib/netbsd-tests/usr.bin/grep/t_grep.sh (revision 4ca50eab86aec8001978a612a42f9ae8388eceab)
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	mkdir -p test/c/d
73	(cd test/c/d && ln -s ../d .)
74	echo "Test string" > test/c/match
75
76	atf_check -o file:"$(atf_get_srcdir)/d_recurse_symlink.out" \
77	    -e file:"$(atf_get_srcdir)/d_recurse_symlink.err" \
78	    grep -r string test
79}
80
81atf_test_case word_regexps
82word_regexps_head()
83{
84	atf_set "descr" "Checks word-regexps"
85}
86word_regexps_body()
87{
88	atf_check -o file:"$(atf_get_srcdir)/d_word_regexps.out" \
89	    grep -w separated $(atf_get_srcdir)/d_input
90}
91
92atf_test_case begin_end
93begin_end_head()
94{
95	atf_set "descr" "Checks handling of line beginnings and ends"
96}
97begin_end_body()
98{
99	atf_check -o file:"$(atf_get_srcdir)/d_begin_end_a.out" \
100	    grep ^Front "$(atf_get_srcdir)/d_input"
101
102	atf_check -o file:"$(atf_get_srcdir)/d_begin_end_b.out" \
103	    grep ending$ "$(atf_get_srcdir)/d_input"
104}
105
106atf_test_case ignore_case
107ignore_case_head()
108{
109	atf_set "descr" "Checks ignore-case option"
110}
111ignore_case_body()
112{
113	atf_check -o file:"$(atf_get_srcdir)/d_ignore_case.out" \
114	    grep -i Upper "$(atf_get_srcdir)/d_input"
115}
116
117atf_test_case invert
118invert_head()
119{
120	atf_set "descr" "Checks selecting non-matching lines with -v option"
121}
122invert_body()
123{
124	atf_check -o file:"$(atf_get_srcdir)/d_invert.out" \
125	    grep -v fish "$(atf_get_srcdir)/d_invert.in"
126}
127
128atf_test_case whole_line
129whole_line_head()
130{
131	atf_set "descr" "Checks whole-line matching with -x flag"
132}
133whole_line_body()
134{
135	atf_check -o file:"$(atf_get_srcdir)/d_whole_line.out" \
136	    grep -x matchme "$(atf_get_srcdir)/d_input"
137}
138
139atf_test_case negative
140negative_head()
141{
142	atf_set "descr" "Checks handling of files with no matches"
143}
144negative_body()
145{
146	atf_check -s ne:0 grep "not a hope in hell" "$(atf_get_srcdir)/d_input"
147}
148
149atf_test_case context
150context_head()
151{
152	atf_set "descr" "Checks displaying context with -A, -B and -C flags"
153}
154context_body()
155{
156	cp $(atf_get_srcdir)/d_context_*.* .
157
158	atf_check -o file:d_context_a.out grep -C2 bamboo d_context_a.in
159	atf_check -o file:d_context_b.out grep -A3 tilt d_context_a.in
160	atf_check -o file:d_context_c.out grep -B4 Whig d_context_a.in
161	atf_check -o file:d_context_d.out grep -C1 pig d_context_a.in d_context_b.in
162}
163
164atf_test_case file_exp
165file_exp_head()
166{
167	atf_set "descr" "Checks reading expressions from file"
168}
169file_exp_body()
170{
171	atf_check -o file:"$(atf_get_srcdir)/d_file_exp.out" -x \
172	    'jot 21 -1 1.00 | grep -f '"$(atf_get_srcdir)"'/d_file_exp.in'
173}
174
175atf_test_case egrep
176egrep_head()
177{
178	atf_set "descr" "Checks matching special characters with egrep"
179}
180egrep_body()
181{
182	atf_check -o file:"$(atf_get_srcdir)/d_egrep.out" \
183		egrep '\?|\*$$' "$(atf_get_srcdir)/d_input"
184}
185
186atf_test_case zgrep
187zgrep_head()
188{
189	atf_set "descr" "Checks handling of gzipped files with zgrep"
190}
191zgrep_body()
192{
193	cp "$(atf_get_srcdir)/d_input" .
194	gzip d_input || atf_fail "gzip failed"
195
196	atf_check -o file:"$(atf_get_srcdir)/d_zgrep.out" zgrep -h line d_input.gz
197}
198
199atf_test_case nonexistent
200nonexistent_head()
201{
202	atf_set "descr" "Checks that -s flag suppresses error" \
203	                "messages about nonexistent files"
204}
205nonexistent_body()
206{
207	atf_check -s ne:0 grep -s foobar nonexistent
208}
209
210atf_test_case context2
211context2_head()
212{
213	atf_set "descr" "Checks displaying context with -z flag"
214}
215context2_body()
216{
217	printf "haddock\000cod\000plaice\000" > test1
218	printf "mackeral\000cod\000crab\000" > test2
219
220	atf_check -o file:"$(atf_get_srcdir)/d_context2_a.out" \
221	    grep -z -A1 cod test1 test2
222
223	atf_check -o file:"$(atf_get_srcdir)/d_context2_b.out" \
224	    grep -z -B1 cod test1 test2
225
226	atf_check -o file:"$(atf_get_srcdir)/d_context2_c.out" \
227	    grep -z -C1 cod test1 test2
228}
229# Begin FreeBSD
230atf_test_case oflag_zerolen
231oflag_zerolen_head()
232{
233	atf_set "descr" "Check behavior of zero-length matches with -o flag (PR 195763)"
234}
235oflag_zerolen_body()
236{
237	atf_check -o file:"$(atf_get_srcdir)/d_oflag_zerolen_a.out" \
238	    grep -Eo '(^|:)0*' "$(atf_get_srcdir)/d_oflag_zerolen_a.in"
239
240	atf_check -o file:"$(atf_get_srcdir)/d_oflag_zerolen_b.out" \
241	    grep -Eo '(^|:)0*' "$(atf_get_srcdir)/d_oflag_zerolen_b.in"
242
243	atf_check -o file:"$(atf_get_srcdir)/d_oflag_zerolen_c.out" \
244	    grep -Eo '[[:alnum:]]*' "$(atf_get_srcdir)/d_oflag_zerolen_c.in"
245
246	atf_check -o empty grep -Eo '' "$(atf_get_srcdir)/d_oflag_zerolen_d.in"
247
248	atf_check -o file:"$(atf_get_srcdir)/d_oflag_zerolen_e.out" \
249	    grep -o -e 'ab' -e 'bc' "$(atf_get_srcdir)/d_oflag_zerolen_e.in"
250
251	atf_check -o file:"$(atf_get_srcdir)/d_oflag_zerolen_e.out" \
252	    grep -o -e 'bc' -e 'ab' "$(atf_get_srcdir)/d_oflag_zerolen_e.in"
253}
254
255atf_test_case xflag
256xflag_head()
257{
258	atf_set "descr" "Check that we actually get a match with -x flag (PR 180990)"
259}
260xflag_body()
261{
262	echo 128 > match_file
263	seq 1 128 > pattern_file
264	grep -xf pattern_file match_file
265}
266
267atf_test_case color
268color_head()
269{
270	atf_set "descr" "Check --color support"
271}
272color_body()
273{
274	echo 'abcd*' > grepfile
275	echo 'abc$' >> grepfile
276	echo '^abc' >> grepfile
277
278	atf_check -o file:"$(atf_get_srcdir)/d_color_a.out" \
279	    grep --color=auto -e '.*' -e 'a' "$(atf_get_srcdir)/d_color_a.in"
280
281	atf_check -o file:"$(atf_get_srcdir)/d_color_b.out" \
282	    grep --color=auto -f grepfile "$(atf_get_srcdir)/d_color_b.in"
283
284	atf_check -o file:"$(atf_get_srcdir)/d_color_c.out" \
285	    grep --color=always -f grepfile "$(atf_get_srcdir)/d_color_b.in"
286}
287
288atf_test_case f_file_empty
289f_file_empty_head()
290{
291	atf_set "descr" "Check for handling of a null byte in empty file, specified by -f (PR 202022)"
292}
293f_file_empty_body()
294{
295	printf "\0\n" > nulpat
296
297	atf_check -s exit:1 grep -f nulpat "$(atf_get_srcdir)/d_f_file_empty.in"
298}
299
300atf_test_case escmap
301escmap_head()
302{
303	atf_set "descr" "Check proper handling of escaped vs. unescaped dot expressions (PR 175314)"
304}
305escmap_body()
306{
307	atf_check -s exit:1 grep -o 'f.o\.' "$(atf_get_srcdir)/d_escmap.in"
308	atf_check -o not-empty grep -o 'f.o.' "$(atf_get_srcdir)/d_escmap.in"
309}
310
311atf_test_case egrep_empty_invalid
312egrep_empty_invalid_head()
313{
314	atf_set "descr" "Check for handling of an invalid empty pattern (PR 194823)"
315}
316egrep_empty_invalid_body()
317{
318	atf_check -s exit:1 egrep '{' /dev/null
319}
320# End FreeBSD
321
322atf_init_test_cases()
323{
324	atf_add_test_case basic
325	atf_add_test_case binary
326	atf_add_test_case recurse
327	atf_add_test_case recurse_symlink
328	atf_add_test_case word_regexps
329	atf_add_test_case begin_end
330	atf_add_test_case ignore_case
331	atf_add_test_case invert
332	atf_add_test_case whole_line
333	atf_add_test_case negative
334	atf_add_test_case context
335	atf_add_test_case file_exp
336	atf_add_test_case egrep
337	atf_add_test_case zgrep
338	atf_add_test_case nonexistent
339	atf_add_test_case context2
340# Begin FreeBSD
341	atf_add_test_case oflag_zerolen
342	atf_add_test_case xflag
343	atf_add_test_case color
344	atf_add_test_case f_file_empty
345	atf_add_test_case escmap
346	atf_add_test_case egrep_empty_invalid
347# End FreeBSD
348}
349