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