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