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>
18 struct io_listener *l;
24 static void finish_ok(struct io_conn *conn, struct data *d)
29 static struct io_plan *end(struct io_conn *conn, struct data *d)
32 /* Close on top of halfclose should work. */
34 return io_close(conn);
36 return io_halfclose(conn);
39 static struct io_plan *make_duplex(struct io_conn *conn, struct data *d)
42 /* Have duplex read the rest of the buffer. */
43 return io_duplex(conn,
44 io_read(conn, d->buf+1, sizeof(d->buf)-1, end, d),
45 io_write(conn, d->wbuf, sizeof(d->wbuf), end, d));
48 static struct io_plan *init_conn(struct io_conn *conn, struct data *d)
51 io_set_debug(conn, true);
56 io_close_listener(d->l);
58 memset(d->wbuf, 7, sizeof(d->wbuf));
59 io_set_finish(conn, finish_ok, d);
60 return io_read(conn, d->buf, 1, make_duplex, d);
63 static int make_listen_fd(const char *port, struct addrinfo **info)
66 struct addrinfo *addrinfo, hints;
68 memset(&hints, 0, sizeof(hints));
69 hints.ai_family = AF_UNSPEC;
70 hints.ai_socktype = SOCK_STREAM;
71 hints.ai_flags = AI_PASSIVE;
72 hints.ai_protocol = 0;
74 if (getaddrinfo(NULL, port, &hints, &addrinfo) != 0)
77 fd = socket(addrinfo->ai_family, addrinfo->ai_socktype,
78 addrinfo->ai_protocol);
82 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
83 if (bind(fd, addrinfo->ai_addr, addrinfo->ai_addrlen) != 0) {
87 if (listen(fd, 1) != 0) {
97 struct data *d = malloc(sizeof(*d));
98 struct addrinfo *addrinfo;
101 /* This is how many tests you plan to run */
104 fd = make_listen_fd(PORT, &addrinfo);
106 d->l = io_new_listener(NULL, fd, init_conn, d);
113 io_close_listener(d->l);
115 fd = socket(addrinfo->ai_family, addrinfo->ai_socktype,
116 addrinfo->ai_protocol);
119 if (connect(fd, addrinfo->ai_addr, addrinfo->ai_addrlen) != 0)
121 signal(SIGPIPE, SIG_IGN);
122 for (i = 0; i < strlen("hellothere"); i++) {
123 if (write(fd, "hellothere" + i, 1) != 1)
126 for (i = 0; i < 32; i++) {
127 if (read(fd, buf+i, 1) != 1)
131 freeaddrinfo(addrinfo);
134 freeaddrinfo(addrinfo);
135 ok1(io_loop(NULL, NULL) == NULL);
137 ok1(memcmp(d->buf, "hellothere", sizeof(d->buf)) == 0);
141 ok1(WIFEXITED(status));
142 ok1(WEXITSTATUS(status) == 0);
144 /* This exits depending on whether all tests passed */
145 return exit_status();