1/* Copyright (c) 2017 - 2022 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
10#include "lsquic.h"
11#include "lsquic_types.h"
12#include "lsquic_parse.h"
13
14//static const struct parse_funcs *const pf = select_pf_by_ver(LSQVER_043); // will not work on MSVC
15#define pf ((const struct parse_funcs *const)select_pf_by_ver(LSQVER_043))
16
17
18/* The test is both for generation and parsing: */
19struct wuf_test {
20    unsigned char   buf[0x20];
21    size_t          buf_len;
22    lsquic_stream_id_t        stream_id;
23    uint64_t        offset;
24    uint64_t        error_code;
25};
26
27static const struct wuf_test wuf_tests[] = {
28    {
29        .buf            = { 0x01, 0x00, 0x67, 0x45, 0x34, 0x00, 0x00, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x00, 0x00, 0x00, 0x03, },
30        .buf_len        = GQUIC_RST_STREAM_SZ,
31        .stream_id      = 0x674534,
32        .offset         = 0x0102030405,
33        .error_code     = 0x03,
34    },
35
36    {   .buf            = { 0 },    }
37};
38
39
40static void
41run_parse_tests (void)
42{
43    const struct wuf_test *test;
44    for (test = wuf_tests; test->buf[0]; ++test)
45    {
46        lsquic_stream_id_t stream_id = ~0;
47        uint64_t offset = ~0, error_code = ~0;
48        int sz = pf->pf_parse_rst_frame(test->buf, test->buf_len, &stream_id, &offset, &error_code);
49        assert(sz == GQUIC_RST_STREAM_SZ);
50        assert(stream_id == test->stream_id);
51        assert(offset == test->offset);
52        assert(error_code == test->error_code);
53    }
54}
55
56
57static void
58run_gen_tests (void)
59{
60    const struct wuf_test *test;
61    for (test = wuf_tests; test->buf[0]; ++test)
62    {
63        unsigned char buf[0x100];
64        int sz = pf->pf_gen_rst_frame(buf, test->buf_len, test->stream_id, test->offset, test->error_code);
65        assert(sz == GQUIC_RST_STREAM_SZ);
66        assert(0 == memcmp(buf, test->buf, sz));
67    }
68}
69
70
71int
72main (void)
73{
74    run_parse_tests();
75    run_gen_tests();
76    return 0;
77}
78