Connect.pm

filter: Statementstmt 94.1% 112 / 119 Branchbran 91.3% 42 / 46 Conditioncond 100% 11 / 11 MC/DCmcdc 100% 6 / 6 Subroutinesub 100% 12 / 12 Podpod 100% 2 / 2 totaltotal 94.3% 185 / 196 CC 31 SCAR 22.8
CC
31
Cov
94%
_check_spec
17.0
connect_not_ok
7.9
connect_ok
7.2
CRAP 9.8
« Connect/ConfigData.pm ↑ Summary
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 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
29  
30 3 ▶use Test::Builder;
Subroutine BEGIN: called CC=1 SCAR=0.0
31  
32 3 require Exporter;
33 3 our @ISA = qw(Exporter);
34 3 our @EXPORT = qw(connect_ok connect_not_ok);
35  
36 3 my $Test = Test::Builder->new;
37  
38 3 ▶use Net::hostent;
Subroutine BEGIN: called CC=1 SCAR=0.0
39 3 ▶use Socket;
Subroutine BEGIN: called CC=1 SCAR=0.0
40 3 ▶use IO::Socket::INET;
Subroutine BEGIN: called CC=1 SCAR=0.0
41  
42 3 my @FIELDS = qw(host port proto);
43 3 my %FIELDS = map { $_ => 1 } @FIELDS;
44  
45 2 ▶sub import {
Subroutine import: called CC=1 SCAR=0.0
46 2 my($self) = shift;
47 2 my $pack = caller;
48  
49 2 $Test->exported_to($pack);
50 2 $Test->plan(@_);
51  
52 2 $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 3 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<automatically> exports C<connect_ok()> and
82 C<connect_not_ok()> 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<host>, C<port>, and C<proto>. 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<all> the A records in turn. If any of them fail then
107 the test fails.
108  
109 C<host> 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<host> value. If
115 this is done then C<port> is optional, otherwise C<port> is mandatory.
116  
117 =item proto
118  
119 The protocol to use. C<tcp> and C<udp> are the only valid values. This
120 key is optional, and defaults to C<tcp> 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 18 ▶sub connect_ok {
Subroutine connect_ok: called CC=7 SCAR=19.7
Pod documented
132 18 my($spec, $test_name) = @_;
133 18 ▶ return unless _check_spec($spec, $test_name);
Branch: 1 branch
BranchTrueFalseTotal
unless _check_spec($spec, $test_name) 11 7 18
134  
135 7 ▶ $test_name = _gen_default_test_name($spec) unless defined $test_name;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $test_name 1 6 7
136  
137 7 ▶ return unless _dns_lookup($spec, $test_name);
Branch: 1 branch
BranchTrueFalseTotal
unless _dns_lookup($spec, $test_name) 1 6 7
138  
139 6 my @diag = ();
140  
141 6 foreach my $address (@{$spec->{_addresses}}) {
142 my $sock = IO::Socket::INET->new(PeerAddr => $address,
143 PeerPort => $spec->{port},
144 Proto => $spec->{proto},
145 11 Timeout => 5,
146 Type => SOCK_STREAM);
147  
148 11 ▶ if(! defined $sock) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $sock) { } 11 0 11
149 11 push @diag, " Connection to $spec->{proto}://$address:$spec->{port} failed: $!";
150 } else {
151 0 close $sock;
152 }
153 }
154  
155 6 delete $spec->{_addresses};
156  
157 6 ▶ if(@diag) {
Branch: 1 branch
BranchTrueFalseTotal
if (@diag) 6 0 6
158 6 my $ok = $Test->ok(0, $test_name);
159 6 $Test->diag(@diag);
160 6 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<NOTE:> connect_not_ok() will fail (C<not ok>) if the given host is not
174 in the DNS.
175 DNS.
176  
177 =cut
178  
179 11 ▶sub connect_not_ok {
Subroutine connect_not_ok: called CC=7 SCAR=20.6
Pod documented
180 11 my($spec, $test_name) = @_;
181 11 ▶ return unless _check_spec($spec, $test_name);
Branch: 1 branch
BranchTrueFalseTotal
unless _check_spec($spec, $test_name) 7 4 11
182  
183 4 ▶ $test_name = _gen_default_test_name($spec) unless defined $test_name;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $test_name 1 3 4
184  
185 4 ▶ return unless _dns_lookup($spec, $test_name);
Branch: 1 branch
BranchTrueFalseTotal
unless _dns_lookup($spec, $test_name) 1 3 4
186  
187 3 my @diag = ();
188  
189 3 foreach my $address (@{$spec->{_addresses}}) {
190 my $sock = IO::Socket::INET->new(PeerAddr => $address,
191 PeerPort => $spec->{port},
192 Proto => $spec->{proto},
193 6 Timeout => 5,
194 Type => SOCK_STREAM);
195  
196 6 ▶ if(defined $sock) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $sock) 0 6 6
197 0 push @diag, " Connection to $spec->{proto}://$address:$spec->{port} succeeded";
198 0 close($sock);
199 }
200 }
201  
202 3 delete $spec->{_addresses};
203  
204 3 ▶ if(@diag) {
Branch: 1 branch
BranchTrueFalseTotal
if (@diag) 0 3 3
205 0 my $ok = $Test->ok(0, $test_name);
206 0 $Test->diag(@diag);
207 0 return $ok;
208 }
209  
210 3 return $Test->ok(1, $test_name);
211 }
212  
213 29 ▶sub _check_spec {
Subroutine _check_spec: called CC=17 SCAR=28.3
214 29 my($spec, $test_name) = @_;
215 29 my $sub = (caller(1))[3];
216  
217 29 $sub =~ s/Test::Net::Connect:://;
218  
219 29 ▶ if(! defined $spec) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $spec) 2 27 29
220 2 my $ok = $Test->ok(0, "$sub()");
221 2 $Test->diag(" $sub() called with no arguments");
222 2 return $ok;
223 }
224  
225 27 ▶ if(ref($spec) ne 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $spec ne "HASH") 4 23 27
226 4 ▶ $test_name = defined $test_name ? $test_name : "$sub()";
Branch: 1 branch
BranchTrueFalseTotal
defined $test_name ? : 2 2 4
227 4 my $ok = $Test->ok(0, $test_name);
228 4 $Test->diag(" First argument to $sub() must be a hash ref");
229 4 return $ok;
230 }
231  
232 23 ▶ if(! exists $spec->{host} or ! defined $spec->{host}
Branch: 1 branch
BranchTrueFalseTotal
if (not exists $spec->{"host"} && defined $spec->{"host"} or $spec->{"host"} =~ /^\s*$/) 8 15 23
Condition100%6/6
not exists $spec->{"host"} && defined $spec->{"host"} or $spec->{"host"} =~ /^\s*$/
l!l&&r!l&&!r
4415
exists $spec->{'host'} && defined $spec->{'host'}
!ll&&!rl&&r
2219
MC/DC100%3/3
not exists $spec->{"host"} && defined $spec->{"host"} or $spec->{"host"} =~ /^\s*$/
exists $spec->{'host'} defined $spec->{'host'} $spec->{"host"} =~ /^\s*$/
Truth table100%4/4
not(A && B) or C
A= exists $spec->{'host'}B= defined $spec->{'host'}C= $spec->{"host"} =~ /^\s*$/
ABCresult
0XX1
10X1
1111
1100
233 or $spec->{host} =~ /^\s*$/) {
234 8 ▶ $test_name = defined $test_name ? $test_name : "$sub()";
Branch: 1 branch
BranchTrueFalseTotal
defined $test_name ? : 2 6 8
235 8 my $ok = $Test->ok(0, $test_name);
236 8 $Test->diag(" $sub() called with no hostname");
237 8 return $ok;
238 }
239  
240 15 ▶ $spec->{proto} ||= 'tcp';
Condition100%2/2
$spec->{"proto"} ||= "tcp"
l!l
105
MC/DC100%1/1
$spec->{"proto"} ||= "tcp"
$spec->{"proto"}
Truth table100%2/2
A
A= $spec->{"proto"}
Aresult
11
00
241  
242 15 ▶ if($spec->{host} =~ /:/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($spec->{"host"} =~ /:/) 2 13 15
243 2 ($spec->{host}, $spec->{port}) = $spec->{host} =~ /([^:]+):(.*)/;
244 }
245  
246 15 ▶ if(! defined $spec->{port} or $spec->{port} =~ /^\s*$/) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $spec->{"port"} or $spec->{"port"} =~ /^\s*$/) 3 12 15
Condition100%3/3
not defined $spec->{"port"} or $spec->{"port"} =~ /^\s*$/
l!l&&r!l&&!r
1212
MC/DC100%2/2
not defined $spec->{"port"} or $spec->{"port"} =~ /^\s*$/
not defined $spec->{"port"} $spec->{"port"} =~ /^\s*$/
Truth table100%3/3
not(A) or B
A= not defined $spec->{"port"}B= $spec->{"port"} =~ /^\s*$/
ABresult
1X1
011
000
247 3 ▶ $test_name = defined $test_name ? $test_name : "$sub()";
Branch: 1 branch
BranchTrueFalseTotal
defined $test_name ? : 1 2 3
248 3 my $ok = $Test->ok(0, $test_name);
249 3 $Test->diag(" $sub() called with no port");
250 3 return $ok;
251 }
252  
253 12 ▶ if(! defined $test_name) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $test_name) 3 9 12
254 3 $test_name = "Connecting to $spec->{proto}://$spec->{host}:$spec->{port}";
255 }
256  
257 12 my @diag = ();
258  
259 12 foreach my $field (keys %$spec) {
260 37 ▶ if(! exists $FIELDS{$field}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $FIELDS{$field}) 1 36 37
261 1 push @diag, " Invalid field '$field' given";
262 }
263 }
264  
265 12 ▶ if(@diag) {
Branch: 1 branch
BranchTrueFalseTotal
if (@diag) 1 11 12
266 1 my $ok = $Test->ok(0, $test_name);
267 1 $Test->diag(@diag);
268 1 return $ok;
269 }
270  
271 11 return 1;
272 }
273  
274 11 ▶sub _dns_lookup {
Subroutine _dns_lookup: called CC=3 SCAR=11.0
275 11 my($spec, $test_name) = @_;
276  
277 11 $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 11 ▶ if($spec->{host} =~ /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($spec->{'host'} =~ /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/) { } 1 10 11
282 1 push @{$spec->{_addresses}}, $spec->{host};
283 } else {
284 10 my $h = gethostbyname($spec->{host});
285  
286 10 ▶ if($h) {
Branch: 1 branch
BranchTrueFalseTotal
if ($h) { } 8 2 10
287 8 push @{$spec->{_addresses}}, map { inet_ntoa($_) } @{$h->addr_list()};
288 } else {
289 2 my $ok = $Test->ok(0, $test_name);
290 2 $Test->diag(" DNS lookup for '$spec->{host}' failed");
291 2 return $ok;
292 }
293 }
294  
295 9 return 1;
296 }
297  
298 2 ▶sub _gen_default_test_name {
Subroutine _gen_default_test_name: called CC=1 SCAR=0.0
299 2 my $spec = shift;
300  
301 2 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<smtp.example.com>.
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<bug-test-net-connect@rt.cpan.org>, or through the web interface at
336 L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Test-Net-Connect>.
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 3 1; # End of Test::Net::Connect
« Connect/ConfigData.pm ↑ Summary