comparison stream_access.t @ 816:77359b849cd5

Tests: stream package.
author Andrey Zelenkov <zelenkov@nginx.com>
date Mon, 20 Jul 2015 15:06:09 +0300
parents 22b06c04b37f
children f4189a38c3a4
comparison
equal deleted inserted replaced
815:9f5f604a840e 816:77359b849cd5
10 use warnings; 10 use warnings;
11 use strict; 11 use strict;
12 12
13 use Test::More; 13 use Test::More;
14 14
15 use IO::Select;
16
17 BEGIN { use FindBin; chdir($FindBin::Bin); } 15 BEGIN { use FindBin; chdir($FindBin::Bin); }
18 16
19 use lib 'lib'; 17 use lib 'lib';
20 use Test::Nginx; 18 use Test::Nginx;
19 use Test::Nginx::Stream qw/ stream /;
21 20
22 ############################################################################### 21 ###############################################################################
23 22
24 select STDERR; $| = 1; 23 select STDERR; $| = 1;
25 select STDOUT; $| = 1; 24 select STDOUT; $| = 1;
157 156
158 my $str = 'SEE-THIS'; 157 my $str = 'SEE-THIS';
159 158
160 # allow all 159 # allow all
161 160
162 is(stream_get($str, '127.0.0.1:8081'), $str, 'inet allow all'); 161 is(stream('127.0.0.1:8081')->io($str), $str, 'inet allow all');
163 is(stream_get($str, '127.0.0.1:8082'), $str, 'inet6 allow all'); 162 is(stream('127.0.0.1:8082')->io($str), $str, 'inet6 allow all');
164 is(stream_get($str, '127.0.0.1:8083'), $str, 'unix allow all'); 163 is(stream('127.0.0.1:8083')->io($str), $str, 'unix allow all');
165 164
166 # deny all 165 # deny all
167 166
168 is(stream_get($str, '127.0.0.1:8084'), '', 'inet deny all'); 167 is(stream('127.0.0.1:8084')->io($str), '', 'inet deny all');
169 is(stream_get($str, '127.0.0.1:8085'), '', 'inet6 deny all'); 168 is(stream('127.0.0.1:8085')->io($str), '', 'inet6 deny all');
170 is(stream_get($str, '127.0.0.1:8086'), '', 'unix deny all'); 169 is(stream('127.0.0.1:8086')->io($str), '', 'unix deny all');
171 170
172 # allow unix 171 # allow unix
173 172
174 is(stream_get($str, '127.0.0.1:8087'), $str, 'inet allow unix'); 173 is(stream('127.0.0.1:8087')->io($str), $str, 'inet allow unix');
175 is(stream_get($str, '127.0.0.1:8088'), $str, 'inet6 allow unix'); 174 is(stream('127.0.0.1:8088')->io($str), $str, 'inet6 allow unix');
176 is(stream_get($str, '127.0.0.1:8089'), $str, 'unix allow unix'); 175 is(stream('127.0.0.1:8089')->io($str), $str, 'unix allow unix');
177 176
178 # deny inet 177 # deny inet
179 178
180 is(stream_get($str, '127.0.0.1:8090'), '', 'inet deny inet'); 179 is(stream('127.0.0.1:8090')->io($str), '', 'inet deny inet');
181 is(stream_get($str, '127.0.0.1:8091'), $str, 'inet6 deny inet'); 180 is(stream('127.0.0.1:8091')->io($str), $str, 'inet6 deny inet');
182 is(stream_get($str, '127.0.0.1:8092'), $str, 'unix deny inet'); 181 is(stream('127.0.0.1:8092')->io($str), $str, 'unix deny inet');
183 182
184 # deny inet6 183 # deny inet6
185 184
186 is(stream_get($str, '127.0.0.1:8093'), $str, 'inet deny inet6'); 185 is(stream('127.0.0.1:8093')->io($str), $str, 'inet deny inet6');
187 is(stream_get($str, '127.0.0.1:8094'), '', 'inet6 deny inet6'); 186 is(stream('127.0.0.1:8094')->io($str), '', 'inet6 deny inet6');
188 is(stream_get($str, '127.0.0.1:8095'), $str, 'unix deny inet6'); 187 is(stream('127.0.0.1:8095')->io($str), $str, 'unix deny inet6');
189 188
190 # deny unix 189 # deny unix
191 190
192 is(stream_get($str, '127.0.0.1:8096'), $str, 'inet deny unix'); 191 is(stream('127.0.0.1:8096')->io($str), $str, 'inet deny unix');
193 is(stream_get($str, '127.0.0.1:8097'), $str, 'inet6 deny unix'); 192 is(stream('127.0.0.1:8097')->io($str), $str, 'inet6 deny unix');
194 is(stream_get($str, '127.0.0.1:8098'), '', 'unix deny unix'); 193 is(stream('127.0.0.1:8098')->io($str), '', 'unix deny unix');
195
196 ###############################################################################
197
198 sub stream_get {
199 my ($data, $peer) = @_;
200
201 my $s = stream_connect($peer);
202 stream_write($s, $data);
203
204 $data = '';
205 while (my $buf = stream_read($s)) {
206 $data .= $buf;
207 }
208
209 return $data;
210 }
211
212 sub stream_connect {
213 my $peer = shift;
214 my $s = IO::Socket::INET->new(
215 Proto => 'tcp',
216 PeerAddr => $peer || '127.0.0.1:8080'
217 )
218 or die "Can't connect to nginx: $!\n";
219
220 return $s;
221 }
222
223 sub stream_write {
224 my ($s, $message) = @_;
225
226 local $SIG{PIPE} = 'IGNORE';
227
228 $s->blocking(0);
229 while (IO::Select->new($s)->can_write(1.5)) {
230 my $n = $s->syswrite($message);
231 last unless $n;
232 $message = substr($message, $n);
233 last unless length $message;
234 }
235
236 if (length $message) {
237 $s->close();
238 }
239 }
240
241 sub stream_read {
242 my ($s) = @_;
243 my ($buf);
244
245 $s->blocking(0);
246 if (IO::Select->new($s)->can_read(5)) {
247 $s->sysread($buf, 1024);
248 };
249
250 log_in($buf);
251 return $buf;
252 }
253 194
254 ############################################################################### 195 ###############################################################################
255 196
256 sub stream_daemon { 197 sub stream_daemon {
257 my $server = IO::Socket::INET->new( 198 my $server = IO::Socket::INET->new(