1 # SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3 # Copyright (c) 2016 Alan Somers
5 # Redistribution and use in source and binary forms, with or without
6 # modification, are permitted provided that the following conditions
8 # 1. Redistributions of source code must retain the above copyright
9 # notice, this list of conditions and the following disclaimer.
10 # 2. Redistributions in binary form must reproduce the above copyright
11 # notice, this list of conditions and the following disclaimer in the
12 # documentation and/or other materials provided with the distribution.
14 # THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 # ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 # OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 atf_set "descr" "Reverse an empty file"
35 touch infile expectfile
36 tail -r infile > outfile
37 tail -r < infile > outpipe
38 atf_check cmp expectfile outfile
39 atf_check cmp expectfile outpipe
45 atf_set "descr" "Reverse a file"
50 This is the first line
51 This is the second line
52 This is the third line
54 cat > expectfile << HERE
55 This is the third line
56 This is the second line
57 This is the first line
59 tail -r infile > outfile
60 tail -r < infile > outpipe
61 atf_check cmp expectfile outfile
62 atf_check cmp expectfile outpipe
65 atf_test_case file_rn2
68 atf_set "descr" "Reverse the last two lines of a file"
73 This is the first line
74 This is the second line
75 This is the third line
77 cat > expectfile << HERE
78 This is the third line
79 This is the second line
81 tail -rn2 infile > outfile
82 tail -rn2 < infile > outpipe
83 atf_check cmp expectfile outfile
84 atf_check cmp expectfile outpipe
87 # Regression test for PR 222671
88 # https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=222671
89 atf_test_case pipe_leading_newline_r
90 pipe_leading_newline_r_head()
92 atf_set "descr" "Reverse a pipe whose first character is a newline"
94 pipe_leading_newline_r_body()
96 cat > expectfile << HERE
102 printf '\n1\n2\n3\n' | tail -r > outfile
103 printf '\n1\n2\n3\n' | tail -r > outpipe
104 atf_check cmp expectfile outfile
105 atf_check cmp expectfile outpipe
108 atf_test_case file_rc28
111 atf_set "descr" "Reverse a file and display the last 28 characters"
116 This is the first line
117 This is the second line
118 This is the third line
120 cat > expectfile << HERE
121 This is the third line
124 tail -rc28 infile > outfile
125 tail -rc28 < infile > outpipe
126 atf_check cmp expectfile outfile
127 atf_check cmp expectfile outpipe
130 atf_test_case file_rc28
133 atf_set "descr" "Reverse a file and display the last 28 characters"
138 This is the first line
139 This is the second line
140 This is the third line
142 cat > expectfile << HERE
143 This is the third line
146 tail -rc28 infile > outfile
147 tail -rc28 < infile > outpipe
148 atf_check cmp expectfile outfile
149 atf_check cmp expectfile outpipe
152 atf_test_case longfile_r
155 atf_set "descr" "Reverse a long file"
159 jot -w "%0511d" 1030 0 > infile
160 jot -w "%0511d" 1030 1029 0 -1 > expectfile
161 tail -r infile > outfile
162 tail -r < infile > outpipe
163 atf_check cmp expectfile outfile
164 atf_check cmp expectfile outpipe
167 atf_test_case longfile_r_enomem
168 longfile_r_enomem_head()
170 atf_set "descr" "Reverse a file that's too long to store in RAM"
172 longfile_r_enomem_body()
174 # When we reverse a file that's too long for RAM, tail should drop the
175 # first part and just print what it can. We'll check that the last
178 ulimit -v 32768 || atf_skip "Can't adjust ulimit"
179 jot -w "%01023d" 32768 0 | tail -r > outfile ;
181 if [ "$?" -ne 1 ]; then
182 atf_skip "Didn't get ENOMEM. Adjust test parameters"
184 # We don't know how much of the input we dropped. So just check that
185 # the first ten lines of tail's output are the same as the last ten of
187 jot -w "%01023d" 10 32767 0 -1 > expectfile
188 head -n 10 outfile > outtrunc
189 diff expectfile outtrunc
190 atf_check cmp expectfile outtrunc
193 atf_test_case longfile_r_longlines
194 longfile_r_longlines_head()
196 atf_set "descr" "Reverse a long file with extremely long lines"
198 longfile_r_longlines_body()
200 jot -s " " -w "%07d" 18000 0 > infile
201 jot -s " " -w "%07d" 18000 18000 >> infile
202 jot -s " " -w "%07d" 18000 36000 >> infile
203 jot -s " " -w "%07d" 18000 36000 > expectfile
204 jot -s " " -w "%07d" 18000 18000 >> expectfile
205 jot -s " " -w "%07d" 18000 0 >> expectfile
206 tail -r infile > outfile
207 tail -r < infile > outpipe
208 atf_check cmp expectfile outfile
209 atf_check cmp expectfile outpipe
212 atf_test_case longfile_rc135782
213 longfile_rc135782_head()
215 atf_set "descr" "Reverse a long file and print the last 135,782 bytes"
217 longfile_rc135782_body()
219 jot -w "%063d" 9000 0 > infile
220 jot -w "%063d" 2121 8999 0 -1 > expectfile
221 echo "0000000000000000000000000000000006878" >> expectfile
222 tail -rc135782 infile > outfile
223 tail -rc135782 < infile > outpipe
224 atf_check cmp expectfile outfile
225 atf_check cmp expectfile outpipe
228 atf_test_case longfile_rc145782_longlines
229 longfile_rc145782_longlines_head()
231 atf_set "descr" "Reverse a long file with extremely long lines and print the last 145,782 bytes"
233 longfile_rc145782_longlines_body()
235 jot -s " " -w "%07d" 18000 0 > infile
236 jot -s " " -w "%07d" 18000 18000 >> infile
237 jot -s " " -w "%07d" 18000 36000 >> infile
238 jot -s " " -w "%07d" 18000 36000 > expectfile
239 echo -n "35777 " >> expectfile
240 jot -s " " -w "%07d" 222 35778 >> expectfile
241 tail -rc145782 infile > outfile
242 tail -rc145782 < infile > outpipe
243 atf_check cmp expectfile outfile
244 atf_check cmp expectfile outpipe
247 atf_test_case longfile_rn2500
248 longfile_rn2500_head()
250 atf_set "descr" "Reverse a long file and print the last 2,500 lines"
252 longfile_rn2500_body()
254 jot -w "%063d" 9000 0 > infile
255 jot -w "%063d" 2500 8999 0 -1 > expectfile
256 tail -rn2500 infile > outfile
257 tail -rn2500 < infile > outpipe
258 atf_check cmp expectfile outfile
259 atf_check cmp expectfile outpipe
262 atf_test_case broken_pipe
265 atf_set "descr" "Do not print bogus errno based output on short writes"
269 atf_check -o save:ints seq -f '%128g' 1 1000
270 atf_check -s ignore \
271 -e "inline:tail: stdout\nexit code: 1\n" \
272 -x '(tail -n 856 ints; echo exit code: $? >&2) | sleep 2'
276 atf_init_test_cases()
278 atf_add_test_case empty_r
279 atf_add_test_case file_r
280 atf_add_test_case file_rc28
281 atf_add_test_case file_rn2
282 atf_add_test_case pipe_leading_newline_r
283 # The longfile tests are designed to exercise behavior in r_buf(),
284 # which operates on 128KB blocks
285 atf_add_test_case longfile_r
286 atf_add_test_case longfile_r_enomem
287 atf_add_test_case longfile_r_longlines
288 atf_add_test_case longfile_rc135782
289 atf_add_test_case longfile_rc145782_longlines
290 atf_add_test_case longfile_rn2500
291 atf_add_test_case broken_pipe