summaryrefslogtreecommitdiffstats
path: root/gnu/usr.bin/perl/dist/Net-Ping/t/300_ping_stream.t
blob: a8285b3dec5c27bb4dd949c4285add7a10609f31 (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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
use strict; 
BEGIN {
  if ($ENV{PERL_CORE}) {
    unless ($ENV{PERL_TEST_Net_Ping}) {
      print "1..0 # Skip: network dependent test\n";
        exit;
    }
  }
  if ($^O eq 'freebsd') {
    print "1..0 \# Skip: unreliable localhost resolver on $^O\n";
    exit;
  }
  unless (eval "require Socket") {
    print "1..0 \# Skip: no Socket\n";
    exit;
  }
  if (my $port = getservbyname('echo', 'tcp')) {
    socket(*ECHO, &Socket::PF_INET(), &Socket::SOCK_STREAM(),
                  (getprotobyname 'tcp')[2]);
    unless (connect(*ECHO,
                    scalar
                      &Socket::sockaddr_in($port,
                                           &Socket::inet_aton("localhost"))))
    {
      print "1..0 \# Skip: loopback tcp echo service is off ($!)\n";
      exit;
    }
    close (*ECHO);
  } else {
    print "1..0 \# Skip: no echo port\n";
    exit;
  }
  unless (Socket::getaddrinfo('localhost', &Socket::AF_INET)) {
    print "1..0 \# Skip: no localhost resolver on $^O\n";
    exit;
  }
}

# Test of stream protocol using loopback interface.
#
# NOTE:
#   The echo service must be enabled on localhost
#   to really test the stream protocol ping.  See
#   the end of this document on how to enable it.

use Test::More tests => 23;
use Net::Ping;

my $p = new Net::Ping "stream";

# new() worked?
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");

is($p->ping("localhost"), 1, 'Attempt to connect to the echo port');

for (1..20) {
  select (undef,undef,undef,0.1);
  is($p->ping("localhost"), 1, 'Try several pings while it is connected');
}

__END__

A simple xinetd configuration to enable the echo service can easily be made.
Just create the following file before restarting xinetd:

/etc/xinetd.d/echo:

# description: An echo server.
service echo
{
        type            = INTERNAL
        id              = echo-stream
        socket_type     = stream
        protocol        = tcp
        user            = root
        wait            = no
        disable         = no
}


Or if you are using inetd, before restarting, add
this line to your /etc/inetd.conf:

echo   stream  tcp     nowait  root    internal