test_reg_pkt_headergen.c revision f07b3eae
106b2a236SDmitri Tikhonov/* Copyright (c) 2017 - 2021 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 Tikhonov
4550aadb33SDmitri Tikhonovstatic void
46f07b3eaeSTyler Youngrun_test (const struct test *const test)
4750aadb33SDmitri Tikhonov{
4850aadb33SDmitri Tikhonov
499626cfc2SDmitri Tikhonov    struct lsquic_packet_out packet_out =
509626cfc2SDmitri Tikhonov    {
519626cfc2SDmitri Tikhonov        .po_flags = (test->cid ? PO_CONN_ID : 0)
529626cfc2SDmitri Tikhonov                  | (test->ver.val ? PO_VERSION : 0)
539626cfc2SDmitri Tikhonov                  | (test->nonce ? PO_NONCE: 0)
549626cfc2SDmitri Tikhonov                  ,
559626cfc2SDmitri Tikhonov        .po_nonce = (unsigned char *) test->nonce,
569626cfc2SDmitri Tikhonov        .po_ver_tag = test->ver.val,
579626cfc2SDmitri Tikhonov        .po_packno = test->packno,
589626cfc2SDmitri Tikhonov    };
599626cfc2SDmitri Tikhonov    lsquic_packet_out_set_packno_bits(&packet_out, test->bits);
609626cfc2SDmitri Tikhonov
615392f7a3SLiteSpeed Tech    lsquic_cid_t cid;
625392f7a3SLiteSpeed Tech    memset(&cid, 0, sizeof(cid));
635392f7a3SLiteSpeed Tech    cid.len = sizeof(test->cid);
645392f7a3SLiteSpeed Tech    memcpy(cid.idbuf, &test->cid, sizeof(test->cid));
655392f7a3SLiteSpeed Tech
665392f7a3SLiteSpeed Tech    struct lsquic_conn lconn = LSCONN_INITIALIZER_CID(lconn, cid);
679626cfc2SDmitri Tikhonov
685392f7a3SLiteSpeed Tech    unsigned char out[GQUIC_MAX_PUBHDR_SZ];
699626cfc2SDmitri Tikhonov    int len = test->pf->pf_gen_reg_pkt_header(&lconn, &packet_out, out,
7049f1f4f6SDmitri Tikhonov                                                    sizeof(out), NULL, NULL);
7150aadb33SDmitri Tikhonov
7250aadb33SDmitri Tikhonov    assert(("Packet length is correct", len == test->len));
7350aadb33SDmitri Tikhonov
7450aadb33SDmitri Tikhonov    if (test->len > 0)
7550aadb33SDmitri Tikhonov        assert(("Packet contents are correct",
7650aadb33SDmitri Tikhonov            0 == memcmp(out, test->out, len)));
7750aadb33SDmitri Tikhonov}
7850aadb33SDmitri Tikhonov
7950aadb33SDmitri Tikhonov
8050aadb33SDmitri Tikhonovint
8150aadb33SDmitri Tikhonovmain (void)
8250aadb33SDmitri Tikhonov{
83f07b3eaeSTyler Young	const struct test tests[] = {
84f07b3eaeSTyler Young	    {
85f07b3eaeSTyler Young	        .pf     = select_pf_by_ver(LSQVER_043),
86f07b3eaeSTyler Young	        .bufsz  = GQUIC_MAX_PUBHDR_SZ,
87f07b3eaeSTyler Young	        .cid    = 0x0102030405060708UL,
88f07b3eaeSTyler Young	        .nonce  = NULL,
89f07b3eaeSTyler Young	        .packno = 0x01020304,
90f07b3eaeSTyler Young	        .bits   = GQUIC_PACKNO_LEN_4,
91f07b3eaeSTyler Young	        .len    = 1 + 8 + 0 + 4,
92f07b3eaeSTyler Young	        .out    = {     (0 << 2)                                        /* Nonce present */
93f07b3eaeSTyler Young	                      | 0x08                                            /* Connection ID present */
94f07b3eaeSTyler Young	                      | 0x20                                            /* Packet number length */
95f07b3eaeSTyler Young	                      ,
96f07b3eaeSTyler Young	                      0x08, 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01,   /* Connection ID */
97f07b3eaeSTyler Young	                      0x01, 0x02, 0x03, 0x04,                           /* Packet number */
98f07b3eaeSTyler Young	        },
99f07b3eaeSTyler Young	    },
100f07b3eaeSTyler Young
101f07b3eaeSTyler Young	    {
102f07b3eaeSTyler Young	        .pf     = select_pf_by_ver(LSQVER_043),
103f07b3eaeSTyler Young	        .bufsz  = GQUIC_MAX_PUBHDR_SZ,
104f07b3eaeSTyler Young	        .cid    = 0x0102030405060708UL,
105f07b3eaeSTyler Young	        .nonce  = NULL,
106f07b3eaeSTyler Young	        .packno = 0x00,
107f07b3eaeSTyler Young	        .bits   = GQUIC_PACKNO_LEN_1,
108f07b3eaeSTyler Young	        .len    = 1 + 8 + 0 + 1,
109f07b3eaeSTyler Young	        .out    = {     (0 << 2)                                        /* Nonce present */
110f07b3eaeSTyler Young	                      | 0x08                                            /* Connection ID present */
111f07b3eaeSTyler Young	                      | 0x00                                            /* Packet number length */
112f07b3eaeSTyler Young	                      ,
113f07b3eaeSTyler Young	                      0x08, 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01,   /* Connection ID */
114f07b3eaeSTyler Young	                      0x00,                                             /* Packet number */
115f07b3eaeSTyler Young	        },
116f07b3eaeSTyler Young	    },
117f07b3eaeSTyler Young
118f07b3eaeSTyler Young	    {
119f07b3eaeSTyler Young	        .pf     = select_pf_by_ver(LSQVER_043),
120f07b3eaeSTyler Young	        .bufsz  = GQUIC_MAX_PUBHDR_SZ,
121f07b3eaeSTyler Young	        .cid    = 0x0102030405060708UL,
122f07b3eaeSTyler Young	        .nonce  = NULL,
123f07b3eaeSTyler Young	        .packno = 0x09,
124f07b3eaeSTyler Young	        .bits   = GQUIC_PACKNO_LEN_1,
125f07b3eaeSTyler Young	        .ver.buf= { 'Q', '0', '4', '3', },
126f07b3eaeSTyler Young	        .len    = 1 + 8 + 4 + 0 + 1,
127f07b3eaeSTyler Young	        .out    = {     (0 << 2)                                        /* Nonce present */
128f07b3eaeSTyler Young	                      | 0x01                                            /* Version present */
129f07b3eaeSTyler Young	                      | 0x08                                            /* Connection ID present */
130f07b3eaeSTyler Young	                      | 0x00                                            /* Packet number length */
131f07b3eaeSTyler Young	                      ,
132f07b3eaeSTyler Young	                      0x08, 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01,   /* Connection ID */
133f07b3eaeSTyler Young	                      'Q', '0', '4', '3',
134f07b3eaeSTyler Young	                      0x09,                                             /* Packet number */
135f07b3eaeSTyler Young	        },
136f07b3eaeSTyler Young	    },
137f07b3eaeSTyler Young
138f07b3eaeSTyler Young	#define NONCENSE "0123456789abcdefghijklmnopqrstuv"
139f07b3eaeSTyler Young	#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'
140f07b3eaeSTyler Young
141f07b3eaeSTyler Young	    {
142f07b3eaeSTyler Young	        .pf     = select_pf_by_ver(LSQVER_043),
143f07b3eaeSTyler Young	        .bufsz  = GQUIC_MAX_PUBHDR_SZ,
144f07b3eaeSTyler Young	        .cid    = 0x0102030405060708UL,
145f07b3eaeSTyler Young	        .nonce  = NONCENSE,
146f07b3eaeSTyler Young	        .packno = 0x00,
147f07b3eaeSTyler Young	        .bits   = GQUIC_PACKNO_LEN_1,
148f07b3eaeSTyler Young	        .len    = 1 + 8 + 32 + 1,
149f07b3eaeSTyler Young	        .out    = {     (1 << 2)                                        /* Nonce present */
150f07b3eaeSTyler Young	                      | 0x08                                            /* Connection ID present */
151f07b3eaeSTyler Young	                      | 0x00                                            /* Packet number length */
152f07b3eaeSTyler Young	                      ,
153f07b3eaeSTyler Young	                      0x08, 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01,   /* Connection ID */
154f07b3eaeSTyler Young	                      NONCENSE_BYTES,
155f07b3eaeSTyler Young	                      0x00,                                             /* Packet number */
156f07b3eaeSTyler Young	        },
157f07b3eaeSTyler Young	    },
158f07b3eaeSTyler Young
159f07b3eaeSTyler Young	    {
160f07b3eaeSTyler Young	        .pf     = select_pf_by_ver(LSQVER_043),
161f07b3eaeSTyler Young	        .bufsz  = GQUIC_MAX_PUBHDR_SZ,
162f07b3eaeSTyler Young	        .cid    = 0,    /* Do not set connection ID */
163f07b3eaeSTyler Young	        .nonce  = NONCENSE,
164f07b3eaeSTyler Young	        .packno = 0x00,
165f07b3eaeSTyler Young	        .bits   = GQUIC_PACKNO_LEN_1,
166f07b3eaeSTyler Young	        .len    = 1 + 0 + 32 + 1,
167f07b3eaeSTyler Young	        .out    = {     (1 << 2)                                        /* Nonce present */
168f07b3eaeSTyler Young	                      | 0x00                                            /* Packet number length */
169f07b3eaeSTyler Young	                      ,
170f07b3eaeSTyler Young	                      NONCENSE_BYTES,
171f07b3eaeSTyler Young	                      0x00,                                             /* Packet number */
172f07b3eaeSTyler Young	        },
173f07b3eaeSTyler Young	    },
174f07b3eaeSTyler Young
175f07b3eaeSTyler Young	    {
176f07b3eaeSTyler Young	        .pf     = select_pf_by_ver(LSQVER_043),
177f07b3eaeSTyler Young	        .bufsz  = GQUIC_MAX_PUBHDR_SZ,
178f07b3eaeSTyler Young	        .cid    = 0x0102030405060708UL,
179f07b3eaeSTyler Young	        .nonce  = NONCENSE,
180f07b3eaeSTyler Young	        .packno = 0x00,
181f07b3eaeSTyler Young	        .bits   = GQUIC_PACKNO_LEN_1,
182f07b3eaeSTyler Young	        .ver.buf= { 'Q', '0', '4', '3', },
183f07b3eaeSTyler Young	        .len    = 1 + 8 + 4 + 32 + 1,
184f07b3eaeSTyler Young	        .out    = {     (1 << 2)                                        /* Nonce present */
185f07b3eaeSTyler Young	                      | 0x01                                            /* Version present */
186f07b3eaeSTyler Young	                      | 0x08                                            /* Connection ID present */
187f07b3eaeSTyler Young	                      | 0x00                                            /* Packet number length */
188f07b3eaeSTyler Young	                      ,
189f07b3eaeSTyler Young	                      0x08, 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01,   /* Connection ID */
190f07b3eaeSTyler Young	                      'Q', '0', '4', '3',
191f07b3eaeSTyler Young	                      NONCENSE_BYTES,
192f07b3eaeSTyler Young	                      0x00,                                             /* Packet number */
193f07b3eaeSTyler Young	        },
194f07b3eaeSTyler Young	    },
195f07b3eaeSTyler Young
196f07b3eaeSTyler Young	    {
197f07b3eaeSTyler Young	        .pf     = select_pf_by_ver(LSQVER_043),
198f07b3eaeSTyler Young	        .bufsz  = GQUIC_MAX_PUBHDR_SZ,
199f07b3eaeSTyler Young	        .cid    = 0x0102030405060708UL,
200f07b3eaeSTyler Young	        .nonce  = NONCENSE,
201f07b3eaeSTyler Young	        .packno = 0xA0A1A2A3A4A5A6A7UL,
202f07b3eaeSTyler Young	        .bits   = GQUIC_PACKNO_LEN_6,
203f07b3eaeSTyler Young	        .len    = 1 + 8 + 32 + 6,
204f07b3eaeSTyler Young	        .out    = {     (1 << 2)                                        /* Nonce present */
205f07b3eaeSTyler Young	                      | 0x08                                            /* Connection ID present */
206f07b3eaeSTyler Young	                      | 0x30                                            /* Packet number length */
207f07b3eaeSTyler Young	                      ,
208f07b3eaeSTyler Young	                      0x08, 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01,   /* Connection ID */
209f07b3eaeSTyler Young	                      NONCENSE_BYTES,
210f07b3eaeSTyler Young	                      0xA2, 0xA3, 0xA4, 0xA5, 0xA6, 0xA7,
211f07b3eaeSTyler Young	        },
212f07b3eaeSTyler Young	    },
213f07b3eaeSTyler Young
214f07b3eaeSTyler Young	    {
215f07b3eaeSTyler Young	        .pf     = select_pf_by_ver(LSQVER_043),
216f07b3eaeSTyler Young	        .bufsz  = GQUIC_MAX_PUBHDR_SZ,
217f07b3eaeSTyler Young	        .cid    = 0x0102030405060708UL,
218f07b3eaeSTyler Young	        .nonce  = NONCENSE,
219f07b3eaeSTyler Young	        .packno = 0xA0A1A2A3A4A5A6A7UL,
220f07b3eaeSTyler Young	        .bits   = GQUIC_PACKNO_LEN_6,
221f07b3eaeSTyler Young	        .len    = 1 + 8 + 32 + 6,
222f07b3eaeSTyler Young	        .out    = {     (1 << 2)                                        /* Nonce present */
223f07b3eaeSTyler Young	                      | 0x08                                            /* Connection ID present */
224f07b3eaeSTyler Young	                      | 0x30                                            /* Packet number length */
225f07b3eaeSTyler Young	                      ,
226f07b3eaeSTyler Young	                      0x08, 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01,   /* Connection ID */
227f07b3eaeSTyler Young	                      NONCENSE_BYTES,
228f07b3eaeSTyler Young	                      0xA2, 0xA3, 0xA4, 0xA5, 0xA6, 0xA7,
229f07b3eaeSTyler Young	        },
230f07b3eaeSTyler Young	    },
231f07b3eaeSTyler Young
232f07b3eaeSTyler Young	};
233f07b3eaeSTyler Young
23450aadb33SDmitri Tikhonov    unsigned i;
23550aadb33SDmitri Tikhonov    for (i = 0; i < sizeof(tests) / sizeof(tests[0]); ++i)
236f07b3eaeSTyler Young        run_test(&tests[i]);
23750aadb33SDmitri Tikhonov    return 0;
23850aadb33SDmitri Tikhonov}
239