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