ipc/chromium/src/third_party/libevent/test/test-eof.c

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/ipc/chromium/src/third_party/libevent/test/test-eof.c	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,124 @@
     1.4 +/*
     1.5 + * Copyright (c) 2002-2007 Niels Provos <provos@citi.umich.edu>
     1.6 + * Copyright (c) 2007-2012 Niels Provos and Nick Mathewson
     1.7 + *
     1.8 + * Redistribution and use in source and binary forms, with or without
     1.9 + * modification, are permitted provided that the following conditions
    1.10 + * are met:
    1.11 + * 1. Redistributions of source code must retain the above copyright
    1.12 + *    notice, this list of conditions and the following disclaimer.
    1.13 + * 2. Redistributions in binary form must reproduce the above copyright
    1.14 + *    notice, this list of conditions and the following disclaimer in the
    1.15 + *    documentation and/or other materials provided with the distribution.
    1.16 + * 3. The name of the author may not be used to endorse or promote products
    1.17 + *    derived from this software without specific prior written permission.
    1.18 + *
    1.19 + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
    1.20 + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
    1.21 + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
    1.22 + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
    1.23 + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
    1.24 + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
    1.25 + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
    1.26 + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
    1.27 + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
    1.28 + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    1.29 + */
    1.30 +#include "event2/event-config.h"
    1.31 +
    1.32 +#ifdef WIN32
    1.33 +#include <winsock2.h>
    1.34 +#else
    1.35 +#include <unistd.h>
    1.36 +#endif
    1.37 +#include <sys/types.h>
    1.38 +#include <sys/stat.h>
    1.39 +#ifdef _EVENT_HAVE_SYS_TIME_H
    1.40 +#include <sys/time.h>
    1.41 +#endif
    1.42 +#ifdef _EVENT_HAVE_SYS_SOCKET_H
    1.43 +#include <sys/socket.h>
    1.44 +#endif
    1.45 +#include <fcntl.h>
    1.46 +#include <stdlib.h>
    1.47 +#include <stdio.h>
    1.48 +#include <string.h>
    1.49 +#include <errno.h>
    1.50 +
    1.51 +#include <event.h>
    1.52 +#include <evutil.h>
    1.53 +
    1.54 +#ifdef _EVENT___func__
    1.55 +#define __func__ _EVENT___func__
    1.56 +#endif
    1.57 +
    1.58 +int test_okay = 1;
    1.59 +int called = 0;
    1.60 +struct timeval timeout = {60, 0};
    1.61 +
    1.62 +static void
    1.63 +read_cb(evutil_socket_t fd, short event, void *arg)
    1.64 +{
    1.65 +	char buf[256];
    1.66 +	int len;
    1.67 +
    1.68 +	if (EV_TIMEOUT & event) {
    1.69 +		printf("%s: Timeout!\n", __func__);
    1.70 +		exit(1);
    1.71 +	}
    1.72 +
    1.73 +	len = recv(fd, buf, sizeof(buf), 0);
    1.74 +
    1.75 +	printf("%s: read %d%s\n", __func__,
    1.76 +	    len, len ? "" : " - means EOF");
    1.77 +
    1.78 +	if (len) {
    1.79 +		if (!called)
    1.80 +			event_add(arg, &timeout);
    1.81 +	} else if (called == 1)
    1.82 +		test_okay = 0;
    1.83 +
    1.84 +	called++;
    1.85 +}
    1.86 +
    1.87 +#ifndef SHUT_WR
    1.88 +#define SHUT_WR 1
    1.89 +#endif
    1.90 +
    1.91 +int
    1.92 +main(int argc, char **argv)
    1.93 +{
    1.94 +	struct event ev;
    1.95 +	const char *test = "test string";
    1.96 +	evutil_socket_t pair[2];
    1.97 +
    1.98 +#ifdef WIN32
    1.99 +	WORD wVersionRequested;
   1.100 +	WSADATA wsaData;
   1.101 +
   1.102 +	wVersionRequested = MAKEWORD(2, 2);
   1.103 +
   1.104 +	(void) WSAStartup(wVersionRequested, &wsaData);
   1.105 +#endif
   1.106 +
   1.107 +	if (evutil_socketpair(AF_UNIX, SOCK_STREAM, 0, pair) == -1)
   1.108 +		return (1);
   1.109 +
   1.110 +
   1.111 +	if (send(pair[0], test, (int)strlen(test)+1, 0) < 0)
   1.112 +		return (1);
   1.113 +	shutdown(pair[0], SHUT_WR);
   1.114 +
   1.115 +	/* Initalize the event library */
   1.116 +	event_init();
   1.117 +
   1.118 +	/* Initalize one event */
   1.119 +	event_set(&ev, pair[1], EV_READ | EV_TIMEOUT, read_cb, &ev);
   1.120 +
   1.121 +	event_add(&ev, &timeout);
   1.122 +
   1.123 +	event_dispatch();
   1.124 +
   1.125 +	return (test_okay);
   1.126 +}
   1.127 +

mercurial