[libvirt] [PATCH 2/6] tests: mock gnutls_dh_params_generate2

Peter Krempa pkrempa at redhat.com
Mon Jun 20 15:25:40 UTC 2016


On Fri, Jun 17, 2016 at 20:04:37 +0200, Ján Tomko wrote:
> This function generates some big random numbers.
> 
> Cache the result and supply it to any subsequent generate2 calls.
> ---
>  tests/virnettlscontexttest.c |  2 +-
>  tests/virnettlssessiontest.c |  2 +-
>  tests/virrandommock.c        | 51 ++++++++++++++++++++++++++++++++++++++++++++
>  3 files changed, 53 insertions(+), 2 deletions(-)


> diff --git a/tests/virrandommock.c b/tests/virrandommock.c
> index 6df5e20..04703a1 100644
> --- a/tests/virrandommock.c
> +++ b/tests/virrandommock.c
> @@ -37,3 +37,54 @@ virRandomBytes(unsigned char *buf,
>  
>      return 0;
>  }
> +
> +
> +#ifdef WITH_GNUTLS
> +# include <assert.h>
> +# include <stdio.h>
> +# include <gnutls/gnutls.h>
> +
> +static int (*realgenerate2)(gnutls_dh_params_t dparams,
> +                            unsigned int bits);
> +
> +static void init_syms(void)
> +{

We have a macro to help with all the stuff below. It also has better
error message. You should use it: VIR_MOCK_REAL_INIT

> +    if (realgenerate2)
> +        return;
> +
> +    realgenerate2 = dlsym(RTLD_NEXT, "gnutls_dh_params_generate2");
> +    if (realgenerate2)
> +        return;
> +
> +    fprintf(stderr, "Error getting symbols");
> +    abort();
> +}
> +
> +static gnutls_dh_params_t params_cache;
> +unsigned int cachebits;

Perhaps this should be static too.

> +
> +int
> +gnutls_dh_params_generate2(gnutls_dh_params_t dparams,
> +                           unsigned int bits)
> +{
> +    int rc = 0;
> +
> +    init_syms();
> +
> +    if (!params_cache) {
> +        if (gnutls_dh_params_init(&params_cache) < 0) {
> +            fprintf(stderr, "Error initializing params cache");
> +            abort();
> +        }
> +        rc = realgenerate2(params_cache, bits);
> +
> +        if (rc < 0)
> +            return rc;
> +        cachebits = bits;
> +    }
> +
> +    assert(cachebits == bits);

I'd rather not use assert here. Since you already have abort available
you can use it directly.

> +
> +    return gnutls_dh_params_cpy(dparams, params_cache);
> +}

ACK with fixes.




More information about the libvir-list mailing list