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