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