]> git.ozlabs.org Git - ccan/blob - ccan/io/test/run-06-idle.c
d75a216db6a48d5cbc68aa6a093e4ba7c0496c77
[ccan] / ccan / io / test / run-06-idle.c
1 #include <ccan/io/io.h>
2 /* Include the C files directly. */
3 #include <ccan/io/poll.c>
4 #include <ccan/io/io.c>
5 #include <ccan/tap/tap.h>
6 #include <sys/wait.h>
7 #include <stdio.h>
8 #include <sys/types.h>
9 #include <sys/stat.h>
10 #include <fcntl.h>
11
12 static struct io_conn *idler;
13
14 struct data {
15         int state;
16         char buf[4];
17 };
18
19 static struct io_plan read_done(struct io_conn *conn, struct data *d)
20 {
21         ok1(d->state == 2 || d->state == 3);
22         d->state++;
23         return io_close(conn, NULL);
24 }
25
26 static void finish_waker(struct io_conn *conn, struct data *d)
27 {
28         io_wake(idler, io_read(d->buf, sizeof(d->buf), read_done, d));
29         ok1(d->state == 1);
30         d->state++;
31 }
32
33 static void finish_idle(struct io_conn *conn, struct data *d)
34 {
35         ok1(d->state == 3);
36         d->state++;
37         io_break(d, io_idle());
38 }
39
40 static struct io_plan never(struct io_conn *conn, void *arg)
41 {
42         abort();
43 }
44
45 static void init_conn(int fd, struct data *d)
46 {
47         int fd2;
48
49         ok1(d->state == 0);
50         d->state++;
51         idler = io_new_conn(fd, io_idle(), finish_idle, d);
52
53         /* This will wake us up, as read will fail. */
54         fd2 = open("/dev/null", O_RDONLY);
55         ok1(fd2 >= 0);
56         ok1(io_new_conn(fd2, io_read(idler, 1, never, NULL), finish_waker, d));
57 }
58
59 static int make_listen_fd(const char *port, struct addrinfo **info)
60 {
61         int fd, on = 1;
62         struct addrinfo *addrinfo, hints;
63
64         memset(&hints, 0, sizeof(hints));
65         hints.ai_family = AF_UNSPEC;
66         hints.ai_socktype = SOCK_STREAM;
67         hints.ai_flags = AI_PASSIVE;
68         hints.ai_protocol = 0;
69
70         if (getaddrinfo(NULL, port, &hints, &addrinfo) != 0)
71                 return -1;
72
73         fd = socket(addrinfo->ai_family, addrinfo->ai_socktype,
74                     addrinfo->ai_protocol);
75         if (fd < 0)
76                 return -1;
77
78         setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
79         if (bind(fd, addrinfo->ai_addr, addrinfo->ai_addrlen) != 0) {
80                 close(fd);
81                 return -1;
82         }
83         if (listen(fd, 1) != 0) {
84                 close(fd);
85                 return -1;
86         }
87         *info = addrinfo;
88         return fd;
89 }
90
91 int main(void)
92 {
93         struct data *d = malloc(sizeof(*d));
94         struct addrinfo *addrinfo;
95         struct io_listener *l;
96         int fd, status;
97
98         /* This is how many tests you plan to run */
99         plan_tests(14);
100         d->state = 0;
101         fd = make_listen_fd("65006", &addrinfo);
102         ok1(fd >= 0);
103         l = io_new_listener(fd, init_conn, d);
104         ok1(l);
105         fflush(stdout);
106         if (!fork()) {
107                 int i;
108
109                 io_close_listener(l);
110                 fd = socket(addrinfo->ai_family, addrinfo->ai_socktype,
111                             addrinfo->ai_protocol);
112                 if (fd < 0)
113                         exit(1);
114                 if (connect(fd, addrinfo->ai_addr, addrinfo->ai_addrlen) != 0)
115                         exit(2);
116                 signal(SIGPIPE, SIG_IGN);
117                 for (i = 0; i < strlen("hellothere"); i++) {
118                         if (write(fd, "hellothere" + i, 1) != 1)
119                                 break;
120                 }
121                 close(fd);
122                 freeaddrinfo(addrinfo);
123                 free(d);
124                 exit(0);
125         }
126         freeaddrinfo(addrinfo);
127
128         ok1(io_loop() == d);
129         ok1(d->state == 4);
130         ok1(memcmp(d->buf, "hellothere", sizeof(d->buf)) == 0);
131         free(d);
132         io_close_listener(l);
133
134         ok1(wait(&status));
135         ok1(WIFEXITED(status));
136         ok1(WEXITSTATUS(status) == 0);
137
138         /* This exits depending on whether all tests passed */
139         return exit_status();
140 }