[PATCH 2/6] tests: mock gnutls_dh_params_generate2

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

 



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/virnettlscontexttest.c b/tests/virnettlscontexttest.c
index b062be6..9f62413 100644
--- a/tests/virnettlscontexttest.c
+++ b/tests/virnettlscontexttest.c
@@ -633,7 +633,7 @@ mymain(void)
     return ret == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
 }
 
-VIRT_TEST_MAIN(mymain)
+VIRT_TEST_MAIN_PRELOAD(mymain, abs_builddir "/.libs/virrandommock.so")
 
 #else
 
diff --git a/tests/virnettlssessiontest.c b/tests/virnettlssessiontest.c
index 47fbec6..0d2e106 100644
--- a/tests/virnettlssessiontest.c
+++ b/tests/virnettlssessiontest.c
@@ -487,7 +487,7 @@ mymain(void)
     return ret == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
 }
 
-VIRT_TEST_MAIN(mymain)
+VIRT_TEST_MAIN_PRELOAD(mymain, abs_builddir "/.libs/virrandommock.so")
 
 #else
 
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)
+{
+    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;
+
+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);
+
+    return gnutls_dh_params_cpy(dparams, params_cache);
+}
+#endif
-- 
2.7.3

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