1b39c5158Smillert# Testing service_check method using tcp and syn protocols. 2b39c5158Smillert 36fb12b70Safresh1use Config; 46fb12b70Safresh1 5b39c5158SmillertBEGIN { 6b39c5158Smillert unless (eval "require IO::Socket") { 7b39c5158Smillert print "1..0 \# Skip: no IO::Socket\n"; 8b39c5158Smillert exit; 9b39c5158Smillert } 10b39c5158Smillert unless (getservbyname('echo', 'tcp')) { 11b39c5158Smillert print "1..0 \# Skip: no echo port\n"; 12b39c5158Smillert exit; 13b39c5158Smillert } 146fb12b70Safresh1 unless ($Config{d_getpbyname}) { 156fb12b70Safresh1 print "1..0 \# Skip: no getprotobyname\n"; 166fb12b70Safresh1 exit; 176fb12b70Safresh1 } 18b39c5158Smillert} 19b39c5158Smillert 20b39c5158Smillertuse strict; 21898184e3Ssthenuse Test::More tests => 26; 22898184e3SsthenBEGIN {use_ok('Net::Ping')}; 23b39c5158Smillert 24b39c5158Smillert# I'm lazy so I'll just use IO::Socket 25b39c5158Smillert# for the TCP Server stuff instead of doing 26b39c5158Smillert# all that direct socket() junk manually. 27b39c5158Smillert 28b39c5158Smillertmy $sock1 = new IO::Socket::INET 29b39c5158Smillert LocalAddr => "127.0.0.1", 30b39c5158Smillert Proto => "tcp", 31b39c5158Smillert Listen => 8, 32b39c5158Smillert or warn "bind: $!"; 33b39c5158Smillert 34898184e3Ssthenisa_ok($sock1, 'IO::Socket::INET', 35898184e3Ssthen 'Start a TCP listen server on ephemeral port'); 36b39c5158Smillert 37b39c5158Smillert# Start listening on another ephemeral port 38b39c5158Smillertmy $sock2 = new IO::Socket::INET 39b39c5158Smillert LocalAddr => "127.0.0.1", 40b39c5158Smillert Proto => "tcp", 41b39c5158Smillert Listen => 8, 42b39c5158Smillert or warn "bind: $!"; 43b39c5158Smillert 44898184e3Ssthenisa_ok($sock2, 'IO::Socket::INET', 45898184e3Ssthen 'Start a second TCP listen server on ephemeral port'); 46b39c5158Smillert 47b39c5158Smillertmy $port1 = $sock1->sockport; 48898184e3Ssthencmp_ok($port1, '>', 0); 49b39c5158Smillert 50b39c5158Smillertmy $port2 = $sock2->sockport; 51898184e3Ssthencmp_ok($port2, '>', 0); 52b39c5158Smillert 53898184e3Ssthen# 54898184e3Ssthenisnt($port1, $port2, 'Make sure the servers are listening on different ports'); 55b39c5158Smillert 56b39c5158Smillert$sock2->close; 57b39c5158Smillert 58b39c5158Smillert# This is how it should be: 59b39c5158Smillert# 127.0.0.1:$port1 - service ON 60b39c5158Smillert# 127.0.0.1:$port2 - service OFF 61b39c5158Smillert 62b39c5158Smillert##### 63b39c5158Smillert# First, we test using the "tcp" protocol. 64b39c5158Smillert# (2 seconds should be long enough to connect to loopback.) 65b39c5158Smillertmy $p = new Net::Ping "tcp", 2; 66b39c5158Smillert 67898184e3Ssthenisa_ok($p, 'Net::Ping', 'new() worked'); 68b39c5158Smillert 69b39c5158Smillert# Disable service checking 70b39c5158Smillert$p->service_check(0); 71b39c5158Smillert 72b39c5158Smillert# Try on the first port 73b39c5158Smillert$p->{port_num} = $port1; 74b39c5158Smillert 75898184e3Ssthenis($p->ping("127.0.0.1"), 1, 'first port is reachable'); 76b39c5158Smillert 77b39c5158Smillert# Try on the other port 78b39c5158Smillert$p->{port_num} = $port2; 79b39c5158Smillert 80898184e3Ssthen{ 81*e0680481Safresh1 local $TODO = "Believed not to work on $^O" if $^O =~ /^(?:MSWin32|os390)$/; 82898184e3Ssthen is($p->ping("127.0.0.1"), 1, 'second port is reachable'); 83898184e3Ssthen} 84b39c5158Smillert 85b39c5158Smillert# Enable service checking 86b39c5158Smillert$p->service_check(1); 87b39c5158Smillert 88b39c5158Smillert# Try on the first port 89b39c5158Smillert$p->{port_num} = $port1; 90b39c5158Smillert 91898184e3Ssthenis($p->ping("127.0.0.1"), 1, 'first service is on'); 92b39c5158Smillert 93b39c5158Smillert# Try on the other port 94b39c5158Smillert$p->{port_num} = $port2; 95b39c5158Smillert 96898184e3Ssthenisnt($p->ping("127.0.0.1"), 2, 'second service is off'); 97b39c5158Smillert 98b39c5158Smillert# test 11 just finished. 99b39c5158Smillert 100b39c5158Smillert##### 101b39c5158Smillert# Lastly, we test using the "syn" protocol. 102b39c5158Smillert$p = new Net::Ping "syn", 2; 103b39c5158Smillert 104898184e3Ssthenisa_ok($p, 'Net::Ping', 'new() worked'); 105b39c5158Smillert 106b39c5158Smillert# Disable service checking 107b39c5158Smillert$p->service_check(0); 108b39c5158Smillert 109b39c5158Smillert# Try on the first port 110b39c5158Smillert$p->{port_num} = $port1; 111b39c5158Smillert 112898184e3Ssthenis($p->ping("127.0.0.1"), 1, "send SYN to first port") or diag ("ERRNO: $!"); 113b39c5158Smillert 114898184e3Ssthenis($p->ack(), '127.0.0.1', 'IP should be reachable'); 115898184e3Ssthenis($p->ack(), undef, 'No more sockets'); 116b39c5158Smillert 117b39c5158Smillert### 118b39c5158Smillert# Get a fresh object 119b39c5158Smillert$p = new Net::Ping "syn", 2; 120b39c5158Smillert 121898184e3Ssthenisa_ok($p, 'Net::Ping', 'new() worked'); 122b39c5158Smillert 123b39c5158Smillert# Disable service checking 124b39c5158Smillert$p->service_check(0); 125b39c5158Smillert 126b39c5158Smillert# Try on the other port 127b39c5158Smillert$p->{port_num} = $port2; 128b39c5158Smillert 12956d68f1eSafresh1SKIP: { 13056d68f1eSafresh1 skip "no localhost resolver on $^O", 2 13156d68f1eSafresh1 unless Socket::getaddrinfo('localhost', &Socket::AF_INET); 132898184e3Ssthen is($p->ping("127.0.0.1"), 1, "send SYN to second port") or diag ("ERRNO: $!"); 133b39c5158Smillert 134898184e3Ssthen { 13556d68f1eSafresh1 local $TODO = "Believed not to work on $^O" 136*e0680481Safresh1 if $^O =~ /^(?:MSWin32|os390)$/; 137898184e3Ssthen is($p->ack(), '127.0.0.1', 'IP should be reachable'); 138898184e3Ssthen } 13956d68f1eSafresh1} 140898184e3Ssthenis($p->ack(), undef, 'No more sockets'); 141b39c5158Smillert 142b39c5158Smillert 143b39c5158Smillert### 144b39c5158Smillert# Get a fresh object 145b39c5158Smillert$p = new Net::Ping "syn", 2; 146b39c5158Smillert 147898184e3Ssthenisa_ok($p, 'Net::Ping', 'new() worked'); 148b39c5158Smillert 149b39c5158Smillert# Enable service checking 150b39c5158Smillert$p->service_check(1); 151b39c5158Smillert 152b39c5158Smillert# Try on the first port 153b39c5158Smillert$p->{port_num} = $port1; 154b39c5158Smillert 155898184e3Ssthenis($p->ping("127.0.0.1"), 1, "send SYN to first port") or diag ("ERRNO: $!"); 156b39c5158Smillert 157898184e3Ssthenis($p->ack(), '127.0.0.1', 'IP should be reachable'); 158898184e3Ssthenis($p->ack(), undef, 'No more sockets'); 159b39c5158Smillert 160b39c5158Smillert 161b39c5158Smillert### 162b39c5158Smillert# Get a fresh object 163b39c5158Smillert$p = new Net::Ping "syn", 2; 164b39c5158Smillert 165898184e3Ssthenisa_ok($p, 'Net::Ping', 'new() worked'); 166b39c5158Smillert 167b39c5158Smillert# Enable service checking 168b39c5158Smillert$p->service_check(1); 169b39c5158Smillert 170b39c5158Smillert# Try on the other port 171b39c5158Smillert$p->{port_num} = $port2; 172b39c5158Smillert 173898184e3Ssthenis($p->ping("127.0.0.1"), 1, "send SYN to second port") or diag ("ERRNO: $!"); 174b39c5158Smillert 175898184e3Ssthenis($p->ack(), undef, 'No sockets should have service on'); 176