Commit | Line | Data |
f569508e |
1 | # Testing tcp_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 | } |
dd5ea221 |
12 | unless (0) { |
13 | print "1..0 \# Skip: too many problems right now\n"; |
14 | exit; |
15 | } |
f569508e |
16 | } |
17 | |
18 | use strict; |
19 | use Test; |
20 | use Net::Ping; |
21 | use IO::Socket; |
22 | |
23 | # I'm lazy so I'll just use IO::Socket |
24 | # for the TCP Server stuff instead of doing |
25 | # all that direct socket() junk manually. |
26 | |
27 | plan tests => 37; |
28 | |
29 | # Everything loaded fine |
30 | ok 1; |
31 | |
32 | "0" =~ /(0)/; # IO::Socket::INET ephemeral buttwag hack |
33 | |
34 | # Start a tcp listen server on ephemeral port |
35 | my $sock1 = new IO::Socket::INET |
36 | LocalAddr => "127.1.1.1", |
37 | Proto => "tcp", |
38 | Listen => 8, |
39 | Reuse => 1, |
40 | Type => SOCK_STREAM, |
41 | ; |
42 | |
43 | # Make sure it worked. |
44 | ok !!$sock1; |
45 | |
46 | "0" =~ /(0)/; # IO::Socket::INET ephemeral buttwag hack |
47 | |
48 | # Start listening on another ephemeral port |
49 | my $sock2 = new IO::Socket::INET |
50 | LocalAddr => "127.2.2.2", |
51 | Proto => "tcp", |
52 | Listen => 8, |
53 | Reuse => 1, |
54 | Type => SOCK_STREAM, |
55 | ; |
56 | |
57 | # Make sure it worked too. |
58 | ok !!$sock2; |
59 | |
60 | my $port1 = $sock1->sockport; |
61 | ok $port1; |
62 | |
63 | my $port2 = $sock2->sockport; |
64 | ok $port2; |
65 | |
66 | # Make sure the sockets are listening on different ports. |
67 | ok ($port1 != $port2); |
68 | |
69 | # This is how it should be: |
70 | # 127.1.1.1:$port1 - service ON |
71 | # 127.2.2.2:$port2 - service ON |
72 | # 127.1.1.1:$port2 - service OFF |
73 | # 127.2.2.2:$port1 - service OFF |
74 | |
75 | ##### |
76 | # First, we test using the "tcp" protocol. |
77 | # (2 seconds should be long enough to connect to loopback.) |
78 | my $p = new Net::Ping "tcp", 2; |
79 | |
80 | # new() worked? |
81 | ok !!$p; |
82 | |
83 | # Disable service checking |
84 | $p->tcp_service_check(0); |
85 | |
86 | # Try on the first port |
87 | $p->{port_num} = $port1; |
88 | |
89 | # Make sure IP1 is reachable |
90 | ok $p -> ping("127.1.1.1"); |
91 | |
92 | # Make sure IP2 is reachable |
93 | ok $p -> ping("127.2.2.2"); |
94 | |
95 | # Try on the other port |
96 | $p->{port_num} = $port2; |
97 | |
98 | # Make sure IP1 is reachable |
99 | ok $p -> ping("127.1.1.1"); |
100 | |
101 | # Make sure IP2 is reachable |
102 | ok $p -> ping("127.2.2.2"); |
103 | |
104 | |
105 | # Enable service checking |
106 | $p->tcp_service_check(1); |
107 | |
108 | # Try on the first port |
109 | $p->{port_num} = $port1; |
110 | |
111 | # Make sure service on IP1 |
112 | ok $p -> ping("127.1.1.1"); |
113 | |
114 | # Make sure not service on IP2 |
115 | ok !$p -> ping("127.2.2.2"); |
116 | |
117 | # Try on the other port |
118 | $p->{port_num} = $port2; |
119 | |
120 | # Make sure not service on IP1 |
121 | ok !$p -> ping("127.1.1.1"); |
122 | |
123 | # Make sure service on IP2 |
124 | ok $p -> ping("127.2.2.2"); |
125 | |
126 | |
127 | ##### |
128 | # Lastly, we test using the "syn" protocol. |
129 | $p = new Net::Ping "syn", 2; |
130 | |
131 | # new() worked? |
132 | ok !!$p; |
133 | |
134 | # Disable service checking |
135 | $p->tcp_service_check(0); |
136 | |
137 | # Try on the first port |
138 | $p->{port_num} = $port1; |
139 | |
140 | # Send SYN to both IPs |
141 | ok $p -> ping("127.1.1.1"); |
142 | ok $p -> ping("127.2.2.2"); |
143 | |
144 | # Both IPs should be reachable |
145 | ok $p -> ack(); |
146 | ok $p -> ack(); |
147 | # No more sockets? |
148 | ok !$p -> ack(); |
149 | |
150 | ### |
151 | # Get a fresh object |
152 | $p = new Net::Ping "syn", 2; |
153 | |
154 | # new() worked? |
155 | ok !!$p; |
156 | |
157 | # Disable service checking |
158 | $p->tcp_service_check(0); |
159 | |
160 | # Try on the other port |
161 | $p->{port_num} = $port2; |
162 | |
163 | # Send SYN to both IPs |
164 | ok $p -> ping("127.1.1.1"); |
165 | ok $p -> ping("127.2.2.2"); |
166 | |
167 | # Both IPs should be reachable |
168 | ok $p -> ack(); |
169 | ok $p -> ack(); |
170 | # No more sockets? |
171 | ok !$p -> ack(); |
172 | |
173 | |
174 | ### |
175 | # Get a fresh object |
176 | $p = new Net::Ping "syn", 2; |
177 | |
178 | # new() worked? |
179 | ok !!$p; |
180 | |
181 | # Enable service checking |
182 | $p->tcp_service_check(1); |
183 | |
184 | # Try on the first port |
185 | $p->{port_num} = $port1; |
186 | |
187 | # Send SYN to both IPs |
188 | ok $p -> ping("127.1.1.1"); |
189 | ok $p -> ping("127.2.2.2"); |
190 | |
191 | # Only IP1 should have service |
192 | ok "127.1.1.1",$p -> ack(); |
193 | # No more good sockets? |
194 | ok !$p -> ack(); |
195 | |
196 | |
197 | ### |
198 | # Get a fresh object |
199 | $p = new Net::Ping "syn", 2; |
200 | |
201 | # new() worked? |
202 | ok !!$p; |
203 | |
204 | # Enable service checking |
205 | $p->tcp_service_check(1); |
206 | |
207 | # Try on the other port |
208 | $p->{port_num} = $port2; |
209 | |
210 | # Send SYN to both IPs |
211 | ok $p -> ping("127.1.1.1"); |
212 | ok $p -> ping("127.2.2.2"); |
213 | |
214 | # Only IP2 should have service |
215 | ok "127.2.2.2",$p -> ack(); |
216 | # No more good sockets? |
217 | ok !$p -> ack(); |