comparison mail_capability.t @ 1146:2634d0ef48d3

Tests: mail capability tests.
author Sergey Kandaurov <pluknet@nginx.com>
date Mon, 06 Mar 2017 19:28:04 +0300
parents
children 57673c7257a3
comparison
equal deleted inserted replaced
1145:f193664e06d8 1146:2634d0ef48d3
1 #!/usr/bin/perl
2
3 # (C) Sergey Kandaurov
4 # (C) Nginx, Inc.
5
6 # Tests for imap/pop3/smtp capabilities.
7
8 ###############################################################################
9
10 use warnings;
11 use strict;
12
13 use Test::More;
14
15 BEGIN { use FindBin; chdir($FindBin::Bin); }
16
17 use lib 'lib';
18 use Test::Nginx;
19 use Test::Nginx::IMAP;
20 use Test::Nginx::POP3;
21 use Test::Nginx::SMTP;
22
23 ###############################################################################
24
25 select STDERR; $| = 1;
26 select STDOUT; $| = 1;
27
28 my $t = Test::Nginx->new()->has(qw/mail mail_ssl imap pop3 smtp/)
29 ->has_daemon('openssl')->plan(17);
30
31 $t->write_file_expand('nginx.conf', <<'EOF');
32
33 %%TEST_GLOBALS%%
34
35 daemon off;
36
37 events {
38 }
39
40 mail {
41 ssl_certificate_key localhost.key;
42 ssl_certificate localhost.crt;
43
44 auth_http http://127.0.0.1:8080; # unused
45
46 pop3_auth plain apop cram-md5;
47
48 server {
49 listen 127.0.0.1:8143;
50 protocol imap;
51 imap_capabilities SEE-THIS;
52 }
53
54 server {
55 listen 127.0.0.1:8144;
56 protocol imap;
57 starttls on;
58 }
59
60 server {
61 listen 127.0.0.1:8145;
62 protocol imap;
63 starttls only;
64 }
65
66 server {
67 listen 127.0.0.1:8110;
68 protocol pop3;
69 }
70
71 server {
72 listen 127.0.0.1:8111;
73 protocol pop3;
74 starttls on;
75 }
76
77 server {
78 listen 127.0.0.1:8112;
79 protocol pop3;
80 starttls only;
81 }
82
83 server {
84 listen 127.0.0.1:8025;
85 protocol smtp;
86 starttls off;
87 }
88
89 server {
90 listen 127.0.0.1:8026;
91 protocol smtp;
92 starttls on;
93 }
94
95 server {
96 listen 127.0.0.1:8027;
97 protocol smtp;
98 starttls only;
99 }
100 }
101
102 EOF
103
104 $t->write_file('openssl.conf', <<EOF);
105 [ req ]
106 default_bits = 1024
107 encrypt_key = no
108 distinguished_name = req_distinguished_name
109 [ req_distinguished_name ]
110 EOF
111
112 my $d = $t->testdir();
113
114 foreach my $name ('localhost') {
115 system('openssl req -x509 -new '
116 . "-config '$d/openssl.conf' -subj '/CN=$name/' "
117 . "-out '$d/$name.crt' -keyout '$d/$name.key' "
118 . ">>$d/openssl.out 2>&1") == 0
119 or die "Can't create certificate for $name: $!\n";
120 }
121
122 $t->run();
123
124 ###############################################################################
125
126 # imap, custom capabilities
127
128 my $s = Test::Nginx::IMAP->new();
129 $s->read();
130
131 $s->send('1 CAPABILITY');
132 $s->check(qr/^\* CAPABILITY SEE-THIS AUTH=PLAIN/, 'imap capability');
133 $s->ok('imap capability completed');
134
135 # imap starttls
136
137 $s = Test::Nginx::IMAP->new(PeerAddr => '127.0.0.1:' . port(8144));
138 $s->read();
139
140 $s->send('1 CAPABILITY');
141 $s->check(qr/^\* CAPABILITY IMAP4 IMAP4rev1 UIDPLUS AUTH=PLAIN STARTTLS/,
142 'imap capability starttls');
143
144 # imap starttls only
145
146 $s = Test::Nginx::IMAP->new(PeerAddr => '127.0.0.1:' . port(8145));
147 $s->read();
148
149 $s->send('1 CAPABILITY');
150 $s->check(qr/^\* CAPABILITY IMAP4 IMAP4rev1 UIDPLUS STARTTLS LOGINDISABLED/,
151 'imap capability starttls only');
152
153 # pop3
154
155 $s = Test::Nginx::POP3->new(PeerAddr => '127.0.0.1:' . port(8110));
156 $s->read();
157
158 $s->send('CAPA');
159 $s->ok('pop3 capa');
160
161 my $caps = get_auth_caps($s);
162 like($caps, qr/USER/, 'pop3 - user');
163
164 TODO: {
165 local $TODO = 'not yet' if $t->has_version('1.11.6')
166 and !$t->has_version('1.11.11');
167
168 like($caps, qr/SASL (PLAIN LOGIN|LOGIN PLAIN) CRAM-MD5/, 'pop3 - methods');
169
170 }
171
172 unlike($caps, qr/STLS/, 'pop3 - no stls');
173
174 # pop3 starttls
175
176 $s = Test::Nginx::POP3->new(PeerAddr => '127.0.0.1:' . port(8111));
177 $s->read();
178
179 $s->send('CAPA');
180
181 $caps = get_auth_caps($s);
182 like($caps, qr/USER/, 'pop3 starttls - user');
183
184 TODO: {
185 local $TODO = 'not yet' if $t->has_version('1.11.6')
186 and !$t->has_version('1.11.11');
187
188 like($caps, qr/SASL (PLAIN LOGIN|LOGIN PLAIN) CRAM-MD5/,
189 'pop3 starttls - methods');
190
191 }
192
193 like($caps, qr/STLS/, 'pop3 startls - stls');
194
195 # pop3 starttls only
196
197 $s = Test::Nginx::POP3->new(PeerAddr => '127.0.0.1:' . port(8112));
198 $s->read();
199
200 $s->send('CAPA');
201
202 $caps = get_auth_caps($s);
203 unlike($caps, qr/USER/, 'pop3 starttls only - no user');
204 unlike($caps, qr/SASL/, 'pop3 starttls only - no methods');
205 like($caps, qr/STLS/, 'pop3 startls only - stls');
206
207 # smtp
208
209 $s = Test::Nginx::SMTP->new(PeerAddr => '127.0.0.1:' . port(8025));
210 $s->read();
211
212 $s->send('EHLO example.com');
213 $s->check(qr/^250 AUTH PLAIN LOGIN\x0d\x0a?/, 'smtp ehlo');
214
215 # smtp starttls
216
217 $s = Test::Nginx::SMTP->new(PeerAddr => '127.0.0.1:' . port(8026));
218 $s->read();
219
220 $s->send('EHLO example.com');
221 $s->check(qr/^250 STARTTLS/, 'smtp ehlo - starttls');
222
223 # smtp starttls only
224
225 $s = Test::Nginx::SMTP->new(PeerAddr => '127.0.0.1:' . port(8027));
226 $s->read();
227
228 $s->send('EHLO example.com');
229 $s->check(qr/^250 STARTTLS/, 'smtp ehlo - starttls only');
230
231 ###############################################################################
232
233 sub get_auth_caps {
234 my ($s) = @_;
235 my @meth;
236
237 while ($s->read() !~ qr/^\./) {
238 push @meth, $1 if /(.*?)\x0d\x0a?/ms;
239 }
240 join ':', @meth;
241 }
242
243 ###############################################################################