| 1 | # | ||
| 2 | # Mail::SPF::Test::Scenario | ||
| 3 | # SPF test-suite scenario class. | ||
| 4 | # | ||
| 5 | # (C) 2006 Julian Mehnle <julian@mehnle.net> | ||
| 6 | # $Id: Scenario.pm 27 2006-12-23 20:11:21Z Julian Mehnle $ | ||
| 7 | # | ||
| 8 | ############################################################################## | ||
| 9 | |||
| 10 | package Mail::SPF::Test::Scenario; | ||
| 11 | |||
| 12 | =head1 NAME | ||
| 13 | |||
| 14 | Mail::SPF::Test::Scenario - SPF test-suite scenario class | ||
| 15 | |||
| 16 | =cut | ||
| 17 | |||
| 18 | use warnings; | ||
| 19 | use strict; | ||
| 20 | |||
| 21 | use base 'Mail::SPF::Test::Base'; | ||
| 22 | |||
| 23 | use Mail::SPF::Test::Case; | ||
| 24 | |||
| 25 | use NetAddr::IP; | ||
| 26 | use Net::DNS::RR; | ||
| 27 | |||
| 28 | #use XML::LibXML; | ||
| 29 | |||
| 30 | use constant TRUE => (0 == 0); | ||
| 31 | use constant FALSE => not TRUE; | ||
| 32 | |||
| 33 | # Interface: | ||
| 34 | ############################################################################## | ||
| 35 | |||
| 36 | =head1 SYNOPSIS | ||
| 37 | |||
| 38 | use Mail::SPF::Test::Scenario; | ||
| 39 | |||
| 40 | my $scenario = Mail::SPF::Test::Scenario->new_from_yaml_struct($struct); | ||
| 41 | |||
| 42 | my $yaml = $scenario->as_yaml; | ||
| 43 | |||
| 44 | my $description = $scenario->description; | ||
| 45 | my @test_cases = $scenario->test_cases; | ||
| 46 | my @spec_refs = $scenario->spec_refs(undef || '*.*/*'); | ||
| 47 | my @records = $scenario->records; | ||
| 48 | my @records_for_domain | ||
| 49 | = $scenario->records_for_domain($domain, $rr_type); | ||
| 50 | |||
| 51 | =cut | ||
| 52 | |||
| 53 | # Implementation: | ||
| 54 | ############################################################################## | ||
| 55 | |||
| 56 | =head1 DESCRIPTION | ||
| 57 | |||
| 58 | An object of class B<Mail::SPF::Test::Scenario> represents an SPF test-suite | ||
| 59 | scenario. | ||
| 60 | |||
| 61 | =head2 Constructors | ||
| 62 | |||
| 63 | The following constructors are provided: | ||
| 64 | |||
| 65 | =over | ||
| 66 | |||
| 67 | =item B<new(%options)>: returns I<Mail::SPF::Test::Scenario> | ||
| 68 | |||
| 69 | Creates a new SPF test-suite scenario object from scratch. | ||
| 70 | |||
| 71 | =cut | ||
| 72 | |||
| 73 | #sub new { | ||
| 74 | # my ($self, %options) = @_; | ||
| 75 | # return $self->SUPER::new(%options); | ||
| 76 | #} | ||
| 77 | |||
| 78 | =item B<new_from_yaml_struct($yaml_struct, %options)>: returns I<Mail::SPF::Test::Scenario> | ||
| 79 | |||
| 80 | Creates a new SPF test-suite scenario object from the given YAML-generated data | ||
| 81 | structure. | ||
| 82 | |||
| 83 | =cut | ||
| 84 | |||
| 85 | sub new_from_yaml_struct { | ||
| 86 | my ($self, $yaml_struct, %options) = @_; | ||
| 87 | $self = $self->new(%options); | ||
| 88 | |||
| 89 | $self->{description} = $yaml_struct->{description}; | ||
| 90 | |||
| 91 | my $tests = $yaml_struct->{tests}; | ||
| 92 | my $test_cases = $self->{test_cases} = {}; | ||
| 93 | foreach my $test_name (keys(%$tests)) { | ||
| 94 | $tests->{$test_name}->{name} = $test_name; | ||
| 95 | $test_cases->{$test_name} = Mail::SPF::Test::Case->new_from_yaml_struct($tests->{$test_name}); | ||
| 96 | } | ||
| 97 | |||
| 98 | my $zonedata = $yaml_struct->{zonedata} || {}; | ||
| 99 | my $records = $self->{records} = []; | ||
| 100 | my $records_by_domain = $self->{records_by_domain} = {}; | ||
| 101 | |||
| 102 | DOMAIN: | ||
| 103 | foreach my $domain (keys(%$zonedata)) { | ||
| 104 | my $records_by_type = $records_by_domain->{lc($domain)} = {}; | ||
| 105 | my $txt_rr_synthesis = TRUE; | ||
| 106 | |||
| 107 | RECORD: | ||
| 108 | foreach my $record_struct (@{$zonedata->{$domain}}) { | ||
| 109 | if (ref($record_struct) eq 'HASH') { | ||
| 110 | # TYPE => DATA | ||
| 111 | my ($type, $data_struct) = %$record_struct; | ||
| 112 | |||
| 113 | if ($data_struct =~ /^(TIMEOUT|RCODE[1-5])$/) { | ||
| 114 | $records_by_type->{$type} = $data_struct; | ||
| 115 | } | ||
| 116 | elsif ($data_struct eq 'NO-SYNTHESIS' and $type eq 'TXT') { | ||
| 117 | $txt_rr_synthesis = FALSE; | ||
| 118 | } | ||
| 119 | else { | ||
| 120 | my %data; | ||
| 121 | if ($type eq 'SPF' or $type eq 'TXT') { | ||
| 122 | if ($data_struct eq 'NONE') { | ||
| 123 | $txt_rr_synthesis = FALSE; | ||
| 124 | next RECORD; | ||
| 125 | } | ||
| 126 | else { | ||
| 127 | $data_struct = [$data_struct] | ||
| 128 | if not ref($data_struct); | ||
| 129 | %data = ( char_str_list => $data_struct ); | ||
| 130 | } | ||
| 131 | } | ||
| 132 | elsif ($type eq 'A' or $type eq 'AAAA') { | ||
| 133 | my $address = NetAddr::IP->new($data_struct); | ||
| 134 | %data = ( address => $address->addr ); # Normalize IP address. | ||
| 135 | } | ||
| 136 | elsif ($type eq 'MX') { | ||
| 137 | %data = ( | ||
| 138 | preference => $data_struct->[0], | ||
| 139 | exchange => $data_struct->[1] | ||
| 140 | ); | ||
| 141 | } | ||
| 142 | elsif ($type eq 'PTR') { | ||
| 143 | %data = ( ptrdname => $data_struct ); | ||
| 144 | } | ||
| 145 | elsif ($type eq 'CNAME') { | ||
| 146 | %data = ( cname => $data_struct ); | ||
| 147 | } | ||
| 148 | else { | ||
| 149 | # Unexpected RR type! | ||
| 150 | die("Unexpected RR type '$type' in zonedata"); | ||
| 151 | } | ||
| 152 | |||
| 153 | my $record = Net::DNS::RR->new( | ||
| 154 | name => $domain, | ||
| 155 | type => $type, | ||
| 156 | %data | ||
| 157 | ); | ||
| 158 | push(@{$records_by_type->{$type}}, $record); | ||
| 159 | push(@$records, $record); | ||
| 160 | } | ||
| 161 | } | ||
| 162 | elsif (not ref($record_struct)) { | ||
| 163 | # TIMEOUT, RCODE#, NO-TXT-SYNTHESIS | ||
| 164 | if ($record_struct =~ /^(TIMEOUT|RCODE[1-5])$/) { | ||
| 165 | $records_by_type->{ANY} = $record_struct; | ||
| 166 | } | ||
| 167 | elsif ($record_struct eq 'NO-TXT-SYNTHESIS') { | ||
| 168 | $txt_rr_synthesis = FALSE; | ||
| 169 | } | ||
| 170 | else { | ||
| 171 | die("Unexpected record token"); | ||
| 172 | } | ||
| 173 | } | ||
| 174 | else { | ||
| 175 | # Unexpected record structure! | ||
| 176 | die("Unexpected record structure"); | ||
| 177 | } | ||
| 178 | } | ||
| 179 | |||
| 180 | # TXT RR synthesis: | ||
| 181 | if ( | ||
| 182 | $txt_rr_synthesis and | ||
| 183 | defined($records_by_type->{SPF}) and | ||
| 184 | not defined($records_by_type->{TXT}) | ||
| 185 | ) { | ||
| 186 | foreach my $spf_record (@{$records_by_type->{SPF}}) { | ||
| 187 | my $txt_record = Net::DNS::RR->new( | ||
| 188 | name => $spf_record->name, | ||
| 189 | type => 'TXT', | ||
| 190 | char_str_list => [$spf_record->char_str_list] | ||
| 191 | ); | ||
| 192 | push(@{$records_by_type->{TXT}}, $txt_record); | ||
| 193 | } | ||
| 194 | } | ||
| 195 | } | ||
| 196 | |||
| 197 | return $self; | ||
| 198 | } | ||
| 199 | |||
| 200 | =item B<new_from_yaml($yaml_text, %options)>: returns I<Mail::SPF::Test::Scenario> | ||
| 201 | |||
| 202 | Creates a new SPF test-suite scenario object from the given YAML string. | ||
| 203 | |||
| 204 | =cut | ||
| 205 | |||
| 206 | sub new_from_yaml { | ||
| 207 | my ($self, $yaml_text, %options) = @_; | ||
| 208 | require YAML::Loader; | ||
| 209 | my $yaml_loader = YAML::Loader->new(); | ||
| 210 | my ($raw_yaml_struct) = $yaml_loader->load($yaml_text); | ||
| 211 | $self = $self->new_from_yaml_struct($raw_yaml_struct); | ||
| 212 | return $self; | ||
| 213 | } | ||
| 214 | |||
| 215 | =back | ||
| 216 | |||
| 217 | =head2 Instance methods | ||
| 218 | |||
| 219 | The following instance methods are provided: | ||
| 220 | |||
| 221 | =over | ||
| 222 | |||
| 223 | =item B<as_yaml>: returns I<string> | ||
| 224 | |||
| 225 | Returns the test-suite scenario formatted as a YAML document. | ||
| 226 | |||
| 227 | =cut | ||
| 228 | |||
| 229 | sub as_yaml { | ||
| 230 | my ($self) = @_; | ||
| 231 | require YAML::Dumper; | ||
| 232 | my $yaml_dumper = YAML::Dumper->new(); | ||
| 233 | my $raw_yaml_data = { | ||
| 234 | description => $self->{description}, | ||
| 235 | tests => $self->{test_cases}, | ||
| 236 | zonedata => $self->{records_by_domain} | ||
| 237 | }; | ||
| 238 | return $yaml_dumper($raw_yaml_data); | ||
| 239 | } | ||
| 240 | |||
| 241 | =item B<description>: returns I<string> | ||
| 242 | |||
| 243 | Returns the description of the test-suite scenario. | ||
| 244 | |||
| 245 | =cut | ||
| 246 | |||
| 247 | # Make read-only accessor: | ||
| 248 | __PACKAGE__->make_accessor('description', TRUE); | ||
| 249 | |||
| 250 | =item B<test_cases>: returns I<list> of I<Mail::SPF::Test::Case> | ||
| 251 | |||
| 252 | Returns a list of the test-suite scenario object's test case objects. | ||
| 253 | |||
| 254 | =cut | ||
| 255 | |||
| 256 | sub test_cases { | ||
| 257 | my ($self) = @_; | ||
| 258 | return values(%{$self->{test_cases}}); | ||
| 259 | } | ||
| 260 | |||
| 261 | =item B<spec_refs>: returns I<list> of I<string> | ||
| 262 | |||
| 263 | =item B<spec_refs($granularity)>: returns I<list> of I<string> | ||
| 264 | |||
| 265 | Returns a combined and sorted list of the specification references of all the | ||
| 266 | test-suite scenario object's test case objects. | ||
| 267 | |||
| 268 | See L<Mail::SPF::Test::Case/spec_refs> for how to specify a granularity for the | ||
| 269 | specification references. | ||
| 270 | |||
| 271 | =cut | ||
| 272 | |||
| 273 | sub spec_refs { | ||
| 274 | my ($self, $granularity) = @_; | ||
| 275 | my @refs = map($_->spec_refs($granularity), $self->test_cases); | ||
| 276 | my %unique_refs; @unique_refs{@refs} = (); | ||
| 277 | { | ||
| 278 | no warnings 'numeric'; | ||
| 279 | @refs = sort { ($a =~ /^\d/ and $b =~ /^\d/ and $a <=> $b) or ($a cmp $b) } keys(%unique_refs);; | ||
| 280 | } | ||
| 281 | return @refs; | ||
| 282 | } | ||
| 283 | |||
| 284 | =item B<records>: returns I<list> of I<Net::DNS::RR> and DNS status tokens | ||
| 285 | |||
| 286 | Returns a list of the test-suite scenario object's DNS RR objects and DNS | ||
| 287 | status tokens. | ||
| 288 | |||
| 289 | See L</records_for_domain> for the description of DNS RR objects and DNS status | ||
| 290 | tokens. | ||
| 291 | |||
| 292 | =cut | ||
| 293 | |||
| 294 | sub records { | ||
| 295 | my ($self) = @_; | ||
| 296 | return @{$self->{records}}; | ||
| 297 | } | ||
| 298 | |||
| 299 | =item B<records_for_domain($domain)>: returns I<list> of I<Net::DNS::RR> and DNS status tokens | ||
| 300 | |||
| 301 | =item B<records_for_domain($domain, $type)>: returns I<list> of I<Net::DNS::RR> and DNS status tokens | ||
| 302 | |||
| 303 | Returns either the DNS RR objects of the test-suite scenario object that match | ||
| 304 | the given domain and, if specified, RR type, or a DNS status token. | ||
| 305 | |||
| 306 | DNS RR objects are of type I<Net::DNS::RR>. A DNS status token is any of | ||
| 307 | B<'TIMEOUT'> or B<'RCODE#'> (where B<#> is a digit from 1 to 5). | ||
| 308 | |||
| 309 | =cut | ||
| 310 | |||
| 311 | sub records_for_domain { | ||
| 312 | my ($self, $domain, $type) = @_; | ||
| 313 | |||
| 314 | defined($domain) | ||
| 315 | or return (); # Invalid domain. | ||
| 316 | $domain =~ s/^(.*?)\.?$/\L$1/; | ||
| 317 | $type ||= 'ANY'; | ||
| 318 | |||
| 319 | my $recordset = $self->{records_by_domain}->{$domain} | ||
| 320 | or return (); # Unknown domain. | ||
| 321 | |||
| 322 | # ANY queries are unsupported., return RCODE 4 ("not implemented"): | ||
| 323 | return 'RCODE4' | ||
| 324 | if $type eq 'ANY'; | ||
| 325 | |||
| 326 | # Use TIMEOUT/RCODE# entry meant for requested type: | ||
| 327 | return $recordset->{$type} | ||
| 328 | if defined($recordset->{$type}) and not ref($recordset->{$type}); | ||
| 329 | # Use RRs applicable specifically to the requested type: | ||
| 330 | return @{$recordset->{$type}} | ||
| 331 | if ref($recordset->{$type}) eq 'ARRAY'; | ||
| 332 | |||
| 333 | # Use TIMEOUT/RCODE# entry meant for any type: | ||
| 334 | return $recordset->{ANY} | ||
| 335 | if defined($recordset->{ANY}) and not ref($recordset->{ANY}); | ||
| 336 | # Use RRs applicable to any type: | ||
| 337 | return @{$recordset->{ANY}} | ||
| 338 | if ref($recordset->{ANY}) eq 'ARRAY'; | ||
| 339 | |||
| 340 | return (); | ||
| 341 | } | ||
| 342 | |||
| 343 | =back | ||
| 344 | |||
| 345 | =head1 SEE ALSO | ||
| 346 | |||
| 347 | L<Mail::SPF::Test> | ||
| 348 | |||
| 349 | For availability, support, and license information, see the README file | ||
| 350 | included with Mail::SPF::Test. | ||
| 351 | |||
| 352 | =head1 AUTHORS | ||
| 353 | |||
| 354 | Julian Mehnle <julian@mehnle.net> | ||
| 355 | |||
| 356 | =cut | ||
| 357 | |||
| 358 | TRUE; |