]> git.ipfire.org Git - thirdparty/openssl.git/blame - test/quic_multistream_test.c
remove files under test directory from coverage report
[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
09a4b4b7 21#include "internal/numbers.h" /* UINT64_C */
ed835673
HL
22
23static const char *certfile, *keyfile;
24
a350db73
HL
25#if defined(OPENSSL_THREADS)
26struct child_thread_args {
27 struct helper *h;
28 const struct script_op *script;
0786483a 29 const char *script_name;
a350db73
HL
30 int thread_idx;
31
32 CRYPTO_THREAD *t;
33 CRYPTO_MUTEX *m;
34 int testresult;
35 int done;
5881dd2c 36 int s_checked_out;
a350db73
HL
37};
38#endif
39
ed835673
HL
40typedef struct stream_info {
41 const char *name;
42 SSL *c_stream;
43 uint64_t s_stream_id;
44} STREAM_INFO;
45
46DEFINE_LHASH_OF_EX(STREAM_INFO);
47
48struct helper {
49 int s_fd;
e26dc8e3 50 BIO *s_net_bio, *s_net_bio_own, *s_qtf_wbio, *s_qtf_wbio_own;
84f371a1
RL
51 /* The BIO_ADDR used for BIO_bind() */
52 BIO_ADDR *s_net_bio_orig_addr;
53 /* The resulting address, which is the one to connect to */
ed835673 54 BIO_ADDR *s_net_bio_addr;
84f371a1 55
5881dd2c
HL
56 /*
57 * When doing a blocking mode test run, s_priv always points to the TSERVER
58 * and s is NULL when the main thread should not be touching s_priv.
59 */
60 QUIC_TSERVER *s, *s_priv;
ed835673
HL
61 LHASH_OF(STREAM_INFO) *s_streams;
62
63 int c_fd;
64 BIO *c_net_bio, *c_net_bio_own;
65 SSL_CTX *c_ctx;
66 SSL *c_conn;
67 LHASH_OF(STREAM_INFO) *c_streams;
68
a350db73
HL
69#if defined(OPENSSL_THREADS)
70 struct child_thread_args *threads;
71 size_t num_threads;
99d6b9f9
HL
72 CRYPTO_MUTEX *misc_m;
73 CRYPTO_CONDVAR *misc_cv;
a350db73
HL
74#endif
75
ed835673 76 OSSL_TIME start_time;
693b23e3
HL
77
78 /*
79 * This is a duration recording the amount of time we have skipped forwards
80 * for testing purposes relative to the real ossl_time_now() clock. We add
81 * a quantity of time to this every time we skip some time.
82 */
83 CRYPTO_RWLOCK *time_lock;
84 OSSL_TIME time_slip; /* protected by time_lock */
85
e26dc8e3
HL
86 QTEST_FAULT *qtf;
87
9715e3aa 88 int init, blocking, check_spin_again;
e26dc8e3
HL
89 int free_order, need_injector;
90
91 int (*qtf_packet_plain_cb)(struct helper *h, QUIC_PKT_HDR *hdr,
92 unsigned char *buf, size_t buf_len);
de56eebd
HL
93 int (*qtf_handshake_cb)(struct helper *h,
94 unsigned char *buf, size_t buf_len);
69169cd9
HL
95 int (*qtf_datagram_cb)(struct helper *h,
96 BIO_MSG *m, size_t stride);
e26dc8e3 97 uint64_t inject_word0, inject_word1;
14551f1e 98 uint64_t scratch0, scratch1, fail_count;
5881dd2c
HL
99#if defined(OPENSSL_THREADS)
100 struct {
101 CRYPTO_THREAD *t;
102 CRYPTO_MUTEX *m;
103 CRYPTO_CONDVAR *c;
104 int ready, stop;
105 } server_thread;
106 int s_checked_out;
107#endif
ed835673
HL
108};
109
a350db73
HL
110struct helper_local {
111 struct helper *h;
112 LHASH_OF(STREAM_INFO) *c_streams;
113 int thread_idx;
5881dd2c 114 const struct script_op *check_op;
a350db73
HL
115};
116
ed835673
HL
117struct script_op {
118 uint32_t op;
119 const void *arg0;
120 size_t arg1;
5881dd2c 121 int (*check_func)(struct helper *h, struct helper_local *hl);
ed835673
HL
122 const char *stream_name;
123 uint64_t arg2;
e26dc8e3
HL
124 int (*qtf_packet_plain_cb)(struct helper *h, QUIC_PKT_HDR *hdr,
125 unsigned char *buf, size_t buf_len);
de56eebd
HL
126 int (*qtf_handshake_cb)(struct helper *h,
127 unsigned char *buf, size_t buf_len);
69169cd9
HL
128 int (*qtf_datagram_cb)(struct helper *h,
129 BIO_MSG *m, size_t stride);
ed835673
HL
130};
131
132#define OPK_END 0
133#define OPK_CHECK 1
134#define OPK_C_SET_ALPN 2
135#define OPK_C_CONNECT_WAIT 3
136#define OPK_C_WRITE 4
137#define OPK_S_WRITE 5
138#define OPK_C_READ_EXPECT 6
139#define OPK_S_READ_EXPECT 7
140#define OPK_C_EXPECT_FIN 8
141#define OPK_S_EXPECT_FIN 9
142#define OPK_C_CONCLUDE 10
143#define OPK_S_CONCLUDE 11
144#define OPK_C_DETACH 12
145#define OPK_C_ATTACH 13
146#define OPK_C_NEW_STREAM 14
147#define OPK_S_NEW_STREAM 15
a350db73 148#define OPK_C_ACCEPT_STREAM_WAIT 16
ed835673
HL
149#define OPK_C_ACCEPT_STREAM_NONE 17
150#define OPK_C_FREE_STREAM 18
151#define OPK_C_SET_DEFAULT_STREAM_MODE 19
83df44ae 152#define OPK_C_SET_INCOMING_STREAM_POLICY 20
cd5e4380 153#define OPK_C_SHUTDOWN_WAIT 21
ed835673
HL
154#define OPK_C_EXPECT_CONN_CLOSE_INFO 22
155#define OPK_S_EXPECT_CONN_CLOSE_INFO 23
156#define OPK_S_BIND_STREAM_ID 24
157#define OPK_C_WAIT_FOR_DATA 25
158#define OPK_C_WRITE_FAIL 26
159#define OPK_S_WRITE_FAIL 27
160#define OPK_C_READ_FAIL 28
161#define OPK_C_STREAM_RESET 29
a350db73
HL
162#define OPK_S_ACCEPT_STREAM_WAIT 30
163#define OPK_NEW_THREAD 31
fca44cfc
HL
164#define OPK_BEGIN_REPEAT 32
165#define OPK_END_REPEAT 33
166#define OPK_S_UNBIND_STREAM_ID 34
0345cac6
TM
167#define OPK_C_READ_FAIL_WAIT 35
168#define OPK_C_CLOSE_SOCKET 36
169#define OPK_C_EXPECT_SSL_ERR 37
170#define OPK_EXPECT_ERR_REASON 38
171#define OPK_EXPECT_ERR_LIB 39
172#define OPK_SLEEP 40
2f018d14 173#define OPK_S_READ_FAIL 41
e26dc8e3
HL
174#define OPK_S_SET_INJECT_PLAIN 42
175#define OPK_SET_INJECT_WORD 43
cd5e4380
HL
176#define OPK_C_INHIBIT_TICK 44
177#define OPK_C_SET_WRITE_BUF_SIZE 45
de56eebd 178#define OPK_S_SET_INJECT_HANDSHAKE 46
614c08c2 179#define OPK_S_NEW_TICKET 47
14551f1e 180#define OPK_C_SKIP_IF_UNBOUND 48
69169cd9 181#define OPK_S_SET_INJECT_DATAGRAM 49
3bc38ba0 182#define OPK_S_SHUTDOWN 50
ed835673
HL
183
184#define EXPECT_CONN_CLOSE_APP (1U << 0)
185#define EXPECT_CONN_CLOSE_REMOTE (1U << 1)
186
14551f1e
HL
187/* OPK_C_NEW_STREAM */
188#define ALLOW_FAIL (1U << 16)
189
ed835673
HL
190#define C_BIDI_ID(ordinal) \
191 (((ordinal) << 2) | QUIC_STREAM_INITIATOR_CLIENT | QUIC_STREAM_DIR_BIDI)
192#define S_BIDI_ID(ordinal) \
193 (((ordinal) << 2) | QUIC_STREAM_INITIATOR_SERVER | QUIC_STREAM_DIR_BIDI)
194#define C_UNI_ID(ordinal) \
195 (((ordinal) << 2) | QUIC_STREAM_INITIATOR_CLIENT | QUIC_STREAM_DIR_UNI)
196#define S_UNI_ID(ordinal) \
197 (((ordinal) << 2) | QUIC_STREAM_INITIATOR_SERVER | QUIC_STREAM_DIR_UNI)
198
a350db73
HL
199#define ANY_ID UINT64_MAX
200
ed835673
HL
201#define OP_END \
202 {OPK_END}
203#define OP_CHECK(func, arg2) \
204 {OPK_CHECK, NULL, 0, (func), NULL, (arg2)},
205#define OP_C_SET_ALPN(alpn) \
206 {OPK_C_SET_ALPN, (alpn), 0, NULL, NULL},
207#define OP_C_CONNECT_WAIT() \
208 {OPK_C_CONNECT_WAIT, NULL, 0, NULL, NULL},
e26dc8e3
HL
209#define OP_C_CONNECT_WAIT_OR_FAIL() \
210 {OPK_C_CONNECT_WAIT, NULL, 1, NULL, NULL},
ed835673
HL
211#define OP_C_WRITE(stream_name, buf, buf_len) \
212 {OPK_C_WRITE, (buf), (buf_len), NULL, #stream_name},
213#define OP_S_WRITE(stream_name, buf, buf_len) \
214 {OPK_S_WRITE, (buf), (buf_len), NULL, #stream_name},
215#define OP_C_READ_EXPECT(stream_name, buf, buf_len) \
216 {OPK_C_READ_EXPECT, (buf), (buf_len), NULL, #stream_name},
217#define OP_S_READ_EXPECT(stream_name, buf, buf_len) \
218 {OPK_S_READ_EXPECT, (buf), (buf_len), NULL, #stream_name},
219#define OP_C_EXPECT_FIN(stream_name) \
220 {OPK_C_EXPECT_FIN, NULL, 0, NULL, #stream_name},
221#define OP_S_EXPECT_FIN(stream_name) \
222 {OPK_S_EXPECT_FIN, NULL, 0, NULL, #stream_name},
223#define OP_C_CONCLUDE(stream_name) \
224 {OPK_C_CONCLUDE, NULL, 0, NULL, #stream_name},
225#define OP_S_CONCLUDE(stream_name) \
226 {OPK_S_CONCLUDE, NULL, 0, NULL, #stream_name},
227#define OP_C_DETACH(stream_name) \
228 {OPK_C_DETACH, NULL, 0, NULL, #stream_name},
229#define OP_C_ATTACH(stream_name) \
230 {OPK_C_ATTACH, NULL, 0, NULL, #stream_name},
231#define OP_C_NEW_STREAM_BIDI(stream_name, expect_id) \
232 {OPK_C_NEW_STREAM, NULL, 0, NULL, #stream_name, (expect_id)},
14551f1e
HL
233#define OP_C_NEW_STREAM_BIDI_EX(stream_name, expect_id, flags) \
234 {OPK_C_NEW_STREAM, NULL, (flags), NULL, #stream_name, (expect_id)},
ed835673 235#define OP_C_NEW_STREAM_UNI(stream_name, expect_id) \
14551f1e
HL
236 {OPK_C_NEW_STREAM, NULL, SSL_STREAM_FLAG_UNI, \
237 NULL, #stream_name, (expect_id)},
238#define OP_C_NEW_STREAM_UNI_EX(stream_name, expect_id, flags) \
239 {OPK_C_NEW_STREAM, NULL, (flags) | SSL_STREAM_FLAG_UNI, \
240 NULL, #stream_name, (expect_id)},
ed835673
HL
241#define OP_S_NEW_STREAM_BIDI(stream_name, expect_id) \
242 {OPK_S_NEW_STREAM, NULL, 0, NULL, #stream_name, (expect_id)},
243#define OP_S_NEW_STREAM_UNI(stream_name, expect_id) \
244 {OPK_S_NEW_STREAM, NULL, 1, NULL, #stream_name, (expect_id)},
a350db73
HL
245#define OP_C_ACCEPT_STREAM_WAIT(stream_name) \
246 {OPK_C_ACCEPT_STREAM_WAIT, NULL, 0, NULL, #stream_name},
ed835673
HL
247#define OP_C_ACCEPT_STREAM_NONE() \
248 {OPK_C_ACCEPT_STREAM_NONE, NULL, 0, NULL, NULL},
249#define OP_C_FREE_STREAM(stream_name) \
250 {OPK_C_FREE_STREAM, NULL, 0, NULL, #stream_name},
251#define OP_C_SET_DEFAULT_STREAM_MODE(mode) \
252 {OPK_C_SET_DEFAULT_STREAM_MODE, NULL, (mode), NULL, NULL},
83df44ae
HL
253#define OP_C_SET_INCOMING_STREAM_POLICY(policy) \
254 {OPK_C_SET_INCOMING_STREAM_POLICY, NULL, (policy), NULL, NULL},
3bc38ba0
HL
255#define OP_C_SHUTDOWN_WAIT(reason, flags) \
256 {OPK_C_SHUTDOWN_WAIT, (reason), (flags), NULL, NULL},
ed835673
HL
257#define OP_C_EXPECT_CONN_CLOSE_INFO(ec, app, remote) \
258 {OPK_C_EXPECT_CONN_CLOSE_INFO, NULL, \
259 ((app) ? EXPECT_CONN_CLOSE_APP : 0) | \
260 ((remote) ? EXPECT_CONN_CLOSE_REMOTE : 0), \
261 NULL, NULL, (ec)},
262#define OP_S_EXPECT_CONN_CLOSE_INFO(ec, app, remote) \
263 {OPK_S_EXPECT_CONN_CLOSE_INFO, NULL, \
264 ((app) ? EXPECT_CONN_CLOSE_APP : 0) | \
265 ((remote) ? EXPECT_CONN_CLOSE_REMOTE : 0), \
266 NULL, NULL, (ec)},
267#define OP_S_BIND_STREAM_ID(stream_name, stream_id) \
268 {OPK_S_BIND_STREAM_ID, NULL, 0, NULL, #stream_name, (stream_id)},
269#define OP_C_WAIT_FOR_DATA(stream_name) \
270 {OPK_C_WAIT_FOR_DATA, NULL, 0, NULL, #stream_name},
271#define OP_C_WRITE_FAIL(stream_name) \
272 {OPK_C_WRITE_FAIL, NULL, 0, NULL, #stream_name},
273#define OP_S_WRITE_FAIL(stream_name) \
274 {OPK_S_WRITE_FAIL, NULL, 0, NULL, #stream_name},
275#define OP_C_READ_FAIL(stream_name) \
276 {OPK_C_READ_FAIL, NULL, 0, NULL, #stream_name},
2f018d14
HL
277#define OP_S_READ_FAIL(stream_name) \
278 {OPK_S_READ_FAIL, NULL, 0, NULL, #stream_name},
ed835673
HL
279#define OP_C_STREAM_RESET(stream_name, aec) \
280 {OPK_C_STREAM_RESET, NULL, 0, NULL, #stream_name, (aec)},
a350db73
HL
281#define OP_S_ACCEPT_STREAM_WAIT(stream_name) \
282 {OPK_S_ACCEPT_STREAM_WAIT, NULL, 0, NULL, #stream_name},
283#define OP_NEW_THREAD(num_threads, script) \
fca44cfc
HL
284 {OPK_NEW_THREAD, (script), (num_threads), NULL, NULL, 0 },
285#define OP_BEGIN_REPEAT(n) \
286 {OPK_BEGIN_REPEAT, NULL, (n)},
287#define OP_END_REPEAT() \
288 {OPK_END_REPEAT},
289#define OP_S_UNBIND_STREAM_ID(stream_name) \
290 {OPK_S_UNBIND_STREAM_ID, NULL, 0, NULL, #stream_name},
0345cac6
TM
291#define OP_C_READ_FAIL_WAIT(stream_name) \
292 {OPK_C_READ_FAIL_WAIT, NULL, 0, NULL, #stream_name},
293#define OP_C_CLOSE_SOCKET() \
294 {OPK_C_CLOSE_SOCKET},
295#define OP_C_EXPECT_SSL_ERR(stream_name, err) \
296 {OPK_C_EXPECT_SSL_ERR, NULL, (err), NULL, #stream_name},
297#define OP_EXPECT_ERR_REASON(err) \
298 {OPK_EXPECT_ERR_REASON, NULL, (err)},
299#define OP_EXPECT_ERR_LIB(lib) \
300 {OPK_EXPECT_ERR_LIB, NULL, (lib)},
301#define OP_SLEEP(ms) \
302 {OPK_SLEEP, NULL, 0, NULL, NULL, (ms)},
e26dc8e3
HL
303#define OP_S_SET_INJECT_PLAIN(f) \
304 {OPK_S_SET_INJECT_PLAIN, NULL, 0, NULL, NULL, 0, (f)},
305#define OP_SET_INJECT_WORD(w0, w1) \
306 {OPK_SET_INJECT_WORD, NULL, (w0), NULL, NULL, (w1), NULL},
cd5e4380
HL
307#define OP_C_INHIBIT_TICK(inhibit) \
308 {OPK_C_INHIBIT_TICK, NULL, (inhibit), NULL, NULL, 0, NULL},
309#define OP_C_SET_WRITE_BUF_SIZE(stream_name, size) \
310 {OPK_C_SET_WRITE_BUF_SIZE, NULL, (size), NULL, #stream_name},
de56eebd
HL
311#define OP_S_SET_INJECT_HANDSHAKE(f) \
312 {OPK_S_SET_INJECT_HANDSHAKE, NULL, 0, NULL, NULL, 0, NULL, (f)},
614c08c2
MC
313#define OP_S_NEW_TICKET() \
314 {OPK_S_NEW_TICKET},
14551f1e
HL
315#define OP_C_SKIP_IF_UNBOUND(stream_name, n) \
316 {OPK_C_SKIP_IF_UNBOUND, NULL, (n), NULL, #stream_name},
69169cd9
HL
317#define OP_S_SET_INJECT_DATAGRAM(f) \
318 {OPK_S_SET_INJECT_DATAGRAM, NULL, 0, NULL, NULL, 0, NULL, NULL, (f)},
3bc38ba0
HL
319#define OP_S_SHUTDOWN(error_code) \
320 {OPK_S_SHUTDOWN, NULL, (error_code)},
ed835673 321
693b23e3
HL
322static OSSL_TIME get_time(void *arg)
323{
324 struct helper *h = arg;
325 OSSL_TIME t;
326
327 if (!TEST_true(CRYPTO_THREAD_read_lock(h->time_lock)))
328 return ossl_time_zero();
329
330 t = ossl_time_add(ossl_time_now(), h->time_slip);
331
332 CRYPTO_THREAD_unlock(h->time_lock);
333 return t;
334}
335
5881dd2c 336static int skip_time_ms(struct helper *h, struct helper_local *hl)
693b23e3
HL
337{
338 if (!TEST_true(CRYPTO_THREAD_write_lock(h->time_lock)))
339 return 0;
340
5881dd2c 341 h->time_slip = ossl_time_add(h->time_slip, ossl_ms2time(hl->check_op->arg2));
693b23e3
HL
342
343 CRYPTO_THREAD_unlock(h->time_lock);
344 return 1;
345}
346
5881dd2c
HL
347static QUIC_TSERVER *s_lock(struct helper *h, struct helper_local *hl);
348static void s_unlock(struct helper *h, struct helper_local *hl);
349
350#define ACQUIRE_S() s_lock(h, hl)
351
352static int check_rejected(struct helper *h, struct helper_local *hl)
ed835673 353{
5881dd2c 354 uint64_t stream_id = hl->check_op->arg2;
ed835673 355
5881dd2c
HL
356 if (!ossl_quic_tserver_stream_has_peer_stop_sending(ACQUIRE_S(), stream_id, NULL)
357 || !ossl_quic_tserver_stream_has_peer_reset_stream(ACQUIRE_S(), stream_id, NULL)) {
9715e3aa 358 h->check_spin_again = 1;
ed835673 359 return 0;
9715e3aa 360 }
ed835673
HL
361
362 return 1;
363}
364
5881dd2c 365static int check_stream_reset(struct helper *h, struct helper_local *hl)
ed835673 366{
5881dd2c 367 uint64_t stream_id = hl->check_op->arg2, aec = 0;
ed835673 368
5881dd2c 369 if (!ossl_quic_tserver_stream_has_peer_reset_stream(ACQUIRE_S(), stream_id, &aec)) {
9715e3aa
HL
370 h->check_spin_again = 1;
371 return 0;
372 }
373
374 return TEST_uint64_t_eq(aec, 42);
ed835673
HL
375}
376
5881dd2c 377static int check_stream_stopped(struct helper *h, struct helper_local *hl)
ed835673 378{
5881dd2c 379 uint64_t stream_id = hl->check_op->arg2;
ed835673 380
5881dd2c 381 if (!ossl_quic_tserver_stream_has_peer_stop_sending(ACQUIRE_S(), stream_id, NULL)) {
9715e3aa
HL
382 h->check_spin_again = 1;
383 return 0;
384 }
385
386 return 1;
ed835673
HL
387}
388
5881dd2c 389static int override_key_update(struct helper *h, struct helper_local *hl)
693b23e3
HL
390{
391 QUIC_CHANNEL *ch = ossl_quic_conn_get_channel(h->c_conn);
392
5881dd2c 393 ossl_quic_channel_set_txku_threshold_override(ch, hl->check_op->arg2);
693b23e3
HL
394 return 1;
395}
396
5881dd2c 397static int trigger_key_update(struct helper *h, struct helper_local *hl)
2525109f
HL
398{
399 if (!TEST_true(SSL_key_update(h->c_conn, SSL_KEY_UPDATE_REQUESTED)))
400 return 0;
401
402 return 1;
403}
404
5881dd2c 405static int check_key_update_ge(struct helper *h, struct helper_local *hl)
693b23e3
HL
406{
407 QUIC_CHANNEL *ch = ossl_quic_conn_get_channel(h->c_conn);
408 int64_t txke = (int64_t)ossl_quic_channel_get_tx_key_epoch(ch);
409 int64_t rxke = (int64_t)ossl_quic_channel_get_rx_key_epoch(ch);
410 int64_t diff = txke - rxke;
411
412 /*
413 * TXKE must always be equal to or ahead of RXKE.
414 * It can be ahead of RXKE by at most 1.
415 */
416 if (!TEST_int64_t_ge(diff, 0) || !TEST_int64_t_le(diff, 1))
417 return 0;
418
419 /* Caller specifies a minimum number of RXKEs which must have happened. */
5881dd2c 420 if (!TEST_uint64_t_ge((uint64_t)rxke, hl->check_op->arg2))
693b23e3
HL
421 return 0;
422
423 return 1;
424}
425
5881dd2c 426static int check_key_update_lt(struct helper *h, struct helper_local *hl)
693b23e3
HL
427{
428 QUIC_CHANNEL *ch = ossl_quic_conn_get_channel(h->c_conn);
429 uint64_t txke = ossl_quic_channel_get_tx_key_epoch(ch);
430
431 /* Caller specifies a maximum number of TXKEs which must have happened. */
5881dd2c 432 if (!TEST_uint64_t_lt(txke, hl->check_op->arg2))
693b23e3
HL
433 return 0;
434
435 return 1;
436}
437
ed835673
HL
438static unsigned long stream_info_hash(const STREAM_INFO *info)
439{
440 return OPENSSL_LH_strhash(info->name);
441}
442
443static int stream_info_cmp(const STREAM_INFO *a, const STREAM_INFO *b)
444{
445 return strcmp(a->name, b->name);
446}
447
448static void cleanup_stream(STREAM_INFO *info)
449{
450 SSL_free(info->c_stream);
451 OPENSSL_free(info);
452}
453
454static void helper_cleanup_streams(LHASH_OF(STREAM_INFO) **lh)
455{
456 if (*lh == NULL)
457 return;
458
459 lh_STREAM_INFO_doall(*lh, cleanup_stream);
460 lh_STREAM_INFO_free(*lh);
461 *lh = NULL;
462}
463
a350db73
HL
464#if defined(OPENSSL_THREADS)
465static CRYPTO_THREAD_RETVAL run_script_child_thread(void *arg);
466
467static int join_threads(struct child_thread_args *threads, size_t num_threads)
468{
469 int ok = 1;
470 size_t i;
471 CRYPTO_THREAD_RETVAL rv;
472
473 for (i = 0; i < num_threads; ++i) {
474 if (threads[i].t != NULL) {
475 ossl_crypto_thread_native_join(threads[i].t, &rv);
476
477 if (!threads[i].testresult)
478 /* Do not log failure here, worker will do it. */
479 ok = 0;
480
481 ossl_crypto_thread_native_clean(threads[i].t);
482 threads[i].t = NULL;
483 }
484
485 ossl_crypto_mutex_free(&threads[i].m);
486 }
487
488 return ok;
489}
5881dd2c
HL
490
491static int join_server_thread(struct helper *h)
492{
493 CRYPTO_THREAD_RETVAL rv;
494
495 if (h->server_thread.t == NULL)
496 return 1;
497
498 ossl_crypto_mutex_lock(h->server_thread.m);
499 h->server_thread.stop = 1;
500 ossl_crypto_mutex_unlock(h->server_thread.m);
501 ossl_crypto_condvar_signal(h->server_thread.c);
502
503 ossl_crypto_thread_native_join(h->server_thread.t, &rv);
504 ossl_crypto_thread_native_clean(h->server_thread.t);
505 h->server_thread.t = NULL;
506 return 1;
507}
508
509/* Ensure the server-state lock is currently held. Idempotent. */
510static int *s_checked_out_p(struct helper *h, int thread_idx)
511{
512 return (thread_idx < 0) ? &h->s_checked_out
513 : &h->threads[thread_idx].s_checked_out;
514}
515
516static QUIC_TSERVER *s_lock(struct helper *h, struct helper_local *hl)
517{
518 int *p_checked_out = s_checked_out_p(h, hl->thread_idx);
519
520 if (h->server_thread.m == NULL || *p_checked_out)
521 return h->s;
522
523 ossl_crypto_mutex_lock(h->server_thread.m);
524 h->s = h->s_priv;
525 *p_checked_out = 1;
526 return h->s;
527}
528
529/* Ensure the server-state lock is currently not held. Idempotent. */
530static void s_unlock(struct helper *h, struct helper_local *hl)
531{
532 int *p_checked_out = s_checked_out_p(h, hl->thread_idx);
533
534 if (h->server_thread.m == NULL || !*p_checked_out)
535 return;
536
537 *p_checked_out = 0;
538 h->s = NULL;
539 ossl_crypto_mutex_unlock(h->server_thread.m);
540}
541
542static unsigned int server_helper_thread(void *arg)
543{
544 struct helper *h = arg;
545
546 ossl_crypto_mutex_lock(h->server_thread.m);
547
548 for (;;) {
549 int ready, stop;
550
551 ready = h->server_thread.ready;
552 stop = h->server_thread.stop;
553
554 if (stop)
555 break;
556
557 if (!ready) {
558 ossl_crypto_condvar_wait(h->server_thread.c, h->server_thread.m);
559 continue;
560 }
561
562 ossl_quic_tserver_tick(h->s_priv);
563 ossl_crypto_mutex_unlock(h->server_thread.m);
025535ec
HL
564 /*
565 * Give the main thread an opportunity to get the mutex, which is
566 * sometimes necessary in some script operations.
567 */
5881dd2c
HL
568 OSSL_sleep(1);
569 ossl_crypto_mutex_lock(h->server_thread.m);
570 }
571
572 ossl_crypto_mutex_unlock(h->server_thread.m);
573 return 1;
574}
575
576#else
577
578static QUIC_TSERVER *s_lock(struct helper *h, struct helper_local *hl)
579{
580 return h->s;
581}
582
583static void s_unlock(struct helper *h, struct helper_local *hl)
584{}
585
a350db73
HL
586#endif
587
ed835673
HL
588static void helper_cleanup(struct helper *h)
589{
a350db73
HL
590#if defined(OPENSSL_THREADS)
591 join_threads(h->threads, h->num_threads);
5881dd2c 592 join_server_thread(h);
a350db73
HL
593 OPENSSL_free(h->threads);
594 h->threads = NULL;
595 h->num_threads = 0;
596#endif
597
97f30fd5
HL
598 if (h->free_order == 0) {
599 /* order 0: streams, then conn */
600 helper_cleanup_streams(&h->c_streams);
601
602 SSL_free(h->c_conn);
603 h->c_conn = NULL;
604 } else {
605 /* order 1: conn, then streams */
606 SSL_free(h->c_conn);
607 h->c_conn = NULL;
608
609 helper_cleanup_streams(&h->c_streams);
610 }
ed835673 611
97f30fd5 612 helper_cleanup_streams(&h->s_streams);
5881dd2c
HL
613 ossl_quic_tserver_free(h->s_priv);
614 h->s_priv = h->s = NULL;
ed835673
HL
615
616 BIO_free(h->s_net_bio_own);
617 h->s_net_bio_own = NULL;
618
619 BIO_free(h->c_net_bio_own);
620 h->c_net_bio_own = NULL;
621
e26dc8e3
HL
622 BIO_free(h->s_qtf_wbio_own);
623 h->s_qtf_wbio_own = NULL;
624
1d547f8f
HL
625 qtest_fault_free(h->qtf);
626 h->qtf = NULL;
7eebc354 627
ed835673
HL
628 if (h->s_fd >= 0) {
629 BIO_closesocket(h->s_fd);
630 h->s_fd = -1;
631 }
632
633 if (h->c_fd >= 0) {
634 BIO_closesocket(h->c_fd);
635 h->c_fd = -1;
636 }
637
638 BIO_ADDR_free(h->s_net_bio_addr);
639 h->s_net_bio_addr = NULL;
84f371a1
RL
640 BIO_ADDR_free(h->s_net_bio_orig_addr);
641 h->s_net_bio_orig_addr = NULL;
ed835673
HL
642
643 SSL_CTX_free(h->c_ctx);
644 h->c_ctx = NULL;
693b23e3
HL
645
646 CRYPTO_THREAD_lock_free(h->time_lock);
647 h->time_lock = NULL;
5881dd2c
HL
648
649#if defined(OPENSSL_THREADS)
99d6b9f9
HL
650 ossl_crypto_mutex_free(&h->misc_m);
651 ossl_crypto_condvar_free(&h->misc_cv);
5881dd2c
HL
652 ossl_crypto_mutex_free(&h->server_thread.m);
653 ossl_crypto_condvar_free(&h->server_thread.c);
654#endif
ed835673
HL
655}
656
5881dd2c
HL
657static int helper_init(struct helper *h, int free_order, int blocking,
658 int need_injector)
ed835673 659{
ed835673
HL
660 struct in_addr ina = {0};
661 QUIC_TSERVER_ARGS s_args = {0};
84f371a1 662 union BIO_sock_info_u info;
ed835673
HL
663
664 memset(h, 0, sizeof(*h));
665 h->c_fd = -1;
666 h->s_fd = -1;
97f30fd5 667 h->free_order = free_order;
5881dd2c 668 h->blocking = blocking;
e26dc8e3 669 h->need_injector = need_injector;
693b23e3
HL
670 h->time_slip = ossl_time_zero();
671
672 if (!TEST_ptr(h->time_lock = CRYPTO_THREAD_lock_new()))
673 goto err;
ed835673
HL
674
675 if (!TEST_ptr(h->s_streams = lh_STREAM_INFO_new(stream_info_hash,
676 stream_info_cmp)))
677 goto err;
678
679 if (!TEST_ptr(h->c_streams = lh_STREAM_INFO_new(stream_info_hash,
680 stream_info_cmp)))
681 goto err;
682
683 ina.s_addr = htonl(0x7f000001UL);
684
685 h->s_fd = BIO_socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP, 0);
686 if (!TEST_int_ge(h->s_fd, 0))
687 goto err;
688
689 if (!TEST_true(BIO_socket_nbio(h->s_fd, 1)))
690 goto err;
691
84f371a1
RL
692 if (!TEST_ptr(h->s_net_bio_orig_addr = BIO_ADDR_new())
693 || !TEST_ptr(h->s_net_bio_addr = BIO_ADDR_new()))
694 goto err;
695
696 if (!TEST_true(BIO_ADDR_rawmake(h->s_net_bio_orig_addr, AF_INET,
697 &ina, sizeof(ina), 0)))
ed835673
HL
698 goto err;
699
84f371a1 700 if (!TEST_true(BIO_bind(h->s_fd, h->s_net_bio_orig_addr, 0)))
ed835673
HL
701 goto err;
702
84f371a1
RL
703 info.addr = h->s_net_bio_addr;
704 if (!TEST_true(BIO_sock_info(h->s_fd, BIO_SOCK_INFO_ADDRESS, &info)))
ed835673
HL
705 goto err;
706
707 if (!TEST_int_gt(BIO_ADDR_rawport(h->s_net_bio_addr), 0))
708 goto err;
709
710 if (!TEST_ptr(h->s_net_bio = h->s_net_bio_own = BIO_new_dgram(h->s_fd, 0)))
711 goto err;
712
713 if (!BIO_up_ref(h->s_net_bio))
714 goto err;
715
e26dc8e3
HL
716 if (need_injector) {
717 h->s_qtf_wbio = h->s_qtf_wbio_own = BIO_new(qtest_get_bio_method());
718 if (!TEST_ptr(h->s_qtf_wbio))
719 goto err;
720
721 if (!TEST_ptr(BIO_push(h->s_qtf_wbio, h->s_net_bio)))
722 goto err;
723
724 s_args.net_wbio = h->s_qtf_wbio;
725 } else {
726 s_args.net_wbio = h->s_net_bio;
727 }
728
693b23e3 729 s_args.net_rbio = h->s_net_bio;
37f27b91 730 s_args.alpn = NULL;
693b23e3
HL
731 s_args.now_cb = get_time;
732 s_args.now_cb_arg = h;
829eec9f 733 s_args.ctx = NULL;
ed835673 734
5881dd2c 735 if (!TEST_ptr(h->s_priv = ossl_quic_tserver_new(&s_args, certfile, keyfile)))
ed835673
HL
736 goto err;
737
5881dd2c
HL
738 if (!blocking)
739 h->s = h->s_priv;
740
e26dc8e3 741 if (need_injector) {
5881dd2c 742 h->qtf = qtest_create_injector(h->s_priv);
e26dc8e3
HL
743 if (!TEST_ptr(h->qtf))
744 goto err;
745
746 BIO_set_data(h->s_qtf_wbio, h->qtf);
747 }
748
18fd0ea0
MC
749 h->s_net_bio_own = NULL;
750 h->s_qtf_wbio_own = NULL;
ed835673
HL
751
752 h->c_fd = BIO_socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP, 0);
753 if (!TEST_int_ge(h->c_fd, 0))
754 goto err;
755
756 if (!TEST_true(BIO_socket_nbio(h->c_fd, 1)))
757 goto err;
758
759 if (!TEST_ptr(h->c_net_bio = h->c_net_bio_own = BIO_new_dgram(h->c_fd, 0)))
760 goto err;
761
762 if (!TEST_true(BIO_dgram_set_peer(h->c_net_bio, h->s_net_bio_addr)))
763 goto err;
764
ed835673
HL
765 if (!TEST_ptr(h->c_ctx = SSL_CTX_new(OSSL_QUIC_client_method())))
766 goto err;
767
768 if (!TEST_ptr(h->c_conn = SSL_new(h->c_ctx)))
769 goto err;
770
693b23e3
HL
771 /* Use custom time function for virtual time skip. */
772 if (!TEST_true(ossl_quic_conn_set_override_now_cb(h->c_conn, get_time, h)))
773 goto err;
774
ed835673
HL
775 /* Takes ownership of our reference to the BIO. */
776 SSL_set0_rbio(h->c_conn, h->c_net_bio);
777 h->c_net_bio_own = NULL;
778
779 if (!TEST_true(BIO_up_ref(h->c_net_bio)))
780 goto err;
781
782 SSL_set0_wbio(h->c_conn, h->c_net_bio);
783
5881dd2c
HL
784 if (!TEST_true(SSL_set_blocking_mode(h->c_conn, h->blocking)))
785 goto err;
786
99d6b9f9
HL
787#if defined(OPENSSL_THREADS)
788 if (!TEST_ptr(h->misc_m = ossl_crypto_mutex_new()))
789 goto err;
790 if (!TEST_ptr(h->misc_cv = ossl_crypto_condvar_new()))
791 goto err;
792#endif
793
5881dd2c
HL
794 if (h->blocking) {
795#if defined(OPENSSL_THREADS)
796 if (!TEST_ptr(h->server_thread.m = ossl_crypto_mutex_new()))
797 goto err;
798
799 if (!TEST_ptr(h->server_thread.c = ossl_crypto_condvar_new()))
800 goto err;
801
802 h->server_thread.t
803 = ossl_crypto_thread_native_start(server_helper_thread, h, 1);
804 if (!TEST_ptr(h->server_thread.t))
805 goto err;
806#else
807 TEST_error("cannot support blocking mode without threads");
ed835673 808 goto err;
5881dd2c
HL
809#endif
810 }
ed835673
HL
811
812 h->start_time = ossl_time_now();
813 h->init = 1;
814 return 1;
815
816err:
817 helper_cleanup(h);
818 return 0;
819}
820
a350db73
HL
821static int helper_local_init(struct helper_local *hl, struct helper *h,
822 int thread_idx)
823{
824 hl->h = h;
825 hl->c_streams = NULL;
826 hl->thread_idx = thread_idx;
827
828 if (!TEST_ptr(h))
829 return 0;
830
831 if (thread_idx < 0) {
832 hl->c_streams = h->c_streams;
833 } else {
834 if (!TEST_ptr(hl->c_streams = lh_STREAM_INFO_new(stream_info_hash,
835 stream_info_cmp)))
836 return 0;
837 }
838
839 return 1;
840}
841
842static void helper_local_cleanup(struct helper_local *hl)
843{
844 if (hl->h == NULL)
845 return;
846
847 if (hl->thread_idx >= 0)
848 helper_cleanup_streams(&hl->c_streams);
849
850 hl->h = NULL;
851}
852
ed835673
HL
853static STREAM_INFO *get_stream_info(LHASH_OF(STREAM_INFO) *lh,
854 const char *stream_name)
855{
856 STREAM_INFO key, *info;
857
858 if (!TEST_ptr(stream_name))
859 return NULL;
860
861 if (!strcmp(stream_name, "DEFAULT"))
862 return NULL;
863
864 key.name = stream_name;
865 info = lh_STREAM_INFO_retrieve(lh, &key);
866 if (info == NULL) {
867 info = OPENSSL_zalloc(sizeof(*info));
868 if (info == NULL)
869 return NULL;
870
871 info->name = stream_name;
872 info->s_stream_id = UINT64_MAX;
873 lh_STREAM_INFO_insert(lh, info);
874 }
875
876 return info;
877}
878
a350db73
HL
879static int helper_local_set_c_stream(struct helper_local *hl,
880 const char *stream_name,
881 SSL *c_stream)
ed835673 882{
a350db73 883 STREAM_INFO *info = get_stream_info(hl->c_streams, stream_name);
ed835673
HL
884
885 if (info == NULL)
886 return 0;
887
888 info->c_stream = c_stream;
889 info->s_stream_id = UINT64_MAX;
890 return 1;
891}
892
a350db73
HL
893static SSL *helper_local_get_c_stream(struct helper_local *hl,
894 const char *stream_name)
ed835673
HL
895{
896 STREAM_INFO *info;
897
898 if (!strcmp(stream_name, "DEFAULT"))
a350db73 899 return hl->h->c_conn;
ed835673 900
a350db73 901 info = get_stream_info(hl->c_streams, stream_name);
ed835673
HL
902 if (info == NULL)
903 return NULL;
904
905 return info->c_stream;
906}
907
908static int
909helper_set_s_stream(struct helper *h, const char *stream_name,
910 uint64_t s_stream_id)
911{
912 STREAM_INFO *info;
913
914 if (!strcmp(stream_name, "DEFAULT"))
915 return 0;
916
917 info = get_stream_info(h->s_streams, stream_name);
918 if (info == NULL)
919 return 0;
920
921 info->c_stream = NULL;
922 info->s_stream_id = s_stream_id;
923 return 1;
924}
925
926static uint64_t helper_get_s_stream(struct helper *h, const char *stream_name)
927{
928 STREAM_INFO *info;
929
930 if (!strcmp(stream_name, "DEFAULT"))
931 return UINT64_MAX;
932
933 info = get_stream_info(h->s_streams, stream_name);
934 if (info == NULL)
935 return UINT64_MAX;
936
937 return info->s_stream_id;
938}
939
e26dc8e3
HL
940static int helper_packet_plain_listener(QTEST_FAULT *qtf, QUIC_PKT_HDR *hdr,
941 unsigned char *buf, size_t buf_len,
942 void *arg)
943{
944 struct helper *h = arg;
945
946 return h->qtf_packet_plain_cb(h, hdr, buf, buf_len);
947}
948
de56eebd
HL
949static int helper_handshake_listener(QTEST_FAULT *fault,
950 unsigned char *buf, size_t buf_len,
951 void *arg)
952{
953 struct helper *h = arg;
954
955 return h->qtf_handshake_cb(h, buf, buf_len);
956}
957
69169cd9
HL
958static int helper_datagram_listener(QTEST_FAULT *fault,
959 BIO_MSG *msg, size_t stride,
960 void *arg)
961{
962 struct helper *h = arg;
963
964 return h->qtf_datagram_cb(h, msg, stride);
965}
966
ed835673
HL
967static int is_want(SSL *s, int ret)
968{
969 int ec = SSL_get_error(s, ret);
970
971 return ec == SSL_ERROR_WANT_READ || ec == SSL_ERROR_WANT_WRITE;
972}
973
9ff81610
HL
974static int check_consistent_want(SSL *s, int ret)
975{
976 int ec = SSL_get_error(s, ret);
977 int w = SSL_want(s);
978
979 int ok = TEST_true(
980 (ec == SSL_ERROR_NONE && w == SSL_NOTHING)
981 || (ec == SSL_ERROR_ZERO_RETURN && w == SSL_NOTHING)
982 || (ec == SSL_ERROR_SSL && w == SSL_NOTHING)
983 || (ec == SSL_ERROR_SYSCALL && w == SSL_NOTHING)
984 || (ec == SSL_ERROR_WANT_READ && w == SSL_READING)
985 || (ec == SSL_ERROR_WANT_WRITE && w == SSL_WRITING)
986 || (ec == SSL_ERROR_WANT_CLIENT_HELLO_CB && w == SSL_CLIENT_HELLO_CB)
987 || (ec == SSL_ERROR_WANT_X509_LOOKUP && w == SSL_X509_LOOKUP)
988 || (ec == SSL_ERROR_WANT_RETRY_VERIFY && w == SSL_RETRY_VERIFY)
989 );
990
991 if (!ok)
992 TEST_error("got error=%d, want=%d", ec, w);
993
994 return ok;
995}
996
a350db73 997static int run_script_worker(struct helper *h, const struct script_op *script,
0786483a 998 const char *script_name,
a350db73 999 int thread_idx)
ed835673 1000{
ed835673 1001 int testresult = 0;
ed835673
HL
1002 unsigned char *tmp_buf = NULL;
1003 int connect_started = 0;
9715e3aa 1004 size_t offset = 0;
a350db73
HL
1005 size_t op_idx = 0;
1006 const struct script_op *op = NULL;
629b408c
HL
1007 int no_advance = 0, first = 1;
1008#if defined(OPENSSL_THREADS)
1009 int end_wait_warning = 0;
1010#endif
a350db73 1011 OSSL_TIME op_start_time = ossl_time_zero(), op_deadline = ossl_time_zero();
5881dd2c 1012 struct helper_local hl_, *hl = &hl_;
fca44cfc
HL
1013#define REPEAT_SLOTS 8
1014 size_t repeat_stack_idx[REPEAT_SLOTS], repeat_stack_done[REPEAT_SLOTS];
1015 size_t repeat_stack_limit[REPEAT_SLOTS];
1016 size_t repeat_stack_len = 0;
ed835673 1017
5881dd2c 1018 if (!TEST_true(helper_local_init(hl, h, thread_idx)))
ed835673
HL
1019 goto out;
1020
769c9b1a
HL
1021#define COMMON_SPIN_AGAIN() \
1022 { \
1023 no_advance = 1; \
1024 continue; \
1025 }
1026#define S_SPIN_AGAIN() \
1027 { \
1028 s_lock(h, hl); \
1029 ossl_quic_tserver_tick(h->s); \
1030 COMMON_SPIN_AGAIN(); \
1031 }
5881dd2c
HL
1032#define C_SPIN_AGAIN() \
1033 { \
1034 if (h->blocking) { \
1035 TEST_error("spin again in blocking mode"); \
1036 goto out; \
1037 } \
769c9b1a 1038 COMMON_SPIN_AGAIN(); \
5881dd2c 1039 }
ed835673 1040
9715e3aa 1041 for (;;) {
a350db73 1042 SSL *c_tgt = h->c_conn;
ed835673
HL
1043 uint64_t s_stream_id = UINT64_MAX;
1044
5881dd2c
HL
1045 s_unlock(h, hl);
1046
9715e3aa
HL
1047 if (no_advance) {
1048 no_advance = 0;
1049 } else {
1050 if (!first)
1051 ++op_idx;
1052
1053 first = 0;
4f2d32d6 1054 offset = 0;
9715e3aa 1055 op_start_time = ossl_time_now();
cd5e4380 1056 op_deadline = ossl_time_add(op_start_time, ossl_ms2time(8000));
9715e3aa
HL
1057 }
1058
1059 if (!TEST_int_le(ossl_time_compare(ossl_time_now(), op_deadline), 0)) {
a350db73 1060 TEST_error("op %zu timed out on thread %d", op_idx + 1, thread_idx);
9715e3aa
HL
1061 goto out;
1062 }
1063
ed835673
HL
1064 op = &script[op_idx];
1065
1066 if (op->stream_name != NULL) {
5881dd2c 1067 c_tgt = helper_local_get_c_stream(hl, op->stream_name);
a350db73
HL
1068 if (thread_idx < 0)
1069 s_stream_id = helper_get_s_stream(h, op->stream_name);
1070 else
1071 s_stream_id = UINT64_MAX;
1072 }
1073
5881dd2c
HL
1074 if (thread_idx < 0) {
1075 if (!h->blocking) {
1076 ossl_quic_tserver_tick(h->s);
1077 }
1078#if defined(OPENSSL_THREADS)
1079 else if (h->blocking && !h->server_thread.ready) {
1080 ossl_crypto_mutex_lock(h->server_thread.m);
1081 h->server_thread.ready = 1;
1082 ossl_crypto_mutex_unlock(h->server_thread.m);
1083 ossl_crypto_condvar_signal(h->server_thread.c);
1084 }
1085 if (h->blocking)
1086 assert(h->s == NULL);
1087#endif
1088 }
7ba8f79a
HL
1089
1090 if (thread_idx >= 0 || connect_started)
6084e04b 1091 SSL_handle_events(h->c_conn);
ed835673 1092
a350db73
HL
1093 if (thread_idx >= 0) {
1094 /* Only allow certain opcodes on child threads. */
1095 switch (op->op) {
1096 case OPK_END:
99d6b9f9 1097 case OPK_CHECK:
a350db73
HL
1098 case OPK_C_ACCEPT_STREAM_WAIT:
1099 case OPK_C_NEW_STREAM:
1100 case OPK_C_READ_EXPECT:
1101 case OPK_C_EXPECT_FIN:
1102 case OPK_C_WRITE:
1103 case OPK_C_CONCLUDE:
1104 case OPK_C_FREE_STREAM:
fca44cfc
HL
1105 case OPK_BEGIN_REPEAT:
1106 case OPK_END_REPEAT:
0345cac6
TM
1107 case OPK_C_READ_FAIL_WAIT:
1108 case OPK_C_EXPECT_SSL_ERR:
1109 case OPK_EXPECT_ERR_REASON:
1110 case OPK_EXPECT_ERR_LIB:
1111 case OPK_SLEEP:
a350db73
HL
1112 break;
1113
1114 default:
0cea6df2
MC
1115 TEST_error("opcode %lu not allowed on child thread",
1116 (unsigned long)op->op);
a350db73
HL
1117 goto out;
1118 }
1119 }
ed835673
HL
1120
1121 switch (op->op) {
1122 case OPK_END:
fca44cfc
HL
1123 if (!TEST_size_t_eq(repeat_stack_len, 0))
1124 goto out;
1125
629b408c 1126#if defined(OPENSSL_THREADS)
a350db73
HL
1127 if (thread_idx < 0) {
1128 int done;
1129 size_t i;
1130
1131 for (i = 0; i < h->num_threads; ++i) {
1132 if (h->threads[i].m == NULL)
1133 continue;
1134
1135 ossl_crypto_mutex_lock(h->threads[i].m);
1136 done = h->threads[i].done;
1137 ossl_crypto_mutex_unlock(h->threads[i].m);
1138
1139 if (!done) {
1140 if (!end_wait_warning) {
1141 TEST_info("still waiting for other threads to finish (%zu)", i);
1142 end_wait_warning = 1;
1143 }
1144
5881dd2c 1145 S_SPIN_AGAIN();
a350db73
HL
1146 }
1147 }
1148 }
629b408c 1149#endif
a350db73 1150
0786483a 1151 TEST_info("script \"%s\" finished on thread %d", script_name, thread_idx);
ed835673
HL
1152 testresult = 1;
1153 goto out;
1154
fca44cfc
HL
1155 case OPK_BEGIN_REPEAT:
1156 if (!TEST_size_t_lt(repeat_stack_len, OSSL_NELEM(repeat_stack_idx)))
1157 goto out;
1158
1159 if (!TEST_size_t_gt(op->arg1, 0))
1160 goto out;
1161
1162 repeat_stack_idx[repeat_stack_len] = op_idx + 1;
1163 repeat_stack_done[repeat_stack_len] = 0;
1164 repeat_stack_limit[repeat_stack_len] = op->arg1;
1165 ++repeat_stack_len;
1166 break;
1167
14551f1e
HL
1168 case OPK_C_SKIP_IF_UNBOUND:
1169 if (c_tgt != NULL)
1170 break;
1171
1172 op_idx += op->arg1;
1173 break;
1174
fca44cfc
HL
1175 case OPK_END_REPEAT:
1176 if (!TEST_size_t_gt(repeat_stack_len, 0))
1177 goto out;
1178
1179 if (++repeat_stack_done[repeat_stack_len - 1]
1180 == repeat_stack_limit[repeat_stack_len - 1]) {
1181 --repeat_stack_len;
1182 } else {
1183 op_idx = repeat_stack_idx[repeat_stack_len - 1];
1184 no_advance = 1;
1185 continue;
1186 }
1187
1188 break;
1189
ed835673 1190 case OPK_CHECK:
9715e3aa 1191 {
5881dd2c
HL
1192 int ok;
1193
1194 hl->check_op = op;
1195 ok = op->check_func(h, hl);
1196 hl->check_op = NULL;
1197
99d6b9f9 1198 if (thread_idx < 0 && h->check_spin_again) {
a350db73 1199 h->check_spin_again = 0;
5881dd2c 1200 S_SPIN_AGAIN();
9715e3aa 1201 }
ed835673 1202
9715e3aa
HL
1203 if (!TEST_true(ok))
1204 goto out;
1205 }
ed835673
HL
1206 break;
1207
1208 case OPK_C_SET_ALPN:
1209 {
1210 const char *alpn = op->arg0;
1211 size_t alpn_len = strlen(alpn);
1212
1213 if (!TEST_size_t_le(alpn_len, UINT8_MAX)
1214 || !TEST_ptr(tmp_buf = (unsigned char *)OPENSSL_malloc(alpn_len + 1)))
1215 goto out;
1216
1217 memcpy(tmp_buf + 1, alpn, alpn_len);
1218 tmp_buf[0] = (unsigned char)alpn_len;
1219
1220 /* 0 is the success case for SSL_set_alpn_protos(). */
a350db73 1221 if (!TEST_false(SSL_set_alpn_protos(h->c_conn, tmp_buf,
ed835673
HL
1222 alpn_len + 1)))
1223 goto out;
1224
1225 OPENSSL_free(tmp_buf);
1226 tmp_buf = NULL;
1227 }
1228 break;
1229
1230 case OPK_C_CONNECT_WAIT:
1231 {
1232 int ret;
1233
1234 connect_started = 1;
1235
a350db73 1236 ret = SSL_connect(h->c_conn);
9ff81610
HL
1237 if (!check_consistent_want(c_tgt, ret))
1238 goto out;
a1d2a9d1
HL
1239 if (ret != 1) {
1240 if (!h->blocking && is_want(h->c_conn, ret))
5881dd2c 1241 C_SPIN_AGAIN();
ed835673 1242
a1d2a9d1
HL
1243 if (op->arg1 == 0 && !TEST_int_eq(ret, 1))
1244 goto out;
1245 }
ed835673
HL
1246 }
1247 break;
1248
1249 case OPK_C_WRITE:
1250 {
1251 size_t bytes_written = 0;
9ff81610 1252 int r;
ed835673
HL
1253
1254 if (!TEST_ptr(c_tgt))
1255 goto out;
1256
9ff81610
HL
1257 r = SSL_write_ex(c_tgt, op->arg0, op->arg1, &bytes_written);
1258 if (!TEST_true(r)
1259 || !check_consistent_want(c_tgt, r)
ed835673
HL
1260 || !TEST_size_t_eq(bytes_written, op->arg1))
1261 goto out;
1262 }
1263 break;
1264
1265 case OPK_S_WRITE:
1266 {
1267 size_t bytes_written = 0;
1268
1269 if (!TEST_uint64_t_ne(s_stream_id, UINT64_MAX))
1270 goto out;
1271
5881dd2c 1272 if (!TEST_true(ossl_quic_tserver_write(ACQUIRE_S(), s_stream_id,
ed835673
HL
1273 op->arg0, op->arg1,
1274 &bytes_written))
1275 || !TEST_size_t_eq(bytes_written, op->arg1))
1276 goto out;
1277 }
1278 break;
1279
1280 case OPK_C_CONCLUDE:
1281 {
1282 if (!TEST_true(SSL_stream_conclude(c_tgt, 0)))
1283 goto out;
1284 }
1285 break;
1286
1287 case OPK_S_CONCLUDE:
1288 {
1289 if (!TEST_uint64_t_ne(s_stream_id, UINT64_MAX))
1290 goto out;
1291
5881dd2c 1292 ossl_quic_tserver_conclude(ACQUIRE_S(), s_stream_id);
ed835673
HL
1293 }
1294 break;
1295
1296 case OPK_C_WAIT_FOR_DATA:
1297 {
1298 char buf[1];
1299 size_t bytes_read = 0;
1300
1301 if (!TEST_ptr(c_tgt))
1302 goto out;
1303
1304 if (!SSL_peek_ex(c_tgt, buf, sizeof(buf), &bytes_read)
1305 || bytes_read == 0)
5881dd2c 1306 C_SPIN_AGAIN();
ed835673
HL
1307 }
1308 break;
1309
1310 case OPK_C_READ_EXPECT:
1311 {
1312 size_t bytes_read = 0;
9ff81610 1313 int r;
ed835673 1314
9715e3aa
HL
1315 if (op->arg1 > 0 && tmp_buf == NULL
1316 && !TEST_ptr(tmp_buf = OPENSSL_malloc(op->arg1)))
ed835673
HL
1317 goto out;
1318
9ff81610
HL
1319 r = SSL_read_ex(c_tgt, tmp_buf + offset, op->arg1 - offset,
1320 &bytes_read);
1321 if (!check_consistent_want(c_tgt, r))
1322 goto out;
1323
1324 if (!r)
5881dd2c 1325 C_SPIN_AGAIN();
ed835673 1326
9715e3aa
HL
1327 if (bytes_read + offset != op->arg1) {
1328 offset += bytes_read;
5881dd2c 1329 C_SPIN_AGAIN();
9715e3aa
HL
1330 }
1331
1332 if (op->arg1 > 0
1333 && !TEST_mem_eq(tmp_buf, op->arg1, op->arg0, op->arg1))
ed835673
HL
1334 goto out;
1335
1336 OPENSSL_free(tmp_buf);
1337 tmp_buf = NULL;
1338 }
1339 break;
1340
1341 case OPK_S_READ_EXPECT:
1342 {
1343 size_t bytes_read = 0;
1344
1345 if (!TEST_uint64_t_ne(s_stream_id, UINT64_MAX))
1346 goto out;
1347
9715e3aa 1348 if (op->arg1 > 0 && tmp_buf == NULL
ed835673
HL
1349 && !TEST_ptr(tmp_buf = OPENSSL_malloc(op->arg1)))
1350 goto out;
1351
5881dd2c 1352 if (!TEST_true(ossl_quic_tserver_read(ACQUIRE_S(), s_stream_id,
9715e3aa
HL
1353 tmp_buf + offset,
1354 op->arg1 - offset,
1355 &bytes_read)))
ed835673
HL
1356 goto out;
1357
9715e3aa
HL
1358 if (bytes_read + offset != op->arg1) {
1359 offset += bytes_read;
5881dd2c 1360 S_SPIN_AGAIN();
9715e3aa
HL
1361 }
1362
ed835673
HL
1363 if (op->arg1 > 0
1364 && !TEST_mem_eq(tmp_buf, op->arg1, op->arg0, op->arg1))
1365 goto out;
1366
1367 OPENSSL_free(tmp_buf);
1368 tmp_buf = NULL;
1369 }
1370 break;
1371
1372 case OPK_C_EXPECT_FIN:
1373 {
1374 char buf[1];
1375 size_t bytes_read = 0;
9ff81610 1376 int r;
ed835673 1377
9ff81610
HL
1378 r = SSL_read_ex(c_tgt, buf, sizeof(buf), &bytes_read);
1379 if (!check_consistent_want(c_tgt, r)
1380 || !TEST_false(r)
9715e3aa
HL
1381 || !TEST_size_t_eq(bytes_read, 0))
1382 goto out;
1383
1384 if (is_want(c_tgt, 0))
5881dd2c 1385 C_SPIN_AGAIN();
9715e3aa
HL
1386
1387 if (!TEST_int_eq(SSL_get_error(c_tgt, 0),
1388 SSL_ERROR_ZERO_RETURN))
1389 goto out;
9ff81610
HL
1390
1391 if (!TEST_int_eq(SSL_want(c_tgt), SSL_NOTHING))
1392 goto out;
ed835673
HL
1393 }
1394 break;
1395
1396 case OPK_S_EXPECT_FIN:
1397 {
9715e3aa 1398 if (!TEST_uint64_t_ne(s_stream_id, UINT64_MAX))
ed835673 1399 goto out;
9715e3aa 1400
5881dd2c
HL
1401 if (!ossl_quic_tserver_has_read_ended(ACQUIRE_S(), s_stream_id))
1402 S_SPIN_AGAIN();
ed835673
HL
1403 }
1404 break;
1405
1406 case OPK_C_DETACH:
1407 {
1408 SSL *c_stream;
1409
1410 if (!TEST_ptr_null(c_tgt))
1411 goto out; /* don't overwrite existing stream with same name */
1412
dbf247ad
HL
1413 if (!TEST_ptr(op->stream_name))
1414 goto out;
1415
a350db73 1416 if (!TEST_ptr(c_stream = ossl_quic_detach_stream(h->c_conn)))
ed835673
HL
1417 goto out;
1418
5881dd2c 1419 if (!TEST_true(helper_local_set_c_stream(hl, op->stream_name, c_stream)))
ed835673
HL
1420 goto out;
1421 }
1422 break;
1423
1424 case OPK_C_ATTACH:
1425 {
1426 if (!TEST_ptr(c_tgt))
1427 goto out;
1428
dbf247ad
HL
1429 if (!TEST_ptr(op->stream_name))
1430 goto out;
1431
a350db73 1432 if (!TEST_true(ossl_quic_attach_stream(h->c_conn, c_tgt)))
ed835673
HL
1433 goto out;
1434
5881dd2c 1435 if (!TEST_true(helper_local_set_c_stream(hl, op->stream_name, NULL)))
ed835673
HL
1436 goto out;
1437 }
1438 break;
1439
1440 case OPK_C_NEW_STREAM:
1441 {
1442 SSL *c_stream;
14551f1e
HL
1443 uint64_t flags = op->arg1;
1444 int allow_fail = ((flags & ALLOW_FAIL) != 0);
1445
1446 flags &= ~(uint64_t)ALLOW_FAIL;
ed835673
HL
1447
1448 if (!TEST_ptr_null(c_tgt))
1449 goto out; /* don't overwrite existing stream with same name */
1450
dbf247ad
HL
1451 if (!TEST_ptr(op->stream_name))
1452 goto out;
1453
14551f1e
HL
1454 c_stream = SSL_new_stream(h->c_conn, flags);
1455 if (!allow_fail && !TEST_ptr(c_stream))
ed835673
HL
1456 goto out;
1457
14551f1e
HL
1458 if (allow_fail && c_stream == NULL) {
1459 if (!TEST_size_t_eq(ERR_GET_REASON(ERR_get_error()),
1460 SSL_R_STREAM_COUNT_LIMITED))
1461 goto out;
1462
1463 ++h->fail_count;
1464 break;
1465 }
1466
ed835673
HL
1467 if (op->arg2 != UINT64_MAX
1468 && !TEST_uint64_t_eq(SSL_get_stream_id(c_stream),
1469 op->arg2))
1470 goto out;
1471
5881dd2c 1472 if (!TEST_true(helper_local_set_c_stream(hl, op->stream_name, c_stream)))
ed835673
HL
1473 goto out;
1474 }
1475 break;
1476
1477 case OPK_S_NEW_STREAM:
1478 {
1479 uint64_t stream_id = UINT64_MAX;
1480
1481 if (!TEST_uint64_t_eq(s_stream_id, UINT64_MAX))
1482 goto out; /* don't overwrite existing stream with same name */
1483
dbf247ad
HL
1484 if (!TEST_ptr(op->stream_name))
1485 goto out;
1486
5881dd2c 1487 if (!TEST_true(ossl_quic_tserver_stream_new(ACQUIRE_S(),
ed835673
HL
1488 op->arg1 > 0,
1489 &stream_id)))
1490 goto out;
1491
1492 if (op->arg2 != UINT64_MAX
1493 && !TEST_uint64_t_eq(stream_id, op->arg2))
1494 goto out;
1495
a350db73 1496 if (!TEST_true(helper_set_s_stream(h, op->stream_name,
ed835673
HL
1497 stream_id)))
1498 goto out;
1499 }
1500 break;
1501
a350db73 1502 case OPK_C_ACCEPT_STREAM_WAIT:
ed835673
HL
1503 {
1504 SSL *c_stream;
1505
1506 if (!TEST_ptr_null(c_tgt))
1507 goto out; /* don't overwrite existing stream with same name */
1508
dbf247ad
HL
1509 if (!TEST_ptr(op->stream_name))
1510 goto out;
1511
a350db73 1512 if ((c_stream = SSL_accept_stream(h->c_conn, 0)) == NULL)
5881dd2c 1513 C_SPIN_AGAIN();
ed835673 1514
5881dd2c 1515 if (!TEST_true(helper_local_set_c_stream(hl, op->stream_name,
a350db73
HL
1516 c_stream)))
1517 goto out;
1518 }
1519 break;
1520
1521 case OPK_S_ACCEPT_STREAM_WAIT:
1522 {
1523 uint64_t new_stream_id;
1524
1525 if (!TEST_uint64_t_eq(s_stream_id, UINT64_MAX))
1526 goto out;
1527
dbf247ad
HL
1528 if (!TEST_ptr(op->stream_name))
1529 goto out;
1530
5881dd2c 1531 new_stream_id = ossl_quic_tserver_pop_incoming_stream(ACQUIRE_S());
a350db73 1532 if (new_stream_id == UINT64_MAX)
5881dd2c 1533 S_SPIN_AGAIN();
a350db73
HL
1534
1535 if (!TEST_true(helper_set_s_stream(h, op->stream_name, new_stream_id)))
ed835673
HL
1536 goto out;
1537 }
1538 break;
1539
1540 case OPK_C_ACCEPT_STREAM_NONE:
1541 {
1542 SSL *c_stream;
1543
5881dd2c
HL
1544 if (!TEST_ptr_null(c_stream = SSL_accept_stream(h->c_conn,
1545 SSL_ACCEPT_STREAM_NO_BLOCK))) {
ed835673
HL
1546 SSL_free(c_stream);
1547 goto out;
1548 }
1549 }
1550 break;
1551
1552 case OPK_C_FREE_STREAM:
1553 {
1554 if (!TEST_ptr(c_tgt)
1555 || !TEST_true(!SSL_is_connection(c_tgt)))
1556 goto out;
1557
dbf247ad
HL
1558 if (!TEST_ptr(op->stream_name))
1559 goto out;
1560
5881dd2c 1561 if (!TEST_true(helper_local_set_c_stream(hl, op->stream_name, NULL)))
ed835673
HL
1562 goto out;
1563
1564 SSL_free(c_tgt);
1565 c_tgt = NULL;
1566 }
1567 break;
1568
1569 case OPK_C_SET_DEFAULT_STREAM_MODE:
1570 {
1571 if (!TEST_ptr(c_tgt))
1572 goto out;
1573
1574 if (!TEST_true(SSL_set_default_stream_mode(c_tgt, op->arg1)))
1575 goto out;
1576 }
1577 break;
1578
83df44ae 1579 case OPK_C_SET_INCOMING_STREAM_POLICY:
ed835673
HL
1580 {
1581 if (!TEST_ptr(c_tgt))
1582 goto out;
1583
83df44ae
HL
1584 if (!TEST_true(SSL_set_incoming_stream_policy(c_tgt,
1585 op->arg1, 0)))
ed835673
HL
1586 goto out;
1587 }
1588 break;
1589
cd5e4380 1590 case OPK_C_SHUTDOWN_WAIT:
ed835673
HL
1591 {
1592 int ret;
cd5e4380 1593 QUIC_CHANNEL *ch = ossl_quic_conn_get_channel(h->c_conn);
d49a1634 1594 SSL_SHUTDOWN_EX_ARGS args = {0};
cd5e4380
HL
1595
1596 ossl_quic_channel_set_inhibit_tick(ch, 0);
ed835673
HL
1597
1598 if (!TEST_ptr(c_tgt))
1599 goto out;
1600
d49a1634
HL
1601 args.quic_reason = (const char *)op->arg0;
1602
3bc38ba0 1603 ret = SSL_shutdown_ex(c_tgt, op->arg1, &args, sizeof(args));
ed835673
HL
1604 if (!TEST_int_ge(ret, 0))
1605 goto out;
1606
cd5e4380 1607 if (ret == 0)
5881dd2c 1608 C_SPIN_AGAIN();
ed835673
HL
1609 }
1610 break;
1611
3bc38ba0
HL
1612 case OPK_S_SHUTDOWN:
1613 {
5881dd2c 1614 ossl_quic_tserver_shutdown(ACQUIRE_S(), op->arg1);
3bc38ba0
HL
1615 }
1616 break;
1617
ed835673
HL
1618 case OPK_C_EXPECT_CONN_CLOSE_INFO:
1619 {
1620 SSL_CONN_CLOSE_INFO cc_info = {0};
1621 int expect_app = (op->arg1 & EXPECT_CONN_CLOSE_APP) != 0;
1622 int expect_remote = (op->arg1 & EXPECT_CONN_CLOSE_REMOTE) != 0;
1623 uint64_t error_code = op->arg2;
1624
1625 if (!TEST_ptr(c_tgt))
1626 goto out;
1627
5881dd2c
HL
1628 if (h->blocking
1629 && !TEST_true(SSL_shutdown_ex(c_tgt,
1630 SSL_SHUTDOWN_FLAG_WAIT_PEER,
1631 NULL, 0)))
1632 goto out;
1633
9715e3aa 1634 if (!SSL_get_conn_close_info(c_tgt, &cc_info, sizeof(cc_info)))
5881dd2c 1635 C_SPIN_AGAIN();
ed835673 1636
7d9e447a
HL
1637 if (!TEST_int_eq(expect_app,
1638 (cc_info.flags
1639 & SSL_CONN_CLOSE_FLAG_TRANSPORT) == 0)
1640 || !TEST_int_eq(expect_remote,
1641 (cc_info.flags
1642 & SSL_CONN_CLOSE_FLAG_LOCAL) == 0)
ed835673
HL
1643 || !TEST_uint64_t_eq(error_code, cc_info.error_code))
1644 goto out;
1645 }
1646 break;
1647
1648 case OPK_S_EXPECT_CONN_CLOSE_INFO:
1649 {
1650 const QUIC_TERMINATE_CAUSE *tc;
1651 int expect_app = (op->arg1 & EXPECT_CONN_CLOSE_APP) != 0;
1652 int expect_remote = (op->arg1 & EXPECT_CONN_CLOSE_REMOTE) != 0;
1653 uint64_t error_code = op->arg2;
1654
5881dd2c
HL
1655 if (!ossl_quic_tserver_is_term_any(ACQUIRE_S())) {
1656 ossl_quic_tserver_ping(ACQUIRE_S());
1657 S_SPIN_AGAIN();
9ff3a99e 1658 }
ed835673 1659
5881dd2c 1660 if (!TEST_ptr(tc = ossl_quic_tserver_get_terminate_cause(ACQUIRE_S())))
ed835673
HL
1661 goto out;
1662
1663 if (!TEST_uint64_t_eq(error_code, tc->error_code)
1664 || !TEST_int_eq(expect_app, tc->app)
1665 || !TEST_int_eq(expect_remote, tc->remote))
1666 goto out;
1667 }
1668 break;
1669
1670 case OPK_S_BIND_STREAM_ID:
1671 {
1672 if (!TEST_uint64_t_eq(s_stream_id, UINT64_MAX))
1673 goto out;
1674
dbf247ad
HL
1675 if (!TEST_ptr(op->stream_name))
1676 goto out;
1677
a350db73 1678 if (!TEST_true(helper_set_s_stream(h, op->stream_name, op->arg2)))
ed835673
HL
1679 goto out;
1680 }
1681 break;
1682
fca44cfc
HL
1683 case OPK_S_UNBIND_STREAM_ID:
1684 {
1685 if (!TEST_uint64_t_ne(s_stream_id, UINT64_MAX))
1686 goto out;
1687
dbf247ad
HL
1688 if (!TEST_ptr(op->stream_name))
1689 goto out;
1690
fca44cfc
HL
1691 if (!TEST_true(helper_set_s_stream(h, op->stream_name, UINT64_MAX)))
1692 goto out;
1693 }
1694 break;
1695
ed835673
HL
1696 case OPK_C_WRITE_FAIL:
1697 {
571aff4b 1698 size_t bytes_written = 0;
9ff81610 1699 int r;
ed835673
HL
1700
1701 if (!TEST_ptr(c_tgt))
1702 goto out;
1703
9ff81610
HL
1704 r = SSL_write_ex(c_tgt, "apple", 5, &bytes_written);
1705 if (!TEST_false(r)
1706 || !check_consistent_want(c_tgt, r))
ed835673
HL
1707 goto out;
1708 }
1709 break;
1710
1711 case OPK_S_WRITE_FAIL:
1712 {
1713 size_t bytes_written = 0;
1714
1715 if (!TEST_uint64_t_ne(s_stream_id, UINT64_MAX))
1716 goto out;
1717
5881dd2c 1718 if (!TEST_false(ossl_quic_tserver_write(ACQUIRE_S(), s_stream_id,
ed835673
HL
1719 (const unsigned char *)"apple", 5,
1720 &bytes_written)))
1721 goto out;
1722 }
1723 break;
1724
1725 case OPK_C_READ_FAIL:
1726 {
571aff4b 1727 size_t bytes_read = 0;
ed835673 1728 char buf[1];
9ff81610 1729 int r;
ed835673
HL
1730
1731 if (!TEST_ptr(c_tgt))
1732 goto out;
1733
9ff81610
HL
1734 r = SSL_read_ex(c_tgt, buf, sizeof(buf), &bytes_read);
1735 if (!TEST_false(r))
1736 goto out;
1737 if (!check_consistent_want(c_tgt, r))
ed835673
HL
1738 goto out;
1739 }
1740 break;
1741
0345cac6
TM
1742 case OPK_C_READ_FAIL_WAIT:
1743 {
1744 size_t bytes_read = 0;
1745 char buf[1];
9ff81610 1746 int r;
0345cac6
TM
1747
1748 if (!TEST_ptr(c_tgt))
1749 goto out;
1750
9ff81610
HL
1751 r = SSL_read_ex(c_tgt, buf, sizeof(buf), &bytes_read);
1752 if (!TEST_false(r))
1753 goto out;
1754 if (!check_consistent_want(c_tgt, r))
0345cac6
TM
1755 goto out;
1756
1757 if (is_want(c_tgt, 0))
5881dd2c 1758 C_SPIN_AGAIN();
0345cac6
TM
1759 }
1760 break;
1761
2f018d14
HL
1762 case OPK_S_READ_FAIL:
1763 {
1764 size_t bytes_read = 0;
1765 unsigned char buf[1];
1766
1767 if (!TEST_uint64_t_ne(s_stream_id, UINT64_MAX))
1768 goto out;
1769
5881dd2c 1770 if (!TEST_false(ossl_quic_tserver_read(ACQUIRE_S(), s_stream_id,
2f018d14
HL
1771 buf, sizeof(buf),
1772 &bytes_read)))
1773 goto out;
1774 }
1775 break;
1776
ed835673
HL
1777 case OPK_C_STREAM_RESET:
1778 {
1779 SSL_STREAM_RESET_ARGS args = {0};
1780
1781 if (!TEST_ptr(c_tgt))
1782 goto out;
1783
1784 args.quic_error_code = op->arg2;
1785
1786 if (!TEST_true(SSL_stream_reset(c_tgt, &args, sizeof(args))))
1787 goto out;
1788 }
1789 break;
1790
a350db73
HL
1791 case OPK_NEW_THREAD:
1792 {
1793#if !defined(OPENSSL_THREADS)
629b408c
HL
1794 /*
1795 * If this test script requires threading and we do not have
1796 * support for it, skip the rest of it.
1797 */
1798 TEST_skip("threading not supported, skipping");
1799 testresult = 1;
a350db73
HL
1800 goto out;
1801#else
1802 size_t i;
1803
1804 if (!TEST_ptr_null(h->threads)) {
1805 TEST_error("max one NEW_THREAD operation per script");
1806 goto out;
1807 }
1808
1809 h->threads = OPENSSL_zalloc(op->arg1 * sizeof(struct child_thread_args));
1810 if (!TEST_ptr(h->threads))
1811 goto out;
1812
1813 h->num_threads = op->arg1;
1814
1815 for (i = 0; i < op->arg1; ++i) {
1816 h->threads[i].h = h;
1817 h->threads[i].script = op->arg0;
0786483a 1818 h->threads[i].script_name = script_name;
a350db73
HL
1819 h->threads[i].thread_idx = i;
1820
1821 h->threads[i].m = ossl_crypto_mutex_new();
1822 if (!TEST_ptr(h->threads[i].m))
1823 goto out;
1824
1825 h->threads[i].t
1826 = ossl_crypto_thread_native_start(run_script_child_thread,
1827 &h->threads[i], 1);
1828 if (!TEST_ptr(h->threads[i].t))
1829 goto out;
1830 }
1831#endif
1832 }
1833 break;
1834
0345cac6
TM
1835 case OPK_C_CLOSE_SOCKET:
1836 {
1837 BIO_closesocket(h->c_fd);
410a90f5 1838 h->c_fd = -1;
0345cac6
TM
1839 }
1840 break;
1841
1842 case OPK_C_EXPECT_SSL_ERR:
1843 {
1844 if (!TEST_size_t_eq((size_t)SSL_get_error(c_tgt, 0), op->arg1))
1845 goto out;
9ff81610
HL
1846 if (!TEST_int_eq(SSL_want(c_tgt), SSL_NOTHING))
1847 goto out;
0345cac6
TM
1848 }
1849 break;
1850
1851 case OPK_EXPECT_ERR_REASON:
1852 {
1853 if (!TEST_size_t_eq((size_t)ERR_GET_REASON(ERR_get_error()), op->arg1))
1854 goto out;
1855 }
1856 break;
1857
1858 case OPK_EXPECT_ERR_LIB:
1859 {
1860 if (!TEST_size_t_eq((size_t)ERR_GET_LIB(ERR_get_error()), op->arg1))
1861 goto out;
1862 }
1863 break;
1864
1865 case OPK_SLEEP:
1866 {
1867 OSSL_sleep(op->arg2);
1868 }
1869 break;
1870
e26dc8e3
HL
1871 case OPK_S_SET_INJECT_PLAIN:
1872 h->qtf_packet_plain_cb = op->qtf_packet_plain_cb;
1873
1874 if (!TEST_true(qtest_fault_set_packet_plain_listener(h->qtf,
1875 h->qtf_packet_plain_cb != NULL ?
1876 helper_packet_plain_listener : NULL,
1877 h)))
1878 goto out;
1879
1880 break;
1881
de56eebd
HL
1882 case OPK_S_SET_INJECT_HANDSHAKE:
1883 h->qtf_handshake_cb = op->qtf_handshake_cb;
1884
1885 if (!TEST_true(qtest_fault_set_handshake_listener(h->qtf,
1886 h->qtf_handshake_cb != NULL ?
1887 helper_handshake_listener : NULL,
1888 h)))
1889 goto out;
1890
1891 break;
1892
69169cd9
HL
1893 case OPK_S_SET_INJECT_DATAGRAM:
1894 h->qtf_datagram_cb = op->qtf_datagram_cb;
1895
1896 if (!TEST_true(qtest_fault_set_datagram_listener(h->qtf,
1897 h->qtf_datagram_cb != NULL ?
1898 helper_datagram_listener : NULL,
1899 h)))
1900 goto out;
1901
1902 break;
1903
e26dc8e3 1904 case OPK_SET_INJECT_WORD:
5881dd2c
HL
1905 /*
1906 * Must hold server tick lock - callbacks can be called from other
1907 * thread when running test in blocking mode (tsan).
1908 */
1909 ACQUIRE_S();
e26dc8e3
HL
1910 h->inject_word0 = op->arg1;
1911 h->inject_word1 = op->arg2;
1912 break;
1913
cd5e4380
HL
1914 case OPK_C_INHIBIT_TICK:
1915 {
1916 QUIC_CHANNEL *ch = ossl_quic_conn_get_channel(h->c_conn);
1917
1918 ossl_quic_channel_set_inhibit_tick(ch, op->arg1);
1919 }
1920 break;
1921
1922 case OPK_C_SET_WRITE_BUF_SIZE:
1923 if (!TEST_ptr(c_tgt))
1924 goto out;
1925
1926 if (!TEST_true(ossl_quic_set_write_buffer_size(c_tgt, op->arg1)))
1927 goto out;
1928
1929 break;
1930
614c08c2 1931 case OPK_S_NEW_TICKET:
5881dd2c 1932 if (!TEST_true(ossl_quic_tserver_new_ticket(ACQUIRE_S())))
614c08c2
MC
1933 goto out;
1934 break;
1935
ed835673
HL
1936 default:
1937 TEST_error("unknown op");
1938 goto out;
1939 }
1940 }
1941
1942out:
5881dd2c 1943 s_unlock(h, hl); /* idempotent */
fca44cfc
HL
1944 if (!testresult) {
1945 size_t i;
1946
0786483a
HL
1947 TEST_error("failed in script \"%s\" at op %zu, thread %d\n",
1948 script_name, op_idx + 1, thread_idx);
ed835673 1949
fca44cfc
HL
1950 for (i = 0; i < repeat_stack_len; ++i)
1951 TEST_info("while repeating, iteration %zu of %zu, starting at script op %zu",
1952 repeat_stack_done[i],
1953 repeat_stack_limit[i],
1954 repeat_stack_idx[i]);
1955 }
1956
ed835673 1957 OPENSSL_free(tmp_buf);
5881dd2c 1958 helper_local_cleanup(hl);
a350db73
HL
1959 return testresult;
1960}
1961
0786483a
HL
1962static int run_script(const struct script_op *script,
1963 const char *script_name,
5881dd2c
HL
1964 int free_order,
1965 int blocking)
a350db73
HL
1966{
1967 int testresult = 0;
1968 struct helper h;
1969
5881dd2c 1970 if (!TEST_true(helper_init(&h, free_order, blocking, 1)))
a350db73
HL
1971 goto out;
1972
0786483a 1973 if (!TEST_true(run_script_worker(&h, script, script_name, -1)))
a350db73
HL
1974 goto out;
1975
629b408c 1976#if defined(OPENSSL_THREADS)
a350db73
HL
1977 if (!TEST_true(join_threads(h.threads, h.num_threads)))
1978 goto out;
629b408c 1979#endif
a350db73
HL
1980
1981 testresult = 1;
1982out:
ed835673
HL
1983 helper_cleanup(&h);
1984 return testresult;
1985}
1986
a350db73
HL
1987#if defined(OPENSSL_THREADS)
1988static CRYPTO_THREAD_RETVAL run_script_child_thread(void *arg)
1989{
1990 int testresult;
1991 struct child_thread_args *args = arg;
1992
1993 testresult = run_script_worker(args->h, args->script,
0786483a 1994 args->script_name,
a350db73
HL
1995 args->thread_idx);
1996
1997 ossl_crypto_mutex_lock(args->m);
1998 args->testresult = testresult;
1999 args->done = 1;
2000 ossl_crypto_mutex_unlock(args->m);
2001 return 1;
2002}
2003#endif
2004
ed835673
HL
2005/* 1. Simple single-stream test */
2006static const struct script_op script_1[] = {
2007 OP_C_SET_ALPN ("ossltest")
2008 OP_C_CONNECT_WAIT ()
2009 OP_C_WRITE (DEFAULT, "apple", 5)
2010 OP_C_CONCLUDE (DEFAULT)
2011 OP_S_BIND_STREAM_ID (a, C_BIDI_ID(0))
2012 OP_S_READ_EXPECT (a, "apple", 5)
2013 OP_S_EXPECT_FIN (a)
2014 OP_S_WRITE (a, "orange", 6)
2015 OP_S_CONCLUDE (a)
2016 OP_C_READ_EXPECT (DEFAULT, "orange", 6)
2017 OP_C_EXPECT_FIN (DEFAULT)
2018 OP_END
2019};
2020
2021/* 2. Multi-stream test */
2022static const struct script_op script_2[] = {
2023 OP_C_SET_ALPN ("ossltest")
2024 OP_C_CONNECT_WAIT ()
83df44ae 2025 OP_C_SET_INCOMING_STREAM_POLICY(SSL_INCOMING_STREAM_POLICY_ACCEPT)
ed835673
HL
2026 OP_C_WRITE (DEFAULT, "apple", 5)
2027 OP_S_BIND_STREAM_ID (a, C_BIDI_ID(0))
2028 OP_S_READ_EXPECT (a, "apple", 5)
2029 OP_S_WRITE (a, "orange", 6)
2030 OP_C_READ_EXPECT (DEFAULT, "orange", 6)
2031
2032 OP_C_NEW_STREAM_BIDI (b, C_BIDI_ID(1))
2033 OP_C_WRITE (b, "flamingo", 8)
2034 OP_C_CONCLUDE (b)
2035 OP_S_BIND_STREAM_ID (b, C_BIDI_ID(1))
2036 OP_S_READ_EXPECT (b, "flamingo", 8)
2037 OP_S_EXPECT_FIN (b)
2038 OP_S_WRITE (b, "gargoyle", 8)
2039 OP_S_CONCLUDE (b)
2040 OP_C_READ_EXPECT (b, "gargoyle", 8)
2041 OP_C_EXPECT_FIN (b)
2042
2043 OP_C_NEW_STREAM_UNI (c, C_UNI_ID(0))
2044 OP_C_WRITE (c, "elephant", 8)
2045 OP_C_CONCLUDE (c)
2046 OP_S_BIND_STREAM_ID (c, C_UNI_ID(0))
2047 OP_S_READ_EXPECT (c, "elephant", 8)
2048 OP_S_EXPECT_FIN (c)
2049 OP_S_WRITE_FAIL (c)
2050
2051 OP_C_ACCEPT_STREAM_NONE ()
2052
2053 OP_S_NEW_STREAM_BIDI (d, S_BIDI_ID(0))
2054 OP_S_WRITE (d, "frog", 4)
2055 OP_S_CONCLUDE (d)
2056
a350db73 2057 OP_C_ACCEPT_STREAM_WAIT (d)
ed835673
HL
2058 OP_C_ACCEPT_STREAM_NONE ()
2059 OP_C_READ_EXPECT (d, "frog", 4)
2060 OP_C_EXPECT_FIN (d)
2061
2062 OP_S_NEW_STREAM_BIDI (e, S_BIDI_ID(1))
2063 OP_S_WRITE (e, "mixture", 7)
2064 OP_S_CONCLUDE (e)
2065
a350db73 2066 OP_C_ACCEPT_STREAM_WAIT (e)
ed835673
HL
2067 OP_C_READ_EXPECT (e, "mixture", 7)
2068 OP_C_EXPECT_FIN (e)
2069 OP_C_WRITE (e, "ramble", 6)
2070 OP_S_READ_EXPECT (e, "ramble", 6)
2071 OP_C_CONCLUDE (e)
2072 OP_S_EXPECT_FIN (e)
2073
2074 OP_S_NEW_STREAM_UNI (f, S_UNI_ID(0))
2075 OP_S_WRITE (f, "yonder", 6)
2076 OP_S_CONCLUDE (f)
2077
a350db73 2078 OP_C_ACCEPT_STREAM_WAIT (f)
ed835673
HL
2079 OP_C_ACCEPT_STREAM_NONE ()
2080 OP_C_READ_EXPECT (f, "yonder", 6)
2081 OP_C_EXPECT_FIN (f)
2082 OP_C_WRITE_FAIL (f)
2083
83df44ae 2084 OP_C_SET_INCOMING_STREAM_POLICY(SSL_INCOMING_STREAM_POLICY_REJECT)
ed835673
HL
2085 OP_S_NEW_STREAM_BIDI (g, S_BIDI_ID(2))
2086 OP_S_WRITE (g, "unseen", 6)
2087 OP_S_CONCLUDE (g)
2088
2089 OP_C_ACCEPT_STREAM_NONE ()
2090
83df44ae 2091 OP_C_SET_INCOMING_STREAM_POLICY(SSL_INCOMING_STREAM_POLICY_AUTO)
ed835673
HL
2092 OP_S_NEW_STREAM_BIDI (h, S_BIDI_ID(3))
2093 OP_S_WRITE (h, "UNSEEN", 6)
2094 OP_S_CONCLUDE (h)
2095
2096 OP_C_ACCEPT_STREAM_NONE ()
2097
2098 /*
2099 * Streams g, h should have been rejected, so server should have got
2100 * STOP_SENDING/RESET_STREAM.
2101 */
2102 OP_CHECK (check_rejected, S_BIDI_ID(2))
2103 OP_CHECK (check_rejected, S_BIDI_ID(3))
2104
2105 OP_END
2106};
2107
2108/* 3. Default stream detach/reattach test */
2109static const struct script_op script_3[] = {
2110 OP_C_SET_ALPN ("ossltest")
2111 OP_C_CONNECT_WAIT ()
2112
2113 OP_C_WRITE (DEFAULT, "apple", 5)
2114 OP_C_DETACH (a) /* DEFAULT becomes stream 'a' */
2115 OP_C_WRITE_FAIL (DEFAULT)
2116
2117 OP_C_WRITE (a, "by", 2)
2118
2119 OP_S_BIND_STREAM_ID (a, C_BIDI_ID(0))
2120 OP_S_READ_EXPECT (a, "appleby", 7)
2121
2122 OP_S_WRITE (a, "hello", 5)
2123 OP_C_READ_EXPECT (a, "hello", 5)
2124
2125 OP_C_WRITE_FAIL (DEFAULT)
2126 OP_C_ATTACH (a)
2127 OP_C_WRITE (DEFAULT, "is here", 7)
2128 OP_S_READ_EXPECT (a, "is here", 7)
2129
2130 OP_C_DETACH (a)
2131 OP_C_CONCLUDE (a)
2132 OP_S_EXPECT_FIN (a)
2133
2134 OP_END
2135};
2136
2137/* 4. Default stream mode test */
2138static const struct script_op script_4[] = {
2139 OP_C_SET_ALPN ("ossltest")
2140 OP_C_CONNECT_WAIT ()
2141
2142 OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_NONE)
2143 OP_C_WRITE_FAIL (DEFAULT)
2144
2145 OP_S_NEW_STREAM_BIDI (a, S_BIDI_ID(0))
2146 OP_S_WRITE (a, "apple", 5)
2147
2148 OP_C_READ_FAIL (DEFAULT)
2149
a350db73 2150 OP_C_ACCEPT_STREAM_WAIT (a)
ed835673
HL
2151 OP_C_READ_EXPECT (a, "apple", 5)
2152
2153 OP_C_ATTACH (a)
2154 OP_C_WRITE (DEFAULT, "orange", 6)
2155 OP_S_READ_EXPECT (a, "orange", 6)
2156
2157 OP_END
2158};
2159
2160/* 5. Test stream reset functionality */
2161static const struct script_op script_5[] = {
2162 OP_C_SET_ALPN ("ossltest")
2163 OP_C_CONNECT_WAIT ()
2164
2165 OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_NONE)
2166 OP_C_NEW_STREAM_BIDI (a, C_BIDI_ID(0))
9ff3a99e 2167 OP_C_NEW_STREAM_BIDI (b, C_BIDI_ID(1))
ed835673
HL
2168
2169 OP_C_WRITE (a, "apple", 5)
2170 OP_C_STREAM_RESET (a, 42)
2171
9ff3a99e
HL
2172 OP_C_WRITE (b, "strawberry", 10)
2173
ed835673 2174 OP_S_BIND_STREAM_ID (a, C_BIDI_ID(0))
9ff3a99e
HL
2175 OP_S_BIND_STREAM_ID (b, C_BIDI_ID(1))
2176 OP_S_READ_EXPECT (b, "strawberry", 10)
2f018d14
HL
2177 /* Reset disrupts read of already sent data */
2178 OP_S_READ_FAIL (a)
ed835673
HL
2179 OP_CHECK (check_stream_reset, C_BIDI_ID(0))
2180
2181 OP_END
2182};
2183
2184/* 6. Test STOP_SENDING functionality */
2185static const struct script_op script_6[] = {
2186 OP_C_SET_ALPN ("ossltest")
2187 OP_C_CONNECT_WAIT ()
2188
2189 OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_NONE)
2190 OP_S_NEW_STREAM_BIDI (a, S_BIDI_ID(0))
2191 OP_S_WRITE (a, "apple", 5)
2192
a350db73 2193 OP_C_ACCEPT_STREAM_WAIT (a)
ed835673
HL
2194 OP_C_FREE_STREAM (a)
2195 OP_C_ACCEPT_STREAM_NONE ()
2196
2197 OP_CHECK (check_stream_stopped, S_BIDI_ID(0))
2198
2199 OP_END
2200};
2201
2202/* 7. Unidirectional default stream mode test (client sends first) */
2203static const struct script_op script_7[] = {
2204 OP_C_SET_ALPN ("ossltest")
2205 OP_C_CONNECT_WAIT ()
2206
2207 OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_AUTO_UNI)
2208 OP_C_WRITE (DEFAULT, "apple", 5)
2209
2210 OP_S_BIND_STREAM_ID (a, C_UNI_ID(0))
2211 OP_S_READ_EXPECT (a, "apple", 5)
2212 OP_S_WRITE_FAIL (a)
2213
2214 OP_END
2215};
2216
2217/* 8. Unidirectional default stream mode test (server sends first) */
2218static const struct script_op script_8[] = {
2219 OP_C_SET_ALPN ("ossltest")
2220 OP_C_CONNECT_WAIT ()
2221
2222 OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_AUTO_UNI)
2223 OP_S_NEW_STREAM_UNI (a, S_UNI_ID(0))
2224 OP_S_WRITE (a, "apple", 5)
2225 OP_C_READ_EXPECT (DEFAULT, "apple", 5)
2226 OP_C_WRITE_FAIL (DEFAULT)
2227
2228 OP_END
2229};
2230
2231/* 9. Unidirectional default stream mode test (server sends first on bidi) */
2232static const struct script_op script_9[] = {
2233 OP_C_SET_ALPN ("ossltest")
2234 OP_C_CONNECT_WAIT ()
2235
2236 OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_AUTO_UNI)
2237 OP_S_NEW_STREAM_BIDI (a, S_BIDI_ID(0))
2238 OP_S_WRITE (a, "apple", 5)
2239 OP_C_READ_EXPECT (DEFAULT, "apple", 5)
2240 OP_C_WRITE (DEFAULT, "orange", 6)
2241 OP_S_READ_EXPECT (a, "orange", 6)
2242
2243 OP_END
2244};
2245
2246/* 10. Shutdown */
2247static const struct script_op script_10[] = {
2248 OP_C_SET_ALPN ("ossltest")
2249 OP_C_CONNECT_WAIT ()
2250
2251 OP_C_WRITE (DEFAULT, "apple", 5)
2252 OP_S_BIND_STREAM_ID (a, C_BIDI_ID(0))
2253 OP_S_READ_EXPECT (a, "apple", 5)
2254
3bc38ba0 2255 OP_C_SHUTDOWN_WAIT (NULL, 0)
ed835673
HL
2256 OP_C_EXPECT_CONN_CLOSE_INFO(0, 1, 0)
2257 OP_S_EXPECT_CONN_CLOSE_INFO(0, 1, 1)
2258
2259 OP_END
2260};
2261
fca44cfc 2262/* 11. Many threads accepted on the same client connection */
274bb489
HL
2263static const struct script_op script_11_child[] = {
2264 OP_C_ACCEPT_STREAM_WAIT (a)
2265 OP_C_READ_EXPECT (a, "foo", 3)
2266 OP_C_EXPECT_FIN (a)
2267
2268 OP_END
2269};
2270
2271static const struct script_op script_11[] = {
2272 OP_C_SET_ALPN ("ossltest")
2273 OP_C_CONNECT_WAIT ()
2274 OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_NONE)
2275
fca44cfc
HL
2276 OP_NEW_THREAD (5, script_11_child)
2277
2278 OP_S_NEW_STREAM_BIDI (a, ANY_ID)
274bb489
HL
2279 OP_S_WRITE (a, "foo", 3)
2280 OP_S_CONCLUDE (a)
2281
fca44cfc 2282 OP_S_NEW_STREAM_BIDI (b, ANY_ID)
274bb489
HL
2283 OP_S_WRITE (b, "foo", 3)
2284 OP_S_CONCLUDE (b)
2285
fca44cfc 2286 OP_S_NEW_STREAM_BIDI (c, ANY_ID)
274bb489
HL
2287 OP_S_WRITE (c, "foo", 3)
2288 OP_S_CONCLUDE (c)
2289
fca44cfc 2290 OP_S_NEW_STREAM_BIDI (d, ANY_ID)
274bb489
HL
2291 OP_S_WRITE (d, "foo", 3)
2292 OP_S_CONCLUDE (d)
2293
fca44cfc 2294 OP_S_NEW_STREAM_BIDI (e, ANY_ID)
274bb489
HL
2295 OP_S_WRITE (e, "foo", 3)
2296 OP_S_CONCLUDE (e)
2297
274bb489
HL
2298 OP_END
2299};
2300
fca44cfc 2301/* 12. Many threads initiated on the same client connection */
274bb489
HL
2302static const struct script_op script_12_child[] = {
2303 OP_C_NEW_STREAM_BIDI (a, ANY_ID)
2304 OP_C_WRITE (a, "foo", 3)
2305 OP_C_CONCLUDE (a)
2306 OP_C_FREE_STREAM (a)
2307
2308 OP_END
2309};
2310
2311static const struct script_op script_12[] = {
2312 OP_C_SET_ALPN ("ossltest")
2313 OP_C_CONNECT_WAIT ()
2314 OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_NONE)
2315
2316 OP_NEW_THREAD (5, script_12_child)
2317
2318 OP_S_BIND_STREAM_ID (a, C_BIDI_ID(0))
2319 OP_S_READ_EXPECT (a, "foo", 3)
2320 OP_S_EXPECT_FIN (a)
2321 OP_S_BIND_STREAM_ID (b, C_BIDI_ID(1))
2322 OP_S_READ_EXPECT (b, "foo", 3)
2323 OP_S_EXPECT_FIN (b)
2324 OP_S_BIND_STREAM_ID (c, C_BIDI_ID(2))
2325 OP_S_READ_EXPECT (c, "foo", 3)
2326 OP_S_EXPECT_FIN (c)
2327 OP_S_BIND_STREAM_ID (d, C_BIDI_ID(3))
2328 OP_S_READ_EXPECT (d, "foo", 3)
2329 OP_S_EXPECT_FIN (d)
2330 OP_S_BIND_STREAM_ID (e, C_BIDI_ID(4))
2331 OP_S_READ_EXPECT (e, "foo", 3)
2332 OP_S_EXPECT_FIN (e)
2333
2334 OP_END
2335};
2336
fca44cfc
HL
2337/* 13. Many threads accepted on the same client connection (stress test) */
2338static const struct script_op script_13_child[] = {
2339 OP_BEGIN_REPEAT (10)
2340
2341 OP_C_ACCEPT_STREAM_WAIT (a)
2342 OP_C_READ_EXPECT (a, "foo", 3)
2343 OP_C_EXPECT_FIN (a)
2344 OP_C_FREE_STREAM (a)
2345
2346 OP_END_REPEAT ()
2347
2348 OP_END
2349};
2350
2351static const struct script_op script_13[] = {
2352 OP_C_SET_ALPN ("ossltest")
2353 OP_C_CONNECT_WAIT ()
2354 OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_NONE)
2355
2356 OP_NEW_THREAD (5, script_13_child)
2357
2358 OP_BEGIN_REPEAT (50)
2359
2360 OP_S_NEW_STREAM_BIDI (a, ANY_ID)
2361 OP_S_WRITE (a, "foo", 3)
2362 OP_S_CONCLUDE (a)
2363 OP_S_UNBIND_STREAM_ID (a)
2364
2365 OP_END_REPEAT ()
2366
2367 OP_END
2368};
2369
2370/* 14. Many threads initiating on the same client connection (stress test) */
2371static const struct script_op script_14_child[] = {
2372 OP_BEGIN_REPEAT (10)
2373
2374 OP_C_NEW_STREAM_BIDI (a, ANY_ID)
2375 OP_C_WRITE (a, "foo", 3)
2376 OP_C_CONCLUDE (a)
2377 OP_C_FREE_STREAM (a)
2378
2379 OP_END_REPEAT ()
2380
2381 OP_END
2382};
2383
2384static const struct script_op script_14[] = {
2385 OP_C_SET_ALPN ("ossltest")
2386 OP_C_CONNECT_WAIT ()
2387 OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_NONE)
2388
2389 OP_NEW_THREAD (5, script_14_child)
2390
2391 OP_BEGIN_REPEAT (50)
2392
2393 OP_S_ACCEPT_STREAM_WAIT (a)
2394 OP_S_READ_EXPECT (a, "foo", 3)
2395 OP_S_EXPECT_FIN (a)
2396 OP_S_UNBIND_STREAM_ID (a)
2397
2398 OP_END_REPEAT ()
2399
2400 OP_END
2401};
2402
0554f723
HL
2403/* 15. Client sending large number of streams, MAX_STREAMS test */
2404static const struct script_op script_15[] = {
2405 OP_C_SET_ALPN ("ossltest")
2406 OP_C_CONNECT_WAIT ()
2407 OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_NONE)
2408
2409 /*
2410 * This will cause a protocol violation to be raised by the server if we are
2411 * not handling the stream limit correctly on the TX side.
2412 */
2413 OP_BEGIN_REPEAT (200)
2414
14551f1e 2415 OP_C_NEW_STREAM_BIDI_EX (a, ANY_ID, SSL_STREAM_FLAG_ADVANCE)
0554f723
HL
2416 OP_C_WRITE (a, "foo", 3)
2417 OP_C_CONCLUDE (a)
2418 OP_C_FREE_STREAM (a)
2419
2420 OP_END_REPEAT ()
2421
2422 /* Prove the connection is still good. */
2423 OP_S_NEW_STREAM_BIDI (a, S_BIDI_ID(0))
2424 OP_S_WRITE (a, "bar", 3)
2425 OP_S_CONCLUDE (a)
2426
2427 OP_C_ACCEPT_STREAM_WAIT (a)
2428 OP_C_READ_EXPECT (a, "bar", 3)
2429 OP_C_EXPECT_FIN (a)
2430
2431 /*
2432 * Drain the queue of incoming streams. We should be able to get all 200
2433 * even though only 100 can be initiated at a time.
2434 */
2435 OP_BEGIN_REPEAT (200)
2436
2437 OP_S_ACCEPT_STREAM_WAIT (b)
2438 OP_S_READ_EXPECT (b, "foo", 3)
2439 OP_S_EXPECT_FIN (b)
2440 OP_S_UNBIND_STREAM_ID (b)
2441
2442 OP_END_REPEAT ()
2443
2444 OP_END
2445};
2446
2447/* 16. Server sending large number of streams, MAX_STREAMS test */
2448static const struct script_op script_16[] = {
2449 OP_C_SET_ALPN ("ossltest")
2450 OP_C_CONNECT_WAIT ()
2451 OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_NONE)
2452
2453 /*
2454 * This will cause a protocol violation to be raised by the client if we are
2455 * not handling the stream limit correctly on the TX side.
2456 */
2457 OP_BEGIN_REPEAT (200)
2458
2459 OP_S_NEW_STREAM_BIDI (a, ANY_ID)
2460 OP_S_WRITE (a, "foo", 3)
2461 OP_S_CONCLUDE (a)
2462 OP_S_UNBIND_STREAM_ID (a)
2463
2464 OP_END_REPEAT ()
2465
2466 /* Prove that the connection is still good. */
2467 OP_C_NEW_STREAM_BIDI (a, ANY_ID)
2468 OP_C_WRITE (a, "bar", 3)
2469 OP_C_CONCLUDE (a)
2470
2471 OP_S_ACCEPT_STREAM_WAIT (b)
2472 OP_S_READ_EXPECT (b, "bar", 3)
2473 OP_S_EXPECT_FIN (b)
2474
2475 /* Drain the queue of incoming streams. */
2476 OP_BEGIN_REPEAT (200)
2477
2478 OP_C_ACCEPT_STREAM_WAIT (b)
2479 OP_C_READ_EXPECT (b, "foo", 3)
2480 OP_C_EXPECT_FIN (b)
2481 OP_C_FREE_STREAM (b)
2482
2483 OP_END_REPEAT ()
2484
2485 OP_END
2486};
2487
693b23e3
HL
2488/* 17. Key update test - unlimited */
2489static const struct script_op script_17[] = {
2490 OP_C_SET_ALPN ("ossltest")
2491 OP_C_CONNECT_WAIT ()
2492
2493 OP_C_WRITE (DEFAULT, "apple", 5)
2494
2495 OP_S_BIND_STREAM_ID (a, C_BIDI_ID(0))
2496 OP_S_READ_EXPECT (a, "apple", 5)
2497
2498 OP_CHECK (override_key_update, 1)
2499
2500 OP_BEGIN_REPEAT (200)
2501
2502 OP_C_WRITE (DEFAULT, "apple", 5)
2503 OP_S_READ_EXPECT (a, "apple", 5)
2504
2505 /*
2506 * TXKU frequency is bounded by RTT because a previous TXKU needs to be
2507 * acknowledged by the peer first before another one can be begin. By
2508 * waiting this long, we eliminate any such concern and ensure as many key
2509 * updates as possible can occur for the purposes of this test.
2510 */
2511 OP_CHECK (skip_time_ms, 100)
2512
2513 OP_END_REPEAT ()
2514
2515 /* At least 5 RXKUs detected */
2516 OP_CHECK (check_key_update_ge, 5)
2517
2518 /*
2519 * Prove the connection is still healthy by sending something in both
2520 * directions.
2521 */
2522 OP_C_WRITE (DEFAULT, "xyzzy", 5)
2523 OP_S_READ_EXPECT (a, "xyzzy", 5)
2524
2525 OP_S_WRITE (a, "plugh", 5)
2526 OP_C_READ_EXPECT (DEFAULT, "plugh", 5)
2527
2528 OP_END
2529};
2530
2531/* 18. Key update test - RTT-bounded */
2532static const struct script_op script_18[] = {
2533 OP_C_SET_ALPN ("ossltest")
2534 OP_C_CONNECT_WAIT ()
2535
2536 OP_C_WRITE (DEFAULT, "apple", 5)
2537
2538 OP_S_BIND_STREAM_ID (a, C_BIDI_ID(0))
2539 OP_S_READ_EXPECT (a, "apple", 5)
2540
2541 OP_CHECK (override_key_update, 1)
2542
2543 OP_BEGIN_REPEAT (200)
2544
2545 OP_C_WRITE (DEFAULT, "apple", 5)
2546 OP_S_READ_EXPECT (a, "apple", 5)
de9564bd 2547 OP_CHECK (skip_time_ms, 8)
693b23e3
HL
2548
2549 OP_END_REPEAT ()
2550
2551 /*
2552 * This time we simulate far less time passing between writes, so there are
2553 * fewer opportunities to initiate TXKUs. Note that we ask for a TXKU every
2554 * 1 packet above, which is absurd; thus this ensures we only actually
2555 * generate TXKUs when we are allowed to.
2556 */
0e1da9d7 2557 OP_CHECK (check_key_update_lt, 240)
693b23e3
HL
2558
2559 /*
2560 * Prove the connection is still healthy by sending something in both
2561 * directions.
2562 */
2563 OP_C_WRITE (DEFAULT, "xyzzy", 5)
2564 OP_S_READ_EXPECT (a, "xyzzy", 5)
2565
2566 OP_S_WRITE (a, "plugh", 5)
2567 OP_C_READ_EXPECT (DEFAULT, "plugh", 5)
2568
2569 OP_END
2570};
2571
2525109f
HL
2572/* 19. Key update test - artificially triggered */
2573static const struct script_op script_19[] = {
2574 OP_C_SET_ALPN ("ossltest")
2575 OP_C_CONNECT_WAIT ()
2576
2577 OP_C_WRITE (DEFAULT, "apple", 5)
2578
2579 OP_S_BIND_STREAM_ID (a, C_BIDI_ID(0))
2580 OP_S_READ_EXPECT (a, "apple", 5)
2581
76696a54
HL
2582 OP_C_WRITE (DEFAULT, "orange", 6)
2583 OP_S_READ_EXPECT (a, "orange", 6)
2584
2585 OP_S_WRITE (a, "strawberry", 10)
2586 OP_C_READ_EXPECT (DEFAULT, "strawberry", 10)
2587
2525109f
HL
2588 OP_CHECK (check_key_update_lt, 1)
2589 OP_CHECK (trigger_key_update, 0)
2590
2591 OP_C_WRITE (DEFAULT, "orange", 6)
2592 OP_S_READ_EXPECT (a, "orange", 6)
9289e59c 2593 OP_S_WRITE (a, "ok", 2)
2525109f 2594
9289e59c 2595 OP_C_READ_EXPECT (DEFAULT, "ok", 2)
2525109f
HL
2596 OP_CHECK (check_key_update_ge, 1)
2597
2598 OP_END
2599};
2600
0345cac6 2601/* 20. Multiple threads accept stream with socket forcibly closed (error test) */
99d6b9f9
HL
2602static int script_20_trigger(struct helper *h, volatile uint64_t *counter)
2603{
2604#if defined(OPENSSL_THREADS)
2605 ossl_crypto_mutex_lock(h->misc_m);
2606 ++*counter;
2607 ossl_crypto_mutex_unlock(h->misc_m);
2608 ossl_crypto_condvar_broadcast(h->misc_cv);
2609#endif
2610 return 1;
2611}
2612
2613static int script_20_wait(struct helper *h, volatile uint64_t *counter, uint64_t threshold)
2614{
2615#if defined(OPENSSL_THREADS)
2616 int stop = 0;
2617
2618 ossl_crypto_mutex_lock(h->misc_m);
2619 while (!stop) {
2620 stop = (*counter >= threshold);
2621 if (stop)
2622 break;
2623
2624 ossl_crypto_condvar_wait(h->misc_cv, h->misc_m);
2625 }
2626
2627 ossl_crypto_mutex_unlock(h->misc_m);
2628#endif
2629 return 1;
2630}
2631
2632static int script_20_trigger1(struct helper *h, struct helper_local *hl)
2633{
2634 return script_20_trigger(h, &h->scratch0);
2635}
2636
2637static int script_20_wait1(struct helper *h, struct helper_local *hl)
2638{
2639 return script_20_wait(h, &h->scratch0, hl->check_op->arg2);
2640}
2641
2642static int script_20_trigger2(struct helper *h, struct helper_local *hl)
2643{
2644 return script_20_trigger(h, &h->scratch1);
2645}
2646
2647static int script_20_wait2(struct helper *h, struct helper_local *hl)
2648{
2649 return script_20_wait(h, &h->scratch1, hl->check_op->arg2);
2650}
2651
0345cac6
TM
2652static const struct script_op script_20_child[] = {
2653 OP_C_ACCEPT_STREAM_WAIT (a)
2654 OP_C_READ_EXPECT (a, "foo", 3)
2655
99d6b9f9
HL
2656 OP_CHECK (script_20_trigger1, 0)
2657 OP_CHECK (script_20_wait2, 1)
0345cac6
TM
2658
2659 OP_C_READ_FAIL_WAIT (a)
2660 OP_C_EXPECT_SSL_ERR (a, SSL_ERROR_SYSCALL)
2661 OP_EXPECT_ERR_LIB (ERR_LIB_SYS)
741170be 2662 OP_EXPECT_ERR_REASON (SSL_R_QUIC_NETWORK_ERROR)
0345cac6
TM
2663 OP_C_FREE_STREAM (a)
2664
2665 OP_END
2666};
2667
2668static const struct script_op script_20[] = {
2669 OP_C_SET_ALPN ("ossltest")
2670 OP_C_CONNECT_WAIT ()
2671 OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_NONE)
2672
2673 OP_NEW_THREAD (5, script_20_child)
2674
2675 OP_BEGIN_REPEAT (5)
2676
2677 OP_S_NEW_STREAM_BIDI (a, ANY_ID)
2678 OP_S_WRITE (a, "foo", 3)
2679 OP_S_UNBIND_STREAM_ID (a)
2680
2681 OP_END_REPEAT ()
2682
99d6b9f9 2683 OP_CHECK (script_20_wait1, 5)
0345cac6
TM
2684
2685 OP_C_CLOSE_SOCKET ()
99d6b9f9 2686 OP_CHECK (script_20_trigger2, 0)
0345cac6
TM
2687
2688 OP_END
2689};
2690
e26dc8e3
HL
2691/* 21. Fault injection - unknown frame in 1-RTT packet */
2692static int script_21_inject_plain(struct helper *h, QUIC_PKT_HDR *hdr,
2693 unsigned char *buf, size_t len)
2694{
7eebc354 2695 int ok = 0;
e26dc8e3
HL
2696 WPACKET wpkt;
2697 unsigned char frame_buf[8];
2698 size_t written;
2699
2700 if (h->inject_word0 == 0 || hdr->type != h->inject_word0)
2701 return 1;
2702
2703 if (!TEST_true(WPACKET_init_static_len(&wpkt, frame_buf,
2704 sizeof(frame_buf), 0)))
2705 return 0;
2706
2707 if (!TEST_true(WPACKET_quic_write_vlint(&wpkt, h->inject_word1)))
7eebc354 2708 goto err;
e26dc8e3
HL
2709
2710 if (!TEST_true(WPACKET_get_total_written(&wpkt, &written)))
7eebc354 2711 goto err;
e26dc8e3
HL
2712
2713 if (!qtest_fault_prepend_frame(h->qtf, frame_buf, written))
7eebc354 2714 goto err;
e26dc8e3 2715
7eebc354
HL
2716 ok = 1;
2717err:
96b7df60
HL
2718 if (ok)
2719 WPACKET_finish(&wpkt);
2720 else
2721 WPACKET_cleanup(&wpkt);
7eebc354 2722 return ok;
e26dc8e3
HL
2723}
2724
2725static const struct script_op script_21[] = {
2726 OP_S_SET_INJECT_PLAIN (script_21_inject_plain)
2727 OP_C_SET_ALPN ("ossltest")
2728 OP_C_CONNECT_WAIT ()
2729
2730 OP_C_WRITE (DEFAULT, "apple", 5)
2731 OP_S_BIND_STREAM_ID (a, C_BIDI_ID(0))
2732 OP_S_READ_EXPECT (a, "apple", 5)
2733
2734 OP_SET_INJECT_WORD (QUIC_PKT_TYPE_1RTT, OSSL_QUIC_VLINT_MAX)
2735
2736 OP_S_WRITE (a, "orange", 6)
2737
2738 OP_C_EXPECT_CONN_CLOSE_INFO(QUIC_ERR_FRAME_ENCODING_ERROR,0,0)
2739
2740 OP_END
2741};
2742
2743/* 22. Fault injection - non-zero packet header reserved bits */
2744static int script_22_inject_plain(struct helper *h, QUIC_PKT_HDR *hdr,
2745 unsigned char *buf, size_t len)
2746{
2747 if (h->inject_word0 == 0)
2748 return 1;
2749
2750 hdr->reserved = 1;
2751 return 1;
2752}
2753
2754static const struct script_op script_22[] = {
2755 OP_S_SET_INJECT_PLAIN (script_22_inject_plain)
2756 OP_C_SET_ALPN ("ossltest")
2757 OP_C_CONNECT_WAIT ()
2758
2759 OP_C_WRITE (DEFAULT, "apple", 5)
2760 OP_S_BIND_STREAM_ID (a, C_BIDI_ID(0))
2761 OP_S_READ_EXPECT (a, "apple", 5)
2762
2763 OP_SET_INJECT_WORD (1, 0)
2764
2765 OP_S_WRITE (a, "orange", 6)
2766
2767 OP_C_EXPECT_CONN_CLOSE_INFO(QUIC_ERR_PROTOCOL_VIOLATION,0,0)
2768
2769 OP_END
2770};
2771
2772/* 23. Fault injection - empty NEW_TOKEN */
2773static int script_23_inject_plain(struct helper *h, QUIC_PKT_HDR *hdr,
2774 unsigned char *buf, size_t len)
2775{
7eebc354 2776 int ok = 0;
e26dc8e3
HL
2777 WPACKET wpkt;
2778 unsigned char frame_buf[16];
2779 size_t written;
2780
2781 if (h->inject_word0 == 0)
2782 return 1;
2783
2784 if (!TEST_true(WPACKET_init_static_len(&wpkt, frame_buf,
2785 sizeof(frame_buf), 0)))
2786 return 0;
2787
2788 if (!TEST_true(WPACKET_quic_write_vlint(&wpkt, OSSL_QUIC_FRAME_TYPE_NEW_TOKEN))
2789 || !TEST_true(WPACKET_quic_write_vlint(&wpkt, 0)))
7eebc354 2790 goto err;
e26dc8e3
HL
2791
2792 if (!TEST_true(WPACKET_get_total_written(&wpkt, &written)))
7eebc354 2793 goto err;
e26dc8e3
HL
2794
2795 if (!qtest_fault_prepend_frame(h->qtf, frame_buf, written))
7eebc354 2796 goto err;
e26dc8e3 2797
7eebc354
HL
2798 ok = 1;
2799err:
96b7df60
HL
2800 if (ok)
2801 WPACKET_finish(&wpkt);
2802 else
2803 WPACKET_cleanup(&wpkt);
7eebc354 2804 return ok;
e26dc8e3
HL
2805}
2806
2807static const struct script_op script_23[] = {
2808 OP_S_SET_INJECT_PLAIN (script_23_inject_plain)
2809 OP_C_SET_ALPN ("ossltest")
2810 OP_C_CONNECT_WAIT ()
2811
2812 OP_C_WRITE (DEFAULT, "apple", 5)
2813 OP_S_BIND_STREAM_ID (a, C_BIDI_ID(0))
2814 OP_S_READ_EXPECT (a, "apple", 5)
2815
2816 OP_SET_INJECT_WORD (1, 0)
2817
2818 OP_S_WRITE (a, "orange", 6)
2819
2820 OP_C_EXPECT_CONN_CLOSE_INFO(QUIC_ERR_FRAME_ENCODING_ERROR,0,0)
2821
2822 OP_END
2823};
2824
2825/* 24. Fault injection - excess value of MAX_STREAMS_BIDI */
2826static int script_24_inject_plain(struct helper *h, QUIC_PKT_HDR *hdr,
2827 unsigned char *buf, size_t len)
2828{
7eebc354 2829 int ok = 0;
e26dc8e3
HL
2830 WPACKET wpkt;
2831 unsigned char frame_buf[16];
2832 size_t written;
2833
2834 if (h->inject_word0 == 0)
2835 return 1;
2836
2837 if (!TEST_true(WPACKET_init_static_len(&wpkt, frame_buf,
2838 sizeof(frame_buf), 0)))
2839 return 0;
2840
2841 if (!TEST_true(WPACKET_quic_write_vlint(&wpkt, h->inject_word1))
2842 || !TEST_true(WPACKET_quic_write_vlint(&wpkt, (((uint64_t)1) << 60) + 1)))
7eebc354 2843 goto err;
e26dc8e3
HL
2844
2845 if (!TEST_true(WPACKET_get_total_written(&wpkt, &written)))
7eebc354 2846 goto err;
e26dc8e3
HL
2847
2848 if (!qtest_fault_prepend_frame(h->qtf, frame_buf, written))
7eebc354 2849 goto err;
e26dc8e3 2850
7eebc354
HL
2851 ok = 1;
2852err:
96b7df60
HL
2853 if (ok)
2854 WPACKET_finish(&wpkt);
2855 else
2856 WPACKET_cleanup(&wpkt);
7eebc354 2857 return ok;
e26dc8e3
HL
2858}
2859
2860static const struct script_op script_24[] = {
2861 OP_S_SET_INJECT_PLAIN (script_24_inject_plain)
2862 OP_C_SET_ALPN ("ossltest")
2863 OP_C_CONNECT_WAIT ()
2864
2865 OP_C_WRITE (DEFAULT, "apple", 5)
2866 OP_S_BIND_STREAM_ID (a, C_BIDI_ID(0))
2867 OP_S_READ_EXPECT (a, "apple", 5)
2868
2869 OP_SET_INJECT_WORD (1, OSSL_QUIC_FRAME_TYPE_MAX_STREAMS_BIDI)
2870
2871 OP_S_WRITE (a, "orange", 6)
2872
2873 OP_C_EXPECT_CONN_CLOSE_INFO(QUIC_ERR_FRAME_ENCODING_ERROR,0,0)
2874
2875 OP_END
2876};
2877
2878/* 25. Fault injection - excess value of MAX_STREAMS_UNI */
2879static const struct script_op script_25[] = {
2880 OP_S_SET_INJECT_PLAIN (script_24_inject_plain)
2881 OP_C_SET_ALPN ("ossltest")
2882 OP_C_CONNECT_WAIT ()
2883
2884 OP_C_WRITE (DEFAULT, "apple", 5)
2885 OP_S_BIND_STREAM_ID (a, C_BIDI_ID(0))
2886 OP_S_READ_EXPECT (a, "apple", 5)
2887
2888 OP_SET_INJECT_WORD (1, OSSL_QUIC_FRAME_TYPE_MAX_STREAMS_UNI)
2889
2890 OP_S_WRITE (a, "orange", 6)
2891
2892 OP_C_EXPECT_CONN_CLOSE_INFO(QUIC_ERR_FRAME_ENCODING_ERROR,0,0)
2893
2894 OP_END
2895};
2896
2897/* 26. Fault injection - excess value of STREAMS_BLOCKED_BIDI */
2898static const struct script_op script_26[] = {
2899 OP_S_SET_INJECT_PLAIN (script_24_inject_plain)
2900 OP_C_SET_ALPN ("ossltest")
2901 OP_C_CONNECT_WAIT ()
2902
2903 OP_C_WRITE (DEFAULT, "apple", 5)
2904 OP_S_BIND_STREAM_ID (a, C_BIDI_ID(0))
2905 OP_S_READ_EXPECT (a, "apple", 5)
2906
2907 OP_SET_INJECT_WORD (1, OSSL_QUIC_FRAME_TYPE_STREAMS_BLOCKED_BIDI)
2908
2909 OP_S_WRITE (a, "orange", 6)
2910
2911 OP_C_EXPECT_CONN_CLOSE_INFO(QUIC_ERR_STREAM_LIMIT_ERROR,0,0)
2912
2913 OP_END
2914};
2915
2916/* 27. Fault injection - excess value of STREAMS_BLOCKED_UNI */
2917static const struct script_op script_27[] = {
2918 OP_S_SET_INJECT_PLAIN (script_24_inject_plain)
2919 OP_C_SET_ALPN ("ossltest")
2920 OP_C_CONNECT_WAIT ()
2921
2922 OP_C_WRITE (DEFAULT, "apple", 5)
2923 OP_S_BIND_STREAM_ID (a, C_BIDI_ID(0))
2924 OP_S_READ_EXPECT (a, "apple", 5)
2925
2926 OP_SET_INJECT_WORD (1, OSSL_QUIC_FRAME_TYPE_STREAMS_BLOCKED_UNI)
2927
2928 OP_S_WRITE (a, "orange", 6)
2929
2930 OP_C_EXPECT_CONN_CLOSE_INFO(QUIC_ERR_STREAM_LIMIT_ERROR,0,0)
2931
2932 OP_END
2933};
2934
2935/* 28. Fault injection - received RESET_STREAM for send-only stream */
2936static int script_28_inject_plain(struct helper *h, QUIC_PKT_HDR *hdr,
2937 unsigned char *buf, size_t len)
2938{
7eebc354 2939 int ok = 0;
e26dc8e3
HL
2940 WPACKET wpkt;
2941 unsigned char frame_buf[32];
2942 size_t written;
2943
2944 if (h->inject_word0 == 0)
2945 return 1;
2946
2947 if (!TEST_true(WPACKET_init_static_len(&wpkt, frame_buf,
2948 sizeof(frame_buf), 0)))
2949 return 0;
2950
2951 if (!TEST_true(WPACKET_quic_write_vlint(&wpkt, h->inject_word1))
2952 || !TEST_true(WPACKET_quic_write_vlint(&wpkt, /* stream ID */
2953 h->inject_word0 - 1))
2954 || !TEST_true(WPACKET_quic_write_vlint(&wpkt, 123))
2955 || (h->inject_word1 == OSSL_QUIC_FRAME_TYPE_RESET_STREAM
2956 && !TEST_true(WPACKET_quic_write_vlint(&wpkt, 5)))) /* final size */
7eebc354 2957 goto err;
e26dc8e3
HL
2958
2959 if (!TEST_true(WPACKET_get_total_written(&wpkt, &written)))
7eebc354 2960 goto err;
e26dc8e3
HL
2961
2962 if (!qtest_fault_prepend_frame(h->qtf, frame_buf, written))
7eebc354 2963 goto err;
e26dc8e3 2964
7eebc354
HL
2965 ok = 1;
2966err:
96b7df60
HL
2967 if (ok)
2968 WPACKET_finish(&wpkt);
2969 else
2970 WPACKET_cleanup(&wpkt);
7eebc354 2971 return ok;
e26dc8e3
HL
2972}
2973
2974static const struct script_op script_28[] = {
2975 OP_S_SET_INJECT_PLAIN (script_28_inject_plain)
2976 OP_C_SET_ALPN ("ossltest")
2977 OP_C_CONNECT_WAIT ()
2978 OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_NONE)
2979
70cafc44
HL
2980 OP_C_NEW_STREAM_BIDI (a, C_BIDI_ID(0))
2981 OP_C_WRITE (a, "orange", 6)
e26dc8e3 2982
70cafc44
HL
2983 OP_S_BIND_STREAM_ID (a, C_BIDI_ID(0))
2984 OP_S_READ_EXPECT (a, "orange", 6)
2985
2986 OP_C_NEW_STREAM_UNI (b, C_UNI_ID(0))
2987 OP_C_WRITE (b, "apple", 5)
2988
2989 OP_S_BIND_STREAM_ID (b, C_UNI_ID(0))
2990 OP_S_READ_EXPECT (b, "apple", 5)
e26dc8e3
HL
2991
2992 OP_SET_INJECT_WORD (C_UNI_ID(0) + 1, OSSL_QUIC_FRAME_TYPE_RESET_STREAM)
70cafc44 2993 OP_S_WRITE (a, "fruit", 5)
e26dc8e3
HL
2994
2995 OP_C_EXPECT_CONN_CLOSE_INFO(QUIC_ERR_STREAM_STATE_ERROR,0,0)
2996
2997 OP_END
2998};
2999
3000/* 29. Fault injection - received RESET_STREAM for nonexistent send-only stream */
3001static const struct script_op script_29[] = {
3002 OP_S_SET_INJECT_PLAIN (script_28_inject_plain)
3003 OP_C_SET_ALPN ("ossltest")
3004 OP_C_CONNECT_WAIT ()
3005 OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_NONE)
3006
70cafc44
HL
3007 OP_C_NEW_STREAM_BIDI (a, C_BIDI_ID(0))
3008 OP_C_WRITE (a, "orange", 6)
e26dc8e3 3009
70cafc44
HL
3010 OP_S_BIND_STREAM_ID (a, C_BIDI_ID(0))
3011 OP_S_READ_EXPECT (a, "orange", 6)
3012
3013 OP_C_NEW_STREAM_UNI (b, C_UNI_ID(0))
3014 OP_C_WRITE (b, "apple", 5)
3015
3016 OP_S_BIND_STREAM_ID (b, C_UNI_ID(0))
3017 OP_S_READ_EXPECT (b, "apple", 5)
e26dc8e3
HL
3018
3019 OP_SET_INJECT_WORD (C_UNI_ID(1) + 1, OSSL_QUIC_FRAME_TYPE_RESET_STREAM)
70cafc44 3020 OP_S_WRITE (a, "fruit", 5)
e26dc8e3
HL
3021
3022 OP_C_EXPECT_CONN_CLOSE_INFO(QUIC_ERR_STREAM_STATE_ERROR,0,0)
3023
3024 OP_END
3025};
3026
3027/* 30. Fault injection - received STOP_SENDING for receive-only stream */
3028static const struct script_op script_30[] = {
3029 OP_S_SET_INJECT_PLAIN (script_28_inject_plain)
3030 OP_C_SET_ALPN ("ossltest")
3031 OP_C_CONNECT_WAIT ()
3032 OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_NONE)
3033
3034 OP_S_NEW_STREAM_UNI (a, S_UNI_ID(0))
3035 OP_S_WRITE (a, "apple", 5)
3036
3037 OP_C_ACCEPT_STREAM_WAIT (a)
3038 OP_C_READ_EXPECT (a, "apple", 5)
3039
3040 OP_SET_INJECT_WORD (S_UNI_ID(0) + 1, OSSL_QUIC_FRAME_TYPE_STOP_SENDING)
3041 OP_S_WRITE (a, "orange", 6)
3042
3043 OP_C_EXPECT_CONN_CLOSE_INFO(QUIC_ERR_STREAM_STATE_ERROR,0,0)
3044
3045 OP_END
3046};
3047
3048/* 31. Fault injection - received STOP_SENDING for nonexistent receive-only stream */
3049static const struct script_op script_31[] = {
3050 OP_S_SET_INJECT_PLAIN (script_28_inject_plain)
3051 OP_C_SET_ALPN ("ossltest")
3052 OP_C_CONNECT_WAIT ()
3053 OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_NONE)
3054
3055 OP_S_NEW_STREAM_UNI (a, S_UNI_ID(0))
3056 OP_S_WRITE (a, "apple", 5)
3057
3058 OP_C_ACCEPT_STREAM_WAIT (a)
3059 OP_C_READ_EXPECT (a, "apple", 5)
3060
3061 OP_SET_INJECT_WORD (C_UNI_ID(0) + 1, OSSL_QUIC_FRAME_TYPE_STOP_SENDING)
3062 OP_S_WRITE (a, "orange", 6)
3063
3064 OP_C_EXPECT_CONN_CLOSE_INFO(QUIC_ERR_STREAM_STATE_ERROR,0,0)
3065
3066 OP_END
3067};
3068
3069/* 32. Fault injection - STREAM frame for nonexistent stream */
3070static int script_32_inject_plain(struct helper *h, QUIC_PKT_HDR *hdr,
3071 unsigned char *buf, size_t len)
3072{
7eebc354 3073 int ok = 0;
e26dc8e3
HL
3074 WPACKET wpkt;
3075 unsigned char frame_buf[64];
3076 size_t written;
3077 uint64_t type = OSSL_QUIC_FRAME_TYPE_STREAM_OFF_LEN, offset, flen, i;
3078
3079 switch (h->inject_word1) {
3080 default:
3081 return 0;
3082 case 0:
3083 return 1;
3084 case 1:
3085 offset = 0;
1623bf37 3086 flen = 0;
e26dc8e3
HL
3087 break;
3088 case 2:
3089 offset = (((uint64_t)1)<<62) - 1;
1623bf37 3090 flen = 5;
e26dc8e3
HL
3091 break;
3092 case 3:
3093 offset = 1 * 1024 * 1024 * 1024; /* 1G */
1623bf37
HL
3094 flen = 5;
3095 break;
3096 case 4:
3097 offset = 0;
3098 flen = 1;
e26dc8e3
HL
3099 break;
3100 }
3101
3102 if (!TEST_true(WPACKET_init_static_len(&wpkt, frame_buf,
3103 sizeof(frame_buf), 0)))
3104 return 0;
3105
3106 if (!TEST_true(WPACKET_quic_write_vlint(&wpkt, type))
3107 || !TEST_true(WPACKET_quic_write_vlint(&wpkt, /* stream ID */
3108 h->inject_word0 - 1))
3109 || !TEST_true(WPACKET_quic_write_vlint(&wpkt, offset))
3110 || !TEST_true(WPACKET_quic_write_vlint(&wpkt, flen)))
7eebc354 3111 goto err;
e26dc8e3
HL
3112
3113 for (i = 0; i < flen; ++i)
3114 if (!TEST_true(WPACKET_put_bytes_u8(&wpkt, 0x42)))
7eebc354 3115 goto err;
e26dc8e3
HL
3116
3117 if (!TEST_true(WPACKET_get_total_written(&wpkt, &written)))
7eebc354 3118 goto err;
e26dc8e3
HL
3119
3120 if (!qtest_fault_prepend_frame(h->qtf, frame_buf, written))
7eebc354 3121 goto err;
e26dc8e3 3122
7eebc354
HL
3123 ok = 1;
3124err:
96b7df60
HL
3125 if (ok)
3126 WPACKET_finish(&wpkt);
3127 else
3128 WPACKET_cleanup(&wpkt);
7eebc354 3129 return ok;
e26dc8e3
HL
3130}
3131
3132static const struct script_op script_32[] = {
3133 OP_S_SET_INJECT_PLAIN (script_32_inject_plain)
3134 OP_C_SET_ALPN ("ossltest")
3135 OP_C_CONNECT_WAIT ()
3136 OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_NONE)
3137
3138 OP_S_NEW_STREAM_UNI (a, S_UNI_ID(0))
3139 OP_S_WRITE (a, "apple", 5)
3140
3141 OP_C_ACCEPT_STREAM_WAIT (a)
3142 OP_C_READ_EXPECT (a, "apple", 5)
3143
3144 OP_SET_INJECT_WORD (C_UNI_ID(0) + 1, 1)
3145 OP_S_WRITE (a, "orange", 6)
3146
3147 OP_C_EXPECT_CONN_CLOSE_INFO(QUIC_ERR_STREAM_STATE_ERROR,0,0)
3148
3149 OP_END
3150};
3151
3152/* 33. Fault injection - STREAM frame with illegal offset */
3153static const struct script_op script_33[] = {
3154 OP_S_SET_INJECT_PLAIN (script_32_inject_plain)
3155 OP_C_SET_ALPN ("ossltest")
3156 OP_C_CONNECT_WAIT ()
3157 OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_NONE)
3158
3159 OP_C_NEW_STREAM_BIDI (a, C_BIDI_ID(0))
3160 OP_C_WRITE (a, "apple", 5)
3161
3162 OP_S_BIND_STREAM_ID (a, C_BIDI_ID(0))
3163 OP_S_READ_EXPECT (a, "apple", 5)
3164
3165 OP_SET_INJECT_WORD (C_BIDI_ID(0) + 1, 2)
3166 OP_S_WRITE (a, "orange", 6)
3167
3168 OP_C_EXPECT_CONN_CLOSE_INFO(QUIC_ERR_FRAME_ENCODING_ERROR,0,0)
3169
3170 OP_END
3171};
3172
3173/* 34. Fault injection - STREAM frame which exceeds FC */
3174static const struct script_op script_34[] = {
3175 OP_S_SET_INJECT_PLAIN (script_32_inject_plain)
3176 OP_C_SET_ALPN ("ossltest")
3177 OP_C_CONNECT_WAIT ()
3178 OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_NONE)
3179
3180 OP_C_NEW_STREAM_BIDI (a, C_BIDI_ID(0))
3181 OP_C_WRITE (a, "apple", 5)
3182
3183 OP_S_BIND_STREAM_ID (a, C_BIDI_ID(0))
3184 OP_S_READ_EXPECT (a, "apple", 5)
3185
3186 OP_SET_INJECT_WORD (C_BIDI_ID(0) + 1, 3)
3187 OP_S_WRITE (a, "orange", 6)
3188
3189 OP_C_EXPECT_CONN_CLOSE_INFO(QUIC_ERR_FLOW_CONTROL_ERROR,0,0)
3190
3191 OP_END
3192};
3193
3194/* 35. Fault injection - MAX_STREAM_DATA for receive-only stream */
3195static const struct script_op script_35[] = {
3196 OP_S_SET_INJECT_PLAIN (script_28_inject_plain)
3197 OP_C_SET_ALPN ("ossltest")
3198 OP_C_CONNECT_WAIT ()
3199 OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_NONE)
3200
3201 OP_S_NEW_STREAM_UNI (a, S_UNI_ID(0))
3202 OP_S_WRITE (a, "apple", 5)
3203
3204 OP_C_ACCEPT_STREAM_WAIT (a)
3205 OP_C_READ_EXPECT (a, "apple", 5)
3206
3207 OP_SET_INJECT_WORD (S_UNI_ID(0) + 1, OSSL_QUIC_FRAME_TYPE_MAX_STREAM_DATA)
3208 OP_S_WRITE (a, "orange", 6)
3209
3210 OP_C_EXPECT_CONN_CLOSE_INFO(QUIC_ERR_STREAM_STATE_ERROR,0,0)
3211
3212 OP_END
3213};
3214
3215/* 36. Fault injection - MAX_STREAM_DATA for nonexistent stream */
3216static const struct script_op script_36[] = {
3217 OP_S_SET_INJECT_PLAIN (script_28_inject_plain)
3218 OP_C_SET_ALPN ("ossltest")
3219 OP_C_CONNECT_WAIT ()
3220 OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_NONE)
3221
3222 OP_S_NEW_STREAM_UNI (a, S_UNI_ID(0))
3223 OP_S_WRITE (a, "apple", 5)
3224
3225 OP_C_ACCEPT_STREAM_WAIT (a)
3226 OP_C_READ_EXPECT (a, "apple", 5)
3227
3228 OP_SET_INJECT_WORD (C_BIDI_ID(0) + 1, OSSL_QUIC_FRAME_TYPE_MAX_STREAM_DATA)
3229 OP_S_WRITE (a, "orange", 6)
3230
3231 OP_C_EXPECT_CONN_CLOSE_INFO(QUIC_ERR_STREAM_STATE_ERROR,0,0)
3232
3233 OP_END
3234};
3235
3236/* 37. Fault injection - STREAM_DATA_BLOCKED for send-only stream */
3237static const struct script_op script_37[] = {
3238 OP_S_SET_INJECT_PLAIN (script_28_inject_plain)
3239 OP_C_SET_ALPN ("ossltest")
3240 OP_C_CONNECT_WAIT ()
3241 OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_NONE)
3242
3243 OP_C_NEW_STREAM_UNI (a, C_UNI_ID(0))
3244 OP_C_WRITE (a, "apple", 5)
3245
3246 OP_S_BIND_STREAM_ID (a, C_UNI_ID(0))
3247 OP_S_READ_EXPECT (a, "apple", 5)
3248
3249 OP_S_NEW_STREAM_UNI (b, S_UNI_ID(0))
3250 OP_SET_INJECT_WORD (C_UNI_ID(0) + 1, OSSL_QUIC_FRAME_TYPE_STREAM_DATA_BLOCKED)
3251 OP_S_WRITE (b, "orange", 5)
3252
3253 OP_C_EXPECT_CONN_CLOSE_INFO(QUIC_ERR_STREAM_STATE_ERROR,0,0)
3254
3255 OP_END
3256};
3257
3258/* 38. Fault injection - STREAM_DATA_BLOCKED for non-existent stream */
3259static const struct script_op script_38[] = {
3260 OP_S_SET_INJECT_PLAIN (script_28_inject_plain)
3261 OP_C_SET_ALPN ("ossltest")
3262 OP_C_CONNECT_WAIT ()
3263 OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_NONE)
3264
3265 OP_S_NEW_STREAM_UNI (b, S_UNI_ID(0))
3266 OP_SET_INJECT_WORD (C_BIDI_ID(0) + 1, OSSL_QUIC_FRAME_TYPE_STREAM_DATA_BLOCKED)
3267 OP_S_WRITE (b, "orange", 5)
3268
3269 OP_C_EXPECT_CONN_CLOSE_INFO(QUIC_ERR_STREAM_STATE_ERROR,0,0)
3270
3271 OP_END
3272};
3273
3274/* 39. Fault injection - NEW_CONN_ID with zero-len CID */
3275static int script_39_inject_plain(struct helper *h, QUIC_PKT_HDR *hdr,
3276 unsigned char *buf, size_t len)
3277{
7eebc354 3278 int ok = 0;
e26dc8e3
HL
3279 WPACKET wpkt;
3280 unsigned char frame_buf[64];
3281 size_t i, written;
ed75eb32
HL
3282 uint64_t seq_no = 0, retire_prior_to = 0;
3283 QUIC_CONN_ID new_cid = {0};
5881dd2c 3284 QUIC_CHANNEL *ch = ossl_quic_tserver_get_channel(h->s_priv);
e26dc8e3 3285
ed75eb32
HL
3286 switch (h->inject_word1) {
3287 case 0:
e26dc8e3 3288 return 1;
ed75eb32
HL
3289 case 1:
3290 new_cid.id_len = 0;
3291 break;
3292 case 2:
3293 new_cid.id_len = 21;
3294 break;
3295 case 3:
3296 new_cid.id_len = 1;
3297 new_cid.id[0] = 0x55;
3298
3299 seq_no = 0;
3300 retire_prior_to = 1;
3301 break;
3302 case 4:
f2609004 3303 /* Use our actual CID so we don't break connectivity. */
ed75eb32
HL
3304 ossl_quic_channel_get_diag_local_cid(ch, &new_cid);
3305
3306 seq_no = 2;
3307 retire_prior_to = 2;
3308 break;
3309 case 5:
3310 /*
3311 * Use a bogus CID which will need to be ignored if connectivity is to
3312 * be continued.
3313 */
3314 new_cid.id_len = 8;
3315 new_cid.id[0] = 0x55;
3316
3317 seq_no = 1;
3318 retire_prior_to = 1;
3319 break;
3320 }
e26dc8e3
HL
3321
3322 if (!TEST_true(WPACKET_init_static_len(&wpkt, frame_buf,
3323 sizeof(frame_buf), 0)))
3324 return 0;
3325
3326 if (!TEST_true(WPACKET_quic_write_vlint(&wpkt, OSSL_QUIC_FRAME_TYPE_NEW_CONN_ID))
ed75eb32
HL
3327 || !TEST_true(WPACKET_quic_write_vlint(&wpkt, seq_no)) /* seq no */
3328 || !TEST_true(WPACKET_quic_write_vlint(&wpkt, retire_prior_to)) /* retire prior to */
3329 || !TEST_true(WPACKET_put_bytes_u8(&wpkt, new_cid.id_len))) /* len */
7eebc354 3330 goto err;
e26dc8e3 3331
d63b8cbb 3332 for (i = 0; i < new_cid.id_len && i < OSSL_NELEM(new_cid.id); ++i)
ed75eb32
HL
3333 if (!TEST_true(WPACKET_put_bytes_u8(&wpkt, new_cid.id[i])))
3334 goto err;
3335
d63b8cbb
HL
3336 for (; i < new_cid.id_len; ++i)
3337 if (!TEST_true(WPACKET_put_bytes_u8(&wpkt, 0x55)))
3338 goto err;
3339
e26dc8e3
HL
3340 for (i = 0; i < QUIC_STATELESS_RESET_TOKEN_LEN; ++i)
3341 if (!TEST_true(WPACKET_put_bytes_u8(&wpkt, 0x42)))
7eebc354 3342 goto err;
e26dc8e3
HL
3343
3344 if (!TEST_true(WPACKET_get_total_written(&wpkt, &written)))
7eebc354 3345 goto err;
e26dc8e3
HL
3346
3347 if (!qtest_fault_prepend_frame(h->qtf, frame_buf, written))
7eebc354 3348 goto err;
e26dc8e3 3349
7eebc354
HL
3350 ok = 1;
3351err:
96b7df60
HL
3352 if (ok)
3353 WPACKET_finish(&wpkt);
3354 else
3355 WPACKET_cleanup(&wpkt);
7eebc354 3356 return ok;
e26dc8e3
HL
3357}
3358
3359static const struct script_op script_39[] = {
3360 OP_S_SET_INJECT_PLAIN (script_39_inject_plain)
3361 OP_C_SET_ALPN ("ossltest")
3362 OP_C_CONNECT_WAIT ()
3363 OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_NONE)
3364
3365 OP_C_NEW_STREAM_BIDI (a, C_BIDI_ID(0))
3366 OP_C_WRITE (a, "apple", 5)
3367 OP_S_BIND_STREAM_ID (a, C_BIDI_ID(0))
3368 OP_S_READ_EXPECT (a, "apple", 5)
3369
3370 OP_SET_INJECT_WORD (0, 1)
3371 OP_S_WRITE (a, "orange", 5)
3372
3373 OP_C_EXPECT_CONN_CLOSE_INFO(QUIC_ERR_FRAME_ENCODING_ERROR,0,0)
3374
3375 OP_END
3376};
3377
cd5e4380
HL
3378/* 40. Shutdown flush test */
3379static const unsigned char script_40_data[1024] = "strawberry";
3380
3381static const struct script_op script_40[] = {
3382 OP_C_SET_ALPN ("ossltest")
3383 OP_C_CONNECT_WAIT ()
3384 OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_NONE)
3385
3386 OP_C_NEW_STREAM_BIDI (a, C_BIDI_ID(0))
3387 OP_C_WRITE (a, "apple", 5)
3388
3389 OP_C_INHIBIT_TICK (1)
3390 OP_C_SET_WRITE_BUF_SIZE (a, 1024 * 100 * 3)
3391
3392 OP_BEGIN_REPEAT (100)
3393
3394 OP_C_WRITE (a, script_40_data, sizeof(script_40_data))
3395
3396 OP_END_REPEAT ()
3397
3398 OP_C_CONCLUDE (a)
3bc38ba0 3399 OP_C_SHUTDOWN_WAIT (NULL, 0) /* disengages tick inhibition */
cd5e4380
HL
3400
3401 OP_S_BIND_STREAM_ID (a, C_BIDI_ID(0))
3402 OP_S_READ_EXPECT (a, "apple", 5)
3403
3404 OP_BEGIN_REPEAT (100)
3405
3406 OP_S_READ_EXPECT (a, script_40_data, sizeof(script_40_data))
3407
3408 OP_END_REPEAT ()
3409
3410 OP_S_EXPECT_FIN (a)
3411
3412 OP_C_EXPECT_CONN_CLOSE_INFO(0, 1, 0)
3413 OP_S_EXPECT_CONN_CLOSE_INFO(0, 1, 1)
3414
3415 OP_END
3416};
3417
7eb330ff
HL
3418/* 41. Fault injection - PATH_CHALLENGE yields PATH_RESPONSE */
3419static const uint64_t path_challenge = UINT64_C(0xbdeb9451169c83aa);
3420
3421static int script_41_inject_plain(struct helper *h, QUIC_PKT_HDR *hdr,
3422 unsigned char *buf, size_t len)
3423{
3424 int ok = 0;
3425 WPACKET wpkt;
3426 unsigned char frame_buf[16];
3427 size_t written;
3428
3429 if (h->inject_word0 == 0)
3430 return 1;
3431
3432 if (!TEST_true(WPACKET_init_static_len(&wpkt, frame_buf,
3433 sizeof(frame_buf), 0)))
3434 return 0;
3435
a1aff2c6 3436 if (!TEST_true(WPACKET_quic_write_vlint(&wpkt, h->inject_word1))
7eb330ff
HL
3437 || !TEST_true(WPACKET_put_bytes_u64(&wpkt, path_challenge)))
3438 goto err;
3439
3440 if (!TEST_true(WPACKET_get_total_written(&wpkt, &written))
3441 || !TEST_size_t_eq(written, 9))
3442 goto err;
3443
3444 if (!qtest_fault_prepend_frame(h->qtf, frame_buf, written))
3445 goto err;
3446
3447 --h->inject_word0;
3448 ok = 1;
3449err:
3450 if (ok)
3451 WPACKET_finish(&wpkt);
3452 else
3453 WPACKET_cleanup(&wpkt);
3454 return ok;
3455}
3456
3457static void script_41_trace(int write_p, int version, int content_type,
3458 const void *buf, size_t len, SSL *ssl, void *arg)
3459{
3460 uint64_t frame_type, frame_data;
3461 int was_minimal;
3462 struct helper *h = arg;
3463 PACKET pkt;
3464
3465 if (version != OSSL_QUIC1_VERSION
3466 || content_type != SSL3_RT_QUIC_FRAME_FULL
3467 || len < 1)
3468 return;
3469
3470 if (!TEST_true(PACKET_buf_init(&pkt, buf, len))) {
3471 ++h->scratch1;
3472 return;
3473 }
3474
3475 if (!TEST_true(ossl_quic_wire_peek_frame_header(&pkt, &frame_type,
3476 &was_minimal))) {
3477 ++h->scratch1;
3478 return;
3479 }
3480
3481 if (frame_type != OSSL_QUIC_FRAME_TYPE_PATH_RESPONSE)
3482 return;
3483
3484 if (!TEST_true(ossl_quic_wire_decode_frame_path_response(&pkt, &frame_data))
3485 || !TEST_uint64_t_eq(frame_data, path_challenge)) {
3486 ++h->scratch1;
3487 return;
3488 }
3489
3490 ++h->scratch0;
3491}
3492
5881dd2c 3493static int script_41_setup(struct helper *h, struct helper_local *hl)
7eb330ff 3494{
5881dd2c 3495 ossl_quic_tserver_set_msg_callback(ACQUIRE_S(), script_41_trace, h);
7eb330ff
HL
3496 return 1;
3497}
3498
5881dd2c 3499static int script_41_check(struct helper *h, struct helper_local *hl)
7eb330ff
HL
3500{
3501 /* At least one valid challenge/response echo? */
3502 if (!TEST_uint64_t_gt(h->scratch0, 0))
3503 return 0;
3504
3505 /* No failed tests? */
3506 if (!TEST_uint64_t_eq(h->scratch1, 0))
3507 return 0;
3508
3509 return 1;
3510}
3511
3512static const struct script_op script_41[] = {
3513 OP_S_SET_INJECT_PLAIN (script_41_inject_plain)
3514 OP_C_SET_ALPN ("ossltest")
3515 OP_C_CONNECT_WAIT ()
3516 OP_CHECK (script_41_setup, 0)
3517
3518 OP_C_WRITE (DEFAULT, "apple", 5)
3519 OP_S_BIND_STREAM_ID (a, C_BIDI_ID(0))
3520 OP_S_READ_EXPECT (a, "apple", 5)
3521
a1aff2c6 3522 OP_SET_INJECT_WORD (1, OSSL_QUIC_FRAME_TYPE_PATH_CHALLENGE)
7eb330ff
HL
3523
3524 OP_S_WRITE (a, "orange", 6)
3525 OP_C_READ_EXPECT (DEFAULT, "orange", 6)
3526
3527 OP_C_WRITE (DEFAULT, "strawberry", 10)
3528 OP_S_READ_EXPECT (a, "strawberry", 10)
3529
3530 OP_CHECK (script_41_check, 0)
3531 OP_END
3532};
3533
27c2f62f
HL
3534/* 42. Fault injection - CRYPTO frame with illegal offset */
3535static int script_42_inject_plain(struct helper *h, QUIC_PKT_HDR *hdr,
3536 unsigned char *buf, size_t len)
3537{
3538 int ok = 0;
3539 unsigned char frame_buf[64];
3540 size_t written;
3541 WPACKET wpkt;
3542
3543 if (h->inject_word0 == 0)
3544 return 1;
3545
3546 --h->inject_word0;
3547
3548 if (!TEST_true(WPACKET_init_static_len(&wpkt, frame_buf,
3549 sizeof(frame_buf), 0)))
3550 return 0;
3551
3552 if (!TEST_true(WPACKET_quic_write_vlint(&wpkt, OSSL_QUIC_FRAME_TYPE_CRYPTO))
ab6c6345 3553 || !TEST_true(WPACKET_quic_write_vlint(&wpkt, h->inject_word1))
27c2f62f
HL
3554 || !TEST_true(WPACKET_quic_write_vlint(&wpkt, 1))
3555 || !TEST_true(WPACKET_put_bytes_u8(&wpkt, 0x42)))
3556 goto err;
3557
3558 if (!TEST_true(WPACKET_get_total_written(&wpkt, &written)))
3559 goto err;
3560
3561 if (!qtest_fault_prepend_frame(h->qtf, frame_buf, written))
3562 goto err;
3563
3564 ok = 1;
3565err:
3566 if (ok)
3567 WPACKET_finish(&wpkt);
3568 else
3569 WPACKET_cleanup(&wpkt);
3570 return ok;
3571}
3572
3573static const struct script_op script_42[] = {
3574 OP_S_SET_INJECT_PLAIN (script_42_inject_plain)
3575 OP_C_SET_ALPN ("ossltest")
3576 OP_C_CONNECT_WAIT ()
3577 OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_NONE)
3578
3579 OP_C_NEW_STREAM_BIDI (a, C_BIDI_ID(0))
3580 OP_C_WRITE (a, "apple", 5)
3581
3582 OP_S_BIND_STREAM_ID (a, C_BIDI_ID(0))
3583 OP_S_READ_EXPECT (a, "apple", 5)
3584
ab6c6345 3585 OP_SET_INJECT_WORD (1, (((uint64_t)1) << 62) - 1)
27c2f62f
HL
3586 OP_S_WRITE (a, "orange", 6)
3587
3588 OP_C_EXPECT_CONN_CLOSE_INFO(QUIC_ERR_FRAME_ENCODING_ERROR,0,0)
3589
3590 OP_END
3591};
3592
ab6c6345
HL
3593/* 43. Fault injection - CRYPTO frame exceeding FC */
3594static const struct script_op script_43[] = {
3595 OP_S_SET_INJECT_PLAIN (script_42_inject_plain)
3596 OP_C_SET_ALPN ("ossltest")
3597 OP_C_CONNECT_WAIT ()
3598 OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_NONE)
3599
3600 OP_C_NEW_STREAM_BIDI (a, C_BIDI_ID(0))
3601 OP_C_WRITE (a, "apple", 5)
3602
3603 OP_S_BIND_STREAM_ID (a, C_BIDI_ID(0))
3604 OP_S_READ_EXPECT (a, "apple", 5)
3605
3606 OP_SET_INJECT_WORD (1, 0x100000 /* 1 MiB */)
3607 OP_S_WRITE (a, "orange", 6)
3608
3609 OP_C_EXPECT_CONN_CLOSE_INFO(QUIC_ERR_CRYPTO_BUFFER_EXCEEDED,0,0)
3610
3611 OP_END
3612};
3613
97684a15
HL
3614/* 44. Fault injection - PADDING */
3615static int script_44_inject_plain(struct helper *h, QUIC_PKT_HDR *hdr,
3616 unsigned char *buf, size_t len)
3617{
3618 int ok = 0;
3619 WPACKET wpkt;
3620 unsigned char frame_buf[16];
3621 size_t written;
3622
3623 if (h->inject_word0 == 0)
3624 return 1;
3625
3626 if (!TEST_true(WPACKET_init_static_len(&wpkt, frame_buf,
3627 sizeof(frame_buf), 0)))
3628 return 0;
3629
3630 if (!TEST_true(ossl_quic_wire_encode_padding(&wpkt, 1)))
3631 goto err;
3632
3633 if (!TEST_true(WPACKET_get_total_written(&wpkt, &written)))
3634 goto err;
3635
3636 if (!qtest_fault_prepend_frame(h->qtf, frame_buf, written))
3637 goto err;
3638
3639 ok = 1;
3640err:
3641 if (ok)
3642 WPACKET_finish(&wpkt);
3643 else
3644 WPACKET_cleanup(&wpkt);
3645 return ok;
3646}
3647
3648static const struct script_op script_44[] = {
3649 OP_S_SET_INJECT_PLAIN (script_44_inject_plain)
3650 OP_C_SET_ALPN ("ossltest")
3651 OP_C_CONNECT_WAIT ()
3652
3653 OP_C_WRITE (DEFAULT, "apple", 5)
3654 OP_S_BIND_STREAM_ID (a, C_BIDI_ID(0))
3655 OP_S_READ_EXPECT (a, "apple", 5)
3656
3657 OP_SET_INJECT_WORD (1, 0)
3658
3659 OP_S_WRITE (a, "Strawberry", 10)
3660 OP_C_READ_EXPECT (DEFAULT, "Strawberry", 10)
3661
3662 OP_END
3663};
3664
17340e87 3665/* 45. PING must generate ACK */
5881dd2c 3666static int force_ping(struct helper *h, struct helper_local *hl)
17340e87 3667{
5881dd2c 3668 QUIC_CHANNEL *ch = ossl_quic_tserver_get_channel(ACQUIRE_S());
17340e87
HL
3669
3670 h->scratch0 = ossl_quic_channel_get_diag_num_rx_ack(ch);
3671
5881dd2c 3672 if (!TEST_true(ossl_quic_tserver_ping(ACQUIRE_S())))
17340e87
HL
3673 return 0;
3674
3675 return 1;
3676}
3677
5881dd2c 3678static int wait_incoming_acks_increased(struct helper *h, struct helper_local *hl)
17340e87 3679{
5881dd2c 3680 QUIC_CHANNEL *ch = ossl_quic_tserver_get_channel(ACQUIRE_S());
17340e87
HL
3681 uint16_t count;
3682
3683 count = ossl_quic_channel_get_diag_num_rx_ack(ch);
3684
3685 if (count == h->scratch0) {
3686 h->check_spin_again = 1;
3687 return 0;
3688 }
3689
3690 return 1;
3691}
3692
3693static const struct script_op script_45[] = {
3694 OP_C_SET_ALPN ("ossltest")
3695 OP_C_CONNECT_WAIT ()
3696
3697 OP_C_WRITE (DEFAULT, "apple", 5)
3698 OP_S_BIND_STREAM_ID (a, C_BIDI_ID(0))
3699 OP_S_READ_EXPECT (a, "apple", 5)
3700
3701 OP_BEGIN_REPEAT (2)
3702
3703 OP_CHECK (force_ping, 0)
3704 OP_CHECK (wait_incoming_acks_increased, 0)
3705
3706 OP_END_REPEAT ()
3707
3708 OP_S_WRITE (a, "Strawberry", 10)
3709 OP_C_READ_EXPECT (DEFAULT, "Strawberry", 10)
3710
3711 OP_END
3712};
3713
ed0d6ba4
HL
3714/* 46. Fault injection - ACK - malformed initial range */
3715static int script_46_inject_plain(struct helper *h, QUIC_PKT_HDR *hdr,
3716 unsigned char *buf, size_t len)
3717{
3718 int ok = 0;
3719 WPACKET wpkt;
3720 unsigned char frame_buf[16];
3721 size_t written;
d49a1634
HL
3722 uint64_t type = 0, largest_acked = 0, first_range = 0, range_count = 0;
3723 uint64_t agap = 0, alen = 0;
ed0d6ba4
HL
3724 uint64_t ect0 = 0, ect1 = 0, ecnce = 0;
3725
3726 if (h->inject_word0 == 0)
3727 return 1;
3728
3729 if (!TEST_true(WPACKET_init_static_len(&wpkt, frame_buf,
3730 sizeof(frame_buf), 0)))
3731 return 0;
3732
3733 type = OSSL_QUIC_FRAME_TYPE_ACK_WITHOUT_ECN;
3734
3735 switch (h->inject_word0) {
3736 case 1:
3737 largest_acked = 100;
3738 first_range = 101;
3739 range_count = 0;
3740 break;
3741 case 2:
3742 largest_acked = 100;
3743 first_range = 80;
3744 /* [20..100]; [0..18] */
3745 range_count = 1;
3746 agap = 0;
3747 alen = 19;
3748 break;
3749 case 3:
3750 largest_acked = 100;
3751 first_range = 80;
3752 range_count = 1;
3753 agap = 18;
3754 alen = 1;
3755 break;
3756 case 4:
3757 type = OSSL_QUIC_FRAME_TYPE_ACK_WITH_ECN;
3758 largest_acked = 100;
3759 first_range = 1;
3760 range_count = 0;
3761 break;
3762 case 5:
3763 type = OSSL_QUIC_FRAME_TYPE_ACK_WITH_ECN;
3764 largest_acked = 0;
3765 first_range = 0;
3766 range_count = 0;
3767 ect0 = 0;
3768 ect1 = 50;
3769 ecnce = 200;
3770 break;
3771 }
3772
3773 h->inject_word0 = 0;
3774
3775 if (!TEST_true(WPACKET_quic_write_vlint(&wpkt, type))
3776 || !TEST_true(WPACKET_quic_write_vlint(&wpkt, largest_acked))
3777 || !TEST_true(WPACKET_quic_write_vlint(&wpkt, /*ack_delay=*/0))
3778 || !TEST_true(WPACKET_quic_write_vlint(&wpkt, /*ack_range_count=*/range_count))
3779 || !TEST_true(WPACKET_quic_write_vlint(&wpkt, /*first_ack_range=*/first_range)))
3780 goto err;
3781
3782 if (range_count > 0)
3783 if (!TEST_true(WPACKET_quic_write_vlint(&wpkt, /*range[0].gap=*/agap))
3784 || !TEST_true(WPACKET_quic_write_vlint(&wpkt, /*range[0].len=*/alen)))
3785 goto err;
3786
3787 if (type == OSSL_QUIC_FRAME_TYPE_ACK_WITH_ECN)
3788 if (!TEST_true(WPACKET_quic_write_vlint(&wpkt, ect0))
3789 || !TEST_true(WPACKET_quic_write_vlint(&wpkt, ect1))
3790 || !TEST_true(WPACKET_quic_write_vlint(&wpkt, ecnce)))
3791 goto err;
3792
3793 if (!TEST_true(WPACKET_get_total_written(&wpkt, &written)))
3794 goto err;
3795
3796 if (!qtest_fault_prepend_frame(h->qtf, frame_buf, written))
3797 goto err;
3798
3799 ok = 1;
3800err:
3801 if (ok)
3802 WPACKET_finish(&wpkt);
3803 else
3804 WPACKET_cleanup(&wpkt);
3805 return ok;
3806}
3807
3808static const struct script_op script_46[] = {
3809 OP_S_SET_INJECT_PLAIN (script_46_inject_plain)
3810 OP_C_SET_ALPN ("ossltest")
3811 OP_C_CONNECT_WAIT ()
3812
3813 OP_C_WRITE (DEFAULT, "apple", 5)
3814 OP_S_BIND_STREAM_ID (a, C_BIDI_ID(0))
3815 OP_S_READ_EXPECT (a, "apple", 5)
3816
3817 OP_SET_INJECT_WORD (1, 0)
3818
3819 OP_S_WRITE (a, "Strawberry", 10)
3820
3821 OP_C_EXPECT_CONN_CLOSE_INFO(QUIC_ERR_FRAME_ENCODING_ERROR,0,0)
3822
3823 OP_END
3824};
3825
3826/* 47. Fault injection - ACK - malformed subsequent range */
3827static const struct script_op script_47[] = {
3828 OP_S_SET_INJECT_PLAIN (script_46_inject_plain)
3829 OP_C_SET_ALPN ("ossltest")
3830 OP_C_CONNECT_WAIT ()
3831
3832 OP_C_WRITE (DEFAULT, "apple", 5)
3833 OP_S_BIND_STREAM_ID (a, C_BIDI_ID(0))
3834 OP_S_READ_EXPECT (a, "apple", 5)
3835
3836 OP_SET_INJECT_WORD (2, 0)
3837
3838 OP_S_WRITE (a, "Strawberry", 10)
3839
3840 OP_C_EXPECT_CONN_CLOSE_INFO(QUIC_ERR_FRAME_ENCODING_ERROR,0,0)
3841
3842 OP_END
3843};
3844
3845/* 48. Fault injection - ACK - malformed subsequent range */
3846static const struct script_op script_48[] = {
3847 OP_S_SET_INJECT_PLAIN (script_46_inject_plain)
3848 OP_C_SET_ALPN ("ossltest")
3849 OP_C_CONNECT_WAIT ()
3850
3851 OP_C_WRITE (DEFAULT, "apple", 5)
3852 OP_S_BIND_STREAM_ID (a, C_BIDI_ID(0))
3853 OP_S_READ_EXPECT (a, "apple", 5)
3854
3855 OP_SET_INJECT_WORD (3, 0)
3856
3857 OP_S_WRITE (a, "Strawberry", 10)
3858
3859 OP_C_EXPECT_CONN_CLOSE_INFO(QUIC_ERR_FRAME_ENCODING_ERROR,0,0)
3860
3861 OP_END
3862};
3863
3864/* 49. Fault injection - ACK - fictional PN */
3865static const struct script_op script_49[] = {
3866 OP_S_SET_INJECT_PLAIN (script_46_inject_plain)
3867 OP_C_SET_ALPN ("ossltest")
3868 OP_C_CONNECT_WAIT ()
3869
3870 OP_C_WRITE (DEFAULT, "apple", 5)
3871 OP_S_BIND_STREAM_ID (a, C_BIDI_ID(0))
3872 OP_S_READ_EXPECT (a, "apple", 5)
3873
3874 OP_SET_INJECT_WORD (4, 0)
3875
3876 OP_S_WRITE (a, "Strawberry", 10)
3877 OP_C_READ_EXPECT (DEFAULT, "Strawberry", 10)
3878
3879 OP_END
3880};
3881
3882/* 50. Fault injection - ACK - duplicate PN */
3883static const struct script_op script_50[] = {
3884 OP_S_SET_INJECT_PLAIN (script_46_inject_plain)
3885 OP_C_SET_ALPN ("ossltest")
3886 OP_C_CONNECT_WAIT ()
3887
3888 OP_C_WRITE (DEFAULT, "apple", 5)
3889 OP_S_BIND_STREAM_ID (a, C_BIDI_ID(0))
3890 OP_S_READ_EXPECT (a, "apple", 5)
3891
3892 OP_BEGIN_REPEAT (2)
3893
3894 OP_SET_INJECT_WORD (5, 0)
3895
3896 OP_S_WRITE (a, "Strawberry", 10)
3897 OP_C_READ_EXPECT (DEFAULT, "Strawberry", 10)
3898
3899 OP_END_REPEAT ()
3900
3901 OP_END
3902};
3903
f2609004 3904/* 51. Fault injection - PATH_RESPONSE is ignored */
a1aff2c6
HL
3905static const struct script_op script_51[] = {
3906 OP_S_SET_INJECT_PLAIN (script_41_inject_plain)
3907 OP_C_SET_ALPN ("ossltest")
3908 OP_C_CONNECT_WAIT ()
3909
3910 OP_C_WRITE (DEFAULT, "apple", 5)
3911 OP_S_BIND_STREAM_ID (a, C_BIDI_ID(0))
3912 OP_S_READ_EXPECT (a, "apple", 5)
3913
3914 OP_SET_INJECT_WORD (1, OSSL_QUIC_FRAME_TYPE_PATH_RESPONSE)
3915
3916 OP_S_WRITE (a, "orange", 6)
3917 OP_C_READ_EXPECT (DEFAULT, "orange", 6)
3918
3919 OP_C_WRITE (DEFAULT, "Strawberry", 10)
3920 OP_S_READ_EXPECT (a, "Strawberry", 10)
3921
3922 OP_END
3923};
3924
477944b6
HL
3925/* 52. Fault injection - ignore BLOCKED frames with bogus values */
3926static int script_52_inject_plain(struct helper *h, QUIC_PKT_HDR *hdr,
3927 unsigned char *buf, size_t len)
3928{
3929 int ok = 0;
3930 unsigned char frame_buf[64];
3931 size_t written;
3932 WPACKET wpkt;
3933 uint64_t type = h->inject_word1;
3934
3935 if (h->inject_word0 == 0)
3936 return 1;
3937
3938 --h->inject_word0;
3939
3940 if (!TEST_true(WPACKET_init_static_len(&wpkt, frame_buf,
3941 sizeof(frame_buf), 0)))
3942 return 0;
3943
3944 if (!TEST_true(WPACKET_quic_write_vlint(&wpkt, type)))
3945 goto err;
3946
3947 if (type == OSSL_QUIC_FRAME_TYPE_STREAM_DATA_BLOCKED)
3948 if (!TEST_true(WPACKET_quic_write_vlint(&wpkt, C_BIDI_ID(0))))
3949 goto err;
3950
3951 if (!TEST_true(WPACKET_quic_write_vlint(&wpkt, 0xFFFFFF)))
3952 goto err;
3953
3954 if (!TEST_true(WPACKET_get_total_written(&wpkt, &written)))
3955 goto err;
3956
3957 if (!qtest_fault_prepend_frame(h->qtf, frame_buf, written))
3958 goto err;
3959
3960 ok = 1;
3961err:
3962 if (ok)
3963 WPACKET_finish(&wpkt);
3964 else
3965 WPACKET_cleanup(&wpkt);
3966 return ok;
3967}
3968
3969static const struct script_op script_52[] = {
3970 OP_S_SET_INJECT_PLAIN (script_52_inject_plain)
3971 OP_C_SET_ALPN ("ossltest")
3972 OP_C_CONNECT_WAIT ()
3973
3974 OP_C_WRITE (DEFAULT, "apple", 5)
3975 OP_S_BIND_STREAM_ID (a, C_BIDI_ID(0))
3976 OP_S_READ_EXPECT (a, "apple", 5)
3977
3978 OP_SET_INJECT_WORD (1, OSSL_QUIC_FRAME_TYPE_DATA_BLOCKED)
3979
3980 OP_S_WRITE (a, "orange", 6)
3981 OP_C_READ_EXPECT (DEFAULT, "orange", 6)
3982
3983 OP_C_WRITE (DEFAULT, "Strawberry", 10)
3984 OP_S_READ_EXPECT (a, "Strawberry", 10)
3985
3986 OP_SET_INJECT_WORD (1, OSSL_QUIC_FRAME_TYPE_STREAM_DATA_BLOCKED)
3987
3988 OP_S_WRITE (a, "orange", 6)
3989 OP_C_READ_EXPECT (DEFAULT, "orange", 6)
3990
3991 OP_C_WRITE (DEFAULT, "Strawberry", 10)
3992 OP_S_READ_EXPECT (a, "Strawberry", 10)
3993
3994 OP_SET_INJECT_WORD (1, OSSL_QUIC_FRAME_TYPE_STREAMS_BLOCKED_UNI)
3995
3996 OP_S_WRITE (a, "orange", 6)
3997 OP_C_READ_EXPECT (DEFAULT, "orange", 6)
3998
3999 OP_C_WRITE (DEFAULT, "Strawberry", 10)
4000 OP_S_READ_EXPECT (a, "Strawberry", 10)
4001
4002 OP_SET_INJECT_WORD (1, OSSL_QUIC_FRAME_TYPE_STREAMS_BLOCKED_BIDI)
4003
4004 OP_S_WRITE (a, "orange", 6)
4005 OP_C_READ_EXPECT (DEFAULT, "orange", 6)
4006
4007 OP_C_WRITE (DEFAULT, "Strawberry", 10)
4008 OP_S_READ_EXPECT (a, "Strawberry", 10)
4009
4010 OP_END
4011};
a1aff2c6 4012
de56eebd
HL
4013/* 53. Fault injection - excess CRYPTO buffer size */
4014static int script_53_inject_plain(struct helper *h, QUIC_PKT_HDR *hdr,
4015 unsigned char *buf, size_t len)
4016{
4017 int ok = 0;
4018 size_t written;
4019 WPACKET wpkt;
4020 uint64_t offset = 0, data_len = 100;
4021 unsigned char *frame_buf = NULL;
4022 size_t frame_len, i;
4023
4024 if (h->inject_word0 == 0)
4025 return 1;
4026
4027 h->inject_word0 = 0;
4028
4029 switch (h->inject_word1) {
4030 case 0:
4031 /*
4032 * Far out offset which will not have been reached during handshake.
4033 * This will not be delivered to the QUIC_TLS instance since it will be
4034 * waiting for in-order delivery of previous bytes. This tests our flow
4035 * control on CRYPTO stream buffering.
4036 */
4037 offset = 100000;
4038 data_len = 1;
4039 break;
4040 }
4041
d49a1634 4042 frame_len = 1 + 8 + 8 + (size_t)data_len;
de56eebd
HL
4043 if (!TEST_ptr(frame_buf = OPENSSL_malloc(frame_len)))
4044 return 0;
4045
4046 if (!TEST_true(WPACKET_init_static_len(&wpkt, frame_buf, frame_len, 0)))
4047 goto err;
4048
4049 if (!TEST_true(WPACKET_quic_write_vlint(&wpkt, OSSL_QUIC_FRAME_TYPE_CRYPTO))
4050 || !TEST_true(WPACKET_quic_write_vlint(&wpkt, offset))
4051 || !TEST_true(WPACKET_quic_write_vlint(&wpkt, data_len)))
4052 goto err;
4053
4054 for (i = 0; i < data_len; ++i)
4055 if (!TEST_true(WPACKET_put_bytes_u8(&wpkt, 0x42)))
4056 goto err;
4057
4058 if (!TEST_true(WPACKET_get_total_written(&wpkt, &written)))
4059 goto err;
4060
4061 if (!qtest_fault_prepend_frame(h->qtf, frame_buf, written))
4062 goto err;
4063
4064 ok = 1;
4065err:
4066 if (ok)
4067 WPACKET_finish(&wpkt);
4068 else
4069 WPACKET_cleanup(&wpkt);
4070 OPENSSL_free(frame_buf);
4071 return ok;
4072}
4073
4074static const struct script_op script_53[] = {
4075 OP_S_SET_INJECT_PLAIN (script_53_inject_plain)
4076 OP_C_SET_ALPN ("ossltest")
4077 OP_C_CONNECT_WAIT ()
4078
4079 OP_C_WRITE (DEFAULT, "apple", 5)
4080 OP_S_BIND_STREAM_ID (a, C_BIDI_ID(0))
4081 OP_S_READ_EXPECT (a, "apple", 5)
4082
4083 OP_SET_INJECT_WORD (1, 0)
4084 OP_S_WRITE (a, "Strawberry", 10)
4085
4086 OP_C_EXPECT_CONN_CLOSE_INFO(QUIC_ERR_CRYPTO_BUFFER_EXCEEDED,0,0)
4087
4088 OP_END
4089};
4090
4091/* 54. Fault injection - corrupted crypto stream data */
4092static int script_54_inject_handshake(struct helper *h,
4093 unsigned char *buf, size_t buf_len)
4094{
4095 size_t i;
4096
4097 for (i = 0; i < buf_len; ++i)
4098 buf[i] ^= 0xff;
4099
4100 return 1;
4101}
4102
4103static const struct script_op script_54[] = {
4104 OP_S_SET_INJECT_HANDSHAKE(script_54_inject_handshake)
4105 OP_C_SET_ALPN ("ossltest")
4106 OP_C_CONNECT_WAIT_OR_FAIL()
4107
4108 OP_C_EXPECT_CONN_CLOSE_INFO(QUIC_ERR_CRYPTO_UNEXPECTED_MESSAGE,0,0)
4109
4110 OP_END
4111};
4112
ed75eb32
HL
4113/* 55. Fault injection - NEW_CONN_ID with >20 byte CID */
4114static const struct script_op script_55[] = {
4115 OP_S_SET_INJECT_PLAIN (script_39_inject_plain)
4116 OP_C_SET_ALPN ("ossltest")
4117 OP_C_CONNECT_WAIT ()
4118 OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_NONE)
4119
4120 OP_C_NEW_STREAM_BIDI (a, C_BIDI_ID(0))
4121 OP_C_WRITE (a, "apple", 5)
4122 OP_S_BIND_STREAM_ID (a, C_BIDI_ID(0))
4123 OP_S_READ_EXPECT (a, "apple", 5)
4124
4125 OP_SET_INJECT_WORD (0, 2)
4126 OP_S_WRITE (a, "orange", 5)
4127
4128 OP_C_EXPECT_CONN_CLOSE_INFO(QUIC_ERR_FRAME_ENCODING_ERROR,0,0)
4129
4130 OP_END
4131};
4132
4133/* 56. Fault injection - NEW_CONN_ID with seq no < retire prior to */
4134static const struct script_op script_56[] = {
4135 OP_S_SET_INJECT_PLAIN (script_39_inject_plain)
4136 OP_C_SET_ALPN ("ossltest")
4137 OP_C_CONNECT_WAIT ()
4138 OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_NONE)
4139
4140 OP_C_NEW_STREAM_BIDI (a, C_BIDI_ID(0))
4141 OP_C_WRITE (a, "apple", 5)
4142 OP_S_BIND_STREAM_ID (a, C_BIDI_ID(0))
4143 OP_S_READ_EXPECT (a, "apple", 5)
4144
4145 OP_SET_INJECT_WORD (0, 3)
4146 OP_S_WRITE (a, "orange", 5)
4147
4148 OP_C_EXPECT_CONN_CLOSE_INFO(QUIC_ERR_FRAME_ENCODING_ERROR,0,0)
4149
4150 OP_END
4151};
4152
f2609004 4153/* 57. Fault injection - NEW_CONN_ID with lower seq so ignored */
ed75eb32
HL
4154static const struct script_op script_57[] = {
4155 OP_S_SET_INJECT_PLAIN (script_39_inject_plain)
4156 OP_C_SET_ALPN ("ossltest")
4157 OP_C_CONNECT_WAIT ()
4158 OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_NONE)
4159
4160 OP_C_NEW_STREAM_BIDI (a, C_BIDI_ID(0))
4161 OP_C_WRITE (a, "apple", 5)
4162 OP_S_BIND_STREAM_ID (a, C_BIDI_ID(0))
4163 OP_S_READ_EXPECT (a, "apple", 5)
4164
4165 OP_SET_INJECT_WORD (0, 4)
4166 OP_S_WRITE (a, "orange", 5)
4167 OP_C_READ_EXPECT (a, "orange", 5)
4168
4169 OP_C_WRITE (a, "Strawberry", 10)
4170 OP_S_READ_EXPECT (a, "Strawberry", 10)
4171
4172 /*
4173 * Now we send a NEW_CONN_ID with a bogus CID. However the sequence number
4174 * is old so it should be ignored and we should still be able to
4175 * communicate.
4176 */
4177 OP_SET_INJECT_WORD (0, 5)
4178 OP_S_WRITE (a, "raspberry", 9)
4179 OP_C_READ_EXPECT (a, "raspberry", 9)
4180
4181 OP_C_WRITE (a, "peach", 5)
4182 OP_S_READ_EXPECT (a, "peach", 5)
4183
4184 OP_END
4185};
4186
d56b81ac
HL
4187/* 58. Fault injection - repeated HANDSHAKE_DONE */
4188static int script_58_inject_plain(struct helper *h, QUIC_PKT_HDR *hdr,
4189 unsigned char *buf, size_t len)
4190{
4191 int ok = 0;
4192 unsigned char frame_buf[64];
4193 size_t written;
4194 WPACKET wpkt;
4195
4196 if (h->inject_word0 == 0)
4197 return 1;
4198
4199 if (!TEST_true(WPACKET_init_static_len(&wpkt, frame_buf,
4200 sizeof(frame_buf), 0)))
4201 return 0;
4202
4203 if (h->inject_word0 == 1) {
4204 if (!TEST_true(WPACKET_quic_write_vlint(&wpkt, OSSL_QUIC_FRAME_TYPE_HANDSHAKE_DONE)))
4205 goto err;
4206 } else {
4207 /* Needless multi-byte encoding */
4208 if (!TEST_true(WPACKET_put_bytes_u8(&wpkt, 0x40))
4209 || !TEST_true(WPACKET_put_bytes_u8(&wpkt, 0x1E)))
4210 goto err;
4211 }
4212
4213 if (!TEST_true(WPACKET_get_total_written(&wpkt, &written)))
4214 goto err;
4215
4216 if (!qtest_fault_prepend_frame(h->qtf, frame_buf, written))
4217 goto err;
4218
4219 ok = 1;
4220err:
4221 if (ok)
4222 WPACKET_finish(&wpkt);
4223 else
4224 WPACKET_cleanup(&wpkt);
4225 return ok;
4226}
4227
4228static const struct script_op script_58[] = {
4229 OP_S_SET_INJECT_PLAIN (script_58_inject_plain)
4230 OP_C_SET_ALPN ("ossltest")
4231 OP_C_CONNECT_WAIT ()
4232
4233 OP_C_WRITE (DEFAULT, "apple", 5)
4234 OP_S_BIND_STREAM_ID (a, C_BIDI_ID(0))
4235 OP_S_READ_EXPECT (a, "apple", 5)
4236
4237 OP_SET_INJECT_WORD (1, 0)
4238
4239 OP_S_WRITE (a, "orange", 6)
4240 OP_C_READ_EXPECT (DEFAULT, "orange", 6)
4241
4242 OP_C_WRITE (DEFAULT, "Strawberry", 10)
4243 OP_S_READ_EXPECT (a, "Strawberry", 10)
4244
4245 OP_END
4246};
4247
4248/* 59. Fault injection - multi-byte frame encoding */
4249static const struct script_op script_59[] = {
4250 OP_S_SET_INJECT_PLAIN (script_58_inject_plain)
4251 OP_C_SET_ALPN ("ossltest")
4252 OP_C_CONNECT_WAIT ()
4253
4254 OP_C_WRITE (DEFAULT, "apple", 5)
4255 OP_S_BIND_STREAM_ID (a, C_BIDI_ID(0))
4256 OP_S_READ_EXPECT (a, "apple", 5)
4257
4258 OP_SET_INJECT_WORD (2, 0)
4259
4260 OP_S_WRITE (a, "orange", 6)
4261
4262 OP_C_EXPECT_CONN_CLOSE_INFO(QUIC_ERR_PROTOCOL_VIOLATION,0,0)
4263
4264 OP_END
4265};
4266
d49a1634
HL
4267/* 60. Connection close reason truncation */
4268static char long_reason[2048];
4269
5881dd2c 4270static int init_reason(struct helper *h, struct helper_local *hl)
d49a1634
HL
4271{
4272 memset(long_reason, '~', sizeof(long_reason));
4273 memcpy(long_reason, "This is a long reason string.", 29);
d63b8cbb 4274 long_reason[OSSL_NELEM(long_reason) - 1] = '\0';
d49a1634
HL
4275 return 1;
4276}
4277
5881dd2c 4278static int check_shutdown_reason(struct helper *h, struct helper_local *hl)
d49a1634 4279{
5881dd2c 4280 const QUIC_TERMINATE_CAUSE *tc = ossl_quic_tserver_get_terminate_cause(ACQUIRE_S());
d49a1634
HL
4281
4282 if (tc == NULL) {
4283 h->check_spin_again = 1;
4284 return 0;
4285 }
4286
4287 if (!TEST_size_t_ge(tc->reason_len, 50)
4288 || !TEST_mem_eq(long_reason, tc->reason_len,
4289 tc->reason, tc->reason_len))
4290 return 0;
4291
4292 return 1;
4293}
4294
4295static const struct script_op script_60[] = {
4296 OP_C_SET_ALPN ("ossltest")
4297 OP_C_CONNECT_WAIT ()
4298
4299 OP_C_WRITE (DEFAULT, "apple", 5)
4300 OP_S_BIND_STREAM_ID (a, C_BIDI_ID(0))
4301 OP_S_READ_EXPECT (a, "apple", 5)
4302
4303 OP_CHECK (init_reason, 0)
3bc38ba0 4304 OP_C_SHUTDOWN_WAIT (long_reason, 0)
d49a1634
HL
4305 OP_CHECK (check_shutdown_reason, 0)
4306
4307 OP_END
4308};
4309
d63b8cbb
HL
4310/* 61. Fault injection - RESET_STREAM exceeding stream count FC */
4311static int script_61_inject_plain(struct helper *h, QUIC_PKT_HDR *hdr,
4312 unsigned char *buf, size_t len)
4313{
4314 int ok = 0;
4315 WPACKET wpkt;
4316 unsigned char frame_buf[32];
4317 size_t written;
4318
4319 if (h->inject_word0 == 0)
4320 return 1;
4321
4322 if (!TEST_true(WPACKET_init_static_len(&wpkt, frame_buf,
4323 sizeof(frame_buf), 0)))
4324 return 0;
4325
4326 if (!TEST_true(WPACKET_quic_write_vlint(&wpkt, h->inject_word0))
4327 || !TEST_true(WPACKET_quic_write_vlint(&wpkt, /* stream ID */
4328 h->inject_word1))
4329 || !TEST_true(WPACKET_quic_write_vlint(&wpkt, 123))
e501e8b6 4330 || (h->inject_word0 == OSSL_QUIC_FRAME_TYPE_RESET_STREAM
d63b8cbb
HL
4331 && !TEST_true(WPACKET_quic_write_vlint(&wpkt, 0)))) /* final size */
4332 goto err;
4333
4334 if (!TEST_true(WPACKET_get_total_written(&wpkt, &written)))
4335 goto err;
4336
4337 if (!qtest_fault_prepend_frame(h->qtf, frame_buf, written))
4338 goto err;
4339
4340 ok = 1;
4341err:
4342 if (ok)
4343 WPACKET_finish(&wpkt);
4344 else
4345 WPACKET_cleanup(&wpkt);
4346 return ok;
4347}
4348
4349static const struct script_op script_61[] = {
4350 OP_S_SET_INJECT_PLAIN (script_61_inject_plain)
4351 OP_C_SET_ALPN ("ossltest")
4352 OP_C_CONNECT_WAIT ()
4353 OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_NONE)
4354
4355 OP_C_NEW_STREAM_BIDI (a, C_BIDI_ID(0))
4356 OP_C_WRITE (a, "orange", 6)
4357
4358 OP_S_BIND_STREAM_ID (a, C_BIDI_ID(0))
4359 OP_S_READ_EXPECT (a, "orange", 6)
4360
4361 OP_SET_INJECT_WORD (OSSL_QUIC_FRAME_TYPE_RESET_STREAM,
4362 S_BIDI_ID(OSSL_QUIC_VLINT_MAX / 4))
4363 OP_S_WRITE (a, "fruit", 5)
4364
4365 OP_C_EXPECT_CONN_CLOSE_INFO(QUIC_ERR_STREAM_LIMIT_ERROR,0,0)
4366
4367 OP_END
4368};
4369
4370/* 62. Fault injection - STOP_SENDING with high ID */
4371static const struct script_op script_62[] = {
4372 OP_S_SET_INJECT_PLAIN (script_61_inject_plain)
4373 OP_C_SET_ALPN ("ossltest")
4374 OP_C_CONNECT_WAIT ()
4375 OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_NONE)
4376
4377 OP_C_NEW_STREAM_BIDI (a, C_BIDI_ID(0))
4378 OP_C_WRITE (a, "orange", 6)
4379
4380 OP_S_BIND_STREAM_ID (a, C_BIDI_ID(0))
4381 OP_S_READ_EXPECT (a, "orange", 6)
4382
4383 OP_SET_INJECT_WORD (OSSL_QUIC_FRAME_TYPE_STOP_SENDING,
4384 C_BIDI_ID(OSSL_QUIC_VLINT_MAX / 4))
4385 OP_S_WRITE (a, "fruit", 5)
4386
4387 OP_C_EXPECT_CONN_CLOSE_INFO(QUIC_ERR_STREAM_STATE_ERROR,0,0)
4388
4389 OP_END
4390};
d49a1634 4391
1623bf37
HL
4392/* 63. Fault injection - STREAM frame exceeding stream limit */
4393static const struct script_op script_63[] = {
4394 OP_S_SET_INJECT_PLAIN (script_32_inject_plain)
4395 OP_C_SET_ALPN ("ossltest")
4396 OP_C_CONNECT_WAIT ()
4397 OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_NONE)
4398
4399 OP_C_NEW_STREAM_BIDI (a, C_BIDI_ID(0))
4400 OP_C_WRITE (a, "apple", 5)
4401
4402 OP_S_BIND_STREAM_ID (a, C_BIDI_ID(0))
4403 OP_S_READ_EXPECT (a, "apple", 5)
4404
4405 OP_SET_INJECT_WORD (S_BIDI_ID(5000) + 1, 4)
4406 OP_S_WRITE (a, "orange", 6)
4407
4408 OP_C_EXPECT_CONN_CLOSE_INFO(QUIC_ERR_STREAM_LIMIT_ERROR,0,0)
4409
4410 OP_END
4411};
4412
4413/* 64. Fault injection - STREAM - zero-length no-FIN is accepted */
4414static const struct script_op script_64[] = {
4415 OP_S_SET_INJECT_PLAIN (script_32_inject_plain)
4416 OP_C_SET_ALPN ("ossltest")
4417 OP_C_CONNECT_WAIT ()
4418 OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_NONE)
4419
4420 OP_S_NEW_STREAM_UNI (a, S_UNI_ID(0))
4421 OP_S_WRITE (a, "apple", 5)
4422
4423 OP_C_ACCEPT_STREAM_WAIT (a)
4424 OP_C_READ_EXPECT (a, "apple", 5)
4425
4426 OP_SET_INJECT_WORD (S_BIDI_ID(20) + 1, 1)
4427 OP_S_WRITE (a, "orange", 6)
4428 OP_C_READ_EXPECT (a, "orange", 6)
4429
4430 OP_END
4431};
4432
4433/* 65. Fault injection - CRYPTO - zero-length is accepted */
4434static int script_65_inject_plain(struct helper *h, QUIC_PKT_HDR *hdr,
4435 unsigned char *buf, size_t len)
4436{
4437 int ok = 0;
4438 unsigned char frame_buf[64];
4439 size_t written;
4440 WPACKET wpkt;
4441
4442 if (h->inject_word0 == 0)
4443 return 1;
4444
4445 --h->inject_word0;
4446
4447 if (!TEST_true(WPACKET_init_static_len(&wpkt, frame_buf,
4448 sizeof(frame_buf), 0)))
4449 return 0;
4450
4451 if (!TEST_true(WPACKET_quic_write_vlint(&wpkt, OSSL_QUIC_FRAME_TYPE_CRYPTO))
4452 || !TEST_true(WPACKET_quic_write_vlint(&wpkt, 0))
4453 || !TEST_true(WPACKET_quic_write_vlint(&wpkt, 0)))
4454 goto err;
4455
4456 if (!TEST_true(WPACKET_get_total_written(&wpkt, &written)))
4457 goto err;
4458
4459 if (!qtest_fault_prepend_frame(h->qtf, frame_buf, written))
4460 goto err;
4461
4462 ok = 1;
4463err:
4464 if (ok)
4465 WPACKET_finish(&wpkt);
4466 else
4467 WPACKET_cleanup(&wpkt);
4468 return ok;
4469}
4470
4471static const struct script_op script_65[] = {
4472 OP_S_SET_INJECT_PLAIN (script_65_inject_plain)
4473 OP_C_SET_ALPN ("ossltest")
4474 OP_C_CONNECT_WAIT ()
4475 OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_NONE)
4476
4477 OP_C_NEW_STREAM_BIDI (a, C_BIDI_ID(0))
4478 OP_C_WRITE (a, "apple", 5)
4479
4480 OP_S_BIND_STREAM_ID (a, C_BIDI_ID(0))
4481 OP_S_READ_EXPECT (a, "apple", 5)
4482
4483 OP_SET_INJECT_WORD (1, 0)
4484 OP_S_WRITE (a, "orange", 6)
4485 OP_C_READ_EXPECT (a, "orange", 6)
4486
4487 OP_END
4488};
4489
4490/* 66. Fault injection - large MAX_STREAM_DATA */
4491static int script_66_inject_plain(struct helper *h, QUIC_PKT_HDR *hdr,
4492 unsigned char *buf, size_t len)
4493{
4494 int ok = 0;
4495 WPACKET wpkt;
4496 unsigned char frame_buf[64];
4497 size_t written;
4498
4499 if (h->inject_word0 == 0)
4500 return 1;
4501
4502 if (!TEST_true(WPACKET_init_static_len(&wpkt, frame_buf,
4503 sizeof(frame_buf), 0)))
4504 return 0;
4505
4506 if (!TEST_true(WPACKET_quic_write_vlint(&wpkt, h->inject_word1)))
4507 goto err;
4508
4509 if (h->inject_word1 == OSSL_QUIC_FRAME_TYPE_MAX_STREAM_DATA)
4510 if (!TEST_true(WPACKET_quic_write_vlint(&wpkt, /* stream ID */
4511 h->inject_word0 - 1)))
4512 goto err;
4513
4514 if (!TEST_true(WPACKET_quic_write_vlint(&wpkt, OSSL_QUIC_VLINT_MAX)))
4515 goto err;
4516
4517 if (!TEST_true(WPACKET_get_total_written(&wpkt, &written)))
4518 goto err;
4519
4520 if (!qtest_fault_prepend_frame(h->qtf, frame_buf, written))
4521 goto err;
4522
4523 ok = 1;
4524err:
4525 if (ok)
4526 WPACKET_finish(&wpkt);
4527 else
4528 WPACKET_cleanup(&wpkt);
4529 return ok;
4530}
4531
4532static const struct script_op script_66[] = {
4533 OP_S_SET_INJECT_PLAIN (script_66_inject_plain)
4534 OP_C_SET_ALPN ("ossltest")
4535 OP_C_CONNECT_WAIT ()
4536 OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_NONE)
4537
4538 OP_S_NEW_STREAM_BIDI (a, S_BIDI_ID(0))
4539 OP_S_WRITE (a, "apple", 5)
4540
4541 OP_C_ACCEPT_STREAM_WAIT (a)
4542 OP_C_READ_EXPECT (a, "apple", 5)
4543
4544 OP_SET_INJECT_WORD (S_BIDI_ID(0) + 1, OSSL_QUIC_FRAME_TYPE_MAX_STREAM_DATA)
4545 OP_S_WRITE (a, "orange", 6)
4546 OP_C_READ_EXPECT (a, "orange", 6)
4547 OP_C_WRITE (a, "Strawberry", 10)
4548 OP_S_READ_EXPECT (a, "Strawberry", 10)
4549
4550 OP_END
4551};
4552
4553/* 67. Fault injection - large MAX_DATA */
4554static const struct script_op script_67[] = {
4555 OP_S_SET_INJECT_PLAIN (script_66_inject_plain)
4556 OP_C_SET_ALPN ("ossltest")
4557 OP_C_CONNECT_WAIT ()
4558 OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_NONE)
4559
4560 OP_S_NEW_STREAM_BIDI (a, S_BIDI_ID(0))
4561 OP_S_WRITE (a, "apple", 5)
4562
4563 OP_C_ACCEPT_STREAM_WAIT (a)
4564 OP_C_READ_EXPECT (a, "apple", 5)
4565
4566 OP_SET_INJECT_WORD (1, OSSL_QUIC_FRAME_TYPE_MAX_DATA)
4567 OP_S_WRITE (a, "orange", 6)
4568 OP_C_READ_EXPECT (a, "orange", 6)
4569 OP_C_WRITE (a, "Strawberry", 10)
4570 OP_S_READ_EXPECT (a, "Strawberry", 10)
4571
4572 OP_END
4573};
4574
644ef0bb
MC
4575/* 68. Fault injection - Unexpected TLS messages */
4576static int script_68_inject_handshake(struct helper *h, unsigned char *msg,
4577 size_t msglen)
4578{
4579 const unsigned char *data;
4580 size_t datalen;
4581 const unsigned char certreq[] = {
4582 SSL3_MT_CERTIFICATE_REQUEST, /* CertificateRequest message */
4583 0, 0, 12, /* Length of message */
4584 1, 1, /* certificate_request_context */
4585 0, 8, /* Extensions block length */
4586 0, TLSEXT_TYPE_signature_algorithms, /* sig_algs extension*/
4587 0, 4, /* 4 bytes of sig algs extension*/
4588 0, 2, /* sigalgs list is 2 bytes long */
4589 8, 4 /* rsa_pss_rsae_sha256 */
4590 };
4591 const unsigned char keyupdate[] = {
4592 SSL3_MT_KEY_UPDATE, /* KeyUpdate message */
4593 0, 0, 1, /* Length of message */
4594 SSL_KEY_UPDATE_NOT_REQUESTED /* update_not_requested */
4595 };
4596
4597 /* We transform the NewSessionTicket message into something else */
4598 switch(h->inject_word0) {
4599 case 0:
4600 return 1;
4601
4602 case 1:
4603 /* CertificateRequest message */
4604 data = certreq;
4605 datalen = sizeof(certreq);
4606 break;
4607
4608 case 2:
4609 /* KeyUpdate message */
4610 data = keyupdate;
4611 datalen = sizeof(keyupdate);
4612 break;
4613
4614 default:
4615 return 0;
4616 }
4617
4618 if (!TEST_true(qtest_fault_resize_message(h->qtf,
4619 datalen - SSL3_HM_HEADER_LENGTH)))
4620 return 0;
4621
4622 memcpy(msg, data, datalen);
4623
4624 return 1;
4625}
4626
4627/* Send a CerticateRequest message post-handshake */
4628static const struct script_op script_68[] = {
4629 OP_S_SET_INJECT_HANDSHAKE(script_68_inject_handshake)
4630 OP_C_SET_ALPN ("ossltest")
4631 OP_C_CONNECT_WAIT ()
4632 OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_NONE)
4633
4634 OP_C_NEW_STREAM_BIDI (a, C_BIDI_ID(0))
4635 OP_C_WRITE (a, "apple", 5)
4636 OP_S_BIND_STREAM_ID (a, C_BIDI_ID(0))
4637 OP_S_READ_EXPECT (a, "apple", 5)
4638
4639 OP_SET_INJECT_WORD (1, 0)
4640 OP_S_NEW_TICKET ()
4641 OP_S_WRITE (a, "orange", 6)
4642
4643 OP_C_EXPECT_CONN_CLOSE_INFO(QUIC_ERR_PROTOCOL_VIOLATION, 0, 0)
4644
4645 OP_END
4646};
4647
4648/* 69. Send a TLS KeyUpdate message post-handshake */
4649static const struct script_op script_69[] = {
4650 OP_S_SET_INJECT_HANDSHAKE(script_68_inject_handshake)
4651 OP_C_SET_ALPN ("ossltest")
4652 OP_C_CONNECT_WAIT ()
4653 OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_NONE)
4654
4655 OP_C_NEW_STREAM_BIDI (a, C_BIDI_ID(0))
4656 OP_C_WRITE (a, "apple", 5)
4657 OP_S_BIND_STREAM_ID (a, C_BIDI_ID(0))
4658 OP_S_READ_EXPECT (a, "apple", 5)
4659
4660 OP_SET_INJECT_WORD (2, 0)
4661 OP_S_NEW_TICKET ()
4662 OP_S_WRITE (a, "orange", 6)
4663
4664 OP_C_EXPECT_CONN_CLOSE_INFO(QUIC_ERR_CRYPTO_ERR_BEGIN
4665 + SSL_AD_UNEXPECTED_MESSAGE, 0, 0)
4666
4667 OP_END
4668};
4669
5881dd2c 4670static int set_max_early_data(struct helper *h, struct helper_local *hl)
644ef0bb
MC
4671{
4672
5881dd2c
HL
4673 if (!TEST_true(ossl_quic_tserver_set_max_early_data(ACQUIRE_S(),
4674 (uint32_t)hl->check_op->arg2)))
644ef0bb
MC
4675 return 0;
4676
4677 return 1;
4678}
4679
4680/* 70. Send a TLS NewSessionTicket message with invalid max_early_data */
4681static const struct script_op script_70[] = {
4682 OP_C_SET_ALPN ("ossltest")
4683 OP_C_CONNECT_WAIT ()
4684 OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_NONE)
4685
4686 OP_C_NEW_STREAM_BIDI (a, C_BIDI_ID(0))
4687 OP_C_WRITE (a, "apple", 5)
4688 OP_S_BIND_STREAM_ID (a, C_BIDI_ID(0))
4689 OP_S_READ_EXPECT (a, "apple", 5)
4690
4691 OP_CHECK (set_max_early_data, 0xfffffffe)
4692 OP_S_NEW_TICKET ()
4693 OP_S_WRITE (a, "orange", 6)
4694
4695 OP_C_EXPECT_CONN_CLOSE_INFO(QUIC_ERR_PROTOCOL_VIOLATION, 0, 0)
4696
4697 OP_END
4698};
4699
4700/* 71. Send a TLS NewSessionTicket message with valid max_early_data */
4701static const struct script_op script_71[] = {
4702 OP_C_SET_ALPN ("ossltest")
4703 OP_C_CONNECT_WAIT ()
4704 OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_NONE)
4705
4706 OP_C_NEW_STREAM_BIDI (a, C_BIDI_ID(0))
4707 OP_C_WRITE (a, "apple", 5)
4708 OP_S_BIND_STREAM_ID (a, C_BIDI_ID(0))
4709 OP_S_READ_EXPECT (a, "apple", 5)
4710
4711 OP_CHECK (set_max_early_data, 0xffffffff)
4712 OP_S_NEW_TICKET ()
4713 OP_S_WRITE (a, "orange", 6)
4714 OP_C_READ_EXPECT (a, "orange", 6)
4715
4716 OP_END
4717};
4718
14551f1e 4719/* 72. Test that APL stops handing out streams after limit reached (bidi) */
5881dd2c 4720static int script_72_check(struct helper *h, struct helper_local *hl)
14551f1e
HL
4721{
4722 if (!TEST_uint64_t_ge(h->fail_count, 50))
4723 return 0;
4724
4725 return 1;
4726}
4727
4728static const struct script_op script_72[] = {
4729 OP_C_SET_ALPN ("ossltest")
4730 OP_C_CONNECT_WAIT ()
4731 OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_NONE)
4732
4733 /*
4734 * Request more streams than a server will initially hand out and test that
4735 * they fail properly.
4736 */
4737 OP_BEGIN_REPEAT (200)
4738
5881dd2c 4739 OP_C_NEW_STREAM_BIDI_EX (a, ANY_ID, ALLOW_FAIL | SSL_STREAM_FLAG_NO_BLOCK)
14551f1e
HL
4740 OP_C_SKIP_IF_UNBOUND (a, 2)
4741 OP_C_WRITE (a, "apple", 5)
4742 OP_C_FREE_STREAM (a)
4743
4744 OP_END_REPEAT ()
4745
4746 OP_CHECK (script_72_check, 0)
4747
4748 OP_END
4749};
4750
4751/* 73. Test that APL stops handing out streams after limit reached (uni) */
4752static const struct script_op script_73[] = {
4753 OP_C_SET_ALPN ("ossltest")
4754 OP_C_CONNECT_WAIT ()
4755 OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_NONE)
4756
4757 /*
4758 * Request more streams than a server will initially hand out and test that
4759 * they fail properly.
4760 */
4761 OP_BEGIN_REPEAT (200)
4762
5881dd2c 4763 OP_C_NEW_STREAM_UNI_EX (a, ANY_ID, ALLOW_FAIL | SSL_STREAM_FLAG_NO_BLOCK)
14551f1e
HL
4764 OP_C_SKIP_IF_UNBOUND (a, 2)
4765 OP_C_WRITE (a, "apple", 5)
4766 OP_C_FREE_STREAM (a)
4767
4768 OP_END_REPEAT ()
4769
4770 OP_CHECK (script_72_check, 0)
4771
4772 OP_END
4773};
4774
69169cd9
HL
4775/* 74. Version negotiation: QUIC_VERSION_1 ignored */
4776static int generate_version_neg(WPACKET *wpkt, uint32_t version)
4777{
4778 QUIC_PKT_HDR hdr = {0};
4779
4780 hdr.type = QUIC_PKT_TYPE_VERSION_NEG;
4781 hdr.fixed = 1;
4782 hdr.dst_conn_id.id_len = 0;
4783 hdr.src_conn_id.id_len = 8;
4784 memset(hdr.src_conn_id.id, 0x55, 8);
4785
4786 if (!TEST_true(ossl_quic_wire_encode_pkt_hdr(wpkt, 0, &hdr, NULL)))
4787 return 0;
4788
4789 if (!TEST_true(WPACKET_put_bytes_u32(wpkt, version)))
4790 return 0;
4791
4792 return 1;
4793}
4794
4795static int server_gen_version_neg(struct helper *h, BIO_MSG *msg, size_t stride)
4796{
4797 int rc = 0, have_wpkt = 0;
4798 size_t l;
4799 WPACKET wpkt;
4800 BUF_MEM *buf = NULL;
4801 uint32_t version;
4802
4803 switch (h->inject_word0) {
4804 case 0:
4805 return 1;
4806 case 1:
4807 version = QUIC_VERSION_1;
4808 break;
4809 default:
4810 version = 0x5432abcd;
4811 break;
4812 }
4813
4814 if (!TEST_ptr(buf = BUF_MEM_new()))
4815 goto err;
4816
4817 if (!TEST_true(WPACKET_init(&wpkt, buf)))
4818 goto err;
4819
4820 have_wpkt = 1;
4821
4822 generate_version_neg(&wpkt, version);
4823
4824 if (!TEST_true(WPACKET_get_total_written(&wpkt, &l)))
4825 goto err;
4826
4827 if (!TEST_true(qtest_fault_resize_datagram(h->qtf, l)))
4828 return 0;
4829
4830 memcpy(msg->data, buf->data, l);
4831 h->inject_word0 = 0;
4832
4833 rc = 1;
4834err:
4835 if (have_wpkt)
4836 WPACKET_finish(&wpkt);
4837
4838 BUF_MEM_free(buf);
4839 return rc;
4840}
4841
4842static const struct script_op script_74[] = {
4843 OP_S_SET_INJECT_DATAGRAM (server_gen_version_neg)
4844 OP_SET_INJECT_WORD (1, 0)
4845
4846 OP_C_SET_ALPN ("ossltest")
4847 OP_C_CONNECT_WAIT ()
4848
4849 OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_NONE)
4850
4851 OP_C_NEW_STREAM_BIDI (a, C_BIDI_ID(0))
4852 OP_C_WRITE (a, "apple", 5)
4853 OP_S_BIND_STREAM_ID (a, C_BIDI_ID(0))
4854 OP_S_READ_EXPECT (a, "apple", 5)
4855
4856 OP_END
4857};
4858
4859/* 75. Version negotiation: Unknown version causes connection abort */
4860static const struct script_op script_75[] = {
4861 OP_S_SET_INJECT_DATAGRAM (server_gen_version_neg)
4862 OP_SET_INJECT_WORD (2, 0)
4863
4864 OP_C_SET_ALPN ("ossltest")
4865 OP_C_CONNECT_WAIT_OR_FAIL()
4866
4867 OP_C_EXPECT_CONN_CLOSE_INFO(QUIC_ERR_CONNECTION_REFUSED,0,0)
4868
4869 OP_END
4870};
4871
5881dd2c
HL
4872/* 76. Test peer-initiated shutdown wait */
4873static int script_76_check(struct helper *h, struct helper_local *hl)
3bc38ba0 4874{
5881dd2c
HL
4875 if (!TEST_false(SSL_shutdown_ex(h->c_conn,
4876 SSL_SHUTDOWN_FLAG_WAIT_PEER
4877 | SSL_SHUTDOWN_FLAG_NO_BLOCK,
3bc38ba0
HL
4878 NULL, 0)))
4879 return 0;
4880
4881 return 1;
4882}
4883
4884static const struct script_op script_76[] = {
4885 OP_C_SET_ALPN ("ossltest")
4886 OP_C_CONNECT_WAIT ()
4887 OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_NONE)
4888
4889 OP_C_NEW_STREAM_BIDI (a, C_BIDI_ID(0))
4890 OP_C_WRITE (a, "apple", 5)
4891
4892 OP_S_BIND_STREAM_ID (a, C_BIDI_ID(0))
4893 OP_S_READ_EXPECT (a, "apple", 5)
4894
4895 /* Check a WAIT_PEER call doesn't succeed yet. */
4896 OP_CHECK (script_76_check, 0)
4897 OP_S_SHUTDOWN (42)
4898
4899 OP_C_SHUTDOWN_WAIT (NULL, SSL_SHUTDOWN_FLAG_WAIT_PEER)
4900 OP_C_EXPECT_CONN_CLOSE_INFO(42, 1, 1)
4901
4902 OP_END
4903};
4904
ed835673
HL
4905static const struct script_op *const scripts[] = {
4906 script_1,
4907 script_2,
4908 script_3,
4909 script_4,
4910 script_5,
4911 script_6,
4912 script_7,
4913 script_8,
4914 script_9,
4915 script_10,
274bb489
HL
4916 script_11,
4917 script_12,
fca44cfc
HL
4918 script_13,
4919 script_14,
0554f723
HL
4920 script_15,
4921 script_16,
693b23e3
HL
4922 script_17,
4923 script_18,
2525109f 4924 script_19,
0345cac6 4925 script_20,
e26dc8e3
HL
4926 script_21,
4927 script_22,
4928 script_23,
4929 script_24,
4930 script_25,
4931 script_26,
4932 script_27,
4933 script_28,
4934 script_29,
4935 script_30,
4936 script_31,
4937 script_32,
4938 script_33,
4939 script_34,
4940 script_35,
4941 script_36,
4942 script_37,
4943 script_38,
8aa6a436 4944 script_39,
0786483a 4945 script_40,
7eb330ff 4946 script_41,
27c2f62f 4947 script_42,
ab6c6345 4948 script_43,
97684a15 4949 script_44,
17340e87 4950 script_45,
ed0d6ba4
HL
4951 script_46,
4952 script_47,
4953 script_48,
4954 script_49,
4955 script_50,
a1aff2c6 4956 script_51,
477944b6 4957 script_52,
de56eebd
HL
4958 script_53,
4959 script_54,
ed75eb32
HL
4960 script_55,
4961 script_56,
4962 script_57,
d56b81ac
HL
4963 script_58,
4964 script_59,
d49a1634 4965 script_60,
d63b8cbb
HL
4966 script_61,
4967 script_62,
1623bf37
HL
4968 script_63,
4969 script_64,
4970 script_65,
4971 script_66,
4972 script_67,
644ef0bb
MC
4973 script_68,
4974 script_69,
4975 script_70,
14551f1e
HL
4976 script_71,
4977 script_72,
69169cd9
HL
4978 script_73,
4979 script_74,
3bc38ba0
HL
4980 script_75,
4981 script_76
ed835673
HL
4982};
4983
4984static int test_script(int idx)
4985{
5881dd2c 4986 int script_idx, free_order, blocking;
0786483a
HL
4987 char script_name[64];
4988
5881dd2c
HL
4989 free_order = idx % 2;
4990 idx /= 2;
4991
4992 blocking = idx % 2;
4993 idx /= 2;
4994
4995 script_idx = idx;
4996
4997 if (blocking && free_order)
4998 return 1; /* don't need to test free_order twice */
4999
5000#if !defined(OPENSSL_THREADS)
5001 if (blocking) {
5002 TEST_skip("cannot test in blocking mode without threads");
5003 return 1;
5004 }
5005#endif
5006
cd5e4380 5007 snprintf(script_name, sizeof(script_name), "script %d", script_idx + 1);
97f30fd5 5008
5881dd2c
HL
5009 TEST_info("Running script %d (order=%d, blocking=%d)", script_idx + 1,
5010 free_order, blocking);
5011 return run_script(scripts[script_idx], script_name, free_order, blocking);
ed835673
HL
5012}
5013
e26dc8e3
HL
5014/* Dynamically generated tests. */
5015static struct script_op dyn_frame_types_script[] = {
8aa6a436 5016 OP_S_SET_INJECT_PLAIN (script_21_inject_plain)
e26dc8e3
HL
5017 OP_SET_INJECT_WORD (0, 0) /* dynamic */
5018
5019 OP_C_SET_ALPN ("ossltest")
5020 OP_C_CONNECT_WAIT_OR_FAIL()
5021
5022 OP_C_EXPECT_CONN_CLOSE_INFO(QUIC_ERR_FRAME_ENCODING_ERROR,0,0)
5023
5024 OP_END
5025};
5026
5027struct forbidden_frame_type {
5028 uint64_t pkt_type, frame_type, expected_err;
5029};
5030
5031static const struct forbidden_frame_type forbidden_frame_types[] = {
5032 { QUIC_PKT_TYPE_INITIAL, OSSL_QUIC_VLINT_MAX, QUIC_ERR_FRAME_ENCODING_ERROR },
5033 { QUIC_PKT_TYPE_HANDSHAKE, OSSL_QUIC_VLINT_MAX, QUIC_ERR_FRAME_ENCODING_ERROR },
5034 { QUIC_PKT_TYPE_1RTT, OSSL_QUIC_VLINT_MAX, QUIC_ERR_FRAME_ENCODING_ERROR },
5035
5036 { QUIC_PKT_TYPE_INITIAL, OSSL_QUIC_FRAME_TYPE_STREAM, QUIC_ERR_PROTOCOL_VIOLATION },
5037 { QUIC_PKT_TYPE_INITIAL, OSSL_QUIC_FRAME_TYPE_RESET_STREAM, QUIC_ERR_PROTOCOL_VIOLATION },
5038 { QUIC_PKT_TYPE_INITIAL, OSSL_QUIC_FRAME_TYPE_STOP_SENDING, QUIC_ERR_PROTOCOL_VIOLATION },
5039 { QUIC_PKT_TYPE_INITIAL, OSSL_QUIC_FRAME_TYPE_NEW_TOKEN, QUIC_ERR_PROTOCOL_VIOLATION },
5040 { QUIC_PKT_TYPE_INITIAL, OSSL_QUIC_FRAME_TYPE_MAX_DATA, QUIC_ERR_PROTOCOL_VIOLATION },
5041 { QUIC_PKT_TYPE_INITIAL, OSSL_QUIC_FRAME_TYPE_MAX_STREAM_DATA, QUIC_ERR_PROTOCOL_VIOLATION },
5042 { QUIC_PKT_TYPE_INITIAL, OSSL_QUIC_FRAME_TYPE_MAX_STREAMS_BIDI, QUIC_ERR_PROTOCOL_VIOLATION },
5043 { QUIC_PKT_TYPE_INITIAL, OSSL_QUIC_FRAME_TYPE_MAX_STREAMS_UNI, QUIC_ERR_PROTOCOL_VIOLATION },
5044 { QUIC_PKT_TYPE_INITIAL, OSSL_QUIC_FRAME_TYPE_DATA_BLOCKED, QUIC_ERR_PROTOCOL_VIOLATION },
5045 { QUIC_PKT_TYPE_INITIAL, OSSL_QUIC_FRAME_TYPE_STREAM_DATA_BLOCKED, QUIC_ERR_PROTOCOL_VIOLATION },
5046 { QUIC_PKT_TYPE_INITIAL, OSSL_QUIC_FRAME_TYPE_STREAMS_BLOCKED_BIDI, QUIC_ERR_PROTOCOL_VIOLATION },
5047 { QUIC_PKT_TYPE_INITIAL, OSSL_QUIC_FRAME_TYPE_STREAMS_BLOCKED_UNI, QUIC_ERR_PROTOCOL_VIOLATION },
5048 { QUIC_PKT_TYPE_INITIAL, OSSL_QUIC_FRAME_TYPE_NEW_CONN_ID, QUIC_ERR_PROTOCOL_VIOLATION },
5049 { QUIC_PKT_TYPE_INITIAL, OSSL_QUIC_FRAME_TYPE_RETIRE_CONN_ID, QUIC_ERR_PROTOCOL_VIOLATION },
5050 { QUIC_PKT_TYPE_INITIAL, OSSL_QUIC_FRAME_TYPE_PATH_CHALLENGE, QUIC_ERR_PROTOCOL_VIOLATION },
5051 { QUIC_PKT_TYPE_INITIAL, OSSL_QUIC_FRAME_TYPE_PATH_RESPONSE, QUIC_ERR_PROTOCOL_VIOLATION },
5052 { QUIC_PKT_TYPE_INITIAL, OSSL_QUIC_FRAME_TYPE_CONN_CLOSE_APP, QUIC_ERR_PROTOCOL_VIOLATION },
5053 { QUIC_PKT_TYPE_INITIAL, OSSL_QUIC_FRAME_TYPE_HANDSHAKE_DONE, QUIC_ERR_PROTOCOL_VIOLATION },
5054
5055 { QUIC_PKT_TYPE_HANDSHAKE, OSSL_QUIC_FRAME_TYPE_STREAM, QUIC_ERR_PROTOCOL_VIOLATION },
5056 { QUIC_PKT_TYPE_HANDSHAKE, OSSL_QUIC_FRAME_TYPE_RESET_STREAM, QUIC_ERR_PROTOCOL_VIOLATION },
5057 { QUIC_PKT_TYPE_HANDSHAKE, OSSL_QUIC_FRAME_TYPE_STOP_SENDING, QUIC_ERR_PROTOCOL_VIOLATION },
5058 { QUIC_PKT_TYPE_HANDSHAKE, OSSL_QUIC_FRAME_TYPE_NEW_TOKEN, QUIC_ERR_PROTOCOL_VIOLATION },
5059 { QUIC_PKT_TYPE_HANDSHAKE, OSSL_QUIC_FRAME_TYPE_MAX_DATA, QUIC_ERR_PROTOCOL_VIOLATION },
5060 { QUIC_PKT_TYPE_HANDSHAKE, OSSL_QUIC_FRAME_TYPE_MAX_STREAM_DATA, QUIC_ERR_PROTOCOL_VIOLATION },
5061 { QUIC_PKT_TYPE_HANDSHAKE, OSSL_QUIC_FRAME_TYPE_MAX_STREAMS_BIDI, QUIC_ERR_PROTOCOL_VIOLATION },
5062 { QUIC_PKT_TYPE_HANDSHAKE, OSSL_QUIC_FRAME_TYPE_MAX_STREAMS_UNI, QUIC_ERR_PROTOCOL_VIOLATION },
5063 { QUIC_PKT_TYPE_HANDSHAKE, OSSL_QUIC_FRAME_TYPE_DATA_BLOCKED, QUIC_ERR_PROTOCOL_VIOLATION },
5064 { QUIC_PKT_TYPE_HANDSHAKE, OSSL_QUIC_FRAME_TYPE_STREAM_DATA_BLOCKED, QUIC_ERR_PROTOCOL_VIOLATION },
5065 { QUIC_PKT_TYPE_HANDSHAKE, OSSL_QUIC_FRAME_TYPE_STREAMS_BLOCKED_BIDI, QUIC_ERR_PROTOCOL_VIOLATION },
5066 { QUIC_PKT_TYPE_HANDSHAKE, OSSL_QUIC_FRAME_TYPE_STREAMS_BLOCKED_UNI, QUIC_ERR_PROTOCOL_VIOLATION },
5067 { QUIC_PKT_TYPE_HANDSHAKE, OSSL_QUIC_FRAME_TYPE_NEW_CONN_ID, QUIC_ERR_PROTOCOL_VIOLATION },
5068 { QUIC_PKT_TYPE_HANDSHAKE, OSSL_QUIC_FRAME_TYPE_RETIRE_CONN_ID, QUIC_ERR_PROTOCOL_VIOLATION },
5069 { QUIC_PKT_TYPE_HANDSHAKE, OSSL_QUIC_FRAME_TYPE_PATH_CHALLENGE, QUIC_ERR_PROTOCOL_VIOLATION },
5070 { QUIC_PKT_TYPE_HANDSHAKE, OSSL_QUIC_FRAME_TYPE_PATH_RESPONSE, QUIC_ERR_PROTOCOL_VIOLATION },
5071 { QUIC_PKT_TYPE_HANDSHAKE, OSSL_QUIC_FRAME_TYPE_CONN_CLOSE_APP, QUIC_ERR_PROTOCOL_VIOLATION },
5072 { QUIC_PKT_TYPE_HANDSHAKE, OSSL_QUIC_FRAME_TYPE_HANDSHAKE_DONE, QUIC_ERR_PROTOCOL_VIOLATION },
5073
5074 /* Client uses a zero-length CID so this is not allowed. */
5075 { QUIC_PKT_TYPE_1RTT, OSSL_QUIC_FRAME_TYPE_RETIRE_CONN_ID, QUIC_ERR_PROTOCOL_VIOLATION },
5076};
5077
5078static ossl_unused int test_dyn_frame_types(int idx)
5079{
5080 size_t i;
0786483a 5081 char script_name[64];
e26dc8e3
HL
5082 struct script_op *s = dyn_frame_types_script;
5083
5084 for (i = 0; i < OSSL_NELEM(dyn_frame_types_script); ++i)
5085 if (s[i].op == OPK_SET_INJECT_WORD) {
49a38dee 5086 s[i].arg1 = (size_t)forbidden_frame_types[idx].pkt_type;
e26dc8e3
HL
5087 s[i].arg2 = forbidden_frame_types[idx].frame_type;
5088 } else if (s[i].op == OPK_C_EXPECT_CONN_CLOSE_INFO) {
5089 s[i].arg2 = forbidden_frame_types[idx].expected_err;
5090 }
5091
0786483a
HL
5092 snprintf(script_name, sizeof(script_name),
5093 "dyn script %d", idx);
5094
5881dd2c 5095 return run_script(dyn_frame_types_script, script_name, 0, 0);
e26dc8e3
HL
5096}
5097
ed835673
HL
5098OPT_TEST_DECLARE_USAGE("certfile privkeyfile\n")
5099
5100int setup_tests(void)
5101{
5102 if (!test_skip_common_options()) {
5103 TEST_error("Error parsing test options\n");
5104 return 0;
5105 }
5106
5107 if (!TEST_ptr(certfile = test_get_argument(0))
5108 || !TEST_ptr(keyfile = test_get_argument(1)))
5109 return 0;
5110
e26dc8e3 5111 ADD_ALL_TESTS(test_dyn_frame_types, OSSL_NELEM(forbidden_frame_types));
5881dd2c 5112 ADD_ALL_TESTS(test_script, OSSL_NELEM(scripts) * 2 * 2);
ed835673
HL
5113 return 1;
5114}