From e4867287195421ed3c41e15ce292b00b1c0e3649 Mon Sep 17 00:00:00 2001 From: Artem Bityutskiy Date: Tue, 15 Jun 2010 14:01:31 +0300 Subject: ubi-test: seed the random genrator in tests Add a common seed_random_generator() and make tests use it for seeding the random generator. Signed-off-by: Artem Bityutskiy --- tests/ubi-tests/common.c | 31 +++++++++++++++++++++++++++++++ tests/ubi-tests/common.h | 24 +++++++++++++----------- tests/ubi-tests/integ.c | 4 +--- tests/ubi-tests/io_paral.c | 1 + tests/ubi-tests/io_update.c | 1 + 5 files changed, 47 insertions(+), 14 deletions(-) diff --git a/tests/ubi-tests/common.c b/tests/ubi-tests/common.c index b605dd9..05cbecc 100644 --- a/tests/ubi-tests/common.c +++ b/tests/ubi-tests/common.c @@ -26,8 +26,10 @@ #include #include #include +#include #include #include +#include #include #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; +} diff --git a/tests/ubi-tests/common.h b/tests/ubi-tests/common.h index 859af14..88e963f 100644 --- a/tests/ubi-tests/common.h +++ b/tests/ubi-tests/common.h @@ -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 } diff --git a/tests/ubi-tests/integ.c b/tests/ubi-tests/integ.c index e0e8bee..f293444 100644 --- a/tests/ubi-tests/integ.c +++ b/tests/ubi-tests/integ.c @@ -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(); diff --git a/tests/ubi-tests/io_paral.c b/tests/ubi-tests/io_paral.c index 3ceda95..9754a0d 100644 --- a/tests/ubi-tests/io_paral.c +++ b/tests/ubi-tests/io_paral.c @@ -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; diff --git a/tests/ubi-tests/io_update.c b/tests/ubi-tests/io_update.c index 3b8169d..0259446 100644 --- a/tests/ubi-tests/io_update.c +++ b/tests/ubi-tests/io_update.c @@ -267,6 +267,7 @@ remove: int main(int argc, char * const argv[]) { + seed_random_generator(); if (initial_check(argc, argv)) return 1; -- cgit v1.2.3