summaryrefslogtreecommitdiffstats
path: root/gnu/usr.bin/perl/dist/Net-Ping/t/200_ping_tcp.t
blob: 47168b014ad9a0026f7c029363c03e08ca60fe78 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
use strict;

BEGIN {
  if ($ENV{PERL_CORE}) {
    unless ($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} || "172.29.249.249";

# Remote network test using tcp protocol.
#
# 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.  If you know you do not have a direct
#   connection to remote networks, but you still want the tests
#   to pass, use the following:
#
# $ PERL_CORE=1 make test

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

my $p = new Net::Ping "tcp",9;

isa_ok($p, 'Net::Ping', 'new() worked');

# message_type can't be used
eval {
  $p->message_type();
};
like($@, qr/message type only supported on 'icmp' protocol/, "message_type() API only concern 'icmp' protocol");

isnt($p->ping("localhost"), 0, 'Test on the default port');

# Change to use the more common web port.
# This will pull from /etc/services on UNIX.
# (Make sure getservbyname works in scalar context.)
isnt($p->{port_num} = (getservbyname("http", "tcp") || 80), undef);

isnt($p->ping("localhost"), 0, 'Test localhost on the web port');

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

# Test a few remote servers
# Hopefully they are up when the tests are run.

if ($p->ping('google.com')) { # check for firewall
  foreach (qw(google.com www.google.com www.wisc.edu
              yahoo.com www.yahoo.com www.about.com)) {
    isnt($p->ping($_), 0, "Can ping $_");
  }
} else {
 SKIP: {
    skip "Cannot ping google.com: no TCP connection or firewall", 6;
  }
}