Mercurial > hg > nginx-tests
annotate spdy.t @ 392:c28ecaef065f
Tests: try_run() introduced.
This method replaces multiple ad-hoc solutions to grecefully skip tests if
nginx startup fails, e.g., due to no support for a particular directive.
author | Maxim Dounin <mdounin@mdounin.ru> |
---|---|
date | Fri, 18 Apr 2014 18:36:26 +0400 |
parents | f42de3a9fd74 |
children | 847ea345becb |
rev | line source |
---|---|
374 | 1 #!/usr/bin/perl |
2 | |
3 # (C) Sergey Kandaurov | |
4 # (C) Nginx, Inc. | |
5 | |
6 # Tests for SPDY protocol version 3.1. | |
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 eval { | |
28 require Compress::Raw::Zlib; | |
29 Compress::Raw::Zlib->Z_OK; | |
30 Compress::Raw::Zlib->Z_SYNC_FLUSH; | |
31 Compress::Raw::Zlib->Z_NO_COMPRESSION; | |
32 Compress::Raw::Zlib->WANT_GZIP_OR_ZLIB; | |
33 }; | |
34 plan(skip_all => 'Compress::Raw::Zlib not installed') if $@; | |
35 plan(skip_all => 'win32') if $^O eq 'MSWin32'; | |
36 | |
37 my $t = Test::Nginx->new()->has(qw/http proxy cache limit_conn rewrite spdy/); | |
38 | |
39 plan(skip_all => 'no SPDY/3.1') unless $t->has_version('1.5.10'); | |
40 | |
41 $t->plan(72)->write_file_expand('nginx.conf', <<'EOF'); | |
42 | |
43 %%TEST_GLOBALS%% | |
44 | |
45 daemon off; | |
46 | |
47 events { | |
48 } | |
49 | |
50 http { | |
51 %%TEST_GLOBALS_HTTP%% | |
52 | |
53 proxy_cache_path %%TESTDIR%%/cache keys_zone=NAME:10m; | |
54 limit_conn_zone $binary_remote_addr zone=conn:1m; | |
55 | |
56 server { | |
57 listen 127.0.0.1:8080 spdy; | |
58 listen 127.0.0.1:8081; | |
59 server_name localhost; | |
60 | |
61 location /s { | |
62 add_header X-Header X-Foo; | |
63 return 200 'body'; | |
64 } | |
65 location /spdy { | |
66 return 200 $spdy; | |
67 } | |
68 location /prio { | |
69 return 200 $spdy_request_priority; | |
70 } | |
71 location /chunk_size { | |
72 spdy_chunk_size 1; | |
73 return 200 'body'; | |
74 } | |
75 location /redirect { | |
76 error_page 405 /s; | |
77 return 405; | |
78 } | |
79 location /proxy { | |
80 add_header X-Body "$request_body"; | |
81 proxy_pass http://127.0.0.1:8081/; | |
82 proxy_cache NAME; | |
83 proxy_cache_valid 1m; | |
84 } | |
85 location /t3.html { | |
86 limit_conn conn 1; | |
87 } | |
88 } | |
89 } | |
90 | |
91 EOF | |
92 | |
93 $t->run(); | |
94 | |
95 # file size is slightly beyond initial window size: 2**16 + 80 bytes | |
96 | |
97 $t->write_file('t1.html', | |
98 join('', map { sprintf "X%04dXXX", $_ } (1 .. 8202))); | |
99 | |
100 $t->write_file('t2.html', 'SEE-THIS'); | |
101 $t->write_file('t3.html', 'SEE-THIS'); | |
102 | |
103 my %cframe = ( | |
104 2 => \&syn_reply, | |
105 3 => \&rst_stream, | |
106 4 => \&settings, | |
107 6 => \&ping, | |
108 7 => \&goaway, | |
109 9 => \&window_update | |
110 ); | |
111 | |
112 ############################################################################### | |
113 | |
114 # PING | |
115 | |
116 my $sess = new_session(); | |
117 spdy_ping($sess, 0x12345678); | |
118 my $frames = spdy_read($sess); | |
119 | |
120 my ($frame) = grep { $_->{type} eq "PING" } @$frames; | |
121 ok($frame, 'PING frame'); | |
122 is($frame->{value}, 0x12345678, 'PING payload'); | |
123 | |
124 # GET | |
125 | |
126 $sess = new_session(); | |
127 my $sid1 = spdy_stream($sess, { path => '/s' }); | |
128 $frames = spdy_read($sess, all => [{ sid => $sid1, fin => 1 }]); | |
129 | |
130 ($frame) = grep { $_->{type} eq "SYN_REPLY" } @$frames; | |
131 ok($frame, 'SYN_REPLAY frame'); | |
132 is($frame->{sid}, $sid1, 'SYN_REPLAY stream'); | |
133 is($frame->{headers}->{':status'}, 200, 'SYN_REPLAY status'); | |
134 is($frame->{headers}->{'x-header'}, 'X-Foo', 'SYN_REPLAY header'); | |
135 | |
136 ($frame) = grep { $_->{type} eq "DATA" } @$frames; | |
137 ok($frame, 'DATA frame'); | |
138 is($frame->{length}, length 'body', 'DATA length'); | |
139 is($frame->{data}, 'body', 'DATA payload'); | |
140 | |
141 # GET in new SPDY stream in same session | |
142 | |
143 my $sid2 = spdy_stream($sess, { path => '/s' }); | |
144 $frames = spdy_read($sess, all => [{ sid => $sid2, fin => 1 }]); | |
145 | |
146 ($frame) = grep { $_->{type} eq "SYN_REPLY" } @$frames; | |
147 is($frame->{sid}, $sid2, 'SYN_REPLAY stream 2'); | |
148 is($frame->{headers}->{':status'}, 200, 'SYN_REPLAY status 2'); | |
149 is($frame->{headers}->{'x-header'}, 'X-Foo', 'SYN_REPLAY header 2'); | |
150 | |
151 ($frame) = grep { $_->{type} eq "DATA" } @$frames; | |
152 ok($frame, 'DATA frame 2'); | |
153 is($frame->{sid}, $sid2, 'SYN_REPLAY stream 2'); | |
154 is($frame->{length}, length 'body', 'DATA length 2'); | |
155 is($frame->{data}, 'body', 'DATA payload 2'); | |
156 | |
157 # HEAD | |
158 | |
159 $sess = new_session(); | |
160 $sid1 = spdy_stream($sess, { path => '/s', method => 'HEAD' }); | |
161 $frames = spdy_read($sess, all => [{ sid => $sid1, fin => 1 }]); | |
162 | |
163 ($frame) = grep { $_->{type} eq "SYN_REPLY" } @$frames; | |
164 is($frame->{sid}, $sid1, 'SYN_REPLAY stream HEAD'); | |
165 is($frame->{headers}->{':status'}, 200, 'SYN_REPLAY status HEAD'); | |
166 is($frame->{headers}->{'x-header'}, 'X-Foo', 'SYN_REPLAY header HEAD'); | |
167 | |
168 ($frame) = grep { $_->{type} eq "DATA" } @$frames; | |
169 is($frame, undef, 'HEAD no body'); | |
170 | |
171 # request header | |
172 | |
173 $sess = new_session(); | |
174 $sid1 = spdy_stream($sess, { path => '/t1.html', | |
175 headers => { "range" => "bytes=10-19" } | |
176 }); | |
177 $frames = spdy_read($sess, all => [{ sid => $sid1, fin => 1 }]); | |
178 | |
179 ($frame) = grep { $_->{type} eq "SYN_REPLY" } @$frames; | |
180 is($frame->{headers}->{':status'}, 206, 'SYN_REPLAY status range'); | |
181 | |
182 ($frame) = grep { $_->{type} eq "DATA" } @$frames; | |
183 is($frame->{length}, 10, 'DATA length range'); | |
184 is($frame->{data}, '002XXXX000', 'DATA payload range'); | |
185 | |
186 # $spdy | |
187 | |
188 $sess = new_session(); | |
189 $sid1 = spdy_stream($sess, { path => '/spdy' }); | |
190 $frames = spdy_read($sess, all => [{ sid => $sid1, fin => 1 }]); | |
191 | |
192 ($frame) = grep { $_->{type} eq "DATA" } @$frames; | |
193 is($frame->{data}, '3.1', 'spdy variable'); | |
194 | |
195 # spdy_chunk_size=1 | |
196 | |
197 $sess = new_session(); | |
198 $sid1 = spdy_stream($sess, { path => '/chunk_size' }); | |
199 $frames = spdy_read($sess, all => [{ sid => $sid1, fin => 1 }]); | |
200 | |
201 my @data = grep { $_->{type} eq "DATA" } @$frames; | |
202 is(@data, 4, 'chunk_size body chunks'); | |
203 is($data[0]->{data}, 'b', 'chunk_size body 1'); | |
204 is($data[1]->{data}, 'o', 'chunk_size body 2'); | |
205 is($data[2]->{data}, 'd', 'chunk_size body 3'); | |
206 is($data[3]->{data}, 'y', 'chunk_size body 4'); | |
207 | |
208 # redirect | |
209 | |
210 $sess = new_session(); | |
211 $sid1 = spdy_stream($sess, { path => '/redirect' }); | |
212 $frames = spdy_read($sess, all => [{ sid => $sid1, fin => 1 }]); | |
213 | |
214 ($frame) = grep { $_->{type} eq "SYN_REPLY" } @$frames; | |
215 is($frame->{headers}->{':status'}, 405, 'SYN_REPLAY status with redirect'); | |
216 | |
217 ($frame) = grep { $_->{type} eq "DATA" } @$frames; | |
218 ok($frame, 'DATA frame with redirect'); | |
219 is($frame->{data}, 'body', 'DATA payload with redirect'); | |
220 | |
221 # ensure that HEAD-like requests, i.e., without response body, do not lead to | |
222 # client connection close due to cache filling up with upstream response body | |
223 | |
224 TODO: { | |
225 local $TODO = 'premature client connection close'; | |
226 | |
227 $sess = new_session(); | |
228 $sid1 = spdy_stream($sess, { path => '/proxy/t2.html', method => 'HEAD' }); | |
229 $frames = spdy_read($sess, all => [{ sid => $sid1, fin => 1 }]); | |
230 | |
231 $sid2 = spdy_stream($sess, { path => '/' }); | |
232 $frames = spdy_read($sess, all => [{ sid => $sid1, fin => 1 }]); | |
233 ok(grep ({ $_->{type} eq "SYN_REPLY" } @$frames), 'proxy cache headers only'); | |
234 | |
235 } | |
236 | |
237 # simple proxy cache test | |
238 | |
239 $sess = new_session(); | |
240 $sid1 = spdy_stream($sess, { path => '/proxy/t2.html' }); | |
241 $frames = spdy_read($sess, all => [{ sid => $sid1, fin => 1 }]); | |
242 | |
243 ($frame) = grep { $_->{type} eq "SYN_REPLY" } @$frames; | |
244 is($frame->{headers}->{':status'}, '200 OK', 'proxy cache unconditional'); | |
245 | |
376
ab2d8abea393
Tests: skip proxy cache conditional test if we didn't get etag.
Sergey Kandaurov <pluknet@nginx.com>
parents:
374
diff
changeset
|
246 my $etag = $frame->{headers}->{'etag'}; |
ab2d8abea393
Tests: skip proxy cache conditional test if we didn't get etag.
Sergey Kandaurov <pluknet@nginx.com>
parents:
374
diff
changeset
|
247 |
ab2d8abea393
Tests: skip proxy cache conditional test if we didn't get etag.
Sergey Kandaurov <pluknet@nginx.com>
parents:
374
diff
changeset
|
248 SKIP: { |
ab2d8abea393
Tests: skip proxy cache conditional test if we didn't get etag.
Sergey Kandaurov <pluknet@nginx.com>
parents:
374
diff
changeset
|
249 skip 'no etag', 1 unless defined $etag; |
374 | 250 |
251 $sid2 = spdy_stream($sess, { path => '/proxy/t2.html', | |
376
ab2d8abea393
Tests: skip proxy cache conditional test if we didn't get etag.
Sergey Kandaurov <pluknet@nginx.com>
parents:
374
diff
changeset
|
252 headers => { "if-none-match" => $etag } |
374 | 253 }); |
254 $frames = spdy_read($sess, all => [{ sid => $sid2, fin => 1 }]); | |
255 | |
256 ($frame) = grep { $_->{type} eq "SYN_REPLY" } @$frames; | |
257 is($frame->{headers}->{':status'}, 304, 'proxy cache conditional'); | |
258 | |
376
ab2d8abea393
Tests: skip proxy cache conditional test if we didn't get etag.
Sergey Kandaurov <pluknet@nginx.com>
parents:
374
diff
changeset
|
259 } |
ab2d8abea393
Tests: skip proxy cache conditional test if we didn't get etag.
Sergey Kandaurov <pluknet@nginx.com>
parents:
374
diff
changeset
|
260 |
374 | 261 # request body (uses proxied response) |
262 | |
263 $sess = new_session(); | |
264 $sid1 = spdy_stream($sess, { path => '/proxy/t2.html', body => 'TEST' }); | |
265 $frames = spdy_read($sess, all => [{ sid => $sid1, fin => 1 }]); | |
266 | |
267 ($frame) = grep { $_->{type} eq "SYN_REPLY" } @$frames; | |
268 is($frame->{headers}->{'x-body'}, 'TEST', 'request body'); | |
269 | |
270 ($frame) = grep { $_->{type} eq "DATA" } @$frames; | |
271 is($frame->{length}, length 'SEE-THIS', 'proxied response length'); | |
272 is($frame->{data}, 'SEE-THIS', 'proxied response'); | |
273 | |
274 # WINDOW_UPDATE (client side) | |
275 | |
276 $sess = new_session(); | |
277 $sid1 = spdy_stream($sess, { path => '/t1.html' }); | |
278 $frames = spdy_read($sess, all => [{ sid => $sid1, fin => 0 }]); | |
279 | |
280 @data = grep { $_->{type} eq "DATA" } @$frames; | |
281 my $sum = eval join '+', map { $_->{length} } @data; | |
282 is($sum, 2**16, 'iws - stream blocked on initial window size'); | |
283 | |
284 spdy_ping($sess, 0xf00ff00f); | |
285 $frames = spdy_read($sess); | |
286 | |
287 ($frame) = grep { $_->{type} eq "PING" } @$frames; | |
288 ok($frame, 'iws - PING not blocked'); | |
289 | |
290 spdy_window($sess, 2**16, $sid1); | |
291 $frames = spdy_read($sess); | |
292 is(@$frames, 0, 'iws - updated stream window'); | |
293 | |
294 spdy_window($sess, 2**16); | |
295 $frames = spdy_read($sess, all => [{ sid => $sid1, fin => 1 }]); | |
296 | |
297 @data = grep { $_->{type} eq "DATA" } @$frames; | |
298 $sum = eval join '+', map { $_->{length} } @data; | |
299 is($sum, 80, 'iws - updated connection window'); | |
300 | |
301 # SETTINGS (initial window size, client side) | |
302 | |
303 $sess = new_session(); | |
304 spdy_settings($sess, 7 => 2**17); | |
305 spdy_window($sess, 2**17); | |
306 | |
307 $sid1 = spdy_stream($sess, { path => '/t1.html' }); | |
308 $frames = spdy_read($sess, all => [{ sid => $sid1, fin => 1 }]); | |
309 | |
310 @data = grep { $_->{type} eq "DATA" } @$frames; | |
311 $sum = eval join '+', map { $_->{length} } @data; | |
312 is($sum, 2**16 + 80, 'increased initial window size'); | |
313 | |
314 # probe for negative available space in a flow control window | |
315 | |
316 $sess = new_session(); | |
317 $sid1 = spdy_stream($sess, { path => '/t1.html' }); | |
318 spdy_read($sess, all => [{ sid => $sid1, fin => 0 }]); | |
319 | |
320 spdy_window($sess, 1); | |
321 spdy_settings($sess, 7 => 42); | |
322 spdy_window($sess, 1024, $sid1); | |
323 | |
324 $frames = spdy_read($sess); | |
325 is(@$frames, 0, 'negative window - no data'); | |
326 | |
327 spdy_window($sess, 2**16 - 42 - 1024, $sid1); | |
328 $frames = spdy_read($sess); | |
329 is(@$frames, 0, 'zero window - no data'); | |
330 | |
331 spdy_window($sess, 1, $sid1); | |
332 $frames = spdy_read($sess, all => [{ sid => $sid1, fin => 0 }]); | |
333 is(@$frames, 1, 'positive window - data'); | |
334 is(@$frames[0]->{length}, 1, 'positive window - data length'); | |
335 | |
336 # stream multiplexing | |
337 | |
338 $sess = new_session(); | |
339 $sid1 = spdy_stream($sess, { path => '/t1.html' }); | |
340 $frames = spdy_read($sess, all => [{ sid => $sid1, fin => 0 }]); | |
341 | |
342 @data = grep { $_->{type} eq "DATA" } @$frames; | |
343 $sum = eval join '+', map { $_->{length} } @data; | |
344 is($sum, 2**16, 'multiple - stream1 data'); | |
345 | |
346 $sid2 = spdy_stream($sess, { path => '/t1.html' }); | |
347 $frames = spdy_read($sess, all => [{ sid => $sid2, fin => 0 }]); | |
348 | |
349 @data = grep { $_->{type} eq "DATA" } @$frames; | |
350 is(@data, 0, 'multiple - stream2 no data'); | |
351 | |
352 spdy_window($sess, 2**17, $sid1); | |
353 spdy_window($sess, 2**17, $sid2); | |
354 spdy_window($sess, 2**17); | |
355 | |
356 $frames = spdy_read($sess, all => [ | |
357 { sid => $sid1, fin => 1 }, | |
358 { sid => $sid2, fin => 1 } | |
359 ]); | |
360 | |
361 @data = grep { $_->{type} eq "DATA" && $_->{sid} == $sid1 } @$frames; | |
362 $sum = eval join '+', map { $_->{length} } @data; | |
363 is($sum, 80, 'multiple - stream1 remain data'); | |
364 | |
365 @data = grep { $_->{type} eq "DATA" && $_->{sid} == $sid2 } @$frames; | |
366 $sum = eval join '+', map { $_->{length} } @data; | |
367 is($sum, 2**16 + 80, 'multiple - stream2 full data'); | |
368 | |
369 # request priority parsing in $spdy_request_priority | |
370 | |
371 $sess = new_session(); | |
372 $sid1 = spdy_stream($sess, { path => '/prio', prio => 0 }); | |
373 $frames = spdy_read($sess, all => [{ sid => $sid1, fin => 1 }]); | |
374 | |
375 ($frame) = grep { $_->{type} eq "DATA" } @$frames; | |
376 is($frame->{data}, 0, 'priority 0'); | |
377 | |
378 $sid1 = spdy_stream($sess, { path => '/prio', prio => 1 }); | |
379 $frames = spdy_read($sess, all => [{ sid => $sid1, fin => 1 }]); | |
380 | |
381 ($frame) = grep { $_->{type} eq "DATA" } @$frames; | |
382 is($frame->{data}, 1, 'priority 1'); | |
383 | |
384 $sid1 = spdy_stream($sess, { path => '/prio', prio => 7 }); | |
385 $frames = spdy_read($sess, all => [{ sid => $sid1, fin => 1 }]); | |
386 | |
387 ($frame) = grep { $_->{type} eq "DATA" } @$frames; | |
388 is($frame->{data}, 7, 'priority 7'); | |
389 | |
390 # stream muliplexing + priority | |
391 | |
392 TODO: { | |
393 local $TODO = 'reversed priority' unless $t->has_version('1.5.11'); | |
394 | |
395 $sess = new_session(); | |
396 $sid1 = spdy_stream($sess, { path => '/t1.html', prio => 7 }); | |
397 $sid2 = spdy_stream($sess, { path => '/t2.html', prio => 0 }); | |
398 spdy_read($sess); | |
399 | |
400 spdy_window($sess, 2**17, $sid1); | |
401 spdy_window($sess, 2**17, $sid2); | |
402 spdy_window($sess, 2**17); | |
403 | |
404 $frames = spdy_read($sess, all => [ | |
405 { sid => $sid1, fin => 1 }, | |
406 { sid => $sid2, fin => 1 } | |
407 ]); | |
408 | |
409 @data = grep { $_->{type} eq "DATA" } @$frames; | |
410 is(join (' ', map { $_->{sid} } @data), "$sid2 $sid1", 'multiple priority 1'); | |
411 | |
412 # and vice versa | |
413 | |
414 $sess = new_session(); | |
415 $sid1 = spdy_stream($sess, { path => '/t1.html', prio => 0 }); | |
416 $sid2 = spdy_stream($sess, { path => '/t2.html', prio => 7 }); | |
417 spdy_read($sess); | |
418 | |
419 spdy_window($sess, 2**17, $sid1); | |
420 spdy_window($sess, 2**17, $sid2); | |
421 spdy_window($sess, 2**17); | |
422 | |
423 $frames = spdy_read($sess, all => [ | |
424 { sid => $sid1, fin => 1 }, | |
425 { sid => $sid2, fin => 1 } | |
426 ]); | |
427 | |
428 @data = grep { $_->{type} eq "DATA" } @$frames; | |
429 is(join (' ', map { $_->{sid} } @data), "$sid1 $sid2", 'multiple priority 2'); | |
430 | |
431 } | |
432 | |
433 # limit_conn | |
434 | |
435 $sess = new_session(); | |
436 spdy_settings($sess, 7 => 1); | |
437 $sid1 = spdy_stream($sess, { path => '/t3.html' }); | |
438 $sid2 = spdy_stream($sess, { path => '/t3.html' }); | |
439 $frames = spdy_read($sess, all => [ | |
440 { sid => $sid1, fin => 0 }, | |
441 { sid => $sid2, fin => 0 } | |
442 ]); | |
443 | |
444 ($frame) = grep { $_->{type} eq "SYN_REPLY" && $_->{sid} == $sid1 } @$frames; | |
445 is($frame->{headers}->{':status'}, 200, 'conn_limit 1'); | |
446 | |
447 ($frame) = grep { $_->{type} eq "SYN_REPLY" && $_->{sid} == $sid2 } @$frames; | |
448 is($frame->{headers}->{':status'}, 503, 'conn_limit 2'); | |
449 | |
450 # limit_conn + client's RST_STREAM | |
451 | |
452 $sess = new_session(); | |
453 spdy_settings($sess, 7 => 1); | |
454 $sid1 = spdy_stream($sess, { path => '/t3.html' }); | |
455 spdy_rst($sess, $sid1, 5); | |
456 $sid2 = spdy_stream($sess, { path => '/t3.html' }); | |
457 $frames = spdy_read($sess, all => [ | |
458 { sid => $sid1, fin => 0 }, | |
459 { sid => $sid2, fin => 0 } | |
460 ]); | |
461 | |
462 ($frame) = grep { $_->{type} eq "SYN_REPLY" && $_->{sid} == $sid1 } @$frames; | |
463 is($frame->{headers}->{':status'}, 200, 'RST_STREAM 1'); | |
464 | |
465 ($frame) = grep { $_->{type} eq "SYN_REPLY" && $_->{sid} == $sid2 } @$frames; | |
466 is($frame->{headers}->{':status'}, 200, 'RST_STREAM 2'); | |
467 | |
468 # GOAWAY on SYN_STREAM with even StreamID | |
469 | |
470 TODO: { | |
471 local $TODO = 'not yet'; | |
472 | |
473 $sess = new_session(); | |
474 spdy_stream($sess, { path => '/s' }, 2); | |
475 $frames = spdy_read($sess); | |
476 | |
477 ($frame) = grep { $_->{type} eq "GOAWAY" } @$frames; | |
478 ok($frame, 'even stream - GOAWAY frame'); | |
479 is($frame->{code}, 1, 'even stream - error code'); | |
480 is($frame->{sid}, 0, 'even stream - last used stream'); | |
481 | |
482 } | |
483 | |
484 # GOAWAY on SYN_STREAM with backward StreamID | |
485 | |
486 TODO: { | |
487 local $TODO = 'not yet'; | |
488 | |
489 $sess = new_session(); | |
490 $sid1 = spdy_stream($sess, { path => '/s' }, 3); | |
491 spdy_read($sess); | |
492 | |
493 $sid2 = spdy_stream($sess, { path => '/s' }, 1); | |
494 $frames = spdy_read($sess); | |
495 | |
496 ($frame) = grep { $_->{type} eq "GOAWAY" } @$frames; | |
497 ok($frame, 'backward stream - GOAWAY frame'); | |
498 is($frame->{code}, 1, 'backward stream - error code'); | |
499 is($frame->{sid}, $sid1, 'backward stream - last used stream'); | |
500 | |
501 } | |
502 | |
503 # RST_STREAM on the second SYN_STREAM with same StreamID | |
504 | |
505 TODO: { | |
506 local $TODO = 'not yet'; | |
507 | |
508 $sess = new_session(); | |
509 $sid1 = spdy_stream($sess, { path => '/s' }, 3); | |
510 spdy_read($sess, all => [{ sid => $sid1, fin => 1 }]); | |
511 $sid2 = spdy_stream($sess, { path => '/s' }, 3); | |
512 $frames = spdy_read($sess); | |
513 | |
514 ($frame) = grep { $_->{type} eq "RST_STREAM" } @$frames; | |
515 ok($frame, 'dup stream - RST_STREAM frame'); | |
516 is($frame->{code}, 1, 'dup stream - error code'); | |
517 is($frame->{sid}, $sid1, 'dup stream - stream'); | |
518 | |
519 } | |
520 | |
521 # awkward protocol version | |
522 | |
523 TODO: { | |
524 local $TODO = 'not yet' unless $t->has_version('1.5.11'); | |
525 | |
526 $sess = new_session(); | |
527 $sid1 = spdy_stream($sess, { path => '/s', version => 'HTTP/1.10' }); | |
528 $frames = spdy_read($sess, all => [{ sid => $sid1, fin => 1 }]); | |
529 | |
530 ($frame) = grep { $_->{type} eq "SYN_REPLY" } @$frames; | |
531 is($frame->{headers}->{':status'}, 200, 'awkward version'); | |
532 | |
533 } | |
534 | |
535 # missing mandatory request header | |
536 | |
537 $sess = new_session(); | |
538 $sid1 = spdy_stream($sess, { path => '/s', version => '' }); | |
539 $frames = spdy_read($sess, all => [{ sid => $sid1, fin => 1 }]); | |
540 | |
541 ($frame) = grep { $_->{type} eq "SYN_REPLY" } @$frames; | |
542 is($frame->{headers}->{':status'}, 400, 'incomplete headers'); | |
543 | |
544 # GOAWAY before closing a connection by server | |
545 | |
546 $t->stop(); | |
547 | |
548 TODO: { | |
549 local $TODO = 'not yet'; | |
550 | |
551 $frames = spdy_read($sess); | |
552 | |
553 ($frame) = grep { $_->{type} eq "GOAWAY" } @$frames; | |
554 ok($frame, 'GOAWAY on connection close'); | |
555 | |
556 } | |
557 | |
558 ############################################################################### | |
559 | |
560 sub spdy_ping { | |
561 my ($sess, $payload) = @_; | |
562 | |
563 raw_write($sess->{socket}, pack("N3", 0x80030006, 0x4, $payload)); | |
564 } | |
565 | |
566 sub spdy_rst { | |
567 my ($sess, $sid, $error) = @_; | |
568 | |
569 raw_write($sess->{socket}, pack("N4", 0x80030003, 0x8, $sid, $error)); | |
570 } | |
571 | |
572 sub spdy_window { | |
573 my ($sess, $win, $stream) = @_; | |
574 | |
575 $stream = 0 unless defined $stream; | |
576 raw_write($sess->{socket}, pack("N4", 0x80030009, 8, $stream, $win)); | |
577 } | |
578 | |
579 sub spdy_settings { | |
580 my ($sess, %extra) = @_; | |
581 | |
582 my $cnt = keys %extra; | |
583 my $len = 4 + 8 * $cnt; | |
584 | |
585 my $buf = pack "N3", 0x80030004, $len, $cnt; | |
586 $buf .= join '', map { pack "N2", $_, $extra{$_} } keys %extra; | |
587 raw_write($sess->{socket}, $buf); | |
588 } | |
589 | |
590 sub spdy_read { | |
591 my ($sess, %extra) = @_; | |
377
ba95a443ff1f
Tests: improved spdy test robustness.
Sergey Kandaurov <pluknet@nginx.com>
parents:
376
diff
changeset
|
592 my ($skip, $length, $buf, @got); |
ba95a443ff1f
Tests: improved spdy test robustness.
Sergey Kandaurov <pluknet@nginx.com>
parents:
376
diff
changeset
|
593 my $tries = 0; |
ba95a443ff1f
Tests: improved spdy test robustness.
Sergey Kandaurov <pluknet@nginx.com>
parents:
376
diff
changeset
|
594 my $maxtried = 3; |
374 | 595 |
596 again: | |
377
ba95a443ff1f
Tests: improved spdy test robustness.
Sergey Kandaurov <pluknet@nginx.com>
parents:
376
diff
changeset
|
597 do { |
ba95a443ff1f
Tests: improved spdy test robustness.
Sergey Kandaurov <pluknet@nginx.com>
parents:
376
diff
changeset
|
598 $buf = raw_read($sess->{socket}); |
ba95a443ff1f
Tests: improved spdy test robustness.
Sergey Kandaurov <pluknet@nginx.com>
parents:
376
diff
changeset
|
599 } until (defined $buf || $tries++ >= $maxtried); |
374 | 600 |
379
f42de3a9fd74
Tests: avoid uninitialized warnings in spdy.t.
Maxim Dounin <mdounin@mdounin.ru>
parents:
377
diff
changeset
|
601 $buf = '' if !defined $buf; |
f42de3a9fd74
Tests: avoid uninitialized warnings in spdy.t.
Maxim Dounin <mdounin@mdounin.ru>
parents:
377
diff
changeset
|
602 |
374 | 603 for ($skip = 0; $skip < length $buf; $skip += $length + 8) { |
604 my $type = unpack("\@$skip B", $buf); | |
605 $length = hex unpack("\@$skip x5 H6", $buf); | |
606 if ($type == 0) { | |
607 push @got, dframe($skip, $buf); | |
608 test_fin($got[-1], $extra{all}); | |
609 next; | |
610 } | |
611 | |
612 my $ctype = unpack("\@$skip x2 n", $buf); | |
613 push @got, $cframe{$ctype}($sess, $skip, $buf); | |
614 test_fin($got[-1], $extra{all}); | |
615 } | |
377
ba95a443ff1f
Tests: improved spdy test robustness.
Sergey Kandaurov <pluknet@nginx.com>
parents:
376
diff
changeset
|
616 goto again if %extra && @{$extra{all}} && $tries < $maxtried; |
374 | 617 return \@got; |
618 } | |
619 | |
620 sub test_fin { | |
621 my ($frame, $all) = @_; | |
622 | |
623 @{$all} = grep { | |
624 !($_->{sid} == $frame->{sid} && $_->{fin} == $frame->{fin}) | |
625 } @{$all} if defined $frame->{fin}; | |
626 } | |
627 | |
628 sub dframe { | |
629 my ($skip, $buf) = @_; | |
630 my %frame; | |
631 | |
632 my $stream = unpack "\@$skip B32", $buf; $skip += 4; | |
633 substr($stream, 0, 1) = 0; | |
634 $stream = unpack("N", pack("B32", $stream)); | |
635 $frame{sid} = $stream; | |
636 | |
637 my $flags = unpack "\@$skip B8", $buf; $skip += 1; | |
638 $frame{fin} = substr($flags, 7, 1); | |
639 | |
640 my $length = hex (unpack "\@$skip H6", $buf); $skip += 3; | |
641 $frame{length} = $length; | |
642 | |
643 $frame{data} = substr($buf, $skip, $length); | |
644 $frame{type} = "DATA"; | |
645 return \%frame; | |
646 } | |
647 | |
648 sub spdy_stream { | |
649 my ($ctx, $uri, $stream) = @_; | |
650 my ($input, $output, $buf); | |
651 my ($d, $status); | |
652 | |
653 my $host = $uri->{host} || '127.0.0.1:8080'; | |
654 my $method = $uri->{method} || 'GET'; | |
655 my $headers = $uri->{headers} || {}; | |
656 my $body = $uri->{body}; | |
657 my $prio = defined $uri->{prio} ? $uri->{prio} : 4; | |
658 my $version = defined $uri->{version} ? $uri->{version} : "HTTP/1.1"; | |
659 | |
660 if ($stream) { | |
661 $ctx->{last_stream} = $stream; | |
662 } else { | |
663 $ctx->{last_stream} += 2; | |
664 } | |
665 | |
666 $buf = pack("NC", 0x80030001, not $body); | |
667 $buf .= pack("xxx"); # Length stub | |
668 $buf .= pack("N", $ctx->{last_stream}); # Stream-ID | |
669 $buf .= pack("N", 0); # Assoc. Stream-ID | |
670 $buf .= pack("n", $prio << 13); | |
671 | |
672 my $ent = 4 + keys %{$headers}; | |
673 $ent++ if $body; | |
674 $ent++ if $version; | |
675 | |
676 $input = pack("N", $ent); | |
677 $input .= hpack(":host", $host); | |
678 $input .= hpack(":method", $method); | |
679 $input .= hpack(":path", $uri->{path}); | |
680 $input .= hpack(":scheme", "http"); | |
681 if ($version) { | |
682 $input .= hpack(":version", $version); | |
683 } | |
684 if ($body) { | |
685 $input .= hpack("content-length", length $body); | |
686 } | |
687 $input .= join '', map { hpack($_, $headers->{$_}) } keys %{$headers}; | |
688 | |
689 $d = $ctx->{zlib}->{d}; | |
690 $status = $d->deflate($input => \my $start); | |
691 $status == Compress::Raw::Zlib->Z_OK or fail "deflate failed"; | |
692 $status = $d->flush(\my $tail => Compress::Raw::Zlib->Z_SYNC_FLUSH); | |
693 $status == Compress::Raw::Zlib->Z_OK or fail "flush failed"; | |
694 $output = $start . $tail; | |
695 | |
696 my $len = ''; | |
697 vec($len, 7, 8) = (length $output) + 10; | |
698 $buf |= $len; | |
699 $buf .= $output; | |
700 | |
701 if (defined $body) { | |
702 $buf .= pack "NCxn", $ctx->{last_stream}, 0x01, length $body; | |
703 $buf .= $body; | |
704 } | |
705 | |
706 raw_write($ctx->{socket}, $buf); | |
707 return $ctx->{last_stream}; | |
708 } | |
709 | |
710 sub syn_reply { | |
711 my ($ctx, $skip, $buf) = @_; | |
712 my ($i, $status); | |
713 my %payload; | |
714 | |
715 $skip += 4; | |
716 my $flags = unpack "\@$skip B8", $buf; $skip += 1; | |
717 $payload{fin} = substr($flags, 7, 1); | |
718 | |
719 my $length = hex unpack "\@$skip H6", $buf; $skip += 3; | |
720 $payload{length} = $length; | |
721 $payload{type} = 'SYN_REPLY'; | |
722 | |
723 my $stream = unpack "\@$skip B32", $buf; $skip += 4; | |
724 substr($stream, 0, 1) = 0; | |
725 $stream = unpack("N", pack("B32", $stream)); | |
726 $payload{sid} = $stream; | |
727 | |
728 my $input = substr($buf, $skip, $length - 4); | |
729 $i = $ctx->{zlib}->{i}; | |
730 | |
731 $status = $i->inflate($input => \my $out); | |
732 fail "Failed: $status" unless $status == Compress::Raw::Zlib->Z_OK; | |
733 $payload{headers} = hunpack($out); | |
734 return \%payload; | |
735 } | |
736 | |
737 sub rst_stream { | |
738 my ($ctx, $skip, $buf) = @_; | |
739 my %payload; | |
740 | |
741 $skip += 5; | |
742 $payload{length} = hex(unpack "\@$skip H6", $buf); $skip += 3; | |
743 $payload{type} = 'RST_STREAM'; | |
744 $payload{sid} = unpack "\@$skip N", $buf; $skip += 4; | |
745 $payload{code} = unpack "\@$skip N", $buf; | |
746 return \%payload; | |
747 } | |
748 | |
749 sub settings { | |
750 my ($ctx, $skip, $buf) = @_; | |
751 my %payload; | |
752 | |
753 $skip += 4; | |
754 $payload{flags} = unpack "\@$skip H", $buf; $skip += 1; | |
755 $payload{length} = hex(unpack "\@$skip H6", $buf); $skip += 3; | |
756 $payload{type} = 'SETTINGS'; | |
757 | |
758 my $nent = unpack "\@$skip N", $buf; $skip += 4; | |
759 for (1 .. $nent) { | |
760 my $flags = hex unpack "\@$skip H2", $buf; $skip += 1; | |
761 my $id = hex unpack "\@$skip H6", $buf; $skip += 3; | |
762 $payload{$id}{flags} = $flags; | |
763 $payload{$id}{value} = unpack "\@$skip N", $buf; $skip += 4; | |
764 } | |
765 return \%payload; | |
766 } | |
767 | |
768 sub ping { | |
769 my ($ctx, $skip, $buf) = @_; | |
770 my %payload; | |
771 | |
772 $skip += 5; | |
773 $payload{length} = hex(unpack "\@$skip H6", $buf); $skip += 3; | |
774 $payload{type} = 'PING'; | |
775 $payload{value} = unpack "\@$skip N", $buf; | |
776 return \%payload; | |
777 } | |
778 | |
779 sub goaway { | |
780 my ($ctx, $skip, $buf) = @_; | |
781 my %payload; | |
782 | |
783 $skip += 5; | |
784 $payload{length} = hex unpack "\@$skip H6", $buf; $skip += 3; | |
785 $payload{type} = 'GOAWAY'; | |
786 $payload{sid} = unpack "\@$skip N", $buf; $skip += 4; | |
787 $payload{code} = unpack "\@$skip N", $buf; | |
788 return \%payload; | |
789 } | |
790 | |
791 sub window_update { | |
792 my ($ctx, $skip, $buf) = @_; | |
793 my %payload; | |
794 | |
795 $skip += 5; | |
796 | |
797 $payload{length} = hex(unpack "\@$skip H6", $buf); $skip += 3; | |
798 $payload{type} = 'WINDOW_UPDATE'; | |
799 | |
800 my $stream = unpack "\@$skip B32", $buf; $skip += 4; | |
801 substr($stream, 0, 1) = 0; | |
802 $stream = unpack("N", pack("B32", $stream)); | |
803 $payload{sid} = $stream; | |
804 | |
805 my $value = unpack "\@$skip B32", $buf; | |
806 substr($value, 0, 1) = 0; | |
807 $payload{wdelta} = unpack("N", pack("B32", $value)); | |
808 return \%payload; | |
809 } | |
810 | |
811 sub hpack { | |
812 my ($name, $value) = @_; | |
813 | |
814 pack("N", length($name)) . $name . pack("N", length($value)) . $value; | |
815 } | |
816 | |
817 sub hunpack { | |
818 my ($data) = @_; | |
819 my %headers; | |
820 my $skip = 0; | |
821 | |
822 my $nent = unpack "\@$skip N", $data; $skip += 4; | |
823 for (1 .. $nent) { | |
824 my $len = unpack("\@$skip N", $data); $skip += 4; | |
825 my $name = unpack("\@$skip A$len", $data); $skip += $len; | |
826 | |
827 $len = unpack("\@$skip N", $data); $skip += 4; | |
828 my $value = unpack("\@$skip A$len", $data); $skip += $len; | |
829 | |
830 $headers{$name} = $value; | |
831 } | |
832 return \%headers; | |
833 } | |
834 | |
835 sub raw_read { | |
836 my ($s) = @_; | |
837 my ($got, $buf); | |
838 | |
839 $s->blocking(0); | |
840 while (IO::Select->new($s)->can_read(0.4)) { | |
841 my $n = $s->sysread($buf, 1024); | |
842 last unless $n; | |
843 $got .= $buf; | |
844 }; | |
845 log_in($got); | |
846 return $got; | |
847 } | |
848 | |
849 sub raw_write { | |
850 my ($s, $message) = @_; | |
851 | |
852 local $SIG{PIPE} = 'IGNORE'; | |
853 | |
854 $s->blocking(0); | |
855 while (IO::Select->new($s)->can_write(0.4)) { | |
856 log_out($message); | |
857 my $n = $s->syswrite($message); | |
858 last unless $n; | |
859 $message = substr($message, $n); | |
860 last unless length $message; | |
861 } | |
862 } | |
863 | |
864 sub new_session { | |
865 my ($d, $i, $status); | |
866 | |
867 ($d, $status) = Compress::Raw::Zlib::Deflate->new( | |
868 -WindowBits => 12, | |
869 -Dictionary => dictionary(), | |
870 -Level => Compress::Raw::Zlib->Z_NO_COMPRESSION | |
871 ); | |
872 fail "Zlib failure: $status" unless $d; | |
873 | |
874 ($i, $status) = Compress::Raw::Zlib::Inflate->new( | |
875 -WindowBits => Compress::Raw::Zlib->WANT_GZIP_OR_ZLIB, | |
876 -Dictionary => dictionary() | |
877 ); | |
878 fail "Zlib failure: $status" unless $i; | |
879 | |
880 return { zlib => { i => $i, d => $d }, | |
881 socket => new_socket(), last_stream => -1 }; | |
882 } | |
883 | |
884 sub new_socket { | |
885 my $s; | |
886 | |
887 eval { | |
888 local $SIG{ALRM} = sub { die "timeout\n" }; | |
889 local $SIG{PIPE} = sub { die "sigpipe\n" }; | |
890 alarm(2); | |
891 $s = IO::Socket::INET->new( | |
892 Proto => 'tcp', | |
893 PeerAddr => '127.0.0.1:8080', | |
894 ); | |
895 alarm(0); | |
896 }; | |
897 alarm(0); | |
898 | |
899 if ($@) { | |
900 log_in("died: $@"); | |
901 return undef; | |
902 } | |
903 | |
904 return $s; | |
905 } | |
906 | |
907 sub dictionary { | |
908 join('', (map pack('N/a*', $_), qw( | |
909 options | |
910 head | |
911 post | |
912 put | |
913 delete | |
914 trace | |
915 accept | |
916 accept-charset | |
917 accept-encoding | |
918 accept-language | |
919 accept-ranges | |
920 age | |
921 allow | |
922 authorization | |
923 cache-control | |
924 connection | |
925 content-base | |
926 content-encoding | |
927 content-language | |
928 content-length | |
929 content-location | |
930 content-md5 | |
931 content-range | |
932 content-type | |
933 date | |
934 etag | |
935 expect | |
936 expires | |
937 from | |
938 host | |
939 if-match | |
940 if-modified-since | |
941 if-none-match | |
942 if-range | |
943 if-unmodified-since | |
944 last-modified | |
945 location | |
946 max-forwards | |
947 pragma | |
948 proxy-authenticate | |
949 proxy-authorization | |
950 range | |
951 referer | |
952 retry-after | |
953 server | |
954 te | |
955 trailer | |
956 transfer-encoding | |
957 upgrade | |
958 user-agent | |
959 vary | |
960 via | |
961 warning | |
962 www-authenticate | |
963 method | |
964 get | |
965 status), "200 OK", | |
966 qw(version HTTP/1.1 url public set-cookie keep-alive origin)), | |
967 "100101201202205206300302303304305306307402405406407408409410", | |
968 "411412413414415416417502504505", | |
969 "203 Non-Authoritative Information", | |
970 "204 No Content", | |
971 "301 Moved Permanently", | |
972 "400 Bad Request", | |
973 "401 Unauthorized", | |
974 "403 Forbidden", | |
975 "404 Not Found", | |
976 "500 Internal Server Error", | |
977 "501 Not Implemented", | |
978 "503 Service Unavailable", | |
979 "Jan Feb Mar Apr May Jun Jul Aug Sept Oct Nov Dec", | |
980 " 00:00:00", | |
981 " Mon, Tue, Wed, Thu, Fri, Sat, Sun, GMT", | |
982 "chunked,text/html,image/png,image/jpg,image/gif,", | |
983 "application/xml,application/xhtml+xml,text/plain,", | |
984 "text/javascript,public", "privatemax-age=gzip,deflate,", | |
985 "sdchcharset=utf-8charset=iso-8859-1,utf-,*,enq=0." | |
986 ); | |
987 } | |
988 | |
989 ############################################################################### |