This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Unit tests for Socket::getaddrinfo() and Socket::getnameinfo()
[perl5.git] / ext / Socket / t / getnameinfo.t
CommitLineData
27dd37a3
PLE
1use strict;
2use warnings;
3use Test::More tests => 10;
4
5use Socket qw(
6 AF_INET NI_NUMERICHOST NI_NUMERICSERV
7 getnameinfo pack_sockaddr_in inet_aton
8);
9
10my ( $err, $host, $service );
11
12( $err, $host, $service ) = getnameinfo( pack_sockaddr_in( 80, inet_aton( "127.0.0.1" ) ), NI_NUMERICHOST|NI_NUMERICSERV );
13cmp_ok( $err, "==", 0, '$err == 0 for {family=AF_INET,port=80,sinaddr=127.0.0.1}/NI_NUMERICHOST|NI_NUMERICSERV' );
14cmp_ok( $err, "eq", "", '$err eq "" for {family=AF_INET,port=80,sinaddr=127.0.0.1}/NI_NUMERICHOST|NI_NUMERICSERV' );
15
16is( $host, "127.0.0.1", '$host is 127.0.0.1 for NH/NS' );
17is( $service, "80", '$service is 80 for NH/NS' );
18
19# Probably "localhost" but we'd better ask the system to be sure
20my $expect_host = gethostbyaddr( inet_aton( "127.0.0.1" ), AF_INET );
21defined $expect_host or $expect_host = "127.0.0.1";
22
23( $err, $host, $service ) = getnameinfo( pack_sockaddr_in( 80, inet_aton( "127.0.0.1" ) ), NI_NUMERICSERV );
24cmp_ok( $err, "==", 0, '$err == 0 for {family=AF_INET,port=80,sinaddr=127.0.0.1}/NI_NUMERICSERV' );
25
26is( $host, $expect_host, "\$host is $expect_host for NS" );
27is( $service, "80", '$service is 80 for NS' );
28
29# Probably "www" but we'd better ask the system to be sure
30my $expect_service = getservbyport( 80, "tcp" );
31defined $expect_service or $expect_service = "80";
32
33( $err, $host, $service ) = getnameinfo( pack_sockaddr_in( 80, inet_aton( "127.0.0.1" ) ), NI_NUMERICHOST );
34cmp_ok( $err, "==", 0, '$err == 0 for {family=AF_INET,port=80,sinaddr=127.0.0.1}/NI_NUMERICHOST' );
35
36is( $host, "127.0.0.1", '$host is 127.0.0.1 for NH' );
37is( $service, $expect_service, "\$service is $expect_service for NH" );