[libvirt] [PATCH] tests: mock virRandomBits to make it endian stable

Daniel P. Berrangé posted 1 patch 5 years, 8 months ago
Patches applied successfully (tree, apply log)
git fetch https://github.com/patchew-project/libvirt tags/patchew/20180802113721.15864-1-berrange@redhat.com
Test syntax-check passed
tests/virrandommock.c | 8 ++++++++
1 file changed, 8 insertions(+)
[libvirt] [PATCH] tests: mock virRandomBits to make it endian stable
Posted by Daniel P. Berrangé 5 years, 8 months ago
virRandomBits is implemented in terms of virRandomBytes. Although we
mock virRandomBytes to give a stable value, this is not sufficient to
make virRandomBits give a stable value. The result of virRandomBits will
vary depending on endianness. Thus we mock virRandomBits to return a
stable value directly.

Signed-off-by: Daniel P. Berrangé <berrange@redhat.com>
---
 tests/virrandommock.c | 8 ++++++++
 1 file changed, 8 insertions(+)

diff --git a/tests/virrandommock.c b/tests/virrandommock.c
index 99a55a576a..3079b8bacb 100644
--- a/tests/virrandommock.c
+++ b/tests/virrandommock.c
@@ -44,6 +44,14 @@ virRandomBytes(unsigned char *buf,
     return 0;
 }
 
+uint64_t virRandomBits(int nbits)
+{
+    /* Chosen by a fair roll of a 2^64 sided dice */
+    uint64_t ret = 0x0706050403020100;
+    if (nbits < 64)
+        ret &= ((1ULL << nbits) - 1);
+    return ret;
+}
 
 int virRandomGenerateWWN(char **wwn,
                          const char *virt_type ATTRIBUTE_UNUSED)
-- 
2.17.1

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list
Re: [libvirt] [PATCH] tests: mock virRandomBits to make it endian stable
Posted by Boris Fiuczynski 5 years, 8 months ago
Reviewed-by: Boris Fiuczynski <fiuczy@linux.ibm.com>
and
Tested-by: Boris Fiuczynski <fiuczy@linux.ibm.com>

On 08/02/2018 01:37 PM, Daniel P. Berrangé wrote:
> virRandomBits is implemented in terms of virRandomBytes. Although we
> mock virRandomBytes to give a stable value, this is not sufficient to
> make virRandomBits give a stable value. The result of virRandomBits will
> vary depending on endianness. Thus we mock virRandomBits to return a
> stable value directly.
> 
> Signed-off-by: Daniel P. Berrangé <berrange@redhat.com>
> ---
>   tests/virrandommock.c | 8 ++++++++
>   1 file changed, 8 insertions(+)
> 
> diff --git a/tests/virrandommock.c b/tests/virrandommock.c
> index 99a55a576a..3079b8bacb 100644
> --- a/tests/virrandommock.c
> +++ b/tests/virrandommock.c
> @@ -44,6 +44,14 @@ virRandomBytes(unsigned char *buf,
>       return 0;
>   }
>   
> +uint64_t virRandomBits(int nbits)
> +{
> +    /* Chosen by a fair roll of a 2^64 sided dice */
Please keep the dice you used for this roll for later reference... ;-)

> +    uint64_t ret = 0x0706050403020100;
> +    if (nbits < 64)
> +        ret &= ((1ULL << nbits) - 1);
> +    return ret;
> +}
>   
>   int virRandomGenerateWWN(char **wwn,
>                            const char *virt_type ATTRIBUTE_UNUSED)
> 


-- 
Mit freundlichen Grüßen/Kind regards
    Boris Fiuczynski

IBM Deutschland Research & Development GmbH
Vorsitzender des Aufsichtsrats: Martina Köderitz
Geschäftsführung: Dirk Wittkopp
Sitz der Gesellschaft: Böblingen
Registergericht: Amtsgericht Stuttgart, HRB 243294

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list
Re: [libvirt] [PATCH] tests: mock virRandomBits to make it endian stable
Posted by Eric Blake 5 years, 8 months ago
On 08/02/2018 06:37 AM, Daniel P. Berrangé wrote:
> virRandomBits is implemented in terms of virRandomBytes. Although we
> mock virRandomBytes to give a stable value, this is not sufficient to
> make virRandomBits give a stable value. The result of virRandomBits will
> vary depending on endianness. Thus we mock virRandomBits to return a
> stable value directly.
> 
> Signed-off-by: Daniel P. Berrangé <berrange@redhat.com>
> ---

> +uint64_t virRandomBits(int nbits)
> +{
> +    /* Chosen by a fair roll of a 2^64 sided dice */

Where do I get my hands on one of those?  :)

Reviewed-by: Eric Blake <eblake@redhat.com>

-- 
Eric Blake, Principal Software Engineer
Red Hat, Inc.           +1-919-301-3266
Virtualization:  qemu.org | libvirt.org

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list
Re: [libvirt] [PATCH] tests: mock virRandomBits to make it endian stable
Posted by Daniel P. Berrangé 5 years, 8 months ago
On Thu, Aug 02, 2018 at 08:17:25AM -0500, Eric Blake wrote:
> On 08/02/2018 06:37 AM, Daniel P. Berrangé wrote:
> > virRandomBits is implemented in terms of virRandomBytes. Although we
> > mock virRandomBytes to give a stable value, this is not sufficient to
> > make virRandomBits give a stable value. The result of virRandomBits will
> > vary depending on endianness. Thus we mock virRandomBits to return a
> > stable value directly.
> > 
> > Signed-off-by: Daniel P. Berrangé <berrange@redhat.com>
> > ---
> 
> > +uint64_t virRandomBits(int nbits)
> > +{
> > +    /* Chosen by a fair roll of a 2^64 sided dice */
> 
> Where do I get my hands on one of those?  :)
> 
> Reviewed-by: Eric Blake <eblake@redhat.com>

Thanks, this is now pushed


Regards,
Daniel
-- 
|: https://berrange.com      -o-    https://www.flickr.com/photos/dberrange :|
|: https://libvirt.org         -o-            https://fstop138.berrange.com :|
|: https://entangle-photo.org    -o-    https://www.instagram.com/dberrange :|

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list