1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
|
#!/usr/bin/perl
use warnings;
use strict;
use Test::More;
if ($ENV{NO_NETWORK}) {
plan skip_all => 'Unset NO_NETWORK to enable tests requiring Internet';
}
plan tests => 4;
sub POE::Kernel::ASSERT_DEFAULT () { 1 }
use POE;
use POE::Component::Resolver qw(AF_INET AF_INET6);
my $r4 = POE::Component::Resolver->new(
max_resolvers => 1,
idle_timeout => 1,
af_order => [ AF_INET ],
);
# Try to detect whether we can resolve IPv6 addresses at all.
use Socket qw(getaddrinfo);
my $has_ipv6 = do {
my ($error, @addresses) = getaddrinfo(
"ipv6.test-ipv6.com", "www", { family => AF_INET6 }
);
($error or not @addresses) ? 0 : 1;
};
# If we can't, don't bother setting up resolvers for them.
my ($r6, $r46, $r64);
if ($has_ipv6) {
$r6 = POE::Component::Resolver->new(
max_resolvers => 1,
idle_timeout => 1,
af_order => [ AF_INET6 ],
);
$r46 = POE::Component::Resolver->new(
max_resolvers => 1,
idle_timeout => 1,
af_order => [ AF_INET, AF_INET6 ],
);
$r64 = POE::Component::Resolver->new(
max_resolvers => 1,
idle_timeout => 1,
af_order => [ AF_INET6, AF_INET ],
);
}
# TODO - Not robust to try a single remote host. I fully expect this
# to bite me later, unless someone wants to take a shot at it.
my $host = 'ipv6-test.com';
my $tcp = getprotobyname("tcp");
my $service = $^O eq 'solaris' ? 80 : 'http';
POE::Session->create(
inline_states => {
_start => sub {
$r4->resolve(
host => $host,
service => $service,,
hints => { protocol => $tcp },
misc => [ AF_INET ],
) or die $!;
SKIP: {
skip("IPv6 not detected; skipping IPv6 tests", 3) unless $has_ipv6;
$r6->resolve(
host => $host,
service => $service,
hints => { protocol => $tcp },
misc => [ AF_INET6 ],
) or die $!;
$r46->resolve(
host => $host,
service => $service,
hints => { protocol => $tcp },
misc => [ AF_INET, AF_INET6 ],
) or die $!;
$r64->resolve(
host => $host,
service => $service,
hints => { protocol => $tcp },
misc => [ AF_INET6, AF_INET ],
) or die $!;
}
},
resolver_response => sub {
my ($error, $addresses, $request) = @_[ARG0..ARG2];
foreach my $a (@$addresses) {
diag("$request->{host} = ", scalar($r4->unpack_addr($a)));
}
my $expected_families = $request->{misc};
my @got_families = map { $_->{family} } @$addresses;
my $i = $#got_families;
while ($i > 0 and $i--) {
splice(@got_families, $i, 1) if (
$got_families[$i] == $got_families[$i+1]
);
}
is_deeply(
\@got_families,
$expected_families,
"address families are as expected (@$expected_families)",
);
},
_stop => sub { undef }, # for ASSERT_DEFAULT
},
);
POE::Kernel->run();
|