backup: Wire up qemu full pull backup commands over QMP
[libvirt/ericb.git] / tests / virrandommock.c
blob3781b0f4efff3ac523c54c6a197b3aee40da0c20
1 /*
2 * Copyright (C) 2016 Red Hat, Inc.
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library. If not, see
16 * <http://www.gnu.org/licenses/>.
19 #include <config.h>
21 #ifndef WIN32
23 # include <gnutls/gnutls.h>
25 # include "internal.h"
26 # include "virstring.h"
27 # include "virrandom.h"
28 # include "virmock.h"
30 # define VIR_FROM_THIS VIR_FROM_NONE
32 int
33 virRandomBytes(unsigned char *buf,
34 size_t buflen)
36 size_t i;
38 for (i = 0; i < buflen; i++)
39 buf[i] = i;
41 return 0;
44 uint64_t virRandomBits(int nbits)
46 /* Chosen by a fair roll of a 2^64 sided dice */
47 uint64_t ret = 0x0706050403020100;
48 if (nbits < 64)
49 ret &= ((1ULL << nbits) - 1);
50 return ret;
53 int virRandomGenerateWWN(char **wwn,
54 const char *virt_type ATTRIBUTE_UNUSED)
56 return virAsprintf(wwn, "5100000%09llx",
57 (unsigned long long)virRandomBits(36));
61 static int (*real_gnutls_dh_params_generate2)(gnutls_dh_params_t dparams,
62 unsigned int bits);
64 static gnutls_dh_params_t params_cache;
65 static unsigned int cachebits;
67 int
68 gnutls_dh_params_generate2(gnutls_dh_params_t dparams,
69 unsigned int bits)
71 int rc = 0;
73 VIR_MOCK_REAL_INIT(gnutls_dh_params_generate2);
75 if (!params_cache) {
76 if (gnutls_dh_params_init(&params_cache) < 0) {
77 fprintf(stderr, "Error initializing params cache");
78 abort();
80 rc = real_gnutls_dh_params_generate2(params_cache, bits);
82 if (rc < 0)
83 return rc;
84 cachebits = bits;
87 if (cachebits != bits) {
88 fprintf(stderr, "Requested bits do not match the cached value");
89 abort();
92 return gnutls_dh_params_cpy(dparams, params_cache);
94 #else /* WIN32 */
95 /* Can't mock on WIN32 */
96 #endif