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