blib/lib/Net/DNS/Create.pm

filter: Statementstmt 89.8% 80 / 89 Branchbran 88.6% 39 / 44 Conditioncond 70.0% 14 / 20 MC/DCmcdc 40.0% 4 / 10 Subroutinesub 78.9% 15 / 19 Podpod 0.0% 0 / 9 totaltotal 79.5% 152 / 191 SCAR 35.3
CC
32
Cov
87%
domain
19.7
full_host
5.0
interval
4.1
CRAP 34.3
↑ Summary blib/lib/Net/DNS/Create/Bind.pm »
1 # Copyright (c) 2011-2014 David Caldwell, All Rights Reserved.
2  
3 package Net::DNS::Create;
4 3 ▶use strict; use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 3 our $VERSION='0.11.0';
7  
8 3 require Exporter;
9 3 our @ISA = qw(Exporter);
10 3 our @EXPORT = qw(domain master soa);
11 3 our @EXPORT_OK = qw(domain master full_host local_host email interval);
12  
13 3 my $kind;
14 3 our %config = (default_ttl=>'1h');
15 sub import {
16 3 ▶ use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 6 ▶ my $package = shift;
Subroutine import: called CC=4 SCAR=13.9
18 6 ▶ my $import_kind = shift // 'bind';
Condition50%1/2
shift() // "bind"
l!l
60
MC/DC0%0/1
shift() // "bind"
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
19  
20 # Tricky junk: If the first thing in our import list is "internal" then we just pass the rest to
21 # Exporter::export_to_level so that our plugins can include us back and import the full_host, email, and
22 # interval utility functions. Otherwise we pass the rest of the import args to the plugin's import so that
23 # conf options pass all the way down. In that case we don't pass anything to Exporter::export_to_level so
24 # that default export happens.
25 6 ▶ if ($import_kind ne 'internal') {
Branch: 1 branch
BranchTrueFalseTotal
if ($import_kind ne "internal") 3 3 6
26 3 $kind = __PACKAGE__ . "::" . $import_kind;
27 3 ▶ eval "require $kind"; die "$@" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 1 2 3
28 2 $kind->import(@_);
29 2 %config = (%config, @_); # Keep around the config for ourselves so we get the default_ttl setting.
30 2 @_ = ();
31 }
32 5 __PACKAGE__->export_to_level(1, $package, @_);
33 }
34  
35 sub full_host($;$);
36 sub full_host($;$) {
37 187 ▶ my ($name,$domain) = @_;
Subroutine full_host: called CC=5 SCAR=16.1
Pod undocumented
38 187 ▶ $name eq '@' ? (defined $domain ? full_host($domain) : die "Need a domain with @") :
Branch: 4 branches
BranchTrueFalseTotal
$name eq '@' ? : 4 183 187
defined $domain ? : 4 0 4
$name =~ /\.$/ ? : 58 125 183
defined $domain ? : 42 83 125
39 $name =~ /\.$/ ? $name : "$name." . (defined $domain ? full_host($domain) : '')
40 }
41  
42 sub local_host($$) {
43 39 ▶ my ($fq,$domain) = (full_host(shift), full_host(shift));
Subroutine local_host: called CC=3 SCAR=11.0
Pod undocumented
44 39 ▶ return '@' if $fq eq $domain;
Branch: 1 branch
BranchTrueFalseTotal
if $fq eq $domain 10 29 39
45 29 my $local = $fq;
46 29 ▶ return $local if substr($local, -length($domain)-1, length($domain)+1, '') eq ".$domain";
Branch: 1 branch
BranchTrueFalseTotal
if substr($local, -length($domain) - 1, length($domain) + 1, "") eq ".$domain" 28 1 29
47 1 return $fq;
48 }
49  
50 sub email($) {
51 6 ▶ my ($email) = @_;
Subroutine email: called CC=1 SCAR=0.0
Pod undocumented
52 6 $email =~ s/@/./g;
53 6 full_host($email);
54 }
55  
56 sub interval($) {
57 38 ▶ $_[0] =~ /(\d+)([hmsdw])/ && $1 * { s=>1, m=>60, h=>3600, d=>3600*24, w=>3600*24*7 }->{$2} || $_[0];
Subroutine interval: called CC=3 SCAR=14.2
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
$_[0] =~ /(\d+)([hmsdw])/ and $1 * {"s", 1, "m", 60, "h", 3600, "d", 86400, "w", 604800}->{$2} or $_[0] 0 38 38
Condition33%1/3
$_[0] =~ /(\d+)([hmsdw])/ and $1 * {"s", 1, "m", 60, "h", 3600, "d", 86400, "w", 604800}->{$2}
!ll&&!rl&&r
0038
MC/DC0%0/2
$_[0] =~ /(\d+)([hmsdw])/ and $1 * {"s", 1, "m", 60, "h", 3600, "d", 86400, "w", 604800}->{$2}
$_[0] =~ /(\d+)([hmsdw])/ $1 * {"s", 1, "m", 60, "h", 3600, "d", 86400, "w", 604800}->{$2}
Truth table33%1/3
A and B
A= $_[0] =~ /(\d+)([hmsdw])/B= $1 * {"s", 1, "m", 60, "h", 3600, "d", 86400, "w", 604800}->{$2}
ABresult
0X0
100
111
58 }
59  
60 sub txt($) {
61 12 ▶ my ($t) = @_;
Subroutine txt: called CC=3 SCAR=11.0
Pod undocumented
62 12 ▶ return "$t" if length $t < 255;
Branch: 1 branch
BranchTrueFalseTotal
if length $t < 255 10 2 12
63 2 my @part;
64 2 ▶ push @part, $1 while ($t =~ s/^(.{255})//);
Branch: 1 branch
BranchTrueFalseTotal
$t =~ s/^(.{255})// and do { push @part, $1 } 2 2 4
65 2 (@part, $t);
66 }
67  
68 3 ▶use Hash::Merge::Simple qw(merge);
Subroutine BEGIN: called CC=1 SCAR=0.0
69 3 ▶use Net::DNS::RR;
Subroutine BEGIN: called CC=1 SCAR=0.0
70 sub domain($@) {
71 2 ▶ my ($domain, @entry_hashes) = @_;
Subroutine domain: called CC=19 SCAR=29.8
Pod undocumented
72 2 my $entries = {};
73 2 for my $e (@entry_hashes) {
74 4 $entries = merge($entries, $e);
75 }
76  
77 2 my $fq_domain = full_host($domain);
78 2 my $ttl = interval($config{default_ttl});
79 2 $entries = [ map { my $node = $_;
80 36 my $fqdn = full_host($_,$domain);
81 map {
82 46 my $rr = lc $_;
83 46 my $val = $entries->{$node}->{$_};
84 46 my %common = (name => $fqdn,
85 ttl => $ttl,
86 type => uc $rr);
87 $rr eq 'a' || $rr eq 'cname' || $rr eq 'rp' || $rr eq 'soa' ?
88 Net::DNS::RR->new(%common,
89 $rr eq 'a' ? (address => $val) :
90 $rr eq 'cname' ? (cname => full_host($val, $fq_domain)) :
91 #$rr eq 'txt' ? (char_str_list => [txt($val)]) :
92 $rr eq 'rp' ? (mbox => email($val->[0]),
93 txtdname => full_host($val->[1], $fq_domain)) :
94 $rr eq 'soa' ? (mname => full_host($val->{primary_ns}, $domain),
95 rname => $val->{rp_email},
96 serial => $val->{serial} // 0,
97 refresh => interval($val->{refresh}),
98 retry => interval($val->{retry}),
99 expire => interval($val->{expire}),
100 minimum => interval($val->{min_ttl})) :
101 die "can't happen") :
102  
103 12 ▶ $rr eq 'txt' ? map { Net::DNS::RR->new(%common, char_str_list => [txt($_)]) } sort {$a cmp $b} (ref $val eq 'ARRAY' ? @{$val} : $val) :
Branch: 2 branches
BranchTrueFalseTotal
ref $val eq 'ARRAY' ? : 2 6 8
$rr eq 'mx' ? : 2 4 6
104 4 ▶ $rr eq 'mx' ? map { Net::DNS::RR->new(%common, preference => $_, exchange => full_host($val->{$_}, $fq_domain)) } sort(keys %$val) :
Branch: 1 branch
BranchTrueFalseTotal
$rr eq 'ns' ? : 2 2 4
105 4 ▶ $rr eq 'ns' ? map { Net::DNS::RR->new(%common, nsdname => $_) } sort(@$val) :
Branch: 1 branch
BranchTrueFalseTotal
$rr eq 'srv' ? : 2 0 2
106 $rr eq 'srv' ? map {
107 46 ▶ my $target = $_;
Branch: 6 branches
BranchTrueFalseTotal
$rr eq 'a' || $rr eq 'cname' || $rr eq 'rp' || $rr eq 'soa' ? : 32 14 46
$rr eq 'a' ? : 22 10 32
$rr eq 'cname' ? : 6 4 10
$rr eq 'rp' ? : 2 2 4
$rr eq 'soa' ? : 2 0 2
$rr eq 'txt' ? : 8 6 14
Condition90%10/11
$rr eq "a" or $rr eq "cname" or $rr eq "rp" or $rr eq "soa"
l!l&&r!l&&!r
30214
$rr eq "a" or $rr eq "cname" or $rr eq "rp"
l!l&&r!l&&!r
28216
$rr eq "a" or $rr eq "cname"
l!l&&r!l&&!r
22618
$val->{"serial"} // 0
l!l
20
MC/DC100%4/4
$rr eq "a" or $rr eq "cname" or $rr eq "rp" or $rr eq "soa"
$rr eq "a" $rr eq "cname" $rr eq "rp" $rr eq "soa"
MC/DC0%0/1
$val->{"serial"} // 0
$val->{"serial"}
Truth table100%5/5
A or B or C or D
A= $rr eq "a"B= $rr eq "cname"C= $rr eq "rp"D= $rr eq "soa"
ABCDresult
1XXX1
01XX1
001X1
00011
00000
Truth table50%1/2
A
A= $val->{"serial"}
Aresult
11
00
108 map {
109 Net::DNS::RR->new(%common,
110 priority => $_->{priority} // 0,
111 weight => $_->{weight} // 0,
112 port => $_->{port},
113 2 ▶ target => full_host($target))
Condition50%2/4
$_->{"priority"} // 0
l!l
02
$_->{"weight"} // 0
l!l
02
MC/DC0%0/1
$_->{"priority"} // 0
$_->{"priority"}
MC/DC0%0/1
$_->{"weight"} // 0
$_->{"weight"}
Truth table50%1/2
A
A= $_->{"priority"}
Aresult
11
00
Truth table50%1/2
A
A= $_->{"weight"}
Aresult
11
00
114 2 ▶ } sort {$a cmp $b} (ref $val->{$_} eq 'ARRAY' ? @{$val->{$_}} : $val->{$_})
Branch: 1 branch
BranchTrueFalseTotal
ref $val->{$_} eq 'ARRAY' ? : 0 2 2
115 } sort(keys %$val) :
116 die uc($rr)." is not supported yet :-("; # Remember to add support for all the backends, too.
117 36 } keys %{$entries->{$node}};
118 } keys %$entries ];
119  
120 2 $kind->domain($fq_domain, $entries);
121 }
122  
123 sub master {
124 2 ▶ $kind->master(@_);
Subroutine master: called CC=1 SCAR=0.0
Pod undocumented
125 }
126  
127 sub list_files() {
128 3 ▶ no warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
129 0 ▶ *domain = *main::domain = \&{"$kind\::domain_list"};
Subroutine list_files: not called CC=1 SCAR=6.9
Pod undocumented
130 0 *master = *main::master = \&{"$kind\::master_list"};
131 }
132  
133 sub list_domains() {
134 3 ▶ no warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
135 0 ▶ *domain = *main::domain = sub { print "$_[0]\n" };
Subroutine __ANON__: not called CC=1 SCAR=6.9
Subroutine list_domains: not called CC=1 SCAR=6.9
Pod undocumented
136 0 ▶ *master = *main::master = sub {};
Subroutine __ANON__: not called CC=1 SCAR=6.9
137 }
138  
139  
140 3 1;
141 __END__
↑ Summary blib/lib/Net/DNS/Create/Bind.pm »