-
Notifications
You must be signed in to change notification settings - Fork 92
Expand file tree
/
Copy pathSMTP.pm
More file actions
223 lines (179 loc) · 4.55 KB
/
SMTP.pm
File metadata and controls
223 lines (179 loc) · 4.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
package Test::Nginx::SMTP;
# (C) Maxim Dounin
# Module for nginx smtp tests.
###############################################################################
use warnings;
use strict;
use Test::More qw//;
use IO::Select;
use IO::Socket;
use Socket qw/ CRLF /;
use Test::Nginx;
sub new {
my $self = {};
bless $self, shift @_;
my $port = {@_}->{'SSL'} ? 8465 : 8025;
eval {
local $SIG{ALRM} = sub { die "timeout\n" };
local $SIG{PIPE} = sub { die "sigpipe\n" };
alarm(8);
$self->{_socket} = IO::Socket::INET->new(
Proto => "tcp",
PeerAddr => "127.0.0.1:" . port($port),
@_
)
or die "Can't connect to nginx: $!\n";
if ({@_}->{'SSL'}) {
require IO::Socket::SSL;
IO::Socket::SSL->start_SSL(
$self->{_socket},
SSL_verify_mode =>
IO::Socket::SSL::SSL_VERIFY_NONE(),
@_
)
or die $IO::Socket::SSL::SSL_ERROR . "\n";
my $s = $self->{_socket};
log_in("ssl cipher: " . $s->get_cipher());
log_in("ssl cert: " . $s->peer_certificate('issuer'));
}
alarm(0);
};
alarm(0);
if ($@) {
log_in("died: $@");
}
$self->{_socket}->autoflush(1);
$self->{_read_buffer} = '';
return $self;
}
sub DESTROY {
my $self = shift;
$self->{_socket}->close();
}
sub eof {
my $self = shift;
return $self->{_socket}->eof();
}
sub print {
my ($self, $cmd) = @_;
log_out($cmd);
$self->{_socket}->print($cmd);
}
sub send {
my ($self, $cmd) = @_;
log_out($cmd);
$self->{_socket}->print($cmd . CRLF);
}
sub getline {
my ($self) = @_;
my $socket = $self->{_socket};
if ($self->{_read_buffer} =~ /^(.*?\x0a)(.*)/ms) {
$self->{_read_buffer} = $2;
return $1;
}
while (IO::Select->new($socket)->can_read(8)) {
$socket->blocking(0);
my $n = $socket->sysread(my $buf, 1024);
my $again = !defined $n && $!{EWOULDBLOCK};
$socket->blocking(1);
next if $again;
last unless $n;
$self->{_read_buffer} .= $buf;
if ($self->{_read_buffer} =~ /^(.*?\x0a)(.*)/ms) {
$self->{_read_buffer} = $2;
return $1;
}
};
}
sub read {
my ($self) = @_;
my $socket = $self->{_socket};
while (defined($_ = $self->getline())) {
log_in($_);
next if m/^\d\d\d-/;
last;
}
return $_;
}
sub check {
my ($self, $regex, $name) = @_;
Test::More->builder->like($self->read(), $regex, $name);
}
sub ok {
my $self = shift;
Test::More->builder->like($self->read(), qr/^2\d\d /, @_);
}
sub authok {
my $self = shift;
Test::More->builder->like($self->read(), qr/^235 /, @_);
}
sub can_read {
my ($self, $timo) = @_;
IO::Select->new($self->{_socket})->can_read($timo || 3);
}
sub socket {
my ($self) = @_;
$self->{_socket};
}
###############################################################################
sub fail {
my ($client, $reason) = @_;
print $client '500 failed: ' . $reason . CRLF;
$client->close();
}
sub smtp_test_daemon {
my ($port, $with_auth) = @_;
my $proxy_protocol;
my $server = IO::Socket::INET->new(
Proto => 'tcp',
LocalAddr => '127.0.0.1:' . ($port || port(8026)),
Listen => 5,
Reuse => 1
)
or die "Can't create listening socket: $!\n";
while (my $client = $server->accept()) {
$client->autoflush(1);
print $client "220 fake esmtp server ready" . CRLF;
$proxy_protocol = '';
my $authenticated = 0;
while (<$client>) {
Test::Nginx::log_core('||', $_);
if (/^quit/i) {
print $client '221 quit ok' . CRLF;
} elsif (/^(ehlo|helo)/i) {
print $client '250 hello ok' . CRLF;
} elsif (/^rset/i) {
print $client '250 rset ok' . CRLF;
} elsif (/^auth/i and not $with_auth) {
fail($client, "No authentication expected");
} elsif (/^auth plain/i) {
print $client '235 auth ok' . CRLF;
$authenticated = 1;
} elsif (/^mail/i and $with_auth and not $authenticated) {
fail($client, "Authentication expected");
} elsif (/^rcpt/i and $with_auth and not $authenticated) {
fail($client, "Authentication expected");
} elsif (/^mail from:[^@]+$/i) {
print $client '500 mail from error' . CRLF;
} elsif (/^mail from:/i) {
print $client '250 mail from ok' . CRLF;
} elsif (/^rcpt to:[^@]+$/i) {
print $client '500 rcpt to error' . CRLF;
} elsif (/^rcpt to:/i) {
print $client '250 rcpt to ok' . CRLF;
} elsif (/^xclient/i) {
print $client '220 xclient ok' . CRLF;
} elsif (/^proxy/i) {
$proxy_protocol = $_;
} elsif (/^xproxy/i) {
print $client '211 ' . $proxy_protocol . CRLF;
} else {
print $client "500 unknown command" . CRLF;
}
}
close $client;
}
}
###############################################################################
1;
###############################################################################