quic: process stateless resets
[openssl.git] / test / quic_txp_test.c
1 /*
2  * Copyright 2022 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 "internal/packet.h"
10 #include "internal/quic_txp.h"
11 #include "internal/quic_statm.h"
12 #include "internal/quic_demux.h"
13 #include "internal/quic_record_rx.h"
14 #include "testutil.h"
15 #include "quic_record_test_util.h"
16
17 static const QUIC_CONN_ID scid_1 = {
18     1, { 0x5f }
19 };
20
21 static const QUIC_CONN_ID dcid_1 = {
22     8, { 0xa1, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7, 0xa8 }
23 };
24
25 static const QUIC_CONN_ID cid_1 = {
26     8, { 0xb1, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7, 0xb8 }
27 };
28
29 static const unsigned char reset_token_1[16] = {
30     0x99, 0x88, 0x77, 0x66, 0x55, 0x44, 0x33, 0x22, 0x11,
31     0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff, 0x12,
32 };
33
34 static const unsigned char secret_1[32] = {
35     0x01
36 };
37
38 static OSSL_TIME fake_now(void *arg)
39 {
40     return ossl_time_now(); /* TODO */
41 }
42
43 struct helper {
44     OSSL_QUIC_TX_PACKETISER         *txp;
45     OSSL_QUIC_TX_PACKETISER_ARGS    args;
46     OSSL_QTX_ARGS                   qtx_args;
47     BIO                             *bio1, *bio2;
48     QUIC_TXFC                       conn_txfc;
49     QUIC_RXFC                       conn_rxfc, stream_rxfc;
50     QUIC_RXFC                       max_streams_bidi_rxfc, max_streams_uni_rxfc;
51     OSSL_STATM                      statm;
52     OSSL_CC_DATA                    *cc_data;
53     const OSSL_CC_METHOD            *cc_method;
54     QUIC_STREAM_MAP                 qsm;
55     char                            have_statm, have_qsm;
56     QUIC_DEMUX                      *demux;
57     OSSL_QRX                        *qrx;
58     OSSL_QRX_ARGS                   qrx_args;
59     OSSL_QRX_PKT                    *qrx_pkt;
60     PACKET                          pkt;
61     uint64_t                        frame_type;
62     union {
63         uint64_t                        max_data;
64         OSSL_QUIC_FRAME_NEW_CONN_ID     new_conn_id;
65         OSSL_QUIC_FRAME_ACK             ack;
66         struct {
67             const unsigned char *token;
68             size_t              token_len;
69         } new_token;
70         OSSL_QUIC_FRAME_CRYPTO          crypto;
71         OSSL_QUIC_FRAME_STREAM          stream;
72         OSSL_QUIC_FRAME_STOP_SENDING    stop_sending;
73         OSSL_QUIC_FRAME_RESET_STREAM    reset_stream;
74         OSSL_QUIC_FRAME_CONN_CLOSE      conn_close;
75     } frame;
76     OSSL_QUIC_ACK_RANGE     ack_ranges[16];
77 };
78
79 static void helper_cleanup(struct helper *h)
80 {
81     size_t i;
82     uint32_t pn_space;
83
84     ossl_qrx_pkt_release(h->qrx_pkt);
85     h->qrx_pkt = NULL;
86
87     for (pn_space = QUIC_PN_SPACE_INITIAL;
88          pn_space < QUIC_PN_SPACE_NUM;
89          ++pn_space)
90         ossl_ackm_on_pkt_space_discarded(h->args.ackm, pn_space);
91
92     ossl_quic_tx_packetiser_free(h->txp);
93     ossl_qtx_free(h->args.qtx);
94     ossl_quic_txpim_free(h->args.txpim);
95     ossl_quic_cfq_free(h->args.cfq);
96     if (h->cc_data != NULL)
97         h->cc_method->free(h->cc_data);
98     if (h->have_statm)
99         ossl_statm_destroy(&h->statm);
100     if (h->have_qsm)
101         ossl_quic_stream_map_cleanup(&h->qsm);
102     for (i = 0; i < QUIC_PN_SPACE_NUM; ++i)
103         ossl_quic_sstream_free(h->args.crypto[i]);
104     ossl_ackm_free(h->args.ackm);
105     ossl_qrx_free(h->qrx);
106     ossl_quic_demux_free(h->demux);
107     BIO_free(h->bio1);
108     BIO_free(h->bio2);
109 }
110
111 static int helper_init(struct helper *h)
112 {
113     int rc = 0;
114     size_t i;
115
116     memset(h, 0, sizeof(*h));
117
118     /* Initialisation */
119     if (!TEST_true(BIO_new_bio_dgram_pair(&h->bio1, 0, &h->bio2, 0)))
120         goto err;
121
122     h->qtx_args.bio    = h->bio1;
123     h->qtx_args.mdpl   = 1200;
124
125     if (!TEST_ptr(h->args.qtx = ossl_qtx_new(&h->qtx_args)))
126         goto err;
127
128     if (!TEST_ptr(h->args.txpim = ossl_quic_txpim_new()))
129         goto err;
130
131     if (!TEST_ptr(h->args.cfq = ossl_quic_cfq_new()))
132         goto err;
133
134     if (!TEST_true(ossl_quic_txfc_init(&h->conn_txfc, NULL)))
135         goto err;
136
137     if (!TEST_true(ossl_quic_rxfc_init(&h->conn_rxfc, NULL,
138                                        2 * 1024 * 1024,
139                                        10 * 1024 * 1024,
140                                        fake_now,
141                                        NULL)))
142         goto err;
143
144     if (!TEST_true(ossl_quic_rxfc_init(&h->stream_rxfc, &h->conn_rxfc,
145                                        1 * 1024 * 1024,
146                                        5 * 1024 * 1024,
147                                        fake_now,
148                                        NULL)))
149         goto err;
150
151     if (!TEST_true(ossl_quic_rxfc_init(&h->max_streams_bidi_rxfc, NULL,
152                                        100, 100,
153                                        fake_now,
154                                        NULL)))
155         goto err;
156
157     if (!TEST_true(ossl_quic_rxfc_init(&h->max_streams_uni_rxfc, NULL,
158                                        100, 100,
159                                        fake_now,
160                                        NULL)))
161
162     if (!TEST_true(ossl_statm_init(&h->statm)))
163         goto err;
164
165     h->have_statm = 1;
166
167     h->cc_method = &ossl_cc_dummy_method;
168     if (!TEST_ptr(h->cc_data = h->cc_method->new(fake_now, NULL)))
169         goto err;
170
171     if (!TEST_ptr(h->args.ackm = ossl_ackm_new(fake_now, NULL,
172                                                &h->statm,
173                                                h->cc_method,
174                                                h->cc_data)))
175         goto err;
176
177     if (!TEST_true(ossl_quic_stream_map_init(&h->qsm, NULL, NULL,
178                                              &h->max_streams_bidi_rxfc,
179                                              &h->max_streams_uni_rxfc,
180                                              /*is_server=*/0)))
181         goto err;
182
183     h->have_qsm = 1;
184
185     for (i = 0; i < QUIC_PN_SPACE_NUM; ++i)
186         if (!TEST_ptr(h->args.crypto[i] = ossl_quic_sstream_new(4096)))
187             goto err;
188
189     h->args.cur_scid                = scid_1;
190     h->args.cur_dcid                = dcid_1;
191     h->args.qsm                     = &h->qsm;
192     h->args.conn_txfc               = &h->conn_txfc;
193     h->args.conn_rxfc               = &h->conn_rxfc;
194     h->args.max_streams_bidi_rxfc   = &h->max_streams_bidi_rxfc;
195     h->args.max_streams_uni_rxfc    = &h->max_streams_uni_rxfc;
196     h->args.cc_method               = h->cc_method;
197     h->args.cc_data                 = h->cc_data;
198     h->args.now                     = fake_now;
199
200     if (!TEST_ptr(h->txp = ossl_quic_tx_packetiser_new(&h->args)))
201         goto err;
202
203     if (!TEST_ptr(h->demux = ossl_quic_demux_new(h->bio2, 8,
204                                                  fake_now, NULL)))
205         goto err;
206
207     h->qrx_args.demux                  = h->demux;
208     h->qrx_args.short_conn_id_len      = 8;
209     h->qrx_args.max_deferred           = 32;
210
211     if (!TEST_ptr(h->qrx = ossl_qrx_new(&h->qrx_args)))
212         goto err;
213
214     if (!TEST_true(ossl_qrx_add_dst_conn_id(h->qrx, &dcid_1)))
215         goto err;
216
217     ossl_qrx_allow_1rtt_processing(h->qrx);
218
219     rc = 1;
220 err:
221     if (!rc)
222         helper_cleanup(h);
223
224     return rc;
225 }
226
227 #define OPK_END                     0   /* End of Script */
228 #define OPK_TXP_GENERATE            1   /* Call generate, expect packet output */
229 #define OPK_TXP_GENERATE_NONE       2   /* Call generate, expect no packet output */
230 #define OPK_RX_PKT                  3   /* Receive, expect packet */
231 #define OPK_RX_PKT_NONE             4   /* Receive, expect no packet */
232 #define OPK_EXPECT_DGRAM_LEN        5   /* Expect received datagram length in range */
233 #define OPK_EXPECT_FRAME            6   /* Expect next frame is of type */
234 #define OPK_EXPECT_INITIAL_TOKEN    7   /* Expect initial token buffer match */
235 #define OPK_EXPECT_HDR              8   /* Expect header structure match */
236 #define OPK_CHECK                   9   /* Call check function */
237 #define OPK_NEXT_FRAME              10  /* Next frame */
238 #define OPK_EXPECT_NO_FRAME         11  /* Expect no further frames */
239 #define OPK_PROVIDE_SECRET          12  /* Provide secret to QTX and QRX */
240 #define OPK_DISCARD_EL              13  /* Discard QTX EL */
241 #define OPK_CRYPTO_SEND             14  /* Push data into crypto send stream */
242 #define OPK_STREAM_NEW              15  /* Create new application stream */
243 #define OPK_STREAM_SEND             16  /* Push data into application send stream */
244 #define OPK_STREAM_FIN              17  /* Mark stream as finished */
245 #define OPK_STOP_SENDING            18  /* Mark stream for STOP_SENDING */
246 #define OPK_RESET_STREAM            19  /* Mark stream for RESET_STREAM */
247 #define OPK_CONN_TXFC_BUMP          20  /* Bump connection TXFC CWM */
248 #define OPK_STREAM_TXFC_BUMP        21  /* Bump stream TXFC CWM */
249 #define OPK_HANDSHAKE_COMPLETE      22  /* Mark handshake as complete */
250 #define OPK_NOP                     23  /* No-op */
251
252 struct script_op {
253     uint32_t opcode;
254     uint64_t arg0, arg1;
255     const void *buf;
256     size_t buf_len;
257     int (*check_func)(struct helper *h);
258 };
259
260 #define OP_END      \
261     { OPK_END }
262 #define OP_TXP_GENERATE() \
263     { OPK_TXP_GENERATE },
264 #define OP_TXP_GENERATE_NONE() \
265     { OPK_TXP_GENERATE_NONE },
266 #define OP_RX_PKT() \
267     { OPK_RX_PKT },
268 #define OP_RX_PKT_NONE() \
269     { OPK_RX_PKT_NONE },
270 #define OP_EXPECT_DGRAM_LEN(lo, hi) \
271     { OPK_EXPECT_DGRAM_LEN, (lo), (hi) },
272 #define OP_EXPECT_FRAME(frame_type) \
273     { OPK_EXPECT_FRAME, (frame_type) },
274 #define OP_EXPECT_INITIAL_TOKEN(buf) \
275     { OPK_EXPECT_INITIAL_TOKEN, sizeof(buf), 0, buf },
276 #define OP_EXPECT_HDR(hdr) \
277     { OPK_EXPECT_HDR, 0, 0, &(hdr) },
278 #define OP_CHECK(func) \
279     { OPK_CHECK, 0, 0, NULL, 0, (func) },
280 #define OP_NEXT_FRAME() \
281     { OPK_NEXT_FRAME },
282 #define OP_EXPECT_NO_FRAME() \
283     { OPK_EXPECT_NO_FRAME },
284 #define OP_PROVIDE_SECRET(el, suite, secret) \
285     { OPK_PROVIDE_SECRET, (el), (suite), (secret), sizeof(secret) },
286 #define OP_DISCARD_EL(el) \
287     { OPK_DISCARD_EL, (el) },
288 #define OP_CRYPTO_SEND(pn_space, buf) \
289     { OPK_CRYPTO_SEND, (pn_space), 0, (buf), sizeof(buf) },
290 #define OP_STREAM_NEW(id) \
291     { OPK_STREAM_NEW, (id) },
292 #define OP_STREAM_SEND(id, buf) \
293     { OPK_STREAM_SEND, (id), 0, (buf), sizeof(buf) },
294 #define OP_STREAM_FIN(id) \
295     { OPK_STREAM_FIN, (id) },
296 #define OP_STOP_SENDING(id, aec) \
297     { OPK_STOP_SENDING, (id), (aec) },
298 #define OP_RESET_STREAM(id, aec) \
299     { OPK_RESET_STREAM, (id), (aec) },
300 #define OP_CONN_TXFC_BUMP(cwm) \
301     { OPK_CONN_TXFC_BUMP, (cwm) },
302 #define OP_STREAM_TXFC_BUMP(id, cwm) \
303     { OPK_STREAM_TXFC_BUMP, (cwm), (id) },
304 #define OP_HANDSHAKE_COMPLETE() \
305     { OPK_HANDSHAKE_COMPLETE },
306 #define OP_NOP() \
307     { OPK_NOP },
308
309 static int schedule_handshake_done(struct helper *h)
310 {
311     ossl_quic_tx_packetiser_schedule_handshake_done(h->txp);
312     return 1;
313 }
314
315 static int schedule_ack_eliciting_app(struct helper *h)
316 {
317     ossl_quic_tx_packetiser_schedule_ack_eliciting(h->txp, QUIC_PN_SPACE_APP);
318     return 1;
319 }
320
321 /* 1. 1-RTT, Single Handshake Done Frame */
322 static const struct script_op script_1[] = {
323     OP_PROVIDE_SECRET(QUIC_ENC_LEVEL_1RTT, QRL_SUITE_AES128GCM, secret_1)
324     OP_TXP_GENERATE_NONE()
325     OP_CHECK(schedule_handshake_done)
326     OP_TXP_GENERATE()
327     OP_RX_PKT()
328     /* Should not be long */
329     OP_EXPECT_DGRAM_LEN(21, 32)
330     OP_NEXT_FRAME()
331     OP_EXPECT_FRAME(OSSL_QUIC_FRAME_TYPE_HANDSHAKE_DONE)
332     OP_EXPECT_NO_FRAME()
333     OP_RX_PKT_NONE()
334     OP_TXP_GENERATE_NONE()
335     OP_END
336 };
337
338 /* 2. 1-RTT, Forced ACK-Eliciting Frame */
339 static const struct script_op script_2[] = {
340     OP_PROVIDE_SECRET(QUIC_ENC_LEVEL_1RTT, QRL_SUITE_AES128GCM, secret_1)
341     OP_TXP_GENERATE_NONE()
342     OP_CHECK(schedule_ack_eliciting_app)
343     OP_TXP_GENERATE()
344     OP_RX_PKT()
345     /* Should not be long */
346     OP_EXPECT_DGRAM_LEN(21, 32)
347     /* A PING frame should have been added */
348     OP_NEXT_FRAME()
349     OP_EXPECT_FRAME(OSSL_QUIC_FRAME_TYPE_PING)
350     OP_EXPECT_NO_FRAME()
351     OP_RX_PKT_NONE()
352     OP_TXP_GENERATE_NONE()
353     OP_END
354 };
355
356 /* 3. 1-RTT, MAX_DATA */
357 static int schedule_max_data(struct helper *h)
358 {
359     uint64_t cwm;
360
361     cwm = ossl_quic_rxfc_get_cwm(&h->stream_rxfc);
362
363     if (!TEST_true(ossl_quic_rxfc_on_rx_stream_frame(&h->stream_rxfc, cwm, 0))
364         || !TEST_true(ossl_quic_rxfc_on_retire(&h->stream_rxfc, cwm,
365                                                ossl_ticks2time(OSSL_TIME_MS))))
366         return 0;
367
368     return 1;
369 }
370
371 static const struct script_op script_3[] = {
372     OP_PROVIDE_SECRET(QUIC_ENC_LEVEL_1RTT, QRL_SUITE_AES128GCM, secret_1)
373     OP_TXP_GENERATE_NONE()
374     OP_CHECK(schedule_max_data)
375     OP_TXP_GENERATE()
376     OP_RX_PKT()
377     /* Should not be long */
378     OP_EXPECT_DGRAM_LEN(21, 40)
379     /* A PING frame should have been added */
380     OP_NEXT_FRAME()
381     OP_EXPECT_FRAME(OSSL_QUIC_FRAME_TYPE_MAX_DATA)
382     OP_EXPECT_NO_FRAME()
383     OP_RX_PKT_NONE()
384     OP_TXP_GENERATE_NONE()
385     OP_END
386 };
387
388 /* 4. 1-RTT, CFQ (NEW_CONN_ID) */
389 static void free_buf_mem(unsigned char *buf, size_t buf_len, void *arg)
390 {
391     BUF_MEM_free((BUF_MEM *)arg);
392 }
393
394 static int schedule_cfq_new_conn_id(struct helper *h)
395 {
396     int rc = 0;
397     QUIC_CFQ_ITEM *cfq_item;
398     WPACKET wpkt;
399     BUF_MEM *buf_mem = NULL;
400     size_t l = 0;
401     OSSL_QUIC_FRAME_NEW_CONN_ID ncid = {0};
402
403     ncid.seq_num         = 2345;
404     ncid.retire_prior_to = 1234;
405     ncid.conn_id         = cid_1;
406     memcpy(ncid.stateless_reset.token, reset_token_1, sizeof(reset_token_1));
407
408     if (!TEST_ptr(buf_mem = BUF_MEM_new()))
409         goto err;
410
411     if (!TEST_true(WPACKET_init(&wpkt, buf_mem)))
412         goto err;
413
414     if (!TEST_true(ossl_quic_wire_encode_frame_new_conn_id(&wpkt, &ncid))) {
415         WPACKET_cleanup(&wpkt);
416         goto err;
417     }
418
419     WPACKET_finish(&wpkt);
420
421     if (!TEST_true(WPACKET_get_total_written(&wpkt, &l)))
422         goto err;
423
424     if (!TEST_ptr(cfq_item = ossl_quic_cfq_add_frame(h->args.cfq, 1,
425                                                      QUIC_PN_SPACE_APP,
426                                                      OSSL_QUIC_FRAME_TYPE_NEW_CONN_ID, 0,
427                                                      (unsigned char *)buf_mem->data, l,
428                                                      free_buf_mem,
429                                                      buf_mem)))
430         goto err;
431
432     rc = 1;
433 err:
434     if (!rc)
435         BUF_MEM_free(buf_mem);
436     return rc;
437 }
438
439 static int check_cfq_new_conn_id(struct helper *h)
440 {
441     if (!TEST_uint64_t_eq(h->frame.new_conn_id.seq_num, 2345)
442         || !TEST_uint64_t_eq(h->frame.new_conn_id.retire_prior_to, 1234)
443         || !TEST_mem_eq(&h->frame.new_conn_id.conn_id, sizeof(cid_1),
444                         &cid_1, sizeof(cid_1))
445         || !TEST_mem_eq(&h->frame.new_conn_id.stateless_reset.token,
446                         sizeof(reset_token_1),
447                         reset_token_1,
448                         sizeof(reset_token_1)))
449         return 0;
450
451     return 1;
452 }
453
454 static const struct script_op script_4[] = {
455     OP_PROVIDE_SECRET(QUIC_ENC_LEVEL_1RTT, QRL_SUITE_AES128GCM, secret_1)
456     OP_TXP_GENERATE_NONE()
457     OP_CHECK(schedule_cfq_new_conn_id)
458     OP_TXP_GENERATE()
459     OP_RX_PKT()
460     OP_EXPECT_DGRAM_LEN(21, 128)
461     OP_NEXT_FRAME()
462     OP_EXPECT_FRAME(OSSL_QUIC_FRAME_TYPE_NEW_CONN_ID)
463     OP_CHECK(check_cfq_new_conn_id)
464     OP_EXPECT_NO_FRAME()
465     OP_RX_PKT_NONE()
466     OP_TXP_GENERATE_NONE()
467     OP_END
468 };
469
470 /* 5. 1-RTT, CFQ (NEW_TOKEN) */
471 static const unsigned char token_1[] = {
472     0x10, 0x11, 0x12, 0x13, 0x14, 0x15
473 };
474
475 static int schedule_cfq_new_token(struct helper *h)
476 {
477     int rc = 0;
478     QUIC_CFQ_ITEM *cfq_item;
479     WPACKET wpkt;
480     BUF_MEM *buf_mem = NULL;
481     size_t l = 0;
482
483     if (!TEST_ptr(buf_mem = BUF_MEM_new()))
484         goto err;
485
486     if (!TEST_true(WPACKET_init(&wpkt, buf_mem)))
487         goto err;
488
489     if (!TEST_true(ossl_quic_wire_encode_frame_new_token(&wpkt, token_1,
490                                                          sizeof(token_1)))) {
491         WPACKET_cleanup(&wpkt);
492         goto err;
493     }
494
495     WPACKET_finish(&wpkt);
496
497     if (!TEST_true(WPACKET_get_total_written(&wpkt, &l)))
498         goto err;
499
500     if (!TEST_ptr(cfq_item = ossl_quic_cfq_add_frame(h->args.cfq, 1,
501                                                      QUIC_PN_SPACE_APP,
502                                                      OSSL_QUIC_FRAME_TYPE_NEW_TOKEN, 0,
503                                                      (unsigned char *)buf_mem->data, l,
504                                                      free_buf_mem,
505                                                      buf_mem)))
506         goto err;
507
508     rc = 1;
509 err:
510     if (!rc)
511         BUF_MEM_free(buf_mem);
512     return rc;
513 }
514
515 static int check_cfq_new_token(struct helper *h)
516 {
517     if (!TEST_mem_eq(h->frame.new_token.token,
518                      h->frame.new_token.token_len,
519                      token_1,
520                      sizeof(token_1)))
521         return 0;
522
523     return 1;
524 }
525
526 static const struct script_op script_5[] = {
527     OP_PROVIDE_SECRET(QUIC_ENC_LEVEL_1RTT, QRL_SUITE_AES128GCM, secret_1)
528     OP_TXP_GENERATE_NONE()
529     OP_CHECK(schedule_cfq_new_token)
530     OP_TXP_GENERATE()
531     OP_RX_PKT()
532     OP_EXPECT_DGRAM_LEN(21, 512)
533     OP_NEXT_FRAME()
534     OP_EXPECT_FRAME(OSSL_QUIC_FRAME_TYPE_NEW_TOKEN)
535     OP_CHECK(check_cfq_new_token)
536     OP_EXPECT_NO_FRAME()
537     OP_RX_PKT_NONE()
538     OP_TXP_GENERATE_NONE()
539     OP_END
540 };
541
542 /* 6. 1-RTT, ACK */
543 static int schedule_ack(struct helper *h)
544 {
545     size_t i;
546     OSSL_ACKM_RX_PKT rx_pkt = {0};
547
548     /* Stimulate ACK emission by simulating a few received packets. */
549     for (i = 0; i < 5; ++i) {
550         rx_pkt.pkt_num          = i;
551         rx_pkt.time             = fake_now(NULL);
552         rx_pkt.pkt_space        = QUIC_PN_SPACE_APP;
553         rx_pkt.is_ack_eliciting = 1;
554
555         if (!TEST_true(ossl_ackm_on_rx_packet(h->args.ackm, &rx_pkt)))
556             return 0;
557     }
558
559     return 1;
560 }
561
562 static const struct script_op script_6[] = {
563     OP_PROVIDE_SECRET(QUIC_ENC_LEVEL_1RTT, QRL_SUITE_AES128GCM, secret_1)
564     OP_TXP_GENERATE_NONE()
565     OP_CHECK(schedule_ack)
566     OP_TXP_GENERATE()
567     OP_RX_PKT()
568     OP_EXPECT_DGRAM_LEN(21, 512)
569     OP_NEXT_FRAME()
570     OP_EXPECT_FRAME(OSSL_QUIC_FRAME_TYPE_ACK_WITHOUT_ECN)
571     OP_EXPECT_NO_FRAME()
572     OP_RX_PKT_NONE()
573     OP_TXP_GENERATE_NONE()
574     OP_END
575 };
576
577 /* 7. 1-RTT, ACK, NEW_TOKEN */
578 static const struct script_op script_7[] = {
579     OP_PROVIDE_SECRET(QUIC_ENC_LEVEL_1RTT, QRL_SUITE_AES128GCM, secret_1)
580     OP_TXP_GENERATE_NONE()
581     OP_CHECK(schedule_cfq_new_token)
582     OP_CHECK(schedule_ack)
583     OP_TXP_GENERATE()
584     OP_RX_PKT()
585     OP_EXPECT_DGRAM_LEN(21, 512)
586     /* ACK must come before NEW_TOKEN */
587     OP_NEXT_FRAME()
588     OP_EXPECT_FRAME(OSSL_QUIC_FRAME_TYPE_ACK_WITHOUT_ECN)
589     OP_NEXT_FRAME()
590     OP_EXPECT_FRAME(OSSL_QUIC_FRAME_TYPE_NEW_TOKEN)
591     OP_EXPECT_NO_FRAME()
592     OP_RX_PKT_NONE()
593     OP_TXP_GENERATE_NONE()
594     OP_END
595 };
596
597 /* 8. 1-RTT, CRYPTO */
598 static const unsigned char crypto_1[] = {
599     0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09
600 };
601
602 static const struct script_op script_8[] = {
603     OP_PROVIDE_SECRET(QUIC_ENC_LEVEL_1RTT, QRL_SUITE_AES128GCM, secret_1)
604     OP_TXP_GENERATE_NONE()
605     OP_CRYPTO_SEND(QUIC_PN_SPACE_APP, crypto_1)
606     OP_TXP_GENERATE()
607     OP_RX_PKT()
608     OP_EXPECT_DGRAM_LEN(21, 512)
609     OP_NEXT_FRAME()
610     OP_EXPECT_FRAME(OSSL_QUIC_FRAME_TYPE_CRYPTO)
611     OP_EXPECT_NO_FRAME()
612     OP_RX_PKT_NONE()
613     OP_TXP_GENERATE_NONE()
614     OP_END
615 };
616
617 /* 9. 1-RTT, STREAM */
618 static const unsigned char stream_9[] = {
619     0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f, 0x7a, 0x7b
620 };
621
622 static int check_stream_9(struct helper *h)
623 {
624     if (!TEST_mem_eq(h->frame.stream.data, (size_t)h->frame.stream.len,
625                      stream_9, sizeof(stream_9)))
626         return 0;
627
628     return 1;
629 }
630
631 static const struct script_op script_9[] = {
632     OP_PROVIDE_SECRET(QUIC_ENC_LEVEL_1RTT, QRL_SUITE_AES128GCM, secret_1)
633     OP_HANDSHAKE_COMPLETE()
634     OP_TXP_GENERATE_NONE()
635     OP_STREAM_NEW(42)
636     OP_STREAM_SEND(42, stream_9)
637     /* Still no output because of TXFC */
638     OP_TXP_GENERATE_NONE()
639     /* Now grant a TXFC budget */
640     OP_CONN_TXFC_BUMP(1000)
641     OP_STREAM_TXFC_BUMP(42, 1000)
642     OP_TXP_GENERATE()
643     OP_RX_PKT()
644     OP_EXPECT_DGRAM_LEN(21, 512)
645     OP_NEXT_FRAME()
646     OP_EXPECT_FRAME(OSSL_QUIC_FRAME_TYPE_STREAM)
647     OP_CHECK(check_stream_9)
648     OP_EXPECT_NO_FRAME()
649     OP_RX_PKT_NONE()
650     OP_TXP_GENERATE_NONE()
651     OP_END
652 };
653
654 /* 10. 1-RTT, STREAM, round robin */
655 /* The data below is randomly generated data. */
656 static const unsigned char stream_10a[1300] = {
657     0x40, 0x0d, 0xb6, 0x0d, 0x25, 0x5f, 0xdd, 0xb9, 0x05, 0x79, 0xa8, 0xe3,
658     0x79, 0x32, 0xb2, 0xa7, 0x30, 0x6d, 0x29, 0xf6, 0xba, 0x50, 0xbe, 0x83,
659     0xcb, 0x56, 0xec, 0xd6, 0xc7, 0x80, 0x84, 0xa2, 0x2f, 0xeb, 0xc4, 0x37,
660     0x40, 0x44, 0xef, 0xd8, 0x78, 0xbb, 0x92, 0x80, 0x22, 0x33, 0xc0, 0xce,
661     0x33, 0x5b, 0x75, 0x8c, 0xa5, 0x1a, 0x7a, 0x2a, 0xa9, 0x88, 0xaf, 0xf6,
662     0x3a, 0xe2, 0x5e, 0x60, 0x52, 0x6d, 0xef, 0x7f, 0x2a, 0x9a, 0xaa, 0x17,
663     0x0e, 0x12, 0x51, 0x82, 0x08, 0x2f, 0x0f, 0x5b, 0xff, 0xf5, 0x7c, 0x7c,
664     0x89, 0x04, 0xfb, 0xa7, 0x80, 0x4e, 0xda, 0x12, 0x89, 0x01, 0x4a, 0x81,
665     0x84, 0x78, 0x15, 0xa9, 0x12, 0x28, 0x69, 0x4a, 0x25, 0xe5, 0x8b, 0x69,
666     0xc2, 0x9f, 0xb6, 0x59, 0x49, 0xe3, 0x53, 0x90, 0xef, 0xc9, 0xb8, 0x40,
667     0xdd, 0x62, 0x5f, 0x99, 0x68, 0xd2, 0x0a, 0x77, 0xde, 0xf3, 0x11, 0x39,
668     0x7f, 0x93, 0x8b, 0x81, 0x69, 0x36, 0xa7, 0x76, 0xa4, 0x10, 0x56, 0x51,
669     0xe5, 0x45, 0x3a, 0x42, 0x49, 0x6c, 0xc6, 0xa0, 0xb4, 0x13, 0x46, 0x59,
670     0x0e, 0x48, 0x60, 0xc9, 0xff, 0x70, 0x10, 0x8d, 0x6a, 0xf9, 0x5b, 0x94,
671     0xc2, 0x9e, 0x49, 0x19, 0x56, 0xf2, 0xc1, 0xff, 0x08, 0x3f, 0x9e, 0x26,
672     0x8e, 0x99, 0x71, 0xc4, 0x25, 0xb1, 0x4e, 0xcc, 0x7e, 0x5f, 0xf0, 0x4e,
673     0x25, 0xa2, 0x2f, 0x3f, 0x68, 0xaa, 0xcf, 0xbd, 0x19, 0x19, 0x1c, 0x92,
674     0xa0, 0xb6, 0xb8, 0x32, 0xb1, 0x0b, 0x91, 0x05, 0xa9, 0xf8, 0x1a, 0x4b,
675     0x74, 0x09, 0xf9, 0x57, 0xd0, 0x1c, 0x38, 0x10, 0x05, 0x54, 0xd8, 0x4e,
676     0x12, 0x67, 0xcc, 0x43, 0xa3, 0x81, 0xa9, 0x3a, 0x12, 0x57, 0xe7, 0x4b,
677     0x0e, 0xe5, 0x51, 0xf9, 0x5f, 0xd4, 0x46, 0x73, 0xa2, 0x78, 0xb7, 0x00,
678     0x24, 0x69, 0x35, 0x10, 0x1e, 0xb8, 0xa7, 0x4a, 0x9b, 0xbc, 0xfc, 0x04,
679     0x6f, 0x1a, 0xb0, 0x4f, 0x12, 0xc9, 0x2b, 0x3b, 0x94, 0x85, 0x1b, 0x8e,
680     0xba, 0xac, 0xfd, 0x10, 0x22, 0x68, 0x90, 0x17, 0x13, 0x44, 0x18, 0x2f,
681     0x33, 0x37, 0x1a, 0x89, 0xc0, 0x2c, 0x14, 0x59, 0xb2, 0xaf, 0xc0, 0x6b,
682     0xdc, 0x28, 0xe1, 0xe9, 0xc1, 0x0c, 0xb4, 0x80, 0x90, 0xb9, 0x1f, 0x45,
683     0xb4, 0x63, 0x9a, 0x0e, 0xfa, 0x33, 0xf5, 0x75, 0x3a, 0x4f, 0xc3, 0x8c,
684     0x70, 0xdb, 0xd7, 0xbf, 0xf6, 0xb8, 0x7f, 0xcc, 0xe5, 0x85, 0xb6, 0xae,
685     0x25, 0x60, 0x18, 0x5b, 0xf1, 0x51, 0x1a, 0x85, 0xc1, 0x7f, 0xf3, 0xbe,
686     0xb6, 0x82, 0x38, 0xe3, 0xd2, 0xff, 0x8a, 0xc4, 0xdb, 0x08, 0xe6, 0x96,
687     0xd5, 0x3d, 0x1f, 0xc5, 0x12, 0x35, 0x45, 0x75, 0x5d, 0x17, 0x4e, 0xe1,
688     0xb8, 0xc9, 0xf0, 0x45, 0x95, 0x0b, 0x03, 0xcb, 0x85, 0x47, 0xaf, 0xc7,
689     0x88, 0xb6, 0xc1, 0x2c, 0xb8, 0x9b, 0xe6, 0x8b, 0x51, 0xd5, 0x2e, 0x71,
690     0xba, 0xc9, 0xa9, 0x37, 0x5e, 0x1c, 0x2c, 0x03, 0xf0, 0xc7, 0xc1, 0xd3,
691     0x72, 0xaa, 0x4d, 0x19, 0xd6, 0x51, 0x64, 0x12, 0xeb, 0x39, 0xeb, 0x45,
692     0xe9, 0xb4, 0x84, 0x08, 0xb6, 0x6c, 0xc7, 0x3e, 0xf0, 0x88, 0x64, 0xc2,
693     0x91, 0xb7, 0xa5, 0x86, 0x66, 0x83, 0xd5, 0xd3, 0x41, 0x24, 0xb2, 0x1c,
694     0x9a, 0x18, 0x10, 0x0e, 0xa5, 0xc9, 0xef, 0xcd, 0x06, 0xce, 0xa8, 0xaf,
695     0x22, 0x52, 0x25, 0x0b, 0x99, 0x3d, 0xe9, 0x26, 0xda, 0xa9, 0x47, 0xd1,
696     0x4b, 0xa6, 0x4c, 0xfc, 0x80, 0xaf, 0x6a, 0x59, 0x4b, 0x35, 0xa4, 0x93,
697     0x39, 0x5b, 0xfa, 0x91, 0x9d, 0xdf, 0x9d, 0x3c, 0xfb, 0x53, 0xca, 0x18,
698     0x19, 0xe4, 0xda, 0x95, 0x47, 0x5a, 0x37, 0x59, 0xd7, 0xd2, 0xe4, 0x75,
699     0x45, 0x0d, 0x03, 0x7f, 0xa0, 0xa9, 0xa0, 0x71, 0x06, 0xb1, 0x9d, 0x46,
700     0xbd, 0xcf, 0x4a, 0x8b, 0x73, 0xc1, 0x45, 0x5c, 0x00, 0x61, 0xfd, 0xd1,
701     0xa4, 0xa2, 0x3e, 0xaa, 0xbe, 0x72, 0xf1, 0x7a, 0x1a, 0x76, 0x88, 0x5c,
702     0x9e, 0x74, 0x6d, 0x2a, 0x34, 0xfc, 0xf7, 0x41, 0x28, 0xe8, 0xa3, 0x43,
703     0x4d, 0x43, 0x1d, 0x6c, 0x36, 0xb1, 0x45, 0x71, 0x5a, 0x3c, 0xd3, 0x28,
704     0x44, 0xe4, 0x9b, 0xbf, 0x54, 0x16, 0xc3, 0x99, 0x6c, 0x42, 0xd8, 0x20,
705     0xb6, 0x20, 0x5f, 0x6e, 0xbc, 0xba, 0x88, 0x5e, 0x2f, 0xa5, 0xd1, 0x82,
706     0x5c, 0x92, 0xd0, 0x79, 0xfd, 0xcc, 0x61, 0x49, 0xd0, 0x73, 0x92, 0xe6,
707     0x98, 0xe3, 0x80, 0x7a, 0xf9, 0x56, 0x63, 0x33, 0x19, 0xda, 0x54, 0x13,
708     0xf0, 0x21, 0xa8, 0x15, 0xf6, 0xb7, 0x43, 0x7c, 0x1c, 0x1e, 0xb1, 0x89,
709     0x8d, 0xce, 0x20, 0x54, 0x81, 0x80, 0xb5, 0x8f, 0x9b, 0xb1, 0x09, 0x92,
710     0xdb, 0x25, 0x6f, 0x30, 0x29, 0x08, 0x1a, 0x05, 0x08, 0xf4, 0x83, 0x8b,
711     0x1e, 0x2d, 0xfd, 0xe4, 0xb2, 0x76, 0xc8, 0x4d, 0xf3, 0xa6, 0x49, 0x5f,
712     0x2c, 0x99, 0x78, 0xbd, 0x07, 0xef, 0xc8, 0xd9, 0xb5, 0x70, 0x3b, 0x0a,
713     0xcb, 0xbd, 0xa0, 0xea, 0x15, 0xfb, 0xd1, 0x6e, 0x61, 0x83, 0xcb, 0x90,
714     0xd0, 0xa3, 0x81, 0x28, 0xdc, 0xd5, 0x84, 0xae, 0x55, 0x28, 0x13, 0x9e,
715     0xc6, 0xd8, 0xf4, 0x67, 0xd6, 0x0d, 0xd4, 0x69, 0xac, 0xf6, 0x35, 0x95,
716     0x99, 0x44, 0x26, 0x72, 0x36, 0x55, 0xf9, 0x42, 0xa6, 0x1b, 0x00, 0x93,
717     0x00, 0x19, 0x2f, 0x70, 0xd3, 0x16, 0x66, 0x4e, 0x80, 0xbb, 0xb6, 0x84,
718     0xa1, 0x2c, 0x09, 0xfb, 0x41, 0xdf, 0x63, 0xde, 0x62, 0x3e, 0xd0, 0xa8,
719     0xd8, 0x0c, 0x03, 0x06, 0xa9, 0x82, 0x17, 0x9c, 0xd2, 0xa9, 0xd5, 0x6f,
720     0xcc, 0xc0, 0xf2, 0x5d, 0xb1, 0xba, 0xf8, 0x2e, 0x37, 0x8b, 0xe6, 0x5d,
721     0x9f, 0x1b, 0xfb, 0x53, 0x0a, 0x96, 0xbe, 0x69, 0x31, 0x19, 0x8f, 0x44,
722     0x1b, 0xc2, 0x42, 0x7e, 0x65, 0x12, 0x1d, 0x52, 0x1e, 0xe2, 0xc0, 0x86,
723     0x70, 0x88, 0xe5, 0xf6, 0x87, 0x5d, 0x03, 0x4b, 0x12, 0x3c, 0x2d, 0xaf,
724     0x09, 0xf5, 0x4f, 0x82, 0x2e, 0x2e, 0xbe, 0x07, 0xe8, 0x8d, 0x57, 0x6e,
725     0xc0, 0xeb, 0xf9, 0x37, 0xac, 0x89, 0x01, 0xb7, 0xc6, 0x52, 0x1c, 0x86,
726     0xe5, 0xbc, 0x1f, 0xbd, 0xde, 0xa2, 0x42, 0xb6, 0x73, 0x85, 0x6f, 0x06,
727     0x36, 0x56, 0x40, 0x2b, 0xea, 0x16, 0x8c, 0xf4, 0x7b, 0x65, 0x6a, 0xca,
728     0x3c, 0x56, 0x68, 0x01, 0xe3, 0x9c, 0xbb, 0xb9, 0x45, 0x54, 0xcd, 0x13,
729     0x74, 0xad, 0x80, 0x40, 0xbc, 0xd0, 0x74, 0xb4, 0x31, 0xe4, 0xca, 0xd5,
730     0xf8, 0x4f, 0x08, 0x5b, 0xc4, 0x15, 0x1a, 0x51, 0x3b, 0xc6, 0x40, 0xc8,
731     0xea, 0x76, 0x30, 0x95, 0xb7, 0x76, 0xa4, 0xda, 0x20, 0xdb, 0x75, 0x1c,
732     0xf4, 0x87, 0x24, 0x29, 0x54, 0xc6, 0x59, 0x0c, 0xf0, 0xed, 0xf5, 0x3d,
733     0xce, 0x95, 0x23, 0x30, 0x49, 0x91, 0xa7, 0x7b, 0x22, 0xb5, 0xd7, 0x71,
734     0xb0, 0x60, 0xe1, 0xf0, 0x84, 0x74, 0x0e, 0x2f, 0xa8, 0x79, 0x35, 0xb9,
735     0x03, 0xb5, 0x2c, 0xdc, 0x60, 0x48, 0x12, 0xd9, 0x14, 0x5a, 0x58, 0x5d,
736     0x95, 0xc6, 0x47, 0xfd, 0xaf, 0x09, 0xc2, 0x67, 0xa5, 0x09, 0xae, 0xff,
737     0x4b, 0xd5, 0x6c, 0x2f, 0x1d, 0x33, 0x31, 0xcb, 0xdb, 0xcf, 0xf5, 0xf6,
738     0xbc, 0x90, 0xb2, 0x15, 0xd4, 0x34, 0xeb, 0xde, 0x0e, 0x8f, 0x3d, 0xea,
739     0xa4, 0x9b, 0x29, 0x8a, 0xf9, 0x4a, 0xac, 0x38, 0x1e, 0x46, 0xb2, 0x2d,
740     0xa2, 0x61, 0xc5, 0x99, 0x5e, 0x85, 0x36, 0x85, 0xb0, 0xb1, 0x6b, 0xc4,
741     0x06, 0x68, 0xc7, 0x9b, 0x54, 0xb9, 0xc8, 0x9d, 0xf3, 0x1a, 0xe0, 0x67,
742     0x0e, 0x4d, 0x5c, 0x13, 0x54, 0xa4, 0x62, 0x62, 0x6f, 0xae, 0x0e, 0x86,
743     0xa2, 0xe0, 0x31, 0xc7, 0x72, 0xa1, 0xbb, 0x87, 0x3e, 0x61, 0x96, 0xb7,
744     0x53, 0xf9, 0x34, 0xcb, 0xfd, 0x6c, 0x67, 0x25, 0x73, 0x61, 0x75, 0x4f,
745     0xab, 0x37, 0x08, 0xef, 0x35, 0x5a, 0x03, 0xe5, 0x08, 0x43, 0xec, 0xdc,
746     0xb5, 0x2c, 0x1f, 0xe6, 0xeb, 0xc6, 0x06, 0x0b, 0xed, 0xad, 0x74, 0xf4,
747     0x55, 0xef, 0xe0, 0x2e, 0x83, 0x00, 0xdb, 0x32, 0xde, 0xe9, 0xe4, 0x2f,
748     0xf5, 0x20, 0x6d, 0x72, 0x47, 0xf4, 0x68, 0xa6, 0x7f, 0x3e, 0x6a, 0x5a,
749     0x21, 0x76, 0x31, 0x97, 0xa0, 0xc6, 0x7d, 0x03, 0xf7, 0x27, 0x45, 0x5a,
750     0x75, 0x03, 0xc1, 0x5c, 0x94, 0x2b, 0x37, 0x9f, 0x46, 0x8f, 0xc3, 0xa7,
751     0x50, 0xe4, 0xe7, 0x23, 0xf7, 0x20, 0xa2, 0x8e, 0x4b, 0xfd, 0x7a, 0xa7,
752     0x8a, 0x54, 0x7b, 0x32, 0xef, 0x0e, 0x82, 0xb9, 0xf9, 0x14, 0x62, 0x68,
753     0x32, 0x9e, 0x55, 0xc0, 0xd8, 0xc7, 0x41, 0x9c, 0x67, 0x95, 0xbf, 0xc3,
754     0x86, 0x74, 0x70, 0x64, 0x44, 0x23, 0x77, 0x79, 0x82, 0x23, 0x1c, 0xf4,
755     0xa1, 0x05, 0xd3, 0x98, 0x89, 0xde, 0x7d, 0xb3, 0x5b, 0xef, 0x38, 0xd2,
756     0x07, 0xbc, 0x5a, 0x69, 0xa3, 0xe4, 0x37, 0x9b, 0x53, 0xff, 0x04, 0x6b,
757     0xd9, 0xd8, 0x32, 0x89, 0xf7, 0x82, 0x77, 0xcf, 0xe6, 0xff, 0xf4, 0x15,
758     0x54, 0x91, 0x65, 0x96, 0x49, 0xd7, 0x0a, 0xa4, 0xf3, 0x55, 0x2b, 0xc1,
759     0x48, 0xc1, 0x7e, 0x56, 0x69, 0x27, 0xf4, 0xd1, 0x47, 0x1f, 0xde, 0x86,
760     0x15, 0x67, 0x04, 0x9d, 0x41, 0x1f, 0xe8, 0xe1, 0x23, 0xe4, 0x56, 0xb9,
761     0xdb, 0x4e, 0xe4, 0x84, 0x6c, 0x63, 0x39, 0xad, 0x44, 0x6d, 0x4e, 0x28,
762     0xcd, 0xf6, 0xac, 0xec, 0xc2, 0xad, 0xcd, 0xc3, 0xed, 0x03, 0x63, 0x5d,
763     0xef, 0x1d, 0x40, 0x8d, 0x9a, 0x02, 0x67, 0x4b, 0x55, 0xb5, 0xfe, 0x75,
764     0xb6, 0x53, 0x34, 0x1d, 0x7b, 0x26, 0x23, 0xfe, 0xb9, 0x21, 0xd3, 0xe0,
765     0xa0, 0x1a, 0x85, 0xe5
766 };
767
768 static const unsigned char stream_10b[1300] = {
769     0x18, 0x00, 0xd7, 0xfb, 0x12, 0xda, 0xdb, 0x68, 0xeb, 0x38, 0x4d, 0xf6,
770     0xb2, 0x45, 0x74, 0x4c, 0xcc, 0xe7, 0xa7, 0xc1, 0x26, 0x84, 0x3d, 0xdf,
771     0x7d, 0xc5, 0xe9, 0xd4, 0x31, 0xa2, 0x51, 0x38, 0x95, 0xe2, 0x68, 0x11,
772     0x9d, 0xd1, 0x52, 0xb5, 0xef, 0x76, 0xe0, 0x3d, 0x11, 0x50, 0xd7, 0xb2,
773     0xc1, 0x7d, 0x12, 0xaf, 0x02, 0x52, 0x97, 0x03, 0xf3, 0x2e, 0x54, 0xdf,
774     0xa0, 0x40, 0x76, 0x52, 0x82, 0x23, 0x3c, 0xbd, 0x20, 0x6d, 0x0a, 0x6f,
775     0x81, 0xfc, 0x41, 0x9d, 0x2e, 0xa7, 0x2c, 0x78, 0x9c, 0xd8, 0x56, 0xb0,
776     0x31, 0x35, 0xc8, 0x53, 0xef, 0xf9, 0x43, 0x17, 0xc0, 0x8c, 0x2c, 0x8f,
777     0x4a, 0x68, 0xe8, 0x9f, 0xbd, 0x3f, 0xf2, 0x18, 0xb8, 0xe6, 0x55, 0xea,
778     0x2a, 0x37, 0x3e, 0xac, 0xb0, 0x75, 0xd4, 0x75, 0x12, 0x82, 0xec, 0x21,
779     0xb9, 0xce, 0xe5, 0xc1, 0x62, 0x49, 0xd5, 0xf1, 0xca, 0xd4, 0x32, 0x76,
780     0x34, 0x5f, 0x3e, 0xc9, 0xb3, 0x54, 0xe4, 0xd0, 0xa9, 0x7d, 0x0c, 0x64,
781     0x48, 0x0a, 0x74, 0x38, 0x03, 0xd0, 0x20, 0xac, 0xe3, 0x58, 0x3d, 0x4b,
782     0xa7, 0x46, 0xac, 0x57, 0x63, 0x12, 0x17, 0xcb, 0x96, 0xed, 0xc9, 0x39,
783     0x64, 0xde, 0xff, 0xc6, 0xb2, 0x40, 0x2c, 0xf9, 0x1d, 0xa6, 0x94, 0x2a,
784     0x16, 0x4d, 0x7f, 0x22, 0x91, 0x8b, 0xfe, 0x83, 0x77, 0x02, 0x68, 0x62,
785     0x27, 0x77, 0x2e, 0xe9, 0xce, 0xbc, 0x20, 0xe8, 0xfb, 0xf8, 0x4e, 0x17,
786     0x07, 0xe1, 0xaa, 0x29, 0xb7, 0x50, 0xcf, 0xb0, 0x6a, 0xcf, 0x01, 0xec,
787     0xbf, 0xff, 0xb5, 0x9f, 0x00, 0x64, 0x80, 0xbb, 0xa6, 0xe4, 0xa2, 0x1e,
788     0xe4, 0xf8, 0xa3, 0x0d, 0xc7, 0x65, 0x45, 0xb7, 0x01, 0x33, 0x80, 0x37,
789     0x11, 0x16, 0x34, 0xc1, 0x06, 0xc5, 0xd3, 0xc4, 0x70, 0x62, 0x75, 0xd8,
790     0xa3, 0xba, 0x84, 0x9f, 0x81, 0x9f, 0xda, 0x01, 0x83, 0x42, 0x84, 0x05,
791     0x69, 0x68, 0xb0, 0x74, 0x73, 0x0f, 0x68, 0x39, 0xd3, 0x11, 0xc5, 0x55,
792     0x3e, 0xf2, 0xb7, 0xf4, 0xa6, 0xed, 0x0b, 0x50, 0xbe, 0x44, 0xf8, 0x67,
793     0x48, 0x46, 0x5e, 0x71, 0x07, 0xcf, 0xca, 0x8a, 0xbc, 0xa4, 0x3c, 0xd2,
794     0x4a, 0x80, 0x2e, 0x4f, 0xc5, 0x3b, 0x61, 0xc1, 0x7e, 0x93, 0x9e, 0xe0,
795     0x05, 0xfb, 0x10, 0xe8, 0x53, 0xff, 0x16, 0x5e, 0x18, 0xe0, 0x9f, 0x39,
796     0xbf, 0xaa, 0x80, 0x6d, 0xb7, 0x9f, 0x51, 0x91, 0xa0, 0xf6, 0xce, 0xad,
797     0xed, 0x56, 0x15, 0xb9, 0x12, 0x57, 0x60, 0xa6, 0xae, 0x54, 0x6e, 0x36,
798     0xf3, 0xe0, 0x05, 0xd8, 0x3e, 0x6d, 0x08, 0x36, 0xc9, 0x79, 0x64, 0x51,
799     0x63, 0x92, 0xa8, 0xa1, 0xbf, 0x55, 0x26, 0x80, 0x75, 0x44, 0x33, 0x33,
800     0xfb, 0xb7, 0xec, 0xf9, 0xc6, 0x01, 0xf9, 0xd5, 0x93, 0xfc, 0xb7, 0x43,
801     0xa2, 0x38, 0x0d, 0x17, 0x75, 0x67, 0xec, 0xc9, 0x98, 0xd6, 0x25, 0xe6,
802     0xb9, 0xed, 0x61, 0xa4, 0xee, 0x2c, 0xda, 0x27, 0xbd, 0xff, 0x86, 0x1e,
803     0x45, 0x64, 0xfe, 0xcf, 0x0c, 0x9b, 0x7b, 0x75, 0x5f, 0xf1, 0xe0, 0xba,
804     0x77, 0x8c, 0x03, 0x8f, 0xb4, 0x3a, 0xb6, 0x9c, 0xda, 0x9a, 0x83, 0xcb,
805     0xe9, 0xcb, 0x3f, 0xf4, 0x10, 0x99, 0x5b, 0xe1, 0x19, 0x8f, 0x6b, 0x95,
806     0x50, 0xe6, 0x78, 0xc9, 0x35, 0xb6, 0x87, 0xd8, 0x9e, 0x17, 0x30, 0x96,
807     0x70, 0xa3, 0x04, 0x69, 0x1c, 0xa2, 0x6c, 0xd4, 0x88, 0x48, 0x44, 0x14,
808     0x94, 0xd4, 0xc9, 0x4d, 0xe3, 0x82, 0x7e, 0x62, 0xf0, 0x0a, 0x18, 0x4d,
809     0xd0, 0xd6, 0x63, 0xa3, 0xdf, 0xea, 0x28, 0xf4, 0x00, 0x75, 0x70, 0x78,
810     0x08, 0x70, 0x3f, 0xff, 0x84, 0x86, 0x72, 0xea, 0x4f, 0x15, 0x8c, 0x17,
811     0x60, 0x5f, 0xa1, 0x50, 0xa0, 0xfc, 0x6f, 0x8a, 0x46, 0xfc, 0x01, 0x8d,
812     0x7c, 0xdc, 0x69, 0x6a, 0xd3, 0x74, 0x69, 0x76, 0x77, 0xdd, 0xe4, 0x9c,
813     0x49, 0x1e, 0x6f, 0x7d, 0x31, 0x14, 0xd9, 0xe9, 0xe7, 0x17, 0x66, 0x82,
814     0x1b, 0xf1, 0x0f, 0xe2, 0xba, 0xd2, 0x28, 0xd1, 0x6f, 0x48, 0xc7, 0xac,
815     0x08, 0x4e, 0xee, 0x94, 0x66, 0x99, 0x34, 0x16, 0x5d, 0x95, 0xae, 0xe3,
816     0x59, 0x79, 0x7f, 0x8e, 0x9f, 0xe3, 0xdb, 0xff, 0xdc, 0x4d, 0xb0, 0xbf,
817     0xf9, 0xf3, 0x3e, 0xec, 0xcf, 0x50, 0x3d, 0x2d, 0xba, 0x94, 0x1f, 0x1a,
818     0xab, 0xa4, 0xf4, 0x67, 0x43, 0x7e, 0xb9, 0x65, 0x20, 0x13, 0xb1, 0xd9,
819     0x88, 0x4a, 0x24, 0x13, 0x84, 0x86, 0xae, 0x2b, 0x0c, 0x6c, 0x7e, 0xd4,
820     0x25, 0x6e, 0xaa, 0x8d, 0x0c, 0x54, 0x99, 0xde, 0x1d, 0xac, 0x8c, 0x5c,
821     0x73, 0x94, 0xd9, 0x75, 0xcb, 0x5a, 0x54, 0x3d, 0xeb, 0xff, 0xc1, 0x95,
822     0x53, 0xb5, 0x39, 0xf7, 0xe5, 0xf1, 0x77, 0xd1, 0x42, 0x82, 0x4b, 0xb0,
823     0xab, 0x19, 0x28, 0xff, 0x53, 0x28, 0x87, 0x46, 0xc6, 0x6f, 0x05, 0x06,
824     0xa6, 0x0c, 0x97, 0x93, 0x68, 0x38, 0xe1, 0x61, 0xed, 0xf8, 0x90, 0x13,
825     0xa3, 0x6f, 0xf2, 0x08, 0x37, 0xd7, 0x05, 0x25, 0x34, 0x43, 0x57, 0x72,
826     0xfd, 0x6c, 0xc2, 0x19, 0x26, 0xe7, 0x50, 0x30, 0xb8, 0x6d, 0x09, 0x71,
827     0x83, 0x75, 0xd4, 0x11, 0x25, 0x29, 0xc6, 0xee, 0xb2, 0x51, 0x1c, 0x1c,
828     0x9e, 0x2d, 0x09, 0xb9, 0x73, 0x2b, 0xbf, 0xda, 0xc8, 0x1e, 0x2b, 0xe5,
829     0x3f, 0x1e, 0x63, 0xe9, 0xc0, 0x6d, 0x04, 0x3a, 0x48, 0x61, 0xa8, 0xc6,
830     0x16, 0x8d, 0x69, 0xc0, 0x67, 0x0c, 0x3b, 0xc4, 0x05, 0x36, 0xa1, 0x30,
831     0x62, 0x92, 0x4d, 0x44, 0x31, 0x66, 0x46, 0xda, 0xef, 0x0f, 0x4e, 0xfb,
832     0x78, 0x6a, 0xa9, 0x5b, 0xf8, 0x56, 0x26, 0x74, 0x16, 0xab, 0x17, 0x93,
833     0x3c, 0x36, 0xbb, 0xa2, 0xbf, 0xad, 0xba, 0xb1, 0xfe, 0xc4, 0x9f, 0x75,
834     0x47, 0x1e, 0x99, 0x7e, 0x32, 0xe8, 0xd4, 0x6c, 0xa4, 0xf8, 0xd2, 0xe4,
835     0xb2, 0x51, 0xbb, 0xb2, 0xd7, 0xce, 0x94, 0xaf, 0x7f, 0xe6, 0x2c, 0x13,
836     0xae, 0xd2, 0x29, 0x30, 0x7b, 0xfd, 0x25, 0x61, 0xf9, 0xe8, 0x35, 0x2d,
837     0x1a, 0xc9, 0x81, 0xa5, 0xfe, 0xce, 0xf6, 0x17, 0xc5, 0xfb, 0x8c, 0x79,
838     0x67, 0xa8, 0x5f, 0x5c, 0x31, 0xbc, 0xfc, 0xf3, 0x6b, 0xd3, 0x0d, 0xe0,
839     0x62, 0xab, 0x86, 0xc3, 0x17, 0x5a, 0xba, 0x97, 0x86, 0x8f, 0x65, 0xd6,
840     0xbd, 0x0c, 0xa1, 0xfb, 0x7f, 0x7c, 0xdc, 0xcb, 0x94, 0x30, 0x0b, 0x04,
841     0x54, 0xc4, 0x31, 0xa1, 0xca, 0x1e, 0xc5, 0xf0, 0xb6, 0x08, 0xd7, 0x2e,
842     0xa1, 0x90, 0x41, 0xce, 0xd9, 0xef, 0x3a, 0x58, 0x01, 0x1a, 0x73, 0x18,
843     0xad, 0xdc, 0x20, 0x25, 0x95, 0x1a, 0xfe, 0x61, 0xf1, 0x58, 0x32, 0x8b,
844     0x43, 0x59, 0xd6, 0x21, 0xdb, 0xa9, 0x8e, 0x54, 0xe6, 0x21, 0xcf, 0xd3,
845     0x6b, 0x59, 0x29, 0x9b, 0x3e, 0x6c, 0x7f, 0xe2, 0x29, 0x72, 0x8c, 0xd1,
846     0x3e, 0x9a, 0x84, 0x98, 0xb0, 0xf3, 0x20, 0x30, 0x34, 0x71, 0xa7, 0x5b,
847     0xf0, 0x26, 0xe1, 0xf4, 0x76, 0x65, 0xc9, 0xd7, 0xe4, 0xb9, 0x25, 0x48,
848     0xc2, 0x7e, 0xa6, 0x0b, 0x0d, 0x05, 0x68, 0xa1, 0x96, 0x61, 0x0b, 0x4c,
849     0x2f, 0x1a, 0xe3, 0x56, 0x71, 0x89, 0x48, 0x66, 0xd8, 0xd0, 0x69, 0x37,
850     0x7a, 0xdf, 0xdb, 0xed, 0xad, 0x82, 0xaa, 0x40, 0x25, 0x47, 0x3e, 0x75,
851     0xa6, 0x0e, 0xf5, 0x2f, 0xa7, 0x4e, 0x97, 0xa2, 0x5f, 0x01, 0x99, 0x48,
852     0x3a, 0x63, 0x18, 0x20, 0x61, 0x72, 0xe4, 0xcf, 0x4b, 0x3b, 0x99, 0x36,
853     0xe1, 0xf3, 0xbf, 0xae, 0x2b, 0x6b, 0xa1, 0x94, 0xa0, 0x15, 0x94, 0xd6,
854     0xe0, 0xba, 0x71, 0xa2, 0x85, 0xa0, 0x8c, 0x5e, 0x58, 0xe2, 0xde, 0x6b,
855     0x08, 0x68, 0x90, 0x82, 0x71, 0x8d, 0xfd, 0x12, 0xa2, 0x49, 0x87, 0x70,
856     0xee, 0x2a, 0x08, 0xe2, 0x26, 0xaf, 0xeb, 0x85, 0x35, 0xd2, 0x0e, 0xfd,
857     0x2b, 0x6f, 0xc0, 0xfe, 0x41, 0xbb, 0xd7, 0x0a, 0xa3, 0x8d, 0x8b, 0xec,
858     0x44, 0x9f, 0x46, 0x59, 0x4d, 0xac, 0x04, 0x1e, 0xde, 0x10, 0x7b, 0x17,
859     0x0a, 0xb0, 0xcc, 0x26, 0x0c, 0xa9, 0x3c, 0x5f, 0xd8, 0xe6, 0x52, 0xd3,
860     0xfd, 0x0b, 0x66, 0x75, 0x06, 0x84, 0x23, 0x64, 0x2b, 0x80, 0x68, 0xf9,
861     0xcb, 0xcd, 0x04, 0x07, 0xf7, 0xe0, 0x07, 0xb4, 0xc6, 0xa0, 0x08, 0xd0,
862     0x76, 0x16, 0x77, 0xd8, 0x48, 0xf0, 0x45, 0x4e, 0xe2, 0xf2, 0x88, 0xcd,
863     0x0f, 0xbd, 0x7d, 0xb6, 0xbe, 0x4e, 0x9e, 0x5d, 0x6c, 0x47, 0x26, 0x34,
864     0x94, 0xfb, 0xc5, 0x4f, 0x5c, 0xb5, 0xb5, 0xfc, 0x99, 0x34, 0x71, 0xe5,
865     0xe1, 0x36, 0x0c, 0xd2, 0x95, 0xb8, 0x93, 0x3c, 0x5d, 0x2d, 0x71, 0x55,
866     0x0b, 0x96, 0x4e, 0x9f, 0x07, 0x9a, 0x38, 0x9a, 0xcc, 0x24, 0xb5, 0xac,
867     0x05, 0x8b, 0x1c, 0x61, 0xd4, 0xf2, 0xdf, 0x9e, 0x11, 0xe3, 0x7d, 0x64,
868     0x2f, 0xe5, 0x13, 0xd4, 0x0a, 0xe9, 0x32, 0x26, 0xa8, 0x93, 0x21, 0x59,
869     0xf3, 0x41, 0x48, 0x0a, 0xbd, 0x59, 0x8f, 0xf8, 0x72, 0xab, 0xd3, 0x65,
870     0x8e, 0xdc, 0xaa, 0x0c, 0xc0, 0x01, 0x36, 0xb7, 0xf5, 0x84, 0x27, 0x9a,
871     0x98, 0x89, 0x73, 0x3a, 0xeb, 0x55, 0x15, 0xc9, 0x3d, 0xe1, 0xf8, 0xea,
872     0xf6, 0x11, 0x28, 0xe0, 0x80, 0x93, 0xcc, 0xba, 0xe1, 0xf1, 0x81, 0xbc,
873     0xa4, 0x30, 0xbc, 0x98, 0xe8, 0x9e, 0x8d, 0x17, 0x7e, 0xb7, 0xb1, 0x27,
874     0x6f, 0xcf, 0x9c, 0x0d, 0x1d, 0x01, 0xea, 0x45, 0xc0, 0x90, 0xda, 0x53,
875     0xf6, 0xde, 0xdf, 0x12, 0xa1, 0x23, 0x3d, 0x92, 0x89, 0x77, 0xa7, 0x2a,
876     0xe7, 0x45, 0x24, 0xdd, 0xf2, 0x17, 0x10, 0xca, 0x6e, 0x14, 0xb2, 0x77,
877     0x08, 0xc4, 0x18, 0xcd
878 };
879
880 static uint64_t stream_10a_off, stream_10b_off;
881
882 static int check_stream_10a(struct helper *h)
883 {
884     /*
885      * Must have filled or almost filled the packet (using default MDPL of
886      * 1200).
887      */
888     if (!TEST_uint64_t_ge(h->frame.stream.len, 1150)
889         || !TEST_uint64_t_le(h->frame.stream.len, 1200))
890         return 0;
891
892     if (!TEST_mem_eq(h->frame.stream.data, (size_t)h->frame.stream.len,
893                      stream_10a, (size_t)h->frame.stream.len))
894         return 0;
895
896     stream_10a_off = h->frame.stream.offset + h->frame.stream.len;
897     return 1;
898 }
899
900 static int check_stream_10b(struct helper *h)
901 {
902     if (!TEST_uint64_t_ge(h->frame.stream.len, 1150)
903         || !TEST_uint64_t_le(h->frame.stream.len, 1200))
904         return 0;
905
906     if (!TEST_mem_eq(h->frame.stream.data, (size_t)h->frame.stream.len,
907                      stream_10b, (size_t)h->frame.stream.len))
908         return 0;
909
910     stream_10b_off = h->frame.stream.offset + h->frame.stream.len;
911     return 1;
912 }
913
914 static int check_stream_10c(struct helper *h)
915 {
916     if (!TEST_uint64_t_ge(h->frame.stream.len, 5)
917         || !TEST_uint64_t_le(h->frame.stream.len, 200))
918         return 0;
919
920     if (!TEST_mem_eq(h->frame.stream.data, (size_t)h->frame.stream.len,
921                      stream_10a + stream_10a_off, (size_t)h->frame.stream.len))
922         return 0;
923
924     return 1;
925 }
926
927 static int check_stream_10d(struct helper *h)
928 {
929     if (!TEST_uint64_t_ge(h->frame.stream.len, 5)
930         || !TEST_uint64_t_le(h->frame.stream.len, 200))
931         return 0;
932
933     if (!TEST_mem_eq(h->frame.stream.data, (size_t)h->frame.stream.len,
934                      stream_10b + stream_10b_off, (size_t)h->frame.stream.len))
935         return 0;
936
937     return 1;
938 }
939
940 static const struct script_op script_10[] = {
941     OP_PROVIDE_SECRET(QUIC_ENC_LEVEL_1RTT, QRL_SUITE_AES128GCM, secret_1)
942     OP_HANDSHAKE_COMPLETE()
943     OP_TXP_GENERATE_NONE()
944     OP_STREAM_NEW(42)
945     OP_STREAM_NEW(43)
946     OP_CONN_TXFC_BUMP(10000)
947     OP_STREAM_TXFC_BUMP(42, 5000)
948     OP_STREAM_TXFC_BUMP(43, 5000)
949     OP_STREAM_SEND(42, stream_10a)
950     OP_STREAM_SEND(43, stream_10b)
951
952     /* First packet containing data from stream 42 */
953     OP_TXP_GENERATE()
954     OP_RX_PKT()
955     OP_EXPECT_DGRAM_LEN(1100, 1200)
956     OP_NEXT_FRAME()
957     OP_EXPECT_FRAME(OSSL_QUIC_FRAME_TYPE_STREAM)
958     OP_CHECK(check_stream_10a)
959     OP_EXPECT_NO_FRAME()
960
961     /* Second packet containing data from stream 43 */
962     OP_TXP_GENERATE()
963     OP_RX_PKT()
964     OP_EXPECT_DGRAM_LEN(1100, 1200)
965     OP_NEXT_FRAME()
966     OP_EXPECT_FRAME(OSSL_QUIC_FRAME_TYPE_STREAM)
967     OP_CHECK(check_stream_10b)
968     OP_EXPECT_NO_FRAME()
969
970     /* Third packet containing data from stream 42 */
971     OP_TXP_GENERATE()
972     OP_RX_PKT()
973     OP_EXPECT_DGRAM_LEN(200, 500)
974     OP_NEXT_FRAME()
975     OP_EXPECT_FRAME(OSSL_QUIC_FRAME_TYPE_STREAM_OFF_LEN)
976     OP_CHECK(check_stream_10c)
977     OP_NEXT_FRAME()
978     OP_EXPECT_FRAME(OSSL_QUIC_FRAME_TYPE_STREAM_OFF)
979     OP_CHECK(check_stream_10d)
980     OP_EXPECT_NO_FRAME()
981
982     OP_RX_PKT_NONE()
983     OP_TXP_GENERATE_NONE()
984
985     OP_END
986 };
987
988 /* 11. Initial, CRYPTO */
989 static const struct script_op script_11[] = {
990     OP_PROVIDE_SECRET(QUIC_ENC_LEVEL_INITIAL, QRL_SUITE_AES128GCM, secret_1)
991     OP_TXP_GENERATE_NONE()
992     OP_CRYPTO_SEND(QUIC_PN_SPACE_INITIAL, crypto_1)
993     OP_TXP_GENERATE()
994     OP_RX_PKT()
995     OP_EXPECT_DGRAM_LEN(1200, 1200)
996     OP_NEXT_FRAME()
997     OP_EXPECT_FRAME(OSSL_QUIC_FRAME_TYPE_CRYPTO)
998     OP_EXPECT_NO_FRAME()
999     OP_RX_PKT_NONE()
1000     OP_TXP_GENERATE_NONE()
1001     OP_END
1002 };
1003
1004 /* 12. 1-RTT, STOP_SENDING */
1005 static int check_stream_12(struct helper *h)
1006 {
1007     if (!TEST_uint64_t_eq(h->frame.stop_sending.stream_id, 42)
1008         || !TEST_uint64_t_eq(h->frame.stop_sending.app_error_code, 4568))
1009         return 0;
1010
1011     return 1;
1012 }
1013
1014 static const struct script_op script_12[] = {
1015     OP_PROVIDE_SECRET(QUIC_ENC_LEVEL_1RTT, QRL_SUITE_AES128GCM, secret_1)
1016     OP_HANDSHAKE_COMPLETE()
1017     OP_TXP_GENERATE_NONE()
1018     OP_STREAM_NEW(42)
1019     OP_STOP_SENDING(42, 4568)
1020     OP_TXP_GENERATE()
1021     OP_RX_PKT()
1022     OP_EXPECT_DGRAM_LEN(21, 128)
1023     OP_NEXT_FRAME()
1024     OP_EXPECT_FRAME(OSSL_QUIC_FRAME_TYPE_STOP_SENDING)
1025     OP_CHECK(check_stream_12)
1026     OP_EXPECT_NO_FRAME()
1027     OP_RX_PKT_NONE()
1028     OP_TXP_GENERATE_NONE()
1029     OP_END
1030 };
1031
1032 /* 13. 1-RTT, RESET_STREAM */
1033 static const unsigned char stream_13[] = {
1034     0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f, 0x7a, 0x7b
1035 };
1036
1037 static ossl_unused int check_stream_13(struct helper *h)
1038 {
1039     if (!TEST_uint64_t_eq(h->frame.reset_stream.stream_id, 42)
1040         || !TEST_uint64_t_eq(h->frame.reset_stream.app_error_code, 4568)
1041         || !TEST_uint64_t_eq(h->frame.reset_stream.final_size, 0))
1042         return 0;
1043
1044     return 1;
1045 }
1046
1047 static const struct script_op script_13[] = {
1048     OP_PROVIDE_SECRET(QUIC_ENC_LEVEL_1RTT, QRL_SUITE_AES128GCM, secret_1)
1049     OP_HANDSHAKE_COMPLETE()
1050     OP_TXP_GENERATE_NONE()
1051     OP_STREAM_NEW(42)
1052     OP_CONN_TXFC_BUMP(8)
1053     OP_STREAM_TXFC_BUMP(42, 8)
1054     OP_STREAM_SEND(42, stream_13)
1055     OP_RESET_STREAM(42, 4568)
1056     OP_TXP_GENERATE()
1057     OP_RX_PKT()
1058     OP_EXPECT_DGRAM_LEN(21, 128)
1059     OP_NEXT_FRAME()
1060     OP_EXPECT_FRAME(OSSL_QUIC_FRAME_TYPE_RESET_STREAM)
1061     OP_CHECK(check_stream_13)
1062     OP_NEXT_FRAME()
1063     OP_EXPECT_NO_FRAME()
1064     OP_RX_PKT_NONE()
1065     OP_TXP_GENERATE_NONE()
1066     OP_END
1067 };
1068
1069 /* 14. 1-RTT, CONNECTION_CLOSE */
1070 static int gen_conn_close(struct helper *h)
1071 {
1072     OSSL_QUIC_FRAME_CONN_CLOSE f = {0};
1073
1074     f.error_code     = 2345;
1075     f.frame_type     = OSSL_QUIC_FRAME_TYPE_HANDSHAKE_DONE;
1076     f.reason         = "Reason string";
1077     f.reason_len     = strlen(f.reason);
1078
1079     if (!TEST_true(ossl_quic_tx_packetiser_schedule_conn_close(h->txp, &f)))
1080         return 0;
1081
1082     return 1;
1083 }
1084
1085 static int check_14(struct helper *h)
1086 {
1087     if (!TEST_int_eq(h->frame.conn_close.is_app, 0)
1088         || !TEST_uint64_t_eq(h->frame.conn_close.frame_type,
1089                              OSSL_QUIC_FRAME_TYPE_HANDSHAKE_DONE)
1090         || !TEST_uint64_t_eq(h->frame.conn_close.error_code, 2345)
1091         || !TEST_mem_eq(h->frame.conn_close.reason, h->frame.conn_close.reason_len,
1092                         "Reason string", 13))
1093         return 0;
1094
1095     return 1;
1096 }
1097
1098 static const struct script_op script_14[] = {
1099     OP_PROVIDE_SECRET(QUIC_ENC_LEVEL_1RTT, QRL_SUITE_AES128GCM, secret_1)
1100     OP_HANDSHAKE_COMPLETE()
1101     OP_TXP_GENERATE_NONE()
1102     OP_CHECK(gen_conn_close)
1103     OP_TXP_GENERATE()
1104     OP_RX_PKT()
1105     OP_EXPECT_DGRAM_LEN(21, 512)
1106     OP_NEXT_FRAME()
1107     OP_EXPECT_FRAME(OSSL_QUIC_FRAME_TYPE_CONN_CLOSE_TRANSPORT)
1108     OP_CHECK(check_14)
1109     OP_EXPECT_NO_FRAME()
1110     OP_RX_PKT_NONE()
1111     OP_END
1112 };
1113
1114 /* 15. INITIAL, Anti-Deadlock Probe Simulation */
1115 static int gen_probe_initial(struct helper *h)
1116 {
1117     OSSL_ACKM_PROBE_INFO *probe = ossl_ackm_get0_probe_request(h->args.ackm);
1118
1119     /*
1120      * Pretend the ACKM asked for an anti-deadlock Initial probe.
1121      * We test output of this in the ACKM unit tests.
1122      */
1123     ++probe->anti_deadlock_initial;
1124     return 1;
1125 }
1126
1127 static const struct script_op script_15[] = {
1128     OP_PROVIDE_SECRET(QUIC_ENC_LEVEL_INITIAL, QRL_SUITE_AES128GCM, secret_1)
1129     OP_TXP_GENERATE_NONE()
1130     OP_CHECK(gen_probe_initial)
1131     OP_TXP_GENERATE()
1132     OP_RX_PKT()
1133     OP_EXPECT_DGRAM_LEN(1200, 1200)
1134     OP_NEXT_FRAME()
1135     OP_EXPECT_FRAME(OSSL_QUIC_FRAME_TYPE_PING)
1136     OP_EXPECT_NO_FRAME()
1137     OP_RX_PKT_NONE()
1138     OP_TXP_GENERATE_NONE()
1139     OP_END
1140 };
1141
1142 /* 16. HANDSHAKE, Anti-Deadlock Probe Simulation */
1143 static int gen_probe_handshake(struct helper *h)
1144 {
1145     OSSL_ACKM_PROBE_INFO *probe = ossl_ackm_get0_probe_request(h->args.ackm);
1146
1147     /*
1148      * Pretend the ACKM asked for an anti-deadlock Handshake probe.
1149      * We test output of this in the ACKM unit tests.
1150      */
1151     ++probe->anti_deadlock_handshake;
1152     return 1;
1153 }
1154
1155 static const struct script_op script_16[] = {
1156     OP_DISCARD_EL(QUIC_ENC_LEVEL_INITIAL)
1157     OP_PROVIDE_SECRET(QUIC_ENC_LEVEL_HANDSHAKE, QRL_SUITE_AES128GCM, secret_1)
1158     OP_TXP_GENERATE_NONE()
1159     OP_CHECK(gen_probe_handshake)
1160     OP_TXP_GENERATE()
1161     OP_RX_PKT()
1162     OP_EXPECT_DGRAM_LEN(21, 512)
1163     OP_NEXT_FRAME()
1164     OP_EXPECT_FRAME(OSSL_QUIC_FRAME_TYPE_PING)
1165     OP_EXPECT_NO_FRAME()
1166     OP_RX_PKT_NONE()
1167     OP_TXP_GENERATE_NONE()
1168     OP_END
1169 };
1170
1171 /* 17. 1-RTT, Probe Simulation */
1172 static int gen_probe_1rtt(struct helper *h)
1173 {
1174     OSSL_ACKM_PROBE_INFO *probe = ossl_ackm_get0_probe_request(h->args.ackm);
1175
1176     /*
1177      * Pretend the ACKM asked for a 1-RTT PTO probe.
1178      * We test output of this in the ACKM unit tests.
1179      */
1180     ++probe->pto[QUIC_PN_SPACE_APP];
1181     return 1;
1182 }
1183
1184 static const struct script_op script_17[] = {
1185     OP_DISCARD_EL(QUIC_ENC_LEVEL_INITIAL)
1186     OP_DISCARD_EL(QUIC_ENC_LEVEL_HANDSHAKE)
1187     OP_PROVIDE_SECRET(QUIC_ENC_LEVEL_1RTT, QRL_SUITE_AES128GCM, secret_1)
1188     OP_TXP_GENERATE_NONE()
1189     OP_CHECK(gen_probe_1rtt)
1190     OP_TXP_GENERATE()
1191     OP_RX_PKT()
1192     OP_EXPECT_DGRAM_LEN(21, 512)
1193     OP_NEXT_FRAME()
1194     OP_EXPECT_FRAME(OSSL_QUIC_FRAME_TYPE_PING)
1195     OP_EXPECT_NO_FRAME()
1196     OP_RX_PKT_NONE()
1197     OP_TXP_GENERATE_NONE()
1198     OP_END
1199 };
1200
1201 static const struct script_op *const scripts[] = {
1202     script_1,
1203     script_2,
1204     script_3,
1205     script_4,
1206     script_5,
1207     script_6,
1208     script_7,
1209     script_8,
1210     script_9,
1211     script_10,
1212     script_11,
1213     script_12,
1214     script_13,
1215     script_14,
1216     script_15,
1217     script_16,
1218     script_17
1219 };
1220
1221 static void skip_padding(struct helper *h)
1222 {
1223     uint64_t frame_type;
1224
1225     if (!ossl_quic_wire_peek_frame_header(&h->pkt, &frame_type, NULL))
1226         return; /* EOF */
1227
1228     if (frame_type == OSSL_QUIC_FRAME_TYPE_PADDING)
1229         ossl_quic_wire_decode_padding(&h->pkt);
1230 }
1231
1232 static int run_script(int script_idx, const struct script_op *script)
1233 {
1234     int testresult = 0, have_helper = 0;
1235     QUIC_TXP_STATUS status;
1236     struct helper h;
1237     const struct script_op *op;
1238     size_t opn = 0;
1239
1240     if (!helper_init(&h))
1241         goto err;
1242
1243     have_helper = 1;
1244     for (op = script, opn = 0; op->opcode != OPK_END; ++op, ++opn) {
1245         switch (op->opcode) {
1246         case OPK_TXP_GENERATE:
1247             if (!TEST_int_eq(ossl_quic_tx_packetiser_generate(h.txp, &status),
1248                              TX_PACKETISER_RES_SENT_PKT))
1249                 goto err;
1250
1251             ossl_qtx_finish_dgram(h.args.qtx);
1252             ossl_qtx_flush_net(h.args.qtx);
1253             break;
1254         case OPK_TXP_GENERATE_NONE:
1255             if (!TEST_int_eq(ossl_quic_tx_packetiser_generate(h.txp, &status),
1256                              TX_PACKETISER_RES_NO_PKT))
1257                 goto err;
1258
1259             break;
1260         case OPK_RX_PKT:
1261             ossl_quic_demux_pump(h.demux);
1262             ossl_qrx_pkt_release(h.qrx_pkt);
1263             h.qrx_pkt = NULL;
1264             if (!TEST_true(ossl_qrx_read_pkt(h.qrx, &h.qrx_pkt)))
1265                 goto err;
1266             if (!TEST_true(PACKET_buf_init(&h.pkt,
1267                                            h.qrx_pkt->hdr->data,
1268                                            h.qrx_pkt->hdr->len)))
1269                 goto err;
1270             h.frame_type = UINT64_MAX;
1271             break;
1272         case OPK_RX_PKT_NONE:
1273             ossl_quic_demux_pump(h.demux);
1274             if (!TEST_false(ossl_qrx_read_pkt(h.qrx, &h.qrx_pkt)))
1275                 goto err;
1276             h.frame_type = UINT64_MAX;
1277             break;
1278         case OPK_EXPECT_DGRAM_LEN:
1279             if (!TEST_size_t_ge(h.qrx_pkt->datagram_len, (size_t)op->arg0)
1280                 || !TEST_size_t_le(h.qrx_pkt->datagram_len, (size_t)op->arg1))
1281                 goto err;
1282             break;
1283         case OPK_EXPECT_FRAME:
1284             if (!TEST_uint64_t_eq(h.frame_type, op->arg0))
1285                 goto err;
1286             break;
1287         case OPK_EXPECT_INITIAL_TOKEN:
1288             if (!TEST_mem_eq(h.qrx_pkt->hdr->token, h.qrx_pkt->hdr->token_len,
1289                              op->buf, (size_t)op->arg0))
1290                 goto err;
1291             break;
1292         case OPK_EXPECT_HDR:
1293             if (!TEST_true(cmp_pkt_hdr(h.qrx_pkt->hdr, op->buf,
1294                                        NULL, 0, 0)))
1295                 goto err;
1296             break;
1297         case OPK_CHECK:
1298             if (!TEST_true(op->check_func(&h)))
1299                 goto err;
1300             break;
1301         case OPK_NEXT_FRAME:
1302             skip_padding(&h);
1303             if (!ossl_quic_wire_peek_frame_header(&h.pkt, &h.frame_type, NULL)) {
1304                 h.frame_type = UINT64_MAX;
1305                 break;
1306             }
1307
1308             switch (h.frame_type) {
1309             case OSSL_QUIC_FRAME_TYPE_HANDSHAKE_DONE:
1310                 if (!TEST_true(ossl_quic_wire_decode_frame_handshake_done(&h.pkt)))
1311                     goto err;
1312                 break;
1313             case OSSL_QUIC_FRAME_TYPE_PING:
1314                 if (!TEST_true(ossl_quic_wire_decode_frame_ping(&h.pkt)))
1315                     goto err;
1316                 break;
1317             case OSSL_QUIC_FRAME_TYPE_MAX_DATA:
1318                 if (!TEST_true(ossl_quic_wire_decode_frame_max_data(&h.pkt,
1319                                                                     &h.frame.max_data)))
1320                     goto err;
1321                 break;
1322             case OSSL_QUIC_FRAME_TYPE_NEW_CONN_ID:
1323                 if (!TEST_true(ossl_quic_wire_decode_frame_new_conn_id(&h.pkt,
1324                                                                        &h.frame.new_conn_id)))
1325                     goto err;
1326                 break;
1327             case OSSL_QUIC_FRAME_TYPE_NEW_TOKEN:
1328                 if (!TEST_true(ossl_quic_wire_decode_frame_new_token(&h.pkt,
1329                                                                      &h.frame.new_token.token,
1330                                                                      &h.frame.new_token.token_len)))
1331                     goto err;
1332                 break;
1333             case OSSL_QUIC_FRAME_TYPE_ACK_WITH_ECN:
1334             case OSSL_QUIC_FRAME_TYPE_ACK_WITHOUT_ECN:
1335                 h.frame.ack.ack_ranges      = h.ack_ranges;
1336                 h.frame.ack.num_ack_ranges  = OSSL_NELEM(h.ack_ranges);
1337                 if (!TEST_true(ossl_quic_wire_decode_frame_ack(&h.pkt,
1338                                                                h.args.ack_delay_exponent,
1339                                                                &h.frame.ack,
1340                                                                NULL)))
1341                     goto err;
1342                 break;
1343             case OSSL_QUIC_FRAME_TYPE_CRYPTO:
1344                 if (!TEST_true(ossl_quic_wire_decode_frame_crypto(&h.pkt, 0, &h.frame.crypto)))
1345                     goto err;
1346                 break;
1347
1348             case OSSL_QUIC_FRAME_TYPE_STREAM:
1349             case OSSL_QUIC_FRAME_TYPE_STREAM_FIN:
1350             case OSSL_QUIC_FRAME_TYPE_STREAM_LEN:
1351             case OSSL_QUIC_FRAME_TYPE_STREAM_LEN_FIN:
1352             case OSSL_QUIC_FRAME_TYPE_STREAM_OFF:
1353             case OSSL_QUIC_FRAME_TYPE_STREAM_OFF_FIN:
1354             case OSSL_QUIC_FRAME_TYPE_STREAM_OFF_LEN:
1355             case OSSL_QUIC_FRAME_TYPE_STREAM_OFF_LEN_FIN:
1356                 if (!TEST_true(ossl_quic_wire_decode_frame_stream(&h.pkt, 0, &h.frame.stream)))
1357                     goto err;
1358                 break;
1359
1360             case OSSL_QUIC_FRAME_TYPE_STOP_SENDING:
1361                 if (!TEST_true(ossl_quic_wire_decode_frame_stop_sending(&h.pkt,
1362                                                                         &h.frame.stop_sending)))
1363                     goto err;
1364                 break;
1365
1366             case OSSL_QUIC_FRAME_TYPE_RESET_STREAM:
1367                 if (!TEST_true(ossl_quic_wire_decode_frame_reset_stream(&h.pkt,
1368                                                                         &h.frame.reset_stream)))
1369                     goto err;
1370                 break;
1371
1372             case OSSL_QUIC_FRAME_TYPE_CONN_CLOSE_TRANSPORT:
1373             case OSSL_QUIC_FRAME_TYPE_CONN_CLOSE_APP:
1374                 if (!TEST_true(ossl_quic_wire_decode_frame_conn_close(&h.pkt,
1375                                                                       &h.frame.conn_close)))
1376                     goto err;
1377                 break;
1378
1379             default:
1380                 TEST_error("unknown frame type");
1381                 goto err;
1382             }
1383             break;
1384         case OPK_EXPECT_NO_FRAME:
1385             skip_padding(&h);
1386             if (!TEST_size_t_eq(PACKET_remaining(&h.pkt), 0))
1387                 goto err;
1388             break;
1389         case OPK_PROVIDE_SECRET:
1390             if (!TEST_true(ossl_qtx_provide_secret(h.args.qtx,
1391                                                    (uint32_t)op->arg0,
1392                                                    (uint32_t)op->arg1,
1393                                                    NULL, op->buf, op->buf_len)))
1394                 goto err;
1395             if (!TEST_true(ossl_qrx_provide_secret(h.qrx,
1396                                                    (uint32_t)op->arg0,
1397                                                    (uint32_t)op->arg1,
1398                                                    NULL, op->buf, op->buf_len)))
1399                 goto err;
1400             break;
1401         case OPK_DISCARD_EL:
1402             if (!TEST_true(ossl_quic_tx_packetiser_discard_enc_level(h.txp,
1403                                                                      (uint32_t)op->arg0)))
1404                 goto err;
1405             /*
1406              * We do not discard on the QRX here, the object is to test the
1407              * TXP so if the TXP does erroneously send at a discarded EL we
1408              * want to know about it.
1409              */
1410             break;
1411         case OPK_CRYPTO_SEND:
1412             {
1413                 size_t consumed = 0;
1414
1415                 if (!TEST_true(ossl_quic_sstream_append(h.args.crypto[op->arg0],
1416                                                         op->buf, op->buf_len,
1417                                                         &consumed)))
1418                     goto err;
1419
1420                 if (!TEST_size_t_eq(consumed, op->buf_len))
1421                     goto err;
1422             }
1423             break;
1424         case OPK_STREAM_NEW:
1425             {
1426                 QUIC_STREAM *s;
1427
1428                 if (!TEST_ptr(s = ossl_quic_stream_map_alloc(h.args.qsm, op->arg0,
1429                                                              QUIC_STREAM_DIR_BIDI)))
1430                     goto err;
1431
1432                 if (!TEST_ptr(s->sstream = ossl_quic_sstream_new(512 * 1024))
1433                     || !TEST_true(ossl_quic_txfc_init(&s->txfc, &h.conn_txfc))
1434                     || !TEST_true(ossl_quic_rxfc_init(&s->rxfc, &h.conn_rxfc,
1435                                                       1 * 1024 * 1024,
1436                                                       16 * 1024 * 1024,
1437                                                       fake_now, NULL))
1438                     || !TEST_ptr(s->rstream = ossl_quic_rstream_new(&s->rxfc,
1439                                                                     NULL, 1024))) {
1440                     ossl_quic_sstream_free(s->sstream);
1441                     ossl_quic_stream_map_release(h.args.qsm, s);
1442                     goto err;
1443                 }
1444             }
1445             break;
1446         case OPK_STREAM_SEND:
1447             {
1448                 QUIC_STREAM *s;
1449                 size_t consumed = 0;
1450
1451                 if (!TEST_ptr(s = ossl_quic_stream_map_get_by_id(h.args.qsm,
1452                                                                  op->arg0)))
1453                     goto err;
1454
1455                 if (!TEST_true(ossl_quic_sstream_append(s->sstream, op->buf,
1456                                                         op->buf_len, &consumed)))
1457                     goto err;
1458
1459                 if (!TEST_size_t_eq(consumed, op->buf_len))
1460                     goto err;
1461
1462                 ossl_quic_stream_map_update_state(h.args.qsm, s);
1463             }
1464             break;
1465         case OPK_STREAM_FIN:
1466             {
1467                 QUIC_STREAM *s;
1468
1469                 if (!TEST_ptr(s = ossl_quic_stream_map_get_by_id(h.args.qsm,
1470                                                                  op->arg0)))
1471                     goto err;
1472
1473                 ossl_quic_sstream_fin(s->sstream);
1474             }
1475             break;
1476         case OPK_STOP_SENDING:
1477             {
1478                 QUIC_STREAM *s;
1479
1480                 if (!TEST_ptr(s = ossl_quic_stream_map_get_by_id(h.args.qsm,
1481                                                                  op->arg0)))
1482                     goto err;
1483
1484                 if (!TEST_true(ossl_quic_stream_map_stop_sending_recv_part(h.args.qsm,
1485                                                                            s, op->arg1)))
1486                     goto err;
1487
1488                 ossl_quic_stream_map_update_state(h.args.qsm, s);
1489
1490                 if (!TEST_true(s->active))
1491                     goto err;
1492             }
1493             break;
1494         case OPK_RESET_STREAM:
1495             {
1496                 QUIC_STREAM *s;
1497
1498                 if (!TEST_ptr(s = ossl_quic_stream_map_get_by_id(h.args.qsm,
1499                                                                  op->arg0)))
1500                     goto err;
1501
1502                 if (!TEST_true(ossl_quic_stream_map_reset_stream_send_part(h.args.qsm,
1503                                                                            s, op->arg1)))
1504                     goto err;
1505
1506                 ossl_quic_stream_map_update_state(h.args.qsm, s);
1507
1508                 if (!TEST_true(s->active))
1509                     goto err;
1510             }
1511             break;
1512         case OPK_CONN_TXFC_BUMP:
1513             if (!TEST_true(ossl_quic_txfc_bump_cwm(h.args.conn_txfc, op->arg0)))
1514                 goto err;
1515
1516             break;
1517         case OPK_STREAM_TXFC_BUMP:
1518             {
1519                 QUIC_STREAM *s;
1520
1521                 if (!TEST_ptr(s = ossl_quic_stream_map_get_by_id(h.args.qsm,
1522                                                                  op->arg1)))
1523                     goto err;
1524
1525                 if (!TEST_true(ossl_quic_txfc_bump_cwm(&s->txfc, op->arg0)))
1526                     goto err;
1527
1528                 ossl_quic_stream_map_update_state(h.args.qsm, s);
1529             }
1530             break;
1531         case OPK_HANDSHAKE_COMPLETE:
1532             ossl_quic_tx_packetiser_notify_handshake_complete(h.txp);
1533             break;
1534         case OPK_NOP:
1535             break;
1536         default:
1537             TEST_error("bad opcode");
1538             goto err;
1539         }
1540     }
1541
1542     testresult = 1;
1543 err:
1544     if (!testresult)
1545         TEST_error("script %d failed at op %zu", script_idx + 1, opn + 1);
1546     if (have_helper)
1547         helper_cleanup(&h);
1548     return testresult;
1549 }
1550
1551 static int test_script(int idx)
1552 {
1553     return run_script(idx, scripts[idx]);
1554 }
1555
1556 /*
1557  * Dynamic Script 1.
1558  *
1559  * This script exists to test the interactions between multiple packets (ELs) in
1560  * the same datagram when there is a padding requirement (due to the datagram
1561  * containing an Initial packet). There are boundary cases which are difficult
1562  * to get right so it is important to test this entire space. Examples of such
1563  * edge cases include:
1564  *
1565  * - If we are planning on generating both an Initial and Handshake packet in a
1566  *   datagram ordinarily we would plan on adding the padding frames to meet the
1567  *   mandatory minimum size to the last packet in the datagram (i.e., the
1568  *   Handshake packet). But if the amount of room remaining in a datagram is
1569  *   e.g. only 3 bytes after generating the Initial packet, this is not
1570  *   enough room for another packet and we have a problem as having finished
1571  *   the Initial packet we have no way to add the necessary padding.
1572  *
1573  * - If we do have room for another packet but it is not enough room to encode
1574  *   any desired frame.
1575  *
1576  * This test confirms we handle these cases correctly for multi-packet datagrams
1577  * by placing two packets in a datagram and varying the size of the first
1578  * datagram.
1579  */
1580 static const unsigned char dyn_script_1_crypto_1a[1200];
1581 static const unsigned char dyn_script_1_crypto_1b[1];
1582
1583 static int check_is_initial(struct helper *h)
1584 {
1585     return h->qrx_pkt->hdr->type == QUIC_PKT_TYPE_INITIAL;
1586 }
1587
1588 static int check_is_handshake(struct helper *h)
1589 {
1590     return h->qrx_pkt->hdr->type == QUIC_PKT_TYPE_HANDSHAKE;
1591 }
1592
1593 static struct script_op dyn_script_1[] = {
1594     OP_PROVIDE_SECRET(QUIC_ENC_LEVEL_INITIAL, QRL_SUITE_AES128GCM, secret_1)
1595     OP_PROVIDE_SECRET(QUIC_ENC_LEVEL_HANDSHAKE, QRL_SUITE_AES128GCM, secret_1)
1596     OP_TXP_GENERATE_NONE()
1597     OP_CRYPTO_SEND(QUIC_PN_SPACE_INITIAL, dyn_script_1_crypto_1a) /* [crypto_idx] */
1598     OP_CRYPTO_SEND(QUIC_PN_SPACE_HANDSHAKE, dyn_script_1_crypto_1b)
1599     OP_TXP_GENERATE()
1600     OP_RX_PKT()
1601     OP_EXPECT_DGRAM_LEN(1200, 1200)
1602     OP_CHECK(check_is_initial)
1603     OP_NOP() /* [pkt_idx] */
1604     OP_NOP() /* [check_idx] */
1605     OP_END
1606 };
1607
1608 static const size_t dyn_script_1_crypto_idx     = 3;
1609 static const size_t dyn_script_1_pkt_idx        = 9;
1610 static const size_t dyn_script_1_check_idx      = 10;
1611 static const size_t dyn_script_1_start_from     = 1000;
1612
1613 static int test_dyn_script_1(int idx)
1614 {
1615     size_t target_size = dyn_script_1_start_from + (size_t)idx;
1616     int expect_handshake_pkt_in_same_dgram = (target_size <= 1115);
1617
1618     dyn_script_1[dyn_script_1_crypto_idx].buf_len = target_size;
1619
1620     if (expect_handshake_pkt_in_same_dgram) {
1621         dyn_script_1[dyn_script_1_pkt_idx].opcode       = OPK_RX_PKT;
1622         dyn_script_1[dyn_script_1_check_idx].opcode     = OPK_CHECK;
1623         dyn_script_1[dyn_script_1_check_idx].check_func = check_is_handshake;
1624     } else {
1625         dyn_script_1[dyn_script_1_pkt_idx].opcode       = OPK_RX_PKT_NONE;
1626         dyn_script_1[dyn_script_1_check_idx].opcode     = OPK_NOP;
1627     }
1628
1629     if (!run_script(idx, dyn_script_1)) {
1630         TEST_error("failed dyn script 1 with target size %zu", target_size);
1631         return 0;
1632     }
1633
1634     return 1;
1635 }
1636
1637 int setup_tests(void)
1638 {
1639     ADD_ALL_TESTS(test_script, OSSL_NELEM(scripts));
1640     ADD_ALL_TESTS(test_dyn_script_1,
1641                   OSSL_NELEM(dyn_script_1_crypto_1a)
1642                   - dyn_script_1_start_from + 1);
1643     return 1;
1644 }