1# Testing service_check method using tcp and syn protocols.
2
3use Config;
4
5BEGIN {
6  unless (eval "require IO::Socket") {
7    print "1..0 \# Skip: no IO::Socket\n";
8    exit;
9  }
10  unless (getservbyname('echo', 'tcp')) {
11    print "1..0 \# Skip: no echo port\n";
12    exit;
13  }
14  unless ($Config{d_getpbyname}) {
15    print "1..0 \# Skip: no getprotobyname\n";
16    exit;
17  }
18}
19
20use strict;
21use Test::More tests => 26;
22BEGIN {use_ok('Net::Ping')};
23
24# I'm lazy so I'll just use IO::Socket
25# for the TCP Server stuff instead of doing
26# all that direct socket() junk manually.
27
28my $sock1 = new IO::Socket::INET
29  LocalAddr => "127.0.0.1",
30  Proto => "tcp",
31  Listen => 8,
32  or warn "bind: $!";
33
34isa_ok($sock1, 'IO::Socket::INET',
35       'Start a TCP listen server on ephemeral port');
36
37# Start listening on another ephemeral port
38my $sock2 = new IO::Socket::INET
39  LocalAddr => "127.0.0.1",
40  Proto => "tcp",
41  Listen => 8,
42  or warn "bind: $!";
43
44isa_ok($sock2, 'IO::Socket::INET',
45       'Start a second TCP listen server on ephemeral port');
46
47my $port1 = $sock1->sockport;
48cmp_ok($port1, '>', 0);
49
50my $port2 = $sock2->sockport;
51cmp_ok($port2, '>', 0);
52
53#
54isnt($port1, $port2, 'Make sure the servers are listening on different ports');
55
56$sock2->close;
57
58# This is how it should be:
59# 127.0.0.1:$port1 - service ON
60# 127.0.0.1:$port2 - service OFF
61
62#####
63# First, we test using the "tcp" protocol.
64# (2 seconds should be long enough to connect to loopback.)
65my $p = new Net::Ping "tcp", 2;
66
67isa_ok($p, 'Net::Ping', 'new() worked');
68
69# Disable service checking
70$p->service_check(0);
71
72# Try on the first port
73$p->{port_num} = $port1;
74
75is($p->ping("127.0.0.1"), 1, 'first port is reachable');
76
77# Try on the other port
78$p->{port_num} = $port2;
79
80{
81    local $TODO = "Believed not to work on $^O" if $^O =~ /^(?:hpux|os390|freebsd)$/;
82    is($p->ping("127.0.0.1"), 1, 'second port is reachable');
83}
84
85# Enable service checking
86$p->service_check(1);
87
88# Try on the first port
89$p->{port_num} = $port1;
90
91is($p->ping("127.0.0.1"), 1, 'first service is on');
92
93# Try on the other port
94$p->{port_num} = $port2;
95
96isnt($p->ping("127.0.0.1"), 2, 'second service is off');
97
98# test 11 just finished.
99
100#####
101# Lastly, we test using the "syn" protocol.
102$p = new Net::Ping "syn", 2;
103
104isa_ok($p, 'Net::Ping', 'new() worked');
105
106# Disable service checking
107$p->service_check(0);
108
109# Try on the first port
110$p->{port_num} = $port1;
111
112is($p->ping("127.0.0.1"), 1, "send SYN to first port") or diag ("ERRNO: $!");
113
114is($p->ack(), '127.0.0.1', 'IP should be reachable');
115is($p->ack(), undef, 'No more sockets');
116
117###
118# Get a fresh object
119$p = new Net::Ping "syn", 2;
120
121isa_ok($p, 'Net::Ping', 'new() worked');
122
123# Disable service checking
124$p->service_check(0);
125
126# Try on the other port
127$p->{port_num} = $port2;
128
129SKIP: {
130  skip "no localhost resolver on $^O", 2
131    unless Socket::getaddrinfo('localhost', &Socket::AF_INET);
132  is($p->ping("127.0.0.1"), 1, "send SYN to second port") or diag ("ERRNO: $!");
133
134  {
135    local $TODO = "Believed not to work on $^O"
136      if $^O =~ /^(?:hpux|MSWin32|os390|freebsd)$/;
137    is($p->ack(), '127.0.0.1', 'IP should be reachable');
138  }
139}
140is($p->ack(), undef, 'No more sockets');
141
142
143###
144# Get a fresh object
145$p = new Net::Ping "syn", 2;
146
147isa_ok($p, 'Net::Ping', 'new() worked');
148
149# Enable service checking
150$p->service_check(1);
151
152# Try on the first port
153$p->{port_num} = $port1;
154
155is($p->ping("127.0.0.1"), 1, "send SYN to first port") or diag ("ERRNO: $!");
156
157is($p->ack(), '127.0.0.1', 'IP should be reachable');
158is($p->ack(), undef, 'No more sockets');
159
160
161###
162# Get a fresh object
163$p = new Net::Ping "syn", 2;
164
165isa_ok($p, 'Net::Ping', 'new() worked');
166
167# Enable service checking
168$p->service_check(1);
169
170# Try on the other port
171$p->{port_num} = $port2;
172
173is($p->ping("127.0.0.1"), 1, "send SYN to second port") or diag ("ERRNO: $!");
174
175is($p->ack(), undef, 'No sockets should have service on');
176