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