blib/lib/Net/DNS/Resolver/Mock.pm

filter: Statementstmt 100% 87 / 87 Branchbran 100% 18 / 18 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 16 / 16 Podpod 77.7% 7 / 9 totaltotal 98.4% 128 / 130 CC 11 SCAR 13.9
CC
11
Cov
100%
send
9.0
build_cache
3.0
BEGIN
1.0
CRAP 4.0
↑ Summary
1 package Net::DNS::Resolver::Mock;
2  
3 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 3 our $VERSION = '1.20230216'; # VERSION
7  
8 3 ▶use base 'Net::DNS::Resolver';
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 3 ▶use Net::DNS::Packet;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 3 ▶use Net::DNS::Question;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 3 ▶use Net::DNS::ZoneFile;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 3 my $die_on = {};
15 {
16 3 my @_debug_output;
17 1 ▶ sub enable_debug {
Subroutine enable_debug: called CC=1 SCAR=0.0
Pod documented
18 1 my ( $self ) = @_;
19 1 $self->{_mock_debug} = 1;
20 1 $self->_add_debug( "Net::DNS::Resolver::Mock Debugging enabled" );
21 1 return;
22 }
23 1 ▶ sub disable_debug {
Subroutine disable_debug: called CC=1 SCAR=0.0
Pod undocumented
24 1 my ( $self ) = @_;
25 1 $self->clear_debug();
26 1 delete $self->{_mock_debug};
27 1 return;
28 }
29 4 ▶ sub _add_debug {
Subroutine _add_debug: called CC=1 SCAR=0.0
30 4 my ( $self, $debug ) = @_;
31 4 push @_debug_output, $debug;
32 4 warn $debug;
33 4 return;
34 }
35 1 ▶ sub clear_debug {
Subroutine clear_debug: called CC=1 SCAR=0.0
Pod documented
36 1 my ( $self ) = @_;
37 1 @_debug_output = ();
38 1 return;
39 }
40 5 ▶ sub get_debug {
Subroutine get_debug: called CC=1 SCAR=0.0
Pod documented
41 5 my ( $self ) = @_;
42 5 return @_debug_output;
43 }
44 }
45  
46 3 ▶sub die_on {
Subroutine die_on: called CC=1 SCAR=0.0
Pod documented
47 1 my ( $self, $name, $type, $error ) = @_;
48 1 $die_on->{ "$name $type" } = $error;
49 1 return;
50 }
51  
52 4 ▶sub build_cache {
Subroutine build_cache: called CC=3 SCAR=11.0
Pod undocumented
53 4 my ( $self ) = @_;
54 4 my $cache = {};
55 4 my $FakeZone = $self->{ 'zonefile' };
56 4 foreach my $Item ( @$FakeZone ) {
57 9 my $itemname = lc $Item->name();
58 9 my $itemtype = lc $Item->type();
59 9 my $key = join( ':', $itemname, $itemtype );
60 9 ▶ if ( ! exists $cache->{$key} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $cache->{$key}) 8 1 9
61 8 $cache->{$key} = [];
62 }
63 9 push @{ $cache->{$key} }, $Item;
64 }
65 4 $self->{ 'zonefile_cache' } = $cache;
66 4 return;
67 }
68  
69 1 ▶sub zonefile_read {
Subroutine zonefile_read: called CC=1 SCAR=0.0
Pod documented
70 1 my ( $self, $zonefile ) = @_;
71 1 $self->{ 'zonefile' } = Net::DNS::ZoneFile->read( $zonefile );
72 1 $self->build_cache();
73 1 return;
74 }
75  
76 3 ▶sub zonefile_parse {
Subroutine zonefile_parse: called CC=1 SCAR=0.0
Pod documented
77 3 my ( $self, $zonefile ) = @_;
78 3 $self->{ 'zonefile' } = Net::DNS::ZoneFile->parse( $zonefile );
79 3 $self->build_cache();
80 3 return;
81 }
82  
83 17 ▶sub send {
Subroutine send: called CC=9 SCAR=22.0
Pod documented
84 17 my ( $self, $name, $type ) = @_;
85  
86 17 ▶ $self->_add_debug( "DNS Lookup '$name' '$type'" ) if $self->{_mock_debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_mock_debug"} 3 14 17
87  
88 17 ▶ if ( exists ( $die_on->{ "$name $type" } ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $$die_on{"$name $type"}) 1 16 17
89 1 die $die_on->{ "$name $type" };
90 }
91  
92 16 ▶ $name =~ s/\.$// unless $name eq '.';
Branch: 1 branch
BranchTrueFalseTotal
unless $name eq "." 15 1 16
93  
94 16 my $origname = $name;
95 16 ▶ if ( lc $type eq 'ptr' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (lc $type eq "ptr") 4 12 16
96 4 ▶ if ( index( lc $name, '.in-addr.arpa' ) == -1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (index(lc $name, ".in-addr.arpa") == -1) 3 1 4
97 3 ▶ if ( $name =~ /^\d+\.\d+\.\d+\.\d+$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($name =~ /^\d+\.\d+\.\d+\.\d+$/) 2 1 3
98 2 $name = join( '.', reverse( split( /\./, $name ) ) );
99 2 $name .= '.in-addr.arpa';
100 }
101 }
102 }
103 16 my $Packet = Net::DNS::Packet->new();
104 16 $Packet->push( 'question' => Net::DNS::Question->new( $origname, $type, 'IN' ) );
105 16 my $key = join( ':', lc $name, lc $type );
106 16 my $cname_key = join( ':', lc $name, 'cname' );
107 16 ▶ if ( exists( $self->{ 'zonefile_cache' }->{ $cname_key } ) ) {
Branch: 2 branches
BranchTrueFalseTotal
if (exists $self->{'zonefile_cache'}{$cname_key}) { } 1 15 16
elsif (exists $self->{'zonefile_cache'}{$key}) { } 7 8 15
108 1 $Packet->push( 'answer' => @{ $self->{ 'zonefile_cache' }->{ $cname_key } } );
109 } elsif ( exists( $self->{ 'zonefile_cache' }->{ $key } ) ) {
110 7 $Packet->push( 'answer' => @{ $self->{ 'zonefile_cache' }->{ $key } } );
111 }
112  
113 16 $Packet->{ 'answerfrom' } = '127.0.0.1';
114 16 $Packet->{ 'status' } = 33152;
115 16 return $Packet;
116 }
117  
118 3 1;
119  
120 __END__
↑ Summary