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