]> git.ipfire.org Git - thirdparty/openssl.git/blame - test/quic_multistream_test.c
Minor updates
[thirdparty/openssl.git] / test / quic_multistream_test.c
CommitLineData
ed835673
HL
1/*
2 * Copyright 2023 The OpenSSL Project Authors. All Rights Reserved.
3 *
4 * Licensed under the Apache License 2.0 (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
8 */
9#include <openssl/ssl.h>
10#include <openssl/quic.h>
11#include <openssl/bio.h>
12#include <openssl/lhash.h>
13#include "internal/quic_tserver.h"
de521629 14#include "internal/quic_ssl.h"
e26dc8e3 15#include "internal/quic_error.h"
ed835673 16#include "testutil.h"
e26dc8e3 17#include "helpers/quictestlib.h"
a350db73
HL
18#if defined(OPENSSL_THREADS)
19# include "internal/thread_arch.h"
20#endif
ed835673
HL
21
22static const char *certfile, *keyfile;
23
a350db73
HL
24#if defined(OPENSSL_THREADS)
25struct child_thread_args {
26 struct helper *h;
27 const struct script_op *script;
28 int thread_idx;
29
30 CRYPTO_THREAD *t;
31 CRYPTO_MUTEX *m;
32 int testresult;
33 int done;
34};
35#endif
36
ed835673
HL
37typedef struct stream_info {
38 const char *name;
39 SSL *c_stream;
40 uint64_t s_stream_id;
41} STREAM_INFO;
42
43DEFINE_LHASH_OF_EX(STREAM_INFO);
44
45struct helper {
46 int s_fd;
e26dc8e3 47 BIO *s_net_bio, *s_net_bio_own, *s_qtf_wbio, *s_qtf_wbio_own;
ed835673
HL
48 BIO_ADDR *s_net_bio_addr;
49 QUIC_TSERVER *s;
50 LHASH_OF(STREAM_INFO) *s_streams;
51
52 int c_fd;
53 BIO *c_net_bio, *c_net_bio_own;
54 SSL_CTX *c_ctx;
55 SSL *c_conn;
56 LHASH_OF(STREAM_INFO) *c_streams;
57
a350db73
HL
58#if defined(OPENSSL_THREADS)
59 struct child_thread_args *threads;
60 size_t num_threads;
61#endif
62
ed835673 63 OSSL_TIME start_time;
693b23e3
HL
64
65 /*
66 * This is a duration recording the amount of time we have skipped forwards
67 * for testing purposes relative to the real ossl_time_now() clock. We add
68 * a quantity of time to this every time we skip some time.
69 */
70 CRYPTO_RWLOCK *time_lock;
71 OSSL_TIME time_slip; /* protected by time_lock */
72
e26dc8e3
HL
73 QTEST_FAULT *qtf;
74
9715e3aa 75 int init, blocking, check_spin_again;
e26dc8e3
HL
76 int free_order, need_injector;
77
78 int (*qtf_packet_plain_cb)(struct helper *h, QUIC_PKT_HDR *hdr,
79 unsigned char *buf, size_t buf_len);
80 uint64_t inject_word0, inject_word1;
ed835673
HL
81};
82
a350db73
HL
83struct helper_local {
84 struct helper *h;
85 LHASH_OF(STREAM_INFO) *c_streams;
86 int thread_idx;
87};
88
ed835673
HL
89struct script_op {
90 uint32_t op;
91 const void *arg0;
92 size_t arg1;
93 int (*check_func)(struct helper *h, const struct script_op *op);
94 const char *stream_name;
95 uint64_t arg2;
e26dc8e3
HL
96 int (*qtf_packet_plain_cb)(struct helper *h, QUIC_PKT_HDR *hdr,
97 unsigned char *buf, size_t buf_len);
ed835673
HL
98};
99
100#define OPK_END 0
101#define OPK_CHECK 1
102#define OPK_C_SET_ALPN 2
103#define OPK_C_CONNECT_WAIT 3
104#define OPK_C_WRITE 4
105#define OPK_S_WRITE 5
106#define OPK_C_READ_EXPECT 6
107#define OPK_S_READ_EXPECT 7
108#define OPK_C_EXPECT_FIN 8
109#define OPK_S_EXPECT_FIN 9
110#define OPK_C_CONCLUDE 10
111#define OPK_S_CONCLUDE 11
112#define OPK_C_DETACH 12
113#define OPK_C_ATTACH 13
114#define OPK_C_NEW_STREAM 14
115#define OPK_S_NEW_STREAM 15
a350db73 116#define OPK_C_ACCEPT_STREAM_WAIT 16
ed835673
HL
117#define OPK_C_ACCEPT_STREAM_NONE 17
118#define OPK_C_FREE_STREAM 18
119#define OPK_C_SET_DEFAULT_STREAM_MODE 19
83df44ae 120#define OPK_C_SET_INCOMING_STREAM_POLICY 20
ed835673
HL
121#define OPK_C_SHUTDOWN 21
122#define OPK_C_EXPECT_CONN_CLOSE_INFO 22
123#define OPK_S_EXPECT_CONN_CLOSE_INFO 23
124#define OPK_S_BIND_STREAM_ID 24
125#define OPK_C_WAIT_FOR_DATA 25
126#define OPK_C_WRITE_FAIL 26
127#define OPK_S_WRITE_FAIL 27
128#define OPK_C_READ_FAIL 28
129#define OPK_C_STREAM_RESET 29
a350db73
HL
130#define OPK_S_ACCEPT_STREAM_WAIT 30
131#define OPK_NEW_THREAD 31
fca44cfc
HL
132#define OPK_BEGIN_REPEAT 32
133#define OPK_END_REPEAT 33
134#define OPK_S_UNBIND_STREAM_ID 34
0345cac6
TM
135#define OPK_C_READ_FAIL_WAIT 35
136#define OPK_C_CLOSE_SOCKET 36
137#define OPK_C_EXPECT_SSL_ERR 37
138#define OPK_EXPECT_ERR_REASON 38
139#define OPK_EXPECT_ERR_LIB 39
140#define OPK_SLEEP 40
2f018d14 141#define OPK_S_READ_FAIL 41
e26dc8e3
HL
142#define OPK_S_SET_INJECT_PLAIN 42
143#define OPK_SET_INJECT_WORD 43
ed835673
HL
144
145#define EXPECT_CONN_CLOSE_APP (1U << 0)
146#define EXPECT_CONN_CLOSE_REMOTE (1U << 1)
147
148#define C_BIDI_ID(ordinal) \
149 (((ordinal) << 2) | QUIC_STREAM_INITIATOR_CLIENT | QUIC_STREAM_DIR_BIDI)
150#define S_BIDI_ID(ordinal) \
151 (((ordinal) << 2) | QUIC_STREAM_INITIATOR_SERVER | QUIC_STREAM_DIR_BIDI)
152#define C_UNI_ID(ordinal) \
153 (((ordinal) << 2) | QUIC_STREAM_INITIATOR_CLIENT | QUIC_STREAM_DIR_UNI)
154#define S_UNI_ID(ordinal) \
155 (((ordinal) << 2) | QUIC_STREAM_INITIATOR_SERVER | QUIC_STREAM_DIR_UNI)
156
a350db73
HL
157#define ANY_ID UINT64_MAX
158
ed835673
HL
159#define OP_END \
160 {OPK_END}
161#define OP_CHECK(func, arg2) \
162 {OPK_CHECK, NULL, 0, (func), NULL, (arg2)},
163#define OP_C_SET_ALPN(alpn) \
164 {OPK_C_SET_ALPN, (alpn), 0, NULL, NULL},
165#define OP_C_CONNECT_WAIT() \
166 {OPK_C_CONNECT_WAIT, NULL, 0, NULL, NULL},
e26dc8e3
HL
167#define OP_C_CONNECT_WAIT_OR_FAIL() \
168 {OPK_C_CONNECT_WAIT, NULL, 1, NULL, NULL},
ed835673
HL
169#define OP_C_WRITE(stream_name, buf, buf_len) \
170 {OPK_C_WRITE, (buf), (buf_len), NULL, #stream_name},
171#define OP_S_WRITE(stream_name, buf, buf_len) \
172 {OPK_S_WRITE, (buf), (buf_len), NULL, #stream_name},
173#define OP_C_READ_EXPECT(stream_name, buf, buf_len) \
174 {OPK_C_READ_EXPECT, (buf), (buf_len), NULL, #stream_name},
175#define OP_S_READ_EXPECT(stream_name, buf, buf_len) \
176 {OPK_S_READ_EXPECT, (buf), (buf_len), NULL, #stream_name},
177#define OP_C_EXPECT_FIN(stream_name) \
178 {OPK_C_EXPECT_FIN, NULL, 0, NULL, #stream_name},
179#define OP_S_EXPECT_FIN(stream_name) \
180 {OPK_S_EXPECT_FIN, NULL, 0, NULL, #stream_name},
181#define OP_C_CONCLUDE(stream_name) \
182 {OPK_C_CONCLUDE, NULL, 0, NULL, #stream_name},
183#define OP_S_CONCLUDE(stream_name) \
184 {OPK_S_CONCLUDE, NULL, 0, NULL, #stream_name},
185#define OP_C_DETACH(stream_name) \
186 {OPK_C_DETACH, NULL, 0, NULL, #stream_name},
187#define OP_C_ATTACH(stream_name) \
188 {OPK_C_ATTACH, NULL, 0, NULL, #stream_name},
189#define OP_C_NEW_STREAM_BIDI(stream_name, expect_id) \
190 {OPK_C_NEW_STREAM, NULL, 0, NULL, #stream_name, (expect_id)},
191#define OP_C_NEW_STREAM_UNI(stream_name, expect_id) \
192 {OPK_C_NEW_STREAM, NULL, 1, NULL, #stream_name, (expect_id)},
193#define OP_S_NEW_STREAM_BIDI(stream_name, expect_id) \
194 {OPK_S_NEW_STREAM, NULL, 0, NULL, #stream_name, (expect_id)},
195#define OP_S_NEW_STREAM_UNI(stream_name, expect_id) \
196 {OPK_S_NEW_STREAM, NULL, 1, NULL, #stream_name, (expect_id)},
a350db73
HL
197#define OP_C_ACCEPT_STREAM_WAIT(stream_name) \
198 {OPK_C_ACCEPT_STREAM_WAIT, NULL, 0, NULL, #stream_name},
ed835673
HL
199#define OP_C_ACCEPT_STREAM_NONE() \
200 {OPK_C_ACCEPT_STREAM_NONE, NULL, 0, NULL, NULL},
201#define OP_C_FREE_STREAM(stream_name) \
202 {OPK_C_FREE_STREAM, NULL, 0, NULL, #stream_name},
203#define OP_C_SET_DEFAULT_STREAM_MODE(mode) \
204 {OPK_C_SET_DEFAULT_STREAM_MODE, NULL, (mode), NULL, NULL},
83df44ae
HL
205#define OP_C_SET_INCOMING_STREAM_POLICY(policy) \
206 {OPK_C_SET_INCOMING_STREAM_POLICY, NULL, (policy), NULL, NULL},
ed835673
HL
207#define OP_C_SHUTDOWN() \
208 {OPK_C_SHUTDOWN, NULL, 0, NULL, NULL},
209#define OP_C_EXPECT_CONN_CLOSE_INFO(ec, app, remote) \
210 {OPK_C_EXPECT_CONN_CLOSE_INFO, NULL, \
211 ((app) ? EXPECT_CONN_CLOSE_APP : 0) | \
212 ((remote) ? EXPECT_CONN_CLOSE_REMOTE : 0), \
213 NULL, NULL, (ec)},
214#define OP_S_EXPECT_CONN_CLOSE_INFO(ec, app, remote) \
215 {OPK_S_EXPECT_CONN_CLOSE_INFO, NULL, \
216 ((app) ? EXPECT_CONN_CLOSE_APP : 0) | \
217 ((remote) ? EXPECT_CONN_CLOSE_REMOTE : 0), \
218 NULL, NULL, (ec)},
219#define OP_S_BIND_STREAM_ID(stream_name, stream_id) \
220 {OPK_S_BIND_STREAM_ID, NULL, 0, NULL, #stream_name, (stream_id)},
221#define OP_C_WAIT_FOR_DATA(stream_name) \
222 {OPK_C_WAIT_FOR_DATA, NULL, 0, NULL, #stream_name},
223#define OP_C_WRITE_FAIL(stream_name) \
224 {OPK_C_WRITE_FAIL, NULL, 0, NULL, #stream_name},
225#define OP_S_WRITE_FAIL(stream_name) \
226 {OPK_S_WRITE_FAIL, NULL, 0, NULL, #stream_name},
227#define OP_C_READ_FAIL(stream_name) \
228 {OPK_C_READ_FAIL, NULL, 0, NULL, #stream_name},
2f018d14
HL
229#define OP_S_READ_FAIL(stream_name) \
230 {OPK_S_READ_FAIL, NULL, 0, NULL, #stream_name},
ed835673
HL
231#define OP_C_STREAM_RESET(stream_name, aec) \
232 {OPK_C_STREAM_RESET, NULL, 0, NULL, #stream_name, (aec)},
a350db73
HL
233#define OP_S_ACCEPT_STREAM_WAIT(stream_name) \
234 {OPK_S_ACCEPT_STREAM_WAIT, NULL, 0, NULL, #stream_name},
235#define OP_NEW_THREAD(num_threads, script) \
fca44cfc
HL
236 {OPK_NEW_THREAD, (script), (num_threads), NULL, NULL, 0 },
237#define OP_BEGIN_REPEAT(n) \
238 {OPK_BEGIN_REPEAT, NULL, (n)},
239#define OP_END_REPEAT() \
240 {OPK_END_REPEAT},
241#define OP_S_UNBIND_STREAM_ID(stream_name) \
242 {OPK_S_UNBIND_STREAM_ID, NULL, 0, NULL, #stream_name},
0345cac6
TM
243#define OP_C_READ_FAIL_WAIT(stream_name) \
244 {OPK_C_READ_FAIL_WAIT, NULL, 0, NULL, #stream_name},
245#define OP_C_CLOSE_SOCKET() \
246 {OPK_C_CLOSE_SOCKET},
247#define OP_C_EXPECT_SSL_ERR(stream_name, err) \
248 {OPK_C_EXPECT_SSL_ERR, NULL, (err), NULL, #stream_name},
249#define OP_EXPECT_ERR_REASON(err) \
250 {OPK_EXPECT_ERR_REASON, NULL, (err)},
251#define OP_EXPECT_ERR_LIB(lib) \
252 {OPK_EXPECT_ERR_LIB, NULL, (lib)},
253#define OP_SLEEP(ms) \
254 {OPK_SLEEP, NULL, 0, NULL, NULL, (ms)},
e26dc8e3
HL
255#define OP_S_SET_INJECT_PLAIN(f) \
256 {OPK_S_SET_INJECT_PLAIN, NULL, 0, NULL, NULL, 0, (f)},
257#define OP_SET_INJECT_WORD(w0, w1) \
258 {OPK_SET_INJECT_WORD, NULL, (w0), NULL, NULL, (w1), NULL},
ed835673 259
693b23e3
HL
260static OSSL_TIME get_time(void *arg)
261{
262 struct helper *h = arg;
263 OSSL_TIME t;
264
265 if (!TEST_true(CRYPTO_THREAD_read_lock(h->time_lock)))
266 return ossl_time_zero();
267
268 t = ossl_time_add(ossl_time_now(), h->time_slip);
269
270 CRYPTO_THREAD_unlock(h->time_lock);
271 return t;
272}
273
274static int skip_time_ms(struct helper *h, const struct script_op *op)
275{
276 if (!TEST_true(CRYPTO_THREAD_write_lock(h->time_lock)))
277 return 0;
278
279 h->time_slip = ossl_time_add(h->time_slip, ossl_ms2time(op->arg2));
280
281 CRYPTO_THREAD_unlock(h->time_lock);
282 return 1;
283}
284
ed835673
HL
285static int check_rejected(struct helper *h, const struct script_op *op)
286{
287 uint64_t stream_id = op->arg2;
288
9715e3aa
HL
289 if (!ossl_quic_tserver_stream_has_peer_stop_sending(h->s, stream_id, NULL)
290 || !ossl_quic_tserver_stream_has_peer_reset_stream(h->s, stream_id, NULL)) {
291 h->check_spin_again = 1;
ed835673 292 return 0;
9715e3aa 293 }
ed835673
HL
294
295 return 1;
296}
297
298static int check_stream_reset(struct helper *h, const struct script_op *op)
299{
300 uint64_t stream_id = op->arg2, aec = 0;
301
9715e3aa
HL
302 if (!ossl_quic_tserver_stream_has_peer_reset_stream(h->s, stream_id, &aec)) {
303 h->check_spin_again = 1;
304 return 0;
305 }
306
307 return TEST_uint64_t_eq(aec, 42);
ed835673
HL
308}
309
310static int check_stream_stopped(struct helper *h, const struct script_op *op)
311{
312 uint64_t stream_id = op->arg2;
313
9715e3aa
HL
314 if (!ossl_quic_tserver_stream_has_peer_stop_sending(h->s, stream_id, NULL)) {
315 h->check_spin_again = 1;
316 return 0;
317 }
318
319 return 1;
ed835673
HL
320}
321
693b23e3
HL
322static int override_key_update(struct helper *h, const struct script_op *op)
323{
324 QUIC_CHANNEL *ch = ossl_quic_conn_get_channel(h->c_conn);
325
326 ossl_quic_channel_set_txku_threshold_override(ch, op->arg2);
327 return 1;
328}
329
2525109f
HL
330static int trigger_key_update(struct helper *h, const struct script_op *op)
331{
332 if (!TEST_true(SSL_key_update(h->c_conn, SSL_KEY_UPDATE_REQUESTED)))
333 return 0;
334
335 return 1;
336}
337
693b23e3
HL
338static int check_key_update_ge(struct helper *h, const struct script_op *op)
339{
340 QUIC_CHANNEL *ch = ossl_quic_conn_get_channel(h->c_conn);
341 int64_t txke = (int64_t)ossl_quic_channel_get_tx_key_epoch(ch);
342 int64_t rxke = (int64_t)ossl_quic_channel_get_rx_key_epoch(ch);
343 int64_t diff = txke - rxke;
344
345 /*
346 * TXKE must always be equal to or ahead of RXKE.
347 * It can be ahead of RXKE by at most 1.
348 */
349 if (!TEST_int64_t_ge(diff, 0) || !TEST_int64_t_le(diff, 1))
350 return 0;
351
352 /* Caller specifies a minimum number of RXKEs which must have happened. */
353 if (!TEST_uint64_t_ge((uint64_t)rxke, op->arg2))
354 return 0;
355
356 return 1;
357}
358
359static int check_key_update_lt(struct helper *h, const struct script_op *op)
360{
361 QUIC_CHANNEL *ch = ossl_quic_conn_get_channel(h->c_conn);
362 uint64_t txke = ossl_quic_channel_get_tx_key_epoch(ch);
363
364 /* Caller specifies a maximum number of TXKEs which must have happened. */
365 if (!TEST_uint64_t_lt(txke, op->arg2))
366 return 0;
367
368 return 1;
369}
370
ed835673
HL
371static unsigned long stream_info_hash(const STREAM_INFO *info)
372{
373 return OPENSSL_LH_strhash(info->name);
374}
375
376static int stream_info_cmp(const STREAM_INFO *a, const STREAM_INFO *b)
377{
378 return strcmp(a->name, b->name);
379}
380
381static void cleanup_stream(STREAM_INFO *info)
382{
383 SSL_free(info->c_stream);
384 OPENSSL_free(info);
385}
386
387static void helper_cleanup_streams(LHASH_OF(STREAM_INFO) **lh)
388{
389 if (*lh == NULL)
390 return;
391
392 lh_STREAM_INFO_doall(*lh, cleanup_stream);
393 lh_STREAM_INFO_free(*lh);
394 *lh = NULL;
395}
396
a350db73
HL
397#if defined(OPENSSL_THREADS)
398static CRYPTO_THREAD_RETVAL run_script_child_thread(void *arg);
399
400static int join_threads(struct child_thread_args *threads, size_t num_threads)
401{
402 int ok = 1;
403 size_t i;
404 CRYPTO_THREAD_RETVAL rv;
405
406 for (i = 0; i < num_threads; ++i) {
407 if (threads[i].t != NULL) {
408 ossl_crypto_thread_native_join(threads[i].t, &rv);
409
410 if (!threads[i].testresult)
411 /* Do not log failure here, worker will do it. */
412 ok = 0;
413
414 ossl_crypto_thread_native_clean(threads[i].t);
415 threads[i].t = NULL;
416 }
417
418 ossl_crypto_mutex_free(&threads[i].m);
419 }
420
421 return ok;
422}
423#endif
424
ed835673
HL
425static void helper_cleanup(struct helper *h)
426{
a350db73
HL
427#if defined(OPENSSL_THREADS)
428 join_threads(h->threads, h->num_threads);
429 OPENSSL_free(h->threads);
430 h->threads = NULL;
431 h->num_threads = 0;
432#endif
433
97f30fd5
HL
434 if (h->free_order == 0) {
435 /* order 0: streams, then conn */
436 helper_cleanup_streams(&h->c_streams);
437
438 SSL_free(h->c_conn);
439 h->c_conn = NULL;
440 } else {
441 /* order 1: conn, then streams */
442 SSL_free(h->c_conn);
443 h->c_conn = NULL;
444
445 helper_cleanup_streams(&h->c_streams);
446 }
ed835673 447
97f30fd5 448 helper_cleanup_streams(&h->s_streams);
ed835673
HL
449 ossl_quic_tserver_free(h->s);
450 h->s = NULL;
451
452 BIO_free(h->s_net_bio_own);
453 h->s_net_bio_own = NULL;
454
455 BIO_free(h->c_net_bio_own);
456 h->c_net_bio_own = NULL;
457
e26dc8e3
HL
458 BIO_free(h->s_qtf_wbio_own);
459 h->s_qtf_wbio_own = NULL;
460
7eebc354
HL
461 if (h->qtf != NULL) {
462 qtest_fault_free(h->qtf);
463 h->qtf = NULL;
464 }
465
ed835673
HL
466 if (h->s_fd >= 0) {
467 BIO_closesocket(h->s_fd);
468 h->s_fd = -1;
469 }
470
471 if (h->c_fd >= 0) {
472 BIO_closesocket(h->c_fd);
473 h->c_fd = -1;
474 }
475
476 BIO_ADDR_free(h->s_net_bio_addr);
477 h->s_net_bio_addr = NULL;
478
479 SSL_CTX_free(h->c_ctx);
480 h->c_ctx = NULL;
693b23e3
HL
481
482 CRYPTO_THREAD_lock_free(h->time_lock);
483 h->time_lock = NULL;
ed835673
HL
484}
485
e26dc8e3 486static int helper_init(struct helper *h, int free_order, int need_injector)
ed835673
HL
487{
488 short port = 8186;
489 struct in_addr ina = {0};
490 QUIC_TSERVER_ARGS s_args = {0};
491
492 memset(h, 0, sizeof(*h));
493 h->c_fd = -1;
494 h->s_fd = -1;
97f30fd5 495 h->free_order = free_order;
e26dc8e3 496 h->need_injector = need_injector;
693b23e3
HL
497 h->time_slip = ossl_time_zero();
498
499 if (!TEST_ptr(h->time_lock = CRYPTO_THREAD_lock_new()))
500 goto err;
ed835673
HL
501
502 if (!TEST_ptr(h->s_streams = lh_STREAM_INFO_new(stream_info_hash,
503 stream_info_cmp)))
504 goto err;
505
506 if (!TEST_ptr(h->c_streams = lh_STREAM_INFO_new(stream_info_hash,
507 stream_info_cmp)))
508 goto err;
509
510 ina.s_addr = htonl(0x7f000001UL);
511
512 h->s_fd = BIO_socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP, 0);
513 if (!TEST_int_ge(h->s_fd, 0))
514 goto err;
515
516 if (!TEST_true(BIO_socket_nbio(h->s_fd, 1)))
517 goto err;
518
519 if (!TEST_ptr(h->s_net_bio_addr = BIO_ADDR_new()))
520 goto err;
521
522 if (!TEST_true(BIO_ADDR_rawmake(h->s_net_bio_addr, AF_INET, &ina, sizeof(ina),
523 htons(port))))
524 goto err;
525
526 if (!TEST_true(BIO_bind(h->s_fd, h->s_net_bio_addr, 0)))
527 goto err;
528
529 if (!TEST_int_gt(BIO_ADDR_rawport(h->s_net_bio_addr), 0))
530 goto err;
531
532 if (!TEST_ptr(h->s_net_bio = h->s_net_bio_own = BIO_new_dgram(h->s_fd, 0)))
533 goto err;
534
535 if (!BIO_up_ref(h->s_net_bio))
536 goto err;
537
e26dc8e3
HL
538 if (need_injector) {
539 h->s_qtf_wbio = h->s_qtf_wbio_own = BIO_new(qtest_get_bio_method());
540 if (!TEST_ptr(h->s_qtf_wbio))
541 goto err;
542
543 if (!TEST_ptr(BIO_push(h->s_qtf_wbio, h->s_net_bio)))
544 goto err;
545
546 s_args.net_wbio = h->s_qtf_wbio;
547 } else {
548 s_args.net_wbio = h->s_net_bio;
549 }
550
693b23e3 551 s_args.net_rbio = h->s_net_bio;
37f27b91 552 s_args.alpn = NULL;
693b23e3
HL
553 s_args.now_cb = get_time;
554 s_args.now_cb_arg = h;
ed835673
HL
555
556 if (!TEST_ptr(h->s = ossl_quic_tserver_new(&s_args, certfile, keyfile)))
557 goto err;
558
e26dc8e3
HL
559 if (need_injector) {
560 h->qtf = qtest_create_injector(h->s);
561 if (!TEST_ptr(h->qtf))
562 goto err;
563
564 BIO_set_data(h->s_qtf_wbio, h->qtf);
565 }
566
7eebc354
HL
567 if (!need_injector)
568 h->s_net_bio_own = NULL;
569
e26dc8e3 570 h->s_qtf_wbio_own = NULL;
ed835673
HL
571
572 h->c_fd = BIO_socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP, 0);
573 if (!TEST_int_ge(h->c_fd, 0))
574 goto err;
575
576 if (!TEST_true(BIO_socket_nbio(h->c_fd, 1)))
577 goto err;
578
579 if (!TEST_ptr(h->c_net_bio = h->c_net_bio_own = BIO_new_dgram(h->c_fd, 0)))
580 goto err;
581
582 if (!TEST_true(BIO_dgram_set_peer(h->c_net_bio, h->s_net_bio_addr)))
583 goto err;
584
ed835673
HL
585 if (!TEST_ptr(h->c_ctx = SSL_CTX_new(OSSL_QUIC_client_method())))
586 goto err;
587
588 if (!TEST_ptr(h->c_conn = SSL_new(h->c_ctx)))
589 goto err;
590
693b23e3
HL
591 /* Use custom time function for virtual time skip. */
592 if (!TEST_true(ossl_quic_conn_set_override_now_cb(h->c_conn, get_time, h)))
593 goto err;
594
ed835673
HL
595 /* Takes ownership of our reference to the BIO. */
596 SSL_set0_rbio(h->c_conn, h->c_net_bio);
597 h->c_net_bio_own = NULL;
598
599 if (!TEST_true(BIO_up_ref(h->c_net_bio)))
600 goto err;
601
602 SSL_set0_wbio(h->c_conn, h->c_net_bio);
603
604 if (!TEST_true(SSL_set_blocking_mode(h->c_conn, 0)))
605 goto err;
606
607 h->start_time = ossl_time_now();
608 h->init = 1;
609 return 1;
610
611err:
612 helper_cleanup(h);
613 return 0;
614}
615
a350db73
HL
616static int helper_local_init(struct helper_local *hl, struct helper *h,
617 int thread_idx)
618{
619 hl->h = h;
620 hl->c_streams = NULL;
621 hl->thread_idx = thread_idx;
622
623 if (!TEST_ptr(h))
624 return 0;
625
626 if (thread_idx < 0) {
627 hl->c_streams = h->c_streams;
628 } else {
629 if (!TEST_ptr(hl->c_streams = lh_STREAM_INFO_new(stream_info_hash,
630 stream_info_cmp)))
631 return 0;
632 }
633
634 return 1;
635}
636
637static void helper_local_cleanup(struct helper_local *hl)
638{
639 if (hl->h == NULL)
640 return;
641
642 if (hl->thread_idx >= 0)
643 helper_cleanup_streams(&hl->c_streams);
644
645 hl->h = NULL;
646}
647
ed835673
HL
648static STREAM_INFO *get_stream_info(LHASH_OF(STREAM_INFO) *lh,
649 const char *stream_name)
650{
651 STREAM_INFO key, *info;
652
653 if (!TEST_ptr(stream_name))
654 return NULL;
655
656 if (!strcmp(stream_name, "DEFAULT"))
657 return NULL;
658
659 key.name = stream_name;
660 info = lh_STREAM_INFO_retrieve(lh, &key);
661 if (info == NULL) {
662 info = OPENSSL_zalloc(sizeof(*info));
663 if (info == NULL)
664 return NULL;
665
666 info->name = stream_name;
667 info->s_stream_id = UINT64_MAX;
668 lh_STREAM_INFO_insert(lh, info);
669 }
670
671 return info;
672}
673
a350db73
HL
674static int helper_local_set_c_stream(struct helper_local *hl,
675 const char *stream_name,
676 SSL *c_stream)
ed835673 677{
a350db73 678 STREAM_INFO *info = get_stream_info(hl->c_streams, stream_name);
ed835673
HL
679
680 if (info == NULL)
681 return 0;
682
683 info->c_stream = c_stream;
684 info->s_stream_id = UINT64_MAX;
685 return 1;
686}
687
a350db73
HL
688static SSL *helper_local_get_c_stream(struct helper_local *hl,
689 const char *stream_name)
ed835673
HL
690{
691 STREAM_INFO *info;
692
693 if (!strcmp(stream_name, "DEFAULT"))
a350db73 694 return hl->h->c_conn;
ed835673 695
a350db73 696 info = get_stream_info(hl->c_streams, stream_name);
ed835673
HL
697 if (info == NULL)
698 return NULL;
699
700 return info->c_stream;
701}
702
703static int
704helper_set_s_stream(struct helper *h, const char *stream_name,
705 uint64_t s_stream_id)
706{
707 STREAM_INFO *info;
708
709 if (!strcmp(stream_name, "DEFAULT"))
710 return 0;
711
712 info = get_stream_info(h->s_streams, stream_name);
713 if (info == NULL)
714 return 0;
715
716 info->c_stream = NULL;
717 info->s_stream_id = s_stream_id;
718 return 1;
719}
720
721static uint64_t helper_get_s_stream(struct helper *h, const char *stream_name)
722{
723 STREAM_INFO *info;
724
725 if (!strcmp(stream_name, "DEFAULT"))
726 return UINT64_MAX;
727
728 info = get_stream_info(h->s_streams, stream_name);
729 if (info == NULL)
730 return UINT64_MAX;
731
732 return info->s_stream_id;
733}
734
e26dc8e3
HL
735static int helper_packet_plain_listener(QTEST_FAULT *qtf, QUIC_PKT_HDR *hdr,
736 unsigned char *buf, size_t buf_len,
737 void *arg)
738{
739 struct helper *h = arg;
740
741 return h->qtf_packet_plain_cb(h, hdr, buf, buf_len);
742}
743
ed835673
HL
744static int is_want(SSL *s, int ret)
745{
746 int ec = SSL_get_error(s, ret);
747
748 return ec == SSL_ERROR_WANT_READ || ec == SSL_ERROR_WANT_WRITE;
749}
750
a350db73
HL
751static int run_script_worker(struct helper *h, const struct script_op *script,
752 int thread_idx)
ed835673 753{
ed835673 754 int testresult = 0;
ed835673
HL
755 unsigned char *tmp_buf = NULL;
756 int connect_started = 0;
9715e3aa 757 size_t offset = 0;
a350db73
HL
758 size_t op_idx = 0;
759 const struct script_op *op = NULL;
629b408c
HL
760 int no_advance = 0, first = 1;
761#if defined(OPENSSL_THREADS)
762 int end_wait_warning = 0;
763#endif
a350db73
HL
764 OSSL_TIME op_start_time = ossl_time_zero(), op_deadline = ossl_time_zero();
765 struct helper_local hl;
fca44cfc
HL
766#define REPEAT_SLOTS 8
767 size_t repeat_stack_idx[REPEAT_SLOTS], repeat_stack_done[REPEAT_SLOTS];
768 size_t repeat_stack_limit[REPEAT_SLOTS];
769 size_t repeat_stack_len = 0;
ed835673 770
a350db73 771 if (!TEST_true(helper_local_init(&hl, h, thread_idx)))
ed835673
HL
772 goto out;
773
2525109f 774#define SPIN_AGAIN() { OSSL_sleep(1); no_advance = 1; continue; }
ed835673 775
9715e3aa 776 for (;;) {
a350db73 777 SSL *c_tgt = h->c_conn;
ed835673
HL
778 uint64_t s_stream_id = UINT64_MAX;
779
9715e3aa
HL
780 if (no_advance) {
781 no_advance = 0;
782 } else {
783 if (!first)
784 ++op_idx;
785
786 first = 0;
4f2d32d6 787 offset = 0;
9715e3aa
HL
788 op_start_time = ossl_time_now();
789 op_deadline = ossl_time_add(op_start_time, ossl_ms2time(2000));
790 }
791
792 if (!TEST_int_le(ossl_time_compare(ossl_time_now(), op_deadline), 0)) {
a350db73 793 TEST_error("op %zu timed out on thread %d", op_idx + 1, thread_idx);
9715e3aa
HL
794 goto out;
795 }
796
ed835673
HL
797 op = &script[op_idx];
798
799 if (op->stream_name != NULL) {
a350db73
HL
800 c_tgt = helper_local_get_c_stream(&hl, op->stream_name);
801 if (thread_idx < 0)
802 s_stream_id = helper_get_s_stream(h, op->stream_name);
803 else
804 s_stream_id = UINT64_MAX;
805 }
806
7ba8f79a 807 if (thread_idx < 0)
a350db73 808 ossl_quic_tserver_tick(h->s);
7ba8f79a
HL
809
810 if (thread_idx >= 0 || connect_started)
6084e04b 811 SSL_handle_events(h->c_conn);
ed835673 812
a350db73
HL
813 if (thread_idx >= 0) {
814 /* Only allow certain opcodes on child threads. */
815 switch (op->op) {
816 case OPK_END:
817 case OPK_C_ACCEPT_STREAM_WAIT:
818 case OPK_C_NEW_STREAM:
819 case OPK_C_READ_EXPECT:
820 case OPK_C_EXPECT_FIN:
821 case OPK_C_WRITE:
822 case OPK_C_CONCLUDE:
823 case OPK_C_FREE_STREAM:
fca44cfc
HL
824 case OPK_BEGIN_REPEAT:
825 case OPK_END_REPEAT:
0345cac6
TM
826 case OPK_C_READ_FAIL_WAIT:
827 case OPK_C_EXPECT_SSL_ERR:
828 case OPK_EXPECT_ERR_REASON:
829 case OPK_EXPECT_ERR_LIB:
830 case OPK_SLEEP:
a350db73
HL
831 break;
832
833 default:
0cea6df2
MC
834 TEST_error("opcode %lu not allowed on child thread",
835 (unsigned long)op->op);
a350db73
HL
836 goto out;
837 }
838 }
ed835673
HL
839
840 switch (op->op) {
841 case OPK_END:
fca44cfc
HL
842 if (!TEST_size_t_eq(repeat_stack_len, 0))
843 goto out;
844
629b408c 845#if defined(OPENSSL_THREADS)
a350db73
HL
846 if (thread_idx < 0) {
847 int done;
848 size_t i;
849
850 for (i = 0; i < h->num_threads; ++i) {
851 if (h->threads[i].m == NULL)
852 continue;
853
854 ossl_crypto_mutex_lock(h->threads[i].m);
855 done = h->threads[i].done;
856 ossl_crypto_mutex_unlock(h->threads[i].m);
857
858 if (!done) {
859 if (!end_wait_warning) {
860 TEST_info("still waiting for other threads to finish (%zu)", i);
861 end_wait_warning = 1;
862 }
863
864 SPIN_AGAIN();
865 }
866 }
867 }
629b408c 868#endif
a350db73
HL
869
870 TEST_info("script finished on thread %d", thread_idx);
ed835673
HL
871 testresult = 1;
872 goto out;
873
fca44cfc
HL
874 case OPK_BEGIN_REPEAT:
875 if (!TEST_size_t_lt(repeat_stack_len, OSSL_NELEM(repeat_stack_idx)))
876 goto out;
877
878 if (!TEST_size_t_gt(op->arg1, 0))
879 goto out;
880
881 repeat_stack_idx[repeat_stack_len] = op_idx + 1;
882 repeat_stack_done[repeat_stack_len] = 0;
883 repeat_stack_limit[repeat_stack_len] = op->arg1;
884 ++repeat_stack_len;
885 break;
886
887 case OPK_END_REPEAT:
888 if (!TEST_size_t_gt(repeat_stack_len, 0))
889 goto out;
890
891 if (++repeat_stack_done[repeat_stack_len - 1]
892 == repeat_stack_limit[repeat_stack_len - 1]) {
893 --repeat_stack_len;
894 } else {
895 op_idx = repeat_stack_idx[repeat_stack_len - 1];
896 no_advance = 1;
897 continue;
898 }
899
900 break;
901
ed835673 902 case OPK_CHECK:
9715e3aa 903 {
a350db73
HL
904 int ok = op->check_func(h, op);
905 if (h->check_spin_again) {
906 h->check_spin_again = 0;
9715e3aa
HL
907 SPIN_AGAIN();
908 }
ed835673 909
9715e3aa
HL
910 if (!TEST_true(ok))
911 goto out;
912 }
ed835673
HL
913 break;
914
915 case OPK_C_SET_ALPN:
916 {
917 const char *alpn = op->arg0;
918 size_t alpn_len = strlen(alpn);
919
920 if (!TEST_size_t_le(alpn_len, UINT8_MAX)
921 || !TEST_ptr(tmp_buf = (unsigned char *)OPENSSL_malloc(alpn_len + 1)))
922 goto out;
923
924 memcpy(tmp_buf + 1, alpn, alpn_len);
925 tmp_buf[0] = (unsigned char)alpn_len;
926
927 /* 0 is the success case for SSL_set_alpn_protos(). */
a350db73 928 if (!TEST_false(SSL_set_alpn_protos(h->c_conn, tmp_buf,
ed835673
HL
929 alpn_len + 1)))
930 goto out;
931
932 OPENSSL_free(tmp_buf);
933 tmp_buf = NULL;
934 }
935 break;
936
937 case OPK_C_CONNECT_WAIT:
938 {
939 int ret;
940
941 connect_started = 1;
942
a350db73 943 ret = SSL_connect(h->c_conn);
e26dc8e3 944 if (!TEST_true((ret == 1 || op->arg1 > 0)
a350db73 945 || (!h->blocking && is_want(h->c_conn, ret))))
ed835673
HL
946 goto out;
947
e26dc8e3 948 if (!h->blocking && ret < 0)
ed835673
HL
949 SPIN_AGAIN();
950 }
951 break;
952
953 case OPK_C_WRITE:
954 {
955 size_t bytes_written = 0;
956
957 if (!TEST_ptr(c_tgt))
958 goto out;
959
960 if (!TEST_true(SSL_write_ex(c_tgt, op->arg0, op->arg1,
961 &bytes_written))
962 || !TEST_size_t_eq(bytes_written, op->arg1))
963 goto out;
964 }
965 break;
966
967 case OPK_S_WRITE:
968 {
969 size_t bytes_written = 0;
970
971 if (!TEST_uint64_t_ne(s_stream_id, UINT64_MAX))
972 goto out;
973
a350db73 974 if (!TEST_true(ossl_quic_tserver_write(h->s, s_stream_id,
ed835673
HL
975 op->arg0, op->arg1,
976 &bytes_written))
977 || !TEST_size_t_eq(bytes_written, op->arg1))
978 goto out;
979 }
980 break;
981
982 case OPK_C_CONCLUDE:
983 {
984 if (!TEST_true(SSL_stream_conclude(c_tgt, 0)))
985 goto out;
986 }
987 break;
988
989 case OPK_S_CONCLUDE:
990 {
991 if (!TEST_uint64_t_ne(s_stream_id, UINT64_MAX))
992 goto out;
993
a350db73 994 ossl_quic_tserver_conclude(h->s, s_stream_id);
ed835673
HL
995 }
996 break;
997
998 case OPK_C_WAIT_FOR_DATA:
999 {
1000 char buf[1];
1001 size_t bytes_read = 0;
1002
1003 if (!TEST_ptr(c_tgt))
1004 goto out;
1005
1006 if (!SSL_peek_ex(c_tgt, buf, sizeof(buf), &bytes_read)
1007 || bytes_read == 0)
1008 SPIN_AGAIN();
1009 }
1010 break;
1011
1012 case OPK_C_READ_EXPECT:
1013 {
1014 size_t bytes_read = 0;
1015
9715e3aa
HL
1016 if (op->arg1 > 0 && tmp_buf == NULL
1017 && !TEST_ptr(tmp_buf = OPENSSL_malloc(op->arg1)))
ed835673
HL
1018 goto out;
1019
9715e3aa
HL
1020 if (!SSL_read_ex(c_tgt, tmp_buf + offset, op->arg1 - offset,
1021 &bytes_read))
1022 SPIN_AGAIN();
ed835673 1023
9715e3aa
HL
1024 if (bytes_read + offset != op->arg1) {
1025 offset += bytes_read;
1026 SPIN_AGAIN();
1027 }
1028
1029 if (op->arg1 > 0
1030 && !TEST_mem_eq(tmp_buf, op->arg1, op->arg0, op->arg1))
ed835673
HL
1031 goto out;
1032
1033 OPENSSL_free(tmp_buf);
1034 tmp_buf = NULL;
1035 }
1036 break;
1037
1038 case OPK_S_READ_EXPECT:
1039 {
1040 size_t bytes_read = 0;
1041
1042 if (!TEST_uint64_t_ne(s_stream_id, UINT64_MAX))
1043 goto out;
1044
9715e3aa 1045 if (op->arg1 > 0 && tmp_buf == NULL
ed835673
HL
1046 && !TEST_ptr(tmp_buf = OPENSSL_malloc(op->arg1)))
1047 goto out;
1048
a350db73 1049 if (!TEST_true(ossl_quic_tserver_read(h->s, s_stream_id,
9715e3aa
HL
1050 tmp_buf + offset,
1051 op->arg1 - offset,
1052 &bytes_read)))
ed835673
HL
1053 goto out;
1054
9715e3aa
HL
1055 if (bytes_read + offset != op->arg1) {
1056 offset += bytes_read;
1057 SPIN_AGAIN();
1058 }
1059
ed835673
HL
1060 if (op->arg1 > 0
1061 && !TEST_mem_eq(tmp_buf, op->arg1, op->arg0, op->arg1))
1062 goto out;
1063
1064 OPENSSL_free(tmp_buf);
1065 tmp_buf = NULL;
1066 }
1067 break;
1068
1069 case OPK_C_EXPECT_FIN:
1070 {
1071 char buf[1];
1072 size_t bytes_read = 0;
1073
1074 if (!TEST_false(SSL_read_ex(c_tgt, buf, sizeof(buf),
1075 &bytes_read))
9715e3aa
HL
1076 || !TEST_size_t_eq(bytes_read, 0))
1077 goto out;
1078
1079 if (is_want(c_tgt, 0))
1080 SPIN_AGAIN();
1081
1082 if (!TEST_int_eq(SSL_get_error(c_tgt, 0),
1083 SSL_ERROR_ZERO_RETURN))
1084 goto out;
ed835673
HL
1085 }
1086 break;
1087
1088 case OPK_S_EXPECT_FIN:
1089 {
9715e3aa 1090 if (!TEST_uint64_t_ne(s_stream_id, UINT64_MAX))
ed835673 1091 goto out;
9715e3aa 1092
a350db73 1093 if (!ossl_quic_tserver_has_read_ended(h->s, s_stream_id))
9715e3aa 1094 SPIN_AGAIN();
ed835673
HL
1095 }
1096 break;
1097
1098 case OPK_C_DETACH:
1099 {
1100 SSL *c_stream;
1101
1102 if (!TEST_ptr_null(c_tgt))
1103 goto out; /* don't overwrite existing stream with same name */
1104
a350db73 1105 if (!TEST_ptr(c_stream = ossl_quic_detach_stream(h->c_conn)))
ed835673
HL
1106 goto out;
1107
a350db73 1108 if (!TEST_true(helper_local_set_c_stream(&hl, op->stream_name, c_stream)))
ed835673
HL
1109 goto out;
1110 }
1111 break;
1112
1113 case OPK_C_ATTACH:
1114 {
1115 if (!TEST_ptr(c_tgt))
1116 goto out;
1117
a350db73 1118 if (!TEST_true(ossl_quic_attach_stream(h->c_conn, c_tgt)))
ed835673
HL
1119 goto out;
1120
a350db73 1121 if (!TEST_true(helper_local_set_c_stream(&hl, op->stream_name, NULL)))
ed835673
HL
1122 goto out;
1123 }
1124 break;
1125
1126 case OPK_C_NEW_STREAM:
1127 {
1128 SSL *c_stream;
1129 uint64_t flags = 0;
1130
1131 if (!TEST_ptr_null(c_tgt))
1132 goto out; /* don't overwrite existing stream with same name */
1133
1134 if (op->arg1 != 0)
1135 flags |= SSL_STREAM_FLAG_UNI;
1136
a350db73 1137 if (!TEST_ptr(c_stream = SSL_new_stream(h->c_conn, flags)))
ed835673
HL
1138 goto out;
1139
1140 if (op->arg2 != UINT64_MAX
1141 && !TEST_uint64_t_eq(SSL_get_stream_id(c_stream),
1142 op->arg2))
1143 goto out;
1144
a350db73 1145 if (!TEST_true(helper_local_set_c_stream(&hl, op->stream_name, c_stream)))
ed835673
HL
1146 goto out;
1147 }
1148 break;
1149
1150 case OPK_S_NEW_STREAM:
1151 {
1152 uint64_t stream_id = UINT64_MAX;
1153
1154 if (!TEST_uint64_t_eq(s_stream_id, UINT64_MAX))
1155 goto out; /* don't overwrite existing stream with same name */
1156
a350db73 1157 if (!TEST_true(ossl_quic_tserver_stream_new(h->s,
ed835673
HL
1158 op->arg1 > 0,
1159 &stream_id)))
1160 goto out;
1161
1162 if (op->arg2 != UINT64_MAX
1163 && !TEST_uint64_t_eq(stream_id, op->arg2))
1164 goto out;
1165
a350db73 1166 if (!TEST_true(helper_set_s_stream(h, op->stream_name,
ed835673
HL
1167 stream_id)))
1168 goto out;
1169 }
1170 break;
1171
a350db73 1172 case OPK_C_ACCEPT_STREAM_WAIT:
ed835673
HL
1173 {
1174 SSL *c_stream;
1175
1176 if (!TEST_ptr_null(c_tgt))
1177 goto out; /* don't overwrite existing stream with same name */
1178
a350db73 1179 if ((c_stream = SSL_accept_stream(h->c_conn, 0)) == NULL)
9715e3aa 1180 SPIN_AGAIN();
ed835673 1181
a350db73
HL
1182 if (!TEST_true(helper_local_set_c_stream(&hl, op->stream_name,
1183 c_stream)))
1184 goto out;
1185 }
1186 break;
1187
1188 case OPK_S_ACCEPT_STREAM_WAIT:
1189 {
1190 uint64_t new_stream_id;
1191
1192 if (!TEST_uint64_t_eq(s_stream_id, UINT64_MAX))
1193 goto out;
1194
1195 new_stream_id = ossl_quic_tserver_pop_incoming_stream(h->s);
1196 if (new_stream_id == UINT64_MAX)
1197 SPIN_AGAIN();
1198
1199 if (!TEST_true(helper_set_s_stream(h, op->stream_name, new_stream_id)))
ed835673
HL
1200 goto out;
1201 }
1202 break;
1203
1204 case OPK_C_ACCEPT_STREAM_NONE:
1205 {
1206 SSL *c_stream;
1207
a350db73 1208 if (!TEST_ptr_null(c_stream = SSL_accept_stream(h->c_conn, 0))) {
ed835673
HL
1209 SSL_free(c_stream);
1210 goto out;
1211 }
1212 }
1213 break;
1214
1215 case OPK_C_FREE_STREAM:
1216 {
1217 if (!TEST_ptr(c_tgt)
1218 || !TEST_true(!SSL_is_connection(c_tgt)))
1219 goto out;
1220
a350db73 1221 if (!TEST_true(helper_local_set_c_stream(&hl, op->stream_name, NULL)))
ed835673
HL
1222 goto out;
1223
1224 SSL_free(c_tgt);
1225 c_tgt = NULL;
1226 }
1227 break;
1228
1229 case OPK_C_SET_DEFAULT_STREAM_MODE:
1230 {
1231 if (!TEST_ptr(c_tgt))
1232 goto out;
1233
1234 if (!TEST_true(SSL_set_default_stream_mode(c_tgt, op->arg1)))
1235 goto out;
1236 }
1237 break;
1238
83df44ae 1239 case OPK_C_SET_INCOMING_STREAM_POLICY:
ed835673
HL
1240 {
1241 if (!TEST_ptr(c_tgt))
1242 goto out;
1243
83df44ae
HL
1244 if (!TEST_true(SSL_set_incoming_stream_policy(c_tgt,
1245 op->arg1, 0)))
ed835673
HL
1246 goto out;
1247 }
1248 break;
1249
1250 case OPK_C_SHUTDOWN:
1251 {
1252 int ret;
1253
1254 if (!TEST_ptr(c_tgt))
1255 goto out;
1256
1257 ret = SSL_shutdown_ex(c_tgt, 0, NULL, 0);
1258 if (!TEST_int_ge(ret, 0))
1259 goto out;
1260
1261 }
1262 break;
1263
1264 case OPK_C_EXPECT_CONN_CLOSE_INFO:
1265 {
1266 SSL_CONN_CLOSE_INFO cc_info = {0};
1267 int expect_app = (op->arg1 & EXPECT_CONN_CLOSE_APP) != 0;
1268 int expect_remote = (op->arg1 & EXPECT_CONN_CLOSE_REMOTE) != 0;
1269 uint64_t error_code = op->arg2;
1270
1271 if (!TEST_ptr(c_tgt))
1272 goto out;
1273
9715e3aa
HL
1274 if (!SSL_get_conn_close_info(c_tgt, &cc_info, sizeof(cc_info)))
1275 SPIN_AGAIN();
ed835673
HL
1276
1277 if (!TEST_int_eq(expect_app, !cc_info.is_transport)
1278 || !TEST_int_eq(expect_remote, !cc_info.is_local)
1279 || !TEST_uint64_t_eq(error_code, cc_info.error_code))
1280 goto out;
1281 }
1282 break;
1283
1284 case OPK_S_EXPECT_CONN_CLOSE_INFO:
1285 {
1286 const QUIC_TERMINATE_CAUSE *tc;
1287 int expect_app = (op->arg1 & EXPECT_CONN_CLOSE_APP) != 0;
1288 int expect_remote = (op->arg1 & EXPECT_CONN_CLOSE_REMOTE) != 0;
1289 uint64_t error_code = op->arg2;
1290
a350db73 1291 if (!ossl_quic_tserver_is_term_any(h->s))
9715e3aa 1292 SPIN_AGAIN();
ed835673 1293
a350db73 1294 if (!TEST_ptr(tc = ossl_quic_tserver_get_terminate_cause(h->s)))
ed835673
HL
1295 goto out;
1296
1297 if (!TEST_uint64_t_eq(error_code, tc->error_code)
1298 || !TEST_int_eq(expect_app, tc->app)
1299 || !TEST_int_eq(expect_remote, tc->remote))
1300 goto out;
1301 }
1302 break;
1303
1304 case OPK_S_BIND_STREAM_ID:
1305 {
1306 if (!TEST_uint64_t_eq(s_stream_id, UINT64_MAX))
1307 goto out;
1308
a350db73 1309 if (!TEST_true(helper_set_s_stream(h, op->stream_name, op->arg2)))
ed835673
HL
1310 goto out;
1311 }
1312 break;
1313
fca44cfc
HL
1314 case OPK_S_UNBIND_STREAM_ID:
1315 {
1316 if (!TEST_uint64_t_ne(s_stream_id, UINT64_MAX))
1317 goto out;
1318
1319 if (!TEST_true(helper_set_s_stream(h, op->stream_name, UINT64_MAX)))
1320 goto out;
1321 }
1322 break;
1323
ed835673
HL
1324 case OPK_C_WRITE_FAIL:
1325 {
571aff4b 1326 size_t bytes_written = 0;
ed835673
HL
1327
1328 if (!TEST_ptr(c_tgt))
1329 goto out;
1330
1331 if (!TEST_false(SSL_write_ex(c_tgt, "apple", 5, &bytes_written)))
1332 goto out;
1333 }
1334 break;
1335
1336 case OPK_S_WRITE_FAIL:
1337 {
1338 size_t bytes_written = 0;
1339
1340 if (!TEST_uint64_t_ne(s_stream_id, UINT64_MAX))
1341 goto out;
1342
a350db73 1343 if (!TEST_false(ossl_quic_tserver_write(h->s, s_stream_id,
ed835673
HL
1344 (const unsigned char *)"apple", 5,
1345 &bytes_written)))
1346 goto out;
1347 }
1348 break;
1349
1350 case OPK_C_READ_FAIL:
1351 {
571aff4b 1352 size_t bytes_read = 0;
ed835673
HL
1353 char buf[1];
1354
1355 if (!TEST_ptr(c_tgt))
1356 goto out;
1357
1358 if (!TEST_false(SSL_read_ex(c_tgt, buf, sizeof(buf), &bytes_read)))
1359 goto out;
1360 }
1361 break;
1362
0345cac6
TM
1363 case OPK_C_READ_FAIL_WAIT:
1364 {
1365 size_t bytes_read = 0;
1366 char buf[1];
1367
1368 if (!TEST_ptr(c_tgt))
1369 goto out;
1370
1371 if (!TEST_false(SSL_read_ex(c_tgt, buf, sizeof(buf), &bytes_read)))
1372 goto out;
1373
1374 if (is_want(c_tgt, 0))
1375 SPIN_AGAIN();
1376 }
1377 break;
1378
2f018d14
HL
1379 case OPK_S_READ_FAIL:
1380 {
1381 size_t bytes_read = 0;
1382 unsigned char buf[1];
1383
1384 if (!TEST_uint64_t_ne(s_stream_id, UINT64_MAX))
1385 goto out;
1386
1387 if (!TEST_false(ossl_quic_tserver_read(h->s, s_stream_id,
1388 buf, sizeof(buf),
1389 &bytes_read)))
1390 goto out;
1391 }
1392 break;
1393
ed835673
HL
1394 case OPK_C_STREAM_RESET:
1395 {
1396 SSL_STREAM_RESET_ARGS args = {0};
1397
1398 if (!TEST_ptr(c_tgt))
1399 goto out;
1400
1401 args.quic_error_code = op->arg2;
1402
1403 if (!TEST_true(SSL_stream_reset(c_tgt, &args, sizeof(args))))
1404 goto out;
1405 }
1406 break;
1407
a350db73
HL
1408 case OPK_NEW_THREAD:
1409 {
1410#if !defined(OPENSSL_THREADS)
629b408c
HL
1411 /*
1412 * If this test script requires threading and we do not have
1413 * support for it, skip the rest of it.
1414 */
1415 TEST_skip("threading not supported, skipping");
1416 testresult = 1;
a350db73
HL
1417 goto out;
1418#else
1419 size_t i;
1420
1421 if (!TEST_ptr_null(h->threads)) {
1422 TEST_error("max one NEW_THREAD operation per script");
1423 goto out;
1424 }
1425
1426 h->threads = OPENSSL_zalloc(op->arg1 * sizeof(struct child_thread_args));
1427 if (!TEST_ptr(h->threads))
1428 goto out;
1429
1430 h->num_threads = op->arg1;
1431
1432 for (i = 0; i < op->arg1; ++i) {
1433 h->threads[i].h = h;
1434 h->threads[i].script = op->arg0;
1435 h->threads[i].thread_idx = i;
1436
1437 h->threads[i].m = ossl_crypto_mutex_new();
1438 if (!TEST_ptr(h->threads[i].m))
1439 goto out;
1440
1441 h->threads[i].t
1442 = ossl_crypto_thread_native_start(run_script_child_thread,
1443 &h->threads[i], 1);
1444 if (!TEST_ptr(h->threads[i].t))
1445 goto out;
1446 }
1447#endif
1448 }
1449 break;
1450
0345cac6
TM
1451 case OPK_C_CLOSE_SOCKET:
1452 {
1453 BIO_closesocket(h->c_fd);
1454 }
1455 break;
1456
1457 case OPK_C_EXPECT_SSL_ERR:
1458 {
1459 if (!TEST_size_t_eq((size_t)SSL_get_error(c_tgt, 0), op->arg1))
1460 goto out;
1461 }
1462 break;
1463
1464 case OPK_EXPECT_ERR_REASON:
1465 {
1466 if (!TEST_size_t_eq((size_t)ERR_GET_REASON(ERR_get_error()), op->arg1))
1467 goto out;
1468 }
1469 break;
1470
1471 case OPK_EXPECT_ERR_LIB:
1472 {
1473 if (!TEST_size_t_eq((size_t)ERR_GET_LIB(ERR_get_error()), op->arg1))
1474 goto out;
1475 }
1476 break;
1477
1478 case OPK_SLEEP:
1479 {
1480 OSSL_sleep(op->arg2);
1481 }
1482 break;
1483
e26dc8e3
HL
1484 case OPK_S_SET_INJECT_PLAIN:
1485 h->qtf_packet_plain_cb = op->qtf_packet_plain_cb;
1486
1487 if (!TEST_true(qtest_fault_set_packet_plain_listener(h->qtf,
1488 h->qtf_packet_plain_cb != NULL ?
1489 helper_packet_plain_listener : NULL,
1490 h)))
1491 goto out;
1492
1493 break;
1494
1495 case OPK_SET_INJECT_WORD:
1496 h->inject_word0 = op->arg1;
1497 h->inject_word1 = op->arg2;
1498 break;
1499
ed835673
HL
1500 default:
1501 TEST_error("unknown op");
1502 goto out;
1503 }
1504 }
1505
1506out:
fca44cfc
HL
1507 if (!testresult) {
1508 size_t i;
1509
a350db73
HL
1510 TEST_error("failed at script op %zu, thread %d\n",
1511 op_idx + 1, thread_idx);
ed835673 1512
fca44cfc
HL
1513 for (i = 0; i < repeat_stack_len; ++i)
1514 TEST_info("while repeating, iteration %zu of %zu, starting at script op %zu",
1515 repeat_stack_done[i],
1516 repeat_stack_limit[i],
1517 repeat_stack_idx[i]);
1518 }
1519
ed835673 1520 OPENSSL_free(tmp_buf);
a350db73
HL
1521 helper_local_cleanup(&hl);
1522 return testresult;
1523}
1524
1525static int run_script(const struct script_op *script, int free_order)
1526{
1527 int testresult = 0;
1528 struct helper h;
1529
e26dc8e3 1530 if (!TEST_true(helper_init(&h, free_order, 1)))
a350db73
HL
1531 goto out;
1532
1533 if (!TEST_true(run_script_worker(&h, script, -1)))
1534 goto out;
1535
629b408c 1536#if defined(OPENSSL_THREADS)
a350db73
HL
1537 if (!TEST_true(join_threads(h.threads, h.num_threads)))
1538 goto out;
629b408c 1539#endif
a350db73
HL
1540
1541 testresult = 1;
1542out:
ed835673
HL
1543 helper_cleanup(&h);
1544 return testresult;
1545}
1546
a350db73
HL
1547#if defined(OPENSSL_THREADS)
1548static CRYPTO_THREAD_RETVAL run_script_child_thread(void *arg)
1549{
1550 int testresult;
1551 struct child_thread_args *args = arg;
1552
1553 testresult = run_script_worker(args->h, args->script,
1554 args->thread_idx);
1555
1556 ossl_crypto_mutex_lock(args->m);
1557 args->testresult = testresult;
1558 args->done = 1;
1559 ossl_crypto_mutex_unlock(args->m);
1560 return 1;
1561}
1562#endif
1563
ed835673
HL
1564/* 1. Simple single-stream test */
1565static const struct script_op script_1[] = {
1566 OP_C_SET_ALPN ("ossltest")
1567 OP_C_CONNECT_WAIT ()
1568 OP_C_WRITE (DEFAULT, "apple", 5)
1569 OP_C_CONCLUDE (DEFAULT)
1570 OP_S_BIND_STREAM_ID (a, C_BIDI_ID(0))
1571 OP_S_READ_EXPECT (a, "apple", 5)
1572 OP_S_EXPECT_FIN (a)
1573 OP_S_WRITE (a, "orange", 6)
1574 OP_S_CONCLUDE (a)
1575 OP_C_READ_EXPECT (DEFAULT, "orange", 6)
1576 OP_C_EXPECT_FIN (DEFAULT)
1577 OP_END
1578};
1579
1580/* 2. Multi-stream test */
1581static const struct script_op script_2[] = {
1582 OP_C_SET_ALPN ("ossltest")
1583 OP_C_CONNECT_WAIT ()
83df44ae 1584 OP_C_SET_INCOMING_STREAM_POLICY(SSL_INCOMING_STREAM_POLICY_ACCEPT)
ed835673
HL
1585 OP_C_WRITE (DEFAULT, "apple", 5)
1586 OP_S_BIND_STREAM_ID (a, C_BIDI_ID(0))
1587 OP_S_READ_EXPECT (a, "apple", 5)
1588 OP_S_WRITE (a, "orange", 6)
1589 OP_C_READ_EXPECT (DEFAULT, "orange", 6)
1590
1591 OP_C_NEW_STREAM_BIDI (b, C_BIDI_ID(1))
1592 OP_C_WRITE (b, "flamingo", 8)
1593 OP_C_CONCLUDE (b)
1594 OP_S_BIND_STREAM_ID (b, C_BIDI_ID(1))
1595 OP_S_READ_EXPECT (b, "flamingo", 8)
1596 OP_S_EXPECT_FIN (b)
1597 OP_S_WRITE (b, "gargoyle", 8)
1598 OP_S_CONCLUDE (b)
1599 OP_C_READ_EXPECT (b, "gargoyle", 8)
1600 OP_C_EXPECT_FIN (b)
1601
1602 OP_C_NEW_STREAM_UNI (c, C_UNI_ID(0))
1603 OP_C_WRITE (c, "elephant", 8)
1604 OP_C_CONCLUDE (c)
1605 OP_S_BIND_STREAM_ID (c, C_UNI_ID(0))
1606 OP_S_READ_EXPECT (c, "elephant", 8)
1607 OP_S_EXPECT_FIN (c)
1608 OP_S_WRITE_FAIL (c)
1609
1610 OP_C_ACCEPT_STREAM_NONE ()
1611
1612 OP_S_NEW_STREAM_BIDI (d, S_BIDI_ID(0))
1613 OP_S_WRITE (d, "frog", 4)
1614 OP_S_CONCLUDE (d)
1615
a350db73 1616 OP_C_ACCEPT_STREAM_WAIT (d)
ed835673
HL
1617 OP_C_ACCEPT_STREAM_NONE ()
1618 OP_C_READ_EXPECT (d, "frog", 4)
1619 OP_C_EXPECT_FIN (d)
1620
1621 OP_S_NEW_STREAM_BIDI (e, S_BIDI_ID(1))
1622 OP_S_WRITE (e, "mixture", 7)
1623 OP_S_CONCLUDE (e)
1624
a350db73 1625 OP_C_ACCEPT_STREAM_WAIT (e)
ed835673
HL
1626 OP_C_READ_EXPECT (e, "mixture", 7)
1627 OP_C_EXPECT_FIN (e)
1628 OP_C_WRITE (e, "ramble", 6)
1629 OP_S_READ_EXPECT (e, "ramble", 6)
1630 OP_C_CONCLUDE (e)
1631 OP_S_EXPECT_FIN (e)
1632
1633 OP_S_NEW_STREAM_UNI (f, S_UNI_ID(0))
1634 OP_S_WRITE (f, "yonder", 6)
1635 OP_S_CONCLUDE (f)
1636
a350db73 1637 OP_C_ACCEPT_STREAM_WAIT (f)
ed835673
HL
1638 OP_C_ACCEPT_STREAM_NONE ()
1639 OP_C_READ_EXPECT (f, "yonder", 6)
1640 OP_C_EXPECT_FIN (f)
1641 OP_C_WRITE_FAIL (f)
1642
83df44ae 1643 OP_C_SET_INCOMING_STREAM_POLICY(SSL_INCOMING_STREAM_POLICY_REJECT)
ed835673
HL
1644 OP_S_NEW_STREAM_BIDI (g, S_BIDI_ID(2))
1645 OP_S_WRITE (g, "unseen", 6)
1646 OP_S_CONCLUDE (g)
1647
1648 OP_C_ACCEPT_STREAM_NONE ()
1649
83df44ae 1650 OP_C_SET_INCOMING_STREAM_POLICY(SSL_INCOMING_STREAM_POLICY_AUTO)
ed835673
HL
1651 OP_S_NEW_STREAM_BIDI (h, S_BIDI_ID(3))
1652 OP_S_WRITE (h, "UNSEEN", 6)
1653 OP_S_CONCLUDE (h)
1654
1655 OP_C_ACCEPT_STREAM_NONE ()
1656
1657 /*
1658 * Streams g, h should have been rejected, so server should have got
1659 * STOP_SENDING/RESET_STREAM.
1660 */
1661 OP_CHECK (check_rejected, S_BIDI_ID(2))
1662 OP_CHECK (check_rejected, S_BIDI_ID(3))
1663
1664 OP_END
1665};
1666
1667/* 3. Default stream detach/reattach test */
1668static const struct script_op script_3[] = {
1669 OP_C_SET_ALPN ("ossltest")
1670 OP_C_CONNECT_WAIT ()
1671
1672 OP_C_WRITE (DEFAULT, "apple", 5)
1673 OP_C_DETACH (a) /* DEFAULT becomes stream 'a' */
1674 OP_C_WRITE_FAIL (DEFAULT)
1675
1676 OP_C_WRITE (a, "by", 2)
1677
1678 OP_S_BIND_STREAM_ID (a, C_BIDI_ID(0))
1679 OP_S_READ_EXPECT (a, "appleby", 7)
1680
1681 OP_S_WRITE (a, "hello", 5)
1682 OP_C_READ_EXPECT (a, "hello", 5)
1683
1684 OP_C_WRITE_FAIL (DEFAULT)
1685 OP_C_ATTACH (a)
1686 OP_C_WRITE (DEFAULT, "is here", 7)
1687 OP_S_READ_EXPECT (a, "is here", 7)
1688
1689 OP_C_DETACH (a)
1690 OP_C_CONCLUDE (a)
1691 OP_S_EXPECT_FIN (a)
1692
1693 OP_END
1694};
1695
1696/* 4. Default stream mode test */
1697static const struct script_op script_4[] = {
1698 OP_C_SET_ALPN ("ossltest")
1699 OP_C_CONNECT_WAIT ()
1700
1701 OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_NONE)
1702 OP_C_WRITE_FAIL (DEFAULT)
1703
1704 OP_S_NEW_STREAM_BIDI (a, S_BIDI_ID(0))
1705 OP_S_WRITE (a, "apple", 5)
1706
1707 OP_C_READ_FAIL (DEFAULT)
1708
a350db73 1709 OP_C_ACCEPT_STREAM_WAIT (a)
ed835673
HL
1710 OP_C_READ_EXPECT (a, "apple", 5)
1711
1712 OP_C_ATTACH (a)
1713 OP_C_WRITE (DEFAULT, "orange", 6)
1714 OP_S_READ_EXPECT (a, "orange", 6)
1715
1716 OP_END
1717};
1718
1719/* 5. Test stream reset functionality */
1720static const struct script_op script_5[] = {
1721 OP_C_SET_ALPN ("ossltest")
1722 OP_C_CONNECT_WAIT ()
1723
1724 OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_NONE)
1725 OP_C_NEW_STREAM_BIDI (a, C_BIDI_ID(0))
1726
1727 OP_C_WRITE (a, "apple", 5)
1728 OP_C_STREAM_RESET (a, 42)
1729
1730 OP_S_BIND_STREAM_ID (a, C_BIDI_ID(0))
2f018d14
HL
1731 /* Reset disrupts read of already sent data */
1732 OP_S_READ_FAIL (a)
ed835673
HL
1733 OP_CHECK (check_stream_reset, C_BIDI_ID(0))
1734
1735 OP_END
1736};
1737
1738/* 6. Test STOP_SENDING functionality */
1739static const struct script_op script_6[] = {
1740 OP_C_SET_ALPN ("ossltest")
1741 OP_C_CONNECT_WAIT ()
1742
1743 OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_NONE)
1744 OP_S_NEW_STREAM_BIDI (a, S_BIDI_ID(0))
1745 OP_S_WRITE (a, "apple", 5)
1746
a350db73 1747 OP_C_ACCEPT_STREAM_WAIT (a)
ed835673
HL
1748 OP_C_FREE_STREAM (a)
1749 OP_C_ACCEPT_STREAM_NONE ()
1750
1751 OP_CHECK (check_stream_stopped, S_BIDI_ID(0))
1752
1753 OP_END
1754};
1755
1756/* 7. Unidirectional default stream mode test (client sends first) */
1757static const struct script_op script_7[] = {
1758 OP_C_SET_ALPN ("ossltest")
1759 OP_C_CONNECT_WAIT ()
1760
1761 OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_AUTO_UNI)
1762 OP_C_WRITE (DEFAULT, "apple", 5)
1763
1764 OP_S_BIND_STREAM_ID (a, C_UNI_ID(0))
1765 OP_S_READ_EXPECT (a, "apple", 5)
1766 OP_S_WRITE_FAIL (a)
1767
1768 OP_END
1769};
1770
1771/* 8. Unidirectional default stream mode test (server sends first) */
1772static const struct script_op script_8[] = {
1773 OP_C_SET_ALPN ("ossltest")
1774 OP_C_CONNECT_WAIT ()
1775
1776 OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_AUTO_UNI)
1777 OP_S_NEW_STREAM_UNI (a, S_UNI_ID(0))
1778 OP_S_WRITE (a, "apple", 5)
1779 OP_C_READ_EXPECT (DEFAULT, "apple", 5)
1780 OP_C_WRITE_FAIL (DEFAULT)
1781
1782 OP_END
1783};
1784
1785/* 9. Unidirectional default stream mode test (server sends first on bidi) */
1786static const struct script_op script_9[] = {
1787 OP_C_SET_ALPN ("ossltest")
1788 OP_C_CONNECT_WAIT ()
1789
1790 OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_AUTO_UNI)
1791 OP_S_NEW_STREAM_BIDI (a, S_BIDI_ID(0))
1792 OP_S_WRITE (a, "apple", 5)
1793 OP_C_READ_EXPECT (DEFAULT, "apple", 5)
1794 OP_C_WRITE (DEFAULT, "orange", 6)
1795 OP_S_READ_EXPECT (a, "orange", 6)
1796
1797 OP_END
1798};
1799
1800/* 10. Shutdown */
1801static const struct script_op script_10[] = {
1802 OP_C_SET_ALPN ("ossltest")
1803 OP_C_CONNECT_WAIT ()
1804
1805 OP_C_WRITE (DEFAULT, "apple", 5)
1806 OP_S_BIND_STREAM_ID (a, C_BIDI_ID(0))
1807 OP_S_READ_EXPECT (a, "apple", 5)
1808
1809 OP_C_SHUTDOWN ()
1810 OP_C_EXPECT_CONN_CLOSE_INFO(0, 1, 0)
1811 OP_S_EXPECT_CONN_CLOSE_INFO(0, 1, 1)
1812
1813 OP_END
1814};
1815
fca44cfc 1816/* 11. Many threads accepted on the same client connection */
274bb489
HL
1817static const struct script_op script_11_child[] = {
1818 OP_C_ACCEPT_STREAM_WAIT (a)
1819 OP_C_READ_EXPECT (a, "foo", 3)
1820 OP_C_EXPECT_FIN (a)
1821
1822 OP_END
1823};
1824
1825static const struct script_op script_11[] = {
1826 OP_C_SET_ALPN ("ossltest")
1827 OP_C_CONNECT_WAIT ()
1828 OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_NONE)
1829
fca44cfc
HL
1830 OP_NEW_THREAD (5, script_11_child)
1831
1832 OP_S_NEW_STREAM_BIDI (a, ANY_ID)
274bb489
HL
1833 OP_S_WRITE (a, "foo", 3)
1834 OP_S_CONCLUDE (a)
1835
fca44cfc 1836 OP_S_NEW_STREAM_BIDI (b, ANY_ID)
274bb489
HL
1837 OP_S_WRITE (b, "foo", 3)
1838 OP_S_CONCLUDE (b)
1839
fca44cfc 1840 OP_S_NEW_STREAM_BIDI (c, ANY_ID)
274bb489
HL
1841 OP_S_WRITE (c, "foo", 3)
1842 OP_S_CONCLUDE (c)
1843
fca44cfc 1844 OP_S_NEW_STREAM_BIDI (d, ANY_ID)
274bb489
HL
1845 OP_S_WRITE (d, "foo", 3)
1846 OP_S_CONCLUDE (d)
1847
fca44cfc 1848 OP_S_NEW_STREAM_BIDI (e, ANY_ID)
274bb489
HL
1849 OP_S_WRITE (e, "foo", 3)
1850 OP_S_CONCLUDE (e)
1851
274bb489
HL
1852 OP_END
1853};
1854
fca44cfc 1855/* 12. Many threads initiated on the same client connection */
274bb489
HL
1856static const struct script_op script_12_child[] = {
1857 OP_C_NEW_STREAM_BIDI (a, ANY_ID)
1858 OP_C_WRITE (a, "foo", 3)
1859 OP_C_CONCLUDE (a)
1860 OP_C_FREE_STREAM (a)
1861
1862 OP_END
1863};
1864
1865static const struct script_op script_12[] = {
1866 OP_C_SET_ALPN ("ossltest")
1867 OP_C_CONNECT_WAIT ()
1868 OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_NONE)
1869
1870 OP_NEW_THREAD (5, script_12_child)
1871
1872 OP_S_BIND_STREAM_ID (a, C_BIDI_ID(0))
1873 OP_S_READ_EXPECT (a, "foo", 3)
1874 OP_S_EXPECT_FIN (a)
1875 OP_S_BIND_STREAM_ID (b, C_BIDI_ID(1))
1876 OP_S_READ_EXPECT (b, "foo", 3)
1877 OP_S_EXPECT_FIN (b)
1878 OP_S_BIND_STREAM_ID (c, C_BIDI_ID(2))
1879 OP_S_READ_EXPECT (c, "foo", 3)
1880 OP_S_EXPECT_FIN (c)
1881 OP_S_BIND_STREAM_ID (d, C_BIDI_ID(3))
1882 OP_S_READ_EXPECT (d, "foo", 3)
1883 OP_S_EXPECT_FIN (d)
1884 OP_S_BIND_STREAM_ID (e, C_BIDI_ID(4))
1885 OP_S_READ_EXPECT (e, "foo", 3)
1886 OP_S_EXPECT_FIN (e)
1887
1888 OP_END
1889};
1890
fca44cfc
HL
1891/* 13. Many threads accepted on the same client connection (stress test) */
1892static const struct script_op script_13_child[] = {
1893 OP_BEGIN_REPEAT (10)
1894
1895 OP_C_ACCEPT_STREAM_WAIT (a)
1896 OP_C_READ_EXPECT (a, "foo", 3)
1897 OP_C_EXPECT_FIN (a)
1898 OP_C_FREE_STREAM (a)
1899
1900 OP_END_REPEAT ()
1901
1902 OP_END
1903};
1904
1905static const struct script_op script_13[] = {
1906 OP_C_SET_ALPN ("ossltest")
1907 OP_C_CONNECT_WAIT ()
1908 OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_NONE)
1909
1910 OP_NEW_THREAD (5, script_13_child)
1911
1912 OP_BEGIN_REPEAT (50)
1913
1914 OP_S_NEW_STREAM_BIDI (a, ANY_ID)
1915 OP_S_WRITE (a, "foo", 3)
1916 OP_S_CONCLUDE (a)
1917 OP_S_UNBIND_STREAM_ID (a)
1918
1919 OP_END_REPEAT ()
1920
1921 OP_END
1922};
1923
1924/* 14. Many threads initiating on the same client connection (stress test) */
1925static const struct script_op script_14_child[] = {
1926 OP_BEGIN_REPEAT (10)
1927
1928 OP_C_NEW_STREAM_BIDI (a, ANY_ID)
1929 OP_C_WRITE (a, "foo", 3)
1930 OP_C_CONCLUDE (a)
1931 OP_C_FREE_STREAM (a)
1932
1933 OP_END_REPEAT ()
1934
1935 OP_END
1936};
1937
1938static const struct script_op script_14[] = {
1939 OP_C_SET_ALPN ("ossltest")
1940 OP_C_CONNECT_WAIT ()
1941 OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_NONE)
1942
1943 OP_NEW_THREAD (5, script_14_child)
1944
1945 OP_BEGIN_REPEAT (50)
1946
1947 OP_S_ACCEPT_STREAM_WAIT (a)
1948 OP_S_READ_EXPECT (a, "foo", 3)
1949 OP_S_EXPECT_FIN (a)
1950 OP_S_UNBIND_STREAM_ID (a)
1951
1952 OP_END_REPEAT ()
1953
1954 OP_END
1955};
1956
0554f723
HL
1957/* 15. Client sending large number of streams, MAX_STREAMS test */
1958static const struct script_op script_15[] = {
1959 OP_C_SET_ALPN ("ossltest")
1960 OP_C_CONNECT_WAIT ()
1961 OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_NONE)
1962
1963 /*
1964 * This will cause a protocol violation to be raised by the server if we are
1965 * not handling the stream limit correctly on the TX side.
1966 */
1967 OP_BEGIN_REPEAT (200)
1968
1969 OP_C_NEW_STREAM_BIDI (a, ANY_ID)
1970 OP_C_WRITE (a, "foo", 3)
1971 OP_C_CONCLUDE (a)
1972 OP_C_FREE_STREAM (a)
1973
1974 OP_END_REPEAT ()
1975
1976 /* Prove the connection is still good. */
1977 OP_S_NEW_STREAM_BIDI (a, S_BIDI_ID(0))
1978 OP_S_WRITE (a, "bar", 3)
1979 OP_S_CONCLUDE (a)
1980
1981 OP_C_ACCEPT_STREAM_WAIT (a)
1982 OP_C_READ_EXPECT (a, "bar", 3)
1983 OP_C_EXPECT_FIN (a)
1984
1985 /*
1986 * Drain the queue of incoming streams. We should be able to get all 200
1987 * even though only 100 can be initiated at a time.
1988 */
1989 OP_BEGIN_REPEAT (200)
1990
1991 OP_S_ACCEPT_STREAM_WAIT (b)
1992 OP_S_READ_EXPECT (b, "foo", 3)
1993 OP_S_EXPECT_FIN (b)
1994 OP_S_UNBIND_STREAM_ID (b)
1995
1996 OP_END_REPEAT ()
1997
1998 OP_END
1999};
2000
2001/* 16. Server sending large number of streams, MAX_STREAMS test */
2002static const struct script_op script_16[] = {
2003 OP_C_SET_ALPN ("ossltest")
2004 OP_C_CONNECT_WAIT ()
2005 OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_NONE)
2006
2007 /*
2008 * This will cause a protocol violation to be raised by the client if we are
2009 * not handling the stream limit correctly on the TX side.
2010 */
2011 OP_BEGIN_REPEAT (200)
2012
2013 OP_S_NEW_STREAM_BIDI (a, ANY_ID)
2014 OP_S_WRITE (a, "foo", 3)
2015 OP_S_CONCLUDE (a)
2016 OP_S_UNBIND_STREAM_ID (a)
2017
2018 OP_END_REPEAT ()
2019
2020 /* Prove that the connection is still good. */
2021 OP_C_NEW_STREAM_BIDI (a, ANY_ID)
2022 OP_C_WRITE (a, "bar", 3)
2023 OP_C_CONCLUDE (a)
2024
2025 OP_S_ACCEPT_STREAM_WAIT (b)
2026 OP_S_READ_EXPECT (b, "bar", 3)
2027 OP_S_EXPECT_FIN (b)
2028
2029 /* Drain the queue of incoming streams. */
2030 OP_BEGIN_REPEAT (200)
2031
2032 OP_C_ACCEPT_STREAM_WAIT (b)
2033 OP_C_READ_EXPECT (b, "foo", 3)
2034 OP_C_EXPECT_FIN (b)
2035 OP_C_FREE_STREAM (b)
2036
2037 OP_END_REPEAT ()
2038
2039 OP_END
2040};
2041
693b23e3
HL
2042/* 17. Key update test - unlimited */
2043static const struct script_op script_17[] = {
2044 OP_C_SET_ALPN ("ossltest")
2045 OP_C_CONNECT_WAIT ()
2046
2047 OP_C_WRITE (DEFAULT, "apple", 5)
2048
2049 OP_S_BIND_STREAM_ID (a, C_BIDI_ID(0))
2050 OP_S_READ_EXPECT (a, "apple", 5)
2051
2052 OP_CHECK (override_key_update, 1)
2053
2054 OP_BEGIN_REPEAT (200)
2055
2056 OP_C_WRITE (DEFAULT, "apple", 5)
2057 OP_S_READ_EXPECT (a, "apple", 5)
2058
2059 /*
2060 * TXKU frequency is bounded by RTT because a previous TXKU needs to be
2061 * acknowledged by the peer first before another one can be begin. By
2062 * waiting this long, we eliminate any such concern and ensure as many key
2063 * updates as possible can occur for the purposes of this test.
2064 */
2065 OP_CHECK (skip_time_ms, 100)
2066
2067 OP_END_REPEAT ()
2068
2069 /* At least 5 RXKUs detected */
2070 OP_CHECK (check_key_update_ge, 5)
2071
2072 /*
2073 * Prove the connection is still healthy by sending something in both
2074 * directions.
2075 */
2076 OP_C_WRITE (DEFAULT, "xyzzy", 5)
2077 OP_S_READ_EXPECT (a, "xyzzy", 5)
2078
2079 OP_S_WRITE (a, "plugh", 5)
2080 OP_C_READ_EXPECT (DEFAULT, "plugh", 5)
2081
2082 OP_END
2083};
2084
2085/* 18. Key update test - RTT-bounded */
2086static const struct script_op script_18[] = {
2087 OP_C_SET_ALPN ("ossltest")
2088 OP_C_CONNECT_WAIT ()
2089
2090 OP_C_WRITE (DEFAULT, "apple", 5)
2091
2092 OP_S_BIND_STREAM_ID (a, C_BIDI_ID(0))
2093 OP_S_READ_EXPECT (a, "apple", 5)
2094
2095 OP_CHECK (override_key_update, 1)
2096
2097 OP_BEGIN_REPEAT (200)
2098
2099 OP_C_WRITE (DEFAULT, "apple", 5)
2100 OP_S_READ_EXPECT (a, "apple", 5)
0e1da9d7 2101 OP_CHECK (skip_time_ms, 4)
693b23e3
HL
2102
2103 OP_END_REPEAT ()
2104
2105 /*
2106 * This time we simulate far less time passing between writes, so there are
2107 * fewer opportunities to initiate TXKUs. Note that we ask for a TXKU every
2108 * 1 packet above, which is absurd; thus this ensures we only actually
2109 * generate TXKUs when we are allowed to.
2110 */
0e1da9d7 2111 OP_CHECK (check_key_update_lt, 240)
693b23e3
HL
2112
2113 /*
2114 * Prove the connection is still healthy by sending something in both
2115 * directions.
2116 */
2117 OP_C_WRITE (DEFAULT, "xyzzy", 5)
2118 OP_S_READ_EXPECT (a, "xyzzy", 5)
2119
2120 OP_S_WRITE (a, "plugh", 5)
2121 OP_C_READ_EXPECT (DEFAULT, "plugh", 5)
2122
2123 OP_END
2124};
2125
2525109f
HL
2126/* 19. Key update test - artificially triggered */
2127static const struct script_op script_19[] = {
2128 OP_C_SET_ALPN ("ossltest")
2129 OP_C_CONNECT_WAIT ()
2130
2131 OP_C_WRITE (DEFAULT, "apple", 5)
2132
2133 OP_S_BIND_STREAM_ID (a, C_BIDI_ID(0))
2134 OP_S_READ_EXPECT (a, "apple", 5)
2135
76696a54
HL
2136 OP_C_WRITE (DEFAULT, "orange", 6)
2137 OP_S_READ_EXPECT (a, "orange", 6)
2138
2139 OP_S_WRITE (a, "strawberry", 10)
2140 OP_C_READ_EXPECT (DEFAULT, "strawberry", 10)
2141
2525109f
HL
2142 OP_CHECK (check_key_update_lt, 1)
2143 OP_CHECK (trigger_key_update, 0)
2144
2145 OP_C_WRITE (DEFAULT, "orange", 6)
2146 OP_S_READ_EXPECT (a, "orange", 6)
9289e59c 2147 OP_S_WRITE (a, "ok", 2)
2525109f 2148
9289e59c 2149 OP_C_READ_EXPECT (DEFAULT, "ok", 2)
2525109f
HL
2150 OP_CHECK (check_key_update_ge, 1)
2151
2152 OP_END
2153};
2154
0345cac6
TM
2155/* 20. Multiple threads accept stream with socket forcibly closed (error test) */
2156static const struct script_op script_20_child[] = {
2157 OP_C_ACCEPT_STREAM_WAIT (a)
2158 OP_C_READ_EXPECT (a, "foo", 3)
2159
2160 OP_SLEEP (500)
2161
2162 OP_C_READ_FAIL_WAIT (a)
2163 OP_C_EXPECT_SSL_ERR (a, SSL_ERROR_SYSCALL)
2164 OP_EXPECT_ERR_LIB (ERR_LIB_SYS)
2165 OP_EXPECT_ERR_REASON (SSL_R_PROTOCOL_IS_SHUTDOWN)
2166 OP_C_FREE_STREAM (a)
2167
2168 OP_END
2169};
2170
2171static const struct script_op script_20[] = {
2172 OP_C_SET_ALPN ("ossltest")
2173 OP_C_CONNECT_WAIT ()
2174 OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_NONE)
2175
2176 OP_NEW_THREAD (5, script_20_child)
2177
2178 OP_BEGIN_REPEAT (5)
2179
2180 OP_S_NEW_STREAM_BIDI (a, ANY_ID)
2181 OP_S_WRITE (a, "foo", 3)
2182 OP_S_UNBIND_STREAM_ID (a)
2183
2184 OP_END_REPEAT ()
2185
2186 OP_SLEEP (100)
2187
2188 OP_C_CLOSE_SOCKET ()
2189
2190 OP_END
2191};
2192
e26dc8e3
HL
2193/* 21. Fault injection - unknown frame in 1-RTT packet */
2194static int script_21_inject_plain(struct helper *h, QUIC_PKT_HDR *hdr,
2195 unsigned char *buf, size_t len)
2196{
7eebc354 2197 int ok = 0;
e26dc8e3
HL
2198 WPACKET wpkt;
2199 unsigned char frame_buf[8];
2200 size_t written;
2201
2202 if (h->inject_word0 == 0 || hdr->type != h->inject_word0)
2203 return 1;
2204
2205 if (!TEST_true(WPACKET_init_static_len(&wpkt, frame_buf,
2206 sizeof(frame_buf), 0)))
2207 return 0;
2208
2209 if (!TEST_true(WPACKET_quic_write_vlint(&wpkt, h->inject_word1)))
7eebc354 2210 goto err;
e26dc8e3
HL
2211
2212 if (!TEST_true(WPACKET_get_total_written(&wpkt, &written)))
7eebc354 2213 goto err;
e26dc8e3
HL
2214
2215 if (!qtest_fault_prepend_frame(h->qtf, frame_buf, written))
7eebc354 2216 goto err;
e26dc8e3 2217
7eebc354
HL
2218 ok = 1;
2219err:
96b7df60
HL
2220 if (ok)
2221 WPACKET_finish(&wpkt);
2222 else
2223 WPACKET_cleanup(&wpkt);
7eebc354 2224 return ok;
e26dc8e3
HL
2225}
2226
2227static const struct script_op script_21[] = {
2228 OP_S_SET_INJECT_PLAIN (script_21_inject_plain)
2229 OP_C_SET_ALPN ("ossltest")
2230 OP_C_CONNECT_WAIT ()
2231
2232 OP_C_WRITE (DEFAULT, "apple", 5)
2233 OP_S_BIND_STREAM_ID (a, C_BIDI_ID(0))
2234 OP_S_READ_EXPECT (a, "apple", 5)
2235
2236 OP_SET_INJECT_WORD (QUIC_PKT_TYPE_1RTT, OSSL_QUIC_VLINT_MAX)
2237
2238 OP_S_WRITE (a, "orange", 6)
2239
2240 OP_C_EXPECT_CONN_CLOSE_INFO(QUIC_ERR_FRAME_ENCODING_ERROR,0,0)
2241
2242 OP_END
2243};
2244
2245/* 22. Fault injection - non-zero packet header reserved bits */
2246static int script_22_inject_plain(struct helper *h, QUIC_PKT_HDR *hdr,
2247 unsigned char *buf, size_t len)
2248{
2249 if (h->inject_word0 == 0)
2250 return 1;
2251
2252 hdr->reserved = 1;
2253 return 1;
2254}
2255
2256static const struct script_op script_22[] = {
2257 OP_S_SET_INJECT_PLAIN (script_22_inject_plain)
2258 OP_C_SET_ALPN ("ossltest")
2259 OP_C_CONNECT_WAIT ()
2260
2261 OP_C_WRITE (DEFAULT, "apple", 5)
2262 OP_S_BIND_STREAM_ID (a, C_BIDI_ID(0))
2263 OP_S_READ_EXPECT (a, "apple", 5)
2264
2265 OP_SET_INJECT_WORD (1, 0)
2266
2267 OP_S_WRITE (a, "orange", 6)
2268
2269 OP_C_EXPECT_CONN_CLOSE_INFO(QUIC_ERR_PROTOCOL_VIOLATION,0,0)
2270
2271 OP_END
2272};
2273
2274/* 23. Fault injection - empty NEW_TOKEN */
2275static int script_23_inject_plain(struct helper *h, QUIC_PKT_HDR *hdr,
2276 unsigned char *buf, size_t len)
2277{
7eebc354 2278 int ok = 0;
e26dc8e3
HL
2279 WPACKET wpkt;
2280 unsigned char frame_buf[16];
2281 size_t written;
2282
2283 if (h->inject_word0 == 0)
2284 return 1;
2285
2286 if (!TEST_true(WPACKET_init_static_len(&wpkt, frame_buf,
2287 sizeof(frame_buf), 0)))
2288 return 0;
2289
2290 if (!TEST_true(WPACKET_quic_write_vlint(&wpkt, OSSL_QUIC_FRAME_TYPE_NEW_TOKEN))
2291 || !TEST_true(WPACKET_quic_write_vlint(&wpkt, 0)))
7eebc354 2292 goto err;
e26dc8e3
HL
2293
2294 if (!TEST_true(WPACKET_get_total_written(&wpkt, &written)))
7eebc354 2295 goto err;
e26dc8e3
HL
2296
2297 if (!qtest_fault_prepend_frame(h->qtf, frame_buf, written))
7eebc354 2298 goto err;
e26dc8e3 2299
7eebc354
HL
2300 ok = 1;
2301err:
96b7df60
HL
2302 if (ok)
2303 WPACKET_finish(&wpkt);
2304 else
2305 WPACKET_cleanup(&wpkt);
7eebc354 2306 return ok;
e26dc8e3
HL
2307}
2308
2309static const struct script_op script_23[] = {
2310 OP_S_SET_INJECT_PLAIN (script_23_inject_plain)
2311 OP_C_SET_ALPN ("ossltest")
2312 OP_C_CONNECT_WAIT ()
2313
2314 OP_C_WRITE (DEFAULT, "apple", 5)
2315 OP_S_BIND_STREAM_ID (a, C_BIDI_ID(0))
2316 OP_S_READ_EXPECT (a, "apple", 5)
2317
2318 OP_SET_INJECT_WORD (1, 0)
2319
2320 OP_S_WRITE (a, "orange", 6)
2321
2322 OP_C_EXPECT_CONN_CLOSE_INFO(QUIC_ERR_FRAME_ENCODING_ERROR,0,0)
2323
2324 OP_END
2325};
2326
2327/* 24. Fault injection - excess value of MAX_STREAMS_BIDI */
2328static int script_24_inject_plain(struct helper *h, QUIC_PKT_HDR *hdr,
2329 unsigned char *buf, size_t len)
2330{
7eebc354 2331 int ok = 0;
e26dc8e3
HL
2332 WPACKET wpkt;
2333 unsigned char frame_buf[16];
2334 size_t written;
2335
2336 if (h->inject_word0 == 0)
2337 return 1;
2338
2339 if (!TEST_true(WPACKET_init_static_len(&wpkt, frame_buf,
2340 sizeof(frame_buf), 0)))
2341 return 0;
2342
2343 if (!TEST_true(WPACKET_quic_write_vlint(&wpkt, h->inject_word1))
2344 || !TEST_true(WPACKET_quic_write_vlint(&wpkt, (((uint64_t)1) << 60) + 1)))
7eebc354 2345 goto err;
e26dc8e3
HL
2346
2347 if (!TEST_true(WPACKET_get_total_written(&wpkt, &written)))
7eebc354 2348 goto err;
e26dc8e3
HL
2349
2350 if (!qtest_fault_prepend_frame(h->qtf, frame_buf, written))
7eebc354 2351 goto err;
e26dc8e3 2352
7eebc354
HL
2353 ok = 1;
2354err:
96b7df60
HL
2355 if (ok)
2356 WPACKET_finish(&wpkt);
2357 else
2358 WPACKET_cleanup(&wpkt);
7eebc354 2359 return ok;
e26dc8e3
HL
2360}
2361
2362static const struct script_op script_24[] = {
2363 OP_S_SET_INJECT_PLAIN (script_24_inject_plain)
2364 OP_C_SET_ALPN ("ossltest")
2365 OP_C_CONNECT_WAIT ()
2366
2367 OP_C_WRITE (DEFAULT, "apple", 5)
2368 OP_S_BIND_STREAM_ID (a, C_BIDI_ID(0))
2369 OP_S_READ_EXPECT (a, "apple", 5)
2370
2371 OP_SET_INJECT_WORD (1, OSSL_QUIC_FRAME_TYPE_MAX_STREAMS_BIDI)
2372
2373 OP_S_WRITE (a, "orange", 6)
2374
2375 OP_C_EXPECT_CONN_CLOSE_INFO(QUIC_ERR_FRAME_ENCODING_ERROR,0,0)
2376
2377 OP_END
2378};
2379
2380/* 25. Fault injection - excess value of MAX_STREAMS_UNI */
2381static const struct script_op script_25[] = {
2382 OP_S_SET_INJECT_PLAIN (script_24_inject_plain)
2383 OP_C_SET_ALPN ("ossltest")
2384 OP_C_CONNECT_WAIT ()
2385
2386 OP_C_WRITE (DEFAULT, "apple", 5)
2387 OP_S_BIND_STREAM_ID (a, C_BIDI_ID(0))
2388 OP_S_READ_EXPECT (a, "apple", 5)
2389
2390 OP_SET_INJECT_WORD (1, OSSL_QUIC_FRAME_TYPE_MAX_STREAMS_UNI)
2391
2392 OP_S_WRITE (a, "orange", 6)
2393
2394 OP_C_EXPECT_CONN_CLOSE_INFO(QUIC_ERR_FRAME_ENCODING_ERROR,0,0)
2395
2396 OP_END
2397};
2398
2399/* 26. Fault injection - excess value of STREAMS_BLOCKED_BIDI */
2400static const struct script_op script_26[] = {
2401 OP_S_SET_INJECT_PLAIN (script_24_inject_plain)
2402 OP_C_SET_ALPN ("ossltest")
2403 OP_C_CONNECT_WAIT ()
2404
2405 OP_C_WRITE (DEFAULT, "apple", 5)
2406 OP_S_BIND_STREAM_ID (a, C_BIDI_ID(0))
2407 OP_S_READ_EXPECT (a, "apple", 5)
2408
2409 OP_SET_INJECT_WORD (1, OSSL_QUIC_FRAME_TYPE_STREAMS_BLOCKED_BIDI)
2410
2411 OP_S_WRITE (a, "orange", 6)
2412
2413 OP_C_EXPECT_CONN_CLOSE_INFO(QUIC_ERR_STREAM_LIMIT_ERROR,0,0)
2414
2415 OP_END
2416};
2417
2418/* 27. Fault injection - excess value of STREAMS_BLOCKED_UNI */
2419static const struct script_op script_27[] = {
2420 OP_S_SET_INJECT_PLAIN (script_24_inject_plain)
2421 OP_C_SET_ALPN ("ossltest")
2422 OP_C_CONNECT_WAIT ()
2423
2424 OP_C_WRITE (DEFAULT, "apple", 5)
2425 OP_S_BIND_STREAM_ID (a, C_BIDI_ID(0))
2426 OP_S_READ_EXPECT (a, "apple", 5)
2427
2428 OP_SET_INJECT_WORD (1, OSSL_QUIC_FRAME_TYPE_STREAMS_BLOCKED_UNI)
2429
2430 OP_S_WRITE (a, "orange", 6)
2431
2432 OP_C_EXPECT_CONN_CLOSE_INFO(QUIC_ERR_STREAM_LIMIT_ERROR,0,0)
2433
2434 OP_END
2435};
2436
2437/* 28. Fault injection - received RESET_STREAM for send-only stream */
2438static int script_28_inject_plain(struct helper *h, QUIC_PKT_HDR *hdr,
2439 unsigned char *buf, size_t len)
2440{
7eebc354 2441 int ok = 0;
e26dc8e3
HL
2442 WPACKET wpkt;
2443 unsigned char frame_buf[32];
2444 size_t written;
2445
2446 if (h->inject_word0 == 0)
2447 return 1;
2448
2449 if (!TEST_true(WPACKET_init_static_len(&wpkt, frame_buf,
2450 sizeof(frame_buf), 0)))
2451 return 0;
2452
2453 if (!TEST_true(WPACKET_quic_write_vlint(&wpkt, h->inject_word1))
2454 || !TEST_true(WPACKET_quic_write_vlint(&wpkt, /* stream ID */
2455 h->inject_word0 - 1))
2456 || !TEST_true(WPACKET_quic_write_vlint(&wpkt, 123))
2457 || (h->inject_word1 == OSSL_QUIC_FRAME_TYPE_RESET_STREAM
2458 && !TEST_true(WPACKET_quic_write_vlint(&wpkt, 5)))) /* final size */
7eebc354 2459 goto err;
e26dc8e3
HL
2460
2461 if (!TEST_true(WPACKET_get_total_written(&wpkt, &written)))
7eebc354 2462 goto err;
e26dc8e3
HL
2463
2464 if (!qtest_fault_prepend_frame(h->qtf, frame_buf, written))
7eebc354 2465 goto err;
e26dc8e3 2466
7eebc354
HL
2467 ok = 1;
2468err:
96b7df60
HL
2469 if (ok)
2470 WPACKET_finish(&wpkt);
2471 else
2472 WPACKET_cleanup(&wpkt);
7eebc354 2473 return ok;
e26dc8e3
HL
2474}
2475
2476static const struct script_op script_28[] = {
2477 OP_S_SET_INJECT_PLAIN (script_28_inject_plain)
2478 OP_C_SET_ALPN ("ossltest")
2479 OP_C_CONNECT_WAIT ()
2480 OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_NONE)
2481
70cafc44
HL
2482 OP_C_NEW_STREAM_BIDI (a, C_BIDI_ID(0))
2483 OP_C_WRITE (a, "orange", 6)
e26dc8e3 2484
70cafc44
HL
2485 OP_S_BIND_STREAM_ID (a, C_BIDI_ID(0))
2486 OP_S_READ_EXPECT (a, "orange", 6)
2487
2488 OP_C_NEW_STREAM_UNI (b, C_UNI_ID(0))
2489 OP_C_WRITE (b, "apple", 5)
2490
2491 OP_S_BIND_STREAM_ID (b, C_UNI_ID(0))
2492 OP_S_READ_EXPECT (b, "apple", 5)
e26dc8e3
HL
2493
2494 OP_SET_INJECT_WORD (C_UNI_ID(0) + 1, OSSL_QUIC_FRAME_TYPE_RESET_STREAM)
70cafc44 2495 OP_S_WRITE (a, "fruit", 5)
e26dc8e3
HL
2496
2497 OP_C_EXPECT_CONN_CLOSE_INFO(QUIC_ERR_STREAM_STATE_ERROR,0,0)
2498
2499 OP_END
2500};
2501
2502/* 29. Fault injection - received RESET_STREAM for nonexistent send-only stream */
2503static const struct script_op script_29[] = {
2504 OP_S_SET_INJECT_PLAIN (script_28_inject_plain)
2505 OP_C_SET_ALPN ("ossltest")
2506 OP_C_CONNECT_WAIT ()
2507 OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_NONE)
2508
70cafc44
HL
2509 OP_C_NEW_STREAM_BIDI (a, C_BIDI_ID(0))
2510 OP_C_WRITE (a, "orange", 6)
e26dc8e3 2511
70cafc44
HL
2512 OP_S_BIND_STREAM_ID (a, C_BIDI_ID(0))
2513 OP_S_READ_EXPECT (a, "orange", 6)
2514
2515 OP_C_NEW_STREAM_UNI (b, C_UNI_ID(0))
2516 OP_C_WRITE (b, "apple", 5)
2517
2518 OP_S_BIND_STREAM_ID (b, C_UNI_ID(0))
2519 OP_S_READ_EXPECT (b, "apple", 5)
e26dc8e3
HL
2520
2521 OP_SET_INJECT_WORD (C_UNI_ID(1) + 1, OSSL_QUIC_FRAME_TYPE_RESET_STREAM)
70cafc44 2522 OP_S_WRITE (a, "fruit", 5)
e26dc8e3
HL
2523
2524 OP_C_EXPECT_CONN_CLOSE_INFO(QUIC_ERR_STREAM_STATE_ERROR,0,0)
2525
2526 OP_END
2527};
2528
2529/* 30. Fault injection - received STOP_SENDING for receive-only stream */
2530static const struct script_op script_30[] = {
2531 OP_S_SET_INJECT_PLAIN (script_28_inject_plain)
2532 OP_C_SET_ALPN ("ossltest")
2533 OP_C_CONNECT_WAIT ()
2534 OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_NONE)
2535
2536 OP_S_NEW_STREAM_UNI (a, S_UNI_ID(0))
2537 OP_S_WRITE (a, "apple", 5)
2538
2539 OP_C_ACCEPT_STREAM_WAIT (a)
2540 OP_C_READ_EXPECT (a, "apple", 5)
2541
2542 OP_SET_INJECT_WORD (S_UNI_ID(0) + 1, OSSL_QUIC_FRAME_TYPE_STOP_SENDING)
2543 OP_S_WRITE (a, "orange", 6)
2544
2545 OP_C_EXPECT_CONN_CLOSE_INFO(QUIC_ERR_STREAM_STATE_ERROR,0,0)
2546
2547 OP_END
2548};
2549
2550/* 31. Fault injection - received STOP_SENDING for nonexistent receive-only stream */
2551static const struct script_op script_31[] = {
2552 OP_S_SET_INJECT_PLAIN (script_28_inject_plain)
2553 OP_C_SET_ALPN ("ossltest")
2554 OP_C_CONNECT_WAIT ()
2555 OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_NONE)
2556
2557 OP_S_NEW_STREAM_UNI (a, S_UNI_ID(0))
2558 OP_S_WRITE (a, "apple", 5)
2559
2560 OP_C_ACCEPT_STREAM_WAIT (a)
2561 OP_C_READ_EXPECT (a, "apple", 5)
2562
2563 OP_SET_INJECT_WORD (C_UNI_ID(0) + 1, OSSL_QUIC_FRAME_TYPE_STOP_SENDING)
2564 OP_S_WRITE (a, "orange", 6)
2565
2566 OP_C_EXPECT_CONN_CLOSE_INFO(QUIC_ERR_STREAM_STATE_ERROR,0,0)
2567
2568 OP_END
2569};
2570
2571/* 32. Fault injection - STREAM frame for nonexistent stream */
2572static int script_32_inject_plain(struct helper *h, QUIC_PKT_HDR *hdr,
2573 unsigned char *buf, size_t len)
2574{
7eebc354 2575 int ok = 0;
e26dc8e3
HL
2576 WPACKET wpkt;
2577 unsigned char frame_buf[64];
2578 size_t written;
2579 uint64_t type = OSSL_QUIC_FRAME_TYPE_STREAM_OFF_LEN, offset, flen, i;
2580
2581 switch (h->inject_word1) {
2582 default:
2583 return 0;
2584 case 0:
2585 return 1;
2586 case 1:
2587 offset = 0;
2588 flen = 0;
2589 break;
2590 case 2:
2591 offset = (((uint64_t)1)<<62) - 1;
2592 flen = 5;
2593 break;
2594 case 3:
2595 offset = 1 * 1024 * 1024 * 1024; /* 1G */
2596 flen = 5;
2597 break;
2598 }
2599
2600 if (!TEST_true(WPACKET_init_static_len(&wpkt, frame_buf,
2601 sizeof(frame_buf), 0)))
2602 return 0;
2603
2604 if (!TEST_true(WPACKET_quic_write_vlint(&wpkt, type))
2605 || !TEST_true(WPACKET_quic_write_vlint(&wpkt, /* stream ID */
2606 h->inject_word0 - 1))
2607 || !TEST_true(WPACKET_quic_write_vlint(&wpkt, offset))
2608 || !TEST_true(WPACKET_quic_write_vlint(&wpkt, flen)))
7eebc354 2609 goto err;
e26dc8e3
HL
2610
2611 for (i = 0; i < flen; ++i)
2612 if (!TEST_true(WPACKET_put_bytes_u8(&wpkt, 0x42)))
7eebc354 2613 goto err;
e26dc8e3
HL
2614
2615 if (!TEST_true(WPACKET_get_total_written(&wpkt, &written)))
7eebc354 2616 goto err;
e26dc8e3
HL
2617
2618 if (!qtest_fault_prepend_frame(h->qtf, frame_buf, written))
7eebc354 2619 goto err;
e26dc8e3 2620
7eebc354
HL
2621 ok = 1;
2622err:
96b7df60
HL
2623 if (ok)
2624 WPACKET_finish(&wpkt);
2625 else
2626 WPACKET_cleanup(&wpkt);
7eebc354 2627 return ok;
e26dc8e3
HL
2628}
2629
2630static const struct script_op script_32[] = {
2631 OP_S_SET_INJECT_PLAIN (script_32_inject_plain)
2632 OP_C_SET_ALPN ("ossltest")
2633 OP_C_CONNECT_WAIT ()
2634 OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_NONE)
2635
2636 OP_S_NEW_STREAM_UNI (a, S_UNI_ID(0))
2637 OP_S_WRITE (a, "apple", 5)
2638
2639 OP_C_ACCEPT_STREAM_WAIT (a)
2640 OP_C_READ_EXPECT (a, "apple", 5)
2641
2642 OP_SET_INJECT_WORD (C_UNI_ID(0) + 1, 1)
2643 OP_S_WRITE (a, "orange", 6)
2644
2645 OP_C_EXPECT_CONN_CLOSE_INFO(QUIC_ERR_STREAM_STATE_ERROR,0,0)
2646
2647 OP_END
2648};
2649
2650/* 33. Fault injection - STREAM frame with illegal offset */
2651static const struct script_op script_33[] = {
2652 OP_S_SET_INJECT_PLAIN (script_32_inject_plain)
2653 OP_C_SET_ALPN ("ossltest")
2654 OP_C_CONNECT_WAIT ()
2655 OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_NONE)
2656
2657 OP_C_NEW_STREAM_BIDI (a, C_BIDI_ID(0))
2658 OP_C_WRITE (a, "apple", 5)
2659
2660 OP_S_BIND_STREAM_ID (a, C_BIDI_ID(0))
2661 OP_S_READ_EXPECT (a, "apple", 5)
2662
2663 OP_SET_INJECT_WORD (C_BIDI_ID(0) + 1, 2)
2664 OP_S_WRITE (a, "orange", 6)
2665
2666 OP_C_EXPECT_CONN_CLOSE_INFO(QUIC_ERR_FRAME_ENCODING_ERROR,0,0)
2667
2668 OP_END
2669};
2670
2671/* 34. Fault injection - STREAM frame which exceeds FC */
2672static const struct script_op script_34[] = {
2673 OP_S_SET_INJECT_PLAIN (script_32_inject_plain)
2674 OP_C_SET_ALPN ("ossltest")
2675 OP_C_CONNECT_WAIT ()
2676 OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_NONE)
2677
2678 OP_C_NEW_STREAM_BIDI (a, C_BIDI_ID(0))
2679 OP_C_WRITE (a, "apple", 5)
2680
2681 OP_S_BIND_STREAM_ID (a, C_BIDI_ID(0))
2682 OP_S_READ_EXPECT (a, "apple", 5)
2683
2684 OP_SET_INJECT_WORD (C_BIDI_ID(0) + 1, 3)
2685 OP_S_WRITE (a, "orange", 6)
2686
2687 OP_C_EXPECT_CONN_CLOSE_INFO(QUIC_ERR_FLOW_CONTROL_ERROR,0,0)
2688
2689 OP_END
2690};
2691
2692/* 35. Fault injection - MAX_STREAM_DATA for receive-only stream */
2693static const struct script_op script_35[] = {
2694 OP_S_SET_INJECT_PLAIN (script_28_inject_plain)
2695 OP_C_SET_ALPN ("ossltest")
2696 OP_C_CONNECT_WAIT ()
2697 OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_NONE)
2698
2699 OP_S_NEW_STREAM_UNI (a, S_UNI_ID(0))
2700 OP_S_WRITE (a, "apple", 5)
2701
2702 OP_C_ACCEPT_STREAM_WAIT (a)
2703 OP_C_READ_EXPECT (a, "apple", 5)
2704
2705 OP_SET_INJECT_WORD (S_UNI_ID(0) + 1, OSSL_QUIC_FRAME_TYPE_MAX_STREAM_DATA)
2706 OP_S_WRITE (a, "orange", 6)
2707
2708 OP_C_EXPECT_CONN_CLOSE_INFO(QUIC_ERR_STREAM_STATE_ERROR,0,0)
2709
2710 OP_END
2711};
2712
2713/* 36. Fault injection - MAX_STREAM_DATA for nonexistent stream */
2714static const struct script_op script_36[] = {
2715 OP_S_SET_INJECT_PLAIN (script_28_inject_plain)
2716 OP_C_SET_ALPN ("ossltest")
2717 OP_C_CONNECT_WAIT ()
2718 OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_NONE)
2719
2720 OP_S_NEW_STREAM_UNI (a, S_UNI_ID(0))
2721 OP_S_WRITE (a, "apple", 5)
2722
2723 OP_C_ACCEPT_STREAM_WAIT (a)
2724 OP_C_READ_EXPECT (a, "apple", 5)
2725
2726 OP_SET_INJECT_WORD (C_BIDI_ID(0) + 1, OSSL_QUIC_FRAME_TYPE_MAX_STREAM_DATA)
2727 OP_S_WRITE (a, "orange", 6)
2728
2729 OP_C_EXPECT_CONN_CLOSE_INFO(QUIC_ERR_STREAM_STATE_ERROR,0,0)
2730
2731 OP_END
2732};
2733
2734/* 37. Fault injection - STREAM_DATA_BLOCKED for send-only stream */
2735static const struct script_op script_37[] = {
2736 OP_S_SET_INJECT_PLAIN (script_28_inject_plain)
2737 OP_C_SET_ALPN ("ossltest")
2738 OP_C_CONNECT_WAIT ()
2739 OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_NONE)
2740
2741 OP_C_NEW_STREAM_UNI (a, C_UNI_ID(0))
2742 OP_C_WRITE (a, "apple", 5)
2743
2744 OP_S_BIND_STREAM_ID (a, C_UNI_ID(0))
2745 OP_S_READ_EXPECT (a, "apple", 5)
2746
2747 OP_S_NEW_STREAM_UNI (b, S_UNI_ID(0))
2748 OP_SET_INJECT_WORD (C_UNI_ID(0) + 1, OSSL_QUIC_FRAME_TYPE_STREAM_DATA_BLOCKED)
2749 OP_S_WRITE (b, "orange", 5)
2750
2751 OP_C_EXPECT_CONN_CLOSE_INFO(QUIC_ERR_STREAM_STATE_ERROR,0,0)
2752
2753 OP_END
2754};
2755
2756/* 38. Fault injection - STREAM_DATA_BLOCKED for non-existent stream */
2757static const struct script_op script_38[] = {
2758 OP_S_SET_INJECT_PLAIN (script_28_inject_plain)
2759 OP_C_SET_ALPN ("ossltest")
2760 OP_C_CONNECT_WAIT ()
2761 OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_NONE)
2762
2763 OP_S_NEW_STREAM_UNI (b, S_UNI_ID(0))
2764 OP_SET_INJECT_WORD (C_BIDI_ID(0) + 1, OSSL_QUIC_FRAME_TYPE_STREAM_DATA_BLOCKED)
2765 OP_S_WRITE (b, "orange", 5)
2766
2767 OP_C_EXPECT_CONN_CLOSE_INFO(QUIC_ERR_STREAM_STATE_ERROR,0,0)
2768
2769 OP_END
2770};
2771
2772/* 39. Fault injection - NEW_CONN_ID with zero-len CID */
2773static int script_39_inject_plain(struct helper *h, QUIC_PKT_HDR *hdr,
2774 unsigned char *buf, size_t len)
2775{
7eebc354 2776 int ok = 0;
e26dc8e3
HL
2777 WPACKET wpkt;
2778 unsigned char frame_buf[64];
2779 size_t i, written;
2780
2781 if (h->inject_word1 == 0)
2782 return 1;
2783
2784 if (!TEST_true(WPACKET_init_static_len(&wpkt, frame_buf,
2785 sizeof(frame_buf), 0)))
2786 return 0;
2787
2788 if (!TEST_true(WPACKET_quic_write_vlint(&wpkt, OSSL_QUIC_FRAME_TYPE_NEW_CONN_ID))
2789 || !TEST_true(WPACKET_quic_write_vlint(&wpkt, 0)) /* seq no */
2790 || !TEST_true(WPACKET_quic_write_vlint(&wpkt, 0)) /* retire prior to */
2791 || !TEST_true(WPACKET_put_bytes_u8(&wpkt, 0))) /* len */
7eebc354 2792 goto err;
e26dc8e3
HL
2793
2794 for (i = 0; i < QUIC_STATELESS_RESET_TOKEN_LEN; ++i)
2795 if (!TEST_true(WPACKET_put_bytes_u8(&wpkt, 0x42)))
7eebc354 2796 goto err;
e26dc8e3
HL
2797
2798 if (!TEST_true(WPACKET_get_total_written(&wpkt, &written)))
7eebc354 2799 goto err;
e26dc8e3
HL
2800
2801 if (!qtest_fault_prepend_frame(h->qtf, frame_buf, written))
7eebc354 2802 goto err;
e26dc8e3 2803
7eebc354
HL
2804 ok = 1;
2805err:
96b7df60
HL
2806 if (ok)
2807 WPACKET_finish(&wpkt);
2808 else
2809 WPACKET_cleanup(&wpkt);
7eebc354 2810 return ok;
e26dc8e3
HL
2811}
2812
2813static const struct script_op script_39[] = {
2814 OP_S_SET_INJECT_PLAIN (script_39_inject_plain)
2815 OP_C_SET_ALPN ("ossltest")
2816 OP_C_CONNECT_WAIT ()
2817 OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_NONE)
2818
2819 OP_C_NEW_STREAM_BIDI (a, C_BIDI_ID(0))
2820 OP_C_WRITE (a, "apple", 5)
2821 OP_S_BIND_STREAM_ID (a, C_BIDI_ID(0))
2822 OP_S_READ_EXPECT (a, "apple", 5)
2823
2824 OP_SET_INJECT_WORD (0, 1)
2825 OP_S_WRITE (a, "orange", 5)
2826
2827 OP_C_EXPECT_CONN_CLOSE_INFO(QUIC_ERR_FRAME_ENCODING_ERROR,0,0)
2828
2829 OP_END
2830};
2831
ed835673
HL
2832static const struct script_op *const scripts[] = {
2833 script_1,
2834 script_2,
2835 script_3,
2836 script_4,
2837 script_5,
2838 script_6,
2839 script_7,
2840 script_8,
2841 script_9,
2842 script_10,
274bb489
HL
2843 script_11,
2844 script_12,
fca44cfc
HL
2845 script_13,
2846 script_14,
0554f723
HL
2847 script_15,
2848 script_16,
693b23e3
HL
2849 script_17,
2850 script_18,
2525109f 2851 script_19,
0345cac6 2852 script_20,
e26dc8e3
HL
2853 script_21,
2854 script_22,
2855 script_23,
2856 script_24,
2857 script_25,
2858 script_26,
2859 script_27,
2860 script_28,
2861 script_29,
2862 script_30,
2863 script_31,
2864 script_32,
2865 script_33,
2866 script_34,
2867 script_35,
2868 script_36,
2869 script_37,
2870 script_38,
ed835673
HL
2871};
2872
2873static int test_script(int idx)
2874{
97f30fd5
HL
2875 int script_idx = idx >> 1;
2876 int free_order = idx & 1;
2877
2878 TEST_info("Running script %d (order=%d)", script_idx + 1, free_order);
2879 return run_script(scripts[script_idx], free_order);
ed835673
HL
2880}
2881
e26dc8e3
HL
2882/* Dynamically generated tests. */
2883static struct script_op dyn_frame_types_script[] = {
70cafc44 2884 OP_S_SET_INJECT_PLAIN (script_20_inject_plain)
e26dc8e3
HL
2885 OP_SET_INJECT_WORD (0, 0) /* dynamic */
2886
2887 OP_C_SET_ALPN ("ossltest")
2888 OP_C_CONNECT_WAIT_OR_FAIL()
2889
2890 OP_C_EXPECT_CONN_CLOSE_INFO(QUIC_ERR_FRAME_ENCODING_ERROR,0,0)
2891
2892 OP_END
2893};
2894
2895struct forbidden_frame_type {
2896 uint64_t pkt_type, frame_type, expected_err;
2897};
2898
2899static const struct forbidden_frame_type forbidden_frame_types[] = {
2900 { QUIC_PKT_TYPE_INITIAL, OSSL_QUIC_VLINT_MAX, QUIC_ERR_FRAME_ENCODING_ERROR },
2901 { QUIC_PKT_TYPE_HANDSHAKE, OSSL_QUIC_VLINT_MAX, QUIC_ERR_FRAME_ENCODING_ERROR },
2902 { QUIC_PKT_TYPE_1RTT, OSSL_QUIC_VLINT_MAX, QUIC_ERR_FRAME_ENCODING_ERROR },
2903
2904 { QUIC_PKT_TYPE_INITIAL, OSSL_QUIC_FRAME_TYPE_STREAM, QUIC_ERR_PROTOCOL_VIOLATION },
2905 { QUIC_PKT_TYPE_INITIAL, OSSL_QUIC_FRAME_TYPE_RESET_STREAM, QUIC_ERR_PROTOCOL_VIOLATION },
2906 { QUIC_PKT_TYPE_INITIAL, OSSL_QUIC_FRAME_TYPE_STOP_SENDING, QUIC_ERR_PROTOCOL_VIOLATION },
2907 { QUIC_PKT_TYPE_INITIAL, OSSL_QUIC_FRAME_TYPE_NEW_TOKEN, QUIC_ERR_PROTOCOL_VIOLATION },
2908 { QUIC_PKT_TYPE_INITIAL, OSSL_QUIC_FRAME_TYPE_MAX_DATA, QUIC_ERR_PROTOCOL_VIOLATION },
2909 { QUIC_PKT_TYPE_INITIAL, OSSL_QUIC_FRAME_TYPE_MAX_STREAM_DATA, QUIC_ERR_PROTOCOL_VIOLATION },
2910 { QUIC_PKT_TYPE_INITIAL, OSSL_QUIC_FRAME_TYPE_MAX_STREAMS_BIDI, QUIC_ERR_PROTOCOL_VIOLATION },
2911 { QUIC_PKT_TYPE_INITIAL, OSSL_QUIC_FRAME_TYPE_MAX_STREAMS_UNI, QUIC_ERR_PROTOCOL_VIOLATION },
2912 { QUIC_PKT_TYPE_INITIAL, OSSL_QUIC_FRAME_TYPE_DATA_BLOCKED, QUIC_ERR_PROTOCOL_VIOLATION },
2913 { QUIC_PKT_TYPE_INITIAL, OSSL_QUIC_FRAME_TYPE_STREAM_DATA_BLOCKED, QUIC_ERR_PROTOCOL_VIOLATION },
2914 { QUIC_PKT_TYPE_INITIAL, OSSL_QUIC_FRAME_TYPE_STREAMS_BLOCKED_BIDI, QUIC_ERR_PROTOCOL_VIOLATION },
2915 { QUIC_PKT_TYPE_INITIAL, OSSL_QUIC_FRAME_TYPE_STREAMS_BLOCKED_UNI, QUIC_ERR_PROTOCOL_VIOLATION },
2916 { QUIC_PKT_TYPE_INITIAL, OSSL_QUIC_FRAME_TYPE_NEW_CONN_ID, QUIC_ERR_PROTOCOL_VIOLATION },
2917 { QUIC_PKT_TYPE_INITIAL, OSSL_QUIC_FRAME_TYPE_RETIRE_CONN_ID, QUIC_ERR_PROTOCOL_VIOLATION },
2918 { QUIC_PKT_TYPE_INITIAL, OSSL_QUIC_FRAME_TYPE_PATH_CHALLENGE, QUIC_ERR_PROTOCOL_VIOLATION },
2919 { QUIC_PKT_TYPE_INITIAL, OSSL_QUIC_FRAME_TYPE_PATH_RESPONSE, QUIC_ERR_PROTOCOL_VIOLATION },
2920 { QUIC_PKT_TYPE_INITIAL, OSSL_QUIC_FRAME_TYPE_CONN_CLOSE_APP, QUIC_ERR_PROTOCOL_VIOLATION },
2921 { QUIC_PKT_TYPE_INITIAL, OSSL_QUIC_FRAME_TYPE_HANDSHAKE_DONE, QUIC_ERR_PROTOCOL_VIOLATION },
2922
2923 { QUIC_PKT_TYPE_HANDSHAKE, OSSL_QUIC_FRAME_TYPE_STREAM, QUIC_ERR_PROTOCOL_VIOLATION },
2924 { QUIC_PKT_TYPE_HANDSHAKE, OSSL_QUIC_FRAME_TYPE_RESET_STREAM, QUIC_ERR_PROTOCOL_VIOLATION },
2925 { QUIC_PKT_TYPE_HANDSHAKE, OSSL_QUIC_FRAME_TYPE_STOP_SENDING, QUIC_ERR_PROTOCOL_VIOLATION },
2926 { QUIC_PKT_TYPE_HANDSHAKE, OSSL_QUIC_FRAME_TYPE_NEW_TOKEN, QUIC_ERR_PROTOCOL_VIOLATION },
2927 { QUIC_PKT_TYPE_HANDSHAKE, OSSL_QUIC_FRAME_TYPE_MAX_DATA, QUIC_ERR_PROTOCOL_VIOLATION },
2928 { QUIC_PKT_TYPE_HANDSHAKE, OSSL_QUIC_FRAME_TYPE_MAX_STREAM_DATA, QUIC_ERR_PROTOCOL_VIOLATION },
2929 { QUIC_PKT_TYPE_HANDSHAKE, OSSL_QUIC_FRAME_TYPE_MAX_STREAMS_BIDI, QUIC_ERR_PROTOCOL_VIOLATION },
2930 { QUIC_PKT_TYPE_HANDSHAKE, OSSL_QUIC_FRAME_TYPE_MAX_STREAMS_UNI, QUIC_ERR_PROTOCOL_VIOLATION },
2931 { QUIC_PKT_TYPE_HANDSHAKE, OSSL_QUIC_FRAME_TYPE_DATA_BLOCKED, QUIC_ERR_PROTOCOL_VIOLATION },
2932 { QUIC_PKT_TYPE_HANDSHAKE, OSSL_QUIC_FRAME_TYPE_STREAM_DATA_BLOCKED, QUIC_ERR_PROTOCOL_VIOLATION },
2933 { QUIC_PKT_TYPE_HANDSHAKE, OSSL_QUIC_FRAME_TYPE_STREAMS_BLOCKED_BIDI, QUIC_ERR_PROTOCOL_VIOLATION },
2934 { QUIC_PKT_TYPE_HANDSHAKE, OSSL_QUIC_FRAME_TYPE_STREAMS_BLOCKED_UNI, QUIC_ERR_PROTOCOL_VIOLATION },
2935 { QUIC_PKT_TYPE_HANDSHAKE, OSSL_QUIC_FRAME_TYPE_NEW_CONN_ID, QUIC_ERR_PROTOCOL_VIOLATION },
2936 { QUIC_PKT_TYPE_HANDSHAKE, OSSL_QUIC_FRAME_TYPE_RETIRE_CONN_ID, QUIC_ERR_PROTOCOL_VIOLATION },
2937 { QUIC_PKT_TYPE_HANDSHAKE, OSSL_QUIC_FRAME_TYPE_PATH_CHALLENGE, QUIC_ERR_PROTOCOL_VIOLATION },
2938 { QUIC_PKT_TYPE_HANDSHAKE, OSSL_QUIC_FRAME_TYPE_PATH_RESPONSE, QUIC_ERR_PROTOCOL_VIOLATION },
2939 { QUIC_PKT_TYPE_HANDSHAKE, OSSL_QUIC_FRAME_TYPE_CONN_CLOSE_APP, QUIC_ERR_PROTOCOL_VIOLATION },
2940 { QUIC_PKT_TYPE_HANDSHAKE, OSSL_QUIC_FRAME_TYPE_HANDSHAKE_DONE, QUIC_ERR_PROTOCOL_VIOLATION },
2941
2942 /* Client uses a zero-length CID so this is not allowed. */
2943 { QUIC_PKT_TYPE_1RTT, OSSL_QUIC_FRAME_TYPE_RETIRE_CONN_ID, QUIC_ERR_PROTOCOL_VIOLATION },
2944};
2945
2946static ossl_unused int test_dyn_frame_types(int idx)
2947{
2948 size_t i;
2949 struct script_op *s = dyn_frame_types_script;
2950
2951 for (i = 0; i < OSSL_NELEM(dyn_frame_types_script); ++i)
2952 if (s[i].op == OPK_SET_INJECT_WORD) {
49a38dee 2953 s[i].arg1 = (size_t)forbidden_frame_types[idx].pkt_type;
e26dc8e3
HL
2954 s[i].arg2 = forbidden_frame_types[idx].frame_type;
2955 } else if (s[i].op == OPK_C_EXPECT_CONN_CLOSE_INFO) {
2956 s[i].arg2 = forbidden_frame_types[idx].expected_err;
2957 }
2958
2959 return run_script(dyn_frame_types_script, 0);
2960}
2961
ed835673
HL
2962OPT_TEST_DECLARE_USAGE("certfile privkeyfile\n")
2963
2964int setup_tests(void)
2965{
2966 if (!test_skip_common_options()) {
2967 TEST_error("Error parsing test options\n");
2968 return 0;
2969 }
2970
2971 if (!TEST_ptr(certfile = test_get_argument(0))
2972 || !TEST_ptr(keyfile = test_get_argument(1)))
2973 return 0;
2974
e26dc8e3 2975 ADD_ALL_TESTS(test_dyn_frame_types, OSSL_NELEM(forbidden_frame_types));
97f30fd5 2976 ADD_ALL_TESTS(test_script, OSSL_NELEM(scripts) * 2);
ed835673
HL
2977 return 1;
2978}