test_varint.c revision a74702c6
1/* Copyright (c) 2017 - 2022 LiteSpeed Technologies Inc.  See LICENSE. */
2#include <assert.h>
3#include <stddef.h>
4#include <stdint.h>
5
6#include "lsquic_varint.h"
7
8#ifdef _MSC_VER
9#include "vc_compat.h"
10#endif
11
12#define MAX_INPUT_SZ 8
13
14struct test_read_varint
15{
16    /* Input: */
17    unsigned char       input[MAX_INPUT_SZ];
18    size_t              in_sz;
19    /* Output: */
20    int                 rv;
21    uint64_t            val;
22};
23
24static const struct test_read_varint read_tests[] =
25{
26    {
27        .input = "\x25",
28        .in_sz = 0,
29        .rv = -1,
30    },
31    {
32        .input = "\x25",
33        .in_sz = 2,
34        .rv = 1,
35        .val = 0x25,
36    },
37    {
38        .input = "\x40\x25",
39        .in_sz = 1,
40        .rv = -1,
41    },
42    {
43        .input = "\x40\x25",
44        .in_sz = 2,
45        .rv = 2,
46        .val = 0x25,
47    },
48    {
49        .input = "\x9d\x7f\x3e\x7d",
50        .in_sz = 2,
51        .rv = -1,
52    },
53    {
54        .input = "\x9d\x7f\x3e\x7d",
55        .in_sz = 4,
56        .rv = 4,
57        .val = 494878333,
58    },
59    {
60        .input = "\xc2\x19\x7c\x5e\xff\x14\xe8\x8c",
61        .in_sz = 7,
62        .rv = -1,
63    },
64    {
65        .input = "\xc2\x19\x7c\x5e\xff\x14\xe8\x8c",
66        .in_sz = 8,
67        .rv = 8,
68        .val = 151288809941952652ull,
69    },
70};
71
72
73int
74main (void)
75{
76    const struct test_read_varint *test;
77    const struct test_read_varint *const end
78        = read_tests + sizeof(read_tests) / sizeof(read_tests[0]);
79    size_t input_sz, avail;
80    struct varint_read_state state;
81    const unsigned char *p, *endp;
82    int s;
83
84    for (test = read_tests; test < end; ++test)
85    {
86        uint64_t val;
87        const int rv = vint_read(test->input, test->input + test->in_sz, &val);
88        assert(rv == test->rv);
89        if (test->rv > 0)
90            assert(val == test->val);
91
92        /* Test non-blocking read */
93        for (input_sz = MAX_INPUT_SZ; input_sz > 0; --input_sz)
94        {
95            state.pos = 0;
96            p = test->input;
97            endp = p + test->in_sz;
98            while (p < endp)
99            {
100                avail = (uintptr_t) (endp - p);
101                if (avail > input_sz)
102                    avail = input_sz;
103                s = lsquic_varint_read_nb(&p, p + avail, &state);
104                if (0 == s)
105                {
106                    assert(state.val == test->val);
107                    assert(p <= endp);
108                    break;
109                }
110                else if (p == endp)
111                {
112                    assert(test->rv == -1);
113                    break;
114                }
115                else
116                {
117                    assert(p < endp);
118                }
119            }
120        }
121    }
122
123    return 0;
124}
125