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