]> granicus.if.org Git - libevent/commitdiff
Use evutil_weakrand() in unit tests.
authorNick Mathewson <nickm@torproject.org>
Thu, 18 Sep 2014 16:02:13 +0000 (12:02 -0400)
committerNick Mathewson <nickm@torproject.org>
Thu, 18 Sep 2014 16:02:13 +0000 (12:02 -0400)
(Coverity doesn't like random() or rand().  We don't care; this is
for unit tests.)

Fixes CID 1239298123929612392951239293.

test/regress.c
test/regress.h
test/regress_main.c
test/regress_minheap.c
test/test-ratelim.c
test/test-time.c

index a1094abfa04e47964c8e3739b0db05bfacdaa3ae..0438a9d3b9f114501d1d7aa14221d265f9a8e294 100644 (file)
@@ -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));
 
index a9892b0ef7c8b3b3ef5414b551e7138d4dd648fd..2e9f427b8fe6add642860c154822e900dc8e390f 100644 (file)
@@ -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 *);
 
index 58cbe5fe2741f858b9cf5ea0d4498029cdaf3ef8..3198ced1da3559bb53832f55ebe91b247ec93238 100644 (file)
@@ -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;
 
index a1f554ebbe8ff0f800fd3a8c90350ce698840c4b..05db32e26f4de7420ea8d9dd60392e2acc224279 100644 (file)
 
 #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;
 }
 
index 27649b8490e0b45a10b2820aabbd20d1d09087ab..28aed2893dd50063f750aad7063cd25d3916de69 100644 (file)
@@ -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;
index dcd6639a56fb88fe0f0a6f95c8be064c4987aca7..5c8593b1385dc3a2cdc729ce0eff8bb4b03f3623 100644 (file)
@@ -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();