Debian Codegraph @

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

raw
1 # -*- coding: utf-8 -*-
2 """
3 test_utility_functions
4 ~~~~~~~~~~~~~~~~~~~~~~
5
6 Tests for the various utility functions provided by hyper-h2.
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 from h2.utilities import SizeLimitDict, extract_method_header
16
17
18 class TestGetNextAvailableStreamID(object):
19 """
20 Tests for the ``H2Connection.get_next_available_stream_id`` method.
21 """
22 example_request_headers = [
23 (':authority', 'example.com'),
24 (':path', '/'),
25 (':scheme', 'https'),
26 (':method', 'GET'),
27 ]
28 example_response_headers = [
29 (':status', '200'),
30 ('server', 'fake-serv/0.1.0')
31 ]
32 server_config = h2.config.H2Configuration(client_side=False)
33
34 def test_returns_correct_sequence_for_clients(self, frame_factory):
35 """
36 For a client connection, the correct sequence of stream IDs is
37 returned.
38 """
39 # Running the exhaustive version of this test (all 1 billion available
40 # stream IDs) is too painful. For that reason, we validate that the
41 # original sequence is right for the first few thousand, and then just
42 # check that it terminates properly.
43 #
44 # Make sure that the streams get cleaned up: 8k streams floating
45 # around would make this test memory-hard, and it's not supposed to be
46 # a test of how much RAM your machine has.
47 c = h2.connection.H2Connection()
48 c.initiate_connection()
49 initial_sequence = range(1, 2**13, 2)
50
51 for expected_stream_id in initial_sequence:
52 stream_id = c.get_next_available_stream_id()
53 assert stream_id == expected_stream_id
54
55 c.send_headers(
56 stream_id=stream_id,
57 headers=self.example_request_headers,
58 end_stream=True
59 )
60 f = frame_factory.build_headers_frame(
61 headers=self.example_response_headers,
62 stream_id=stream_id,
63 flags=['END_STREAM'],
64 )
65 c.receive_data(f.serialize())
66 c.clear_outbound_data_buffer()
67
68 # Jump up to the last available stream ID. Don't clean up the stream
69 # here because who cares about one stream.
70 last_client_id = 2**31 - 1
71 c.send_headers(
72 stream_id=last_client_id,
73 headers=self.example_request_headers,
74 end_stream=True
75 )
76
77 with pytest.raises(h2.exceptions.NoAvailableStreamIDError):
78 c.get_next_available_stream_id()
79
80 def test_returns_correct_sequence_for_servers(self, frame_factory):
81 """
82 For a server connection, the correct sequence of stream IDs is
83 returned.
84 """
85 # Running the exhaustive version of this test (all 1 billion available
86 # stream IDs) is too painful. For that reason, we validate that the
87 # original sequence is right for the first few thousand, and then just
88 # check that it terminates properly.
89 #
90 # Make sure that the streams get cleaned up: 8k streams floating
91 # around would make this test memory-hard, and it's not supposed to be
92 # a test of how much RAM your machine has.
93 c = h2.connection.H2Connection(config=self.server_config)
94 c.initiate_connection()
95 c.receive_data(frame_factory.preamble())
96 f = frame_factory.build_headers_frame(
97 headers=self.example_request_headers
98 )
99 c.receive_data(f.serialize())
100
101 initial_sequence = range(2, 2**13, 2)
102
103 for expected_stream_id in initial_sequence:
104 stream_id = c.get_next_available_stream_id()
105 assert stream_id == expected_stream_id
106
107 c.push_stream(
108 stream_id=1,
109 promised_stream_id=stream_id,
110 request_headers=self.example_request_headers
111 )
112 c.send_headers(
113 stream_id=stream_id,
114 headers=self.example_response_headers,
115 end_stream=True
116 )
117 c.clear_outbound_data_buffer()
118
119 # Jump up to the last available stream ID. Don't clean up the stream
120 # here because who cares about one stream.
121 last_server_id = 2**31 - 2
122 c.push_stream(
123 stream_id=1,
124 promised_stream_id=last_server_id,
125 request_headers=self.example_request_headers,
126 )
127
128 with pytest.raises(h2.exceptions.NoAvailableStreamIDError):
129 c.get_next_available_stream_id()
130
131 def test_does_not_increment_without_stream_send(self):
132 """
133 If a new stream isn't actually created, the next stream ID doesn't
134 change.
135 """
136 c = h2.connection.H2Connection()
137 c.initiate_connection()
138
139 first_stream_id = c.get_next_available_stream_id()
140 second_stream_id = c.get_next_available_stream_id()
141
142 assert first_stream_id == second_stream_id
143
144 c.send_headers(
145 stream_id=first_stream_id,
146 headers=self.example_request_headers
147 )
148
149 third_stream_id = c.get_next_available_stream_id()
150 assert third_stream_id == (first_stream_id + 2)
151
152
153 class TestExtractHeader(object):
154
155 example_request_headers = [
156 (u':authority', u'example.com'),
157 (u':path', u'/'),
158 (u':scheme', u'https'),
159 (u':method', u'GET'),
160 ]
161 example_headers_with_bytes = [
162 (b':authority', b'example.com'),
163 (b':path', b'/'),
164 (b':scheme', b'https'),
165 (b':method', b'GET'),
166 ]
167
168 @pytest.mark.parametrize(
169 'headers', [example_request_headers, example_headers_with_bytes]
170 )
171 def test_extract_header_method(self, headers):
172 assert extract_method_header(headers) == b'GET'
173
174
175 def test_size_limit_dict_limit():
176 dct = SizeLimitDict(size_limit=2)
177
178 dct[1] = 1
179 dct[2] = 2
180
181 assert len(dct) == 2
182 assert dct[1] == 1
183 assert dct[2] == 2
184
185 dct[3] = 3
186
187 assert len(dct) == 2
188 assert dct[2] == 2
189 assert dct[3] == 3
190 assert 1 not in dct
191
192
193 def test_size_limit_dict_limit_init():
194 initial_dct = {
195 1: 1,
196 2: 2,
197 3: 3,
198 }
199
200 dct = SizeLimitDict(initial_dct, size_limit=2)
201
202 assert len(dct) == 2
203
204
205 def test_size_limit_dict_no_limit():
206 dct = SizeLimitDict(size_limit=None)
207
208 dct[1] = 1
209 dct[2] = 2
210
211 assert len(dct) == 2
212 assert dct[1] == 1
213 assert dct[2] == 2
214
215 dct[3] = 3
216
217 assert len(dct) == 3
218 assert dct[1] == 1
219 assert dct[2] == 2
220 assert dct[3] == 3