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