File Coverage

blib/lib/Test/Internet.pm
Criterion Covered Total %
statement 35 36 97.2
branch 5 10 50.0
condition n/a
subroutine 8 8 100.0
pod 1 1 100.0
total 49 55 89.0


line stmt bran cond sub pod time code
1             package Test::Internet;
2              
3             $Test::Internet::VERSION = '0.05';
4             $Test::Internet::AUTHORITY = 'cpan:MANWAR';
5              
6             =head1 NAME
7              
8             Test::Internet - Interface to test internet connection.
9              
10             =head1 VERSION
11              
12             Version 0.05
13              
14             =cut
15              
16 3     3   19231 use strict; use warnings;
  3     3   5  
  3         91  
  3         9  
  3         4  
  3         62  
17              
18 3     3   42 use 5.006;
  3         9  
  3         83  
19 3     3   1840 use Socket;
  3         10371  
  3         1440  
20 3     3   1794 use Net::DNS;
  3         223847  
  3         321  
21 3     3   2116 use Data::Dumper;
  3         28113  
  3         300  
22 3     3   1998 use Test::Builder ();
  3         29366  
  3         976  
23              
24             require Exporter;
25             our @ISA = qw(Exporter);
26             our @EXPORT = qw(connect_ok);
27              
28             our $DEFAULT_TIMEOUT = 2;
29             our $STD_NAMESERVERS = [ 'a.root-servers.net',
30             'b.root-servers.net',
31             'c.root-servers.net',
32             'd.root-servers.net',
33             'e.root-servers.net',
34             'f.root-servers.net',
35             'g.root-servers.net',
36             'h.root-servers.net',
37             'i.root-servers.net',
38             'j.root-servers.net' ];
39              
40             =head1 DESCRIPTION
41              
42             It provides a simple interface to test the internet connection reliably. I needed
43             this feature in the test script for one of my package L. The
44             code can be found L.
45              
46             =head1 METHODS
47              
48             =head2 connect_ok($timeout)
49              
50             Return true/false depending on whether there is an active internet connection.The
51             default timeout is 2 seconds unless the user pass the timeout period. It gets
52             exported by default.
53              
54             use strict; use warnings;
55             use Test::More;
56             use Test::Internet;
57              
58             plan skip_all => "No internet connection." unless connect_ok();
59             ok(connect_ok());
60             done_testing();
61              
62             =cut
63              
64             sub connect_ok {
65 2     2 1 104 my ($timeout) = @_;
66              
67 2         7 my @nameservers = ();
68 2         8 foreach (@$STD_NAMESERVERS) {
69 20 50       58369 inet_aton($_) && (push @nameservers, $_);
70             }
71              
72 2 50       17 return 0 unless (scalar(@nameservers));
73              
74 2 50       12 $timeout = $DEFAULT_TIMEOUT unless defined $timeout;
75 2         53 my $resolver = Net::DNS::Resolver->new;
76 2         710 $resolver->tcp_timeout($timeout);
77 2         54 $resolver->udp_timeout($timeout);
78 2         44 $resolver->nameservers(@nameservers);
79              
80 2         103903 my $response = $resolver->query("root-servers.net", "NS");
81 2 50       25167 if (defined $response) {
82 2 50       12 return 1 if (grep { $_->type eq 'NS' } $response->answer);
  26         266  
83             }
84              
85 0           return 0;
86             }
87              
88             =head1 Why L?
89              
90             Karen Etheridge raised this question as in RT# 102095 and introduced me to a very
91             similar module L on CPAN.What a shame that it didn't turn
92             up in a search on CPAN, while I was looking for any module with the word Internet.
93             I am not an expert on how the CPAN search engine works, though. Had I known about
94             it, I wouldn't have bothered creating L to be honest.
95              
96             The nice thing about the L is that it does not need any
97             external module and just uses what is available in core perl i.e. Socket. However
98             it relies on a webservice to exist and respond, so if that webservice is down the
99             module will give a false negative.
100              
101             So if the requirement is to check if there is an active internet connection only
102             then I would recommend L. In case you want to check if you can
103             reach a particular given host as well then go for L.
104              
105             =head1 AUTHOR
106              
107             Mohammad S Anwar, C<< >>
108              
109             =head1 REPOSITORY
110              
111             L
112              
113             =head1 ACKNOWLEDGEMENT
114              
115             David Kitcher-Jones (m4ddav3) for his immensely valuable inputs.
116              
117             =head1 SEE ALSO
118              
119             L
120              
121             =head1 BUGS
122              
123             Please report any bugs or feature requests to C
124             or through the web interface at L.
125             I will be notified, and then you'll automatically be notified of progress on your
126             bug as I make changes.
127              
128             =head1 SUPPORT
129              
130             You can find documentation for this module with the perldoc command.
131              
132             perldoc Test::Internet
133              
134             You can also look for information at:
135              
136             =over 4
137              
138             =item * RT: CPAN's request tracker (report bugs here)
139              
140             L
141              
142             =item * AnnoCPAN: Annotated CPAN documentation
143              
144             L
145              
146             =item * CPAN Ratings
147              
148             L
149              
150             =item * Search CPAN
151              
152             L
153              
154             =back
155              
156             =head1 LICENSE AND COPYRIGHT
157              
158             Copyright (C) 2015 Mohammad S Anwar.
159              
160             This program is free software; you can redistribute it and/or modify it under
161             the terms of the the Artistic License (2.0). You may obtain a copy of the full
162             license at:
163              
164             L
165              
166             Any use, modification, and distribution of the Standard or Modified Versions is
167             governed by this Artistic License.By using, modifying or distributing the Package,
168             you accept this license. Do not use, modify, or distribute the Package, if you do
169             not accept this license.
170              
171             If your Modified Version has been derived from a Modified Version made by someone
172             other than you,you are nevertheless required to ensure that your Modified Version
173             complies with the requirements of this license.
174              
175             This license does not grant you the right to use any trademark, service mark,
176             tradename, or logo of the Copyright Holder.
177              
178             This license includes the non-exclusive, worldwide, free-of-charge patent license
179             to make, have made, use, offer to sell, sell, import and otherwise transfer the
180             Package with respect to any patent claims licensable by the Copyright Holder that
181             are necessarily infringed by the Package. If you institute patent litigation
182             (including a cross-claim or counterclaim) against any party alleging that the
183             Package constitutes direct or contributory patent infringement,then this Artistic
184             License to you shall terminate on the date that such litigation is filed.
185              
186             Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER AND
187             CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES. THE IMPLIED
188             WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR
189             NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY YOUR LOCAL LAW. UNLESS
190             REQUIRED BY LAW, NO COPYRIGHT HOLDER OR CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT,
191             INDIRECT, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE
192             OF THE PACKAGE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
193              
194             =cut
195              
196             1; # End of Test::Internet