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