Commit 07015b0a7b for openssl.org

commit 07015b0a7b2562c7f2dd1bb5019848b2a1abd866
Author: Liu Dongmiao <473522+liudongmiao@users.noreply.github.com>
Date:   Sat Nov 21 06:10:24 2026 +0800

    quic: test Initial PTO probes and stream data sent twice

    Add quic_txp_test scripts for Initial PTO probes with CRYPTO data of
    four, two and one packets in flight, with an acknowledged gap, with a
    tail that has never been sent, with lost data queued before or after
    data in flight, and with the second probe deferred until
    anti-amplification credit is available. Check that the probes carry
    consecutive CRYPTO data, that nothing is sent after them, and that a
    second PTO counts a pending probe towards the two. Also check that
    discarding the Initial space clears its pending PTO and anti-deadlock
    probes.

    Add test_sstream_two_copies to quic_stream_test. One copy of the data is
    ACKed and the other is declared lost, in either order, with full and
    partial ACKs. Add test_sstream_ack_merge for an ACK that overlaps one
    ACKed range and touches the next.

    Co-authored-by: Mounir IDRASSI <mounir.idrassi@idrix.fr>
    Assisted-by: Claude:claude-opus-5-5
    Reviewed-by: Saša NedvÄ›dický <sashan@openssl.org>
    Reviewed-by: Neil Horman <nhorman@openssl.org>
    Merge-date: Fri Dec  4 13:55:46 2026
    Merged-from: https://github.com/openssl/openssl/pull/33782

diff --git a/test/quic_stream_test.c b/test/quic_stream_test.c
index 9eeefad1e1..075a3c8a5e 100644
--- a/test/quic_stream_test.c
+++ b/test/quic_stream_test.c
@@ -378,6 +378,97 @@ err:
     return testresult;
 }

+static const unsigned char data_2[] = "abcdefghijklmnopqrstuvwxy";
+
+/* A length of 0 expects no frame at idx */
+static int check_stream_frame(QUIC_SSTREAM *sstream, size_t idx,
+    uint64_t offset, uint64_t len)
+{
+    OSSL_QUIC_FRAME_STREAM hdr;
+    OSSL_QTX_IOVEC iov[2];
+    size_t i, num_iov = OSSL_NELEM(iov), total = 0;
+    int got;
+
+    got = ossl_quic_sstream_get_stream_frame(sstream, idx, &hdr, iov, &num_iov);
+    if (len == 0)
+        return TEST_false(got);
+
+    if (!TEST_true(got)
+        || !TEST_uint64_t_eq(hdr.offset, offset)
+        || !TEST_uint64_t_eq(hdr.len, len))
+        return 0;
+
+    for (i = 0; i < num_iov; ++i) {
+        if (!TEST_mem_eq(iov[i].buf, iov[i].buf_len,
+                data_2 + offset + total, iov[i].buf_len))
+            return 0;
+
+        total += iov[i].buf_len;
+    }
+
+    return TEST_uint64_t_eq(total, len);
+}
+
+/*
+ * One copy of the data is ACKed and another is declared lost, in either order,
+ * as when a PTO probe resends data that is still in flight.
+ */
+static int test_sstream_two_copies(int idx)
+{
+    int testresult = 0, ack_first = idx & 1, partial = idx & 2;
+    QUIC_SSTREAM *sstream = NULL;
+    size_t consumed = 0;
+
+    if (!TEST_ptr(sstream = ossl_quic_sstream_new(32))
+        || !TEST_true(ossl_quic_sstream_append(sstream, data_2, 24, &consumed))
+        || !TEST_size_t_eq(consumed, 24)
+        || !TEST_true(ossl_quic_sstream_mark_transmitted(sstream, 0, 23))
+        || !TEST_true(ossl_quic_sstream_mark_lost(sstream, 0, 23))
+        || !TEST_true(ossl_quic_sstream_mark_transmitted(sstream, 0, 23)))
+        goto err;
+
+    if (!ack_first && !TEST_true(ossl_quic_sstream_mark_lost(sstream, 0, 23)))
+        goto err;
+
+    if (partial) {
+        if (!TEST_true(ossl_quic_sstream_mark_acked(sstream, 0, 5))
+            || !TEST_true(ossl_quic_sstream_mark_acked(sstream, 12, 15)))
+            goto err;
+    } else if (!TEST_true(ossl_quic_sstream_mark_acked(sstream, 0, 23))) {
+        goto err;
+    }
+
+    if (ack_first && !TEST_true(ossl_quic_sstream_mark_lost(sstream, 0, 23)))
+        goto err;
+
+    /* Only bytes not ACKed are sent again, and the ACKed prefix is culled */
+    if (partial) {
+        if (!check_stream_frame(sstream, 0, 6, 6)
+            || !check_stream_frame(sstream, 1, 16, 8)
+            || !check_stream_frame(sstream, 2, 0, 0)
+            || !TEST_false(ossl_quic_sstream_is_totally_acked(sstream))
+            || !TEST_size_t_eq(ossl_quic_sstream_get_buffer_used(sstream), 18))
+            goto err;
+    } else {
+        if (!check_stream_frame(sstream, 0, 0, 0)
+            || !TEST_true(ossl_quic_sstream_is_totally_acked(sstream))
+            || !TEST_size_t_eq(ossl_quic_sstream_get_buffer_used(sstream), 0))
+            goto err;
+
+        /* New data is sent next, so no culled range is left pending */
+        if (!TEST_true(ossl_quic_sstream_append(sstream, data_2 + 24, 1,
+                &consumed))
+            || !check_stream_frame(sstream, 0, 24, 1)
+            || !check_stream_frame(sstream, 1, 0, 0))
+            goto err;
+    }
+
+    testresult = 1;
+err:
+    ossl_quic_sstream_free(sstream);
+    return testresult;
+}
+
 static int test_single_copy_read(QUIC_RSTREAM *qrs,
     unsigned char *buf, size_t size,
     size_t *readbytes, int *fin)
@@ -3558,6 +3649,7 @@ int setup_tests(void)
 {
     ADD_TEST(test_sstream_simple);
     ADD_ALL_TESTS(test_sstream_bulk, 100);
+    ADD_ALL_TESTS(test_sstream_two_copies, 4);
     ADD_ALL_TESTS(test_rstream_simple, 2);
     ADD_ALL_TESTS(test_rstream_random, 100);
     ADD_TEST(test_rstream_pkt);
diff --git a/test/quic_txp_test.c b/test/quic_txp_test.c
index 38006e7e3d..f52d9f9ef3 100644
--- a/test/quic_txp_test.c
+++ b/test/quic_txp_test.c
@@ -1685,6 +1685,409 @@ static int test_script(int idx)
     return run_script(idx, scripts[idx]);
 }

+/*
+ * Helpers for the Initial PTO scripts. The byte at offset o of their CRYPTO
+ * data is o % 251, so each probe payload can be checked against its offset.
+ * crypto_probe_end is the end of the CRYPTO data carried by the probes so far.
+ */
+static uint64_t crypto_probe_end;
+
+static int check_is_initial(struct helper *h);
+static int check_is_handshake(struct helper *h);
+
+static void fill_pto_crypto(unsigned char *buf, size_t len)
+{
+    size_t i;
+
+    for (i = 0; i < len; ++i)
+        buf[i] = (unsigned char)(i % 251);
+}
+
+static int check_crypto_data(struct helper *h)
+{
+    size_t i;
+
+    if (!TEST_uint64_t_gt(h->frame.crypto.len, 0))
+        return 0;
+
+    for (i = 0; i < h->frame.crypto.len; ++i)
+        if (!TEST_uchar_eq(h->frame.crypto.data[i],
+                (unsigned char)((h->frame.crypto.offset + i) % 251)))
+            return 0;
+
+    return 1;
+}
+
+static int trigger_pto(struct helper *h)
+{
+    return TEST_true(ossl_ackm_on_timeout(h->args.ackm));
+}
+
+static int check_first_probe(struct helper *h)
+{
+    crypto_probe_end = h->frame.crypto.offset + h->frame.crypto.len;
+    return TEST_uint64_t_eq(h->frame.crypto.offset, 0)
+        && check_crypto_data(h);
+}
+
+static int check_next_probe(struct helper *h)
+{
+    if (!TEST_uint64_t_eq(h->frame.crypto.offset, crypto_probe_end)
+        || !check_crypto_data(h))
+        return 0;
+
+    crypto_probe_end += h->frame.crypto.len;
+    return 1;
+}
+
+static int check_nothing_sent(struct helper *h)
+{
+    QUIC_TXP_STATUS status;
+
+    return TEST_true(ossl_quic_tx_packetiser_generate(h->txp, &status))
+        && TEST_size_t_eq(status.sent_pkt, 0);
+}
+
+/* TODO: replace with flush_qtx once #34049 is merged into 3.5 and 4.0 */
+static int pto_flush_qtx(struct helper *h)
+{
+    ossl_qtx_finish_dgram(h->args.qtx);
+    ossl_qtx_flush_net(h->args.qtx);
+    return 1;
+}
+
+/* Initial PTO with four packets of CRYPTO data in flight */
+static unsigned char pto_crypto_1[3600];
+
+static const struct script_op pto_script_1[] = {
+    { OPK_PROVIDE_SECRET, QUIC_ENC_LEVEL_INITIAL, QRL_SUITE_AES128GCM,
+        secret_1, sizeof(secret_1) },
+    { OPK_CRYPTO_SEND, QUIC_PN_SPACE_INITIAL, 0, pto_crypto_1,
+        sizeof(pto_crypto_1) },
+    { OPK_TXP_GENERATE },
+    { OPK_RX_PKT },
+    { OPK_TXP_GENERATE },
+    { OPK_RX_PKT },
+    { OPK_TXP_GENERATE },
+    { OPK_RX_PKT },
+    { OPK_TXP_GENERATE },
+    { OPK_RX_PKT },
+    { OPK_CHECK, 0, 0, NULL, 0, trigger_pto },
+    /* two probes carry consecutive data */
+    { OPK_TXP_GENERATE },
+    { OPK_RX_PKT },
+    { OPK_NEXT_FRAME },
+    { OPK_EXPECT_FRAME, OSSL_QUIC_FRAME_TYPE_CRYPTO },
+    { OPK_CHECK, 0, 0, NULL, 0, check_first_probe },
+    { OPK_EXPECT_NO_FRAME },
+    { OPK_TXP_GENERATE },
+    { OPK_RX_PKT },
+    { OPK_NEXT_FRAME },
+    { OPK_EXPECT_FRAME, OSSL_QUIC_FRAME_TYPE_CRYPTO },
+    { OPK_CHECK, 0, 0, NULL, 0, check_next_probe },
+    { OPK_EXPECT_NO_FRAME },
+    /* the rest of pto_crypto_1 stays in flight */
+    { OPK_CHECK, 0, 0, NULL, 0, check_nothing_sent },
+    { OPK_CHECK, 0, 0, NULL, 0, pto_flush_qtx },
+    { OPK_RX_PKT_NONE },
+    { OPK_END }
+};
+
+/* Initial PTO with two packets of CRYPTO data in flight */
+static unsigned char pto_crypto_2[2312];
+
+static int check_all_sent_2(struct helper *h)
+{
+    return TEST_uint64_t_eq(crypto_probe_end, sizeof(pto_crypto_2));
+}
+
+static const struct script_op pto_script_2[] = {
+    { OPK_PROVIDE_SECRET, QUIC_ENC_LEVEL_INITIAL, QRL_SUITE_AES128GCM,
+        secret_1, sizeof(secret_1) },
+    { OPK_CRYPTO_SEND, QUIC_PN_SPACE_INITIAL, 0, pto_crypto_2,
+        sizeof(pto_crypto_2) },
+    { OPK_TXP_GENERATE },
+    { OPK_RX_PKT },
+    { OPK_TXP_GENERATE },
+    { OPK_RX_PKT },
+    { OPK_CHECK, 0, 0, NULL, 0, trigger_pto },
+    { OPK_TXP_GENERATE },
+    { OPK_RX_PKT },
+    { OPK_NEXT_FRAME },
+    { OPK_EXPECT_FRAME, OSSL_QUIC_FRAME_TYPE_CRYPTO },
+    { OPK_CHECK, 0, 0, NULL, 0, check_first_probe },
+    { OPK_EXPECT_NO_FRAME },
+    { OPK_TXP_GENERATE },
+    { OPK_RX_PKT },
+    { OPK_NEXT_FRAME },
+    { OPK_EXPECT_FRAME, OSSL_QUIC_FRAME_TYPE_CRYPTO },
+    { OPK_CHECK, 0, 0, NULL, 0, check_next_probe },
+    { OPK_EXPECT_NO_FRAME },
+    /* together, the two probes carry all the CRYPTO data */
+    { OPK_CHECK, 0, 0, NULL, 0, check_all_sent_2 },
+    { OPK_CHECK, 0, 0, NULL, 0, check_nothing_sent },
+    { OPK_CHECK, 0, 0, NULL, 0, pto_flush_qtx },
+    { OPK_RX_PKT_NONE },
+    /* the probes covered everything, so a second PTO resends from the start */
+    { OPK_CHECK, 0, 0, NULL, 0, trigger_pto },
+    { OPK_TXP_GENERATE },
+    { OPK_RX_PKT },
+    { OPK_NEXT_FRAME },
+    { OPK_EXPECT_FRAME, OSSL_QUIC_FRAME_TYPE_CRYPTO },
+    { OPK_CHECK, 0, 0, NULL, 0, check_first_probe },
+    { OPK_EXPECT_NO_FRAME },
+    { OPK_CHECK, 0, 0, NULL, 0, pto_flush_qtx },
+    { OPK_RX_PKT_NONE },
+    { OPK_END }
+};
+
+/* Initial PTO with an acknowledged gap in the CRYPTO data */
+static unsigned char pto_crypto_3[64];
+
+static int ack_gap_3(struct helper *h)
+{
+    return TEST_true(ossl_quic_sstream_mark_acked(
+        h->args.crypto[QUIC_PN_SPACE_INITIAL], 16, 31));
+}
+
+static int check_before_gap_3(struct helper *h)
+{
+    return TEST_uint64_t_eq(h->frame.crypto.offset, 0)
+        && TEST_uint64_t_eq(h->frame.crypto.len, 16)
+        && check_crypto_data(h);
+}
+
+static int check_after_gap_3(struct helper *h)
+{
+    return TEST_uint64_t_eq(h->frame.crypto.offset, 32)
+        && TEST_uint64_t_eq(h->frame.crypto.len, 32)
+        && check_crypto_data(h);
+}
+
+static const struct script_op pto_script_3[] = {
+    { OPK_PROVIDE_SECRET, QUIC_ENC_LEVEL_INITIAL, QRL_SUITE_AES128GCM,
+        secret_1, sizeof(secret_1) },
+    { OPK_CRYPTO_SEND, QUIC_PN_SPACE_INITIAL, 0, pto_crypto_3,
+        sizeof(pto_crypto_3) },
+    { OPK_TXP_GENERATE },
+    { OPK_RX_PKT },
+    { OPK_CHECK, 0, 0, NULL, 0, ack_gap_3 },
+    { OPK_CHECK, 0, 0, NULL, 0, trigger_pto },
+    /* both ranges around the gap go into the first probe */
+    { OPK_TXP_GENERATE },
+    { OPK_RX_PKT },
+    { OPK_NEXT_FRAME },
+    { OPK_EXPECT_FRAME, OSSL_QUIC_FRAME_TYPE_CRYPTO },
+    { OPK_CHECK, 0, 0, NULL, 0, check_before_gap_3 },
+    { OPK_NEXT_FRAME },
+    { OPK_EXPECT_FRAME, OSSL_QUIC_FRAME_TYPE_CRYPTO },
+    { OPK_CHECK, 0, 0, NULL, 0, check_after_gap_3 },
+    { OPK_EXPECT_NO_FRAME },
+    { OPK_CHECK, 0, 0, NULL, 0, check_nothing_sent },
+    { OPK_CHECK, 0, 0, NULL, 0, pto_flush_qtx },
+    { OPK_RX_PKT_NONE },
+    { OPK_END }
+};
+
+/* Initial PTO with a CRYPTO tail that has never been sent */
+static unsigned char pto_crypto_4[2377];
+
+static int check_tail_4(struct helper *h)
+{
+    return TEST_uint64_t_eq(h->frame.crypto.offset, 2313)
+        && TEST_uint64_t_eq(h->frame.crypto.len, 64)
+        && check_crypto_data(h);
+}
+
+static const struct script_op pto_script_4[] = {
+    { OPK_PROVIDE_SECRET, QUIC_ENC_LEVEL_INITIAL, QRL_SUITE_AES128GCM,
+        secret_1, sizeof(secret_1) },
+    { OPK_CRYPTO_SEND, QUIC_PN_SPACE_INITIAL, 0, pto_crypto_4, 2313 },
+    { OPK_TXP_GENERATE },
+    { OPK_RX_PKT },
+    { OPK_TXP_GENERATE },
+    { OPK_RX_PKT },
+    /* the tail is queued but not sent before the PTO */
+    { OPK_CRYPTO_SEND, QUIC_PN_SPACE_INITIAL, 0, pto_crypto_4 + 2313, 64 },
+    { OPK_CHECK, 0, 0, NULL, 0, trigger_pto },
+    /* the first probe sends the queued tail */
+    { OPK_TXP_GENERATE },
+    { OPK_RX_PKT },
+    { OPK_NEXT_FRAME },
+    { OPK_EXPECT_FRAME, OSSL_QUIC_FRAME_TYPE_CRYPTO },
+    { OPK_CHECK, 0, 0, NULL, 0, check_tail_4 },
+    { OPK_EXPECT_NO_FRAME },
+    /* the second probe resends the unacknowledged data from the start */
+    { OPK_TXP_GENERATE },
+    { OPK_RX_PKT },
+    { OPK_NEXT_FRAME },
+    { OPK_EXPECT_FRAME, OSSL_QUIC_FRAME_TYPE_CRYPTO },
+    { OPK_CHECK, 0, 0, NULL, 0, check_first_probe },
+    { OPK_EXPECT_NO_FRAME },
+    { OPK_CHECK, 0, 0, NULL, 0, check_nothing_sent },
+    { OPK_CHECK, 0, 0, NULL, 0, pto_flush_qtx },
+    { OPK_RX_PKT_NONE },
+    { OPK_END }
+};
+
+/* Initial PTO with the Handshake keys available and Handshake data queued */
+static int check_handshake_5(struct helper *h)
+{
+    return TEST_uint64_t_eq(h->frame.crypto.offset, 0)
+        && TEST_uint64_t_eq(h->frame.crypto.len, sizeof(pto_crypto_3))
+        && check_crypto_data(h);
+}
+
+static int check_no_pto_pending(struct helper *h)
+{
+    OSSL_ACKM_PROBE_INFO *probe = ossl_ackm_get0_probe_request(h->args.ackm);
+
+    return TEST_uint_eq(probe->pto[QUIC_PN_SPACE_INITIAL], 0);
+}
+
+static const struct script_op pto_script_5[] = {
+    { OPK_PROVIDE_SECRET, QUIC_ENC_LEVEL_INITIAL, QRL_SUITE_AES128GCM,
+        secret_1, sizeof(secret_1) },
+    { OPK_PROVIDE_SECRET, QUIC_ENC_LEVEL_HANDSHAKE, QRL_SUITE_AES128GCM,
+        secret_1, sizeof(secret_1) },
+    { OPK_CRYPTO_SEND, QUIC_PN_SPACE_INITIAL, 0, pto_crypto_3,
+        sizeof(pto_crypto_3) },
+    { OPK_TXP_GENERATE },
+    { OPK_RX_PKT },
+    /* the Handshake data is queued but not sent before the PTO */
+    { OPK_CRYPTO_SEND, QUIC_PN_SPACE_HANDSHAKE, 0, pto_crypto_3,
+        sizeof(pto_crypto_3) },
+    { OPK_CHECK, 0, 0, NULL, 0, trigger_pto },
+    /* the Initial probe leaves the datagram to the queued Handshake data */
+    { OPK_TXP_GENERATE },
+    { OPK_RX_PKT },
+    { OPK_CHECK, 0, 0, NULL, 0, check_is_initial },
+    { OPK_NEXT_FRAME },
+    { OPK_EXPECT_FRAME, OSSL_QUIC_FRAME_TYPE_PING },
+    { OPK_EXPECT_NO_FRAME },
+    { OPK_RX_PKT },
+    { OPK_CHECK, 0, 0, NULL, 0, check_is_handshake },
+    { OPK_NEXT_FRAME },
+    { OPK_EXPECT_FRAME, OSSL_QUIC_FRAME_TYPE_CRYPTO },
+    { OPK_CHECK, 0, 0, NULL, 0, check_handshake_5 },
+    { OPK_EXPECT_NO_FRAME },
+    /* a single probe, as the Handshake keys are available */
+    { OPK_CHECK, 0, 0, NULL, 0, check_no_pto_pending },
+    { OPK_END }
+};
+
+/* TODO: drop the #ifdef once #34049 is merged into 3.5 and 4.0 */
+#ifdef OPK_NO_VALIDATION
+/* Initial PTO probes when anti-amplification credit runs out */
+static int rx_initial_6(struct helper *h)
+{
+    OSSL_ACKM_RX_PKT pkt = { 0 };
+
+    pkt.time = fake_now(NULL);
+    pkt.pkt_space = QUIC_PN_SPACE_INITIAL;
+    pkt.is_ack_eliciting = 1;
+    return TEST_true(ossl_ackm_on_rx_packet(h->args.ackm, &pkt));
+}
+
+static const struct script_op pto_script_6[] = {
+    { OPK_NO_VALIDATION },
+    { OPK_PROVIDE_SECRET, QUIC_ENC_LEVEL_INITIAL, QRL_SUITE_AES128GCM,
+        secret_1, sizeof(secret_1) },
+    /* 3 * 1600 = 4800 covers the four datagrams of pto_crypto_1 */
+    { OPK_ADD_CREDIT, 1600 },
+    { OPK_CRYPTO_SEND, QUIC_PN_SPACE_INITIAL, 0, pto_crypto_1,
+        sizeof(pto_crypto_1) },
+    { OPK_TXP_GENERATE },
+    { OPK_RX_PKT },
+    { OPK_TXP_GENERATE },
+    { OPK_RX_PKT },
+    { OPK_TXP_GENERATE },
+    { OPK_RX_PKT },
+    { OPK_TXP_GENERATE },
+    { OPK_RX_PKT },
+    { OPK_CHECK, 0, 0, NULL, 0, trigger_pto },
+    /* credit for one datagram, so the second probe is not sent yet */
+    { OPK_ADD_CREDIT, 400 },
+    { OPK_TXP_GENERATE },
+    { OPK_RX_PKT },
+    { OPK_NEXT_FRAME },
+    { OPK_EXPECT_FRAME, OSSL_QUIC_FRAME_TYPE_CRYPTO },
+    { OPK_CHECK, 0, 0, NULL, 0, check_first_probe },
+    { OPK_EXPECT_NO_FRAME },
+    { OPK_CHECK, 0, 0, NULL, 0, check_nothing_sent },
+    { OPK_CHECK, 0, 0, NULL, 0, flush_qtx },
+    { OPK_RX_PKT_NONE },
+    /* receiving an Initial packet adds credit and an ACK to send */
+    { OPK_CHECK, 0, 0, NULL, 0, rx_initial_6 },
+    { OPK_ADD_CREDIT, 1000 },
+    { OPK_TXP_GENERATE },
+    { OPK_RX_PKT },
+    { OPK_NEXT_FRAME },
+    { OPK_EXPECT_FRAME, OSSL_QUIC_FRAME_TYPE_ACK_WITHOUT_ECN },
+    { OPK_NEXT_FRAME },
+    { OPK_EXPECT_FRAME, OSSL_QUIC_FRAME_TYPE_CRYPTO },
+    { OPK_CHECK, 0, 0, NULL, 0, check_next_probe },
+    { OPK_EXPECT_NO_FRAME },
+    /* the rest of pto_crypto_1 stays in flight */
+    { OPK_CHECK, 0, 0, NULL, 0, check_nothing_sent },
+    { OPK_CHECK, 0, 0, NULL, 0, flush_qtx },
+    { OPK_RX_PKT_NONE },
+    { OPK_END }
+};
+
+#endif
+
+/* No Initial CRYPTO in a probe once a CONNECTION_CLOSE is scheduled */
+static const struct script_op pto_script_7[] = {
+    { OPK_PROVIDE_SECRET, QUIC_ENC_LEVEL_INITIAL, QRL_SUITE_AES128GCM,
+        secret_1, sizeof(secret_1) },
+    { OPK_CRYPTO_SEND, QUIC_PN_SPACE_INITIAL, 0, pto_crypto_2,
+        sizeof(pto_crypto_2) },
+    { OPK_TXP_GENERATE },
+    { OPK_RX_PKT },
+    { OPK_TXP_GENERATE },
+    { OPK_RX_PKT },
+    /* the application closes the connection before the PTO expires */
+    { OPK_CHECK, 0, 0, NULL, 0, gen_conn_close },
+    { OPK_TXP_GENERATE },
+    { OPK_RX_PKT },
+    { OPK_NEXT_FRAME },
+    { OPK_EXPECT_FRAME, OSSL_QUIC_FRAME_TYPE_CONN_CLOSE_TRANSPORT },
+    { OPK_EXPECT_NO_FRAME },
+    /*
+     * The PTO expires and yields one PING probe, with no CRYPTO data and no
+     * second probe.
+     */
+    { OPK_CHECK, 0, 0, NULL, 0, trigger_pto },
+    { OPK_TXP_GENERATE },
+    { OPK_RX_PKT },
+    { OPK_NEXT_FRAME },
+    { OPK_EXPECT_FRAME, OSSL_QUIC_FRAME_TYPE_PING },
+    { OPK_EXPECT_NO_FRAME },
+    { OPK_CHECK, 0, 0, NULL, 0, check_nothing_sent },
+    { OPK_CHECK, 0, 0, NULL, 0, pto_flush_qtx },
+    { OPK_RX_PKT_NONE },
+    { OPK_END }
+};
+
+/* TODO: move these into scripts[] once #34049 is merged into 3.5 and 4.0 */
+static const struct script_op *const pto_scripts[] = {
+    pto_script_1,
+    pto_script_2,
+    pto_script_3,
+    pto_script_4,
+    pto_script_5,
+#ifdef OPK_NO_VALIDATION
+    pto_script_6,
+#endif
+    pto_script_7,
+};
+
+static int test_pto_script(int idx)
+{
+    return run_script(idx, pto_scripts[idx]);
+}
+
 /*
  * Dynamic Script 1.
  *
@@ -1768,6 +2171,11 @@ static int test_dyn_script_1(int idx)

 int setup_tests(void)
 {
+    fill_pto_crypto(pto_crypto_1, sizeof(pto_crypto_1));
+    fill_pto_crypto(pto_crypto_2, sizeof(pto_crypto_2));
+    fill_pto_crypto(pto_crypto_3, sizeof(pto_crypto_3));
+    fill_pto_crypto(pto_crypto_4, sizeof(pto_crypto_4));
+    ADD_ALL_TESTS(test_pto_script, OSSL_NELEM(pto_scripts));
     ADD_ALL_TESTS(test_script, OSSL_NELEM(scripts));
     ADD_ALL_TESTS(test_dyn_script_1,
         (int)(OSSL_NELEM(dyn_script_1_crypto_1a)