File Coverage

blib/lib/Data/Checker/DNS.pm
Criterion Covered Total %
statement 63 67 94.0
branch 30 36 83.3
condition 4 6 66.6
subroutine 4 4 100.0
pod 1 1 100.0
total 102 114 89.4


line stmt bran cond sub pod time code
1             package Data::Checker::DNS;
2             # Copyright (c) 2013-2014 Sullivan Beck. All rights reserved.
3             # This program is free software; you can redistribute it and/or modify it
4             # under the same terms as Perl itself.
5              
6             ###############################################################################
7              
8             require 5.008;
9 1     1   6 use warnings 'all';
  1         2  
  1         48  
10 1     1   5 use strict;
  1         1  
  1         30  
11 1     1   4 use Net::DNS;
  1         3  
  1         824  
12              
13             our($VERSION);
14             $VERSION='1.05';
15              
16             ###############################################################################
17             ###############################################################################
18              
19             sub check {
20 22     22 1 27 my($obj,$element,$desc,$check_opts) = @_;
21 22         35 my $err = [];
22 22         22 my $warn = [];
23 22         23 my $info = [];
24             # 0 - 255
25 22         73 my $oct_rx = qr/([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])/;
26              
27 22 100       59 if (! defined $check_opts) {
28 2         5 $check_opts = { 'dns' => undef };
29             }
30              
31             # Check to see if it's an IP
32              
33 22         215 my $is_hostname = ($element !~ /^$oct_rx\.$oct_rx\.$oct_rx\.$oct_rx$/);
34              
35             # Do the qualified check
36              
37 22         64 $obj->check_value($check_opts,'qualified',$element,$is_hostname,
38             'Only hostnames can be check with qualified',undef,
39             $err,$warn,$info);
40 22 100       46 return ($element,$err,$warn,$info) if (@$err);
41              
42 21 50       35 if ($is_hostname) {
43 21         78 my @host = split(/\./,$element);
44 21         24 my($fqhost,$uqhost,$domain);
45 21 100       42 if (@host == 1) {
46 4         6 $uqhost = $element;
47             } else {
48 17         23 $fqhost = $element;
49 17         32 $uqhost = shift(@host);
50 17         36 $domain = join('.',@host);
51             }
52              
53 21         49 $obj->check_value($check_opts,'qualified',$element,$fqhost,
54             'Host is not fully qualified',
55             'Host is fully qualified',
56             $err,$warn,$info);
57 21 100       56 return ($element,$err,$warn,$info) if (@$err);
58             }
59              
60             # Set up the resolver
61              
62 17         17 my $res;
63 17         42 my $nameservers = $obj->check_option($check_opts,'nameservers');
64 17 50       30 if ($nameservers) {
65 0         0 my @nameservers = split(/\s+/,$nameservers);
66 0         0 $res = Net::DNS::Resolver->new(nameservers => [@nameservers]);
67             } else {
68 17         114 $res = Net::DNS::Resolver->new();
69             }
70              
71             # Do the dns check
72              
73 17         1072 my $q = $res->search($element);
74 17 100       555488 my $in_dns = ($q ? 1 : 0);
75              
76 17         103 $obj->check_value($check_opts,'dns',$element,$in_dns,
77             'Host is not defined in DNS',
78             'Host is already in DNS',
79             $err,$warn,$info);
80 17 100       87 return ($element,$err,$warn,$info) if (@$err);
81              
82             # Do the expected_* checks
83              
84 14         26 foreach my $check ('ip','domain','hostname') {
85 38         51 my $label = "expected_$check";
86 38 100       63 next if (! $obj->check_performed($check_opts,$label));
87              
88             # Get the expected value(s)
89              
90 7         10 my $vals;
91 7 100 66     50 if (defined($desc) &&
      66        
92             ref($desc) eq 'HASH' &&
93             exists $$desc{$check}) {
94 5         10 $vals = $$desc{$check};
95             } else {
96 2         9 $vals = $obj->check_option($check_opts,'value',undef,$label);
97             }
98              
99 7         15 my %vals = ();
100 7 50       15 if (defined($vals)) {
101 7 100       28 if (ref($vals) eq 'ARRAY') {
    50          
102 3         7 %vals = map { $_,1 } @$vals;
  4         15  
103             } elsif (! ref($vals)) {
104 4         10 %vals = ( $vals => 1 );
105             }
106             }
107              
108 7         26 my @vals = keys %vals;
109 7 50       17 if (! @vals) {
110 0         0 die "ERROR: No value provided for expected_$check DNS check.\n";
111             }
112              
113             # Test each value in DNS
114              
115 7         36 my @a = $q->answer();
116 7         43 foreach my $rr (@a) {
117 9 100       53 next if ($rr->type ne 'A');
118              
119 7         145 my $value;
120 7 100       24 if ($check eq 'ip') {
    50          
121 2         8 $value = $rr->address;
122             } elsif ($check eq 'domain') {
123 5         20 $value = $rr->name;
124 5         318 $value =~ s/^.*?\.//;
125             } else {
126 0         0 $value = $rr->name;
127             }
128              
129 7         68 $obj->check_value($check_opts,$label,$element,exists $vals{$value},
130             "DNS $check value does not match expected value",
131             "DNS $check value is a restricted value",
132             $err,$warn,$info);
133 7 100       70 return ($element,$err,$warn,$info) if (@$err);
134             }
135             }
136              
137 11         158 return ($element,$err,$warn,$info);
138             }
139              
140              
141             1;
142             # Local Variables:
143             # mode: cperl
144             # indent-tabs-mode: nil
145             # cperl-indent-level: 3
146             # cperl-continued-statement-offset: 2
147             # cperl-continued-brace-offset: 0
148             # cperl-brace-offset: 0
149             # cperl-brace-imaginary-offset: 0
150             # cperl-label-offset: 0
151             # End: