Tizen 2.1 base
[platform/upstream/gcd.git] / kqueue-1.0.4 / test / main.c
1 /*
2  * Copyright (c) 2009 Mark Heily <mark@heily.com>
3  *
4  * Permission to use, copy, modify, and distribute this software for any
5  * purpose with or without fee is hereby granted, provided that the above
6  * copyright notice and this permission notice appear in all copies.
7  *
8  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15  */
16
17 #include <sys/types.h>
18 #include <poll.h>
19
20 #include "common.h"
21
22 struct unit_test {
23     const char *ut_name;
24     int         ut_enabled;
25     void      (*ut_func)(int);
26 };
27
28 /*
29  * Test the method for detecting when one end of a socketpair 
30  * has been closed. This technique is used in kqueue_validate()
31  */
32 static void
33 test_peer_close_detection(void)
34 {
35     int sockfd[2];
36     char buf[1];
37     struct pollfd pfd;
38
39     if (socketpair(AF_UNIX, SOCK_STREAM, 0, sockfd) < 0)
40         die("socketpair");
41
42     pfd.fd = sockfd[0];
43     pfd.events = POLLIN | POLLHUP;
44     pfd.revents = 0;
45
46     if (poll(&pfd, 1, 0) > 0) 
47         die("unexpected data");
48
49     if (close(sockfd[1]) < 0)
50         die("close");
51
52     if (poll(&pfd, 1, 0) > 0) {
53         if (recv(sockfd[0], buf, sizeof(buf), MSG_PEEK | MSG_DONTWAIT) != 0) 
54             die("failed to detect peer shutdown");
55     }
56 }
57
58 void
59 test_kqueue(void)
60 {
61     int kqfd;
62
63     if ((kqfd = kqueue()) < 0)
64         die("kqueue()");
65     test_no_kevents(kqfd);
66     if (close(kqfd) < 0)
67         die("close()");
68 }
69
70 void
71 test_ev_receipt(void)
72 {
73     int kq;
74     struct kevent kev;
75
76     if ((kq = kqueue()) < 0)
77         die("kqueue()");
78 #if HAVE_EV_RECEIPT
79
80     EV_SET(&kev, SIGUSR2, EVFILT_SIGNAL, EV_ADD | EV_RECEIPT, 0, 0, NULL);
81     if (kevent(kq, &kev, 1, &kev, 1, NULL) < 0)
82         die("kevent");
83
84     /* TODO: check the receipt */
85
86     close(kq);
87 #else
88     memset(&kev, 0, sizeof(kev));
89     puts("Skipped -- EV_RECEIPT is not available");
90 #endif
91 }
92
93 int 
94 main(int argc, char **argv)
95 {
96     struct unit_test tests[] = {
97         { "socket", 1, test_evfilt_read },
98         { "signal", 1, test_evfilt_signal },
99 #if FIXME
100         { "proc", 1, test_evfilt_proc },
101 #endif
102         { "vnode", 1, test_evfilt_vnode },
103         { "timer", 1, test_evfilt_timer },
104 #if HAVE_EVFILT_USER
105         { "user", 1, test_evfilt_user },
106 #endif
107         { NULL, 0, NULL },
108     };
109     struct unit_test *test;
110     char *arg;
111     int match, kqfd;
112
113     /* If specific tests are requested, disable all tests by default */
114     if (argc > 1) {
115         for (test = &tests[0]; test->ut_name != NULL; test++) {
116             test->ut_enabled = 0;
117         }
118     }
119
120     while (argc > 1) {
121         match = 0;
122         arg = argv[1];
123         for (test = &tests[0]; test->ut_name != NULL; test++) {
124             if (strcmp(arg, test->ut_name) == 0) {
125                 test->ut_enabled = 1;
126                 match = 1;
127                 break;
128             }
129         }
130         if (!match) {
131             printf("ERROR: invalid option: %s\n", arg);
132             exit(1);
133         } else {
134             printf("enabled test: %s\n", arg);
135         }
136         argv++;
137         argc--;
138     }
139
140     testing_begin();
141
142     test(peer_close_detection);
143
144     test(kqueue);
145
146     if ((kqfd = kqueue()) < 0)
147         die("kqueue()");
148
149     for (test = &tests[0]; test->ut_name != NULL; test++) {
150         if (test->ut_enabled)
151             test->ut_func(kqfd);
152     }
153
154     test(ev_receipt);
155
156     testing_end();
157
158     return (0);
159 }