test_reg_pkt_headergen.c revision f07b3eae
1/* Copyright (c) 2017 - 2021 LiteSpeed Technologies Inc.  See LICENSE. */
2#include <assert.h>
3#include <stdio.h>
4#include <stdlib.h>
5#include <string.h>
6#ifndef WIN32
7#include <sys/time.h>
8#endif
9#include <sys/queue.h>
10
11#include "lsquic_types.h"
12#include "lsquic.h"
13#include "lsquic_int_types.h"
14#include "lsquic_packet_common.h"
15#include "lsquic_packet_out.h"
16#include "lsquic_hash.h"
17#include "lsquic_conn.h"
18#include "lsquic_parse.h"
19
20struct test {
21    /* Inputs. */
22    const struct parse_funcs
23                   *pf;
24    size_t          bufsz;
25    uint64_t        cid;    /* Zero means connection ID is not specified */
26    const char     *nonce;
27    lsquic_packno_t packno;
28    enum packno_bits
29                    bits;   /* The test has been retrofitted by adding bits parameter.  The test can
30                             * be made more complicated by calculating packet number length based on
31                             * some other inputs.  However, this is tested elsewhere.
32                             */
33    union {
34        unsigned char   buf[4];
35        lsquic_ver_tag_t    val;
36    }               ver;
37
38    /* Outputs */
39    int             len;            /* Retval */
40    char            out[0x100];     /* Contents */
41};
42
43
44
45static void
46run_test (const struct test *const test)
47{
48
49    struct lsquic_packet_out packet_out =
50    {
51        .po_flags = (test->cid ? PO_CONN_ID : 0)
52                  | (test->ver.val ? PO_VERSION : 0)
53                  | (test->nonce ? PO_NONCE: 0)
54                  ,
55        .po_nonce = (unsigned char *) test->nonce,
56        .po_ver_tag = test->ver.val,
57        .po_packno = test->packno,
58    };
59    lsquic_packet_out_set_packno_bits(&packet_out, test->bits);
60
61    lsquic_cid_t cid;
62    memset(&cid, 0, sizeof(cid));
63    cid.len = sizeof(test->cid);
64    memcpy(cid.idbuf, &test->cid, sizeof(test->cid));
65
66    struct lsquic_conn lconn = LSCONN_INITIALIZER_CID(lconn, cid);
67
68    unsigned char out[GQUIC_MAX_PUBHDR_SZ];
69    int len = test->pf->pf_gen_reg_pkt_header(&lconn, &packet_out, out,
70                                                    sizeof(out), NULL, NULL);
71
72    assert(("Packet length is correct", len == test->len));
73
74    if (test->len > 0)
75        assert(("Packet contents are correct",
76            0 == memcmp(out, test->out, len)));
77}
78
79
80int
81main (void)
82{
83	const struct test tests[] = {
84	    {
85	        .pf     = select_pf_by_ver(LSQVER_043),
86	        .bufsz  = GQUIC_MAX_PUBHDR_SZ,
87	        .cid    = 0x0102030405060708UL,
88	        .nonce  = NULL,
89	        .packno = 0x01020304,
90	        .bits   = GQUIC_PACKNO_LEN_4,
91	        .len    = 1 + 8 + 0 + 4,
92	        .out    = {     (0 << 2)                                        /* Nonce present */
93	                      | 0x08                                            /* Connection ID present */
94	                      | 0x20                                            /* Packet number length */
95	                      ,
96	                      0x08, 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01,   /* Connection ID */
97	                      0x01, 0x02, 0x03, 0x04,                           /* Packet number */
98	        },
99	    },
100
101	    {
102	        .pf     = select_pf_by_ver(LSQVER_043),
103	        .bufsz  = GQUIC_MAX_PUBHDR_SZ,
104	        .cid    = 0x0102030405060708UL,
105	        .nonce  = NULL,
106	        .packno = 0x00,
107	        .bits   = GQUIC_PACKNO_LEN_1,
108	        .len    = 1 + 8 + 0 + 1,
109	        .out    = {     (0 << 2)                                        /* Nonce present */
110	                      | 0x08                                            /* Connection ID present */
111	                      | 0x00                                            /* Packet number length */
112	                      ,
113	                      0x08, 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01,   /* Connection ID */
114	                      0x00,                                             /* Packet number */
115	        },
116	    },
117
118	    {
119	        .pf     = select_pf_by_ver(LSQVER_043),
120	        .bufsz  = GQUIC_MAX_PUBHDR_SZ,
121	        .cid    = 0x0102030405060708UL,
122	        .nonce  = NULL,
123	        .packno = 0x09,
124	        .bits   = GQUIC_PACKNO_LEN_1,
125	        .ver.buf= { 'Q', '0', '4', '3', },
126	        .len    = 1 + 8 + 4 + 0 + 1,
127	        .out    = {     (0 << 2)                                        /* Nonce present */
128	                      | 0x01                                            /* Version present */
129	                      | 0x08                                            /* Connection ID present */
130	                      | 0x00                                            /* Packet number length */
131	                      ,
132	                      0x08, 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01,   /* Connection ID */
133	                      'Q', '0', '4', '3',
134	                      0x09,                                             /* Packet number */
135	        },
136	    },
137
138	#define NONCENSE "0123456789abcdefghijklmnopqrstuv"
139	#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'
140
141	    {
142	        .pf     = select_pf_by_ver(LSQVER_043),
143	        .bufsz  = GQUIC_MAX_PUBHDR_SZ,
144	        .cid    = 0x0102030405060708UL,
145	        .nonce  = NONCENSE,
146	        .packno = 0x00,
147	        .bits   = GQUIC_PACKNO_LEN_1,
148	        .len    = 1 + 8 + 32 + 1,
149	        .out    = {     (1 << 2)                                        /* Nonce present */
150	                      | 0x08                                            /* Connection ID present */
151	                      | 0x00                                            /* Packet number length */
152	                      ,
153	                      0x08, 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01,   /* Connection ID */
154	                      NONCENSE_BYTES,
155	                      0x00,                                             /* Packet number */
156	        },
157	    },
158
159	    {
160	        .pf     = select_pf_by_ver(LSQVER_043),
161	        .bufsz  = GQUIC_MAX_PUBHDR_SZ,
162	        .cid    = 0,    /* Do not set connection ID */
163	        .nonce  = NONCENSE,
164	        .packno = 0x00,
165	        .bits   = GQUIC_PACKNO_LEN_1,
166	        .len    = 1 + 0 + 32 + 1,
167	        .out    = {     (1 << 2)                                        /* Nonce present */
168	                      | 0x00                                            /* Packet number length */
169	                      ,
170	                      NONCENSE_BYTES,
171	                      0x00,                                             /* Packet number */
172	        },
173	    },
174
175	    {
176	        .pf     = select_pf_by_ver(LSQVER_043),
177	        .bufsz  = GQUIC_MAX_PUBHDR_SZ,
178	        .cid    = 0x0102030405060708UL,
179	        .nonce  = NONCENSE,
180	        .packno = 0x00,
181	        .bits   = GQUIC_PACKNO_LEN_1,
182	        .ver.buf= { 'Q', '0', '4', '3', },
183	        .len    = 1 + 8 + 4 + 32 + 1,
184	        .out    = {     (1 << 2)                                        /* Nonce present */
185	                      | 0x01                                            /* Version present */
186	                      | 0x08                                            /* Connection ID present */
187	                      | 0x00                                            /* Packet number length */
188	                      ,
189	                      0x08, 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01,   /* Connection ID */
190	                      'Q', '0', '4', '3',
191	                      NONCENSE_BYTES,
192	                      0x00,                                             /* Packet number */
193	        },
194	    },
195
196	    {
197	        .pf     = select_pf_by_ver(LSQVER_043),
198	        .bufsz  = GQUIC_MAX_PUBHDR_SZ,
199	        .cid    = 0x0102030405060708UL,
200	        .nonce  = NONCENSE,
201	        .packno = 0xA0A1A2A3A4A5A6A7UL,
202	        .bits   = GQUIC_PACKNO_LEN_6,
203	        .len    = 1 + 8 + 32 + 6,
204	        .out    = {     (1 << 2)                                        /* Nonce present */
205	                      | 0x08                                            /* Connection ID present */
206	                      | 0x30                                            /* Packet number length */
207	                      ,
208	                      0x08, 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01,   /* Connection ID */
209	                      NONCENSE_BYTES,
210	                      0xA2, 0xA3, 0xA4, 0xA5, 0xA6, 0xA7,
211	        },
212	    },
213
214	    {
215	        .pf     = select_pf_by_ver(LSQVER_043),
216	        .bufsz  = GQUIC_MAX_PUBHDR_SZ,
217	        .cid    = 0x0102030405060708UL,
218	        .nonce  = NONCENSE,
219	        .packno = 0xA0A1A2A3A4A5A6A7UL,
220	        .bits   = GQUIC_PACKNO_LEN_6,
221	        .len    = 1 + 8 + 32 + 6,
222	        .out    = {     (1 << 2)                                        /* Nonce present */
223	                      | 0x08                                            /* Connection ID present */
224	                      | 0x30                                            /* Packet number length */
225	                      ,
226	                      0x08, 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01,   /* Connection ID */
227	                      NONCENSE_BYTES,
228	                      0xA2, 0xA3, 0xA4, 0xA5, 0xA6, 0xA7,
229	        },
230	    },
231
232	};
233
234    unsigned i;
235    for (i = 0; i < sizeof(tests) / sizeof(tests[0]); ++i)
236        run_test(&tests[i]);
237    return 0;
238}
239