]> git.ozlabs.org Git - ccan/blob - ccan/io/test/run-16-duplex-test.c
ea588661f7f091f2532da8fc717fb50dcf54cced
[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 #ifndef PORT
12 #define PORT "65016"
13 #endif
14
15 struct data {
16         struct io_listener *l;
17         int state;
18         char buf[4];
19         char wbuf[32];
20 };
21
22 static void finish_ok(struct io_conn *conn, struct data *d)
23 {
24         d->state++;
25 }
26
27 static struct io_plan *io_done(struct io_conn *conn, struct data *d)
28 {
29         d->state++;
30         if (d->state == 3)
31                 return io_close(conn);
32         return io_wait(conn, d, io_close_cb, NULL);
33 }
34
35 static struct io_plan *init_conn(struct io_conn *conn, struct data *d)
36 {
37         ok1(d->state == 0);
38         d->state++;
39
40         memset(d->wbuf, 7, sizeof(d->wbuf));
41
42         io_set_finish(conn, finish_ok, d);
43
44         io_close_listener(d->l);
45
46         return io_duplex(io_read(conn, d->buf, sizeof(d->buf), io_done, d),
47                          io_write(conn, d->wbuf, sizeof(d->wbuf), io_done, 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(9);
90         d->state = 0;
91         fd = make_listen_fd(PORT, &addrinfo);
92         ok1(fd >= 0);
93         d->l = io_new_listener(NULL, 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, NULL) == 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 }