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