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::INET->new(
28      ( $socktype eq "SOCK_STREAM" ? ( Listen => 1 ) : () ),
29      LocalHost => "127.0.0.1",
30      Type      => Socket->$socktype,
31      Proto     => ( $socktype eq "SOCK_STREAM" ? "tcp" : "udp" ), # Because IO::Socket::INET is stupid and always presumes tcp
32   ) or die "Cannot listen on PF_INET - $@";
33
34   my $socket = IO::Socket::IP->new(
35      PeerHost    => "127.0.0.1",
36      PeerService => $testserver->sockport,
37      Type        => Socket->$socktype,
38   );
39
40   ok( defined $socket, "IO::Socket::IP->new constructs a $socktype socket" ) or
41      diag( "  error was $@" );
42
43   is( $socket->sockdomain, AF_INET,           "\$socket->sockdomain for $socktype" );
44   is( $socket->socktype,   Socket->$socktype, "\$socket->socktype for $socktype" );
45
46   my $testclient = ( $socktype eq "SOCK_STREAM" ) ?
47      $testserver->accept :
48      do { $testserver->connect( $socket->sockname ); $testserver };
49
50   ok( defined $testclient, "accepted test $socktype client" );
51
52   ok( $socket->connected, "\$socket is connected for $socktype" );
53   ok( $socket->blocking, "\$socket is in blocking mode after connect for $socktype" );
54
55   is_deeply( [ unpack_sockaddr_in $socket->sockname ],
56              [ unpack_sockaddr_in $testclient->peername ],
57              "\$socket->sockname for $socktype" );
58
59   is_deeply( [ unpack_sockaddr_in $socket->peername ],
60              [ unpack_sockaddr_in $testclient->sockname ],
61              "\$socket->peername for $socktype" );
62
63   is( $socket->peerhost, $INADDR_LOOPBACK_HOST, "\$socket->peerhost for $socktype" );
64   is( $socket->peerport, $testserver->sockport, "\$socket->peerport for $socktype" );
65
66   # Unpack just so it pretty prints without wrecking the terminal if it fails
67   is( unpack("H*", $socket->sockaddr), $INADDR_LOOPBACK_HEX, "\$socket->sockaddr for $socktype" );
68   is( unpack("H*", $socket->peeraddr), $INADDR_LOOPBACK_HEX, "\$socket->peeraddr for $socktype" );
69
70   # Can't easily test the non-numeric versions without relying on the system's
71   # ability to resolve the name "localhost"
72
73   $socket->close;
74   ok( !$socket->connected, "\$socket not connected after close for $socktype" );
75}
76
77done_testing;
78