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