test_stop_waiting_gquic_be.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_043);
15
16
17struct parse_test {
18    /* Input: */
19    unsigned char   buf[0x10];
20    size_t          buf_len;
21    lsquic_packno_t cur_packno;
22    enum packno_bits
23                    bits;
24    /* Expected values: */
25    int             retval;
26    lsquic_packno_t least_unacked;
27};
28
29static const struct parse_test parse_tests[] = {
30    {
31        .buf            = { 0x06, 0x34, 0x12, 0x45, 0x67, 0x89, 0xAB, },
32        .buf_len        = 3,
33        .least_unacked  = 0x1111,
34        .cur_packno     = 0x4523,
35        .bits           = GQUIC_PACKNO_LEN_2,
36        .retval         = 3,
37    },
38
39    {
40        .buf            = { 0x06, 0x12, 0x34, 0x45, 0x67, 0x89, 0xAB, },
41        .buf_len        = 2,
42        .least_unacked  = 0x1111,
43        .cur_packno     = 0x4523,
44        .bits           = GQUIC_PACKNO_LEN_2,
45        .retval         = -1,
46    },
47
48    {
49        .buf            = { 0x06, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, },
50        .buf_len        = 7,
51        .least_unacked  = 0x1122324252627282,
52        .cur_packno     = 0x1122334455667788,
53        .bits           = GQUIC_PACKNO_LEN_6,
54        .retval         = 7,
55    },
56
57    {   .buf            = { 0 },    }
58};
59
60
61static void
62run_parse_tests (void)
63{
64    const struct parse_test *test;
65    for (test = parse_tests; test->buf[0]; ++test)
66    {
67        lsquic_packno_t least;
68        memset(&least, 0x33, sizeof(least));
69        int sz = pf->pf_parse_stop_waiting_frame(test->buf, test->buf_len,
70                                          test->cur_packno, test->bits, &least);
71        assert(("return value is correct", sz == test->retval));
72        if (test->retval > 0)
73            assert(("least ACKed value is correct", least == test->least_unacked));
74    }
75}
76
77
78static void
79run_gen_tests (void)
80{
81    const struct parse_test *test;
82    for (test = parse_tests; test->buf[0]; ++test)
83    {
84        unsigned char buf[0x100];
85        int sz = pf->pf_gen_stop_waiting_frame(buf, test->buf_len,
86                      test->cur_packno, test->bits, test->least_unacked);
87        assert(("return value is correct", sz == test->retval));
88        if (test->retval > 0)
89            assert(("generated frame is correct", 0 == memcmp(test->buf, buf, sz)));
90    }
91}
92
93
94int
95main (void)
96{
97    run_parse_tests();
98    run_gen_tests();
99    return 0;
100}
101