Zonemaster/Backend/TestAgent.pm untested

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