Authentication/Tester.pm

filter: Statementstmt 90.7% 304 / 335 Branchbran 63.1% 91 / 144 Conditioncond 38.4% 5 / 13 MC/DCmcdc 14.2% 1 / 7 Subroutinesub 96.4% 27 / 28 Podpod 0.0% 0 / 11 totaltotal 79.5% 428 / 538 CC 89 SCAR 31.7
CC
89
Cov
81%
__ANON__
71.1
smtpput
31.5
smtpcat
22.9
CRAP 23.8
« Authentication/App/ProcessLog/App/Command/arex.pm ↑ Summary Authentication/Handler/RBLDNS.pm »
1 package Mail::Milter::Authentication::Tester;
2 113 ▶use 5.20.0;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 113 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 113 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 113 ▶use Mail::Milter::Authentication::Pragmas;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 # ABSTRACT: Class used for testing
7 113 our $VERSION = '4.20261116'; # VERSION
8 113 ▶use Mail::Milter::Authentication;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 113 ▶use Mail::Milter::Authentication::Client;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 113 ▶use Mail::Milter::Authentication::Protocol::Milter;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 113 ▶use Mail::Milter::Authentication::Protocol::SMTP;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 113 ▶use Cwd qw{ cwd };
Subroutine BEGIN: called CC=1 SCAR=0.0
13 113 ▶use IO::Socket::INET;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 113 ▶use IO::Socket::UNIX;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 113 ▶use Net::DNS::Resolver::Mock 1.20171219;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 113 ▶use Test::File::Contents;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 113 ▶use Test::More;
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 113 our @ISA = qw{ Exporter }; ## no critic
20 113 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
21  
22 113 my $base_dir = cwd();
23  
24 113 our $MASTER_PROCESS_PID = $$;
25  
26  
27 {
28 113 my $milter_pid;
29  
30 293 ▶ sub start_milter {
Subroutine start_milter: called CC=5 SCAR=16.3
Pod undocumented
31 293 my ( $prefix ) = @_;
32  
33 293 ▶ return if $milter_pid;
Branch: 1 branch
BranchTrueFalseTotal
if $milter_pid 0 293 293
34  
35 293 ▶ if ( ! -e $prefix . '/authentication_milter.json' ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-e $prefix . '/authentication_milter.json') 0 293 293
36 0 die "Could not find config";
37 }
38  
39 293 system "cp $prefix/mail-dmarc.ini .";
40  
41 293 $milter_pid = fork();
42 293 ▶ die "unable to fork: $!" unless defined($milter_pid);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $milter_pid 0 293 293
43 293 ▶ if (!$milter_pid) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($milter_pid) 35 258 293
44 35 $Mail::Milter::Authentication::Config::PREFIX = $prefix;
45 35 $Mail::Milter::Authentication::Config::IDENT = 'test_authentication_milter_test';
46 35 my $Resolver = Net::DNS::Resolver::Mock->new();
47 35 $Resolver->zonefile_read( 'zonefile' );
48 35 $Mail::Milter::Authentication::Handler::TestResolver = $Resolver;
49 35 Mail::Milter::Authentication::start({
50 'pid_file' => 'tmp/authentication_milter.pid',
51 'daemon' => 0,
52 });
53 0 die;
54 }
55  
56 258 sleep 5;
57 258 open my $pid_file, '<', 'tmp/authentication_milter.pid';
58 258 $milter_pid = <$pid_file>;
59 258 close $pid_file;
60 258 print "Milter started at pid $milter_pid\n";
61 258 return;
62 }
63  
64 196 ▶ sub stop_milter {
Subroutine stop_milter: called CC=2 SCAR=6.9
Pod undocumented
65 196 ▶ return if ! $milter_pid;
Branch: 1 branch
BranchTrueFalseTotal
unless $milter_pid 7 189 196
66 189 kill( 'HUP', $milter_pid );
67 189 waitpid ($milter_pid,0);
68 189 print "Milter killed at pid $milter_pid\n";
69 189 undef $milter_pid;
70 189 unlink 'tmp/authentication_milter.pid';
71 189 unlink 'mail-dmarc.ini';
72 189 return;
73 }
74  
75 113 ▶ END {
Subroutine END: called CC=2 SCAR=6.9
76 113 ▶ return if $MASTER_PROCESS_PID != $$;
Branch: 1 branch
BranchTrueFalseTotal
if $Mail::Milter::Authentication::Tester::MASTER_PROCESS_PID != $$ 106 7 113
77 7 stop_milter();
78 }
79 }
80  
81 113 ▶sub get_metrics {
Subroutine get_metrics: called CC=5 SCAR=16.1
Pod undocumented
82 189 my ( $path ) = @_;
83  
84 189 my $sock = IO::Socket::UNIX->new(
85 'Peer' => $path,
86 );
87  
88 189 print $sock "GET /metrics HTTP/1.0\n\n";
89  
90 189 my $data = {};
91  
92 189 ▶ while ( my $line = <$sock> ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = readline $sock)) 567 0 567
93 567 chomp $line;
94 567 ▶ last if $line eq q{};
Branch: 1 branch
BranchTrueFalseTotal
if $line eq "" 189 378 567
95 }
96 189 ▶ while ( my $line = <$sock> ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = readline $sock)) 22190 189 22379
97 22190 chomp $line;
98 22190 ▶ next if $line =~ /^#/;
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /^#/u 7762 14428 22190
99 14428 $line =~ /^(.*)\{(.*)\} (.*)$/;
100 14428 my $count_id = $1;
101 14428 my $labels = $2;
102 14428 my $count = $3;
103 14428 $data->{ $count_id . '{' . $labels . '}' } = $count;
104 }
105  
106 189 return $data;
107 }
108  
109 189 ▶sub test_metrics {
Subroutine test_metrics: called CC=1 SCAR=1.2
Pod undocumented
110 189 my ( $expected ) = @_;
111  
112 # Sleep for 5 to allow server to catch up on metrics
113 189 sleep 5;
114  
115 189 ▶ subtest $expected => sub {
Subroutine __ANON__: called CC=19 SCAR=42.6
116  
117 189 my $metrics = get_metrics( 'tmp/authentication_milter_test_metrics.sock' );
118 189 my $j = JSON::XS->new();
119  
120 189 ▶ if ( -e $expected ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-e $expected) { } 189 0 189
121  
122 189 open my $InF, '<', $expected;
123 189 my @content = <$InF>;
124 189 close $InF;
125 189 my $data = $j->decode( join( q{}, @content ) );
126  
127 189 for my $key ( sort keys %$data ) {
128 14614 ▶ next unless $key =~ /^authmilter_version/;
Branch: 1 branch
BranchTrueFalseTotal
unless $key =~ /^authmilter_version/u 12857 1757 14614
129 1757 delete $data->{$key};
130 }
131  
132 189 plan tests => scalar keys %$data;
133  
134 189 foreach my $key ( sort keys %$data ) {
135 12857 ▶ if ( $key =~ /seconds_total/ ) {
Branch: 4 branches
BranchTrueFalseTotal
if ($key =~ /seconds_total/u) { } 6508 6349 12857
elsif ($key =~ /microseconds_sum/u) { } 0 6349 6349
elsif ($key =~ /authmilter_forked_children_total/u) { } 189 6160 6349
elsif ($key =~ /authmilter_processes_/u) { } 378 5782 6160
136 6508 is( $metrics->{ $key } > 0, $data->{ $key } > 0, "Metrics $expected $key" );
137 }
138 elsif ( $key =~ /microseconds_sum/ ) {
139 0 is( $metrics->{ $key } > 0, $data->{ $key } > 0, "Metrics $expected $key" );
140 }
141 elsif ( $key =~ /authmilter_forked_children_total/ ) {
142 189 is( $metrics->{ $key } > 0, $data->{ $key } > 0, "Metrics $expected $key" );
143 }
144 elsif ( $key =~ /authmilter_processes_/) {
145 378 is( $metrics->{ $key } > -1, $data->{ $key } > -1, "Metrics $expected $key" );
146 }
147 else {
148 5782 is( $metrics->{ $key }, $data->{ $key }, "Metrics $expected $key" );
149 }
150 }
151  
152 }
153 else {
154 0 fail( 'Metrics data does not exist' );
155 }
156  
157 189 ▶ if ( $ENV{'WRITE_METRICS'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ENV{"WRITE_METRICS"}) 0 189 189
158 0 foreach my $key ( sort keys %$metrics ) {
159 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
160 0 ▶ $metrics->{ $key } = 123456 if $metrics->{ $key } > 0;
Branch: 1 branch
BranchTrueFalseTotal
if $metrics->{$key} > 0 0 0 0
161 }
162 elsif ( $key =~ /microseconds_sum/ ) {
163 0 ▶ $metrics->{ $key } = 123456 if $metrics->{ $key } > 0;
Branch: 1 branch
BranchTrueFalseTotal
if $metrics->{$key} > 0 0 0 0
164 }
165 elsif ( $key =~ /authmilter_forked_children_total/ ) {
166 0 ▶ $metrics->{ $key } = 123456 if $metrics->{ $key } > 0;
Branch: 1 branch
BranchTrueFalseTotal
if $metrics->{$key} > 0 0 0 0
167 }
168 elsif ( $key =~ /authmilter_processes_/) {
169 0 ▶ $metrics->{ $key } = 123456 if $metrics->{ $key } > -1;
Branch: 1 branch
BranchTrueFalseTotal
if $metrics->{$key} > -1 0 0 0
170 }
171 }
172 0 open my $OutF, '>', $expected;
173 0 $j->pretty();
174 0 $j->canonical();
175 0 print $OutF $j->encode( $metrics );
176 0 close $OutF;
177 }
178  
179 189 };
180 }
181  
182 953 ▶sub smtp_process {
Subroutine smtp_process: called CC=5 SCAR=16.3
Pod undocumented
183 953 my ( $args ) = @_;
184  
185 953 ▶ if ( ! -e $args->{'prefix'} . '/authentication_milter.json' ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-e $args->{"prefix"} . '/authentication_milter.json') 0 953 953
186 0 die "Could not find config " . $args->{'prefix'};
187 }
188 953 ▶ if ( ! -e 'data/source/' . $args->{'source'} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-e 'data/source/' . $args->{"source"}) 0 953 953
189 0 die "Could not find source";
190 }
191  
192 my $catargs = {
193 'sock_type' => 'unix',
194 'sock_path' => 'tmp/authentication_milter_smtp_out.sock',
195 'remove' => [10,11],
196 953 'output' => 'tmp/result/' . $args->{'dest'},
197 };
198 953 unlink 'tmp/authentication_milter_smtp_out.sock';
199 953 my $cat_pid;
200 953 ▶ if ( ! $args->{'no_cat'} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($args->{"no_cat"}) 932 21 953
201 932 $cat_pid = smtpcat( $catargs );
202 898 sleep 2;
203 }
204  
205 my $return = smtpput({
206 'sock_type' => 'unix',
207 'sock_path' => 'tmp/authentication_milter_test.sock',
208 'mailer_name' => 'test.module',
209 'connect_ip' => [ $args->{'ip'} ],
210 'connect_name' => [ $args->{'name'} ],
211 'helo_host' => [ $args->{'name'} ],
212 'mail_from' => [ $args->{'from'} ],
213 'rcpt_to' => [ $args->{'to'} ],
214 'mail_file' => [ 'data/source/' . $args->{'source'} ],
215 919 'eom_expect' => $args->{'eom_expect'},
216 });
217  
218 919 ▶ if ( ! $args->{'no_cat'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $args->{'no_cat'}) { } 898 21 919
219 898 waitpid( $cat_pid,0 );
220 898 files_eq_or_diff( 'data/example/' . $args->{'dest'}, 'tmp/result/' . $args->{'dest'}, 'smtp ' . $args->{'desc'} );
221 }
222 else {
223 21 is( $return, 1, 'SMTP Put Returned ok' );
224 }
225 }
226  
227 97 ▶sub smtp_process_multi {
Subroutine smtp_process_multi: called CC=8 SCAR=20.8
Pod undocumented
228 97 my ( $args ) = @_;
229  
230 97 ▶ if ( ! -e $args->{'prefix'} . '/authentication_milter.json' ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-e $args->{"prefix"} . '/authentication_milter.json') 0 97 97
231 0 die "Could not find config";
232 }
233  
234 # Hardcoded lines to remove in subsequent messages
235 # If you change the source email then change the awk
236 # numbers here too.
237 # This could be better!
238  
239 my $catargs = {
240 'sock_type' => 'unix',
241 'sock_path' => 'tmp/authentication_milter_smtp_out.sock',
242 'remove' => $args->{'filter'},
243 97 'output' => 'tmp/result/' . $args->{'dest'},
244 };
245 97 unlink 'tmp/authentication_milter_smtp_out.sock';
246 97 my $cat_pid = smtpcat( $catargs );
247 95 sleep 2;
248  
249 95 my $putargs = {
250 'sock_type' => 'unix',
251 'sock_path' => 'tmp/authentication_milter_test.sock',
252 'mailer_name' => 'test.module',
253 'connect_ip' => [],
254 'connect_name' => [],
255 'helo_host' => [],
256 'mail_from' => [],
257 'rcpt_to' => [],
258 'mail_file' => [],
259 };
260  
261 95 foreach my $item ( @{$args->{'ip'}} ) {
262 426 push @{$putargs->{'connect_ip'}}, $item;
263 }
264 95 foreach my $item ( @{$args->{'name'}} ) {
265 426 push @{$putargs->{'connect_name'}}, $item;
266 }
267 95 foreach my $item ( @{$args->{'name'}} ) {
268 426 push @{$putargs->{'helo_host'}}, $item;
269 }
270 95 foreach my $item ( @{$args->{'from'}} ) {
271 426 push @{$putargs->{'mail_from'}}, $item;
272 }
273 95 foreach my $item ( @{$args->{'to'}} ) {
274 426 push @{$putargs->{'rcpt_to'}}, $item;
275 }
276 95 foreach my $item ( @{$args->{'source'}} ) {
277 426 push @{$putargs->{'mail_file'}}, 'data/source/' . $item;
278 }
279 #warn 'Testing ' . $args->{'source'} . ' > ' . $args->{'dest'} . "\n";
280  
281 95 smtpput( $putargs );
282  
283 95 waitpid( $cat_pid,0 );
284  
285 95 files_eq_or_diff( 'data/example/' . $args->{'dest'}, 'tmp/result/' . $args->{'dest'}, 'smtp ' . $args->{'desc'} );
286 }
287  
288 885 ▶sub milter_process {
Subroutine milter_process: called CC=3 SCAR=12.3
Pod undocumented
289 885 my ( $args ) = @_;
290  
291 885 ▶ if ( ! -e $args->{'prefix'} . '/authentication_milter.json' ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-e $args->{"prefix"} . '/authentication_milter.json') 0 885 885
292 0 die "Could not find config";
293 }
294 885 ▶ if ( ! -e 'data/source/' . $args->{'source'} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-e 'data/source/' . $args->{"source"}) 0 885 885
295 0 die "Could not find source";
296 }
297  
298 client({
299 'prefix' => $args->{'prefix'},
300 'mailer_name' => 'test.module',
301 'mail_file' => 'data/source/' . $args->{'source'},
302 'connect_ip' => $args->{'ip'},
303 'connect_name' => $args->{'name'},
304 'helo_host' => $args->{'name'},
305 'mail_from' => $args->{'from'},
306 'rcpt_to' => $args->{'to'},
307 885 'output' => 'tmp/result/' . $args->{'dest'},
308 });
309  
310 852 files_eq_or_diff( 'data/example/' . $args->{'dest'}, 'tmp/result/' . $args->{'dest'}, 'milter ' . $args->{'desc'} );
311 }
312  
313 1016 ▶sub smtpput {
Subroutine smtpput: called CC=23 SCAR=34.5
Pod undocumented
314 1016 my ( $args ) = @_;
315  
316 1016 my $mailer_name = $args->{'mailer_name'};
317  
318 1016 my $mail_file_a = $args->{'mail_file'};
319 1016 my $mail_from_a = $args->{'mail_from'};
320 1016 my $rcpt_to_a = $args->{'rcpt_to'};
321 1016 my $x_name_a = $args->{'connect_name'};
322 1016 my $x_addr_a = $args->{'connect_ip'};
323 1016 my $x_helo_a = $args->{'helo_host'};
324  
325 1016 my $sock_type = $args->{'sock_type'};
326 1016 my $sock_path = $args->{'sock_path'};
327 1016 my $sock_host = $args->{'sock_host'};
328 1016 my $sock_port = $args->{'sock_port'};
329  
330 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
331  
332 1016 my $sock;
333 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
334 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
335 'Proto' => 'tcp',
336 'PeerAddr' => $sock_host,
337 'PeerPort' => $sock_port,
338 ) || die "could not open outbound SMTP socket: $!";
339 }
340 elsif ( $sock_type eq 'unix' ) {
341 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
342 'Peer' => $sock_path,
343 ) || die "could not open outbound SMTP socket: $!";
344 }
345  
346 1016 my $line = <$sock>;
347  
348 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
349 0 die "Unexpected SMTP response $line";
350 }
351  
352 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
353  
354 1016 my $first_time = 1;
355  
356 1016 ▶ while ( @$mail_from_a ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$mail_from_a) 1302 969 2271
357  
358 1302 ▶ if ( ! $first_time ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($first_time) 286 1016 1302
359 286 ▶ if ( ! send_smtp_packet( $sock, 'RSET', '250' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (send_smtp_packet($sock, "RSET", 250)) 47 239 286
360 47 $sock->close();
361 47 return;
362 };
363 }
364 1255 $first_time = 0;
365  
366 1255 my $mail_file = shift @$mail_file_a;
367 1255 my $mail_from = shift @$mail_from_a;
368 1255 my $rcpt_to = shift @$rcpt_to_a;
369 1255 my $x_name = shift @$x_name_a;
370 1255 my $x_addr = shift @$x_addr_a;
371 1255 my $x_helo = shift @$x_helo_a;
372  
373 1255 my $mail_data = q{};
374  
375 1255 ▶ if ( $mail_file eq '-' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mail_file eq '-') { } 0 1255 1255
376 0 ▶ while ( my $l = <> ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $l = readline ARGV)) 0 0 0
377 0 $mail_data .= $l;
378 }
379 }
380 else {
381 1255 ▶ if ( ! -e $mail_file ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-e $mail_file) 0 1255 1255
382 0 die "Mail file $mail_file does not exist";
383 }
384 1255 open my $inf, '<', $mail_file;
385 1255 my @all = <$inf>;
386 1255 $mail_data = join( q{}, @all );
387 1255 close $inf;
388 }
389  
390 1255 $mail_data =~ s/\015?\012/\015\012/g;
391 # Handle transparency
392 1255 $mail_data =~ s/\015\012\./\015\012\.\./g;
393  
394 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
395 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
396 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
397  
398 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
399 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
400 1255 ▶ send_smtp_packet( $sock, 'DATA', '354' ) || die;
Branch: 1 branch
BranchTrueFalseTotal
send_smtp_packet($sock, "DATA", 354) or die 1255 0 1255
401  
402 1255 print $sock $mail_data;
403 1255 print $sock "\r\n";
404  
405 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
406  
407 }
408  
409 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
410 969 $sock->close();
411  
412 969 return 1;
413 }
414  
415 11056 ▶sub send_smtp_packet {
Subroutine send_smtp_packet: called CC=4 SCAR=13.9
Pod undocumented
416 11056 my ( $socket, $send, $expect ) = @_;
417 11056 print $socket "$send\r\n";
418 11056 my $recv = <$socket>;
419 11056 ▶ $recv = '' if !defined $recv;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $recv 0 11056 11056
420 11056 ▶ while ( $recv =~ /^\d\d\d\-/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($recv =~ /^\d\d\d\-/u) 4056 11056 15112
421 4056 $recv = <$socket>;
422 }
423 11056 ▶ if ( $recv =~ /^$expect/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($recv =~ /^$expect/u) { } 11009 47 11056
424 11009 return 1;
425 }
426 else {
427 47 $recv =~ s/\r?\n?$//;
428 47 $send =~ s/\r?\n?$//;
429 47 warn "SMTP Send expected \"$expect\" received \"$recv\" when sending \"$send\"\n";
430 47 return 0;
431 }
432 }
433  
434 1033 ▶sub smtpcat {
Subroutine smtpcat: called CC=21 SCAR=31.3
Pod undocumented
435 1033 my ( $args ) = @_;
436  
437 1033 my $cat_pid = fork();
438 1033 ▶ die "unable to fork: $!" unless defined($cat_pid);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $cat_pid 0 1033 1033
439 1033 ▶ return $cat_pid if $cat_pid;
Branch: 1 branch
BranchTrueFalseTotal
if $cat_pid 995 38 1033
440  
441 38 my $sock_type = $args->{'sock_type'};
442 38 my $sock_path = $args->{'sock_path'};
443 38 my $sock_host = $args->{'sock_host'};
444 38 my $sock_port = $args->{'sock_port'};
445  
446 38 my $remove = $args->{'remove'};
447 38 my $output = $args->{'output'};
448  
449 38 my @out_lines;
450  
451 my $sock;
452 38 ▶ if ( $sock_type eq 'inet' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($sock_type eq 'inet') { } 0 38 38
elsif ($sock_type eq 'unix') { } 38 0 38
453 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
454 'Listen' => 5,
455 'LocalHost' => $sock_host,
456 'LocalPort' => $sock_port,
457 'Protocol' => 'tcp',
458 ) || die "could not open socket: $!";
459 }
460 elsif ( $sock_type eq 'unix' ) {
461 38 ▶ $sock = IO::Socket::UNIX->new(
Condition50%1/2
'IO::Socket::UNIX'->new('Listen', 5, 'Local', $sock_path) || die("could not open socket: $!")
l!l
380
MC/DC0%0/1
'IO::Socket::UNIX'->new('Listen', 5, 'Local', $sock_path) || die("could not open socket: $!")
'IO::Socket::UNIX'->new('Listen', 5, 'Local', $sock_path)
Truth table50%1/2
A
A= 'IO::Socket::UNIX'->new('Listen', 5, 'Local', $sock_path)
Aresult
11
00
462 'Listen' => 5,
463 'Local' => $sock_path,
464 ) || die "could not open socket: $!";
465 }
466  
467 38 my $accept = $sock->accept();
468  
469 38 print $accept "220 smtp.cat ESMTP Test\r\n";
470  
471 0 ▶ local $SIG{'ALRM'} = sub{ die "Timeout\n" };
Subroutine __ANON__: not called CC=1 SCAR=1.2
472 38 alarm( 60 );
473  
474 38 while ( 1 ) {
475 353 my $command = <$accept>;
476 353 ▶ last unless defined $command;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $command 0 353 353
477 353 alarm( 60 );
478  
479 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
480 0 push @out_lines, $command;
481 0 print $accept "250 HELO Ok\r\n";
482 }
483 elsif ( $command =~ /^EHLO/ ) {
484 38 push @out_lines, $command;
485 38 print $accept "250 EHLO Ok\r\n";
486 }
487 elsif ( $command =~ /^MAIL/ ) {
488 45 push @out_lines, $command;
489 45 print $accept "250 MAIL Ok\r\n";
490 }
491 elsif ( $command =~ /^XFORWARD/ ) {
492 135 push @out_lines, $command;
493 135 print $accept "250 XFORWARD Ok\r\n";
494 }
495 elsif ( $command =~ /^RCPT/ ) {
496 45 push @out_lines, $command;
497 45 print $accept "250 RCPT Ok\r\n";
498 }
499 elsif ( $command =~ /^RSET/ ) {
500 7 push @out_lines, $command;
501 7 print $accept "250 RSET Ok\r\n";
502 }
503 elsif ( $command =~ /^DATA/ ) {
504 45 push @out_lines, $command;
505 45 print $accept "354 Send\r\n";
506 DATA:
507 45 ▶ while ( my $line = <$accept> ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = readline $accept)) 3059 0 3059
508 3059 alarm( 60 );
509 3059 push @out_lines, $line;
510 3059 ▶ last DATA if $line eq ".\r\n";
Branch: 1 branch
BranchTrueFalseTotal
if $line eq ".\r\n" 45 3014 3059
511 # Handle transparency
512 3014 ▶ if ( $line =~ /^\./ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^\./u) 28 2986 3014
513 28 $line = substr( $line, 1 );
514 }
515 }
516 45 print $accept "250 DATA Ok\r\n";
517 }
518 elsif ( $command =~ /^QUIT/ ) {
519 38 push @out_lines, $command;
520 38 print $accept "221 Bye\r\n";
521 38 last;
522 }
523 else {
524 0 push @out_lines, $command;
525 0 print $accept "250 Unknown Ok\r\n";
526 }
527 }
528  
529 38 open my $file, '>', $output;
530 38 my $i = 0;
531 38 foreach my $line ( @out_lines ) {
532 3412 $i++;
533 3412 ▶ $line = "############\n" if grep { $i == $_ } @$remove;
Branch: 1 branch
BranchTrueFalseTotal
if grep {$i == $_;} @$remove 82 3330 3412
534 3412 print $file $line;
535 }
536 38 close $file;
537  
538 38 $accept->close();
539 38 $sock->close();
540  
541 38 exit 0;
542 }
543  
544 885 ▶sub client {
Subroutine client: called CC=3 SCAR=11.0
Pod undocumented
545 885 my ( $args ) = @_;
546 885 my $pid = fork();
547 885 ▶ die "unable to fork: $!" unless defined($pid);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $pid 0 885 885
548 885 ▶ if ( ! $pid ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($pid) 33 852 885
549  
550 33 my $output = $args->{'output'};
551 33 delete $args->{'output'};
552  
553 33 $Mail::Milter::Authentication::Config::PREFIX = $args->{'prefix'};
554 33 delete $args->{'prefix'};
555 33 $args->{'testing'} = 1;
556  
557 33 my $client = Mail::Milter::Authentication::Client->new( $args );
558  
559 33 $client->process();
560  
561 33 open my $file, '>', $output;
562 33 print $file $client->result();
563 33 close $file;
564 33 exit 0;
565  
566 }
567 852 waitpid( $pid, 0 );
568 }
569  
570 113 1;
571  
572 __END__
« Authentication/App/ProcessLog/App/Command/arex.pm ↑ Summary Authentication/Handler/RBLDNS.pm »