File Coverage

blib/lib/Test/Net/Connect.pm
Criterion Covered Total %
statement 23 110 20.9
branch 0 46 0.0
condition 0 11 0.0
subroutine 7 12 58.3
pod 2 2 100.0
total 32 181 17.6


line stmt bran cond sub pod time code
1             package Test::Net::Connect;
2              
3             # Copyright (c) 2005 Nik Clayton
4             # All rights reserved.
5             #
6             # Redistribution and use in source and binary forms, with or without
7             # modification, are permitted provided that the following conditions
8             # are met:
9             # 1. Redistributions of source code must retain the above copyright
10             # notice, this list of conditions and the following disclaimer.
11             # 2. Redistributions in binary form must reproduce the above copyright
12             # notice, this list of conditions and the following disclaimer in the
13             # documentation and/or other materials provided with the distribution.
14             #
15             # THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16             # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17             # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18             # ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19             # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20             # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21             # OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22             # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23             # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24             # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25             # SUCH DAMAGE.
26              
27 1     1   30206 use warnings;
  1         2  
  1         27  
28 1     1   4 use strict;
  1         2  
  1         29  
29              
30 1     1   5 use Test::Builder;
  1         6  
  1         62  
31              
32             require Exporter;
33             our @ISA = qw(Exporter);
34             our @EXPORT = qw(connect_ok connect_not_ok);
35              
36             my $Test = Test::Builder->new;
37              
38 1     1   892 use Net::hostent;
  1         5297  
  1         5  
39 1     1   1168 use Socket;
  1         4652  
  1         617  
40 1     1   798 use IO::Socket::INET;
  1         23037  
  1         80  
41              
42             my @FIELDS = qw(host port proto);
43             my %FIELDS = map { $_ => 1 } @FIELDS;
44              
45             sub import {
46 1     1   11 my($self) = shift;
47 1         5 my $pack = caller;
48              
49 1         6 $Test->exported_to($pack);
50 1         13 $Test->plan(@_);
51              
52 1         267 $self->export_to_level(1, $self, qw(connect_ok connect_not_ok));
53             }
54              
55             =head1 NAME
56              
57             Test::Net::Connect - Test::Builder based tests for network connectivity
58              
59             =head1 VERSION
60              
61             Version 0.03
62              
63             =cut
64              
65             our $VERSION = '0.03';
66              
67             =head1 SYNOPSIS
68              
69             use Test::Net::Connect tests => 3;
70              
71             connect_ok({ host => 'smtp.example.com', port => 25,
72             proto => 'tcp' }, 'Check tcp://smtp.example.com:25');
73              
74             # proto defaults to 'tcp', the test name can be omitted, and the
75             # port can be appended to the host name
76             connect_ok({ host => 'smtp.example.com:25' });
77              
78             connect_not_ok({ host => 'localhost:23' },
79             'Telnet connections should not be accepted locally');
80              
81             Test::Net::Connect B exports C and
82             C to make it easier to test whether or not a network
83             connection can be made from this host to a port on another host using
84             TCP or UDP.
85              
86             Test::Net::Connect uses Test::Builder, so plays nicely with Test::Simple,
87             Test::More, and other Test::Builder based modules.
88              
89             =head1 FUNCTIONS
90              
91             =head2 connect_ok($spec, [ $test_name ]);
92              
93             connect_ok() tests that a connection to a host, given in C<$spec>, can
94             be made.
95              
96             The specification is a hashref that contains one or more keys. Valid
97             keys are C, C, and C. Each value associated with
98             the key is the value that entry is supposed to have.
99              
100             =over 4
101              
102             =item host
103              
104             Specifies the hostname or IP address to connect to. If a hostname is
105             given then the A records for that host will be retrieved and connections
106             will be made to B the A records in turn. If any of them fail then
107             the test fails.
108              
109             C is mandatory.
110              
111             =item port
112              
113             Specifies the port to connect to. The port may also be specified by
114             appending a colon C<:> and the port number to the C value. If
115             this is done then C is optional, otherwise C is mandatory.
116              
117             =item proto
118              
119             The protocol to use. C and C are the only valid values. This
120             key is optional, and defaults to C if it is not specified.
121              
122             =back
123              
124             The C<$test_name> is optional. If it is not present then a sensible one
125             is generated following the form
126              
127             Connecting to $proto://$host:$port
128              
129             =cut
130              
131             sub connect_ok {
132 0     0 1   my($spec, $test_name) = @_;
133 0 0         return unless _check_spec($spec, $test_name);
134              
135 0 0         $test_name = _gen_default_test_name($spec) unless defined $test_name;
136              
137 0 0         return unless _dns_lookup($spec, $test_name);
138              
139 0           my @diag = ();
140              
141 0           foreach my $address (@{$spec->{_addresses}}) {
  0            
142 0           my $sock = IO::Socket::INET->new(PeerAddr => $address,
143             PeerPort => $spec->{port},
144             Proto => $spec->{proto},
145             Timeout => 5,
146             Type => SOCK_STREAM);
147              
148 0 0         if(! defined $sock) {
149 0           push @diag, " Connection to $spec->{proto}://$address:$spec->{port} failed: $!";
150             } else {
151 0           close $sock;
152             }
153             }
154              
155 0           delete $spec->{_addresses};
156              
157 0 0         if(@diag) {
158 0           my $ok = $Test->ok(0, $test_name);
159 0           $Test->diag(@diag);
160 0           return $ok;
161             }
162              
163 0           return $Test->ok(1, $test_name);
164             }
165              
166             =head2 connect_not_ok($spec, [ $test_name ]);
167              
168             connect_not_ok() tests that a connection to a host, given in $spec, can
169             not be made.
170              
171             The arguments are handled in the same manner as for connect_ok().
172              
173             B connect_not_ok() will fail (C) if the given host is not
174             in the DNS.
175             DNS.
176              
177             =cut
178              
179             sub connect_not_ok {
180 0     0 1   my($spec, $test_name) = @_;
181 0 0         return unless _check_spec($spec, $test_name);
182              
183 0 0         $test_name = _gen_default_test_name($spec) unless defined $test_name;
184              
185 0 0         return unless _dns_lookup($spec, $test_name);
186              
187 0           my @diag = ();
188              
189 0           foreach my $address (@{$spec->{_addresses}}) {
  0            
190 0           my $sock = IO::Socket::INET->new(PeerAddr => $address,
191             PeerPort => $spec->{port},
192             Proto => $spec->{proto},
193             Timeout => 5,
194             Type => SOCK_STREAM);
195              
196 0 0         if(defined $sock) {
197 0           push @diag, " Connection to $spec->{proto}://$address:$spec->{port} succeeded";
198 0           close($sock);
199             }
200             }
201              
202 0           delete $spec->{_addresses};
203              
204 0 0         if(@diag) {
205 0           my $ok = $Test->ok(0, $test_name);
206 0           $Test->diag(@diag);
207 0           return $ok;
208             }
209              
210 0           return $Test->ok(1, $test_name);
211             }
212              
213             sub _check_spec {
214 0     0     my($spec, $test_name) = @_;
215 0           my $sub = (caller(1))[3];
216              
217 0           $sub =~ s/Test::Net::Connect:://;
218              
219 0 0         if(! defined $spec) {
220 0           my $ok = $Test->ok(0, "$sub()");
221 0           $Test->diag(" $sub() called with no arguments");
222 0           return $ok;
223             }
224              
225 0 0         if(ref($spec) ne 'HASH') {
226 0 0         $test_name = defined $test_name ? $test_name : "$sub()";
227 0           my $ok = $Test->ok(0, $test_name);
228 0           $Test->diag(" First argument to $sub() must be a hash ref");
229 0           return $ok;
230             }
231              
232 0 0 0       if(! exists $spec->{host} or ! defined $spec->{host}
      0        
233             or $spec->{host} =~ /^\s*$/) {
234 0 0         $test_name = defined $test_name ? $test_name : "$sub()";
235 0           my $ok = $Test->ok(0, $test_name);
236 0           $Test->diag(" $sub() called with no hostname");
237 0           return $ok;
238             }
239              
240 0   0       $spec->{proto} ||= 'tcp';
241              
242 0 0         if($spec->{host} =~ /:/) {
243 0           ($spec->{host}, $spec->{port}) = $spec->{host} =~ /([^:]+):(.*)/;
244             }
245              
246 0 0 0       if(! defined $spec->{port} or $spec->{port} =~ /^\s*$/) {
247 0 0         $test_name = defined $test_name ? $test_name : "$sub()";
248 0           my $ok = $Test->ok(0, $test_name);
249 0           $Test->diag(" $sub() called with no port");
250 0           return $ok;
251             }
252              
253 0 0         if(! defined $test_name) {
254 0           $test_name = "Connecting to $spec->{proto}://$spec->{host}:$spec->{port}";
255             }
256              
257 0           my @diag = ();
258              
259 0           foreach my $field (keys %$spec) {
260 0 0         if(! exists $FIELDS{$field}) {
261 0           push @diag, " Invalid field '$field' given";
262             }
263             }
264              
265 0 0         if(@diag) {
266 0           my $ok = $Test->ok(0, $test_name);
267 0           $Test->diag(@diag);
268 0           return $ok;
269             }
270              
271 0           return 1;
272             }
273              
274             sub _dns_lookup {
275 0     0     my($spec, $test_name) = @_;
276              
277 0           $spec->{_addresses} = [];
278              
279             # If we've been handed a single IP address use that. Otherwise,
280             # look up all the IP addresses for the host
281 0 0         if($spec->{host} =~ /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/) {
282 0           push @{$spec->{_addresses}}, $spec->{host};
  0            
283             } else {
284 0           my $h = gethostbyname($spec->{host});
285              
286 0 0         if($h) {
287 0           push @{$spec->{_addresses}}, map { inet_ntoa($_) } @{$h->addr_list()};
  0            
  0            
  0            
288             } else {
289 0           my $ok = $Test->ok(0, $test_name);
290 0           $Test->diag(" DNS lookup for '$spec->{host}' failed");
291 0           return $ok;
292             }
293             }
294              
295 0           return 1;
296             }
297              
298             sub _gen_default_test_name {
299 0     0     my $spec = shift;
300              
301 0           return "Connecting to $spec->{proto}://$spec->{host}:$spec->{port}";
302             }
303              
304             =head1 EXAMPLES
305              
306             Verify that port 25 can be reached on all the A records for
307             C.
308              
309             connect_ok({ host => 'smtp.example.com', port => 25,
310             proto => 'tcp' }, "Checking mail to smtp.example.com");
311              
312             Do the same thing, but shorter.
313              
314             connect_ok({ host => 'smtp.example.com:25' });
315              
316             Verify that the local SSH daemon is responding.
317              
318             connect_ok({ host => '127.0.0.1:22' });
319              
320             Verify that the host www.example.com is not running a server on port 80.
321              
322             connect_not_ok({ host => 'www.example.com:80' });
323              
324             =head1 SEE ALSO
325              
326             Test::Simple, Test::Builder, IO::Socket::INET.
327              
328             =head1 AUTHOR
329              
330             Nik Clayton, nik@FreeBSD.org
331              
332             =head1 BUGS
333              
334             Please report any bugs or feature requests to
335             C, or through the web interface at
336             L.
337             I will be notified, and then you'll automatically be notified of progress on
338             your bug as I make changes.
339              
340             =head1 COPYRIGHT & LICENSE
341              
342             Copyright (c) 2005 Nik Clayton
343             All rights reserved.
344              
345             Redistribution and use in source and binary forms, with or without
346             modification, are permitted provided that the following conditions
347             are met:
348              
349             1. Redistributions of source code must retain the above copyright
350             notice, this list of conditions and the following disclaimer.
351             2. Redistributions in binary form must reproduce the above copyright
352             notice, this list of conditions and the following disclaimer in the
353             documentation and/or other materials provided with the distribution.
354              
355             THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
356             ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
357             IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
358             ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
359             FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
360             DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
361             OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
362             HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
363             LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
364             OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
365             SUCH DAMAGE.
366              
367             =cut
368              
369             1; # End of Test::Net::Connect