]> git.ozlabs.org Git - ccan/blob - ccan/io/test/run-19-always.c
io: io_never for events that should never happen.
[ccan] / ccan / io / test / run-19-always.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
9 #ifndef PORT
10 #define PORT "65019"
11 #endif
12
13 struct data {
14         int state;
15         size_t bytes;
16         char *buf;
17 };
18
19 static void finish_ok(struct io_conn *conn, struct data *d)
20 {
21         ok1(d->state == 1);
22         d->state++;
23         io_break(d, io_never());
24 }
25
26 static struct io_plan write_buf(struct io_conn *conn, struct data *d)
27 {
28         return io_write(d->buf, d->bytes, io_close_cb, d);
29 }
30
31 static void init_conn(int fd, struct data *d)
32 {
33         ok1(d->state == 0);
34         d->state++;
35         /* Empty read should run immediately... */
36         io_set_finish(io_new_conn(fd, io_read(NULL, 0, write_buf, d)),
37                       finish_ok, d);
38 }
39
40 static int make_listen_fd(const char *port, struct addrinfo **info)
41 {
42         int fd, on = 1;
43         struct addrinfo *addrinfo, hints;
44
45         memset(&hints, 0, sizeof(hints));
46         hints.ai_family = AF_UNSPEC;
47         hints.ai_socktype = SOCK_STREAM;
48         hints.ai_flags = AI_PASSIVE;
49         hints.ai_protocol = 0;
50
51         if (getaddrinfo(NULL, port, &hints, &addrinfo) != 0)
52                 return -1;
53
54         fd = socket(addrinfo->ai_family, addrinfo->ai_socktype,
55                     addrinfo->ai_protocol);
56         if (fd < 0)
57                 return -1;
58
59         setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
60         if (bind(fd, addrinfo->ai_addr, addrinfo->ai_addrlen) != 0) {
61                 close(fd);
62                 return -1;
63         }
64         if (listen(fd, 1) != 0) {
65                 close(fd);
66                 return -1;
67         }
68         *info = addrinfo;
69         return fd;
70 }
71
72 static void read_from_socket(size_t bytes, const struct addrinfo *addrinfo)
73 {
74         int fd, done, r;
75         char buf[100];
76
77         fd = socket(addrinfo->ai_family, addrinfo->ai_socktype,
78                     addrinfo->ai_protocol);
79         if (fd < 0)
80                 exit(1);
81         if (connect(fd, addrinfo->ai_addr, addrinfo->ai_addrlen) != 0)
82                 exit(2);
83
84         for (done = 0; done < bytes; done += r) {
85                 r = read(fd, buf, sizeof(buf));
86                 if (r < 0)
87                         exit(3);
88                 done += r;
89         }
90         close(fd);
91 }
92
93 int main(void)
94 {
95         struct data *d = malloc(sizeof(*d));
96         struct addrinfo *addrinfo;
97         struct io_listener *l;
98         int fd, status;
99
100         /* This is how many tests you plan to run */
101         plan_tests(9);
102         d->state = 0;
103         d->bytes = 1024*1024;
104         d->buf = malloc(d->bytes);
105         memset(d->buf, 'a', d->bytes);
106         fd = make_listen_fd(PORT, &addrinfo);
107         ok1(fd >= 0);
108         l = io_new_listener(fd, init_conn, d);
109         ok1(l);
110         fflush(stdout);
111         if (!fork()) {
112                 io_close_listener(l);
113                 read_from_socket(d->bytes, addrinfo);
114                 freeaddrinfo(addrinfo);
115                 free(d->buf);
116                 free(d);
117                 exit(0);
118         }
119         ok1(io_loop() == d);
120         ok1(d->state == 2);
121
122         ok1(wait(&status));
123         ok1(WIFEXITED(status));
124         ok1(WEXITSTATUS(status) == 0);
125
126         freeaddrinfo(addrinfo);
127         free(d->buf);
128         free(d);
129         io_close_listener(l);
130
131         /* This exits depending on whether all tests passed */
132         return exit_status();
133 }