test_rst_stream_ietf.c revision a74702c6
1a74702c6SGeorge Wang/* Copyright (c) 2017 - 2022 LiteSpeed Technologies Inc.  See LICENSE. */
25392f7a3SLiteSpeed Tech#include <assert.h>
35392f7a3SLiteSpeed Tech#include <stdio.h>
45392f7a3SLiteSpeed Tech#include <stdlib.h>
55392f7a3SLiteSpeed Tech#include <string.h>
65392f7a3SLiteSpeed Tech#ifndef WIN32
75392f7a3SLiteSpeed Tech#include <sys/time.h>
85392f7a3SLiteSpeed Tech#endif
95392f7a3SLiteSpeed Tech
105392f7a3SLiteSpeed Tech#include "lsquic.h"
115392f7a3SLiteSpeed Tech#include "lsquic_types.h"
125392f7a3SLiteSpeed Tech#include "lsquic_parse.h"
135392f7a3SLiteSpeed Tech
145392f7a3SLiteSpeed Techstatic const struct parse_funcs *const pf = select_pf_by_ver(LSQVER_041);
155392f7a3SLiteSpeed Tech
165392f7a3SLiteSpeed Tech
175392f7a3SLiteSpeed Tech/* The test is both for generation and parsing: */
185392f7a3SLiteSpeed Techstruct rst_stream_test {
195392f7a3SLiteSpeed Tech    unsigned char   buf[0x20];
205392f7a3SLiteSpeed Tech    size_t          buf_len;
215392f7a3SLiteSpeed Tech    lsquic_stream_id_t        stream_id;
225392f7a3SLiteSpeed Tech    uint64_t        offset;
235392f7a3SLiteSpeed Tech    uint32_t        error_code;
245392f7a3SLiteSpeed Tech};
255392f7a3SLiteSpeed Tech
265392f7a3SLiteSpeed Techstatic const struct rst_stream_test rst_stream_tests[] = {
275392f7a3SLiteSpeed Tech    {
285392f7a3SLiteSpeed Tech        .buf            = { 0x01,
295392f7a3SLiteSpeed Tech                            0x00, 0x67, 0x45, 0x34,
305392f7a3SLiteSpeed Tech                            0x00, 0x00, 0x00, 0x03,
315392f7a3SLiteSpeed Tech                            0x00, 0x00, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05,
325392f7a3SLiteSpeed Tech                          },
335392f7a3SLiteSpeed Tech        .buf_len        = GQUIC_RST_STREAM_SZ,
345392f7a3SLiteSpeed Tech        .stream_id      = 0x674534,
355392f7a3SLiteSpeed Tech        .offset         = 0x0102030405,
365392f7a3SLiteSpeed Tech        .error_code     = 0x03,
375392f7a3SLiteSpeed Tech    },
385392f7a3SLiteSpeed Tech
395392f7a3SLiteSpeed Tech    {   .buf            = { 0 },    }
405392f7a3SLiteSpeed Tech};
415392f7a3SLiteSpeed Tech
425392f7a3SLiteSpeed Tech
435392f7a3SLiteSpeed Techstatic void
445392f7a3SLiteSpeed Techrun_parse_tests (void)
455392f7a3SLiteSpeed Tech{
465392f7a3SLiteSpeed Tech    const struct rst_stream_test *test;
475392f7a3SLiteSpeed Tech    for (test = rst_stream_tests; test->buf[0]; ++test)
485392f7a3SLiteSpeed Tech    {
495392f7a3SLiteSpeed Tech        lsquic_stream_id_t stream_id = ~0;
505392f7a3SLiteSpeed Tech        uint64_t offset = ~0;
515392f7a3SLiteSpeed Tech        uint32_t error_code = ~0;
525392f7a3SLiteSpeed Tech        int sz = pf->pf_parse_rst_frame(test->buf, test->buf_len, &stream_id, &offset, &error_code);
535392f7a3SLiteSpeed Tech        assert(sz == GQUIC_RST_STREAM_SZ);
545392f7a3SLiteSpeed Tech        assert(stream_id == test->stream_id);
555392f7a3SLiteSpeed Tech        assert(offset == test->offset);
565392f7a3SLiteSpeed Tech        assert(error_code == test->error_code);
575392f7a3SLiteSpeed Tech    }
585392f7a3SLiteSpeed Tech}
595392f7a3SLiteSpeed Tech
605392f7a3SLiteSpeed Tech
615392f7a3SLiteSpeed Techstatic void
625392f7a3SLiteSpeed Techrun_gen_tests (void)
635392f7a3SLiteSpeed Tech{
645392f7a3SLiteSpeed Tech    const struct rst_stream_test *test;
655392f7a3SLiteSpeed Tech    for (test = rst_stream_tests; test->buf[0]; ++test)
665392f7a3SLiteSpeed Tech    {
675392f7a3SLiteSpeed Tech        unsigned char buf[0x100];
685392f7a3SLiteSpeed Tech        int sz = pf->pf_gen_rst_frame(buf, test->buf_len, test->stream_id, test->offset, test->error_code);
695392f7a3SLiteSpeed Tech        assert(sz == GQUIC_RST_STREAM_SZ);
705392f7a3SLiteSpeed Tech        assert(0 == memcmp(buf, test->buf, sz));
715392f7a3SLiteSpeed Tech    }
725392f7a3SLiteSpeed Tech}
735392f7a3SLiteSpeed Tech
745392f7a3SLiteSpeed Tech
755392f7a3SLiteSpeed Techint
765392f7a3SLiteSpeed Techmain (void)
775392f7a3SLiteSpeed Tech{
785392f7a3SLiteSpeed Tech    run_parse_tests();
795392f7a3SLiteSpeed Tech    run_gen_tests();
805392f7a3SLiteSpeed Tech    return 0;
815392f7a3SLiteSpeed Tech}
82