1898184e3Ssthenuse strict; 2898184e3Ssthen 3b39c5158SmillertBEGIN { 4*f2a19305Safresh1 if ($ENV{NO_NETWORK_TESTING} || 5*f2a19305Safresh1 ($ENV{PERL_CORE}) && !$ENV{PERL_TEST_Net_Ping}) { 6b39c5158Smillert print "1..0 # Skip: network dependent test\n"; 7b39c5158Smillert exit; 8b39c5158Smillert } 9b39c5158Smillert unless (eval "require Socket") { 10b39c5158Smillert print "1..0 \# Skip: no Socket\n"; 11b39c5158Smillert exit; 12b39c5158Smillert } 13b39c5158Smillert unless (getservbyname('echo', 'tcp')) { 14b39c5158Smillert print "1..0 \# Skip: no echo port\n"; 15b39c5158Smillert exit; 16b39c5158Smillert } 17b39c5158Smillert} 18b39c5158Smillert 19f3efcd01Safresh1# Hopefully this is never a routeable host 20*f2a19305Safresh1my $fail_ip = $ENV{NET_PING_FAIL_IP} || "192.0.2.0"; 21f3efcd01Safresh1 22b39c5158Smillert# Remote network test using tcp protocol. 23b39c5158Smillert# 24b39c5158Smillert# NOTE: 25b39c5158Smillert# Network connectivity will be required for all tests to pass. 26b39c5158Smillert# Firewalls may also cause some tests to fail, so test it 27b39c5158Smillert# on a clear network. If you know you do not have a direct 28b39c5158Smillert# connection to remote networks, but you still want the tests 29b39c5158Smillert# to pass, use the following: 30b39c5158Smillert# 31b39c5158Smillert# $ PERL_CORE=1 make test 32b39c5158Smillert 33f3efcd01Safresh1use Test::More tests => 13; 34898184e3SsthenBEGIN {use_ok('Net::Ping');} 35b39c5158Smillert 36b39c5158Smillertmy $p = new Net::Ping "tcp",9; 37b39c5158Smillert 38898184e3Ssthenisa_ok($p, 'Net::Ping', 'new() worked'); 39b39c5158Smillert 40f3efcd01Safresh1# message_type can't be used 41f3efcd01Safresh1eval { 42f3efcd01Safresh1 $p->message_type(); 43f3efcd01Safresh1}; 44f3efcd01Safresh1like($@, qr/message type only supported on 'icmp' protocol/, "message_type() API only concern 'icmp' protocol"); 45f3efcd01Safresh1 46256a93a4Safresh1my $localhost = $p->ping("localhost"); 47256a93a4Safresh1if ($localhost) { 48898184e3Ssthen isnt($p->ping("localhost"), 0, 'Test on the default port'); 49256a93a4Safresh1} else { 50256a93a4Safresh1 ok(1, "SKIP localhost on the default port on $^O"); 51256a93a4Safresh1} 52b39c5158Smillert 53b39c5158Smillert# Change to use the more common web port. 54b39c5158Smillert# This will pull from /etc/services on UNIX. 55b39c5158Smillert# (Make sure getservbyname works in scalar context.) 56256a93a4Safresh1isnt($p->{port_num} = (getservbyname("http", "tcp") || 80), undef, "getservbyname http"); 57b39c5158Smillert 58256a93a4Safresh1if ($localhost) { 59898184e3Ssthen isnt($p->ping("localhost"), 0, 'Test localhost on the web port'); 60256a93a4Safresh1} else { 61256a93a4Safresh1 my $result = $p->ping("localhost"); 62256a93a4Safresh1 if ($result) { 63256a93a4Safresh1 isnt($p->ping("localhost"), 0, "localhost on the web port unexpectedly worked on $^O"); 64256a93a4Safresh1 } else { 65256a93a4Safresh1 ok(1, "SKIP localhost on the web port on $^O"); 66256a93a4Safresh1 } 67256a93a4Safresh1} 68b39c5158Smillert 69f3efcd01Safresh1is($p->ping($fail_ip), 0, "Can't reach $fail_ip"); 70b39c5158Smillert 71b39c5158Smillert# Test a few remote servers 72b39c5158Smillert# Hopefully they are up when the tests are run. 73b39c5158Smillert 745759b3d2Safresh1if ($p->ping('google.com')) { # check for firewall 755759b3d2Safresh1 foreach (qw(google.com www.google.com www.wisc.edu 76898184e3Ssthen yahoo.com www.yahoo.com www.about.com)) { 77898184e3Ssthen isnt($p->ping($_), 0, "Can ping $_"); 78898184e3Ssthen } 795759b3d2Safresh1} else { 805759b3d2Safresh1 SKIP: { 815759b3d2Safresh1 skip "Cannot ping google.com: no TCP connection or firewall", 6; 825759b3d2Safresh1 } 835759b3d2Safresh1} 84