| 1 | # | ||
| 2 | # Mail::SPF::Test::Case | ||
| 3 | # SPF test-suite test case class. | ||
| 4 | # | ||
| 5 | # (C) 2006 Julian Mehnle <julian@mehnle.net> | ||
| 6 | # $Id: Case.pm 27 2006-12-23 20:11:21Z Julian Mehnle $ | ||
| 7 | # | ||
| 8 | ############################################################################## | ||
| 9 | |||
| 10 | package Mail::SPF::Test::Case; | ||
| 11 | |||
| 12 | =head1 NAME | ||
| 13 | |||
| 14 | Mail::SPF::Test::Case - SPF test-suite test case class | ||
| 15 | |||
| 16 | =cut | ||
| 17 | |||
| 18 | use warnings; | ||
| 19 | use strict; | ||
| 20 | |||
| 21 | use base 'Mail::SPF::Test::Base'; | ||
| 22 | |||
| 23 | use constant TRUE => (0 == 0); | ||
| 24 | use constant FALSE => not TRUE; | ||
| 25 | |||
| 26 | # Interface: | ||
| 27 | ############################################################################## | ||
| 28 | |||
| 29 | =head1 SYNOPSIS | ||
| 30 | |||
| 31 | use Mail::SPF::Test::Case; | ||
| 32 | |||
| 33 | my $test_case = Mail::SPF::Test::Case->new_from_yaml_struct($struct); | ||
| 34 | |||
| 35 | my $name = $test_case->name; | ||
| 36 | my $description = $test_case->description; | ||
| 37 | my $comment = $test_case->comment; | ||
| 38 | my @spec_refs = $test_case->spec_refs(undef || '*.*/*'); | ||
| 39 | |||
| 40 | my $scope = $test_case->scope; | ||
| 41 | my $identity = $test_case->identity; | ||
| 42 | |||
| 43 | my $ip_address = $test_case->ip_address; | ||
| 44 | my $helo_identity | ||
| 45 | = $test_case->helo_identity; | ||
| 46 | |||
| 47 | my $expected_results | ||
| 48 | = $test_case->expected_results; | ||
| 49 | my $expected_explanation | ||
| 50 | = $test_case->expected_explanation; | ||
| 51 | |||
| 52 | my $ok = | ||
| 53 | $test_case->is_expected_result($result_code) and | ||
| 54 | $expected_explanation eq $authority_explanation; | ||
| 55 | |||
| 56 | =cut | ||
| 57 | |||
| 58 | # Implementation: | ||
| 59 | ############################################################################## | ||
| 60 | |||
| 61 | =head1 DESCRIPTION | ||
| 62 | |||
| 63 | An object of class B<Mail::SPF::Test::Case> represents a single test case | ||
| 64 | within an SPF test-suite scenario. | ||
| 65 | |||
| 66 | =head2 Constructors | ||
| 67 | |||
| 68 | The following constructors are provided: | ||
| 69 | |||
| 70 | =over | ||
| 71 | |||
| 72 | =item B<new(%options)>: returns I<Mail::SPF::Test::Case> | ||
| 73 | |||
| 74 | Creates a new SPF test-suite test case object from scratch. | ||
| 75 | |||
| 76 | =cut | ||
| 77 | |||
| 78 | sub new { | ||
| 79 | my ($self, %options) = @_; | ||
| 80 | $self = $self->SUPER::new(%options); | ||
| 81 | $self->{scope} ||= 'mfrom'; | ||
| 82 | return $self; | ||
| 83 | } | ||
| 84 | |||
| 85 | =item B<new_from_yaml_struct($yaml_struct)>: returns I<Mail::SPF::Test::Case> | ||
| 86 | |||
| 87 | Creates a new SPF test-suite test case object from the given YAML-generated | ||
| 88 | data structure. | ||
| 89 | |||
| 90 | =cut | ||
| 91 | |||
| 92 | sub new_from_yaml_struct { | ||
| 93 | my ($self, $yaml_struct, %options) = @_; | ||
| 94 | my $scope = $yaml_struct->{scope} || | ||
| 95 | (defined($yaml_struct->{identity}) || length($yaml_struct->{mailfrom}) ? 'mfrom' : 'helo'); | ||
| 96 | $self = $self->new( | ||
| 97 | %options, | ||
| 98 | name => $yaml_struct->{name}, | ||
| 99 | description => $yaml_struct->{description}, | ||
| 100 | comment => $yaml_struct->{comment}, | ||
| 101 | spec_refs => $self->arrayify($yaml_struct->{spec}), | ||
| 102 | |||
| 103 | scope => $scope, | ||
| 104 | identity => $yaml_struct->{identity}, | ||
| 105 | |||
| 106 | ip_address => $yaml_struct->{host}, | ||
| 107 | helo_identity => $yaml_struct->{helo}, | ||
| 108 | |||
| 109 | expected_results => $self->arrayify($yaml_struct->{result}), | ||
| 110 | expected_explanation | ||
| 111 | => $yaml_struct->{explanation} | ||
| 112 | ); | ||
| 113 | if ($self->{scope} eq 'helo') { | ||
| 114 | $self->{identity} ||= $yaml_struct->{helo}; | ||
| 115 | } | ||
| 116 | elsif ($self->{scope} eq 'mfrom') { | ||
| 117 | $self->{identity} ||= $yaml_struct->{mailfrom}; | ||
| 118 | } | ||
| 119 | return $self; | ||
| 120 | } | ||
| 121 | |||
| 122 | =back | ||
| 123 | |||
| 124 | =head2 Instance methods | ||
| 125 | |||
| 126 | The following instance methods are provided: | ||
| 127 | |||
| 128 | =over | ||
| 129 | |||
| 130 | =item B<name>: returns I<string> | ||
| 131 | |||
| 132 | Returns the name of the test case. | ||
| 133 | |||
| 134 | =item B<description>: returns I<string> | ||
| 135 | |||
| 136 | Returns the description of the test case. | ||
| 137 | |||
| 138 | =item B<comment>: returns I<string> | ||
| 139 | |||
| 140 | Returns the optional comment of the test case. | ||
| 141 | |||
| 142 | =item B<spec_refs>: returns I<list> of I<string> | ||
| 143 | |||
| 144 | Returns a list of the specification references for the test case. | ||
| 145 | |||
| 146 | =item B<scope>: returns I<string> | ||
| 147 | |||
| 148 | Returns the SPF identity's scope for the test case. | ||
| 149 | |||
| 150 | =item B<identity>: returns I<string> | ||
| 151 | |||
| 152 | Returns the SPF identity for the test case. | ||
| 153 | |||
| 154 | =item B<ip_address>: returns I<string> | ||
| 155 | |||
| 156 | Returns the SMTP sender's IP address for the test case. | ||
| 157 | |||
| 158 | =item B<helo_identity>: returns I<string> | ||
| 159 | |||
| 160 | Returns the SPF C<HELO> identity for the test case. | ||
| 161 | |||
| 162 | =item B<expected_results>: returns I<list> of I<string> | ||
| 163 | |||
| 164 | Returns the list of acceptable SPF result codes for the test case. | ||
| 165 | |||
| 166 | =item B<is_expected_result($result_code)>: returns I<boolean> | ||
| 167 | |||
| 168 | Returns B<true> if the given result code is among the acceptable SPF result | ||
| 169 | codes for the test case, B<false> otherwise. | ||
| 170 | |||
| 171 | =item B<expected_explanation>: returns I<string> | ||
| 172 | |||
| 173 | Returns the expected authority explanation string for the test case. | ||
| 174 | |||
| 175 | =cut | ||
| 176 | |||
| 177 | # Make read-only accessors: | ||
| 178 | __PACKAGE__->make_accessor($_, TRUE) | ||
| 179 | foreach qw( | ||
| 180 | name description comment | ||
| 181 | scope identity ip_address helo_identity | ||
| 182 | expected_explanation | ||
| 183 | ); | ||
| 184 | |||
| 185 | sub spec_refs { | ||
| 186 | my ($self, $granularity) = @_; | ||
| 187 | $granularity ||= '*.*/*'; | ||
| 188 | my @refs = @{$self->{spec_refs}}; | ||
| 189 | if ($granularity eq '*') { | ||
| 190 | @refs = map(/^(\p{IsAlnum}+)/ && $1, @refs); | ||
| 191 | } | ||
| 192 | elsif ($granularity eq '*.*') { | ||
| 193 | @refs = map(/^([^\/]+)/ && $1, @refs); | ||
| 194 | } | ||
| 195 | return @refs; | ||
| 196 | } | ||
| 197 | |||
| 198 | sub expected_results { | ||
| 199 | my ($self) = @_; | ||
| 200 | return @{$self->{expected_results}}; | ||
| 201 | } | ||
| 202 | |||
| 203 | sub is_expected_result { | ||
| 204 | my ($self, $result_code) = @_; | ||
| 205 | my %expected_results; @expected_results{$self->expected_results} = (); | ||
| 206 | return exists($expected_results{$result_code}); | ||
| 207 | } | ||
| 208 | |||
| 209 | =back | ||
| 210 | |||
| 211 | =cut | ||
| 212 | |||
| 213 | sub arrayify { | ||
| 214 | my ($self, $value) = @_; | ||
| 215 | return [] | ||
| 216 | if not defined($value); | ||
| 217 | return [$value] | ||
| 218 | if not ref($value) eq 'ARRAY'; | ||
| 219 | return $value; | ||
| 220 | } | ||
| 221 | |||
| 222 | =head1 SEE ALSO | ||
| 223 | |||
| 224 | L<Mail::SPF::Test> | ||
| 225 | |||
| 226 | For availability, support, and license information, see the README file | ||
| 227 | included with Mail::SPF::Test. | ||
| 228 | |||
| 229 | =head1 AUTHORS | ||
| 230 | |||
| 231 | Julian Mehnle <julian@mehnle.net> | ||
| 232 | |||
| 233 | =cut | ||
| 234 | |||
| 235 | TRUE; |