1#!/usr/bin/perl
2
3use strict;
4use warnings;
5
6use Test::More;
7
8use IO::Socket::IP;
9use Socket 1.95 qw(
10   PF_INET SOCK_STREAM IPPROTO_TCP pack_sockaddr_in INADDR_ANY
11   AI_PASSIVE AI_NUMERICSERV
12);
13
14my $AI_ADDRCONFIG = eval { Socket::AI_ADDRCONFIG() } || 0;
15
16my @gai_args;
17my @gai_rets;
18
19no strict 'refs';
20no warnings 'redefine';
21
22*{"IO::Socket::IP::getaddrinfo"} = sub {
23   push @gai_args, [ @_ ];
24   return @{ shift @gai_rets };
25};
26
27@gai_args = ();
28@gai_rets = (
29   [ "", {
30         family   => PF_INET,
31         socktype => SOCK_STREAM,
32         protocol => IPPROTO_TCP,
33         addr     => pack_sockaddr_in( 80, INADDR_ANY )
34      } ],
35);
36IO::Socket::IP->new( LocalPort => "80" );
37
38is_deeply( \@gai_args,
39           [
40              [ undef, "80", { flags => AI_PASSIVE|$AI_ADDRCONFIG, socktype => SOCK_STREAM, protocol => IPPROTO_TCP } ],
41           ],
42           '@gai_args for LocalPort => "80"' );
43
44SKIP: {
45   skip "No AI_NUMERICSERV", 1 unless defined eval { AI_NUMERICSERV() };
46
47   @gai_args = ();
48   @gai_rets = (
49      [ "", {
50            family   => PF_INET,
51            socktype => SOCK_STREAM,
52            protocol => IPPROTO_TCP,
53            addr     => pack_sockaddr_in( 80, INADDR_ANY )
54         } ],
55   );
56   IO::Socket::IP->new( LocalPort => "80", GetAddrInfoFlags => AI_NUMERICSERV );
57
58   is_deeply( \@gai_args,
59              [
60                 [ undef, "80", { flags => AI_PASSIVE|AI_NUMERICSERV, socktype => SOCK_STREAM, protocol => IPPROTO_TCP } ],
61              ],
62              '@gai_args for LocalPort => "80", GetAddrInfoFlags => AI_NUMERICSERV' );
63}
64
65done_testing;
66