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