Zonemaster/Backend/TestAgent.pm untested

filter: Statementstmt 0.0% 0 / 154 Branchbran 0.0% 0 / 42 Conditioncond 0.0% 0 / 18 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 23 Podpod 0.0% 0 / 6 totaltotal 0.0% 0 / 243 CC 22 SCAR 44.6
CC
22
Cov
0%
run
240.0
add_fake_delegation
20.0
new
12.0
CRAP 86.1
1 package Zonemaster::Backend::TestAgent;
2 our $VERSION = '1.1.0';
3  
4 use strict;
5 use warnings;
6 use 5.14.2;
7  
8 use DBI qw(:utils);
9 use JSON::PP;
10 use Scalar::Util qw( blessed );
11 use File::Slurp;
12 use Locale::TextDomain qw[Zonemaster-Backend];
13 use Time::HiRes qw[time sleep gettimeofday tv_interval];
14  
15 use Zonemaster::LDNS;
16  
17 use Zonemaster::Engine;
18 use Zonemaster::Engine::Translator;
19 use Zonemaster::Engine::Profile;
20 use Zonemaster::Engine::Util;
21 use Zonemaster::Engine::Logger::Entry;
22 use Zonemaster::Backend::Config;
23 use Zonemaster::Backend::Metrics;
24  
25 sub new {
26 my ( $class, $params ) = @_;
27 my $self = {};
28  
29 if ( !$params || !$params->{config} ) {
30 die "missing 'config' parameter";
31 }
32  
33 my $config = $params->{config};
34  
35 my $dbtype;
36 if ( $params->{dbtype} ) {
37 $dbtype = $config->check_db( $params->{dbtype} );
38 }
39 else {
40 $dbtype = $config->DB_engine;
41 }
42  
43 my $dbclass = Zonemaster::Backend::DB->get_db_class( $dbtype );
44 $self->{_db} = $dbclass->from_config( $config );
45  
46 $self->{_profiles} = Zonemaster::Backend::Config->load_profiles( #
47 $config->PUBLIC_PROFILES,
48 $config->PRIVATE_PROFILES,
49 );
50  
51 bless( $self, $class );
52 return $self;
53 }
54  
55 sub run {
56 my ( $self, $test_id, $show_progress ) = @_;
57 my @accumulator;
58  
59 my $params;
60  
61 $params = $self->{_db}->get_test_params( $test_id );
62  
63 my ( $domain ) = $params->{domain};
64 if ( !$domain ) {
65 die "Must give the name of a domain to test.\n";
66 }
67 $domain = $self->to_idn( $domain );
68 my %numeric = Zonemaster::Engine::Logger::Entry->levels();
69  
70 if ( $params->{nameservers} && @{ $params->{nameservers} } > 0 ) {
71 $self->add_fake_delegation( $domain, $params->{nameservers} );
72 }
73  
74 if ( $params->{ds_info} && @{ $params->{ds_info} } > 0 ) {
75 $self->add_fake_ds( $domain, $params->{ds_info} );
76 }
77  
78 # If the profile parameter has been set in the API, then load a profile
79 if ( $params->{profile} ) {
80 $params->{profile} = lc($params->{profile});
81 if ( defined $self->{_profiles}{ $params->{profile} } ) {
82 Zonemaster::Engine::Profile->effective->merge( $self->{_profiles}{ $params->{profile} } );
83 }
84 else {
85 die "The profile [$params->{profile}] is not defined in the backend_config ini file";
86 }
87 }
88  
89 # If IPv4 or IPv6 transport has been explicitly disabled or enabled, then load it after
90 # any explicitly set profile has been loaded.
91 if (defined $params->{ipv4}) {
92 Zonemaster::Engine::Profile->effective->set( q{net.ipv4}, ( $params->{ipv4} ) ? ( 1 ) : ( 0 ) );
93 }
94  
95 if (defined $params->{ipv6}) {
96 Zonemaster::Engine::Profile->effective->set( q{net.ipv6}, ( $params->{ipv6} ) ? ( 1 ) : ( 0 ) );
97 }
98  
99 if ( $show_progress ) {
100 my %methods = Zonemaster::Engine->all_methods;
101  
102 # BASIC methods are always run: Basic0{0..4}
103 my $nbr_testcases_planned = 5;
104 my $nbr_testcases_finished = 0;
105  
106 foreach my $module ( keys %methods ) {
107 foreach my $method ( @{ $methods{$module} } ) {
108 if ( Zonemaster::Engine::Util::should_run_test( $method ) ) {
109 $nbr_testcases_planned++;
110 }
111 }
112 }
113  
114 Zonemaster::Engine->logger->callback(
115 sub {
116 my ( $entry ) = @_;
117  
118 if ( $entry->{tag} and $entry->{tag} eq 'TEST_CASE_END' ) {
119 $nbr_testcases_finished++;
120 my $progress_percent = int( 100 * $nbr_testcases_finished / $nbr_testcases_planned );
121 $self->{_db}->test_progress( $test_id, $progress_percent );
122 }
123 }
124 );
125 }
126  
127 # Actually run tests!
128 eval { Zonemaster::Engine->test_zone( $domain ); };
129 if ( $@ ) {
130 my $err = $@;
131 if ( blessed $err and $err->isa( "NormalExit" ) ) {
132 say STDERR "Exited early: " . $err->message;
133 }
134 else {
135 die "$err\n"; # Don't know what it is, rethrow
136 }
137 }
138  
139 my $insert_result_start_time = [ gettimeofday ];
140  
141 # TODO: Make minimum level configurable
142 my @entries = grep { $_->numeric_level >= $numeric{INFO} } @{ Zonemaster::Engine->logger->entries };
143  
144 Zonemaster::Backend::Metrics::timing("zonemaster.testagent.log_callback_add_result_entry_filter_duration", tv_interval($insert_result_start_time) * 1000);
145  
146 $self->{_db}->add_result_entries( $test_id, @entries);
147  
148 my $callback_add_result_entry_duration = tv_interval($insert_result_start_time);
149 Zonemaster::Backend::Metrics::timing("zonemaster.testagent.log_callback_add_result_entry_duration", $callback_add_result_entry_duration * 1000);
150  
151 $self->{_db}->set_test_completed( $test_id );
152  
153 return;
154 } ## end sub run
155  
156 sub reset {
157 my ( $self ) = @_;
158 Zonemaster::Engine->reset();
159 }
160  
161 sub add_fake_delegation {
162 my ( $self, $domain, $nameservers ) = @_;
163 my @ns_with_no_ip;
164 my %data;
165  
166 foreach my $ns_ip_pair ( @$nameservers ) {
167 if ( $ns_ip_pair->{ns} && $ns_ip_pair->{ip} ) {
168 push( @{ $data{ $self->to_idn( $ns_ip_pair->{ns} ) } }, $ns_ip_pair->{ip} );
169 }
170 elsif ($ns_ip_pair->{ns}) {
171 push(@ns_with_no_ip, $self->to_idn( $ns_ip_pair->{ns} ) );
172 }
173 else {
174 die "Invalid ns_ip_pair";
175 }
176 }
177  
178 foreach my $ns ( @ns_with_no_ip ) {
179 if ( not exists $data{ $ns } ) {
180 $data{ $self->to_idn( $ns ) } = undef;
181 }
182 }
183  
184 Zonemaster::Engine->add_fake_delegation( $domain => \%data );
185  
186 return;
187 }
188  
189 sub add_fake_ds {
190 my ( $self, $domain, $ds_info ) = @_;
191 my @data;
192  
193 foreach my $ds ( @{ $ds_info } ) {
194 push @data, { keytag => $ds->{keytag}, algorithm => $ds->{algorithm}, type => $ds->{digtype}, digest => $ds->{digest} };
195 }
196  
197 Zonemaster::Engine->add_fake_ds( $domain => \@data );
198  
199 return;
200 }
201  
202 sub to_idn {
203 my ( $self, $str ) = @_;
204  
205 if ( $str =~ m/^[[:ascii:]]+$/ ) {
206 return $str;
207 }
208  
209 if ( Zonemaster::LDNS::has_idn() ) {
210 return Zonemaster::LDNS::to_idn( $str );
211 }
212 else {
213 warn __( "Warning: Zonemaster::LDNS not compiled with IDN support, cannot handle non-ASCII names correctly." );
214 return $str;
215 }
216 }
217  
218 1;