xref: /freebsd/contrib/ntp/sntp/libevent/test/test-dumpevents.c (revision 416ba5c74546f32a993436a99516d35008e9f384)
1*2b15cb3dSCy Schubert /*
2*2b15cb3dSCy Schubert  * Copyright (c) 2012 Niels Provos and Nick Mathewson
3*2b15cb3dSCy Schubert  *
4*2b15cb3dSCy Schubert  * Redistribution and use in source and binary forms, with or without
5*2b15cb3dSCy Schubert  * modification, are permitted provided that the following conditions
6*2b15cb3dSCy Schubert  * are met:
7*2b15cb3dSCy Schubert  * 1. Redistributions of source code must retain the above copyright
8*2b15cb3dSCy Schubert  *    notice, this list of conditions and the following disclaimer.
9*2b15cb3dSCy Schubert  * 2. Redistributions in binary form must reproduce the above copyright
10*2b15cb3dSCy Schubert  *    notice, this list of conditions and the following disclaimer in the
11*2b15cb3dSCy Schubert  *    documentation and/or other materials provided with the distribution.
12*2b15cb3dSCy Schubert  * 3. The name of the author may not be used to endorse or promote products
13*2b15cb3dSCy Schubert  *    derived from this software without specific prior written permission.
14*2b15cb3dSCy Schubert  *
15*2b15cb3dSCy Schubert  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16*2b15cb3dSCy Schubert  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17*2b15cb3dSCy Schubert  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18*2b15cb3dSCy Schubert  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19*2b15cb3dSCy Schubert  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20*2b15cb3dSCy Schubert  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21*2b15cb3dSCy Schubert  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22*2b15cb3dSCy Schubert  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23*2b15cb3dSCy Schubert  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24*2b15cb3dSCy Schubert  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25*2b15cb3dSCy Schubert  */
26*2b15cb3dSCy Schubert #include "util-internal.h"
27*2b15cb3dSCy Schubert #include "event2/event-config.h"
28*2b15cb3dSCy Schubert 
29*2b15cb3dSCy Schubert #ifdef _WIN32
30*2b15cb3dSCy Schubert #include <winsock2.h>
31*2b15cb3dSCy Schubert #include <windows.h>
32*2b15cb3dSCy Schubert #else
33*2b15cb3dSCy Schubert #include <unistd.h>
34*2b15cb3dSCy Schubert #endif
35*2b15cb3dSCy Schubert 
36*2b15cb3dSCy Schubert #include <stdio.h>
37*2b15cb3dSCy Schubert #include <event2/event.h>
38*2b15cb3dSCy Schubert #include <signal.h>
39*2b15cb3dSCy Schubert 
40*2b15cb3dSCy Schubert static void
sock_perror(const char * s)41*2b15cb3dSCy Schubert sock_perror(const char *s)
42*2b15cb3dSCy Schubert {
43*2b15cb3dSCy Schubert #ifdef _WIN32
44*2b15cb3dSCy Schubert 	const char *err = evutil_socket_error_to_string(EVUTIL_SOCKET_ERROR());
45*2b15cb3dSCy Schubert 	fprintf(stderr, "%s: %s\n", s, err);
46*2b15cb3dSCy Schubert #else
47*2b15cb3dSCy Schubert 	perror(s);
48*2b15cb3dSCy Schubert #endif
49*2b15cb3dSCy Schubert }
50*2b15cb3dSCy Schubert 
51*2b15cb3dSCy Schubert static void
callback1(evutil_socket_t fd,short events,void * arg)52*2b15cb3dSCy Schubert callback1(evutil_socket_t fd, short events, void *arg)
53*2b15cb3dSCy Schubert {
54*2b15cb3dSCy Schubert }
55*2b15cb3dSCy Schubert static void
callback2(evutil_socket_t fd,short events,void * arg)56*2b15cb3dSCy Schubert callback2(evutil_socket_t fd, short events, void *arg)
57*2b15cb3dSCy Schubert {
58*2b15cb3dSCy Schubert }
59*2b15cb3dSCy Schubert 
60*2b15cb3dSCy Schubert /* Testing code for event_base_dump_events().
61*2b15cb3dSCy Schubert 
62*2b15cb3dSCy Schubert    Notes that just because we have code to exercise this function,
63*2b15cb3dSCy Schubert    doesn't mean that *ANYTHING* about the output format is guaranteed to
64*2b15cb3dSCy Schubert    remain in the future.
65*2b15cb3dSCy Schubert  */
66*2b15cb3dSCy Schubert int
main(int argc,char ** argv)67*2b15cb3dSCy Schubert main(int argc, char **argv)
68*2b15cb3dSCy Schubert {
69*2b15cb3dSCy Schubert #define N_EVENTS 13
70*2b15cb3dSCy Schubert 	int i;
71*2b15cb3dSCy Schubert 	struct event *ev[N_EVENTS];
72*2b15cb3dSCy Schubert 	evutil_socket_t pair1[2];
73*2b15cb3dSCy Schubert 	evutil_socket_t pair2[2];
74*2b15cb3dSCy Schubert 	struct timeval tv_onesec = {1,0};
75*2b15cb3dSCy Schubert 	struct timeval tv_two5sec = {2,500*1000};
76*2b15cb3dSCy Schubert 	const struct timeval *tv_onesec_common;
77*2b15cb3dSCy Schubert 	const struct timeval *tv_two5sec_common;
78*2b15cb3dSCy Schubert 	struct event_base *base;
79*2b15cb3dSCy Schubert 	struct timeval now;
80*2b15cb3dSCy Schubert 
81*2b15cb3dSCy Schubert #ifdef _WIN32
82*2b15cb3dSCy Schubert 	WORD wVersionRequested;
83*2b15cb3dSCy Schubert 	WSADATA wsaData;
84*2b15cb3dSCy Schubert 
85*2b15cb3dSCy Schubert 	wVersionRequested = MAKEWORD(2, 2);
86*2b15cb3dSCy Schubert 
87*2b15cb3dSCy Schubert 	WSAStartup(wVersionRequested, &wsaData);
88*2b15cb3dSCy Schubert #endif
89*2b15cb3dSCy Schubert 
90*2b15cb3dSCy Schubert #ifdef _WIN32
91*2b15cb3dSCy Schubert #define LOCAL_SOCKETPAIR_AF AF_INET
92*2b15cb3dSCy Schubert #else
93*2b15cb3dSCy Schubert #define LOCAL_SOCKETPAIR_AF AF_UNIX
94*2b15cb3dSCy Schubert #endif
95*2b15cb3dSCy Schubert 
96*2b15cb3dSCy Schubert 	if (evutil_make_internal_pipe_(pair1) < 0 ||
97*2b15cb3dSCy Schubert 	    evutil_make_internal_pipe_(pair2) < 0) {
98*2b15cb3dSCy Schubert 		sock_perror("evutil_make_internal_pipe_");
99*2b15cb3dSCy Schubert 		return 1;
100*2b15cb3dSCy Schubert 	}
101*2b15cb3dSCy Schubert 
102*2b15cb3dSCy Schubert 	if (!(base = event_base_new())) {
103*2b15cb3dSCy Schubert 		fprintf(stderr,"Couldn't make event_base\n");
104*2b15cb3dSCy Schubert 		return 2;
105*2b15cb3dSCy Schubert 	}
106*2b15cb3dSCy Schubert 
107*2b15cb3dSCy Schubert 	tv_onesec_common = event_base_init_common_timeout(base, &tv_onesec);
108*2b15cb3dSCy Schubert 	tv_two5sec_common = event_base_init_common_timeout(base, &tv_two5sec);
109*2b15cb3dSCy Schubert 
110*2b15cb3dSCy Schubert 	ev[0] = event_new(base, pair1[0], EV_WRITE, callback1, NULL);
111*2b15cb3dSCy Schubert 	ev[1] = event_new(base, pair1[1], EV_READ|EV_PERSIST, callback1, NULL);
112*2b15cb3dSCy Schubert 	ev[2] = event_new(base, pair2[0], EV_WRITE|EV_PERSIST, callback2, NULL);
113*2b15cb3dSCy Schubert 	ev[3] = event_new(base, pair2[1], EV_READ, callback2, NULL);
114*2b15cb3dSCy Schubert 
115*2b15cb3dSCy Schubert 	/* For timers */
116*2b15cb3dSCy Schubert 	ev[4] = evtimer_new(base, callback1, NULL);
117*2b15cb3dSCy Schubert 	ev[5] = evtimer_new(base, callback1, NULL);
118*2b15cb3dSCy Schubert 	ev[6] = evtimer_new(base, callback1, NULL);
119*2b15cb3dSCy Schubert 	ev[7] = event_new(base, -1, EV_PERSIST, callback2, NULL);
120*2b15cb3dSCy Schubert 	ev[8] = event_new(base, -1, EV_PERSIST, callback2, NULL);
121*2b15cb3dSCy Schubert 	ev[9] = event_new(base, -1, EV_PERSIST, callback2, NULL);
122*2b15cb3dSCy Schubert 
123*2b15cb3dSCy Schubert 	/* To activate */
124*2b15cb3dSCy Schubert 	ev[10] = event_new(base, -1, 0, callback1, NULL);
125*2b15cb3dSCy Schubert 	ev[11] = event_new(base, -1, 0, callback2, NULL);
126*2b15cb3dSCy Schubert 
127*2b15cb3dSCy Schubert 	/* Signals */
128*2b15cb3dSCy Schubert 	ev[12] = evsignal_new(base, SIGINT, callback2, NULL);
129*2b15cb3dSCy Schubert 
130*2b15cb3dSCy Schubert 	event_add(ev[0], NULL);
131*2b15cb3dSCy Schubert 	event_add(ev[1], &tv_onesec);
132*2b15cb3dSCy Schubert 	event_add(ev[2], tv_onesec_common);
133*2b15cb3dSCy Schubert 	event_add(ev[3], tv_two5sec_common);
134*2b15cb3dSCy Schubert 
135*2b15cb3dSCy Schubert 	event_add(ev[4], tv_onesec_common);
136*2b15cb3dSCy Schubert 	event_add(ev[5], tv_onesec_common);
137*2b15cb3dSCy Schubert 	event_add(ev[6], &tv_onesec);
138*2b15cb3dSCy Schubert 	event_add(ev[7], tv_two5sec_common);
139*2b15cb3dSCy Schubert 	event_add(ev[8], tv_onesec_common);
140*2b15cb3dSCy Schubert 	event_add(ev[9], &tv_two5sec);
141*2b15cb3dSCy Schubert 
142*2b15cb3dSCy Schubert 	event_active(ev[10], EV_READ, 1);
143*2b15cb3dSCy Schubert 	event_active(ev[11], EV_READ|EV_WRITE|EV_TIMEOUT, 1);
144*2b15cb3dSCy Schubert 	event_active(ev[1], EV_READ, 1);
145*2b15cb3dSCy Schubert 
146*2b15cb3dSCy Schubert 	event_add(ev[12], NULL);
147*2b15cb3dSCy Schubert 
148*2b15cb3dSCy Schubert 	evutil_gettimeofday(&now,NULL);
149*2b15cb3dSCy Schubert 	puts("=====expected");
150*2b15cb3dSCy Schubert 	printf("Now= %ld.%06d\n",(long)now.tv_sec,(int)now.tv_usec);
151*2b15cb3dSCy Schubert 	puts("Inserted:");
152*2b15cb3dSCy Schubert 	printf("  %p [fd  %ld] Write\n",ev[0],(long)pair1[0]);
153*2b15cb3dSCy Schubert 	printf("  %p [fd  %ld] Read Persist Timeout=T+1\n",ev[1],(long)pair1[1]);
154*2b15cb3dSCy Schubert 	printf("  %p [fd  %ld] Write Persist Timeout=T+1\n",ev[2],(long)pair2[0]);
155*2b15cb3dSCy Schubert 	printf("  %p [fd  %ld] Read Timeout=T+2.5\n",ev[3],(long)pair2[1]);
156*2b15cb3dSCy Schubert 	printf("  %p [fd  -1] Timeout=T+1\n",ev[4]);
157*2b15cb3dSCy Schubert 	printf("  %p [fd  -1] Timeout=T+1\n",ev[5]);
158*2b15cb3dSCy Schubert 	printf("  %p [fd  -1] Timeout=T+1\n",ev[6]);
159*2b15cb3dSCy Schubert 	printf("  %p [fd  -1] Persist Timeout=T+2.5\n",ev[7]);
160*2b15cb3dSCy Schubert 	printf("  %p [fd  -1] Persist Timeout=T+1\n",ev[8]);
161*2b15cb3dSCy Schubert 	printf("  %p [fd  -1] Persist Timeout=T+2.5\n",ev[9]);
162*2b15cb3dSCy Schubert 	printf("  %p [sig %d] Signal Persist\n", ev[12], (int)SIGINT);
163*2b15cb3dSCy Schubert 
164*2b15cb3dSCy Schubert 	puts("Active:");
165*2b15cb3dSCy Schubert 	printf("  %p [fd  -1, priority=0] Read active\n", ev[10]);
166*2b15cb3dSCy Schubert 	printf("  %p [fd  -1, priority=0] Read Write Timeout active\n", ev[11]);
167*2b15cb3dSCy Schubert 	printf("  %p [fd  %ld, priority=0] Read active\n", ev[1], (long)pair1[1]);
168*2b15cb3dSCy Schubert 
169*2b15cb3dSCy Schubert 	puts("======received");
170*2b15cb3dSCy Schubert 	event_base_dump_events(base, stdout);
171*2b15cb3dSCy Schubert 
172*2b15cb3dSCy Schubert 	for (i = 0; i < N_EVENTS; ++i) {
173*2b15cb3dSCy Schubert 		event_free(ev[i]);
174*2b15cb3dSCy Schubert 	}
175*2b15cb3dSCy Schubert 	event_base_free(base);
176*2b15cb3dSCy Schubert 
177*2b15cb3dSCy Schubert 	return 0;
178*2b15cb3dSCy Schubert }
179*2b15cb3dSCy Schubert 
180