Debian Codegraph @

test/rfc9204_SUITE.erl

raw
1 %% Copyright (c) Loïc Hoguin <[email protected]>
2 %%
3 %% Permission to use, copy, modify, and/or distribute this software for any
4 %% purpose with or without fee is hereby granted, provided that the above
5 %% copyright notice and this permission notice appear in all copies.
6 %%
7 %% THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
8 %% WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
9 %% MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
10 %% ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
11 %% WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
12 %% ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
13 %% OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
14
15 -module(rfc9204_SUITE).
16 -compile(export_all).
17 -compile(nowarn_export_all).
18
19 -import(ct_helper, [config/2]).
20 -import(ct_helper, [doc/1]).
21
22 -ifdef(COWBOY_QUICER).
23
24 -include_lib("quicer/include/quicer.hrl").
25
26 all() ->
27 [{group, h3}].
28
29 groups() ->
30 %% @todo Enable parallel tests but for this issues in the
31 %% QUIC accept loop need to be figured out (can't connect
32 %% concurrently somehow, no backlog?).
33 [{h3, [], ct_helper:all(?MODULE)}].
34
35 init_per_group(Name = h3, Config) ->
36 cowboy_test:init_http3(Name, #{
37 env => #{dispatch => cowboy_router:compile(init_routes(Config))}
38 }, Config).
39
40 end_per_group(Name, _) ->
41 cowboy_test:stop_group(Name).
42
43 init_routes(_) -> [
44 {"localhost", [
45 {"/", hello_h, []}
46 ]}
47 ].
48
49 %% Encoder.
50
51 %% 2.1
52 %% QPACK preserves the ordering of field lines within
53 %% each field section. An encoder MUST emit field
54 %% representations in the order they appear in the
55 %% input field section.
56
57 %% 2.1.1
58 %% If the dynamic table does not contain enough room
59 %% for a new entry without evicting other entries,
60 %% and the entries that would be evicted are not evictable,
61 %% the encoder MUST NOT insert that entry into the dynamic
62 %% table (including duplicates of existing entries).
63 %% In order to avoid this, an encoder that uses the
64 %% dynamic table has to keep track of each dynamic
65 %% table entry referenced by each field section until
66 %% those representations are acknowledged by the decoder;
67 %% see Section 4.4.1.
68
69 %% 2.1.2
70 %% The decoder specifies an upper bound on the number
71 %% of streams that can be blocked using the
72 %% SETTINGS_QPACK_BLOCKED_STREAMS setting; see Section 5.
73 %% An encoder MUST limit the number of streams that could
74 %% become blocked to the value of SETTINGS_QPACK_BLOCKED_STREAMS
75 %% at all times. If a decoder encounters more blocked streams
76 %% than it promised to support, it MUST treat this as a
77 %% connection error of type QPACK_DECOMPRESSION_FAILED.
78
79 %% 2.1.3
80 %% To avoid these deadlocks, an encoder SHOULD NOT
81 %% write an instruction unless sufficient stream and
82 %% connection flow-control credit is available for
83 %% the entire instruction.
84
85 %% Decoder.
86
87 %% 2.2
88 %% The decoder MUST emit field lines in the order their
89 %% representations appear in the encoded field section.
90
91 %% 2.2.1
92 %% While blocked, encoded field section data SHOULD
93 %% remain in the blocked stream's flow-control window.
94
95 %% If it encounters a Required Insert Count smaller than
96 %% expected, it MUST treat this as a connection error of
97 %% type QPACK_DECOMPRESSION_FAILED; see Section 2.2.3.
98
99 %% If it encounters a Required Insert Count larger than
100 %% expected, it MAY treat this as a connection error of
101 %% type QPACK_DECOMPRESSION_FAILED.
102
103 %% After the decoder finishes decoding a field section
104 %% encoded using representations containing dynamic table
105 %% references, it MUST emit a Section Acknowledgment
106 %% instruction (Section 4.4.1).
107
108 %% 2.2.2.2
109 %% A decoder with a maximum dynamic table capacity
110 %% (Section 3.2.3) equal to zero MAY omit sending Stream
111 %% Cancellations, because the encoder cannot have any
112 %% dynamic table references.
113
114 %% 2.2.3
115 %% If the decoder encounters a reference in a field line
116 %% representation to a dynamic table entry that has already
117 %% been evicted or that has an absolute index greater than
118 %% or equal to the declared Required Insert Count (Section 4.5.1),
119 %% it MUST treat this as a connection error of type
120 %% QPACK_DECOMPRESSION_FAILED.
121
122 %% If the decoder encounters a reference in an encoder
123 %% instruction to a dynamic table entry that has already
124 %% been evicted, it MUST treat this as a connection error
125 %% of type QPACK_ENCODER_STREAM_ERROR.
126
127 %% Static table.
128
129 %% 3.1
130 %% When the decoder encounters an invalid static table index
131 %% in a field line representation, it MUST treat this as a
132 %% connection error of type QPACK_DECOMPRESSION_FAILED.
133 %%
134 %% If this index is received on the encoder stream, this
135 %% MUST be treated as a connection error of type
136 %% QPACK_ENCODER_STREAM_ERROR.
137
138 %% Dynamic table.
139
140 %% 3.2
141 %% The dynamic table can contain duplicate entries
142 %% (i.e., entries with the same name and same value).
143 %% Therefore, duplicate entries MUST NOT be treated
144 %% as an error by the decoder.
145
146 %% 3.2.2
147 %% The encoder MUST NOT cause a dynamic table entry to be
148 %% evicted unless that entry is evictable; see Section 2.1.1.
149
150 %% It is an error if the encoder attempts to add an entry
151 %% that is larger than the dynamic table capacity; the
152 %% decoder MUST treat this as a connection error of type
153 %% QPACK_ENCODER_STREAM_ERROR.
154
155 %% 3.2.3
156 %% The encoder MUST NOT set a dynamic table capacity that
157 %% exceeds this maximum, but it can choose to use a lower
158 %% dynamic table capacity; see Section 4.3.1.
159
160 %% When the client's 0-RTT value of the SETTING is zero,
161 %% the server MAY set it to a non-zero value in its SETTINGS
162 %% frame. If the remembered value is non-zero, the server
163 %% MUST send the same non-zero value in its SETTINGS frame.
164 %% If it specifies any other value, or omits
165 %% SETTINGS_QPACK_MAX_TABLE_CAPACITY from SETTINGS,
166 %% the encoder must treat this as a connection error of
167 %% type QPACK_DECODER_STREAM_ERROR.
168
169 %% When the maximum table capacity is zero, the encoder
170 %% MUST NOT insert entries into the dynamic table and
171 %% MUST NOT send any encoder instructions on the encoder stream.
172
173 %% Wire format.
174
175 %% 4.1.1
176 %% QPACK implementations MUST be able to decode integers
177 %% up to and including 62 bits long.
178
179 %% Encoder and decoder streams.
180
181 decoder_reject_multiple(Config) ->
182 doc("Endpoints must not create multiple decoder streams. (RFC9204 4.2)"),
183 rfc9114_SUITE:do_critical_reject_multiple(Config, <<3>>).
184
185 encoder_reject_multiple(Config) ->
186 doc("Endpoints must not create multiple encoder streams. (RFC9204 4.2)"),
187 rfc9114_SUITE:do_critical_reject_multiple(Config, <<2>>).
188
189 %% 4.2
190 %% The sender MUST NOT close either of these streams,
191 %% and the receiver MUST NOT request that the sender close
192 %% either of these streams. Closure of either unidirectional
193 %% stream type MUST be treated as a connection error of type
194 %% H3_CLOSED_CRITICAL_STREAM.
195
196 decoder_local_closed_abort(Config) ->
197 doc("Endpoints must not close the decoder stream. (RFC9204 4.2)"),
198 rfc9114_SUITE:do_critical_local_closed_abort(Config, <<3>>).
199
200 decoder_local_closed_graceful(Config) ->
201 doc("Endpoints must not close the decoder stream. (RFC9204 4.2)"),
202 rfc9114_SUITE:do_critical_local_closed_graceful(Config, <<3>>).
203
204 decoder_remote_closed_abort(Config) ->
205 doc("Endpoints must not close the decoder stream. (RFC9204 4.2)"),
206 #{conn := Conn} = rfc9114_SUITE:do_connect(Config, #{peer_unidi_stream_count => 3}),
207 {ok, #{decoder := StreamRef}} = do_wait_unidi_streams(Conn, #{}),
208 %% Close the control stream.
209 quicer:async_shutdown_stream(StreamRef, ?QUIC_STREAM_SHUTDOWN_FLAG_ABORT, 0),
210 %% The connection should have been closed.
211 #{reason := h3_closed_critical_stream} = rfc9114_SUITE:do_wait_connection_closed(Conn),
212 ok.
213
214 encoder_local_closed_abort(Config) ->
215 doc("Endpoints must not close the encoder stream. (RFC9204 4.2)"),
216 rfc9114_SUITE:do_critical_local_closed_abort(Config, <<2>>).
217
218 encoder_local_closed_graceful(Config) ->
219 doc("Endpoints must not close the encoder stream. (RFC9204 4.2)"),
220 rfc9114_SUITE:do_critical_local_closed_graceful(Config, <<2>>).
221
222 encoder_remote_closed_abort(Config) ->
223 doc("Endpoints must not close the encoder stream. (RFC9204 4.2)"),
224 #{conn := Conn} = rfc9114_SUITE:do_connect(Config, #{peer_unidi_stream_count => 3}),
225 {ok, #{encoder := StreamRef}} = do_wait_unidi_streams(Conn, #{}),
226 %% Close the control stream.
227 quicer:async_shutdown_stream(StreamRef, ?QUIC_STREAM_SHUTDOWN_FLAG_ABORT, 0),
228 %% The connection should have been closed.
229 #{reason := h3_closed_critical_stream} = rfc9114_SUITE:do_wait_connection_closed(Conn),
230 ok.
231
232 do_wait_unidi_streams(_, Acc=#{decoder := _, encoder := _}) ->
233 {ok, Acc};
234 do_wait_unidi_streams(Conn, Acc) ->
235 receive
236 {quic, new_stream, StreamRef, #{flags := Flags}} ->
237 ok = quicer:setopt(StreamRef, active, true),
238 true = quicer:is_unidirectional(Flags),
239 receive {quic, <<TypeValue>>, StreamRef, _} ->
240 Type = case TypeValue of
241 2 -> encoder;
242 3 -> decoder
243 end,
244 do_wait_unidi_streams(Conn, Acc#{Type => StreamRef})
245 after 5000 ->
246 {error, timeout}
247 end
248 after 5000 ->
249 {error, timeout}
250 end.
251
252 %% An endpoint MAY avoid creating an encoder stream if it will
253 %% not be used (for example, if its encoder does not wish to
254 %% use the dynamic table or if the maximum size of the dynamic
255 %% table permitted by the peer is zero).
256
257 %% An endpoint MAY avoid creating a decoder stream if its
258 %% decoder sets the maximum capacity of the dynamic table to zero.
259
260 %% An endpoint MUST allow its peer to create an encoder stream
261 %% and a decoder stream even if the connection's settings
262 %% prevent their use.
263
264 %% Encoder instructions.
265
266 %% 4.3.1
267 %% The new capacity MUST be lower than or equal to the limit
268 %% described in Section 3.2.3. In HTTP/3, this limit is the
269 %% value of the SETTINGS_QPACK_MAX_TABLE_CAPACITY parameter
270 %% (Section 5) received from the decoder. The decoder MUST
271 %% treat a new dynamic table capacity value that exceeds this
272 %% limit as a connection error of type QPACK_ENCODER_STREAM_ERROR.
273
274 %% Reducing the dynamic table capacity can cause entries to be
275 %% evicted; see Section 3.2.2. This MUST NOT cause the eviction
276 %% of entries that are not evictable; see Section 2.1.1.
277
278 %% Decoder instructions.
279
280 %% 4.4.1
281 %% If an encoder receives a Section Acknowledgment instruction
282 %% referring to a stream on which every encoded field section
283 %% with a non-zero Required Insert Count has already been
284 %% acknowledged, this MUST be treated as a connection error
285 %% of type QPACK_DECODER_STREAM_ERROR.
286
287 %% 4.4.3
288 %% An encoder that receives an Increment field equal to zero,
289 %% or one that increases the Known Received Count beyond what
290 %% the encoder has sent, MUST treat this as a connection error
291 %% of type QPACK_DECODER_STREAM_ERROR.
292
293 %% Field line representation.
294
295 %% 4.5.1.1
296 %% If the decoder encounters a value of EncodedInsertCount that
297 %% could not have been produced by a conformant encoder, it MUST
298 %% treat this as a connection error of type QPACK_DECOMPRESSION_FAILED.
299
300 %% 4.5.1.2
301 %% The value of Base MUST NOT be negative. Though the protocol
302 %% might operate correctly with a negative Base using post-Base
303 %% indexing, it is unnecessary and inefficient. An endpoint MUST
304 %% treat a field block with a Sign bit of 1 as invalid if the
305 %% value of Required Insert Count is less than or equal to the
306 %% value of Delta Base.
307
308 %% 4.5.4
309 %% When the 'N' bit is set, the encoded field line MUST always
310 %% be encoded with a literal representation. In particular,
311 %% when a peer sends a field line that it received represented
312 %% as a literal field line with the 'N' bit set, it MUST use a
313 %% literal representation to forward this field line. This bit
314 %% is intended for protecting field values that are not to be
315 %% put at risk by compressing them; see Section 7.1 for more details.
316
317 %% Configuration.
318
319 %% 5
320 %% SETTINGS_QPACK_MAX_TABLE_CAPACITY
321 %% SETTINGS_QPACK_BLOCKED_STREAMS
322
323 %% Security considerations.
324
325 %% 7.1.2
326 %% (security if used as a proxy merging many connections into one)
327 %% An ideal solution segregates access to the dynamic table
328 %% based on the entity that is constructing the message.
329 %% Field values that are added to the table are attributed
330 %% to an entity, and only the entity that created a particular
331 %% value can extract that value.
332
333 %% 7.1.3
334 %% An intermediary MUST NOT re-encode a value that uses a
335 %% literal representation with the 'N' bit set with another
336 %% representation that would index it. If QPACK is used for
337 %% re-encoding, a literal representation with the 'N' bit set
338 %% MUST be used. If HPACK is used for re-encoding, the
339 %% never-indexed literal representation (see Section 6.2.3
340 %% of [RFC7541]) MUST be used.
341
342 %% 7.4
343 %% An implementation has to set a limit for the values it
344 %% accepts for integers, as well as for the encoded length;
345 %% see Section 4.1.1. In the same way, it has to set a limit
346 %% to the length it accepts for string literals; see Section 4.1.2.
347 %% These limits SHOULD be large enough to process the largest
348 %% individual field the HTTP implementation can be configured
349 %% to accept.
350
351 %% If an implementation encounters a value larger than it is
352 %% able to decode, this MUST be treated as a stream error of
353 %% type QPACK_DECOMPRESSION_FAILED if on a request stream or
354 %% a connection error of the appropriate type if on the
355 %% encoder or decoder stream.
356
357 -else.
358
359 all() -> [].
360
361 -endif.