xref: /netbsd-src/external/bsd/ntp/dist/sntp/libevent/test/test-closed.c (revision eabc0478de71e4e011a5b4e0392741e01d491794)
1*eabc0478Schristos /*	$NetBSD: test-closed.c,v 1.6 2024/08/18 20:47:23 christos Exp $	*/
2b8ecfcfeSchristos 
3b8ecfcfeSchristos /*
4b8ecfcfeSchristos  * Copyright (c) 2002-2007 Niels Provos <provos@citi.umich.edu>
5b8ecfcfeSchristos  * Copyright (c) 2007-2013 Niels Provos and Nick Mathewson
6b8ecfcfeSchristos  *
7b8ecfcfeSchristos  * Redistribution and use in source and binary forms, with or without
8b8ecfcfeSchristos  * modification, are permitted provided that the following conditions
9b8ecfcfeSchristos  * are met:
10b8ecfcfeSchristos  * 1. Redistributions of source code must retain the above copyright
11b8ecfcfeSchristos  *    notice, this list of conditions and the following disclaimer.
12b8ecfcfeSchristos  * 2. Redistributions in binary form must reproduce the above copyright
13b8ecfcfeSchristos  *    notice, this list of conditions and the following disclaimer in the
14b8ecfcfeSchristos  *    documentation and/or other materials provided with the distribution.
15b8ecfcfeSchristos  * 3. The name of the author may not be used to endorse or promote products
16b8ecfcfeSchristos  *    derived from this software without specific prior written permission.
17b8ecfcfeSchristos  *
18b8ecfcfeSchristos  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19b8ecfcfeSchristos  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20b8ecfcfeSchristos  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21b8ecfcfeSchristos  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22b8ecfcfeSchristos  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23b8ecfcfeSchristos  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24b8ecfcfeSchristos  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25b8ecfcfeSchristos  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26b8ecfcfeSchristos  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27b8ecfcfeSchristos  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28b8ecfcfeSchristos  */
29*eabc0478Schristos #include "../util-internal.h"
30b8ecfcfeSchristos #include "event2/event-config.h"
31b8ecfcfeSchristos 
32b8ecfcfeSchristos #ifdef _WIN32
33b8ecfcfeSchristos #include <winsock2.h>
34b8ecfcfeSchristos #else
35b8ecfcfeSchristos #include <unistd.h>
36b8ecfcfeSchristos #endif
37b8ecfcfeSchristos #include <sys/types.h>
38b8ecfcfeSchristos #include <sys/stat.h>
39b8ecfcfeSchristos #ifdef EVENT__HAVE_SYS_TIME_H
40b8ecfcfeSchristos #include <sys/time.h>
41b8ecfcfeSchristos #endif
42b8ecfcfeSchristos #ifdef EVENT__HAVE_SYS_SOCKET_H
43b8ecfcfeSchristos #include <sys/socket.h>
44b8ecfcfeSchristos #endif
45b8ecfcfeSchristos #include <fcntl.h>
46b8ecfcfeSchristos #include <stdlib.h>
47b8ecfcfeSchristos #include <stdio.h>
48b8ecfcfeSchristos #include <string.h>
49b8ecfcfeSchristos #include <errno.h>
50b8ecfcfeSchristos 
51b8ecfcfeSchristos #include <event.h>
52b8ecfcfeSchristos #include <evutil.h>
53b8ecfcfeSchristos 
54b8ecfcfeSchristos struct timeval timeout = {3, 0};
55b8ecfcfeSchristos 
56b8ecfcfeSchristos static void
57b8ecfcfeSchristos closed_cb(evutil_socket_t fd, short event, void *arg)
58b8ecfcfeSchristos {
59b8ecfcfeSchristos 	if (EV_TIMEOUT & event) {
60b8ecfcfeSchristos 		printf("%s: Timeout!\n", __func__);
61b8ecfcfeSchristos 		exit(1);
62b8ecfcfeSchristos 	}
63b8ecfcfeSchristos 
64b8ecfcfeSchristos 	if (EV_CLOSED & event) {
65b8ecfcfeSchristos 		printf("%s: detected socket close with success\n", __func__);
66b8ecfcfeSchristos 		return;
67b8ecfcfeSchristos 	}
68b8ecfcfeSchristos 
69b8ecfcfeSchristos 	printf("%s: unable to detect socket close\n", __func__);
70b8ecfcfeSchristos 	exit(1);
71b8ecfcfeSchristos }
72b8ecfcfeSchristos 
73b8ecfcfeSchristos int
74b8ecfcfeSchristos main(int argc, char **argv)
75b8ecfcfeSchristos {
76b8ecfcfeSchristos 	struct event_base *base;
77b8ecfcfeSchristos 	struct event_config *cfg;
78b8ecfcfeSchristos 	struct event *ev;
79b8ecfcfeSchristos 	const char *test = "test string";
80b8ecfcfeSchristos 	evutil_socket_t pair[2];
81b8ecfcfeSchristos 
82b8ecfcfeSchristos 	/* Initialize the library and check if the backend
83b8ecfcfeSchristos 	   supports EV_FEATURE_EARLY_CLOSE
84b8ecfcfeSchristos 	*/
85b8ecfcfeSchristos 	cfg = event_config_new();
86b8ecfcfeSchristos 	event_config_require_features(cfg, EV_FEATURE_EARLY_CLOSE);
87b8ecfcfeSchristos 	base = event_base_new_with_config(cfg);
88b8ecfcfeSchristos 	event_config_free(cfg);
89b8ecfcfeSchristos 	if (!base) {
90b8ecfcfeSchristos 		/* Backend doesn't support EV_FEATURE_EARLY_CLOSE */
91b8ecfcfeSchristos 		return 0;
92b8ecfcfeSchristos 	}
93b8ecfcfeSchristos 
94b8ecfcfeSchristos 	/* Create a pair of sockets */
95b8ecfcfeSchristos 	if (evutil_socketpair(AF_UNIX, SOCK_STREAM, 0, pair) == -1)
96b8ecfcfeSchristos 		return (1);
97b8ecfcfeSchristos 
98b8ecfcfeSchristos 	/* Send some data on socket 0 and immediately close it */
99b8ecfcfeSchristos 	if (send(pair[0], test, (int)strlen(test)+1, 0) < 0)
100b8ecfcfeSchristos 		return (1);
101*eabc0478Schristos 	shutdown(pair[0], EVUTIL_SHUT_WR);
102b8ecfcfeSchristos 
103b8ecfcfeSchristos 	/* Dispatch */
104b8ecfcfeSchristos 	ev = event_new(base, pair[1], EV_CLOSED | EV_TIMEOUT, closed_cb, event_self_cbarg());
105b8ecfcfeSchristos 	event_add(ev, &timeout);
106b8ecfcfeSchristos 	event_base_dispatch(base);
107b8ecfcfeSchristos 
108b8ecfcfeSchristos 	/* Finalize library */
109*eabc0478Schristos 	event_free(ev);
110b8ecfcfeSchristos 	event_base_free(base);
111b8ecfcfeSchristos 	return 0;
112b8ecfcfeSchristos }
113b8ecfcfeSchristos 
114