]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - usr.bin/tail/tests/tail_test.sh
fd: refactor closefp in preparation for close_range rework
[FreeBSD/FreeBSD.git] / usr.bin / tail / tests / tail_test.sh
1 # SPDX-License-Identifier: BSD-2-Clause-FreeBSD
2 #
3 # Copyright (c) 2016 Alan Somers
4 #
5 # Redistribution and use in source and binary forms, with or without
6 # modification, are permitted provided that the following conditions
7 # are met:
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.
13 #
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
24 # SUCH DAMAGE.
25 #
26 # $FreeBSD$
27
28 atf_test_case empty_r
29 empty_r_head()
30 {
31         atf_set "descr" "Reverse an empty file"
32 }
33 empty_r_body()
34 {
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
40 }
41
42 atf_test_case file_r
43 file_r_head()
44 {
45         atf_set "descr" "Reverse a file"
46 }
47 file_r_body()
48 {
49         cat > infile <<HERE
50 This is the first line
51 This is the second line
52 This is the third line
53 HERE
54         cat > expectfile << HERE
55 This is the third line
56 This is the second line
57 This is the first line
58 HERE
59         tail -r infile > outfile
60         tail -r < infile > outpipe
61         atf_check cmp expectfile outfile
62         atf_check cmp expectfile outpipe
63 }
64
65 atf_test_case file_rn2
66 file_rn2_head()
67 {
68         atf_set "descr" "Reverse the last two lines of a file"
69 }
70 file_rn2_body()
71 {
72         cat > infile <<HERE
73 This is the first line
74 This is the second line
75 This is the third line
76 HERE
77         cat > expectfile << HERE
78 This is the third line
79 This is the second line
80 HERE
81         tail -rn2 infile > outfile
82         tail -rn2 < infile > outpipe
83         atf_check cmp expectfile outfile
84         atf_check cmp expectfile outpipe
85 }
86
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()
91 {
92         atf_set "descr" "Reverse a pipe whose first character is a newline"
93 }
94 pipe_leading_newline_r_body()
95 {
96         cat > expectfile << HERE
97 3
98 2
99 1
100
101 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
106 }
107
108 atf_test_case file_rc28
109 file_rc28_head()
110 {
111         atf_set "descr" "Reverse a file and display the last 28 characters"
112 }
113 file_rc28_body()
114 {
115         cat > infile <<HERE
116 This is the first line
117 This is the second line
118 This is the third line
119 HERE
120         cat > expectfile << HERE
121 This is the third line
122 line
123 HERE
124         tail -rc28 infile > outfile
125         tail -rc28 < infile > outpipe
126         atf_check cmp expectfile outfile
127         atf_check cmp expectfile outpipe
128 }
129
130 atf_test_case file_rc28
131 file_rc28_head()
132 {
133         atf_set "descr" "Reverse a file and display the last 28 characters"
134 }
135 file_rc28_body()
136 {
137         cat > infile <<HERE
138 This is the first line
139 This is the second line
140 This is the third line
141 HERE
142         cat > expectfile << HERE
143 This is the third line
144 line
145 HERE
146         tail -rc28 infile > outfile
147         tail -rc28 < infile > outpipe
148         atf_check cmp expectfile outfile
149         atf_check cmp expectfile outpipe
150 }
151
152 atf_test_case longfile_r
153 longfile_r_head()
154 {
155         atf_set "descr" "Reverse a long file"
156 }
157 longfile_r_body()
158 {
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
165 }
166
167 atf_test_case longfile_r_enomem
168 longfile_r_enomem_head()
169 {
170         atf_set "descr" "Reverse a file that's too long to store in RAM"
171 }
172 longfile_r_enomem_body()
173 {
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
176         # part is ok
177         {
178                 ulimit -v 32768 || atf_skip "Can't adjust ulimit"
179                 jot -w "%01023d" 32768 0 | tail -r > outfile ;
180         }
181         if [ "$?" -ne 1 ]; then
182                 atf_skip "Didn't get ENOMEM.  Adjust test parameters"
183         fi
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
186         # the input
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
191 }
192
193 atf_test_case longfile_r_longlines
194 longfile_r_longlines_head()
195 {
196         atf_set "descr" "Reverse a long file with extremely long lines"
197 }
198 longfile_r_longlines_body()
199 {
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
210 }
211
212 atf_test_case longfile_rc135782
213 longfile_rc135782_head()
214 {
215         atf_set "descr" "Reverse a long file and print the last 135,782 bytes"
216 }
217 longfile_rc135782_body()
218 {
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
226 }
227
228 atf_test_case longfile_rc145782_longlines
229 longfile_rc145782_longlines_head()
230 {
231         atf_set "descr" "Reverse a long file with extremely long lines and print the last 145,782 bytes"
232 }
233 longfile_rc145782_longlines_body()
234 {
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
245 }
246
247 atf_test_case longfile_rn2500
248 longfile_rn2500_head()
249 {
250         atf_set "descr" "Reverse a long file and print the last 2,500 lines"
251 }
252 longfile_rn2500_body()
253 {
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
260 }
261
262 atf_test_case broken_pipe
263 broken_pipe_head()
264 {
265         atf_set "descr" "Do not print bogus errno based output on short writes"
266 }
267 broken_pipe_body()
268 {
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'
273 }
274
275
276 atf_init_test_cases()
277 {
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
292 }