test_conn_hash.c revision a74702c6
1a74702c6SGeorge Wang/* Copyright (c) 2017 - 2022 LiteSpeed Technologies Inc.  See LICENSE. */
250aadb33SDmitri Tikhonov#include <assert.h>
350aadb33SDmitri Tikhonov#include <stdint.h>
450aadb33SDmitri Tikhonov#include <stdio.h>
550aadb33SDmitri Tikhonov#include <stdlib.h>
650aadb33SDmitri Tikhonov#include <string.h>
750aadb33SDmitri Tikhonov#include <sys/queue.h>
8461e84d8SAmol Deshpande#ifndef WIN32
950aadb33SDmitri Tikhonov#include <unistd.h>
10461e84d8SAmol Deshpande#endif
1150aadb33SDmitri Tikhonov
1250aadb33SDmitri Tikhonov#include "lsquic.h"
1350aadb33SDmitri Tikhonov#include "lsquic_int_types.h"
1450aadb33SDmitri Tikhonov#include "lsquic_types.h"
155392f7a3SLiteSpeed Tech#include "lsquic_hash.h"
1650aadb33SDmitri Tikhonov#include "lsquic_conn.h"
1750aadb33SDmitri Tikhonov#include "lsquic_conn_hash.h"
1850aadb33SDmitri Tikhonov#include "lsquic_mm.h"
1950aadb33SDmitri Tikhonov#include "lsquic_malo.h"
2050aadb33SDmitri Tikhonov#include "lsquic_logger.h"
2150aadb33SDmitri Tikhonov#include "lsquic.h"
2250aadb33SDmitri Tikhonov
2350aadb33SDmitri Tikhonov
2450aadb33SDmitri Tikhonovstatic struct lsquic_conn *
2550aadb33SDmitri Tikhonovget_new_lsquic_conn (struct malo *malo)
2650aadb33SDmitri Tikhonov{
2750aadb33SDmitri Tikhonov    struct lsquic_conn *lconn = lsquic_malo_get(malo);
2850aadb33SDmitri Tikhonov    memset(lconn, 0, sizeof(*lconn));
295392f7a3SLiteSpeed Tech    memcpy(lconn->cn_cid.idbuf, &lconn, sizeof(lconn));
305392f7a3SLiteSpeed Tech    lconn->cn_cid.len = sizeof(lconn);
3150aadb33SDmitri Tikhonov    return lconn;
3250aadb33SDmitri Tikhonov}
3350aadb33SDmitri Tikhonov
3450aadb33SDmitri Tikhonov
3550aadb33SDmitri Tikhonovint
3650aadb33SDmitri Tikhonovmain (int argc, char **argv)
3750aadb33SDmitri Tikhonov{
3850aadb33SDmitri Tikhonov    struct malo *malo;
3950aadb33SDmitri Tikhonov    struct conn_hash conn_hash;
407fbe101bSDmitri Tikhonov    unsigned n, nelems;
4150aadb33SDmitri Tikhonov    struct lsquic_conn *lconn, *find_lsconn;
4250aadb33SDmitri Tikhonov    int s;
4350aadb33SDmitri Tikhonov
4450aadb33SDmitri Tikhonov    if (argc > 1)
4550aadb33SDmitri Tikhonov        nelems = atoi(argv[1]);
4650aadb33SDmitri Tikhonov    else
4750aadb33SDmitri Tikhonov        nelems = 1000000;
4850aadb33SDmitri Tikhonov
4950aadb33SDmitri Tikhonov    lsquic_log_to_fstream(stderr, LLTS_HHMMSSMS);
5050aadb33SDmitri Tikhonov    lsquic_set_log_level("info");
5150aadb33SDmitri Tikhonov
5250aadb33SDmitri Tikhonov    malo = lsquic_malo_create(sizeof(*lconn));
53cd7bc383SDmitri Tikhonov    s = conn_hash_init(&conn_hash, 0);
5450aadb33SDmitri Tikhonov    assert(0 == s);
5550aadb33SDmitri Tikhonov
5650aadb33SDmitri Tikhonov    for (n = 0; n < nelems; ++n)
5750aadb33SDmitri Tikhonov    {
5850aadb33SDmitri Tikhonov        lconn = get_new_lsquic_conn(malo);
5950aadb33SDmitri Tikhonov        lconn->cn_if = (void *) (uintptr_t) n;              /* This will be used for verification later the test */
605392f7a3SLiteSpeed Tech        find_lsconn = conn_hash_find_by_cid(&conn_hash, &lconn->cn_cid);
6150aadb33SDmitri Tikhonov        assert(!find_lsconn);
627fbe101bSDmitri Tikhonov        s = conn_hash_add(&conn_hash, lconn);
6350aadb33SDmitri Tikhonov        assert(0 == s);
642d296031SDmitri Tikhonov        lconn->cn_flags = 1;    /* In hash */
6550aadb33SDmitri Tikhonov    }
6650aadb33SDmitri Tikhonov
6750aadb33SDmitri Tikhonov    assert(nelems == conn_hash_count(&conn_hash));
6850aadb33SDmitri Tikhonov
6950aadb33SDmitri Tikhonov    for (lconn = lsquic_malo_first(malo); lconn;
7050aadb33SDmitri Tikhonov             lconn = lsquic_malo_next(malo))
7150aadb33SDmitri Tikhonov    {
722d296031SDmitri Tikhonov        assert(lconn->cn_flags == 1);
735392f7a3SLiteSpeed Tech        find_lsconn = conn_hash_find_by_cid(&conn_hash, &lconn->cn_cid);
7450aadb33SDmitri Tikhonov        assert(find_lsconn == lconn);
7550aadb33SDmitri Tikhonov        conn_hash_remove(&conn_hash, lconn);
762d296031SDmitri Tikhonov        lconn->cn_flags = 0;
775392f7a3SLiteSpeed Tech        find_lsconn = conn_hash_find_by_cid(&conn_hash, &lconn->cn_cid);
7850aadb33SDmitri Tikhonov        assert(!find_lsconn);
7950aadb33SDmitri Tikhonov    }
8050aadb33SDmitri Tikhonov
8150aadb33SDmitri Tikhonov    assert(0 == conn_hash_count(&conn_hash));
8250aadb33SDmitri Tikhonov
8350aadb33SDmitri Tikhonov    conn_hash_cleanup(&conn_hash);
8450aadb33SDmitri Tikhonov    lsquic_malo_destroy(malo);
8550aadb33SDmitri Tikhonov
8650aadb33SDmitri Tikhonov    exit(0);
8750aadb33SDmitri Tikhonov}
88