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