Skip to content

Commit 9af34f3

Browse files
committed
Close stdin where it's not needed in TestLib.pm procedures
Where possible, do this using a pseudoterminal, so that things like openssl that want to open /dev/tty if stdin isn't a tty won't. Elsewhere, i.e. Windows, just close by providing an empty string using the standard IPC::Run pipe mechanism. Patch by Andrew Dunstan, based on an idea from Craig Ringer. Reviewed by Mark Dilger. Discussion: https://postgr.es/m/873ebb57-fc98-340d-949d-691b1810bf66@2ndQuadrant.com
1 parent 0dc8ead commit 9af34f3

File tree

1 file changed

+27
-10
lines changed

1 file changed

+27
-10
lines changed

src/test/perl/TestLib.pm

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,8 @@ our @EXPORT = qw(
8787

8888
our ($windows_os, $tmp_check, $log_path, $test_logfile);
8989

90+
my @no_stdin;
91+
9092
BEGIN
9193
{
9294

@@ -178,6 +180,21 @@ INIT
178180
autoflush STDOUT 1;
179181
autoflush STDERR 1;
180182
autoflush $testlog 1;
183+
184+
# Settings to close stdin for certain commands.
185+
# On non-Windows, use a pseudo-terminal, so that libraries like openssl
186+
# which open the tty if they think stdin isn't one for a password
187+
# don't block. Windows doesn't have ptys, so just provide an empty
188+
# string for stdin.
189+
if ($windows_os)
190+
{
191+
@no_stdin = ('<', \"");
192+
}
193+
else
194+
{
195+
use charnames ':full';
196+
@no_stdin = ('<pty<', \"\N{END OF TRANSMISSION}");
197+
}
181198
}
182199

183200
END
@@ -343,7 +360,7 @@ sub run_command
343360
{
344361
my ($cmd) = @_;
345362
my ($stdout, $stderr);
346-
my $result = IPC::Run::run $cmd, '>', \$stdout, '2>', \$stderr;
363+
my $result = IPC::Run::run $cmd, '>', \$stdout, '2>', \$stderr, @no_stdin;
347364
chomp($stdout);
348365
chomp($stderr);
349366
return ($stdout, $stderr);
@@ -576,7 +593,7 @@ sub check_pg_config
576593
my ($regexp) = @_;
577594
my ($stdout, $stderr);
578595
my $result = IPC::Run::run [ 'pg_config', '--includedir' ], '>',
579-
\$stdout, '2>', \$stderr
596+
\$stdout, '2>', \$stderr, @no_stdin
580597
or die "could not execute pg_config";
581598
chomp($stdout);
582599
$stdout =~ s/\r$//;
@@ -673,7 +690,7 @@ sub program_help_ok
673690
my ($stdout, $stderr);
674691
print("# Running: $cmd --help\n");
675692
my $result = IPC::Run::run [ $cmd, '--help' ], '>', \$stdout, '2>',
676-
\$stderr;
693+
\$stderr, @no_stdin;
677694
ok($result, "$cmd --help exit code 0");
678695
isnt($stdout, '', "$cmd --help goes to stdout");
679696
is($stderr, '', "$cmd --help nothing to stderr");
@@ -695,7 +712,7 @@ sub program_version_ok
695712
my ($stdout, $stderr);
696713
print("# Running: $cmd --version\n");
697714
my $result = IPC::Run::run [ $cmd, '--version' ], '>', \$stdout, '2>',
698-
\$stderr;
715+
\$stderr, @no_stdin;
699716
ok($result, "$cmd --version exit code 0");
700717
isnt($stdout, '', "$cmd --version goes to stdout");
701718
is($stderr, '', "$cmd --version nothing to stderr");
@@ -718,8 +735,7 @@ sub program_options_handling_ok
718735
my ($stdout, $stderr);
719736
print("# Running: $cmd --not-a-valid-option\n");
720737
my $result = IPC::Run::run [ $cmd, '--not-a-valid-option' ], '>',
721-
\$stdout,
722-
'2>', \$stderr;
738+
\$stdout, '2>', \$stderr, @no_stdin;
723739
ok(!$result, "$cmd with invalid option nonzero exit code");
724740
isnt($stderr, '', "$cmd with invalid option prints error message");
725741
return;
@@ -740,7 +756,7 @@ sub command_like
740756
my ($cmd, $expected_stdout, $test_name) = @_;
741757
my ($stdout, $stderr);
742758
print("# Running: " . join(" ", @{$cmd}) . "\n");
743-
my $result = IPC::Run::run $cmd, '>', \$stdout, '2>', \$stderr;
759+
my $result = IPC::Run::run $cmd, '>', \$stdout, '2>', \$stderr, @no_stdin;
744760
ok($result, "$test_name: exit code 0");
745761
is($stderr, '', "$test_name: no stderr");
746762
like($stdout, $expected_stdout, "$test_name: matches");
@@ -769,7 +785,8 @@ sub command_like_safe
769785
my $stdoutfile = File::Temp->new();
770786
my $stderrfile = File::Temp->new();
771787
print("# Running: " . join(" ", @{$cmd}) . "\n");
772-
my $result = IPC::Run::run $cmd, '>', $stdoutfile, '2>', $stderrfile;
788+
my $result = IPC::Run::run $cmd, '>', $stdoutfile, '2>', $stderrfile,
789+
@no_stdin;
773790
$stdout = slurp_file($stdoutfile);
774791
$stderr = slurp_file($stderrfile);
775792
ok($result, "$test_name: exit code 0");
@@ -793,7 +810,7 @@ sub command_fails_like
793810
my ($cmd, $expected_stderr, $test_name) = @_;
794811
my ($stdout, $stderr);
795812
print("# Running: " . join(" ", @{$cmd}) . "\n");
796-
my $result = IPC::Run::run $cmd, '>', \$stdout, '2>', \$stderr;
813+
my $result = IPC::Run::run $cmd, '>', \$stdout, '2>', \$stderr, @no_stdin;
797814
ok(!$result, "$test_name: exit code not 0");
798815
like($stderr, $expected_stderr, "$test_name: matches");
799816
return;
@@ -831,7 +848,7 @@ sub command_checks_all
831848
# run command
832849
my ($stdout, $stderr);
833850
print("# Running: " . join(" ", @{$cmd}) . "\n");
834-
IPC::Run::run($cmd, '>', \$stdout, '2>', \$stderr);
851+
IPC::Run::run($cmd, '>', \$stdout, '2>', \$stderr, @no_stdin);
835852

836853
# See http://perldoc.perl.org/perlvar.html#%24CHILD_ERROR
837854
my $ret = $?;

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy