82dee7de92a0bd35 failed to add ext/lib/Makefile.PL. Oops.
[p5sagit/p5-mst-13.2.git] / ext / Net-Ping / t / 450_service.t
1 # Testing service_check method using tcp and syn protocols.
2
3 BEGIN {
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
14 use strict;
15 use Test;
16 use 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
22 plan tests => 26, ($^O eq 'MSWin32' ? (todo => [18]) :
23                    $^O eq "hpux"    ? (todo => [9, 18]) : ());
24
25 # Everything loaded fine
26 ok 1;
27
28 # Start a tcp listen server on ephemeral port
29 my $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.
36 ok !!$sock1;
37
38 # Start listening on another ephemeral port
39 my $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.
46 ok !!$sock2;
47
48 my $port1 = $sock1->sockport;
49 ok $port1;
50
51 my $port2 = $sock2->sockport;
52 ok $port2;
53
54 # Make sure the sockets are listening on different ports.
55 ok ($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.)
66 my $p = new Net::Ping "tcp", 2;
67
68 # new() worked?
69 ok !!$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
78 ok $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
84 ok $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
95 ok $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
101 ok !$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?
110 ok !!$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
119 if (!ok $p -> ping("127.0.0.1")) {warn "ERRNO: $!";}
120
121 # IP should be reachable
122 ok $p -> ack();
123 # No more sockets?
124 ok !$p -> ack();
125
126 ###
127 # Get a fresh object
128 $p = new Net::Ping "syn", 2;
129
130 # new() worked?
131 ok !!$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
140 if (!ok $p -> ping("127.0.0.1")) {warn "ERRNO: $!";}
141
142 # IP should still be reachable
143 ok $p -> ack();
144 # No more sockets?
145 ok !$p -> ack();
146
147
148 ###
149 # Get a fresh object
150 $p = new Net::Ping "syn", 2;
151
152 # new() worked?
153 ok !!$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
162 ok $p -> ping("127.0.0.1");
163
164 # Should have service on
165 ok ($p -> ack(),"127.0.0.1");
166 # No more good sockets?
167 ok !$p -> ack();
168
169
170 ###
171 # Get a fresh object
172 $p = new Net::Ping "syn", 2;
173
174 # new() worked?
175 ok !!$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
184 if (!ok $p -> ping("127.0.0.1")) {warn "ERRNO: $!";}
185
186 # No sockets should have service on
187 ok !$p -> ack();