| 1 | # | ||
| 2 | # Mail::SPF::Test | ||
| 3 | # SPF test-suite class. | ||
| 4 | # | ||
| 5 | # (C) 2006-2007 Julian Mehnle <julian@mehnle.net> | ||
| 6 | # $Id: Test.pm 105 2007-05-30 20:41:57Z Julian Mehnle $ | ||
| 7 | # | ||
| 8 | ############################################################################## | ||
| 9 | |||
| 10 | package Mail::SPF::Test; | ||
| 11 | |||
| 12 | =head1 NAME | ||
| 13 | |||
| 14 | Mail::SPF::Test - SPF test-suite class | ||
| 15 | |||
| 16 | =head1 VERSION | ||
| 17 | |||
| 18 | 1.001 | ||
| 19 | |||
| 20 | =cut | ||
| 21 | |||
| 22 | use version; our $VERSION = qv('1.001'); | ||
| 23 | |||
| 24 | use warnings; | ||
| 25 | use strict; | ||
| 26 | |||
| 27 | use base 'Mail::SPF::Test::Base'; | ||
| 28 | |||
| 29 | use Mail::SPF::Test::Scenario; | ||
| 30 | |||
| 31 | use IO::File; | ||
| 32 | |||
| 33 | use constant TRUE => (0 == 0); | ||
| 34 | use constant FALSE => not TRUE; | ||
| 35 | |||
| 36 | # Interface: | ||
| 37 | ############################################################################## | ||
| 38 | |||
| 39 | =head1 SYNOPSIS | ||
| 40 | |||
| 41 | use Mail::SPF::Test; | ||
| 42 | |||
| 43 | my $test_suite = Mail::SPF::Test->new_from_yaml_file('rfc4408-tests.yml'); | ||
| 44 | |||
| 45 | foreach my $scenario ($test_suite->scenarios) { | ||
| 46 | printf("Scenario '%s'\n", $scenario->description); | ||
| 47 | |||
| 48 | foreach my $test_case ($scenario->test_cases) { | ||
| 49 | my $name = $test_case->name; | ||
| 50 | my $description = $test_case->description; | ||
| 51 | my $comment = $test_case->comment; | ||
| 52 | my @spec_refs = $test_case->spec_refs(undef || '*.*/*'); | ||
| 53 | |||
| 54 | my $scope = $test_case->scope; | ||
| 55 | my $identity = $test_case->identity; | ||
| 56 | |||
| 57 | my $ip_address = $test_case->ip_address; | ||
| 58 | my $helo_identity | ||
| 59 | = $test_case->helo_identity; | ||
| 60 | |||
| 61 | my @expected_results | ||
| 62 | = $test_case->expected_results; | ||
| 63 | my $expected_explanation | ||
| 64 | = $test_case->expected_explanation; | ||
| 65 | |||
| 66 | my $ok = | ||
| 67 | $test_case->is_expected_result($result_code) and | ||
| 68 | $expected_explanation eq $authority_explanation; | ||
| 69 | } | ||
| 70 | } | ||
| 71 | |||
| 72 | =cut | ||
| 73 | |||
| 74 | # Implementation: | ||
| 75 | ############################################################################## | ||
| 76 | |||
| 77 | =head1 DESCRIPTION | ||
| 78 | |||
| 79 | B<Mail::SPF::Test> is a class for reading and manipulating SPF test-suite | ||
| 80 | data. | ||
| 81 | |||
| 82 | =head2 Constructors | ||
| 83 | |||
| 84 | The following constructors are provided: | ||
| 85 | |||
| 86 | =over | ||
| 87 | |||
| 88 | =item B<new(%options)>: returns I<Mail::SPF::Test> | ||
| 89 | |||
| 90 | Creates a new SPF test-suite object from scratch. | ||
| 91 | |||
| 92 | =cut | ||
| 93 | |||
| 94 | #sub new { | ||
| 95 | # my ($self, %options) = @_; | ||
| 96 | # return $self->SUPER::new(%options); | ||
| 97 | #} | ||
| 98 | |||
| 99 | =item B<new_from_yaml($yaml_text, %options)>: returns I<Mail::SPF::Test> | ||
| 100 | |||
| 101 | Creates a new SPF test-suite object from the given YAML string. | ||
| 102 | |||
| 103 | =cut | ||
| 104 | |||
| 105 | sub new_from_yaml { | ||
| 106 | my ($self, $yaml_text, %options) = @_; | ||
| 107 | $self = $self->new(%options); | ||
| 108 | require YAML::Loader; | ||
| 109 | my $yaml_loader = YAML::Loader->new(); | ||
| 110 | my @yaml_scenario_structs = $yaml_loader->load($yaml_text); | ||
| 111 | $self->{scenarios} = [ | ||
| 112 | map(Mail::SPF::Test::Scenario->new_from_yaml_struct($_), @yaml_scenario_structs) | ||
| 113 | ]; | ||
| 114 | return $self; | ||
| 115 | } | ||
| 116 | |||
| 117 | =item B<new_from_yaml_file($file_name, %options)>: returns I<Mail::SPF::Test> | ||
| 118 | |||
| 119 | Creates a new SPF test-suite object by reading from the YAML file of the given | ||
| 120 | name. | ||
| 121 | |||
| 122 | =cut | ||
| 123 | |||
| 124 | sub new_from_yaml_file { | ||
| 125 | my ($self, $file_name, %options) = @_; | ||
| 126 | my $file = IO::File->new($file_name, '<'); | ||
| 127 | defined($file) | ||
| 128 | or return undef; | ||
| 129 | my $yaml_text = do { local $/; <$file> }; | ||
| 130 | return $self->new_from_yaml($yaml_text); | ||
| 131 | } | ||
| 132 | |||
| 133 | =back | ||
| 134 | |||
| 135 | =head2 Instance methods | ||
| 136 | |||
| 137 | The following instance methods are provided: | ||
| 138 | |||
| 139 | =over | ||
| 140 | |||
| 141 | =item B<as_yaml>: returns I<string> | ||
| 142 | |||
| 143 | Returns the test-suite object's list of scenarios formatted as a stream of YAML | ||
| 144 | documents. | ||
| 145 | |||
| 146 | =cut | ||
| 147 | |||
| 148 | sub as_yaml { | ||
| 149 | my ($self) = @_; | ||
| 150 | require YAML::Dumper; | ||
| 151 | my $yaml_dumper = YAML::Dumper->new(); | ||
| 152 | my @yaml_scenario_structs = map( $_->as_yaml_struct(), @{$self->{scenarios}} ); | ||
| 153 | return $yaml_dumper(@yaml_scenario_structs); | ||
| 154 | } | ||
| 155 | |||
| 156 | =item B<scenarios>: returns I<list> of I<Mail::SPF::Test::Scenario> | ||
| 157 | |||
| 158 | Returns a list of the test-suite object's scenario objects. | ||
| 159 | |||
| 160 | =cut | ||
| 161 | |||
| 162 | sub scenarios { | ||
| 163 | my ($self) = @_; | ||
| 164 | return @{$self->{scenarios}}; | ||
| 165 | } | ||
| 166 | |||
| 167 | =back | ||
| 168 | |||
| 169 | =head1 SEE ALSO | ||
| 170 | |||
| 171 | For availability, support, and license information, see the README file | ||
| 172 | included with Mail::SPF::Test. | ||
| 173 | |||
| 174 | =head1 AUTHORS | ||
| 175 | |||
| 176 | Julian Mehnle <julian@mehnle.net> | ||
| 177 | |||
| 178 | =cut | ||
| 179 | |||
| 180 | TRUE; |