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