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