| 1 | package Zonemaster::Test v1.0.0; | ||
| 2 | |||
| 3 | use 5.14.2; | ||
| 4 | use strict; | ||
| 5 | use warnings; | ||
| 6 | |||
| 7 | use Zonemaster; | ||
| 8 | use Zonemaster::Util; | ||
| 9 | use Zonemaster::Test::Basic; | ||
| 10 | |||
| 11 | use IO::Socket::INET6; # Lazy-loads, so make sure it's here for the version logging | ||
| 12 | |||
| 13 | use Module::Find qw[useall]; | ||
| 14 | use Scalar::Util qw[blessed]; | ||
| 15 | use POSIX qw[strftime]; | ||
| 16 | |||
| 17 | my @all_test_modules; | ||
| 18 | |||
| 19 | @all_test_modules = | ||
| 20 | sort { $a cmp $b } | ||
| 21 | map { my $f = $_; $f =~ s|^Zonemaster::Test::||; $f } | ||
| 22 | grep { $_ ne 'Zonemaster::Test::Basic' } useall( 'Zonemaster::Test' ); | ||
| 23 | |||
| 24 | sub _log_versions { | ||
| 25 | info( GLOBAL_VERSION => { version => Zonemaster->VERSION } ); | ||
| 26 | |||
| 27 | info( DEPENDENCY_VERSION => { name => 'Net::LDNS', version => $Net::LDNS::VERSION } ); | ||
| 28 | info( DEPENDENCY_VERSION => { name => 'IO::Socket::INET6', version => $IO::Socket::INET6::VERSION } ); | ||
| 29 | info( DEPENDENCY_VERSION => { name => 'Moose', version => $Moose::VERSION } ); | ||
| 30 | info( DEPENDENCY_VERSION => { name => 'Module::Find', version => $Module::Find::VERSION } ); | ||
| 31 | info( DEPENDENCY_VERSION => { name => 'JSON', version => $JSON::VERSION } ); | ||
| 32 | info( DEPENDENCY_VERSION => { name => 'File::ShareDir', version => $File::ShareDir::VERSION } ); | ||
| 33 | info( DEPENDENCY_VERSION => { name => 'File::Slurp', version => $File::Slurp::VERSION } ); | ||
| 34 | info( DEPENDENCY_VERSION => { name => 'Net::IP::XS', version => $Net::IP::XS::VERSION } ); | ||
| 35 | info( DEPENDENCY_VERSION => { name => 'List::MoreUtils', version => $List::MoreUtils::VERSION } ); | ||
| 36 | info( DEPENDENCY_VERSION => { name => 'Mail::RFC822::Address', version => $Mail::RFC822::Address::VERSION } ); | ||
| 37 | info( DEPENDENCY_VERSION => { name => 'Scalar::Util', version => $Scalar::Util::VERSION } ); | ||
| 38 | info( DEPENDENCY_VERSION => { name => 'Hash::Merge', version => $Hash::Merge::VERSION } ); | ||
| 39 | info( DEPENDENCY_VERSION => { name => 'Readonly', version => $Readonly::VERSION } ); | ||
| 40 | |||
| 41 | foreach my $file ( @{ Zonemaster->config->cfiles } ) { | ||
| 42 | info( CONFIG_FILE => { name => $file } ); | ||
| 43 | } | ||
| 44 | foreach my $file ( @{ Zonemaster->config->pfiles } ) { | ||
| 45 | info( POLICY_FILE => { name => $file } ); | ||
| 46 | } | ||
| 47 | } ## end sub _log_versions | ||
| 48 | |||
| 49 | sub modules { | ||
| 50 | return @all_test_modules; | ||
| 51 | } | ||
| 52 | |||
| 53 | sub run_all_for { | ||
| 54 | my ( $class, $zone ) = @_; | ||
| 55 | my @results; | ||
| 56 | |||
| 57 | Zonemaster->start_time_now(); | ||
| 58 | push @results, info( START_TIME => { time_t => time(), string => strftime("%F %T %z", (localtime())) } ); | ||
| 59 | push @results, info( TEST_TARGET => { zone => $zone->name->string, module => 'all' } ); | ||
| 60 | |||
| 61 | info( | ||
| 62 | MODULE_VERSION => { | ||
| 63 | module => 'Zonemaster::Test::Basic', | ||
| 64 | version => Zonemaster::Test::Basic->version | ||
| 65 | } | ||
| 66 | ); | ||
| 67 | _log_versions(); | ||
| 68 | |||
| 69 | if ( not( Zonemaster->config->ipv4_ok or Zonemaster->config->ipv6_ok ) ) { | ||
| 70 | return info( NO_NETWORK => {} ); | ||
| 71 | } | ||
| 72 | |||
| 73 | push @results, Zonemaster::Test::Basic->all( $zone ); | ||
| 74 | info( MODULE_END => { module => 'Zonemaster::Test::Basic' } ); | ||
| 75 | |||
| 76 | if ( Zonemaster::Test::Basic->can_continue( @results ) ) { | ||
| 77 | ## no critic (Modules::RequireExplicitInclusion) | ||
| 78 | foreach my $mod ( __PACKAGE__->modules ) { | ||
| 79 | Zonemaster->config->load_module_policy( $mod ); | ||
| 80 | |||
| 81 | if ( not _policy_allowed( $mod ) ) { | ||
| 82 | push @results, info( POLICY_DISABLED => { name => $mod } ); | ||
| 83 | next; | ||
| 84 | } | ||
| 85 | |||
| 86 | my $module = "Zonemaster::Test::$mod"; | ||
| 87 | info( MODULE_VERSION => { module => $module, version => $module->version } ); | ||
| 88 | my @res = eval { $module->all( $zone ) }; | ||
| 89 | if ( $@ ) { | ||
| 90 | my $err = $@; | ||
| 91 | if ( blessed $err and $err->isa( 'Zonemaster::Exception' ) ) { | ||
| 92 | die $err; # Utility exception, pass it on | ||
| 93 | } | ||
| 94 | else { | ||
| 95 | push @res, info( MODULE_ERROR => { module => $module, msg => "$err" } ); | ||
| 96 | } | ||
| 97 | } | ||
| 98 | info( MODULE_END => { module => $module } ); | ||
| 99 | |||
| 100 | push @results, @res; | ||
| 101 | } ## end foreach my $mod ( __PACKAGE__...) | ||
| 102 | } ## end if ( Zonemaster::Test::Basic...) | ||
| 103 | else { | ||
| 104 | push @results, info( CANNOT_CONTINUE => { zone => $zone->name->string } ); | ||
| 105 | } | ||
| 106 | |||
| 107 | return @results; | ||
| 108 | } ## end sub run_all_for | ||
| 109 | |||
| 110 | sub run_module { | ||
| 111 | my ( $class, $requested, $zone ) = @_; | ||
| 112 | my @res; | ||
| 113 | my ( $module ) = grep { lc( $requested ) eq lc( $_ ) } $class->modules; | ||
| 114 | $module = 'Basic' if ( not $module and lc( $requested ) eq 'basic' ); | ||
| 115 | |||
| 116 | Zonemaster->start_time_now(); | ||
| 117 | push @res, info( START_TIME => { time_t => time(), string => strftime("%F %T %z", (localtime())) } ); | ||
| 118 | push @res, info( TEST_TARGET => { zone => $zone->name->string, module => $requested } ); | ||
| 119 | _log_versions(); | ||
| 120 | if ( not( Zonemaster->config->ipv4_ok or Zonemaster->config->ipv6_ok ) ) { | ||
| 121 | return info( NO_NETWORK => {} ); | ||
| 122 | } | ||
| 123 | |||
| 124 | if ( $module ) { | ||
| 125 | Zonemaster->config->load_module_policy( $module ); | ||
| 126 | my $m = "Zonemaster::Test::$module"; | ||
| 127 | info( MODULE_VERSION => { module => $m, version => $m->version } ); | ||
| 128 | push @res, eval { $m->all( $zone ) }; | ||
| 129 | if ( $@ ) { | ||
| 130 | my $err = $@; | ||
| 131 | if ( blessed $err and $err->isa( 'Zonemaster::Exception' ) ) { | ||
| 132 | die $err; # Utility exception, pass it on | ||
| 133 | } | ||
| 134 | else { | ||
| 135 | push @res, info( MODULE_ERROR => { module => $module, msg => "$err" } ); | ||
| 136 | } | ||
| 137 | } | ||
| 138 | info( MODULE_END => { module => $module } ); | ||
| 139 | return @res; | ||
| 140 | } | ||
| 141 | else { | ||
| 142 | info( UNKNOWN_MODULE => { name => $requested, method => 'all', known => join( ':', sort $class->modules ) } ); | ||
| 143 | } | ||
| 144 | |||
| 145 | return; | ||
| 146 | } ## end sub run_module | ||
| 147 | |||
| 148 | sub run_one { | ||
| 149 | my ( $class, $requested, $test, @arguments ) = @_; | ||
| 150 | my @res; | ||
| 151 | my ( $module ) = grep { lc( $requested ) eq lc( $_ ) } $class->modules; | ||
| 152 | $module = 'Basic' if ( not $module and lc( $requested ) eq 'basic' ); | ||
| 153 | |||
| 154 | Zonemaster->start_time_now(); | ||
| 155 | push @res, info( START_TIME => { time_t => time(), string => strftime("%F %T %z", (localtime())) } ); | ||
| 156 | push @res, info( TEST_ARGS => { module => $requested, method => $test, args => join(';', map {"$_"} @arguments ) } ); | ||
| 157 | _log_versions(); | ||
| 158 | if ( not( Zonemaster->config->ipv4_ok or Zonemaster->config->ipv6_ok ) ) { | ||
| 159 | return info( NO_NETWORK => {} ); | ||
| 160 | } | ||
| 161 | |||
| 162 | if ( $module ) { | ||
| 163 | Zonemaster->config->load_module_policy( $module ); | ||
| 164 | my $m = "Zonemaster::Test::$module"; | ||
| 165 | if ( $m->metadata->{$test} ) { | ||
| 166 | info( MODULE_CALL => { module => $module, method => $test, version => $m->version } ); | ||
| 167 | push @res, eval { $m->$test( @arguments ) }; | ||
| 168 | if ( $@ ) { | ||
| 169 | my $err = $@; | ||
| 170 | if ( blessed $err and $err->isa( 'Zonemaster::Exception' ) ) { | ||
| 171 | die $err; # Utility exception, pass it on | ||
| 172 | } | ||
| 173 | else { | ||
| 174 | push @res, info( MODULE_ERROR => { module => $module, msg => "$err" } ); | ||
| 175 | } | ||
| 176 | } | ||
| 177 | info( MODULE_CALL_END => { module => $module, method => $test } ); | ||
| 178 | return @res; | ||
| 179 | } | ||
| 180 | else { | ||
| 181 | info( UNKNOWN_METHOD => { module => $m, method => $test } ); | ||
| 182 | } | ||
| 183 | } ## end if ( $module ) | ||
| 184 | else { | ||
| 185 | info( UNKNOWN_MODULE => { module => $requested, method => $test, known => join( ':', sort $class->modules ) } ); | ||
| 186 | } | ||
| 187 | |||
| 188 | return; | ||
| 189 | } ## end sub run_one | ||
| 190 | |||
| 191 | sub _policy_allowed { | ||
| 192 | my ( $name ) = @_; | ||
| 193 | |||
| 194 | return not Zonemaster::Util::policy()->{ uc( $name ) }{DISABLED}; | ||
| 195 | } | ||
| 196 | |||
| 197 | 1; | ||
| 198 | |||
| 199 | =head1 NAME | ||
| 200 | |||
| 201 | Zonemaster::Test - module to find, load and execute all test modules | ||
| 202 | |||
| 203 | =head1 SYNOPSIS | ||
| 204 | |||
| 205 | my @results = Zonemaster::Test->run_all_for($zone); | ||
| 206 | my @results = Zonemaster::Test->run_module('DNSSEC', $zone); | ||
| 207 | |||
| 208 | |||
| 209 | =head1 TEST MODULES | ||
| 210 | |||
| 211 | Test modules are defined as modules with names starting with | ||
| 212 | "Zonemaster::Test::". They are expected to provide at least four | ||
| 213 | class methods, and optionally a fifth one. | ||
| 214 | |||
| 215 | =over | ||
| 216 | |||
| 217 | =item all($zone) | ||
| 218 | |||
| 219 | C<all> will be given a zone object as its only argument, and is | ||
| 220 | epected to return a list of L<Zonemaster::Logger::Entry> objects. This | ||
| 221 | is the entry point used by the C<run_all_for> and C<run_module> | ||
| 222 | methods. | ||
| 223 | |||
| 224 | =item version() | ||
| 225 | |||
| 226 | This must return the version of the test module. | ||
| 227 | |||
| 228 | =item metadata() | ||
| 229 | |||
| 230 | This must return a reference to a hash where the keys are the names of | ||
| 231 | callable methods implementing tests, and the values are references to | ||
| 232 | arrays with the tags of the messages the test methods can generate. | ||
| 233 | |||
| 234 | =item translation() | ||
| 235 | |||
| 236 | This must return a reference to a hash where the keys are all the | ||
| 237 | message tags the test module can produce, and the corresponing keys | ||
| 238 | are the english translations of those messages. The translation | ||
| 239 | strings will be used as keys to look up translations into other | ||
| 240 | languages, so think twice before editing them. | ||
| 241 | |||
| 242 | =item policy() | ||
| 243 | |||
| 244 | Optionally, a test module can implement this method, which if | ||
| 245 | implemented should return a reference to a hash where the keys are all | ||
| 246 | the message tags the module can produce and the correspondning values | ||
| 247 | are their recommended default log levels. | ||
| 248 | |||
| 249 | =back | ||
| 250 | |||
| 251 | =head1 CLASS METHODS | ||
| 252 | |||
| 253 | =over | ||
| 254 | |||
| 255 | =item modules() | ||
| 256 | |||
| 257 | Returns a list with the names of all available test modules except | ||
| 258 | L<Zonemaster::Test::Basic> (since that one is a bit special). | ||
| 259 | |||
| 260 | =item run_all_for($zone) | ||
| 261 | |||
| 262 | Runs all (default) tests in all test modules found, and returns a list | ||
| 263 | of the log entry objects they returned. | ||
| 264 | |||
| 265 | The order in which the test modules found will be executed is not | ||
| 266 | defined, except that L<Zonemaster::Test::Basic> is always executed | ||
| 267 | first. If the Basic tests fail to indicate a very basic level of | ||
| 268 | function (it must have a parent domain, and it must have at least one | ||
| 269 | functional nameserver) for the zone, no further tests will be | ||
| 270 | executed. | ||
| 271 | |||
| 272 | =item run_module($module, $zone) | ||
| 273 | |||
| 274 | Runs all default tests in the named module for the given zone. | ||
| 275 | |||
| 276 | =item run_one($module, $method, @arguments) | ||
| 277 | |||
| 278 | Run one particular test method in one particular module. The requested | ||
| 279 | module must be in the list of active loaded modules (that is, not a | ||
| 280 | module disabled by the current policy), and the method must be listed | ||
| 281 | in the metadata the module exports. If those requirements are | ||
| 282 | fulfilled, the method will be called with the provided arguments. No | ||
| 283 | attempt is made to check that the provided arguments make sense for | ||
| 284 | the particular method called. That is left entirely to the user. | ||
| 285 | |||
| 286 | =back | ||
| 287 | |||
| 288 | =cut |