test_reg_pkt_headergen.c revision 49f1f4f6
17d09751dSDmitri Tikhonov/* Copyright (c) 2017 - 2020 LiteSpeed Technologies Inc.  See LICENSE. */
250aadb33SDmitri Tikhonov#include <assert.h>
350aadb33SDmitri Tikhonov#include <stdio.h>
450aadb33SDmitri Tikhonov#include <stdlib.h>
550aadb33SDmitri Tikhonov#include <string.h>
6461e84d8SAmol Deshpande#ifndef WIN32
750aadb33SDmitri Tikhonov#include <sys/time.h>
8461e84d8SAmol Deshpande#endif
95392f7a3SLiteSpeed Tech#include <sys/queue.h>
1050aadb33SDmitri Tikhonov
1150aadb33SDmitri Tikhonov#include "lsquic_types.h"
129626cfc2SDmitri Tikhonov#include "lsquic.h"
135392f7a3SLiteSpeed Tech#include "lsquic_int_types.h"
1450aadb33SDmitri Tikhonov#include "lsquic_packet_common.h"
159626cfc2SDmitri Tikhonov#include "lsquic_packet_out.h"
165392f7a3SLiteSpeed Tech#include "lsquic_hash.h"
179626cfc2SDmitri Tikhonov#include "lsquic_conn.h"
1850aadb33SDmitri Tikhonov#include "lsquic_parse.h"
1950aadb33SDmitri Tikhonov
2050aadb33SDmitri Tikhonovstruct test {
2150aadb33SDmitri Tikhonov    /* Inputs. */
2250aadb33SDmitri Tikhonov    const struct parse_funcs
2350aadb33SDmitri Tikhonov                   *pf;
2450aadb33SDmitri Tikhonov    size_t          bufsz;
255392f7a3SLiteSpeed Tech    uint64_t        cid;    /* Zero means connection ID is not specified */
2650aadb33SDmitri Tikhonov    const char     *nonce;
2750aadb33SDmitri Tikhonov    lsquic_packno_t packno;
28c7d81ce1SDmitri Tikhonov    enum packno_bits
2950aadb33SDmitri Tikhonov                    bits;   /* The test has been retrofitted by adding bits parameter.  The test can
3050aadb33SDmitri Tikhonov                             * be made more complicated by calculating packet number length based on
3150aadb33SDmitri Tikhonov                             * some other inputs.  However, this is tested elsewhere.
3250aadb33SDmitri Tikhonov                             */
3350aadb33SDmitri Tikhonov    union {
3450aadb33SDmitri Tikhonov        unsigned char   buf[4];
3550aadb33SDmitri Tikhonov        lsquic_ver_tag_t    val;
3650aadb33SDmitri Tikhonov    }               ver;
3750aadb33SDmitri Tikhonov
3850aadb33SDmitri Tikhonov    /* Outputs */
3950aadb33SDmitri Tikhonov    int             len;            /* Retval */
4050aadb33SDmitri Tikhonov    char            out[0x100];     /* Contents */
4150aadb33SDmitri Tikhonov};
4250aadb33SDmitri Tikhonov
4350aadb33SDmitri Tikhonov
4450aadb33SDmitri Tikhonovstatic const struct test tests[] = {
4550aadb33SDmitri Tikhonov
4650aadb33SDmitri Tikhonov    {
47fb96f4ddSDmitri Tikhonov        .pf     = select_pf_by_ver(LSQVER_043),
485392f7a3SLiteSpeed Tech        .bufsz  = GQUIC_MAX_PUBHDR_SZ,
4950aadb33SDmitri Tikhonov        .cid    = 0x0102030405060708UL,
5050aadb33SDmitri Tikhonov        .nonce  = NULL,
5150aadb33SDmitri Tikhonov        .packno = 0x01020304,
52c7d81ce1SDmitri Tikhonov        .bits   = GQUIC_PACKNO_LEN_4,
5350aadb33SDmitri Tikhonov        .len    = 1 + 8 + 0 + 4,
5450aadb33SDmitri Tikhonov        .out    = {     (0 << 2)                                        /* Nonce present */
5550aadb33SDmitri Tikhonov                      | 0x08                                            /* Connection ID present */
5650aadb33SDmitri Tikhonov                      | 0x20                                            /* Packet number length */
5750aadb33SDmitri Tikhonov                      ,
5850aadb33SDmitri Tikhonov                      0x08, 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01,   /* Connection ID */
5950aadb33SDmitri Tikhonov                      0x01, 0x02, 0x03, 0x04,                           /* Packet number */
6050aadb33SDmitri Tikhonov        },
6150aadb33SDmitri Tikhonov    },
6250aadb33SDmitri Tikhonov
6350aadb33SDmitri Tikhonov    {
64fb96f4ddSDmitri Tikhonov        .pf     = select_pf_by_ver(LSQVER_043),
655392f7a3SLiteSpeed Tech        .bufsz  = GQUIC_MAX_PUBHDR_SZ,
6650aadb33SDmitri Tikhonov        .cid    = 0x0102030405060708UL,
6750aadb33SDmitri Tikhonov        .nonce  = NULL,
6850aadb33SDmitri Tikhonov        .packno = 0x00,
69c7d81ce1SDmitri Tikhonov        .bits   = GQUIC_PACKNO_LEN_1,
7050aadb33SDmitri Tikhonov        .len    = 1 + 8 + 0 + 1,
7150aadb33SDmitri Tikhonov        .out    = {     (0 << 2)                                        /* Nonce present */
7250aadb33SDmitri Tikhonov                      | 0x08                                            /* Connection ID present */
7350aadb33SDmitri Tikhonov                      | 0x00                                            /* Packet number length */
7450aadb33SDmitri Tikhonov                      ,
7550aadb33SDmitri Tikhonov                      0x08, 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01,   /* Connection ID */
7650aadb33SDmitri Tikhonov                      0x00,                                             /* Packet number */
7750aadb33SDmitri Tikhonov        },
7850aadb33SDmitri Tikhonov    },
7950aadb33SDmitri Tikhonov
8050aadb33SDmitri Tikhonov    {
81fb96f4ddSDmitri Tikhonov        .pf     = select_pf_by_ver(LSQVER_043),
825392f7a3SLiteSpeed Tech        .bufsz  = GQUIC_MAX_PUBHDR_SZ,
8350aadb33SDmitri Tikhonov        .cid    = 0x0102030405060708UL,
8450aadb33SDmitri Tikhonov        .nonce  = NULL,
8550aadb33SDmitri Tikhonov        .packno = 0x09,
86c7d81ce1SDmitri Tikhonov        .bits   = GQUIC_PACKNO_LEN_1,
87fb96f4ddSDmitri Tikhonov        .ver.buf= { 'Q', '0', '4', '3', },
8850aadb33SDmitri Tikhonov        .len    = 1 + 8 + 4 + 0 + 1,
8950aadb33SDmitri Tikhonov        .out    = {     (0 << 2)                                        /* Nonce present */
9050aadb33SDmitri Tikhonov                      | 0x01                                            /* Version present */
9150aadb33SDmitri Tikhonov                      | 0x08                                            /* Connection ID present */
9250aadb33SDmitri Tikhonov                      | 0x00                                            /* Packet number length */
9350aadb33SDmitri Tikhonov                      ,
9450aadb33SDmitri Tikhonov                      0x08, 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01,   /* Connection ID */
95fb96f4ddSDmitri Tikhonov                      'Q', '0', '4', '3',
9650aadb33SDmitri Tikhonov                      0x09,                                             /* Packet number */
9750aadb33SDmitri Tikhonov        },
9850aadb33SDmitri Tikhonov    },
9950aadb33SDmitri Tikhonov
10050aadb33SDmitri Tikhonov#define NONCENSE "0123456789abcdefghijklmnopqrstuv"
10150aadb33SDmitri Tikhonov#define NONCENSE_BYTES '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v'
10250aadb33SDmitri Tikhonov
10350aadb33SDmitri Tikhonov    {
104fb96f4ddSDmitri Tikhonov        .pf     = select_pf_by_ver(LSQVER_043),
1055392f7a3SLiteSpeed Tech        .bufsz  = GQUIC_MAX_PUBHDR_SZ,
10650aadb33SDmitri Tikhonov        .cid    = 0x0102030405060708UL,
10750aadb33SDmitri Tikhonov        .nonce  = NONCENSE,
10850aadb33SDmitri Tikhonov        .packno = 0x00,
109c7d81ce1SDmitri Tikhonov        .bits   = GQUIC_PACKNO_LEN_1,
11050aadb33SDmitri Tikhonov        .len    = 1 + 8 + 32 + 1,
11150aadb33SDmitri Tikhonov        .out    = {     (1 << 2)                                        /* Nonce present */
11250aadb33SDmitri Tikhonov                      | 0x08                                            /* Connection ID present */
11350aadb33SDmitri Tikhonov                      | 0x00                                            /* Packet number length */
11450aadb33SDmitri Tikhonov                      ,
11550aadb33SDmitri Tikhonov                      0x08, 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01,   /* Connection ID */
11650aadb33SDmitri Tikhonov                      NONCENSE_BYTES,
11750aadb33SDmitri Tikhonov                      0x00,                                             /* Packet number */
11850aadb33SDmitri Tikhonov        },
11950aadb33SDmitri Tikhonov    },
12050aadb33SDmitri Tikhonov
12150aadb33SDmitri Tikhonov    {
122fb96f4ddSDmitri Tikhonov        .pf     = select_pf_by_ver(LSQVER_043),
1235392f7a3SLiteSpeed Tech        .bufsz  = GQUIC_MAX_PUBHDR_SZ,
12450aadb33SDmitri Tikhonov        .cid    = 0,    /* Do not set connection ID */
12550aadb33SDmitri Tikhonov        .nonce  = NONCENSE,
12650aadb33SDmitri Tikhonov        .packno = 0x00,
127c7d81ce1SDmitri Tikhonov        .bits   = GQUIC_PACKNO_LEN_1,
12850aadb33SDmitri Tikhonov        .len    = 1 + 0 + 32 + 1,
12950aadb33SDmitri Tikhonov        .out    = {     (1 << 2)                                        /* Nonce present */
13050aadb33SDmitri Tikhonov                      | 0x00                                            /* Packet number length */
13150aadb33SDmitri Tikhonov                      ,
13250aadb33SDmitri Tikhonov                      NONCENSE_BYTES,
13350aadb33SDmitri Tikhonov                      0x00,                                             /* Packet number */
13450aadb33SDmitri Tikhonov        },
13550aadb33SDmitri Tikhonov    },
13650aadb33SDmitri Tikhonov
13750aadb33SDmitri Tikhonov    {
138fb96f4ddSDmitri Tikhonov        .pf     = select_pf_by_ver(LSQVER_043),
1395392f7a3SLiteSpeed Tech        .bufsz  = GQUIC_MAX_PUBHDR_SZ,
14050aadb33SDmitri Tikhonov        .cid    = 0x0102030405060708UL,
14150aadb33SDmitri Tikhonov        .nonce  = NONCENSE,
14250aadb33SDmitri Tikhonov        .packno = 0x00,
143c7d81ce1SDmitri Tikhonov        .bits   = GQUIC_PACKNO_LEN_1,
144fb96f4ddSDmitri Tikhonov        .ver.buf= { 'Q', '0', '4', '3', },
14550aadb33SDmitri Tikhonov        .len    = 1 + 8 + 4 + 32 + 1,
14650aadb33SDmitri Tikhonov        .out    = {     (1 << 2)                                        /* Nonce present */
14750aadb33SDmitri Tikhonov                      | 0x01                                            /* Version present */
14850aadb33SDmitri Tikhonov                      | 0x08                                            /* Connection ID present */
14950aadb33SDmitri Tikhonov                      | 0x00                                            /* Packet number length */
15050aadb33SDmitri Tikhonov                      ,
15150aadb33SDmitri Tikhonov                      0x08, 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01,   /* Connection ID */
152fb96f4ddSDmitri Tikhonov                      'Q', '0', '4', '3',
15350aadb33SDmitri Tikhonov                      NONCENSE_BYTES,
15450aadb33SDmitri Tikhonov                      0x00,                                             /* Packet number */
15550aadb33SDmitri Tikhonov        },
15650aadb33SDmitri Tikhonov    },
15750aadb33SDmitri Tikhonov
15850aadb33SDmitri Tikhonov    {
159fb96f4ddSDmitri Tikhonov        .pf     = select_pf_by_ver(LSQVER_043),
1605392f7a3SLiteSpeed Tech        .bufsz  = GQUIC_MAX_PUBHDR_SZ,
16150aadb33SDmitri Tikhonov        .cid    = 0x0102030405060708UL,
16250aadb33SDmitri Tikhonov        .nonce  = NONCENSE,
16350aadb33SDmitri Tikhonov        .packno = 0xA0A1A2A3A4A5A6A7UL,
164c7d81ce1SDmitri Tikhonov        .bits   = GQUIC_PACKNO_LEN_6,
16550aadb33SDmitri Tikhonov        .len    = 1 + 8 + 32 + 6,
16650aadb33SDmitri Tikhonov        .out    = {     (1 << 2)                                        /* Nonce present */
16750aadb33SDmitri Tikhonov                      | 0x08                                            /* Connection ID present */
16850aadb33SDmitri Tikhonov                      | 0x30                                            /* Packet number length */
16950aadb33SDmitri Tikhonov                      ,
17050aadb33SDmitri Tikhonov                      0x08, 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01,   /* Connection ID */
17150aadb33SDmitri Tikhonov                      NONCENSE_BYTES,
1725392f7a3SLiteSpeed Tech                      0xA2, 0xA3, 0xA4, 0xA5, 0xA6, 0xA7,
17350aadb33SDmitri Tikhonov        },
17450aadb33SDmitri Tikhonov    },
17550aadb33SDmitri Tikhonov
17650aadb33SDmitri Tikhonov    {
177fb96f4ddSDmitri Tikhonov        .pf     = select_pf_by_ver(LSQVER_043),
1785392f7a3SLiteSpeed Tech        .bufsz  = GQUIC_MAX_PUBHDR_SZ,
17950aadb33SDmitri Tikhonov        .cid    = 0x0102030405060708UL,
18050aadb33SDmitri Tikhonov        .nonce  = NONCENSE,
18150aadb33SDmitri Tikhonov        .packno = 0xA0A1A2A3A4A5A6A7UL,
182c7d81ce1SDmitri Tikhonov        .bits   = GQUIC_PACKNO_LEN_6,
18350aadb33SDmitri Tikhonov        .len    = 1 + 8 + 32 + 6,
18450aadb33SDmitri Tikhonov        .out    = {     (1 << 2)                                        /* Nonce present */
18550aadb33SDmitri Tikhonov                      | 0x08                                            /* Connection ID present */
18650aadb33SDmitri Tikhonov                      | 0x30                                            /* Packet number length */
18750aadb33SDmitri Tikhonov                      ,
18850aadb33SDmitri Tikhonov                      0x08, 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01,   /* Connection ID */
18950aadb33SDmitri Tikhonov                      NONCENSE_BYTES,
19050aadb33SDmitri Tikhonov                      0xA2, 0xA3, 0xA4, 0xA5, 0xA6, 0xA7,
19150aadb33SDmitri Tikhonov        },
19250aadb33SDmitri Tikhonov    },
19350aadb33SDmitri Tikhonov
19450aadb33SDmitri Tikhonov};
19550aadb33SDmitri Tikhonov
19650aadb33SDmitri Tikhonov
19750aadb33SDmitri Tikhonovstatic void
19850aadb33SDmitri Tikhonovrun_test (int i)
19950aadb33SDmitri Tikhonov{
20050aadb33SDmitri Tikhonov    const struct test *const test = &tests[i];
20150aadb33SDmitri Tikhonov
2029626cfc2SDmitri Tikhonov    struct lsquic_packet_out packet_out =
2039626cfc2SDmitri Tikhonov    {
2049626cfc2SDmitri Tikhonov        .po_flags = (test->cid ? PO_CONN_ID : 0)
2059626cfc2SDmitri Tikhonov                  | (test->ver.val ? PO_VERSION : 0)
2069626cfc2SDmitri Tikhonov                  | (test->nonce ? PO_NONCE: 0)
2079626cfc2SDmitri Tikhonov                  ,
2089626cfc2SDmitri Tikhonov        .po_nonce = (unsigned char *) test->nonce,
2099626cfc2SDmitri Tikhonov        .po_ver_tag = test->ver.val,
2109626cfc2SDmitri Tikhonov        .po_packno = test->packno,
2119626cfc2SDmitri Tikhonov    };
2129626cfc2SDmitri Tikhonov    lsquic_packet_out_set_packno_bits(&packet_out, test->bits);
2139626cfc2SDmitri Tikhonov
2145392f7a3SLiteSpeed Tech    lsquic_cid_t cid;
2155392f7a3SLiteSpeed Tech    memset(&cid, 0, sizeof(cid));
2165392f7a3SLiteSpeed Tech    cid.len = sizeof(test->cid);
2175392f7a3SLiteSpeed Tech    memcpy(cid.idbuf, &test->cid, sizeof(test->cid));
2185392f7a3SLiteSpeed Tech
2195392f7a3SLiteSpeed Tech    struct lsquic_conn lconn = LSCONN_INITIALIZER_CID(lconn, cid);
2209626cfc2SDmitri Tikhonov
2215392f7a3SLiteSpeed Tech    unsigned char out[GQUIC_MAX_PUBHDR_SZ];
2229626cfc2SDmitri Tikhonov    int len = test->pf->pf_gen_reg_pkt_header(&lconn, &packet_out, out,
22349f1f4f6SDmitri Tikhonov                                                    sizeof(out), NULL, NULL);
22450aadb33SDmitri Tikhonov
22550aadb33SDmitri Tikhonov    assert(("Packet length is correct", len == test->len));
22650aadb33SDmitri Tikhonov
22750aadb33SDmitri Tikhonov    if (test->len > 0)
22850aadb33SDmitri Tikhonov        assert(("Packet contents are correct",
22950aadb33SDmitri Tikhonov            0 == memcmp(out, test->out, len)));
23050aadb33SDmitri Tikhonov}
23150aadb33SDmitri Tikhonov
23250aadb33SDmitri Tikhonov
23350aadb33SDmitri Tikhonovint
23450aadb33SDmitri Tikhonovmain (void)
23550aadb33SDmitri Tikhonov{
23650aadb33SDmitri Tikhonov    unsigned i;
23750aadb33SDmitri Tikhonov    for (i = 0; i < sizeof(tests) / sizeof(tests[0]); ++i)
23850aadb33SDmitri Tikhonov        run_test(i);
23950aadb33SDmitri Tikhonov    return 0;
24050aadb33SDmitri Tikhonov}
241