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

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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.

--
libvir-list mailing list
libvir-list@xxxxxxxxxx
https://www.redhat.com/mailman/listinfo/libvir-list




[Index of Archives]     [Virt Tools]     [Libvirt Users]     [Lib OS Info]     [Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [KDE Users]     [Fedora Tools]