This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Skip the Net/Ping/450_service failures on HP-UX for the time being
[perl5.git] / lib / Net / Ping / t / 450_service.t
CommitLineData
9539e94f 1# Testing service_check method using tcp and syn protocols.
f569508e
HS
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;
f569508e
HS
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
ff4091f1
MB
22plan tests => 26, ($^O eq 'MSWin32' ? (todo => [18]) :
23 $^O eq "hpux" ? (todo => [9, 18]) : ());
f569508e
HS
24
25# Everything loaded fine
26ok 1;
27
f569508e
HS
28# Start a tcp listen server on ephemeral port
29my $sock1 = new IO::Socket::INET
69c74a9c 30 LocalAddr => "127.0.0.1",
f569508e
HS
31 Proto => "tcp",
32 Listen => 8,
69c74a9c 33 or warn "bind: $!";
f569508e
HS
34
35# Make sure it worked.
36ok !!$sock1;
37
f569508e
HS
38# Start listening on another ephemeral port
39my $sock2 = new IO::Socket::INET
69c74a9c 40 LocalAddr => "127.0.0.1",
f569508e
HS
41 Proto => "tcp",
42 Listen => 8,
69c74a9c 43 or warn "bind: $!";
f569508e
HS
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
69c74a9c
HS
57$sock2->close;
58
f569508e 59# This is how it should be:
69c74a9c
HS
60# 127.0.0.1:$port1 - service ON
61# 127.0.0.1:$port2 - service OFF
f569508e
HS
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
9539e94f 72$p->service_check(0);
f569508e
HS
73
74# Try on the first port
75$p->{port_num} = $port1;
76
69c74a9c
HS
77# Make sure it is reachable
78ok $p -> ping("127.0.0.1");
f569508e
HS
79
80# Try on the other port
81$p->{port_num} = $port2;
82
69c74a9c
HS
83# Make sure it is reachable
84ok $p -> ping("127.0.0.1");
f569508e 85
f569508e
HS
86
87
88# Enable service checking
9539e94f 89$p->service_check(1);
f569508e
HS
90
91# Try on the first port
92$p->{port_num} = $port1;
93
69c74a9c
HS
94# Make sure service is on
95ok $p -> ping("127.0.0.1");
f569508e
HS
96
97# Try on the other port
98$p->{port_num} = $port2;
99
69c74a9c
HS
100# Make sure service is off
101ok !$p -> ping("127.0.0.1");
f569508e 102
69c74a9c 103# test 11 just finished.
f569508e
HS
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
9539e94f 113$p->service_check(0);
f569508e
HS
114
115# Try on the first port
116$p->{port_num} = $port1;
117
69c74a9c
HS
118# Send SYN
119if (!ok $p -> ping("127.0.0.1")) {warn "ERRNO: $!";}
f569508e 120
69c74a9c 121# IP should be reachable
f569508e
HS
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
9539e94f 134$p->service_check(0);
f569508e
HS
135
136# Try on the other port
137$p->{port_num} = $port2;
138
69c74a9c
HS
139# Send SYN
140if (!ok $p -> ping("127.0.0.1")) {warn "ERRNO: $!";}
f569508e 141
69c74a9c 142# IP should still be reachable
f569508e
HS
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
9539e94f 156$p->service_check(1);
f569508e
HS
157
158# Try on the first port
159$p->{port_num} = $port1;
160
69c74a9c
HS
161# Send SYN
162ok $p -> ping("127.0.0.1");
f569508e 163
69c74a9c
HS
164# Should have service on
165ok ($p -> ack(),"127.0.0.1");
f569508e
HS
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
9539e94f 178$p->service_check(1);
f569508e
HS
179
180# Try on the other port
181$p->{port_num} = $port2;
182
69c74a9c
HS
183# Send SYN
184if (!ok $p -> ping("127.0.0.1")) {warn "ERRNO: $!";}
f569508e 185
69c74a9c 186# No sockets should have service on
f569508e 187ok !$p -> ack();