[libvirt] [PATCH] tests: viriscsitest: make it work on big endian archs

Pino Toscano ptoscano at redhat.com
Wed Aug 1 13:08:07 UTC 2018


viriscsitest tries to ensure the interface IQN used is a specific one,
checking later on that it is the same all during the whole test.  Since
the IQN generation involves random bytes, viriscsitest got a fake
virRandomBytes from the virrandommock helper library, setting static
values.  virRandomBits(), called by virStorageBackendCreateIfaceIQN(),
always requests 8 random bytes, chopping off the ones not requested by
the caller -- this meant that on big endian machines it would chop bytes
from the wrong size of the data buffer, and thus not returning the
expected numbers.

As a fix, do not rely on the mock virRandomBytes, but provide an own
version of it: this version will fill the values in the expected order,
depending on the endianness of the system.  This way, the result of
virStorageBackendCreateIfaceIQN() will be what the test actually
expects.

Signed-off-by: Pino Toscano <ptoscano at redhat.com>
---
 tests/viriscsitest.c | 30 ++++++++++++++++++++++++++++--
 1 file changed, 28 insertions(+), 2 deletions(-)

diff --git a/tests/viriscsitest.c b/tests/viriscsitest.c
index 4fd7ff6e19..d43c0115d3 100644
--- a/tests/viriscsitest.c
+++ b/tests/viriscsitest.c
@@ -21,6 +21,7 @@
 #include <config.h>
 
 #include "testutils.h"
+#include "virrandom.h"
 
 #ifdef WIN32
 int
@@ -36,6 +37,32 @@ main(void)
 
 # define VIR_FROM_THIS VIR_FROM_NONE
 
+bool isLittleEndian(void);
+bool isLittleEndian(void)
+{
+    static int cache = -1;
+    if (cache < 0) {
+        int val = 1;
+        char *ptr = (char *)&val;
+        cache = *ptr ? 1 : 0;
+    }
+    return cache > 0;
+}
+
+
+int
+virRandomBytes(unsigned char *buf,
+               size_t buflen)
+{
+    size_t i;
+    const bool isLE = isLittleEndian();
+
+    for (i = 0; i < buflen; i++)
+        buf[i] = isLE ? i : buflen - i - 1;
+
+    return 0;
+}
+
 static const char *iscsiadmSessionOutput =
     "tcp: [1] 10.20.30.40:3260,1 iqn.2004-06.example:example1:iscsi.test\n"
     "tcp: [2] 10.20.30.41:3260,1 iqn.2005-05.example:example1:iscsi.hello\n"
@@ -368,6 +395,5 @@ mymain(void)
     return EXIT_SUCCESS;
 }
 
-VIR_TEST_MAIN_PRELOAD(mymain,
-                      abs_builddir "/.libs/virrandommock.so")
+VIR_TEST_MAIN(mymain)
 #endif /* WIN32 */
-- 
2.17.1




More information about the libvir-list mailing list