Authentication/Tester.pm

filter: Statementstmt 91.4% 352 / 385 Branchbran 63.2% 105 / 166 Conditioncond 48.4% 16 / 33 MC/DCmcdc 22.2% 4 / 18 Subroutinesub 97.1% 34 / 35 Podpod 0.0% 0 / 11 totaltotal 78.8% 511 / 648 CC 111 SCAR 30.8
CC
111
Cov
81%
__ANON__
103.1
smtpput
31.5
smtpcat
26.6
CRAP 21.7
« Authentication/Handler/IPRev.pm ↑ Summary Authentication/Handler/XGoogleDKIM.pm »
1 package Mail::Milter::Authentication::Tester;
2 112 ▶use 5.20.0;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 112 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 112 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 112 ▶use Mail::Milter::Authentication::Pragmas;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 # ABSTRACT: Class used for testing
7 112 our $VERSION = '4.20261122'; # VERSION
8 112 ▶use Mail::Milter::Authentication;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 112 ▶use Mail::Milter::Authentication::Client;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 112 ▶use Mail::Milter::Authentication::Protocol::Milter;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 112 ▶use Mail::Milter::Authentication::Protocol::SMTP;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 112 ▶use Cwd qw{ cwd };
Subroutine BEGIN: called CC=1 SCAR=0.0
13 112 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 112 ▶use IO::Socket::INET;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 112 ▶use IO::Socket::UNIX;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 112 ▶use Net::DNS::Resolver::Mock 1.20171219;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 112 ▶use Test::File::Contents;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 112 ▶use Test::More;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 112 ▶use Time::HiRes qw{ sleep time };
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 112 our @ISA = qw{ Exporter }; ## no critic
22 112 our @EXPORT = qw{ start_milter stop_milter get_metrics test_metrics smtp_process smtp_process_multi milter_process smtpput send_smtp_packet smtpcat }; ## no critic
23  
24 112 my $base_dir = cwd();
25  
26 112 our $MASTER_PROCESS_PID = $$;
27  
28  
29 {
30 112 my $milter_pid;
31  
32 292 ▶ sub start_milter {
Subroutine start_milter: called CC=7 SCAR=19.7
Pod undocumented
33 292 my ( $prefix ) = @_;
34  
35 292 ▶ return if $milter_pid;
Branch: 1 branch
BranchTrueFalseTotal
if $milter_pid 0 292 292
36  
37 292 ▶ if ( ! -e $prefix . '/authentication_milter.json' ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-e $prefix . '/authentication_milter.json') 0 292 292
38 0 die "Could not find config";
39 }
40  
41 292 system "cp $prefix/mail-dmarc.ini .";
42  
43 # Remove anything left from a previous run, so we can tell when this one is listening
44 292 my @sockets = _unix_socket_paths( $prefix );
45 292 unlink 'tmp/authentication_milter.pid', @sockets;
46  
47 292 $milter_pid = fork();
48 292 ▶ die "unable to fork: $!" unless defined($milter_pid);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $milter_pid 0 292 292
49 292 ▶ if (!$milter_pid) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($milter_pid) 34 258 292
50 34 $Mail::Milter::Authentication::Config::PREFIX = $prefix;
51 34 $Mail::Milter::Authentication::Config::IDENT = 'test_authentication_milter_test';
52 # Keep runtime files, including metrics, with this run rather than in a
53 # directory shared by every run, so test files can run in parallel
54 34 my $config = Mail::Milter::Authentication::Config::get_config();
55 34 foreach my $type ( qw{ cache lib spool } ) {
56 102 my $dir = File::Spec->rel2abs( "tmp/$type" );
57 102 mkdir $dir;
58 102 ▶ $config->{ $type . '_dir' } //= $dir;
Condition33%1/3
$$config{$type . '_dir'} //= $dir
l!l&&r!l&&!r
01020
MC/DC0%0/2
$$config{$type . '_dir'} //= $dir
$$config{$type . '_dir'} $dir
Truth table33%1/3
A //= B
A= $$config{$type . '_dir'}B= $dir
ABresult
1X1
011
000
59 }
60 34 my $Resolver = Net::DNS::Resolver::Mock->new();
61 34 $Resolver->zonefile_read( 'zonefile' );
62 34 $Mail::Milter::Authentication::Handler::TestResolver = $Resolver;
63 34 Mail::Milter::Authentication::start({
64 'pid_file' => 'tmp/authentication_milter.pid',
65 'daemon' => 0,
66 });
67 0 die;
68 }
69  
70 258 ▶ _wait_for( 'milter to start', sub { -s 'tmp/authentication_milter.pid' && ! grep { ! -S $_ } @sockets } );
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
-s "tmp/authentication_milter.pid" and not grep({not -S $_;} @sockets) 581 319 900
71 # The socket file is created when bound, just before it starts listening
72 258 sleep 0.1;
73 258 open my $pid_file, '<', 'tmp/authentication_milter.pid';
74 258 $milter_pid = <$pid_file>;
75 258 close $pid_file;
76 258 print "Milter started at pid $milter_pid\n";
77 258 return;
78 }
79  
80 196 ▶ sub stop_milter {
Subroutine stop_milter: called CC=2 SCAR=6.9
Pod undocumented
81 196 ▶ return if ! $milter_pid;
Branch: 1 branch
BranchTrueFalseTotal
unless $milter_pid 7 189 196
82 189 kill( 'HUP', $milter_pid );
83 189 waitpid ($milter_pid,0);
84 189 print "Milter killed at pid $milter_pid\n";
85 189 undef $milter_pid;
86 189 unlink 'tmp/authentication_milter.pid';
87 189 unlink 'mail-dmarc.ini';
88 189 return;
89 }
90  
91 112 ▶ END {
Subroutine END: called CC=2 SCAR=6.9
92 112 ▶ return if $MASTER_PROCESS_PID != $$;
Branch: 1 branch
BranchTrueFalseTotal
if $Mail::Milter::Authentication::Tester::MASTER_PROCESS_PID != $$ 105 7 112
93 7 stop_milter();
94 }
95 }
96  
97 # The unix socket paths the milter with this config will listen on
98 112 ▶sub _unix_socket_paths {
Subroutine _unix_socket_paths: called CC=3 SCAR=11.3
99 292 my ( $prefix ) = @_;
100 292 my $config = Mail::Milter::Authentication::Config::load_file( $prefix . '/authentication_milter.json' );
101 292 my @connections = ( $config->{'connection'}, $config->{'metric_connection'} );
102 0 ▶ push @connections, map { $_->{'connection'} } values %{ $config->{'connections'} // {} };
Condition50%1/2
$config->{"connections"} // {}
l!l
0292
MC/DC0%0/1
$config->{"connections"} // {}
$config->{"connections"}
Truth table50%1/2
A
A= $config->{"connections"}
Aresult
11
00
103 292 ▶ return map { /^unix:(.+)$/ ? $1 : () } grep { defined } @connections;
Branch: 1 branch
BranchTrueFalseTotal
/^unix:(.+)$/u ? : 584 0 584
104 }
105  
106 # Wait for a condition to become true, polling rather than sleeping for a fixed time
107 258 ▶sub _wait_for {
Subroutine _wait_for: called CC=4 SCAR=14.0
108 258 my ( $what, $check, $timeout ) = @_;
109 258 ▶ my $deadline = time + ( $timeout // 30 );
Condition50%1/2
$timeout // 30
l!l
0258
MC/DC0%0/1
$timeout // 30
$timeout
Truth table50%1/2
A
A= $timeout
Aresult
11
00
110 258 ▶ while ( ! $check->() ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (&$check()) 642 258 900
111 642 ▶ die "Timed out waiting for $what\n" if time > $deadline;
Branch: 1 branch
BranchTrueFalseTotal
if &Mail::Milter::Authentication::Tester::time() > $deadline 0 642 642
112 642 sleep 0.05;
113 }
114 258 return;
115 }
116  
117 13736 ▶sub get_metrics {
Subroutine get_metrics: called CC=5 SCAR=16.1
Pod undocumented
118 13736 my ( $path ) = @_;
119  
120 13736 my $sock = IO::Socket::UNIX->new(
121 'Peer' => $path,
122 );
123  
124 13736 print $sock "GET /metrics HTTP/1.0\n\n";
125  
126 13736 my $data = {};
127  
128 13736 ▶ while ( my $line = <$sock> ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = readline $sock)) 41208 0 41208
129 41208 chomp $line;
130 41208 ▶ last if $line eq q{};
Branch: 1 branch
BranchTrueFalseTotal
if $line eq "" 13736 27472 41208
131 }
132 13736 ▶ while ( my $line = <$sock> ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = readline $sock)) 1941862 13736 1955598
133 1941862 chomp $line;
134 1941862 ▶ next if $line =~ /^#/;
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /^#/u 652066 1289796 1941862
135 1289796 $line =~ /^(.*)\{(.*)\} (.*)$/;
136 1289796 my $count_id = $1;
137 1289796 my $labels = $2;
138 1289796 my $count = $3;
139 1289796 $data->{ $count_id . '{' . $labels . '}' } = $count;
140 }
141  
142 13736 return $data;
143 }
144  
145 # Some metrics vary between runs, so are compared by whether they are set
146 768652 ▶sub _metric_value {
Subroutine _metric_value: called CC=9 SCAR=23.6
147 768652 my ( $key, $value ) = @_;
148 768652 ▶ return ( $value // 0 ) > 0 if $key =~ /seconds_total/;
Branch: 1 branch
BranchTrueFalseTotal
if $key =~ /seconds_total/u 396230 372422 768652
Condition50%1/2
$value // 0
l!l
3962300
MC/DC0%0/1
$value // 0
$value
Truth table50%1/2
A
A= $value
Aresult
11
00
149 372422 ▶ return ( $value // 0 ) > 0 if $key =~ /microseconds_sum/;
Branch: 1 branch
BranchTrueFalseTotal
if $key =~ /microseconds_sum/u 0 372422 372422
Condition0%0/2
$value // 0
l!l
00
MC/DC0%0/1
$value // 0
$value
Truth table0%0/2
A
A= $value
Aresult
11
00
150 372422 ▶ return ( $value // 0 ) > 0 if $key =~ /authmilter_forked_children_total/;
Branch: 1 branch
BranchTrueFalseTotal
if $key =~ /authmilter_forked_children_total/u 9010 363412 372422
Condition50%1/2
$value // 0
l!l
90100
MC/DC0%0/1
$value // 0
$value
Truth table50%1/2
A
A= $value
Aresult
11
00
151 363412 ▶ return ( $value // 0 ) > -1 if $key =~ /authmilter_processes_/;
Branch: 1 branch
BranchTrueFalseTotal
if $key =~ /authmilter_processes_/u 18214 345198 363412
Condition50%1/2
$value // 0
l!l
182140
MC/DC0%0/1
$value // 0
$value
Truth table50%1/2
A
A= $value
Aresult
11
00
152 345198 return $value;
153 }
154  
155 13736 ▶sub _metrics_match {
Subroutine _metrics_match: called CC=5 SCAR=16.1
156 13736 my ( $metrics, $data ) = @_;
157 13736 foreach my $key ( keys %$data ) {
158 371469 my $got = _metric_value( $key, $metrics->{ $key } );
159 371469 my $want = _metric_value( $key, $data->{ $key } );
160 371469 ▶ return 0 if ( $got // q{} ) ne ( $want // q{} );
Branch: 1 branch
BranchTrueFalseTotal
if ($got // "") ne ($want // "") 13688 357781 371469
Condition75%3/4
$got // ""
l!l
35778113688
$want // ""
l!l
3714690
MC/DC100%1/1
$got // ""
$got
MC/DC0%0/1
$want // ""
$want
Truth table100%2/2
A
A= $got
Aresult
11
00
Truth table50%1/2
A
A= $want
Aresult
11
00
161 }
162 48 return 1;
163 }
164  
165 189 ▶sub test_metrics {
Subroutine test_metrics: called CC=7 SCAR=25.3
Pod undocumented
166 189 my ( $expected ) = @_;
167  
168 189 my $j = JSON::XS->new();
169  
170 189 my $data;
171 189 ▶ if ( -e $expected ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-e $expected) 189 0 189
172 189 open my $InF, '<', $expected;
173 189 my @content = <$InF>;
174 189 close $InF;
175 189 $data = $j->decode( join( q{}, @content ) );
176 189 for my $key ( sort keys %$data ) {
177 14614 ▶ next unless $key =~ /^authmilter_version/;
Branch: 1 branch
BranchTrueFalseTotal
unless $key =~ /^authmilter_version/u 12857 1757 14614
178 1757 delete $data->{$key};
179 }
180 }
181  
182 # The server updates metrics after replying, so wait for it to catch up,
183 # but not for long if they never match, the test will then report the differences
184 189 my $metrics = get_metrics( 'tmp/authentication_milter_test_metrics.sock' );
185 189 ▶ if ( $data ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($data) 189 0 189
186 189 my $deadline = time + 10;
187 189 ▶ while ( ! _metrics_match( $metrics, $data ) && time < $deadline ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not _metrics_match($metrics, $data) and &Mail::Milter::Authentication::Tester::time() < $deadline) 13547 189 13736
Condition100%3/3
not _metrics_match($metrics, $data) and &Mail::Milter::Authentication::Tester::time() < $deadline
!ll&&!rl&&r
4814113547
MC/DC100%2/2
not _metrics_match($metrics, $data) and &Mail::Milter::Authentication::Tester::time() < $deadline
not _metrics_match($metrics, $data) &Mail::Milter::Authentication::Tester::time() < $deadline
Truth table100%3/3
not(A) and B
A= not _metrics_match($metrics, $data)B= &Mail::Milter::Authentication::Tester::time() < $deadline
ABresult
0X0
100
111
188 13547 sleep 0.1;
189 13547 $metrics = get_metrics( 'tmp/authentication_milter_test_metrics.sock' );
190 }
191 }
192  
193 189 ▶ subtest $expected => sub {
Subroutine __ANON__: called CC=13 SCAR=46.4
194  
195 189 ▶ if ( $data ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($data) { } 189 0 189
196  
197 189 plan tests => scalar keys %$data;
198  
199 189 foreach my $key ( sort keys %$data ) {
200 12857 is( _metric_value( $key, $metrics->{ $key } ), _metric_value( $key, $data->{ $key } ), "Metrics $expected $key" );
201 }
202  
203 }
204 else {
205 0 fail( 'Metrics data does not exist' );
206 }
207  
208 189 ▶ if ( $ENV{'WRITE_METRICS'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ENV{"WRITE_METRICS"}) 0 189 189
209 0 foreach my $key ( sort keys %$metrics ) {
210 0 ▶ if ( $key =~ /seconds_total/ ) {
Branch: 4 branches
BranchTrueFalseTotal
if ($key =~ /seconds_total/u) { } 0 0 0
elsif ($key =~ /microseconds_sum/u) { } 0 0 0
elsif ($key =~ /authmilter_forked_children_total/u) { } 0 0 0
elsif ($key =~ /authmilter_processes_/u) { } 0 0 0
211 0 ▶ $metrics->{ $key } = 123456 if $metrics->{ $key } > 0;
Branch: 1 branch
BranchTrueFalseTotal
if $metrics->{$key} > 0 0 0 0
212 }
213 elsif ( $key =~ /microseconds_sum/ ) {
214 0 ▶ $metrics->{ $key } = 123456 if $metrics->{ $key } > 0;
Branch: 1 branch
BranchTrueFalseTotal
if $metrics->{$key} > 0 0 0 0
215 }
216 elsif ( $key =~ /authmilter_forked_children_total/ ) {
217 0 ▶ $metrics->{ $key } = 123456 if $metrics->{ $key } > 0;
Branch: 1 branch
BranchTrueFalseTotal
if $metrics->{$key} > 0 0 0 0
218 }
219 elsif ( $key =~ /authmilter_processes_/) {
220 0 ▶ $metrics->{ $key } = 123456 if $metrics->{ $key } > -1;
Branch: 1 branch
BranchTrueFalseTotal
if $metrics->{$key} > -1 0 0 0
221 }
222 }
223 0 open my $OutF, '>', $expected;
224 0 $j->pretty();
225 0 $j->canonical();
226 0 print $OutF $j->encode( $metrics );
227 0 close $OutF;
228 }
229  
230 189 };
231 }
232  
233 953 ▶sub smtp_process {
Subroutine smtp_process: called CC=5 SCAR=16.4
Pod undocumented
234 953 my ( $args ) = @_;
235  
236 953 ▶ if ( ! -e $args->{'prefix'} . '/authentication_milter.json' ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-e $args->{"prefix"} . '/authentication_milter.json') 0 953 953
237 0 die "Could not find config " . $args->{'prefix'};
238 }
239 953 ▶ if ( ! -e 'data/source/' . $args->{'source'} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-e 'data/source/' . $args->{"source"}) 0 953 953
240 0 die "Could not find source";
241 }
242  
243 my $catargs = {
244 'sock_type' => 'unix',
245 'sock_path' => 'tmp/authentication_milter_smtp_out.sock',
246 'remove' => [10,11],
247 953 'output' => 'tmp/result/' . $args->{'dest'},
248 };
249 953 unlink 'tmp/authentication_milter_smtp_out.sock';
250 953 my $cat_pid;
251 953 ▶ if ( ! $args->{'no_cat'} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($args->{"no_cat"}) 932 21 953
252 932 $cat_pid = smtpcat( $catargs );
253 }
254  
255 my $return = smtpput({
256 'sock_type' => 'unix',
257 'sock_path' => 'tmp/authentication_milter_test.sock',
258 'mailer_name' => 'test.module',
259 'connect_ip' => [ $args->{'ip'} ],
260 'connect_name' => [ $args->{'name'} ],
261 'helo_host' => [ $args->{'name'} ],
262 'mail_from' => [ $args->{'from'} ],
263 'rcpt_to' => [ $args->{'to'} ],
264 'mail_file' => [ 'data/source/' . $args->{'source'} ],
265 919 'eom_expect' => $args->{'eom_expect'},
266 });
267  
268 919 ▶ if ( ! $args->{'no_cat'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $args->{'no_cat'}) { } 898 21 919
269 898 waitpid( $cat_pid,0 );
270 898 files_eq_or_diff( 'data/example/' . $args->{'dest'}, 'tmp/result/' . $args->{'dest'}, 'smtp ' . $args->{'desc'} );
271 }
272 else {
273 21 is( $return, 1, 'SMTP Put Returned ok' );
274 }
275 }
276  
277 97 ▶sub smtp_process_multi {
Subroutine smtp_process_multi: called CC=8 SCAR=20.8
Pod undocumented
278 97 my ( $args ) = @_;
279  
280 97 ▶ if ( ! -e $args->{'prefix'} . '/authentication_milter.json' ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-e $args->{"prefix"} . '/authentication_milter.json') 0 97 97
281 0 die "Could not find config";
282 }
283  
284 # Hardcoded lines to remove in subsequent messages
285 # If you change the source email then change the awk
286 # numbers here too.
287 # This could be better!
288  
289 my $catargs = {
290 'sock_type' => 'unix',
291 'sock_path' => 'tmp/authentication_milter_smtp_out.sock',
292 'remove' => $args->{'filter'},
293 97 'output' => 'tmp/result/' . $args->{'dest'},
294 };
295 97 unlink 'tmp/authentication_milter_smtp_out.sock';
296 97 my $cat_pid = smtpcat( $catargs );
297  
298 95 my $putargs = {
299 'sock_type' => 'unix',
300 'sock_path' => 'tmp/authentication_milter_test.sock',
301 'mailer_name' => 'test.module',
302 'connect_ip' => [],
303 'connect_name' => [],
304 'helo_host' => [],
305 'mail_from' => [],
306 'rcpt_to' => [],
307 'mail_file' => [],
308 };
309  
310 95 foreach my $item ( @{$args->{'ip'}} ) {
311 426 push @{$putargs->{'connect_ip'}}, $item;
312 }
313 95 foreach my $item ( @{$args->{'name'}} ) {
314 426 push @{$putargs->{'connect_name'}}, $item;
315 }
316 95 foreach my $item ( @{$args->{'name'}} ) {
317 426 push @{$putargs->{'helo_host'}}, $item;
318 }
319 95 foreach my $item ( @{$args->{'from'}} ) {
320 426 push @{$putargs->{'mail_from'}}, $item;
321 }
322 95 foreach my $item ( @{$args->{'to'}} ) {
323 426 push @{$putargs->{'rcpt_to'}}, $item;
324 }
325 95 foreach my $item ( @{$args->{'source'}} ) {
326 426 push @{$putargs->{'mail_file'}}, 'data/source/' . $item;
327 }
328 #warn 'Testing ' . $args->{'source'} . ' > ' . $args->{'dest'} . "\n";
329  
330 95 smtpput( $putargs );
331  
332 95 waitpid( $cat_pid,0 );
333  
334 95 files_eq_or_diff( 'data/example/' . $args->{'dest'}, 'tmp/result/' . $args->{'dest'}, 'smtp ' . $args->{'desc'} );
335 }
336  
337 885 ▶sub milter_process {
Subroutine milter_process: called CC=3 SCAR=12.3
Pod undocumented
338 885 my ( $args ) = @_;
339  
340 885 ▶ if ( ! -e $args->{'prefix'} . '/authentication_milter.json' ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-e $args->{"prefix"} . '/authentication_milter.json') 0 885 885
341 0 die "Could not find config";
342 }
343 885 ▶ if ( ! -e 'data/source/' . $args->{'source'} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-e 'data/source/' . $args->{"source"}) 0 885 885
344 0 die "Could not find source";
345 }
346  
347 client({
348 'prefix' => $args->{'prefix'},
349 'mailer_name' => 'test.module',
350 'mail_file' => 'data/source/' . $args->{'source'},
351 'connect_ip' => $args->{'ip'},
352 'connect_name' => $args->{'name'},
353 'helo_host' => $args->{'name'},
354 'mail_from' => $args->{'from'},
355 'rcpt_to' => $args->{'to'},
356 885 'output' => 'tmp/result/' . $args->{'dest'},
357 });
358  
359 852 files_eq_or_diff( 'data/example/' . $args->{'dest'}, 'tmp/result/' . $args->{'dest'}, 'milter ' . $args->{'desc'} );
360 }
361  
362 1016 ▶sub smtpput {
Subroutine smtpput: called CC=23 SCAR=34.5
Pod undocumented
363 1016 my ( $args ) = @_;
364  
365 1016 my $mailer_name = $args->{'mailer_name'};
366  
367 1016 my $mail_file_a = $args->{'mail_file'};
368 1016 my $mail_from_a = $args->{'mail_from'};
369 1016 my $rcpt_to_a = $args->{'rcpt_to'};
370 1016 my $x_name_a = $args->{'connect_name'};
371 1016 my $x_addr_a = $args->{'connect_ip'};
372 1016 my $x_helo_a = $args->{'helo_host'};
373  
374 1016 my $sock_type = $args->{'sock_type'};
375 1016 my $sock_path = $args->{'sock_path'};
376 1016 my $sock_host = $args->{'sock_host'};
377 1016 my $sock_port = $args->{'sock_port'};
378  
379 1016 ▶ my $eom_expect = $args->{'eom_expect'} || '250';
Condition100%2/2
$args->{'eom_expect'} || '250'
l!l
21995
MC/DC100%1/1
$args->{'eom_expect'} || '250'
$args->{'eom_expect'}
Truth table100%2/2
A
A= $args->{'eom_expect'}
Aresult
11
00
380  
381 1016 my $sock;
382 1016 ▶ if ( $sock_type eq 'inet' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($sock_type eq 'inet') { } 0 1016 1016
elsif ($sock_type eq 'unix') { } 1016 0 1016
383 0 ▶ $sock = IO::Socket::INET->new(
Condition0%0/2
'IO::Socket::INET'->new('Proto', 'tcp', 'PeerAddr', $sock_host, 'PeerPort', $sock_port) || die("could not open outbound SMTP socket: $!")
l!l
00
MC/DC0%0/1
'IO::Socket::INET'->new('Proto', 'tcp', 'PeerAddr', $sock_host, 'PeerPort', $sock_port) || die("could not open outbound SMTP socket: $!")
'IO::Socket::INET'->new('Proto', 'tcp', 'PeerAddr', $sock_host, 'PeerPort', $sock_port)
Truth table0%0/2
A
A= 'IO::Socket::INET'->new('Proto', 'tcp', 'PeerAddr', $sock_host, 'PeerPort', $sock_port)
Aresult
11
00
384 'Proto' => 'tcp',
385 'PeerAddr' => $sock_host,
386 'PeerPort' => $sock_port,
387 ) || die "could not open outbound SMTP socket: $!";
388 }
389 elsif ( $sock_type eq 'unix' ) {
390 1016 ▶ $sock = IO::Socket::UNIX->new(
Condition50%1/2
'IO::Socket::UNIX'->new('Peer', $sock_path) || die("could not open outbound SMTP socket: $!")
l!l
10160
MC/DC0%0/1
'IO::Socket::UNIX'->new('Peer', $sock_path) || die("could not open outbound SMTP socket: $!")
'IO::Socket::UNIX'->new('Peer', $sock_path)
Truth table50%1/2
A
A= 'IO::Socket::UNIX'->new('Peer', $sock_path)
Aresult
11
00
391 'Peer' => $sock_path,
392 ) || die "could not open outbound SMTP socket: $!";
393 }
394  
395 1016 my $line = <$sock>;
396  
397 1016 ▶ if ( ($line =~ /^4/) || ($line =~ /^5/) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^4/u or $line =~ /^5/u) 0 1016 1016
Condition33%1/3
$line =~ /^4/u or $line =~ /^5/u
l!l&&r!l&&!r
001016
MC/DC0%0/2
$line =~ /^4/u or $line =~ /^5/u
$line =~ /^4/u $line =~ /^5/u
Truth table33%1/3
A or B
A= $line =~ /^4/uB= $line =~ /^5/u
ABresult
1X1
011
000
398 0 die "Unexpected SMTP response $line";
399 }
400  
401 1016 ▶ send_smtp_packet( $sock, 'EHLO ' . $mailer_name, '250' ) || die;
Branch: 1 branch
BranchTrueFalseTotal
send_smtp_packet($sock, 'EHLO ' . $mailer_name, 250) or die 1016 0 1016
402  
403 1016 my $first_time = 1;
404  
405 1016 ▶ while ( @$mail_from_a ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$mail_from_a) 1302 969 2271
406  
407 1302 ▶ if ( ! $first_time ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($first_time) 286 1016 1302
408 286 ▶ if ( ! send_smtp_packet( $sock, 'RSET', '250' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (send_smtp_packet($sock, "RSET", 250)) 47 239 286
409 47 $sock->close();
410 47 return;
411 };
412 }
413 1255 $first_time = 0;
414  
415 1255 my $mail_file = shift @$mail_file_a;
416 1255 my $mail_from = shift @$mail_from_a;
417 1255 my $rcpt_to = shift @$rcpt_to_a;
418 1255 my $x_name = shift @$x_name_a;
419 1255 my $x_addr = shift @$x_addr_a;
420 1255 my $x_helo = shift @$x_helo_a;
421  
422 1255 my $mail_data = q{};
423  
424 1255 ▶ if ( $mail_file eq '-' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mail_file eq '-') { } 0 1255 1255
425 0 ▶ while ( my $l = <> ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $l = readline ARGV)) 0 0 0
426 0 $mail_data .= $l;
427 }
428 }
429 else {
430 1255 ▶ if ( ! -e $mail_file ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-e $mail_file) 0 1255 1255
431 0 die "Mail file $mail_file does not exist";
432 }
433 1255 open my $inf, '<', $mail_file;
434 1255 my @all = <$inf>;
435 1255 $mail_data = join( q{}, @all );
436 1255 close $inf;
437 }
438  
439 1255 $mail_data =~ s/\015?\012/\015\012/g;
440 # Handle transparency
441 1255 $mail_data =~ s/\015\012\./\015\012\.\./g;
442  
443 1255 ▶ send_smtp_packet( $sock, 'XFORWARD NAME=' . $x_name, '250' ) || die;
Branch: 1 branch
BranchTrueFalseTotal
send_smtp_packet($sock, 'XFORWARD NAME=' . $x_name, 250) or die 1255 0 1255
444 1255 ▶ send_smtp_packet( $sock, 'XFORWARD ADDR=' . $x_addr, '250' ) || die;
Branch: 1 branch
BranchTrueFalseTotal
send_smtp_packet($sock, 'XFORWARD ADDR=' . $x_addr, 250) or die 1255 0 1255
445 1255 ▶ send_smtp_packet( $sock, 'XFORWARD HELO=' . $x_helo, '250' ) || die;
Branch: 1 branch
BranchTrueFalseTotal
send_smtp_packet($sock, 'XFORWARD HELO=' . $x_helo, 250) or die 1255 0 1255
446  
447 1255 ▶ send_smtp_packet( $sock, 'MAIL FROM:' . $mail_from, '250' ) || die;
Branch: 1 branch
BranchTrueFalseTotal
send_smtp_packet($sock, 'MAIL FROM:' . $mail_from, 250) or die 1255 0 1255
448 1255 ▶ send_smtp_packet( $sock, 'RCPT TO:' . $rcpt_to, '250' ) || die;
Branch: 1 branch
BranchTrueFalseTotal
send_smtp_packet($sock, 'RCPT TO:' . $rcpt_to, 250) or die 1255 0 1255
449 1255 ▶ send_smtp_packet( $sock, 'DATA', '354' ) || die;
Branch: 1 branch
BranchTrueFalseTotal
send_smtp_packet($sock, "DATA", 354) or die 1255 0 1255
450  
451 1255 print $sock $mail_data;
452 1255 print $sock "\r\n";
453  
454 1255 ▶ send_smtp_packet( $sock, '.', $eom_expect ) || return 0;
Branch: 1 branch
BranchTrueFalseTotal
send_smtp_packet($sock, ".", $eom_expect) or return 0 1255 0 1255
455  
456 }
457  
458 969 ▶ send_smtp_packet( $sock, 'QUIT', '221' ) || return 0;
Branch: 1 branch
BranchTrueFalseTotal
send_smtp_packet($sock, "QUIT", 221) or return 0 969 0 969
459 969 $sock->close();
460  
461 969 return 1;
462 }
463  
464 11056 ▶sub send_smtp_packet {
Subroutine send_smtp_packet: called CC=4 SCAR=13.9
Pod undocumented
465 11056 my ( $socket, $send, $expect ) = @_;
466 11056 print $socket "$send\r\n";
467 11056 my $recv = <$socket>;
468 11056 ▶ $recv = '' if !defined $recv;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $recv 0 11056 11056
469 11056 ▶ while ( $recv =~ /^\d\d\d\-/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($recv =~ /^\d\d\d\-/u) 4056 11056 15112
470 4056 $recv = <$socket>;
471 }
472 11056 ▶ if ( $recv =~ /^$expect/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($recv =~ /^$expect/u) { } 11009 47 11056
473 11009 return 1;
474 }
475 else {
476 47 $recv =~ s/\r?\n?$//;
477 47 $send =~ s/\r?\n?$//;
478 47 warn "SMTP Send expected \"$expect\" received \"$recv\" when sending \"$send\"\n";
479 47 return 0;
480 }
481 }
482  
483 1033 ▶sub smtpcat {
Subroutine smtpcat: called CC=23 SCAR=32.8
Pod undocumented
484 1033 my ( $args ) = @_;
485  
486 1033 my $sock_type = $args->{'sock_type'};
487 1033 my $sock_path = $args->{'sock_path'};
488 1033 my $sock_host = $args->{'sock_host'};
489 1033 my $sock_port = $args->{'sock_port'};
490  
491 1033 my $remove = $args->{'remove'};
492 1033 my $output = $args->{'output'};
493  
494 1033 my @out_lines;
495  
496 my $sock;
497 1033 ▶ if ( $sock_type eq 'inet' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($sock_type eq 'inet') { } 0 1033 1033
elsif ($sock_type eq 'unix') { } 1033 0 1033
498 0 ▶ $sock = IO::Socket::INET->new(
Condition0%0/2
'IO::Socket::INET'->new('Listen', 5, 'LocalHost', $sock_host, 'LocalPort', $sock_port, 'Protocol', 'tcp') || die("could not open socket: $!")
l!l
00
MC/DC0%0/1
'IO::Socket::INET'->new('Listen', 5, 'LocalHost', $sock_host, 'LocalPort', $sock_port, 'Protocol', 'tcp') || die("could not open socket: $!")
'IO::Socket::INET'->new('Listen', 5, 'LocalHost', $sock_host, 'LocalPort', $sock_port, 'Protocol', 'tcp')
Truth table0%0/2
A
A= 'IO::Socket::INET'->new('Listen', 5, 'LocalHost', $sock_host, 'LocalPort', $sock_port, 'Protocol', 'tcp')
Aresult
11
00
499 'Listen' => 5,
500 'LocalHost' => $sock_host,
501 'LocalPort' => $sock_port,
502 'Protocol' => 'tcp',
503 ) || die "could not open socket: $!";
504 }
505 elsif ( $sock_type eq 'unix' ) {
506 # Some filesystems briefly refuse to bind a path whose previous socket
507 # is still closing, so retry for a short while
508 1033 my $deadline = time + 10;
509 1033 ▶ until ( $sock ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($sock) 1033 0 1033
510 1033 unlink $sock_path;
511 1033 $sock = IO::Socket::UNIX->new(
512 'Listen' => 5,
513 'Local' => $sock_path,
514 );
515 1033 ▶ last if $sock;
Branch: 1 branch
BranchTrueFalseTotal
if $sock 1033 0 1033
516 0 ▶ die "could not open socket $sock_path: $!" if time > $deadline;
Branch: 1 branch
BranchTrueFalseTotal
if &Mail::Milter::Authentication::Tester::time() > $deadline 0 0 0
517 0 sleep 0.05;
518 }
519 }
520  
521 # Listen before forking, so the socket is ready when we return
522 1033 my $cat_pid = fork();
523 1033 ▶ die "unable to fork: $!" unless defined($cat_pid);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $cat_pid 0 1033 1033
524 1033 ▶ if ( $cat_pid ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cat_pid) 995 38 1033
525 995 $sock->close();
526 995 return $cat_pid;
527 }
528  
529 38 my $accept = $sock->accept();
530  
531 38 print $accept "220 smtp.cat ESMTP Test\r\n";
532  
533 0 ▶ local $SIG{'ALRM'} = sub{ die "Timeout\n" };
Subroutine __ANON__: not called CC=1 SCAR=1.2
534 38 alarm( 60 );
535  
536 38 while ( 1 ) {
537 353 my $command = <$accept>;
538 353 ▶ last unless defined $command;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $command 0 353 353
539 353 alarm( 60 );
540  
541 353 ▶ if ( $command =~ /^HELO/ ) {
Branch: 8 branches
BranchTrueFalseTotal
if ($command =~ /^HELO/u) { } 0 353 353
elsif ($command =~ /^EHLO/u) { } 38 315 353
elsif ($command =~ /^MAIL/u) { } 45 270 315
elsif ($command =~ /^XFORWARD/u) { } 135 135 270
elsif ($command =~ /^RCPT/u) { } 45 90 135
elsif ($command =~ /^RSET/u) { } 7 83 90
elsif ($command =~ /^DATA/u) { } 45 38 83
elsif ($command =~ /^QUIT/u) { } 38 0 38
542 0 push @out_lines, $command;
543 0 print $accept "250 HELO Ok\r\n";
544 }
545 elsif ( $command =~ /^EHLO/ ) {
546 38 push @out_lines, $command;
547 38 print $accept "250 EHLO Ok\r\n";
548 }
549 elsif ( $command =~ /^MAIL/ ) {
550 45 push @out_lines, $command;
551 45 print $accept "250 MAIL Ok\r\n";
552 }
553 elsif ( $command =~ /^XFORWARD/ ) {
554 135 push @out_lines, $command;
555 135 print $accept "250 XFORWARD Ok\r\n";
556 }
557 elsif ( $command =~ /^RCPT/ ) {
558 45 push @out_lines, $command;
559 45 print $accept "250 RCPT Ok\r\n";
560 }
561 elsif ( $command =~ /^RSET/ ) {
562 7 push @out_lines, $command;
563 7 print $accept "250 RSET Ok\r\n";
564 }
565 elsif ( $command =~ /^DATA/ ) {
566 45 push @out_lines, $command;
567 45 print $accept "354 Send\r\n";
568 DATA:
569 45 ▶ while ( my $line = <$accept> ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = readline $accept)) 3059 0 3059
570 3059 alarm( 60 );
571 3059 push @out_lines, $line;
572 3059 ▶ last DATA if $line eq ".\r\n";
Branch: 1 branch
BranchTrueFalseTotal
if $line eq ".\r\n" 45 3014 3059
573 # Handle transparency
574 3014 ▶ if ( $line =~ /^\./ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^\./u) 28 2986 3014
575 28 $line = substr( $line, 1 );
576 }
577 }
578 45 print $accept "250 DATA Ok\r\n";
579 }
580 elsif ( $command =~ /^QUIT/ ) {
581 38 push @out_lines, $command;
582 38 print $accept "221 Bye\r\n";
583 38 last;
584 }
585 else {
586 0 push @out_lines, $command;
587 0 print $accept "250 Unknown Ok\r\n";
588 }
589 }
590  
591 38 open my $file, '>', $output;
592 38 my $i = 0;
593 38 foreach my $line ( @out_lines ) {
594 3412 $i++;
595 3412 ▶ $line = "############\n" if grep { $i == $_ } @$remove;
Branch: 1 branch
BranchTrueFalseTotal
if grep {$i == $_;} @$remove 82 3330 3412
596 3412 print $file $line;
597 }
598 38 close $file;
599  
600 38 $accept->close();
601 38 $sock->close();
602  
603 38 exit 0;
604 }
605  
606 885 ▶sub client {
Subroutine client: called CC=3 SCAR=11.0
Pod undocumented
607 885 my ( $args ) = @_;
608 885 my $pid = fork();
609 885 ▶ die "unable to fork: $!" unless defined($pid);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $pid 0 885 885
610 885 ▶ if ( ! $pid ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($pid) 33 852 885
611  
612 33 my $output = $args->{'output'};
613 33 delete $args->{'output'};
614  
615 33 $Mail::Milter::Authentication::Config::PREFIX = $args->{'prefix'};
616 33 delete $args->{'prefix'};
617 33 $args->{'testing'} = 1;
618  
619 33 my $client = Mail::Milter::Authentication::Client->new( $args );
620  
621 33 $client->process();
622  
623 33 open my $file, '>', $output;
624 33 print $file $client->result();
625 33 close $file;
626 33 exit 0;
627  
628 }
629 852 waitpid( $pid, 0 );
630 }
631  
632 112 1;
633  
634 __END__
« Authentication/Handler/IPRev.pm ↑ Summary Authentication/Handler/XGoogleDKIM.pm »