15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)/*
25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * Copyright (c) 2003, 2004 Niels Provos <provos@citi.umich.edu>
35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * All rights reserved.
45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) *
55821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * Redistribution and use in source and binary forms, with or without
65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * modification, are permitted provided that the following conditions
75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * are met:
85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * 1. Redistributions of source code must retain the above copyright
95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) *    notice, this list of conditions and the following disclaimer.
105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * 2. Redistributions in binary form must reproduce the above copyright
115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) *    notice, this list of conditions and the following disclaimer in the
125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) *    documentation and/or other materials provided with the distribution.
135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * 3. The name of the author may not be used to endorse or promote products
145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) *    derived from this software without specific prior written permission.
155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) *
165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) */
275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#ifdef WIN32
295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <winsock2.h>
305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <windows.h>
315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif
325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#ifdef HAVE_CONFIG_H
345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "config.h"
355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif
365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <sys/types.h>
385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <sys/stat.h>
395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#ifdef HAVE_SYS_TIME_H
405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <sys/time.h>
415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif
425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <sys/queue.h>
435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#ifndef WIN32
445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <sys/socket.h>
455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <sys/wait.h>
465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <signal.h>
475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <unistd.h>
485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <netdb.h>
495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif
505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <assert.h>
515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <fcntl.h>
525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <signal.h>
535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <stdlib.h>
545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <stdio.h>
555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <string.h>
565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <errno.h>
575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "event.h"
595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "evutil.h"
605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "event-internal.h"
615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "log.h"
625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "regress.h"
645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#ifndef WIN32
655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "regress.gen.h"
665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif
675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)int pair[2];
695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)int test_ok;
705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)static int called;
715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)static char wbuf[4096];
725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)static char rbuf[4096];
735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)static int woff;
745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)static int roff;
755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)static int usepersist;
765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)static struct timeval tset;
775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)static struct timeval tcalled;
785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)static struct event_base *global_base;
795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define TEST1	"this is a test"
815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define SECONDS	1
825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#ifndef SHUT_WR
845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define SHUT_WR 1
855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif
865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#ifdef WIN32
885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define write(fd,buf,len) send((fd),(buf),(len),0)
895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define read(fd,buf,len) recv((fd),(buf),(len),0)
905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif
915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)static void
935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)simple_read_cb(int fd, short event, void *arg)
945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles){
955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	char buf[256];
965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	int len;
975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	if (arg == NULL)
995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		return;
1005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	len = read(fd, buf, sizeof(buf));
1025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	if (len) {
1045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		if (!called) {
1055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)			if (event_add(arg, NULL) == -1)
1065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)				exit(1);
1075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		}
1085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	} else if (called == 1)
1095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		test_ok = 1;
1105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	called++;
1125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)static void
1155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)simple_write_cb(int fd, short event, void *arg)
1165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles){
1175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	int len;
1185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	if (arg == NULL)
1205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		return;
1215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	len = write(fd, TEST1, strlen(TEST1) + 1);
1235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	if (len == -1)
1245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		test_ok = 0;
1255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	else
1265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		test_ok = 1;
1275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)static void
1305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)multiple_write_cb(int fd, short event, void *arg)
1315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles){
1325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	struct event *ev = arg;
1335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	int len;
1345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	len = 128;
1365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	if (woff + len >= sizeof(wbuf))
1375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		len = sizeof(wbuf) - woff;
1385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	len = write(fd, wbuf + woff, len);
1405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	if (len == -1) {
1415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		fprintf(stderr, "%s: write\n", __func__);
1425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		if (usepersist)
1435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)			event_del(ev);
1445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		return;
1455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	}
1465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	woff += len;
1485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	if (woff >= sizeof(wbuf)) {
1505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		shutdown(fd, SHUT_WR);
1515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		if (usepersist)
1525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)			event_del(ev);
1535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		return;
1545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	}
1555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	if (!usepersist) {
1575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		if (event_add(ev, NULL) == -1)
1585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)			exit(1);
1595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	}
1605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)static void
1635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)multiple_read_cb(int fd, short event, void *arg)
1645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles){
1655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	struct event *ev = arg;
1665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	int len;
1675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	len = read(fd, rbuf + roff, sizeof(rbuf) - roff);
1695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	if (len == -1)
1705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		fprintf(stderr, "%s: read\n", __func__);
1715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	if (len <= 0) {
1725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		if (usepersist)
1735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)			event_del(ev);
1745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		return;
1755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	}
1765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	roff += len;
1785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	if (!usepersist) {
1795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		if (event_add(ev, NULL) == -1)
1805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)			exit(1);
1815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	}
1825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)static void
1855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)timeout_cb(int fd, short event, void *arg)
1865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles){
1875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	struct timeval tv;
1885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	int diff;
1895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	evutil_gettimeofday(&tcalled, NULL);
1915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	if (evutil_timercmp(&tcalled, &tset, >))
1925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		evutil_timersub(&tcalled, &tset, &tv);
1935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	else
1945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		evutil_timersub(&tset, &tcalled, &tv);
1955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	diff = tv.tv_sec*1000 + tv.tv_usec/1000 - SECONDS * 1000;
1975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	if (diff < 0)
1985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		diff = -diff;
1995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	if (diff < 100)
2015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		test_ok = 1;
2025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
2035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#ifndef WIN32
2055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)static void
2065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)signal_cb_sa(int sig)
2075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles){
2085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	test_ok = 2;
2095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
2105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)static void
2125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)signal_cb(int fd, short event, void *arg)
2135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles){
2145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	struct event *ev = arg;
2155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	signal_del(ev);
2175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	test_ok = 1;
2185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
2195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif
2205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)struct both {
2225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	struct event ev;
2235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	int nread;
2245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)};
2255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)static void
2275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)combined_read_cb(int fd, short event, void *arg)
2285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles){
2295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	struct both *both = arg;
2305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	char buf[128];
2315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	int len;
2325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	len = read(fd, buf, sizeof(buf));
2345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	if (len == -1)
2355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		fprintf(stderr, "%s: read\n", __func__);
2365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	if (len <= 0)
2375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		return;
2385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	both->nread += len;
2405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	if (event_add(&both->ev, NULL) == -1)
2415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		exit(1);
2425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
2435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)static void
2455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)combined_write_cb(int fd, short event, void *arg)
2465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles){
2475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	struct both *both = arg;
2485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	char buf[128];
2495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	int len;
2505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	len = sizeof(buf);
2525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	if (len > both->nread)
2535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		len = both->nread;
2545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	len = write(fd, buf, len);
2565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	if (len == -1)
2575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		fprintf(stderr, "%s: write\n", __func__);
2585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	if (len <= 0) {
2595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		shutdown(fd, SHUT_WR);
2605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		return;
2615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	}
2625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	both->nread -= len;
2645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	if (event_add(&both->ev, NULL) == -1)
2655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		exit(1);
2665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
2675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)/* Test infrastructure */
2695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)static int
2715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)setup_test(const char *name)
2725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles){
2735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	fprintf(stdout, "%s", name);
2755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	if (evutil_socketpair(AF_UNIX, SOCK_STREAM, 0, pair) == -1) {
2775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		fprintf(stderr, "%s: socketpair\n", __func__);
2785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		exit(1);
2795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	}
2805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#ifdef HAVE_FCNTL
2825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        if (fcntl(pair[0], F_SETFL, O_NONBLOCK) == -1)
2835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		fprintf(stderr, "fcntl(O_NONBLOCK)");
2845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        if (fcntl(pair[1], F_SETFL, O_NONBLOCK) == -1)
2865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		fprintf(stderr, "fcntl(O_NONBLOCK)");
2875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif
2885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	test_ok = 0;
2905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	called = 0;
2915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	return (0);
2925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
2935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)static int
2955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)cleanup_test(void)
2965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles){
2975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#ifndef WIN32
2985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	close(pair[0]);
2995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	close(pair[1]);
3005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#else
3015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	CloseHandle((HANDLE)pair[0]);
3025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	CloseHandle((HANDLE)pair[1]);
3035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif
3045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	if (test_ok)
3055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		fprintf(stdout, "OK\n");
3065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	else {
3075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		fprintf(stdout, "FAILED\n");
3085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		exit(1);
3095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	}
3105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        test_ok = 0;
3115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	return (0);
3125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
3135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)static void
3155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)test_registerfds(void)
3165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles){
3175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	int i, j;
3185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	int pair[2];
3195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	struct event read_evs[512];
3205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	struct event write_evs[512];
3215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	struct event_base *base = event_base_new();
3235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	fprintf(stdout, "Testing register fds: ");
3255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	for (i = 0; i < 512; ++i) {
3275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		if (evutil_socketpair(AF_UNIX, SOCK_STREAM, 0, pair) == -1) {
3285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)			/* run up to the limit of file descriptors */
3295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)			break;
3305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		}
3315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		event_set(&read_evs[i], pair[0],
3325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		    EV_READ|EV_PERSIST, simple_read_cb, NULL);
3335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		event_base_set(base, &read_evs[i]);
3345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		event_add(&read_evs[i], NULL);
3355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		event_set(&write_evs[i], pair[1],
3365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		    EV_WRITE|EV_PERSIST, simple_write_cb, NULL);
3375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		event_base_set(base, &write_evs[i]);
3385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		event_add(&write_evs[i], NULL);
3395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		/* just loop once */
3415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		event_base_loop(base, EVLOOP_ONCE);
3425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	}
3435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	/* now delete everything */
3455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	for (j = 0; j < i; ++j) {
3465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		event_del(&read_evs[j]);
3475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		event_del(&write_evs[j]);
3485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#ifndef WIN32
3495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		close(read_evs[j].ev_fd);
3505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		close(write_evs[j].ev_fd);
3515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#else
3525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		CloseHandle((HANDLE)read_evs[j].ev_fd);
3535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		CloseHandle((HANDLE)write_evs[j].ev_fd);
3545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif
3555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		/* just loop once */
3575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		event_base_loop(base, EVLOOP_ONCE);
3585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	}
3595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	event_base_free(base);
3615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	fprintf(stdout, "OK\n");
3635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
3645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)static void
3665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)test_simpleread(void)
3675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles){
3685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	struct event ev;
3695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	/* Very simple read test */
3715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	setup_test("Simple read: ");
3725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	write(pair[0], TEST1, strlen(TEST1)+1);
3745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	shutdown(pair[0], SHUT_WR);
3755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	event_set(&ev, pair[1], EV_READ, simple_read_cb, &ev);
3775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	if (event_add(&ev, NULL) == -1)
3785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		exit(1);
3795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	event_dispatch();
3805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	cleanup_test();
3825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
3835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)static void
3855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)test_simplewrite(void)
3865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles){
3875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	struct event ev;
3885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	/* Very simple write test */
3905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	setup_test("Simple write: ");
3915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	event_set(&ev, pair[0], EV_WRITE, simple_write_cb, &ev);
3935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	if (event_add(&ev, NULL) == -1)
3945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		exit(1);
3955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	event_dispatch();
3965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	cleanup_test();
3985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
3995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)static void
4015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)test_multiple(void)
4025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles){
4035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	struct event ev, ev2;
4045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	int i;
4055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	/* Multiple read and write test */
4075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	setup_test("Multiple read/write: ");
4085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	memset(rbuf, 0, sizeof(rbuf));
4095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	for (i = 0; i < sizeof(wbuf); i++)
4105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		wbuf[i] = i;
4115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	roff = woff = 0;
4135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	usepersist = 0;
4145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	event_set(&ev, pair[0], EV_WRITE, multiple_write_cb, &ev);
4165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	if (event_add(&ev, NULL) == -1)
4175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		exit(1);
4185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	event_set(&ev2, pair[1], EV_READ, multiple_read_cb, &ev2);
4195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	if (event_add(&ev2, NULL) == -1)
4205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		exit(1);
4215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	event_dispatch();
4225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	if (roff == woff)
4245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		test_ok = memcmp(rbuf, wbuf, sizeof(wbuf)) == 0;
4255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	cleanup_test();
4275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
4285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)static void
4305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)test_persistent(void)
4315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles){
4325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	struct event ev, ev2;
4335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	int i;
4345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	/* Multiple read and write test with persist */
4365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	setup_test("Persist read/write: ");
4375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	memset(rbuf, 0, sizeof(rbuf));
4385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	for (i = 0; i < sizeof(wbuf); i++)
4395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		wbuf[i] = i;
4405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	roff = woff = 0;
4425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	usepersist = 1;
4435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	event_set(&ev, pair[0], EV_WRITE|EV_PERSIST, multiple_write_cb, &ev);
4455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	if (event_add(&ev, NULL) == -1)
4465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		exit(1);
4475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	event_set(&ev2, pair[1], EV_READ|EV_PERSIST, multiple_read_cb, &ev2);
4485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	if (event_add(&ev2, NULL) == -1)
4495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		exit(1);
4505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	event_dispatch();
4515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	if (roff == woff)
4535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		test_ok = memcmp(rbuf, wbuf, sizeof(wbuf)) == 0;
4545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	cleanup_test();
4565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
4575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)static void
4595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)test_combined(void)
4605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles){
4615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	struct both r1, r2, w1, w2;
4625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	setup_test("Combined read/write: ");
4645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	memset(&r1, 0, sizeof(r1));
4655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	memset(&r2, 0, sizeof(r2));
4665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	memset(&w1, 0, sizeof(w1));
4675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	memset(&w2, 0, sizeof(w2));
4685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	w1.nread = 4096;
4705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	w2.nread = 8192;
4715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	event_set(&r1.ev, pair[0], EV_READ, combined_read_cb, &r1);
4735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	event_set(&w1.ev, pair[0], EV_WRITE, combined_write_cb, &w1);
4745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	event_set(&r2.ev, pair[1], EV_READ, combined_read_cb, &r2);
4755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	event_set(&w2.ev, pair[1], EV_WRITE, combined_write_cb, &w2);
4765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	if (event_add(&r1.ev, NULL) == -1)
4775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		exit(1);
4785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	if (event_add(&w1.ev, NULL))
4795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		exit(1);
4805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	if (event_add(&r2.ev, NULL))
4815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		exit(1);
4825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	if (event_add(&w2.ev, NULL))
4835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		exit(1);
4845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	event_dispatch();
4865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	if (r1.nread == 8192 && r2.nread == 4096)
4885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		test_ok = 1;
4895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	cleanup_test();
4915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
4925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)static void
4945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)test_simpletimeout(void)
4955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles){
4965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	struct timeval tv;
4975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	struct event ev;
4985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	setup_test("Simple timeout: ");
5005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	tv.tv_usec = 0;
5025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	tv.tv_sec = SECONDS;
5035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	evtimer_set(&ev, timeout_cb, NULL);
5045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	evtimer_add(&ev, &tv);
5055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	evutil_gettimeofday(&tset, NULL);
5075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	event_dispatch();
5085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	cleanup_test();
5105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
5115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#ifndef WIN32
5135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)extern struct event_base *current_base;
5145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)static void
5165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)child_signal_cb(int fd, short event, void *arg)
5175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles){
5185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	struct timeval tv;
5195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	int *pint = arg;
5205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	*pint = 1;
5225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	tv.tv_usec = 500000;
5245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	tv.tv_sec = 0;
5255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	event_loopexit(&tv);
5265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
5275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)static void
5295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)test_fork(void)
5305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles){
5315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	int status, got_sigchld = 0;
5325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	struct event ev, sig_ev;
5335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	pid_t pid;
5345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	setup_test("After fork: ");
5365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	write(pair[0], TEST1, strlen(TEST1)+1);
5385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	event_set(&ev, pair[1], EV_READ, simple_read_cb, &ev);
5405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	if (event_add(&ev, NULL) == -1)
5415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		exit(1);
5425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	signal_set(&sig_ev, SIGCHLD, child_signal_cb, &got_sigchld);
5445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	signal_add(&sig_ev, NULL);
5455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	if ((pid = fork()) == 0) {
5475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		/* in the child */
5485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		if (event_reinit(current_base) == -1) {
5495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)			fprintf(stderr, "FAILED (reinit)\n");
5505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)			exit(1);
5515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		}
5525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		signal_del(&sig_ev);
5545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		called = 0;
5565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		event_dispatch();
5585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		/* we do not send an EOF; simple_read_cb requires an EOF
5605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		 * to set test_ok.  we just verify that the callback was
5615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		 * called. */
5625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		exit(test_ok != 0 || called != 2 ? -2 : 76);
5635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	}
5645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	/* wait for the child to read the data */
5665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	sleep(1);
5675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	write(pair[0], TEST1, strlen(TEST1)+1);
5695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	if (waitpid(pid, &status, 0) == -1) {
5715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		fprintf(stderr, "FAILED (fork)\n");
5725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		exit(1);
5735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	}
5745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	if (WEXITSTATUS(status) != 76) {
5765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		fprintf(stderr, "FAILED (exit): %d\n", WEXITSTATUS(status));
5775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		exit(1);
5785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	}
5795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	/* test that the current event loop still works */
5815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	write(pair[0], TEST1, strlen(TEST1)+1);
5825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	shutdown(pair[0], SHUT_WR);
5835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	event_dispatch();
5855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	if (!got_sigchld) {
5875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		fprintf(stdout, "FAILED (sigchld)\n");
5885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		exit(1);
5895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	}
5905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	signal_del(&sig_ev);
5925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	cleanup_test();
5945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
5955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)static void
5975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)test_simplesignal(void)
5985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles){
5995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	struct event ev;
6005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	struct itimerval itv;
6015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	setup_test("Simple signal: ");
6035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	signal_set(&ev, SIGALRM, signal_cb, &ev);
6045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	signal_add(&ev, NULL);
6055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	/* find bugs in which operations are re-ordered */
6065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	signal_del(&ev);
6075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	signal_add(&ev, NULL);
6085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	memset(&itv, 0, sizeof(itv));
6105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	itv.it_value.tv_sec = 1;
6115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	if (setitimer(ITIMER_REAL, &itv, NULL) == -1)
6125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		goto skip_simplesignal;
6135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	event_dispatch();
6155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) skip_simplesignal:
6165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	if (signal_del(&ev) == -1)
6175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		test_ok = 0;
6185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	cleanup_test();
6205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
6215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)static void
6235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)test_multiplesignal(void)
6245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles){
6255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	struct event ev_one, ev_two;
6265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	struct itimerval itv;
6275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	setup_test("Multiple signal: ");
6295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	signal_set(&ev_one, SIGALRM, signal_cb, &ev_one);
6315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	signal_add(&ev_one, NULL);
6325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	signal_set(&ev_two, SIGALRM, signal_cb, &ev_two);
6345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	signal_add(&ev_two, NULL);
6355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	memset(&itv, 0, sizeof(itv));
6375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	itv.it_value.tv_sec = 1;
6385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	if (setitimer(ITIMER_REAL, &itv, NULL) == -1)
6395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		goto skip_simplesignal;
6405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	event_dispatch();
6425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) skip_simplesignal:
6445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	if (signal_del(&ev_one) == -1)
6455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		test_ok = 0;
6465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	if (signal_del(&ev_two) == -1)
6475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		test_ok = 0;
6485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	cleanup_test();
6505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
6515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)static void
6535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)test_immediatesignal(void)
6545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles){
6555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	struct event ev;
6565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	test_ok = 0;
6585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	printf("Immediate signal: ");
6595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	signal_set(&ev, SIGUSR1, signal_cb, &ev);
6605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	signal_add(&ev, NULL);
6615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	raise(SIGUSR1);
6625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	event_loop(EVLOOP_NONBLOCK);
6635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	signal_del(&ev);
6645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	cleanup_test();
6655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
6665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)static void
6685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)test_signal_dealloc(void)
6695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles){
6705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	/* make sure that signal_event is event_del'ed and pipe closed */
6715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	struct event ev;
6725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	struct event_base *base = event_init();
6735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	printf("Signal dealloc: ");
6745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	signal_set(&ev, SIGUSR1, signal_cb, &ev);
6755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	signal_add(&ev, NULL);
6765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	signal_del(&ev);
6775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	event_base_free(base);
6785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        /* If we got here without asserting, we're fine. */
6795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        test_ok = 1;
6805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	cleanup_test();
6815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
6825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)static void
6845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)test_signal_pipeloss(void)
6855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles){
6865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	/* make sure that the base1 pipe is closed correctly. */
6875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	struct event_base *base1, *base2;
6885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	int pipe1;
6895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	test_ok = 0;
6905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	printf("Signal pipeloss: ");
6915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	base1 = event_init();
6925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	pipe1 = base1->sig.ev_signal_pair[0];
6935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	base2 = event_init();
6945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	event_base_free(base2);
6955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	event_base_free(base1);
6965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	if (close(pipe1) != -1 || errno!=EBADF) {
6975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		/* fd must be closed, so second close gives -1, EBADF */
6985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		printf("signal pipe not closed. ");
6995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		test_ok = 0;
7005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	} else {
7015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		test_ok = 1;
7025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	}
7035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	cleanup_test();
7045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
7055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
7065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)/*
7075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * make two bases to catch signals, use both of them.  this only works
7085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * for event mechanisms that use our signal pipe trick.  kqueue handles
7095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * signals internally, and all interested kqueues get all the signals.
7105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) */
7115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)static void
7125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)test_signal_switchbase(void)
7135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles){
7145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	struct event ev1, ev2;
7155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	struct event_base *base1, *base2;
7165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        int is_kqueue;
7175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	test_ok = 0;
7185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	printf("Signal switchbase: ");
7195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	base1 = event_init();
7205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	base2 = event_init();
7215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        is_kqueue = !strcmp(event_get_method(),"kqueue");
7225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	signal_set(&ev1, SIGUSR1, signal_cb, &ev1);
7235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	signal_set(&ev2, SIGUSR1, signal_cb, &ev2);
7245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	if (event_base_set(base1, &ev1) ||
7255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	    event_base_set(base2, &ev2) ||
7265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	    event_add(&ev1, NULL) ||
7275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	    event_add(&ev2, NULL)) {
7285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		fprintf(stderr, "%s: cannot set base, add\n", __func__);
7295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		exit(1);
7305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	}
7315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
7325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	test_ok = 0;
7335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	/* can handle signal before loop is called */
7345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	raise(SIGUSR1);
7355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	event_base_loop(base2, EVLOOP_NONBLOCK);
7365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        if (is_kqueue) {
7375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                if (!test_ok)
7385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                        goto done;
7395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                test_ok = 0;
7405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        }
7415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	event_base_loop(base1, EVLOOP_NONBLOCK);
7425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	if (test_ok && !is_kqueue) {
7435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		test_ok = 0;
7445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
7455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		/* set base1 to handle signals */
7465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		event_base_loop(base1, EVLOOP_NONBLOCK);
7475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		raise(SIGUSR1);
7485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		event_base_loop(base1, EVLOOP_NONBLOCK);
7495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		event_base_loop(base2, EVLOOP_NONBLOCK);
7505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	}
7515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) done:
7525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	event_base_free(base1);
7535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	event_base_free(base2);
7545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	cleanup_test();
7555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
7565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
7575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)/*
7585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * assert that a signal event removed from the event queue really is
7595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * removed - with no possibility of it's parent handler being fired.
7605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) */
7615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)static void
7625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)test_signal_assert(void)
7635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles){
7645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	struct event ev;
7655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	struct event_base *base = event_init();
7665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	test_ok = 0;
7675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	printf("Signal handler assert: ");
7685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	/* use SIGCONT so we don't kill ourselves when we signal to nowhere */
7695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	signal_set(&ev, SIGCONT, signal_cb, &ev);
7705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	signal_add(&ev, NULL);
7715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	/*
7725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	 * if signal_del() fails to reset the handler, it's current handler
7735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	 * will still point to evsignal_handler().
7745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	 */
7755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	signal_del(&ev);
7765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
7775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	raise(SIGCONT);
7785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	/* only way to verify we were in evsignal_handler() */
7795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	if (base->sig.evsignal_caught)
7805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		test_ok = 0;
7815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	else
7825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		test_ok = 1;
7835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
7845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	event_base_free(base);
7855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	cleanup_test();
7865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	return;
7875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
7885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
7895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)/*
7905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * assert that we restore our previous signal handler properly.
7915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) */
7925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)static void
7935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)test_signal_restore(void)
7945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles){
7955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	struct event ev;
7965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	struct event_base *base = event_init();
7975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#ifdef HAVE_SIGACTION
7985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	struct sigaction sa;
7995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif
8005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
8015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	test_ok = 0;
8025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	printf("Signal handler restore: ");
8035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#ifdef HAVE_SIGACTION
8045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	sa.sa_handler = signal_cb_sa;
8055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	sa.sa_flags = 0x0;
8065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	sigemptyset(&sa.sa_mask);
8075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	if (sigaction(SIGUSR1, &sa, NULL) == -1)
8085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		goto out;
8095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#else
8105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	if (signal(SIGUSR1, signal_cb_sa) == SIG_ERR)
8115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		goto out;
8125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif
8135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	signal_set(&ev, SIGUSR1, signal_cb, &ev);
8145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	signal_add(&ev, NULL);
8155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	signal_del(&ev);
8165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
8175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	raise(SIGUSR1);
8185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	/* 1 == signal_cb, 2 == signal_cb_sa, we want our previous handler */
8195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	if (test_ok != 2)
8205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		test_ok = 0;
8215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)out:
8225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	event_base_free(base);
8235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	cleanup_test();
8245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	return;
8255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
8265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
8275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)static void
8285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)signal_cb_swp(int sig, short event, void *arg)
8295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles){
8305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	called++;
8315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	if (called < 5)
8325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		raise(sig);
8335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	else
8345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		event_loopexit(NULL);
8355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
8365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)static void
8375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)timeout_cb_swp(int fd, short event, void *arg)
8385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles){
8395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	if (called == -1) {
8405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		struct timeval tv = {5, 0};
8415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
8425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		called = 0;
8435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		evtimer_add((struct event *)arg, &tv);
8445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		raise(SIGUSR1);
8455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		return;
8465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	}
8475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	test_ok = 0;
8485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	event_loopexit(NULL);
8495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
8505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
8515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)static void
8525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)test_signal_while_processing(void)
8535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles){
8545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	struct event_base *base = event_init();
8555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	struct event ev, ev_timer;
8565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	struct timeval tv = {0, 0};
8575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
8585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	setup_test("Receiving a signal while processing other signal: ");
8595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
8605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	called = -1;
8615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	test_ok = 1;
8625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	signal_set(&ev, SIGUSR1, signal_cb_swp, NULL);
8635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	signal_add(&ev, NULL);
8645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	evtimer_set(&ev_timer, timeout_cb_swp, &ev_timer);
8655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	evtimer_add(&ev_timer, &tv);
8665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	event_dispatch();
8675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
8685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	event_base_free(base);
8695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	cleanup_test();
8705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	return;
8715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
8725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif
8735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
8745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)static void
8755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)test_free_active_base(void)
8765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles){
8775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	struct event_base *base1;
8785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	struct event ev1;
8795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	setup_test("Free active base: ");
8805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	base1 = event_init();
8815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	event_set(&ev1, pair[1], EV_READ, simple_read_cb, &ev1);
8825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	event_base_set(base1, &ev1);
8835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	event_add(&ev1, NULL);
8845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	/* event_del(&ev1); */
8855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	event_base_free(base1);
8865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	test_ok = 1;
8875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	cleanup_test();
8885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
8895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
8905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)static void
8915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)test_event_base_new(void)
8925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles){
8935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	struct event_base *base;
8945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	struct event ev1;
8955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	setup_test("Event base new: ");
8965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
8975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	write(pair[0], TEST1, strlen(TEST1)+1);
8985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	shutdown(pair[0], SHUT_WR);
8995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
9005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	base = event_base_new();
9015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	event_set(&ev1, pair[1], EV_READ, simple_read_cb, &ev1);
9025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	event_base_set(base, &ev1);
9035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	event_add(&ev1, NULL);
9045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
9055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	event_base_dispatch(base);
9065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
9075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	event_base_free(base);
9085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	test_ok = 1;
9095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	cleanup_test();
9105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
9115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
9125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)static void
9135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)test_loopexit(void)
9145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles){
9155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	struct timeval tv, tv_start, tv_end;
9165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	struct event ev;
9175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
9185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	setup_test("Loop exit: ");
9195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
9205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	tv.tv_usec = 0;
9215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	tv.tv_sec = 60*60*24;
9225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	evtimer_set(&ev, timeout_cb, NULL);
9235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	evtimer_add(&ev, &tv);
9245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
9255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	tv.tv_usec = 0;
9265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	tv.tv_sec = 1;
9275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	event_loopexit(&tv);
9285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
9295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	evutil_gettimeofday(&tv_start, NULL);
9305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	event_dispatch();
9315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	evutil_gettimeofday(&tv_end, NULL);
9325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	evutil_timersub(&tv_end, &tv_start, &tv_end);
9335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
9345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	evtimer_del(&ev);
9355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
9365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	if (tv.tv_sec < 2)
9375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		test_ok = 1;
9385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
9395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	cleanup_test();
9405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
9415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
9425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)static void
9435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)test_loopexit_multiple(void)
9445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles){
9455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	struct timeval tv;
9465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	struct event_base *base;
9475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
9485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	setup_test("Loop Multiple exit: ");
9495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
9505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	base = event_base_new();
9515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
9525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	tv.tv_usec = 0;
9535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	tv.tv_sec = 1;
9545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	event_base_loopexit(base, &tv);
9555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
9565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	tv.tv_usec = 0;
9575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	tv.tv_sec = 2;
9585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	event_base_loopexit(base, &tv);
9595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
9605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	event_base_dispatch(base);
9615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
9625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	event_base_free(base);
9635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
9645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	test_ok = 1;
9655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
9665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	cleanup_test();
9675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
9685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
9695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)static void
9705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)break_cb(int fd, short events, void *arg)
9715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles){
9725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	test_ok = 1;
9735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	event_loopbreak();
9745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
9755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
9765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)static void
9775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)fail_cb(int fd, short events, void *arg)
9785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles){
9795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	test_ok = 0;
9805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
9815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
9825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)static void
9835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)test_loopbreak(void)
9845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles){
9855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	struct event ev1, ev2;
9865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	struct timeval tv;
9875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
9885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	setup_test("Loop break: ");
9895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
9905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	tv.tv_sec = 0;
9915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	tv.tv_usec = 0;
9925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	evtimer_set(&ev1, break_cb, NULL);
9935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	evtimer_add(&ev1, &tv);
9945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	evtimer_set(&ev2, fail_cb, NULL);
9955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	evtimer_add(&ev2, &tv);
9965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
9975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	event_dispatch();
9985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
9995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	evtimer_del(&ev1);
10005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	evtimer_del(&ev2);
10015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
10025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	cleanup_test();
10035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
10045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
10055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)static void
10065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)test_evbuffer(void) {
10075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
10085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	struct evbuffer *evb = evbuffer_new();
10095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	setup_test("Testing Evbuffer: ");
10105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
10115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	evbuffer_add_printf(evb, "%s/%d", "hello", 1);
10125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
10135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	if (EVBUFFER_LENGTH(evb) == 7 &&
10145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	    strcmp((char*)EVBUFFER_DATA(evb), "hello/1") == 0)
10155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	    test_ok = 1;
10165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
10175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	evbuffer_free(evb);
10185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
10195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	cleanup_test();
10205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
10215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
10225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)static void
10235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)test_evbuffer_find(void)
10245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles){
10255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	u_char* p;
10265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	const char* test1 = "1234567890\r\n";
10275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	const char* test2 = "1234567890\r";
10285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define EVBUFFER_INITIAL_LENGTH 256
10295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	char test3[EVBUFFER_INITIAL_LENGTH];
10305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	unsigned int i;
10315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	struct evbuffer * buf = evbuffer_new();
10325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
10335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	/* make sure evbuffer_find doesn't match past the end of the buffer */
10345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	fprintf(stdout, "Testing evbuffer_find 1: ");
10355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	evbuffer_add(buf, (u_char*)test1, strlen(test1));
10365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	evbuffer_drain(buf, strlen(test1));
10375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	evbuffer_add(buf, (u_char*)test2, strlen(test2));
10385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	p = evbuffer_find(buf, (u_char*)"\r\n", 2);
10395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	if (p == NULL) {
10405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		fprintf(stdout, "OK\n");
10415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	} else {
10425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		fprintf(stdout, "FAILED\n");
10435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		exit(1);
10445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	}
10455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
10465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	/*
10475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	 * drain the buffer and do another find; in r309 this would
10485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	 * read past the allocated buffer causing a valgrind error.
10495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	 */
10505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	fprintf(stdout, "Testing evbuffer_find 2: ");
10515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	evbuffer_drain(buf, strlen(test2));
10525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	for (i = 0; i < EVBUFFER_INITIAL_LENGTH; ++i)
10535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		test3[i] = 'a';
10545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	test3[EVBUFFER_INITIAL_LENGTH - 1] = 'x';
10555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	evbuffer_add(buf, (u_char *)test3, EVBUFFER_INITIAL_LENGTH);
10565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	p = evbuffer_find(buf, (u_char *)"xy", 2);
10575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	if (p == NULL) {
10585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		printf("OK\n");
10595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	} else {
10605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		fprintf(stdout, "FAILED\n");
10615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		exit(1);
10625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	}
10635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
10645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	/* simple test for match at end of allocated buffer */
10655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	fprintf(stdout, "Testing evbuffer_find 3: ");
10665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	p = evbuffer_find(buf, (u_char *)"ax", 2);
10675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	if (p != NULL && strncmp((char*)p, "ax", 2) == 0) {
10685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		printf("OK\n");
10695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	} else {
10705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		fprintf(stdout, "FAILED\n");
10715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		exit(1);
10725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	}
10735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
10745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	evbuffer_free(buf);
10755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
10765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
10775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)/*
10785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * simple bufferevent test
10795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) */
10805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
10815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)static void
10825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)readcb(struct bufferevent *bev, void *arg)
10835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles){
10845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	if (EVBUFFER_LENGTH(bev->input) == 8333) {
10855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		bufferevent_disable(bev, EV_READ);
10865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		test_ok++;
10875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	}
10885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
10895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
10905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)static void
10915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)writecb(struct bufferevent *bev, void *arg)
10925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles){
10935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	if (EVBUFFER_LENGTH(bev->output) == 0)
10945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		test_ok++;
10955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
10965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
10975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)static void
10985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)errorcb(struct bufferevent *bev, short what, void *arg)
10995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles){
11005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	test_ok = -2;
11015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
11025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
11035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)static void
11045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)test_bufferevent(void)
11055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles){
11065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	struct bufferevent *bev1, *bev2;
11075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	char buffer[8333];
11085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	int i;
11095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
11105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	setup_test("Bufferevent: ");
11115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
11125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	bev1 = bufferevent_new(pair[0], readcb, writecb, errorcb, NULL);
11135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	bev2 = bufferevent_new(pair[1], readcb, writecb, errorcb, NULL);
11145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
11155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	bufferevent_disable(bev1, EV_READ);
11165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	bufferevent_enable(bev2, EV_READ);
11175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
11185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	for (i = 0; i < sizeof(buffer); i++)
11195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		buffer[i] = i;
11205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
11215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	bufferevent_write(bev1, buffer, sizeof(buffer));
11225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
11235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	event_dispatch();
11245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
11255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	bufferevent_free(bev1);
11265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	bufferevent_free(bev2);
11275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
11285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	if (test_ok != 2)
11295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		test_ok = 0;
11305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
11315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	cleanup_test();
11325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
11335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
11345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)/*
11355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * test watermarks and bufferevent
11365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) */
11375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
11385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)static void
11395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)wm_readcb(struct bufferevent *bev, void *arg)
11405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles){
11415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	int len = EVBUFFER_LENGTH(bev->input);
11425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	static int nread;
11435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
11445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	assert(len >= 10 && len <= 20);
11455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
11465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	evbuffer_drain(bev->input, len);
11475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
11485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	nread += len;
11495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	if (nread == 65000) {
11505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		bufferevent_disable(bev, EV_READ);
11515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		test_ok++;
11525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	}
11535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
11545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
11555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)static void
11565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)wm_writecb(struct bufferevent *bev, void *arg)
11575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles){
11585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	if (EVBUFFER_LENGTH(bev->output) == 0)
11595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		test_ok++;
11605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
11615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
11625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)static void
11635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)wm_errorcb(struct bufferevent *bev, short what, void *arg)
11645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles){
11655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	test_ok = -2;
11665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
11675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
11685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)static void
11695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)test_bufferevent_watermarks(void)
11705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles){
11715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	struct bufferevent *bev1, *bev2;
11725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	char buffer[65000];
11735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	int i;
11745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
11755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	setup_test("Bufferevent Watermarks: ");
11765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
11775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	bev1 = bufferevent_new(pair[0], NULL, wm_writecb, wm_errorcb, NULL);
11785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	bev2 = bufferevent_new(pair[1], wm_readcb, NULL, wm_errorcb, NULL);
11795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
11805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	bufferevent_disable(bev1, EV_READ);
11815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	bufferevent_enable(bev2, EV_READ);
11825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
11835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	for (i = 0; i < sizeof(buffer); i++)
11845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		buffer[i] = i;
11855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
11865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	bufferevent_write(bev1, buffer, sizeof(buffer));
11875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
11885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	/* limit the reading on the receiving bufferevent */
11895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	bufferevent_setwatermark(bev2, EV_READ, 10, 20);
11905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
11915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	event_dispatch();
11925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
11935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	bufferevent_free(bev1);
11945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	bufferevent_free(bev2);
11955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
11965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	if (test_ok != 2)
11975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		test_ok = 0;
11985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
11995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	cleanup_test();
12005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
12015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
12025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)struct test_pri_event {
12035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	struct event ev;
12045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	int count;
12055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)};
12065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
12075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)static void
12085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)test_priorities_cb(int fd, short what, void *arg)
12095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles){
12105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	struct test_pri_event *pri = arg;
12115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	struct timeval tv;
12125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
12135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	if (pri->count == 3) {
12145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		event_loopexit(NULL);
12155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		return;
12165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	}
12175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
12185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	pri->count++;
12195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
12205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	evutil_timerclear(&tv);
12215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	event_add(&pri->ev, &tv);
12225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
12235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
12245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)static void
12255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)test_priorities(int npriorities)
12265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles){
12275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	char buf[32];
12285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	struct test_pri_event one, two;
12295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	struct timeval tv;
12305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
12315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	evutil_snprintf(buf, sizeof(buf), "Testing Priorities %d: ", npriorities);
12325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	setup_test(buf);
12335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
12345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	event_base_priority_init(global_base, npriorities);
12355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
12365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	memset(&one, 0, sizeof(one));
12375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	memset(&two, 0, sizeof(two));
12385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
12395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	timeout_set(&one.ev, test_priorities_cb, &one);
12405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	if (event_priority_set(&one.ev, 0) == -1) {
12415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		fprintf(stderr, "%s: failed to set priority", __func__);
12425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		exit(1);
12435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	}
12445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
12455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	timeout_set(&two.ev, test_priorities_cb, &two);
12465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	if (event_priority_set(&two.ev, npriorities - 1) == -1) {
12475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		fprintf(stderr, "%s: failed to set priority", __func__);
12485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		exit(1);
12495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	}
12505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
12515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	evutil_timerclear(&tv);
12525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
12535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	if (event_add(&one.ev, &tv) == -1)
12545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		exit(1);
12555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	if (event_add(&two.ev, &tv) == -1)
12565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		exit(1);
12575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
12585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	event_dispatch();
12595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
12605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	event_del(&one.ev);
12615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	event_del(&two.ev);
12625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
12635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	if (npriorities == 1) {
12645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		if (one.count == 3 && two.count == 3)
12655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)			test_ok = 1;
12665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	} else if (npriorities == 2) {
12675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		/* Two is called once because event_loopexit is priority 1 */
12685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		if (one.count == 3 && two.count == 1)
12695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)			test_ok = 1;
12705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	} else {
12715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		if (one.count == 3 && two.count == 0)
12725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)			test_ok = 1;
12735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	}
12745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
12755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	cleanup_test();
12765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
12775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
12785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)static void
12795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)test_multiple_cb(int fd, short event, void *arg)
12805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles){
12815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	if (event & EV_READ)
12825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		test_ok |= 1;
12835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	else if (event & EV_WRITE)
12845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		test_ok |= 2;
12855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
12865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
12875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)static void
12885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)test_multiple_events_for_same_fd(void)
12895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles){
12905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   struct event e1, e2;
12915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
12925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   setup_test("Multiple events for same fd: ");
12935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
12945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   event_set(&e1, pair[0], EV_READ, test_multiple_cb, NULL);
12955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   event_add(&e1, NULL);
12965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   event_set(&e2, pair[0], EV_WRITE, test_multiple_cb, NULL);
12975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   event_add(&e2, NULL);
12985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   event_loop(EVLOOP_ONCE);
12995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   event_del(&e2);
13005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   write(pair[1], TEST1, strlen(TEST1)+1);
13015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   event_loop(EVLOOP_ONCE);
13025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   event_del(&e1);
13035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
13045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   if (test_ok != 3)
13055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	   test_ok = 0;
13065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
13075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   cleanup_test();
13085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
13095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
13105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)int evtag_decode_int(uint32_t *pnumber, struct evbuffer *evbuf);
13115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)int evtag_encode_tag(struct evbuffer *evbuf, uint32_t number);
13125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)int evtag_decode_tag(uint32_t *pnumber, struct evbuffer *evbuf);
13135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
13145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)static void
13155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)read_once_cb(int fd, short event, void *arg)
13165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles){
13175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	char buf[256];
13185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	int len;
13195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
13205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	len = read(fd, buf, sizeof(buf));
13215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
13225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	if (called) {
13235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		test_ok = 0;
13245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	} else if (len) {
13255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		/* Assumes global pair[0] can be used for writing */
13265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		write(pair[0], TEST1, strlen(TEST1)+1);
13275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		test_ok = 1;
13285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	}
13295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
13305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	called++;
13315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
13325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
13335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)static void
13345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)test_want_only_once(void)
13355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles){
13365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	struct event ev;
13375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	struct timeval tv;
13385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
13395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	/* Very simple read test */
13405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	setup_test("Want read only once: ");
13415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
13425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	write(pair[0], TEST1, strlen(TEST1)+1);
13435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
13445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	/* Setup the loop termination */
13455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	evutil_timerclear(&tv);
13465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	tv.tv_sec = 1;
13475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	event_loopexit(&tv);
13485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
13495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	event_set(&ev, pair[1], EV_READ, read_once_cb, &ev);
13505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	if (event_add(&ev, NULL) == -1)
13515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		exit(1);
13525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	event_dispatch();
13535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
13545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	cleanup_test();
13555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
13565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
13575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define TEST_MAX_INT	6
13585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
13595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)static void
13605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)evtag_int_test(void)
13615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles){
13625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	struct evbuffer *tmp = evbuffer_new();
13635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	uint32_t integers[TEST_MAX_INT] = {
13645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		0xaf0, 0x1000, 0x1, 0xdeadbeef, 0x00, 0xbef000
13655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	};
13665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	uint32_t integer;
13675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	int i;
13685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
13695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	for (i = 0; i < TEST_MAX_INT; i++) {
13705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		int oldlen, newlen;
13715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		oldlen = EVBUFFER_LENGTH(tmp);
13725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		encode_int(tmp, integers[i]);
13735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		newlen = EVBUFFER_LENGTH(tmp);
13745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		fprintf(stdout, "\t\tencoded 0x%08x with %d bytes\n",
13755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		    integers[i], newlen - oldlen);
13765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	}
13775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
13785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	for (i = 0; i < TEST_MAX_INT; i++) {
13795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		if (evtag_decode_int(&integer, tmp) == -1) {
13805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)			fprintf(stderr, "decode %d failed", i);
13815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)			exit(1);
13825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		}
13835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		if (integer != integers[i]) {
13845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)			fprintf(stderr, "got %x, wanted %x",
13855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)			    integer, integers[i]);
13865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)			exit(1);
13875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		}
13885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	}
13895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
13905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	if (EVBUFFER_LENGTH(tmp) != 0) {
13915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		fprintf(stderr, "trailing data");
13925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		exit(1);
13935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	}
13945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	evbuffer_free(tmp);
13955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
13965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	fprintf(stdout, "\t%s: OK\n", __func__);
13975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
13985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
13995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)static void
14005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)evtag_fuzz(void)
14015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles){
14025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	u_char buffer[4096];
14035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	struct evbuffer *tmp = evbuffer_new();
14045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	struct timeval tv;
14055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	int i, j;
14065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
14075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	int not_failed = 0;
14085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	for (j = 0; j < 100; j++) {
14095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		for (i = 0; i < sizeof(buffer); i++)
14105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)			buffer[i] = rand();
14115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		evbuffer_drain(tmp, -1);
14125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		evbuffer_add(tmp, buffer, sizeof(buffer));
14135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
14145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		if (evtag_unmarshal_timeval(tmp, 0, &tv) != -1)
14155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)			not_failed++;
14165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	}
14175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
14185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	/* The majority of decodes should fail */
14195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	if (not_failed >= 10) {
14205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		fprintf(stderr, "evtag_unmarshal should have failed");
14215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		exit(1);
14225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	}
14235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
14245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	/* Now insert some corruption into the tag length field */
14255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	evbuffer_drain(tmp, -1);
14265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	evutil_timerclear(&tv);
14275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	tv.tv_sec = 1;
14285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	evtag_marshal_timeval(tmp, 0, &tv);
14295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	evbuffer_add(tmp, buffer, sizeof(buffer));
14305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
14315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	EVBUFFER_DATA(tmp)[1] = 0xff;
14325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	if (evtag_unmarshal_timeval(tmp, 0, &tv) != -1) {
14335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		fprintf(stderr, "evtag_unmarshal_timeval should have failed");
14345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		exit(1);
14355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	}
14365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
14375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	evbuffer_free(tmp);
14385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
14395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	fprintf(stdout, "\t%s: OK\n", __func__);
14405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
14415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
14425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)static void
14435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)evtag_tag_encoding(void)
14445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles){
14455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	struct evbuffer *tmp = evbuffer_new();
14465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	uint32_t integers[TEST_MAX_INT] = {
14475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		0xaf0, 0x1000, 0x1, 0xdeadbeef, 0x00, 0xbef000
14485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	};
14495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	uint32_t integer;
14505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	int i;
14515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
14525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	for (i = 0; i < TEST_MAX_INT; i++) {
14535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		int oldlen, newlen;
14545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		oldlen = EVBUFFER_LENGTH(tmp);
14555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		evtag_encode_tag(tmp, integers[i]);
14565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		newlen = EVBUFFER_LENGTH(tmp);
14575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		fprintf(stdout, "\t\tencoded 0x%08x with %d bytes\n",
14585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		    integers[i], newlen - oldlen);
14595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	}
14605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
14615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	for (i = 0; i < TEST_MAX_INT; i++) {
14625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		if (evtag_decode_tag(&integer, tmp) == -1) {
14635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)			fprintf(stderr, "decode %d failed", i);
14645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)			exit(1);
14655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		}
14665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		if (integer != integers[i]) {
14675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)			fprintf(stderr, "got %x, wanted %x",
14685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)			    integer, integers[i]);
14695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)			exit(1);
14705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		}
14715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	}
14725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
14735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	if (EVBUFFER_LENGTH(tmp) != 0) {
14745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		fprintf(stderr, "trailing data");
14755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		exit(1);
14765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	}
14775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	evbuffer_free(tmp);
14785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
14795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	fprintf(stdout, "\t%s: OK\n", __func__);
14805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
14815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
14825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)static void
14835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)evtag_test(void)
14845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles){
14855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	fprintf(stdout, "Testing Tagging:\n");
14865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
14875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	evtag_init();
14885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	evtag_int_test();
14895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	evtag_fuzz();
14905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
14915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	evtag_tag_encoding();
14925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
14935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	fprintf(stdout, "OK\n");
14945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
14955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
14965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#ifndef WIN32
14975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)static void
14985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)rpc_test(void)
14995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles){
15005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	struct msg *msg, *msg2;
15015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	struct kill *attack;
15025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	struct run *run;
15035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	struct evbuffer *tmp = evbuffer_new();
15045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	struct timeval tv_start, tv_end;
15055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	uint32_t tag;
15065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	int i;
15075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
15085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	fprintf(stdout, "Testing RPC: ");
15095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
15105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	msg = msg_new();
15115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	EVTAG_ASSIGN(msg, from_name, "niels");
15125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	EVTAG_ASSIGN(msg, to_name, "phoenix");
15135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
15145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	if (EVTAG_GET(msg, attack, &attack) == -1) {
15155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		fprintf(stderr, "Failed to set kill message.\n");
15165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		exit(1);
15175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	}
15185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
15195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	EVTAG_ASSIGN(attack, weapon, "feather");
15205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	EVTAG_ASSIGN(attack, action, "tickle");
15215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
15225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	evutil_gettimeofday(&tv_start, NULL);
15235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	for (i = 0; i < 1000; ++i) {
15245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		run = EVTAG_ADD(msg, run);
15255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		if (run == NULL) {
15265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)			fprintf(stderr, "Failed to add run message.\n");
15275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)			exit(1);
15285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		}
15295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		EVTAG_ASSIGN(run, how, "very fast but with some data in it");
15305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		EVTAG_ASSIGN(run, fixed_bytes,
15315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		    (unsigned char*)"012345678901234567890123");
15325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	}
15335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
15345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	if (msg_complete(msg) == -1) {
15355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		fprintf(stderr, "Failed to make complete message.\n");
15365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		exit(1);
15375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	}
15385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
15395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	evtag_marshal_msg(tmp, 0xdeaf, msg);
15405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
15415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	if (evtag_peek(tmp, &tag) == -1) {
15425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		fprintf(stderr, "Failed to peak tag.\n");
15435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		exit (1);
15445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	}
15455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
15465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	if (tag != 0xdeaf) {
15475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		fprintf(stderr, "Got incorrect tag: %0x.\n", tag);
15485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		exit (1);
15495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	}
15505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
15515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	msg2 = msg_new();
15525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	if (evtag_unmarshal_msg(tmp, 0xdeaf, msg2) == -1) {
15535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		fprintf(stderr, "Failed to unmarshal message.\n");
15545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		exit(1);
15555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	}
15565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
15575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	evutil_gettimeofday(&tv_end, NULL);
15585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	evutil_timersub(&tv_end, &tv_start, &tv_end);
15595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	fprintf(stderr, "(%.1f us/add) ",
15605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	    (float)tv_end.tv_sec/(float)i * 1000000.0 +
15615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	    tv_end.tv_usec / (float)i);
15625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
15635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	if (!EVTAG_HAS(msg2, from_name) ||
15645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	    !EVTAG_HAS(msg2, to_name) ||
15655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	    !EVTAG_HAS(msg2, attack)) {
15665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		fprintf(stderr, "Missing data structures.\n");
15675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		exit(1);
15685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	}
15695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
15705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	if (EVTAG_LEN(msg2, run) != i) {
15715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		fprintf(stderr, "Wrong number of run messages.\n");
15725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		exit(1);
15735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	}
15745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
15755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	msg_free(msg);
15765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	msg_free(msg2);
15775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
15785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	evbuffer_free(tmp);
15795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
15805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	fprintf(stdout, "OK\n");
15815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
15825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif
15835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
15845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)static void
15855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)test_evutil_strtoll(void)
15865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles){
15875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        const char *s;
15885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        char *endptr;
15895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        setup_test("evutil_stroll: ");
15905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        test_ok = 0;
15915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
15925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        if (evutil_strtoll("5000000000", NULL, 10) != ((ev_int64_t)5000000)*1000)
15935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                goto err;
15945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        if (evutil_strtoll("-5000000000", NULL, 10) != ((ev_int64_t)5000000)*-1000)
15955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                goto err;
15965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        s = " 99999stuff";
15975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        if (evutil_strtoll(s, &endptr, 10) != (ev_int64_t)99999)
15985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                goto err;
15995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        if (endptr != s+6)
16005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                goto err;
16015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        if (evutil_strtoll("foo", NULL, 10) != 0)
16025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                goto err;
16035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
16045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        test_ok = 1;
16055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) err:
16065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        cleanup_test();
16075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
16085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
16095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
16105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)int
16115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)main (int argc, char **argv)
16125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles){
16135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#ifdef WIN32
16145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	WORD wVersionRequested;
16155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	WSADATA wsaData;
16165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	int	err;
16175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
16185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	wVersionRequested = MAKEWORD( 2, 2 );
16195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
16205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	err = WSAStartup( wVersionRequested, &wsaData );
16215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif
16225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
16235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#ifndef WIN32
16245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
16255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		return (1);
16265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif
16275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	setvbuf(stdout, NULL, _IONBF, 0);
16285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
16295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	/* Initalize the event library */
16305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	global_base = event_init();
16315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
16325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	test_registerfds();
16335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
16345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        test_evutil_strtoll();
16355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
16365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	/* use the global event base and need to be called first */
16375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	test_priorities(1);
16385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	test_priorities(2);
16395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	test_priorities(3);
16405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
16415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	test_evbuffer();
16425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	test_evbuffer_find();
16435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
16445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	test_bufferevent();
16455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	test_bufferevent_watermarks();
16465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
16475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	test_free_active_base();
16485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
16495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	test_event_base_new();
16505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
16515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	http_suite();
16525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
16535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#ifndef WIN32
16545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	rpc_suite();
16555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif
16565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
16575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	dns_suite();
16585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
16595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#ifndef WIN32
16605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	test_fork();
16615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif
16625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
16635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	test_simpleread();
16645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
16655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	test_simplewrite();
16665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
16675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	test_multiple();
16685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
16695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	test_persistent();
16705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
16715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	test_combined();
16725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
16735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	test_simpletimeout();
16745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#ifndef WIN32
16755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	test_simplesignal();
16765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	test_multiplesignal();
16775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	test_immediatesignal();
16785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif
16795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	test_loopexit();
16805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	test_loopbreak();
16815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
16825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	test_loopexit_multiple();
16835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
16845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	test_multiple_events_for_same_fd();
16855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
16865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	test_want_only_once();
16875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
16885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	evtag_test();
16895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
16905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#ifndef WIN32
16915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	rpc_test();
16925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
16935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	test_signal_dealloc();
16945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	test_signal_pipeloss();
16955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	test_signal_switchbase();
16965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	test_signal_restore();
16975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	test_signal_assert();
16985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	test_signal_while_processing();
16995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif
17005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
17015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	return (0);
17025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
17035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1704