xref: /freebsd/usr.bin/tail/tests/tail_test.sh (revision 076ad2f836d5f49dc1375f1677335a48fe0d4b82)
1# Copyright (c) 2016 Alan Somers
2# All rights reserved.
3#
4# Redistribution and use in source and binary forms, with or without
5# modification, are permitted provided that the following conditions
6# are met:
7# 1. Redistributions of source code must retain the above copyright
8#    notice, this list of conditions and the following disclaimer.
9# 2. Redistributions in binary form must reproduce the above copyright
10#    notice, this list of conditions and the following disclaimer in the
11#    documentation and/or other materials provided with the distribution.
12#
13# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
17# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23# SUCH DAMAGE.
24#
25# $FreeBSD$
26
27atf_test_case empty_r
28empty_r_head()
29{
30	atf_set "descr" "Reverse an empty file"
31}
32empty_r_body()
33{
34	touch infile expectfile
35	tail -r infile > outfile
36	tail -r < infile > outpipe
37	atf_check cmp expectfile outfile
38	atf_check cmp expectfile outpipe
39}
40
41atf_test_case file_r
42file_r_head()
43{
44	atf_set "descr" "Reverse a file"
45}
46file_r_body()
47{
48	cat > infile <<HERE
49This is the first line
50This is the second line
51This is the third line
52HERE
53	cat > expectfile << HERE
54This is the third line
55This is the second line
56This is the first line
57HERE
58	tail -r infile > outfile
59	tail -r < infile > outpipe
60	atf_check cmp expectfile outfile
61	atf_check cmp expectfile outpipe
62}
63
64atf_test_case file_rn2
65file_rn2_head()
66{
67	atf_set "descr" "Reverse the last two lines of a file"
68}
69file_rn2_body()
70{
71	cat > infile <<HERE
72This is the first line
73This is the second line
74This is the third line
75HERE
76	cat > expectfile << HERE
77This is the third line
78This is the second line
79HERE
80	tail -rn2 infile > outfile
81	tail -rn2 < infile > outpipe
82	atf_check cmp expectfile outfile
83	atf_check cmp expectfile outpipe
84}
85
86atf_test_case file_rc28
87file_rc28_head()
88{
89	atf_set "descr" "Reverse a file and display the last 28 characters"
90}
91file_rc28_body()
92{
93	cat > infile <<HERE
94This is the first line
95This is the second line
96This is the third line
97HERE
98	cat > expectfile << HERE
99This is the third line
100line
101HERE
102	tail -rc28 infile > outfile
103	tail -rc28 < infile > outpipe
104	atf_check cmp expectfile outfile
105	atf_check cmp expectfile outpipe
106}
107
108atf_test_case longfile_r
109longfile_r_head()
110{
111	atf_set "descr" "Reverse a long file"
112}
113longfile_r_body()
114{
115	jot -w "%0511d" 1030 0 > infile
116	jot -w "%0511d" 1030 1029 0 -1 > expectfile
117	tail -r infile > outfile
118	tail -r < infile > outpipe
119	atf_check cmp expectfile outfile
120	atf_check cmp expectfile outpipe
121}
122
123atf_test_case longfile_r_enomem
124longfile_r_enomem_head()
125{
126	atf_set "descr" "Reverse a file that's too long to store in RAM"
127}
128longfile_r_enomem_body()
129{
130	# When we reverse a file that's too long for RAM, tail should drop the
131	# first part and just print what it can.  We'll check that the last
132	# part is ok
133	{
134		ulimit -v 32768 || atf_skip "Can't adjust ulimit"
135		jot -w "%01023d" 32768 0 | tail -r > outfile ;
136	}
137	if [ "$?" -ne 1 ]; then
138		atf_skip "Didn't get ENOMEM.  Adjust test parameters"
139	fi
140	# We don't know how much of the input we dropped.  So just check that
141	# the first ten lines of tail's output are the same as the last ten of
142	# the input
143	jot -w "%01023d" 10 32767 0 -1 > expectfile
144	head -n 10 outfile > outtrunc
145	diff expectfile outtrunc
146	atf_check cmp expectfile outtrunc
147}
148
149atf_test_case longfile_r_longlines
150longfile_r_longlines_head()
151{
152	atf_set "descr" "Reverse a long file with extremely long lines"
153}
154longfile_r_longlines_body()
155{
156	jot -s " " -w "%07d" 18000 0 > infile
157	jot -s " " -w "%07d" 18000 18000 >> infile
158	jot -s " " -w "%07d" 18000 36000 >> infile
159	jot -s " " -w "%07d" 18000 36000 > expectfile
160	jot -s " " -w "%07d" 18000 18000 >> expectfile
161	jot -s " " -w "%07d" 18000 0 >> expectfile
162	tail -r infile > outfile
163	tail -r < infile > outpipe
164	atf_check cmp expectfile outfile
165	atf_check cmp expectfile outpipe
166}
167
168atf_test_case longfile_rc135782
169longfile_rc135782_head()
170{
171	atf_set "descr" "Reverse a long file and print the last 135,782 bytes"
172}
173longfile_rc135782_body()
174{
175	jot -w "%063d" 9000 0 > infile
176	jot -w "%063d" 2121 8999 0 -1 > expectfile
177	echo "0000000000000000000000000000000006878" >> expectfile
178	tail -rc135782 infile > outfile
179	tail -rc135782 < infile > outpipe
180	atf_check cmp expectfile outfile
181	atf_check cmp expectfile outpipe
182}
183
184atf_test_case longfile_rc145782_longlines
185longfile_rc145782_longlines_head()
186{
187	atf_set "descr" "Reverse a long file with extremely long lines and print the last 145,782 bytes"
188}
189longfile_rc145782_longlines_body()
190{
191	jot -s " " -w "%07d" 18000 0 > infile
192	jot -s " " -w "%07d" 18000 18000 >> infile
193	jot -s " " -w "%07d" 18000 36000 >> infile
194	jot -s " " -w "%07d" 18000 36000 > expectfile
195	echo -n "35777 " >> expectfile
196	jot -s " " -w "%07d" 222 35778 >> expectfile
197	tail -rc145782 infile > outfile
198	tail -rc145782 < infile > outpipe
199	atf_check cmp expectfile outfile
200	atf_check cmp expectfile outpipe
201}
202
203atf_test_case longfile_rn2500
204longfile_rn2500_head()
205{
206	atf_set "descr" "Reverse a long file and print the last 2,500 lines"
207}
208longfile_rn2500_body()
209{
210	jot -w "%063d" 9000 0 > infile
211	jot -w "%063d" 2500 8999 0 -1 > expectfile
212	tail -rn2500 infile > outfile
213	tail -rn2500 < infile > outpipe
214	atf_check cmp expectfile outfile
215	atf_check cmp expectfile outpipe
216}
217
218
219atf_init_test_cases()
220{
221	atf_add_test_case empty_r
222	atf_add_test_case file_r
223	atf_add_test_case file_rc28
224	atf_add_test_case file_rn2
225	# The longfile tests are designed to exercise behavior in r_buf(),
226	# which operates on 128KB blocks
227	atf_add_test_case longfile_r
228	atf_add_test_case longfile_r_enomem
229	atf_add_test_case longfile_r_longlines
230	atf_add_test_case longfile_rc135782
231	atf_add_test_case longfile_rc145782_longlines
232	atf_add_test_case longfile_rn2500
233}
234