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>
16 struct io_listener *l;
18 struct io_conn *c1, *c2;
23 static void finish_ok(struct io_conn *conn, struct data *d)
28 static struct io_plan end(struct io_conn *conn, struct data *d)
34 static struct io_plan make_duplex(struct io_conn *conn, struct data *d)
36 /* Have duplex read the rest of the buffer. */
37 d->c2 = io_duplex(conn, io_read(d->buf+1, sizeof(d->buf)-1, end, d));
39 io_set_finish(d->c2, finish_ok, d);
41 return io_write(d->wbuf, sizeof(d->wbuf), end, d);
44 static void init_conn(int fd, struct data *d)
49 io_close_listener(d->l);
51 memset(d->wbuf, 7, sizeof(d->wbuf));
53 d->c1 = io_new_conn(fd, io_read(d->buf, 1, make_duplex, d));
54 io_set_finish(d->c1, finish_ok, d);
57 static int make_listen_fd(const char *port, struct addrinfo **info)
60 struct addrinfo *addrinfo, hints;
62 memset(&hints, 0, sizeof(hints));
63 hints.ai_family = AF_UNSPEC;
64 hints.ai_socktype = SOCK_STREAM;
65 hints.ai_flags = AI_PASSIVE;
66 hints.ai_protocol = 0;
68 if (getaddrinfo(NULL, port, &hints, &addrinfo) != 0)
71 fd = socket(addrinfo->ai_family, addrinfo->ai_socktype,
72 addrinfo->ai_protocol);
76 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
77 if (bind(fd, addrinfo->ai_addr, addrinfo->ai_addrlen) != 0) {
81 if (listen(fd, 1) != 0) {
91 struct data *d = malloc(sizeof(*d));
92 struct addrinfo *addrinfo;
95 /* This is how many tests you plan to run */
98 fd = make_listen_fd(PORT, &addrinfo);
100 d->l = io_new_listener(fd, init_conn, d);
107 io_close_listener(d->l);
109 fd = socket(addrinfo->ai_family, addrinfo->ai_socktype,
110 addrinfo->ai_protocol);
113 if (connect(fd, addrinfo->ai_addr, addrinfo->ai_addrlen) != 0)
115 signal(SIGPIPE, SIG_IGN);
116 for (i = 0; i < strlen("hellothere"); i++) {
117 if (write(fd, "hellothere" + i, 1) != 1)
120 for (i = 0; i < 32; i++) {
121 if (read(fd, buf+i, 1) != 1)
125 freeaddrinfo(addrinfo);
128 freeaddrinfo(addrinfo);
129 ok1(io_loop() == NULL);
131 ok1(memcmp(d->buf, "hellothere", sizeof(d->buf)) == 0);
135 ok1(WIFEXITED(status));
136 ok1(WEXITSTATUS(status) == 0);
138 /* This exits depending on whether all tests passed */
139 return exit_status();