test_crypto_gen.c revision fb73393f
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#include <sys/queue.h>
7
8#include "lsquic.h"
9#include "lsquic_types.h"
10#include "lsquic_parse.h"
11
12
13struct test {
14    int                 lineno;
15    const struct parse_funcs *
16                        pf;
17    uint64_t            offset;
18    size_t              avail;      /* Space to write stream frame to */
19    size_t              min_sz;     /* Minimum size needed to generate CRYPTO
20                                     * frame.  Any sizes smaller than this
21                                     * should fail.
22                                     */
23    size_t              data_sz;
24    char                data[0x100];
25
26    /* Output.  This is how we expect the resulting frame to look.
27     */
28    int                 len;        /* Return value of pf_gen_crypto_frame() */
29    char                out[0x100];
30};
31
32
33static const struct test tests[] = {
34
35    {   .lineno     = __LINE__,
36        .pf         = select_pf_by_ver(LSQVER_ID27),
37        .offset     = 0,
38        .data_sz    = 10,
39        .data       = "0123456789",
40        .avail      = 0x100,
41        .out        =
42        { /* Type */    0x06,
43          /* Offset */  0x00,
44          /* Size */    0x0A,
45          /* Data */    '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
46        },
47        .len        = 1 + 1 + 1 + 10,
48        .min_sz     = 1 + 1 + 1 + 1,
49    },
50
51    {   .lineno     = __LINE__,
52        .pf         = select_pf_by_ver(LSQVER_ID27),
53        .offset     = 500,
54        .data_sz    = 10,
55        .data       = "0123456789",
56        .avail      = 0x100,
57        .out        =
58        { /* Type */    0x06,
59          /* Offset */  0x41, 0xF4,
60          /* Size */    0x0A,
61          /* Data */    '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
62        },
63        .len        = 1 + 2 + 1 + 10,
64        .min_sz     = 1 + 2 + 1 + 1,
65    },
66
67};
68
69
70struct test_ctx {
71    const struct test   *test;
72    unsigned             off;
73};
74
75
76static size_t
77crypto_read (void *ctx, void *buf, size_t len)
78{
79    struct test_ctx *test_ctx = ctx;
80    if (test_ctx->test->data_sz - test_ctx->off < len)
81        len = test_ctx->test->data_sz - test_ctx->off;
82    memcpy(buf, test_ctx->test->data, len);
83    test_ctx->off += len;
84    return len;
85}
86
87
88static void
89init_ctx (struct test_ctx *test_ctx, const struct test *test)
90{
91    test_ctx->test = test;
92    test_ctx->off  = 0;
93}
94
95
96static void
97run_test (int i)
98{
99    const struct test *const test = &tests[i];
100
101    int len;
102    size_t min;
103    struct test_ctx test_ctx;
104    unsigned char out[0x100];
105
106    if (test->len > 0)
107    {
108        /* Test that all sizes under specified min fail to produce a frame */
109        for (min = 0; min < test->min_sz; ++min)
110        {
111            init_ctx(&test_ctx, test);
112            len = test->pf->pf_gen_crypto_frame(out, min, test->offset,
113                        test->data_sz, crypto_read, &test_ctx);
114            assert(-1 == len);
115        }
116
117        /* Test that it succeeds now: */
118        init_ctx(&test_ctx, test);
119        len = test->pf->pf_gen_crypto_frame(out, min, test->offset,
120                    test->data_sz, crypto_read, &test_ctx);
121        assert(len == (int) min);
122    }
123
124    init_ctx(&test_ctx, test);
125    len = test->pf->pf_gen_crypto_frame(out, test->avail, test->offset,
126                test->data_sz, crypto_read, &test_ctx);
127
128    if (test->len > 0) {
129        assert(test->len == len);
130        assert(0 == memcmp(test->out, out, test->len));
131    }
132    else
133    {
134        assert(len < 0);
135    }
136}
137
138
139int
140main (void)
141{
142    unsigned i;
143    for (i = 0; i < sizeof(tests) / sizeof(tests[0]); ++i)
144        run_test(i);
145    return 0;
146}
147