| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package Net::DNS::Resolver::Mock; | 
| 2 |  |  |  |  |  |  |  | 
| 3 | 3 |  |  | 3 |  | 207641 | use strict; | 
|  | 3 |  |  |  |  | 28 |  | 
|  | 3 |  |  |  |  | 85 |  | 
| 4 | 3 |  |  | 3 |  | 15 | use warnings; | 
|  | 3 |  |  |  |  | 7 |  | 
|  | 3 |  |  |  |  | 164 |  | 
| 5 |  |  |  |  |  |  |  | 
| 6 |  |  |  |  |  |  | our $VERSION = '1.20200215'; # VERSION | 
| 7 |  |  |  |  |  |  |  | 
| 8 | 3 |  |  | 3 |  | 20 | use base 'Net::DNS::Resolver'; | 
|  | 3 |  |  |  |  | 6 |  | 
|  | 3 |  |  |  |  | 1733 |  | 
| 9 |  |  |  |  |  |  |  | 
| 10 | 3 |  |  | 3 |  | 273787 | use Net::DNS::Packet; | 
|  | 3 |  |  |  |  | 22 |  | 
|  | 3 |  |  |  |  | 208 |  | 
| 11 | 3 |  |  | 3 |  | 38 | use Net::DNS::Question; | 
|  | 3 |  |  |  |  | 20 |  | 
|  | 3 |  |  |  |  | 198 |  | 
| 12 | 3 |  |  | 3 |  | 2828 | use Net::DNS::ZoneFile; | 
|  | 3 |  |  |  |  | 28266 |  | 
|  | 3 |  |  |  |  | 2280 |  | 
| 13 |  |  |  |  |  |  |  | 
| 14 |  |  |  |  |  |  | my $die_on = {}; | 
| 15 |  |  |  |  |  |  | { | 
| 16 |  |  |  |  |  |  | my @_debug_output; | 
| 17 |  |  |  |  |  |  | sub enable_debug { | 
| 18 | 1 |  |  | 1 | 1 | 563 | my ( $self ) = @_; | 
| 19 | 1 |  |  |  |  | 3 | $self->{_mock_debug} = 1; | 
| 20 | 1 |  |  |  |  | 5 | $self->_add_debug( "Net::DNS::Resolver::Mock Debugging enabled" ); | 
| 21 | 1 |  |  |  |  | 3 | return; | 
| 22 |  |  |  |  |  |  | } | 
| 23 |  |  |  |  |  |  | sub disable_debug { | 
| 24 | 1 |  |  | 1 | 0 | 536 | my ( $self ) = @_; | 
| 25 | 1 |  |  |  |  | 6 | $self->clear_debug(); | 
| 26 | 1 |  |  |  |  | 2 | delete $self->{_mock_debug}; | 
| 27 | 1 |  |  |  |  | 2 | return; | 
| 28 |  |  |  |  |  |  | } | 
| 29 |  |  |  |  |  |  | sub _add_debug { | 
| 30 | 4 |  |  | 4 |  | 10 | my ( $self, $debug ) = @_; | 
| 31 | 4 |  |  |  |  | 8 | push @_debug_output, $debug; | 
| 32 | 4 |  |  |  |  | 58 | warn $debug; | 
| 33 | 4 |  |  |  |  | 17 | return; | 
| 34 |  |  |  |  |  |  | } | 
| 35 |  |  |  |  |  |  | sub clear_debug { | 
| 36 | 1 |  |  | 1 | 1 | 3 | my ( $self ) = @_; | 
| 37 | 1 |  |  |  |  | 3 | @_debug_output = (); | 
| 38 | 1 |  |  |  |  | 2 | return; | 
| 39 |  |  |  |  |  |  | } | 
| 40 |  |  |  |  |  |  | sub get_debug { | 
| 41 | 5 |  |  | 5 | 1 | 2979 | my ( $self ) = @_; | 
| 42 | 5 |  |  |  |  | 19 | return @_debug_output; | 
| 43 |  |  |  |  |  |  | } | 
| 44 |  |  |  |  |  |  | } | 
| 45 |  |  |  |  |  |  |  | 
| 46 |  |  |  |  |  |  | sub die_on { | 
| 47 | 1 |  |  | 1 | 1 | 1152 | my ( $self, $name, $type, $error ) = @_; | 
| 48 | 1 |  |  |  |  | 5 | $die_on->{ "$name $type" } = $error; | 
| 49 | 1 |  |  |  |  | 3 | return; | 
| 50 |  |  |  |  |  |  | } | 
| 51 |  |  |  |  |  |  |  | 
| 52 |  |  |  |  |  |  | sub zonefile_read { | 
| 53 | 1 |  |  | 1 | 1 | 2240 | my ( $self, $zonefile ) = @_; | 
| 54 | 1 |  |  |  |  | 8 | $self->{ 'zonefile' } = Net::DNS::ZoneFile->read( $zonefile ); | 
| 55 | 1 |  |  |  |  | 3263 | return; | 
| 56 |  |  |  |  |  |  | } | 
| 57 |  |  |  |  |  |  |  | 
| 58 |  |  |  |  |  |  | sub zonefile_parse { | 
| 59 | 2 |  |  | 2 | 1 | 2306 | my ( $self, $zonefile ) = @_; | 
| 60 | 2 |  |  |  |  | 22 | $self->{ 'zonefile' } = Net::DNS::ZoneFile->parse( $zonefile ); | 
| 61 | 2 |  |  |  |  | 3394 | return; | 
| 62 |  |  |  |  |  |  | } | 
| 63 |  |  |  |  |  |  |  | 
| 64 |  |  |  |  |  |  | sub send { | 
| 65 | 15 |  |  | 15 | 1 | 9576 | my ( $self, $name, $type ) = @_; | 
| 66 |  |  |  |  |  |  |  | 
| 67 | 15 | 100 |  |  |  | 61 | $self->_add_debug( "DNS Lookup '$name' '$type'" ) if $self->{_mock_debug}; | 
| 68 |  |  |  |  |  |  |  | 
| 69 | 15 | 100 |  |  |  | 57 | if ( exists ( $die_on->{ "$name $type" } ) ) { | 
| 70 | 1 |  |  |  |  | 12 | die $die_on->{ "$name $type" }; | 
| 71 |  |  |  |  |  |  | } | 
| 72 |  |  |  |  |  |  |  | 
| 73 | 14 |  |  |  |  | 41 | $name =~ s/\.$//; | 
| 74 |  |  |  |  |  |  |  | 
| 75 | 14 |  |  |  |  | 27 | my $FakeZone = $self->{ 'zonefile' }; | 
| 76 |  |  |  |  |  |  |  | 
| 77 | 14 |  |  |  |  | 23 | my $origname = $name; | 
| 78 | 14 | 100 |  |  |  | 41 | if ( lc $type eq 'ptr' ) { | 
| 79 | 4 | 100 |  |  |  | 17 | if ( index( lc $name, '.in-addr.arpa' )  == -1 ) { | 
| 80 | 3 | 100 |  |  |  | 14 | if ( $name =~ /^\d+\.\d+\.\d+\.\d+$/ ) { | 
| 81 | 2 |  |  |  |  | 10 | $name = join( '.', reverse( split( /\./, $name ) ) ); | 
| 82 | 2 |  |  |  |  | 7 | $name .= '.in-addr.arpa'; | 
| 83 |  |  |  |  |  |  | } | 
| 84 |  |  |  |  |  |  | } | 
| 85 |  |  |  |  |  |  | } | 
| 86 | 14 |  |  |  |  | 75 | my $Packet = Net::DNS::Packet->new(); | 
| 87 | 14 |  |  |  |  | 212 | $Packet->push( 'question' => Net::DNS::Question->new( $origname, $type, 'IN' ) ); | 
| 88 | 14 |  |  |  |  | 1104 | foreach my $Item ( @$FakeZone ) { | 
| 89 | 31 |  |  |  |  | 132 | my $itemname = $Item->name(); | 
| 90 | 31 |  |  |  |  | 590 | my $itemtype = $Item->type(); | 
| 91 | 31 | 100 | 100 |  |  | 376 | if ( ( lc $itemname eq lc $name ) && ( lc $itemtype eq lc $type ) ) { | 
|  |  | 100 | 66 |  |  |  |  | 
| 92 | 5 |  |  |  |  | 11 | $Packet->push( 'answer' => $Item ); | 
| 93 |  |  |  |  |  |  | } | 
| 94 |  |  |  |  |  |  | elsif ( ( lc $itemname eq lc $name ) && ( lc $itemtype eq lc 'cname' ) ) { | 
| 95 | 1 |  |  |  |  | 3 | $Packet->push( 'answer' => $Item ); | 
| 96 |  |  |  |  |  |  | } | 
| 97 |  |  |  |  |  |  | } | 
| 98 | 14 |  |  |  |  | 57 | $Packet->{ 'answerfrom' } = '127.0.0.1'; | 
| 99 | 14 |  |  |  |  | 23 | $Packet->{ 'status' } = 33152; | 
| 100 | 14 |  |  |  |  | 37 | return $Packet; | 
| 101 |  |  |  |  |  |  | } | 
| 102 |  |  |  |  |  |  |  | 
| 103 |  |  |  |  |  |  | 1; | 
| 104 |  |  |  |  |  |  |  | 
| 105 |  |  |  |  |  |  | __END__ |