1 /* Check a bug where we have just completed a read, then set up a duplex
2 * which tries to do a read. */
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>
15 #define is_idle(conn) ((conn)->plan.io == NULL)
18 struct io_listener *l;
20 struct io_conn *c1, *c2;
25 static void finish_ok(struct io_conn *conn, struct data *d)
30 static struct io_plan end(struct io_conn *conn, struct data *d)
34 /* last one out closes. */
35 if (conn == d->c1 && is_idle(d->c2))
38 /* last one out closes. */
39 if (conn == d->c2 && is_idle(d->c1))
45 static struct io_plan make_duplex(struct io_conn *conn, struct data *d)
47 /* Have duplex read the rest of the buffer. */
48 d->c2 = io_duplex(conn, io_read(d->buf+1, sizeof(d->buf)-1, end, d));
50 io_set_finish(d->c2, finish_ok, d);
52 return io_write(d->wbuf, sizeof(d->wbuf), end, d);
55 static void init_conn(int fd, struct data *d)
60 io_close_listener(d->l);
62 memset(d->wbuf, 7, sizeof(d->wbuf));
64 d->c1 = io_new_conn(fd, io_read(d->buf, 1, make_duplex, d));
65 io_set_finish(d->c1, finish_ok, d);
68 static int make_listen_fd(const char *port, struct addrinfo **info)
71 struct addrinfo *addrinfo, hints;
73 memset(&hints, 0, sizeof(hints));
74 hints.ai_family = AF_UNSPEC;
75 hints.ai_socktype = SOCK_STREAM;
76 hints.ai_flags = AI_PASSIVE;
77 hints.ai_protocol = 0;
79 if (getaddrinfo(NULL, port, &hints, &addrinfo) != 0)
82 fd = socket(addrinfo->ai_family, addrinfo->ai_socktype,
83 addrinfo->ai_protocol);
87 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
88 if (bind(fd, addrinfo->ai_addr, addrinfo->ai_addrlen) != 0) {
92 if (listen(fd, 1) != 0) {
102 struct data *d = malloc(sizeof(*d));
103 struct addrinfo *addrinfo;
106 /* This is how many tests you plan to run */
109 fd = make_listen_fd(PORT, &addrinfo);
111 d->l = io_new_listener(fd, init_conn, d);
118 io_close_listener(d->l);
120 fd = socket(addrinfo->ai_family, addrinfo->ai_socktype,
121 addrinfo->ai_protocol);
124 if (connect(fd, addrinfo->ai_addr, addrinfo->ai_addrlen) != 0)
126 signal(SIGPIPE, SIG_IGN);
127 for (i = 0; i < strlen("hellothere"); i++) {
128 if (write(fd, "hellothere" + i, 1) != 1)
131 for (i = 0; i < 32; i++) {
132 if (read(fd, buf+i, 1) != 1)
136 freeaddrinfo(addrinfo);
139 freeaddrinfo(addrinfo);
140 ok1(io_loop() == NULL);
142 ok1(memcmp(d->buf, "hellothere", sizeof(d->buf)) == 0);
146 ok1(WIFEXITED(status));
147 ok1(WEXITSTATUS(status) == 0);
149 /* This exits depending on whether all tests passed */
150 return exit_status();