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