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