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