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