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