test_streamgen.c revision fb73393f
17d09751dSDmitri Tikhonov/* Copyright (c) 2017 - 2020 LiteSpeed Technologies Inc.  See LICENSE. */
250aadb33SDmitri Tikhonov#include <assert.h>
350aadb33SDmitri Tikhonov#include <stdio.h>
450aadb33SDmitri Tikhonov#include <stdlib.h>
550aadb33SDmitri Tikhonov#include <string.h>
650aadb33SDmitri Tikhonov#include <sys/queue.h>
7461e84d8SAmol Deshpande#ifndef WIN32
850aadb33SDmitri Tikhonov#include <sys/time.h>
9461e84d8SAmol Deshpande#endif
1050aadb33SDmitri Tikhonov
11461e84d8SAmol Deshpande#include "lsquic.h"
1250aadb33SDmitri Tikhonov#include "lsquic_types.h"
1350aadb33SDmitri Tikhonov#include "lsquic_parse.h"
1450aadb33SDmitri Tikhonov#include "lsquic_sfcw.h"
155392f7a3SLiteSpeed Tech#include "lsquic_varint.h"
165392f7a3SLiteSpeed Tech#include "lsquic_hq.h"
175392f7a3SLiteSpeed Tech#include "lsquic_hash.h"
1850aadb33SDmitri Tikhonov#include "lsquic_stream.h"
1950aadb33SDmitri Tikhonov
2050aadb33SDmitri Tikhonovstruct test {
2150aadb33SDmitri Tikhonov    int             lineno;
2250aadb33SDmitri Tikhonov    const struct parse_funcs *
2350aadb33SDmitri Tikhonov                    pf;
2450aadb33SDmitri Tikhonov    /* Inputs.  These are returned by lsquic_stream_tosend_fin(),
2550aadb33SDmitri Tikhonov     * lsquic_stream_tosend_read(), and lsquic_stream_tosend_offset().
2650aadb33SDmitri Tikhonov     */
2750aadb33SDmitri Tikhonov    int             fin[2];         /* There may be two calls to lsquic_stream_tosend_fin() */
2850aadb33SDmitri Tikhonov    uint64_t        offset;
2950aadb33SDmitri Tikhonov    uint32_t        stream_id;
3050aadb33SDmitri Tikhonov    size_t          avail;          /* Space to write stream frame to */
3150aadb33SDmitri Tikhonov    size_t          min_sz;         /* Minimum size needed to generate stream frame.  Any
3250aadb33SDmitri Tikhonov                                     * sizes smaller than this should fail.
3350aadb33SDmitri Tikhonov                                     */
3450aadb33SDmitri Tikhonov    size_t          data_sz;
3550aadb33SDmitri Tikhonov    char            data[0x100];
3650aadb33SDmitri Tikhonov
3750aadb33SDmitri Tikhonov    /* Output.  This is how we expect the resulting frame to look.
3850aadb33SDmitri Tikhonov     */
3950aadb33SDmitri Tikhonov    int             len;            /* Return value of gen_stream_frame() */
4050aadb33SDmitri Tikhonov    char            out[0x100];
4150aadb33SDmitri Tikhonov};
4250aadb33SDmitri Tikhonov
4350aadb33SDmitri Tikhonov
4450aadb33SDmitri Tikhonovstatic const struct test tests[] = {
4550aadb33SDmitri Tikhonov    /*
465392f7a3SLiteSpeed Tech     * Big-endian:
4750aadb33SDmitri Tikhonov     */
4850aadb33SDmitri Tikhonov    {   .lineno     = __LINE__,
49fb96f4ddSDmitri Tikhonov        .pf         = select_pf_by_ver(LSQVER_043),
5050aadb33SDmitri Tikhonov        .fin        = { 0, 1, },
5150aadb33SDmitri Tikhonov        .offset     = 0x0807060504030201UL,
5250aadb33SDmitri Tikhonov        .stream_id  = 0x210,
5350aadb33SDmitri Tikhonov        .data_sz    = 10,
5450aadb33SDmitri Tikhonov        .data       = "0123456789",
5550aadb33SDmitri Tikhonov        .avail      = 0x100,
5650aadb33SDmitri Tikhonov        .out        =
5750aadb33SDmitri Tikhonov      /*  1      f      d      ooo    ss            1fdoooss */
5850aadb33SDmitri Tikhonov      /*  TYPE   FIN    DLEN   OLEN   SLEN  */
5950aadb33SDmitri Tikhonov        { 0x80 | 0x40 | 0x20 | 0x1C | 0x01,
605392f7a3SLiteSpeed Tech          0x02, 0x10,                                       /* Stream ID */
615392f7a3SLiteSpeed Tech          0x08, 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01,   /* Offset */
625392f7a3SLiteSpeed Tech          0x00, 0x0A,                                       /* Data length */
6350aadb33SDmitri Tikhonov          '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
6450aadb33SDmitri Tikhonov        },
6550aadb33SDmitri Tikhonov        .len        = 1 + 2 + 8 + 2 + 10,
6650aadb33SDmitri Tikhonov        .min_sz     = 1 + 2 + 8 + 0 + 1,
6750aadb33SDmitri Tikhonov    },
6850aadb33SDmitri Tikhonov
6950aadb33SDmitri Tikhonov    {   .lineno     = __LINE__,
70fb96f4ddSDmitri Tikhonov        .pf         = select_pf_by_ver(LSQVER_043),
7150aadb33SDmitri Tikhonov        .fin        = { 0, 0, },
7250aadb33SDmitri Tikhonov        .offset     = 0x0807060504030201UL,
7350aadb33SDmitri Tikhonov        .stream_id  = 0x210,
7450aadb33SDmitri Tikhonov        .data_sz    = 10,
7550aadb33SDmitri Tikhonov        .data       = "0123456789",
7650aadb33SDmitri Tikhonov        .avail      = 0x100,
7750aadb33SDmitri Tikhonov        .out        =
7850aadb33SDmitri Tikhonov      /*  1      f      d      ooo    ss            1fdoooss */
7950aadb33SDmitri Tikhonov      /*  TYPE   FIN    DLEN   OLEN   SLEN  */
8050aadb33SDmitri Tikhonov        { 0x80 | 0x00 | 0x20 | 0x1C | 0x01,
815392f7a3SLiteSpeed Tech          0x02, 0x10,                                       /* Stream ID */
825392f7a3SLiteSpeed Tech          0x08, 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01,   /* Offset */
835392f7a3SLiteSpeed Tech          0x00, 0x0A,                                       /* Data length */
8450aadb33SDmitri Tikhonov          '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
8550aadb33SDmitri Tikhonov        },
8650aadb33SDmitri Tikhonov        .len        = 1 + 2 + 8 + 2 + 10,
8750aadb33SDmitri Tikhonov        .min_sz     = 1 + 2 + 8 + 0 + 1,
8850aadb33SDmitri Tikhonov    },
8950aadb33SDmitri Tikhonov
9050aadb33SDmitri Tikhonov    {   .lineno     = __LINE__,
91fb96f4ddSDmitri Tikhonov        .pf         = select_pf_by_ver(LSQVER_043),
9250aadb33SDmitri Tikhonov        .fin        = { 1, 0, },
9350aadb33SDmitri Tikhonov        .offset     = 0x0807060504030201UL,
9450aadb33SDmitri Tikhonov        .stream_id  = 0x210,
9550aadb33SDmitri Tikhonov        .data_sz    = 10,
9650aadb33SDmitri Tikhonov        .data       = "0123456789",
9750aadb33SDmitri Tikhonov        .avail      = 0x100,
9850aadb33SDmitri Tikhonov        .out        =
9950aadb33SDmitri Tikhonov      /*  1      f      d      ooo    ss            1fdoooss */
10050aadb33SDmitri Tikhonov      /*  TYPE   FIN    DLEN   OLEN   SLEN  */
10150aadb33SDmitri Tikhonov        { 0x80 | 0x40 | 0x20 | 0x1C | 0x01,
1025392f7a3SLiteSpeed Tech          0x02, 0x10,                                       /* Stream ID */
1035392f7a3SLiteSpeed Tech          0x08, 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01,   /* Offset */
10450aadb33SDmitri Tikhonov          0x00, 0x00,                                       /* Data length */
10550aadb33SDmitri Tikhonov          '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
10650aadb33SDmitri Tikhonov        },
10750aadb33SDmitri Tikhonov        .len        = 1 + 2 + 8 + 2,
10850aadb33SDmitri Tikhonov        .min_sz     = 1 + 2 + 8 + 2,
10950aadb33SDmitri Tikhonov    },
11050aadb33SDmitri Tikhonov
11150aadb33SDmitri Tikhonov    {   .lineno     = __LINE__,
112fb96f4ddSDmitri Tikhonov        .pf         = select_pf_by_ver(LSQVER_043),
11350aadb33SDmitri Tikhonov        .fin        = { 1, 0, },
11450aadb33SDmitri Tikhonov        .offset     = 0x0807060504030201UL,
11550aadb33SDmitri Tikhonov        .stream_id  = 0x21,
11650aadb33SDmitri Tikhonov        .data_sz    = 10,
11750aadb33SDmitri Tikhonov        .data       = "0123456789",
11850aadb33SDmitri Tikhonov        .avail      = 0x100,
11950aadb33SDmitri Tikhonov        .out        =
12050aadb33SDmitri Tikhonov      /*  1      f      d      ooo    ss            1fdoooss */
12150aadb33SDmitri Tikhonov      /*  TYPE   FIN    DLEN   OLEN   SLEN  */
12250aadb33SDmitri Tikhonov        { 0x80 | 0x40 | 0x20 | 0x1C | 0x00,
12350aadb33SDmitri Tikhonov          0x21,                                             /* Stream ID */
1245392f7a3SLiteSpeed Tech          0x08, 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01,   /* Offset */
12550aadb33SDmitri Tikhonov          0x00, 0x00,                                       /* Data length */
12650aadb33SDmitri Tikhonov          '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
12750aadb33SDmitri Tikhonov        },
12850aadb33SDmitri Tikhonov        .len        = 1 + 1 + 8 + 2,
12950aadb33SDmitri Tikhonov        .min_sz     = 1 + 1 + 8 + 2,
13050aadb33SDmitri Tikhonov    },
13150aadb33SDmitri Tikhonov
13250aadb33SDmitri Tikhonov    {   .lineno     = __LINE__,
133fb96f4ddSDmitri Tikhonov        .pf         = select_pf_by_ver(LSQVER_043),
13450aadb33SDmitri Tikhonov        .fin        = { 0, 0, },
13550aadb33SDmitri Tikhonov        .offset     = 0x77,
13650aadb33SDmitri Tikhonov        .stream_id  = 0x210,
13750aadb33SDmitri Tikhonov        .data_sz    = 10,
13850aadb33SDmitri Tikhonov        .data       = "0123456789",
13950aadb33SDmitri Tikhonov        .avail      = 0x100,
14050aadb33SDmitri Tikhonov        .out        =
14150aadb33SDmitri Tikhonov      /*  1      f      d      ooo    ss            1fdoooss */
14250aadb33SDmitri Tikhonov      /*  TYPE   FIN    DLEN   OLEN   SLEN  */
14350aadb33SDmitri Tikhonov        { 0x80 | 0x00 | 0x20 | 0x04 | 0x01,
1445392f7a3SLiteSpeed Tech          0x02, 0x10,                                       /* Stream ID */
1455392f7a3SLiteSpeed Tech          0x00, 0x77,                                       /* Offset */
1465392f7a3SLiteSpeed Tech          0x00, 0x0A,                                       /* Data length */
14750aadb33SDmitri Tikhonov          '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
14850aadb33SDmitri Tikhonov        },
14950aadb33SDmitri Tikhonov        .len        = 1 + 2 + 2 + 2 + 10,
15050aadb33SDmitri Tikhonov        .min_sz     = 1 + 2 + 2 + 0 + 1,
15150aadb33SDmitri Tikhonov    },
15250aadb33SDmitri Tikhonov
15350aadb33SDmitri Tikhonov    {   .lineno     = __LINE__,
154fb96f4ddSDmitri Tikhonov        .pf         = select_pf_by_ver(LSQVER_043),
15550aadb33SDmitri Tikhonov        .fin        = { 0, 0, },
15650aadb33SDmitri Tikhonov        .offset     = 0x0,
15750aadb33SDmitri Tikhonov        .stream_id  = 0x210,
15850aadb33SDmitri Tikhonov        .data_sz    = 10,
15950aadb33SDmitri Tikhonov        .data       = "0123456789",
16050aadb33SDmitri Tikhonov        .avail      = 0x100,
16150aadb33SDmitri Tikhonov        .out        =
16250aadb33SDmitri Tikhonov      /*  1      f      d      ooo    ss            1fdoooss */
16350aadb33SDmitri Tikhonov      /*  TYPE   FIN    DLEN   OLEN   SLEN  */
16450aadb33SDmitri Tikhonov        { 0x80 | 0x00 | 0x20 | 0x00 | 0x01,
1655392f7a3SLiteSpeed Tech          0x02, 0x10,                                       /* Stream ID */
16650aadb33SDmitri Tikhonov                                                            /* Offset */
1675392f7a3SLiteSpeed Tech          0x00, 0x0A,                                       /* Data length */
16850aadb33SDmitri Tikhonov          '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
16950aadb33SDmitri Tikhonov        },
17050aadb33SDmitri Tikhonov        .len        = 1 + 2 + 0 + 2 + 10,
17150aadb33SDmitri Tikhonov        .min_sz     = 1 + 2 + 0 + 0 + 1,
17250aadb33SDmitri Tikhonov    },
17350aadb33SDmitri Tikhonov
17450aadb33SDmitri Tikhonov    {   .lineno     = __LINE__,
175fb96f4ddSDmitri Tikhonov        .pf         = select_pf_by_ver(LSQVER_043),
17650aadb33SDmitri Tikhonov        .fin        = { 0, 1, },
17750aadb33SDmitri Tikhonov        .offset     = 0x0,
17850aadb33SDmitri Tikhonov        .stream_id  = 0x210,
17950aadb33SDmitri Tikhonov        .data_sz    = 1,
18050aadb33SDmitri Tikhonov        .data       = "0123456789",
18150aadb33SDmitri Tikhonov        .avail      = 0x100,
18250aadb33SDmitri Tikhonov        .out        =
18350aadb33SDmitri Tikhonov      /*  1      f      d      ooo    ss            1fdoooss */
18450aadb33SDmitri Tikhonov      /*  TYPE   FIN    DLEN   OLEN   SLEN  */
18550aadb33SDmitri Tikhonov        { 0x80 | 0x40 | 0x20 | 0x00 | 0x01,
1865392f7a3SLiteSpeed Tech          0x02, 0x10,                                       /* Stream ID */
18750aadb33SDmitri Tikhonov                                                            /* Offset */
1885392f7a3SLiteSpeed Tech          0x00, 0x01,                                       /* Data length */
18950aadb33SDmitri Tikhonov          '0',
19050aadb33SDmitri Tikhonov        },
19150aadb33SDmitri Tikhonov        .len        = 1 + 2 + 0 + 2 + 1,
19250aadb33SDmitri Tikhonov        .min_sz     = 1 + 2 + 0 + 0 + 1,
19350aadb33SDmitri Tikhonov    },
19450aadb33SDmitri Tikhonov
19550aadb33SDmitri Tikhonov    {   .lineno     = __LINE__,
196fb96f4ddSDmitri Tikhonov        .pf         = select_pf_by_ver(LSQVER_043),
19750aadb33SDmitri Tikhonov        .fin        = { 0, 0, },
19850aadb33SDmitri Tikhonov        .offset     = 0xFFFFFF,
19950aadb33SDmitri Tikhonov        .stream_id  = 0x210,
20050aadb33SDmitri Tikhonov        .data_sz    = 10,
20150aadb33SDmitri Tikhonov        .data       = "0123456789",
20250aadb33SDmitri Tikhonov        .avail      = 0x100,
20350aadb33SDmitri Tikhonov        .out        =
20450aadb33SDmitri Tikhonov      /*  1      f      d      ooo    ss            1fdoooss */
20550aadb33SDmitri Tikhonov      /*  TYPE   FIN    DLEN   OLEN   SLEN  */
20650aadb33SDmitri Tikhonov        { 0x80 | 0x00 | 0x20 | 0x08 | 0x01,
2075392f7a3SLiteSpeed Tech          0x02, 0x10,                                       /* Stream ID */
20850aadb33SDmitri Tikhonov          0xFF, 0xFF, 0xFF,                                 /* Offset */
2095392f7a3SLiteSpeed Tech          0x00, 0x0A,                                       /* Data length */
21050aadb33SDmitri Tikhonov          '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
21150aadb33SDmitri Tikhonov        },
21250aadb33SDmitri Tikhonov        .len        = 1 + 2 + 3 + 2 + 10,
21350aadb33SDmitri Tikhonov        .min_sz     = 1 + 2 + 3 + 0 + 1,
21450aadb33SDmitri Tikhonov    },
21550aadb33SDmitri Tikhonov
21650aadb33SDmitri Tikhonov    {   .lineno     = __LINE__,
217fb96f4ddSDmitri Tikhonov        .pf         = select_pf_by_ver(LSQVER_043),
21850aadb33SDmitri Tikhonov        .fin        = { 0, 0, },
21950aadb33SDmitri Tikhonov        .offset     = 0xFFFFFF + 1,
22050aadb33SDmitri Tikhonov        .stream_id  = 0x210,
22150aadb33SDmitri Tikhonov        .data_sz    = 10,
22250aadb33SDmitri Tikhonov        .data       = "0123456789",
22350aadb33SDmitri Tikhonov        .avail      = 0x100,
22450aadb33SDmitri Tikhonov        .out        =
22550aadb33SDmitri Tikhonov      /*  1      f      d      ooo    ss            1fdoooss */
22650aadb33SDmitri Tikhonov      /*  TYPE   FIN    DLEN   OLEN   SLEN  */
22750aadb33SDmitri Tikhonov        { 0x80 | 0x00 | 0x20 | 0x0C | 0x01,
2285392f7a3SLiteSpeed Tech          0x02, 0x10,                                       /* Stream ID */
2295392f7a3SLiteSpeed Tech          0x01, 0x00, 0x00, 0x00,                           /* Offset */
2305392f7a3SLiteSpeed Tech          0x00, 0x0A,                                       /* Data length */
23150aadb33SDmitri Tikhonov          '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
23250aadb33SDmitri Tikhonov        },
23350aadb33SDmitri Tikhonov        .len        = 1 + 2 + 4 + 2 + 10,
23450aadb33SDmitri Tikhonov        .min_sz     = 1 + 2 + 4 + 0 + 1,
23550aadb33SDmitri Tikhonov    },
23650aadb33SDmitri Tikhonov
23750aadb33SDmitri Tikhonov    {   .lineno     = __LINE__,
238fb96f4ddSDmitri Tikhonov        .pf         = select_pf_by_ver(LSQVER_043),
23950aadb33SDmitri Tikhonov        .fin        = { 0, 0, },
24050aadb33SDmitri Tikhonov        .offset     = 0xFFFFFF + 1,
24150aadb33SDmitri Tikhonov        .stream_id  = 0x210,
24250aadb33SDmitri Tikhonov        .data_sz    = 10,
24350aadb33SDmitri Tikhonov        .data       = "0123456789",
24450aadb33SDmitri Tikhonov        .avail      = 10,
24550aadb33SDmitri Tikhonov        .out        =
24650aadb33SDmitri Tikhonov      /*  1      f      d      ooo    ss            1fdoooss */
24750aadb33SDmitri Tikhonov      /*  TYPE   FIN    DLEN   OLEN   SLEN  */
24850aadb33SDmitri Tikhonov        { 0x80 | 0x00 | 0x00 | 0x0C | 0x01,
2495392f7a3SLiteSpeed Tech          0x02, 0x10,                                       /* Stream ID */
2505392f7a3SLiteSpeed Tech          0x01, 0x00, 0x00, 0x00,                           /* Offset */
25150aadb33SDmitri Tikhonov          '0', '1', '2',
25250aadb33SDmitri Tikhonov        },
25350aadb33SDmitri Tikhonov        .len        = 1 + 2 + 4 + 0 + 3,
25450aadb33SDmitri Tikhonov        .min_sz     = 1 + 2 + 4 + 0 + 1,
25550aadb33SDmitri Tikhonov    },
25650aadb33SDmitri Tikhonov
25750aadb33SDmitri Tikhonov    {   .lineno     = __LINE__,
258fb96f4ddSDmitri Tikhonov        .pf         = select_pf_by_ver(LSQVER_043),
25950aadb33SDmitri Tikhonov        .fin        = { 1, 0, },
26050aadb33SDmitri Tikhonov        .offset     = 0xB4,
26150aadb33SDmitri Tikhonov        .stream_id  = 0x01,
26250aadb33SDmitri Tikhonov        .data_sz    = 0,
26350aadb33SDmitri Tikhonov        .data       = "0123456789",
26450aadb33SDmitri Tikhonov        .avail      = 0x100,
26550aadb33SDmitri Tikhonov        .out        =
26650aadb33SDmitri Tikhonov      /*  1      f      d      ooo    ss            1fdoooss */
26750aadb33SDmitri Tikhonov      /*  TYPE   FIN    DLEN   OLEN   SLEN  */
26850aadb33SDmitri Tikhonov        { 0x80 | 0x40 | 0x20 | 0x04 | 0x00,
26950aadb33SDmitri Tikhonov          0x01,                                             /* Stream ID */
2705392f7a3SLiteSpeed Tech          0x00, 0xB4,                                       /* Offset */
27150aadb33SDmitri Tikhonov          0x00, 0x00,                                       /* Data length */
27250aadb33SDmitri Tikhonov        },
27350aadb33SDmitri Tikhonov        .len        = 6,
27450aadb33SDmitri Tikhonov        .min_sz     = 6,
27550aadb33SDmitri Tikhonov    },
27650aadb33SDmitri Tikhonov
27750aadb33SDmitri Tikhonov    /*
2785392f7a3SLiteSpeed Tech     * IETF QUIC Internet-Draft 17:
27950aadb33SDmitri Tikhonov     */
2805392f7a3SLiteSpeed Tech
28150aadb33SDmitri Tikhonov    {   .lineno     = __LINE__,
282fb73393fSDmitri Tikhonov        .pf         = select_pf_by_ver(LSQVER_ID27),
28350aadb33SDmitri Tikhonov        .fin        = { 0, 1, },
28450aadb33SDmitri Tikhonov        .offset     = 0x0807060504030201UL,
28550aadb33SDmitri Tikhonov        .stream_id  = 0x210,
28650aadb33SDmitri Tikhonov        .data_sz    = 10,
28750aadb33SDmitri Tikhonov        .data       = "0123456789",
28850aadb33SDmitri Tikhonov        .avail      = 0x100,
28950aadb33SDmitri Tikhonov        .out        =
2905392f7a3SLiteSpeed Tech      /*  TYPE   OFF    DLEN   FIN  */
2915392f7a3SLiteSpeed Tech        { 0x08 | 1<<2 | 1<<1 | 1<<0,
2925392f7a3SLiteSpeed Tech          0x42, 0x10,                                       /* Stream ID */
2935392f7a3SLiteSpeed Tech          0xC8, 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01,   /* Offset */
2945392f7a3SLiteSpeed Tech          0x0A,                                             /* Data length */
29550aadb33SDmitri Tikhonov          '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
29650aadb33SDmitri Tikhonov        },
2975392f7a3SLiteSpeed Tech        .len        = 1 + 2 + 8 + 1 + 10,
29850aadb33SDmitri Tikhonov        .min_sz     = 1 + 2 + 8 + 0 + 1,
29950aadb33SDmitri Tikhonov    },
30050aadb33SDmitri Tikhonov
30150aadb33SDmitri Tikhonov    {   .lineno     = __LINE__,
302fb73393fSDmitri Tikhonov        .pf         = select_pf_by_ver(LSQVER_ID27),
30350aadb33SDmitri Tikhonov        .fin        = { 0, 0, },
3045392f7a3SLiteSpeed Tech        .offset     = 0,
30550aadb33SDmitri Tikhonov        .stream_id  = 0x210,
30650aadb33SDmitri Tikhonov        .data_sz    = 10,
30750aadb33SDmitri Tikhonov        .data       = "0123456789",
30850aadb33SDmitri Tikhonov        .avail      = 0x100,
30950aadb33SDmitri Tikhonov        .out        =
3105392f7a3SLiteSpeed Tech      /*  TYPE   OFF    DLEN   FIN  */
3115392f7a3SLiteSpeed Tech        { 0x08 | 0<<2 | 1<<1 | 0<<0,
3125392f7a3SLiteSpeed Tech          0x42, 0x10,                                       /* Stream ID */
3135392f7a3SLiteSpeed Tech          0x0A,                                             /* Data length */
31450aadb33SDmitri Tikhonov          '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
31550aadb33SDmitri Tikhonov        },
3165392f7a3SLiteSpeed Tech        .len        = 1 + 2 + 0 + 1 + 10,
31750aadb33SDmitri Tikhonov        .min_sz     = 1 + 2 + 0 + 0 + 1,
31850aadb33SDmitri Tikhonov    },
31950aadb33SDmitri Tikhonov
32050aadb33SDmitri Tikhonov    {   .lineno     = __LINE__,
321fb73393fSDmitri Tikhonov        .pf         = select_pf_by_ver(LSQVER_ID27),
32250aadb33SDmitri Tikhonov        .fin        = { 0, 0, },
3235392f7a3SLiteSpeed Tech        .offset     = 0,
3245392f7a3SLiteSpeed Tech        .stream_id  = 0x21,
32550aadb33SDmitri Tikhonov        .data_sz    = 10,
32650aadb33SDmitri Tikhonov        .data       = "0123456789",
3275392f7a3SLiteSpeed Tech        .avail      = 12,
32850aadb33SDmitri Tikhonov        .out        =
3295392f7a3SLiteSpeed Tech      /*  TYPE   OFF    DLEN   FIN  */
3305392f7a3SLiteSpeed Tech        { 0x08 | 0<<2 | 0<<1 | 0<<0,
3315392f7a3SLiteSpeed Tech          0x21,                                             /* Stream ID */
33250aadb33SDmitri Tikhonov          '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
33350aadb33SDmitri Tikhonov        },
3345392f7a3SLiteSpeed Tech        .len        = 1 + 1 + 0 + 0 + 10,
3355392f7a3SLiteSpeed Tech        .min_sz     = 1 + 1 + 0 + 0 + 1,
33650aadb33SDmitri Tikhonov    },
33750aadb33SDmitri Tikhonov
33850aadb33SDmitri Tikhonov    {   .lineno     = __LINE__,
339fb73393fSDmitri Tikhonov        .pf         = select_pf_by_ver(LSQVER_ID27),
34050aadb33SDmitri Tikhonov        .fin        = { 0, 0, },
3415392f7a3SLiteSpeed Tech        .offset     = 0x0807060504030201UL,
34250aadb33SDmitri Tikhonov        .stream_id  = 0x210,
34350aadb33SDmitri Tikhonov        .data_sz    = 10,
34450aadb33SDmitri Tikhonov        .data       = "0123456789",
34550aadb33SDmitri Tikhonov        .avail      = 0x100,
34650aadb33SDmitri Tikhonov        .out        =
3475392f7a3SLiteSpeed Tech      /*  TYPE   OFF    DLEN   FIN  */
3485392f7a3SLiteSpeed Tech        { 0x08 | 1<<2 | 1<<1 | 0<<0,
3495392f7a3SLiteSpeed Tech          0x42, 0x10,                                       /* Stream ID */
3505392f7a3SLiteSpeed Tech          0xC8, 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01,   /* Offset */
3515392f7a3SLiteSpeed Tech          0x0A,                                             /* Data length */
35250aadb33SDmitri Tikhonov          '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
35350aadb33SDmitri Tikhonov        },
3545392f7a3SLiteSpeed Tech        .len        = 1 + 2 + 8 + 1 + 10,
3555392f7a3SLiteSpeed Tech        .min_sz     = 1 + 2 + 8 + 0 + 1,
35650aadb33SDmitri Tikhonov    },
35750aadb33SDmitri Tikhonov
35850aadb33SDmitri Tikhonov    {   .lineno     = __LINE__,
359fb73393fSDmitri Tikhonov        .pf         = select_pf_by_ver(LSQVER_ID27),
3605392f7a3SLiteSpeed Tech        .fin        = { 1, 0, },
3615392f7a3SLiteSpeed Tech        .offset     = 0x0807060504030201UL,
36250aadb33SDmitri Tikhonov        .stream_id  = 0x210,
3635392f7a3SLiteSpeed Tech        .data_sz    = 0,
36450aadb33SDmitri Tikhonov        .data       = "0123456789",
3655392f7a3SLiteSpeed Tech        .avail      = 11,
36650aadb33SDmitri Tikhonov        .out        =
3675392f7a3SLiteSpeed Tech      /*  TYPE   OFF    DLEN   FIN  */
3685392f7a3SLiteSpeed Tech        { 0x08 | 1<<2 | 0<<1 | 1<<0,
3695392f7a3SLiteSpeed Tech          0x42, 0x10,                                       /* Stream ID */
3705392f7a3SLiteSpeed Tech          0xC8, 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01,   /* Offset */
37150aadb33SDmitri Tikhonov        },
3725392f7a3SLiteSpeed Tech        .len        = 1 + 2 + 8,
3735392f7a3SLiteSpeed Tech        .min_sz     = 1 + 2 + 8,
37450aadb33SDmitri Tikhonov    },
37550aadb33SDmitri Tikhonov
37650aadb33SDmitri Tikhonov    {   .lineno     = __LINE__,
377fb73393fSDmitri Tikhonov        .pf         = select_pf_by_ver(LSQVER_ID27),
37850aadb33SDmitri Tikhonov        .fin        = { 1, 0, },
3795392f7a3SLiteSpeed Tech        .offset     = 0x0807060504030201UL,
3805392f7a3SLiteSpeed Tech        .stream_id  = 0x210,
38150aadb33SDmitri Tikhonov        .data_sz    = 0,
38250aadb33SDmitri Tikhonov        .data       = "0123456789",
38350aadb33SDmitri Tikhonov        .avail      = 0x100,
38450aadb33SDmitri Tikhonov        .out        =
3855392f7a3SLiteSpeed Tech      /*  TYPE   OFF    DLEN   FIN  */
3865392f7a3SLiteSpeed Tech        { 0x08 | 1<<2 | 1<<1 | 1<<0,
3875392f7a3SLiteSpeed Tech          0x42, 0x10,                                       /* Stream ID */
3885392f7a3SLiteSpeed Tech          0xC8, 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01,   /* Offset */
3895392f7a3SLiteSpeed Tech          0x00,                                             /* Data length */
39050aadb33SDmitri Tikhonov        },
3915392f7a3SLiteSpeed Tech        .len        = 1 + 2 + 8 + 1,
3925392f7a3SLiteSpeed Tech        .min_sz     = 1 + 2 + 8,
39350aadb33SDmitri Tikhonov    },
39450aadb33SDmitri Tikhonov
39550aadb33SDmitri Tikhonov};
39650aadb33SDmitri Tikhonov
39750aadb33SDmitri Tikhonov
39850aadb33SDmitri Tikhonovstatic struct test_ctx {
39950aadb33SDmitri Tikhonov    const struct test *test;
40050aadb33SDmitri Tikhonov    int                next_fin;
40150aadb33SDmitri Tikhonov    lsquic_stream_t    stream;
40250aadb33SDmitri Tikhonov} test_ctx;
40350aadb33SDmitri Tikhonov
40450aadb33SDmitri Tikhonov
40550aadb33SDmitri Tikhonovstatic int
40650aadb33SDmitri Tikhonovstream_tosend_fin (void *stream)
40750aadb33SDmitri Tikhonov{
408461e84d8SAmol Deshpande    struct test_ctx *test_ctx2 = stream;
409461e84d8SAmol Deshpande    return test_ctx2->test->fin[ test_ctx2->next_fin++ ];
41050aadb33SDmitri Tikhonov}
41150aadb33SDmitri Tikhonov
41250aadb33SDmitri Tikhonov
41350aadb33SDmitri Tikhonovstatic size_t
41450aadb33SDmitri Tikhonovstream_tosend_read (void *stream, void *buf, size_t len, int *reached_fin)
41550aadb33SDmitri Tikhonov{
416461e84d8SAmol Deshpande    struct test_ctx *test_ctx2 = stream;
417461e84d8SAmol Deshpande    if (test_ctx2->test->data_sz < len)
418461e84d8SAmol Deshpande        len = test_ctx2->test->data_sz;
419461e84d8SAmol Deshpande    memcpy(buf, test_ctx2->test->data, len);
42050aadb33SDmitri Tikhonov    *reached_fin = stream_tosend_fin(stream);
42150aadb33SDmitri Tikhonov    return len;
42250aadb33SDmitri Tikhonov}
42350aadb33SDmitri Tikhonov
42450aadb33SDmitri Tikhonov
42550aadb33SDmitri Tikhonovstatic size_t
42650aadb33SDmitri Tikhonovstream_tosend_size (void *stream)
42750aadb33SDmitri Tikhonov{
428461e84d8SAmol Deshpande    struct test_ctx *test_ctx2 = stream;
429461e84d8SAmol Deshpande    return test_ctx2->test->data_sz;
43050aadb33SDmitri Tikhonov}
43150aadb33SDmitri Tikhonov
43250aadb33SDmitri Tikhonov
43350aadb33SDmitri Tikhonovstatic void
43450aadb33SDmitri Tikhonovreset_ctx (const struct test *test)
43550aadb33SDmitri Tikhonov{
43650aadb33SDmitri Tikhonov    test_ctx.test      = test;
43750aadb33SDmitri Tikhonov    test_ctx.next_fin  = 0;
43850aadb33SDmitri Tikhonov    test_ctx.stream.id = test->stream_id;
43950aadb33SDmitri Tikhonov}
44050aadb33SDmitri Tikhonov
44150aadb33SDmitri Tikhonov
44250aadb33SDmitri Tikhonovstatic void
44350aadb33SDmitri Tikhonovrun_test (int i)
44450aadb33SDmitri Tikhonov{
44550aadb33SDmitri Tikhonov    const struct test *const test = &tests[i];
44650aadb33SDmitri Tikhonov
44750aadb33SDmitri Tikhonov    unsigned char out[0x100];
44850aadb33SDmitri Tikhonov    int len;
44950aadb33SDmitri Tikhonov    size_t min;
45050aadb33SDmitri Tikhonov
45150aadb33SDmitri Tikhonov    if (test->len > 0)
45250aadb33SDmitri Tikhonov    {
45350aadb33SDmitri Tikhonov        /* Test that all sizes under specified min fail to produce a frame */
45450aadb33SDmitri Tikhonov        for (min = 0; min < test->min_sz; ++min)
45550aadb33SDmitri Tikhonov        {
45650aadb33SDmitri Tikhonov            reset_ctx(test);
457bfc7bfd8SDmitri Tikhonov            len = test->pf->pf_gen_stream_frame(out, min, test->stream_id,
458bfc7bfd8SDmitri Tikhonov                        test_ctx.test->offset, stream_tosend_fin(&test_ctx),
459bfc7bfd8SDmitri Tikhonov                        stream_tosend_size(&test_ctx), stream_tosend_read, &test_ctx);
46014e3680dSDmitri Tikhonov            assert(len < 0);
46150aadb33SDmitri Tikhonov        }
46250aadb33SDmitri Tikhonov
46350aadb33SDmitri Tikhonov        /* Test that it succeeds now: */
46450aadb33SDmitri Tikhonov        reset_ctx(test);
465bfc7bfd8SDmitri Tikhonov        len = test->pf->pf_gen_stream_frame(out, min, test->stream_id,
466bfc7bfd8SDmitri Tikhonov                        test_ctx.test->offset, stream_tosend_fin(&test_ctx),
467bfc7bfd8SDmitri Tikhonov                        stream_tosend_size(&test_ctx), stream_tosend_read, &test_ctx);
46850aadb33SDmitri Tikhonov        assert(len == (int) min);
46950aadb33SDmitri Tikhonov    }
47050aadb33SDmitri Tikhonov
47150aadb33SDmitri Tikhonov    reset_ctx(test);
472bfc7bfd8SDmitri Tikhonov    len = test->pf->pf_gen_stream_frame(out, test->avail, test->stream_id,
473bfc7bfd8SDmitri Tikhonov                    test_ctx.test->offset, stream_tosend_fin(&test_ctx),
474bfc7bfd8SDmitri Tikhonov                    stream_tosend_size(&test_ctx), stream_tosend_read, &test_ctx);
47550aadb33SDmitri Tikhonov
47650aadb33SDmitri Tikhonov    if (test->len > 0) {
47750aadb33SDmitri Tikhonov        /* Check parser operation */
47850aadb33SDmitri Tikhonov        assert(test->len == len);
47950aadb33SDmitri Tikhonov        assert(("Generated frame is correct", 0 == memcmp(test->out, out, test->len)));
48050aadb33SDmitri Tikhonov    }
48150aadb33SDmitri Tikhonov    else
48250aadb33SDmitri Tikhonov    {
48350aadb33SDmitri Tikhonov        assert(("This test should fail", len < 0));
48450aadb33SDmitri Tikhonov    }
48550aadb33SDmitri Tikhonov}
48650aadb33SDmitri Tikhonov
48750aadb33SDmitri Tikhonov
48850aadb33SDmitri Tikhonovint
48950aadb33SDmitri Tikhonovmain (void)
49050aadb33SDmitri Tikhonov{
49150aadb33SDmitri Tikhonov    unsigned i;
49250aadb33SDmitri Tikhonov    for (i = 0; i < sizeof(tests) / sizeof(tests[0]); ++i)
49350aadb33SDmitri Tikhonov        run_test(i);
49450aadb33SDmitri Tikhonov    return 0;
49550aadb33SDmitri Tikhonov}
496