Authentication/Tester/HandlerTester.pm

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