4 unless(grep /blib/, @INC) {
13 if (-d "lib" && -f "TEST") {
15 if (! $Config{'d_fork'}) {
18 elsif ($Config{'extensions'} !~ /\bSocket\b/) {
19 $reason = 'Socket extension unavailable';
21 elsif ($Config{'extensions'} !~ /\bIO\b/) {
22 $reason = 'IO extension unavailable';
25 print "1..0 # Skip: $reason\n";
31 my $has_perlio = $] >= 5.008 && find PerlIO::Layer 'perlio';
37 $SIG{ALRM} = sub { die; };
43 $listen = IO::Socket::INET->new(Listen => 2,
45 # some systems seem to need as much as 10,
46 # so be generous with the timeout
52 # Check if can fork with dynamic extensions (bug in CRT):
54 system "$^X -I../lib -MOpcode -e 'defined fork or die' > /dev/null 2>&1") {
55 print "ok $_ # skipped: broken fork\n" for 2..5;
59 $port = $listen->sockport;
63 $sock = $listen->accept() or die "accept failed: $!";
67 print $sock->getline();
77 } elsif(defined $pid) {
79 $sock = IO::Socket::INET->new(PeerPort => $port,
81 PeerAddr => 'localhost'
83 || IO::Socket::INET->new(PeerPort => $port,
85 PeerAddr => '127.0.0.1'
87 or die "$! (maybe your system does not have a localhost at all, 'localhost' or 127.0.0.1)";
93 print $sock->getline();
102 # Test various other ways to create INET sockets that should
104 $listen = IO::Socket::INET->new(Listen => '', Timeout => 15) or die "$!";
105 $port = $listen->sockport;
110 last SERVER_LOOP unless $sock = $listen->accept;
112 last SERVER_LOOP if /^quit/;
119 } elsif (defined $pid) {
120 # child, try various ways to connect
121 $sock = IO::Socket::INET->new("localhost:$port")
122 || IO::Socket::INET->new("127.0.0.1:$port");
124 print "not " unless $sock->connected;
126 $sock->print("ok 7\n");
129 $sock->print("ok 9\n");
130 $sock->print("done\n");
141 # some machines seem to suffer from a race condition here
144 $sock = IO::Socket::INET->new("127.0.0.1:$port");
146 $sock->print("ok 10\n");
147 $sock->print("done\n");
155 # some machines seem to suffer from a race condition here
158 $sock = IO::Socket->new(Domain => AF_INET,
159 PeerAddr => "localhost:$port")
160 || IO::Socket->new(Domain => AF_INET,
161 PeerAddr => "127.0.0.1:$port");
163 $sock->print("ok 11\n");
164 $sock->print("quit\n");
175 # Then test UDP sockets
176 $server = IO::Socket->new(Domain => AF_INET,
178 LocalAddr => 'localhost')
179 || IO::Socket->new(Domain => AF_INET,
181 LocalAddr => '127.0.0.1');
182 $port = $server->sockport;
186 $server->recv($buf, 100);
188 } elsif (defined($pid)) {
190 $sock = IO::Socket::INET->new(Proto => 'udp',
191 PeerAddr => "localhost:$port")
192 || IO::Socket::INET->new(Proto => 'udp',
193 PeerAddr => "127.0.0.1:$port");
194 $sock->send("ok 12\n");
196 $sock->send("ok 12\n"); # send another one to be sure
202 print "not " unless $server->blocking;
205 if ( $^O eq 'qnx' ) {
206 # QNX4 library bug: Can set non-blocking on socket, but
207 # cannot return that status.
208 print "ok 14 # skipped on QNX4\n";
210 $server->blocking(0);
211 print "not " if $server->blocking;
216 ### Set up some data to be transfered between the server and
217 ### the client. We'll use own source code ...
220 if( !open( SRC, "< $0")) {
221 print "not ok 15 - $!\n";
231 my $listen = IO::Socket::INET->new( Listen => 2, Proto => 'tcp', Timeout => 15) ||
234 die if( !defined( $listen));
235 my $serverport = $listen->sockport;
236 my $server_pid = fork();
239 ### TEST 17 Client/Server establishment
244 ### Get data from the server using a single stream
246 $sock = IO::Socket::INET->new("localhost:$serverport")
247 || IO::Socket::INET->new("127.0.0.1:$serverport");
250 $sock->print("send\n");
257 $sock->print("done\n");
260 print "not " if( @array != @data);
267 ### Get data from the server using a stream, which is
268 ### interrupted by eof calls.
269 ### On perl-5.7.0@7673 this failed in a SOCKS environment, because eof
270 ### did an getc followed by an ungetc in order to check for the streams
271 ### end. getc(3) got replaced by the SOCKS funktion, which ended up in
272 ### a recv(2) call on the socket, while ungetc(3) put back a character
273 ### to an IO buffer, which never again was read.
275 ### TESTS 19,20,21,22
276 ### Try to ping-pong some Unicode.
278 $sock = IO::Socket::INET->new("localhost:$serverport")
279 || IO::Socket::INET->new("127.0.0.1:$serverport");
282 print binmode($sock, ":utf8") ? "ok 19\n" : "not ok 19\n";
284 print "ok 19 - Skip: no perlio\n";
290 $sock->print("ping \x{100}\n");
291 chomp(my $pong = scalar <$sock>);
292 print $pong =~ /^pong (.+)$/ && $1 eq "\x{100}" ?
293 "ok 20\n" : "not ok 20\n";
295 $sock->print("ord \x{100}\n");
296 chomp(my $ord = scalar <$sock>);
297 print $ord == 0x100 ?
298 "ok 21\n" : "not ok 21\n";
300 $sock->print("chr 0x100\n");
301 chomp(my $chr = scalar <$sock>);
302 print $chr eq "\x{100}" ?
303 "ok 22\n" : "not ok 22\n";
305 print "ok $_ - Skip: no perlio\n" for 20..22;
308 $sock->print("send\n");
311 while( !eof( $sock ) ){
318 $sock->print("done\n");
321 print "not " if( @array != @data);
330 $sock = IO::Socket::INET->new("localhost:$serverport")
331 || IO::Socket::INET->new("127.0.0.1:$serverport");
334 $sock->print("done\n");
337 print "not " if( 1 != kill 0, $server_pid);
343 } elsif (defined($server_pid)) {
347 SERVER_LOOP: while (1) {
348 last SERVER_LOOP unless $sock = $listen->accept;
349 # Do not print ok/not ok for this binmode() since there's
350 # a race condition with our client, just die if we fail.
351 if ($has_perlio) { binmode($sock, ":utf8") or die }
353 last SERVER_LOOP if /^quit/;
356 print $sock "pong $1\n";
360 print $sock ord($1), "\n";
364 print $sock chr(hex($1)), "\n";
386 # test Blocking option in constructor
388 $sock = IO::Socket::INET->new(Blocking => 0)
392 if ( $^O eq 'qnx' ) {
393 print "ok 26 # skipped on QNX4\n";
394 # QNX4 library bug: Can set non-blocking on socket, but
395 # cannot return that status.
397 my $status = $sock->blocking;
398 print "not " unless defined $status && !$status;