3085ffff0da8c19f855f61b5a2b023b4640aac34
[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 "testutil.h"
16 #if defined(OPENSSL_THREADS)
17 # include "internal/thread_arch.h"
18 #endif
19
20 static const char *certfile, *keyfile;
21
22 #if defined(OPENSSL_THREADS)
23 struct child_thread_args {
24     struct helper *h;
25     const struct script_op *script;
26     int thread_idx;
27
28     CRYPTO_THREAD *t;
29     CRYPTO_MUTEX *m;
30     int testresult;
31     int done;
32 };
33 #endif
34
35 typedef struct stream_info {
36     const char      *name;
37     SSL             *c_stream;
38     uint64_t        s_stream_id;
39 } STREAM_INFO;
40
41 DEFINE_LHASH_OF_EX(STREAM_INFO);
42
43 struct helper {
44     int                     s_fd;
45     BIO                     *s_net_bio, *s_net_bio_own;
46     BIO_ADDR                *s_net_bio_addr;
47     QUIC_TSERVER            *s;
48     LHASH_OF(STREAM_INFO)   *s_streams;
49
50     int                     c_fd;
51     BIO                     *c_net_bio, *c_net_bio_own;
52     SSL_CTX                 *c_ctx;
53     SSL                     *c_conn;
54     LHASH_OF(STREAM_INFO)   *c_streams;
55
56 #if defined(OPENSSL_THREADS)
57     struct child_thread_args    *threads;
58     size_t                      num_threads;
59 #endif
60
61     OSSL_TIME       start_time;
62
63     /*
64      * This is a duration recording the amount of time we have skipped forwards
65      * for testing purposes relative to the real ossl_time_now() clock. We add
66      * a quantity of time to this every time we skip some time.
67      */
68     CRYPTO_RWLOCK   *time_lock;
69     OSSL_TIME       time_slip; /* protected by time_lock */
70
71     int             init, blocking, check_spin_again;
72     int             free_order;
73 };
74
75 struct helper_local {
76     struct helper           *h;
77     LHASH_OF(STREAM_INFO)   *c_streams;
78     int                     thread_idx;
79 };
80
81 struct script_op {
82     uint32_t        op;
83     const void      *arg0;
84     size_t          arg1;
85     int             (*check_func)(struct helper *h, const struct script_op *op);
86     const char      *stream_name;
87     uint64_t        arg2;
88 };
89
90 #define OPK_END                                     0
91 #define OPK_CHECK                                   1
92 #define OPK_C_SET_ALPN                              2
93 #define OPK_C_CONNECT_WAIT                          3
94 #define OPK_C_WRITE                                 4
95 #define OPK_S_WRITE                                 5
96 #define OPK_C_READ_EXPECT                           6
97 #define OPK_S_READ_EXPECT                           7
98 #define OPK_C_EXPECT_FIN                            8
99 #define OPK_S_EXPECT_FIN                            9
100 #define OPK_C_CONCLUDE                              10
101 #define OPK_S_CONCLUDE                              11
102 #define OPK_C_DETACH                                12
103 #define OPK_C_ATTACH                                13
104 #define OPK_C_NEW_STREAM                            14
105 #define OPK_S_NEW_STREAM                            15
106 #define OPK_C_ACCEPT_STREAM_WAIT                    16
107 #define OPK_C_ACCEPT_STREAM_NONE                    17
108 #define OPK_C_FREE_STREAM                           18
109 #define OPK_C_SET_DEFAULT_STREAM_MODE               19
110 #define OPK_C_SET_INCOMING_STREAM_POLICY            20
111 #define OPK_C_SHUTDOWN                              21
112 #define OPK_C_EXPECT_CONN_CLOSE_INFO                22
113 #define OPK_S_EXPECT_CONN_CLOSE_INFO                23
114 #define OPK_S_BIND_STREAM_ID                        24
115 #define OPK_C_WAIT_FOR_DATA                         25
116 #define OPK_C_WRITE_FAIL                            26
117 #define OPK_S_WRITE_FAIL                            27
118 #define OPK_C_READ_FAIL                             28
119 #define OPK_C_STREAM_RESET                          29
120 #define OPK_S_ACCEPT_STREAM_WAIT                    30
121 #define OPK_NEW_THREAD                              31
122 #define OPK_BEGIN_REPEAT                            32
123 #define OPK_END_REPEAT                              33
124 #define OPK_S_UNBIND_STREAM_ID                      34
125 #define OPK_C_READ_FAIL_WAIT                        35
126 #define OPK_C_CLOSE_SOCKET                          36
127 #define OPK_C_EXPECT_SSL_ERR                        37
128 #define OPK_EXPECT_ERR_REASON                       38
129 #define OPK_EXPECT_ERR_LIB                          39
130 #define OPK_SLEEP                                   40
131
132 #define EXPECT_CONN_CLOSE_APP       (1U << 0)
133 #define EXPECT_CONN_CLOSE_REMOTE    (1U << 1)
134
135 #define C_BIDI_ID(ordinal) \
136     (((ordinal) << 2) | QUIC_STREAM_INITIATOR_CLIENT | QUIC_STREAM_DIR_BIDI)
137 #define S_BIDI_ID(ordinal) \
138     (((ordinal) << 2) | QUIC_STREAM_INITIATOR_SERVER | QUIC_STREAM_DIR_BIDI)
139 #define C_UNI_ID(ordinal) \
140     (((ordinal) << 2) | QUIC_STREAM_INITIATOR_CLIENT | QUIC_STREAM_DIR_UNI)
141 #define S_UNI_ID(ordinal) \
142     (((ordinal) << 2) | QUIC_STREAM_INITIATOR_SERVER | QUIC_STREAM_DIR_UNI)
143
144 #define ANY_ID UINT64_MAX
145
146 #define OP_END  \
147     {OPK_END}
148 #define OP_CHECK(func, arg2)  \
149     {OPK_CHECK, NULL, 0, (func), NULL, (arg2)},
150 #define OP_C_SET_ALPN(alpn) \
151     {OPK_C_SET_ALPN, (alpn), 0, NULL, NULL},
152 #define OP_C_CONNECT_WAIT() \
153     {OPK_C_CONNECT_WAIT, NULL, 0, NULL, NULL},
154 #define OP_C_WRITE(stream_name, buf, buf_len)   \
155     {OPK_C_WRITE, (buf), (buf_len), NULL, #stream_name},
156 #define OP_S_WRITE(stream_name, buf, buf_len)   \
157     {OPK_S_WRITE, (buf), (buf_len), NULL, #stream_name},
158 #define OP_C_READ_EXPECT(stream_name, buf, buf_len)   \
159     {OPK_C_READ_EXPECT, (buf), (buf_len), NULL, #stream_name},
160 #define OP_S_READ_EXPECT(stream_name, buf, buf_len)   \
161     {OPK_S_READ_EXPECT, (buf), (buf_len), NULL, #stream_name},
162 #define OP_C_EXPECT_FIN(stream_name) \
163     {OPK_C_EXPECT_FIN, NULL, 0, NULL, #stream_name},
164 #define OP_S_EXPECT_FIN(stream_name) \
165     {OPK_S_EXPECT_FIN, NULL, 0, NULL, #stream_name},
166 #define OP_C_CONCLUDE(stream_name) \
167     {OPK_C_CONCLUDE, NULL, 0, NULL, #stream_name},
168 #define OP_S_CONCLUDE(stream_name) \
169     {OPK_S_CONCLUDE, NULL, 0, NULL, #stream_name},
170 #define OP_C_DETACH(stream_name) \
171     {OPK_C_DETACH, NULL, 0, NULL, #stream_name},
172 #define OP_C_ATTACH(stream_name) \
173     {OPK_C_ATTACH, NULL, 0, NULL, #stream_name},
174 #define OP_C_NEW_STREAM_BIDI(stream_name, expect_id) \
175     {OPK_C_NEW_STREAM, NULL, 0, NULL, #stream_name, (expect_id)},
176 #define OP_C_NEW_STREAM_UNI(stream_name, expect_id) \
177     {OPK_C_NEW_STREAM, NULL, 1, NULL, #stream_name, (expect_id)},
178 #define OP_S_NEW_STREAM_BIDI(stream_name, expect_id) \
179     {OPK_S_NEW_STREAM, NULL, 0, NULL, #stream_name, (expect_id)},
180 #define OP_S_NEW_STREAM_UNI(stream_name, expect_id) \
181     {OPK_S_NEW_STREAM, NULL, 1, NULL, #stream_name, (expect_id)},
182 #define OP_C_ACCEPT_STREAM_WAIT(stream_name) \
183     {OPK_C_ACCEPT_STREAM_WAIT, NULL, 0, NULL, #stream_name},
184 #define OP_C_ACCEPT_STREAM_NONE() \
185     {OPK_C_ACCEPT_STREAM_NONE, NULL, 0, NULL, NULL},
186 #define OP_C_FREE_STREAM(stream_name) \
187     {OPK_C_FREE_STREAM, NULL, 0, NULL, #stream_name},
188 #define OP_C_SET_DEFAULT_STREAM_MODE(mode) \
189     {OPK_C_SET_DEFAULT_STREAM_MODE, NULL, (mode), NULL, NULL},
190 #define OP_C_SET_INCOMING_STREAM_POLICY(policy) \
191     {OPK_C_SET_INCOMING_STREAM_POLICY, NULL, (policy), NULL, NULL},
192 #define OP_C_SHUTDOWN() \
193     {OPK_C_SHUTDOWN, NULL, 0, NULL, NULL},
194 #define OP_C_EXPECT_CONN_CLOSE_INFO(ec, app, remote)                \
195     {OPK_C_EXPECT_CONN_CLOSE_INFO, NULL,                            \
196         ((app) ? EXPECT_CONN_CLOSE_APP : 0) |                       \
197         ((remote) ? EXPECT_CONN_CLOSE_REMOTE : 0),                  \
198         NULL, NULL, (ec)},
199 #define OP_S_EXPECT_CONN_CLOSE_INFO(ec, app, remote) \
200     {OPK_S_EXPECT_CONN_CLOSE_INFO, NULL,            \
201         ((app) ? EXPECT_CONN_CLOSE_APP : 0) |       \
202         ((remote) ? EXPECT_CONN_CLOSE_REMOTE : 0),  \
203         NULL, NULL, (ec)},
204 #define OP_S_BIND_STREAM_ID(stream_name, stream_id) \
205     {OPK_S_BIND_STREAM_ID, NULL, 0, NULL, #stream_name, (stream_id)},
206 #define OP_C_WAIT_FOR_DATA(stream_name) \
207     {OPK_C_WAIT_FOR_DATA, NULL, 0, NULL, #stream_name},
208 #define OP_C_WRITE_FAIL(stream_name)  \
209     {OPK_C_WRITE_FAIL, NULL, 0, NULL, #stream_name},
210 #define OP_S_WRITE_FAIL(stream_name)  \
211     {OPK_S_WRITE_FAIL, NULL, 0, NULL, #stream_name},
212 #define OP_C_READ_FAIL(stream_name)  \
213     {OPK_C_READ_FAIL, NULL, 0, NULL, #stream_name},
214 #define OP_C_STREAM_RESET(stream_name, aec)  \
215     {OPK_C_STREAM_RESET, NULL, 0, NULL, #stream_name, (aec)},
216 #define OP_S_ACCEPT_STREAM_WAIT(stream_name)  \
217     {OPK_S_ACCEPT_STREAM_WAIT, NULL, 0, NULL, #stream_name},
218 #define OP_NEW_THREAD(num_threads, script) \
219     {OPK_NEW_THREAD, (script), (num_threads), NULL, NULL, 0 },
220 #define OP_BEGIN_REPEAT(n)  \
221     {OPK_BEGIN_REPEAT, NULL, (n)},
222 #define OP_END_REPEAT() \
223     {OPK_END_REPEAT},
224 #define OP_S_UNBIND_STREAM_ID(stream_name) \
225     {OPK_S_UNBIND_STREAM_ID, NULL, 0, NULL, #stream_name},
226 #define OP_C_READ_FAIL_WAIT(stream_name) \
227     {OPK_C_READ_FAIL_WAIT, NULL, 0, NULL, #stream_name},
228 #define OP_C_CLOSE_SOCKET() \
229     {OPK_C_CLOSE_SOCKET},
230 #define OP_C_EXPECT_SSL_ERR(stream_name, err) \
231     {OPK_C_EXPECT_SSL_ERR, NULL, (err), NULL, #stream_name},
232 #define OP_EXPECT_ERR_REASON(err) \
233     {OPK_EXPECT_ERR_REASON, NULL, (err)},
234 #define OP_EXPECT_ERR_LIB(lib) \
235     {OPK_EXPECT_ERR_LIB, NULL, (lib)},
236 #define OP_SLEEP(ms) \
237     {OPK_SLEEP, NULL, 0, NULL, NULL, (ms)},
238
239 static OSSL_TIME get_time(void *arg)
240 {
241     struct helper *h = arg;
242     OSSL_TIME t;
243
244     if (!TEST_true(CRYPTO_THREAD_read_lock(h->time_lock)))
245         return ossl_time_zero();
246
247     t = ossl_time_add(ossl_time_now(), h->time_slip);
248
249     CRYPTO_THREAD_unlock(h->time_lock);
250     return t;
251 }
252
253 static int skip_time_ms(struct helper *h, const struct script_op *op)
254 {
255     if (!TEST_true(CRYPTO_THREAD_write_lock(h->time_lock)))
256         return 0;
257
258     h->time_slip = ossl_time_add(h->time_slip, ossl_ms2time(op->arg2));
259
260     CRYPTO_THREAD_unlock(h->time_lock);
261     return 1;
262 }
263
264 static int check_rejected(struct helper *h, const struct script_op *op)
265 {
266     uint64_t stream_id = op->arg2;
267
268     if (!ossl_quic_tserver_stream_has_peer_stop_sending(h->s, stream_id, NULL)
269         || !ossl_quic_tserver_stream_has_peer_reset_stream(h->s, stream_id, NULL)) {
270         h->check_spin_again = 1;
271         return 0;
272     }
273
274     return 1;
275 }
276
277 static int check_stream_reset(struct helper *h, const struct script_op *op)
278 {
279     uint64_t stream_id = op->arg2, aec = 0;
280
281     if (!ossl_quic_tserver_stream_has_peer_reset_stream(h->s, stream_id, &aec)) {
282         h->check_spin_again = 1;
283         return 0;
284     }
285
286     return TEST_uint64_t_eq(aec, 42);
287 }
288
289 static int check_stream_stopped(struct helper *h, const struct script_op *op)
290 {
291     uint64_t stream_id = op->arg2;
292
293     if (!ossl_quic_tserver_stream_has_peer_stop_sending(h->s, stream_id, NULL)) {
294         h->check_spin_again = 1;
295         return 0;
296     }
297
298     return 1;
299 }
300
301 static int override_key_update(struct helper *h, const struct script_op *op)
302 {
303     QUIC_CHANNEL *ch = ossl_quic_conn_get_channel(h->c_conn);
304
305     ossl_quic_channel_set_txku_threshold_override(ch, op->arg2);
306     return 1;
307 }
308
309 static int trigger_key_update(struct helper *h, const struct script_op *op)
310 {
311     if (!TEST_true(SSL_key_update(h->c_conn, SSL_KEY_UPDATE_REQUESTED)))
312         return 0;
313
314     return 1;
315 }
316
317 static int check_key_update_ge(struct helper *h, const struct script_op *op)
318 {
319     QUIC_CHANNEL *ch = ossl_quic_conn_get_channel(h->c_conn);
320     int64_t txke = (int64_t)ossl_quic_channel_get_tx_key_epoch(ch);
321     int64_t rxke = (int64_t)ossl_quic_channel_get_rx_key_epoch(ch);
322     int64_t diff = txke - rxke;
323
324     /*
325      * TXKE must always be equal to or ahead of RXKE.
326      * It can be ahead of RXKE by at most 1.
327      */
328     if (!TEST_int64_t_ge(diff, 0) || !TEST_int64_t_le(diff, 1))
329         return 0;
330
331     /* Caller specifies a minimum number of RXKEs which must have happened. */
332     if (!TEST_uint64_t_ge((uint64_t)rxke, op->arg2))
333         return 0;
334
335     return 1;
336 }
337
338 static int check_key_update_lt(struct helper *h, const struct script_op *op)
339 {
340     QUIC_CHANNEL *ch = ossl_quic_conn_get_channel(h->c_conn);
341     uint64_t txke = ossl_quic_channel_get_tx_key_epoch(ch);
342
343     /* Caller specifies a maximum number of TXKEs which must have happened. */
344     if (!TEST_uint64_t_lt(txke, op->arg2))
345         return 0;
346
347     return 1;
348 }
349
350 static unsigned long stream_info_hash(const STREAM_INFO *info)
351 {
352     return OPENSSL_LH_strhash(info->name);
353 }
354
355 static int stream_info_cmp(const STREAM_INFO *a, const STREAM_INFO *b)
356 {
357     return strcmp(a->name, b->name);
358 }
359
360 static void cleanup_stream(STREAM_INFO *info)
361 {
362     SSL_free(info->c_stream);
363     OPENSSL_free(info);
364 }
365
366 static void helper_cleanup_streams(LHASH_OF(STREAM_INFO) **lh)
367 {
368     if (*lh == NULL)
369         return;
370
371     lh_STREAM_INFO_doall(*lh, cleanup_stream);
372     lh_STREAM_INFO_free(*lh);
373     *lh = NULL;
374 }
375
376 #if defined(OPENSSL_THREADS)
377 static CRYPTO_THREAD_RETVAL run_script_child_thread(void *arg);
378
379 static int join_threads(struct child_thread_args *threads, size_t num_threads)
380 {
381     int ok = 1;
382     size_t i;
383     CRYPTO_THREAD_RETVAL rv;
384
385     for (i = 0; i < num_threads; ++i) {
386         if (threads[i].t != NULL) {
387             ossl_crypto_thread_native_join(threads[i].t, &rv);
388
389             if (!threads[i].testresult)
390                 /* Do not log failure here, worker will do it. */
391                 ok = 0;
392
393             ossl_crypto_thread_native_clean(threads[i].t);
394             threads[i].t = NULL;
395         }
396
397         ossl_crypto_mutex_free(&threads[i].m);
398     }
399
400     return ok;
401 }
402 #endif
403
404 static void helper_cleanup(struct helper *h)
405 {
406 #if defined(OPENSSL_THREADS)
407     join_threads(h->threads, h->num_threads);
408     OPENSSL_free(h->threads);
409     h->threads = NULL;
410     h->num_threads = 0;
411 #endif
412
413     if (h->free_order == 0) {
414         /* order 0: streams, then conn */
415         helper_cleanup_streams(&h->c_streams);
416
417         SSL_free(h->c_conn);
418         h->c_conn = NULL;
419     } else {
420         /* order 1: conn, then streams */
421         SSL_free(h->c_conn);
422         h->c_conn = NULL;
423
424         helper_cleanup_streams(&h->c_streams);
425     }
426
427     helper_cleanup_streams(&h->s_streams);
428     ossl_quic_tserver_free(h->s);
429     h->s = NULL;
430
431     BIO_free(h->s_net_bio_own);
432     h->s_net_bio_own = NULL;
433
434     BIO_free(h->c_net_bio_own);
435     h->c_net_bio_own = NULL;
436
437     if (h->s_fd >= 0) {
438         BIO_closesocket(h->s_fd);
439         h->s_fd = -1;
440     }
441
442     if (h->c_fd >= 0) {
443         BIO_closesocket(h->c_fd);
444         h->c_fd = -1;
445     }
446
447     BIO_ADDR_free(h->s_net_bio_addr);
448     h->s_net_bio_addr = NULL;
449
450     SSL_CTX_free(h->c_ctx);
451     h->c_ctx = NULL;
452
453     CRYPTO_THREAD_lock_free(h->time_lock);
454     h->time_lock = NULL;
455 }
456
457 static int helper_init(struct helper *h, int free_order)
458 {
459     short port = 8186;
460     struct in_addr ina = {0};
461     QUIC_TSERVER_ARGS s_args = {0};
462
463     memset(h, 0, sizeof(*h));
464     h->c_fd = -1;
465     h->s_fd = -1;
466     h->free_order = free_order;
467     h->time_slip = ossl_time_zero();
468
469     if (!TEST_ptr(h->time_lock = CRYPTO_THREAD_lock_new()))
470         goto err;
471
472     if (!TEST_ptr(h->s_streams = lh_STREAM_INFO_new(stream_info_hash,
473                                                     stream_info_cmp)))
474         goto err;
475
476     if (!TEST_ptr(h->c_streams = lh_STREAM_INFO_new(stream_info_hash,
477                                                     stream_info_cmp)))
478         goto err;
479
480     ina.s_addr = htonl(0x7f000001UL);
481
482     h->s_fd = BIO_socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP, 0);
483     if (!TEST_int_ge(h->s_fd, 0))
484         goto err;
485
486     if (!TEST_true(BIO_socket_nbio(h->s_fd, 1)))
487         goto err;
488
489     if (!TEST_ptr(h->s_net_bio_addr = BIO_ADDR_new()))
490         goto err;
491
492     if (!TEST_true(BIO_ADDR_rawmake(h->s_net_bio_addr, AF_INET, &ina, sizeof(ina),
493                                     htons(port))))
494         goto err;
495
496     if (!TEST_true(BIO_bind(h->s_fd, h->s_net_bio_addr, 0)))
497         goto err;
498
499     if (!TEST_int_gt(BIO_ADDR_rawport(h->s_net_bio_addr), 0))
500         goto err;
501
502     if  (!TEST_ptr(h->s_net_bio = h->s_net_bio_own = BIO_new_dgram(h->s_fd, 0)))
503         goto err;
504
505     if (!BIO_up_ref(h->s_net_bio))
506         goto err;
507
508     s_args.net_rbio     = h->s_net_bio;
509     s_args.net_wbio     = h->s_net_bio;
510     s_args.alpn         = NULL;
511     s_args.now_cb       = get_time;
512     s_args.now_cb_arg   = h;
513
514     if (!TEST_ptr(h->s = ossl_quic_tserver_new(&s_args, certfile, keyfile)))
515         goto err;
516
517     h->s_net_bio_own = NULL;
518
519     h->c_fd = BIO_socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP, 0);
520     if (!TEST_int_ge(h->c_fd, 0))
521         goto err;
522
523     if (!TEST_true(BIO_socket_nbio(h->c_fd, 1)))
524         goto err;
525
526     if (!TEST_ptr(h->c_net_bio = h->c_net_bio_own = BIO_new_dgram(h->c_fd, 0)))
527         goto err;
528
529     if (!TEST_true(BIO_dgram_set_peer(h->c_net_bio, h->s_net_bio_addr)))
530         goto err;
531
532
533     if (!TEST_ptr(h->c_ctx = SSL_CTX_new(OSSL_QUIC_client_method())))
534         goto err;
535
536     if (!TEST_ptr(h->c_conn = SSL_new(h->c_ctx)))
537         goto err;
538
539     /* Use custom time function for virtual time skip. */
540     if (!TEST_true(ossl_quic_conn_set_override_now_cb(h->c_conn, get_time, h)))
541         goto err;
542
543     /* Takes ownership of our reference to the BIO. */
544     SSL_set0_rbio(h->c_conn, h->c_net_bio);
545     h->c_net_bio_own = NULL;
546
547     if (!TEST_true(BIO_up_ref(h->c_net_bio)))
548         goto err;
549
550     SSL_set0_wbio(h->c_conn, h->c_net_bio);
551
552     if (!TEST_true(SSL_set_blocking_mode(h->c_conn, 0)))
553         goto err;
554
555     h->start_time   = ossl_time_now();
556     h->init         = 1;
557     return 1;
558
559 err:
560     helper_cleanup(h);
561     return 0;
562 }
563
564 static int helper_local_init(struct helper_local *hl, struct helper *h,
565                              int thread_idx)
566 {
567     hl->h           = h;
568     hl->c_streams   = NULL;
569     hl->thread_idx  = thread_idx;
570
571     if (!TEST_ptr(h))
572         return 0;
573
574     if (thread_idx < 0) {
575         hl->c_streams = h->c_streams;
576     } else {
577         if (!TEST_ptr(hl->c_streams = lh_STREAM_INFO_new(stream_info_hash,
578                                                          stream_info_cmp)))
579             return 0;
580     }
581
582     return 1;
583 }
584
585 static void helper_local_cleanup(struct helper_local *hl)
586 {
587     if (hl->h == NULL)
588         return;
589
590     if (hl->thread_idx >= 0)
591         helper_cleanup_streams(&hl->c_streams);
592
593     hl->h = NULL;
594 }
595
596 static STREAM_INFO *get_stream_info(LHASH_OF(STREAM_INFO) *lh,
597                                     const char *stream_name)
598 {
599     STREAM_INFO key, *info;
600
601     if (!TEST_ptr(stream_name))
602         return NULL;
603
604     if (!strcmp(stream_name, "DEFAULT"))
605         return NULL;
606
607     key.name = stream_name;
608     info = lh_STREAM_INFO_retrieve(lh, &key);
609     if (info == NULL) {
610         info = OPENSSL_zalloc(sizeof(*info));
611         if (info == NULL)
612             return NULL;
613
614         info->name          = stream_name;
615         info->s_stream_id   = UINT64_MAX;
616         lh_STREAM_INFO_insert(lh, info);
617     }
618
619     return info;
620 }
621
622 static int helper_local_set_c_stream(struct helper_local *hl,
623                                      const char *stream_name,
624                                      SSL *c_stream)
625 {
626     STREAM_INFO *info = get_stream_info(hl->c_streams, stream_name);
627
628     if (info == NULL)
629         return 0;
630
631     info->c_stream      = c_stream;
632     info->s_stream_id   = UINT64_MAX;
633     return 1;
634 }
635
636 static SSL *helper_local_get_c_stream(struct helper_local *hl,
637                                       const char *stream_name)
638 {
639     STREAM_INFO *info;
640
641     if (!strcmp(stream_name, "DEFAULT"))
642         return hl->h->c_conn;
643
644     info = get_stream_info(hl->c_streams, stream_name);
645     if (info == NULL)
646         return NULL;
647
648     return info->c_stream;
649 }
650
651 static int
652 helper_set_s_stream(struct helper *h, const char *stream_name,
653                     uint64_t s_stream_id)
654 {
655     STREAM_INFO *info;
656
657     if (!strcmp(stream_name, "DEFAULT"))
658         return 0;
659
660     info = get_stream_info(h->s_streams, stream_name);
661     if (info == NULL)
662         return 0;
663
664     info->c_stream      = NULL;
665     info->s_stream_id   = s_stream_id;
666     return 1;
667 }
668
669 static uint64_t helper_get_s_stream(struct helper *h, const char *stream_name)
670 {
671     STREAM_INFO *info;
672
673     if (!strcmp(stream_name, "DEFAULT"))
674         return UINT64_MAX;
675
676     info = get_stream_info(h->s_streams, stream_name);
677     if (info == NULL)
678         return UINT64_MAX;
679
680     return info->s_stream_id;
681 }
682
683 static int is_want(SSL *s, int ret)
684 {
685     int ec = SSL_get_error(s, ret);
686
687     return ec == SSL_ERROR_WANT_READ || ec == SSL_ERROR_WANT_WRITE;
688 }
689
690 static int run_script_worker(struct helper *h, const struct script_op *script,
691                              int thread_idx)
692 {
693     int testresult = 0;
694     unsigned char *tmp_buf = NULL;
695     int connect_started = 0;
696     size_t offset = 0;
697     size_t op_idx = 0;
698     const struct script_op *op = NULL;
699     int no_advance = 0, first = 1;
700 #if defined(OPENSSL_THREADS)
701     int end_wait_warning = 0;
702 #endif
703     OSSL_TIME op_start_time = ossl_time_zero(), op_deadline = ossl_time_zero();
704     struct helper_local hl;
705 #define REPEAT_SLOTS 8
706     size_t repeat_stack_idx[REPEAT_SLOTS], repeat_stack_done[REPEAT_SLOTS];
707     size_t repeat_stack_limit[REPEAT_SLOTS];
708     size_t repeat_stack_len = 0;
709
710     if (!TEST_true(helper_local_init(&hl, h, thread_idx)))
711         goto out;
712
713 #define SPIN_AGAIN() { OSSL_sleep(1); no_advance = 1; continue; }
714
715     for (;;) {
716         SSL *c_tgt              = h->c_conn;
717         uint64_t s_stream_id    = UINT64_MAX;
718
719         if (no_advance) {
720             no_advance = 0;
721         } else {
722             if (!first)
723                 ++op_idx;
724
725             first           = 0;
726             offset          = 0;
727             op_start_time   = ossl_time_now();
728             op_deadline     = ossl_time_add(op_start_time, ossl_ms2time(2000));
729         }
730
731         if (!TEST_int_le(ossl_time_compare(ossl_time_now(), op_deadline), 0)) {
732             TEST_error("op %zu timed out on thread %d", op_idx + 1, thread_idx);
733             goto out;
734         }
735
736         op = &script[op_idx];
737
738         if (op->stream_name != NULL) {
739             c_tgt = helper_local_get_c_stream(&hl, op->stream_name);
740             if (thread_idx < 0)
741                 s_stream_id = helper_get_s_stream(h, op->stream_name);
742             else
743                 s_stream_id = UINT64_MAX;
744         }
745
746         if (thread_idx < 0)
747             ossl_quic_tserver_tick(h->s);
748
749         if (thread_idx >= 0 || connect_started)
750             SSL_handle_events(h->c_conn);
751
752         if (thread_idx >= 0) {
753             /* Only allow certain opcodes on child threads. */
754             switch (op->op) {
755                 case OPK_END:
756                 case OPK_C_ACCEPT_STREAM_WAIT:
757                 case OPK_C_NEW_STREAM:
758                 case OPK_C_READ_EXPECT:
759                 case OPK_C_EXPECT_FIN:
760                 case OPK_C_WRITE:
761                 case OPK_C_CONCLUDE:
762                 case OPK_C_FREE_STREAM:
763                 case OPK_BEGIN_REPEAT:
764                 case OPK_END_REPEAT:
765                 case OPK_C_READ_FAIL_WAIT:
766                 case OPK_C_EXPECT_SSL_ERR:
767                 case OPK_EXPECT_ERR_REASON:
768                 case OPK_EXPECT_ERR_LIB:
769                 case OPK_SLEEP:
770                     break;
771
772                 default:
773                     TEST_error("opcode %lu not allowed on child thread",
774                                (unsigned long)op->op);
775                     goto out;
776             }
777         }
778
779         switch (op->op) {
780         case OPK_END:
781             if (!TEST_size_t_eq(repeat_stack_len, 0))
782                 goto out;
783
784 #if defined(OPENSSL_THREADS)
785             if (thread_idx < 0) {
786                 int done;
787                 size_t i;
788
789                 for (i = 0; i < h->num_threads; ++i) {
790                     if (h->threads[i].m == NULL)
791                         continue;
792
793                     ossl_crypto_mutex_lock(h->threads[i].m);
794                     done = h->threads[i].done;
795                     ossl_crypto_mutex_unlock(h->threads[i].m);
796
797                     if (!done) {
798                         if (!end_wait_warning) {
799                             TEST_info("still waiting for other threads to finish (%zu)", i);
800                             end_wait_warning = 1;
801                         }
802
803                         SPIN_AGAIN();
804                     }
805                 }
806             }
807 #endif
808
809             TEST_info("script finished on thread %d", thread_idx);
810             testresult = 1;
811             goto out;
812
813         case OPK_BEGIN_REPEAT:
814             if (!TEST_size_t_lt(repeat_stack_len, OSSL_NELEM(repeat_stack_idx)))
815                 goto out;
816
817             if (!TEST_size_t_gt(op->arg1, 0))
818                 goto out;
819
820             repeat_stack_idx[repeat_stack_len] = op_idx + 1;
821             repeat_stack_done[repeat_stack_len] = 0;
822             repeat_stack_limit[repeat_stack_len] = op->arg1;
823             ++repeat_stack_len;
824             break;
825
826         case OPK_END_REPEAT:
827             if (!TEST_size_t_gt(repeat_stack_len, 0))
828                 goto out;
829
830             if (++repeat_stack_done[repeat_stack_len - 1]
831                 == repeat_stack_limit[repeat_stack_len - 1]) {
832                 --repeat_stack_len;
833             } else {
834                 op_idx = repeat_stack_idx[repeat_stack_len - 1];
835                 no_advance = 1;
836                 continue;
837             }
838
839             break;
840
841         case OPK_CHECK:
842             {
843                 int ok = op->check_func(h, op);
844                 if (h->check_spin_again) {
845                     h->check_spin_again = 0;
846                     SPIN_AGAIN();
847                 }
848
849                 if (!TEST_true(ok))
850                     goto out;
851             }
852             break;
853
854         case OPK_C_SET_ALPN:
855             {
856                 const char *alpn = op->arg0;
857                 size_t alpn_len = strlen(alpn);
858
859                 if (!TEST_size_t_le(alpn_len, UINT8_MAX)
860                     || !TEST_ptr(tmp_buf = (unsigned char *)OPENSSL_malloc(alpn_len + 1)))
861                     goto out;
862
863                 memcpy(tmp_buf + 1, alpn, alpn_len);
864                 tmp_buf[0] = (unsigned char)alpn_len;
865
866                 /* 0 is the success case for SSL_set_alpn_protos(). */
867                 if (!TEST_false(SSL_set_alpn_protos(h->c_conn, tmp_buf,
868                                                     alpn_len + 1)))
869                     goto out;
870
871                 OPENSSL_free(tmp_buf);
872                 tmp_buf = NULL;
873             }
874             break;
875
876         case OPK_C_CONNECT_WAIT:
877             {
878                 int ret;
879
880                 connect_started = 1;
881
882                 ret = SSL_connect(h->c_conn);
883                 if (!TEST_true(ret == 1
884                                || (!h->blocking && is_want(h->c_conn, ret))))
885                     goto out;
886
887                 if (!h->blocking && ret != 1)
888                     SPIN_AGAIN();
889             }
890             break;
891
892         case OPK_C_WRITE:
893             {
894                 size_t bytes_written = 0;
895
896                 if (!TEST_ptr(c_tgt))
897                     goto out;
898
899                 if (!TEST_true(SSL_write_ex(c_tgt, op->arg0, op->arg1,
900                                             &bytes_written))
901                     || !TEST_size_t_eq(bytes_written, op->arg1))
902                     goto out;
903             }
904             break;
905
906         case OPK_S_WRITE:
907             {
908                 size_t bytes_written = 0;
909
910                 if (!TEST_uint64_t_ne(s_stream_id, UINT64_MAX))
911                     goto out;
912
913                 if (!TEST_true(ossl_quic_tserver_write(h->s, s_stream_id,
914                                                        op->arg0, op->arg1,
915                                                        &bytes_written))
916                     || !TEST_size_t_eq(bytes_written, op->arg1))
917                     goto out;
918             }
919             break;
920
921         case OPK_C_CONCLUDE:
922             {
923                 if (!TEST_true(SSL_stream_conclude(c_tgt, 0)))
924                     goto out;
925             }
926             break;
927
928         case OPK_S_CONCLUDE:
929             {
930                 if (!TEST_uint64_t_ne(s_stream_id, UINT64_MAX))
931                     goto out;
932
933                 ossl_quic_tserver_conclude(h->s, s_stream_id);
934             }
935             break;
936
937         case OPK_C_WAIT_FOR_DATA:
938             {
939                 char buf[1];
940                 size_t bytes_read = 0;
941
942                 if (!TEST_ptr(c_tgt))
943                     goto out;
944
945                 if (!SSL_peek_ex(c_tgt, buf, sizeof(buf), &bytes_read)
946                     || bytes_read == 0)
947                     SPIN_AGAIN();
948             }
949             break;
950
951         case OPK_C_READ_EXPECT:
952             {
953                 size_t bytes_read = 0;
954
955                 if (op->arg1 > 0 && tmp_buf == NULL
956                     && !TEST_ptr(tmp_buf = OPENSSL_malloc(op->arg1)))
957                     goto out;
958
959                 if (!SSL_read_ex(c_tgt, tmp_buf + offset, op->arg1 - offset,
960                                  &bytes_read))
961                     SPIN_AGAIN();
962
963                 if (bytes_read + offset != op->arg1) {
964                     offset += bytes_read;
965                     SPIN_AGAIN();
966                 }
967
968                 if (op->arg1 > 0
969                     && !TEST_mem_eq(tmp_buf, op->arg1, op->arg0, op->arg1))
970                     goto out;
971
972                 OPENSSL_free(tmp_buf);
973                 tmp_buf = NULL;
974             }
975             break;
976
977         case OPK_S_READ_EXPECT:
978             {
979                 size_t bytes_read = 0;
980
981                 if (!TEST_uint64_t_ne(s_stream_id, UINT64_MAX))
982                     goto out;
983
984                 if (op->arg1 > 0 && tmp_buf == NULL
985                     && !TEST_ptr(tmp_buf = OPENSSL_malloc(op->arg1)))
986                     goto out;
987
988                 if (!TEST_true(ossl_quic_tserver_read(h->s, s_stream_id,
989                                                       tmp_buf + offset,
990                                                       op->arg1 - offset,
991                                                       &bytes_read)))
992                     goto out;
993
994                 if (bytes_read + offset != op->arg1) {
995                     offset += bytes_read;
996                     SPIN_AGAIN();
997                 }
998
999                 if (op->arg1 > 0
1000                     && !TEST_mem_eq(tmp_buf, op->arg1, op->arg0, op->arg1))
1001                     goto out;
1002
1003                 OPENSSL_free(tmp_buf);
1004                 tmp_buf = NULL;
1005             }
1006             break;
1007
1008         case OPK_C_EXPECT_FIN:
1009             {
1010                 char buf[1];
1011                 size_t bytes_read = 0;
1012
1013                 if (!TEST_false(SSL_read_ex(c_tgt, buf, sizeof(buf),
1014                                             &bytes_read))
1015                     || !TEST_size_t_eq(bytes_read, 0))
1016                     goto out;
1017
1018                 if (is_want(c_tgt, 0))
1019                     SPIN_AGAIN();
1020
1021                 if (!TEST_int_eq(SSL_get_error(c_tgt, 0),
1022                                  SSL_ERROR_ZERO_RETURN))
1023                     goto out;
1024             }
1025             break;
1026
1027         case OPK_S_EXPECT_FIN:
1028             {
1029                 if (!TEST_uint64_t_ne(s_stream_id, UINT64_MAX))
1030                     goto out;
1031
1032                 if (!ossl_quic_tserver_has_read_ended(h->s, s_stream_id))
1033                     SPIN_AGAIN();
1034             }
1035             break;
1036
1037         case OPK_C_DETACH:
1038             {
1039                 SSL *c_stream;
1040
1041                 if (!TEST_ptr_null(c_tgt))
1042                     goto out; /* don't overwrite existing stream with same name */
1043
1044                 if (!TEST_ptr(c_stream = ossl_quic_detach_stream(h->c_conn)))
1045                     goto out;
1046
1047                 if (!TEST_true(helper_local_set_c_stream(&hl, op->stream_name, c_stream)))
1048                     goto out;
1049             }
1050             break;
1051
1052         case OPK_C_ATTACH:
1053             {
1054                 if (!TEST_ptr(c_tgt))
1055                     goto out;
1056
1057                 if (!TEST_true(ossl_quic_attach_stream(h->c_conn, c_tgt)))
1058                     goto out;
1059
1060                 if (!TEST_true(helper_local_set_c_stream(&hl, op->stream_name, NULL)))
1061                     goto out;
1062             }
1063             break;
1064
1065         case OPK_C_NEW_STREAM:
1066             {
1067                 SSL *c_stream;
1068                 uint64_t flags = 0;
1069
1070                 if (!TEST_ptr_null(c_tgt))
1071                     goto out; /* don't overwrite existing stream with same name */
1072
1073                 if (op->arg1 != 0)
1074                     flags |= SSL_STREAM_FLAG_UNI;
1075
1076                 if (!TEST_ptr(c_stream = SSL_new_stream(h->c_conn, flags)))
1077                     goto out;
1078
1079                 if (op->arg2 != UINT64_MAX
1080                     && !TEST_uint64_t_eq(SSL_get_stream_id(c_stream),
1081                                          op->arg2))
1082                     goto out;
1083
1084                 if (!TEST_true(helper_local_set_c_stream(&hl, op->stream_name, c_stream)))
1085                     goto out;
1086             }
1087             break;
1088
1089         case OPK_S_NEW_STREAM:
1090             {
1091                 uint64_t stream_id = UINT64_MAX;
1092
1093                 if (!TEST_uint64_t_eq(s_stream_id, UINT64_MAX))
1094                     goto out; /* don't overwrite existing stream with same name */
1095
1096                 if (!TEST_true(ossl_quic_tserver_stream_new(h->s,
1097                                                             op->arg1 > 0,
1098                                                             &stream_id)))
1099                     goto out;
1100
1101                 if (op->arg2 != UINT64_MAX
1102                     && !TEST_uint64_t_eq(stream_id, op->arg2))
1103                     goto out;
1104
1105                 if (!TEST_true(helper_set_s_stream(h, op->stream_name,
1106                                                    stream_id)))
1107                     goto out;
1108             }
1109             break;
1110
1111         case OPK_C_ACCEPT_STREAM_WAIT:
1112             {
1113                 SSL *c_stream;
1114
1115                 if (!TEST_ptr_null(c_tgt))
1116                     goto out; /* don't overwrite existing stream with same name */
1117
1118                 if ((c_stream = SSL_accept_stream(h->c_conn, 0)) == NULL)
1119                     SPIN_AGAIN();
1120
1121                 if (!TEST_true(helper_local_set_c_stream(&hl, op->stream_name,
1122                                                           c_stream)))
1123                     goto out;
1124             }
1125             break;
1126
1127         case OPK_S_ACCEPT_STREAM_WAIT:
1128             {
1129                 uint64_t new_stream_id;
1130
1131                 if (!TEST_uint64_t_eq(s_stream_id, UINT64_MAX))
1132                     goto out;
1133
1134                 new_stream_id = ossl_quic_tserver_pop_incoming_stream(h->s);
1135                 if (new_stream_id == UINT64_MAX)
1136                     SPIN_AGAIN();
1137
1138                 if (!TEST_true(helper_set_s_stream(h, op->stream_name, new_stream_id)))
1139                     goto out;
1140             }
1141             break;
1142
1143         case OPK_C_ACCEPT_STREAM_NONE:
1144             {
1145                 SSL *c_stream;
1146
1147                 if (!TEST_ptr_null(c_stream = SSL_accept_stream(h->c_conn, 0))) {
1148                     SSL_free(c_stream);
1149                     goto out;
1150                 }
1151             }
1152             break;
1153
1154         case OPK_C_FREE_STREAM:
1155             {
1156                 if (!TEST_ptr(c_tgt)
1157                     || !TEST_true(!SSL_is_connection(c_tgt)))
1158                     goto out;
1159
1160                 if (!TEST_true(helper_local_set_c_stream(&hl, op->stream_name, NULL)))
1161                     goto out;
1162
1163                 SSL_free(c_tgt);
1164                 c_tgt = NULL;
1165             }
1166             break;
1167
1168         case OPK_C_SET_DEFAULT_STREAM_MODE:
1169             {
1170                 if (!TEST_ptr(c_tgt))
1171                     goto out;
1172
1173                 if (!TEST_true(SSL_set_default_stream_mode(c_tgt, op->arg1)))
1174                     goto out;
1175             }
1176             break;
1177
1178         case OPK_C_SET_INCOMING_STREAM_POLICY:
1179             {
1180                 if (!TEST_ptr(c_tgt))
1181                     goto out;
1182
1183                 if (!TEST_true(SSL_set_incoming_stream_policy(c_tgt,
1184                                                               op->arg1, 0)))
1185                     goto out;
1186             }
1187             break;
1188
1189         case OPK_C_SHUTDOWN:
1190             {
1191                 int ret;
1192
1193                 if (!TEST_ptr(c_tgt))
1194                     goto out;
1195
1196                 ret = SSL_shutdown_ex(c_tgt, 0, NULL, 0);
1197                 if (!TEST_int_ge(ret, 0))
1198                     goto out;
1199
1200             }
1201             break;
1202
1203         case OPK_C_EXPECT_CONN_CLOSE_INFO:
1204             {
1205                 SSL_CONN_CLOSE_INFO cc_info = {0};
1206                 int expect_app = (op->arg1 & EXPECT_CONN_CLOSE_APP) != 0;
1207                 int expect_remote = (op->arg1 & EXPECT_CONN_CLOSE_REMOTE) != 0;
1208                 uint64_t error_code = op->arg2;
1209
1210                 if (!TEST_ptr(c_tgt))
1211                     goto out;
1212
1213                 if (!SSL_get_conn_close_info(c_tgt, &cc_info, sizeof(cc_info)))
1214                     SPIN_AGAIN();
1215
1216                 if (!TEST_int_eq(expect_app, !cc_info.is_transport)
1217                     || !TEST_int_eq(expect_remote, !cc_info.is_local)
1218                     || !TEST_uint64_t_eq(error_code, cc_info.error_code))
1219                     goto out;
1220             }
1221             break;
1222
1223         case OPK_S_EXPECT_CONN_CLOSE_INFO:
1224             {
1225                 const QUIC_TERMINATE_CAUSE *tc;
1226                 int expect_app = (op->arg1 & EXPECT_CONN_CLOSE_APP) != 0;
1227                 int expect_remote = (op->arg1 & EXPECT_CONN_CLOSE_REMOTE) != 0;
1228                 uint64_t error_code = op->arg2;
1229
1230                 if (!ossl_quic_tserver_is_term_any(h->s))
1231                     SPIN_AGAIN();
1232
1233                 if (!TEST_ptr(tc = ossl_quic_tserver_get_terminate_cause(h->s)))
1234                     goto out;
1235
1236                 if (!TEST_uint64_t_eq(error_code, tc->error_code)
1237                     || !TEST_int_eq(expect_app, tc->app)
1238                     || !TEST_int_eq(expect_remote, tc->remote))
1239                     goto out;
1240             }
1241             break;
1242
1243         case OPK_S_BIND_STREAM_ID:
1244             {
1245                 if (!TEST_uint64_t_eq(s_stream_id, UINT64_MAX))
1246                     goto out;
1247
1248                 if (!TEST_true(helper_set_s_stream(h, op->stream_name, op->arg2)))
1249                     goto out;
1250             }
1251             break;
1252
1253         case OPK_S_UNBIND_STREAM_ID:
1254             {
1255                 if (!TEST_uint64_t_ne(s_stream_id, UINT64_MAX))
1256                     goto out;
1257
1258                 if (!TEST_true(helper_set_s_stream(h, op->stream_name, UINT64_MAX)))
1259                     goto out;
1260             }
1261             break;
1262
1263         case OPK_C_WRITE_FAIL:
1264             {
1265                 size_t bytes_written = 0;
1266
1267                 if (!TEST_ptr(c_tgt))
1268                     goto out;
1269
1270                 if (!TEST_false(SSL_write_ex(c_tgt, "apple", 5, &bytes_written)))
1271                     goto out;
1272             }
1273             break;
1274
1275         case OPK_S_WRITE_FAIL:
1276             {
1277                 size_t bytes_written = 0;
1278
1279                 if (!TEST_uint64_t_ne(s_stream_id, UINT64_MAX))
1280                     goto out;
1281
1282                 if (!TEST_false(ossl_quic_tserver_write(h->s, s_stream_id,
1283                                                        (const unsigned char *)"apple", 5,
1284                                                        &bytes_written)))
1285                     goto out;
1286             }
1287             break;
1288
1289         case OPK_C_READ_FAIL:
1290             {
1291                 size_t bytes_read = 0;
1292                 char buf[1];
1293
1294                 if (!TEST_ptr(c_tgt))
1295                     goto out;
1296
1297                 if (!TEST_false(SSL_read_ex(c_tgt, buf, sizeof(buf), &bytes_read)))
1298                     goto out;
1299             }
1300             break;
1301
1302         case OPK_C_READ_FAIL_WAIT:
1303             {
1304                 size_t bytes_read = 0;
1305                 char buf[1];
1306
1307                 if (!TEST_ptr(c_tgt))
1308                     goto out;
1309
1310                 if (!TEST_false(SSL_read_ex(c_tgt, buf, sizeof(buf), &bytes_read)))
1311                     goto out;
1312
1313                 if (is_want(c_tgt, 0))
1314                     SPIN_AGAIN();
1315             }
1316             break;
1317
1318         case OPK_C_STREAM_RESET:
1319             {
1320                 SSL_STREAM_RESET_ARGS args = {0};
1321
1322                 if (!TEST_ptr(c_tgt))
1323                     goto out;
1324
1325                 args.quic_error_code = op->arg2;
1326
1327                 if (!TEST_true(SSL_stream_reset(c_tgt, &args, sizeof(args))))
1328                     goto out;
1329             }
1330             break;
1331
1332         case OPK_NEW_THREAD:
1333             {
1334 #if !defined(OPENSSL_THREADS)
1335                 /*
1336                  * If this test script requires threading and we do not have
1337                  * support for it, skip the rest of it.
1338                  */
1339                 TEST_skip("threading not supported, skipping");
1340                 testresult = 1;
1341                 goto out;
1342 #else
1343                 size_t i;
1344
1345                 if (!TEST_ptr_null(h->threads)) {
1346                     TEST_error("max one NEW_THREAD operation per script");
1347                     goto out;
1348                 }
1349
1350                 h->threads = OPENSSL_zalloc(op->arg1 * sizeof(struct child_thread_args));
1351                 if (!TEST_ptr(h->threads))
1352                     goto out;
1353
1354                 h->num_threads = op->arg1;
1355
1356                 for (i = 0; i < op->arg1; ++i) {
1357                     h->threads[i].h            = h;
1358                     h->threads[i].script       = op->arg0;
1359                     h->threads[i].thread_idx   = i;
1360
1361                     h->threads[i].m = ossl_crypto_mutex_new();
1362                     if (!TEST_ptr(h->threads[i].m))
1363                         goto out;
1364
1365                     h->threads[i].t
1366                         = ossl_crypto_thread_native_start(run_script_child_thread,
1367                                                           &h->threads[i], 1);
1368                     if (!TEST_ptr(h->threads[i].t))
1369                         goto out;
1370                 }
1371 #endif
1372             }
1373             break;
1374
1375         case OPK_C_CLOSE_SOCKET:
1376             {
1377                 BIO_closesocket(h->c_fd);
1378             }
1379             break;
1380
1381         case OPK_C_EXPECT_SSL_ERR:
1382             {
1383                 if (!TEST_size_t_eq((size_t)SSL_get_error(c_tgt, 0), op->arg1))
1384                     goto out;
1385             }
1386             break;
1387
1388         case OPK_EXPECT_ERR_REASON:
1389             {
1390                 if (!TEST_size_t_eq((size_t)ERR_GET_REASON(ERR_get_error()), op->arg1))
1391                     goto out;
1392             }
1393             break;
1394
1395         case OPK_EXPECT_ERR_LIB:
1396             {
1397                 if (!TEST_size_t_eq((size_t)ERR_GET_LIB(ERR_get_error()), op->arg1))
1398                     goto out;
1399             }
1400             break;
1401
1402         case OPK_SLEEP:
1403             {
1404                 OSSL_sleep(op->arg2);
1405             }
1406             break;
1407
1408         default:
1409             TEST_error("unknown op");
1410             goto out;
1411         }
1412     }
1413
1414 out:
1415     if (!testresult) {
1416         size_t i;
1417
1418         TEST_error("failed at script op %zu, thread %d\n",
1419                    op_idx + 1, thread_idx);
1420
1421         for (i = 0; i < repeat_stack_len; ++i)
1422             TEST_info("while repeating, iteration %zu of %zu, starting at script op %zu",
1423                       repeat_stack_done[i],
1424                       repeat_stack_limit[i],
1425                       repeat_stack_idx[i]);
1426     }
1427
1428     OPENSSL_free(tmp_buf);
1429     helper_local_cleanup(&hl);
1430     return testresult;
1431 }
1432
1433 static int run_script(const struct script_op *script, int free_order)
1434 {
1435     int testresult = 0;
1436     struct helper h;
1437
1438     if (!TEST_true(helper_init(&h, free_order)))
1439         goto out;
1440
1441     if (!TEST_true(run_script_worker(&h, script, -1)))
1442         goto out;
1443
1444 #if defined(OPENSSL_THREADS)
1445     if (!TEST_true(join_threads(h.threads, h.num_threads)))
1446         goto out;
1447 #endif
1448
1449     testresult = 1;
1450 out:
1451     helper_cleanup(&h);
1452     return testresult;
1453 }
1454
1455 #if defined(OPENSSL_THREADS)
1456 static CRYPTO_THREAD_RETVAL run_script_child_thread(void *arg)
1457 {
1458     int testresult;
1459     struct child_thread_args *args = arg;
1460
1461     testresult = run_script_worker(args->h, args->script,
1462                                    args->thread_idx);
1463
1464     ossl_crypto_mutex_lock(args->m);
1465     args->testresult    = testresult;
1466     args->done          = 1;
1467     ossl_crypto_mutex_unlock(args->m);
1468     return 1;
1469 }
1470 #endif
1471
1472 /* 1. Simple single-stream test */
1473 static const struct script_op script_1[] = {
1474     OP_C_SET_ALPN           ("ossltest")
1475     OP_C_CONNECT_WAIT       ()
1476     OP_C_WRITE              (DEFAULT, "apple", 5)
1477     OP_C_CONCLUDE           (DEFAULT)
1478     OP_S_BIND_STREAM_ID     (a, C_BIDI_ID(0))
1479     OP_S_READ_EXPECT        (a, "apple", 5)
1480     OP_S_EXPECT_FIN         (a)
1481     OP_S_WRITE              (a, "orange", 6)
1482     OP_S_CONCLUDE           (a)
1483     OP_C_READ_EXPECT        (DEFAULT, "orange", 6)
1484     OP_C_EXPECT_FIN         (DEFAULT)
1485     OP_END
1486 };
1487
1488 /* 2. Multi-stream test */
1489 static const struct script_op script_2[] = {
1490     OP_C_SET_ALPN           ("ossltest")
1491     OP_C_CONNECT_WAIT       ()
1492     OP_C_SET_INCOMING_STREAM_POLICY(SSL_INCOMING_STREAM_POLICY_ACCEPT)
1493     OP_C_WRITE              (DEFAULT,  "apple", 5)
1494     OP_S_BIND_STREAM_ID     (a, C_BIDI_ID(0))
1495     OP_S_READ_EXPECT        (a, "apple", 5)
1496     OP_S_WRITE              (a, "orange", 6)
1497     OP_C_READ_EXPECT        (DEFAULT, "orange", 6)
1498
1499     OP_C_NEW_STREAM_BIDI    (b, C_BIDI_ID(1))
1500     OP_C_WRITE              (b, "flamingo", 8)
1501     OP_C_CONCLUDE           (b)
1502     OP_S_BIND_STREAM_ID     (b, C_BIDI_ID(1))
1503     OP_S_READ_EXPECT        (b, "flamingo", 8)
1504     OP_S_EXPECT_FIN         (b)
1505     OP_S_WRITE              (b, "gargoyle", 8)
1506     OP_S_CONCLUDE           (b)
1507     OP_C_READ_EXPECT        (b, "gargoyle", 8)
1508     OP_C_EXPECT_FIN         (b)
1509
1510     OP_C_NEW_STREAM_UNI     (c, C_UNI_ID(0))
1511     OP_C_WRITE              (c, "elephant", 8)
1512     OP_C_CONCLUDE           (c)
1513     OP_S_BIND_STREAM_ID     (c, C_UNI_ID(0))
1514     OP_S_READ_EXPECT        (c, "elephant", 8)
1515     OP_S_EXPECT_FIN         (c)
1516     OP_S_WRITE_FAIL         (c)
1517
1518     OP_C_ACCEPT_STREAM_NONE ()
1519
1520     OP_S_NEW_STREAM_BIDI    (d, S_BIDI_ID(0))
1521     OP_S_WRITE              (d, "frog", 4)
1522     OP_S_CONCLUDE           (d)
1523
1524     OP_C_ACCEPT_STREAM_WAIT (d)
1525     OP_C_ACCEPT_STREAM_NONE ()
1526     OP_C_READ_EXPECT        (d, "frog", 4)
1527     OP_C_EXPECT_FIN         (d)
1528
1529     OP_S_NEW_STREAM_BIDI    (e, S_BIDI_ID(1))
1530     OP_S_WRITE              (e, "mixture", 7)
1531     OP_S_CONCLUDE           (e)
1532
1533     OP_C_ACCEPT_STREAM_WAIT (e)
1534     OP_C_READ_EXPECT        (e, "mixture", 7)
1535     OP_C_EXPECT_FIN         (e)
1536     OP_C_WRITE              (e, "ramble", 6)
1537     OP_S_READ_EXPECT        (e, "ramble", 6)
1538     OP_C_CONCLUDE           (e)
1539     OP_S_EXPECT_FIN         (e)
1540
1541     OP_S_NEW_STREAM_UNI     (f, S_UNI_ID(0))
1542     OP_S_WRITE              (f, "yonder", 6)
1543     OP_S_CONCLUDE           (f)
1544
1545     OP_C_ACCEPT_STREAM_WAIT (f)
1546     OP_C_ACCEPT_STREAM_NONE ()
1547     OP_C_READ_EXPECT        (f, "yonder", 6)
1548     OP_C_EXPECT_FIN         (f)
1549     OP_C_WRITE_FAIL         (f)
1550
1551     OP_C_SET_INCOMING_STREAM_POLICY(SSL_INCOMING_STREAM_POLICY_REJECT)
1552     OP_S_NEW_STREAM_BIDI    (g, S_BIDI_ID(2))
1553     OP_S_WRITE              (g, "unseen", 6)
1554     OP_S_CONCLUDE           (g)
1555
1556     OP_C_ACCEPT_STREAM_NONE ()
1557
1558     OP_C_SET_INCOMING_STREAM_POLICY(SSL_INCOMING_STREAM_POLICY_AUTO)
1559     OP_S_NEW_STREAM_BIDI    (h, S_BIDI_ID(3))
1560     OP_S_WRITE              (h, "UNSEEN", 6)
1561     OP_S_CONCLUDE           (h)
1562
1563     OP_C_ACCEPT_STREAM_NONE ()
1564
1565     /*
1566      * Streams g, h should have been rejected, so server should have got
1567      * STOP_SENDING/RESET_STREAM.
1568      */
1569     OP_CHECK                (check_rejected, S_BIDI_ID(2))
1570     OP_CHECK                (check_rejected, S_BIDI_ID(3))
1571
1572     OP_END
1573 };
1574
1575 /* 3. Default stream detach/reattach test */
1576 static const struct script_op script_3[] = {
1577     OP_C_SET_ALPN           ("ossltest")
1578     OP_C_CONNECT_WAIT       ()
1579
1580     OP_C_WRITE              (DEFAULT, "apple", 5)
1581     OP_C_DETACH             (a)             /* DEFAULT becomes stream 'a' */
1582     OP_C_WRITE_FAIL         (DEFAULT)
1583
1584     OP_C_WRITE              (a, "by", 2)
1585
1586     OP_S_BIND_STREAM_ID     (a, C_BIDI_ID(0))
1587     OP_S_READ_EXPECT        (a, "appleby", 7)
1588
1589     OP_S_WRITE              (a, "hello", 5)
1590     OP_C_READ_EXPECT        (a, "hello", 5)
1591
1592     OP_C_WRITE_FAIL         (DEFAULT)
1593     OP_C_ATTACH             (a)
1594     OP_C_WRITE              (DEFAULT, "is here", 7)
1595     OP_S_READ_EXPECT        (a, "is here", 7)
1596
1597     OP_C_DETACH             (a)
1598     OP_C_CONCLUDE           (a)
1599     OP_S_EXPECT_FIN         (a)
1600
1601     OP_END
1602 };
1603
1604 /* 4. Default stream mode test */
1605 static const struct script_op script_4[] = {
1606     OP_C_SET_ALPN           ("ossltest")
1607     OP_C_CONNECT_WAIT       ()
1608
1609     OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_NONE)
1610     OP_C_WRITE_FAIL         (DEFAULT)
1611
1612     OP_S_NEW_STREAM_BIDI    (a, S_BIDI_ID(0))
1613     OP_S_WRITE              (a, "apple", 5)
1614
1615     OP_C_READ_FAIL          (DEFAULT)
1616
1617     OP_C_ACCEPT_STREAM_WAIT (a)
1618     OP_C_READ_EXPECT        (a, "apple", 5)
1619
1620     OP_C_ATTACH             (a)
1621     OP_C_WRITE              (DEFAULT, "orange", 6)
1622     OP_S_READ_EXPECT        (a, "orange", 6)
1623
1624     OP_END
1625 };
1626
1627 /* 5. Test stream reset functionality */
1628 static const struct script_op script_5[] = {
1629     OP_C_SET_ALPN           ("ossltest")
1630     OP_C_CONNECT_WAIT       ()
1631
1632     OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_NONE)
1633     OP_C_NEW_STREAM_BIDI    (a, C_BIDI_ID(0))
1634
1635     OP_C_WRITE              (a, "apple", 5)
1636     OP_C_STREAM_RESET       (a, 42)
1637
1638     OP_S_BIND_STREAM_ID     (a, C_BIDI_ID(0))
1639     OP_S_READ_EXPECT        (a, "apple", 5)
1640     OP_CHECK                (check_stream_reset, C_BIDI_ID(0))
1641
1642     OP_END
1643 };
1644
1645 /* 6. Test STOP_SENDING functionality */
1646 static const struct script_op script_6[] = {
1647     OP_C_SET_ALPN           ("ossltest")
1648     OP_C_CONNECT_WAIT       ()
1649
1650     OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_NONE)
1651     OP_S_NEW_STREAM_BIDI    (a, S_BIDI_ID(0))
1652     OP_S_WRITE              (a, "apple", 5)
1653
1654     OP_C_ACCEPT_STREAM_WAIT (a)
1655     OP_C_FREE_STREAM        (a)
1656     OP_C_ACCEPT_STREAM_NONE ()
1657
1658     OP_CHECK                (check_stream_stopped, S_BIDI_ID(0))
1659
1660     OP_END
1661 };
1662
1663 /* 7. Unidirectional default stream mode test (client sends first) */
1664 static const struct script_op script_7[] = {
1665     OP_C_SET_ALPN           ("ossltest")
1666     OP_C_CONNECT_WAIT       ()
1667
1668     OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_AUTO_UNI)
1669     OP_C_WRITE              (DEFAULT, "apple", 5)
1670
1671     OP_S_BIND_STREAM_ID     (a, C_UNI_ID(0))
1672     OP_S_READ_EXPECT        (a, "apple", 5)
1673     OP_S_WRITE_FAIL         (a)
1674
1675     OP_END
1676 };
1677
1678 /* 8. Unidirectional default stream mode test (server sends first) */
1679 static const struct script_op script_8[] = {
1680     OP_C_SET_ALPN           ("ossltest")
1681     OP_C_CONNECT_WAIT       ()
1682
1683     OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_AUTO_UNI)
1684     OP_S_NEW_STREAM_UNI     (a, S_UNI_ID(0))
1685     OP_S_WRITE              (a, "apple", 5)
1686     OP_C_READ_EXPECT        (DEFAULT, "apple", 5)
1687     OP_C_WRITE_FAIL         (DEFAULT)
1688
1689     OP_END
1690 };
1691
1692 /* 9. Unidirectional default stream mode test (server sends first on bidi) */
1693 static const struct script_op script_9[] = {
1694     OP_C_SET_ALPN           ("ossltest")
1695     OP_C_CONNECT_WAIT       ()
1696
1697     OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_AUTO_UNI)
1698     OP_S_NEW_STREAM_BIDI    (a, S_BIDI_ID(0))
1699     OP_S_WRITE              (a, "apple", 5)
1700     OP_C_READ_EXPECT        (DEFAULT, "apple", 5)
1701     OP_C_WRITE              (DEFAULT, "orange", 6)
1702     OP_S_READ_EXPECT        (a, "orange", 6)
1703
1704     OP_END
1705 };
1706
1707 /* 10. Shutdown */
1708 static const struct script_op script_10[] = {
1709     OP_C_SET_ALPN           ("ossltest")
1710     OP_C_CONNECT_WAIT       ()
1711
1712     OP_C_WRITE              (DEFAULT, "apple", 5)
1713     OP_S_BIND_STREAM_ID     (a, C_BIDI_ID(0))
1714     OP_S_READ_EXPECT        (a, "apple", 5)
1715
1716     OP_C_SHUTDOWN           ()
1717     OP_C_EXPECT_CONN_CLOSE_INFO(0, 1, 0)
1718     OP_S_EXPECT_CONN_CLOSE_INFO(0, 1, 1)
1719
1720     OP_END
1721 };
1722
1723 /* 11. Many threads accepted on the same client connection */
1724 static const struct script_op script_11_child[] = {
1725     OP_C_ACCEPT_STREAM_WAIT (a)
1726     OP_C_READ_EXPECT        (a, "foo", 3)
1727     OP_C_EXPECT_FIN         (a)
1728
1729     OP_END
1730 };
1731
1732 static const struct script_op script_11[] = {
1733     OP_C_SET_ALPN           ("ossltest")
1734     OP_C_CONNECT_WAIT       ()
1735     OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_NONE)
1736
1737     OP_NEW_THREAD           (5, script_11_child)
1738
1739     OP_S_NEW_STREAM_BIDI    (a, ANY_ID)
1740     OP_S_WRITE              (a, "foo", 3)
1741     OP_S_CONCLUDE           (a)
1742
1743     OP_S_NEW_STREAM_BIDI    (b, ANY_ID)
1744     OP_S_WRITE              (b, "foo", 3)
1745     OP_S_CONCLUDE           (b)
1746
1747     OP_S_NEW_STREAM_BIDI    (c, ANY_ID)
1748     OP_S_WRITE              (c, "foo", 3)
1749     OP_S_CONCLUDE           (c)
1750
1751     OP_S_NEW_STREAM_BIDI    (d, ANY_ID)
1752     OP_S_WRITE              (d, "foo", 3)
1753     OP_S_CONCLUDE           (d)
1754
1755     OP_S_NEW_STREAM_BIDI    (e, ANY_ID)
1756     OP_S_WRITE              (e, "foo", 3)
1757     OP_S_CONCLUDE           (e)
1758
1759     OP_END
1760 };
1761
1762 /* 12. Many threads initiated on the same client connection */
1763 static const struct script_op script_12_child[] = {
1764     OP_C_NEW_STREAM_BIDI    (a, ANY_ID)
1765     OP_C_WRITE              (a, "foo", 3)
1766     OP_C_CONCLUDE           (a)
1767     OP_C_FREE_STREAM        (a)
1768
1769     OP_END
1770 };
1771
1772 static const struct script_op script_12[] = {
1773     OP_C_SET_ALPN           ("ossltest")
1774     OP_C_CONNECT_WAIT       ()
1775     OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_NONE)
1776
1777     OP_NEW_THREAD           (5, script_12_child)
1778
1779     OP_S_BIND_STREAM_ID     (a, C_BIDI_ID(0))
1780     OP_S_READ_EXPECT        (a, "foo", 3)
1781     OP_S_EXPECT_FIN         (a)
1782     OP_S_BIND_STREAM_ID     (b, C_BIDI_ID(1))
1783     OP_S_READ_EXPECT        (b, "foo", 3)
1784     OP_S_EXPECT_FIN         (b)
1785     OP_S_BIND_STREAM_ID     (c, C_BIDI_ID(2))
1786     OP_S_READ_EXPECT        (c, "foo", 3)
1787     OP_S_EXPECT_FIN         (c)
1788     OP_S_BIND_STREAM_ID     (d, C_BIDI_ID(3))
1789     OP_S_READ_EXPECT        (d, "foo", 3)
1790     OP_S_EXPECT_FIN         (d)
1791     OP_S_BIND_STREAM_ID     (e, C_BIDI_ID(4))
1792     OP_S_READ_EXPECT        (e, "foo", 3)
1793     OP_S_EXPECT_FIN         (e)
1794
1795     OP_END
1796 };
1797
1798 /* 13. Many threads accepted on the same client connection (stress test) */
1799 static const struct script_op script_13_child[] = {
1800     OP_BEGIN_REPEAT         (10)
1801
1802     OP_C_ACCEPT_STREAM_WAIT (a)
1803     OP_C_READ_EXPECT        (a, "foo", 3)
1804     OP_C_EXPECT_FIN         (a)
1805     OP_C_FREE_STREAM        (a)
1806
1807     OP_END_REPEAT           ()
1808
1809     OP_END
1810 };
1811
1812 static const struct script_op script_13[] = {
1813     OP_C_SET_ALPN           ("ossltest")
1814     OP_C_CONNECT_WAIT       ()
1815     OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_NONE)
1816
1817     OP_NEW_THREAD           (5, script_13_child)
1818
1819     OP_BEGIN_REPEAT         (50)
1820
1821     OP_S_NEW_STREAM_BIDI    (a, ANY_ID)
1822     OP_S_WRITE              (a, "foo", 3)
1823     OP_S_CONCLUDE           (a)
1824     OP_S_UNBIND_STREAM_ID   (a)
1825
1826     OP_END_REPEAT           ()
1827
1828     OP_END
1829 };
1830
1831 /* 14. Many threads initiating on the same client connection (stress test) */
1832 static const struct script_op script_14_child[] = {
1833     OP_BEGIN_REPEAT         (10)
1834
1835     OP_C_NEW_STREAM_BIDI    (a, ANY_ID)
1836     OP_C_WRITE              (a, "foo", 3)
1837     OP_C_CONCLUDE           (a)
1838     OP_C_FREE_STREAM        (a)
1839
1840     OP_END_REPEAT           ()
1841
1842     OP_END
1843 };
1844
1845 static const struct script_op script_14[] = {
1846     OP_C_SET_ALPN           ("ossltest")
1847     OP_C_CONNECT_WAIT       ()
1848     OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_NONE)
1849
1850     OP_NEW_THREAD           (5, script_14_child)
1851
1852     OP_BEGIN_REPEAT         (50)
1853
1854     OP_S_ACCEPT_STREAM_WAIT (a)
1855     OP_S_READ_EXPECT        (a, "foo", 3)
1856     OP_S_EXPECT_FIN         (a)
1857     OP_S_UNBIND_STREAM_ID   (a)
1858
1859     OP_END_REPEAT           ()
1860
1861     OP_END
1862 };
1863
1864 /* 15. Client sending large number of streams, MAX_STREAMS test */
1865 static const struct script_op script_15[] = {
1866     OP_C_SET_ALPN           ("ossltest")
1867     OP_C_CONNECT_WAIT       ()
1868     OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_NONE)
1869
1870     /*
1871      * This will cause a protocol violation to be raised by the server if we are
1872      * not handling the stream limit correctly on the TX side.
1873      */
1874     OP_BEGIN_REPEAT         (200)
1875
1876     OP_C_NEW_STREAM_BIDI    (a, ANY_ID)
1877     OP_C_WRITE              (a, "foo", 3)
1878     OP_C_CONCLUDE           (a)
1879     OP_C_FREE_STREAM        (a)
1880
1881     OP_END_REPEAT           ()
1882
1883     /* Prove the connection is still good. */
1884     OP_S_NEW_STREAM_BIDI    (a, S_BIDI_ID(0))
1885     OP_S_WRITE              (a, "bar", 3)
1886     OP_S_CONCLUDE           (a)
1887
1888     OP_C_ACCEPT_STREAM_WAIT (a)
1889     OP_C_READ_EXPECT        (a, "bar", 3)
1890     OP_C_EXPECT_FIN         (a)
1891
1892     /*
1893      * Drain the queue of incoming streams. We should be able to get all 200
1894      * even though only 100 can be initiated at a time.
1895      */
1896     OP_BEGIN_REPEAT         (200)
1897
1898     OP_S_ACCEPT_STREAM_WAIT (b)
1899     OP_S_READ_EXPECT        (b, "foo", 3)
1900     OP_S_EXPECT_FIN         (b)
1901     OP_S_UNBIND_STREAM_ID   (b)
1902
1903     OP_END_REPEAT           ()
1904
1905     OP_END
1906 };
1907
1908 /* 16. Server sending large number of streams, MAX_STREAMS test */
1909 static const struct script_op script_16[] = {
1910     OP_C_SET_ALPN           ("ossltest")
1911     OP_C_CONNECT_WAIT       ()
1912     OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_NONE)
1913
1914     /*
1915      * This will cause a protocol violation to be raised by the client if we are
1916      * not handling the stream limit correctly on the TX side.
1917      */
1918     OP_BEGIN_REPEAT         (200)
1919
1920     OP_S_NEW_STREAM_BIDI    (a, ANY_ID)
1921     OP_S_WRITE              (a, "foo", 3)
1922     OP_S_CONCLUDE           (a)
1923     OP_S_UNBIND_STREAM_ID   (a)
1924
1925     OP_END_REPEAT           ()
1926
1927     /* Prove that the connection is still good. */
1928     OP_C_NEW_STREAM_BIDI    (a, ANY_ID)
1929     OP_C_WRITE              (a, "bar", 3)
1930     OP_C_CONCLUDE           (a)
1931
1932     OP_S_ACCEPT_STREAM_WAIT (b)
1933     OP_S_READ_EXPECT        (b, "bar", 3)
1934     OP_S_EXPECT_FIN         (b)
1935
1936     /* Drain the queue of incoming streams. */
1937     OP_BEGIN_REPEAT         (200)
1938
1939     OP_C_ACCEPT_STREAM_WAIT (b)
1940     OP_C_READ_EXPECT        (b, "foo", 3)
1941     OP_C_EXPECT_FIN         (b)
1942     OP_C_FREE_STREAM        (b)
1943
1944     OP_END_REPEAT           ()
1945
1946     OP_END
1947 };
1948
1949 /* 17. Key update test - unlimited */
1950 static const struct script_op script_17[] = {
1951     OP_C_SET_ALPN           ("ossltest")
1952     OP_C_CONNECT_WAIT       ()
1953
1954     OP_C_WRITE              (DEFAULT, "apple", 5)
1955
1956     OP_S_BIND_STREAM_ID     (a, C_BIDI_ID(0))
1957     OP_S_READ_EXPECT        (a, "apple", 5)
1958
1959     OP_CHECK                (override_key_update, 1)
1960
1961     OP_BEGIN_REPEAT         (200)
1962
1963     OP_C_WRITE              (DEFAULT, "apple", 5)
1964     OP_S_READ_EXPECT        (a, "apple", 5)
1965
1966     /*
1967      * TXKU frequency is bounded by RTT because a previous TXKU needs to be
1968      * acknowledged by the peer first before another one can be begin. By
1969      * waiting this long, we eliminate any such concern and ensure as many key
1970      * updates as possible can occur for the purposes of this test.
1971      */
1972     OP_CHECK                (skip_time_ms,    100)
1973
1974     OP_END_REPEAT           ()
1975
1976     /* At least 5 RXKUs detected */
1977     OP_CHECK                (check_key_update_ge, 5)
1978
1979     /*
1980      * Prove the connection is still healthy by sending something in both
1981      * directions.
1982      */
1983     OP_C_WRITE              (DEFAULT, "xyzzy", 5)
1984     OP_S_READ_EXPECT        (a, "xyzzy", 5)
1985
1986     OP_S_WRITE              (a, "plugh", 5)
1987     OP_C_READ_EXPECT        (DEFAULT, "plugh", 5)
1988
1989     OP_END
1990 };
1991
1992 /* 18. Key update test - RTT-bounded */
1993 static const struct script_op script_18[] = {
1994     OP_C_SET_ALPN           ("ossltest")
1995     OP_C_CONNECT_WAIT       ()
1996
1997     OP_C_WRITE              (DEFAULT, "apple", 5)
1998
1999     OP_S_BIND_STREAM_ID     (a, C_BIDI_ID(0))
2000     OP_S_READ_EXPECT        (a, "apple", 5)
2001
2002     OP_CHECK                (override_key_update, 1)
2003
2004     OP_BEGIN_REPEAT         (200)
2005
2006     OP_C_WRITE              (DEFAULT, "apple", 5)
2007     OP_S_READ_EXPECT        (a, "apple", 5)
2008     OP_CHECK                (skip_time_ms,    4)
2009
2010     OP_END_REPEAT           ()
2011
2012     /*
2013      * This time we simulate far less time passing between writes, so there are
2014      * fewer opportunities to initiate TXKUs. Note that we ask for a TXKU every
2015      * 1 packet above, which is absurd; thus this ensures we only actually
2016      * generate TXKUs when we are allowed to.
2017      */
2018     OP_CHECK                (check_key_update_lt, 240)
2019
2020     /*
2021      * Prove the connection is still healthy by sending something in both
2022      * directions.
2023      */
2024     OP_C_WRITE              (DEFAULT, "xyzzy", 5)
2025     OP_S_READ_EXPECT        (a, "xyzzy", 5)
2026
2027     OP_S_WRITE              (a, "plugh", 5)
2028     OP_C_READ_EXPECT        (DEFAULT, "plugh", 5)
2029
2030     OP_END
2031 };
2032
2033 /* 19. Key update test - artificially triggered */
2034 static const struct script_op script_19[] = {
2035     OP_C_SET_ALPN           ("ossltest")
2036     OP_C_CONNECT_WAIT       ()
2037
2038     OP_C_WRITE              (DEFAULT, "apple", 5)
2039
2040     OP_S_BIND_STREAM_ID     (a, C_BIDI_ID(0))
2041     OP_S_READ_EXPECT        (a, "apple", 5)
2042
2043     OP_C_WRITE              (DEFAULT, "orange", 6)
2044     OP_S_READ_EXPECT        (a, "orange", 6)
2045
2046     OP_S_WRITE              (a, "strawberry", 10)
2047     OP_C_READ_EXPECT        (DEFAULT, "strawberry", 10)
2048
2049     OP_CHECK                (check_key_update_lt, 1)
2050     OP_CHECK                (trigger_key_update, 0)
2051
2052     OP_C_WRITE              (DEFAULT, "orange", 6)
2053     OP_S_READ_EXPECT        (a, "orange", 6)
2054     OP_S_WRITE              (a, "ok", 2)
2055
2056     OP_C_READ_EXPECT        (DEFAULT, "ok", 2)
2057     OP_CHECK                (check_key_update_ge, 1)
2058
2059     OP_END
2060 };
2061
2062 /* 20. Multiple threads accept stream with socket forcibly closed (error test) */
2063 static const struct script_op script_20_child[] = {
2064     OP_C_ACCEPT_STREAM_WAIT (a)
2065     OP_C_READ_EXPECT        (a, "foo", 3)
2066
2067     OP_SLEEP                (500)
2068
2069     OP_C_READ_FAIL_WAIT     (a)
2070     OP_C_EXPECT_SSL_ERR     (a, SSL_ERROR_SYSCALL)
2071     OP_EXPECT_ERR_LIB       (ERR_LIB_SYS)
2072     OP_EXPECT_ERR_REASON    (SSL_R_PROTOCOL_IS_SHUTDOWN)
2073     OP_C_FREE_STREAM        (a)
2074
2075     OP_END
2076 };
2077
2078 static const struct script_op script_20[] = {
2079     OP_C_SET_ALPN           ("ossltest")
2080     OP_C_CONNECT_WAIT       ()
2081     OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_NONE)
2082
2083     OP_NEW_THREAD           (5, script_20_child)
2084
2085     OP_BEGIN_REPEAT         (5)
2086
2087     OP_S_NEW_STREAM_BIDI    (a, ANY_ID)
2088     OP_S_WRITE              (a, "foo", 3)
2089     OP_S_UNBIND_STREAM_ID   (a)
2090
2091     OP_END_REPEAT           ()
2092
2093     OP_SLEEP                (100)
2094
2095     OP_C_CLOSE_SOCKET       ()
2096
2097     OP_END
2098 };
2099
2100 static const struct script_op *const scripts[] = {
2101     script_1,
2102     script_2,
2103     script_3,
2104     script_4,
2105     script_5,
2106     script_6,
2107     script_7,
2108     script_8,
2109     script_9,
2110     script_10,
2111     script_11,
2112     script_12,
2113     script_13,
2114     script_14,
2115     script_15,
2116     script_16,
2117     script_17,
2118     script_18,
2119     script_19,
2120     script_20,
2121 };
2122
2123 static int test_script(int idx)
2124 {
2125     int script_idx = idx >> 1;
2126     int free_order = idx & 1;
2127
2128     TEST_info("Running script %d (order=%d)", script_idx + 1, free_order);
2129     return run_script(scripts[script_idx], free_order);
2130 }
2131
2132 OPT_TEST_DECLARE_USAGE("certfile privkeyfile\n")
2133
2134 int setup_tests(void)
2135 {
2136     if (!test_skip_common_options()) {
2137         TEST_error("Error parsing test options\n");
2138         return 0;
2139     }
2140
2141     if (!TEST_ptr(certfile = test_get_argument(0))
2142         || !TEST_ptr(keyfile = test_get_argument(1)))
2143         return 0;
2144
2145     ADD_ALL_TESTS(test_script, OSSL_NELEM(scripts) * 2);
2146     return 1;
2147 }