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/Handler/Size.pm ↑ Summary Authentication/Handler.pm »
1 package Mail::Milter::Authentication::Tester::HandlerTester;
2 28 ▶use 5.20.0;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 28 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 28 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 28 ▶use Mail::Milter::Authentication::Pragmas;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 # ABSTRACT: Class for testing handlers
7 28 our $VERSION = '4.20261111'; # VERSION
8 28 ▶use Mail::Milter::Authentication;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 28 ▶use Mail::Milter::Authentication::Protocol::Milter;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 28 ▶use Mail::Milter::Authentication::Protocol::SMTP;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 28 ▶use Net::DNS::Resolver::Mock;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 28 ▶use Net::IP;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 96 ▶sub _build_config_smtp {
Subroutine _build_config_smtp: called CC=1 SCAR=0.0
15 96 my ( $self, $handler_config ) = @_;
16  
17 96 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 96 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 107 ▶sub new {
Subroutine new: called CC=16 SCAR=27.8
Pod documented
84 107 my ( $class, $args ) = @_;
85 107 my $self = {};
86 107 bless $self, $class;
87  
88 107 $self->{ 'snapshots' } = {};
89  
90 107 foreach my $arg ( qw{ prefix zonefile zonedata } ) {
91 321 ▶ $self->{ $arg } = $args->{ $arg } if exists $args->{ $arg };
Branch: 1 branch
BranchTrueFalseTotal
if exists $args->{$arg} 213 108 321
92 }
93  
94 107 ▶ croak 'prefix must be supplies' if ! exists $self->{ 'prefix' };
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"prefix"} 0 107 107
95 107 ▶ 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 107 107
Condition66%2/3
exists $self->{"zonefile"} and exists $self->{"zonedata"}
!ll&&!rl&&r
75320
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 107 ▶ $self->{ 'zonedata' } = q{} if ( ! exists $self->{ 'zonefile' } ) && ( ! exists $self->{ 'zonedata' });
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"zonefile"} or exists $self->{"zonedata"} 1 106 107
Condition100%3/3
exists $self->{"zonefile"} or exists $self->{"zonedata"}
l!l&&r!l&&!r
32741
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 107 ▶ my $protocol = $args->{ 'protocol' } // 'smtp';
Condition100%2/2
$args->{"protocol"} // "smtp"
l!l
2186
MC/DC100%1/1
$args->{"protocol"} // "smtp"
$args->{"protocol"}
Truth table100%2/2
A
A= $args->{"protocol"}
Aresult
11
00
99  
100 107 ▶ if ( exists( $args->{ 'handler_config' } ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args->{"handler_config"}) 107 0 107
101 107 ▶ if ( $protocol eq 'smtp' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($protocol eq 'smtp') { } 96 11 107
102 96 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 107 $Mail::Milter::Authentication::Config::PREFIX = $self->{ 'prefix' };
110 107 Mail::Milter::Authentication::Config::setup_config;
111  
112 107 my $config = get_config();
113  
114 107 my $Resolver = Net::DNS::Resolver::Mock->new();
115 107 ▶ $Resolver->zonefile_read( $self->{ 'zonefile' } ) if exists $self->{ 'zonefile' };
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"zonefile"} 32 75 107
116 107 ▶ $Resolver->zonefile_parse( $self->{ 'zonedata' } ) if exists $self->{ 'zonedata' };
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"zonedata"} 75 32 107
117 107 $Mail::Milter::Authentication::Handler::TestResolver = $Resolver;
118  
119 # Setup a new authentication milter object
120 107 my $authmilter = Mail::Milter::Authentication->new();
121 107 $authmilter->{'metric'} = Mail::Milter::Authentication::Metric->new($authmilter);
122 107 $authmilter->{'config'} = $config;
123  
124 # if ( $protocol eq 'smtp' ) {
125 107 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 107 $authmilter->{ 'server' }->{ 'ppid' } = $PID;
133  
134 # Load handlers
135 107 foreach my $name ( @{$config->{'load_handlers'}} ) {
136 229 $authmilter->load_handler( $name );
137  
138 229 my $package = "Mail::Milter::Authentication::Handler::$name";
139 229 my $object = $package->new( $authmilter );
140 229 ▶ if ( $object->can( 'pre_loop_setup' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($object->can("pre_loop_setup")) 36 193 229
141 36 $object->pre_loop_setup();
142 }
143 229 ▶ if ( $object->can( 'register_metrics' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($object->can("register_metrics")) 229 0 229
144 229 $authmilter->{'metric'}->register_metrics( $object->register_metrics() );
145 }
146  
147 }
148  
149 # Init handlers
150  
151 107 my $callbacks_list = {};
152 107 my $callbacks = {};
153 107 my $handler = {};
154 107 my $object = {};
155 107 my $object_maker = {};
156 107 my $count = 0;
157  
158 107 $authmilter->{'callbacks_list'} = $callbacks_list;
159 107 $authmilter->{'callbacks'} = $callbacks;
160 107 $authmilter->{'count'} = $count;
161 107 $authmilter->{'handler'} = $handler;
162 107 $authmilter->{'object'} = $object;
163 107 $authmilter->{'object_maker'} = $object_maker;
164  
165 107 $authmilter->setup_handlers();
166  
167 107 $self->{ 'authmilter' } = $authmilter;
168  
169 107 $self->handler()->top_setup_callback();
170  
171 107 $self->snapshot( '_new' );
172  
173 107 return $self;
174 }
175  
176 119 ▶sub snapshot {
Subroutine snapshot: called CC=1 SCAR=0.0
Pod documented
177 119 my ( $self, $name ) = @_;
178 119 my $snapshot = clone( $self->{ 'authmilter' } );
179 119 $self->{ 'snapshots' }->{ $name } = $snapshot;
180 }
181  
182 308 ▶sub switch {
Subroutine switch: called CC=2 SCAR=7.0
Pod documented
183 308 my ( $self, $name ) = @_;
184 308 ▶ croak 'unknown snapshot' if ! exists ( $self->{ 'snapshots' }->{ $name } );
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"snapshots"}{$name} 0 308 308
185 308 my $snapshot = clone( $self->{ 'snapshots' }->{ $name } );
186 308 $self->{ 'authmilter' } = $snapshot;
187 }
188  
189 5120 ▶sub handler {
Subroutine handler: called CC=1 SCAR=0.0
Pod documented
190 5120 my ( $self ) = @_;
191 5120 return $self->{ 'authmilter' }->{ 'handler' }->{ '_Handler' };
192 }
193  
194 268 ▶sub connect { ## no critic
Subroutine connect: called CC=2 SCAR=7.0
Pod documented
195 268 my ( $self, $name, $ip ) = @_;
196 268 my $authmilter = $self->{ 'authmilter' };
197 268 ▶ my $ip_obj = eval{ Net::IP->new( $ip ) } // undef;
Condition50%1/2
eval { do { "Net::IP"->new($ip) } } // undef
l!l
2680
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 268 $self->handler()->remap_connect_callback( $name, $ip_obj );
201 268 return $self->handler()->top_connect_callback( $name, $self->handler()->{ 'ip_object' } );
202 }
203  
204 262 ▶sub helo {
Subroutine helo: called CC=1 SCAR=0.0
Pod documented
205 262 my ( $self, $helo ) = @_;
206 262 $self->handler()->remap_helo_callback( $helo );
207 262 return $self->handler()->top_helo_callback( $self->handler()->{ 'helo_name' } );
208 }
209  
210 261 ▶sub mailfrom {
Subroutine mailfrom: called CC=1 SCAR=0.0
Pod documented
211 261 my ( $self, $from ) = @_;
212 261 return $self->handler()->top_envfrom_callback( $from );
213 }
214  
215 260 ▶sub rcptto {
Subroutine rcptto: called CC=1 SCAR=0.0
Pod documented
216 260 my ( $self, $to ) = @_;
217 260 return $self->handler()->top_envrcpt_callback( $to );
218 }
219  
220 1108 ▶sub header {
Subroutine header: called CC=2 SCAR=7.1
Pod documented
221 1108 my ( $self, $key, $value, $original ) = @_;
222 1108 ▶ $original = "$key: $value" if ! defined $original;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $original 1108 0 1108
223 1108 return $self->handler()->top_header_callback( $key, $value, $original );
224 }
225  
226 258 ▶sub end_of_headers {
Subroutine end_of_headers: called CC=1 SCAR=0.0
Pod documented
227 258 my ( $self ) = @_;
228 258 return $self->handler()->top_eoh_callback();
229 }
230  
231 258 ▶sub body {
Subroutine body: called CC=1 SCAR=0.0
Pod documented
232 258 my ( $self, $body ) = @_;
233 258 return $self->handler()->top_body_callback( $body );
234 }
235  
236 258 ▶sub end_of_message {
Subroutine end_of_message: called CC=1 SCAR=0.0
Pod documented
237 258 my ( $self ) = @_;
238 258 return $self->handler()->top_eom_callback();
239 }
240  
241 61 ▶sub close { ## no critic
Subroutine close: called CC=1 SCAR=0.0
Pod documented
242 61 my ( $self ) = @_;
243 61 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 245 ▶sub addheader {
Subroutine addheader: called CC=1 SCAR=0.0
Pod documented
252 245 my ( $self ) = @_;
253 245 return $self->handler()->top_addheader_callback();
254 }
255  
256 267 ▶sub run {
Subroutine run: called CC=16 SCAR=27.8
Pod documented
257 267 my ( $self, $args ) = @_;
258  
259 267 $self->switch( '_new' );
260  
261 267 my $returncode;
262 267 $returncode = $self->connect( $args->{ 'connect_name' }, $args->{ 'connect_ip' } );
263 267 ▶ die 'connect' if ( $returncode != SMFIS_CONTINUE );
Branch: 1 branch
BranchTrueFalseTotal
if $returncode != 100 6 261 267
264 261 $returncode = $self->helo( $args->{ 'helo' } );
265 261 ▶ die 'helo' if ( $returncode != SMFIS_CONTINUE );
Branch: 1 branch
BranchTrueFalseTotal
if $returncode != 100 1 260 261
266 260 $returncode = $self->mailfrom( $args->{ 'mailfrom' } );
267 260 ▶ die 'mailfrom' if ( $returncode != SMFIS_CONTINUE );
Branch: 1 branch
BranchTrueFalseTotal
if $returncode != 100 1 259 260
268 259 foreach my $rcptto ( @{ $args->{ 'rcptto' } } ) {
269 259 $returncode = $self->rcptto( $rcptto );
270 259 ▶ die 'rcptto ' . $rcptto if ( $returncode != SMFIS_CONTINUE );
Branch: 1 branch
BranchTrueFalseTotal
if $returncode != 100 1 258 259
271 }
272  
273 258 my $body = $args->{ 'body' };
274 258 $body =~ s/\r?\n/\n/g;
275  
276 258 my @lines = split( /\n/, $body );
277  
278 # Process headers
279 258 my $buffer = q{};
280 258 ▶ while ( my $line = shift @lines ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $line = shift @lines) 1776 257 2033
281 1776 chomp $line;
282 1776 ▶ last if $line eq q{};
Branch: 1 branch
BranchTrueFalseTotal
if $line eq "" 0 1776 1776
283  
284 1776 ▶ if ( $line =~ /^\s/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^\s/u) { } 669 1107 1776
285 669 $buffer .= "\n" . $line;
286 }
287 else {
288 1107 ▶ if ( $buffer ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($buffer) 849 258 1107
289 849 my ( $key, $value ) = split( ':', $buffer, 2 );
290 849 $key =~ s/\s+$//;
291 849 $value =~ s/^\s+//;
292 849 $returncode = $self->header( $key, $value );
293 849 ▶ die "header $key: $value" if ( $returncode != SMFIS_CONTINUE );
Branch: 1 branch
BranchTrueFalseTotal
if $returncode != 100 1 848 849
294 }
295 1106 $buffer = $line;
296 }
297  
298 }
299 257 ▶ if ( $buffer ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($buffer) 257 0 257
300 257 my ( $key, $value ) = split( ':', $buffer, 2 );
301 257 $key =~ s/\s+$//;
302 257 $value =~ s/^\s+//;
303 257 $returncode = $self->header( $key, $value );
304 257 ▶ die "header $key: $value" if ( $returncode != SMFIS_CONTINUE );
Branch: 1 branch
BranchTrueFalseTotal
if $returncode != 100 0 257 257
305 }
306  
307 257 $returncode = $self->end_of_headers();
308 257 ▶ die 'eoh' if ( $returncode != SMFIS_CONTINUE );
Branch: 1 branch
BranchTrueFalseTotal
if $returncode != 100 0 257 257
309  
310 257 $returncode = $self->body( join( "\n", @lines) . "\n" );
311 257 ▶ die 'body' if ( $returncode != SMFIS_CONTINUE );
Branch: 1 branch
BranchTrueFalseTotal
if $returncode != 100 0 257 257
312  
313 257 $returncode = $self->end_of_message();
314 257 ▶ die 'body' if ( $returncode != SMFIS_CONTINUE );
Branch: 1 branch
BranchTrueFalseTotal
if $returncode != 100 12 245 257
315  
316 245 $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 28 1;
350  
351 __END__
« Authentication/Handler/Size.pm ↑ Summary Authentication/Handler.pm »