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