2021-01-06 14:00:05 +00:00
|
|
|
/* Copyright (c) 2017 - 2021 LiteSpeed Technologies Inc. See LICENSE. */
|
2019-09-11 15:27:58 +00:00
|
|
|
/*
|
|
|
|
* test_trapa.c -- Test transport parameters.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <assert.h>
|
|
|
|
|
|
|
|
#include <stddef.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2020-06-03 04:13:30 +00:00
|
|
|
#ifndef WIN32
|
2019-09-11 15:27:58 +00:00
|
|
|
#include <unistd.h>
|
2020-06-03 04:13:30 +00:00
|
|
|
#else
|
|
|
|
#include "getopt.h"
|
|
|
|
#endif
|
2019-09-11 15:27:58 +00:00
|
|
|
|
|
|
|
#include "lsquic.h"
|
|
|
|
#include "lsquic_types.h"
|
|
|
|
#include "lsquic_sizes.h"
|
|
|
|
#include "lsquic_logger.h"
|
|
|
|
#include "lsquic_trans_params.h"
|
|
|
|
|
|
|
|
#define ENC_BUF_SZ 0x1000
|
|
|
|
|
|
|
|
struct trapa_test
|
|
|
|
{
|
|
|
|
int line;
|
|
|
|
enum {
|
|
|
|
TEST_ENCODE = 1 << 0,
|
|
|
|
TEST_DECODE = 1 << 1,
|
|
|
|
} flags;
|
|
|
|
struct transport_params params;
|
|
|
|
size_t enc_len, dec_len;
|
2020-02-13 14:36:04 +00:00
|
|
|
unsigned addl_set;
|
|
|
|
int is_server;
|
2019-09-11 15:27:58 +00:00
|
|
|
int expect_decode_err;
|
|
|
|
unsigned char encoded[ENC_BUF_SZ];
|
|
|
|
};
|
|
|
|
|
|
|
|
static const struct trapa_test tests[] =
|
|
|
|
{
|
|
|
|
|
|
|
|
{
|
|
|
|
.line = __LINE__,
|
|
|
|
.flags = TEST_ENCODE | TEST_DECODE,
|
|
|
|
.params = {
|
|
|
|
TP_DEFAULT_VALUES,
|
|
|
|
},
|
2020-02-24 17:02:57 +00:00
|
|
|
.enc_len = 0,
|
2019-09-11 15:27:58 +00:00
|
|
|
.encoded =
|
|
|
|
/* Trailer to make the end easily visible in gdb: */
|
|
|
|
"\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF"
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
.line = __LINE__,
|
|
|
|
.flags = TEST_ENCODE | TEST_DECODE,
|
|
|
|
.params = {
|
2020-02-13 14:36:04 +00:00
|
|
|
.tp_set = (1 << TPI_INIT_MAX_STREAM_DATA_BIDI_LOCAL)
|
|
|
|
| (1 << TPI_INIT_MAX_DATA)
|
|
|
|
| (1 << TPI_MAX_IDLE_TIMEOUT)
|
|
|
|
| (1 << TPI_MAX_ACK_DELAY)
|
2020-05-27 14:26:32 +00:00
|
|
|
| (1 << TPI_MAX_UDP_PAYLOAD_SIZE)
|
2020-02-13 14:36:04 +00:00
|
|
|
| (1 << TPI_ACK_DELAY_EXPONENT)
|
2020-05-27 14:26:32 +00:00
|
|
|
| (1 << TPI_INITIAL_SOURCE_CID)
|
2020-02-13 14:36:04 +00:00
|
|
|
| (1 << TPI_ACTIVE_CONNECTION_ID_LIMIT),
|
2019-09-11 15:27:58 +00:00
|
|
|
.tp_init_max_stream_data_bidi_local = 0x12348877,
|
|
|
|
.tp_init_max_data = 0xAABB,
|
2020-05-27 14:26:32 +00:00
|
|
|
.tp_max_udp_payload_size = 1213,
|
2020-01-28 14:35:09 +00:00
|
|
|
.tp_max_idle_timeout = 10 * 1000,
|
2019-09-11 15:27:58 +00:00
|
|
|
.tp_max_ack_delay = TP_DEF_MAX_ACK_DELAY,
|
|
|
|
.tp_active_connection_id_limit = 7,
|
2020-05-27 14:26:32 +00:00
|
|
|
.tp_initial_source_cid = { .len = 8, .u_cid.id = 0x0807060504030201ull, },
|
2019-09-11 15:27:58 +00:00
|
|
|
},
|
2020-02-13 14:36:04 +00:00
|
|
|
.is_server = 0,
|
2020-05-27 14:26:32 +00:00
|
|
|
.enc_len = 36,
|
2019-09-11 15:27:58 +00:00
|
|
|
.encoded =
|
2020-02-24 17:02:57 +00:00
|
|
|
/* Idle timeout */ "\x01\x02\x67\x10"
|
2020-04-07 15:25:43 +00:00
|
|
|
/* Packet size */ "\x03\x02\x44\xBD"
|
2020-02-24 17:02:57 +00:00
|
|
|
/* Max data */ "\x04\x04\x80\x00\xAA\xBB"
|
|
|
|
/* Bidi local */ "\x05\x04\x92\x34\x88\x77"
|
|
|
|
/* Ack delay exp */ "\x0A\x01\x00"
|
|
|
|
/* Active CID limit */ "\x0E\x01\x07"
|
2020-05-27 14:26:32 +00:00
|
|
|
/* Initial SCID */ "\x0F\x08\x01\x02\x03\x04\x05\x06\x07\x08"
|
2019-09-11 15:27:58 +00:00
|
|
|
/* Trailer to make the end easily visible in gdb: */
|
|
|
|
"\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF"
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
.line = __LINE__,
|
|
|
|
.flags = TEST_DECODE,
|
|
|
|
.dec_len = 1,
|
|
|
|
.expect_decode_err = 1,
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
.line = __LINE__,
|
|
|
|
.flags = TEST_DECODE,
|
|
|
|
.dec_len = 3,
|
|
|
|
.expect_decode_err = 1,
|
|
|
|
.encoded = "\x00\x04",
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
|
|
{
|
|
|
|
.line = __LINE__,
|
|
|
|
.flags = TEST_ENCODE | TEST_DECODE,
|
|
|
|
.params = {
|
|
|
|
TP_DEFAULT_VALUES,
|
|
|
|
.tp_init_max_data = 0x123456,
|
|
|
|
.tp_init_max_stream_data_bidi_local = 0xABCDEF88,
|
2020-05-27 14:26:32 +00:00
|
|
|
.tp_max_udp_payload_size = 0x555,
|
2019-09-11 15:27:58 +00:00
|
|
|
},
|
2020-02-13 14:36:04 +00:00
|
|
|
.is_server = 1,
|
|
|
|
.addl_set = 1 << TPI_DISABLE_ACTIVE_MIGRATION,
|
2020-02-24 17:02:57 +00:00
|
|
|
.enc_len = 22,
|
2019-09-11 15:27:58 +00:00
|
|
|
.encoded =
|
2020-04-07 15:25:43 +00:00
|
|
|
/* Packet size */ "\x03\x02\x45\x55"
|
2020-02-24 17:02:57 +00:00
|
|
|
/* Max data */ "\x04\x04\x80\x12\x34\x56"
|
|
|
|
/* Bidi local */ "\x05\x08\xC0\x00\x00\x00\xAB\xCD\xEF\x88"
|
|
|
|
/* Migration */ "\x0C\x00"
|
2019-09-11 15:27:58 +00:00
|
|
|
/* Trailer to make the end easily visible in gdb: */
|
|
|
|
"\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF"
|
|
|
|
},
|
|
|
|
|
|
|
|
/* Test server preferred address. */
|
|
|
|
{
|
|
|
|
.line = __LINE__,
|
|
|
|
.flags = TEST_DECODE,
|
|
|
|
.params = {
|
|
|
|
TP_DEFAULT_VALUES,
|
|
|
|
.tp_max_ack_delay = 25,
|
2020-05-27 14:26:32 +00:00
|
|
|
.tp_max_udp_payload_size = 0x555,
|
2019-09-11 15:27:58 +00:00
|
|
|
.tp_preferred_address = {
|
|
|
|
.ipv4_addr = "\x01\x02\x03\x04",
|
|
|
|
.ipv4_port = 0x1234,
|
|
|
|
.ipv6_addr = "\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0A\x0B\x0C\x0D\x0E\x0F",
|
|
|
|
.ipv6_port = 0x9001,
|
|
|
|
.cid = { .len = 11, .idbuf = "\x20\x21\x22\x23\x24\x25\x26\x27\x28\x29\x2A", },
|
|
|
|
.srst = "\x30\x31\x32\x33\x34\x35\x36\x37\x38\x39\x3A\x3B\x3C\x3D\x3E\x3F",
|
|
|
|
},
|
|
|
|
},
|
2020-02-13 14:36:04 +00:00
|
|
|
.is_server = 1,
|
|
|
|
.addl_set = 1 << TPI_PREFERRED_ADDRESS,
|
2020-02-24 17:02:57 +00:00
|
|
|
.enc_len = 0x3A,
|
|
|
|
.dec_len = 0x3A,
|
2019-09-11 15:27:58 +00:00
|
|
|
.encoded =
|
2020-02-24 17:02:57 +00:00
|
|
|
/* Preferred Address */"\x0D"
|
|
|
|
"\x34"
|
2019-09-11 15:27:58 +00:00
|
|
|
"\x01\x02\x03\x04"
|
|
|
|
"\x12\x34"
|
|
|
|
"\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0A\x0B\x0C\x0D\x0E\x0F"
|
|
|
|
"\x90\x01"
|
|
|
|
"\x0B" /* CID len */
|
|
|
|
"\x20\x21\x22\x23\x24\x25\x26\x27\x28\x29\x2A"
|
|
|
|
"\x30\x31\x32\x33\x34\x35\x36\x37\x38\x39\x3A\x3B\x3C\x3D\x3E\x3F"
|
2020-04-07 15:25:43 +00:00
|
|
|
/* Packet size */ "\x03\x02\x45\x55"
|
2019-09-11 15:27:58 +00:00
|
|
|
/* Trailer to make the end easily visible in gdb: */
|
|
|
|
"\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF"
|
|
|
|
},
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
static int
|
|
|
|
params_are_equal (const struct transport_params *a,
|
|
|
|
const struct transport_params *b)
|
|
|
|
{
|
|
|
|
#define MCMP(f) 0 == memcmp(&a->f, &b->f, sizeof(a->f))
|
2020-02-13 14:36:04 +00:00
|
|
|
return MCMP(tp_numerics)
|
|
|
|
&& MCMP(tp_set)
|
2019-09-11 15:27:58 +00:00
|
|
|
&& MCMP(tp_stateless_reset_token)
|
|
|
|
&& MCMP(tp_preferred_address.ipv4_addr)
|
|
|
|
&& MCMP(tp_preferred_address.ipv6_addr)
|
|
|
|
&& MCMP(tp_preferred_address.srst)
|
|
|
|
&& MCMP(tp_preferred_address.cid.idbuf)
|
|
|
|
&& a->tp_preferred_address.ipv4_port == b->tp_preferred_address.ipv4_port
|
|
|
|
&& a->tp_preferred_address.ipv6_port == b->tp_preferred_address.ipv6_port
|
|
|
|
&& a->tp_preferred_address.cid.len == b->tp_preferred_address.cid.len
|
2020-05-27 14:26:32 +00:00
|
|
|
&& MCMP(tp_original_dest_cid.idbuf)
|
|
|
|
&& a->tp_original_dest_cid.len == b->tp_original_dest_cid.len
|
2019-09-11 15:27:58 +00:00
|
|
|
;
|
|
|
|
#undef MCMP
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
run_test (const struct trapa_test *test)
|
|
|
|
{
|
2020-02-13 14:36:04 +00:00
|
|
|
struct transport_params source_params;
|
2019-09-11 15:27:58 +00:00
|
|
|
struct transport_params decoded_params;
|
|
|
|
size_t dec_len;
|
|
|
|
int s;
|
|
|
|
unsigned char buf[ENC_BUF_SZ];
|
|
|
|
|
2020-02-13 14:36:04 +00:00
|
|
|
source_params = test->params;
|
|
|
|
source_params.tp_set |= test->addl_set;
|
|
|
|
|
2019-09-11 15:27:58 +00:00
|
|
|
if (test->flags & TEST_ENCODE)
|
|
|
|
{
|
2020-02-13 14:36:04 +00:00
|
|
|
s = lsquic_tp_encode(&source_params, test->is_server, buf, sizeof(buf));
|
2020-02-24 17:02:57 +00:00
|
|
|
assert(s >= 0);
|
2019-09-11 15:27:58 +00:00
|
|
|
assert((size_t) s == test->enc_len);
|
|
|
|
assert(0 == memcmp(test->encoded, buf, s));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (test->flags & TEST_DECODE)
|
|
|
|
{
|
|
|
|
if (test->dec_len)
|
|
|
|
dec_len = test->dec_len;
|
|
|
|
else
|
2020-02-24 17:02:57 +00:00
|
|
|
dec_len = test->enc_len;
|
2019-09-11 15:27:58 +00:00
|
|
|
s = lsquic_tp_decode(test->encoded, dec_len,
|
2020-02-13 14:36:04 +00:00
|
|
|
test->is_server, &decoded_params);
|
2019-09-11 15:27:58 +00:00
|
|
|
if (!test->expect_decode_err)
|
|
|
|
{
|
2020-02-24 17:02:57 +00:00
|
|
|
assert(s >= 0);
|
2019-09-11 15:27:58 +00:00
|
|
|
assert((size_t) s == test->enc_len);
|
2020-02-13 14:36:04 +00:00
|
|
|
/* The decoder initializes all default values, so set the flag
|
|
|
|
* accordingly:
|
|
|
|
*/
|
|
|
|
source_params.tp_set |= ((1 << (MAX_NUM_WITH_DEF_TPI + 1)) - 1);
|
|
|
|
s = params_are_equal(&source_params, &decoded_params);
|
2019-09-11 15:27:58 +00:00
|
|
|
assert(s);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
assert(s < 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
decode_file (const char *name)
|
|
|
|
{
|
|
|
|
FILE *file;
|
|
|
|
size_t nread;
|
|
|
|
int s;
|
|
|
|
struct transport_params params;
|
|
|
|
unsigned char buf[0x1000];
|
|
|
|
|
|
|
|
file = fopen(name, "rb");
|
|
|
|
if (!file)
|
|
|
|
{
|
|
|
|
perror("fopen");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
nread = fread(buf, 1, sizeof(buf), file);
|
|
|
|
|
|
|
|
s = lsquic_tp_decode(buf, nread, 0, ¶ms);
|
|
|
|
|
|
|
|
fclose(file);
|
|
|
|
|
|
|
|
printf("decoded params from %s: %d (%s)\n", name, s, s > 0 ? "OK" : "FAIL");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
main (int argc, char **argv)
|
|
|
|
{
|
|
|
|
unsigned i;
|
|
|
|
int opt;
|
|
|
|
|
|
|
|
while (-1 != (opt = getopt(argc, argv, "d:l:")))
|
|
|
|
{
|
|
|
|
switch (opt)
|
|
|
|
{
|
|
|
|
case 'd':
|
|
|
|
decode_file(optarg);
|
|
|
|
return 0;
|
|
|
|
case 'l':
|
|
|
|
lsquic_log_to_fstream(stderr, 0);
|
|
|
|
lsquic_logger_lopt(optarg);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < sizeof(tests) / sizeof(tests[0]); ++i)
|
|
|
|
run_test(&tests[i]);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|