tail_test.sh revision 332610
1202719Sgabor# Copyright (c) 2016 Alan Somers
2265533Sdelphij# All rights reserved.
3202719Sgabor#
4202719Sgabor# Redistribution and use in source and binary forms, with or without
5202719Sgabor# modification, are permitted provided that the following conditions
6202719Sgabor# are met:
7202719Sgabor# 1. Redistributions of source code must retain the above copyright
8202719Sgabor#    notice, this list of conditions and the following disclaimer.
9202719Sgabor# 2. Redistributions in binary form must reproduce the above copyright
10202719Sgabor#    notice, this list of conditions and the following disclaimer in the
11202719Sgabor#    documentation and/or other materials provided with the distribution.
12202719Sgabor#
13202719Sgabor# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14202719Sgabor# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15202719Sgabor# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16202719Sgabor# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
17202719Sgabor# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18202719Sgabor# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19202719Sgabor# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20202719Sgabor# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21202719Sgabor# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22202719Sgabor# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23202719Sgabor# SUCH DAMAGE.
24202719Sgabor#
25202719Sgabor# $FreeBSD: stable/10/usr.bin/tail/tests/tail_test.sh 332610 2018-04-16 16:42:16Z asomers $
26202719Sgabor
27202719Sgaboratf_test_case empty_r
28202719Sgaborempty_r_head()
29202719Sgabor{
30202719Sgabor	atf_set "descr" "Reverse an empty file"
31202719Sgabor}
32202719Sgaborempty_r_body()
33202719Sgabor{
34202719Sgabor	touch infile expectfile
35202719Sgabor	tail -r infile > outfile
36202719Sgabor	tail -r < infile > outpipe
37202719Sgabor	atf_check cmp expectfile outfile
38202719Sgabor	atf_check cmp expectfile outpipe
39202719Sgabor}
40202719Sgabor
41202719Sgaboratf_test_case file_r
42202719Sgaborfile_r_head()
43202719Sgabor{
44202719Sgabor	atf_set "descr" "Reverse a file"
45202719Sgabor}
46202719Sgaborfile_r_body()
47202719Sgabor{
48202719Sgabor	cat > infile <<HERE
49232994SkevloThis is the first line
50232994SkevloThis is the second line
51233121SkevloThis is the third line
52202719SgaborHERE
53202719Sgabor	cat > expectfile << HERE
54202719SgaborThis is the third line
55202719SgaborThis is the second line
56202719SgaborThis is the first line
57202719SgaborHERE
58202719Sgabor	tail -r infile > outfile
59202719Sgabor	tail -r < infile > outpipe
60202719Sgabor	atf_check cmp expectfile outfile
61202719Sgabor	atf_check cmp expectfile outpipe
62202719Sgabor}
63202719Sgabor
64202719Sgaboratf_test_case file_rn2
65202719Sgaborfile_rn2_head()
66202719Sgabor{
67202719Sgabor	atf_set "descr" "Reverse the last two lines of a file"
68202719Sgabor}
69202719Sgaborfile_rn2_body()
70202719Sgabor{
71202719Sgabor	cat > infile <<HERE
72202719SgaborThis is the first line
73202719SgaborThis is the second line
74202719SgaborThis is the third line
75202719SgaborHERE
76202719Sgabor	cat > expectfile << HERE
77202719SgaborThis is the third line
78202719SgaborThis is the second line
79202719SgaborHERE
80202719Sgabor	tail -rn2 infile > outfile
81202719Sgabor	tail -rn2 < infile > outpipe
82202719Sgabor	atf_check cmp expectfile outfile
83202719Sgabor	atf_check cmp expectfile outpipe
84202719Sgabor}
85202719Sgabor
86202719Sgabor# Regression test for PR 222671
87202719Sgabor# https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=222671
88202719Sgaboratf_test_case pipe_leading_newline_r
89202719Sgaborpipe_leading_newline_r_head()
90202719Sgabor{
91202719Sgabor	atf_set "descr" "Reverse a pipe whose first character is a newline"
92202719Sgabor}
93202719Sgaborpipe_leading_newline_r_body()
94202719Sgabor{
95202719Sgabor	cat > expectfile << HERE
96202719Sgabor3
97202719Sgabor2
98202719Sgabor1
99202719Sgabor
100232994SkevloHERE
101232994Skevlo	printf '\n1\n2\n3\n' | tail -r > outfile
102232994Skevlo	printf '\n1\n2\n3\n' | tail -r > outpipe
103265533Sdelphij	atf_check cmp expectfile outfile
104232994Skevlo	atf_check cmp expectfile outpipe
105232994Skevlo}
106232994Skevlo
107232994Skevloatf_test_case file_rc28
108202719Sgaborfile_rc28_head()
109265533Sdelphij{
110232994Skevlo	atf_set "descr" "Reverse a file and display the last 28 characters"
111232994Skevlo}
112202719Sgaborfile_rc28_body()
113202719Sgabor{
114202719Sgabor	cat > infile <<HERE
115202719SgaborThis is the first line
116232994SkevloThis is the second line
117232994SkevloThis is the third line
118232994SkevloHERE
119202719Sgabor	cat > expectfile << HERE
120202719SgaborThis is the third line
121202719Sgaborline
122202719SgaborHERE
123233121Skevlo	tail -rc28 infile > outfile
124233121Skevlo	tail -rc28 < infile > outpipe
125202719Sgabor	atf_check cmp expectfile outfile
126202719Sgabor	atf_check cmp expectfile outpipe
127232994Skevlo}
128202719Sgabor
129202719Sgaboratf_test_case file_rc28
130202719Sgaborfile_rc28_head()
131202719Sgabor{
132202719Sgabor	atf_set "descr" "Reverse a file and display the last 28 characters"
133202719Sgabor}
134202719Sgaborfile_rc28_body()
135202719Sgabor{
136202719Sgabor	cat > infile <<HERE
137202719SgaborThis is the first line
138202719SgaborThis is the second line
139202719SgaborThis is the third line
140202719SgaborHERE
141202719Sgabor	cat > expectfile << HERE
142202719SgaborThis is the third line
143202719Sgaborline
144202719SgaborHERE
145202719Sgabor	tail -rc28 infile > outfile
146202719Sgabor	tail -rc28 < infile > outpipe
147202719Sgabor	atf_check cmp expectfile outfile
148202719Sgabor	atf_check cmp expectfile outpipe
149202719Sgabor}
150202719Sgabor
151202719Sgaboratf_test_case longfile_r
152202719Sgaborlongfile_r_head()
153202719Sgabor{
154202719Sgabor	atf_set "descr" "Reverse a long file"
155202719Sgabor}
156202719Sgaborlongfile_r_body()
157202719Sgabor{
158202719Sgabor	jot -w "%0511d" 1030 0 > infile
159202719Sgabor	jot -w "%0511d" 1030 1029 0 -1 > expectfile
160202719Sgabor	tail -r infile > outfile
161202719Sgabor	tail -r < infile > outpipe
162202719Sgabor	atf_check cmp expectfile outfile
163202719Sgabor	atf_check cmp expectfile outpipe
164202719Sgabor}
165202719Sgabor
166202719Sgaboratf_test_case longfile_r_enomem
167202719Sgaborlongfile_r_enomem_head()
168202719Sgabor{
169202719Sgabor	atf_set "descr" "Reverse a file that's too long to store in RAM"
170202719Sgabor}
171202719Sgaborlongfile_r_enomem_body()
172202719Sgabor{
173202719Sgabor	# When we reverse a file that's too long for RAM, tail should drop the
174202719Sgabor	# first part and just print what it can.  We'll check that the last
175202719Sgabor	# part is ok
176202719Sgabor	{
177202719Sgabor		ulimit -v 32768 || atf_skip "Can't adjust ulimit"
178202719Sgabor		jot -w "%01023d" 32768 0 | tail -r > outfile ;
179202719Sgabor	}
180202719Sgabor	if [ "$?" -ne 1 ]; then
181202719Sgabor		atf_skip "Didn't get ENOMEM.  Adjust test parameters"
182202719Sgabor	fi
183202719Sgabor	# We don't know how much of the input we dropped.  So just check that
184202719Sgabor	# the first ten lines of tail's output are the same as the last ten of
185202719Sgabor	# the input
186202719Sgabor	jot -w "%01023d" 10 32767 0 -1 > expectfile
187202719Sgabor	head -n 10 outfile > outtrunc
188202719Sgabor	diff expectfile outtrunc
189202719Sgabor	atf_check cmp expectfile outtrunc
190202719Sgabor}
191202719Sgabor
192202719Sgaboratf_test_case longfile_r_longlines
193202719Sgaborlongfile_r_longlines_head()
194202719Sgabor{
195202719Sgabor	atf_set "descr" "Reverse a long file with extremely long lines"
196202719Sgabor}
197202719Sgaborlongfile_r_longlines_body()
198202719Sgabor{
199202719Sgabor	jot -s " " -w "%07d" 18000 0 > infile
200202719Sgabor	jot -s " " -w "%07d" 18000 18000 >> infile
201202719Sgabor	jot -s " " -w "%07d" 18000 36000 >> infile
202202719Sgabor	jot -s " " -w "%07d" 18000 36000 > expectfile
203202719Sgabor	jot -s " " -w "%07d" 18000 18000 >> expectfile
204202719Sgabor	jot -s " " -w "%07d" 18000 0 >> expectfile
205202719Sgabor	tail -r infile > outfile
206202719Sgabor	tail -r < infile > outpipe
207202719Sgabor	atf_check cmp expectfile outfile
208202719Sgabor	atf_check cmp expectfile outpipe
209202719Sgabor}
210202719Sgabor
211202719Sgaboratf_test_case longfile_rc135782
212202719Sgaborlongfile_rc135782_head()
213202719Sgabor{
214202719Sgabor	atf_set "descr" "Reverse a long file and print the last 135,782 bytes"
215202719Sgabor}
216202719Sgaborlongfile_rc135782_body()
217202719Sgabor{
218202719Sgabor	jot -w "%063d" 9000 0 > infile
219202719Sgabor	jot -w "%063d" 2121 8999 0 -1 > expectfile
220202719Sgabor	echo "0000000000000000000000000000000006878" >> expectfile
221202719Sgabor	tail -rc135782 infile > outfile
222202719Sgabor	tail -rc135782 < infile > outpipe
223202719Sgabor	atf_check cmp expectfile outfile
224202719Sgabor	atf_check cmp expectfile outpipe
225202719Sgabor}
226202719Sgabor
227202719Sgaboratf_test_case longfile_rc145782_longlines
228202719Sgaborlongfile_rc145782_longlines_head()
229202719Sgabor{
230202719Sgabor	atf_set "descr" "Reverse a long file with extremely long lines and print the last 145,782 bytes"
231202719Sgabor}
232202719Sgaborlongfile_rc145782_longlines_body()
233202719Sgabor{
234202719Sgabor	jot -s " " -w "%07d" 18000 0 > infile
235202719Sgabor	jot -s " " -w "%07d" 18000 18000 >> infile
236202719Sgabor	jot -s " " -w "%07d" 18000 36000 >> infile
237202719Sgabor	jot -s " " -w "%07d" 18000 36000 > expectfile
238202719Sgabor	echo -n "35777 " >> expectfile
239202719Sgabor	jot -s " " -w "%07d" 222 35778 >> expectfile
240202719Sgabor	tail -rc145782 infile > outfile
241202719Sgabor	tail -rc145782 < infile > outpipe
242202719Sgabor	atf_check cmp expectfile outfile
243202719Sgabor	atf_check cmp expectfile outpipe
244202719Sgabor}
245202719Sgabor
246202719Sgaboratf_test_case longfile_rn2500
247202719Sgaborlongfile_rn2500_head()
248202719Sgabor{
249202719Sgabor	atf_set "descr" "Reverse a long file and print the last 2,500 lines"
250202719Sgabor}
251202719Sgaborlongfile_rn2500_body()
252202719Sgabor{
253202719Sgabor	jot -w "%063d" 9000 0 > infile
254202719Sgabor	jot -w "%063d" 2500 8999 0 -1 > expectfile
255202719Sgabor	tail -rn2500 infile > outfile
256202719Sgabor	tail -rn2500 < infile > outpipe
257202719Sgabor	atf_check cmp expectfile outfile
258202719Sgabor	atf_check cmp expectfile outpipe
259202719Sgabor}
260202719Sgabor
261202719Sgabor
262202719Sgaboratf_init_test_cases()
263202719Sgabor{
264202719Sgabor	atf_add_test_case empty_r
265202719Sgabor	atf_add_test_case file_r
266202719Sgabor	atf_add_test_case file_rc28
267202719Sgabor	atf_add_test_case file_rn2
268202719Sgabor	atf_add_test_case pipe_leading_newline_r
269202719Sgabor	# The longfile tests are designed to exercise behavior in r_buf(),
270202719Sgabor	# which operates on 128KB blocks
271202719Sgabor	atf_add_test_case longfile_r
272202719Sgabor	atf_add_test_case longfile_r_enomem
273202719Sgabor	atf_add_test_case longfile_r_longlines
274232994Skevlo	atf_add_test_case longfile_rc135782
275	atf_add_test_case longfile_rc145782_longlines
276	atf_add_test_case longfile_rn2500
277}
278