1#!/usr/bin/perl 2 3use strict; 4use warnings; 5 6use Test::More; 7 8use IO::Socket::IP; 9 10use IO::Socket::INET; 11use Socket qw( inet_aton inet_ntoa pack_sockaddr_in unpack_sockaddr_in ); 12 13# Some odd locations like BSD jails might not like INADDR_LOOPBACK. We'll 14# establish a baseline first to test against 15my $INADDR_LOOPBACK = do { 16 socket my $sockh, PF_INET, SOCK_STREAM, 0 or die "Cannot socket(PF_INET) - $!"; 17 bind $sockh, pack_sockaddr_in( 0, inet_aton( "127.0.0.1" ) ) or die "Cannot bind() - $!"; 18 ( unpack_sockaddr_in( getsockname $sockh ) )[1]; 19}; 20my $INADDR_LOOPBACK_HOST = inet_ntoa( $INADDR_LOOPBACK ); 21if( $INADDR_LOOPBACK ne INADDR_LOOPBACK ) { 22 diag( "Testing with INADDR_LOOPBACK=$INADDR_LOOPBACK_HOST; this may be because of odd networking" ); 23} 24my $INADDR_LOOPBACK_HEX = unpack "H*", $INADDR_LOOPBACK; 25 26foreach my $socktype (qw( SOCK_STREAM SOCK_DGRAM )) { 27 my $testserver = IO::Socket::IP->new( 28 ( $socktype eq "SOCK_STREAM" ? ( Listen => 1 ) : () ), 29 LocalHost => "127.0.0.1", 30 LocalPort => "0", 31 Type => Socket->$socktype, 32 ); 33 34 ok( defined $testserver, "IO::Socket::IP->new constructs a $socktype socket" ) or 35 diag( " error was $@" ); 36 37 is( $testserver->sockdomain, AF_INET, "\$testserver->sockdomain for $socktype" ); 38 is( $testserver->socktype, Socket->$socktype, "\$testserver->socktype for $socktype" ); 39 40 is( $testserver->sockhost, $INADDR_LOOPBACK_HOST, "\$testserver->sockhost for $socktype" ); 41 like( $testserver->sockport, qr/^\d+$/, "\$testserver->sockport for $socktype" ); 42 43 ok( eval { $testserver->peerport; 1 }, "\$testserver->peerport does not die for $socktype" ) 44 or do { chomp( my $e = $@ ); diag( "Exception was: $e" ) }; 45 46 is_deeply( { host => $testserver->peerhost, port => $testserver->peerport }, 47 { host => undef, port => undef }, 48 'peerhost/peersock yield scalar' ); 49 50 my $socket = IO::Socket::INET->new( 51 PeerHost => "127.0.0.1", 52 PeerPort => $testserver->sockport, 53 Type => Socket->$socktype, 54 Proto => ( $socktype eq "SOCK_STREAM" ? "tcp" : "udp" ), # Because IO::Socket::INET is stupid and always presumes tcp 55 ) or die "Cannot connect to PF_INET - $@"; 56 57 my $testclient = ( $socktype eq "SOCK_STREAM" ) ? 58 $testserver->accept : 59 do { $testserver->connect( $socket->sockname ); $testserver }; 60 61 ok( defined $testclient, "accepted test $socktype client" ); 62 isa_ok( $testclient, "IO::Socket::IP", "\$testclient for $socktype" ); 63 64 is( $testclient->sockdomain, AF_INET, "\$testclient->sockdomain for $socktype" ); 65 is( $testclient->socktype, Socket->$socktype, "\$testclient->socktype for $socktype" ); 66 67 is_deeply( [ unpack_sockaddr_in $socket->sockname ], 68 [ unpack_sockaddr_in $testclient->peername ], 69 "\$socket->sockname for $socktype" ); 70 71 is_deeply( [ unpack_sockaddr_in $socket->peername ], 72 [ unpack_sockaddr_in $testclient->sockname ], 73 "\$socket->peername for $socktype" ); 74 75 is( $testclient->sockport, $socket->peerport, "\$testclient->sockport for $socktype" ); 76 is( $testclient->peerport, $socket->sockport, "\$testclient->peerport for $socktype" ); 77 78 # Unpack just so it pretty prints without wrecking the terminal if it fails 79 is( unpack("H*", $testclient->sockaddr), $INADDR_LOOPBACK_HEX, "\$testclient->sockaddr for $socktype" ); 80 is( unpack("H*", $testclient->peeraddr), $INADDR_LOOPBACK_HEX, "\$testclient->peeraddr for $socktype" ); 81} 82 83done_testing; 84