]> www.infradead.org Git - mtd-utils.git/commitdiff
ubi-test: seed the random genrator in tests
authorArtem Bityutskiy <Artem.Bityutskiy@nokia.com>
Tue, 15 Jun 2010 11:01:31 +0000 (14:01 +0300)
committerArtem Bityutskiy <Artem.Bityutskiy@nokia.com>
Tue, 15 Jun 2010 11:02:48 +0000 (14:02 +0300)
Add a common seed_random_generator() and make tests use it
for seeding the random generator.

Signed-off-by: Artem Bityutskiy <Artem.Bityutskiy@nokia.com>
tests/ubi-tests/common.c
tests/ubi-tests/common.h
tests/ubi-tests/integ.c
tests/ubi-tests/io_paral.c
tests/ubi-tests/io_update.c

index b605dd9dcca697382a813a485ee648ca23cd1156..05cbeccd6034a8944e0bade0b55c3717766c214d 100644 (file)
 #include <errno.h>
 #include <string.h>
 #include <unistd.h>
+#include <limits.h>
 #include <sys/types.h>
 #include <sys/stat.h>
+#include <sys/time.h>
 #include <fcntl.h>
 #include "libubi.h"
 #include "common.h"
@@ -332,3 +334,32 @@ close:
        close(fd);
        return -1;
 }
+
+/**
+ * seed_random_generator - randomly seed the standard pseudo-random generator.
+ *
+ * This helper function seeds the standard libc pseudo-random generator with a
+ * more or less random value to make sure the 'rand()' call does not return the
+ * same sequence every time UBI utilities run. Returns the random seed in case
+ * of success and a %-1 in case of error.
+ */
+int seed_random_generator(void)
+{
+       struct timeval tv;
+       struct timezone tz;
+       int seed;
+
+       /*
+        * Just assume that a combination of the PID + current time is a
+        * reasonably random number.
+        */
+       if (gettimeofday(&tv, &tz))
+               return -1;
+
+       seed = (unsigned int)tv.tv_sec;
+       seed += (unsigned int)tv.tv_usec;
+       seed *= getpid();
+       seed %= INT_MAX;
+       srand(seed);
+       return seed;
+}
index 859af14451ec1427bc1fe66fad249bf8e6528d38..88e963fe1f776250d9b13482044b91d6b8a436c0 100644 (file)
@@ -90,19 +90,21 @@ extern "C" {
         (s)/5-3, (s)/5-2, (s)/5-1, (s)/5+1, (s)/5+2, (s)/5+3, (s)-17, (s)-9,  \
         (s)-8, (s)-6, (s)-4, (s)-1, (s)};
 
+extern int seed_random_generator(void);
+
 extern void __errmsg(const char *test, const char *func, int line,
                     const char *fmt, ...);
-void __failed(const char *test, const char *func, int line,
-             const char *failed);
-int __initial_check(const char *test, int argc, char * const argv[]);
-int __check_volume(libubi_t libubi, struct ubi_dev_info *dev_info,
-                  const char *test, const char *func, int line, int vol_id,
-                  const struct ubi_mkvol_request *req);
-int __check_vol_patt(libubi_t libubi, const char *test, const char *func,
-                    int line, const char *node, uint8_t byte);
-int __update_vol_patt(libubi_t libubi, const char *test, const char *func,
-                     int line, const char *node, long long bytes,
-                     uint8_t byte);
+extern void __failed(const char *test, const char *func, int line,
+                    const char *failed);
+extern int __initial_check(const char *test, int argc, char * const argv[]);
+extern int __check_volume(libubi_t libubi, struct ubi_dev_info *dev_info,
+                         const char *test, const char *func, int line,
+                         int vol_id, const struct ubi_mkvol_request *req);
+extern int __check_vol_patt(libubi_t libubi, const char *test, const char *func,
+                           int line, const char *node, uint8_t byte);
+extern int __update_vol_patt(libubi_t libubi, const char *test, const char *func,
+                            int line, const char *node, long long bytes,
+                            uint8_t byte);
 
 #ifdef __cplusplus
 }
index e0e8bee7be9befca447f0280ff37cef7a6f092ef..f2934446f48383feff1afa73505cad97c7d6333c 100644 (file)
@@ -731,10 +731,8 @@ int main(int argc,char *argv[])
                return 1;
        }
 
-       initial_seed = getpid();
+       next_seed = initial_seed = seed_random_generator();
        printf("Initial seed = %u\n", (unsigned) initial_seed);
-       next_seed = initial_seed;
-       srand(initial_seed);
        load_ubi();
 
        libubi = libubi_open();
index 3ceda952a4c506d5395a8b01f20361073fedd043..9754a0d8e594b831f4b2bb4662f991fce20148f6 100644 (file)
@@ -233,6 +233,7 @@ int main(int argc, char * const argv[])
        int i, ret;
        pthread_t threads[THREADS_NUM];
 
+       seed_random_generator();
        if (initial_check(argc, argv))
                return 1;
 
index 3b8169d41bc74df7663e6d6df4863bfecdb87736..0259446a861456acff184eb316eb1d8dc0f9fbef 100644 (file)
@@ -267,6 +267,7 @@ remove:
 
 int main(int argc, char * const argv[])
 {
+       seed_random_generator();
        if (initial_check(argc, argv))
                return 1;