1/* Copyright (c) 2017 - 2022 LiteSpeed Technologies Inc.  See LICENSE. */
2#include <assert.h>
3#include <stdint.h>
4#include <stdio.h>
5#include <stdlib.h>
6#include <string.h>
7#include <sys/queue.h>
8#ifndef WIN32
9#include <unistd.h>
10#endif
11
12#include "lsquic.h"
13#include "lsquic_int_types.h"
14#include "lsquic_types.h"
15#include "lsquic_hash.h"
16#include "lsquic_conn.h"
17#include "lsquic_conn_hash.h"
18#include "lsquic_mm.h"
19#include "lsquic_malo.h"
20#include "lsquic_logger.h"
21#include "lsquic.h"
22
23
24static struct lsquic_conn *
25get_new_lsquic_conn (struct malo *malo)
26{
27    struct lsquic_conn *lconn = lsquic_malo_get(malo);
28    memset(lconn, 0, sizeof(*lconn));
29    memcpy(lconn->cn_cid.idbuf, &lconn, sizeof(lconn));
30    lconn->cn_cid.len = sizeof(lconn);
31    return lconn;
32}
33
34
35int
36main (int argc, char **argv)
37{
38    struct malo *malo;
39    struct conn_hash conn_hash;
40    unsigned n, nelems;
41    struct lsquic_conn *lconn, *find_lsconn;
42    int s;
43
44    if (argc > 1)
45        nelems = atoi(argv[1]);
46    else
47        nelems = 1000000;
48
49    lsquic_log_to_fstream(stderr, LLTS_HHMMSSMS);
50    lsquic_set_log_level("info");
51
52    malo = lsquic_malo_create(sizeof(*lconn));
53    s = conn_hash_init(&conn_hash, 0);
54    assert(0 == s);
55
56    for (n = 0; n < nelems; ++n)
57    {
58        lconn = get_new_lsquic_conn(malo);
59        lconn->cn_if = (void *) (uintptr_t) n;              /* This will be used for verification later the test */
60        find_lsconn = conn_hash_find_by_cid(&conn_hash, &lconn->cn_cid);
61        assert(!find_lsconn);
62        s = conn_hash_add(&conn_hash, lconn);
63        assert(0 == s);
64        lconn->cn_flags = 1;    /* In hash */
65    }
66
67    assert(nelems == conn_hash_count(&conn_hash));
68
69    for (lconn = lsquic_malo_first(malo); lconn;
70             lconn = lsquic_malo_next(malo))
71    {
72        assert(lconn->cn_flags == 1);
73        find_lsconn = conn_hash_find_by_cid(&conn_hash, &lconn->cn_cid);
74        assert(find_lsconn == lconn);
75        conn_hash_remove(&conn_hash, lconn);
76        lconn->cn_flags = 0;
77        find_lsconn = conn_hash_find_by_cid(&conn_hash, &lconn->cn_cid);
78        assert(!find_lsconn);
79    }
80
81    assert(0 == conn_hash_count(&conn_hash));
82
83    conn_hash_cleanup(&conn_hash);
84    lsquic_malo_destroy(malo);
85
86    exit(0);
87}
88