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