summaryrefslogtreecommitdiff
path: root/test/quic_fifd_test.c
blob: bb0cd77105c7ccbdca4e2f7e8fed63d523eee9a9 (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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
/*
 * 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_txpim.h"
#include "internal/quic_fifd.h"
#include "testutil.h"

static OSSL_TIME cur_time;

static OSSL_TIME fake_now(void *arg) {
    return cur_time;
}

static void step_time(uint64_t ms) {
    cur_time = ossl_time_add(cur_time, ossl_ms2time(ms));
}

static QUIC_SSTREAM *(*get_sstream_by_id_p)(uint64_t stream_id, uint32_t pn_space,
                                            void *arg);

static QUIC_SSTREAM *get_sstream_by_id(uint64_t stream_id, uint32_t pn_space,
                                       void *arg)
{
    return get_sstream_by_id_p(stream_id, pn_space, arg);
}

static void (*regen_frame_p)(uint64_t frame_type, uint64_t stream_id,
                             QUIC_TXPIM_PKT *pkt, void *arg);

static void regen_frame(uint64_t frame_type, uint64_t stream_id,
                        QUIC_TXPIM_PKT *pkt, void *arg)
{
    regen_frame_p(frame_type, stream_id, pkt, arg);
}

static void confirm_frame(uint64_t frame_type, uint64_t stream_id,
                          QUIC_TXPIM_PKT *pkt, void *arg)
{}

static void sstream_updated(uint64_t stream_id, void *arg)
{}

typedef struct info_st {
    QUIC_FIFD fifd;
    OSSL_ACKM *ackm;
    QUIC_CFQ *cfq;
    QUIC_TXPIM *txpim;
    OSSL_STATM statm;
    OSSL_CC_DATA *ccdata;
    QUIC_SSTREAM *sstream[4];
} INFO;

static INFO *cur_info;
static int cb_fail;
static int cfq_freed;

/* ----------------------------------------------------------------------
 * 1. Test that a submitted packet, on ack, acks all streams inside of it
 *    Test that a submitted packet, on ack, calls the get by ID function
 *      correctly
 *    Test that a submitted packet, on ack, acks all fins inside it
 *    Test that a submitted packet, on ack, releases the TXPIM packet
 */
static QUIC_SSTREAM *sstream_expect(uint64_t stream_id, uint32_t pn_space,
                                    void *arg)
{
    if (stream_id == 42 || stream_id == 43)
        return cur_info->sstream[stream_id - 42];

    cb_fail = 1;
    return NULL;
}

static uint64_t regen_frame_type[16];
static uint64_t regen_stream_id[16];
static size_t regen_count;

static void regen_expect(uint64_t frame_type, uint64_t stream_id,
                         QUIC_TXPIM_PKT *pkt, void *arg)
{
    regen_frame_type[regen_count] = frame_type;
    regen_stream_id[regen_count] = stream_id;
    ++regen_count;
}

static const unsigned char placeholder_data[] = "placeholder";

static void cfq_free_cb_(unsigned char *buf, size_t buf_len, void *arg)
{
    if (buf == placeholder_data && buf_len == sizeof(placeholder_data))
        cfq_freed = 1;
}

#define TEST_KIND_ACK       0
#define TEST_KIND_LOSS      1
#define TEST_KIND_DISCARD   2
#define TEST_KIND_NUM       3

static int test_generic(INFO *info, int kind)
{
    int testresult = 0;
    size_t i, consumed = 0;
    QUIC_TXPIM_PKT *pkt = NULL, *pkt2 = NULL;
    OSSL_QUIC_FRAME_STREAM hdr = {0};
    OSSL_QTX_IOVEC iov[2];
    size_t num_iov;
    QUIC_TXPIM_CHUNK chunk = {42, 0, 11, 0};
    OSSL_QUIC_FRAME_ACK ack = {0};
    OSSL_QUIC_ACK_RANGE ack_ranges[1] = {0};
    QUIC_CFQ_ITEM *cfq_item = NULL;
    uint32_t pn_space = (kind == TEST_KIND_DISCARD)
        ? QUIC_PN_SPACE_HANDSHAKE : QUIC_PN_SPACE_APP;

    cur_time            = ossl_seconds2time(1000);
    regen_count         = 0;

    get_sstream_by_id_p = sstream_expect;
    regen_frame_p       = regen_expect;

    if (!TEST_ptr(pkt = ossl_quic_txpim_pkt_alloc(info->txpim)))
        goto err;

    for (i = 0; i < 2; ++i) {
        num_iov = OSSL_NELEM(iov);
        if (!TEST_true(ossl_quic_sstream_append(info->sstream[i],
                                                (unsigned char *)"Test message",
                                                12, &consumed))
            || !TEST_size_t_eq(consumed, 12))
            goto err;

        if (i == 1)
            ossl_quic_sstream_fin(info->sstream[i]);

        if (!TEST_true(ossl_quic_sstream_get_stream_frame(info->sstream[i], 0,
                                                          &hdr, iov, &num_iov))
            || !TEST_int_eq(hdr.is_fin, i == 1)
            || !TEST_uint64_t_eq(hdr.offset, 0)
            || !TEST_uint64_t_eq(hdr.len, 12)
            || !TEST_size_t_eq(ossl_quic_sstream_get_buffer_used(info->sstream[i]), 12)
            || !TEST_true(ossl_quic_sstream_mark_transmitted(info->sstream[i],
                                                             hdr.offset,
                                                             hdr.offset + hdr.len - 1)))
            goto err;

        if (i == 1 && !TEST_true(ossl_quic_sstream_mark_transmitted_fin(info->sstream[i],
                                                                        hdr.offset + hdr.len)))
            goto err;

        chunk.has_fin   = hdr.is_fin;
        chunk.stream_id = 42 + i;
        if (!TEST_true(ossl_quic_txpim_pkt_append_chunk(pkt, &chunk)))
            goto err;
    }

    cfq_freed = 0;
    if (!TEST_ptr(cfq_item = ossl_quic_cfq_add_frame(info->cfq, 10,
                                                     pn_space,
                                                     OSSL_QUIC_FRAME_TYPE_NEW_CONN_ID,
                                                     placeholder_data,
                                                     sizeof(placeholder_data),
                                                     cfq_free_cb_, NULL))
        || !TEST_ptr_eq(cfq_item, ossl_quic_cfq_get_priority_head(info->cfq, pn_space)))
        goto err;

    ossl_quic_txpim_pkt_add_cfq_item(pkt, cfq_item);

    pkt->ackm_pkt.pkt_num           = 0;
    pkt->ackm_pkt.pkt_space         = pn_space;
    pkt->ackm_pkt.largest_acked     = QUIC_PN_INVALID;
    pkt->ackm_pkt.num_bytes         = 50;
    pkt->ackm_pkt.time              = cur_time;
    pkt->ackm_pkt.is_inflight       = 1;
    pkt->ackm_pkt.is_ack_eliciting  = 1;
    if (kind == TEST_KIND_LOSS) {
        pkt->had_handshake_done_frame   = 1;
        pkt->had_max_data_frame         = 1;
        pkt->had_max_streams_bidi_frame = 1;
        pkt->had_max_streams_uni_frame  = 1;
        pkt->had_ack_frame              = 1;
    }

    ack_ranges[0].start = 0;
    ack_ranges[0].end   = 0;
    ack.ack_ranges      = ack_ranges;
    ack.num_ack_ranges  = 1;

    if (!TEST_true(ossl_quic_fifd_pkt_commit(&info->fifd, pkt)))
        goto err;

    /* CFQ item should have been marked as transmitted */
    if (!TEST_ptr_null(ossl_quic_cfq_get_priority_head(info->cfq, pn_space)))
        goto err;

    switch (kind) {
    case TEST_KIND_ACK:
        if (!TEST_true(ossl_ackm_on_rx_ack_frame(info->ackm, &ack,
                                                 pn_space,
                                                 cur_time)))
            goto err;

        for (i = 0; i < 2; ++i)
            if (!TEST_size_t_eq(ossl_quic_sstream_get_buffer_used(info->sstream[i]), 0))
                goto err;

        /* This should fail, which proves the FIN was acked */
        if (!TEST_false(ossl_quic_sstream_mark_lost_fin(info->sstream[1])))
            goto err;

        /* CFQ item must have been released */
        if (!TEST_true(cfq_freed))
            goto err;

        /* No regen calls should have been made */
        if (!TEST_size_t_eq(regen_count, 0))
            goto err;

        break;

    case TEST_KIND_LOSS:
        /* Trigger loss detection via packet threshold. */
        if (!TEST_ptr(pkt2 = ossl_quic_txpim_pkt_alloc(info->txpim)))
            goto err;

        step_time(10000);
        pkt2->ackm_pkt.pkt_num          = 50;
        pkt2->ackm_pkt.pkt_space        = pn_space;
        pkt2->ackm_pkt.largest_acked    = QUIC_PN_INVALID;
        pkt2->ackm_pkt.num_bytes        = 50;
        pkt2->ackm_pkt.time             = cur_time;
        pkt2->ackm_pkt.is_inflight      = 1;
        pkt2->ackm_pkt.is_ack_eliciting = 1;

        ack_ranges[0].start = 50;
        ack_ranges[0].end   = 50;
        ack.ack_ranges      = ack_ranges;
        ack.num_ack_ranges  = 1;

        if (!TEST_true(ossl_quic_fifd_pkt_commit(&info->fifd, pkt2))
            || !TEST_true(ossl_ackm_on_rx_ack_frame(info->ackm, &ack,
                                                    pn_space, cur_time)))
            goto err;

        for (i = 0; i < 2; ++i) {
            num_iov = OSSL_NELEM(iov);
            /*
             * Stream data we sent must have been marked as lost; check by
             * ensuring it is returned again
             */
            if (!TEST_true(ossl_quic_sstream_get_stream_frame(info->sstream[i], 0,
                                                              &hdr, iov, &num_iov))
                || !TEST_uint64_t_eq(hdr.offset, 0)
                || !TEST_uint64_t_eq(hdr.len, 12))
                goto err;
        }

        /* FC frame should have regenerated for each stream */
        if (!TEST_size_t_eq(regen_count, 7)
            || !TEST_uint64_t_eq(regen_stream_id[0], 42)
            || !TEST_uint64_t_eq(regen_frame_type[0], OSSL_QUIC_FRAME_TYPE_MAX_STREAM_DATA)
            || !TEST_uint64_t_eq(regen_stream_id[1], 43)
            || !TEST_uint64_t_eq(regen_frame_type[1], OSSL_QUIC_FRAME_TYPE_MAX_STREAM_DATA)
            || !TEST_uint64_t_eq(regen_frame_type[2], OSSL_QUIC_FRAME_TYPE_HANDSHAKE_DONE)
            || !TEST_uint64_t_eq(regen_stream_id[2], UINT64_MAX)
            || !TEST_uint64_t_eq(regen_frame_type[3], OSSL_QUIC_FRAME_TYPE_MAX_DATA)
            || !TEST_uint64_t_eq(regen_stream_id[3], UINT64_MAX)
            || !TEST_uint64_t_eq(regen_frame_type[4], OSSL_QUIC_FRAME_TYPE_MAX_STREAMS_BIDI)
            || !TEST_uint64_t_eq(regen_stream_id[4], UINT64_MAX)
            || !TEST_uint64_t_eq(regen_frame_type[5], OSSL_QUIC_FRAME_TYPE_MAX_STREAMS_UNI)
            || !TEST_uint64_t_eq(regen_stream_id[5], UINT64_MAX)
            || !TEST_uint64_t_eq(regen_frame_type[6], OSSL_QUIC_FRAME_TYPE_ACK_WITH_ECN)
            || !TEST_uint64_t_eq(regen_stream_id[6], UINT64_MAX))
            goto err;

        /* CFQ item should have been marked as lost */
        if (!TEST_ptr_eq(cfq_item, ossl_quic_cfq_get_priority_head(info->cfq, pn_space)))
            goto err;

        /* FIN should have been marked as lost */
        num_iov = OSSL_NELEM(iov);
        if (!TEST_true(ossl_quic_sstream_get_stream_frame(info->sstream[1], 1,
                                                          &hdr, iov, &num_iov))
            || !TEST_true(hdr.is_fin)
            || !TEST_uint64_t_eq(hdr.len, 0))
            goto err;

        break;

    case TEST_KIND_DISCARD:
        if (!TEST_true(ossl_ackm_on_pkt_space_discarded(info->ackm, pn_space)))
            goto err;

        /* CFQ item must have been released */
        if (!TEST_true(cfq_freed))
            goto err;

        break;

    default:
        goto err;
    }

    /* TXPIM must have been released */
    if (!TEST_size_t_eq(ossl_quic_txpim_get_in_use(info->txpim), 0))
        goto err;

    testresult = 1;
err:
    return testresult;
}

static int test_fifd(int idx)
{
    int testresult = 0;
    INFO info = {0};
    size_t i;

    cur_info = &info;
    cb_fail = 0;

    if (!TEST_true(ossl_statm_init(&info.statm))
        || !TEST_ptr(info.ccdata = ossl_cc_dummy_method.new(fake_now, NULL))
        || !TEST_ptr(info.ackm = ossl_ackm_new(fake_now, NULL,
                                               &info.statm,
                                               &ossl_cc_dummy_method,
                                               info.ccdata))
        || !TEST_true(ossl_ackm_on_handshake_confirmed(info.ackm))
        || !TEST_ptr(info.cfq = ossl_quic_cfq_new())
        || !TEST_ptr(info.txpim = ossl_quic_txpim_new())
        || !TEST_true(ossl_quic_fifd_init(&info.fifd, info.cfq, info.ackm,
                                          info.txpim,
                                          get_sstream_by_id, NULL,
                                          regen_frame, NULL,
                                          confirm_frame, NULL,
                                          sstream_updated, NULL)))
        goto err;

    for (i = 0; i < OSSL_NELEM(info.sstream); ++i)
        if (!TEST_ptr(info.sstream[i] = ossl_quic_sstream_new(1024)))
            goto err;

    ossl_statm_update_rtt(&info.statm, ossl_time_zero(), ossl_ms2time(1));

    if (!TEST_true(test_generic(&info, idx))
        || !TEST_false(cb_fail))
        goto err;

    testresult = 1;
err:
    ossl_quic_fifd_cleanup(&info.fifd);
    ossl_quic_cfq_free(info.cfq);
    ossl_quic_txpim_free(info.txpim);
    ossl_ackm_free(info.ackm);
    ossl_statm_destroy(&info.statm);
    if (info.ccdata != NULL)
        ossl_cc_dummy_method.free(info.ccdata);
    for (i = 0; i < OSSL_NELEM(info.sstream); ++i)
        ossl_quic_sstream_free(info.sstream[i]);
    cur_info = NULL;
    return testresult;
}

int setup_tests(void)
{
    ADD_ALL_TESTS(test_fifd, TEST_KIND_NUM);
    return 1;
}