annotate lib/Test/Nginx/SMTP.pm @ 1978:79753dd514e6 default tip

Tests: added test for mail proxy timeout.
author Rob Mueller <robm@fastmailteam.com>
date Wed, 15 May 2024 23:51:31 +0300
parents 6d3a8f4eb9b2
children
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//;
1244
575d39cc0e35 Tests: worker_shutdown_timeout within the mail module.
Sergey Kandaurov <pluknet@nginx.com>
parents: 969
diff changeset
13 use IO::Select;
5
4d75bdb05ecf Tests: some generic code and ssi tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
14 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
15 use Socket qw/ CRLF /;
5
4d75bdb05ecf Tests: some generic code and ssi tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
16
14
d4b74207a627 Tests: refactor common functions.
Maxim Dounin <mdounin@mdounin.ru>
parents: 12
diff changeset
17 use Test::Nginx;
5
4d75bdb05ecf Tests: some generic code and ssi tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
18
540
481d705b8610 Tests: SSL support in mail backends.
Sergey Kandaurov <pluknet@nginx.com>
parents: 539
diff changeset
19 sub new {
481d705b8610 Tests: SSL support in mail backends.
Sergey Kandaurov <pluknet@nginx.com>
parents: 539
diff changeset
20 my $self = {};
481d705b8610 Tests: SSL support in mail backends.
Sergey Kandaurov <pluknet@nginx.com>
parents: 539
diff changeset
21 bless $self, shift @_;
12
d19146b30334 Tests: move write_file() into _common.pm.
Maxim Dounin <mdounin@mdounin.ru>
parents: 8
diff changeset
22
1861
7b7b64569f55 Tests: reworked mail SSL tests to use IO::Socket::SSL.
Maxim Dounin <mdounin@mdounin.ru>
parents: 1825
diff changeset
23 my $port = {@_}->{'SSL'} ? 8465 : 8025;
7b7b64569f55 Tests: reworked mail SSL tests to use IO::Socket::SSL.
Maxim Dounin <mdounin@mdounin.ru>
parents: 1825
diff changeset
24
7b7b64569f55 Tests: reworked mail SSL tests to use IO::Socket::SSL.
Maxim Dounin <mdounin@mdounin.ru>
parents: 1825
diff changeset
25 eval {
7b7b64569f55 Tests: reworked mail SSL tests to use IO::Socket::SSL.
Maxim Dounin <mdounin@mdounin.ru>
parents: 1825
diff changeset
26 local $SIG{ALRM} = sub { die "timeout\n" };
7b7b64569f55 Tests: reworked mail SSL tests to use IO::Socket::SSL.
Maxim Dounin <mdounin@mdounin.ru>
parents: 1825
diff changeset
27 local $SIG{PIPE} = sub { die "sigpipe\n" };
7b7b64569f55 Tests: reworked mail SSL tests to use IO::Socket::SSL.
Maxim Dounin <mdounin@mdounin.ru>
parents: 1825
diff changeset
28 alarm(8);
7b7b64569f55 Tests: reworked mail SSL tests to use IO::Socket::SSL.
Maxim Dounin <mdounin@mdounin.ru>
parents: 1825
diff changeset
29
7b7b64569f55 Tests: reworked mail SSL tests to use IO::Socket::SSL.
Maxim Dounin <mdounin@mdounin.ru>
parents: 1825
diff changeset
30 $self->{_socket} = IO::Socket::INET->new(
7b7b64569f55 Tests: reworked mail SSL tests to use IO::Socket::SSL.
Maxim Dounin <mdounin@mdounin.ru>
parents: 1825
diff changeset
31 Proto => "tcp",
7b7b64569f55 Tests: reworked mail SSL tests to use IO::Socket::SSL.
Maxim Dounin <mdounin@mdounin.ru>
parents: 1825
diff changeset
32 PeerAddr => "127.0.0.1:" . port($port),
7b7b64569f55 Tests: reworked mail SSL tests to use IO::Socket::SSL.
Maxim Dounin <mdounin@mdounin.ru>
parents: 1825
diff changeset
33 @_
7b7b64569f55 Tests: reworked mail SSL tests to use IO::Socket::SSL.
Maxim Dounin <mdounin@mdounin.ru>
parents: 1825
diff changeset
34 )
7b7b64569f55 Tests: reworked mail SSL tests to use IO::Socket::SSL.
Maxim Dounin <mdounin@mdounin.ru>
parents: 1825
diff changeset
35 or die "Can't connect to nginx: $!\n";
5
4d75bdb05ecf Tests: some generic code and ssi tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
36
1861
7b7b64569f55 Tests: reworked mail SSL tests to use IO::Socket::SSL.
Maxim Dounin <mdounin@mdounin.ru>
parents: 1825
diff changeset
37 if ({@_}->{'SSL'}) {
7b7b64569f55 Tests: reworked mail SSL tests to use IO::Socket::SSL.
Maxim Dounin <mdounin@mdounin.ru>
parents: 1825
diff changeset
38 require IO::Socket::SSL;
7b7b64569f55 Tests: reworked mail SSL tests to use IO::Socket::SSL.
Maxim Dounin <mdounin@mdounin.ru>
parents: 1825
diff changeset
39 IO::Socket::SSL->start_SSL(
7b7b64569f55 Tests: reworked mail SSL tests to use IO::Socket::SSL.
Maxim Dounin <mdounin@mdounin.ru>
parents: 1825
diff changeset
40 $self->{_socket},
1970
6d3a8f4eb9b2 Tests: relaxed SSL version used in testing.
Maxim Dounin <mdounin@mdounin.ru>
parents: 1861
diff changeset
41 SSL_version => 'SSLv23',
1861
7b7b64569f55 Tests: reworked mail SSL tests to use IO::Socket::SSL.
Maxim Dounin <mdounin@mdounin.ru>
parents: 1825
diff changeset
42 SSL_verify_mode =>
7b7b64569f55 Tests: reworked mail SSL tests to use IO::Socket::SSL.
Maxim Dounin <mdounin@mdounin.ru>
parents: 1825
diff changeset
43 IO::Socket::SSL::SSL_VERIFY_NONE(),
7b7b64569f55 Tests: reworked mail SSL tests to use IO::Socket::SSL.
Maxim Dounin <mdounin@mdounin.ru>
parents: 1825
diff changeset
44 @_
7b7b64569f55 Tests: reworked mail SSL tests to use IO::Socket::SSL.
Maxim Dounin <mdounin@mdounin.ru>
parents: 1825
diff changeset
45 )
7b7b64569f55 Tests: reworked mail SSL tests to use IO::Socket::SSL.
Maxim Dounin <mdounin@mdounin.ru>
parents: 1825
diff changeset
46 or die $IO::Socket::SSL::SSL_ERROR . "\n";
7b7b64569f55 Tests: reworked mail SSL tests to use IO::Socket::SSL.
Maxim Dounin <mdounin@mdounin.ru>
parents: 1825
diff changeset
47
7b7b64569f55 Tests: reworked mail SSL tests to use IO::Socket::SSL.
Maxim Dounin <mdounin@mdounin.ru>
parents: 1825
diff changeset
48 my $s = $self->{_socket};
7b7b64569f55 Tests: reworked mail SSL tests to use IO::Socket::SSL.
Maxim Dounin <mdounin@mdounin.ru>
parents: 1825
diff changeset
49 log_in("ssl cipher: " . $s->get_cipher());
7b7b64569f55 Tests: reworked mail SSL tests to use IO::Socket::SSL.
Maxim Dounin <mdounin@mdounin.ru>
parents: 1825
diff changeset
50 log_in("ssl cert: " . $s->peer_certificate('issuer'));
7b7b64569f55 Tests: reworked mail SSL tests to use IO::Socket::SSL.
Maxim Dounin <mdounin@mdounin.ru>
parents: 1825
diff changeset
51 }
7b7b64569f55 Tests: reworked mail SSL tests to use IO::Socket::SSL.
Maxim Dounin <mdounin@mdounin.ru>
parents: 1825
diff changeset
52
7b7b64569f55 Tests: reworked mail SSL tests to use IO::Socket::SSL.
Maxim Dounin <mdounin@mdounin.ru>
parents: 1825
diff changeset
53 alarm(0);
7b7b64569f55 Tests: reworked mail SSL tests to use IO::Socket::SSL.
Maxim Dounin <mdounin@mdounin.ru>
parents: 1825
diff changeset
54 };
7b7b64569f55 Tests: reworked mail SSL tests to use IO::Socket::SSL.
Maxim Dounin <mdounin@mdounin.ru>
parents: 1825
diff changeset
55 alarm(0);
7b7b64569f55 Tests: reworked mail SSL tests to use IO::Socket::SSL.
Maxim Dounin <mdounin@mdounin.ru>
parents: 1825
diff changeset
56 if ($@) {
7b7b64569f55 Tests: reworked mail SSL tests to use IO::Socket::SSL.
Maxim Dounin <mdounin@mdounin.ru>
parents: 1825
diff changeset
57 log_in("died: $@");
540
481d705b8610 Tests: SSL support in mail backends.
Sergey Kandaurov <pluknet@nginx.com>
parents: 539
diff changeset
58 }
481d705b8610 Tests: SSL support in mail backends.
Sergey Kandaurov <pluknet@nginx.com>
parents: 539
diff changeset
59
481d705b8610 Tests: SSL support in mail backends.
Sergey Kandaurov <pluknet@nginx.com>
parents: 539
diff changeset
60 $self->{_socket}->autoflush(1);
1678
d0025a0dead7 Tests: better timeout handling in mail tests.
Maxim Dounin <mdounin@mdounin.ru>
parents: 1660
diff changeset
61 $self->{_read_buffer} = '';
5
4d75bdb05ecf Tests: some generic code and ssi tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
62
14
d4b74207a627 Tests: refactor common functions.
Maxim Dounin <mdounin@mdounin.ru>
parents: 12
diff changeset
63 return $self;
5
4d75bdb05ecf Tests: some generic code and ssi tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
64 }
4d75bdb05ecf Tests: some generic code and ssi tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
65
1861
7b7b64569f55 Tests: reworked mail SSL tests to use IO::Socket::SSL.
Maxim Dounin <mdounin@mdounin.ru>
parents: 1825
diff changeset
66 sub DESTROY {
7b7b64569f55 Tests: reworked mail SSL tests to use IO::Socket::SSL.
Maxim Dounin <mdounin@mdounin.ru>
parents: 1825
diff changeset
67 my $self = shift;
7b7b64569f55 Tests: reworked mail SSL tests to use IO::Socket::SSL.
Maxim Dounin <mdounin@mdounin.ru>
parents: 1825
diff changeset
68 $self->{_socket}->close();
7b7b64569f55 Tests: reworked mail SSL tests to use IO::Socket::SSL.
Maxim Dounin <mdounin@mdounin.ru>
parents: 1825
diff changeset
69 }
7b7b64569f55 Tests: reworked mail SSL tests to use IO::Socket::SSL.
Maxim Dounin <mdounin@mdounin.ru>
parents: 1825
diff changeset
70
540
481d705b8610 Tests: SSL support in mail backends.
Sergey Kandaurov <pluknet@nginx.com>
parents: 539
diff changeset
71 sub eof {
481d705b8610 Tests: SSL support in mail backends.
Sergey Kandaurov <pluknet@nginx.com>
parents: 539
diff changeset
72 my $self = shift;
481d705b8610 Tests: SSL support in mail backends.
Sergey Kandaurov <pluknet@nginx.com>
parents: 539
diff changeset
73 return $self->{_socket}->eof();
481d705b8610 Tests: SSL support in mail backends.
Sergey Kandaurov <pluknet@nginx.com>
parents: 539
diff changeset
74 }
481d705b8610 Tests: SSL support in mail backends.
Sergey Kandaurov <pluknet@nginx.com>
parents: 539
diff changeset
75
481d705b8610 Tests: SSL support in mail backends.
Sergey Kandaurov <pluknet@nginx.com>
parents: 539
diff changeset
76 sub print {
481d705b8610 Tests: SSL support in mail backends.
Sergey Kandaurov <pluknet@nginx.com>
parents: 539
diff changeset
77 my ($self, $cmd) = @_;
481d705b8610 Tests: SSL support in mail backends.
Sergey Kandaurov <pluknet@nginx.com>
parents: 539
diff changeset
78 log_out($cmd);
481d705b8610 Tests: SSL support in mail backends.
Sergey Kandaurov <pluknet@nginx.com>
parents: 539
diff changeset
79 $self->{_socket}->print($cmd);
481d705b8610 Tests: SSL support in mail backends.
Sergey Kandaurov <pluknet@nginx.com>
parents: 539
diff changeset
80 }
481d705b8610 Tests: SSL support in mail backends.
Sergey Kandaurov <pluknet@nginx.com>
parents: 539
diff changeset
81
14
d4b74207a627 Tests: refactor common functions.
Maxim Dounin <mdounin@mdounin.ru>
parents: 12
diff changeset
82 sub send {
d4b74207a627 Tests: refactor common functions.
Maxim Dounin <mdounin@mdounin.ru>
parents: 12
diff changeset
83 my ($self, $cmd) = @_;
5
4d75bdb05ecf Tests: some generic code and ssi tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
84 log_out($cmd);
540
481d705b8610 Tests: SSL support in mail backends.
Sergey Kandaurov <pluknet@nginx.com>
parents: 539
diff changeset
85 $self->{_socket}->print($cmd . CRLF);
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
1678
d0025a0dead7 Tests: better timeout handling in mail tests.
Maxim Dounin <mdounin@mdounin.ru>
parents: 1660
diff changeset
88 sub getline {
d0025a0dead7 Tests: better timeout handling in mail tests.
Maxim Dounin <mdounin@mdounin.ru>
parents: 1660
diff changeset
89 my ($self) = @_;
d0025a0dead7 Tests: better timeout handling in mail tests.
Maxim Dounin <mdounin@mdounin.ru>
parents: 1660
diff changeset
90 my $socket = $self->{_socket};
d0025a0dead7 Tests: better timeout handling in mail tests.
Maxim Dounin <mdounin@mdounin.ru>
parents: 1660
diff changeset
91
d0025a0dead7 Tests: better timeout handling in mail tests.
Maxim Dounin <mdounin@mdounin.ru>
parents: 1660
diff changeset
92 if ($self->{_read_buffer} =~ /^(.*?\x0a)(.*)/ms) {
d0025a0dead7 Tests: better timeout handling in mail tests.
Maxim Dounin <mdounin@mdounin.ru>
parents: 1660
diff changeset
93 $self->{_read_buffer} = $2;
d0025a0dead7 Tests: better timeout handling in mail tests.
Maxim Dounin <mdounin@mdounin.ru>
parents: 1660
diff changeset
94 return $1;
d0025a0dead7 Tests: better timeout handling in mail tests.
Maxim Dounin <mdounin@mdounin.ru>
parents: 1660
diff changeset
95 }
d0025a0dead7 Tests: better timeout handling in mail tests.
Maxim Dounin <mdounin@mdounin.ru>
parents: 1660
diff changeset
96
d0025a0dead7 Tests: better timeout handling in mail tests.
Maxim Dounin <mdounin@mdounin.ru>
parents: 1660
diff changeset
97 while (IO::Select->new($socket)->can_read(8)) {
d0025a0dead7 Tests: better timeout handling in mail tests.
Maxim Dounin <mdounin@mdounin.ru>
parents: 1660
diff changeset
98 $socket->blocking(0);
d0025a0dead7 Tests: better timeout handling in mail tests.
Maxim Dounin <mdounin@mdounin.ru>
parents: 1660
diff changeset
99 my $n = $socket->sysread(my $buf, 1024);
1825
3629eda94c1b Tests: handling of EWOULDBLOCK from sysread() with IO::Socket::SSL.
Maxim Dounin <mdounin@mdounin.ru>
parents: 1678
diff changeset
100 my $again = !defined $n && $!{EWOULDBLOCK};
1678
d0025a0dead7 Tests: better timeout handling in mail tests.
Maxim Dounin <mdounin@mdounin.ru>
parents: 1660
diff changeset
101 $socket->blocking(1);
1825
3629eda94c1b Tests: handling of EWOULDBLOCK from sysread() with IO::Socket::SSL.
Maxim Dounin <mdounin@mdounin.ru>
parents: 1678
diff changeset
102 next if $again;
1678
d0025a0dead7 Tests: better timeout handling in mail tests.
Maxim Dounin <mdounin@mdounin.ru>
parents: 1660
diff changeset
103 last unless $n;
d0025a0dead7 Tests: better timeout handling in mail tests.
Maxim Dounin <mdounin@mdounin.ru>
parents: 1660
diff changeset
104
d0025a0dead7 Tests: better timeout handling in mail tests.
Maxim Dounin <mdounin@mdounin.ru>
parents: 1660
diff changeset
105 $self->{_read_buffer} .= $buf;
d0025a0dead7 Tests: better timeout handling in mail tests.
Maxim Dounin <mdounin@mdounin.ru>
parents: 1660
diff changeset
106
d0025a0dead7 Tests: better timeout handling in mail tests.
Maxim Dounin <mdounin@mdounin.ru>
parents: 1660
diff changeset
107 if ($self->{_read_buffer} =~ /^(.*?\x0a)(.*)/ms) {
d0025a0dead7 Tests: better timeout handling in mail tests.
Maxim Dounin <mdounin@mdounin.ru>
parents: 1660
diff changeset
108 $self->{_read_buffer} = $2;
d0025a0dead7 Tests: better timeout handling in mail tests.
Maxim Dounin <mdounin@mdounin.ru>
parents: 1660
diff changeset
109 return $1;
d0025a0dead7 Tests: better timeout handling in mail tests.
Maxim Dounin <mdounin@mdounin.ru>
parents: 1660
diff changeset
110 }
d0025a0dead7 Tests: better timeout handling in mail tests.
Maxim Dounin <mdounin@mdounin.ru>
parents: 1660
diff changeset
111 };
d0025a0dead7 Tests: better timeout handling in mail tests.
Maxim Dounin <mdounin@mdounin.ru>
parents: 1660
diff changeset
112 }
d0025a0dead7 Tests: better timeout handling in mail tests.
Maxim Dounin <mdounin@mdounin.ru>
parents: 1660
diff changeset
113
14
d4b74207a627 Tests: refactor common functions.
Maxim Dounin <mdounin@mdounin.ru>
parents: 12
diff changeset
114 sub read {
d4b74207a627 Tests: refactor common functions.
Maxim Dounin <mdounin@mdounin.ru>
parents: 12
diff changeset
115 my ($self) = @_;
540
481d705b8610 Tests: SSL support in mail backends.
Sergey Kandaurov <pluknet@nginx.com>
parents: 539
diff changeset
116 my $socket = $self->{_socket};
1678
d0025a0dead7 Tests: better timeout handling in mail tests.
Maxim Dounin <mdounin@mdounin.ru>
parents: 1660
diff changeset
117
d0025a0dead7 Tests: better timeout handling in mail tests.
Maxim Dounin <mdounin@mdounin.ru>
parents: 1660
diff changeset
118 while (defined($_ = $self->getline())) {
d0025a0dead7 Tests: better timeout handling in mail tests.
Maxim Dounin <mdounin@mdounin.ru>
parents: 1660
diff changeset
119 log_in($_);
d0025a0dead7 Tests: better timeout handling in mail tests.
Maxim Dounin <mdounin@mdounin.ru>
parents: 1660
diff changeset
120 next if m/^\d\d\d-/;
d0025a0dead7 Tests: better timeout handling in mail tests.
Maxim Dounin <mdounin@mdounin.ru>
parents: 1660
diff changeset
121 last;
5
4d75bdb05ecf Tests: some generic code and ssi tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
122 }
1678
d0025a0dead7 Tests: better timeout handling in mail tests.
Maxim Dounin <mdounin@mdounin.ru>
parents: 1660
diff changeset
123
5
4d75bdb05ecf Tests: some generic code and ssi tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
124 return $_;
4d75bdb05ecf Tests: some generic code and ssi tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
125 }
4d75bdb05ecf Tests: some generic code and ssi tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
126
14
d4b74207a627 Tests: refactor common functions.
Maxim Dounin <mdounin@mdounin.ru>
parents: 12
diff changeset
127 sub check {
d4b74207a627 Tests: refactor common functions.
Maxim Dounin <mdounin@mdounin.ru>
parents: 12
diff changeset
128 my ($self, $regex, $name) = @_;
57
b5b4271554b0 Test: use TODO for smtp pipelining tests.
Maxim Dounin <mdounin@mdounin.ru>
parents: 45
diff changeset
129 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
130 }
4d75bdb05ecf Tests: some generic code and ssi tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
131
14
d4b74207a627 Tests: refactor common functions.
Maxim Dounin <mdounin@mdounin.ru>
parents: 12
diff changeset
132 sub ok {
243
de7338227832 Tests: removed trailing spaces.
Homutov Vladimir <vl@nginx.com>
parents: 209
diff changeset
133 my $self = shift;
57
b5b4271554b0 Test: use TODO for smtp pipelining tests.
Maxim Dounin <mdounin@mdounin.ru>
parents: 45
diff changeset
134 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
135 }
4d75bdb05ecf Tests: some generic code and ssi tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
136
60
11e33ba0656a Tests: check for 235 code on successful smtp auth.
Maxim Dounin <mdounin@mdounin.ru>
parents: 57
diff changeset
137 sub authok {
243
de7338227832 Tests: removed trailing spaces.
Homutov Vladimir <vl@nginx.com>
parents: 209
diff changeset
138 my $self = shift;
60
11e33ba0656a Tests: check for 235 code on successful smtp auth.
Maxim Dounin <mdounin@mdounin.ru>
parents: 57
diff changeset
139 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
140 }
11e33ba0656a Tests: check for 235 code on successful smtp auth.
Maxim Dounin <mdounin@mdounin.ru>
parents: 57
diff changeset
141
1244
575d39cc0e35 Tests: worker_shutdown_timeout within the mail module.
Sergey Kandaurov <pluknet@nginx.com>
parents: 969
diff changeset
142 sub can_read {
575d39cc0e35 Tests: worker_shutdown_timeout within the mail module.
Sergey Kandaurov <pluknet@nginx.com>
parents: 969
diff changeset
143 my ($self, $timo) = @_;
575d39cc0e35 Tests: worker_shutdown_timeout within the mail module.
Sergey Kandaurov <pluknet@nginx.com>
parents: 969
diff changeset
144 IO::Select->new($self->{_socket})->can_read($timo || 3);
575d39cc0e35 Tests: worker_shutdown_timeout within the mail module.
Sergey Kandaurov <pluknet@nginx.com>
parents: 969
diff changeset
145 }
575d39cc0e35 Tests: worker_shutdown_timeout within the mail module.
Sergey Kandaurov <pluknet@nginx.com>
parents: 969
diff changeset
146
1861
7b7b64569f55 Tests: reworked mail SSL tests to use IO::Socket::SSL.
Maxim Dounin <mdounin@mdounin.ru>
parents: 1825
diff changeset
147 sub socket {
7b7b64569f55 Tests: reworked mail SSL tests to use IO::Socket::SSL.
Maxim Dounin <mdounin@mdounin.ru>
parents: 1825
diff changeset
148 my ($self) = @_;
7b7b64569f55 Tests: reworked mail SSL tests to use IO::Socket::SSL.
Maxim Dounin <mdounin@mdounin.ru>
parents: 1825
diff changeset
149 $self->{_socket};
7b7b64569f55 Tests: reworked mail SSL tests to use IO::Socket::SSL.
Maxim Dounin <mdounin@mdounin.ru>
parents: 1825
diff changeset
150 }
7b7b64569f55 Tests: reworked mail SSL tests to use IO::Socket::SSL.
Maxim Dounin <mdounin@mdounin.ru>
parents: 1825
diff changeset
151
5
4d75bdb05ecf Tests: some generic code and ssi tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
152 ###############################################################################
4d75bdb05ecf Tests: some generic code and ssi tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
153
26
0880e0fafde4 Tests: implement smtp test backend and use it.
Maxim Dounin <mdounin@mdounin.ru>
parents: 14
diff changeset
154 sub smtp_test_daemon {
951
9361c7eddfc1 Tests: parallel tests support.
Andrey Zelenkov <zelenkov@nginx.com>
parents: 540
diff changeset
155 my ($port) = @_;
1660
068c30e9d2c6 Tests: smtp tests with proxy protocol to backend.
Sergey Kandaurov <pluknet@nginx.com>
parents: 1599
diff changeset
156 my $proxy_protocol;
951
9361c7eddfc1 Tests: parallel tests support.
Andrey Zelenkov <zelenkov@nginx.com>
parents: 540
diff changeset
157
26
0880e0fafde4 Tests: implement smtp test backend and use it.
Maxim Dounin <mdounin@mdounin.ru>
parents: 14
diff changeset
158 my $server = IO::Socket::INET->new(
0880e0fafde4 Tests: implement smtp test backend and use it.
Maxim Dounin <mdounin@mdounin.ru>
parents: 14
diff changeset
159 Proto => 'tcp',
969
1edb092149e2 Tests: simplified parallel tests.
Maxim Dounin <mdounin@mdounin.ru>
parents: 951
diff changeset
160 LocalAddr => '127.0.0.1:' . ($port || port(8026)),
26
0880e0fafde4 Tests: implement smtp test backend and use it.
Maxim Dounin <mdounin@mdounin.ru>
parents: 14
diff changeset
161 Listen => 5,
0880e0fafde4 Tests: implement smtp test backend and use it.
Maxim Dounin <mdounin@mdounin.ru>
parents: 14
diff changeset
162 Reuse => 1
0880e0fafde4 Tests: implement smtp test backend and use it.
Maxim Dounin <mdounin@mdounin.ru>
parents: 14
diff changeset
163 )
0880e0fafde4 Tests: implement smtp test backend and use it.
Maxim Dounin <mdounin@mdounin.ru>
parents: 14
diff changeset
164 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
165
0880e0fafde4 Tests: implement smtp test backend and use it.
Maxim Dounin <mdounin@mdounin.ru>
parents: 14
diff changeset
166 while (my $client = $server->accept()) {
0880e0fafde4 Tests: implement smtp test backend and use it.
Maxim Dounin <mdounin@mdounin.ru>
parents: 14
diff changeset
167 $client->autoflush(1);
0880e0fafde4 Tests: implement smtp test backend and use it.
Maxim Dounin <mdounin@mdounin.ru>
parents: 14
diff changeset
168 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
169
1660
068c30e9d2c6 Tests: smtp tests with proxy protocol to backend.
Sergey Kandaurov <pluknet@nginx.com>
parents: 1599
diff changeset
170 $proxy_protocol = '';
068c30e9d2c6 Tests: smtp tests with proxy protocol to backend.
Sergey Kandaurov <pluknet@nginx.com>
parents: 1599
diff changeset
171
26
0880e0fafde4 Tests: implement smtp test backend and use it.
Maxim Dounin <mdounin@mdounin.ru>
parents: 14
diff changeset
172 while (<$client>) {
68
5f56040c39df Tests: smtp xclient tests.
Maxim Dounin <mdounin@mdounin.ru>
parents: 60
diff changeset
173 Test::Nginx::log_core('||', $_);
5f56040c39df Tests: smtp xclient tests.
Maxim Dounin <mdounin@mdounin.ru>
parents: 60
diff changeset
174
26
0880e0fafde4 Tests: implement smtp test backend and use it.
Maxim Dounin <mdounin@mdounin.ru>
parents: 14
diff changeset
175 if (/^quit/i) {
0880e0fafde4 Tests: implement smtp test backend and use it.
Maxim Dounin <mdounin@mdounin.ru>
parents: 14
diff changeset
176 print $client '221 quit ok' . CRLF;
0880e0fafde4 Tests: implement smtp test backend and use it.
Maxim Dounin <mdounin@mdounin.ru>
parents: 14
diff changeset
177 } elsif (/^(ehlo|helo)/i) {
0880e0fafde4 Tests: implement smtp test backend and use it.
Maxim Dounin <mdounin@mdounin.ru>
parents: 14
diff changeset
178 print $client '250 hello ok' . CRLF;
0880e0fafde4 Tests: implement smtp test backend and use it.
Maxim Dounin <mdounin@mdounin.ru>
parents: 14
diff changeset
179 } elsif (/^rset/i) {
0880e0fafde4 Tests: implement smtp test backend and use it.
Maxim Dounin <mdounin@mdounin.ru>
parents: 14
diff changeset
180 print $client '250 rset ok' . CRLF;
1599
4e0644119341 Tests: proxy_smtp_auth directive tests.
Sergey Kandaurov <pluknet@nginx.com>
parents: 1423
diff changeset
181 } elsif (/^auth plain/i) {
4e0644119341 Tests: proxy_smtp_auth directive tests.
Sergey Kandaurov <pluknet@nginx.com>
parents: 1423
diff changeset
182 print $client '235 auth ok' . CRLF;
26
0880e0fafde4 Tests: implement smtp test backend and use it.
Maxim Dounin <mdounin@mdounin.ru>
parents: 14
diff changeset
183 } elsif (/^mail from:[^@]+$/i) {
0880e0fafde4 Tests: implement smtp test backend and use it.
Maxim Dounin <mdounin@mdounin.ru>
parents: 14
diff changeset
184 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
185 } elsif (/^mail from:/i) {
0880e0fafde4 Tests: implement smtp test backend and use it.
Maxim Dounin <mdounin@mdounin.ru>
parents: 14
diff changeset
186 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
187 } elsif (/^rcpt to:[^@]+$/i) {
0880e0fafde4 Tests: implement smtp test backend and use it.
Maxim Dounin <mdounin@mdounin.ru>
parents: 14
diff changeset
188 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
189 } elsif (/^rcpt to:/i) {
0880e0fafde4 Tests: implement smtp test backend and use it.
Maxim Dounin <mdounin@mdounin.ru>
parents: 14
diff changeset
190 print $client '250 rcpt to ok' . CRLF;
68
5f56040c39df Tests: smtp xclient tests.
Maxim Dounin <mdounin@mdounin.ru>
parents: 60
diff changeset
191 } elsif (/^xclient/i) {
5f56040c39df Tests: smtp xclient tests.
Maxim Dounin <mdounin@mdounin.ru>
parents: 60
diff changeset
192 print $client '220 xclient ok' . CRLF;
1660
068c30e9d2c6 Tests: smtp tests with proxy protocol to backend.
Sergey Kandaurov <pluknet@nginx.com>
parents: 1599
diff changeset
193 } elsif (/^proxy/i) {
068c30e9d2c6 Tests: smtp tests with proxy protocol to backend.
Sergey Kandaurov <pluknet@nginx.com>
parents: 1599
diff changeset
194 $proxy_protocol = $_;
068c30e9d2c6 Tests: smtp tests with proxy protocol to backend.
Sergey Kandaurov <pluknet@nginx.com>
parents: 1599
diff changeset
195 } elsif (/^xproxy/i) {
068c30e9d2c6 Tests: smtp tests with proxy protocol to backend.
Sergey Kandaurov <pluknet@nginx.com>
parents: 1599
diff changeset
196 print $client '211 ' . $proxy_protocol . CRLF;
26
0880e0fafde4 Tests: implement smtp test backend and use it.
Maxim Dounin <mdounin@mdounin.ru>
parents: 14
diff changeset
197 } else {
0880e0fafde4 Tests: implement smtp test backend and use it.
Maxim Dounin <mdounin@mdounin.ru>
parents: 14
diff changeset
198 print $client "500 unknown command" . CRLF;
0880e0fafde4 Tests: implement smtp test backend and use it.
Maxim Dounin <mdounin@mdounin.ru>
parents: 14
diff changeset
199 }
539
3fcad5e66735 Tests: whitespace fixes in mail modules.
Sergey Kandaurov <pluknet@nginx.com>
parents: 526
diff changeset
200 }
26
0880e0fafde4 Tests: implement smtp test backend and use it.
Maxim Dounin <mdounin@mdounin.ru>
parents: 14
diff changeset
201
0880e0fafde4 Tests: implement smtp test backend and use it.
Maxim Dounin <mdounin@mdounin.ru>
parents: 14
diff changeset
202 close $client;
0880e0fafde4 Tests: implement smtp test backend and use it.
Maxim Dounin <mdounin@mdounin.ru>
parents: 14
diff changeset
203 }
0880e0fafde4 Tests: implement smtp test backend and use it.
Maxim Dounin <mdounin@mdounin.ru>
parents: 14
diff changeset
204 }
0880e0fafde4 Tests: implement smtp test backend and use it.
Maxim Dounin <mdounin@mdounin.ru>
parents: 14
diff changeset
205
0880e0fafde4 Tests: implement smtp test backend and use it.
Maxim Dounin <mdounin@mdounin.ru>
parents: 14
diff changeset
206 ###############################################################################
0880e0fafde4 Tests: implement smtp test backend and use it.
Maxim Dounin <mdounin@mdounin.ru>
parents: 14
diff changeset
207
5
4d75bdb05ecf Tests: some generic code and ssi tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
208 1;
4d75bdb05ecf Tests: some generic code and ssi tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
209
4d75bdb05ecf Tests: some generic code and ssi tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
210 ###############################################################################