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>
14 struct io_listener *l;
20 static void finish_ok(struct io_conn *conn, struct data *d)
25 static struct io_plan *end(struct io_conn *conn, struct data *d)
28 /* Close on top of halfclose should work. */
30 return io_close(conn);
32 return io_halfclose(conn);
35 static struct io_plan *make_duplex(struct io_conn *conn, struct data *d)
38 /* Have duplex read the rest of the buffer. */
39 return io_duplex(conn,
40 io_read(conn, d->buf+1, sizeof(d->buf)-1, end, d),
41 io_write(conn, d->wbuf, sizeof(d->wbuf), end, d));
44 static struct io_plan *init_conn(struct io_conn *conn, struct data *d)
49 io_close_listener(d->l);
51 memset(d->wbuf, 7, sizeof(d->wbuf));
52 io_set_finish(conn, finish_ok, d);
53 return io_read(conn, d->buf, 1, make_duplex, d);
56 static int make_listen_fd(const char *port, struct addrinfo **info)
59 struct addrinfo *addrinfo, hints;
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;
67 if (getaddrinfo(NULL, port, &hints, &addrinfo) != 0)
70 fd = socket(addrinfo->ai_family, addrinfo->ai_socktype,
71 addrinfo->ai_protocol);
75 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
76 if (bind(fd, addrinfo->ai_addr, addrinfo->ai_addrlen) != 0) {
80 if (listen(fd, 1) != 0) {
90 struct data *d = malloc(sizeof(*d));
91 struct addrinfo *addrinfo;
94 /* This is how many tests you plan to run */
97 fd = make_listen_fd(PORT, &addrinfo);
99 d->l = io_new_listener(NULL, fd, init_conn, d);
106 io_close_listener(d->l);
108 fd = socket(addrinfo->ai_family, addrinfo->ai_socktype,
109 addrinfo->ai_protocol);
112 if (connect(fd, addrinfo->ai_addr, addrinfo->ai_addrlen) != 0)
114 signal(SIGPIPE, SIG_IGN);
115 for (i = 0; i < strlen("hellothere"); i++) {
116 if (write(fd, "hellothere" + i, 1) != 1)
119 for (i = 0; i < 32; i++) {
120 if (read(fd, buf+i, 1) != 1)
124 freeaddrinfo(addrinfo);
127 freeaddrinfo(addrinfo);
128 ok1(io_loop(NULL, NULL) == NULL);
130 ok1(memcmp(d->buf, "hellothere", sizeof(d->buf)) == 0);
134 ok1(WIFEXITED(status));
135 ok1(WEXITSTATUS(status) == 0);
137 /* This exits depending on whether all tests passed */
138 return exit_status();