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 = 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;
184 if ($^O eq 'mpeix') {
185 print("ok 12 # skipped\n")
189 $server->recv($buf, 100);
191 } elsif (defined($pid)) {
193 $sock = IO::Socket::INET->new(Proto => 'udp',
194 PeerAddr => "localhost:$port")
195 || IO::Socket::INET->new(Proto => 'udp',
196 PeerAddr => "127.0.0.1:$port");
197 $sock->send("ok 12\n");
199 $sock->send("ok 12\n"); # send another one to be sure
206 print "not " unless $server->blocking;
209 if ( $^O eq 'qnx' ) {
210 # QNX4 library bug: Can set non-blocking on socket, but
211 # cannot return that status.
212 print "ok 14 # skipped on QNX4\n";
214 $server->blocking(0);
215 print "not " if $server->blocking;
220 ### Set up some data to be transfered between the server and
221 ### the client. We'll use own source code ...
224 if( !open( SRC, "< $0")) {
225 print "not ok 15 - $!\n";
235 my $listen = IO::Socket::INET->new( Listen => 2, Proto => 'tcp', Timeout => 15) ||
238 die if( !defined( $listen));
239 my $serverport = $listen->sockport;
240 my $server_pid = fork();
243 ### TEST 17 Client/Server establishment
248 ### Get data from the server using a single stream
250 $sock = IO::Socket::INET->new("localhost:$serverport")
251 || IO::Socket::INET->new("127.0.0.1:$serverport");
254 $sock->print("send\n");
261 $sock->print("done\n");
264 print "not " if( @array != @data);
271 ### Get data from the server using a stream, which is
272 ### interrupted by eof calls.
273 ### On perl-5.7.0@7673 this failed in a SOCKS environment, because eof
274 ### did an getc followed by an ungetc in order to check for the streams
275 ### end. getc(3) got replaced by the SOCKS funktion, which ended up in
276 ### a recv(2) call on the socket, while ungetc(3) put back a character
277 ### to an IO buffer, which never again was read.
279 ### TESTS 19,20,21,22
280 ### Try to ping-pong some Unicode.
282 if ($^O eq 'mpeix') {
283 print "ok 19 # skipped: broken on MPE/iX\n";
285 $sock = IO::Socket::INET->new("localhost:$serverport")
286 || IO::Socket::INET->new("127.0.0.1:$serverport");
289 print binmode($sock, ":utf8") ? "ok 19\n" : "not ok 19\n";
291 print "ok 19 - Skip: no perlio\n";
297 $sock->print("ping \x{100}\n");
298 chomp(my $pong = scalar <$sock>);
299 print $pong =~ /^pong (.+)$/ && $1 eq "\x{100}" ?
300 "ok 20\n" : "not ok 20\n";
302 $sock->print("ord \x{100}\n");
303 chomp(my $ord = scalar <$sock>);
304 print $ord == 0x100 ?
305 "ok 21\n" : "not ok 21\n";
307 $sock->print("chr 0x100\n");
308 chomp(my $chr = scalar <$sock>);
309 print $chr eq "\x{100}" ?
310 "ok 22\n" : "not ok 22\n";
312 print "ok $_ - Skip: no perlio\n" for 20..22;
315 $sock->print("send\n");
318 while( !eof( $sock ) ){
325 $sock->print("done\n");
328 print "not " if( @array != @data);
338 $sock = IO::Socket::INET->new("localhost:$serverport")
339 || IO::Socket::INET->new("127.0.0.1:$serverport");
342 $sock->print("done\n");
345 print "not " if( 1 != kill 0, $server_pid);
351 } elsif (defined($server_pid)) {
355 SERVER_LOOP: while (1) {
356 last SERVER_LOOP unless $sock = $listen->accept;
357 # Do not print ok/not ok for this binmode() since there's
358 # a race condition with our client, just die if we fail.
359 if ($has_perlio) { binmode($sock, ":utf8") or die }
361 last SERVER_LOOP if /^quit/;
364 print $sock "pong $1\n";
368 print $sock ord($1), "\n";
372 print $sock chr(hex($1)), "\n";
394 # test Blocking option in constructor
396 $sock = IO::Socket::INET->new(Blocking => 0)
400 if ( $^O eq 'qnx' ) {
401 print "ok 26 # skipped on QNX4\n";
402 # QNX4 library bug: Can set non-blocking on socket, but
403 # cannot return that status.
405 my $status = $sock->blocking;
406 print "not " unless defined $status && !$status;