test_reg_pkt_headergen.c revision 9a690580
1/* Copyright (c) 2017 - 2020 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 44static const struct test tests[] = { 45 46 { 47 .pf = select_pf_by_ver(LSQVER_043), 48 .bufsz = GQUIC_MAX_PUBHDR_SZ, 49 .cid = 0x0102030405060708UL, 50 .nonce = NULL, 51 .packno = 0x01020304, 52 .bits = GQUIC_PACKNO_LEN_4, 53 .len = 1 + 8 + 0 + 4, 54 .out = { (0 << 2) /* Nonce present */ 55 | 0x08 /* Connection ID present */ 56 | 0x20 /* Packet number length */ 57 , 58 0x08, 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01, /* Connection ID */ 59 0x01, 0x02, 0x03, 0x04, /* Packet number */ 60 }, 61 }, 62 63 { 64 .pf = select_pf_by_ver(LSQVER_043), 65 .bufsz = GQUIC_MAX_PUBHDR_SZ, 66 .cid = 0x0102030405060708UL, 67 .nonce = NULL, 68 .packno = 0x00, 69 .bits = GQUIC_PACKNO_LEN_1, 70 .len = 1 + 8 + 0 + 1, 71 .out = { (0 << 2) /* Nonce present */ 72 | 0x08 /* Connection ID present */ 73 | 0x00 /* Packet number length */ 74 , 75 0x08, 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01, /* Connection ID */ 76 0x00, /* Packet number */ 77 }, 78 }, 79 80 { 81 .pf = select_pf_by_ver(LSQVER_043), 82 .bufsz = GQUIC_MAX_PUBHDR_SZ, 83 .cid = 0x0102030405060708UL, 84 .nonce = NULL, 85 .packno = 0x09, 86 .bits = GQUIC_PACKNO_LEN_1, 87 .ver.buf= { 'Q', '0', '4', '3', }, 88 .len = 1 + 8 + 4 + 0 + 1, 89 .out = { (0 << 2) /* Nonce present */ 90 | 0x01 /* Version present */ 91 | 0x08 /* Connection ID present */ 92 | 0x00 /* Packet number length */ 93 , 94 0x08, 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01, /* Connection ID */ 95 'Q', '0', '4', '3', 96 0x09, /* Packet number */ 97 }, 98 }, 99 100#define NONCENSE "0123456789abcdefghijklmnopqrstuv" 101#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' 102 103 { 104 .pf = select_pf_by_ver(LSQVER_043), 105 .bufsz = GQUIC_MAX_PUBHDR_SZ, 106 .cid = 0x0102030405060708UL, 107 .nonce = NONCENSE, 108 .packno = 0x00, 109 .bits = GQUIC_PACKNO_LEN_1, 110 .len = 1 + 8 + 32 + 1, 111 .out = { (1 << 2) /* Nonce present */ 112 | 0x08 /* Connection ID present */ 113 | 0x00 /* Packet number length */ 114 , 115 0x08, 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01, /* Connection ID */ 116 NONCENSE_BYTES, 117 0x00, /* Packet number */ 118 }, 119 }, 120 121 { 122 .pf = select_pf_by_ver(LSQVER_043), 123 .bufsz = GQUIC_MAX_PUBHDR_SZ, 124 .cid = 0, /* Do not set connection ID */ 125 .nonce = NONCENSE, 126 .packno = 0x00, 127 .bits = GQUIC_PACKNO_LEN_1, 128 .len = 1 + 0 + 32 + 1, 129 .out = { (1 << 2) /* Nonce present */ 130 | 0x00 /* Packet number length */ 131 , 132 NONCENSE_BYTES, 133 0x00, /* Packet number */ 134 }, 135 }, 136 137 { 138 .pf = select_pf_by_ver(LSQVER_043), 139 .bufsz = GQUIC_MAX_PUBHDR_SZ, 140 .cid = 0x0102030405060708UL, 141 .nonce = NONCENSE, 142 .packno = 0x00, 143 .bits = GQUIC_PACKNO_LEN_1, 144 .ver.buf= { 'Q', '0', '4', '3', }, 145 .len = 1 + 8 + 4 + 32 + 1, 146 .out = { (1 << 2) /* Nonce present */ 147 | 0x01 /* Version present */ 148 | 0x08 /* Connection ID present */ 149 | 0x00 /* Packet number length */ 150 , 151 0x08, 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01, /* Connection ID */ 152 'Q', '0', '4', '3', 153 NONCENSE_BYTES, 154 0x00, /* Packet number */ 155 }, 156 }, 157 158 { 159 .pf = select_pf_by_ver(LSQVER_043), 160 .bufsz = GQUIC_MAX_PUBHDR_SZ, 161 .cid = 0x0102030405060708UL, 162 .nonce = NONCENSE, 163 .packno = 0xA0A1A2A3A4A5A6A7UL, 164 .bits = GQUIC_PACKNO_LEN_6, 165 .len = 1 + 8 + 32 + 6, 166 .out = { (1 << 2) /* Nonce present */ 167 | 0x08 /* Connection ID present */ 168 | 0x30 /* Packet number length */ 169 , 170 0x08, 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01, /* Connection ID */ 171 NONCENSE_BYTES, 172 0xA2, 0xA3, 0xA4, 0xA5, 0xA6, 0xA7, 173 }, 174 }, 175 176 { 177 .pf = select_pf_by_ver(LSQVER_043), 178 .bufsz = GQUIC_MAX_PUBHDR_SZ, 179 .cid = 0x0102030405060708UL, 180 .nonce = NONCENSE, 181 .packno = 0xA0A1A2A3A4A5A6A7UL, 182 .bits = GQUIC_PACKNO_LEN_6, 183 .len = 1 + 8 + 32 + 6, 184 .out = { (1 << 2) /* Nonce present */ 185 | 0x08 /* Connection ID present */ 186 | 0x30 /* Packet number length */ 187 , 188 0x08, 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01, /* Connection ID */ 189 NONCENSE_BYTES, 190 0xA2, 0xA3, 0xA4, 0xA5, 0xA6, 0xA7, 191 }, 192 }, 193 194}; 195 196 197static void 198run_test (int i) 199{ 200 const struct test *const test = &tests[i]; 201 202 struct lsquic_packet_out packet_out = 203 { 204 .po_flags = (test->cid ? PO_CONN_ID : 0) 205 | (test->ver.val ? PO_VERSION : 0) 206 | (test->nonce ? PO_NONCE: 0) 207 , 208 .po_nonce = (unsigned char *) test->nonce, 209 .po_ver_tag = test->ver.val, 210 .po_packno = test->packno, 211 }; 212 lsquic_packet_out_set_packno_bits(&packet_out, test->bits); 213 214 lsquic_cid_t cid; 215 memset(&cid, 0, sizeof(cid)); 216 cid.len = sizeof(test->cid); 217 memcpy(cid.idbuf, &test->cid, sizeof(test->cid)); 218 219 struct lsquic_conn lconn = LSCONN_INITIALIZER_CID(lconn, cid); 220 221 unsigned char out[GQUIC_MAX_PUBHDR_SZ]; 222 int len = test->pf->pf_gen_reg_pkt_header(&lconn, &packet_out, out, 223 sizeof(out)); 224 225 assert(("Packet length is correct", len == test->len)); 226 227 if (test->len > 0) 228 assert(("Packet contents are correct", 229 0 == memcmp(out, test->out, len))); 230} 231 232 233int 234main (void) 235{ 236 unsigned i; 237 for (i = 0; i < sizeof(tests) / sizeof(tests[0]); ++i) 238 run_test(i); 239 return 0; 240} 241