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.
This commit is contained in:
Nick Mathewson 2014-09-18 12:02:13 -04:00
parent 76643dd0e7
commit a677b72bd6
6 changed files with 22 additions and 13 deletions

View 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));

View 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 *);

View 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;

View File

@ -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;
}

View 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;

View 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();