]> xenbits.xensource.com Git - libvirt.git/commitdiff
tests: Add mock for virRandomBytes
authorJohn Ferlan <jferlan@redhat.com>
Thu, 12 May 2016 16:15:44 +0000 (12:15 -0400)
committerJohn Ferlan <jferlan@redhat.com>
Fri, 20 May 2016 13:36:28 +0000 (09:36 -0400)
Create a mock for virRandomBytes to generate a not so random value.
This should be usable by other tests that need a not so random number
to be generated by including the virrandommock at preload.

The "random number" generated is based upon the size of the expected
stream of bytes being returned where each byte in the result gets
the index of the array - hence a 4 byte array returns 0x00010203.

tests/Makefile.am
tests/virrandommock.c [new file with mode: 0644]
tests/virrandomtest.c [new file with mode: 0644]

index c7c9a037afc3677690deccb46aa14c47f5be93cc..98b8bb9ddcc6814aed1976da4fd78c209903a416 100644 (file)
@@ -180,6 +180,7 @@ test_programs = virshtest sockettest \
        virbitmaptest \
        vircgrouptest \
        vircryptotest \
+       virrandomtest \
        virpcitest \
        virendiantest \
        virfiletest \
@@ -423,6 +424,7 @@ test_libraries = libshunload.la \
                vircgroupmock.la \
                virpcimock.la \
                virnetdevmock.la \
+               virrandommock.la \
                nodeinfomock.la \
                nssmock.la \
                $(NULL)
@@ -1080,6 +1082,10 @@ vircryptotest_SOURCES = \
        vircryptotest.c testutils.h testutils.c
 vircryptotest_LDADD = $(LDADDS)
 
+virrandomtest_SOURCES = \
+       virrandomtest.c testutils.h testutils.c
+virrandomtest_LDADD = $(LDADDS)
+
 virhostdevtest_SOURCES = \
        virhostdevtest.c testutils.h testutils.c
 virhostdevtest_LDADD = $(LDADDS)
@@ -1094,6 +1100,12 @@ virpcimock_la_CFLAGS = $(AM_CFLAGS)
 virpcimock_la_LDFLAGS = $(MOCKLIBS_LDFLAGS)
 virpcimock_la_LIBADD = $(MOCKLIBS_LIBS)
 
+virrandommock_la_SOURCES = \
+       virrandommock.c
+virrandommock_la_CFLAGS = $(AM_CFLAGS)
+virrandommock_la_LDFLAGS = $(MOCKLIBS_LDFLAGS)
+virrandommock_la_LIBADD = $(MOCKLIBS_LIBS)
+
 nodeinfomock_la_SOURCES = \
        nodeinfomock.c
 nodeinfomock_la_CFLAGS = $(AM_CFLAGS)
diff --git a/tests/virrandommock.c b/tests/virrandommock.c
new file mode 100644 (file)
index 0000000..6df5e20
--- /dev/null
@@ -0,0 +1,39 @@
+/*
+ * Copyright (C) 2016 Red Hat, Inc.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library.  If not, see
+ * <http://www.gnu.org/licenses/>.
+ *
+ * Author: John Ferlan <jferlan@redhat.com>
+ */
+
+#include <config.h>
+
+#include "internal.h"
+#include "virrandom.h"
+#include "virmock.h"
+
+#define VIR_FROM_THIS VIR_FROM_NONE
+
+int
+virRandomBytes(unsigned char *buf,
+               size_t buflen)
+{
+    size_t i;
+
+    for (i = 0; i < buflen; i++)
+        buf[i] = i;
+
+    return 0;
+}
diff --git a/tests/virrandomtest.c b/tests/virrandomtest.c
new file mode 100644 (file)
index 0000000..367bdc7
--- /dev/null
@@ -0,0 +1,86 @@
+/*
+ * Copyright (C) 2016 Red Hat, Inc.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library.  If not, see
+ * <http://www.gnu.org/licenses/>.
+ *
+ * Author: John Ferlan <jferlan@redhat.com>
+ */
+
+#include <config.h>
+
+#include "internal.h"
+#include "viralloc.h"
+#include "virrandom.h"
+#include "testutils.h"
+
+#ifndef WIN32
+
+# define VIR_FROM_THIS VIR_FROM_NONE
+
+static int
+testRandomBytes(const void *unused ATTRIBUTE_UNUSED)
+{
+    int ret = -1;
+    size_t i;
+    uint8_t *data;
+    size_t datalen = 32;
+
+    if (VIR_ALLOC_N(data, datalen) < 0)
+        return -1;
+
+    if (virRandomBytes(data, datalen) < 0) {
+        fprintf(stderr, "Failed to generate random bytes");
+        goto cleanup;
+    }
+
+    for (i = 0; i < datalen; i++) {
+        if (data[i] != i) {
+            fprintf(stderr,
+                    "virRandomBytes data[%zu]='%x' not in sequence\n",
+                    i, data[i]);
+            goto cleanup;
+        }
+    }
+
+    ret = 0;
+
+ cleanup:
+    VIR_FREE(data);
+    return ret;
+}
+
+
+static int
+mymain(void)
+{
+    int ret = 0;
+
+    if (virtTestRun("RandomBytes", testRandomBytes, NULL) < 0)
+        ret = -1;
+
+    return ret == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
+}
+
+VIRT_TEST_MAIN_PRELOAD(mymain, abs_builddir "/.libs/virrandommock.so")
+
+#else
+
+int
+main(void)
+{
+    return EXIT_AM_SKIP;
+}
+
+#endif