test_varint.c revision 06b2a236
106b2a236SDmitri Tikhonov/* Copyright (c) 2017 - 2021 LiteSpeed Technologies Inc.  See LICENSE. */
25392f7a3SLiteSpeed Tech#include <assert.h>
35392f7a3SLiteSpeed Tech#include <stddef.h>
45392f7a3SLiteSpeed Tech#include <stdint.h>
55392f7a3SLiteSpeed Tech
65392f7a3SLiteSpeed Tech#include "lsquic_varint.h"
75392f7a3SLiteSpeed Tech
85392f7a3SLiteSpeed Tech#define MAX_INPUT_SZ 8
95392f7a3SLiteSpeed Tech
105392f7a3SLiteSpeed Techstruct test_read_varint
115392f7a3SLiteSpeed Tech{
125392f7a3SLiteSpeed Tech    /* Input: */
135392f7a3SLiteSpeed Tech    unsigned char       input[MAX_INPUT_SZ];
145392f7a3SLiteSpeed Tech    size_t              in_sz;
155392f7a3SLiteSpeed Tech    /* Output: */
165392f7a3SLiteSpeed Tech    int                 rv;
175392f7a3SLiteSpeed Tech    uint64_t            val;
185392f7a3SLiteSpeed Tech};
195392f7a3SLiteSpeed Tech
205392f7a3SLiteSpeed Techstatic const struct test_read_varint read_tests[] =
215392f7a3SLiteSpeed Tech{
225392f7a3SLiteSpeed Tech    {
235392f7a3SLiteSpeed Tech        .input = "\x25",
245392f7a3SLiteSpeed Tech        .in_sz = 0,
255392f7a3SLiteSpeed Tech        .rv = -1,
265392f7a3SLiteSpeed Tech    },
275392f7a3SLiteSpeed Tech    {
285392f7a3SLiteSpeed Tech        .input = "\x25",
295392f7a3SLiteSpeed Tech        .in_sz = 2,
305392f7a3SLiteSpeed Tech        .rv = 1,
315392f7a3SLiteSpeed Tech        .val = 0x25,
325392f7a3SLiteSpeed Tech    },
335392f7a3SLiteSpeed Tech    {
345392f7a3SLiteSpeed Tech        .input = "\x40\x25",
355392f7a3SLiteSpeed Tech        .in_sz = 1,
365392f7a3SLiteSpeed Tech        .rv = -1,
375392f7a3SLiteSpeed Tech    },
385392f7a3SLiteSpeed Tech    {
395392f7a3SLiteSpeed Tech        .input = "\x40\x25",
405392f7a3SLiteSpeed Tech        .in_sz = 2,
415392f7a3SLiteSpeed Tech        .rv = 2,
425392f7a3SLiteSpeed Tech        .val = 0x25,
435392f7a3SLiteSpeed Tech    },
445392f7a3SLiteSpeed Tech    {
455392f7a3SLiteSpeed Tech        .input = "\x9d\x7f\x3e\x7d",
465392f7a3SLiteSpeed Tech        .in_sz = 2,
475392f7a3SLiteSpeed Tech        .rv = -1,
485392f7a3SLiteSpeed Tech    },
495392f7a3SLiteSpeed Tech    {
505392f7a3SLiteSpeed Tech        .input = "\x9d\x7f\x3e\x7d",
515392f7a3SLiteSpeed Tech        .in_sz = 4,
525392f7a3SLiteSpeed Tech        .rv = 4,
535392f7a3SLiteSpeed Tech        .val = 494878333,
545392f7a3SLiteSpeed Tech    },
555392f7a3SLiteSpeed Tech    {
565392f7a3SLiteSpeed Tech        .input = "\xc2\x19\x7c\x5e\xff\x14\xe8\x8c",
575392f7a3SLiteSpeed Tech        .in_sz = 7,
585392f7a3SLiteSpeed Tech        .rv = -1,
595392f7a3SLiteSpeed Tech    },
605392f7a3SLiteSpeed Tech    {
615392f7a3SLiteSpeed Tech        .input = "\xc2\x19\x7c\x5e\xff\x14\xe8\x8c",
625392f7a3SLiteSpeed Tech        .in_sz = 8,
635392f7a3SLiteSpeed Tech        .rv = 8,
645392f7a3SLiteSpeed Tech        .val = 151288809941952652ull,
655392f7a3SLiteSpeed Tech    },
665392f7a3SLiteSpeed Tech};
675392f7a3SLiteSpeed Tech
685392f7a3SLiteSpeed Tech
695392f7a3SLiteSpeed Techint
705392f7a3SLiteSpeed Techmain (void)
715392f7a3SLiteSpeed Tech{
725392f7a3SLiteSpeed Tech    const struct test_read_varint *test;
735392f7a3SLiteSpeed Tech    const struct test_read_varint *const end
745392f7a3SLiteSpeed Tech        = read_tests + sizeof(read_tests) / sizeof(read_tests[0]);
755392f7a3SLiteSpeed Tech    size_t input_sz, avail;
765392f7a3SLiteSpeed Tech    struct varint_read_state state;
775392f7a3SLiteSpeed Tech    const unsigned char *p, *endp;
785392f7a3SLiteSpeed Tech    int s;
795392f7a3SLiteSpeed Tech
805392f7a3SLiteSpeed Tech    for (test = read_tests; test < end; ++test)
815392f7a3SLiteSpeed Tech    {
825392f7a3SLiteSpeed Tech        uint64_t val;
835392f7a3SLiteSpeed Tech        const int rv = vint_read(test->input, test->input + test->in_sz, &val);
845392f7a3SLiteSpeed Tech        assert(rv == test->rv);
855392f7a3SLiteSpeed Tech        if (test->rv > 0)
865392f7a3SLiteSpeed Tech            assert(val == test->val);
875392f7a3SLiteSpeed Tech
885392f7a3SLiteSpeed Tech        /* Test non-blocking read */
895392f7a3SLiteSpeed Tech        for (input_sz = MAX_INPUT_SZ; input_sz > 0; --input_sz)
905392f7a3SLiteSpeed Tech        {
915392f7a3SLiteSpeed Tech            state.pos = 0;
925392f7a3SLiteSpeed Tech            p = test->input;
935392f7a3SLiteSpeed Tech            endp = p + test->in_sz;
945392f7a3SLiteSpeed Tech            while (p < endp)
955392f7a3SLiteSpeed Tech            {
965392f7a3SLiteSpeed Tech                avail = (uintptr_t) (endp - p);
975392f7a3SLiteSpeed Tech                if (avail > input_sz)
985392f7a3SLiteSpeed Tech                    avail = input_sz;
995392f7a3SLiteSpeed Tech                s = lsquic_varint_read_nb(&p, p + avail, &state);
1005392f7a3SLiteSpeed Tech                if (0 == s)
1015392f7a3SLiteSpeed Tech                {
1025392f7a3SLiteSpeed Tech                    assert(state.val == test->val);
1035392f7a3SLiteSpeed Tech                    assert(p <= endp);
1045392f7a3SLiteSpeed Tech                    break;
1055392f7a3SLiteSpeed Tech                }
1065392f7a3SLiteSpeed Tech                else if (p == endp)
1075392f7a3SLiteSpeed Tech                {
1085392f7a3SLiteSpeed Tech                    assert(test->rv == -1);
1095392f7a3SLiteSpeed Tech                    break;
1105392f7a3SLiteSpeed Tech                }
1115392f7a3SLiteSpeed Tech                else
1125392f7a3SLiteSpeed Tech                {
1135392f7a3SLiteSpeed Tech                    assert(p < endp);
1145392f7a3SLiteSpeed Tech                }
1155392f7a3SLiteSpeed Tech            }
1165392f7a3SLiteSpeed Tech        }
1175392f7a3SLiteSpeed Tech    }
1185392f7a3SLiteSpeed Tech
1195392f7a3SLiteSpeed Tech    return 0;
1205392f7a3SLiteSpeed Tech}
121