Blame libevent/test/test-eof.c

Packit e9ba0d
/*
Packit e9ba0d
 * Copyright (c) 2002-2007 Niels Provos <provos@citi.umich.edu>
Packit e9ba0d
 * Copyright (c) 2007-2012 Niels Provos and Nick Mathewson
Packit e9ba0d
 *
Packit e9ba0d
 * Redistribution and use in source and binary forms, with or without
Packit e9ba0d
 * modification, are permitted provided that the following conditions
Packit e9ba0d
 * are met:
Packit e9ba0d
 * 1. Redistributions of source code must retain the above copyright
Packit e9ba0d
 *    notice, this list of conditions and the following disclaimer.
Packit e9ba0d
 * 2. Redistributions in binary form must reproduce the above copyright
Packit e9ba0d
 *    notice, this list of conditions and the following disclaimer in the
Packit e9ba0d
 *    documentation and/or other materials provided with the distribution.
Packit e9ba0d
 * 3. The name of the author may not be used to endorse or promote products
Packit e9ba0d
 *    derived from this software without specific prior written permission.
Packit e9ba0d
 *
Packit e9ba0d
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
Packit e9ba0d
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
Packit e9ba0d
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
Packit e9ba0d
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
Packit e9ba0d
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
Packit e9ba0d
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
Packit e9ba0d
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
Packit e9ba0d
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
Packit e9ba0d
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
Packit e9ba0d
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Packit e9ba0d
 */
Packit e9ba0d
#include "event2/event-config.h"
Packit e9ba0d
Packit e9ba0d
#ifdef WIN32
Packit e9ba0d
#include <winsock2.h>
Packit e9ba0d
#else
Packit e9ba0d
#include <unistd.h>
Packit e9ba0d
#endif
Packit e9ba0d
#include <sys/types.h>
Packit e9ba0d
#include <sys/stat.h>
Packit e9ba0d
#ifdef _EVENT_HAVE_SYS_TIME_H
Packit e9ba0d
#include <sys/time.h>
Packit e9ba0d
#endif
Packit e9ba0d
#ifdef _EVENT_HAVE_SYS_SOCKET_H
Packit e9ba0d
#include <sys/socket.h>
Packit e9ba0d
#endif
Packit e9ba0d
#include <fcntl.h>
Packit e9ba0d
#include <stdlib.h>
Packit e9ba0d
#include <stdio.h>
Packit e9ba0d
#include <string.h>
Packit e9ba0d
#include <errno.h>
Packit e9ba0d
Packit e9ba0d
#include <event.h>
Packit e9ba0d
#include <evutil.h>
Packit e9ba0d
Packit e9ba0d
#ifdef _EVENT___func__
Packit e9ba0d
#define __func__ _EVENT___func__
Packit e9ba0d
#endif
Packit e9ba0d
Packit e9ba0d
int test_okay = 1;
Packit e9ba0d
int called = 0;
Packit e9ba0d
struct timeval timeout = {60, 0};
Packit e9ba0d
Packit e9ba0d
static void
Packit e9ba0d
read_cb(evutil_socket_t fd, short event, void *arg)
Packit e9ba0d
{
Packit e9ba0d
	char buf[256];
Packit e9ba0d
	int len;
Packit e9ba0d
Packit e9ba0d
	if (EV_TIMEOUT & event) {
Packit e9ba0d
		printf("%s: Timeout!\n", __func__);
Packit e9ba0d
		exit(1);
Packit e9ba0d
	}
Packit e9ba0d
Packit e9ba0d
	len = recv(fd, buf, sizeof(buf), 0);
Packit e9ba0d
Packit e9ba0d
	printf("%s: read %d%s\n", __func__,
Packit e9ba0d
	    len, len ? "" : " - means EOF");
Packit e9ba0d
Packit e9ba0d
	if (len) {
Packit e9ba0d
		if (!called)
Packit e9ba0d
			event_add(arg, &timeout);
Packit e9ba0d
	} else if (called == 1)
Packit e9ba0d
		test_okay = 0;
Packit e9ba0d
Packit e9ba0d
	called++;
Packit e9ba0d
}
Packit e9ba0d
Packit e9ba0d
#ifndef SHUT_WR
Packit e9ba0d
#define SHUT_WR 1
Packit e9ba0d
#endif
Packit e9ba0d
Packit e9ba0d
int
Packit e9ba0d
main(int argc, char **argv)
Packit e9ba0d
{
Packit e9ba0d
	struct event ev;
Packit e9ba0d
	const char *test = "test string";
Packit e9ba0d
	evutil_socket_t pair[2];
Packit e9ba0d
Packit e9ba0d
#ifdef WIN32
Packit e9ba0d
	WORD wVersionRequested;
Packit e9ba0d
	WSADATA wsaData;
Packit e9ba0d
Packit e9ba0d
	wVersionRequested = MAKEWORD(2, 2);
Packit e9ba0d
Packit e9ba0d
	(void) WSAStartup(wVersionRequested, &wsaData);
Packit e9ba0d
#endif
Packit e9ba0d
Packit e9ba0d
	if (evutil_socketpair(AF_UNIX, SOCK_STREAM, 0, pair) == -1)
Packit e9ba0d
		return (1);
Packit e9ba0d
Packit e9ba0d
Packit e9ba0d
	if (send(pair[0], test, (int)strlen(test)+1, 0) < 0)
Packit e9ba0d
		return (1);
Packit e9ba0d
	shutdown(pair[0], SHUT_WR);
Packit e9ba0d
Packit e9ba0d
	/* Initalize the event library */
Packit e9ba0d
	event_init();
Packit e9ba0d
Packit e9ba0d
	/* Initalize one event */
Packit e9ba0d
	event_set(&ev, pair[1], EV_READ | EV_TIMEOUT, read_cb, &ev;;
Packit e9ba0d
Packit e9ba0d
	event_add(&ev, &timeout);
Packit e9ba0d
Packit e9ba0d
	event_dispatch();
Packit e9ba0d
Packit e9ba0d
	return (test_okay);
Packit e9ba0d
}
Packit e9ba0d