BEGIN {
if
(
$ENV
{NO_NETWORK_TESTING} ||
(
$ENV
{PERL_CORE} && !
$ENV
{PERL_TEST_Net_Ping})) {
print
"1..0 # Skip: network dependent test\n"
;
exit
;
}
unless
(
eval
"require Socket"
) {
print
"1..0 \# Skip: no Socket\n"
;
exit
;
}
unless
(
getservbyname
(
'echo'
,
'tcp'
)) {
print
"1..0 \# Skip: no echo port\n"
;
exit
;
}
unless
(
getservbyname
(
'http'
,
'tcp'
)) {
print
"1..0 \# Skip: no http port\n"
;
exit
;
}
}
my
$fail_ip
=
$ENV
{NET_PING_FAIL_IP} ||
"192.0.2.0"
;
my
%webs
;
my
@hosts
= (
$fail_ip
,
"www.google.com"
,
"www.duckduckgo.com"
,
"www.microsoft.com"
,
);
BEGIN {use_ok(
'Net::Ping'
)};
my
$can_alarm
=
eval
{
alarm
0; 1;};
sub
Alarm {
alarm
(
shift
)
if
$can_alarm
;
}
Alarm(50);
$SIG
{ALRM} =
sub
{
fail(
'Alarm timed out'
);
die
"TIMED OUT!"
;
};
my
$p
= Net::Ping->new(
"syn"
, 10);
isa_ok(
$p
,
'Net::Ping'
,
'new() worked'
);
cmp_ok((
$p
->{port_num} =
getservbyname
(
"http"
,
"tcp"
)),
'>'
, 0,
'valid port for http/tcp'
);
my
%contacted
;
foreach
my
$host
(
@hosts
) {
Alarm(50);
foreach
my
$port
(80, 443) {
$p
->port_number(
$port
);
is(
$p
->ping(
$host
), 1,
"Sent SYN to $host at port $port ["
. (
$p
->{bad}->{
$host
} ||
""
) .
"]"
);
$contacted
{
"$host:$port"
} = 1;
}
}
Alarm(20);
while
(
my
@r
=
$p
->ack()) {
my
%res
;
@res
{
qw(host ack_time ip port)
} =
@r
;
my
$answered
=
"$res{host}:$res{port}"
;
like(
$answered
,
qr/^[\w\.]+:\d+$/
,
"Supposed to be up: $res{host}:$res{port}"
);
delete
$contacted
{
$answered
};
}
Alarm(0);
is
keys
%contacted
, 2,
'2 servers did not acknowledge our ping'
or diag
sort
keys
%contacted
;
delete
$contacted
{
$_
}
foreach
(
"$fail_ip:80"
,
"$fail_ip:443"
,
'www.about.com:443'
);
is
keys
%contacted
, 0,
'The servers that did not acknowledge our ping were correct'
;