summaryrefslogtreecommitdiff
path: root/test/quic_cfq_test.c
blob: dbc4eb4b7ee9d3acae9f7d391bf93772e47a94a6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/*
 * Copyright 2022 The OpenSSL Project Authors. All Rights Reserved.
 *
 * Licensed under the Apache License 2.0 (the "License").  You may not use
 * this file except in compliance with the License.  You can obtain a copy
 * in the file LICENSE in the source distribution or at
 * https://www.openssl.org/source/license.html
 */

#include "internal/packet.h"
#include "internal/quic_cfq.h"
#include "internal/quic_wire.h"
#include "testutil.h"

static const unsigned char ref_buf[] = {
    0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19
};

static const uint32_t ref_priority[] = {
    90, 80, 70, 60, 95, 40, 94, 20, 10, 0
};

static const uint32_t ref_pn_space[] = {
    QUIC_PN_SPACE_INITIAL,
    QUIC_PN_SPACE_HANDSHAKE,
    QUIC_PN_SPACE_HANDSHAKE,
    QUIC_PN_SPACE_INITIAL,
    QUIC_PN_SPACE_INITIAL,
    QUIC_PN_SPACE_INITIAL,
    QUIC_PN_SPACE_INITIAL,
    QUIC_PN_SPACE_INITIAL,
    QUIC_PN_SPACE_APP,
    QUIC_PN_SPACE_APP,
};

static const uint64_t ref_frame_type[] = {
    OSSL_QUIC_FRAME_TYPE_NEW_CONN_ID,
    OSSL_QUIC_FRAME_TYPE_NEW_CONN_ID,
    OSSL_QUIC_FRAME_TYPE_NEW_CONN_ID,
    OSSL_QUIC_FRAME_TYPE_NEW_CONN_ID,
    OSSL_QUIC_FRAME_TYPE_NEW_CONN_ID,
    OSSL_QUIC_FRAME_TYPE_NEW_CONN_ID,
    OSSL_QUIC_FRAME_TYPE_NEW_CONN_ID,
    OSSL_QUIC_FRAME_TYPE_NEW_CONN_ID,
    OSSL_QUIC_FRAME_TYPE_NEW_CONN_ID,
    OSSL_QUIC_FRAME_TYPE_RETIRE_CONN_ID,
};

static const uint32_t expect[QUIC_PN_SPACE_NUM][11] = {
    { 4, 6, 0, 3, 5, 7, UINT32_MAX },
    { 1, 2, UINT32_MAX },
    { 8, 9, UINT32_MAX },
};

static QUIC_CFQ_ITEM *items[QUIC_PN_SPACE_NUM][10];

static unsigned char *g_free;
static size_t g_free_len;

static void free_cb(unsigned char *buf, size_t buf_len, void *arg)
{
    g_free      = buf;
    g_free_len  = buf_len;
}

static int check(QUIC_CFQ *cfq)
{
    int testresult = 0;
    QUIC_CFQ_ITEM *item;
    size_t i;
    uint32_t pn_space;

    for (pn_space = QUIC_PN_SPACE_INITIAL; pn_space < QUIC_PN_SPACE_NUM; ++pn_space)
        for (i = 0, item = ossl_quic_cfq_get_priority_head(cfq, pn_space);;
             ++i, item = ossl_quic_cfq_item_get_priority_next(item, pn_space)) {

            if (expect[pn_space][i] == UINT32_MAX) {
                if (!TEST_ptr_null(item))
                    goto err;

                break;
            }

            items[pn_space][i] = item;

            if (!TEST_ptr(item)
                || !TEST_ptr_eq(ossl_quic_cfq_item_get_encoded(item),
                                ref_buf + expect[pn_space][i])
                || !TEST_int_eq(ossl_quic_cfq_item_get_pn_space(item), pn_space)
                || !TEST_int_eq(ossl_quic_cfq_item_get_state(item),
                                QUIC_CFQ_STATE_NEW))
                goto err;
        }

    testresult = 1;
err:
    return testresult;
}

static int test_cfq(void)
{
    int testresult = 0;
    QUIC_CFQ *cfq = NULL;
    QUIC_CFQ_ITEM *item, *inext;
    size_t i;
    uint32_t pn_space;

    if (!TEST_ptr(cfq = ossl_quic_cfq_new()))
        goto err;

    g_free      = NULL;
    g_free_len  = 0;

    for (i = 0; i < OSSL_NELEM(ref_buf); ++i) {
        if (!TEST_ptr(item = ossl_quic_cfq_add_frame(cfq, ref_priority[i],
                                                     ref_pn_space[i],
                                                     ref_frame_type[i],
                                                     ref_buf + i,
                                                     1,
                                                     free_cb,
                                                     NULL))
            || !TEST_int_eq(ossl_quic_cfq_item_get_state(item),
                            QUIC_CFQ_STATE_NEW)
            || !TEST_uint_eq(ossl_quic_cfq_item_get_pn_space(item),
                             ref_pn_space[i])
            || !TEST_uint64_t_eq(ossl_quic_cfq_item_get_frame_type(item),
                                 ref_frame_type[i])
            || !TEST_ptr_eq(ossl_quic_cfq_item_get_encoded(item),
                            ref_buf + i)
            || !TEST_size_t_eq(ossl_quic_cfq_item_get_encoded_len(item),
                               1))
            goto err;
    }

    if (!check(cfq))
        goto err;

    for (pn_space = QUIC_PN_SPACE_INITIAL; pn_space < QUIC_PN_SPACE_NUM; ++pn_space)
        for (item = ossl_quic_cfq_get_priority_head(cfq, pn_space);
             item != NULL; item = inext) {
            inext = ossl_quic_cfq_item_get_priority_next(item, pn_space);

            ossl_quic_cfq_mark_tx(cfq, item);
        }

    for (pn_space = QUIC_PN_SPACE_INITIAL; pn_space < QUIC_PN_SPACE_NUM; ++pn_space)
        if (!TEST_ptr_null(ossl_quic_cfq_get_priority_head(cfq, pn_space)))
            goto err;

    for (pn_space = QUIC_PN_SPACE_INITIAL; pn_space < QUIC_PN_SPACE_NUM; ++pn_space)
        for (i = 0; i < OSSL_NELEM(items[0]); ++i)
            if (items[pn_space][i] != NULL)
                ossl_quic_cfq_mark_lost(cfq, items[pn_space][i], UINT32_MAX);

    if (!check(cfq))
        goto err;

    for (pn_space = QUIC_PN_SPACE_INITIAL; pn_space < QUIC_PN_SPACE_NUM; ++pn_space)
        for (i = 0; i < OSSL_NELEM(items[0]); ++i)
            if (items[pn_space][i] != NULL)
                ossl_quic_cfq_release(cfq, items[pn_space][i]);

    for (pn_space = QUIC_PN_SPACE_INITIAL; pn_space < QUIC_PN_SPACE_NUM; ++pn_space)
        if (!TEST_ptr_null(ossl_quic_cfq_get_priority_head(cfq, pn_space)))
            goto err;

    testresult = 1;
err:
    ossl_quic_cfq_free(cfq);
    return testresult;
}

int setup_tests(void)
{
    ADD_TEST(test_cfq);
    return 1;
}