test_rechist.c revision fb3e20e0
17d09751dSDmitri Tikhonov/* Copyright (c) 2017 - 2020 LiteSpeed Technologies Inc.  See LICENSE. */
250aadb33SDmitri Tikhonov#include <assert.h>
350aadb33SDmitri Tikhonov#include <inttypes.h>
450aadb33SDmitri Tikhonov#include <stdio.h>
550aadb33SDmitri Tikhonov#include <stdlib.h>
650aadb33SDmitri Tikhonov#include <string.h>
750aadb33SDmitri Tikhonov
8fb3e20e0SDmitri Tikhonov#ifdef WIN32
9fb3e20e0SDmitri Tikhonov#include "vc_compat.h"
10fb3e20e0SDmitri Tikhonov#endif
11fb3e20e0SDmitri Tikhonov
1250aadb33SDmitri Tikhonov#include "lsquic_types.h"
1350aadb33SDmitri Tikhonov#include "lsquic_int_types.h"
1450aadb33SDmitri Tikhonov#include "lsquic_rechist.h"
1550aadb33SDmitri Tikhonov#include "lsquic_parse.h"
1650aadb33SDmitri Tikhonov#include "lsquic_util.h"
1750aadb33SDmitri Tikhonov#include "lsquic_logger.h"
1850aadb33SDmitri Tikhonov#include "lsquic.h"
195392f7a3SLiteSpeed Tech#include "lsquic_hash.h"
205392f7a3SLiteSpeed Tech#include "lsquic_conn.h"
215392f7a3SLiteSpeed Tech
225392f7a3SLiteSpeed Tech
235392f7a3SLiteSpeed Techstatic struct lsquic_conn lconn = LSCONN_INITIALIZER_CIDLEN(lconn, 0);
2450aadb33SDmitri Tikhonov
2550aadb33SDmitri Tikhonov
2650aadb33SDmitri Tikhonovstatic void
2750aadb33SDmitri Tikhonovtest4 (void)
2850aadb33SDmitri Tikhonov{
2950aadb33SDmitri Tikhonov    lsquic_rechist_t rechist;
3050aadb33SDmitri Tikhonov    const struct lsquic_packno_range *range;
3150aadb33SDmitri Tikhonov    lsquic_packno_t packno;
3250aadb33SDmitri Tikhonov
335392f7a3SLiteSpeed Tech    lsquic_rechist_init(&rechist, &lconn, 0);
3450aadb33SDmitri Tikhonov
3550aadb33SDmitri Tikhonov    for (packno = 11917; packno <= 11941; ++packno)
3650aadb33SDmitri Tikhonov        lsquic_rechist_received(&rechist, packno, 0);
3750aadb33SDmitri Tikhonov    for (packno = 11946; packno <= 11994; ++packno)
3850aadb33SDmitri Tikhonov        lsquic_rechist_received(&rechist, packno, 0);
3950aadb33SDmitri Tikhonov
4050aadb33SDmitri Tikhonov    range = lsquic_rechist_first(&rechist);
4150aadb33SDmitri Tikhonov    assert(range);
4250aadb33SDmitri Tikhonov    assert(range->high == 11994);
4350aadb33SDmitri Tikhonov    assert(range->low == 11946);
4450aadb33SDmitri Tikhonov    range = lsquic_rechist_next(&rechist);
4550aadb33SDmitri Tikhonov    assert(range);
4650aadb33SDmitri Tikhonov    assert(range->high == 11941);
4750aadb33SDmitri Tikhonov    assert(range->low == 11917);
4850aadb33SDmitri Tikhonov    range = lsquic_rechist_next(&rechist);
4950aadb33SDmitri Tikhonov    assert(!range);
5050aadb33SDmitri Tikhonov
5150aadb33SDmitri Tikhonov    lsquic_rechist_received(&rechist, 11995, 0);
5250aadb33SDmitri Tikhonov    lsquic_rechist_received(&rechist, 11996, 0);
5350aadb33SDmitri Tikhonov
5450aadb33SDmitri Tikhonov    range = lsquic_rechist_first(&rechist);
5550aadb33SDmitri Tikhonov    assert(range);
5650aadb33SDmitri Tikhonov    assert(range->high == 11996);
5750aadb33SDmitri Tikhonov    assert(range->low == 11946);
5850aadb33SDmitri Tikhonov    range = lsquic_rechist_next(&rechist);
5950aadb33SDmitri Tikhonov    assert(range);
6050aadb33SDmitri Tikhonov    assert(range->high == 11941);
6150aadb33SDmitri Tikhonov    assert(range->low == 11917);
6250aadb33SDmitri Tikhonov    range = lsquic_rechist_next(&rechist);
6350aadb33SDmitri Tikhonov    assert(!range);
6450aadb33SDmitri Tikhonov
6550aadb33SDmitri Tikhonov    lsquic_rechist_received(&rechist, 11912, 0);
6650aadb33SDmitri Tikhonov    lsquic_rechist_stop_wait(&rechist, 11860);
6750aadb33SDmitri Tikhonov
6850aadb33SDmitri Tikhonov    range = lsquic_rechist_first(&rechist);
6950aadb33SDmitri Tikhonov    assert(range);
7050aadb33SDmitri Tikhonov    assert(range->high == 11996);
7150aadb33SDmitri Tikhonov    assert(range->low == 11946);
7250aadb33SDmitri Tikhonov    range = lsquic_rechist_next(&rechist);
7350aadb33SDmitri Tikhonov    assert(range);
7450aadb33SDmitri Tikhonov    assert(range->high == 11941);
7550aadb33SDmitri Tikhonov    assert(range->low == 11917);
7650aadb33SDmitri Tikhonov    range = lsquic_rechist_next(&rechist);
7750aadb33SDmitri Tikhonov    assert(range);
7850aadb33SDmitri Tikhonov    assert(range->high == 11912);
7950aadb33SDmitri Tikhonov    assert(range->low == 11912);
8050aadb33SDmitri Tikhonov    range = lsquic_rechist_next(&rechist);
8150aadb33SDmitri Tikhonov    assert(!range);
8250aadb33SDmitri Tikhonov
8350aadb33SDmitri Tikhonov    for (packno = 12169; packno <= 12193; ++packno)
8450aadb33SDmitri Tikhonov        lsquic_rechist_received(&rechist, packno, 0);
8550aadb33SDmitri Tikhonov
8650aadb33SDmitri Tikhonov    range = lsquic_rechist_first(&rechist);
8750aadb33SDmitri Tikhonov    assert(range);
8850aadb33SDmitri Tikhonov    assert(range->high == 12193);
8950aadb33SDmitri Tikhonov    assert(range->low == 12169);
9050aadb33SDmitri Tikhonov    range = lsquic_rechist_next(&rechist);
9150aadb33SDmitri Tikhonov    assert(range);
9250aadb33SDmitri Tikhonov    assert(range->high == 11996);
9350aadb33SDmitri Tikhonov    assert(range->low == 11946);
9450aadb33SDmitri Tikhonov    range = lsquic_rechist_next(&rechist);
9550aadb33SDmitri Tikhonov    assert(range);
9650aadb33SDmitri Tikhonov    assert(range->high == 11941);
9750aadb33SDmitri Tikhonov    assert(range->low == 11917);
9850aadb33SDmitri Tikhonov    range = lsquic_rechist_next(&rechist);
9950aadb33SDmitri Tikhonov    assert(range);
10050aadb33SDmitri Tikhonov    assert(range->high == 11912);
10150aadb33SDmitri Tikhonov    assert(range->low == 11912);
10250aadb33SDmitri Tikhonov    range = lsquic_rechist_next(&rechist);
10350aadb33SDmitri Tikhonov    assert(!range);
10450aadb33SDmitri Tikhonov
10550aadb33SDmitri Tikhonov    lsquic_rechist_cleanup(&rechist);
10650aadb33SDmitri Tikhonov}
10750aadb33SDmitri Tikhonov
10850aadb33SDmitri Tikhonov
10950aadb33SDmitri Tikhonovstatic void
11050aadb33SDmitri Tikhonovrechist2str (lsquic_rechist_t *rechist, char *buf, size_t bufsz)
11150aadb33SDmitri Tikhonov{
11250aadb33SDmitri Tikhonov    const struct lsquic_packno_range *range;
11350aadb33SDmitri Tikhonov    size_t off;
11450aadb33SDmitri Tikhonov    int n;
11550aadb33SDmitri Tikhonov
11650aadb33SDmitri Tikhonov    for (off = 0, range = lsquic_rechist_first(rechist);
11750aadb33SDmitri Tikhonov            range && off < bufsz;
11850aadb33SDmitri Tikhonov                off += n, range = lsquic_rechist_next(rechist))
11950aadb33SDmitri Tikhonov    {
12050aadb33SDmitri Tikhonov        n = snprintf(buf + off, bufsz - off, "[%"PRIu64"-%"PRIu64"]",
12150aadb33SDmitri Tikhonov                                                    range->high, range->low);
12250aadb33SDmitri Tikhonov        if (n < 0 || (size_t) n >= bufsz - off)
12350aadb33SDmitri Tikhonov            break;
12450aadb33SDmitri Tikhonov    }
12550aadb33SDmitri Tikhonov}
12650aadb33SDmitri Tikhonov
12750aadb33SDmitri Tikhonov
12850aadb33SDmitri Tikhonovstatic void
12950aadb33SDmitri Tikhonovtest5 (void)
13050aadb33SDmitri Tikhonov{
13150aadb33SDmitri Tikhonov    lsquic_rechist_t rechist;
13250aadb33SDmitri Tikhonov    char buf[100];
13350aadb33SDmitri Tikhonov
1345392f7a3SLiteSpeed Tech    lsquic_rechist_init(&rechist, &lconn, 0);
13550aadb33SDmitri Tikhonov
13650aadb33SDmitri Tikhonov    lsquic_rechist_received(&rechist, 1, 0);
13750aadb33SDmitri Tikhonov    /* Packet 2 omitted because it could not be decrypted */
13850aadb33SDmitri Tikhonov    lsquic_rechist_received(&rechist, 3, 0);
13950aadb33SDmitri Tikhonov    lsquic_rechist_received(&rechist, 12, 0);
14050aadb33SDmitri Tikhonov
14150aadb33SDmitri Tikhonov    rechist2str(&rechist, buf, sizeof(buf));
14250aadb33SDmitri Tikhonov    assert(0 == strcmp(buf, "[12-12][3-3][1-1]"));
14350aadb33SDmitri Tikhonov
14450aadb33SDmitri Tikhonov    lsquic_rechist_received(&rechist, 4, 0);
14550aadb33SDmitri Tikhonov    lsquic_rechist_received(&rechist, 10, 0);
14650aadb33SDmitri Tikhonov
14750aadb33SDmitri Tikhonov    rechist2str(&rechist, buf, sizeof(buf));
14850aadb33SDmitri Tikhonov    assert(0 == strcmp(buf, "[12-12][10-10][4-3][1-1]"));
14950aadb33SDmitri Tikhonov
15050aadb33SDmitri Tikhonov    lsquic_rechist_received(&rechist, 6, 0);
15150aadb33SDmitri Tikhonov
15250aadb33SDmitri Tikhonov    rechist2str(&rechist, buf, sizeof(buf));
15350aadb33SDmitri Tikhonov    assert(0 == strcmp(buf, "[12-12][10-10][6-6][4-3][1-1]"));
15450aadb33SDmitri Tikhonov
15550aadb33SDmitri Tikhonov    lsquic_rechist_received(&rechist, 7, 0);
15650aadb33SDmitri Tikhonov    lsquic_rechist_received(&rechist, 8, 0);
15750aadb33SDmitri Tikhonov
15850aadb33SDmitri Tikhonov    rechist2str(&rechist, buf, sizeof(buf));
15950aadb33SDmitri Tikhonov    assert(0 == strcmp(buf, "[12-12][10-10][8-6][4-3][1-1]"));
16050aadb33SDmitri Tikhonov
16150aadb33SDmitri Tikhonov    lsquic_rechist_received(&rechist, 9, 0);
16250aadb33SDmitri Tikhonov
16350aadb33SDmitri Tikhonov    rechist2str(&rechist, buf, sizeof(buf));
16450aadb33SDmitri Tikhonov    assert(0 == strcmp(buf, "[12-12][10-6][4-3][1-1]"));
16550aadb33SDmitri Tikhonov
16650aadb33SDmitri Tikhonov    lsquic_rechist_received(&rechist, 5, 0);
16750aadb33SDmitri Tikhonov    lsquic_rechist_received(&rechist, 11, 0);
16850aadb33SDmitri Tikhonov
16950aadb33SDmitri Tikhonov    rechist2str(&rechist, buf, sizeof(buf));
17050aadb33SDmitri Tikhonov    assert(0 == strcmp(buf, "[12-3][1-1]"));
17150aadb33SDmitri Tikhonov
17250aadb33SDmitri Tikhonov    lsquic_rechist_cleanup(&rechist);
17350aadb33SDmitri Tikhonov}
17450aadb33SDmitri Tikhonov
17550aadb33SDmitri Tikhonov
17650aadb33SDmitri Tikhonovint
17750aadb33SDmitri Tikhonovmain (void)
17850aadb33SDmitri Tikhonov{
17950aadb33SDmitri Tikhonov    enum received_st st;
18050aadb33SDmitri Tikhonov    lsquic_rechist_t rechist;
18150aadb33SDmitri Tikhonov    unsigned i;
18250aadb33SDmitri Tikhonov    const struct lsquic_packno_range *range;
18350aadb33SDmitri Tikhonov
184c51ce338SDmitri Tikhonov    lsquic_global_init(LSQUIC_GLOBAL_SERVER);
185c51ce338SDmitri Tikhonov
18650aadb33SDmitri Tikhonov    lsquic_log_to_fstream(stderr, 0);
18750aadb33SDmitri Tikhonov    lsq_log_levels[LSQLM_PARSE]   = LSQ_LOG_DEBUG;
18850aadb33SDmitri Tikhonov    lsq_log_levels[LSQLM_RECHIST] = LSQ_LOG_DEBUG;
18950aadb33SDmitri Tikhonov
1905392f7a3SLiteSpeed Tech    lsquic_rechist_init(&rechist, &lconn, 0);
19150aadb33SDmitri Tikhonov
19250aadb33SDmitri Tikhonov    lsquic_time_t now = lsquic_time_now();
19350aadb33SDmitri Tikhonov    st = lsquic_rechist_received(&rechist, 0, now);
19450aadb33SDmitri Tikhonov    assert(("inserting packet number zero results in error", st == REC_ST_ERR));
19550aadb33SDmitri Tikhonov
19650aadb33SDmitri Tikhonov    st = lsquic_rechist_received(&rechist, 1, now);
19750aadb33SDmitri Tikhonov    assert(("inserting packet number one is successful", st == REC_ST_OK));
19850aadb33SDmitri Tikhonov
19950aadb33SDmitri Tikhonov    st = lsquic_rechist_received(&rechist, 1, now);
20050aadb33SDmitri Tikhonov    assert(("inserting packet number one again results in duplicate error",
20150aadb33SDmitri Tikhonov                                                            st == REC_ST_DUP));
20250aadb33SDmitri Tikhonov
20350aadb33SDmitri Tikhonov    range = lsquic_rechist_first(&rechist);
20450aadb33SDmitri Tikhonov    assert(("first range returned correctly", range));
20550aadb33SDmitri Tikhonov    assert(("first range low value checks out", range->low == 1));
20650aadb33SDmitri Tikhonov    assert(("first range high value checks out", range->high == 1));
20750aadb33SDmitri Tikhonov    range = lsquic_rechist_next(&rechist);
20850aadb33SDmitri Tikhonov    assert(("second range does not exist", !range));
20950aadb33SDmitri Tikhonov
21050aadb33SDmitri Tikhonov    for (i = 3; i <= 5; ++i)
21150aadb33SDmitri Tikhonov    {
21250aadb33SDmitri Tikhonov        st = lsquic_rechist_received(&rechist, i, now);
21350aadb33SDmitri Tikhonov        assert(("inserting packet", st == REC_ST_OK));
21450aadb33SDmitri Tikhonov    }
21550aadb33SDmitri Tikhonov
21650aadb33SDmitri Tikhonov    range = lsquic_rechist_first(&rechist);
21750aadb33SDmitri Tikhonov    assert(("first range returned correctly", range));
21850aadb33SDmitri Tikhonov    assert(("first range low value checks out", range->low == 3));
21950aadb33SDmitri Tikhonov    assert(("first range high value checks out", range->high == 5));
22050aadb33SDmitri Tikhonov    range = lsquic_rechist_next(&rechist);
22150aadb33SDmitri Tikhonov    assert(("second range returned correctly", range));
22250aadb33SDmitri Tikhonov    assert(("second range low value checks out", range->low == 1));
22350aadb33SDmitri Tikhonov    assert(("second range high value checks out", range->high == 1));
22450aadb33SDmitri Tikhonov    range = lsquic_rechist_next(&rechist);
22550aadb33SDmitri Tikhonov    assert(("third range does not exist", !range));
22650aadb33SDmitri Tikhonov
22750aadb33SDmitri Tikhonov    lsquic_rechist_stop_wait(&rechist, 3);
22850aadb33SDmitri Tikhonov
22950aadb33SDmitri Tikhonov    st = lsquic_rechist_received(&rechist, 1, now);
23050aadb33SDmitri Tikhonov    assert(("inserting packet number one is unsuccessful after cutoff 3",
23150aadb33SDmitri Tikhonov                                                            st == REC_ST_DUP));
23250aadb33SDmitri Tikhonov
23350aadb33SDmitri Tikhonov    range = lsquic_rechist_first(&rechist);
23450aadb33SDmitri Tikhonov    assert(("first range returned correctly", range));
23550aadb33SDmitri Tikhonov    assert(("first range low value checks out", range->low == 3));
23650aadb33SDmitri Tikhonov    assert(("first range high value checks out", range->high == 5));
23750aadb33SDmitri Tikhonov    range = lsquic_rechist_next(&rechist);
23850aadb33SDmitri Tikhonov    assert(("second range does not exist", !range));
23950aadb33SDmitri Tikhonov
24050aadb33SDmitri Tikhonov    for (i = 9; i >= 7; --i)
24150aadb33SDmitri Tikhonov    {
24250aadb33SDmitri Tikhonov        st = lsquic_rechist_received(&rechist, i, now);
24350aadb33SDmitri Tikhonov        assert(("inserting packet", st == REC_ST_OK));
24450aadb33SDmitri Tikhonov    }
24550aadb33SDmitri Tikhonov
24650aadb33SDmitri Tikhonov    range = lsquic_rechist_first(&rechist);
24750aadb33SDmitri Tikhonov    assert(("first range returned correctly", range));
24850aadb33SDmitri Tikhonov    assert(("first range low value checks out", range->low == 7));
24950aadb33SDmitri Tikhonov    assert(("first range high value checks out", range->high == 9));
25050aadb33SDmitri Tikhonov    range = lsquic_rechist_next(&rechist);
25150aadb33SDmitri Tikhonov    assert(("second range returned correctly", range));
25250aadb33SDmitri Tikhonov    assert(("second range low value checks out", range->low == 3));
25350aadb33SDmitri Tikhonov    assert(("second range high value checks out", range->high == 5));
25450aadb33SDmitri Tikhonov    range = lsquic_rechist_next(&rechist);
25550aadb33SDmitri Tikhonov    assert(("third range does not exist", !range));
25650aadb33SDmitri Tikhonov
25750aadb33SDmitri Tikhonov    lsquic_rechist_cleanup(&rechist);
25850aadb33SDmitri Tikhonov
25950aadb33SDmitri Tikhonov    test4();
26050aadb33SDmitri Tikhonov
26150aadb33SDmitri Tikhonov    test5();
26250aadb33SDmitri Tikhonov
26350aadb33SDmitri Tikhonov    return 0;
26450aadb33SDmitri Tikhonov}
265