xref: /netbsd-src/external/bsd/libevent/dist/test/test-weof.c (revision 657871a79c9a2060a6255a242fa1a1ef76b56ec6)
1*657871a7Schristos /*	$NetBSD: test-weof.c,v 1.1.1.3 2021/04/07 02:43:15 christos Exp $	*/
26ecf6635Schristos /*
36ecf6635Schristos  * Copyright (c) 2002-2007 Niels Provos <provos@citi.umich.edu>
46ecf6635Schristos  * Copyright (c) 2007-2012 Niels Provos and Nick Mathewson
56ecf6635Schristos  *
66ecf6635Schristos  * Redistribution and use in source and binary forms, with or without
76ecf6635Schristos  * modification, are permitted provided that the following conditions
86ecf6635Schristos  * are met:
96ecf6635Schristos  * 1. Redistributions of source code must retain the above copyright
106ecf6635Schristos  *    notice, this list of conditions and the following disclaimer.
116ecf6635Schristos  * 2. Redistributions in binary form must reproduce the above copyright
126ecf6635Schristos  *    notice, this list of conditions and the following disclaimer in the
136ecf6635Schristos  *    documentation and/or other materials provided with the distribution.
146ecf6635Schristos  * 3. The name of the author may not be used to endorse or promote products
156ecf6635Schristos  *    derived from this software without specific prior written permission.
166ecf6635Schristos  *
176ecf6635Schristos  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
186ecf6635Schristos  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
196ecf6635Schristos  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
206ecf6635Schristos  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
216ecf6635Schristos  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
226ecf6635Schristos  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
236ecf6635Schristos  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
246ecf6635Schristos  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
256ecf6635Schristos  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
266ecf6635Schristos  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
276ecf6635Schristos  */
28805a1ce9Schristos #include "../util-internal.h"
296ecf6635Schristos #include "event2/event-config.h"
306ecf6635Schristos #include <sys/cdefs.h>
31*657871a7Schristos __RCSID("$NetBSD: test-weof.c,v 1.1.1.3 2021/04/07 02:43:15 christos Exp $");
326ecf6635Schristos 
33805a1ce9Schristos #ifdef _WIN32
346ecf6635Schristos #include <winsock2.h>
356ecf6635Schristos #else
366ecf6635Schristos #include <unistd.h>
376ecf6635Schristos #endif
386ecf6635Schristos #include <sys/types.h>
396ecf6635Schristos #include <sys/stat.h>
40805a1ce9Schristos #ifdef EVENT__HAVE_SYS_TIME_H
416ecf6635Schristos #include <sys/time.h>
426ecf6635Schristos #endif
43805a1ce9Schristos #ifdef EVENT__HAVE_SYS_SOCKET_H
446ecf6635Schristos #include <sys/socket.h>
456ecf6635Schristos #endif
466ecf6635Schristos #include <fcntl.h>
476ecf6635Schristos #include <stdlib.h>
486ecf6635Schristos #include <stdio.h>
496ecf6635Schristos #include <string.h>
506ecf6635Schristos #include <signal.h>
516ecf6635Schristos #include <errno.h>
526ecf6635Schristos 
536ecf6635Schristos #include "event2/event.h"
546ecf6635Schristos #include "event2/event_struct.h"
556ecf6635Schristos #include "event2/event_compat.h"
566ecf6635Schristos #include "event2/util.h"
576ecf6635Schristos 
586ecf6635Schristos evutil_socket_t pair[2];
596ecf6635Schristos int test_okay = 1;
606ecf6635Schristos int called = 0;
616ecf6635Schristos 
626ecf6635Schristos static void
write_cb(evutil_socket_t fd,short event,void * arg)636ecf6635Schristos write_cb(evutil_socket_t fd, short event, void *arg)
646ecf6635Schristos {
656ecf6635Schristos 	const char *test = "test string";
666ecf6635Schristos 	int len;
676ecf6635Schristos 
686ecf6635Schristos 	len = send(fd, test, (int)strlen(test) + 1, 0);
696ecf6635Schristos 
706ecf6635Schristos 	printf("%s: write %d%s\n", __func__,
716ecf6635Schristos 	    len, len ? "" : " - means EOF");
726ecf6635Schristos 
736ecf6635Schristos 	if (len > 0) {
746ecf6635Schristos 		if (!called)
756ecf6635Schristos 			event_add(arg, NULL);
766ecf6635Schristos 		evutil_closesocket(pair[0]);
776ecf6635Schristos 	} else if (called == 1)
786ecf6635Schristos 		test_okay = 0;
796ecf6635Schristos 
806ecf6635Schristos 	called++;
816ecf6635Schristos }
826ecf6635Schristos 
836ecf6635Schristos int
main(int argc,char ** argv)846ecf6635Schristos main(int argc, char **argv)
856ecf6635Schristos {
866ecf6635Schristos 	struct event ev;
876ecf6635Schristos 
88805a1ce9Schristos #ifdef _WIN32
896ecf6635Schristos 	WORD wVersionRequested;
906ecf6635Schristos 	WSADATA wsaData;
916ecf6635Schristos 
926ecf6635Schristos 	wVersionRequested = MAKEWORD(2, 2);
936ecf6635Schristos 
946ecf6635Schristos 	(void) WSAStartup(wVersionRequested, &wsaData);
956ecf6635Schristos #endif
966ecf6635Schristos 
97805a1ce9Schristos #ifndef _WIN32
986ecf6635Schristos 	if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
996ecf6635Schristos 		return (1);
1006ecf6635Schristos #endif
1016ecf6635Schristos 
1026ecf6635Schristos 	if (evutil_socketpair(AF_UNIX, SOCK_STREAM, 0, pair) == -1)
1036ecf6635Schristos 		return (1);
1046ecf6635Schristos 
105*657871a7Schristos 	/* Initialize the event library */
1066ecf6635Schristos 	event_init();
1076ecf6635Schristos 
108*657871a7Schristos 	/* Initialize one event */
1096ecf6635Schristos 	event_set(&ev, pair[1], EV_WRITE, write_cb, &ev);
1106ecf6635Schristos 
1116ecf6635Schristos 	event_add(&ev, NULL);
1126ecf6635Schristos 
1136ecf6635Schristos 	event_dispatch();
1146ecf6635Schristos 
1156ecf6635Schristos 	return (test_okay);
1166ecf6635Schristos }
1176ecf6635Schristos 
118