Skip to main content
  • Home
  • login
  • Browse the archive

    swh mirror partner logo
swh logo
SoftwareHeritage
Software
Heritage
Mirror
Features
  • Search

  • Downloads

  • Save code now

  • Add forge now

  • Help

swh:1:snp:dc2a5002442a00b1c0eda7c65d04ea7455e166cd
  • Code
  • Branches (204)
  • Releases (207)
    • Branches
    • Releases
    • HEAD
    • refs/heads/OpenSSL-engine-0_9_6-stable
    • refs/heads/OpenSSL-fips-0_9_7-stable
    • refs/heads/OpenSSL-fips-0_9_8-stable
    • refs/heads/OpenSSL-fips-1_2-stable
    • refs/heads/OpenSSL-fips-2_0-dev
    • refs/heads/OpenSSL-fips-2_0-stable
    • refs/heads/OpenSSL-fips2-0_9_7-stable
    • refs/heads/OpenSSL_0_9_6-stable
    • refs/heads/OpenSSL_0_9_7-stable
    • refs/heads/OpenSSL_0_9_8-stable
    • refs/heads/OpenSSL_0_9_8fg-stable
    • refs/heads/OpenSSL_1_0_0-stable
    • refs/heads/OpenSSL_1_0_1-stable
    • refs/heads/OpenSSL_1_0_2-stable
    • refs/heads/OpenSSL_1_1_0-stable
    • refs/heads/OpenSSL_1_1_1-stable
    • refs/heads/SSLeay
    • refs/heads/feature/dtls-1.3
    • refs/heads/feature/ech
    • refs/heads/feature/quic-server
    • refs/heads/master
    • refs/heads/openssl-3.0
    • refs/heads/openssl-3.1
    • refs/heads/openssl-3.2
    • refs/heads/openssl-3.3
    • refs/heads/openssl-3.4
    • refs/heads/tls1.3-draft-18
    • refs/heads/tls1.3-draft-19
    • refs/tags/AFTER_COMPAQ_PATCH
    • refs/tags/BEFORE_COMPAQ_PATCH
    • refs/tags/BEFORE_engine
    • refs/tags/BEN_FIPS_TEST_1
    • refs/tags/BEN_FIPS_TEST_2
    • refs/tags/BEN_FIPS_TEST_3
    • refs/tags/BEN_FIPS_TEST_4
    • refs/tags/BEN_FIPS_TEST_5
    • refs/tags/BEN_FIPS_TEST_6
    • refs/tags/BEN_FIPS_TEST_7
    • refs/tags/BEN_FIPS_TEST_8
    • refs/tags/FIPS_098_TEST_1
    • refs/tags/FIPS_098_TEST_2
    • refs/tags/FIPS_098_TEST_3
    • refs/tags/FIPS_098_TEST_4
    • refs/tags/FIPS_098_TEST_5
    • refs/tags/FIPS_098_TEST_6
    • refs/tags/FIPS_098_TEST_7
    • refs/tags/FIPS_098_TEST_8
    • refs/tags/FIPS_TEST_10
    • refs/tags/FIPS_TEST_9
    • refs/tags/LEVITTE_after_const
    • refs/tags/LEVITTE_before_const
    • refs/tags/OpenSSL-engine-0_9_6
    • refs/tags/OpenSSL-engine-0_9_6-beta1
    • refs/tags/OpenSSL-engine-0_9_6-beta2
    • refs/tags/OpenSSL-engine-0_9_6-beta3
    • refs/tags/OpenSSL-engine-0_9_6a
    • refs/tags/OpenSSL-engine-0_9_6a-beta1
    • refs/tags/OpenSSL-engine-0_9_6a-beta2
    • refs/tags/OpenSSL-engine-0_9_6a-beta3
    • refs/tags/OpenSSL-engine-0_9_6b
    • refs/tags/OpenSSL-engine-0_9_6c
    • refs/tags/OpenSSL-engine-0_9_6d
    • refs/tags/OpenSSL-engine-0_9_6d-beta1
    • refs/tags/OpenSSL-engine-0_9_6e
    • refs/tags/OpenSSL-engine-0_9_6f
    • refs/tags/OpenSSL-engine-0_9_6g
    • refs/tags/OpenSSL-engine-0_9_6h
    • refs/tags/OpenSSL-engine-0_9_6i
    • refs/tags/OpenSSL-engine-0_9_6j
    • refs/tags/OpenSSL-engine-0_9_6k
    • refs/tags/OpenSSL-engine-0_9_6l
    • refs/tags/OpenSSL-engine-0_9_6m
    • refs/tags/OpenSSL-fips-1_2_0
    • refs/tags/OpenSSL-fips-1_2_1
    • refs/tags/OpenSSL-fips-1_2_2
    • refs/tags/OpenSSL-fips-1_2_3
    • refs/tags/OpenSSL-fips-2_0
    • refs/tags/OpenSSL-fips-2_0-pl1
    • refs/tags/OpenSSL-fips-2_0-rc1
    • refs/tags/OpenSSL-fips-2_0-rc2
    • refs/tags/OpenSSL-fips-2_0-rc3
    • refs/tags/OpenSSL-fips-2_0-rc4
    • refs/tags/OpenSSL-fips-2_0-rc5
    • refs/tags/OpenSSL-fips-2_0-rc6
    • refs/tags/OpenSSL-fips-2_0-rc7
    • refs/tags/OpenSSL-fips-2_0-rc8
    • refs/tags/OpenSSL-fips-2_0-rc9
    • refs/tags/OpenSSL-fips-2_0_1
    • refs/tags/OpenSSL_0_9_1c
    • refs/tags/OpenSSL_0_9_2b
    • refs/tags/OpenSSL_0_9_3
    • refs/tags/OpenSSL_0_9_3a
    • refs/tags/OpenSSL_0_9_3beta1
    • refs/tags/OpenSSL_0_9_3beta2
    • refs/tags/OpenSSL_0_9_4
    • refs/tags/OpenSSL_0_9_5
    • refs/tags/OpenSSL_0_9_5a
    • refs/tags/OpenSSL_0_9_5a-beta1
    • refs/tags/OpenSSL_0_9_5a-beta2
    • refs/tags/OpenSSL_0_9_5beta1
    • refs/tags/OpenSSL_0_9_5beta2
    • refs/tags/OpenSSL_0_9_6
    • refs/tags/OpenSSL_0_9_6-beta1
    • refs/tags/OpenSSL_0_9_6-beta2
    • refs/tags/OpenSSL_0_9_6-beta3
    • refs/tags/OpenSSL_0_9_6a
    • refs/tags/OpenSSL_0_9_6a-beta1
    • refs/tags/OpenSSL_0_9_6a-beta2
    • refs/tags/OpenSSL_0_9_6a-beta3
    • refs/tags/OpenSSL_0_9_6b
    • refs/tags/OpenSSL_0_9_6c
    • refs/tags/OpenSSL_0_9_6d
    • refs/tags/OpenSSL_0_9_6d-beta1
    • refs/tags/OpenSSL_0_9_6e
    • refs/tags/OpenSSL_0_9_6f
    • refs/tags/OpenSSL_0_9_6g
    • refs/tags/OpenSSL_0_9_6h
    • refs/tags/OpenSSL_0_9_6i
    • refs/tags/OpenSSL_0_9_6j
    • refs/tags/OpenSSL_0_9_6k
    • refs/tags/OpenSSL_0_9_6l
    • refs/tags/OpenSSL_0_9_6m
    • refs/tags/OpenSSL_0_9_7
    • refs/tags/OpenSSL_0_9_7-beta1
    • refs/tags/OpenSSL_0_9_7-beta2
    • refs/tags/OpenSSL_0_9_7-beta3
    • refs/tags/OpenSSL_0_9_7-beta4
    • refs/tags/OpenSSL_0_9_7-beta5
    • refs/tags/OpenSSL_0_9_7-beta6
    • refs/tags/OpenSSL_0_9_7a
    • refs/tags/OpenSSL_0_9_7b
    • refs/tags/OpenSSL_0_9_7c
    • refs/tags/OpenSSL_0_9_7d
    • refs/tags/OpenSSL_0_9_7e
    • refs/tags/OpenSSL_0_9_7f
    • refs/tags/OpenSSL_0_9_7g
    • refs/tags/OpenSSL_0_9_7h
    • refs/tags/OpenSSL_0_9_7i
    • refs/tags/OpenSSL_0_9_7j
    • refs/tags/OpenSSL_0_9_7k
    • refs/tags/OpenSSL_0_9_7l
    • refs/tags/OpenSSL_0_9_7m
    • refs/tags/OpenSSL_0_9_8
    • refs/tags/OpenSSL_0_9_8-beta1
    • refs/tags/OpenSSL_0_9_8-beta2
    • refs/tags/OpenSSL_0_9_8-beta3
    • refs/tags/OpenSSL_0_9_8-beta4
    • refs/tags/OpenSSL_0_9_8-beta5
    • refs/tags/OpenSSL_0_9_8-beta6
    • refs/tags/OpenSSL_0_9_8a
    • refs/tags/OpenSSL_0_9_8b
    • refs/tags/OpenSSL_0_9_8c
    • refs/tags/OpenSSL_0_9_8d
    • refs/tags/OpenSSL_0_9_8e
    • refs/tags/OpenSSL_0_9_8f
    • refs/tags/OpenSSL_0_9_8g
    • refs/tags/OpenSSL_0_9_8h
    • refs/tags/OpenSSL_0_9_8i
    • refs/tags/OpenSSL_0_9_8j
    • refs/tags/OpenSSL_0_9_8k
    • refs/tags/OpenSSL_0_9_8l
    • refs/tags/OpenSSL_0_9_8m
    • refs/tags/OpenSSL_0_9_8m-beta1
    • refs/tags/OpenSSL_0_9_8n
    • refs/tags/OpenSSL_0_9_8o
    • refs/tags/OpenSSL_0_9_8p
    • refs/tags/OpenSSL_0_9_8q
    • refs/tags/OpenSSL_0_9_8r
    • refs/tags/OpenSSL_0_9_8s
    • refs/tags/OpenSSL_0_9_8t
    • refs/tags/OpenSSL_0_9_8u
    • refs/tags/OpenSSL_0_9_8v
    • refs/tags/OpenSSL_0_9_8w
    • refs/tags/OpenSSL_0_9_8x
    • refs/tags/OpenSSL_1_0_0
    • refs/tags/OpenSSL_1_0_0-beta1
    • refs/tags/OpenSSL_1_0_0-beta2
    • refs/tags/OpenSSL_1_0_0-beta3
    • refs/tags/OpenSSL_1_0_0-beta4
    • refs/tags/OpenSSL_1_0_0-beta5
    • refs/tags/OpenSSL_1_0_0a
    • refs/tags/OpenSSL_1_0_0b
    • refs/tags/OpenSSL_1_0_0c
    • refs/tags/OpenSSL_1_0_0d
    • refs/tags/OpenSSL_1_0_0e
    • refs/tags/OpenSSL_1_0_0f
    • refs/tags/OpenSSL_1_0_0g
    • refs/tags/OpenSSL_1_0_0h
    • refs/tags/OpenSSL_1_0_0i
    • refs/tags/OpenSSL_1_0_0j
    • refs/tags/OpenSSL_1_0_1
    • refs/tags/OpenSSL_1_0_1-beta1
    • refs/tags/OpenSSL_1_0_1-beta2
    • refs/tags/OpenSSL_1_0_1-beta3
    • refs/tags/OpenSSL_1_0_1a
    • refs/tags/OpenSSL_1_0_1b
    • refs/tags/OpenSSL_1_0_1c
    • refs/tags/OpenSSL_FIPS_1_0
    • refs/tags/SSLeay_0_8_1b
    • refs/tags/SSLeay_0_9_0b
    • refs/tags/SSLeay_0_9_1b
    • refs/tags/STATE_after_zlib
    • refs/tags/STATE_before_zlib
    • refs/tags/rsaref
    • openssl-3.4.0-alpha1
    • openssl-3.3.2
    • openssl-3.3.1
    • openssl-3.3.0-beta1
    • openssl-3.3.0-alpha1
    • openssl-3.3.0
    • openssl-3.2.3
    • openssl-3.2.2
    • openssl-3.2.1
    • openssl-3.2.0-beta1
    • openssl-3.2.0-alpha2
    • openssl-3.2.0-alpha1
    • openssl-3.2.0
    • openssl-3.1.7
    • openssl-3.1.6
    • openssl-3.1.5
    • openssl-3.1.4
    • openssl-3.1.3
    • openssl-3.1.2
    • openssl-3.1.1
    • openssl-3.1.0-beta1
    • openssl-3.1.0-alpha1
    • openssl-3.1.0
    • openssl-3.0.9
    • openssl-3.0.8
    • openssl-3.0.7
    • openssl-3.0.6
    • openssl-3.0.5
    • openssl-3.0.4
    • openssl-3.0.3
    • openssl-3.0.2
    • openssl-3.0.15
    • openssl-3.0.14
    • openssl-3.0.13
    • openssl-3.0.12
    • openssl-3.0.11
    • openssl-3.0.10
    • openssl-3.0.1
    • openssl-3.0.0-beta2
    • openssl-3.0.0-beta1
    • openssl-3.0.0-alpha9
    • openssl-3.0.0-alpha8
    • openssl-3.0.0-alpha7
    • openssl-3.0.0-alpha6
    • openssl-3.0.0-alpha5
    • openssl-3.0.0-alpha4
    • openssl-3.0.0-alpha3
    • openssl-3.0.0-alpha2
    • openssl-3.0.0-alpha17
    • openssl-3.0.0-alpha16
    • openssl-3.0.0-alpha15
    • openssl-3.0.0-alpha14
    • openssl-3.0.0-alpha13
    • openssl-3.0.0-alpha12
    • openssl-3.0.0-alpha11
    • openssl-3.0.0-alpha10
    • openssl-3.0.0-alpha1
    • openssl-3.0.0
    • master-pre-reformat
    • master-pre-auto-reformat
    • master-post-reformat
    • master-post-auto-reformat
    • OpenSSL_1_1_1w
    • OpenSSL_1_1_1v
    • OpenSSL_1_1_1u
    • OpenSSL_1_1_1t
    • OpenSSL_1_1_1s
    • OpenSSL_1_1_1r
    • OpenSSL_1_1_1q
    • OpenSSL_1_1_1p
    • OpenSSL_1_1_1o
    • OpenSSL_1_1_1n
    • OpenSSL_1_1_1m
    • OpenSSL_1_1_1l
    • OpenSSL_1_1_1k
    • OpenSSL_1_1_1j
    • OpenSSL_1_1_1i
    • OpenSSL_1_1_1h
    • OpenSSL_1_1_1g
    • OpenSSL_1_1_1f
    • OpenSSL_1_1_1e
    • OpenSSL_1_1_1d
    • OpenSSL_1_1_1c
    • OpenSSL_1_1_1b
    • OpenSSL_1_1_1a
    • OpenSSL_1_1_1-pre9
    • OpenSSL_1_1_1-pre8
    • OpenSSL_1_1_1-pre7
    • OpenSSL_1_1_1-pre6
    • OpenSSL_1_1_1-pre5
    • OpenSSL_1_1_1-pre4
    • OpenSSL_1_1_1-pre3
    • OpenSSL_1_1_1-pre2
    • OpenSSL_1_1_1-pre1
    • OpenSSL_1_1_1
    • OpenSSL_1_1_0l
    • OpenSSL_1_1_0k
    • OpenSSL_1_1_0j
    • OpenSSL_1_1_0i
    • OpenSSL_1_1_0h
    • OpenSSL_1_1_0g
    • OpenSSL_1_1_0f
    • OpenSSL_1_1_0e
    • OpenSSL_1_1_0d
    • OpenSSL_1_1_0c
    • OpenSSL_1_1_0b
    • OpenSSL_1_1_0a
    • OpenSSL_1_1_0-pre6
    • OpenSSL_1_1_0-pre5
    • OpenSSL_1_1_0-pre4
    • OpenSSL_1_1_0-pre3
    • OpenSSL_1_1_0-pre2
    • OpenSSL_1_1_0-pre1
    • OpenSSL_1_1_0
    • OpenSSL_1_0_2u
    • OpenSSL_1_0_2t
    • OpenSSL_1_0_2s
    • OpenSSL_1_0_2r
    • OpenSSL_1_0_2q
    • OpenSSL_1_0_2p
    • OpenSSL_1_0_2o
    • OpenSSL_1_0_2n
    • OpenSSL_1_0_2m
    • OpenSSL_1_0_2l
    • OpenSSL_1_0_2k
    • OpenSSL_1_0_2j
    • OpenSSL_1_0_2i
    • OpenSSL_1_0_2h
    • OpenSSL_1_0_2g
    • OpenSSL_1_0_2f
    • OpenSSL_1_0_2e
    • OpenSSL_1_0_2d
    • OpenSSL_1_0_2c
    • OpenSSL_1_0_2b
    • OpenSSL_1_0_2a
    • OpenSSL_1_0_2-pre-reformat
    • OpenSSL_1_0_2-pre-auto-reformat
    • OpenSSL_1_0_2-post-reformat
    • OpenSSL_1_0_2-post-auto-reformat
    • OpenSSL_1_0_2-beta3
    • OpenSSL_1_0_2-beta2
    • OpenSSL_1_0_2-beta1
    • OpenSSL_1_0_2
    • OpenSSL_1_0_1u
    • OpenSSL_1_0_1t
    • OpenSSL_1_0_1s
    • OpenSSL_1_0_1r
    • OpenSSL_1_0_1q
    • OpenSSL_1_0_1p
    • OpenSSL_1_0_1o
    • OpenSSL_1_0_1n
    • OpenSSL_1_0_1m
    • OpenSSL_1_0_1l
    • OpenSSL_1_0_1k
    • OpenSSL_1_0_1j
    • OpenSSL_1_0_1i
    • OpenSSL_1_0_1h
    • OpenSSL_1_0_1g
    • OpenSSL_1_0_1f
    • OpenSSL_1_0_1e
    • OpenSSL_1_0_1d
    • OpenSSL_1_0_1-pre-reformat
    • OpenSSL_1_0_1-pre-auto-reformat
    • OpenSSL_1_0_1-post-reformat
    • OpenSSL_1_0_1-post-auto-reformat
    • OpenSSL_1_0_0t
    • OpenSSL_1_0_0s
    • OpenSSL_1_0_0r
    • OpenSSL_1_0_0q
    • OpenSSL_1_0_0p
    • OpenSSL_1_0_0o
    • OpenSSL_1_0_0n
    • OpenSSL_1_0_0m
    • OpenSSL_1_0_0l
    • OpenSSL_1_0_0k
    • OpenSSL_1_0_0-pre-reformat
    • OpenSSL_1_0_0-pre-auto-reformat
    • OpenSSL_1_0_0-post-reformat
    • OpenSSL_1_0_0-post-auto-reformat
    • OpenSSL_0_9_8zh
    • OpenSSL_0_9_8zg
    • OpenSSL_0_9_8zf
    • OpenSSL_0_9_8ze
    • OpenSSL_0_9_8zd
    • OpenSSL_0_9_8zc
    • OpenSSL_0_9_8zb
    • OpenSSL_0_9_8za
    • OpenSSL_0_9_8y
    • OpenSSL_0_9_8-pre-reformat
    • OpenSSL_0_9_8-pre-auto-reformat
    • OpenSSL_0_9_8-post-reformat
    • OpenSSL_0_9_8-post-auto-reformat
    • OpenSSL-fips-2_0_9
    • OpenSSL-fips-2_0_8
    • OpenSSL-fips-2_0_7
    • OpenSSL-fips-2_0_6
    • OpenSSL-fips-2_0_5
    • OpenSSL-fips-2_0_4
    • OpenSSL-fips-2_0_3
    • OpenSSL-fips-2_0_2
    • OpenSSL-fips-2_0_16
    • OpenSSL-fips-2_0_15
    • OpenSSL-fips-2_0_14
    • OpenSSL-fips-2_0_13
    • OpenSSL-fips-2_0_12
    • OpenSSL-fips-2_0_11
    • OpenSSL-fips-2_0_10
  • 5bbf7ca
  • /
  • test
  • /
  • lhash_test.c
Raw File
Permalinks

To reference or cite the objects present in the Software Heritage archive, permalinks based on SoftWare Hash IDentifiers (SWHIDs) must be used.
Select below a type of object currently browsed in order to display its associated SWHID and permalink.

  • content
  • directory
  • revision
  • snapshot
content badge Iframe embedding
swh:1:cnt:76bed5d7e529af414bcaff25597b14391a45d65a
directory badge Iframe embedding
swh:1:dir:150621d75a97c81406424eee833ff5907a83f2ad
revision badge
swh:1:rev:d5abf112e16c18ae0290ffbfb18d9dbf3a2dad0e
snapshot badge
swh:1:snp:dc2a5002442a00b1c0eda7c65d04ea7455e166cd
Tip revision: d5abf112e16c18ae0290ffbfb18d9dbf3a2dad0e authored by Frederik Wedel-Heinen on 03 January 2024, 13:31:15 UTC
Support dtls 1.3 downgrade mechanism
Tip revision: d5abf11
lhash_test.c
/*
 * Copyright 2017-2020 The OpenSSL Project Authors. All Rights Reserved.
 * Copyright (c) 2017, Oracle and/or its affiliates.  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 <stdio.h>
#include <string.h>

#include <openssl/opensslconf.h>
#include <openssl/lhash.h>
#include <openssl/err.h>
#include <openssl/rand.h>
#include <openssl/crypto.h>
#include <internal/hashtable.h>

#include "internal/nelem.h"
#include "threadstest.h"
#include "testutil.h"

/*
 * The macros below generate unused functions which error out one of the clang
 * builds.  We disable this check here.
 */
#ifdef __clang__
#pragma clang diagnostic ignored "-Wunused-function"
#endif

DEFINE_LHASH_OF_EX(int);

static int int_tests[] = { 65537, 13, 1, 3, -5, 6, 7, 4, -10, -12, -14, 22, 9,
                           -17, 16, 17, -23, 35, 37, 173, 11 };
static const size_t n_int_tests = OSSL_NELEM(int_tests);
static short int_found[OSSL_NELEM(int_tests)];
static short int_not_found;

static unsigned long int int_hash(const int *p)
{
    return 3 & *p;      /* To force collisions */
}

static int int_cmp(const int *p, const int *q)
{
    return *p != *q;
}

static int int_find(int n)
{
    unsigned int i;

    for (i = 0; i < n_int_tests; i++)
        if (int_tests[i] == n)
            return i;
    return -1;
}

static void int_doall(int *v)
{
    const int n = int_find(*v);

    if (n < 0)
        int_not_found++;
    else
        int_found[n]++;
}

static void int_doall_arg(int *p, short *f)
{
    const int n = int_find(*p);

    if (n < 0)
        int_not_found++;
    else
        f[n]++;
}

IMPLEMENT_LHASH_DOALL_ARG(int, short);

static int test_int_lhash(void)
{
    static struct {
        int data;
        int null;
    } dels[] = {
        { 65537,    0 },
        { 173,      0 },
        { 999,      1 },
        { 37,       0 },
        { 1,        0 },
        { 34,       1 }
    };
    const unsigned int n_dels = OSSL_NELEM(dels);
    LHASH_OF(int) *h = lh_int_new(&int_hash, &int_cmp);
    unsigned int i;
    int testresult = 0, j, *p;

    if (!TEST_ptr(h))
        goto end;

    /* insert */
    for (i = 0; i < n_int_tests; i++)
        if (!TEST_ptr_null(lh_int_insert(h, int_tests + i))) {
            TEST_info("int insert %d", i);
            goto end;
        }

    /* num_items */
    if (!TEST_int_eq((size_t)lh_int_num_items(h), n_int_tests))
        goto end;

    /* retrieve */
    for (i = 0; i < n_int_tests; i++)
        if (!TEST_int_eq(*lh_int_retrieve(h, int_tests + i), int_tests[i])) {
            TEST_info("lhash int retrieve value %d", i);
            goto end;
        }
    for (i = 0; i < n_int_tests; i++)
        if (!TEST_ptr_eq(lh_int_retrieve(h, int_tests + i), int_tests + i)) {
            TEST_info("lhash int retrieve address %d", i);
            goto end;
        }
    j = 1;
    if (!TEST_ptr_eq(lh_int_retrieve(h, &j), int_tests + 2))
        goto end;

    /* replace */
    j = 13;
    if (!TEST_ptr(p = lh_int_insert(h, &j)))
        goto end;
    if (!TEST_ptr_eq(p, int_tests + 1))
        goto end;
    if (!TEST_ptr_eq(lh_int_retrieve(h, int_tests + 1), &j))
        goto end;

    /* do_all */
    memset(int_found, 0, sizeof(int_found));
    int_not_found = 0;
    lh_int_doall(h, &int_doall);
    if (!TEST_int_eq(int_not_found, 0)) {
        TEST_info("lhash int doall encountered a not found condition");
        goto end;
    }
    for (i = 0; i < n_int_tests; i++)
        if (!TEST_int_eq(int_found[i], 1)) {
            TEST_info("lhash int doall %d", i);
            goto end;
        }

    /* do_all_arg */
    memset(int_found, 0, sizeof(int_found));
    int_not_found = 0;
    lh_int_doall_short(h, int_doall_arg, int_found);
    if (!TEST_int_eq(int_not_found, 0)) {
        TEST_info("lhash int doall arg encountered a not found condition");
        goto end;
    }
    for (i = 0; i < n_int_tests; i++)
        if (!TEST_int_eq(int_found[i], 1)) {
            TEST_info("lhash int doall arg %d", i);
            goto end;
        }

    /* delete */
    for (i = 0; i < n_dels; i++) {
        const int b = lh_int_delete(h, &dels[i].data) == NULL;
        if (!TEST_int_eq(b ^ dels[i].null,  0)) {
            TEST_info("lhash int delete %d", i);
            goto end;
        }
    }

    /* error */
    if (!TEST_int_eq(lh_int_error(h), 0))
        goto end;

    testresult = 1;
end:
    lh_int_free(h);
    return testresult;
}


static int int_filter_all(HT_VALUE *v, void *arg)
{
    return 1;
}

HT_START_KEY_DEFN(intkey)
HT_DEF_KEY_FIELD(mykey, int)
HT_END_KEY_DEFN(INTKEY)

IMPLEMENT_HT_VALUE_TYPE_FNS(int, test, static)

static int int_foreach(HT_VALUE *v, void *arg)
{
    int *vd = ossl_ht_test_int_from_value(v);
    const int n = int_find(*vd);

    if (n < 0)
        int_not_found++;
    else
        int_found[n]++;
    return 1;
}

static uint64_t hashtable_hash(uint8_t *key, size_t keylen)
{
    return (uint64_t)(*(uint32_t *)key);
}

static int test_int_hashtable(void)
{
    static struct {
        int data;
        int should_del;
    } dels[] = {
        { 65537 , 1},
        { 173 , 1},
        { 999 , 0 },
        { 37 , 1 },
        { 1 , 1 },
        { 34 , 0 }
    };
    const size_t n_dels = OSSL_NELEM(dels);
    HT_CONFIG hash_conf = {
        NULL,
        NULL,
        NULL,
        0,
    };
    INTKEY key;
    int rc = 0;
    size_t i;
    HT *ht = NULL;
    int todel;
    HT_VALUE_LIST *list = NULL;

    ht = ossl_ht_new(&hash_conf);

    if (ht == NULL)
        return 0;

    /* insert */
    HT_INIT_KEY(&key);
    for (i = 0; i < n_int_tests; i++) {
        HT_SET_KEY_FIELD(&key, mykey, int_tests[i]);
        if (!TEST_int_eq(ossl_ht_test_int_insert(ht, TO_HT_KEY(&key),
                         &int_tests[i], NULL), 1)) {
            TEST_info("int insert %zu", i);
            goto end;
        }
    }

    /* num_items */
    if (!TEST_int_eq((size_t)ossl_ht_count(ht), n_int_tests))
        goto end;

    /* foreach, no arg */
    memset(int_found, 0, sizeof(int_found));
    int_not_found = 0;
    ossl_ht_foreach_until(ht, int_foreach, NULL);
    if (!TEST_int_eq(int_not_found, 0)) {
        TEST_info("hashtable int foreach encountered a not found condition");
        goto end;
    }

    for (i = 0; i < n_int_tests; i++)
        if (!TEST_int_eq(int_found[i], 1)) {
            TEST_info("hashtable int foreach %zu", i);
            goto end;
    }

    /* filter */
    list = ossl_ht_filter(ht, 64, int_filter_all, NULL);
    if (!TEST_int_eq((size_t)list->list_len, n_int_tests))
        goto end;
    ossl_ht_value_list_free(list);

    /* delete */
    for (i = 0; i < n_dels; i++) {
        HT_SET_KEY_FIELD(&key, mykey, dels[i].data);
        todel = ossl_ht_delete(ht, TO_HT_KEY(&key));
        if (dels[i].should_del) {
            if (!TEST_int_eq(todel, 1)) {
                TEST_info("hashtable couldn't find entry %d to delete\n",
                          dels[i].data);
                goto end;
            }
        } else {
            if (!TEST_int_eq(todel, 0)) {
                TEST_info("%d found an entry that shouldn't be there\n", dels[i].data);
                goto end;
            }
       }
    }

    rc = 1;
end:
    ossl_ht_free(ht);
    return rc;
}

static unsigned long int stress_hash(const int *p)
{
    return *p;
}

#ifdef MEASURE_HASH_PERFORMANCE
static int
timeval_subtract (struct timeval *result, struct timeval *x, struct timeval *y)
{
    /* Perform the carry for the later subtraction by updating y. */
    if (x->tv_usec < y->tv_usec) {
        int nsec = (y->tv_usec - x->tv_usec) / 1000000 + 1;
        y->tv_usec -= 1000000 * nsec;
        y->tv_sec += nsec;
    }
    if (x->tv_usec - y->tv_usec > 1000000) {
        int nsec = (x->tv_usec - y->tv_usec) / 1000000;
        y->tv_usec += 1000000 * nsec;
        y->tv_sec -= nsec;
    }

    /*
     * Compute the time remaining to wait.
     * tv_usec is certainly positive.
     */
    result->tv_sec = x->tv_sec - y->tv_sec;
    result->tv_usec = x->tv_usec - y->tv_usec;

    /* Return 1 if result is negative. */
    return x->tv_sec < y->tv_sec;
}
#endif

static int test_stress(void)
{
    LHASH_OF(int) *h = lh_int_new(&stress_hash, &int_cmp);
    const unsigned int n = 2500000;
    unsigned int i;
    int testresult = 0, *p;
#ifdef MEASURE_HASH_PERFORMANCE
    struct timeval start, end, delta;
#endif

    if (!TEST_ptr(h))
        goto end;
#ifdef MEASURE_HASH_PERFORMANCE
    gettimeofday(&start, NULL);
#endif
    /* insert */
    for (i = 0; i < n; i++) {
        p = OPENSSL_malloc(sizeof(i));
        if (!TEST_ptr(p)) {
            TEST_info("lhash stress out of memory %d", i);
            goto end;
        }
        *p = 3 * i + 1;
        lh_int_insert(h, p);
    }

    /* num_items */
    if (!TEST_int_eq(lh_int_num_items(h), n))
            goto end;

    /* delete in a different order */
    for (i = 0; i < n; i++) {
        const int j = (7 * i + 4) % n * 3 + 1;

        if (!TEST_ptr(p = lh_int_delete(h, &j))) {
            TEST_info("lhash stress delete %d\n", i);
            goto end;
        }
        if (!TEST_int_eq(*p, j)) {
            TEST_info("lhash stress bad value %d", i);
            goto end;
        }
        OPENSSL_free(p);
    }

    testresult = 1;
end:
#ifdef MEASURE_HASH_PERFORMANCE
    gettimeofday(&end, NULL);
    timeval_subtract(&delta, &end, &start);
    TEST_info("lhash stress runs in %ld.%ld seconds", delta.tv_sec, delta.tv_usec);
#endif
    lh_int_free(h);
    return testresult;
}

static void hashtable_intfree(HT_VALUE *v)
{
    OPENSSL_free(v->value);
}

static int test_hashtable_stress(void)
{
    const unsigned int n = 2500000;
    unsigned int i;
    int testresult = 0, *p;
    HT_CONFIG hash_conf = {
        NULL,              /* use default context */
        hashtable_intfree, /* our free function */
        hashtable_hash,    /* our hash function */
        625000,            /* preset hash size */
    };
    HT *h;
    INTKEY key;
#ifdef MEASURE_HASH_PERFORMANCE
    struct timeval start, end, delta;
#endif

    h = ossl_ht_new(&hash_conf);


    if (!TEST_ptr(h))
        goto end;
#ifdef MEASURE_HASH_PERFORMANCE
    gettimeofday(&start, NULL);
#endif

    HT_INIT_KEY(&key);

    /* insert */
    for (i = 0; i < n; i++) {
        p = OPENSSL_malloc(sizeof(i));
        if (!TEST_ptr(p)) {
            TEST_info("hashtable stress out of memory %d", i);
            goto end;
        }
        *p = 3 * i + 1;
        HT_SET_KEY_FIELD(&key, mykey, *p);
        if (!TEST_int_eq(ossl_ht_test_int_insert(h, TO_HT_KEY(&key),
                         p, NULL), 1)) {
            TEST_info("hashtable unable to insert element %d\n", *p);
            goto end;
        }
    }

    /* make sure we stored everything */
    if (!TEST_int_eq((size_t)ossl_ht_count(h), n))
            goto end;

    /* delete in a different order */
    for (i = 0; i < n; i++) {
        const int j = (7 * i + 4) % n * 3 + 1;
        HT_SET_KEY_FIELD(&key, mykey, j);
        if (!TEST_int_eq((ossl_ht_delete(h, TO_HT_KEY(&key))), 1)) {
            TEST_info("hashtable didn't delete key %d\n", j);
            goto end;
        }
    }

    testresult = 1;
end:
#ifdef MEASURE_HASH_PERFORMANCE
    gettimeofday(&end, NULL);
    timeval_subtract(&delta, &end, &start);
    TEST_info("hashtable stress runs in %ld.%ld seconds", delta.tv_sec, delta.tv_usec);
#endif
    ossl_ht_free(h);
    return testresult;
}

typedef struct test_mt_entry {
    int in_table;
    int pending_delete;
} TEST_MT_ENTRY;

static HT *m_ht = NULL;
#define TEST_MT_POOL_SZ 256
#define TEST_THREAD_ITERATIONS 10000

static struct test_mt_entry test_mt_entries[TEST_MT_POOL_SZ];
static char *worker_exits[4];

HT_START_KEY_DEFN(mtkey)
HT_DEF_KEY_FIELD(index, unsigned int)
HT_END_KEY_DEFN(MTKEY)

IMPLEMENT_HT_VALUE_TYPE_FNS(TEST_MT_ENTRY, mt, static)

static int worker_num = 0;
static CRYPTO_RWLOCK *worker_lock;
static int free_failure = 0;
static int shutting_down = 0;
static int global_iteration = 0;

static void hashtable_mt_free(HT_VALUE *v)
{
    TEST_MT_ENTRY *m = ossl_ht_mt_TEST_MT_ENTRY_from_value(v);
    int pending_delete;
    int ret;

    CRYPTO_atomic_load_int(&m->pending_delete, &pending_delete, worker_lock);

    if (shutting_down == 1)
        return;

    if (pending_delete == 0) {
        TEST_info("Freeing element which was not scheduled for free");
        free_failure = 1;
    } else {
        CRYPTO_atomic_add(&m->pending_delete, -1,
                          &ret, worker_lock);
    }
}

#define BEHAVIOR_MASK 0x3
#define DO_LOOKUP 0
#define DO_INSERT 1
#define DO_REPLACE 2
#define DO_DELETE 3

static void do_mt_hash_work(void)
{
    MTKEY key;
    unsigned int index;
    int num;
    TEST_MT_ENTRY *m;
    TEST_MT_ENTRY *expected_m = NULL;
    HT_VALUE *v = NULL;
    TEST_MT_ENTRY **r = NULL;
    int expected_rc;
    int ret;
    char behavior;
    size_t iter = 0;
    int giter;

    CRYPTO_atomic_add(&worker_num, 1, &num, worker_lock);
    num--; /* atomic_add is an add/fetch operation */

    HT_INIT_KEY(&key);

    for (iter = 0; iter < TEST_THREAD_ITERATIONS; iter++) {
        if (!RAND_bytes((unsigned char *)&index, sizeof(unsigned int))) {
            worker_exits[num] = "Failed to get random index";
            return;
        }
        index %= TEST_MT_POOL_SZ;
        if (!RAND_bytes((unsigned char *)&behavior, sizeof(char))) {
            worker_exits[num] = "Failed to get random behavior";
            return;
        }
        behavior &= BEHAVIOR_MASK;

        expected_m = &test_mt_entries[index];
        HT_KEY_RESET(&key);
        HT_SET_KEY_FIELD(&key, index, index);

        if (!CRYPTO_atomic_add(&global_iteration, 1, &giter, worker_lock)) {
            worker_exits[num] = "Unable to increment global iterator";
            return;
        }
        switch(behavior) {
        case DO_LOOKUP:
            ossl_ht_read_lock(m_ht);
            m = ossl_ht_mt_TEST_MT_ENTRY_get(m_ht, TO_HT_KEY(&key), &v);
            if (m != NULL && m != expected_m) {
                worker_exits[num] = "Read unexpected value from hashtable";
                TEST_info("Iteration %d Read unexpected value %p when %p expected",
                          giter, (void *)m, (void *)expected_m);
            }
            ossl_ht_read_unlock(m_ht);
            if (worker_exits[num] != NULL)
                return;
            break;
        case DO_INSERT:
        case DO_REPLACE:
            ossl_ht_write_lock(m_ht);
            if (behavior == DO_REPLACE) {
                expected_rc = 1;
                r = &m;
            } else {
                expected_rc = !expected_m->in_table;
                r = NULL;
            }

            if (expected_rc != ossl_ht_mt_TEST_MT_ENTRY_insert(m_ht,
                                                               TO_HT_KEY(&key),
                                                               expected_m, r)) {
                TEST_info("Iteration %d Expected rc %d on %s of element %d which is %s\n",
                          giter, expected_rc, behavior == DO_REPLACE ? "replace" : "insert",
                          index, expected_m->in_table ? "in table" : "not in table");
                worker_exits[num] = "Failure on insert";
            }
            if (expected_rc == 1)
                expected_m->in_table = 1;
            ossl_ht_write_unlock(m_ht);
            if (worker_exits[num] != NULL)
                return;
            break;
        case DO_DELETE:
            ossl_ht_write_lock(m_ht);
            expected_rc = expected_m->in_table;
            if (expected_rc != ossl_ht_delete(m_ht, TO_HT_KEY(&key))) {
                TEST_info("Iteration %d Expected rc %d on delete of element %d which is %s\n",
                          giter, expected_rc, index,
                          expected_m->in_table ? "in table" : "not in table");
                worker_exits[num] = "Failure on delete";
            }
            if (expected_rc == 1) {
                expected_m->in_table = 0;
                CRYPTO_atomic_add(&expected_m->pending_delete, 1, &ret, worker_lock); 
            }
            ossl_ht_write_unlock(m_ht);
            if (worker_exits[num] != NULL)
                return;
            break;
        default:
            worker_exits[num] = "Undefined behavior specified";
            return;
        }
    }
}

static int test_hashtable_multithread(void)
{
    HT_CONFIG hash_conf = {
        NULL,              /* use default context */
        hashtable_mt_free, /* our free function */
        NULL,              /* default hash function */
        0,                 /* default hash size */
    };
    int ret = 0;
    thread_t workers[4];
    int i;
#ifdef MEASURE_HASH_PERFORMANCE
    struct timeval start, end, delta;
#endif

    memset(worker_exits, 0, sizeof(char *) * 4);
    memset(test_mt_entries, 0, sizeof(TEST_MT_ENTRY) * TEST_MT_POOL_SZ);
    memset(workers, 0, sizeof(thread_t) * 4);

    m_ht = ossl_ht_new(&hash_conf);

    if (!TEST_ptr(m_ht))
        goto end;

    worker_lock = CRYPTO_THREAD_lock_new();
    if (worker_lock == NULL)
        goto end_free;
#ifdef MEASURE_HASH_PERFORMANCE
    gettimeofday(&start, NULL);
#endif

    for (i = 0; i < 4; i++) {
        if (!run_thread(&workers[i], do_mt_hash_work))
            goto shutdown;
    }

shutdown:
    for (--i; i >= 0; i--) {
        wait_for_thread(workers[i]);
    }


    /*
     * Now that the workers are done, check for any error
     * conditions
     */
    ret = 1;
    for (i = 0; i < 4; i++) {
        if (worker_exits[i] != NULL) {
            TEST_info("Worker %d failed: %s\n", i, worker_exits[i]);
            ret = 0;
        }
    }
    if (free_failure == 1) {
        TEST_info("Encountered a free failure");
        ret = 0;
    }

#ifdef MEASURE_HASH_PERFORMANCE
    gettimeofday(&end, NULL);
    timeval_subtract(&delta, &end, &start);
    TEST_info("multithread stress runs 40000 ops in %ld.%ld seconds", delta.tv_sec, delta.tv_usec);
#endif

end_free:
    shutting_down = 1;
    ossl_ht_free(m_ht);
end:
    return ret;
}

int setup_tests(void)
{
    ADD_TEST(test_int_lhash);
    ADD_TEST(test_stress);
    ADD_TEST(test_int_hashtable);
    ADD_TEST(test_hashtable_stress);
    ADD_TEST(test_hashtable_multithread);
    return 1;
}

ENEA — Copyright (C), ENEA. License: GNU AGPLv3+.
Legal notes  ::  JavaScript license information ::  Web API

back to top