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