litespeed-quic/test/unittests/test_reg_pkt_headergen.c

241 lines
9.1 KiB
C
Raw Normal View History

/* Copyright (c) 2017 - 2020 LiteSpeed Technologies Inc. See LICENSE. */
2017-09-22 21:00:03 +00:00
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifndef WIN32
2017-09-22 21:00:03 +00:00
#include <sys/time.h>
#endif
#include <sys/queue.h>
2017-09-22 21:00:03 +00:00
#include "lsquic_types.h"
2018-08-15 19:06:31 +00:00
#include "lsquic.h"
#include "lsquic_int_types.h"
2017-09-22 21:00:03 +00:00
#include "lsquic_packet_common.h"
2018-08-15 19:06:31 +00:00
#include "lsquic_packet_out.h"
#include "lsquic_hash.h"
2018-08-15 19:06:31 +00:00
#include "lsquic_conn.h"
2017-09-22 21:00:03 +00:00
#include "lsquic_parse.h"
struct test {
/* Inputs. */
const struct parse_funcs
*pf;
size_t bufsz;
uint64_t cid; /* Zero means connection ID is not specified */
2017-09-22 21:00:03 +00:00
const char *nonce;
lsquic_packno_t packno;
enum packno_bits
2017-09-22 21:00:03 +00:00
bits; /* The test has been retrofitted by adding bits parameter. The test can
* be made more complicated by calculating packet number length based on
* some other inputs. However, this is tested elsewhere.
*/
union {
unsigned char buf[4];
lsquic_ver_tag_t val;
} ver;
/* Outputs */
int len; /* Retval */
char out[0x100]; /* Contents */
};
static const struct test tests[] = {
{
.pf = select_pf_by_ver(LSQVER_043),
.bufsz = GQUIC_MAX_PUBHDR_SZ,
2017-09-22 21:00:03 +00:00
.cid = 0x0102030405060708UL,
.nonce = NULL,
.packno = 0x01020304,
.bits = GQUIC_PACKNO_LEN_4,
2017-09-22 21:00:03 +00:00
.len = 1 + 8 + 0 + 4,
.out = { (0 << 2) /* Nonce present */
| 0x08 /* Connection ID present */
| 0x20 /* Packet number length */
,
0x08, 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01, /* Connection ID */
0x01, 0x02, 0x03, 0x04, /* Packet number */
},
},
{
.pf = select_pf_by_ver(LSQVER_043),
.bufsz = GQUIC_MAX_PUBHDR_SZ,
2017-09-22 21:00:03 +00:00
.cid = 0x0102030405060708UL,
.nonce = NULL,
.packno = 0x00,
.bits = GQUIC_PACKNO_LEN_1,
2017-09-22 21:00:03 +00:00
.len = 1 + 8 + 0 + 1,
.out = { (0 << 2) /* Nonce present */
| 0x08 /* Connection ID present */
| 0x00 /* Packet number length */
,
0x08, 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01, /* Connection ID */
0x00, /* Packet number */
},
},
{
.pf = select_pf_by_ver(LSQVER_043),
.bufsz = GQUIC_MAX_PUBHDR_SZ,
2017-09-22 21:00:03 +00:00
.cid = 0x0102030405060708UL,
.nonce = NULL,
.packno = 0x09,
.bits = GQUIC_PACKNO_LEN_1,
.ver.buf= { 'Q', '0', '4', '3', },
2017-09-22 21:00:03 +00:00
.len = 1 + 8 + 4 + 0 + 1,
.out = { (0 << 2) /* Nonce present */
| 0x01 /* Version present */
| 0x08 /* Connection ID present */
| 0x00 /* Packet number length */
,
0x08, 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01, /* Connection ID */
'Q', '0', '4', '3',
2017-09-22 21:00:03 +00:00
0x09, /* Packet number */
},
},
#define NONCENSE "0123456789abcdefghijklmnopqrstuv"
#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'
{
.pf = select_pf_by_ver(LSQVER_043),
.bufsz = GQUIC_MAX_PUBHDR_SZ,
2017-09-22 21:00:03 +00:00
.cid = 0x0102030405060708UL,
.nonce = NONCENSE,
.packno = 0x00,
.bits = GQUIC_PACKNO_LEN_1,
2017-09-22 21:00:03 +00:00
.len = 1 + 8 + 32 + 1,
.out = { (1 << 2) /* Nonce present */
| 0x08 /* Connection ID present */
| 0x00 /* Packet number length */
,
0x08, 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01, /* Connection ID */
NONCENSE_BYTES,
0x00, /* Packet number */
},
},
{
.pf = select_pf_by_ver(LSQVER_043),
.bufsz = GQUIC_MAX_PUBHDR_SZ,
2017-09-22 21:00:03 +00:00
.cid = 0, /* Do not set connection ID */
.nonce = NONCENSE,
.packno = 0x00,
.bits = GQUIC_PACKNO_LEN_1,
2017-09-22 21:00:03 +00:00
.len = 1 + 0 + 32 + 1,
.out = { (1 << 2) /* Nonce present */
| 0x00 /* Packet number length */
,
NONCENSE_BYTES,
0x00, /* Packet number */
},
},
{
.pf = select_pf_by_ver(LSQVER_043),
.bufsz = GQUIC_MAX_PUBHDR_SZ,
2017-09-22 21:00:03 +00:00
.cid = 0x0102030405060708UL,
.nonce = NONCENSE,
.packno = 0x00,
.bits = GQUIC_PACKNO_LEN_1,
.ver.buf= { 'Q', '0', '4', '3', },
2017-09-22 21:00:03 +00:00
.len = 1 + 8 + 4 + 32 + 1,
.out = { (1 << 2) /* Nonce present */
| 0x01 /* Version present */
| 0x08 /* Connection ID present */
| 0x00 /* Packet number length */
,
0x08, 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01, /* Connection ID */
'Q', '0', '4', '3',
2017-09-22 21:00:03 +00:00
NONCENSE_BYTES,
0x00, /* Packet number */
},
},
{
.pf = select_pf_by_ver(LSQVER_043),
.bufsz = GQUIC_MAX_PUBHDR_SZ,
2017-09-22 21:00:03 +00:00
.cid = 0x0102030405060708UL,
.nonce = NONCENSE,
.packno = 0xA0A1A2A3A4A5A6A7UL,
.bits = GQUIC_PACKNO_LEN_6,
2017-09-22 21:00:03 +00:00
.len = 1 + 8 + 32 + 6,
.out = { (1 << 2) /* Nonce present */
| 0x08 /* Connection ID present */
| 0x30 /* Packet number length */
,
0x08, 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01, /* Connection ID */
NONCENSE_BYTES,
0xA2, 0xA3, 0xA4, 0xA5, 0xA6, 0xA7,
2017-09-22 21:00:03 +00:00
},
},
{
.pf = select_pf_by_ver(LSQVER_043),
.bufsz = GQUIC_MAX_PUBHDR_SZ,
2017-09-22 21:00:03 +00:00
.cid = 0x0102030405060708UL,
.nonce = NONCENSE,
.packno = 0xA0A1A2A3A4A5A6A7UL,
.bits = GQUIC_PACKNO_LEN_6,
2017-09-22 21:00:03 +00:00
.len = 1 + 8 + 32 + 6,
.out = { (1 << 2) /* Nonce present */
| 0x08 /* Connection ID present */
| 0x30 /* Packet number length */
,
0x08, 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01, /* Connection ID */
NONCENSE_BYTES,
0xA2, 0xA3, 0xA4, 0xA5, 0xA6, 0xA7,
},
},
};
static void
run_test (int i)
{
const struct test *const test = &tests[i];
2018-08-15 19:06:31 +00:00
struct lsquic_packet_out packet_out =
{
.po_flags = (test->cid ? PO_CONN_ID : 0)
| (test->ver.val ? PO_VERSION : 0)
| (test->nonce ? PO_NONCE: 0)
,
.po_nonce = (unsigned char *) test->nonce,
.po_ver_tag = test->ver.val,
.po_packno = test->packno,
};
lsquic_packet_out_set_packno_bits(&packet_out, test->bits);
lsquic_cid_t cid;
memset(&cid, 0, sizeof(cid));
cid.len = sizeof(test->cid);
memcpy(cid.idbuf, &test->cid, sizeof(test->cid));
struct lsquic_conn lconn = LSCONN_INITIALIZER_CID(lconn, cid);
2018-08-15 19:06:31 +00:00
unsigned char out[GQUIC_MAX_PUBHDR_SZ];
2018-08-15 19:06:31 +00:00
int len = test->pf->pf_gen_reg_pkt_header(&lconn, &packet_out, out,
sizeof(out));
2017-09-22 21:00:03 +00:00
assert(("Packet length is correct", len == test->len));
if (test->len > 0)
assert(("Packet contents are correct",
0 == memcmp(out, test->out, len)));
}
int
main (void)
{
unsigned i;
for (i = 0; i < sizeof(tests) / sizeof(tests[0]); ++i)
run_test(i);
return 0;
}