xref: /netbsd-src/external/bsd/ntp/dist/sntp/libevent/test/test-weof.c (revision 4439cfd0acf9c7dc90625e5cd83b2317a9ab8967)
1 /*	$NetBSD: test-weof.c,v 1.6 2024/08/18 20:47:24 christos Exp $	*/
2 
3 /*
4  * Copyright (c) 2002-2007 Niels Provos <provos@citi.umich.edu>
5  * Copyright (c) 2007-2012 Niels Provos and Nick Mathewson
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. The name of the author may not be used to endorse or promote products
16  *    derived from this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  */
29 #include "../util-internal.h"
30 #include "event2/event-config.h"
31 
32 #ifdef _WIN32
33 #include <winsock2.h>
34 #else
35 #include <unistd.h>
36 #endif
37 #include <sys/types.h>
38 #include <sys/stat.h>
39 #ifdef EVENT__HAVE_SYS_TIME_H
40 #include <sys/time.h>
41 #endif
42 #ifdef EVENT__HAVE_SYS_SOCKET_H
43 #include <sys/socket.h>
44 #endif
45 #include <fcntl.h>
46 #include <stdlib.h>
47 #include <stdio.h>
48 #include <string.h>
49 #include <signal.h>
50 #include <errno.h>
51 
52 #include "event2/event.h"
53 #include "event2/event_struct.h"
54 #include "event2/event_compat.h"
55 #include "event2/util.h"
56 
57 evutil_socket_t pair[2];
58 int test_okay = 1;
59 int called = 0;
60 
61 static void
62 write_cb(evutil_socket_t fd, short event, void *arg)
63 {
64 	const char *test = "test string";
65 	int len;
66 
67 	len = send(fd, test, (int)strlen(test) + 1, 0);
68 
69 	printf("%s: write %d%s\n", __func__,
70 	    len, len ? "" : " - means EOF");
71 
72 	if (len > 0) {
73 		if (!called)
74 			event_add(arg, NULL);
75 		evutil_closesocket(pair[0]);
76 	} else if (called == 1)
77 		test_okay = 0;
78 
79 	called++;
80 }
81 
82 int
83 main(int argc, char **argv)
84 {
85 	struct event ev;
86 
87 #ifdef _WIN32
88 	WORD wVersionRequested;
89 	WSADATA wsaData;
90 
91 	wVersionRequested = MAKEWORD(2, 2);
92 
93 	(void) WSAStartup(wVersionRequested, &wsaData);
94 #endif
95 
96 #ifndef _WIN32
97 	if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
98 		return (1);
99 #endif
100 
101 	if (evutil_socketpair(AF_UNIX, SOCK_STREAM, 0, pair) == -1)
102 		return (1);
103 
104 	/* Initialize the event library */
105 	event_init();
106 
107 	/* Initialize one event */
108 	event_set(&ev, pair[1], EV_WRITE, write_cb, &ev);
109 
110 	event_add(&ev, NULL);
111 
112 	event_dispatch();
113 
114 	return (test_okay);
115 }
116 
117