Debian Codegraph @

testing/web-platform/tests/tools/third_party/h2/test/test_closed_streams.py

raw
1 # -*- coding: utf-8 -*-
2 """
3 test_closed_streams
4 ~~~~~~~~~~~~~~~~~~~
5
6 Tests that we handle closed streams correctly.
7 """
8 import pytest
9
10 import h2.config
11 import h2.connection
12 import h2.errors
13 import h2.events
14 import h2.exceptions
15
16
17 class TestClosedStreams(object):
18 example_request_headers = [
19 (':authority', 'example.com'),
20 (':path', '/'),
21 (':scheme', 'https'),
22 (':method', 'GET'),
23 ]
24 example_response_headers = [
25 (':status', '200'),
26 ('server', 'fake-serv/0.1.0')
27 ]
28 server_config = h2.config.H2Configuration(client_side=False)
29
30 def test_can_receive_multiple_rst_stream_frames(self, frame_factory):
31 """
32 Multiple RST_STREAM frames can be received, either at once or well
33 after one another. Only the first fires an event.
34 """
35 c = h2.connection.H2Connection()
36 c.initiate_connection()
37 c.send_headers(1, self.example_request_headers, end_stream=True)
38
39 f = frame_factory.build_rst_stream_frame(stream_id=1)
40 events = c.receive_data(f.serialize() * 3)
41
42 # Force an iteration over all the streams to remove them.
43 c.open_outbound_streams
44
45 # Receive more data.
46 events += c.receive_data(f.serialize() * 3)
47
48 assert len(events) == 1
49 event = events[0]
50
51 assert isinstance(event, h2.events.StreamReset)
52
53 def test_receiving_low_stream_id_causes_goaway(self, frame_factory):
54 """
55 The remote peer creating a stream with a lower ID than one we've seen
56 causes a GOAWAY frame.
57 """
58 c = h2.connection.H2Connection(config=self.server_config)
59 c.receive_data(frame_factory.preamble())
60 c.initiate_connection()
61
62 f = frame_factory.build_headers_frame(
63 self.example_request_headers,
64 stream_id=3,
65 )
66 c.receive_data(f.serialize())
67 c.clear_outbound_data_buffer()
68
69 f = frame_factory.build_headers_frame(
70 self.example_request_headers,
71 stream_id=1,
72 )
73
74 with pytest.raises(h2.exceptions.StreamIDTooLowError) as e:
75 c.receive_data(f.serialize())
76
77 assert e.value.stream_id == 1
78 assert e.value.max_stream_id == 3
79
80 f = frame_factory.build_goaway_frame(
81 last_stream_id=3,
82 error_code=h2.errors.ErrorCodes.PROTOCOL_ERROR,
83 )
84 assert c.data_to_send() == f.serialize()
85
86 def test_closed_stream_not_present_in_streams_dict(self, frame_factory):
87 """
88 When streams have been closed, they get removed from the streams
89 dictionary the next time we count the open streams.
90 """
91 c = h2.connection.H2Connection(config=self.server_config)
92 c.receive_data(frame_factory.preamble())
93 c.initiate_connection()
94
95 f = frame_factory.build_headers_frame(self.example_request_headers)
96 c.receive_data(f.serialize())
97 c.push_stream(1, 2, self.example_request_headers)
98 c.reset_stream(1)
99 c.clear_outbound_data_buffer()
100
101 f = frame_factory.build_rst_stream_frame(stream_id=2)
102 c.receive_data(f.serialize())
103
104 # Force a count of the streams.
105 assert not c.open_outbound_streams
106
107 # The streams dictionary should be empty.
108 assert not c.streams
109
110 def test_receive_rst_stream_on_closed_stream(self, frame_factory):
111 """
112 RST_STREAM frame should be ignored if stream is in a closed state.
113 See RFC 7540 Section 5.1 (closed state)
114 """
115 c = h2.connection.H2Connection()
116 c.initiate_connection()
117
118 # Client sends request
119 c.send_headers(1, self.example_request_headers)
120
121 # Some time passes and client sends DATA frame and closes stream,
122 # so it is in a half-closed state
123 c.send_data(1, b'some data', end_stream=True)
124
125 # Server received HEADERS frame but DATA frame is still on the way.
126 # Stream is in open state on the server-side. In this state server is
127 # allowed to end stream and reset it - this trick helps immediately
128 # close stream on the server-side.
129 headers_frame = frame_factory.build_headers_frame(
130 [(':status', '200')],
131 flags=['END_STREAM'],
132 stream_id=1,
133 )
134 events = c.receive_data(headers_frame.serialize())
135 assert len(events) == 2
136 response_received, stream_ended = events
137 assert isinstance(response_received, h2.events.ResponseReceived)
138 assert isinstance(stream_ended, h2.events.StreamEnded)
139
140 rst_stream_frame = frame_factory.build_rst_stream_frame(stream_id=1)
141 events = c.receive_data(rst_stream_frame.serialize())
142 assert not events
143
144 def test_receive_window_update_on_closed_stream(self, frame_factory):
145 """
146 WINDOW_UPDATE frame should be ignored if stream is in a closed state.
147 See RFC 7540 Section 5.1 (closed state)
148 """
149 c = h2.connection.H2Connection()
150 c.initiate_connection()
151
152 # Client sends request
153 c.send_headers(1, self.example_request_headers)
154
155 # Some time passes and client sends DATA frame and closes stream,
156 # so it is in a half-closed state
157 c.send_data(1, b'some data', end_stream=True)
158
159 # Server received HEADERS frame but DATA frame is still on the way.
160 # Stream is in open state on the server-side. In this state server is
161 # allowed to end stream and after that acknowledge received data by
162 # sending WINDOW_UPDATE frames.
163 headers_frame = frame_factory.build_headers_frame(
164 [(':status', '200')],
165 flags=['END_STREAM'],
166 stream_id=1,
167 )
168 events = c.receive_data(headers_frame.serialize())
169 assert len(events) == 2
170 response_received, stream_ended = events
171 assert isinstance(response_received, h2.events.ResponseReceived)
172 assert isinstance(stream_ended, h2.events.StreamEnded)
173
174 window_update_frame = frame_factory.build_window_update_frame(
175 stream_id=1,
176 increment=1,
177 )
178 events = c.receive_data(window_update_frame.serialize())
179 assert not events
180
181 # Getting open_inbound_streams property will trigger stream accounting
182 # which results in removing closed streams.
183 c.open_inbound_streams
184
185 # Another window_update_frame is received for stream 1
186 events = c.receive_data(window_update_frame.serialize())
187 assert not events
188
189
190 class TestStreamsClosedByEndStream(object):
191 example_request_headers = [
192 (':authority', 'example.com'),
193 (':path', '/'),
194 (':scheme', 'https'),
195 (':method', 'GET'),
196 ]
197 example_response_headers = [
198 (':status', '200'),
199 ('server', 'fake-serv/0.1.0')
200 ]
201 server_config = h2.config.H2Configuration(client_side=False)
202
203 @pytest.mark.parametrize(
204 "frame",
205 [
206 lambda self, ff: ff.build_headers_frame(
207 self.example_request_headers, flags=['END_STREAM']),
208 lambda self, ff: ff.build_headers_frame(
209 self.example_request_headers),
210 ]
211 )
212 @pytest.mark.parametrize("clear_streams", [True, False])
213 def test_frames_after_recv_end_will_error(self,
214 frame_factory,
215 frame,
216 clear_streams):
217 """
218 A stream that is closed by receiving END_STREAM raises
219 ProtocolError when it receives an unexpected frame.
220 """
221 c = h2.connection.H2Connection(config=self.server_config)
222 c.receive_data(frame_factory.preamble())
223 c.initiate_connection()
224
225 f = frame_factory.build_headers_frame(
226 self.example_request_headers, flags=['END_STREAM']
227 )
228 c.receive_data(f.serialize())
229 c.send_headers(
230 stream_id=1,
231 headers=self.example_response_headers,
232 end_stream=True
233 )
234
235 if clear_streams:
236 # Call open_inbound_streams to force the connection to clean
237 # closed streams.
238 c.open_inbound_streams
239
240 c.clear_outbound_data_buffer()
241
242 f = frame(self, frame_factory)
243 with pytest.raises(h2.exceptions.ProtocolError):
244 c.receive_data(f.serialize())
245
246 f = frame_factory.build_goaway_frame(
247 last_stream_id=1,
248 error_code=h2.errors.ErrorCodes.STREAM_CLOSED,
249 )
250 assert c.data_to_send() == f.serialize()
251
252 @pytest.mark.parametrize(
253 "frame",
254 [
255 lambda self, ff: ff.build_headers_frame(
256 self.example_response_headers, flags=['END_STREAM']),
257 lambda self, ff: ff.build_headers_frame(
258 self.example_response_headers),
259 ]
260 )
261 @pytest.mark.parametrize("clear_streams", [True, False])
262 def test_frames_after_send_end_will_error(self,
263 frame_factory,
264 frame,
265 clear_streams):
266 """
267 A stream that is closed by sending END_STREAM raises
268 ProtocolError when it receives an unexpected frame.
269 """
270 c = h2.connection.H2Connection()
271 c.initiate_connection()
272 c.send_headers(stream_id=1, headers=self.example_request_headers,
273 end_stream=True)
274
275 f = frame_factory.build_headers_frame(
276 self.example_response_headers, flags=['END_STREAM']
277 )
278 c.receive_data(f.serialize())
279
280 if clear_streams:
281 # Call open_outbound_streams to force the connection to clean
282 # closed streams.
283 c.open_outbound_streams
284
285 c.clear_outbound_data_buffer()
286
287 f = frame(self, frame_factory)
288 with pytest.raises(h2.exceptions.ProtocolError):
289 c.receive_data(f.serialize())
290
291 f = frame_factory.build_goaway_frame(
292 last_stream_id=0,
293 error_code=h2.errors.ErrorCodes.STREAM_CLOSED,
294 )
295 assert c.data_to_send() == f.serialize()
296
297 @pytest.mark.parametrize(
298 "frame",
299 [
300 lambda self, ff: ff.build_window_update_frame(1, 1),
301 lambda self, ff: ff.build_rst_stream_frame(1)
302 ]
303 )
304 def test_frames_after_send_end_will_be_ignored(self,
305 frame_factory,
306 frame):
307 """
308 A stream that is closed by sending END_STREAM will raise
309 ProtocolError when received unexpected frame.
310 """
311 c = h2.connection.H2Connection(config=self.server_config)
312 c.receive_data(frame_factory.preamble())
313 c.initiate_connection()
314
315 f = frame_factory.build_headers_frame(
316 self.example_request_headers, flags=['END_STREAM']
317 )
318 c.receive_data(f.serialize())
319 c.send_headers(
320 stream_id=1,
321 headers=self.example_response_headers,
322 end_stream=True
323 )
324
325 c.clear_outbound_data_buffer()
326
327 f = frame(self, frame_factory)
328 events = c.receive_data(f.serialize())
329
330 assert not events
331
332
333 class TestStreamsClosedByRstStream(object):
334 example_request_headers = [
335 (':authority', 'example.com'),
336 (':path', '/'),
337 (':scheme', 'https'),
338 (':method', 'GET'),
339 ]
340 example_response_headers = [
341 (':status', '200'),
342 ('server', 'fake-serv/0.1.0')
343 ]
344 server_config = h2.config.H2Configuration(client_side=False)
345
346 @pytest.mark.parametrize(
347 "frame",
348 [
349 lambda self, ff: ff.build_headers_frame(
350 self.example_request_headers),
351 lambda self, ff: ff.build_headers_frame(
352 self.example_request_headers, flags=['END_STREAM']),
353 ]
354 )
355 def test_resets_further_frames_after_recv_reset(self,
356 frame_factory,
357 frame):
358 """
359 A stream that is closed by receive RST_STREAM can receive further
360 frames: it simply sends RST_STREAM for it, and additionally
361 WINDOW_UPDATE for DATA frames.
362 """
363 c = h2.connection.H2Connection(config=self.server_config)
364 c.receive_data(frame_factory.preamble())
365 c.initiate_connection()
366
367 header_frame = frame_factory.build_headers_frame(
368 self.example_request_headers, flags=['END_STREAM']
369 )
370 c.receive_data(header_frame.serialize())
371
372 c.send_headers(
373 stream_id=1,
374 headers=self.example_response_headers,
375 end_stream=False
376 )
377
378 rst_frame = frame_factory.build_rst_stream_frame(
379 1, h2.errors.ErrorCodes.STREAM_CLOSED
380 )
381 c.receive_data(rst_frame.serialize())
382 c.clear_outbound_data_buffer()
383
384 f = frame(self, frame_factory)
385 events = c.receive_data(f.serialize())
386
387 rst_frame = frame_factory.build_rst_stream_frame(
388 1, h2.errors.ErrorCodes.STREAM_CLOSED
389 )
390 assert not events
391 assert c.data_to_send() == rst_frame.serialize()
392
393 events = c.receive_data(f.serialize() * 3)
394 assert not events
395 assert c.data_to_send() == rst_frame.serialize() * 3
396
397 # Iterate over the streams to make sure it's gone, then confirm the
398 # behaviour is unchanged.
399 c.open_outbound_streams
400
401 events = c.receive_data(f.serialize() * 3)
402 assert not events
403 assert c.data_to_send() == rst_frame.serialize() * 3
404
405 def test_resets_further_data_frames_after_recv_reset(self,
406 frame_factory):
407 """
408 A stream that is closed by receive RST_STREAM can receive further
409 DATA frames: it simply sends WINDOW_UPDATE for the connection flow
410 window, and RST_STREAM for the stream.
411 """
412 c = h2.connection.H2Connection(config=self.server_config)
413 c.receive_data(frame_factory.preamble())
414 c.initiate_connection()
415
416 header_frame = frame_factory.build_headers_frame(
417 self.example_request_headers, flags=['END_STREAM']
418 )
419 c.receive_data(header_frame.serialize())
420
421 c.send_headers(
422 stream_id=1,
423 headers=self.example_response_headers,
424 end_stream=False
425 )
426
427 rst_frame = frame_factory.build_rst_stream_frame(
428 1, h2.errors.ErrorCodes.STREAM_CLOSED
429 )
430 c.receive_data(rst_frame.serialize())
431 c.clear_outbound_data_buffer()
432
433 f = frame_factory.build_data_frame(
434 data=b'some data'
435 )
436
437 events = c.receive_data(f.serialize())
438 assert not events
439
440 expected = frame_factory.build_rst_stream_frame(
441 stream_id=1,
442 error_code=h2.errors.ErrorCodes.STREAM_CLOSED,
443 ).serialize()
444 assert c.data_to_send() == expected
445
446 events = c.receive_data(f.serialize() * 3)
447 assert not events
448 assert c.data_to_send() == expected * 3
449
450 # Iterate over the streams to make sure it's gone, then confirm the
451 # behaviour is unchanged.
452 c.open_outbound_streams
453
454 events = c.receive_data(f.serialize() * 3)
455 assert not events
456 assert c.data_to_send() == expected * 3
457
458 @pytest.mark.parametrize(
459 "frame",
460 [
461 lambda self, ff: ff.build_headers_frame(
462 self.example_request_headers),
463 lambda self, ff: ff.build_headers_frame(
464 self.example_request_headers, flags=['END_STREAM']),
465 ]
466 )
467 def test_resets_further_frames_after_send_reset(self,
468 frame_factory,
469 frame):
470 """
471 A stream that is closed by sent RST_STREAM can receive further frames:
472 it simply sends RST_STREAM for it.
473 """
474 c = h2.connection.H2Connection(config=self.server_config)
475 c.receive_data(frame_factory.preamble())
476 c.initiate_connection()
477
478 header_frame = frame_factory.build_headers_frame(
479 self.example_request_headers, flags=['END_STREAM']
480 )
481 c.receive_data(header_frame.serialize())
482
483 c.send_headers(
484 stream_id=1,
485 headers=self.example_response_headers,
486 end_stream=False
487 )
488
489 c.reset_stream(1, h2.errors.ErrorCodes.INTERNAL_ERROR)
490
491 rst_frame = frame_factory.build_rst_stream_frame(
492 1, h2.errors.ErrorCodes.STREAM_CLOSED
493 )
494 c.clear_outbound_data_buffer()
495
496 f = frame(self, frame_factory)
497 events = c.receive_data(f.serialize())
498
499 rst_frame = frame_factory.build_rst_stream_frame(
500 1, h2.errors.ErrorCodes.STREAM_CLOSED
501 )
502 assert not events
503 assert c.data_to_send() == rst_frame.serialize()
504
505 events = c.receive_data(f.serialize() * 3)
506 assert not events
507 assert c.data_to_send() == rst_frame.serialize() * 3
508
509 # Iterate over the streams to make sure it's gone, then confirm the
510 # behaviour is unchanged.
511 c.open_outbound_streams
512
513 events = c.receive_data(f.serialize() * 3)
514 assert not events
515 assert c.data_to_send() == rst_frame.serialize() * 3
516
517 def test_resets_further_data_frames_after_send_reset(self,
518 frame_factory):
519 """
520 A stream that is closed by sent RST_STREAM can receive further
521 data frames: it simply sends WINDOW_UPDATE and RST_STREAM for it.
522 """
523 c = h2.connection.H2Connection(config=self.server_config)
524 c.receive_data(frame_factory.preamble())
525 c.initiate_connection()
526
527 header_frame = frame_factory.build_headers_frame(
528 self.example_request_headers, flags=['END_STREAM']
529 )
530 c.receive_data(header_frame.serialize())
531
532 c.send_headers(
533 stream_id=1,
534 headers=self.example_response_headers,
535 end_stream=False
536 )
537
538 c.reset_stream(1, h2.errors.ErrorCodes.INTERNAL_ERROR)
539
540 c.clear_outbound_data_buffer()
541
542 f = frame_factory.build_data_frame(
543 data=b'some data'
544 )
545 events = c.receive_data(f.serialize())
546 assert not events
547 expected = frame_factory.build_rst_stream_frame(
548 stream_id=1,
549 error_code=h2.errors.ErrorCodes.STREAM_CLOSED,
550 ).serialize()
551 assert c.data_to_send() == expected
552
553 events = c.receive_data(f.serialize() * 3)
554 assert not events
555 assert c.data_to_send() == expected * 3
556
557 # Iterate over the streams to make sure it's gone, then confirm the
558 # behaviour is unchanged.
559 c.open_outbound_streams
560
561 events = c.receive_data(f.serialize() * 3)
562 assert not events
563 assert c.data_to_send() == expected * 3