xref: /freebsd/contrib/ntp/sntp/libevent/test/test-weof.c (revision a466cc55373fc3cf86837f09da729535b57e69a1)
12b15cb3dSCy Schubert /*
22b15cb3dSCy Schubert  * Copyright (c) 2002-2007 Niels Provos <provos@citi.umich.edu>
32b15cb3dSCy Schubert  * Copyright (c) 2007-2012 Niels Provos and Nick Mathewson
42b15cb3dSCy Schubert  *
52b15cb3dSCy Schubert  * Redistribution and use in source and binary forms, with or without
62b15cb3dSCy Schubert  * modification, are permitted provided that the following conditions
72b15cb3dSCy Schubert  * are met:
82b15cb3dSCy Schubert  * 1. Redistributions of source code must retain the above copyright
92b15cb3dSCy Schubert  *    notice, this list of conditions and the following disclaimer.
102b15cb3dSCy Schubert  * 2. Redistributions in binary form must reproduce the above copyright
112b15cb3dSCy Schubert  *    notice, this list of conditions and the following disclaimer in the
122b15cb3dSCy Schubert  *    documentation and/or other materials provided with the distribution.
132b15cb3dSCy Schubert  * 3. The name of the author may not be used to endorse or promote products
142b15cb3dSCy Schubert  *    derived from this software without specific prior written permission.
152b15cb3dSCy Schubert  *
162b15cb3dSCy Schubert  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
172b15cb3dSCy Schubert  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
182b15cb3dSCy Schubert  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
192b15cb3dSCy Schubert  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
202b15cb3dSCy Schubert  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
212b15cb3dSCy Schubert  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
222b15cb3dSCy Schubert  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
232b15cb3dSCy Schubert  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
242b15cb3dSCy Schubert  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
252b15cb3dSCy Schubert  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
262b15cb3dSCy Schubert  */
27*a466cc55SCy Schubert #include "../util-internal.h"
282b15cb3dSCy Schubert #include "event2/event-config.h"
292b15cb3dSCy Schubert 
302b15cb3dSCy Schubert #ifdef _WIN32
312b15cb3dSCy Schubert #include <winsock2.h>
322b15cb3dSCy Schubert #else
332b15cb3dSCy Schubert #include <unistd.h>
342b15cb3dSCy Schubert #endif
352b15cb3dSCy Schubert #include <sys/types.h>
362b15cb3dSCy Schubert #include <sys/stat.h>
372b15cb3dSCy Schubert #ifdef EVENT__HAVE_SYS_TIME_H
382b15cb3dSCy Schubert #include <sys/time.h>
392b15cb3dSCy Schubert #endif
402b15cb3dSCy Schubert #ifdef EVENT__HAVE_SYS_SOCKET_H
412b15cb3dSCy Schubert #include <sys/socket.h>
422b15cb3dSCy Schubert #endif
432b15cb3dSCy Schubert #include <fcntl.h>
442b15cb3dSCy Schubert #include <stdlib.h>
452b15cb3dSCy Schubert #include <stdio.h>
462b15cb3dSCy Schubert #include <string.h>
472b15cb3dSCy Schubert #include <signal.h>
482b15cb3dSCy Schubert #include <errno.h>
492b15cb3dSCy Schubert 
502b15cb3dSCy Schubert #include "event2/event.h"
512b15cb3dSCy Schubert #include "event2/event_struct.h"
522b15cb3dSCy Schubert #include "event2/event_compat.h"
532b15cb3dSCy Schubert #include "event2/util.h"
542b15cb3dSCy Schubert 
552b15cb3dSCy Schubert evutil_socket_t pair[2];
562b15cb3dSCy Schubert int test_okay = 1;
572b15cb3dSCy Schubert int called = 0;
582b15cb3dSCy Schubert 
592b15cb3dSCy Schubert static void
write_cb(evutil_socket_t fd,short event,void * arg)602b15cb3dSCy Schubert write_cb(evutil_socket_t fd, short event, void *arg)
612b15cb3dSCy Schubert {
622b15cb3dSCy Schubert 	const char *test = "test string";
632b15cb3dSCy Schubert 	int len;
642b15cb3dSCy Schubert 
652b15cb3dSCy Schubert 	len = send(fd, test, (int)strlen(test) + 1, 0);
662b15cb3dSCy Schubert 
672b15cb3dSCy Schubert 	printf("%s: write %d%s\n", __func__,
682b15cb3dSCy Schubert 	    len, len ? "" : " - means EOF");
692b15cb3dSCy Schubert 
702b15cb3dSCy Schubert 	if (len > 0) {
712b15cb3dSCy Schubert 		if (!called)
722b15cb3dSCy Schubert 			event_add(arg, NULL);
732b15cb3dSCy Schubert 		evutil_closesocket(pair[0]);
742b15cb3dSCy Schubert 	} else if (called == 1)
752b15cb3dSCy Schubert 		test_okay = 0;
762b15cb3dSCy Schubert 
772b15cb3dSCy Schubert 	called++;
782b15cb3dSCy Schubert }
792b15cb3dSCy Schubert 
802b15cb3dSCy Schubert int
main(int argc,char ** argv)812b15cb3dSCy Schubert main(int argc, char **argv)
822b15cb3dSCy Schubert {
832b15cb3dSCy Schubert 	struct event ev;
842b15cb3dSCy Schubert 
852b15cb3dSCy Schubert #ifdef _WIN32
862b15cb3dSCy Schubert 	WORD wVersionRequested;
872b15cb3dSCy Schubert 	WSADATA wsaData;
882b15cb3dSCy Schubert 
892b15cb3dSCy Schubert 	wVersionRequested = MAKEWORD(2, 2);
902b15cb3dSCy Schubert 
912b15cb3dSCy Schubert 	(void) WSAStartup(wVersionRequested, &wsaData);
922b15cb3dSCy Schubert #endif
932b15cb3dSCy Schubert 
942b15cb3dSCy Schubert #ifndef _WIN32
952b15cb3dSCy Schubert 	if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
962b15cb3dSCy Schubert 		return (1);
972b15cb3dSCy Schubert #endif
982b15cb3dSCy Schubert 
992b15cb3dSCy Schubert 	if (evutil_socketpair(AF_UNIX, SOCK_STREAM, 0, pair) == -1)
1002b15cb3dSCy Schubert 		return (1);
1012b15cb3dSCy Schubert 
102*a466cc55SCy Schubert 	/* Initialize the event library */
1032b15cb3dSCy Schubert 	event_init();
1042b15cb3dSCy Schubert 
105*a466cc55SCy Schubert 	/* Initialize one event */
1062b15cb3dSCy Schubert 	event_set(&ev, pair[1], EV_WRITE, write_cb, &ev);
1072b15cb3dSCy Schubert 
1082b15cb3dSCy Schubert 	event_add(&ev, NULL);
1092b15cb3dSCy Schubert 
1102b15cb3dSCy Schubert 	event_dispatch();
1112b15cb3dSCy Schubert 
1122b15cb3dSCy Schubert 	return (test_okay);
1132b15cb3dSCy Schubert }
1142b15cb3dSCy Schubert 
115