[PATCH 2/3] tests: Need help... Trying to add virRandomBytes mock

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

 



What I'd like to do is merge this and the previous patch together;
however, I'm missing something (hopefully obvious) with the building
of the mocked function.

Since this won't be a qemu specific function, the test wouldn't belong
in qemuxml2argvmock - rather a separate mock would seemingly have to be
created and then a separate virrandomtest; however, I'm lost in the
maze of Makefile magic - so hopefully someone can help... The current
code works, but generates a nefarious:

*** Warning: Linking the executable virrandomtest against the loadable module
*** virrandommock.so is not portable!

during the build...

Signed-off-by: John Ferlan <jferlan@xxxxxxxxxx>
---
 tests/Makefile.am        | 16 +++++++++
 tests/commandhelper.c    |  1 +
 tests/qemuxml2argvmock.c | 31 +----------------
 tests/virrandommock.c    | 56 +++++++++++++++++++++++++++++++
 tests/virrandomtest.c    | 86 ++++++++++++++++++++++++++++++++++++++++++++++++
 5 files changed, 160 insertions(+), 30 deletions(-)
 create mode 100644 tests/virrandommock.c
 create mode 100644 tests/virrandomtest.c

diff --git a/tests/Makefile.am b/tests/Makefile.am
index 238f6da..6ba6c5b 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -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,14 @@ vircryptotest_SOURCES = \
 	vircryptotest.c testutils.h testutils.c
 vircryptotest_LDADD = $(LDADDS)
 
+virrandomtest_SOURCES = \
+	virrandomtest.c testutils.h testutils.c
+virrandomtest_LDADD = \
+	virrandommock.la \
+	../src/libvirt.la \
+	../src/libvirt_util.la \
+	$(GNULIB_LIBS)
+
 virhostdevtest_SOURCES = \
 	virhostdevtest.c testutils.h testutils.c
 virhostdevtest_LDADD = $(LDADDS)
@@ -1094,6 +1104,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/commandhelper.c b/tests/commandhelper.c
index 015efda..288d651 100644
--- a/tests/commandhelper.c
+++ b/tests/commandhelper.c
@@ -31,6 +31,7 @@
 #include "virutil.h"
 #include "viralloc.h"
 #include "virfile.h"
+
 #include "testutils.h"
 #include "virstring.h"
 
diff --git a/tests/qemuxml2argvmock.c b/tests/qemuxml2argvmock.c
index dade748..1616eed 100644
--- a/tests/qemuxml2argvmock.c
+++ b/tests/qemuxml2argvmock.c
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2014-2016 Red Hat, Inc.
+ * Copyright (C) 2014 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
@@ -30,13 +30,6 @@
 #include "virstring.h"
 #include "virtpm.h"
 #include "virutil.h"
-#include "virrandom.h"
-#ifdef WITH_GNUTLS
-# include <gnutls/gnutls.h>
-# if HAVE_GNUTLS_CRYPTO_H
-#  include <gnutls/crypto.h>
-# endif
-#endif
 #include <time.h>
 #include <unistd.h>
 
@@ -152,25 +145,3 @@ virCommandPassFD(virCommandPtr cmd ATTRIBUTE_UNUSED,
 {
     /* nada */
 }
-
-int
-virRandomBytes(unsigned char *buf,
-               size_t buflen)
-{
-    size_t i;
-
-    for (i = 0; i < buflen; i++)
-        buf[i] = i;
-
-    return 0;
-}
-
-#ifdef WITH_GNUTLS
-int
-gnutls_rnd(gnutls_rnd_level_t level ATTRIBUTE_UNUSED,
-           void *data,
-           size_t len)
-{
-    return virRandomBytes(data, len);
-#endif
-}
diff --git a/tests/virrandommock.c b/tests/virrandommock.c
new file mode 100644
index 0000000..1c9f763
--- /dev/null
+++ b/tests/virrandommock.c
@@ -0,0 +1,56 @@
+/*
+ * 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@xxxxxxxxxx>
+ */
+
+#include <config.h>
+
+#include "internal.h"
+#include "virrandom.h"
+#include "virmock.h"
+
+#ifdef WITH_GNUTLS
+# include <gnutls/gnutls.h>
+# if HAVE_GNUTLS_CRYPTO_H
+#  include <gnutls/crypto.h>
+# endif
+#endif
+
+#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;
+}
+
+#ifdef WITH_GNUTLS
+int
+gnutls_rnd(gnutls_rnd_level_t level ATTRIBUTE_UNUSED,
+           void *data,
+           size_t len)
+{
+    return virRandomBytes(data, len);
+}
+#endif
diff --git a/tests/virrandomtest.c b/tests/virrandomtest.c
new file mode 100644
index 0000000..76e187c
--- /dev/null
+++ b/tests/virrandomtest.c
@@ -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@xxxxxxxxxx>
+ */
+
+#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]='%02x' not in seqence\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(mymain)
+
+#else
+
+int
+main(void)
+{
+    return EXIT_AM_SKIP;
+}
+
+#endif
-- 
2.5.5

--
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]