Blame t/getaddrinfo.t

Packit Service 098c73
use strict;
Packit Service 098c73
use warnings;
Packit Service 098c73
use Test::More tests => 31;
Packit Service 098c73
Packit Service 098c73
use Socket qw(:addrinfo AF_INET SOCK_STREAM IPPROTO_TCP unpack_sockaddr_in inet_aton);
Packit Service 098c73
Packit Service 098c73
my ( $err, @res );
Packit Service 098c73
Packit Service 098c73
( $err, @res ) = getaddrinfo( "127.0.0.1", "80", { socktype => SOCK_STREAM } );
Packit Service 098c73
cmp_ok( $err, "==", 0, '$err == 0 for host=127.0.0.1/service=80/socktype=STREAM' );
Packit Service 098c73
cmp_ok( $err, "eq", "", '$err eq "" for host=127.0.0.1/service=80/socktype=STREAM' );
Packit Service 098c73
is( scalar @res, 1,
Packit Service 098c73
    '@res has 1 result' );
Packit Service 098c73
Packit Service 098c73
is( $res[0]->{family}, AF_INET,
Packit Service 098c73
    '$res[0] family is AF_INET' );
Packit Service 098c73
is( $res[0]->{socktype}, SOCK_STREAM,
Packit Service 098c73
    '$res[0] socktype is SOCK_STREAM' );
Packit Service 098c73
ok( $res[0]->{protocol} == 0 || $res[0]->{protocol} == IPPROTO_TCP,
Packit Service 098c73
    '$res[0] protocol is 0 or IPPROTO_TCP' );
Packit Service 098c73
ok( defined $res[0]->{addr},
Packit Service 098c73
    '$res[0] addr is defined' );
Packit Service 098c73
if (length $res[0]->{addr}) {
Packit Service 098c73
    is_deeply( [ unpack_sockaddr_in $res[0]->{addr} ],
Packit Service 098c73
               [ 80, inet_aton( "127.0.0.1" ) ],
Packit Service 098c73
               '$res[0] addr is {"127.0.0.1", 80}' );
Packit Service 098c73
} else {
Packit Service 098c73
    fail( '$res[0] addr is empty: check $socksizetype' );
Packit Service 098c73
}
Packit Service 098c73
Packit Service 098c73
# Check actual IV integers work just as well as PV strings
Packit Service 098c73
( $err, @res ) = getaddrinfo( "127.0.0.1", 80, { socktype => SOCK_STREAM } );
Packit Service 098c73
cmp_ok( $err, "==", 0, '$err == 0 for host=127.0.0.1/service=80/socktype=STREAM' );
Packit Service 098c73
is_deeply( [ unpack_sockaddr_in $res[0]->{addr} ],
Packit Service 098c73
           [ 80, inet_aton( "127.0.0.1" ) ],
Packit Service 098c73
           '$res[0] addr is {"127.0.0.1", 80}' );
Packit Service 098c73
Packit Service 098c73
( $err, @res ) = getaddrinfo( "127.0.0.1", "" );
Packit Service 098c73
cmp_ok( $err, "==", 0, '$err == 0 for host=127.0.0.1' );
Packit Service 098c73
# Might get more than one; e.g. different socktypes
Packit Service 098c73
ok( scalar @res > 0, '@res has results' );
Packit Service 098c73
Packit Service 098c73
( $err, @res ) = getaddrinfo( "127.0.0.1", undef );
Packit Service 098c73
cmp_ok( $err, "==", 0, '$err == 0 for host=127.0.0.1/service=undef' );
Packit Service 098c73
Packit Service 098c73
# Test GETMAGIC
Packit Service 098c73
{
Packit Service 098c73
    "127.0.0.1" =~ /(.+)/;
Packit Service 098c73
    ( $err, @res ) = getaddrinfo($1, undef);
Packit Service 098c73
    cmp_ok( $err, "==", 0, '$err == 0 for host=$1' );
Packit Service 098c73
    ok( scalar @res > 0, '@res has results' );
Packit Service 098c73
    is( (unpack_sockaddr_in $res[0]->{addr})[1],
Packit Service 098c73
        inet_aton( "127.0.0.1" ),
Packit Service 098c73
        '$res[0] addr is {"127.0.0.1", ??}' );
Packit Service 098c73
}
Packit Service 098c73
Packit Service 098c73
( $err, @res ) = getaddrinfo( "", "80", { family => AF_INET, socktype => SOCK_STREAM, protocol => IPPROTO_TCP } );
Packit Service 098c73
cmp_ok( $err, "==", 0, '$err == 0 for service=80/family=AF_INET/socktype=STREAM/protocol=IPPROTO_TCP' );
Packit Service 098c73
is( scalar @res, 1, '@res has 1 result' );
Packit Service 098c73
Packit Service 098c73
# Just pick the first one
Packit Service 098c73
is( $res[0]->{family}, AF_INET,
Packit Service 098c73
    '$res[0] family is AF_INET' );
Packit Service 098c73
is( $res[0]->{socktype}, SOCK_STREAM,
Packit Service 098c73
    '$res[0] socktype is SOCK_STREAM' );
Packit Service 098c73
ok( $res[0]->{protocol} == 0 || $res[0]->{protocol} == IPPROTO_TCP,
Packit Service 098c73
    '$res[0] protocol is 0 or IPPROTO_TCP' );
Packit Service 098c73
Packit Service 098c73
# Now some tests of a few well-known internet hosts
Packit Service 098c73
my $goodhost = "cpan.perl.org";
Packit Service 098c73
Packit Service 098c73
SKIP: {
Packit Service 098c73
    skip "Resolver has no answer for $goodhost", 2 unless gethostbyname( $goodhost );
Packit Service 098c73
Packit Service 098c73
    ( $err, @res ) = getaddrinfo( "cpan.perl.org", "ftp", { socktype => SOCK_STREAM } );
Packit Service 098c73
    cmp_ok( $err, "==", 0, '$err == 0 for host=cpan.perl.org/service=ftp/socktype=STREAM' );
Packit Service 098c73
    # Might get more than one; e.g. different families
Packit Service 098c73
    ok( scalar @res > 0, '@res has results' );
Packit Service 098c73
}
Packit Service 098c73
Packit Service 098c73
# Now something I hope doesn't exist - we put it in a known-missing TLD
Packit Service 098c73
my $missinghost = "TbK4jM2M0OS.lm57DWIyu4i";
Packit Service 098c73
Packit Service 098c73
# Some CPAN testing machines seem to have wildcard DNS servers that reply to
Packit Service 098c73
# any request. We'd better check for them
Packit Service 098c73
Packit Service 098c73
SKIP: {
Packit Service 098c73
    skip "Resolver has an answer for $missinghost", 1 if gethostbyname( $missinghost );
Packit Service 098c73
Packit Service 098c73
    # Some OSes return $err == 0 but no results
Packit Service 098c73
    ( $err, @res ) = getaddrinfo( $missinghost, "ftp", { socktype => SOCK_STREAM } );
Packit Service 098c73
    ok( $err != 0 || ( $err == 0 && @res == 0 ),
Packit Service 098c73
        '$err != 0 or @res == 0 for host=TbK4jM2M0OS.lm57DWIyu4i/service=ftp/socktype=SOCK_STREAM' );
Packit Service 098c73
    if( @res ) {
Packit Service 098c73
        # Diagnostic that might help
Packit Service 098c73
        while( my $r = shift @res ) {
Packit Service 098c73
            diag( "family=$r->{family} socktype=$r->{socktype} protocol=$r->{protocol} addr=[" . length( $r->{addr} ) . " bytes]" );
Packit Service 098c73
            diag( "  addr=" . join( ", ", map { sprintf '0x%02x', ord $_ } split m//, $r->{addr} ) );
Packit Service 098c73
        }
Packit Service 098c73
    }
Packit Service 098c73
}
Packit Service 098c73
Packit Service 098c73
# Numeric addresses with AI_NUMERICHOST should pass (RT95758)
Packit Service 098c73
AI_NUMERICHOST: {
Packit Service 098c73
    # Here we need a port that is open to the world. Not all places have all
Packit Service 098c73
    # the ports. For example Solaris by default doesn't have http/80 in
Packit Service 098c73
    # /etc/services, and that would fail. Let's try a couple of commonly open
Packit Service 098c73
    # ports, and hope one of them will succeed. Conversely this means that
Packit Service 098c73
    # sometimes this will fail.
Packit Service 098c73
    #
Packit Service 098c73
    # An alternative method would be to manually parse /etc/services and look
Packit Service 098c73
    # for enabled services but that's kind of yuck, too.
Packit Service 098c73
    my @port = (80, 7, 22, 25, 88, 123, 110, 389, 443, 445, 873, 2049, 3306);
Packit Service 098c73
    foreach my $port ( @port ) {
Packit Service 098c73
        ( $err, @res ) = getaddrinfo( "127.0.0.1", $port, { flags => AI_NUMERICHOST, socktype => SOCK_STREAM } );
Packit Service 098c73
        if( $err == 0 ) {
Packit Service 098c73
            ok( $err == 0, "\$err == 0 for 127.0.0.1/$port/flags=AI_NUMERICHOST" );
Packit Service 098c73
            last AI_NUMERICHOST;
Packit Service 098c73
        }
Packit Service 098c73
    }
Packit Service 098c73
    fail( "$err for 127.0.0.1/$port[-1]/flags=AI_NUMERICHOST (failed for ports @port)" );
Packit Service 098c73
}
Packit Service 098c73
Packit Service 098c73
# Now check that names with AI_NUMERICHOST fail
Packit Service 098c73
Packit Service 098c73
SKIP: {
Packit Service 098c73
    skip "Resolver has no answer for $goodhost", 1 unless gethostbyname( $goodhost );
Packit Service 098c73
Packit Service 098c73
    ( $err, @res ) = getaddrinfo( $goodhost, "ftp", { flags => AI_NUMERICHOST, socktype => SOCK_STREAM } );
Packit Service 098c73
    ok( $err != 0, "\$err != 0 for host=$goodhost/service=ftp/flags=AI_NUMERICHOST/socktype=SOCK_STREAM" );
Packit Service 098c73
}
Packit Service 098c73
Packit Service 098c73
# Some sanity checking on the hints hash
Packit Service 098c73
ok( defined eval { getaddrinfo( "127.0.0.1", "80", undef ); 1 },
Packit Service 098c73
    'getaddrinfo() with undef hints works' );
Packit Service 098c73
ok( !defined eval { getaddrinfo( "127.0.0.1", "80", "hints" ); 1 },
Packit Service 098c73
    'getaddrinfo() with string hints dies' );
Packit Service 098c73
ok( !defined eval { getaddrinfo( "127.0.0.1", "80", [] ); 1 },
Packit Service 098c73
    'getaddrinfo() with ARRAY hints dies' );
Packit Service 098c73
Packit Service 098c73
# Ensure it doesn't segfault if args are missing
Packit Service 098c73
Packit Service 098c73
( $err, @res ) = getaddrinfo();
Packit Service 098c73
ok( defined $err, '$err defined for getaddrinfo()' );
Packit Service 098c73
Packit Service 098c73
( $err, @res ) = getaddrinfo( "127.0.0.1" );
Packit Service 098c73
ok( defined $err, '$err defined for getaddrinfo("127.0.0.1")' );