1#!/usr/bin/perl
2# Test tail.
3
4# Copyright (C) 2008-2023 Free Software Foundation, Inc.
5
6# This program is free software: you can redistribute it and/or modify
7# it under the terms of the GNU General Public License as published by
8# the Free Software Foundation, either version 3 of the License, or
9# (at your option) any later version.
10
11# This program is distributed in the hope that it will be useful,
12# but WITHOUT ANY WARRANTY; without even the implied warranty of
13# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14# GNU General Public License for more details.
15
16# You should have received a copy of the GNU General Public License
17# along with this program.  If not, see <https://www.gnu.org/licenses/>.
18
19use strict;
20
21my $prog = 'tail';
22my $normalize_strerror = "s/': .*/'/";
23
24# Turn off localization of executable's output.
25@ENV{qw(LANGUAGE LANG LC_ALL)} = ('C') x 3;
26
27my @tv = (
28# test name, options, input, expected output, expected return code
29#
30['obs-plus-c1', '+2c', 'abcd', 'bcd', 0],
31['obs-plus-c2', '+8c', 'abcd', '', 0],
32['obs-c3', '-1c', 'abcd', 'd', 0],
33['obs-c4', '-9c', 'abcd', 'abcd', 0],
34['obs-c5', '-12c', 'x' . ('y' x 12) . 'z', ('y' x 11) . 'z', 0],
35
36['obs-l1', '-1l', 'x', 'x', 0],
37['obs-l2', '-1l', "x\ny\n", "y\n", 0],
38['obs-l3', '-1l', "x\ny", "y", 0],
39['obs-plus-l4', '+1l', "x\ny\n", "x\ny\n", 0],
40['obs-plus-l5', '+2l', "x\ny\n", "y\n", 0],
41
42# Same as -l tests, but without the 'l'.
43['obs-1', '-1', 'x', 'x', 0],
44['obs-2', '-1', "x\ny\n", "y\n", 0],
45['obs-3', '-1', "x\ny", "y", 0],
46['obs-plus-4', '+1', "x\ny\n", "x\ny\n", 0],
47['obs-plus-5', '+2', "x\ny\n", "y\n", 0],
48
49# This is equivalent to +10c
50['obs-plus-x1', '+c', 'x' . ('y' x 10) . 'z', 'yyz', 0],
51# This is equivalent to +10l
52['obs-plus-x2', '+l', "x\n" . ("y\n" x 10) . 'z', "y\ny\nz", 0],
53# With no number, this is like -10l
54['obs-l', '-l', "x\n" . ("y\n" x 10) . 'z', ("y\n" x 9) . 'z', 0],
55
56['obs-b', '-b', "x\n" x (512 * 10 / 2 + 1), "x\n" x (512 * 10 / 2), 0],
57
58['err-1', '+cl', '', '', 1,
59 "$prog: cannot open '+cl' for reading: No such file or directory\n"],
60
61['err-2', '-cl', '', '', 1,
62 "$prog: invalid number of bytes: 'l'\n", $normalize_strerror],
63
64['err-3', '+2cz', '', '', 1,
65 "$prog: cannot open '+2cz' for reading: No such file or directory\n"],
66
67# This should get 'tail: invalid option -- 2'
68['err-4', '-2cX', '', '', 1,
69 "$prog: option used in invalid context -- 2\n"],
70
71# Since the number is larger than 2^64, this should provoke
72# the diagnostic: 'tail: 99999999999999999999: invalid number of bytes'
73# on all systems... probably, for now, maybe.
74['err-5', '-c99999999999999999999', '', '', 1,
75 "$prog: invalid number of bytes: '99999999999999999999'\n",
76 $normalize_strerror],
77['err-6', '-c --', '', '', 1,
78 "$prog: invalid number of bytes: '-'\n", $normalize_strerror],
79
80# Same as -n 10
81['minus-1', '-', '', '', 0],
82['minus-2', '-', "x\n" . ("y\n" x 10) . 'z', ("y\n" x 9) . 'z', 0],
83
84['c-2', '-c 2', "abcd\n", "d\n", 0],
85['c-2-minus', '-c 2 --', "abcd\n", "d\n", 0],
86['c2', '-c2', "abcd\n", "d\n", 0],
87['c2-minus', '-c2 --', "abcd\n", "d\n", 0],
88
89['n-1', '-n 10', "x\n" . ("y\n" x 10) . 'z', ("y\n" x 9) . 'z', 0],
90['n-2', '-n -10', "x\n" . ("y\n" x 10) . 'z', ("y\n" x 9) . 'z', 0],
91['n-3', '-n +10', "x\n" . ("y\n" x 10) . 'z', "y\ny\nz", 0],
92
93# Accept +0 as synonym for +1.
94['n-4',  '-n +0', "y\n" x 5, "y\n" x 5, 0],
95['n-4a', '-n +1', "y\n" x 5, "y\n" x 5, 0],
96
97# Note that -0 is *not* a synonym for -1.
98['n-5',  '-n -0', "y\n" x 5, '', 0],
99['n-5a', '-n -1', "y\n" x 5, "y\n", 0],
100['n-5b', '-n  0', "y\n" x 5, '', 0],
101
102# With textutils-1.22, this failed.
103['f-pipe-1', '-f -n 1', "a\nb\n", "b\n", 0],
104
105# --zero-terminated
106['zero-1', '-z -n 1', "x\0y", "y", 0],
107['zero-2', '-z -n 2', "x\0y", "x\0y", 0],
108);
109
110my @Tests;
111
112foreach my $t (@tv)
113  {
114    my ($test_name, $flags, $in, $exp, $ret, $err_msg, $err_sub) = @$t;
115    my $e = [$test_name, $flags, {IN=>$in}, {OUT=>$exp}];
116    $ret
117      and push @$e, {EXIT=>$ret}, {ERR=>$err_msg}, {ERR_SUBST=>$err_sub};
118
119    $test_name =~ /^minus-/
120      and push @$e, {ENV=>'_POSIX2_VERSION=199209'};
121
122    $test_name =~ /^(err-6|c-2)$/
123      and push @$e, {ENV=>'_POSIX2_VERSION=200112'};
124
125    $test_name =~ /^obs-plus-/
126      and push @$e, {ENV=>'_POSIX2_VERSION=200809'};
127
128    $test_name =~ /^f-pipe-/
129      and push @$e, {ENV=>'POSIXLY_CORRECT=1'};
130
131    push @Tests, $e;
132  }
133
134@Tests = triple_test \@Tests;
135
136# If you run the minus* tests with a FILE arg they'd hang.
137# If you run the err-1 or err-3 tests with a FILE, they'd misinterpret
138# the arg unless we are using the obsolete form.
139@Tests = grep { $_->[0] !~ /^(minus|err-[13])/ || $_->[0] =~ /\.[rp]$/ } @Tests;
140
141# Using redirection or a file would make this hang.
142@Tests = grep { $_->[0] !~ /^f-/ || $_->[0] =~ /\.p$/ } @Tests;
143
144my $save_temps = $ENV{DEBUG};
145my $verbose = $ENV{VERBOSE};
146
147my $fail = run_tests ($prog, $prog, \@Tests, $save_temps, $verbose);
148exit $fail;
149