xref: /netbsd-src/external/bsd/ntp/dist/sntp/libevent/test/test-weof.c (revision bdc22b2e01993381dcefeff2bc9b56ca75a4235c)
1 /*	$NetBSD: test-weof.c,v 1.4 2016/01/08 21:35:41 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 "event2/event-config.h"
30 
31 #ifdef _WIN32
32 #include <winsock2.h>
33 #else
34 #include <unistd.h>
35 #endif
36 #include <sys/types.h>
37 #include <sys/stat.h>
38 #ifdef EVENT__HAVE_SYS_TIME_H
39 #include <sys/time.h>
40 #endif
41 #ifdef EVENT__HAVE_SYS_SOCKET_H
42 #include <sys/socket.h>
43 #endif
44 #include <fcntl.h>
45 #include <stdlib.h>
46 #include <stdio.h>
47 #include <string.h>
48 #include <signal.h>
49 #include <errno.h>
50 
51 #include "event2/event.h"
52 #include "event2/event_struct.h"
53 #include "event2/event_compat.h"
54 #include "event2/util.h"
55 
56 #ifdef EVENT____func__
57 #define __func__ EVENT____func__
58 #endif
59 
60 evutil_socket_t pair[2];
61 int test_okay = 1;
62 int called = 0;
63 
64 static void
65 write_cb(evutil_socket_t fd, short event, void *arg)
66 {
67 	const char *test = "test string";
68 	int len;
69 
70 	len = send(fd, test, (int)strlen(test) + 1, 0);
71 
72 	printf("%s: write %d%s\n", __func__,
73 	    len, len ? "" : " - means EOF");
74 
75 	if (len > 0) {
76 		if (!called)
77 			event_add(arg, NULL);
78 		evutil_closesocket(pair[0]);
79 	} else if (called == 1)
80 		test_okay = 0;
81 
82 	called++;
83 }
84 
85 int
86 main(int argc, char **argv)
87 {
88 	struct event ev;
89 
90 #ifdef _WIN32
91 	WORD wVersionRequested;
92 	WSADATA wsaData;
93 
94 	wVersionRequested = MAKEWORD(2, 2);
95 
96 	(void) WSAStartup(wVersionRequested, &wsaData);
97 #endif
98 
99 #ifndef _WIN32
100 	if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
101 		return (1);
102 #endif
103 
104 	if (evutil_socketpair(AF_UNIX, SOCK_STREAM, 0, pair) == -1)
105 		return (1);
106 
107 	/* Initalize the event library */
108 	event_init();
109 
110 	/* Initalize one event */
111 	event_set(&ev, pair[1], EV_WRITE, write_cb, &ev);
112 
113 	event_add(&ev, NULL);
114 
115 	event_dispatch();
116 
117 	return (test_okay);
118 }
119 
120