1a74702c6SGeorge Wang/* Copyright (c) 2017 - 2022 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
950aadb33SDmitri Tikhonov
1050aadb33SDmitri Tikhonov#include "lsquic.h"
1150aadb33SDmitri Tikhonov#include "lsquic_types.h"
1250aadb33SDmitri Tikhonov#include "lsquic_parse.h"
1350aadb33SDmitri Tikhonov
14f07b3eaeSTyler Young//static const struct parse_funcs *const pf = select_pf_by_ver(LSQVER_043); // will not work on MSVC
15f07b3eaeSTyler Young#define pf ((const struct parse_funcs *const)select_pf_by_ver(LSQVER_043))
1650aadb33SDmitri Tikhonov
1750aadb33SDmitri Tikhonov
1850aadb33SDmitri Tikhonovstruct conn_close_parse_test {
1950aadb33SDmitri Tikhonov    int             lineno;
2050aadb33SDmitri Tikhonov    unsigned char   buf[0x100];
2150aadb33SDmitri Tikhonov    size_t          buf_len;
2250aadb33SDmitri Tikhonov    uint32_t        error_code;
2350aadb33SDmitri Tikhonov    uint16_t        reason_len;
2450aadb33SDmitri Tikhonov    uint8_t         reason_off;
2550aadb33SDmitri Tikhonov    int             retval;
2650aadb33SDmitri Tikhonov};
2750aadb33SDmitri Tikhonov
2850aadb33SDmitri Tikhonovstatic const struct conn_close_parse_test parse_tests[] = {
2950aadb33SDmitri Tikhonov
3050aadb33SDmitri Tikhonov    {
3150aadb33SDmitri Tikhonov        .lineno         = __LINE__,
3250aadb33SDmitri Tikhonov        .buf            = { 0x02, 0x00, 0x00, 0x00, 0x31, 0x00, 0x05, 'D', 'u', 'd', 'e', '!', },
3350aadb33SDmitri Tikhonov        .buf_len        = 0x100,
3450aadb33SDmitri Tikhonov        .error_code     = 0x31,
3550aadb33SDmitri Tikhonov        .reason_len     = 0x05,
3650aadb33SDmitri Tikhonov        .reason_off     = 7,
3750aadb33SDmitri Tikhonov        .retval         = 7 + 5,
3850aadb33SDmitri Tikhonov    },
3950aadb33SDmitri Tikhonov
4050aadb33SDmitri Tikhonov    {
4150aadb33SDmitri Tikhonov        .lineno         = __LINE__,
4250aadb33SDmitri Tikhonov        .buf            = { 0x02, 0x00, 0x00, 0x00, 0x31, 0x00, 0x05, 'D', 'u', 'd', 'e', '!', },
4350aadb33SDmitri Tikhonov        .buf_len        = 6,    /* Too short #1 */
4450aadb33SDmitri Tikhonov        .error_code     = 0x31,
4550aadb33SDmitri Tikhonov        .reason_len     = 0x05,
4650aadb33SDmitri Tikhonov        .reason_off     = 7,
4750aadb33SDmitri Tikhonov        .retval         = -1,
4850aadb33SDmitri Tikhonov    },
4950aadb33SDmitri Tikhonov
5050aadb33SDmitri Tikhonov    {
5150aadb33SDmitri Tikhonov        .lineno         = __LINE__,
5250aadb33SDmitri Tikhonov        .buf            = { 0x02, 0x00, 0x00, 0x00, 0x31, 0x00, 0x05, 'D', 'u', 'd', 'e', '!', },
5350aadb33SDmitri Tikhonov        .buf_len        = 9,    /* Too short #2 */
5450aadb33SDmitri Tikhonov        .error_code     = 0x31,
5550aadb33SDmitri Tikhonov        .reason_len     = 0x05,
5650aadb33SDmitri Tikhonov        .reason_off     = 7,
5750aadb33SDmitri Tikhonov        .retval         = -2,
5850aadb33SDmitri Tikhonov    },
5950aadb33SDmitri Tikhonov
6050aadb33SDmitri Tikhonov    {   .buf            = { 0 },    }
6150aadb33SDmitri Tikhonov};
6250aadb33SDmitri Tikhonov
6350aadb33SDmitri Tikhonov
6450aadb33SDmitri Tikhonovstruct conn_close_gen_test {
6550aadb33SDmitri Tikhonov    int             lineno;
6650aadb33SDmitri Tikhonov    uint32_t        error_code;
6750aadb33SDmitri Tikhonov    const char     *reason;
6850aadb33SDmitri Tikhonov    int             retval;
6950aadb33SDmitri Tikhonov    unsigned char   buf[0x100];
7050aadb33SDmitri Tikhonov    size_t          buf_len;
7150aadb33SDmitri Tikhonov};
7250aadb33SDmitri Tikhonov
7350aadb33SDmitri Tikhonov
7450aadb33SDmitri Tikhonovstatic const struct conn_close_gen_test gen_tests[] = {
7550aadb33SDmitri Tikhonov
7650aadb33SDmitri Tikhonov    {
7750aadb33SDmitri Tikhonov        .lineno         = __LINE__,
7850aadb33SDmitri Tikhonov        .error_code     = 0x12345678,
7950aadb33SDmitri Tikhonov        .reason         = "Dude, where is my car?",
8050aadb33SDmitri Tikhonov        .retval         = 7 + sizeof("Dude, where is my car?") - 1,
8150aadb33SDmitri Tikhonov        .buf_len        = 0x100,
8250aadb33SDmitri Tikhonov        .buf            = {
8350aadb33SDmitri Tikhonov            0x02,
8450aadb33SDmitri Tikhonov            0x12, 0x34, 0x56, 0x78,
8550aadb33SDmitri Tikhonov            0x00, sizeof("Dude, where is my car?") - 1,
8650aadb33SDmitri Tikhonov            'D', 'u', 'd', 'e', ',', ' ', 'w', 'h', 'e', 'r', 'e', ' ', 'i', 's', ' ', 'm', 'y', ' ', 'c', 'a', 'r', '?',
8750aadb33SDmitri Tikhonov        },
8850aadb33SDmitri Tikhonov    },
8950aadb33SDmitri Tikhonov
9050aadb33SDmitri Tikhonov    {
9150aadb33SDmitri Tikhonov        .lineno         = __LINE__,
9250aadb33SDmitri Tikhonov        .error_code     = 0x12345678,
9350aadb33SDmitri Tikhonov        .reason         = NULL,
9450aadb33SDmitri Tikhonov        .retval         = 7,
9550aadb33SDmitri Tikhonov        .buf_len        = 0x100,
9650aadb33SDmitri Tikhonov        .buf            = {
9750aadb33SDmitri Tikhonov            0x02,
9850aadb33SDmitri Tikhonov            0x12, 0x34, 0x56, 0x78,
9950aadb33SDmitri Tikhonov            0x00, 0x00, /* Zero-sized string */
10050aadb33SDmitri Tikhonov        },
10150aadb33SDmitri Tikhonov    },
10250aadb33SDmitri Tikhonov
10350aadb33SDmitri Tikhonov    {
10450aadb33SDmitri Tikhonov        .lineno         = __LINE__,
10550aadb33SDmitri Tikhonov        .error_code     = 0x12345678,
10650aadb33SDmitri Tikhonov        .reason         = "Dude, where is my car?",
10750aadb33SDmitri Tikhonov        .retval         = -1,   /* Too short */
10850aadb33SDmitri Tikhonov        .buf_len        = 0x10,
10950aadb33SDmitri Tikhonov    },
11050aadb33SDmitri Tikhonov
11150aadb33SDmitri Tikhonov    {   .buf            = { 0 },    }
11250aadb33SDmitri Tikhonov
11350aadb33SDmitri Tikhonov};
11450aadb33SDmitri Tikhonov
11550aadb33SDmitri Tikhonov
11650aadb33SDmitri Tikhonovstatic void
11750aadb33SDmitri Tikhonovrun_parse_tests (void)
11850aadb33SDmitri Tikhonov{
11950aadb33SDmitri Tikhonov    const struct conn_close_parse_test *test;
12050aadb33SDmitri Tikhonov    for (test = parse_tests; test->buf[0]; ++test)
12150aadb33SDmitri Tikhonov    {
1225392f7a3SLiteSpeed Tech        uint64_t error_code = ~0;
12350aadb33SDmitri Tikhonov        uint16_t reason_len = ~0;
12450aadb33SDmitri Tikhonov        uint8_t reason_off = ~0;
12550aadb33SDmitri Tikhonov        int sz = pf->pf_parse_connect_close_frame(test->buf, test->buf_len,
1265392f7a3SLiteSpeed Tech                                NULL, &error_code, &reason_len, &reason_off);
12750aadb33SDmitri Tikhonov        assert(sz == test->retval);
12850aadb33SDmitri Tikhonov        if (0 == sz)
12950aadb33SDmitri Tikhonov        {
13050aadb33SDmitri Tikhonov            assert(test->error_code == error_code);
13150aadb33SDmitri Tikhonov            assert(test->reason_len == reason_len);
13250aadb33SDmitri Tikhonov            assert(test->reason_off == reason_off);
13350aadb33SDmitri Tikhonov        }
13450aadb33SDmitri Tikhonov    }
13550aadb33SDmitri Tikhonov}
13650aadb33SDmitri Tikhonov
13750aadb33SDmitri Tikhonov
13850aadb33SDmitri Tikhonovstatic void
13950aadb33SDmitri Tikhonovrun_gen_tests (void)
14050aadb33SDmitri Tikhonov{
14150aadb33SDmitri Tikhonov    const struct conn_close_gen_test *test;
14250aadb33SDmitri Tikhonov    for (test = gen_tests; test->buf[0]; ++test)
14350aadb33SDmitri Tikhonov    {
14450aadb33SDmitri Tikhonov        unsigned char buf[0x100];
14550aadb33SDmitri Tikhonov        int sz = pf->pf_gen_connect_close_frame(buf, sizeof(buf),
1465392f7a3SLiteSpeed Tech                    0, test->error_code, test->reason,
14750aadb33SDmitri Tikhonov                    test->reason ? strlen(test->reason) : 0);
14850aadb33SDmitri Tikhonov        assert(sz == test->retval);
14950aadb33SDmitri Tikhonov        if (0 == sz)
15050aadb33SDmitri Tikhonov            assert(0 == memcmp(test->buf, buf, sz));
15150aadb33SDmitri Tikhonov    }
15250aadb33SDmitri Tikhonov}
15350aadb33SDmitri Tikhonov
15450aadb33SDmitri Tikhonov
15550aadb33SDmitri Tikhonovint
15650aadb33SDmitri Tikhonovmain (void)
15750aadb33SDmitri Tikhonov{
15850aadb33SDmitri Tikhonov    run_parse_tests();
15950aadb33SDmitri Tikhonov    run_gen_tests();
16050aadb33SDmitri Tikhonov    return 0;
16150aadb33SDmitri Tikhonov}
162