Codebase list libmail-dkim-perl / 7752386
Improve output of make test Marc Bradshaw 6 years ago
1 changed file(s) with 115 addition(s) and 99 deletion(s). Raw diff Collapse all Expand all
88 use Net::DNS::Resolver::Mock;
99
1010 use Mail::DKIM;
11
1112 #$Mail::DKIM::SORTTAGS = 1;
1213
1314 use Mail::DKIM::ARC::Signer;
2728
2829 =cut
2930
30 sub new {
31 sub new {
3132 my ( $class, %args ) = @_;
3233 my $self = {};
3334 bless $self, $class;
4748
4849 sub LoadFile {
4950 my ( $self, $file ) = @_;
50 my @data = YAML::XS::LoadFile( $file );
51 $self->{ 'tests' } = \@data;
51 my @data = YAML::XS::LoadFile($file);
52 $self->{'tests'} = \@data;
5253 return;
5354 }
5455
6263
6364 sub SetOperation {
6465 my ( $self, $operation ) = @_;
65 die "Invalid operation $operation" unless $operation =~ m{^(validate|sign)$};
66 $self->{ 'operation' } = $operation;
66 die "Invalid operation $operation"
67 unless $operation =~ m{^(validate|sign)$};
68 $self->{'operation'} = $operation;
6769 return;
6870 }
6971
7981
8082 sub DumpTests {
8183 my ( $self, $testpat ) = @_;
82 $self->{ 'testpat' } = $testpat;
84 $self->{'testpat'} = $testpat;
8385 return;
8486 }
8587
99101 my ( $self, $nsx ) = @_;
100102
101103 $nskip = $nsx if $nsx > 0;
102 foreach my $Scenario ( @{ $self->{ 'tests' } } ) {
103 $self->RunScenario( $Scenario );
104 foreach my $Scenario ( @{ $self->{'tests'} } ) {
105 $self->RunScenario($Scenario);
104106 }
105107 return;
106108 }
116118 sub RunScenario {
117119 my ( $self, $scenario ) = @_;
118120
119 my $description = $scenario->{ 'description' };
120 my $tests = $scenario->{ 'tests' };
121 my $txt_records = $scenario->{ 'txt-records' } || q{};
122 my $comment = $scenario->{ 'comment' };
123 my $domain = $scenario->{ 'domain '};
124 my $sel = $scenario->{ 'sel' };
125 my $private_key = $scenario->{ 'privatekey' } || q{};
126
127 diag("--- $description ---");
121 my $description = $scenario->{'description'};
122 my $tests = $scenario->{'tests'};
123 my $txt_records = $scenario->{'txt-records'} || q{};
124 my $comment = $scenario->{'comment'};
125 my $domain = $scenario->{'domain '};
126 my $sel = $scenario->{'sel'};
127 my $private_key = $scenario->{'privatekey'} || q{};
128
129 diag("--- $description ---") unless $ENV{HARNESS_ACTIVE};
128130
129131 # remove key BEGIN / END
130 if($private_key) {
132 if ($private_key) {
131133 my @chompkey = split( "\n", $private_key );
132 $private_key = join( q{}, @chompkey[1..($#chompkey-1)] );
134 $private_key = join( q{}, @chompkey[ 1 .. ( $#chompkey - 1 ) ] );
133135 }
134136
135137 my $ZoneFile = q{};
136138 foreach my $Record ( sort keys %$txt_records ) {
137 my $Txt = $txt_records->{ $Record };
139 my $Txt = $txt_records->{$Record};
138140 $ZoneFile .= $Record . '. 60 TXT';
139141 foreach my $TxtLine ( split "\n", $Txt ) {
140142 $ZoneFile .= ' "' . $TxtLine . '"';
142144 $ZoneFile .= "\n";
143145 }
144146 my $FakeResolver = Net::DNS::Resolver::Mock->new();
145 $FakeResolver->zonefile_parse( $ZoneFile );
146
147 TEST:
147 $FakeResolver->zonefile_parse($ZoneFile);
148
149 TEST:
148150 foreach my $test ( sort keys %$tests ) {
149151
150 if($nskip > 0) {
151 diag("skip $description - $test");
152 if ( $nskip > 0 ) {
153 diag("skip $description - $test") unless $ENV{HARNESS_ACTIVE};
152154 $nskip--;
153155 next;
154156 }
155 my $testhash = $tests->{ $test };
157 my $testhash = $tests->{$test};
156158
157159 # keys relevant to validate and signing tests
158 my $comment = $testhash->{ 'comment' };
159 my $cv = $testhash->{ 'cv' };
160 my $description = $testhash->{ 'description' };
161 my $message = $testhash->{ 'message' };
162 my $spec = $testhash->{ 'spec' };
160 my $comment = $testhash->{'comment'};
161 my $cv = $testhash->{'cv'};
162 my $description = $testhash->{'description'};
163 my $message = $testhash->{'message'};
164 my $spec = $testhash->{'spec'};
163165
164166 # dump test to a file
165 if($self->{ 'testpat' }) {
167 if ( $self->{'testpat'} ) {
166168 local *TOUT;
167169 my $tfn = $test;
168170 $tfn =~ s:[ /]:_:g;
169171
170 open TOUT, ">" . sprintf($self->{ 'testpat' }, $tfn)
171 or die "cannot write file for $description";
172 open TOUT, ">" . sprintf( $self->{'testpat'}, $tfn )
173 or die "cannot write file for $description";
172174 print TOUT $message;
173175 close TOUT;
174176 }
175177
176178 # HACK - skip sha1 tests
177 if($test =~ /sha1/) {
178 #diag("Skip SHA-1 test $test");
179 if ( $test =~ /sha1/ ) {
180 diag("Skip SHA-1 test $test") unless $ENV{HARNESS_ACTIVE};
179181 next;
180182 }
181183
182184 $message =~ s/\015?\012/\015\012/g;
183185
184 if ($self->{ 'operation' } eq 'validate' and (!defined $cv or $cv eq q{}) ) {
185 $cv = 'fail';
186 #diag( "Null test cv treated as fail for $description - $test" );
187 }
188
189186 my $arc_result;
190187
191 if($self->{ 'operation' } eq 'validate') {
192 if (!defined $cv or $cv eq q{}) {
188 if ( $self->{'operation'} eq 'validate' ) {
189 if ( !defined $cv or $cv eq q{} ) {
193190 $cv = 'fail';
194 diag( "Null test cv treated as fail for $description - $test" );
191 diag("Null test cv treated as fail for $description - $test")
192 unless $ENV{HARNESS_ACTIVE};
195193 }
196194
197195 eval {
198 my $arc = new Mail::DKIM::ARC::Verifier(Strict => $self->{"Strict"});
199 Mail::DKIM::DNS::resolver( $FakeResolver );
200 $arc->PRINT( $message );
201 $arc->CLOSE();
202 $arc_result = $arc->result();
203 my $arc_result_detail = $arc->result_detail();
204 my $mycv = lc $arc_result eq 'pass' ? 'Pass' :
205 lc $arc_result eq 'none' ? 'None' : 'Fail';
206
207 is( lc $mycv, lc $cv, "$description - $test ARC Result $mycv want $cv" );
208 if ( lc $mycv ne lc $cv ) {
209 diag( "Got: $arc_result ( $arc_result_detail )" );
210 }
196 my $arc =
197 new Mail::DKIM::ARC::Verifier( Strict => $self->{"Strict"} );
198 Mail::DKIM::DNS::resolver($FakeResolver);
199 $arc->PRINT($message);
200 $arc->CLOSE();
201 $arc_result = $arc->result();
202 my $arc_result_detail = $arc->result_detail();
203 my $mycv =
204 lc $arc_result eq 'pass' ? 'Pass'
205 : lc $arc_result eq 'none' ? 'None'
206 : 'Fail';
207
208 is( lc $mycv, lc $cv,
209 "$description - $test ARC Result $mycv want $cv" );
210 if ( lc $mycv ne lc $cv ) {
211 diag("Got: $arc_result ( $arc_result_detail )")
212 unless $ENV{HARNESS_ACTIVE};
213 }
211214 };
212215 if ( my $error = $@ ) {
213216 is( 0, 1, "$description- $test - died with $error" );
216219 }
217220
218221 # keys relevant to signing tests only
219 my $aar = $testhash->{ 'AAR' };
220 my $ams = $testhash->{ 'AMS' };
221 my $as = $testhash->{ 'AS' };
222 my $sigheaders = $testhash->{ 'sig-headers' };
223 my $srvid = $testhash->{ 'srv-id' } || $domain;
224 my $t = $testhash->{ 't' };
222 my $aar = $testhash->{'AAR'};
223 my $ams = $testhash->{'AMS'};
224 my $as = $testhash->{'AS'};
225 my $sigheaders = $testhash->{'sig-headers'};
226 my $srvid = $testhash->{'srv-id'} || $domain;
227 my $t = $testhash->{'t'};
225228
226229 my $arc = Mail::DKIM::ARC::Signer->new(
227 'Algorithm' => 'rsa-sha256',
228 'Domain' => $domain,
229 'SrvId' => $srvid,
230 'Selector' => $sel,
231 'Key' => Mail::DKIM::PrivateKey->load( 'Data' => $private_key ),
232 'Chain' => 'ar', # use the result from A-R, since message might have changed since verified
233 'Headers' => $sigheaders,
234 'Timestamp' => $t,
230 'Algorithm' => 'rsa-sha256',
231 'Domain' => $domain,
232 'SrvId' => $srvid,
233 'Selector' => $sel,
234 'Key' => Mail::DKIM::PrivateKey->load( 'Data' => $private_key ),
235 'Chain' => 'ar'
236 , # use the result from A-R, since message might have changed since verified
237 'Headers' => $sigheaders,
238 'Timestamp' => $t,
235239 );
236 $arc->{ 'NoDefaultHeaders' } = 1;
240 $arc->{'NoDefaultHeaders'} = 1;
237241 $Mail::DKIM::SORTTAGS = 1;
238 Mail::DKIM::DNS::resolver( $FakeResolver );
239 $arc->PRINT( $message );
242 Mail::DKIM::DNS::resolver($FakeResolver);
243 $arc->PRINT($message);
240244 $arc->CLOSE();
241245 my $arcsign_result = $arc->as_string();
242 my $arcsign_as = $arc->{ '_AS' };
243 my $arcsign_ams = $arc->{ '_AMS' };
244 my $arcsign_aar = $arc->{ '_AAR' };
245
246 is( sqish( $arcsign_as ), sqish( 'ARC-Seal: ' . $as ), "$description - $test ARC-Seal" );
247 is( sqish( $arcsign_ams ), sqish( 'ARC-Message-Signature: ' . $ams ), "$description - $test ARC-Message-Signature" );
248 is( sqsh( $arcsign_aar), sqsh( 'ARC-Authentication-Results: ' . $aar) , "$description - $test ARC-Authentication-Results" );
246 my $arcsign_as = $arc->{'_AS'};
247 my $arcsign_ams = $arc->{'_AMS'};
248 my $arcsign_aar = $arc->{'_AAR'};
249
250 is(
251 sqish($arcsign_as),
252 sqish( 'ARC-Seal: ' . $as ),
253 "$description - $test ARC-Seal"
254 );
255 is(
256 sqish($arcsign_ams),
257 sqish( 'ARC-Message-Signature: ' . $ams ),
258 "$description - $test ARC-Message-Signature"
259 );
260 is(
261 sqsh($arcsign_aar),
262 sqsh( 'ARC-Authentication-Results: ' . $aar ),
263 "$description - $test ARC-Authentication-Results"
264 );
249265
250266 }
251267 return;
253269
254270 # sort tags
255271 sub srt {
256 my ( $header ) = @_;
272 my ($header) = @_;
257273 my ( $key, $value ) = split( ': ', $header, 2 );
258274 $value =~ s/^\s+//gm;
259 $value =~s/\n//g;
275 $value =~ s/\n//g;
260276 my @values = split( /;\s*/, $value );
277
261278 # @values = map { local $_ = $_ ; s/^\s+|\s+$//g ; $_ } @values;
262279 @values = map { s/^\s+|\s+$//g } @values;
263280 my $sorted = join( '; ', sort @values );
266283
267284 # squash all white space
268285 sub sqish {
269 my ( $header ) = @_;
270 return "" unless $header; # completely empty
286 my ($header) = @_;
287 return "" unless $header; # completely empty
271288 my ( $key, $value ) = split( ': ', $header, 2 );
272 return "" unless $value; # empty value
273
274 $value =~ s/[ \t\r\n]+//gs; # remove all white space
275 $value =~ s/\s*;\s*/; /g; # squash put in one space around semicolons
276 #print "SQUISH $key: $value\n";
289 return "" unless $value; # empty value
290
291 $value =~ s/[ \t\r\n]+//gs; # remove all white space
292 $value =~ s/\s*;\s*/; /g; # squash put in one space around semicolons
293 #print "SQUISH $key: $value\n";
277294 return "$key: $value";
278295 }
279296
280297 # squash white space between fields
281298 sub sqsh {
282 my ( $header ) = @_;
283 return "" unless $header; # completely empty
299 my ($header) = @_;
300 return "" unless $header; # completely empty
284301 my ( $key, $value ) = split( ': ', $header, 2 );
285 return "" unless $value; # empty value
286
287 $value =~ s/^\s+|[ \t\r\n]+$//gs; # remove leading and trailing white space
288 $value =~ s/\n/ /g; # flatten into one line
289 $value =~ s/\s*;\s*/; /g; # squash white space around semicolons
290 #print "SQUASH $key: $value\n";
302 return "" unless $value; # empty value
303
304 $value =~ s/^\s+|[ \t\r\n]+$//gs; # remove leading and trailing white space
305 $value =~ s/\n/ /g; # flatten into one line
306 $value =~ s/\s*;\s*/; /g; # squash white space around semicolons
307 #print "SQUASH $key: $value\n";
291308 return "$key: $value";
292309 }
293310
296313
297314 =head1 AUTHORS
298315
299 Marc Bradshaw, E<lt>marc@marcbradshaw.netE<gt>,
300316 Bron Gondwana, E<lt>brong@fastmailteam.comE<gt>,
301317 John Levine, E<lt>john.levine@standcore.comE<gt>
302318