comparison mail_resolver.t @ 352:145c37f27c5a

Tests: added resolver tests.
author Sergey Kandaurov <pluknet@nginx.com>
date Thu, 14 Nov 2013 16:17:03 +0400
parents
children 14eba1ad0880
comparison
equal deleted inserted replaced
351:3d3c8b5ea8ee 352:145c37f27c5a
1 #!/usr/bin/perl
2
3 # (C) Sergey Kandaurov
4 # (C) Nginx, Inc.
5
6 # Tests for mail resolver
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::SMTP;
20
21 ###############################################################################
22
23 select STDERR; $| = 1;
24 select STDOUT; $| = 1;
25
26 local $SIG{PIPE} = 'IGNORE';
27
28 eval { require Net::DNS::Nameserver; };
29 plan(skip_all => "Net::DNS::Nameserver not installed") if $@;
30
31 my $t = Test::Nginx->new()->has(qw/http mail smtp rewrite/);
32
33 $t->write_file_expand('nginx.conf', <<'EOF');
34
35 %%TEST_GLOBALS%%
36
37 daemon off;
38
39 events {
40 }
41
42 mail {
43 auth_http http://127.0.0.1:8080/mail/auth;
44 smtp_auth none;
45 server_name locahost;
46
47 server {
48 listen 127.0.0.1:8025;
49 protocol smtp;
50 resolver 127.0.0.1:8081 127.0.0.1:8082 127.0.0.1:8083;
51 }
52
53 server {
54 listen 127.0.0.1:8027;
55 protocol smtp;
56 resolver 127.0.0.1:8082;
57 }
58
59 server {
60 listen 127.0.0.1:8028;
61 protocol smtp;
62 resolver 127.0.0.1:8083;
63
64 # prevent useless resend
65 resolver_timeout 1s;
66 }
67
68 server {
69 listen 127.0.0.1:8029;
70 protocol smtp;
71 resolver 127.0.0.1:8084;
72 }
73 }
74
75 http {
76 %%TEST_GLOBALS_HTTP%%
77
78 server {
79 listen 127.0.0.1:8080;
80 server_name localhost;
81
82 location = /mail/auth {
83 set $reply $http_client_host;
84
85 if ($http_client_host !~ UNAVAIL) {
86 set $reply OK;
87 }
88
89 add_header Auth-Status $reply;
90 add_header Auth-Server 127.0.0.1;
91 add_header Auth-Port 8026;
92 return 204;
93 }
94 }
95 }
96
97 EOF
98
99 $t->run_daemon(\&dns_daemon, 8081);
100 $t->run_daemon(\&dns_daemon, 8082);
101 $t->run_daemon(\&dns_daemon, 8083);
102 $t->run_daemon(\&dns_daemon, 8084);
103 $t->run_daemon(\&Test::Nginx::SMTP::smtp_test_daemon);
104 $t->run();
105
106 $t->waitforsocket('127.0.0.1:8081');
107 $t->waitforsocket('127.0.0.1:8082');
108 $t->waitforsocket('127.0.0.1:8083');
109 $t->waitforsocket('127.0.0.1:8084');
110
111 $t->plan(5);
112
113 ###############################################################################
114
115 # PTR
116
117 my $s = Test::Nginx::SMTP->new();
118 $s->read();
119 $s->send('EHLO example.com');
120 $s->read();
121 $s->send('MAIL FROM:<test@example.com> SIZE=100');
122 $s->read();
123
124 $s->send('RCPT TO:<test@example.com>');
125 $s->ok('PTR');
126
127 $s->send('QUIT');
128 $s->read();
129 close $s;
130
131 # Cached PTR prevents from querying bad ns on port 8083
132
133 $s = Test::Nginx::SMTP->new();
134 $s->read();
135 $s->send('EHLO example.com');
136 $s->read();
137 $s->send('MAIL FROM:<test@example.com> SIZE=100');
138 $s->read();
139
140 $s->send('RCPT TO:<test@example.com>');
141 $s->ok('PTR cached');
142
143 $s->send('QUIT');
144 $s->read();
145 close $s;
146
147 # SERVFAIL
148
149 $s = Test::Nginx::SMTP->new(PeerAddr => "127.0.0.1:8027");
150 $s->read();
151 $s->send('EHLO example.com');
152 $s->read();
153 $s->send('MAIL FROM:<test@example.com> SIZE=100');
154 $s->read();
155
156 $s->send('RCPT TO:<test@example.com>');
157 $s->check(qr/TEMPUNAVAIL/, 'PTR SERVFAIL');
158
159 $s->send('QUIT');
160 $s->read();
161 close $s;
162
163 # PTR with zero length RDATA
164
165 TODO: {
166 local $TODO = 'not yet';
167
168 $s = Test::Nginx::SMTP->new(PeerAddr => "127.0.0.1:8028");
169 $s->read();
170 $s->send('EHLO example.com');
171 $s->read();
172 $s->send('MAIL FROM:<test@example.com> SIZE=100');
173 $s->read();
174
175 $s->send('RCPT TO:<test@example.com>');
176 $s->check(qr/TEMPUNAVAIL/, 'PTR empty');
177
178 $s->send('QUIT');
179 $s->read();
180 close $s;
181
182 }
183
184 # CNAME
185
186 TODO: {
187 local $TODO = 'support for CNAME RR';
188
189 $s = Test::Nginx::SMTP->new(PeerAddr => "127.0.0.1:8029");
190 $s->read();
191 $s->send('EHLO example.com');
192 $s->read();
193 $s->send('MAIL FROM:<test@example.com> SIZE=100');
194 $s->read();
195
196 $s->send('RCPT TO:<test@example.com>');
197 $s->ok('PTR with CNAME');
198
199 $s->send('QUIT');
200 $s->read();
201 close $s;
202
203 }
204
205 ###############################################################################
206
207 sub reply_handler {
208 my ($name, $class, $type, $peerhost, $query, $conn) = @_;
209 my ($rcode, @ans, $ttl, $rdata);
210
211 $rcode = 'NOERROR';
212 $ttl = 3600;
213
214 if ($name eq 'a.example.net' && $type eq 'A') {
215 ($rdata) = ('127.0.0.1');
216 push @ans, Net::DNS::RR->new("$name $ttl $class $type $rdata");
217
218 } elsif ($name eq '1.0.0.127.in-addr.arpa' && $type eq 'PTR') {
219 if ($conn->{sockport} == 8081) {
220 $rdata = 'a.example.net';
221 push @ans, Net::DNS::RR->new(
222 "$name $ttl $class $type $rdata"
223 );
224
225 } elsif ($conn->{sockport} == 8082) {
226 return 'SERVFAIL';
227
228 } elsif ($conn->{sockport} == 8083) {
229 # zero length RDATA
230 $rdata = '';
231 push @ans, Net::DNS::RR->new(
232 "$name $ttl $class $type $rdata"
233 );
234
235 } elsif ($conn->{sockport} == 8084) {
236 # PTR answered with CNAME
237 ($type, $rdata) = ('CNAME',
238 '1.1.0.0.127.in-addr.arpa');
239 push @ans, Net::DNS::RR->new(
240 "$name $ttl $class $type $rdata"
241 );
242 }
243
244 } elsif ($name eq '1.1.0.0.127.in-addr.arpa' && $type eq 'PTR') {
245 $rdata = 'a.example.net';
246 push @ans, Net::DNS::RR->new("$name $ttl $class $type $rdata");
247
248 } else {
249 $rcode = 'NXDOMAIN';
250 }
251
252 return ($rcode, \@ans);
253 }
254
255 sub dns_daemon {
256 my ($port) = @_;
257
258 my $ns = Net::DNS::Nameserver->new(
259 LocalAddr => '127.0.0.1',
260 LocalPort => $port,
261 Proto => 'udp',
262 ReplyHandler => \&reply_handler,
263 )
264 or die "Can't create nameserver object: $!\n";
265
266 $ns->main_loop;
267 }
268
269 ###############################################################################