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