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