build: ensure make-prime-list doesn't access out of bounds memory
[platform/upstream/coreutils.git] / tests / misc / tty-eof.pl
1 #!/usr/bin/perl
2 # Test whether programs exit upon a single EOF from a tty.
3 # Ensure that e.g., cat exits upon a single EOF (^D) from a tty.
4 # Do the same for all programs that can read stdin,
5 # require no arguments and that write to standard output.
6
7 # Copyright (C) 2003-2013 Free Software Foundation, Inc.
8
9 # This program is free software: you can redistribute it and/or modify
10 # it under the terms of the GNU General Public License as published by
11 # the Free Software Foundation, either version 3 of the License, or
12 # (at your option) any later version.
13
14 # This program is distributed in the hope that it will be useful,
15 # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 # GNU General Public License for more details.
18
19 # You should have received a copy of the GNU General Public License
20 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
21
22 use strict;
23 (my $ME = $0) =~ s|.*/||;
24
25 # Some older versions of Expect.pm (e.g. 1.07) lack the log_user method,
26 # so check for that, too.
27 eval { require Expect; Expect->require_version('1.11') };
28 $@
29   and CuSkip::skip "$ME: this script requires Perl's Expect package >=1.11\n";
30
31 {
32   my $fail = 0;
33   my @stdin_reading_commands = qw(
34     base64
35     cat
36     cksum
37     dd
38     expand
39     fmt
40     fold
41     head
42     md5sum
43     nl
44     od
45     paste
46     pr
47     ptx
48     sha1sum
49     sha224sum
50     sha256sum
51     sha384sum
52     sha512sum
53     shuf
54     sort
55     sum
56     tac
57     tail
58     tee
59     tsort
60     unexpand
61     uniq
62     wc
63   );
64   my $stderr = 'tty-eof.err';
65   foreach my $cmd ((@stdin_reading_commands), 'cut -f2')
66     {
67       my $exp = new Expect;
68       $exp->log_user(0);
69       $exp->spawn("$cmd 2> $stderr")
70         or (warn "$ME: cannot run '$cmd': $!\n"), $fail=1, next;
71       # No input for cut -f2.
72       $cmd =~ /^cut/
73         or $exp->send("a b\n");
74       $exp->send("\cD");  # This is Control-D.  FIXME: what if that's not EOF?
75       $exp->expect (0, '-re', "^a b\\r?\$");
76       my $found = $exp->expect (1, '-re', "^.+\$");
77       $found and warn "F: $found: " . $exp->exp_match () . "\n";
78       $exp->expect(10, 'eof');
79       # Expect no output from cut, since we gave it no input.
80       defined $found || $cmd =~ /^cut/
81         or (warn "$ME: $cmd didn't produce expected output\n"),
82           $fail=1, next;
83       defined $exp->exitstatus
84         or (warn "$ME: $cmd didn't exit after ^D from standard input\n"),
85           $fail=1, next;
86       my $s = $exp->exitstatus;
87       $s == 0
88         or (warn "$ME: $cmd exited with status $s (expected 0)\n"),
89           $fail=1;
90       $exp->hard_close();
91
92       # dd normally writes to stderr.  If it exits successfully, we're done.
93       $cmd eq 'dd' && $s == 0
94         and next;
95
96       if (-s $stderr)
97         {
98           warn "$ME: $cmd wrote to stderr:\n";
99           system "cat $stderr";
100           $fail = 1;
101         }
102     }
103   continue
104     {
105       unlink $stderr
106         or warn "$ME: failed to remove stderr file from $cmd, $stderr: $!\n";
107     }
108
109   exit $fail
110 }