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