xref: /openbsd-src/gnu/usr.bin/perl/dist/Net-Ping/t/300_ping_stream.t (revision f2a19305cfc49ea4d1a5feb55cd6c283c6f1e031)
1use strict;
2BEGIN {
3  if ($ENV{NO_NETWORK_TESTING} ||
4      ($ENV{PERL_CORE}) && !$ENV{PERL_TEST_Net_Ping}) {
5    print "1..0 \# Skip: network dependent test\n";
6    exit;
7  }
8  if ($^O eq 'freebsd') {
9    print "1..0 \# Skip: unreliable localhost resolver on $^O\n";
10    exit;
11  }
12  unless (eval "require Socket") {
13    print "1..0 \# Skip: no Socket\n";
14    exit;
15  }
16  if (my $port = getservbyname('echo', 'tcp')) {
17    socket(*ECHO, &Socket::PF_INET(), &Socket::SOCK_STREAM(),
18                  (getprotobyname 'tcp')[2]);
19    unless (connect(*ECHO,
20                    scalar
21                      &Socket::sockaddr_in($port,
22                                           &Socket::inet_aton("localhost"))))
23    {
24      print "1..0 \# Skip: loopback tcp echo service is off ($!)\n";
25      exit;
26    }
27    close (*ECHO);
28  } else {
29    print "1..0 \# Skip: no echo port\n";
30    exit;
31  }
32  unless (Socket::getaddrinfo('localhost', &Socket::AF_INET)) {
33    print "1..0 \# Skip: no localhost resolver on $^O\n";
34    exit;
35  }
36}
37
38# Test of stream protocol using loopback interface.
39#
40# NOTE:
41#   The echo service must be enabled on localhost
42#   to really test the stream protocol ping.  See
43#   the end of this document on how to enable it.
44
45use Test::More tests => 23;
46use Net::Ping;
47
48my $p = new Net::Ping "stream";
49
50# new() worked?
51isa_ok($p, 'Net::Ping', 'new() worked');
52
53# message_type can't be used
54eval {
55  $p->message_type();
56};
57like($@, qr/message type only supported on 'icmp' protocol/, "message_type() API only concern 'icmp' protocol");
58
59is($p->ping("localhost"), 1, 'Attempt to connect to the echo port');
60
61for (1..20) {
62  select (undef,undef,undef,0.1);
63  is($p->ping("localhost"), 1, 'Try several pings while it is connected');
64}
65
66__END__
67
68A simple xinetd configuration to enable the echo service can easily be made.
69Just create the following file before restarting xinetd:
70
71/etc/xinetd.d/echo:
72
73# description: An echo server.
74service echo
75{
76        type            = INTERNAL
77        id              = echo-stream
78        socket_type     = stream
79        protocol        = tcp
80        user            = root
81        wait            = no
82        disable         = no
83}
84
85
86Or if you are using inetd, before restarting, add
87this line to your /etc/inetd.conf:
88
89echo   stream  tcp     nowait  root    internal
90