From c20c71a07919d6553894d390f0f8edbabf2454f4 Mon Sep 17 00:00:00 2001 From: Amaury Denoyelle Date: Thu, 4 Sep 2025 14:53:03 +0200 Subject: [PATCH] TESTS: quic: add unit-tests for QUIC TX part Define a new "quic_tx" unit-test which is used to test QUIC TX module. For the moment, a single test is performed on qc_do_build_pkt(). It checks that PADDING is correctly added for HP sampling in case of a small packet. --- src/quic_tx.c | 59 +++++++++++++++++++++++++++++++++++++++++ tests/unit/quic/quic.sh | 1 + 2 files changed, 60 insertions(+) diff --git a/src/quic_tx.c b/src/quic_tx.c index 4c6d64518..7a2da7eed 100644 --- a/src/quic_tx.c +++ b/src/quic_tx.c @@ -2231,6 +2231,65 @@ static struct quic_tx_packet *qc_build_pkt(unsigned char **pos, TRACE_DEVEL("leaving on error", QUIC_EV_CONN_TXPKT, qc); return NULL; } + +int quic_tx_unittest(int argc, char **argv) +{ + struct quic_conn qc; + struct quic_cc_path path; + struct quic_pktns pktns; + struct quic_enc_level qel; + struct quic_tx_packet pkt; + + unsigned char area[1024]; + unsigned char *end = area + 1024; + + unsigned char *buf_pn; + size_t pn_len; + + struct list frms = LIST_HEAD_INIT(frms); + struct quic_frame frm; + + /* Minimal quic_conn init and linked elements to be able to use + * qc_do_build_pkt() safely. + */ + qc.dcid.len = 8; + qc.scid.len = 8; + + qc.path = &path; + path.prep_in_flight = 0; + path.cwnd = 16384; + + qel.pktns = &pktns; + pktns.rx.largest_acked_pn = 0; + pktns.tx.pto_probe = 0; + pktns.flags = 0; + + /* short header packet */ + pkt.type = QUIC_PACKET_TYPE_SHORT; + LIST_INIT(&pkt.frms); + + /* payload frames content */ + frm.type = QUIC_FT_MAX_STREAMS_BIDI; + frm.max_streams_bidi.max_streams = 4; + LIST_APPEND(&frms, &frm.list); + + /* Built a small packet with a single MAX_STREAMS frame. Padding must + * be added so that PN decoding can be performed by the peer. + */ + qc_do_build_pkt(area, end, 0, &pkt, 0, &pn_len, &buf_pn, + 0, 0, 0, 1, + &qel, &qc, NULL, &frms); + + /* Ensure padding has been added to packet. + * packet length = 1 (short hdlen) + 8 (DCID len) + 1 (PN len) + 3 (payload) + * payload = 2 (MAX_STREAMS frm) + 1 (PADDING) + */ + BUG_ON(pkt.len != 13 || pn_len != 1); + + return 0; +} +REGISTER_UNITTEST("quic_tx", quic_tx_unittest); + /* * Local variables: * c-indent-level: 8 diff --git a/tests/unit/quic/quic.sh b/tests/unit/quic/quic.sh index ecea5d550..e9f05c688 100755 --- a/tests/unit/quic/quic.sh +++ b/tests/unit/quic/quic.sh @@ -6,6 +6,7 @@ check() { run() { ${HAPROXY_PROGRAM} -U quic_enc + ${HAPROXY_PROGRAM} -U quic_tx } case "$1" in