Mercurial > hg > nginx-tests
comparison stream_access.t @ 631:fca0079e0aab
Tests: stream access tests.
author | Andrey Zelenkov <zelenkov@nginx.com> |
---|---|
date | Fri, 10 Jul 2015 18:06:37 +0300 |
parents | |
children | 1925b27f24b6 |
comparison
equal
deleted
inserted
replaced
630:e6132dd8e341 | 631:fca0079e0aab |
---|---|
1 #!/usr/bin/perl | |
2 | |
3 # (C) Andrey Zelenkov | |
4 # (C) Nginx, Inc. | |
5 | |
6 # Tests for stream access module. | |
7 | |
8 ############################################################################### | |
9 | |
10 use warnings; | |
11 use strict; | |
12 | |
13 use Test::More; | |
14 | |
15 use IO::Select; | |
16 | |
17 BEGIN { use FindBin; chdir($FindBin::Bin); } | |
18 | |
19 use lib 'lib'; | |
20 use Test::Nginx; | |
21 | |
22 ############################################################################### | |
23 | |
24 select STDERR; $| = 1; | |
25 select STDOUT; $| = 1; | |
26 | |
27 my $t = Test::Nginx->new()->has(qw/stream stream_access ipv6/); | |
28 | |
29 $t->write_file_expand('nginx.conf', <<'EOF'); | |
30 | |
31 %%TEST_GLOBALS%% | |
32 | |
33 daemon off; | |
34 | |
35 events { | |
36 } | |
37 | |
38 stream { | |
39 server { | |
40 listen 127.0.0.1:8082; | |
41 proxy_pass [::1]:8080; | |
42 } | |
43 | |
44 server { | |
45 listen 127.0.0.1:8083; | |
46 proxy_pass unix:%%TESTDIR%%/unix.sock.0; | |
47 } | |
48 | |
49 server { | |
50 listen 127.0.0.1:8085; | |
51 proxy_pass [::1]:8081; | |
52 } | |
53 | |
54 server { | |
55 listen 127.0.0.1:8086; | |
56 proxy_pass unix:%%TESTDIR%%/unix.sock.1; | |
57 } | |
58 | |
59 server { | |
60 listen 127.0.0.1:8088; | |
61 proxy_pass [::1]:8082; | |
62 } | |
63 | |
64 server { | |
65 listen 127.0.0.1:8089; | |
66 proxy_pass unix:%%TESTDIR%%/unix.sock.2; | |
67 } | |
68 | |
69 server { | |
70 listen 127.0.0.1:8091; | |
71 proxy_pass [::1]:8083; | |
72 } | |
73 | |
74 server { | |
75 listen 127.0.0.1:8092; | |
76 proxy_pass unix:%%TESTDIR%%/unix.sock.3; | |
77 } | |
78 | |
79 server { | |
80 listen 127.0.0.1:8094; | |
81 proxy_pass [::1]:8084; | |
82 } | |
83 | |
84 server { | |
85 listen 127.0.0.1:8095; | |
86 proxy_pass unix:%%TESTDIR%%/unix.sock.4; | |
87 } | |
88 | |
89 server { | |
90 listen 127.0.0.1:8097; | |
91 proxy_pass [::1]:8085; | |
92 } | |
93 | |
94 server { | |
95 listen 127.0.0.1:8098; | |
96 proxy_pass unix:%%TESTDIR%%/unix.sock.5; | |
97 } | |
98 | |
99 server { | |
100 listen 127.0.0.1:8081; | |
101 listen [::1]:8080; | |
102 listen unix:%%TESTDIR%%/unix.sock.0; | |
103 proxy_pass 127.0.0.1:8080; | |
104 allow all; | |
105 } | |
106 | |
107 server { | |
108 listen 127.0.0.1:8084; | |
109 listen [::1]:8081; | |
110 listen unix:%%TESTDIR%%/unix.sock.1; | |
111 proxy_pass 127.0.0.1:8080; | |
112 deny all; | |
113 } | |
114 | |
115 server { | |
116 listen 127.0.0.1:8087; | |
117 listen [::1]:8082; | |
118 listen unix:%%TESTDIR%%/unix.sock.2; | |
119 proxy_pass 127.0.0.1:8080; | |
120 allow unix:; | |
121 } | |
122 | |
123 server { | |
124 listen 127.0.0.1:8090; | |
125 listen [::1]:8083; | |
126 listen unix:%%TESTDIR%%/unix.sock.3; | |
127 proxy_pass 127.0.0.1:8080; | |
128 deny 127.0.0.1; | |
129 } | |
130 | |
131 server { | |
132 listen 127.0.0.1:8093; | |
133 listen [::1]:8084; | |
134 listen unix:%%TESTDIR%%/unix.sock.4; | |
135 proxy_pass 127.0.0.1:8080; | |
136 deny ::1; | |
137 } | |
138 | |
139 server { | |
140 listen 127.0.0.1:8096; | |
141 listen [::1]:8085; | |
142 listen unix:%%TESTDIR%%/unix.sock.5; | |
143 proxy_pass 127.0.0.1:8080; | |
144 deny unix:; | |
145 } | |
146 } | |
147 | |
148 EOF | |
149 | |
150 $t->run_daemon(\&stream_daemon); | |
151 $t->try_run('no inet6 and/or unix support')->plan(18); | |
152 $t->waitforsocket('127.0.0.1:8080'); | |
153 | |
154 ############################################################################### | |
155 | |
156 my $str = 'SEE-THIS'; | |
157 | |
158 # allow all | |
159 | |
160 is(stream_get($str, '127.0.0.1:8081'), $str, 'inet allow all'); | |
161 is(stream_get($str, '127.0.0.1:8082'), $str, 'inet6 allow all'); | |
162 is(stream_get($str, '127.0.0.1:8083'), $str, 'unix allow all'); | |
163 | |
164 # deny all | |
165 | |
166 is(stream_get($str, '127.0.0.1:8084'), '', 'inet deny all'); | |
167 is(stream_get($str, '127.0.0.1:8085'), '', 'inet6 deny all'); | |
168 is(stream_get($str, '127.0.0.1:8086'), '', 'unix deny all'); | |
169 | |
170 # allow unix | |
171 | |
172 is(stream_get($str, '127.0.0.1:8087'), $str, 'inet allow unix'); | |
173 is(stream_get($str, '127.0.0.1:8088'), $str, 'inet6 allow unix'); | |
174 is(stream_get($str, '127.0.0.1:8089'), $str, 'unix allow unix'); | |
175 | |
176 # deny inet | |
177 | |
178 is(stream_get($str, '127.0.0.1:8090'), '', 'inet deny inet'); | |
179 is(stream_get($str, '127.0.0.1:8091'), $str, 'inet6 deny inet'); | |
180 is(stream_get($str, '127.0.0.1:8092'), $str, 'unix deny inet'); | |
181 | |
182 # deny inet6 | |
183 | |
184 is(stream_get($str, '127.0.0.1:8093'), $str, 'inet deny inet6'); | |
185 is(stream_get($str, '127.0.0.1:8094'), '', 'inet6 deny inet6'); | |
186 is(stream_get($str, '127.0.0.1:8095'), $str, 'unix deny inet6'); | |
187 | |
188 # deny unix | |
189 | |
190 is(stream_get($str, '127.0.0.1:8096'), $str, 'inet deny unix'); | |
191 is(stream_get($str, '127.0.0.1:8097'), $str, 'inet6 deny unix'); | |
192 is(stream_get($str, '127.0.0.1:8098'), '', 'unix deny unix'); | |
193 | |
194 ############################################################################### | |
195 | |
196 sub stream_get { | |
197 my ($data, $peer) = @_; | |
198 | |
199 my $s = stream_connect($peer); | |
200 stream_write($s, $data); | |
201 | |
202 $data = ''; | |
203 while (my $buf = stream_read($s)) { | |
204 $data .= $buf; | |
205 } | |
206 | |
207 return $data; | |
208 } | |
209 | |
210 sub stream_connect { | |
211 my $peer = shift; | |
212 my $s = IO::Socket::INET->new( | |
213 Proto => 'tcp', | |
214 PeerAddr => $peer || '127.0.0.1:8080' | |
215 ) | |
216 or die "Can't connect to nginx: $!\n"; | |
217 | |
218 return $s; | |
219 } | |
220 | |
221 sub stream_write { | |
222 my ($s, $message) = @_; | |
223 | |
224 local $SIG{PIPE} = 'IGNORE'; | |
225 | |
226 $s->blocking(0); | |
227 while (IO::Select->new($s)->can_write(1.5)) { | |
228 my $n = $s->syswrite($message); | |
229 last unless $n; | |
230 $message = substr($message, $n); | |
231 last unless length $message; | |
232 } | |
233 | |
234 if (length $message) { | |
235 $s->close(); | |
236 } | |
237 } | |
238 | |
239 sub stream_read { | |
240 my ($s) = @_; | |
241 my ($buf); | |
242 | |
243 $s->blocking(0); | |
244 if (IO::Select->new($s)->can_read(3)) { | |
245 $s->sysread($buf, 1024); | |
246 }; | |
247 | |
248 log_in($buf); | |
249 return $buf; | |
250 } | |
251 | |
252 ############################################################################### | |
253 | |
254 sub stream_daemon { | |
255 my $server = IO::Socket::INET->new( | |
256 Proto => 'tcp', | |
257 LocalAddr => '127.0.0.1:8080', | |
258 Listen => 5, | |
259 Reuse => 1 | |
260 ) | |
261 or die "Can't create listening socket: $!\n"; | |
262 | |
263 local $SIG{PIPE} = 'IGNORE'; | |
264 | |
265 while (my $client = $server->accept()) { | |
266 $client->autoflush(1); | |
267 | |
268 log2c("(new connection $client)"); | |
269 | |
270 $client->sysread(my $buffer, 65536) or next; | |
271 | |
272 log2i("$client $buffer"); | |
273 | |
274 log2o("$client $buffer"); | |
275 | |
276 $client->syswrite($buffer); | |
277 | |
278 close $client; | |
279 } | |
280 } | |
281 | |
282 sub log2i { Test::Nginx::log_core('|| <<', @_); } | |
283 sub log2o { Test::Nginx::log_core('|| >>', @_); } | |
284 sub log2c { Test::Nginx::log_core('||', @_); } | |
285 | |
286 ############################################################################### |