Authentication/Tester/HandlerTester.pm

filter: Statementstmt 97.8% 186 / 190 Branchbran 78.8% 41 / 52 Conditioncond 76.4% 13 / 17 MC/DCmcdc 50.0% 5 / 10 Subroutinesub 93.7% 30 / 32 Podpod 100% 20 / 20 totaltotal 91.9% 295 / 321 CC 39 SCAR 22.1
CC
39
Cov
93%
new
18.2
run
16.1
get_authresults_header
4.0
CRAP 9.2
« Authentication/App/Blocker/App/Command/delete.pm ↑ Summary Authentication/Handler.pm »
1 package Mail::Milter::Authentication::Tester::HandlerTester;
2 29 ▶use 5.20.0;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 29 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 29 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 29 ▶use Mail::Milter::Authentication::Pragmas;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 # ABSTRACT: Class for testing handlers
7 29 our $VERSION = '4.20261122'; # VERSION
8 29 ▶use Mail::Milter::Authentication;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 29 ▶use Mail::Milter::Authentication::Protocol::Milter;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 29 ▶use Mail::Milter::Authentication::Protocol::SMTP;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 29 ▶use File::Temp qw{ tempdir };
Subroutine BEGIN: called CC=1 SCAR=0.0
12 29 ▶use Net::DNS::Resolver::Mock;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 29 ▶use Net::IP;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 97 ▶sub _build_config_smtp {
Subroutine _build_config_smtp: called CC=1 SCAR=0.0
16 97 my ( $self, $handler_config ) = @_;
17  
18 97 my $config = {
19  
20 '_is_test' => 1,
21 'debug' => 1,
22 'dryrun' => 0,
23 'logtoerr' => 1,
24 'error_log' => 'tmp/smtp.err',
25 'connection' => 'unix:tmp/authentication_milter_test.sock',
26 'umask' => '0000',
27 'connect_timeout' => 55,
28 'command_timeout' => 55,
29 'content_timeout' => 595,
30 'tempfail_on_error' => 1,
31 'tempfail_on_error_authenticated' => 1,
32 'tempfail_on_error_local' => 1,
33 'tempfail_on_error_trusted' => 1,
34  
35 '#metric_connection' => 'unix:tmp/authentication_milter_test_metrics.sock',
36 '#metric_umask' => '0000',
37  
38 'protocol' => 'smtp',
39 'smtp' => {
40 'sock_type' => 'unix',
41 'sock_path' => 'tmp/authentication_milter_smtp_out.sock',
42 'pipeline_limit' => '4',
43 },
44  
45 'handlers' => $handler_config,
46  
47 };
48  
49 97 return $config;
50 }
51  
52 11 ▶sub _build_config_milter {
Subroutine _build_config_milter: called CC=1 SCAR=0.0
53 11 my ( $self, $handler_config ) = @_;
54  
55 11 my $config = {
56  
57 '_is_test' => 1,
58 'debug' => 1,
59 'dryrun' => 0,
60 'logtoerr' => 1,
61 'error_log' => 'tmp/milter.err',
62 'connection' => 'unix:tmp/authentication_milter_test.sock',
63 'umask' => '0000',
64 'connect_timeout' => 55,
65 'command_timeout' => 55,
66 'content_timeout' => 595,
67 'tempfail_on_error' => 1,
68 'tempfail_on_error_authenticated' => 1,
69 'tempfail_on_error_local' => 1,
70 'tempfail_on_error_trusted' => 1,
71  
72 '#metric_connection' => 'unix:tmp/authentication_milter_test_metrics.sock',
73 '#metric_umask' => '0000',
74  
75 'protocol' => 'milter',
76  
77 'handlers' => $handler_config,
78  
79 };
80  
81 11 return $config;
82 }
83  
84 108 ▶sub new {
Subroutine new: called CC=18 SCAR=29.0
Pod documented
85 108 my ( $class, $args ) = @_;
86 108 my $self = {};
87 108 bless $self, $class;
88  
89 108 $self->{ 'snapshots' } = {};
90  
91 108 foreach my $arg ( qw{ prefix zonefile zonedata } ) {
92 324 ▶ $self->{ $arg } = $args->{ $arg } if exists $args->{ $arg };
Branch: 1 branch
BranchTrueFalseTotal
if exists $args->{$arg} 215 109 324
93 }
94  
95 108 ▶ croak 'prefix must be supplies' if ! exists $self->{ 'prefix' };
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"prefix"} 0 108 108
96 108 ▶ croak 'zonefile or zonedata cannot both be supplied' if ( exists $self->{ 'zonefile' } ) && ( exists $self->{ 'zonedata' });
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"zonefile"} and exists $self->{"zonedata"} 0 108 108
Condition66%2/3
exists $self->{"zonefile"} and exists $self->{"zonedata"}
!ll&&!rl&&r
75330
MC/DC0%0/2
exists $self->{"zonefile"} and exists $self->{"zonedata"}
exists $self->{"zonefile"} exists $self->{"zonedata"}
Truth table66%2/3
A and B
A= exists $self->{"zonefile"}B= exists $self->{"zonedata"}
ABresult
0X0
100
111
97 108 ▶ $self->{ 'zonedata' } = q{} if ( ! exists $self->{ 'zonefile' } ) && ( ! exists $self->{ 'zonedata' });
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"zonefile"} or exists $self->{"zonedata"} 1 107 108
Condition100%3/3
exists $self->{"zonefile"} or exists $self->{"zonedata"}
l!l&&r!l&&!r
33741
MC/DC100%2/2
exists $self->{"zonefile"} or exists $self->{"zonedata"}
exists $self->{"zonefile"} exists $self->{"zonedata"}
Truth table100%3/3
A or B
A= exists $self->{"zonefile"}B= exists $self->{"zonedata"}
ABresult
1X1
011
000
98  
99 108 ▶ my $protocol = $args->{ 'protocol' } // 'smtp';
Condition100%2/2
$args->{"protocol"} // "smtp"
l!l
2187
MC/DC100%1/1
$args->{"protocol"} // "smtp"
$args->{"protocol"}
Truth table100%2/2
A
A= $args->{"protocol"}
Aresult
11
00
100  
101 108 ▶ if ( exists( $args->{ 'handler_config' } ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args->{"handler_config"}) 108 0 108
102 108 ▶ if ( $protocol eq 'smtp' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($protocol eq 'smtp') { } 97 11 108
103 97 set_config( $self->_build_config_smtp( $args->{ 'handler_config' } ) );
104 }
105 else {
106 11 set_config( $self->_build_config_milter( $args->{ 'handler_config' } ) );
107 }
108 }
109  
110 108 $Mail::Milter::Authentication::Config::PREFIX = $self->{ 'prefix' };
111  
112 108 my $config = get_config();
113  
114 # Give each tester its own working directories unless the config sets
115 # them, so tests can run in parallel without sharing state such as the
116 # metrics store.
117 108 foreach my $type ( qw{ cache lib spool } ) {
118 324 ▶ $config->{ $type . '_dir' } //= tempdir( "authmilter-test-$type-XXXXXX", TMPDIR => 1, CLEANUP => 1 );
Condition33%1/3
$$config{$type . '_dir'} //= tempdir("authmilter-test-$type-XXXXXX", "TMPDIR", 1, "CLEANUP", 1)
l!l&&r!l&&!r
03240
MC/DC0%0/2
$$config{$type . '_dir'} //= tempdir("authmilter-test-$type-XXXXXX", "TMPDIR", 1, "CLEANUP", 1)
$$config{$type . '_dir'} tempdir("authmilter-test-$type-XXXXXX", "TMPDIR", 1, "CLEANUP", 1)
Truth table33%1/3
A //= B
A= $$config{$type . '_dir'}B= tempdir("authmilter-test-$type-XXXXXX", "TMPDIR", 1, "CLEANUP", 1)
ABresult
1X1
011
000
119 }
120  
121 108 Mail::Milter::Authentication::Config::setup_config;
122  
123 108 my $Resolver = Net::DNS::Resolver::Mock->new();
124 108 ▶ $Resolver->zonefile_read( $self->{ 'zonefile' } ) if exists $self->{ 'zonefile' };
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"zonefile"} 33 75 108
125 108 ▶ $Resolver->zonefile_parse( $self->{ 'zonedata' } ) if exists $self->{ 'zonedata' };
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"zonedata"} 75 33 108
126 108 $Mail::Milter::Authentication::Handler::TestResolver = $Resolver;
127  
128 # Setup a new authentication milter object
129 108 my $authmilter = Mail::Milter::Authentication->new();
130 108 $authmilter->{'metric'} = Mail::Milter::Authentication::Metric->new($authmilter);
131 108 $authmilter->{'config'} = $config;
132  
133 # if ( $protocol eq 'smtp' ) {
134 108 push @Mail::Milter::Authentication::ISA, 'Mail::Milter::Authentication::Protocol::SMTP';
135 #}
136 #else {
137 #push @Mail::Milter::Authentication::ISA, 'Mail::Milter::Authentication::Protocol::Milter';
138 #}
139  
140 # Setup a fake server object
141 108 $authmilter->{ 'server' }->{ 'ppid' } = $PID;
142  
143 # Load handlers
144 108 foreach my $name ( @{$config->{'load_handlers'}} ) {
145 246 $authmilter->load_handler( $name );
146  
147 246 my $package = "Mail::Milter::Authentication::Handler::$name";
148 246 my $object = $package->new( $authmilter );
149 246 ▶ if ( $object->can( 'pre_loop_setup' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($object->can("pre_loop_setup")) 39 207 246
150 39 $object->pre_loop_setup();
151 }
152 246 ▶ if ( $object->can( 'register_metrics' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($object->can("register_metrics")) 246 0 246
153 246 $authmilter->{'metric'}->register_metrics( $object->register_metrics() );
154 }
155  
156 }
157  
158 # Init handlers
159  
160 108 my $callbacks_list = {};
161 108 my $callbacks = {};
162 108 my $handler = {};
163 108 my $object = {};
164 108 my $object_maker = {};
165 108 my $count = 0;
166  
167 108 $authmilter->{'callbacks_list'} = $callbacks_list;
168 108 $authmilter->{'callbacks'} = $callbacks;
169 108 $authmilter->{'count'} = $count;
170 108 $authmilter->{'handler'} = $handler;
171 108 $authmilter->{'object'} = $object;
172 108 $authmilter->{'object_maker'} = $object_maker;
173  
174 108 $authmilter->setup_handlers();
175  
176 108 $self->{ 'authmilter' } = $authmilter;
177  
178 108 $self->handler()->top_setup_callback();
179  
180 108 $self->snapshot( '_new' );
181  
182 108 return $self;
183 }
184  
185 120 ▶sub snapshot {
Subroutine snapshot: called CC=1 SCAR=0.0
Pod documented
186 120 my ( $self, $name ) = @_;
187 120 my $snapshot = clone( $self->{ 'authmilter' } );
188 120 $self->{ 'snapshots' }->{ $name } = $snapshot;
189 }
190  
191 309 ▶sub switch {
Subroutine switch: called CC=2 SCAR=7.0
Pod documented
192 309 my ( $self, $name ) = @_;
193 309 ▶ croak 'unknown snapshot' if ! exists ( $self->{ 'snapshots' }->{ $name } );
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"snapshots"}{$name} 0 309 309
194 309 my $snapshot = clone( $self->{ 'snapshots' }->{ $name } );
195 309 $self->{ 'authmilter' } = $snapshot;
196 }
197  
198 5141 ▶sub handler {
Subroutine handler: called CC=1 SCAR=0.0
Pod documented
199 5141 my ( $self ) = @_;
200 5141 return $self->{ 'authmilter' }->{ 'handler' }->{ '_Handler' };
201 }
202  
203 269 ▶sub connect { ## no critic
Subroutine connect: called CC=2 SCAR=7.0
Pod documented
204 269 my ( $self, $name, $ip ) = @_;
205 269 my $authmilter = $self->{ 'authmilter' };
206 269 ▶ my $ip_obj = eval{ Net::IP->new( $ip ) } // undef;
Condition50%1/2
eval { do { "Net::IP"->new($ip) } } // undef
l!l
2690
MC/DC0%0/1
eval { do { "Net::IP"->new($ip) } } // undef
eval { do { "Net::IP"->new($ip) } }
Truth table50%1/2
A
A= eval { do { "Net::IP"->new($ip) } }
Aresult
11
00
207 # An undef here should not make it through to handlers, however
208 # for testing we will allow this.
209 269 $self->handler()->remap_connect_callback( $name, $ip_obj );
210 269 return $self->handler()->top_connect_callback( $name, $self->handler()->{ 'ip_object' } );
211 }
212  
213 263 ▶sub helo {
Subroutine helo: called CC=1 SCAR=0.0
Pod documented
214 263 my ( $self, $helo ) = @_;
215 263 $self->handler()->remap_helo_callback( $helo );
216 263 return $self->handler()->top_helo_callback( $self->handler()->{ 'helo_name' } );
217 }
218  
219 262 ▶sub mailfrom {
Subroutine mailfrom: called CC=1 SCAR=0.0
Pod documented
220 262 my ( $self, $from ) = @_;
221 262 return $self->handler()->top_envfrom_callback( $from );
222 }
223  
224 261 ▶sub rcptto {
Subroutine rcptto: called CC=1 SCAR=0.0
Pod documented
225 261 my ( $self, $to ) = @_;
226 261 return $self->handler()->top_envrcpt_callback( $to );
227 }
228  
229 1114 ▶sub header {
Subroutine header: called CC=2 SCAR=7.1
Pod documented
230 1114 my ( $self, $key, $value, $original ) = @_;
231 1114 ▶ $original = "$key: $value" if ! defined $original;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $original 1114 0 1114
232 1114 return $self->handler()->top_header_callback( $key, $value, $original );
233 }
234  
235 259 ▶sub end_of_headers {
Subroutine end_of_headers: called CC=1 SCAR=0.0
Pod documented
236 259 my ( $self ) = @_;
237 259 return $self->handler()->top_eoh_callback();
238 }
239  
240 259 ▶sub body {
Subroutine body: called CC=1 SCAR=0.0
Pod documented
241 259 my ( $self, $body ) = @_;
242 259 return $self->handler()->top_body_callback( $body );
243 }
244  
245 259 ▶sub end_of_message {
Subroutine end_of_message: called CC=1 SCAR=0.0
Pod documented
246 259 my ( $self ) = @_;
247 259 return $self->handler()->top_eom_callback();
248 }
249  
250 62 ▶sub close { ## no critic
Subroutine close: called CC=1 SCAR=0.0
Pod documented
251 62 my ( $self ) = @_;
252 62 return $self->handler()->top_close_callback();
253 }
254  
255 0 ▶sub abort {
Subroutine abort: not called CC=1 SCAR=6.9
Pod documented
256 0 my ( $self ) = @_;
257 0 return $self->handler()->top_abort_callback();
258 }
259  
260 246 ▶sub addheader {
Subroutine addheader: called CC=1 SCAR=0.0
Pod documented
261 246 my ( $self ) = @_;
262 246 return $self->handler()->top_addheader_callback();
263 }
264  
265 268 ▶sub run {
Subroutine run: called CC=16 SCAR=27.8
Pod documented
266 268 my ( $self, $args ) = @_;
267  
268 268 $self->switch( '_new' );
269  
270 268 my $returncode;
271 268 $returncode = $self->connect( $args->{ 'connect_name' }, $args->{ 'connect_ip' } );
272 268 ▶ die 'connect' if ( $returncode != SMFIS_CONTINUE );
Branch: 1 branch
BranchTrueFalseTotal
if $returncode != 100 6 262 268
273 262 $returncode = $self->helo( $args->{ 'helo' } );
274 262 ▶ die 'helo' if ( $returncode != SMFIS_CONTINUE );
Branch: 1 branch
BranchTrueFalseTotal
if $returncode != 100 1 261 262
275 261 $returncode = $self->mailfrom( $args->{ 'mailfrom' } );
276 261 ▶ die 'mailfrom' if ( $returncode != SMFIS_CONTINUE );
Branch: 1 branch
BranchTrueFalseTotal
if $returncode != 100 1 260 261
277 260 foreach my $rcptto ( @{ $args->{ 'rcptto' } } ) {
278 260 $returncode = $self->rcptto( $rcptto );
279 260 ▶ die 'rcptto ' . $rcptto if ( $returncode != SMFIS_CONTINUE );
Branch: 1 branch
BranchTrueFalseTotal
if $returncode != 100 1 259 260
280 }
281  
282 259 my $body = $args->{ 'body' };
283 259 $body =~ s/\r?\n/\n/g;
284  
285 259 my @lines = split( /\n/, $body );
286  
287 # Process headers
288 259 my $buffer = q{};
289 259 ▶ while ( my $line = shift @lines ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $line = shift @lines) 1782 258 2040
290 1782 chomp $line;
291 1782 ▶ last if $line eq q{};
Branch: 1 branch
BranchTrueFalseTotal
if $line eq "" 0 1782 1782
292  
293 1782 ▶ if ( $line =~ /^\s/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^\s/u) { } 669 1113 1782
294 669 $buffer .= "\n" . $line;
295 }
296 else {
297 1113 ▶ if ( $buffer ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($buffer) 854 259 1113
298 854 my ( $key, $value ) = split( ':', $buffer, 2 );
299 854 $key =~ s/\s+$//;
300 854 $value =~ s/^\s+//;
301 854 $returncode = $self->header( $key, $value );
302 854 ▶ die "header $key: $value" if ( $returncode != SMFIS_CONTINUE );
Branch: 1 branch
BranchTrueFalseTotal
if $returncode != 100 1 853 854
303 }
304 1112 $buffer = $line;
305 }
306  
307 }
308 258 ▶ if ( $buffer ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($buffer) 258 0 258
309 258 my ( $key, $value ) = split( ':', $buffer, 2 );
310 258 $key =~ s/\s+$//;
311 258 $value =~ s/^\s+//;
312 258 $returncode = $self->header( $key, $value );
313 258 ▶ die "header $key: $value" if ( $returncode != SMFIS_CONTINUE );
Branch: 1 branch
BranchTrueFalseTotal
if $returncode != 100 0 258 258
314 }
315  
316 258 $returncode = $self->end_of_headers();
317 258 ▶ die 'eoh' if ( $returncode != SMFIS_CONTINUE );
Branch: 1 branch
BranchTrueFalseTotal
if $returncode != 100 0 258 258
318  
319 258 $returncode = $self->body( join( "\n", @lines) . "\n" );
320 258 ▶ die 'body' if ( $returncode != SMFIS_CONTINUE );
Branch: 1 branch
BranchTrueFalseTotal
if $returncode != 100 0 258 258
321  
322 258 $returncode = $self->end_of_message();
323 258 ▶ die 'body' if ( $returncode != SMFIS_CONTINUE );
Branch: 1 branch
BranchTrueFalseTotal
if $returncode != 100 12 246 258
324  
325 246 $self->addheader();
326 # $self->close();
327 }
328  
329 0 ▶sub get_return {
Subroutine get_return: not called CC=1 SCAR=6.9
Pod documented
330 0 my ( $self ) = @_;
331 0 return $self->handler()->get_return();
332 }
333  
334 13 ▶sub get_reject_mail {
Subroutine get_reject_mail: called CC=1 SCAR=0.0
Pod documented
335 13 my ( $self ) = @_;
336 13 return $self->handler()->get_reject_mail();
337 }
338  
339 323 ▶sub servername {
Subroutine servername: called CC=1 SCAR=0.0
Pod documented
340 323 my ( $self ) = @_;
341 323 return 'handlertester.test.authmilter.org';
342 }
343  
344 323 ▶sub get_authresults_header {
Subroutine get_authresults_header: called CC=4 SCAR=13.9
Pod documented
345 323 my ( $self ) = @_;
346 # Build a Mail::AuthenticationReslts object
347 323 ▶ my $c_auth_headers = eval{ clone( $self->handler()->{ 'c_auth_headers'}->{'Authentication-Results'} ) } // [];
Condition100%2/2
eval { do { &clone($self->handler->{"c_auth_headers"}{"Authentication-Results"}) } } // []
l!l
45278
MC/DC100%1/1
eval { do { &clone($self->handler->{"c_auth_headers"}{"Authentication-Results"}) } } // []
eval { do { &clone($self->handler->{"c_auth_headers"}{"Authentication-Results"}) } }
Truth table100%2/2
A
A= eval { do { &clone($self->handler->{"c_auth_headers"}{"Authentication-Results"}) } }
Aresult
11
00
348 323 ▶ my $auth_headers = eval{ clone( $self->handler()->{ 'auth_headers'}->{'Authentication-Results'} ) } // [];
Condition100%2/2
eval { do { &clone($self->handler->{"auth_headers"}{"Authentication-Results"}) } } // []
l!l
26954
MC/DC100%1/1
eval { do { &clone($self->handler->{"auth_headers"}{"Authentication-Results"}) } } // []
eval { do { &clone($self->handler->{"auth_headers"}{"Authentication-Results"}) } }
Truth table100%2/2
A
A= eval { do { &clone($self->handler->{"auth_headers"}{"Authentication-Results"}) } }
Aresult
11
00
349 323 my @added_ar_headers = ( @{ $c_auth_headers }, @{ $auth_headers } );
350 323 my $header = Mail::AuthenticationResults::Header->new()->set_value( Mail::AuthenticationResults::Header::AuthServID->new()->set_value( $self->servername() ) );
351 323 foreach my $ar_header ( @added_ar_headers ) {
352 924 eval{ $ar_header->orphan(); }; # Remove parent for testing.
353 924 $header->add_child( $ar_header );
354 }
355 323 return $header;
356 }
357  
358 29 1;
359  
360 __END__
« Authentication/App/Blocker/App/Command/delete.pm ↑ Summary Authentication/Handler.pm »