[gnome-multi-writer/wip/hadess/more-random: 1/2] Make gmw-probe unpredictable



commit 7401c8be8da4be77653d26f1f1045ad3a8a90b7f
Author: Bastien Nocera <hadess hadess net>
Date:   Mon Jul 22 12:16:24 2019 +0200

    Make gmw-probe unpredictable
    
    Instead of setting a predictable seed, randomise the data to write,
    as well as the offsets to which to write. A failed run can then
    be replicated by reusing the seed mentioned in the error message.

 src/gmw-probe.c | 19 ++++++++++++++-----
 1 file changed, 14 insertions(+), 5 deletions(-)
---
diff --git a/src/gmw-probe.c b/src/gmw-probe.c
index 114f293..2571fba 100644
--- a/src/gmw-probe.c
+++ b/src/gmw-probe.c
@@ -60,6 +60,8 @@ typedef struct {
        GUdevDevice             *udev_device;
 } GmwProbeDevice;
 
+static guint32 seed = 0;
+
 #define GMW_ERROR              1
 #define GMW_ERROR_FAILED       0
 #define GMW_ERROR_IS_FAKE      1
@@ -246,7 +248,8 @@ gmw_probe_device_data_verify (GmwProbeDevice *dev,
                        g_set_error (error,
                                     GMW_ERROR,
                                     GMW_ERROR_FAILED,
-                                    "Failed to read data");
+                                    "Failed to read data (seed: %u)",
+                                    seed);
                        return FALSE;
                }
                item->valid = memcmp (item->data_random,
@@ -378,8 +381,9 @@ gmw_probe_scan_device (GmwProbeDevice *dev, GCancellable *cancellable, GError **
                        g_set_error (error,
                                     GMW_ERROR,
                                     GMW_ERROR_IS_FAKE,
-                                    "Failed to verify data at %" G_GUINT64_FORMAT "MB",
-                                    item->address / ONE_MB);
+                                    "Failed to verify data at %" G_GUINT64_FORMAT "MB  (seed: %u)",
+                                    item->address / ONE_MB,
+                                    seed);
                        return FALSE;
                }
        }
@@ -472,11 +476,16 @@ main (int argc, char **argv)
                { "verbose", 'v', 0, G_OPTION_ARG_NONE, &verbose,
                        /* TRANSLATORS: command line option */
                        _("Show extra debugging information"), NULL },
+               { "seed", 's', 0, G_OPTION_ARG_INT, &seed,
+                       /* TRANSLATORS: command line option */
+                       _("Random seed for predictability"), NULL },
                { NULL}
        };
 
-       /* make this predictable */
-       g_random_set_seed (0xdead);
+       if (seed == 0)
+               seed = g_random_int_range (0, G_MAXINT);
+       g_random_set_seed (seed);
+       g_debug ("Using %u as a random seed", seed);
 
        /* TRANSLATORS: A program to copy the LiveUSB image onto USB hardware */
        context = g_option_context_new (NULL);


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