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