From a677b72bd6d9d378daf9775966aa8ca574e26e67 Mon Sep 17 00:00:00 2001 From: Nick Mathewson Date: Thu, 18 Sep 2014 12:02:13 -0400 Subject: [PATCH] Use evutil_weakrand() in unit tests. (Coverity doesn't like random() or rand(). We don't care; this is for unit tests.) Fixes CID 1239298, 1239296, 1239295, 1239293. --- test/regress.c | 2 +- test/regress.h | 4 ++++ test/regress_main.c | 4 ++++ test/regress_minheap.c | 5 +++-- test/test-ratelim.c | 9 ++++----- test/test-time.c | 11 ++++++----- 6 files changed, 22 insertions(+), 13 deletions(-) diff --git a/test/regress.c b/test/regress.c index a1094abf..0438a9d3 100644 --- a/test/regress.c +++ b/test/regress.c @@ -2296,7 +2296,7 @@ evtag_fuzz(void *ptr) for (j = 0; j < 100; j++) { for (i = 0; i < (int)sizeof(buffer); i++) - buffer[i] = rand(); + buffer[i] = test_weakrand(); evbuffer_drain(tmp, -1); evbuffer_add(tmp, buffer, sizeof(buffer)); diff --git a/test/regress.h b/test/regress.h index a9892b0e..2e9f427b 100644 --- a/test/regress.h +++ b/test/regress.h @@ -53,6 +53,10 @@ extern struct testcase_t listener_testcases[]; extern struct testcase_t listener_iocp_testcases[]; extern struct testcase_t thread_testcases[]; +extern struct evutil_weakrand_state test_weakrand_state; + +#define test_weakrand() (evutil_weakrand_(&test_weakrand_state)) + void regress_threads(void *); void test_bufferevent_zlib(void *); diff --git a/test/regress_main.c b/test/regress_main.c index 58cbe5fe..3198ced1 100644 --- a/test/regress_main.c +++ b/test/regress_main.c @@ -90,6 +90,8 @@ #include "../iocp-internal.h" #include "../event-internal.h" +struct evutil_weakrand_state test_weakrand_state; + long timeval_msec_diff(const struct timeval *start, const struct timeval *end) { @@ -452,6 +454,8 @@ main(int argc, const char **argv) tinytest_set_aliases(testaliases); + evutil_weakrand_seed_(&test_weakrand_state, 0); + if (tinytest_main(argc,argv,testgroups)) return 1; diff --git a/test/regress_minheap.c b/test/regress_minheap.c index a1f554eb..05db32e2 100644 --- a/test/regress_minheap.c +++ b/test/regress_minheap.c @@ -30,12 +30,13 @@ #include "tinytest.h" #include "tinytest_macros.h" +#include "regress.h" static void set_random_timeout(struct event *ev) { - ev->ev_timeout.tv_sec = rand(); - ev->ev_timeout.tv_usec = rand() & 0xfffff; + ev->ev_timeout.tv_sec = test_weakrand(); + ev->ev_timeout.tv_usec = test_weakrand() & 0xfffff; ev->ev_timeout_pos.min_heap_idx = -1; } diff --git a/test/test-ratelim.c b/test/test-ratelim.c index 27649b84..28aed289 100644 --- a/test/test-ratelim.c +++ b/test/test-ratelim.c @@ -50,6 +50,7 @@ #include "event2/listener.h" #include "event2/thread.h" +static struct evutil_weakrand_state weakrand_state; static int cfg_verbose = 0; static int cfg_help = 0; @@ -113,11 +114,7 @@ loud_writecb(struct bufferevent *bev, void *ctx) struct client_state *cs = ctx; struct evbuffer *output = bufferevent_get_output(bev); char buf[1024]; -#ifdef _WIN32 - int r = rand() % 256; -#else - int r = random() % 256; -#endif + int r = evutil_weakrand_(&weakrand_state); memset(buf, r, sizeof(buf)); while (evbuffer_get_length(output) < 8192) { evbuffer_add(output, buf, sizeof(buf)); @@ -553,6 +550,8 @@ main(int argc, char **argv) (void) WSAStartup(wVersionRequested, &wsaData); #endif + evutil_weakrand_seed_(&weakrand_state, 0); + #ifndef _WIN32 if (signal(SIGPIPE, SIG_IGN) == SIG_ERR) return 1; diff --git a/test/test-time.c b/test/test-time.c index dcd6639a..5c8593b1 100644 --- a/test/test-time.c +++ b/test/test-time.c @@ -41,6 +41,7 @@ #include "event2/event.h" #include "event2/event_compat.h" #include "event2/event_struct.h" +#include "util-internal.h" int called = 0; @@ -48,14 +49,12 @@ int called = 0; struct event *ev[NEVENT]; +struct evutil_weakrand_state weakrand_state; + static int rand_int(int n) { -#ifdef _WIN32 - return (int)(rand() % n); -#else - return (int)(random() % n); -#endif + return evutil_weakrand_(&weakrand_state); } static void @@ -93,6 +92,8 @@ main(int argc, char **argv) (void) WSAStartup(wVersionRequested, &wsaData); #endif + evutil_weakrand_seed_(&weakrand_state, 0); + /* Initalize the event library */ event_init();