138032Speter/*	$NetBSD: regress.c,v 1.1.1.1 2009/11/02 10:01:03 plunky Exp $	*/
238032Speter/*
338032Speter * Copyright (c) 2003, 2004 Niels Provos <provos@citi.umich.edu>
438032Speter * All rights reserved.
590792Sgshapiro *
690792Sgshapiro * Redistribution and use in source and binary forms, with or without
738032Speter * modification, are permitted provided that the following conditions
838032Speter * are met:
938032Speter * 1. Redistributions of source code must retain the above copyright
1038032Speter *    notice, this list of conditions and the following disclaimer.
1164562Sgshapiro * 2. Redistributions in binary form must reproduce the above copyright
1264562Sgshapiro *    notice, this list of conditions and the following disclaimer in the
1364562Sgshapiro *    documentation and/or other materials provided with the distribution.
1464562Sgshapiro * 3. The name of the author may not be used to endorse or promote products
1564562Sgshapiro *    derived from this software without specific prior written permission.
1664562Sgshapiro *
1764562Sgshapiro * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
1864562Sgshapiro * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
1964562Sgshapiro * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
2064562Sgshapiro * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
2164562Sgshapiro * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
2264562Sgshapiro * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2364562Sgshapiro * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2464562Sgshapiro * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2564562Sgshapiro * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
2664562Sgshapiro * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2764562Sgshapiro */
2864562Sgshapiro
2964562Sgshapiro#ifdef WIN32
3064562Sgshapiro#include <winsock2.h>
3164562Sgshapiro#include <windows.h>
3290792Sgshapiro#endif
3364562Sgshapiro
3464562Sgshapiro#ifdef HAVE_CONFIG_H
3564562Sgshapiro#include "config.h"
3664562Sgshapiro#endif
3764562Sgshapiro
3864562Sgshapiro#include <sys/types.h>
39132943Sgshapiro#include <sys/stat.h>
40132943Sgshapiro#ifdef HAVE_SYS_TIME_H
41132943Sgshapiro#include <sys/time.h>
42132943Sgshapiro#endif
43132943Sgshapiro#include <sys/queue.h>
44132943Sgshapiro#ifndef WIN32
45132943Sgshapiro#include <sys/socket.h>
46132943Sgshapiro#include <sys/wait.h>
47132943Sgshapiro#include <signal.h>
48132943Sgshapiro#include <unistd.h>
49132943Sgshapiro#include <netdb.h>
50132943Sgshapiro#endif
51132943Sgshapiro#include <assert.h>
52132943Sgshapiro#include <fcntl.h>
53132943Sgshapiro#include <signal.h>
54132943Sgshapiro#include <stdlib.h>
55132943Sgshapiro#include <stdio.h>
56132943Sgshapiro#include <string.h>
57132943Sgshapiro#include <errno.h>
58132943Sgshapiro
59132943Sgshapiro#include "event.h"
60132943Sgshapiro#include "evutil.h"
61132943Sgshapiro#include "event-internal.h"
62132943Sgshapiro#include "log.h"
63132943Sgshapiro
64266527Sgshapiro#include "regress.h"
65#ifndef WIN32
66#include "regress.gen.h"
67#endif
68
69int pair[2];
70int test_ok;
71static int called;
72static char wbuf[4096];
73static char rbuf[4096];
74static int woff;
75static int roff;
76static int usepersist;
77static struct timeval tset;
78static struct timeval tcalled;
79static struct event_base *global_base;
80
81static int interval;
82
83#define TEST1	"this is a test"
84#define SECONDS	1
85
86#ifndef SHUT_WR
87#define SHUT_WR 1
88#endif
89
90#ifdef WIN32
91#define write(fd,buf,len) send((fd),(buf),(len),0)
92#define read(fd,buf,len) recv((fd),(buf),(len),0)
93#endif
94
95static void
96simple_read_cb(int fd, short event, void *arg)
97{
98	char buf[256];
99	int len;
100
101	if (arg == NULL)
102		return;
103
104	len = read(fd, buf, sizeof(buf));
105
106	if (len) {
107		if (!called) {
108			if (event_add(arg, NULL) == -1)
109				exit(1);
110		}
111	} else if (called == 1)
112		test_ok = 1;
113
114	called++;
115}
116
117static void
118simple_write_cb(int fd, short event, void *arg)
119{
120	int len;
121
122	if (arg == NULL)
123		return;
124
125	len = write(fd, TEST1, strlen(TEST1) + 1);
126	if (len == -1)
127		test_ok = 0;
128	else
129		test_ok = 1;
130}
131
132static void
133multiple_write_cb(int fd, short event, void *arg)
134{
135	struct event *ev = arg;
136	int len;
137
138	len = 128;
139	if (woff + len >= sizeof(wbuf))
140		len = sizeof(wbuf) - woff;
141
142	len = write(fd, wbuf + woff, len);
143	if (len == -1) {
144		fprintf(stderr, "%s: write\n", __func__);
145		if (usepersist)
146			event_del(ev);
147		return;
148	}
149
150	woff += len;
151
152	if (woff >= sizeof(wbuf)) {
153		shutdown(fd, SHUT_WR);
154		if (usepersist)
155			event_del(ev);
156		return;
157	}
158
159	if (!usepersist) {
160		if (event_add(ev, NULL) == -1)
161			exit(1);
162	}
163}
164
165static void
166multiple_read_cb(int fd, short event, void *arg)
167{
168	struct event *ev = arg;
169	int len;
170
171	len = read(fd, rbuf + roff, sizeof(rbuf) - roff);
172	if (len == -1)
173		fprintf(stderr, "%s: read\n", __func__);
174	if (len <= 0) {
175		if (usepersist)
176			event_del(ev);
177		return;
178	}
179
180	roff += len;
181	if (!usepersist) {
182		if (event_add(ev, NULL) == -1)
183			exit(1);
184	}
185}
186
187static void
188calibrate(void)
189{
190	struct timeval start, end, diff;
191	int delta;
192
193	gettimeofday(&start, NULL);
194	sleep(SECONDS);
195	gettimeofday(&end, NULL);
196
197	timersub(&end, &start, &diff);
198
199	delta = diff.tv_sec * 1000 + diff.tv_usec / 1000;
200	if (delta < 0)
201		delta = -delta;
202
203	interval = delta;
204	fprintf(stdout, "Calibrated interval: %d sec = %d msec\n", SECONDS,
205		interval);
206}
207
208static void
209timeout_cb(int fd, short event, void *arg)
210{
211	struct timeval tv;
212	int diff;
213
214	evutil_gettimeofday(&tcalled, NULL);
215	if (evutil_timercmp(&tcalled, &tset, >))
216		evutil_timersub(&tcalled, &tset, &tv);
217	else
218		evutil_timersub(&tset, &tcalled, &tv);
219
220	diff = tv.tv_sec*1000 + tv.tv_usec/1000 - interval;
221	if (diff < 0)
222		diff = -diff;
223
224	if (diff < 100)
225		test_ok = 1;
226}
227
228#ifndef WIN32
229static void
230signal_cb_sa(int sig)
231{
232	test_ok = 2;
233}
234
235static void
236signal_cb(int fd, short event, void *arg)
237{
238	struct event *ev = arg;
239
240	signal_del(ev);
241	test_ok = 1;
242}
243#endif
244
245struct both {
246	struct event ev;
247	int nread;
248};
249
250static void
251combined_read_cb(int fd, short event, void *arg)
252{
253	struct both *both = arg;
254	char buf[128];
255	int len;
256
257	len = read(fd, buf, sizeof(buf));
258	if (len == -1)
259		fprintf(stderr, "%s: read\n", __func__);
260	if (len <= 0)
261		return;
262
263	both->nread += len;
264	if (event_add(&both->ev, NULL) == -1)
265		exit(1);
266}
267
268static void
269combined_write_cb(int fd, short event, void *arg)
270{
271	struct both *both = arg;
272	char buf[128];
273	int len;
274
275	len = sizeof(buf);
276	if (len > both->nread)
277		len = both->nread;
278
279	len = write(fd, buf, len);
280	if (len == -1)
281		fprintf(stderr, "%s: write\n", __func__);
282	if (len <= 0) {
283		shutdown(fd, SHUT_WR);
284		return;
285	}
286
287	both->nread -= len;
288	if (event_add(&both->ev, NULL) == -1)
289		exit(1);
290}
291
292/* Test infrastructure */
293
294static int
295setup_test(const char *name)
296{
297
298	fprintf(stdout, "%s", name);
299
300	if (evutil_socketpair(AF_UNIX, SOCK_STREAM, 0, pair) == -1) {
301		fprintf(stderr, "%s: socketpair\n", __func__);
302		exit(1);
303	}
304
305#ifdef HAVE_FCNTL
306        if (fcntl(pair[0], F_SETFL, O_NONBLOCK) == -1)
307		fprintf(stderr, "fcntl(O_NONBLOCK)");
308
309        if (fcntl(pair[1], F_SETFL, O_NONBLOCK) == -1)
310		fprintf(stderr, "fcntl(O_NONBLOCK)");
311#endif
312
313	test_ok = 0;
314	called = 0;
315	return (0);
316}
317
318static int
319cleanup_test(void)
320{
321#ifndef WIN32
322	close(pair[0]);
323	close(pair[1]);
324#else
325	CloseHandle((HANDLE)pair[0]);
326	CloseHandle((HANDLE)pair[1]);
327#endif
328	if (test_ok)
329		fprintf(stdout, "OK\n");
330	else {
331		fprintf(stdout, "FAILED\n");
332		exit(1);
333	}
334        test_ok = 0;
335	return (0);
336}
337
338static void
339test_registerfds(void)
340{
341	int i, j;
342	int pair[2];
343	struct event read_evs[512];
344	struct event write_evs[512];
345
346	struct event_base *base = event_base_new();
347
348	fprintf(stdout, "Testing register fds: ");
349
350	for (i = 0; i < 512; ++i) {
351		if (evutil_socketpair(AF_UNIX, SOCK_STREAM, 0, pair) == -1) {
352			/* run up to the limit of file descriptors */
353			break;
354		}
355		event_set(&read_evs[i], pair[0],
356		    EV_READ|EV_PERSIST, simple_read_cb, NULL);
357		event_base_set(base, &read_evs[i]);
358		event_add(&read_evs[i], NULL);
359		event_set(&write_evs[i], pair[1],
360		    EV_WRITE|EV_PERSIST, simple_write_cb, NULL);
361		event_base_set(base, &write_evs[i]);
362		event_add(&write_evs[i], NULL);
363
364		/* just loop once */
365		event_base_loop(base, EVLOOP_ONCE);
366	}
367
368	/* now delete everything */
369	for (j = 0; j < i; ++j) {
370		event_del(&read_evs[j]);
371		event_del(&write_evs[j]);
372#ifndef WIN32
373		close(read_evs[j].ev_fd);
374		close(write_evs[j].ev_fd);
375#else
376		CloseHandle((HANDLE)read_evs[j].ev_fd);
377		CloseHandle((HANDLE)write_evs[j].ev_fd);
378#endif
379
380		/* just loop once */
381		event_base_loop(base, EVLOOP_ONCE);
382	}
383
384	event_base_free(base);
385
386	fprintf(stdout, "OK\n");
387}
388
389static void
390test_simpleread(void)
391{
392	struct event ev;
393
394	/* Very simple read test */
395	setup_test("Simple read: ");
396
397	write(pair[0], TEST1, strlen(TEST1)+1);
398	shutdown(pair[0], SHUT_WR);
399
400	event_set(&ev, pair[1], EV_READ, simple_read_cb, &ev);
401	if (event_add(&ev, NULL) == -1)
402		exit(1);
403	event_dispatch();
404
405	cleanup_test();
406}
407
408static void
409test_simplewrite(void)
410{
411	struct event ev;
412
413	/* Very simple write test */
414	setup_test("Simple write: ");
415
416	event_set(&ev, pair[0], EV_WRITE, simple_write_cb, &ev);
417	if (event_add(&ev, NULL) == -1)
418		exit(1);
419	event_dispatch();
420
421	cleanup_test();
422}
423
424static void
425test_multiple(void)
426{
427	struct event ev, ev2;
428	int i;
429
430	/* Multiple read and write test */
431	setup_test("Multiple read/write: ");
432	memset(rbuf, 0, sizeof(rbuf));
433	for (i = 0; i < sizeof(wbuf); i++)
434		wbuf[i] = i;
435
436	roff = woff = 0;
437	usepersist = 0;
438
439	event_set(&ev, pair[0], EV_WRITE, multiple_write_cb, &ev);
440	if (event_add(&ev, NULL) == -1)
441		exit(1);
442	event_set(&ev2, pair[1], EV_READ, multiple_read_cb, &ev2);
443	if (event_add(&ev2, NULL) == -1)
444		exit(1);
445	event_dispatch();
446
447	if (roff == woff)
448		test_ok = memcmp(rbuf, wbuf, sizeof(wbuf)) == 0;
449
450	cleanup_test();
451}
452
453static void
454test_persistent(void)
455{
456	struct event ev, ev2;
457	int i;
458
459	/* Multiple read and write test with persist */
460	setup_test("Persist read/write: ");
461	memset(rbuf, 0, sizeof(rbuf));
462	for (i = 0; i < sizeof(wbuf); i++)
463		wbuf[i] = i;
464
465	roff = woff = 0;
466	usepersist = 1;
467
468	event_set(&ev, pair[0], EV_WRITE|EV_PERSIST, multiple_write_cb, &ev);
469	if (event_add(&ev, NULL) == -1)
470		exit(1);
471	event_set(&ev2, pair[1], EV_READ|EV_PERSIST, multiple_read_cb, &ev2);
472	if (event_add(&ev2, NULL) == -1)
473		exit(1);
474	event_dispatch();
475
476	if (roff == woff)
477		test_ok = memcmp(rbuf, wbuf, sizeof(wbuf)) == 0;
478
479	cleanup_test();
480}
481
482static void
483test_combined(void)
484{
485	struct both r1, r2, w1, w2;
486
487	setup_test("Combined read/write: ");
488	memset(&r1, 0, sizeof(r1));
489	memset(&r2, 0, sizeof(r2));
490	memset(&w1, 0, sizeof(w1));
491	memset(&w2, 0, sizeof(w2));
492
493	w1.nread = 4096;
494	w2.nread = 8192;
495
496	event_set(&r1.ev, pair[0], EV_READ, combined_read_cb, &r1);
497	event_set(&w1.ev, pair[0], EV_WRITE, combined_write_cb, &w1);
498	event_set(&r2.ev, pair[1], EV_READ, combined_read_cb, &r2);
499	event_set(&w2.ev, pair[1], EV_WRITE, combined_write_cb, &w2);
500	if (event_add(&r1.ev, NULL) == -1)
501		exit(1);
502	if (event_add(&w1.ev, NULL))
503		exit(1);
504	if (event_add(&r2.ev, NULL))
505		exit(1);
506	if (event_add(&w2.ev, NULL))
507		exit(1);
508
509	event_dispatch();
510
511	if (r1.nread == 8192 && r2.nread == 4096)
512		test_ok = 1;
513
514	cleanup_test();
515}
516
517static void
518test_simpletimeout(void)
519{
520	struct timeval tv;
521	struct event ev;
522
523	setup_test("Simple timeout: ");
524
525	tv.tv_usec = 0;
526	tv.tv_sec = SECONDS;
527	evtimer_set(&ev, timeout_cb, NULL);
528	evtimer_add(&ev, &tv);
529
530	evutil_gettimeofday(&tset, NULL);
531	event_dispatch();
532
533	cleanup_test();
534}
535
536#ifndef WIN32
537extern struct event_base *current_base;
538
539static void
540child_signal_cb(int fd, short event, void *arg)
541{
542	struct timeval tv;
543	int *pint = arg;
544
545	*pint = 1;
546
547	tv.tv_usec = 500000;
548	tv.tv_sec = 0;
549	event_loopexit(&tv);
550}
551
552static void
553test_fork(void)
554{
555	int status, got_sigchld = 0;
556	struct event ev, sig_ev;
557	pid_t pid;
558
559	setup_test("After fork: ");
560
561	write(pair[0], TEST1, strlen(TEST1)+1);
562
563	event_set(&ev, pair[1], EV_READ, simple_read_cb, &ev);
564	if (event_add(&ev, NULL) == -1)
565		exit(1);
566
567	signal_set(&sig_ev, SIGCHLD, child_signal_cb, &got_sigchld);
568	signal_add(&sig_ev, NULL);
569
570	if ((pid = fork()) == 0) {
571		/* in the child */
572		if (event_reinit(current_base) == -1) {
573			fprintf(stderr, "FAILED (reinit)\n");
574			exit(1);
575		}
576
577		signal_del(&sig_ev);
578
579		called = 0;
580
581		event_dispatch();
582
583		/* we do not send an EOF; simple_read_cb requires an EOF
584		 * to set test_ok.  we just verify that the callback was
585		 * called. */
586		exit(test_ok != 0 || called != 2 ? -2 : 76);
587	}
588
589	/* wait for the child to read the data */
590	sleep(1);
591
592	write(pair[0], TEST1, strlen(TEST1)+1);
593
594	if (waitpid(pid, &status, 0) == -1) {
595		fprintf(stderr, "FAILED (fork)\n");
596		exit(1);
597	}
598
599	if (WEXITSTATUS(status) != 76) {
600		fprintf(stderr, "FAILED (exit): %d\n", WEXITSTATUS(status));
601		exit(1);
602	}
603
604	/* test that the current event loop still works */
605	write(pair[0], TEST1, strlen(TEST1)+1);
606	shutdown(pair[0], SHUT_WR);
607
608	event_dispatch();
609
610	if (!got_sigchld) {
611		fprintf(stdout, "FAILED (sigchld)\n");
612		exit(1);
613	}
614
615	signal_del(&sig_ev);
616
617	cleanup_test();
618}
619
620static void
621test_simplesignal(void)
622{
623	struct event ev;
624	struct itimerval itv;
625
626	setup_test("Simple signal: ");
627	signal_set(&ev, SIGALRM, signal_cb, &ev);
628	signal_add(&ev, NULL);
629	/* find bugs in which operations are re-ordered */
630	signal_del(&ev);
631	signal_add(&ev, NULL);
632
633	memset(&itv, 0, sizeof(itv));
634	itv.it_value.tv_sec = 1;
635	if (setitimer(ITIMER_REAL, &itv, NULL) == -1)
636		goto skip_simplesignal;
637
638	event_dispatch();
639 skip_simplesignal:
640	if (signal_del(&ev) == -1)
641		test_ok = 0;
642
643	cleanup_test();
644}
645
646static void
647test_multiplesignal(void)
648{
649	struct event ev_one, ev_two;
650	struct itimerval itv;
651
652	setup_test("Multiple signal: ");
653
654	signal_set(&ev_one, SIGALRM, signal_cb, &ev_one);
655	signal_add(&ev_one, NULL);
656
657	signal_set(&ev_two, SIGALRM, signal_cb, &ev_two);
658	signal_add(&ev_two, NULL);
659
660	memset(&itv, 0, sizeof(itv));
661	itv.it_value.tv_sec = 1;
662	if (setitimer(ITIMER_REAL, &itv, NULL) == -1)
663		goto skip_simplesignal;
664
665	event_dispatch();
666
667 skip_simplesignal:
668	if (signal_del(&ev_one) == -1)
669		test_ok = 0;
670	if (signal_del(&ev_two) == -1)
671		test_ok = 0;
672
673	cleanup_test();
674}
675
676static void
677test_immediatesignal(void)
678{
679	struct event ev;
680
681	test_ok = 0;
682	printf("Immediate signal: ");
683	signal_set(&ev, SIGUSR1, signal_cb, &ev);
684	signal_add(&ev, NULL);
685	raise(SIGUSR1);
686	event_loop(EVLOOP_NONBLOCK);
687	signal_del(&ev);
688	cleanup_test();
689}
690
691static void
692test_signal_dealloc(void)
693{
694	/* make sure that signal_event is event_del'ed and pipe closed */
695	struct event ev;
696	struct event_base *base = event_init();
697	printf("Signal dealloc: ");
698	signal_set(&ev, SIGUSR1, signal_cb, &ev);
699	signal_add(&ev, NULL);
700	signal_del(&ev);
701	event_base_free(base);
702        /* If we got here without asserting, we're fine. */
703        test_ok = 1;
704	cleanup_test();
705}
706
707static void
708test_signal_pipeloss(void)
709{
710	/* make sure that the base1 pipe is closed correctly. */
711	struct event_base *base1, *base2;
712	int pipe1;
713	test_ok = 0;
714	printf("Signal pipeloss: ");
715	base1 = event_init();
716	pipe1 = base1->sig.ev_signal_pair[0];
717	base2 = event_init();
718	event_base_free(base2);
719	event_base_free(base1);
720	if (close(pipe1) != -1 || errno!=EBADF) {
721		/* fd must be closed, so second close gives -1, EBADF */
722		printf("signal pipe not closed. ");
723		test_ok = 0;
724	} else {
725		test_ok = 1;
726	}
727	cleanup_test();
728}
729
730/*
731 * make two bases to catch signals, use both of them.  this only works
732 * for event mechanisms that use our signal pipe trick.  kqueue handles
733 * signals internally, and all interested kqueues get all the signals.
734 */
735static void
736test_signal_switchbase(void)
737{
738	struct event ev1, ev2;
739	struct event_base *base1, *base2;
740        int is_kqueue;
741	test_ok = 0;
742	printf("Signal switchbase: ");
743	base1 = event_init();
744	base2 = event_init();
745        is_kqueue = !strcmp(event_get_method(),"kqueue");
746	signal_set(&ev1, SIGUSR1, signal_cb, &ev1);
747	signal_set(&ev2, SIGUSR1, signal_cb, &ev2);
748	if (event_base_set(base1, &ev1) ||
749	    event_base_set(base2, &ev2) ||
750	    event_add(&ev1, NULL) ||
751	    event_add(&ev2, NULL)) {
752		fprintf(stderr, "%s: cannot set base, add\n", __func__);
753		exit(1);
754	}
755
756	test_ok = 0;
757	/* can handle signal before loop is called */
758	raise(SIGUSR1);
759	event_base_loop(base2, EVLOOP_NONBLOCK);
760        if (is_kqueue) {
761                if (!test_ok)
762                        goto done;
763                test_ok = 0;
764        }
765	event_base_loop(base1, EVLOOP_NONBLOCK);
766	if (test_ok && !is_kqueue) {
767		test_ok = 0;
768
769		/* set base1 to handle signals */
770		event_base_loop(base1, EVLOOP_NONBLOCK);
771		raise(SIGUSR1);
772		event_base_loop(base1, EVLOOP_NONBLOCK);
773		event_base_loop(base2, EVLOOP_NONBLOCK);
774	}
775 done:
776	event_base_free(base1);
777	event_base_free(base2);
778	cleanup_test();
779}
780
781/*
782 * assert that a signal event removed from the event queue really is
783 * removed - with no possibility of it's parent handler being fired.
784 */
785static void
786test_signal_assert(void)
787{
788	struct event ev;
789	struct event_base *base = event_init();
790	test_ok = 0;
791	printf("Signal handler assert: ");
792	/* use SIGCONT so we don't kill ourselves when we signal to nowhere */
793	signal_set(&ev, SIGCONT, signal_cb, &ev);
794	signal_add(&ev, NULL);
795	/*
796	 * if signal_del() fails to reset the handler, it's current handler
797	 * will still point to evsignal_handler().
798	 */
799	signal_del(&ev);
800
801	raise(SIGCONT);
802	/* only way to verify we were in evsignal_handler() */
803	if (base->sig.evsignal_caught)
804		test_ok = 0;
805	else
806		test_ok = 1;
807
808	event_base_free(base);
809	cleanup_test();
810	return;
811}
812
813/*
814 * assert that we restore our previous signal handler properly.
815 */
816static void
817test_signal_restore(void)
818{
819	struct event ev;
820	struct event_base *base = event_init();
821#ifdef HAVE_SIGACTION
822	struct sigaction sa;
823#endif
824
825	test_ok = 0;
826	printf("Signal handler restore: ");
827#ifdef HAVE_SIGACTION
828	sa.sa_handler = signal_cb_sa;
829	sa.sa_flags = 0x0;
830	sigemptyset(&sa.sa_mask);
831	if (sigaction(SIGUSR1, &sa, NULL) == -1)
832		goto out;
833#else
834	if (signal(SIGUSR1, signal_cb_sa) == SIG_ERR)
835		goto out;
836#endif
837	signal_set(&ev, SIGUSR1, signal_cb, &ev);
838	signal_add(&ev, NULL);
839	signal_del(&ev);
840
841	raise(SIGUSR1);
842	/* 1 == signal_cb, 2 == signal_cb_sa, we want our previous handler */
843	if (test_ok != 2)
844		test_ok = 0;
845out:
846	event_base_free(base);
847	cleanup_test();
848	return;
849}
850
851static void
852signal_cb_swp(int sig, short event, void *arg)
853{
854	called++;
855	if (called < 5)
856		raise(sig);
857	else
858		event_loopexit(NULL);
859}
860static void
861timeout_cb_swp(int fd, short event, void *arg)
862{
863	if (called == -1) {
864		struct timeval tv = {5, 0};
865
866		called = 0;
867		evtimer_add((struct event *)arg, &tv);
868		raise(SIGUSR1);
869		return;
870	}
871	test_ok = 0;
872	event_loopexit(NULL);
873}
874
875static void
876test_signal_while_processing(void)
877{
878	struct event_base *base = event_init();
879	struct event ev, ev_timer;
880	struct timeval tv = {0, 0};
881
882	setup_test("Receiving a signal while processing other signal: ");
883
884	called = -1;
885	test_ok = 1;
886	signal_set(&ev, SIGUSR1, signal_cb_swp, NULL);
887	signal_add(&ev, NULL);
888	evtimer_set(&ev_timer, timeout_cb_swp, &ev_timer);
889	evtimer_add(&ev_timer, &tv);
890	event_dispatch();
891
892	event_base_free(base);
893	cleanup_test();
894	return;
895}
896#endif
897
898static void
899test_free_active_base(void)
900{
901	struct event_base *base1;
902	struct event ev1;
903	setup_test("Free active base: ");
904	base1 = event_init();
905	event_set(&ev1, pair[1], EV_READ, simple_read_cb, &ev1);
906	event_base_set(base1, &ev1);
907	event_add(&ev1, NULL);
908	/* event_del(&ev1); */
909	event_base_free(base1);
910	test_ok = 1;
911	cleanup_test();
912}
913
914static void
915test_event_base_new(void)
916{
917	struct event_base *base;
918	struct event ev1;
919	setup_test("Event base new: ");
920
921	write(pair[0], TEST1, strlen(TEST1)+1);
922	shutdown(pair[0], SHUT_WR);
923
924	base = event_base_new();
925	event_set(&ev1, pair[1], EV_READ, simple_read_cb, &ev1);
926	event_base_set(base, &ev1);
927	event_add(&ev1, NULL);
928
929	event_base_dispatch(base);
930
931	event_base_free(base);
932	test_ok = 1;
933	cleanup_test();
934}
935
936static void
937test_loopexit(void)
938{
939	struct timeval tv, tv_start, tv_end;
940	struct event ev;
941
942	setup_test("Loop exit: ");
943
944	tv.tv_usec = 0;
945	tv.tv_sec = 60*60*24;
946	evtimer_set(&ev, timeout_cb, NULL);
947	evtimer_add(&ev, &tv);
948
949	tv.tv_usec = 0;
950	tv.tv_sec = SECONDS;
951	event_loopexit(&tv);
952
953	evutil_gettimeofday(&tv_start, NULL);
954	event_dispatch();
955	evutil_gettimeofday(&tv_end, NULL);
956	evutil_timersub(&tv_end, &tv_start, &tv_end);
957
958	evtimer_del(&ev);
959
960	if (tv.tv_sec < 2)
961		test_ok = 1;
962
963	cleanup_test();
964}
965
966static void
967test_loopexit_multiple(void)
968{
969	struct timeval tv;
970	struct event_base *base;
971
972	setup_test("Loop Multiple exit: ");
973
974	base = event_base_new();
975
976	tv.tv_usec = 0;
977	tv.tv_sec = 1;
978	event_base_loopexit(base, &tv);
979
980	tv.tv_usec = 0;
981	tv.tv_sec = 2;
982	event_base_loopexit(base, &tv);
983
984	event_base_dispatch(base);
985
986	event_base_free(base);
987
988	test_ok = 1;
989
990	cleanup_test();
991}
992
993static void
994break_cb(int fd, short events, void *arg)
995{
996	test_ok = 1;
997	event_loopbreak();
998}
999
1000static void
1001fail_cb(int fd, short events, void *arg)
1002{
1003	test_ok = 0;
1004}
1005
1006static void
1007test_loopbreak(void)
1008{
1009	struct event ev1, ev2;
1010	struct timeval tv;
1011
1012	setup_test("Loop break: ");
1013
1014	tv.tv_sec = 0;
1015	tv.tv_usec = 0;
1016	evtimer_set(&ev1, break_cb, NULL);
1017	evtimer_add(&ev1, &tv);
1018	evtimer_set(&ev2, fail_cb, NULL);
1019	evtimer_add(&ev2, &tv);
1020
1021	event_dispatch();
1022
1023	evtimer_del(&ev1);
1024	evtimer_del(&ev2);
1025
1026	cleanup_test();
1027}
1028
1029static void
1030test_evbuffer(void) {
1031
1032	struct evbuffer *evb = evbuffer_new();
1033	setup_test("Testing Evbuffer: ");
1034
1035	evbuffer_add_printf(evb, "%s/%d", "hello", 1);
1036
1037	if (EVBUFFER_LENGTH(evb) == 7 &&
1038	    strcmp((char*)EVBUFFER_DATA(evb), "hello/1") == 0)
1039	    test_ok = 1;
1040
1041	evbuffer_free(evb);
1042
1043	cleanup_test();
1044}
1045
1046static void
1047test_evbuffer_find(void)
1048{
1049	u_char* p;
1050	const char* test1 = "1234567890\r\n";
1051	const char* test2 = "1234567890\r";
1052#define EVBUFFER_INITIAL_LENGTH 256
1053	char test3[EVBUFFER_INITIAL_LENGTH];
1054	unsigned int i;
1055	struct evbuffer * buf = evbuffer_new();
1056
1057	/* make sure evbuffer_find doesn't match past the end of the buffer */
1058	fprintf(stdout, "Testing evbuffer_find 1: ");
1059	evbuffer_add(buf, (u_char*)test1, strlen(test1));
1060	evbuffer_drain(buf, strlen(test1));
1061	evbuffer_add(buf, (u_char*)test2, strlen(test2));
1062	p = evbuffer_find(buf, (u_char*)"\r\n", 2);
1063	if (p == NULL) {
1064		fprintf(stdout, "OK\n");
1065	} else {
1066		fprintf(stdout, "FAILED\n");
1067		exit(1);
1068	}
1069
1070	/*
1071	 * drain the buffer and do another find; in r309 this would
1072	 * read past the allocated buffer causing a valgrind error.
1073	 */
1074	fprintf(stdout, "Testing evbuffer_find 2: ");
1075	evbuffer_drain(buf, strlen(test2));
1076	for (i = 0; i < EVBUFFER_INITIAL_LENGTH; ++i)
1077		test3[i] = 'a';
1078	test3[EVBUFFER_INITIAL_LENGTH - 1] = 'x';
1079	evbuffer_add(buf, (u_char *)test3, EVBUFFER_INITIAL_LENGTH);
1080	p = evbuffer_find(buf, (u_char *)"xy", 2);
1081	if (p == NULL) {
1082		printf("OK\n");
1083	} else {
1084		fprintf(stdout, "FAILED\n");
1085		exit(1);
1086	}
1087
1088	/* simple test for match at end of allocated buffer */
1089	fprintf(stdout, "Testing evbuffer_find 3: ");
1090	p = evbuffer_find(buf, (u_char *)"ax", 2);
1091	if (p != NULL && strncmp((char*)p, "ax", 2) == 0) {
1092		printf("OK\n");
1093	} else {
1094		fprintf(stdout, "FAILED\n");
1095		exit(1);
1096	}
1097
1098	evbuffer_free(buf);
1099}
1100
1101/*
1102 * simple bufferevent test
1103 */
1104
1105static void
1106readcb(struct bufferevent *bev, void *arg)
1107{
1108	if (EVBUFFER_LENGTH(bev->input) == 8333) {
1109		bufferevent_disable(bev, EV_READ);
1110		test_ok++;
1111	}
1112}
1113
1114static void
1115writecb(struct bufferevent *bev, void *arg)
1116{
1117	if (EVBUFFER_LENGTH(bev->output) == 0)
1118		test_ok++;
1119}
1120
1121static void
1122errorcb(struct bufferevent *bev, short what, void *arg)
1123{
1124	test_ok = -2;
1125}
1126
1127static void
1128test_bufferevent(void)
1129{
1130	struct bufferevent *bev1, *bev2;
1131	char buffer[8333];
1132	int i;
1133
1134	setup_test("Bufferevent: ");
1135
1136	bev1 = bufferevent_new(pair[0], readcb, writecb, errorcb, NULL);
1137	bev2 = bufferevent_new(pair[1], readcb, writecb, errorcb, NULL);
1138
1139	bufferevent_disable(bev1, EV_READ);
1140	bufferevent_enable(bev2, EV_READ);
1141
1142	for (i = 0; i < sizeof(buffer); i++)
1143		buffer[i] = i;
1144
1145	bufferevent_write(bev1, buffer, sizeof(buffer));
1146
1147	event_dispatch();
1148
1149	bufferevent_free(bev1);
1150	bufferevent_free(bev2);
1151
1152	if (test_ok != 2)
1153		test_ok = 0;
1154
1155	cleanup_test();
1156}
1157
1158/*
1159 * test watermarks and bufferevent
1160 */
1161
1162static void
1163wm_readcb(struct bufferevent *bev, void *arg)
1164{
1165	int len = EVBUFFER_LENGTH(bev->input);
1166	static int nread;
1167
1168	assert(len >= 10 && len <= 20);
1169
1170	evbuffer_drain(bev->input, len);
1171
1172	nread += len;
1173	if (nread == 65000) {
1174		bufferevent_disable(bev, EV_READ);
1175		test_ok++;
1176	}
1177}
1178
1179static void
1180wm_writecb(struct bufferevent *bev, void *arg)
1181{
1182	if (EVBUFFER_LENGTH(bev->output) == 0)
1183		test_ok++;
1184}
1185
1186static void
1187wm_errorcb(struct bufferevent *bev, short what, void *arg)
1188{
1189	test_ok = -2;
1190}
1191
1192static void
1193test_bufferevent_watermarks(void)
1194{
1195	struct bufferevent *bev1, *bev2;
1196	char buffer[65000];
1197	int i;
1198
1199	setup_test("Bufferevent Watermarks: ");
1200
1201	bev1 = bufferevent_new(pair[0], NULL, wm_writecb, wm_errorcb, NULL);
1202	bev2 = bufferevent_new(pair[1], wm_readcb, NULL, wm_errorcb, NULL);
1203
1204	bufferevent_disable(bev1, EV_READ);
1205	bufferevent_enable(bev2, EV_READ);
1206
1207	for (i = 0; i < sizeof(buffer); i++)
1208		buffer[i] = i;
1209
1210	bufferevent_write(bev1, buffer, sizeof(buffer));
1211
1212	/* limit the reading on the receiving bufferevent */
1213	bufferevent_setwatermark(bev2, EV_READ, 10, 20);
1214
1215	event_dispatch();
1216
1217	bufferevent_free(bev1);
1218	bufferevent_free(bev2);
1219
1220	if (test_ok != 2)
1221		test_ok = 0;
1222
1223	cleanup_test();
1224}
1225
1226struct test_pri_event {
1227	struct event ev;
1228	int count;
1229};
1230
1231static void
1232test_priorities_cb(int fd, short what, void *arg)
1233{
1234	struct test_pri_event *pri = arg;
1235	struct timeval tv;
1236
1237	if (pri->count == 3) {
1238		event_loopexit(NULL);
1239		return;
1240	}
1241
1242	pri->count++;
1243
1244	evutil_timerclear(&tv);
1245	event_add(&pri->ev, &tv);
1246}
1247
1248static void
1249test_priorities(int npriorities)
1250{
1251	char buf[32];
1252	struct test_pri_event one, two;
1253	struct timeval tv;
1254
1255	evutil_snprintf(buf, sizeof(buf), "Testing Priorities %d: ", npriorities);
1256	setup_test(buf);
1257
1258	event_base_priority_init(global_base, npriorities);
1259
1260	memset(&one, 0, sizeof(one));
1261	memset(&two, 0, sizeof(two));
1262
1263	timeout_set(&one.ev, test_priorities_cb, &one);
1264	if (event_priority_set(&one.ev, 0) == -1) {
1265		fprintf(stderr, "%s: failed to set priority", __func__);
1266		exit(1);
1267	}
1268
1269	timeout_set(&two.ev, test_priorities_cb, &two);
1270	if (event_priority_set(&two.ev, npriorities - 1) == -1) {
1271		fprintf(stderr, "%s: failed to set priority", __func__);
1272		exit(1);
1273	}
1274
1275	evutil_timerclear(&tv);
1276
1277	if (event_add(&one.ev, &tv) == -1)
1278		exit(1);
1279	if (event_add(&two.ev, &tv) == -1)
1280		exit(1);
1281
1282	event_dispatch();
1283
1284	event_del(&one.ev);
1285	event_del(&two.ev);
1286
1287	if (npriorities == 1) {
1288		if (one.count == 3 && two.count == 3)
1289			test_ok = 1;
1290	} else if (npriorities == 2) {
1291		/* Two is called once because event_loopexit is priority 1 */
1292		if (one.count == 3 && two.count == 1)
1293			test_ok = 1;
1294	} else {
1295		if (one.count == 3 && two.count == 0)
1296			test_ok = 1;
1297	}
1298
1299	cleanup_test();
1300}
1301
1302static void
1303test_multiple_cb(int fd, short event, void *arg)
1304{
1305	if (event & EV_READ)
1306		test_ok |= 1;
1307	else if (event & EV_WRITE)
1308		test_ok |= 2;
1309}
1310
1311static void
1312test_multiple_events_for_same_fd(void)
1313{
1314   struct event e1, e2;
1315
1316   setup_test("Multiple events for same fd: ");
1317
1318   event_set(&e1, pair[0], EV_READ, test_multiple_cb, NULL);
1319   event_add(&e1, NULL);
1320   event_set(&e2, pair[0], EV_WRITE, test_multiple_cb, NULL);
1321   event_add(&e2, NULL);
1322   event_loop(EVLOOP_ONCE);
1323   event_del(&e2);
1324   write(pair[1], TEST1, strlen(TEST1)+1);
1325   event_loop(EVLOOP_ONCE);
1326   event_del(&e1);
1327
1328   if (test_ok != 3)
1329	   test_ok = 0;
1330
1331   cleanup_test();
1332}
1333
1334int evtag_decode_int(uint32_t *pnumber, struct evbuffer *evbuf);
1335int evtag_encode_tag(struct evbuffer *evbuf, uint32_t number);
1336int evtag_decode_tag(uint32_t *pnumber, struct evbuffer *evbuf);
1337
1338static void
1339read_once_cb(int fd, short event, void *arg)
1340{
1341	char buf[256];
1342	int len;
1343
1344	len = read(fd, buf, sizeof(buf));
1345
1346	if (called) {
1347		test_ok = 0;
1348	} else if (len) {
1349		/* Assumes global pair[0] can be used for writing */
1350		write(pair[0], TEST1, strlen(TEST1)+1);
1351		test_ok = 1;
1352	}
1353
1354	called++;
1355}
1356
1357static void
1358test_want_only_once(void)
1359{
1360	struct event ev;
1361	struct timeval tv;
1362
1363	/* Very simple read test */
1364	setup_test("Want read only once: ");
1365
1366	write(pair[0], TEST1, strlen(TEST1)+1);
1367
1368	/* Setup the loop termination */
1369	evutil_timerclear(&tv);
1370	tv.tv_sec = 1;
1371	event_loopexit(&tv);
1372
1373	event_set(&ev, pair[1], EV_READ, read_once_cb, &ev);
1374	if (event_add(&ev, NULL) == -1)
1375		exit(1);
1376	event_dispatch();
1377
1378	cleanup_test();
1379}
1380
1381#define TEST_MAX_INT	6
1382
1383static void
1384evtag_int_test(void)
1385{
1386	struct evbuffer *tmp = evbuffer_new();
1387	uint32_t integers[TEST_MAX_INT] = {
1388		0xaf0, 0x1000, 0x1, 0xdeadbeef, 0x00, 0xbef000
1389	};
1390	uint32_t integer;
1391	int i;
1392
1393	for (i = 0; i < TEST_MAX_INT; i++) {
1394		int oldlen, newlen;
1395		oldlen = EVBUFFER_LENGTH(tmp);
1396		encode_int(tmp, integers[i]);
1397		newlen = EVBUFFER_LENGTH(tmp);
1398		fprintf(stdout, "\t\tencoded 0x%08x with %d bytes\n",
1399		    integers[i], newlen - oldlen);
1400	}
1401
1402	for (i = 0; i < TEST_MAX_INT; i++) {
1403		if (evtag_decode_int(&integer, tmp) == -1) {
1404			fprintf(stderr, "decode %d failed", i);
1405			exit(1);
1406		}
1407		if (integer != integers[i]) {
1408			fprintf(stderr, "got %x, wanted %x",
1409			    integer, integers[i]);
1410			exit(1);
1411		}
1412	}
1413
1414	if (EVBUFFER_LENGTH(tmp) != 0) {
1415		fprintf(stderr, "trailing data");
1416		exit(1);
1417	}
1418	evbuffer_free(tmp);
1419
1420	fprintf(stdout, "\t%s: OK\n", __func__);
1421}
1422
1423static void
1424evtag_fuzz(void)
1425{
1426	u_char buffer[4096];
1427	struct evbuffer *tmp = evbuffer_new();
1428	struct timeval tv;
1429	int i, j;
1430
1431	int not_failed = 0;
1432	for (j = 0; j < 100; j++) {
1433		for (i = 0; i < sizeof(buffer); i++)
1434			buffer[i] = rand();
1435		evbuffer_drain(tmp, -1);
1436		evbuffer_add(tmp, buffer, sizeof(buffer));
1437
1438		if (evtag_unmarshal_timeval(tmp, 0, &tv) != -1)
1439			not_failed++;
1440	}
1441
1442	/* The majority of decodes should fail */
1443	if (not_failed >= 10) {
1444		fprintf(stderr, "evtag_unmarshal should have failed");
1445		exit(1);
1446	}
1447
1448	/* Now insert some corruption into the tag length field */
1449	evbuffer_drain(tmp, -1);
1450	evutil_timerclear(&tv);
1451	tv.tv_sec = 1;
1452	evtag_marshal_timeval(tmp, 0, &tv);
1453	evbuffer_add(tmp, buffer, sizeof(buffer));
1454
1455	EVBUFFER_DATA(tmp)[1] = 0xff;
1456	if (evtag_unmarshal_timeval(tmp, 0, &tv) != -1) {
1457		fprintf(stderr, "evtag_unmarshal_timeval should have failed");
1458		exit(1);
1459	}
1460
1461	evbuffer_free(tmp);
1462
1463	fprintf(stdout, "\t%s: OK\n", __func__);
1464}
1465
1466static void
1467evtag_tag_encoding(void)
1468{
1469	struct evbuffer *tmp = evbuffer_new();
1470	uint32_t integers[TEST_MAX_INT] = {
1471		0xaf0, 0x1000, 0x1, 0xdeadbeef, 0x00, 0xbef000
1472	};
1473	uint32_t integer;
1474	int i;
1475
1476	for (i = 0; i < TEST_MAX_INT; i++) {
1477		int oldlen, newlen;
1478		oldlen = EVBUFFER_LENGTH(tmp);
1479		evtag_encode_tag(tmp, integers[i]);
1480		newlen = EVBUFFER_LENGTH(tmp);
1481		fprintf(stdout, "\t\tencoded 0x%08x with %d bytes\n",
1482		    integers[i], newlen - oldlen);
1483	}
1484
1485	for (i = 0; i < TEST_MAX_INT; i++) {
1486		if (evtag_decode_tag(&integer, tmp) == -1) {
1487			fprintf(stderr, "decode %d failed", i);
1488			exit(1);
1489		}
1490		if (integer != integers[i]) {
1491			fprintf(stderr, "got %x, wanted %x",
1492			    integer, integers[i]);
1493			exit(1);
1494		}
1495	}
1496
1497	if (EVBUFFER_LENGTH(tmp) != 0) {
1498		fprintf(stderr, "trailing data");
1499		exit(1);
1500	}
1501	evbuffer_free(tmp);
1502
1503	fprintf(stdout, "\t%s: OK\n", __func__);
1504}
1505
1506static void
1507evtag_test(void)
1508{
1509	fprintf(stdout, "Testing Tagging:\n");
1510
1511	evtag_init();
1512	evtag_int_test();
1513	evtag_fuzz();
1514
1515	evtag_tag_encoding();
1516
1517	fprintf(stdout, "OK\n");
1518}
1519
1520#ifndef WIN32
1521static void
1522rpc_test(void)
1523{
1524	struct msg *msg, *msg2;
1525	struct kill *attack;
1526	struct run *run;
1527	struct evbuffer *tmp = evbuffer_new();
1528	struct timeval tv_start, tv_end;
1529	uint32_t tag;
1530	int i;
1531
1532	fprintf(stdout, "Testing RPC: ");
1533
1534	msg = msg_new();
1535	EVTAG_ASSIGN(msg, from_name, "niels");
1536	EVTAG_ASSIGN(msg, to_name, "phoenix");
1537
1538	if (EVTAG_GET(msg, attack, &attack) == -1) {
1539		fprintf(stderr, "Failed to set kill message.\n");
1540		exit(1);
1541	}
1542
1543	EVTAG_ASSIGN(attack, weapon, "feather");
1544	EVTAG_ASSIGN(attack, action, "tickle");
1545
1546	evutil_gettimeofday(&tv_start, NULL);
1547	for (i = 0; i < 1000; ++i) {
1548		run = EVTAG_ADD(msg, run);
1549		if (run == NULL) {
1550			fprintf(stderr, "Failed to add run message.\n");
1551			exit(1);
1552		}
1553		EVTAG_ASSIGN(run, how, "very fast but with some data in it");
1554		EVTAG_ASSIGN(run, fixed_bytes,
1555		    (unsigned char*)"012345678901234567890123");
1556	}
1557
1558	if (msg_complete(msg) == -1) {
1559		fprintf(stderr, "Failed to make complete message.\n");
1560		exit(1);
1561	}
1562
1563	evtag_marshal_msg(tmp, 0xdeaf, msg);
1564
1565	if (evtag_peek(tmp, &tag) == -1) {
1566		fprintf(stderr, "Failed to peak tag.\n");
1567		exit (1);
1568	}
1569
1570	if (tag != 0xdeaf) {
1571		fprintf(stderr, "Got incorrect tag: %0x.\n", tag);
1572		exit (1);
1573	}
1574
1575	msg2 = msg_new();
1576	if (evtag_unmarshal_msg(tmp, 0xdeaf, msg2) == -1) {
1577		fprintf(stderr, "Failed to unmarshal message.\n");
1578		exit(1);
1579	}
1580
1581	evutil_gettimeofday(&tv_end, NULL);
1582	evutil_timersub(&tv_end, &tv_start, &tv_end);
1583	fprintf(stderr, "(%.1f us/add) ",
1584	    (float)tv_end.tv_sec/(float)i * 1000000.0 +
1585	    tv_end.tv_usec / (float)i);
1586
1587	if (!EVTAG_HAS(msg2, from_name) ||
1588	    !EVTAG_HAS(msg2, to_name) ||
1589	    !EVTAG_HAS(msg2, attack)) {
1590		fprintf(stderr, "Missing data structures.\n");
1591		exit(1);
1592	}
1593
1594	if (EVTAG_LEN(msg2, run) != i) {
1595		fprintf(stderr, "Wrong number of run messages.\n");
1596		exit(1);
1597	}
1598
1599	msg_free(msg);
1600	msg_free(msg2);
1601
1602	evbuffer_free(tmp);
1603
1604	fprintf(stdout, "OK\n");
1605}
1606#endif
1607
1608static void
1609test_evutil_strtoll(void)
1610{
1611        const char *s;
1612        char *endptr;
1613        setup_test("evutil_stroll: ");
1614        test_ok = 0;
1615
1616        if (evutil_strtoll("5000000000", NULL, 10) != ((ev_int64_t)5000000)*1000)
1617                goto err;
1618        if (evutil_strtoll("-5000000000", NULL, 10) != ((ev_int64_t)5000000)*-1000)
1619                goto err;
1620        s = " 99999stuff";
1621        if (evutil_strtoll(s, &endptr, 10) != (ev_int64_t)99999)
1622                goto err;
1623        if (endptr != s+6)
1624                goto err;
1625        if (evutil_strtoll("foo", NULL, 10) != 0)
1626                goto err;
1627
1628        test_ok = 1;
1629 err:
1630        cleanup_test();
1631}
1632
1633
1634int
1635main (int argc, char **argv)
1636{
1637#ifdef WIN32
1638	WORD wVersionRequested;
1639	WSADATA wsaData;
1640	int	err;
1641
1642	wVersionRequested = MAKEWORD( 2, 2 );
1643
1644	err = WSAStartup( wVersionRequested, &wsaData );
1645#endif
1646
1647#ifndef WIN32
1648	if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
1649		return (1);
1650#endif
1651	/*
1652	 * Calibrate sleep() vs elapsed real-time
1653	 */
1654	calibrate();
1655
1656	setvbuf(stdout, NULL, _IONBF, 0);
1657
1658	/* Initalize the event library */
1659	global_base = event_init();
1660
1661	test_registerfds();
1662
1663        test_evutil_strtoll();
1664
1665	/* use the global event base and need to be called first */
1666	test_priorities(1);
1667	test_priorities(2);
1668	test_priorities(3);
1669
1670	test_evbuffer();
1671	test_evbuffer_find();
1672
1673	test_bufferevent();
1674	test_bufferevent_watermarks();
1675
1676	test_free_active_base();
1677
1678	test_event_base_new();
1679
1680	http_suite();
1681
1682#ifndef WIN32
1683	rpc_suite();
1684#endif
1685
1686	dns_suite();
1687
1688#ifndef WIN32
1689	test_fork();
1690#endif
1691
1692	test_simpleread();
1693
1694	test_simplewrite();
1695
1696	test_multiple();
1697
1698	test_persistent();
1699
1700	test_combined();
1701
1702	test_simpletimeout();
1703#ifndef WIN32
1704	test_simplesignal();
1705	test_multiplesignal();
1706	test_immediatesignal();
1707#endif
1708	test_loopexit();
1709	test_loopbreak();
1710
1711	test_loopexit_multiple();
1712
1713	test_multiple_events_for_same_fd();
1714
1715	test_want_only_once();
1716
1717	evtag_test();
1718
1719#ifndef WIN32
1720	rpc_test();
1721
1722	test_signal_dealloc();
1723	test_signal_pipeloss();
1724	test_signal_switchbase();
1725	test_signal_restore();
1726	test_signal_assert();
1727	test_signal_while_processing();
1728#endif
1729
1730	return (0);
1731}
1732
1733