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