Debian Codegraph @

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

raw
1 # -*- coding: utf-8 -*-
2 """
3 test_complex_logic
4 ~~~~~~~~~~~~~~~~
5
6 More complex tests that try to do more.
7
8 Certain tests don't really eliminate incorrect behaviour unless they do quite
9 a bit. These tests should live here, to keep the pain in once place rather than
10 hide it in the other parts of the test suite.
11 """
12 import pytest
13
14 import h2
15 import h2.config
16 import h2.connection
17
18
19 class TestComplexClient(object):
20 """
21 Complex tests for client-side stacks.
22 """
23 example_request_headers = [
24 (':authority', 'example.com'),
25 (':path', '/'),
26 (':scheme', 'https'),
27 (':method', 'GET'),
28 ]
29 example_response_headers = [
30 (':status', '200'),
31 ('server', 'fake-serv/0.1.0')
32 ]
33
34 def test_correctly_count_server_streams(self, frame_factory):
35 """
36 We correctly count the number of server streams, both inbound and
37 outbound.
38 """
39 # This test makes no sense unless you do both inbound and outbound,
40 # because it's important to confirm that we count them correctly.
41 c = h2.connection.H2Connection()
42 c.initiate_connection()
43 expected_inbound_streams = expected_outbound_streams = 0
44
45 assert c.open_inbound_streams == expected_inbound_streams
46 assert c.open_outbound_streams == expected_outbound_streams
47
48 for stream_id in range(1, 15, 2):
49 # Open an outbound stream
50 c.send_headers(stream_id, self.example_request_headers)
51 expected_outbound_streams += 1
52 assert c.open_inbound_streams == expected_inbound_streams
53 assert c.open_outbound_streams == expected_outbound_streams
54
55 # Receive a pushed stream (to create an inbound one). This doesn't
56 # open until we also receive headers.
57 f = frame_factory.build_push_promise_frame(
58 stream_id=stream_id,
59 promised_stream_id=stream_id+1,
60 headers=self.example_request_headers,
61 )
62 c.receive_data(f.serialize())
63 assert c.open_inbound_streams == expected_inbound_streams
64 assert c.open_outbound_streams == expected_outbound_streams
65
66 f = frame_factory.build_headers_frame(
67 stream_id=stream_id+1,
68 headers=self.example_response_headers,
69 )
70 c.receive_data(f.serialize())
71 expected_inbound_streams += 1
72 assert c.open_inbound_streams == expected_inbound_streams
73 assert c.open_outbound_streams == expected_outbound_streams
74
75 for stream_id in range(13, 0, -2):
76 # Close an outbound stream.
77 c.end_stream(stream_id)
78
79 # Stream doesn't close until both sides close it.
80 assert c.open_inbound_streams == expected_inbound_streams
81 assert c.open_outbound_streams == expected_outbound_streams
82
83 f = frame_factory.build_headers_frame(
84 stream_id=stream_id,
85 headers=self.example_response_headers,
86 flags=['END_STREAM'],
87 )
88 c.receive_data(f.serialize())
89 expected_outbound_streams -= 1
90 assert c.open_inbound_streams == expected_inbound_streams
91 assert c.open_outbound_streams == expected_outbound_streams
92
93 # Pushed streams can only be closed remotely.
94 f = frame_factory.build_data_frame(
95 stream_id=stream_id+1,
96 data=b'the content',
97 flags=['END_STREAM'],
98 )
99 c.receive_data(f.serialize())
100 expected_inbound_streams -= 1
101 assert c.open_inbound_streams == expected_inbound_streams
102 assert c.open_outbound_streams == expected_outbound_streams
103
104 assert c.open_inbound_streams == 0
105 assert c.open_outbound_streams == 0
106
107
108 class TestComplexServer(object):
109 """
110 Complex tests for server-side stacks.
111 """
112 example_request_headers = [
113 (b':authority', b'example.com'),
114 (b':path', b'/'),
115 (b':scheme', b'https'),
116 (b':method', b'GET'),
117 ]
118 example_response_headers = [
119 (b':status', b'200'),
120 (b'server', b'fake-serv/0.1.0')
121 ]
122 server_config = h2.config.H2Configuration(client_side=False)
123
124 def test_correctly_count_server_streams(self, frame_factory):
125 """
126 We correctly count the number of server streams, both inbound and
127 outbound.
128 """
129 # This test makes no sense unless you do both inbound and outbound,
130 # because it's important to confirm that we count them correctly.
131 c = h2.connection.H2Connection(config=self.server_config)
132 c.receive_data(frame_factory.preamble())
133 expected_inbound_streams = expected_outbound_streams = 0
134
135 assert c.open_inbound_streams == expected_inbound_streams
136 assert c.open_outbound_streams == expected_outbound_streams
137
138 for stream_id in range(1, 15, 2):
139 # Receive an inbound stream.
140 f = frame_factory.build_headers_frame(
141 headers=self.example_request_headers,
142 stream_id=stream_id,
143 )
144 c.receive_data(f.serialize())
145 expected_inbound_streams += 1
146 assert c.open_inbound_streams == expected_inbound_streams
147 assert c.open_outbound_streams == expected_outbound_streams
148
149 # Push a stream (to create a outbound one). This doesn't open
150 # until we send our response headers.
151 c.push_stream(stream_id, stream_id+1, self.example_request_headers)
152 assert c.open_inbound_streams == expected_inbound_streams
153 assert c.open_outbound_streams == expected_outbound_streams
154
155 c.send_headers(stream_id+1, self.example_response_headers)
156 expected_outbound_streams += 1
157 assert c.open_inbound_streams == expected_inbound_streams
158 assert c.open_outbound_streams == expected_outbound_streams
159
160 for stream_id in range(13, 0, -2):
161 # Close an inbound stream.
162 f = frame_factory.build_data_frame(
163 data=b'',
164 flags=['END_STREAM'],
165 stream_id=stream_id,
166 )
167 c.receive_data(f.serialize())
168
169 # Stream doesn't close until both sides close it.
170 assert c.open_inbound_streams == expected_inbound_streams
171 assert c.open_outbound_streams == expected_outbound_streams
172
173 c.send_data(stream_id, b'', end_stream=True)
174 expected_inbound_streams -= 1
175 assert c.open_inbound_streams == expected_inbound_streams
176 assert c.open_outbound_streams == expected_outbound_streams
177
178 # Pushed streams, however, we can close ourselves.
179 c.send_data(
180 stream_id=stream_id+1,
181 data=b'',
182 end_stream=True,
183 )
184 expected_outbound_streams -= 1
185 assert c.open_inbound_streams == expected_inbound_streams
186 assert c.open_outbound_streams == expected_outbound_streams
187
188 assert c.open_inbound_streams == 0
189 assert c.open_outbound_streams == 0
190
191
192 class TestContinuationFrames(object):
193 """
194 Tests for the relatively complex CONTINUATION frame logic.
195 """
196 example_request_headers = [
197 (b':authority', b'example.com'),
198 (b':path', b'/'),
199 (b':scheme', b'https'),
200 (b':method', b'GET'),
201 ]
202 server_config = h2.config.H2Configuration(client_side=False)
203
204 def _build_continuation_sequence(self, headers, block_size, frame_factory):
205 f = frame_factory.build_headers_frame(headers)
206 header_data = f.data
207 chunks = [
208 header_data[x:x+block_size]
209 for x in range(0, len(header_data), block_size)
210 ]
211 f.data = chunks.pop(0)
212 frames = [
213 frame_factory.build_continuation_frame(c) for c in chunks
214 ]
215 f.flags = {'END_STREAM'}
216 frames[-1].flags.add('END_HEADERS')
217 frames.insert(0, f)
218 return frames
219
220 def test_continuation_frame_basic(self, frame_factory):
221 """
222 Test that we correctly decode a header block split across continuation
223 frames.
224 """
225 c = h2.connection.H2Connection(config=self.server_config)
226 c.initiate_connection()
227 c.receive_data(frame_factory.preamble())
228
229 frames = self._build_continuation_sequence(
230 headers=self.example_request_headers,
231 block_size=5,
232 frame_factory=frame_factory,
233 )
234 data = b''.join(f.serialize() for f in frames)
235 events = c.receive_data(data)
236
237 assert len(events) == 2
238 first_event, second_event = events
239
240 assert isinstance(first_event, h2.events.RequestReceived)
241 assert first_event.headers == self.example_request_headers
242 assert first_event.stream_id == 1
243
244 assert isinstance(second_event, h2.events.StreamEnded)
245 assert second_event.stream_id == 1
246
247 @pytest.mark.parametrize('stream_id', [3, 1])
248 def test_continuation_cannot_interleave_headers(self,
249 frame_factory,
250 stream_id):
251 """
252 We cannot interleave a new headers block with a CONTINUATION sequence.
253 """
254 c = h2.connection.H2Connection(config=self.server_config)
255 c.initiate_connection()
256 c.receive_data(frame_factory.preamble())
257 c.clear_outbound_data_buffer()
258
259 frames = self._build_continuation_sequence(
260 headers=self.example_request_headers,
261 block_size=5,
262 frame_factory=frame_factory,
263 )
264 assert len(frames) > 2 # This is mostly defensive.
265
266 bogus_frame = frame_factory.build_headers_frame(
267 headers=self.example_request_headers,
268 stream_id=stream_id,
269 flags=['END_STREAM'],
270 )
271 frames.insert(len(frames) - 2, bogus_frame)
272 data = b''.join(f.serialize() for f in frames)
273
274 with pytest.raises(h2.exceptions.ProtocolError) as e:
275 c.receive_data(data)
276
277 assert "invalid frame" in str(e.value).lower()
278
279 def test_continuation_cannot_interleave_data(self, frame_factory):
280 """
281 We cannot interleave a data frame with a CONTINUATION sequence.
282 """
283 c = h2.connection.H2Connection(config=self.server_config)
284 c.initiate_connection()
285 c.receive_data(frame_factory.preamble())
286 c.clear_outbound_data_buffer()
287
288 frames = self._build_continuation_sequence(
289 headers=self.example_request_headers,
290 block_size=5,
291 frame_factory=frame_factory,
292 )
293 assert len(frames) > 2 # This is mostly defensive.
294
295 bogus_frame = frame_factory.build_data_frame(
296 data=b'hello',
297 stream_id=1,
298 )
299 frames.insert(len(frames) - 2, bogus_frame)
300 data = b''.join(f.serialize() for f in frames)
301
302 with pytest.raises(h2.exceptions.ProtocolError) as e:
303 c.receive_data(data)
304
305 assert "invalid frame" in str(e.value).lower()
306
307 def test_continuation_cannot_interleave_unknown_frame(self, frame_factory):
308 """
309 We cannot interleave an unknown frame with a CONTINUATION sequence.
310 """
311 c = h2.connection.H2Connection(config=self.server_config)
312 c.initiate_connection()
313 c.receive_data(frame_factory.preamble())
314 c.clear_outbound_data_buffer()
315
316 frames = self._build_continuation_sequence(
317 headers=self.example_request_headers,
318 block_size=5,
319 frame_factory=frame_factory,
320 )
321 assert len(frames) > 2 # This is mostly defensive.
322
323 bogus_frame = frame_factory.build_data_frame(
324 data=b'hello',
325 stream_id=1,
326 )
327 bogus_frame.type = 88
328 frames.insert(len(frames) - 2, bogus_frame)
329 data = b''.join(f.serialize() for f in frames)
330
331 with pytest.raises(h2.exceptions.ProtocolError) as e:
332 c.receive_data(data)
333
334 assert "invalid frame" in str(e.value).lower()
335
336 def test_continuation_frame_multiple_blocks(self, frame_factory):
337 """
338 Test that we correctly decode several header blocks split across
339 continuation frames.
340 """
341 c = h2.connection.H2Connection(config=self.server_config)
342 c.initiate_connection()
343 c.receive_data(frame_factory.preamble())
344
345 for stream_id in range(1, 7, 2):
346 frames = self._build_continuation_sequence(
347 headers=self.example_request_headers,
348 block_size=2,
349 frame_factory=frame_factory,
350 )
351 for frame in frames:
352 frame.stream_id = stream_id
353
354 data = b''.join(f.serialize() for f in frames)
355 events = c.receive_data(data)
356
357 assert len(events) == 2
358 first_event, second_event = events
359
360 assert isinstance(first_event, h2.events.RequestReceived)
361 assert first_event.headers == self.example_request_headers
362 assert first_event.stream_id == stream_id
363
364 assert isinstance(second_event, h2.events.StreamEnded)
365 assert second_event.stream_id == stream_id
366
367
368 class TestContinuationFramesPushPromise(object):
369 """
370 Tests for the relatively complex CONTINUATION frame logic working with
371 PUSH_PROMISE frames.
372 """
373 example_request_headers = [
374 (b':authority', b'example.com'),
375 (b':path', b'/'),
376 (b':scheme', b'https'),
377 (b':method', b'GET'),
378 ]
379 example_response_headers = [
380 (b':status', b'200'),
381 (b'server', b'fake-serv/0.1.0')
382 ]
383
384 def _build_continuation_sequence(self, headers, block_size, frame_factory):
385 f = frame_factory.build_push_promise_frame(
386 stream_id=1, promised_stream_id=2, headers=headers
387 )
388 header_data = f.data
389 chunks = [
390 header_data[x:x+block_size]
391 for x in range(0, len(header_data), block_size)
392 ]
393 f.data = chunks.pop(0)
394 frames = [
395 frame_factory.build_continuation_frame(c) for c in chunks
396 ]
397 f.flags = {'END_STREAM'}
398 frames[-1].flags.add('END_HEADERS')
399 frames.insert(0, f)
400 return frames
401
402 def test_continuation_frame_basic_push_promise(self, frame_factory):
403 """
404 Test that we correctly decode a header block split across continuation
405 frames when that header block is initiated with a PUSH_PROMISE.
406 """
407 c = h2.connection.H2Connection()
408 c.initiate_connection()
409 c.send_headers(stream_id=1, headers=self.example_request_headers)
410
411 frames = self._build_continuation_sequence(
412 headers=self.example_request_headers,
413 block_size=5,
414 frame_factory=frame_factory,
415 )
416 data = b''.join(f.serialize() for f in frames)
417 events = c.receive_data(data)
418
419 assert len(events) == 1
420 event = events[0]
421
422 assert isinstance(event, h2.events.PushedStreamReceived)
423 assert event.headers == self.example_request_headers
424 assert event.parent_stream_id == 1
425 assert event.pushed_stream_id == 2
426
427 @pytest.mark.parametrize('stream_id', [3, 1, 2])
428 def test_continuation_cannot_interleave_headers_pp(self,
429 frame_factory,
430 stream_id):
431 """
432 We cannot interleave a new headers block with a CONTINUATION sequence
433 when the headers block is based on a PUSH_PROMISE frame.
434 """
435 c = h2.connection.H2Connection()
436 c.initiate_connection()
437 c.send_headers(stream_id=1, headers=self.example_request_headers)
438
439 frames = self._build_continuation_sequence(
440 headers=self.example_request_headers,
441 block_size=5,
442 frame_factory=frame_factory,
443 )
444 assert len(frames) > 2 # This is mostly defensive.
445
446 bogus_frame = frame_factory.build_headers_frame(
447 headers=self.example_response_headers,
448 stream_id=stream_id,
449 flags=['END_STREAM'],
450 )
451 frames.insert(len(frames) - 2, bogus_frame)
452 data = b''.join(f.serialize() for f in frames)
453
454 with pytest.raises(h2.exceptions.ProtocolError) as e:
455 c.receive_data(data)
456
457 assert "invalid frame" in str(e.value).lower()
458
459 def test_continuation_cannot_interleave_data(self, frame_factory):
460 """
461 We cannot interleave a data frame with a CONTINUATION sequence when
462 that sequence began with a PUSH_PROMISE frame.
463 """
464 c = h2.connection.H2Connection()
465 c.initiate_connection()
466 c.send_headers(stream_id=1, headers=self.example_request_headers)
467
468 frames = self._build_continuation_sequence(
469 headers=self.example_request_headers,
470 block_size=5,
471 frame_factory=frame_factory,
472 )
473 assert len(frames) > 2 # This is mostly defensive.
474
475 bogus_frame = frame_factory.build_data_frame(
476 data=b'hello',
477 stream_id=1,
478 )
479 frames.insert(len(frames) - 2, bogus_frame)
480 data = b''.join(f.serialize() for f in frames)
481
482 with pytest.raises(h2.exceptions.ProtocolError) as e:
483 c.receive_data(data)
484
485 assert "invalid frame" in str(e.value).lower()
486
487 def test_continuation_cannot_interleave_unknown_frame(self, frame_factory):
488 """
489 We cannot interleave an unknown frame with a CONTINUATION sequence when
490 that sequence began with a PUSH_PROMISE frame.
491 """
492 c = h2.connection.H2Connection()
493 c.initiate_connection()
494 c.send_headers(stream_id=1, headers=self.example_request_headers)
495
496 frames = self._build_continuation_sequence(
497 headers=self.example_request_headers,
498 block_size=5,
499 frame_factory=frame_factory,
500 )
501 assert len(frames) > 2 # This is mostly defensive.
502
503 bogus_frame = frame_factory.build_data_frame(
504 data=b'hello',
505 stream_id=1,
506 )
507 bogus_frame.type = 88
508 frames.insert(len(frames) - 2, bogus_frame)
509 data = b''.join(f.serialize() for f in frames)
510
511 with pytest.raises(h2.exceptions.ProtocolError) as e:
512 c.receive_data(data)
513
514 assert "invalid frame" in str(e.value).lower()
515
516 @pytest.mark.parametrize('evict', [True, False])
517 def test_stream_remotely_closed_disallows_push_promise(self,
518 evict,
519 frame_factory):
520 """
521 Streams closed normally by the remote peer disallow PUSH_PROMISE
522 frames, and cause a GOAWAY.
523 """
524 c = h2.connection.H2Connection()
525 c.initiate_connection()
526 c.send_headers(
527 stream_id=1,
528 headers=self.example_request_headers,
529 end_stream=True
530 )
531
532 f = frame_factory.build_headers_frame(
533 stream_id=1,
534 headers=self.example_response_headers,
535 flags=['END_STREAM']
536 )
537 c.receive_data(f.serialize())
538 c.clear_outbound_data_buffer()
539
540 if evict:
541 # This is annoyingly stateful, but enumerating the list of open
542 # streams will force us to flush state.
543 assert not c.open_outbound_streams
544
545 f = frame_factory.build_push_promise_frame(
546 stream_id=1,
547 promised_stream_id=2,
548 headers=self.example_request_headers,
549 )
550
551 with pytest.raises(h2.exceptions.ProtocolError):
552 c.receive_data(f.serialize())
553
554 f = frame_factory.build_goaway_frame(
555 last_stream_id=0,
556 error_code=h2.errors.ErrorCodes.PROTOCOL_ERROR,
557 )
558 assert c.data_to_send() == f.serialize()
559
560 def test_continuation_frame_multiple_push_promise(self, frame_factory):
561 """
562 Test that we correctly decode header blocks split across continuation
563 frames when those header block is initiated with a PUSH_PROMISE, for
564 more than one pushed stream.
565 """
566 c = h2.connection.H2Connection()
567 c.initiate_connection()
568 c.send_headers(stream_id=1, headers=self.example_request_headers)
569
570 for promised_stream_id in range(2, 8, 2):
571 frames = self._build_continuation_sequence(
572 headers=self.example_request_headers,
573 block_size=2,
574 frame_factory=frame_factory,
575 )
576 frames[0].promised_stream_id = promised_stream_id
577 data = b''.join(f.serialize() for f in frames)
578 events = c.receive_data(data)
579
580 assert len(events) == 1
581 event = events[0]
582
583 assert isinstance(event, h2.events.PushedStreamReceived)
584 assert event.headers == self.example_request_headers
585 assert event.parent_stream_id == 1
586 assert event.pushed_stream_id == promised_stream_id