1229fce07SDmitri Tikhonov/* Copyright (c) 2017 - 2019 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_alarmset.h"
1350aadb33SDmitri Tikhonov#include "lsquic_parse.h"
1450aadb33SDmitri Tikhonov
15052a1c28SDmitri Tikhonovstatic const struct parse_funcs *const pf = select_pf_by_ver(LSQVER_035);
1650aadb33SDmitri Tikhonov
1750aadb33SDmitri Tikhonov
1850aadb33SDmitri Tikhonov/* The test is both for generation and parsing: */
1950aadb33SDmitri Tikhonovstruct wuf_test {
2050aadb33SDmitri Tikhonov    unsigned char   buf[0x20];
2150aadb33SDmitri Tikhonov    size_t          buf_len;
2250aadb33SDmitri Tikhonov    uint32_t        stream_id;
2350aadb33SDmitri Tikhonov    uint64_t        offset;
2450aadb33SDmitri Tikhonov    uint32_t        error_code;
2550aadb33SDmitri Tikhonov};
2650aadb33SDmitri Tikhonov
2750aadb33SDmitri Tikhonovstatic const struct wuf_test wuf_tests[] = {
2850aadb33SDmitri Tikhonov    {
2950aadb33SDmitri Tikhonov        .buf            = { 0x01, 0x34, 0x45, 0x67, 0x00, 0x05, 0x04, 0x03, 0x02, 0x01, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, },
3050aadb33SDmitri Tikhonov        .buf_len        = QUIC_RST_STREAM_SZ,
3150aadb33SDmitri Tikhonov        .stream_id      = 0x674534,
3250aadb33SDmitri Tikhonov        .offset         = 0x0102030405,
3350aadb33SDmitri Tikhonov        .error_code     = 0x03,
3450aadb33SDmitri Tikhonov    },
3550aadb33SDmitri Tikhonov
3650aadb33SDmitri Tikhonov    {   .buf            = { 0 },    }
3750aadb33SDmitri Tikhonov};
3850aadb33SDmitri Tikhonov
3950aadb33SDmitri Tikhonov
4050aadb33SDmitri Tikhonovstatic void
4150aadb33SDmitri Tikhonovrun_parse_tests (void)
4250aadb33SDmitri Tikhonov{
4350aadb33SDmitri Tikhonov    const struct wuf_test *test;
4450aadb33SDmitri Tikhonov    for (test = wuf_tests; test->buf[0]; ++test)
4550aadb33SDmitri Tikhonov    {
4650aadb33SDmitri Tikhonov        uint32_t stream_id = ~0;
4750aadb33SDmitri Tikhonov        uint64_t offset = ~0;
4850aadb33SDmitri Tikhonov        uint32_t error_code = ~0;
4950aadb33SDmitri Tikhonov        int sz = pf->pf_parse_rst_frame(test->buf, test->buf_len, &stream_id, &offset, &error_code);
5050aadb33SDmitri Tikhonov        assert(sz == QUIC_RST_STREAM_SZ);
5150aadb33SDmitri Tikhonov        assert(stream_id == test->stream_id);
5250aadb33SDmitri Tikhonov        assert(offset == test->offset);
5350aadb33SDmitri Tikhonov        assert(error_code == test->error_code);
5450aadb33SDmitri Tikhonov    }
5550aadb33SDmitri Tikhonov}
5650aadb33SDmitri Tikhonov
5750aadb33SDmitri Tikhonov
5850aadb33SDmitri Tikhonovstatic void
5950aadb33SDmitri Tikhonovrun_gen_tests (void)
6050aadb33SDmitri Tikhonov{
6150aadb33SDmitri Tikhonov    const struct wuf_test *test;
6250aadb33SDmitri Tikhonov    for (test = wuf_tests; test->buf[0]; ++test)
6350aadb33SDmitri Tikhonov    {
6450aadb33SDmitri Tikhonov        unsigned char buf[0x100];
6550aadb33SDmitri Tikhonov        int sz = pf->pf_gen_rst_frame(buf, test->buf_len, test->stream_id, test->offset, test->error_code);
6650aadb33SDmitri Tikhonov        assert(sz == QUIC_RST_STREAM_SZ);
6750aadb33SDmitri Tikhonov        assert(0 == memcmp(buf, test->buf, sz));
6850aadb33SDmitri Tikhonov    }
6950aadb33SDmitri Tikhonov}
7050aadb33SDmitri Tikhonov
7150aadb33SDmitri Tikhonov
7250aadb33SDmitri Tikhonovint
7350aadb33SDmitri Tikhonovmain (void)
7450aadb33SDmitri Tikhonov{
7550aadb33SDmitri Tikhonov    run_parse_tests();
7650aadb33SDmitri Tikhonov    run_gen_tests();
7750aadb33SDmitri Tikhonov    return 0;
7850aadb33SDmitri Tikhonov}
79