-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathtest.t
240 lines (204 loc) · 7.15 KB
/
test.t
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
#!perl
use strict;
use warnings;
use Test::More tests => 5;
$^W = 1; # enable warnings
use IO::Pty;
use IO::Tty qw(TIOCSCTTY TIOCNOTTY TCSETCTTY);
$IO::Tty::DEBUG = 1;
require POSIX;
my $Perl = $^X;
diag("Configuration: $IO::Tty::CONFIG");
diag("Checking for appropriate ioctls:");
diag("TIOCNOTTY") if defined TIOCNOTTY;
diag("TIOCSCTTY") if defined TIOCSCTTY;
diag("TCSETCTTY") if defined TCSETCTTY;
{
my $pid = fork();
die "Cannot fork" if not defined $pid;
unless ($pid) {
# child closes stdin/out and reports test result via exit status
sleep 0;
close STDIN;
close STDOUT;
my $master = new IO::Pty;
my $slave = $master->slave();
my $master_fileno = $master->fileno;
my $slave_fileno = $slave->fileno;
$master->close();
if ( $master_fileno < 3 or $slave_fileno < 3 ) { # altered
die("ERROR: masterfd=$master_fileno, slavefd=$slave_fileno"); # altered
}
exit(0);
}
is( wait, $pid, "fork exits with 0 exit code" ) or die("Wrong child");
is( $?, 0, "0 exit code from forked child - Checking that returned fd's don't clash with stdin/out/err" );
}
{
diag(" === Checking if child gets pty as controlling terminal");
my $master = new IO::Pty;
pipe( FROM_CHILD, TO_PARENT )
or die "Cannot create pipe: $!";
my $pid = fork();
die "Cannot fork" if not defined $pid;
unless ($pid) {
# child
sleep(1);
$master->make_slave_controlling_terminal();
my $slave = $master->slave();
close $master;
close FROM_CHILD;
print TO_PARENT "\n";
close TO_PARENT;
open( TTY, "+>/dev/tty" ) or die "no controlling terminal";
autoflush TTY 1;
print TTY "gimme on /dev/tty: ";
my $s = <TTY>;
chomp $s;
print $slave "back on STDOUT: \U$s\n";
close TTY;
close $slave;
sleep(1);
exit 0;
}
close TO_PARENT;
$master->close_slave();
my $dummy;
my $stat = sysread( FROM_CHILD, $dummy, 1 );
die "Cannot sync with child: $!" if not $stat;
close FROM_CHILD;
my ( $s, $chunk );
$SIG{ALRM} = sub { die("Timeout ($s)"); };
alarm(10);
sysread( $master, $s, 100 ) or die "sysread() failed: $!";
like( $s, qr/gimme.*:/, "master object outputs: '$s'" );
print $master "seems OK!\n";
# collect all responses
my $ret;
while ( $ret = sysread( $master, $chunk, 100 ) ) {
$s .= $chunk;
}
like( $s, qr/back on STDOUT: SEEMS OK!/, "STDOUT looks right" );
warn <<"_EOT_" unless defined $ret;
WARNING: when the client closes the slave pty, the master gets an error
(undef return value and \$! eq "$!")
instead of EOF (0 return value). Please be sure to handle this
in your application (Expect already does).
_EOT_
alarm(0);
kill TERM => $pid;
}
# now for the echoback tests
diag(
"Checking basic functionality and how your ptys handle large strings...
This test may hang on certain systems, even though it is protected
by alarm(). If the counter stops, try Ctrl-C, the test should continue."
);
{
my $randstring =
q{fakjdf ijj845jtirg\r8e 4jy8 gfuoyhj\agt8h\0x00 gues98\0xFF 45th guoa\beh gt98hae 45t8u ha8rhg ue4ht 8eh tgo8he4 t8 gfj aoingf9a8hgf uain dgkjadshft+uehgf =usüand9ß87vgh afugh 8*h 98H 978H 7HG zG 86G (&g (O/g &(GF(/EG F78G F87SG F(/G F(/a sldjkf ha\@j<\rksdhf jk>~|ahsd fjkh asdHJKGDSG TRJKSGO JGDSFJDFHJGSDK1%&FJGSDGFSH\0xADJäDGFljkhf lakjs(dh fkjahs djfk hasjkdh fjklahs dfkjhdjkf haöjksdh fkjah sdjf)\$/§&k hasÄÜÖjkdh fkjhuerhtuwe htui eruth ZI AHD BIZA Di7GH )/g98 9 97 86tr(& TA&(t 6t &T 75r 5\$R%/4r76 5&/% R79 5 )/&};
my $master = new IO::Pty;
diag( "isatty(\$master): ", POSIX::isatty($master) ? "YES" : "NO" );
if ( POSIX::isatty($master) ) {
$master->set_raw()
or warn "warning: \$master->set_raw(): $!";
}
pipe( FROM_CHILD, TO_PARENT )
or die "Cannot create pipe: $!";
my $pid = fork();
die "Cannot fork" if not defined $pid;
unless ($pid) {
# child sends back everything inverted
my $c;
my $slave = $master->slave();
close $master;
diag( "isatty(\$slave): ", POSIX::isatty($slave) ? "YES" : "NO" );
$slave->set_raw()
or warn "warning: \$slave->set_raw(): $!";
close FROM_CHILD;
print TO_PARENT "\n";
close TO_PARENT;
my $cnt = 0;
my $linecnt = 0;
while (1) {
my $ret = sysread( $slave, $c, 1 );
warn "sysread(): $!" unless defined $ret;
die "Slave got EOF at line $linecnt, byte $cnt.\n" unless $ret;
$cnt++;
if ( $c eq "\n" ) {
$linecnt++;
$cnt = 0;
}
else {
$c = ~$c;
}
$ret = syswrite( $slave, $c, 1 );
warn "syswrite(): $!" unless defined $ret;
}
}
close TO_PARENT;
$master->close_slave();
my $dummy;
my $stat = sysread( FROM_CHILD, $dummy, 1 );
die "Cannot sync with child: $!" if not $stat;
close FROM_CHILD;
diag("Child PID = $pid");
# parent sends down some strings and expects to get them back inverted
my $maxlen = 0;
foreach my $len ( 1 .. length($randstring) ) {
my $s = substr( $randstring, 0, $len );
my $buf;
my $ret = "";
my $inv = ~$s . "\n";
$s .= "\n";
my $sendbuf = $s;
$SIG{ALRM} = $SIG{TERM} = $SIG{INT} = sub { die "TIMEOUT(SIG" . shift() . ")"; };
eval {
alarm(15);
while ( $sendbuf or length($ret) < length($s) ) {
if ($sendbuf) {
my $sent = syswrite( $master, $sendbuf, length($sendbuf) );
die "syswrite() failed: $!" unless defined $sent;
$sendbuf = substr( $sendbuf, $sent );
}
$buf = "";
my $read = sysread( $master, $buf, length($s) );
die "Couldn't read from child: $!" if not $read;
$ret .= $buf;
}
alarm(0);
};
if ($@) {
warn $@;
last;
}
if ( $ret eq $inv ) {
$maxlen = $len;
}
else {
if ( length($s) == length($ret) ) {
warn "Got back a wrong string with the right length " . length($ret) . "\n";
}
else {
warn "Got back a wrong string with the wrong length " . length($ret) . " (instead of " . length($s) . ")\n";
}
ok(0);
last;
}
}
$SIG{ALRM} = $SIG{TERM} = $SIG{INT} = 'DEFAULT';
if ( $maxlen < length($randstring) ) {
warn <<"_EOT_";
WARNING: your raw ptys block when sending more than $maxlen bytes!
This may cause problems under special scenarios, but you probably
will never encounter that problem.
_EOT_
}
else {
diag("Good, your raw ptys can handle at least $maxlen bytes at once.");
}
ok( $maxlen >= 200, "\$maxlen >= 200 ($maxlen)" );
close($master);
sleep(1);
kill TERM => $pid;
}