]> git.ozlabs.org Git - ccan/blob - ccan/io/test/run-16-duplex-test.c
io: update for new timer API.
[ccan] / ccan / io / test / run-16-duplex-test.c
1 /* Tests when the last connection is a duplex, and poll.c moves it over
2  * deleted fd. */
3 #include <ccan/io/io.h>
4 /* Include the C files directly. */
5 #include <ccan/io/poll.c>
6 #include <ccan/io/io.c>
7 #include <ccan/tap/tap.h>
8 #include <sys/wait.h>
9 #include <stdio.h>
10
11 #ifdef DEBUG_CONN
12 #define PORT "64016"
13 #else
14 #define PORT "65016"
15 #endif
16
17 struct data {
18         struct io_listener *l;
19         int state;
20         char buf[4];
21         char wbuf[32];
22 };
23
24 static void finish_ok(struct io_conn *conn, struct data *d)
25 {
26         d->state++;
27 }
28
29 static struct io_plan *io_done(struct io_conn *conn, struct data *d)
30 {
31         d->state++;
32         return io_halfclose(conn);
33 }
34
35 static struct io_plan *init_conn(struct io_conn *conn, struct data *d)
36 {
37 #ifdef DEBUG_CONN
38         io_set_debug(conn, true);
39 #endif
40         ok1(d->state == 0);
41         d->state++;
42
43         memset(d->wbuf, 7, sizeof(d->wbuf));
44
45         io_set_finish(conn, finish_ok, d);
46
47         io_close_listener(d->l);
48
49         return io_duplex(conn,
50                          io_read(conn, d->buf, sizeof(d->buf), io_done, d),
51                          io_write(conn, d->wbuf, sizeof(d->wbuf), io_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 }