Zonemaster/Engine/DNSName.pm

filter: Statementstmt 26.1% 23 / 88 Branchbran 0.0% 0 / 30 Conditioncond 0.0% 0 / 26 MC/DCmcdc 0.0% 0 / 17 Subroutinesub 41.1% 7 / 17 Podpod 100% 10 / 10 totaltotal 21.2% 40 / 188 CC 25 SCAR 40.3
CC
25
Cov
16%
new
156.0
str_cmp
42.0
common
20.0
CRAP 56.2
« Zonemaster/Engine/Test/Consistency.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=6 SCAR=37.4
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 ▶ 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
103 0 ▶ return $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
104 }
105 else {
106 # Assume $other is a string; remove trailing dot except if only character
107 0 return $me cmp uc( $other =~ s/.\K [.] \z//xr );
108 }
109 }
110  
111 0 ▶sub next_higher {
Subroutine next_higher: not called CC=2 SCAR=17.9
Pod documented
112 0 my $self = shift;
113 0 my @l = @{ $self->labels };
114 0 ▶ if ( @l ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@l) { } 0 0 0
115 0 shift @l;
116 0 return Zonemaster::Engine::DNSName->new({ labels => \@l });
117 }
118 else {
119 0 return;
120 }
121 }
122  
123 0 ▶sub common {
Subroutine common: not called CC=4 SCAR=30.0
Pod documented
124 0 my ( $self, $other ) = @_;
125  
126 0 my @me = reverse @{ $self->labels };
127 0 my @them = reverse @{ $other->labels };
128  
129 0 my $count = 0;
130 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
131 0 my $m = shift @me;
132 0 my $t = shift @them;
133 0 ▶ if ( uc( $m ) eq uc( $t ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (uc $m eq uc $t) { } 0 0 0
134 0 $count += 1;
135 0 next;
136 }
137 else {
138 0 last;
139 }
140 }
141  
142 0 return $count;
143 } ## end sub common
144  
145 0 ▶sub is_in_bailiwick {
Subroutine is_in_bailiwick: not called CC=1 SCAR=6.9
Pod documented
146 0 my ( $self, $other ) = @_;
147  
148 0 return scalar( @{ $self->labels } ) == $self->common( $other );
149 }
150  
151 0 ▶sub prepend {
Subroutine prepend: not called CC=1 SCAR=6.9
Pod documented
152 0 my ( $self, $label ) = @_;
153 0 my @labels = ( $label, @{ $self->labels } );
154  
155 0 return $self->new( { labels => \@labels } );
156 }
157  
158 0 ▶sub TO_JSON {
Subroutine TO_JSON: not called CC=1 SCAR=6.9
Pod documented
159 0 my ( $self ) = @_;
160  
161 0 return $self->string;
162 }
163  
164 1 1;
165  
166 =head1 NAME
167  
168 Zonemaster::Engine::DNSName - class representing DNS names
169  
170 =head1 SYNOPSIS
171  
172 my $name1 = Zonemaster::Name->new('www.example.org');
173 my $name2 = Zonemaster::Name->new('ns.example.org');
174 say "Yay!" if $name1->common($name2) == 2;
175  
176 =head1 ATTRIBUTES
177  
178 =over
179  
180 =item labels
181  
182 A reference to a list of strings, being the labels the DNS name is made up from.
183  
184 =back
185  
186 =head1 METHODS
187  
188 =over
189  
190 =item new($input) _or_ new({ labels => \@labellist })
191  
192 The constructor can be called with either a single argument or with a reference
193 to a hash as in the example above.
194  
195 If there is a single argument, it must be either a non-reference, a
196 L<Zonemaster::Engine::DNSName> object or a L<Zonemaster::Engine::Zone> object.
197  
198 If it's a non-reference, it will be split at period characters (possibly after
199 stringification) and the resulting list used as the name's labels.
200  
201 If it's a L<Zonemaster::Engine::DNSName> object it will simply be returned.
202  
203 If it's a L<Zonemaster::Engine::Zone> object, the value of its C<name> attribute will
204 be returned.
205  
206 =item from_string($domain)
207  
208 A specialized constructor that must be called with a string.
209  
210 =item string()
211  
212 Returns a string representation of the name. The string representation is created by joining the labels with dots. If there are no labels, a
213 single dot is returned. The names created this way do not have a trailing dot.
214  
215 The stringification operator is overloaded to this function, so it should rarely be necessary to call it directly.
216  
217 =item fqdn()
218  
219 Returns the name as a string complete with a trailing dot.
220  
221 =item str_cmp($other)
222  
223 Overloads string comparison. Comparison is made after converting the names to upper case, and ignores any trailing dot on the other name.
224  
225 =item next_higher()
226  
227 Returns a new L<Zonemaster::Engine::DNSName> object, representing the name of the called one with the leftmost label removed.
228  
229 =item common($other)
230  
231 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
232 up the DNS tree.
233  
234 =item is_in_bailiwick($other)
235  
236 Returns true if $other is in-bailiwick of $self, and false otherwise.
237 See also L<https://tools.ietf.org/html/rfc7719#section-6>.
238  
239 =item prepend($label)
240  
241 Returns a new L<Zonemaster::Engine::DNSName> object, representing the called one with the given label prepended.
242  
243 =item TO_JSON
244  
245 Helper method for JSON encoding.
246  
247 =back
248  
249 =cut
« Zonemaster/Engine/Test/Consistency.pm ↑ Summary Zonemaster/Engine/Profile.pm »