Mercurial > hg > nginx-tests
comparison http_resolver_cname.t @ 825:ff8c68cd2be4
Tests: http resolver tests with CNAME, complicated cases.
author | Sergey Kandaurov <pluknet@nginx.com> |
---|---|
date | Wed, 27 Jan 2016 14:08:02 +0300 |
parents | |
children | 44a9a45aa041 |
comparison
equal
deleted
inserted
replaced
824:9caacdb56b53 | 825:ff8c68cd2be4 |
---|---|
1 #!/usr/bin/perl | |
2 | |
3 # (C) Sergey Kandaurov | |
4 # (C) Nginx, Inc. | |
5 | |
6 # Tests for http resolver with CNAME. | |
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 qw/ :DEFAULT http_end /; | |
19 | |
20 ############################################################################### | |
21 | |
22 select STDERR; $| = 1; | |
23 select STDOUT; $| = 1; | |
24 | |
25 my $t = Test::Nginx->new()->has(qw/http proxy/)->plan(11); | |
26 | |
27 $t->write_file_expand('nginx.conf', <<'EOF'); | |
28 | |
29 %%TEST_GLOBALS%% | |
30 | |
31 daemon off; | |
32 | |
33 events { | |
34 } | |
35 | |
36 http { | |
37 %%TEST_GLOBALS_HTTP%% | |
38 | |
39 server { | |
40 listen 127.0.0.1:8080; | |
41 server_name localhost; | |
42 | |
43 location /short { | |
44 resolver 127.0.0.1:8081; | |
45 resolver_timeout 2s; | |
46 | |
47 proxy_pass http://$host:8080/t; | |
48 } | |
49 | |
50 location /long { | |
51 resolver 127.0.0.1:8081; | |
52 resolver_timeout 5s; | |
53 | |
54 proxy_pass http://$host:8080/t; | |
55 } | |
56 | |
57 location / { } | |
58 } | |
59 } | |
60 | |
61 EOF | |
62 | |
63 $t->run_daemon(\&dns_daemon, 8081, $t); | |
64 | |
65 $t->write_file('t', ''); | |
66 $t->run(); | |
67 | |
68 $t->waitforfile($t->testdir . '/8081'); | |
69 | |
70 ############################################################################### | |
71 | |
72 # CNAME pointing to name which times out | |
73 | |
74 like(http_host('cn01.example.net', '/short'), qr/502 Bad/, 'CNAME timeout'); | |
75 | |
76 # several requests on CNAME pointing to invalid name | |
77 | |
78 my @s; | |
79 | |
80 push @s, http_host('cn03.example.net', '/long', start => 1); | |
81 push @s, http_host('cn03.example.net', '/long', start => 1); | |
82 | |
83 like(http_end(pop @s), qr/502 Bad/, 'invalid CNAME - first'); | |
84 like(http_end(pop @s), qr/502 Bad/, 'invalid CNAME - last'); | |
85 | |
86 # several requests on CNAME pointing to cached name | |
87 | |
88 @s = (); | |
89 | |
90 http_host('a.example.net', '/long'); | |
91 | |
92 push @s, http_host('cn04.example.net', '/long', start => 1); | |
93 push @s, http_host('cn04.example.net', '/long', start => 1); | |
94 | |
95 like(http_end(pop @s), qr/200 OK/, 'cached CNAME - first'); | |
96 like(http_end(pop @s), qr/200 OK/, 'cached CNAME - last'); | |
97 | |
98 # several requests on CNAME pointing to name being resolved | |
99 | |
100 @s = (); | |
101 | |
102 my $s = http_host('cn06.example.net', '/long', start => 1); | |
103 | |
104 sleep 1; | |
105 | |
106 push @s, http_host('cn05.example.net', '/long', start => 1); | |
107 push @s, http_host('cn05.example.net', '/long', start => 1); | |
108 | |
109 like(http_end(pop @s), qr/502 Bad/, 'CNAME in progress - first'); | |
110 like(http_end(pop @s), qr/502 Bad/, 'CNAME in progress - last'); | |
111 | |
112 # several requests on CNAME pointing to name which times out | |
113 # 1st request receives CNAME with short ttl | |
114 # 2nd request replaces expired CNAME | |
115 | |
116 @s = (); | |
117 | |
118 push @s, http_host('cn07.example.net', '/long', start => 1); | |
119 | |
120 sleep 2; | |
121 | |
122 push @s, http_host('cn07.example.net', '/long', start => 1); | |
123 | |
124 like(http_end(pop @s), qr/502 Bad/, 'CNAME ttl - first'); | |
125 like(http_end(pop @s), qr/502 Bad/, 'CNAME ttl - last'); | |
126 | |
127 # several requests on CNAME pointing to name | |
128 # 1st request aborts before name is resolved | |
129 # 2nd request finishes with name resolved | |
130 | |
131 @s = (); | |
132 | |
133 push @s, http_host('cn09.example.net', '/long', start => 1); | |
134 push @s, http_host('cn09.example.net', '/long', start => 1); | |
135 | |
136 sleep 2; | |
137 | |
138 close(shift @s); | |
139 | |
140 like(http_end(pop @s), qr/200 OK/, 'abort on CNAME'); | |
141 | |
142 like(http_host('cn001.example.net', '/short'), qr/502 Bad/, 'recurse uncached'); | |
143 | |
144 ############################################################################### | |
145 | |
146 sub http_host { | |
147 my ($host, $uri, %extra) = @_; | |
148 return http(<<EOF, %extra); | |
149 GET $uri HTTP/1.0 | |
150 Host: $host | |
151 | |
152 EOF | |
153 } | |
154 | |
155 ############################################################################### | |
156 | |
157 sub reply_handler { | |
158 my ($recv_data, $port) = @_; | |
159 | |
160 my (@name, @rdata); | |
161 | |
162 use constant NOERROR => 0; | |
163 | |
164 use constant A => 1; | |
165 use constant CNAME => 5; | |
166 | |
167 use constant IN => 1; | |
168 | |
169 # default values | |
170 | |
171 my ($hdr, $rcode, $ttl) = (0x8180, NOERROR, 3600); | |
172 | |
173 # decode name | |
174 | |
175 my ($len, $offset) = (undef, 12); | |
176 while (1) { | |
177 $len = unpack("\@$offset C", $recv_data); | |
178 last if $len == 0; | |
179 $offset++; | |
180 push @name, unpack("\@$offset A$len", $recv_data); | |
181 $offset += $len; | |
182 } | |
183 | |
184 $offset -= 1; | |
185 my ($id, $type, $class) = unpack("n x$offset n2", $recv_data); | |
186 | |
187 my $name = join('.', @name); | |
188 if ($name eq 'a.example.net' && $type == A) { | |
189 push @rdata, rd_addr($ttl, '127.0.0.1'); | |
190 | |
191 } elsif ($name eq 'b.example.net' && $type == A) { | |
192 sleep 2; | |
193 push @rdata, rd_addr($ttl, '127.0.0.1'); | |
194 | |
195 } elsif ($name eq 'cn01.example.net') { | |
196 $ttl = 1; | |
197 push @rdata, pack("n3N nCa4n", 0xc00c, CNAME, IN, $ttl, | |
198 7, 4, "cn02", 0xc011); | |
199 | |
200 } elsif ($name =~ /cn0[268].example.net/) { | |
201 # resolver timeout | |
202 | |
203 return; | |
204 | |
205 } elsif ($name eq 'cn03.example.net') { | |
206 select undef, undef, undef, 1.1; | |
207 push @rdata, pack("n3N nC", 0xc00c, CNAME, IN, $ttl, 0); | |
208 | |
209 } elsif ($name eq 'cn04.example.net') { | |
210 select undef, undef, undef, 1.1; | |
211 push @rdata, pack("n3N nCa1n", 0xc00c, CNAME, IN, $ttl, | |
212 4, 1, "a", 0xc011); | |
213 | |
214 } elsif ($name eq 'cn05.example.net') { | |
215 select undef, undef, undef, 1.1; | |
216 push @rdata, pack("n3N nCa4n", 0xc00c, CNAME, IN, $ttl, | |
217 7, 4, "cn06", 0xc011); | |
218 | |
219 } elsif ($name eq 'cn07.example.net') { | |
220 $ttl = 1; | |
221 push @rdata, pack("n3N nCa4n", 0xc00c, CNAME, IN, $ttl, | |
222 7, 4, "cn08", 0xc011); | |
223 | |
224 } elsif ($name eq 'cn09.example.net') { | |
225 if ($type == A) { | |
226 select undef, undef, undef, 1.1; | |
227 } | |
228 push @rdata, pack("n3N nCa1n", 0xc00c, CNAME, IN, $ttl, | |
229 4, 1, "b", 0xc011); | |
230 | |
231 } elsif ($name eq 'cn052.example.net') { | |
232 if ($type == A) { | |
233 push @rdata, rd_addr($ttl, '127.0.0.1'); | |
234 } | |
235 | |
236 } elsif ($name =~ /cn0\d+.example.net/) { | |
237 my ($id) = $name =~ /cn(\d+)/; | |
238 $id++; | |
239 push @rdata, pack("n3N nCa5n", 0xc00c, CNAME, IN, $ttl, | |
240 8, 5, "cn$id", 0xc012); | |
241 } | |
242 | |
243 $len = @name; | |
244 pack("n6 (C/a*)$len x n2", $id, $hdr | $rcode, 1, scalar @rdata, | |
245 0, 0, @name, $type, $class) . join('', @rdata); | |
246 } | |
247 | |
248 sub rd_addr { | |
249 my ($ttl, $addr) = @_; | |
250 | |
251 my $code = 'split(/\./, $addr)'; | |
252 | |
253 return pack 'n3N', 0xc00c, A, IN, $ttl if $addr eq ''; | |
254 | |
255 pack 'n3N nC4', 0xc00c, A, IN, $ttl, eval "scalar $code", eval($code); | |
256 } | |
257 | |
258 sub dns_daemon { | |
259 my ($port, $t) = @_; | |
260 | |
261 my ($data, $recv_data); | |
262 my $socket = IO::Socket::INET->new( | |
263 LocalAddr => '127.0.0.1', | |
264 LocalPort => $port, | |
265 Proto => 'udp', | |
266 ) | |
267 or die "Can't create listening socket: $!\n"; | |
268 | |
269 # signal we are ready | |
270 | |
271 open my $fh, '>', $t->testdir() . '/' . $port; | |
272 close $fh; | |
273 | |
274 while (1) { | |
275 $socket->recv($recv_data, 65536); | |
276 $data = reply_handler($recv_data, $port); | |
277 $socket->send($data); | |
278 } | |
279 } | |
280 | |
281 ############################################################################### |