blib/lib/Test/Mock/Net/Server/Mail.pm

filter: Statementstmt 57.5% 99 / 172 Branchbran 27.5% 16 / 58 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 66.6% 22 / 33 Podpod 83.3% 10 / 12 totaltotal 52.5% 147 / 280 CC 32 SCAR 21.7
CC
32
Cov
49%
_process_connection
30.0
process_mail_rcpt
30.0
_log_callback
20.0
CRAP 8.8
↑ Summary
1 package Test::Mock::Net::Server::Mail;
2  
3 2 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 # ABSTRACT: mock SMTP server for use in tests
6 2 our $VERSION = '1.02'; # VERSION
7  
8  
9 2 ▶use Net::Server::Mail::ESMTP;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use IO::Socket::INET;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use IO::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use Test::More;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 2 ▶use Test::Exception;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 2 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 2 ▶use File::Temp;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17  
18 2 has 'bind_address' => ( is => 'ro', isa => 'Str', default => '127.0.0.1' );
19 2 has 'port' => ( is => 'rw', isa => 'Maybe[Int]' );
20 2 has 'pid' => ( is => 'rw', isa => 'Maybe[Int]' );
21  
22 has 'start_port' => ( is => 'rw', isa => 'Int', lazy => 1,
23 1 ▶ default => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
24 1 return 50000 + int(rand(10000));
25 },
26 2 );
27  
28 has 'socket' => ( is => 'ro', isa => 'IO::Socket::INET', lazy => 1,
29 1 ▶ default => sub {
Subroutine __ANON__: called CC=4 SCAR=14.7
30 1 my $self = shift;
31 1 my $cur_port = $self->start_port;
32 1 my $socket;
33 1 ▶ for( my $i = 0 ; $i < 100 ; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < 100) 1 0 1
34 1 $socket = IO::Socket::INET->new(
35 Listen => 1,
36 LocalPort => $cur_port,
37 LocalAddr => $self->bind_address,
38 );
39 1 ▶ if( defined $socket ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $socket) 1 0 1
40 1 last;
41 }
42 0 $cur_port += 10;
43 }
44 1 ▶ if( ! defined $socket ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $socket) 0 1 1
45 0 die("giving up to find free port to bind: $@");
46 }
47 1 $self->port( $cur_port );
48 1 return $socket;
49 },
50 2 );
51  
52 2 has 'support_8bitmime' => ( is => 'ro', isa => 'Bool', default => 1 );
53 2 has 'support_pipelining' => ( is => 'ro', isa => 'Bool', default => 1 );
54 2 has 'support_starttls' => ( is => 'ro', isa => 'Bool', default => 1 );
55  
56 has 'mock_verbs' => (
57 is => 'ro',
58 isa => 'ArrayRef[Str]',
59 1 ▶ default => sub { [ qw(
Subroutine __ANON__: called CC=1 SCAR=0.0
60 EHLO
61 HELO
62 MAIL
63 RCPT
64 DATA
65 QUIT
66 ) ] },
67 2 );
68  
69 2 has 'logging' => (
70 is => 'ro',
71 isa => 'Bool',
72 default => 1,
73 );
74  
75 1 ▶sub BUILD {
Subroutine BUILD: called CC=2 SCAR=7.0
Pod undocumented
76 1 my $self = shift;
77 1 ▶ if( $self->logging ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->logging) 1 0 1
78 1 $self->_init_log;
79 }
80 1 return;
81 }
82  
83 2 has '_log_fh' => (
84 is => 'rw',
85 isa => 'IO::Handle',
86 );
87  
88 1 ▶sub _init_log {
Subroutine _init_log: called CC=1 SCAR=0.0
89 1 my $self = shift;
90 1 $self->_log_fh(File::Temp->new);
91 1 return;
92 }
93  
94 0 ▶sub _reopen_log {
Subroutine _reopen_log: not called CC=2 SCAR=17.9
95 0 my $self = shift;
96 0 ▶ my $fh = IO::File->new($self->_log_fh->filename, O_WRONLY|O_APPEND)
Branch: 1 branch
BranchTrueFalseTotal
my $fh = "IO::File"->new($self->_log_fh->filename, 1025) or die 'cannot reopen temporary logfile: ' . $! 0 0 0
97 or die('cannot reopen temporary logfile: '.$!);
98 0 $self->_log_fh($fh);
99 0 return;
100 }
101  
102 0 ▶sub _write_log {
Subroutine _write_log: not called CC=1 SCAR=6.9
103 0 my $self = shift;
104 0 $self->_log_fh->print(join('',@_));
105 0 $self->_log_fh->flush;
106 0 return;
107 }
108  
109  
110 9 ▶sub next_log {
Subroutine next_log: called CC=2 SCAR=7.2
Pod documented
111 9 my $self = shift;
112 9 my $line = $self->_log_fh->getline;
113 9 ▶ if($line) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line) 9 0 9
114 9 chomp $line;
115 9 return decode_json $line;
116 }
117 0 return;
118 }
119  
120  
121 9 ▶sub next_log_ok {
Subroutine next_log_ok: called CC=5 SCAR=17.5
Pod documented
122 9 my ($self, $verb, $params, $text) = @_;
123 9 my $log = $self->next_log;
124 9 ▶ if(!defined $log) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $log) 0 9 9
125 0 fail($text);
126 0 diag('no more logs to read!');
127 0 return;
128 }
129  
130 9 ▶ if($log->{'verb'} ne $verb) {
Branch: 1 branch
BranchTrueFalseTotal
if ($log->{"verb"} ne $verb) 0 9 9
131 0 fail($text);
132 0 diag('expected verb '.$verb.' but got '.$log->{'verb'});
133 0 return;
134 }
135  
136 9 ▶ if(defined $params) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $params) 8 1 9
137 8 ▶ if(ref($params) eq 'Regexp') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $params eq "Regexp") 1 7 8
138 1 like($log->{'params'}, $params, $text);
139 1 return;
140 }
141 7 cmp_ok($log->{'params'}, 'eq', $params, $text);
142 7 return;
143 }
144  
145 1 pass($text);
146 1 return;
147 }
148  
149 0 ▶sub _process_callback {
Subroutine _process_callback: not called CC=3 SCAR=24.8
150 0 my ($self, $verb, $session, $params) = @_;
151  
152 0 ▶ if($self->logging) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->logging) 0 0 0
153 0 $self->_log_callback($verb, $params);
154 }
155  
156 0 my $method = "process_".lc($verb);
157 0 ▶ if($self->can($method)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->can($method)) 0 0 0
158 0 return $self->$method($session, $params);
159 }
160 0 return;
161 }
162  
163 0 ▶sub _log_callback {
Subroutine _log_callback: not called CC=4 SCAR=30.0
164 0 my ($self, $verb, $params) = @_;
165 0 my $params_out;
166 0 ▶ if(ref($params) eq '') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $params eq '') { } 0 0 0
elsif (ref $params eq 'SCALAR') { } 0 0 0
167 0 $params_out = $params;
168 } elsif(ref($params) eq 'SCALAR') {
169 0 $params_out = $$params;
170 } else {
171 0 $params_out = $verb.' passed unprintable '.ref($params);
172 }
173 0 ▶ $self->_write_log(
Branch: 1 branch
BranchTrueFalseTotal
defined $params_out ? : 0 0 0
174 encode_json( {
175 verb => $verb,
176 defined $params_out ? (params => $params_out) : (),
177 } )."\n"
178 );
179 0 return;
180 }
181  
182 0 ▶sub _process_connection {
Subroutine _process_connection: not called CC=5 SCAR=34.0
183 0 my ( $self, $conn ) = @_;
184 0 my $smtp = Net::Server::Mail::ESMTP->new(
185 socket => $conn,
186 );
187  
188 0 ▶ $self->support_8bitmime
Branch: 1 branch
BranchTrueFalseTotal
$self->support_8bitmime and $smtp->register("Net::Server::Mail::ESMTP::8BITMIME") 0 0 0
189 && $smtp->register('Net::Server::Mail::ESMTP::8BITMIME');
190 0 ▶ $self->support_pipelining
Branch: 1 branch
BranchTrueFalseTotal
$self->support_pipelining and $smtp->register("Net::Server::Mail::ESMTP::PIPELINING") 0 0 0
191 && $smtp->register('Net::Server::Mail::ESMTP::PIPELINING');
192 0 ▶ $self->support_starttls
Branch: 1 branch
BranchTrueFalseTotal
$self->support_starttls and $smtp->register("Net::Server::Mail::ESMTP::STARTTLS") 0 0 0
193 && $smtp->register('Net::Server::Mail::ESMTP::STARTTLS');
194  
195 0 foreach my $verb (@{$self->mock_verbs}) {
196 0 ▶ $smtp->set_callback($verb => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
197 0 my ( $session, $params ) = @_;
198 0 return $self->_process_callback( $verb, $session, $params );
199 0 } );
200 }
201  
202 0 $self->before_process( $smtp );
203 0 $smtp->process();
204 0 $conn->close();
205
206 0 return;
207 };
208  
209  
210  
211 0 ▶sub before_process {
Subroutine before_process: not called CC=1 SCAR=6.9
Pod documented
212 0 my ( $self, $smtp ) = @_;
213 0 return;
214 }
215  
216  
217 0 ▶sub process_ehlo {
Subroutine process_ehlo: not called CC=2 SCAR=17.9
Pod documented
218 0 my ( $self, $session, $name ) = @_;
219 0 ▶ if( $name =~ /^bad/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($name =~ /^bad/) 0 0 0
220 0 return(1, 501, "$name is a bad helo name");
221 }
222 0 return 1;
223 }
224  
225 0 ▶sub process_mail_rcpt {
Subroutine process_mail_rcpt: not called CC=5 SCAR=34.0
Pod undocumented
226 0 my ( $self, $session, $rcpt ) = @_;
227 0 my ( $user, $domain ) = split('@', $rcpt, 2);
228 0 ▶ if( ! defined $user ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $user) 0 0 0
229 0 return(0, 513, 'Syntax error.');
230 }
231 0 ▶ if( $user =~ /^bad/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($user =~ /^bad/) 0 0 0
232 0 return(0, 552, "$rcpt Recipient address rejected: bad user");
233 }
234 0 ▶ if( defined $domain && $domain =~ /^bad/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $domain and $domain =~ /^bad/) 0 0 0
Condition0%0/3
defined $domain and $domain =~ /^bad/
!ll&&!rl&&r
000
MC/DC0%0/2
defined $domain and $domain =~ /^bad/
defined $domain $domain =~ /^bad/
Truth table0%0/3
A and B
A= defined $domainB= $domain =~ /^bad/
ABresult
0X0
100
111
235 0 return(0, 552, "$rcpt Recipient address rejected: bad domain");
236 }
237 0 return(1);
238 }
239 2 *process_mail = \&process_mail_rcpt;
240 2 *process_rcpt = \&process_mail_rcpt;
241  
242 0 ▶sub process_data {
Subroutine process_data: not called CC=2 SCAR=17.9
Pod documented
243 0 my ( $self, $session, $data ) = @_;
244 0 ▶ if( $$data =~ /bad mail content/msi ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$data =~ /bad mail content/ism) 0 0 0
245 0 return(0, 554, 'Message rejected: bad mail content');
246 }
247 0 return 1;
248 }
249  
250  
251 0 ▶sub main_loop {
Subroutine main_loop: not called CC=2 SCAR=17.9
Pod documented
252 0 my $self = shift;
253  
254 0 $self->_reopen_log;
255  
256 0 ▶ while( my $conn = $self->socket->accept ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $conn = $self->socket->accept) 0 0 0
257 0 $self->_process_connection( $conn );
258 }
259  
260 0 exit 1;
261 0 return;
262 }
263  
264  
265 1 ▶sub start {
Subroutine start: called CC=3 SCAR=11.8
Pod documented
266 1 my $self = shift;
267 1 ▶ if( defined $self->pid ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->pid) 0 1 1
268 0 die('already running with pid '.$self->pid);
269 }
270  
271 # make sure socket is initialized
272 # we need to know the port number in parent
273 1 $self->socket;
274  
275 1 my $pid = fork;
276 1 ▶ if( $pid == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pid == 0) { } 0 1 1
277 0 $self->main_loop;
278 } else {
279 1 $self->pid( $pid );
280 }
281  
282 1 return;
283 }
284  
285  
286 1 ▶sub start_ok {
Subroutine start_ok: called CC=2 SCAR=7.0
Pod documented
287 1 my ( $self, $text ) = @_;
288 1 ▶ lives_ok {
Subroutine __ANON__: called CC=1 SCAR=0.0
289 1 $self->start;
290 1 ▶ } defined $text ? $text : 'start smtp mock server';
Branch: 1 branch
BranchTrueFalseTotal
defined $text ? : 0 1 1
291 1 return;
292 }
293  
294  
295 2 ▶sub stop {
Subroutine stop: called CC=2 SCAR=7.0
Pod documented
296 2 my $self = shift;
297 2 my $pid = $self->pid;
298 2 ▶ if( defined $pid ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $pid) 2 0 2
299 2 kill( 'QUIT', $pid );
300 2 waitpid( $pid, 0 );
301 }
302  
303 2 return;
304 }
305  
306 1 ▶sub DESTROY {
Subroutine DESTROY: called CC=1 SCAR=0.0
307 1 my $self = shift;
308 # try to stop server when going out of scope
309 1 $self->stop;
310 1 return;
311 }
312  
313  
314 1 ▶sub stop_ok {
Subroutine stop_ok: called CC=2 SCAR=7.0
Pod documented
315 1 my ( $self, $text ) = @_;
316 1 ▶ lives_ok {
Subroutine __ANON__: called CC=1 SCAR=0.0
317 1 $self->stop;
318 1 ▶ } defined $text ? $text : 'stop smtp mock server';
Branch: 1 branch
BranchTrueFalseTotal
defined $text ? : 1 0 1
319 1 return;
320 }
321  
322 2 1;
323  
324 __END__
↑ Summary