1use warnings; 2use strict; 3use Config; 4 5BEGIN { 6 unless (my $port = getservbyname('echo', 'tcp')) { 7 print "1..0 \# Skip: no echo port\n"; 8 exit; 9 } 10 unless ($Config{d_getpbyname}) { 11 print "1..0 \# Skip: no getprotobyname\n"; 12 exit; 13 } 14} 15 16use Test::More qw(no_plan); 17BEGIN {use_ok('Net::Ping')}; 18 19# plain ol' constuctor call 20my $p = Net::Ping->new(); 21isa_ok($p, "Net::Ping"); 22 23# call new from an instantiated object 24my $p2 = $p->new(); 25isa_ok($p2, "Net::Ping"); 26 27# named args 28my $p3 = Net::Ping->new({proto => 'tcp', ttl => 5}); 29isa_ok($p3, "Net::Ping"); 30 31# check for invalid proto 32eval { 33 $p = Net::Ping->new("thwackkk"); 34}; 35like($@, qr/Protocol for ping must be "icmp", "icmpv6", "udp", "tcp", "syn", "stream" or "external"/, "new() errors for invalid protocol"); 36 37# check for invalid timeout 38eval { 39 $p = Net::Ping->new("tcp", -1); 40}; 41like($@, qr/Default timeout for ping must be greater than 0 seconds/, "new() errors for invalid timeout"); 42 43# check for invalid data sizes 44eval { 45 $p = Net::Ping->new("udp", 10, -1); 46}; 47like($@, qr/Data for ping must be from/, "new() errors for invalid data size"); 48 49eval { 50 $p = Net::Ping->new("udp", 10, 70000); 51}; 52like($@, qr/Data for ping must be from/, "new() errors for invalid data size"); 53 54# force failures for udp 55 56 57# force failures for tcp 58SKIP: { 59 # diag "Checking icmp"; 60 eval { $p = Net::Ping->new('icmp'); }; 61 skip "icmp ping requires root privileges.", 3 62 if !Net::Ping::_isroot() or $^O eq 'MSWin32'; 63 if($> and $^O ne 'VMS' and $^O ne 'cygwin') { 64 like($@, qr/icmp ping requires root privilege/, "Need root for icmp"); 65 skip "icmp tests require root", 2; 66 } else { 67 isa_ok($p, "Net::Ping"); 68 } 69 70 # set IP TOS to "Minimum Delay" 71 $p = Net::Ping->new("icmp", undef, undef, undef, 8); 72 isa_ok($p, "Net::Ping"); 73 74 # This really shouldn't work. Not sure who to blame. 75 $p = Net::Ping->new("icmp", undef, undef, undef, "does this fail"); 76 isa_ok($p, "Net::Ping"); 77} 78 79