1e51f1c27SDmitri Tikhonov#include <assert.h>
20310513dSDmitri Tikhonov#include <stdint.h>
3e51f1c27SDmitri Tikhonov#include <stdlib.h>
40310513dSDmitri Tikhonov#include <string.h>
5e51f1c27SDmitri Tikhonov
6e51f1c27SDmitri Tikhonov#include "lsqpack.h"
7e51f1c27SDmitri Tikhonov#include "lsqpack-test.h"
8e51f1c27SDmitri Tikhonov
9e51f1c27SDmitri Tikhonovstruct int_test
10e51f1c27SDmitri Tikhonov{
11e51f1c27SDmitri Tikhonov    int             it_lineno;
12e51f1c27SDmitri Tikhonov    unsigned        it_prefix_bits;
13e51f1c27SDmitri Tikhonov    unsigned char   it_encoded[20];
14e51f1c27SDmitri Tikhonov    size_t          it_enc_sz;
15e51f1c27SDmitri Tikhonov    uint64_t        it_decoded;
16e51f1c27SDmitri Tikhonov    int             it_dec_retval;
17e51f1c27SDmitri Tikhonov};
18e51f1c27SDmitri Tikhonov
19e51f1c27SDmitri Tikhonovstatic const struct int_test tests[] =
20e51f1c27SDmitri Tikhonov{
21e51f1c27SDmitri Tikhonov
22e51f1c27SDmitri Tikhonov    {   .it_lineno      = __LINE__,
23e51f1c27SDmitri Tikhonov        .it_prefix_bits = 7,
24e51f1c27SDmitri Tikhonov        .it_encoded     = { 0x7F, 0x02, },
25e51f1c27SDmitri Tikhonov        .it_enc_sz      = 2,
26e51f1c27SDmitri Tikhonov        .it_decoded     = 0x81,
27e51f1c27SDmitri Tikhonov        .it_dec_retval  = 0,
28e51f1c27SDmitri Tikhonov    },
29e51f1c27SDmitri Tikhonov
30a1a3b017SDmitri Tikhonov    /* RFC 7541, Appendinx C.1.1 */
31a1a3b017SDmitri Tikhonov    {   .it_lineno      = __LINE__,
32a1a3b017SDmitri Tikhonov        .it_prefix_bits = 5,
33a1a3b017SDmitri Tikhonov        .it_encoded     = { 0b1010, 0x02, },
34a1a3b017SDmitri Tikhonov        .it_enc_sz      = 1,
35a1a3b017SDmitri Tikhonov        .it_decoded     = 10,
36a1a3b017SDmitri Tikhonov        .it_dec_retval  = 0,
37a1a3b017SDmitri Tikhonov    },
38a1a3b017SDmitri Tikhonov
39a1a3b017SDmitri Tikhonov    /* RFC 7541, Appendinx C.1.2 */
40a1a3b017SDmitri Tikhonov    {   .it_lineno      = __LINE__,
41a1a3b017SDmitri Tikhonov        .it_prefix_bits = 5,
42a1a3b017SDmitri Tikhonov        .it_encoded     = { 0b11111, 0b10011010, 0b00001010, },
43a1a3b017SDmitri Tikhonov        .it_enc_sz      = 3,
44a1a3b017SDmitri Tikhonov        .it_decoded     = 1337,
45a1a3b017SDmitri Tikhonov        .it_dec_retval  = 0,
46a1a3b017SDmitri Tikhonov    },
47a1a3b017SDmitri Tikhonov
48a1a3b017SDmitri Tikhonov    /* RFC 7541, Appendinx C.1.3 */
49a1a3b017SDmitri Tikhonov    {   .it_lineno      = __LINE__,
50a1a3b017SDmitri Tikhonov        .it_prefix_bits = 8,
51a1a3b017SDmitri Tikhonov        .it_encoded     = { 0b101010, },
52a1a3b017SDmitri Tikhonov        .it_enc_sz      = 1,
53a1a3b017SDmitri Tikhonov        .it_decoded     = 42,
54a1a3b017SDmitri Tikhonov        .it_dec_retval  = 0,
55a1a3b017SDmitri Tikhonov    },
56a1a3b017SDmitri Tikhonov
57a1a3b017SDmitri Tikhonov    {   .it_lineno      = __LINE__,
58a1a3b017SDmitri Tikhonov        .it_prefix_bits = 7,
59a1a3b017SDmitri Tikhonov        .it_encoded     = { 0b01111111, 0b10000001, 0b10000010, 0b00000011, },
60a1a3b017SDmitri Tikhonov        .it_enc_sz      = 4,
61a1a3b017SDmitri Tikhonov                       /*     01234560123456 */
62a1a3b017SDmitri Tikhonov        .it_decoded     = 0b1100000100000001    + 0b1111111,
63a1a3b017SDmitri Tikhonov        .it_dec_retval  = 0,
64a1a3b017SDmitri Tikhonov    },
65a1a3b017SDmitri Tikhonov
66a1a3b017SDmitri Tikhonov    {   .it_lineno      = __LINE__,
67a1a3b017SDmitri Tikhonov        .it_prefix_bits = 7,
68a1a3b017SDmitri Tikhonov        .it_encoded     = { 0b01111111, 0b10000001, 0b10000010, 0b10000011,
69a1a3b017SDmitri Tikhonov                            0b00000011, },
70a1a3b017SDmitri Tikhonov        .it_enc_sz      = 5,
71a1a3b017SDmitri Tikhonov                       /*     012345601234560123456 */
72a1a3b017SDmitri Tikhonov        .it_decoded     = 0b11000001100000100000001    + 0b1111111,
73a1a3b017SDmitri Tikhonov        .it_dec_retval  = 0,
74a1a3b017SDmitri Tikhonov    },
75a1a3b017SDmitri Tikhonov
760acadf61SDmitri Tikhonov    {   .it_lineno      = __LINE__,
770acadf61SDmitri Tikhonov        .it_prefix_bits = 7,
780acadf61SDmitri Tikhonov        .it_encoded     = { 0b01111111, 0b10000001, 0b10000010, 0b10000011,
790acadf61SDmitri Tikhonov                            0b10000100, 0b10000101, 0b10000110, 0b10000111,
800acadf61SDmitri Tikhonov                            0b10001000,
810acadf61SDmitri Tikhonov                            0b00000011, },
820acadf61SDmitri Tikhonov        .it_enc_sz      = 10,
830acadf61SDmitri Tikhonov                       /*     01234560123456012345601234560123456012345601234560123456 */
840acadf61SDmitri Tikhonov        .it_decoded     = 0b1100010000000111000011000001010000100000001100000100000001
850acadf61SDmitri Tikhonov                                + 0b1111111,
860acadf61SDmitri Tikhonov        .it_dec_retval  = 0,
870acadf61SDmitri Tikhonov    },
880acadf61SDmitri Tikhonov
890acadf61SDmitri Tikhonov    {   .it_lineno      = __LINE__,
900acadf61SDmitri Tikhonov        .it_prefix_bits = 7,
910acadf61SDmitri Tikhonov        .it_encoded     = { 0b01111111, 0b10000001, 0b10000010, 0b10000011,
920acadf61SDmitri Tikhonov                            0b10000100, 0b10000101, 0b10000110, 0b10000111,
930acadf61SDmitri Tikhonov                            0b10001000, 0b10001001,
940acadf61SDmitri Tikhonov                            0b00000001, },
950acadf61SDmitri Tikhonov        .it_enc_sz      = 11,
960acadf61SDmitri Tikhonov                       /*    012345601234560123456012345601234560123456012345601234560123456 */
970acadf61SDmitri Tikhonov        .it_decoded     = 0b1000100100010000000111000011000001010000100000001100000100000001
980acadf61SDmitri Tikhonov                                + 0b1111111,
990acadf61SDmitri Tikhonov        .it_dec_retval  = 0,
1000acadf61SDmitri Tikhonov    },
1010acadf61SDmitri Tikhonov
1020acadf61SDmitri Tikhonov    {   .it_lineno      = __LINE__,
1030acadf61SDmitri Tikhonov        .it_prefix_bits = 7,
1040310513dSDmitri Tikhonov        .it_encoded     = { 0b01111111, 0b10000000, 0b11111111, 0b11111111,
1050310513dSDmitri Tikhonov                            0b11111111, 0b11111111, 0b11111111, 0b11111111,
1060310513dSDmitri Tikhonov                            0b11111111, 0b11111111,
1070310513dSDmitri Tikhonov                            0b00000001, },
1080310513dSDmitri Tikhonov        .it_enc_sz      = 11,
1090310513dSDmitri Tikhonov        .it_decoded     = UINT64_MAX,
1100310513dSDmitri Tikhonov        .it_dec_retval  = 0,
1110310513dSDmitri Tikhonov    },
1120310513dSDmitri Tikhonov
1130310513dSDmitri Tikhonov    {   .it_lineno      = __LINE__,
1140310513dSDmitri Tikhonov        .it_prefix_bits = 7,
1150310513dSDmitri Tikhonov            /* Same as above, but with extra bit that overflows it */
1160310513dSDmitri Tikhonov                                      /* ----v---- */
1170310513dSDmitri Tikhonov        .it_encoded     = { 0b01111111, 0b10010000, 0b11111111, 0b11111111,
1180310513dSDmitri Tikhonov                            0b11111111, 0b11111111, 0b11111111, 0b11111111,
1190310513dSDmitri Tikhonov                            0b11111111, 0b11111111,
1200310513dSDmitri Tikhonov                            0b00000001, },
1210310513dSDmitri Tikhonov        .it_enc_sz      = 11,
1220310513dSDmitri Tikhonov        .it_dec_retval  = -2,
1230310513dSDmitri Tikhonov    },
1240310513dSDmitri Tikhonov
1250310513dSDmitri Tikhonov    {   .it_lineno      = __LINE__,
1260310513dSDmitri Tikhonov        .it_prefix_bits = 8,
1270310513dSDmitri Tikhonov        .it_encoded     = { 0b11111111, 0b10000001, 0b10000010, 0b10000011,
1280310513dSDmitri Tikhonov                            0b10000100, 0b10000101, 0b10000110, 0b10000111,
1290310513dSDmitri Tikhonov                            0b10001000, 0b10001001, 0b00000001, },
1300310513dSDmitri Tikhonov        .it_enc_sz      = 11,
1310310513dSDmitri Tikhonov                       /*    012345601234560123456012345601234560123456012345601234560123456 */
1320310513dSDmitri Tikhonov        .it_decoded     = 0b1000100100010000000111000011000001010000100000001100000100000001
1330310513dSDmitri Tikhonov                                + 0b11111111,
1340310513dSDmitri Tikhonov        .it_dec_retval  = 0,
1350310513dSDmitri Tikhonov    },
1360310513dSDmitri Tikhonov
1370310513dSDmitri Tikhonov    {   .it_lineno      = __LINE__,
1380310513dSDmitri Tikhonov        .it_prefix_bits = 7,
1390acadf61SDmitri Tikhonov        .it_encoded     = { 0b01111111, 0b11101111, 0b11111111, 0b11111111,
1400acadf61SDmitri Tikhonov                            0b11111111, 0b11111111, 0b11111111, 0b11111111,
1410acadf61SDmitri Tikhonov                            0b11111111, 0b11111111,
1420acadf61SDmitri Tikhonov                            0b00000001, },
1430acadf61SDmitri Tikhonov        .it_enc_sz      = 11,
1440acadf61SDmitri Tikhonov        .it_dec_retval  = -2,
1450acadf61SDmitri Tikhonov    },
1460acadf61SDmitri Tikhonov
1470acadf61SDmitri Tikhonov    {   .it_lineno      = __LINE__,
1480acadf61SDmitri Tikhonov        .it_prefix_bits = 7,
1490acadf61SDmitri Tikhonov        .it_encoded     = { 0b01111111, 0b10000001, 0b10000010, 0b10000011,
1500acadf61SDmitri Tikhonov                            0b10000100, 0b10000101, 0b10000110, 0b10000111,
1510acadf61SDmitri Tikhonov                            0b10001000, 0b10001001,
1520acadf61SDmitri Tikhonov                            0b00000011, },
1530acadf61SDmitri Tikhonov        .it_enc_sz      = 11,
1540acadf61SDmitri Tikhonov        .it_dec_retval  = -2,
1550acadf61SDmitri Tikhonov    },
1560acadf61SDmitri Tikhonov
1571368392eSDmitri Tikhonov    {   .it_lineno      = __LINE__,
1581368392eSDmitri Tikhonov        .it_prefix_bits = 7,
1591368392eSDmitri Tikhonov        .it_encoded     = { 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
1601368392eSDmitri Tikhonov                            0xFF, 0xFF, 0xFF, },
1611368392eSDmitri Tikhonov        .it_enc_sz      = 11,
1621368392eSDmitri Tikhonov        .it_dec_retval  = -2,
1631368392eSDmitri Tikhonov    },
1641368392eSDmitri Tikhonov
165e51f1c27SDmitri Tikhonov};
166e51f1c27SDmitri Tikhonov
167e51f1c27SDmitri Tikhonovint
168e51f1c27SDmitri Tikhonovmain (void)
169e51f1c27SDmitri Tikhonov{
170e51f1c27SDmitri Tikhonov    const struct int_test *test;
171e51f1c27SDmitri Tikhonov    const unsigned char *src;
1720310513dSDmitri Tikhonov    unsigned char *dst;
1730310513dSDmitri Tikhonov    unsigned char buf[ sizeof(((struct int_test *) NULL)->it_encoded) ];
174e51f1c27SDmitri Tikhonov    uint64_t val;
175a1a3b017SDmitri Tikhonov    size_t sz;
176e51f1c27SDmitri Tikhonov    int rv;
177e51f1c27SDmitri Tikhonov
1780310513dSDmitri Tikhonov    /* Test the decoder */
179e51f1c27SDmitri Tikhonov    for (test = tests; test < tests + sizeof(tests) / sizeof(tests[0]); ++test)
180e51f1c27SDmitri Tikhonov    {
181d8424881SDmitri Tikhonov        struct lsqpack_dec_int_state state;
182d8424881SDmitri Tikhonov        state.resume = 0;
183d8424881SDmitri Tikhonov        for (sz = 0; sz < test->it_enc_sz - 1; ++sz)
184d8424881SDmitri Tikhonov        {
185d8424881SDmitri Tikhonov            src = test->it_encoded + sz;
1866c252d20SDmitri Tikhonov            rv = lsqpack_dec_int(&src, src + 1, test->it_prefix_bits,
187d8424881SDmitri Tikhonov                                                                &val, &state);
188d8424881SDmitri Tikhonov            assert(-1 == rv);
189d8424881SDmitri Tikhonov        }
190d8424881SDmitri Tikhonov        src = test->it_encoded + sz;
1916c252d20SDmitri Tikhonov        rv = lsqpack_dec_int(&src, src + 1, test->it_prefix_bits,
192d8424881SDmitri Tikhonov                                                            &val, &state);
193d8424881SDmitri Tikhonov        assert(rv == test->it_dec_retval);
194d8424881SDmitri Tikhonov        if (0 == rv)
195d8424881SDmitri Tikhonov            assert(val == test->it_decoded);
196d8424881SDmitri Tikhonov    }
197d8424881SDmitri Tikhonov
1980310513dSDmitri Tikhonov    /* Test the encoder */
1990310513dSDmitri Tikhonov    for (test = tests; test < tests + sizeof(tests) / sizeof(tests[0]); ++test)
2000310513dSDmitri Tikhonov    {
2010310513dSDmitri Tikhonov        if (test->it_dec_retval != 0)
2020310513dSDmitri Tikhonov            continue;
2030310513dSDmitri Tikhonov        for (sz = 1; sz < test->it_enc_sz; ++sz)
2040310513dSDmitri Tikhonov        {
2050310513dSDmitri Tikhonov            dst = lsqpack_enc_int(buf, buf + sz, test->it_decoded,
2060310513dSDmitri Tikhonov                                                        test->it_prefix_bits);
2070310513dSDmitri Tikhonov            assert(dst == buf);     /* Not enough room */
2080310513dSDmitri Tikhonov        }
2090310513dSDmitri Tikhonov        for (; sz <= sizeof(buf); ++sz)
2100310513dSDmitri Tikhonov        {
2110310513dSDmitri Tikhonov            buf[0] = '\0';
2120310513dSDmitri Tikhonov            dst = lsqpack_enc_int(buf, buf + sz, test->it_decoded,
2130310513dSDmitri Tikhonov                                                        test->it_prefix_bits);
2140310513dSDmitri Tikhonov            assert(dst - buf == (intptr_t) test->it_enc_sz);
2150310513dSDmitri Tikhonov            assert(0 == memcmp(buf, test->it_encoded, test->it_enc_sz));
2160310513dSDmitri Tikhonov        }
2170310513dSDmitri Tikhonov    }
2180310513dSDmitri Tikhonov
219e51f1c27SDmitri Tikhonov    return 0;
220e51f1c27SDmitri Tikhonov}
221