Skip to content

Commit

Permalink
add generic ping test
Browse files Browse the repository at this point in the history
See GH #17
  • Loading branch information
rurban committed Feb 22, 2021
1 parent f2a896c commit a281abe
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 5 deletions.
12 changes: 12 additions & 0 deletions Changes
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
CHANGES
-------
2.75 2021-02-22 11:08:05 rurban
Bugfixes
- Fixed wrong link to p5p message (jamieletual PR #25)
Test fixes:
- Disable all network tests with NO_NETWORK_TESTING=1 (Petr Písař PR #24)
- Use non-routable addresses for negative tests (Petr Písař PR #24)
- Remove some TEST_REQUIRES modules (Chris Williams PR #23)
- Fixed TEST_REQUIRES for older EUMM
- Added a test that ping on an unresolvable hostname does not croak,
just warns. (See GH #17)
- Added tests that named args override defaults (See GH #26)

2.74 2020-09-09 09:21:39 rurban
Features
- Add ICMPv6_NI_REPLY support.
Expand Down
1 change: 1 addition & 0 deletions MANIFEST
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ t/140_stream_inst.t
t/150_syn_inst.t
t/190_alarm.t
t/200_ping_tcp.t
t/201_ping.t
t/250_ping_hires.t
t/300_ping_stream.t
t/400_ping_syn.t
Expand Down
13 changes: 8 additions & 5 deletions t/001_new.t
Original file line number Diff line number Diff line change
Expand Up @@ -27,24 +27,28 @@ isa_ok($p3, "Net::Ping");
eval {
$p = Net::Ping->new("thwackkk");
};
like($@, qr/Protocol for ping must be "icmp", "icmpv6", "udp", "tcp", "syn", "stream" or "external"/, "new() errors for invalid protocol");
like($@, qr/Protocol for ping must be "icmp", "icmpv6", "udp", "tcp", "syn", "stream" or "external"/,
"new() errors for invalid protocol");

# check for invalid timeout
eval {
$p = Net::Ping->new("tcp", -1);
};
like($@, qr/Default timeout for ping must be greater than 0 seconds/, "new() errors for invalid timeout");
like($@, qr/Default timeout for ping must be greater than 0 seconds/,
"new() errors for invalid timeout");

# check for invalid data sizes
eval {
$p = Net::Ping->new("udp", 10, -1);
};
like($@, qr/Data for ping must be from/, "new() errors for invalid data size");
like($@, qr/Data for ping must be from/,
"new() errors for invalid data size");

eval {
$p = Net::Ping->new("udp", 10, 70000);
};
like($@, qr/Data for ping must be from/, "new() errors for invalid data size");
like($@, qr/Data for ping must be from/,
"new() errors for invalid data size");

# force failures for udp

Expand All @@ -70,4 +74,3 @@ SKIP: {
$p = Net::Ping->new("icmp", undef, undef, undef, "does this fail");
isa_ok($p, "Net::Ping");
}

43 changes: 43 additions & 0 deletions t/201_ping.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
use strict;

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;
}
}

# Hopefully this is never a routeable host
my $fail_ip = $ENV{NET_PING_FAIL_IP} || "192.0.2.0";
my $fail_host = 'invalid-hostname.xxxx';

# Remote network test using defaults.
#
# NOTE:
# Network connectivity will be required for all tests to pass.
# Firewalls may also cause some tests to fail, so test it
# on a clear network.

use Test::More tests => 4;
BEGIN {use_ok('Net::Ping');}

my $p = Net::Ping->new();
isa_ok($p, 'Net::Ping', 'new() worked');

# failing host lookup does not croak
if ($p->ping($fail_host)) {
ok (1, "SKIP $fail_host unexpectedly resolved");
} else {
ok (1, "$fail_host returns undef");
}

is($p->ping($fail_ip), 0, "Can't reach $fail_ip");

0 comments on commit a281abe

Please sign in to comment.