annotate lib/Test/Nginx/SMTP.pm @ 951:9361c7eddfc1

Tests: parallel tests support.
author Andrey Zelenkov <zelenkov@nginx.com>
date Tue, 21 Jun 2016 16:37:58 +0300
parents 481d705b8610
children 1edb092149e2
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
14
d4b74207a627 Tests: refactor common functions.
Maxim Dounin <mdounin@mdounin.ru>
parents: 12
diff changeset
1 package Test::Nginx::SMTP;
5
4d75bdb05ecf Tests: some generic code and ssi tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
2
4d75bdb05ecf Tests: some generic code and ssi tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
3 # (C) Maxim Dounin
4d75bdb05ecf Tests: some generic code and ssi tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
4
14
d4b74207a627 Tests: refactor common functions.
Maxim Dounin <mdounin@mdounin.ru>
parents: 12
diff changeset
5 # Module for nginx smtp tests.
5
4d75bdb05ecf Tests: some generic code and ssi tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
6
4d75bdb05ecf Tests: some generic code and ssi tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
7 ###############################################################################
4d75bdb05ecf Tests: some generic code and ssi tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
8
4d75bdb05ecf Tests: some generic code and ssi tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
9 use warnings;
4d75bdb05ecf Tests: some generic code and ssi tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
10 use strict;
4d75bdb05ecf Tests: some generic code and ssi tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
11
14
d4b74207a627 Tests: refactor common functions.
Maxim Dounin <mdounin@mdounin.ru>
parents: 12
diff changeset
12 use Test::More qw//;
5
4d75bdb05ecf Tests: some generic code and ssi tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
13 use IO::Socket;
7
9eb509695651 Tests: drop our own CRLF constant, use Socket's one instead.
Maxim Dounin <mdounin@mdounin.ru>
parents: 6
diff changeset
14 use Socket qw/ CRLF /;
5
4d75bdb05ecf Tests: some generic code and ssi tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
15
14
d4b74207a627 Tests: refactor common functions.
Maxim Dounin <mdounin@mdounin.ru>
parents: 12
diff changeset
16 use Test::Nginx;
5
4d75bdb05ecf Tests: some generic code and ssi tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
17
540
481d705b8610 Tests: SSL support in mail backends.
Sergey Kandaurov <pluknet@nginx.com>
parents: 539
diff changeset
18 sub new {
481d705b8610 Tests: SSL support in mail backends.
Sergey Kandaurov <pluknet@nginx.com>
parents: 539
diff changeset
19 my $self = {};
481d705b8610 Tests: SSL support in mail backends.
Sergey Kandaurov <pluknet@nginx.com>
parents: 539
diff changeset
20 bless $self, shift @_;
12
d19146b30334 Tests: move write_file() into _common.pm.
Maxim Dounin <mdounin@mdounin.ru>
parents: 8
diff changeset
21
540
481d705b8610 Tests: SSL support in mail backends.
Sergey Kandaurov <pluknet@nginx.com>
parents: 539
diff changeset
22 $self->{_socket} = IO::Socket::INET->new(
5
4d75bdb05ecf Tests: some generic code and ssi tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
23 Proto => "tcp",
45
d68b85def521 Tests: use 127.0.0.1 instead of localhost.
Maxim Dounin <mdounin@mdounin.ru>
parents: 44
diff changeset
24 PeerAddr => "127.0.0.1:8025",
5
4d75bdb05ecf Tests: some generic code and ssi tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
25 @_
4d75bdb05ecf Tests: some generic code and ssi tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
26 )
4d75bdb05ecf Tests: some generic code and ssi tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
27 or die "Can't connect to nginx: $!\n";
4d75bdb05ecf Tests: some generic code and ssi tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
28
540
481d705b8610 Tests: SSL support in mail backends.
Sergey Kandaurov <pluknet@nginx.com>
parents: 539
diff changeset
29 if ({@_}->{'SSL'}) {
481d705b8610 Tests: SSL support in mail backends.
Sergey Kandaurov <pluknet@nginx.com>
parents: 539
diff changeset
30 require IO::Socket::SSL;
481d705b8610 Tests: SSL support in mail backends.
Sergey Kandaurov <pluknet@nginx.com>
parents: 539
diff changeset
31 IO::Socket::SSL->start_SSL($self->{_socket}, @_)
481d705b8610 Tests: SSL support in mail backends.
Sergey Kandaurov <pluknet@nginx.com>
parents: 539
diff changeset
32 or die $IO::Socket::SSL::SSL_ERROR . "\n";
481d705b8610 Tests: SSL support in mail backends.
Sergey Kandaurov <pluknet@nginx.com>
parents: 539
diff changeset
33 }
481d705b8610 Tests: SSL support in mail backends.
Sergey Kandaurov <pluknet@nginx.com>
parents: 539
diff changeset
34
481d705b8610 Tests: SSL support in mail backends.
Sergey Kandaurov <pluknet@nginx.com>
parents: 539
diff changeset
35 $self->{_socket}->autoflush(1);
5
4d75bdb05ecf Tests: some generic code and ssi tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
36
14
d4b74207a627 Tests: refactor common functions.
Maxim Dounin <mdounin@mdounin.ru>
parents: 12
diff changeset
37 return $self;
5
4d75bdb05ecf Tests: some generic code and ssi tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
38 }
4d75bdb05ecf Tests: some generic code and ssi tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
39
540
481d705b8610 Tests: SSL support in mail backends.
Sergey Kandaurov <pluknet@nginx.com>
parents: 539
diff changeset
40 sub eof {
481d705b8610 Tests: SSL support in mail backends.
Sergey Kandaurov <pluknet@nginx.com>
parents: 539
diff changeset
41 my $self = shift;
481d705b8610 Tests: SSL support in mail backends.
Sergey Kandaurov <pluknet@nginx.com>
parents: 539
diff changeset
42 return $self->{_socket}->eof();
481d705b8610 Tests: SSL support in mail backends.
Sergey Kandaurov <pluknet@nginx.com>
parents: 539
diff changeset
43 }
481d705b8610 Tests: SSL support in mail backends.
Sergey Kandaurov <pluknet@nginx.com>
parents: 539
diff changeset
44
481d705b8610 Tests: SSL support in mail backends.
Sergey Kandaurov <pluknet@nginx.com>
parents: 539
diff changeset
45 sub print {
481d705b8610 Tests: SSL support in mail backends.
Sergey Kandaurov <pluknet@nginx.com>
parents: 539
diff changeset
46 my ($self, $cmd) = @_;
481d705b8610 Tests: SSL support in mail backends.
Sergey Kandaurov <pluknet@nginx.com>
parents: 539
diff changeset
47 log_out($cmd);
481d705b8610 Tests: SSL support in mail backends.
Sergey Kandaurov <pluknet@nginx.com>
parents: 539
diff changeset
48 $self->{_socket}->print($cmd);
481d705b8610 Tests: SSL support in mail backends.
Sergey Kandaurov <pluknet@nginx.com>
parents: 539
diff changeset
49 }
481d705b8610 Tests: SSL support in mail backends.
Sergey Kandaurov <pluknet@nginx.com>
parents: 539
diff changeset
50
14
d4b74207a627 Tests: refactor common functions.
Maxim Dounin <mdounin@mdounin.ru>
parents: 12
diff changeset
51 sub send {
d4b74207a627 Tests: refactor common functions.
Maxim Dounin <mdounin@mdounin.ru>
parents: 12
diff changeset
52 my ($self, $cmd) = @_;
5
4d75bdb05ecf Tests: some generic code and ssi tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
53 log_out($cmd);
540
481d705b8610 Tests: SSL support in mail backends.
Sergey Kandaurov <pluknet@nginx.com>
parents: 539
diff changeset
54 $self->{_socket}->print($cmd . CRLF);
5
4d75bdb05ecf Tests: some generic code and ssi tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
55 }
4d75bdb05ecf Tests: some generic code and ssi tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
56
14
d4b74207a627 Tests: refactor common functions.
Maxim Dounin <mdounin@mdounin.ru>
parents: 12
diff changeset
57 sub read {
d4b74207a627 Tests: refactor common functions.
Maxim Dounin <mdounin@mdounin.ru>
parents: 12
diff changeset
58 my ($self) = @_;
540
481d705b8610 Tests: SSL support in mail backends.
Sergey Kandaurov <pluknet@nginx.com>
parents: 539
diff changeset
59 my $socket = $self->{_socket};
5
4d75bdb05ecf Tests: some generic code and ssi tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
60 eval {
209
5137f27935b4 Tests: log timeouts in IMAP/POP3/SMTP.
Maxim Dounin <mdounin@mdounin.ru>
parents: 162
diff changeset
61 local $SIG{ALRM} = sub { die "timeout\n" };
339
9d0a2fa47ac6 Tests: change http timeout to 5s, imap/pop3/smtp to 3s.
Maxim Dounin <mdounin@mdounin.ru>
parents: 243
diff changeset
62 alarm(3);
540
481d705b8610 Tests: SSL support in mail backends.
Sergey Kandaurov <pluknet@nginx.com>
parents: 539
diff changeset
63 while (<$socket>) {
5
4d75bdb05ecf Tests: some generic code and ssi tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
64 log_in($_);
4d75bdb05ecf Tests: some generic code and ssi tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
65 next if m/^\d\d\d-/;
4d75bdb05ecf Tests: some generic code and ssi tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
66 last;
4d75bdb05ecf Tests: some generic code and ssi tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
67 }
4d75bdb05ecf Tests: some generic code and ssi tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
68 alarm(0);
4d75bdb05ecf Tests: some generic code and ssi tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
69 };
4d75bdb05ecf Tests: some generic code and ssi tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
70 alarm(0);
4d75bdb05ecf Tests: some generic code and ssi tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
71 if ($@) {
209
5137f27935b4 Tests: log timeouts in IMAP/POP3/SMTP.
Maxim Dounin <mdounin@mdounin.ru>
parents: 162
diff changeset
72 log_in("died: $@");
5
4d75bdb05ecf Tests: some generic code and ssi tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
73 return undef;
4d75bdb05ecf Tests: some generic code and ssi tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
74 }
4d75bdb05ecf Tests: some generic code and ssi tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
75 return $_;
4d75bdb05ecf Tests: some generic code and ssi tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
76 }
4d75bdb05ecf Tests: some generic code and ssi tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
77
14
d4b74207a627 Tests: refactor common functions.
Maxim Dounin <mdounin@mdounin.ru>
parents: 12
diff changeset
78 sub check {
d4b74207a627 Tests: refactor common functions.
Maxim Dounin <mdounin@mdounin.ru>
parents: 12
diff changeset
79 my ($self, $regex, $name) = @_;
57
b5b4271554b0 Test: use TODO for smtp pipelining tests.
Maxim Dounin <mdounin@mdounin.ru>
parents: 45
diff changeset
80 Test::More->builder->like($self->read(), $regex, $name);
5
4d75bdb05ecf Tests: some generic code and ssi tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
81 }
4d75bdb05ecf Tests: some generic code and ssi tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
82
14
d4b74207a627 Tests: refactor common functions.
Maxim Dounin <mdounin@mdounin.ru>
parents: 12
diff changeset
83 sub ok {
243
de7338227832 Tests: removed trailing spaces.
Homutov Vladimir <vl@nginx.com>
parents: 209
diff changeset
84 my $self = shift;
57
b5b4271554b0 Test: use TODO for smtp pipelining tests.
Maxim Dounin <mdounin@mdounin.ru>
parents: 45
diff changeset
85 Test::More->builder->like($self->read(), qr/^2\d\d /, @_);
5
4d75bdb05ecf Tests: some generic code and ssi tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
86 }
4d75bdb05ecf Tests: some generic code and ssi tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
87
60
11e33ba0656a Tests: check for 235 code on successful smtp auth.
Maxim Dounin <mdounin@mdounin.ru>
parents: 57
diff changeset
88 sub authok {
243
de7338227832 Tests: removed trailing spaces.
Homutov Vladimir <vl@nginx.com>
parents: 209
diff changeset
89 my $self = shift;
60
11e33ba0656a Tests: check for 235 code on successful smtp auth.
Maxim Dounin <mdounin@mdounin.ru>
parents: 57
diff changeset
90 Test::More->builder->like($self->read(), qr/^235 /, @_);
11e33ba0656a Tests: check for 235 code on successful smtp auth.
Maxim Dounin <mdounin@mdounin.ru>
parents: 57
diff changeset
91 }
11e33ba0656a Tests: check for 235 code on successful smtp auth.
Maxim Dounin <mdounin@mdounin.ru>
parents: 57
diff changeset
92
5
4d75bdb05ecf Tests: some generic code and ssi tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
93 ###############################################################################
4d75bdb05ecf Tests: some generic code and ssi tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
94
26
0880e0fafde4 Tests: implement smtp test backend and use it.
Maxim Dounin <mdounin@mdounin.ru>
parents: 14
diff changeset
95 sub smtp_test_daemon {
951
9361c7eddfc1 Tests: parallel tests support.
Andrey Zelenkov <zelenkov@nginx.com>
parents: 540
diff changeset
96 my ($port) = @_;
9361c7eddfc1 Tests: parallel tests support.
Andrey Zelenkov <zelenkov@nginx.com>
parents: 540
diff changeset
97
26
0880e0fafde4 Tests: implement smtp test backend and use it.
Maxim Dounin <mdounin@mdounin.ru>
parents: 14
diff changeset
98 my $server = IO::Socket::INET->new(
0880e0fafde4 Tests: implement smtp test backend and use it.
Maxim Dounin <mdounin@mdounin.ru>
parents: 14
diff changeset
99 Proto => 'tcp',
951
9361c7eddfc1 Tests: parallel tests support.
Andrey Zelenkov <zelenkov@nginx.com>
parents: 540
diff changeset
100 LocalAddr => '127.0.0.1:' . ($port || 8026),
26
0880e0fafde4 Tests: implement smtp test backend and use it.
Maxim Dounin <mdounin@mdounin.ru>
parents: 14
diff changeset
101 Listen => 5,
0880e0fafde4 Tests: implement smtp test backend and use it.
Maxim Dounin <mdounin@mdounin.ru>
parents: 14
diff changeset
102 Reuse => 1
0880e0fafde4 Tests: implement smtp test backend and use it.
Maxim Dounin <mdounin@mdounin.ru>
parents: 14
diff changeset
103 )
0880e0fafde4 Tests: implement smtp test backend and use it.
Maxim Dounin <mdounin@mdounin.ru>
parents: 14
diff changeset
104 or die "Can't create listening socket: $!\n";
0880e0fafde4 Tests: implement smtp test backend and use it.
Maxim Dounin <mdounin@mdounin.ru>
parents: 14
diff changeset
105
0880e0fafde4 Tests: implement smtp test backend and use it.
Maxim Dounin <mdounin@mdounin.ru>
parents: 14
diff changeset
106 while (my $client = $server->accept()) {
0880e0fafde4 Tests: implement smtp test backend and use it.
Maxim Dounin <mdounin@mdounin.ru>
parents: 14
diff changeset
107 $client->autoflush(1);
0880e0fafde4 Tests: implement smtp test backend and use it.
Maxim Dounin <mdounin@mdounin.ru>
parents: 14
diff changeset
108 print $client "220 fake esmtp server ready" . CRLF;
0880e0fafde4 Tests: implement smtp test backend and use it.
Maxim Dounin <mdounin@mdounin.ru>
parents: 14
diff changeset
109
0880e0fafde4 Tests: implement smtp test backend and use it.
Maxim Dounin <mdounin@mdounin.ru>
parents: 14
diff changeset
110 while (<$client>) {
68
5f56040c39df Tests: smtp xclient tests.
Maxim Dounin <mdounin@mdounin.ru>
parents: 60
diff changeset
111 Test::Nginx::log_core('||', $_);
5f56040c39df Tests: smtp xclient tests.
Maxim Dounin <mdounin@mdounin.ru>
parents: 60
diff changeset
112
26
0880e0fafde4 Tests: implement smtp test backend and use it.
Maxim Dounin <mdounin@mdounin.ru>
parents: 14
diff changeset
113 if (/^quit/i) {
0880e0fafde4 Tests: implement smtp test backend and use it.
Maxim Dounin <mdounin@mdounin.ru>
parents: 14
diff changeset
114 print $client '221 quit ok' . CRLF;
0880e0fafde4 Tests: implement smtp test backend and use it.
Maxim Dounin <mdounin@mdounin.ru>
parents: 14
diff changeset
115 } elsif (/^(ehlo|helo)/i) {
0880e0fafde4 Tests: implement smtp test backend and use it.
Maxim Dounin <mdounin@mdounin.ru>
parents: 14
diff changeset
116 print $client '250 hello ok' . CRLF;
0880e0fafde4 Tests: implement smtp test backend and use it.
Maxim Dounin <mdounin@mdounin.ru>
parents: 14
diff changeset
117 } elsif (/^rset/i) {
0880e0fafde4 Tests: implement smtp test backend and use it.
Maxim Dounin <mdounin@mdounin.ru>
parents: 14
diff changeset
118 print $client '250 rset ok' . CRLF;
0880e0fafde4 Tests: implement smtp test backend and use it.
Maxim Dounin <mdounin@mdounin.ru>
parents: 14
diff changeset
119 } elsif (/^mail from:[^@]+$/i) {
0880e0fafde4 Tests: implement smtp test backend and use it.
Maxim Dounin <mdounin@mdounin.ru>
parents: 14
diff changeset
120 print $client '500 mail from error' . CRLF;
0880e0fafde4 Tests: implement smtp test backend and use it.
Maxim Dounin <mdounin@mdounin.ru>
parents: 14
diff changeset
121 } elsif (/^mail from:/i) {
0880e0fafde4 Tests: implement smtp test backend and use it.
Maxim Dounin <mdounin@mdounin.ru>
parents: 14
diff changeset
122 print $client '250 mail from ok' . CRLF;
0880e0fafde4 Tests: implement smtp test backend and use it.
Maxim Dounin <mdounin@mdounin.ru>
parents: 14
diff changeset
123 } elsif (/^rcpt to:[^@]+$/i) {
0880e0fafde4 Tests: implement smtp test backend and use it.
Maxim Dounin <mdounin@mdounin.ru>
parents: 14
diff changeset
124 print $client '500 rcpt to error' . CRLF;
0880e0fafde4 Tests: implement smtp test backend and use it.
Maxim Dounin <mdounin@mdounin.ru>
parents: 14
diff changeset
125 } elsif (/^rcpt to:/i) {
0880e0fafde4 Tests: implement smtp test backend and use it.
Maxim Dounin <mdounin@mdounin.ru>
parents: 14
diff changeset
126 print $client '250 rcpt to ok' . CRLF;
68
5f56040c39df Tests: smtp xclient tests.
Maxim Dounin <mdounin@mdounin.ru>
parents: 60
diff changeset
127 } elsif (/^xclient/i) {
5f56040c39df Tests: smtp xclient tests.
Maxim Dounin <mdounin@mdounin.ru>
parents: 60
diff changeset
128 print $client '220 xclient ok' . CRLF;
26
0880e0fafde4 Tests: implement smtp test backend and use it.
Maxim Dounin <mdounin@mdounin.ru>
parents: 14
diff changeset
129 } else {
0880e0fafde4 Tests: implement smtp test backend and use it.
Maxim Dounin <mdounin@mdounin.ru>
parents: 14
diff changeset
130 print $client "500 unknown command" . CRLF;
0880e0fafde4 Tests: implement smtp test backend and use it.
Maxim Dounin <mdounin@mdounin.ru>
parents: 14
diff changeset
131 }
539
3fcad5e66735 Tests: whitespace fixes in mail modules.
Sergey Kandaurov <pluknet@nginx.com>
parents: 526
diff changeset
132 }
26
0880e0fafde4 Tests: implement smtp test backend and use it.
Maxim Dounin <mdounin@mdounin.ru>
parents: 14
diff changeset
133
0880e0fafde4 Tests: implement smtp test backend and use it.
Maxim Dounin <mdounin@mdounin.ru>
parents: 14
diff changeset
134 close $client;
0880e0fafde4 Tests: implement smtp test backend and use it.
Maxim Dounin <mdounin@mdounin.ru>
parents: 14
diff changeset
135 }
0880e0fafde4 Tests: implement smtp test backend and use it.
Maxim Dounin <mdounin@mdounin.ru>
parents: 14
diff changeset
136 }
0880e0fafde4 Tests: implement smtp test backend and use it.
Maxim Dounin <mdounin@mdounin.ru>
parents: 14
diff changeset
137
0880e0fafde4 Tests: implement smtp test backend and use it.
Maxim Dounin <mdounin@mdounin.ru>
parents: 14
diff changeset
138 ###############################################################################
0880e0fafde4 Tests: implement smtp test backend and use it.
Maxim Dounin <mdounin@mdounin.ru>
parents: 14
diff changeset
139
5
4d75bdb05ecf Tests: some generic code and ssi tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
140 1;
4d75bdb05ecf Tests: some generic code and ssi tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
141
4d75bdb05ecf Tests: some generic code and ssi tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
142 ###############################################################################