Zonemaster/Engine/DNSName.pm

filter: Statementstmt 25.5% 23 / 90 Branchbran 0.0% 0 / 32 Conditioncond 0.0% 0 / 26 MC/DCmcdc 0.0% 0 / 17 Subroutinesub 41.1% 7 / 17 Podpod 100% 10 / 10 totaltotal 20.8% 40 / 192 CC 26 SCAR 40.6
CC
26
Cov
16%
new
156.0
str_cmp
56.0
common
20.0
CRAP 58.2
« Zonemaster/Engine/ASNLookup.pm ↑ Summary Zonemaster/Engine/Profile.pm »
1 package Zonemaster::Engine::DNSName;
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.0.3");
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Scalar::Util qw( blessed );
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 1 ▶use Class::Accessor "antlers";
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 use overload
14 1 ▶ '""' => \&string,
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 'cmp' => \&str_cmp;
16  
17 1 has 'labels' => ( is => 'ro' );
18  
19 0 ▶sub from_string {
Subroutine from_string: not called CC=3 SCAR=24.8
Pod documented
20 0 my ( $class, $domain ) = @_;
21  
22 0 ▶ confess 'Argument must be a string: $domain'
Branch: 1 branch
BranchTrueFalseTotal
if not defined $domain or ref $domain ne "" 0 0 0
Condition0%0/3
not defined $domain or ref $domain ne ""
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $domain or ref $domain ne ""
not defined $domain ref $domain ne ""
Truth table0%0/3
not(A) or B
A= not defined $domainB= ref $domain ne ""
ABresult
1X1
011
000
23 if !defined $domain || ref $domain ne '';
24  
25 0 my $obj = Class::Accessor::new( $class, { labels => [ split( /[.]/x, $domain ) ] } );
26  
27 # We have the raw string, so we can precompute the string representation
28 # easily and cheaply so it can be immediately returned by the string()
29 # method instead of recomputing it from the labels list. The only thing we
30 # need to do is to remove any trailing dot except if it’s the only
31 # character.
32 0 $obj->{_string} = ( $domain =~ s/.\K [.] \z//rx );
33  
34 0 return $obj;
35 }
36  
37 0 ▶sub new {
Subroutine new: not called CC=12 SCAR=50.5
Pod documented
38 0 my ( $class, $input ) = @_;
39  
40 0 my $attrs = {};
41 0 ▶ if ( !defined $input ) {
Branch: 5 branches
BranchTrueFalseTotal
if (not defined $input) { } 0 0 0
elsif (builtin::blessed($input) and $input->isa('Zonemaster::Engine::DNSName')) { } 0 0 0
elsif (builtin::blessed($input) and $input->isa('Zonemaster::Engine::Zone')) { } 0 0 0
elsif (ref $input eq '') { } 0 0 0
elsif (ref $input eq 'HASH') { } 0 0 0
42 0 $attrs->{labels} = [];
43 }
44 ▶ elsif ( blessed $input && $input->isa( 'Zonemaster::Engine::DNSName' ) ) {
Condition0%0/3
builtin::blessed($input) and $input->isa("Zonemaster::Engine::DNSName")
!ll&&!rl&&r
000
MC/DC0%0/2
builtin::blessed($input) and $input->isa("Zonemaster::Engine::DNSName")
builtin::blessed($input) $input->isa("Zonemaster::Engine::DNSName")
Truth table0%0/3
A and B
A= builtin::blessed($input)B= $input->isa("Zonemaster::Engine::DNSName")
ABresult
0X0
100
111
45 0 $attrs->{labels} = \@{ $input->labels };
46 }
47 ▶ elsif ( blessed $input && $input->isa( 'Zonemaster::Engine::Zone' ) ) {
Condition0%0/3
builtin::blessed($input) and $input->isa("Zonemaster::Engine::Zone")
!ll&&!rl&&r
000
MC/DC0%0/2
builtin::blessed($input) and $input->isa("Zonemaster::Engine::Zone")
builtin::blessed($input) $input->isa("Zonemaster::Engine::Zone")
Truth table0%0/3
A and B
A= builtin::blessed($input)B= $input->isa("Zonemaster::Engine::Zone")
ABresult
0X0
100
111
48 0 $attrs->{labels} = \@{ $input->name->labels };
49 }
50 elsif ( ref $input eq '' ) {
51 0 $attrs->{labels} = [ split( /[.]/x, $input ) ];
52 }
53 elsif ( ref $input eq 'HASH' ) {
54 confess "Attribute \(labels\) is required"
55 0 ▶ if !exists $input->{labels};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $input->{"labels"} 0 0 0
56  
57 confess "Argument must be an ARRAYREF: labels"
58 if exists $input->{labels}
59 0 ▶ && ref $input->{labels} ne 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if exists $input->{"labels"} and ref $input->{"labels"} ne "ARRAY" 0 0 0
Condition0%0/3
exists $input->{"labels"} and ref $input->{"labels"} ne "ARRAY"
!ll&&!rl&&r
000
MC/DC0%0/2
exists $input->{"labels"} and ref $input->{"labels"} ne "ARRAY"
exists $input->{"labels"} ref $input->{"labels"} ne "ARRAY"
Truth table0%0/3
A and B
A= exists $input->{"labels"}B= ref $input->{"labels"} ne "ARRAY"
ABresult
0X0
100
111
60  
61 0 $attrs->{labels} = $input->{labels};
62 }
63 else {
64 0 ▶ my $what =
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($input) ? : 0 0 0
65 ( blessed $input )
66 ? "blessed(" . blessed $input . ")"
67 : "ref(" . ref $input . ")";
68 0 confess "Unrecognized argument: " . $what;
69 }
70  
71 0 return Class::Accessor::new( $class, $attrs );
72 }
73  
74 0 ▶sub string {
Subroutine string: not called CC=3 SCAR=24.8
Pod documented
75 0 my $self = shift;
76  
77 0 ▶ if ( not exists $self->{_string} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->{"_string"}) 0 0 0
78 0 my $string = join( '.', @{ $self->labels } );
79 0 ▶ $string = '.' if $string eq q{};
Branch: 1 branch
BranchTrueFalseTotal
if $string eq "" 0 0 0
80  
81 0 $self->{_string} = $string;
82 }
83  
84 0 return $self->{_string};
85 }
86  
87 0 ▶sub fqdn {
Subroutine fqdn: not called CC=1 SCAR=6.9
Pod documented
88 0 my ( $self ) = @_;
89  
90 0 return join( '.', @{ $self->labels } ) . '.';
91 }
92  
93 0 ▶sub str_cmp {
Subroutine str_cmp: not called CC=7 SCAR=40.3
Pod documented
94 # For performance reasons, we do not unpack @_.
95 # As a reminder, the calling convention is my ( $self, $other, $swap ) = @_.
96  
97 0 ▶ my $me = uc ( $_[0]->{_string} // $_[0]->string );
Condition0%0/3
$_[0]{"_string"} // $_[0]->string
l!l&&r!l&&!r
000
MC/DC0%0/2
$_[0]{"_string"} // $_[0]->string
$_[0]{"_string"} $_[0]->string
Truth table0%0/3
A // B
A= $_[0]{"_string"}B= $_[0]->string
ABresult
1X1
011
000
98  
99 # Treat undefined value as root
100 0 ▶ my $other = $_[1] // q{};
Condition0%0/2
$_[1] // ""
l!l
00
MC/DC0%0/1
$_[1] // ""
$_[1]
Truth table0%0/2
A
A= $_[1]
Aresult
11
00
101  
102 0 my $comparison = do {
103 0 ▶ if ( blessed $other and $other->isa( 'Zonemaster::Engine::DNSName' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($other) and $other->isa('Zonemaster::Engine::DNSName')) { } 0 0 0
Condition0%0/3
builtin::blessed($other) and $other->isa("Zonemaster::Engine::DNSName")
!ll&&!rl&&r
000
MC/DC0%0/2
builtin::blessed($other) and $other->isa("Zonemaster::Engine::DNSName")
builtin::blessed($other) $other->isa("Zonemaster::Engine::DNSName")
Truth table0%0/3
A and B
A= builtin::blessed($other)B= $other->isa("Zonemaster::Engine::DNSName")
ABresult
0X0
100
111
104 0 ▶ $me cmp uc( $other->{_string} // $other->string() );
Condition0%0/3
$other->{"_string"} // $other->string
l!l&&r!l&&!r
000
MC/DC0%0/2
$other->{"_string"} // $other->string
$other->{"_string"} $other->string
Truth table0%0/3
A // B
A= $other->{"_string"}B= $other->string
ABresult
1X1
011
000
105 }
106 else {
107 # Assume $other is a string; remove trailing dot except if only character
108 0 $me cmp uc( $other =~ s/.\K [.] \z//xr );
109 }
110 };
111  
112 0 ▶ return $_[2] ? -$comparison : $comparison;
Branch: 1 branch
BranchTrueFalseTotal
$_[2] ? : 0 0 0
113 }
114  
115 0 ▶sub next_higher {
Subroutine next_higher: not called CC=2 SCAR=17.9
Pod documented
116 0 my $self = shift;
117 0 my @l = @{ $self->labels };
118 0 ▶ if ( @l ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@l) { } 0 0 0
119 0 shift @l;
120 0 return Zonemaster::Engine::DNSName->new({ labels => \@l });
121 }
122 else {
123 0 return;
124 }
125 }
126  
127 0 ▶sub common {
Subroutine common: not called CC=4 SCAR=30.0
Pod documented
128 0 my ( $self, $other ) = @_;
129  
130 0 my @me = reverse @{ $self->labels };
131 0 my @them = reverse @{ $other->labels };
132  
133 0 my $count = 0;
134 0 ▶ while ( @me and @them ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@me and @them) 0 0 0
Condition0%0/3
@me and @them
!ll&&!rl&&r
000
MC/DC0%0/2
@me and @them
@me @them
Truth table0%0/3
A and B
A= @meB= @them
ABresult
0X0
100
111
135 0 my $m = shift @me;
136 0 my $t = shift @them;
137 0 ▶ if ( uc( $m ) eq uc( $t ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (uc $m eq uc $t) { } 0 0 0
138 0 $count += 1;
139 0 next;
140 }
141 else {
142 0 last;
143 }
144 }
145  
146 0 return $count;
147 } ## end sub common
148  
149 0 ▶sub is_in_bailiwick {
Subroutine is_in_bailiwick: not called CC=1 SCAR=6.9
Pod documented
150 0 my ( $self, $other ) = @_;
151  
152 0 return scalar( @{ $self->labels } ) == $self->common( $other );
153 }
154  
155 0 ▶sub prepend {
Subroutine prepend: not called CC=1 SCAR=6.9
Pod documented
156 0 my ( $self, $label ) = @_;
157 0 my @labels = ( $label, @{ $self->labels } );
158  
159 0 return $self->new( { labels => \@labels } );
160 }
161  
162 0 ▶sub TO_JSON {
Subroutine TO_JSON: not called CC=1 SCAR=6.9
Pod documented
163 0 my ( $self ) = @_;
164  
165 0 return $self->string;
166 }
167  
168 1 1;
169  
170 =head1 NAME
171  
172 Zonemaster::Engine::DNSName - class representing DNS names
173  
174 =head1 SYNOPSIS
175  
176 my $name1 = Zonemaster::Name->new('www.example.org');
177 my $name2 = Zonemaster::Name->new('ns.example.org');
178 say "Yay!" if $name1->common($name2) == 2;
179  
180 =head1 ATTRIBUTES
181  
182 =over
183  
184 =item labels
185  
186 A reference to a list of strings, being the labels the DNS name is made up from.
187  
188 =back
189  
190 =head1 METHODS
191  
192 =over
193  
194 =item new($input) _or_ new({ labels => \@labellist })
195  
196 The constructor can be called with either a single argument or with a reference
197 to a hash as in the example above.
198  
199 If there is a single argument, it must be either a non-reference, a
200 L<Zonemaster::Engine::DNSName> object or a L<Zonemaster::Engine::Zone> object.
201  
202 If it's a non-reference, it will be split at period characters (possibly after
203 stringification) and the resulting list used as the name's labels.
204  
205 If it's a L<Zonemaster::Engine::DNSName> object it will simply be returned.
206  
207 If it's a L<Zonemaster::Engine::Zone> object, the value of its C<name> attribute will
208 be returned.
209  
210 =item from_string($domain)
211  
212 A specialized constructor that must be called with a string.
213  
214 =item string()
215  
216 Returns a string representation of the name. The string representation is created by joining the labels with dots. If there are no labels, a
217 single dot is returned. The names created this way do not have a trailing dot.
218  
219 The stringification operator is overloaded to this function, so it should rarely be necessary to call it directly.
220  
221 =item fqdn()
222  
223 Returns the name as a string complete with a trailing dot.
224  
225 =item str_cmp($other)
226  
227 Overloads string comparison. Comparison is made after converting the names to upper case, and ignores any trailing dot on the other name.
228  
229 =item next_higher()
230  
231 Returns a new L<Zonemaster::Engine::DNSName> object, representing the name of the called one with the leftmost label removed.
232  
233 =item common($other)
234  
235 Returns the number of labels from the rightmost going left that are the same in both names. Used by the recursor to check for redirections going
236 up the DNS tree.
237  
238 =item is_in_bailiwick($other)
239  
240 Returns true if $other is in-bailiwick of $self, and false otherwise.
241 See also L<https://tools.ietf.org/html/rfc7719#section-6>.
242  
243 =item prepend($label)
244  
245 Returns a new L<Zonemaster::Engine::DNSName> object, representing the called one with the given label prepended.
246  
247 =item TO_JSON
248  
249 Helper method for JSON encoding.
250  
251 =back
252  
253 =cut
« Zonemaster/Engine/ASNLookup.pm ↑ Summary Zonemaster/Engine/Profile.pm »