Zonemaster/Engine/Util.pm

filter: Statementstmt 32.6% 34 / 104 Branchbran 0.0% 0 / 26 Conditioncond 0.0% 0 / 15 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 52.3% 11 / 21 Podpod 100% 10 / 10 totaltotal 29.8% 55 / 184 CC 24 SCAR 48.1
CC
24
Cov
23%
parse_hints
306.0
serial_gt
20.0
ipversion_ok
12.0
CRAP 122.2
« Zonemaster/Engine/Test/Syntax.pm ↑ Summary Zonemaster/Engine/Test/Connectivity.pm »
1 package Zonemaster::Engine::Util;
2  
3 1 ▶use v5.16.0;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 ▶use version; our $VERSION = version->declare("v1.1.13");
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 1 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
9 BEGIN {
10 1 ▶ our @EXPORT_OK = qw[
Subroutine BEGIN: called CC=1 SCAR=0.0
11 info
12 ipversion_ok
13 name
14 ns
15 parse_hints
16 should_run_test
17 scramble_case
18 test_levels
19 zone
20 ];
21 1 our %EXPORT_TAGS = ( all => \@EXPORT_OK );
22  
23 ## no critic (Modules::ProhibitAutomaticExportation)
24 1 our @EXPORT = qw[ ns info name scramble_case ];
25 }
26  
27 1 ▶use Net::DNS::ZoneFile;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 1 ▶use Pod::Simple::SimpleTree;
Subroutine BEGIN: called CC=1 SCAR=0.0
29  
30 1 ▶use Zonemaster::Engine;
Subroutine BEGIN: called CC=1 SCAR=0.0
31 1 ▶use Zonemaster::Engine::Constants qw[:ip :soa];
Subroutine BEGIN: called CC=1 SCAR=0.0
32 1 ▶use Zonemaster::Engine::DNSName;
Subroutine BEGIN: called CC=1 SCAR=0.0
33 1 ▶use Zonemaster::Engine::Profile;
Subroutine BEGIN: called CC=1 SCAR=0.0
34  
35 sub ns {
36 0 ▶ my ( $name, $address ) = @_;
Subroutine ns: not called CC=1 SCAR=6.9
Pod documented
37 0 return Zonemaster::Engine::Nameserver->new( { name => $name, address => $address } );
38 }
39  
40 sub info {
41 0 ▶ my ( $tag, $argref ) = @_;
Subroutine info: not called CC=1 SCAR=6.9
Pod documented
42  
43 0 return Zonemaster::Engine->logger->add( $tag, $argref );
44 }
45  
46 sub zone {
47 0 ▶ my ( $name ) = @_;
Subroutine zone: not called CC=1 SCAR=6.9
Pod documented
48  
49 0 return Zonemaster::Engine::Zone->new( { name => Zonemaster::Engine::DNSName->new( $name ) } );
50 }
51  
52 sub should_run_test {
53 0 ▶ my ( $test_name ) = @_;
Subroutine should_run_test: not called CC=1 SCAR=6.9
Pod documented
54 0 my %test_names = map { $_ => 1 } @{ Zonemaster::Engine::Profile->effective->get( q{test_cases} ) };
55  
56 0 return exists $test_names{$test_name};
57 }
58  
59 sub ipversion_ok {
60 0 ▶ my ( $version ) = @_;
Subroutine ipversion_ok: not called CC=3 SCAR=24.8
Pod documented
61  
62 0 ▶ if ( $version == $IP_VERSION_4 ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($version == $Zonemaster::Engine::Util::IP_VERSION_4) { } 0 0 0
elsif ($version == $Zonemaster::Engine::Util::IP_VERSION_6) { } 0 0 0
63 0 return Zonemaster::Engine::Profile->effective->get( q{net.ipv4} );
64 }
65 elsif ( $version == $IP_VERSION_6 ) {
66 0 return Zonemaster::Engine::Profile->effective->get( q{net.ipv6} );
67 }
68 else {
69 0 return;
70 }
71 }
72  
73 sub test_levels {
74 0 ▶ return Zonemaster::Engine::Profile->effective->get( q{test_levels} );
Subroutine test_levels: not called CC=1 SCAR=6.9
Pod documented
75 }
76  
77 ## no critic (Subroutines::RequireArgUnpacking)
78 sub name {
79 # We do not unpack @_ here for performance reasons.
80 # If we did, the calling convention is: my ( $name ) = @_.
81 0 ▶ return Zonemaster::Engine::DNSName->new( @_ );
Subroutine name: not called CC=1 SCAR=6.9
Pod documented
82 }
83  
84 # Function from CPAN package Text::Capitalize that causes
85 # issues when installing ZM.
86 #
87 sub scramble_case {
88 0 ▶ my $string = shift;
Subroutine scramble_case: not called CC=3 SCAR=24.8
Pod documented
89 0 my ( @chars, $uppity, $newstring, $uppers, $downers );
90  
91 0 @chars = split //, $string;
92  
93 0 $uppers = 2;
94 0 $downers = 1;
95 0 foreach my $c ( @chars ) {
96 0 $uppity = int( rand( 1 + $downers / $uppers ) );
97  
98 0 ▶ if ( $uppity ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($uppity) { } 0 0 0
99 0 $c = uc( $c );
100 0 $uppers++;
101 }
102 else {
103 0 $c = lc( $c );
104 0 $downers++;
105 }
106 }
107 0 $newstring = join q{}, @chars;
108 0 return $newstring;
109 } # end sub scramble_case
110  
111 sub parse_hints {
112 0 ▶ my $string = shift;
Subroutine parse_hints: not called CC=17 SCAR=57.2
Pod documented
113  
114 # Reject anything that is forbidden in hints files but allowed in zone files
115 # in general.
116 0 ▶ if ( $string =~ /^\$(TTL|INCLUDE|ORIGIN|GENERATE)/m ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($string =~ /^\$(TTL|INCLUDE|ORIGIN|GENERATE)/mu) 0 0 0
117 0 die "Forbidden directive \$$1\n";
118 }
119  
120 0 my $rrs = Net::DNS::ZoneFile->parse( \$string );
121 0 ▶ if ( !defined $rrs ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $rrs) 0 0 0
122 0 die "Unable to parse root hints\n";
123 }
124  
125 0 my %ns;
126 my %glue;
127 0 for my $rr ( @$rrs ) {
128 0 ▶ if ( $rr->class ne 'IN' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rr->class ne "IN") 0 0 0
129 0 my $rrclass = $rr->class;
130 0 die "Forbidden RR class $rrclass\n";
131 }
132  
133 0 ▶ if ( $rr->type eq 'NS' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($rr->type eq 'NS') { } 0 0 0
elsif ($rr->type eq 'A' or $rr->type eq 'AAAA') { } 0 0 0
134 0 ▶ if ( $rr->owner ne '.' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rr->owner ne ".") 0 0 0
135 0 my $owner = $rr->owner;
136 0 die "Owner name for NS record must be \".\"\n";
137 }
138 0 $ns{ $rr->nsdname } = 0;
139 }
140 ▶ elsif ( $rr->type eq 'A' || $rr->type eq 'AAAA' ) {
Condition0%0/3
$rr->type eq "A" or $rr->type eq "AAAA"
l!l&&r!l&&!r
000
MC/DC0%0/2
$rr->type eq "A" or $rr->type eq "AAAA"
$rr->type eq "A" $rr->type eq "AAAA"
Truth table0%0/3
A or B
A= $rr->type eq "A"B= $rr->type eq "AAAA"
ABresult
1X1
011
000
141 0 $glue{ $rr->owner } = $rr->type;
142 }
143 else {
144 0 my $rrtype = $rr->type;
145 0 die "Forbidden RR type $rrtype\n";
146 }
147 } ## end for my $rr ( @$rrs )
148  
149 0 for my $owner ( sort keys %glue ) {
150 0 ▶ if ( exists $ns{$owner} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $ns{$owner}) { } 0 0 0
151 0 $ns{$owner} = 1;
152 }
153 else {
154 0 my $rrtype = $glue{$owner};
155 0 die "Owner name of $rrtype record does not match any NS RDATA\n";
156 }
157 }
158  
159 0 for my $nsdname ( sort keys %ns ) {
160 0 ▶ if ( $ns{$nsdname} == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ns{$nsdname} == 0) 0 0 0
161 0 die "No address record found for NS $nsdname\n";
162 }
163 }
164  
165 0 ▶ if ( !%ns ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (%ns) 0 0 0
166 0 die "No NS record found\n";
167 }
168  
169 # Extract hint data
170 0 my %hints;
171 0 for my $rr ( @{ $rrs } ) {
172 0 ▶ if ( $rr->type eq 'A' or $rr->type eq 'AAAA' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rr->type eq "A" or $rr->type eq "AAAA") 0 0 0
Condition0%0/3
$rr->type eq "A" or $rr->type eq "AAAA"
l!l&&r!l&&!r
000
MC/DC0%0/2
$rr->type eq "A" or $rr->type eq "AAAA"
$rr->type eq "A" $rr->type eq "AAAA"
Truth table0%0/3
A or B
A= $rr->type eq "A"B= $rr->type eq "AAAA"
ABresult
1X1
011
000
173 0 push @{ $hints{$rr->owner} }, $rr->address;
174 }
175 }
176  
177 0 return \%hints;
178 }
179  
180 sub serial_gt {
181 0 ▶ my ( $sa, $sb ) = @_;
Subroutine serial_gt: not called CC=4 SCAR=30.0
Pod documented
182  
183 0 ▶ return ( ( $sa < $sb and ( ($sb - $sa) > 2**( $SERIAL_BITS - 1 ) ) ) or
Condition0%0/9
$sa < $sb && $sb - $sa > 2 ** ($Zonemaster::Engine::Util::SERIAL_BITS - 1) || $sa > $sb && $sa - $sb < 2 ** ($Zonemaster::Engine::Util::SERIAL_BITS - 1)
l!l&&r!l&&!r
000
$sa < $sb and $sb - $sa > 2 ** ($Zonemaster::Engine::Util::SERIAL_BITS - 1)
!ll&&!rl&&r
000
$sa > $sb and $sa - $sb < 2 ** ($Zonemaster::Engine::Util::SERIAL_BITS - 1)
!ll&&!rl&&r
000
MC/DC0%0/4
$sa < $sb && $sb - $sa > 2 ** ($Zonemaster::Engine::Util::SERIAL_BITS - 1) || $sa > $sb && $sa - $sb < 2 ** ($Zonemaster::Engine::Util::SERIAL_BITS - 1)
$sa < $sb $sb - $sa > 2 ** ($Zonemaster::Engine::Util::SERIAL_BITS - 1) $sa > $sb $sa - $sb < 2 ** ($Zonemaster::Engine::Util::SERIAL_BITS - 1)
Truth table0%0/7
A and B || C and D
A= $sa < $sbB= $sb - $sa > 2 ** ($Zonemaster::Engine::Util::SERIAL_BITS - 1)C= $sa > $sbD= $sa - $sb < 2 ** ($Zonemaster::Engine::Util::SERIAL_BITS - 1)
ABCDresult
11XX1
0X111
10111
0X0X0
0X100
100X0
10100
184 ( $sa > $sb and ( ($sa - $sb) < 2**( $SERIAL_BITS - 1 ) ) )
185 );
186 }
187  
188 1 1;
189  
190 =head1 NAME
191  
192 Zonemaster::Engine::Util - utility functions for other Zonemaster modules
193  
194 =head1 SYNOPSIS
195  
196 use Zonemaster::Engine::Util;
197 info(TAG => { some => 'argument'});
198 my $ns = ns($name, $address);
199 my $name = name('whatever.example.org');
200  
201 =head1 EXPORTED FUNCTIONS
202  
203 =over
204  
205 =item info($tag, $href)
206  
207 Creates and returns a L<Zonemaster::Engine::Logger::Entry> object. The object
208 is also added to the global logger object's list of entries.
209  
210 =item ns($name, $address)
211  
212 Creates and returns a nameserver object with the given name and address.
213  
214 =item name($string_name_or_zone)
215  
216 Creates and returns a L<Zonemaster::Engine::DNSName> object for the given argument.
217  
218 =item zone($name)
219  
220 Returns a L<Zonemaster::Engine::Zone> object for the given name.
221  
222 =item parse_hints($string)
223  
224 Parses a string in the root hints format into the format expected by
225 Zonemaster::Engine::Resolver->add_fake_addresses().
226  
227 Returns a hashref with domain names as keys and arrayrefs to IP addresses as
228 values.
229  
230 Throws an exception if the inputs is not valid root hints text.
231  
232 A root hints file is a valid RFC 1035 zone file of the same type IANA publishes
233 to be used as hint file for name servers
234 L<https://www.internic.net/domain/named.root>.
235  
236 In addition to being valid zone file the following restrictions are imposed on
237 the root hints format:
238  
239 =over
240  
241 =item *
242 The file must not contain any $TTL, $ORIGIN, $INCLUDE or $GENERATE directives.
243  
244 =item *
245 The class field of all records must be "IN" or absent. If class is absent, IN is
246 assumed.
247  
248 =item *
249 The TTL field may be absent or present. The TTL value is ignored.
250  
251 =item *
252 The RR type of all DNS records must be NS, A or AAAA.
253  
254 =item *
255 The file must contain at least one NS record.
256  
257 =item *
258 The owner name of all NS records must be C<.>.
259  
260 =item *
261 For every NS record there must be at least one address record (A or AAAA) whose
262 owner name is identical to the domain name in the RDATA of the NS record.
263  
264 =item *
265 All address records (A or AAAA) must have an owner name that is identical to the
266 domain name in the RDATA of some NS record in the zone.
267  
268 =back
269  
270 =item serial_gt($serial_a, $serial_b)
271 Checks if serial_a is greater than serial_b, according to
272 serial number arithmetic as defined in RFC1982, section 3.2.
273  
274 Return a boolean.
275  
276 =item scramble_case
277  
278 This routine provides a special effect: sCraMBliNg tHe CaSe
279  
280 =item should_run_test
281  
282 Check if a test is blacklisted and should run or not.
283  
284 =item ipversion_ok
285  
286 Check if IP version operations are permitted. Tests are done against Zonemaster::Engine::Profile->effective content.
287  
288 =item test_levels
289  
290 WIP, here to please L<Pod::Coverage>.
291  
292 =back
« Zonemaster/Engine/Test/Syntax.pm ↑ Summary Zonemaster/Engine/Test/Connectivity.pm »