xref: /openbsd-src/gnu/usr.bin/perl/cpan/IO-Socket-IP/t/02local-server-v4.t (revision f2da64fbbbf1b03f09f390ab01267c93dfd77c4c)
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::IP->new(
28      ( $socktype eq "SOCK_STREAM" ? ( Listen => 1 ) : () ),
29      LocalHost => "127.0.0.1",
30      Type      => Socket->$socktype,
31   );
32
33   ok( defined $testserver, "IO::Socket::IP->new constructs a $socktype socket" ) or
34      diag( "  error was $@" );
35
36   is( $testserver->sockdomain, AF_INET,           "\$testserver->sockdomain for $socktype" );
37   is( $testserver->socktype,   Socket->$socktype, "\$testserver->socktype for $socktype" );
38
39   is( $testserver->sockhost, $INADDR_LOOPBACK_HOST, "\$testserver->sockhost for $socktype" );
40   like( $testserver->sockport, qr/^\d+$/, "\$testserver->sockport for $socktype" );
41
42   my $socket = IO::Socket::INET->new(
43      PeerHost => "127.0.0.1",
44      PeerPort => $testserver->sockport,
45      Type     => Socket->$socktype,
46      Proto    => ( $socktype eq "SOCK_STREAM" ? "tcp" : "udp" ), # Because IO::Socket::INET is stupid and always presumes tcp
47   ) or die "Cannot connect to PF_INET - $@";
48
49   my $testclient = ( $socktype eq "SOCK_STREAM" ) ?
50      $testserver->accept :
51      do { $testserver->connect( $socket->sockname ); $testserver };
52
53   ok( defined $testclient, "accepted test $socktype client" );
54   isa_ok( $testclient, "IO::Socket::IP", "\$testclient for $socktype" );
55
56   is( $testclient->sockdomain, AF_INET,           "\$testclient->sockdomain for $socktype" );
57   is( $testclient->socktype,   Socket->$socktype, "\$testclient->socktype for $socktype" );
58
59   is_deeply( [ unpack_sockaddr_in $socket->sockname ],
60              [ unpack_sockaddr_in $testclient->peername ],
61              "\$socket->sockname for $socktype" );
62
63   is_deeply( [ unpack_sockaddr_in $socket->peername ],
64              [ unpack_sockaddr_in $testclient->sockname ],
65              "\$socket->peername for $socktype" );
66
67   is( $testclient->sockport, $socket->peerport, "\$testclient->sockport for $socktype" );
68   is( $testclient->peerport, $socket->sockport, "\$testclient->peerport for $socktype" );
69
70   # Unpack just so it pretty prints without wrecking the terminal if it fails
71   is( unpack("H*", $testclient->sockaddr), $INADDR_LOOPBACK_HEX, "\$testclient->sockaddr for $socktype" );
72   is( unpack("H*", $testclient->peeraddr), $INADDR_LOOPBACK_HEX, "\$testclient->peeraddr for $socktype" );
73}
74
75done_testing;
76