1#!/usr/bin/perl
2
3# Copyright (C) 2008-2023 Free Software Foundation, Inc.
4
5# This program is free software: you can redistribute it and/or modify
6# it under the terms of the GNU General Public License as published by
7# the Free Software Foundation, either version 3 of the License, or
8# (at your option) any later version.
9
10# This program is distributed in the hope that it will be useful,
11# but WITHOUT ANY WARRANTY; without even the implied warranty of
12# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13# GNU General Public License for more details.
14
15# You should have received a copy of the GNU General Public License
16# along with this program.  If not, see <https://www.gnu.org/licenses/>.
17
18use strict;
19
20my $prog = 'tac';
21
22# Turn off localization of executable's output.
23@ENV{qw(LANGUAGE LANG LC_ALL)} = ('C') x 3;
24
25my $bad_dir = 'no/such/dir';
26
27# This must be longer than 16KiB to trigger the double free in coreutils-8.5.
28my $long_line = 'o' x (16 * 1024 + 1);
29
30my @Tests =
31(
32  ['segfault', '-r', {IN=>"a\n"}, {IN=>"b\n"}, {OUT=>"a\nb\n"}],
33  ['segfault2','-r', {IN=>"a\nb\n"}, {IN=>"1\n2\n"}, {OUT=>"b\na\n2\n1\n"}],
34
35  ['basic-0', '', {IN=>""}, {OUT=>""}],
36  ['basic-a', '', {IN=>"a"}, {OUT=>"a"}],
37  ['basic-b', '', {IN=>"\n"}, {OUT=>"\n"}],
38  ['basic-c', '', {IN=>"a\n"}, {OUT=>"a\n"}],
39  ['basic-d', '', {IN=>"a\nb"}, {OUT=>"ba\n"}],
40  ['basic-e', '', {IN=>"a\nb\n"}, {OUT=>"b\na\n"}],
41  ['basic-f', '', {IN=>"1234567\n8\n"}, {OUT=>"8\n1234567\n"}],
42  ['basic-g', '', {IN=>"12345678\n9\n"}, {OUT=>"9\n12345678\n"}],
43  ['basic-h', '', {IN=>"123456\n8\n"}, {OUT=>"8\n123456\n"}],
44  ['basic-i', '', {IN=>"12345\n8\n"}, {OUT=>"8\n12345\n"}],
45  ['basic-j', '', {IN=>"1234\n8\n"}, {OUT=>"8\n1234\n"}],
46  ['basic-k', '', {IN=>"123\n8\n"}, {OUT=>"8\n123\n"}],
47
48  ['nul-0', '-s ""', {IN=>""}, {OUT=>""}],
49  ['nul-a', '-s ""', {IN=>"a"}, {OUT=>"a"}],
50  ['nul-b', '-s ""', {IN=>"\0"}, {OUT=>"\0"}],
51  ['nul-c', '-s ""', {IN=>"a\0"}, {OUT=>"a\0"}],
52  ['nul-d', '-s ""', {IN=>"a\0b"}, {OUT=>"ba\0"}],
53  ['nul-e', '-s ""', {IN=>"a\0b\0"}, {OUT=>"b\0a\0"}],
54
55  ['opt-b', '-b', {IN=>"\na\nb\nc"}, {OUT=>"\nc\nb\na"}],
56  ['opt-s', '-s:', {IN=>"a:b:c:"}, {OUT=>"c:b:a:"}],
57  ['opt-sb', qw(-s : -b), {IN=>":a:b:c"}, {OUT=>":c:b:a"}],
58  ['opt-r', qw(-r -s '\._+'),
59   {IN=>"1._2.__3.___4._"},
60   {OUT=>"4._3.___2.__1._"}],
61
62  ['opt-r2', qw(-r -s '\._+'),
63   {IN=>"a.___b.__1._2.__3.___4._"},
64   {OUT=>"4._3.___2.__1._b.__a.___"}],
65
66  # This gave incorrect output (.___4._2.__3._1) with tac-1.22.
67  ['opt-br', qw(-b -r -s '\._+'),
68   {IN=>"._1._2.__3.___4"}, {OUT=>".___4.__3._2._1"}],
69
70  ['opt-br2', qw(-b -r -s '\._+'),
71   {IN=>".__x.___y.____z._1._2.__3.___4"},
72   {OUT=>".___4.__3._2._1.____z.___y.__x"}],
73
74  ['pipe-bad-tmpdir',
75   {ENV => "TMPDIR=$bad_dir"},
76   {IN_PIPE => "a\n"},
77   {OUT=>"a\n"}],
78
79  # coreutils-8.5's tac would double-free its primary buffer.
80  ['double-free', {IN=>$long_line}, {OUT=>$long_line}],
81);
82
83@Tests = triple_test \@Tests;
84
85my $save_temps = $ENV{DEBUG};
86my $verbose = $ENV{VERBOSE};
87
88my $fail = run_tests ($prog, $prog, \@Tests, $save_temps, $verbose);
89exit $fail;
90